terorama

PHP / Browser

Mar 21st, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 9.42 KB | None | 0 0
  1. <?php
  2.  
  3.    $dir = dirname(__FILE__);
  4. //--------------------------------------
  5. function srt($a, $b) {
  6.  
  7.         $a = $a['path'];
  8.         $b = $b['path'];
  9.  
  10.         if (is_dir($a)) {
  11.         if (is_dir($b)) {
  12.            return strcasecmp($a,$b);
  13.         }
  14.         else return -1;
  15.      } else {
  16.        if (is_dir($b))
  17.           return 1;
  18.        else
  19.           return strcasecmp($a,$b);}
  20. }
  21. //--------------------------------------
  22. function out($z) {
  23.  
  24.   $z1 = ($z['name']=='..') ? '..up' : $z['name'];
  25.  
  26.    echo "<li class=\"".(is_dir($z['path'])?'':'fl')."\"><a href=\"{$z['path']}\">$z1</a></li>";
  27. }
  28. //--------------------------------------
  29.  
  30. function showdir($dirr) {
  31.  
  32.  
  33.     $z = opendir($dirr);
  34.     $files = array();
  35.     while ($f= readdir($z)) {
  36.  
  37.         $el = array();
  38.         $el['name'] = $f;
  39.         $el['path'] = $dirr.'/'.$f;
  40.  
  41.         $files[] = $el;
  42.  
  43.     }
  44.     closedir($z);
  45.  
  46.     usort($files, 'srt');
  47.  
  48.     array_splice($files,0, 1);
  49.    
  50.  
  51.     array_walk($files,'out');
  52. }
  53. //--------------------------------------
  54.  
  55.    session_start();
  56.  
  57.    if (!isset($_SESSION['logged8'])) {
  58.       if (isset($_POST['pass'])) {
  59.          //$s = file_get_contents('hash.txt');
  60.          $s = md5('pass');
  61.  
  62.          if (md5($_POST['pass'])==$s) {
  63.             $_SESSION['logged8']=true;
  64.  
  65.             echo 'successfully logged in<br/>';
  66.             echo '<a href="'.$_SERVER['PHP_SELF'].'" title="back" >back</a>';
  67.  
  68.             exit();
  69.          } else {
  70.  
  71.             echo 'incorrect password<br/>';
  72.             echo '<a href="'.$_SERVER['PHP_SELF'].'" title="back">back</a>';
  73.             exit();
  74.  
  75.          }
  76.       } else {
  77.        
  78.         $ss = <<<ZZ
  79.            <form name="authform" action="{$_SERVER['PHP_SELF']}"
  80.              method="post" enctype="application/x-www-form-urlencoded">
  81.  
  82.            <p>input password <input type="text" name="pass" /> </p>
  83.            <input type="submit" value="send" />
  84.  
  85.            </form>  
  86.  
  87. ZZ;
  88.  
  89.          echo $ss;
  90.          exit();
  91.       }
  92.          
  93.    }
  94.    
  95.    if (isset($_GET['ajax'])) {
  96.       switch ($_GET['action']) {
  97.  
  98.          case 'getfile' :
  99.          
  100.             $fn= $_GET['fname'];
  101.            
  102.             /*$fh = fopen ($fn, 'r');
  103.             $i=1;
  104.             $strz='';
  105.             while ($row= fgets($fh)) {
  106.                $row=htmlentities($row);
  107.                $strz.= $row;
  108.                $i++;
  109.             }
  110.             fclose($fh);*/
  111.            
  112.             $strz = htmlentities(file_get_contents($fn));
  113.  
  114.             $ctype='getfile';
  115.             $content='<pre>'.$strz.'</pre>';
  116.  
  117.             $u = explode('/',$fn);
  118.             $fn=array_pop($u);
  119.  
  120.             echo json_encode(Array('ctype'=>$ctype, 'content'=>base64_encode($content), 'fname'=>$fn));
  121.             break;
  122.  
  123.          case 'getdir' :
  124.  
  125.             ob_start();
  126.             showdir($_GET['fname']);
  127.             $ctype = 'getdir';
  128.             $content = ob_get_clean();
  129.  
  130.             echo json_encode(Array('ctype'=>$ctype, 'content'=>$content));
  131.             break;
  132.       }
  133.  
  134.       exit();
  135.    }
  136.  
  137. ?>
  138.  
  139. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  140. <html xmlns="http://www.w3.org/1999/xhtml">
  141.  
  142. <head>
  143. <title> file browser </title>
  144.  
  145. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  146. <script type="text/javascript">
  147.  
  148. function decode_base64(s) {
  149.     var e={},i,k,v=[],r='',w=String.fromCharCode;
  150.     var n=[[65,91],[97,123],[48,58],[43,44],[47,48]];
  151.  
  152.     for(z in n){for(i=n[z][0];i<n[z][1];i++){v.push(w(i));}}
  153.     for(i=0;i<64;i++){e[v[i]]=i;}
  154.  
  155.     for(i=0;i<s.length;i+=72){
  156.     var b=0,c,x,l=0,o=s.substring(i,i+72);
  157.          for(x=0;x<o.length;x++){
  158.                 c=e[o.charAt(x)];b=(b<<6)+c;l+=6;
  159.                 while(l>=8){r+=w((b>>>(l-=8))%256);}
  160.          }
  161.     }
  162.     return r;
  163.     }
  164.  
  165.    function onSuccess(inf,block) {
  166.       if (inf.ctype=='getfile') {
  167.  
  168.          $('#messageload').html('');
  169.  
  170.          var tabs =  $('#filebrowse ul');
  171.          var l = tabs.children().length;
  172.          tabs.prepend($('<li/>').data('inf',l).html(inf.fname).append($('<span/>').text('x')
  173.  
  174.              .click ( function(ev) {
  175.                 var liinf = $(this).parent().data('inf');
  176.                 $('#filebrowse div').filter(
  177.                   function(index) {
  178.                      return (liinf===$(this).data('inf'));
  179.                   }
  180.                ).remove();
  181.                
  182.                var _selfp=$(this).parent().get(0);
  183.                $(this).parent().parent().children().filter(
  184.                      function(index) {
  185.                        return (this!==_selfp)
  186.                     }
  187.                  )
  188.                 .first().trigger('click');
  189.  
  190.                $(this).parent().remove();
  191.              
  192.              }
  193.               ))
  194.              .click (  function(ev) {
  195.              
  196.             var liinf = $(this).data('inf');
  197.  
  198.             $('#filebrowse div[id!="messageload"]:visible').fadeOut('fast');
  199.             $('#filebrowse div').filter(
  200.                function(index) {
  201.  
  202.                    return (liinf===$(this).data('inf'));
  203.                }
  204.             ).fadeIn('fast');
  205.          }));
  206.  
  207.          $('#filebrowse').append($('<div/>').data('inf',l));
  208.  
  209.          $("#filebrowse div[id!='messageload']").fadeOut('fast');
  210.          $('#filebrowse div:last').html(decode_base64(inf.content)).fadeIn('fast');
  211.        
  212.          
  213.       }
  214.  
  215.       if (inf.ctype=='getdir') {
  216.          $(block).html(inf.content);
  217.       }
  218.  
  219.       init();
  220.       busy=false;
  221.    }
  222.  
  223.    function ajax_get(action, fname, block) {
  224.  
  225.       //if (window.busy)
  226.       //   return;
  227.  
  228.       busy=true;
  229.  
  230.       if (!block)
  231.          block = $(document.body);
  232.  
  233.       switch (action) {
  234.  
  235.          case 'getfile':
  236.             var zht = $('#messageload').html();
  237.             $('#messageload').html(zht+fname+' loading, please wait...<br/>');
  238.             break;
  239.          
  240.          case 'getdir':
  241.             block.html('Loading...');
  242.             break;
  243.       }
  244.       $.ajax({
  245.           type: 'GET',
  246.           url:"<?=$_SERVER["PHP_SELF"]?>",
  247.           dataType: 'json',
  248.           data: {
  249.              ajax: '',
  250.              action: action,
  251.              fname: fname
  252.           },
  253.           success: function(inf) {
  254.              
  255.              onSuccess(inf, block);
  256.           }
  257.          
  258.       })
  259.    }
  260.  
  261.  
  262.    $(document).ready(init);
  263.  
  264.    function init() {
  265.       $('#dir_structure li.fl').children('a').unbind('click').click (
  266.        
  267.         function(ev) {
  268.            var fname = $(this).attr('href');
  269.            ajax_get('getfile', fname);
  270.  
  271.            
  272.            return false;
  273.            
  274.         } );
  275.  
  276.    
  277.  
  278.      $('#dir_structure li:not(.fl)').children('a').unbind('click').click(
  279.        
  280.         function(ev) {
  281.            var el = $('<ul/>');
  282.            $(this).parent().append(el);
  283.            var fname = $(this).attr('href');
  284.            ajax_get('getdir', fname, el);
  285.  
  286.            return false;
  287.         }
  288.      );
  289.  
  290.         $("#dir_structure li a:contains('..up')").unbind('click').click (
  291.  
  292.           function(ev) {
  293.  
  294.            var el = $('#dir_structure');
  295.            
  296.            var fname = $(this).attr('href');
  297.            ajax_get('getdir', fname, el);
  298.  
  299.            return false;
  300.           }
  301.       );
  302.  
  303.       $("#dir_structure li ul li a:contains('..up'), a:contains('close..')")
  304.  
  305.        .html('close..')
  306.        .unbind('click').click (
  307.  
  308.           function(ev) {
  309.  
  310.              var _el = $(this);
  311.              $.when ($(this).parent().parent().slideUp('slow')).done(
  312.  
  313.                   function() {
  314.                      _el.parent().parent().remove();
  315.                   }
  316.              );
  317.  
  318.              return false;
  319.           }
  320.       );
  321.  
  322.    }
  323. </script>
  324.  
  325. <style type="text/css">
  326.    * {
  327.       margin:0;
  328.       padding:0;
  329.    }
  330.  
  331.    body {
  332.       font-family: verdana, sans-serif;
  333.       font-size: 1.2em;
  334.    }
  335.  
  336.    li {
  337.       list-style-type: none;
  338.    }
  339.  
  340.    a {
  341.       text-decoration: none;
  342.       color: #400;
  343.    }
  344.  
  345.    a:hover {
  346.       text-decoration: underline;
  347.    }
  348.  
  349.    #dir_structure {
  350.       padding: 10px;
  351.       border: solid 1px #eee;
  352.       background-color: #ffe;
  353.       margin:50px 0 0 20px;
  354.       float:left;
  355.       width:300px;
  356.    }
  357.  
  358.    #dir_structure li ul {
  359.       margin-left: 10px;
  360.       margin-bottom: 20px;
  361.    }
  362.    
  363.    .fl a{
  364.       color: #900;
  365.    }
  366.  
  367.    #filebrowse {
  368.      
  369.       margin:50px 0 0 350px;
  370.       background-color: #fffff0;
  371.       font-family: "courier new", monospace;
  372.       font-size: 12px;
  373.       padding: 5px;
  374.       border: solid 1px #aaa;
  375.       word-wrap:break-word;
  376.    }
  377.  
  378.    #filebrowse ul {
  379.  
  380.       margin-bottom:10px;
  381.       float:left;
  382.       width: 100%;
  383.    }
  384.  
  385.    #filebrowse ul li {
  386.  
  387.       float: left;
  388.       cursor:pointer;
  389.       margin: 0 5px 5px 0;
  390.       border: solid 1px #888;
  391.       border-radius: 7px;
  392.       padding: 4px 8px;
  393.  
  394.    }
  395.  
  396.    #filebrowse ul li:hover {
  397.      
  398.       background: #666;
  399.       color: white;
  400.    }
  401.  
  402.    #filebrowse ul li span {
  403.       padding-left:10px;
  404.       padding-right:10px;
  405.    }
  406.  
  407.    #filebrowse ul li span:hover {
  408.       color: red;
  409.    }
  410.  
  411.    #filebrowse div {
  412.       display: none;
  413.    }
  414.  
  415.    #filebrowse #messageload {
  416.       display: block;
  417.    }
  418.    
  419. </style>
  420.  
  421. </head>
  422. <body>
  423. <ul id="dir_structure">
  424. <?php
  425.  
  426.  
  427.    showdir($dir);
  428.  
  429. ?>
  430. </ul>
  431.  
  432. <div id="filebrowse">
  433. <div id="messageload"></div>
  434. <ul></ul>
  435.  
  436. </div>
  437.  
  438. </body>
  439. </html>
Add Comment
Please, Sign In to add comment