terorama

Browser7 (Editor)

Dec 9th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.05 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('password');
  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.        
  103.            
  104.             $strz = htmlentities(file_get_contents($fn));
  105.  
  106.             $ctype='getfile';
  107.             $content='<pre>'.$strz.'</pre>';
  108.  
  109.             $u = explode('/',$fn);
  110.             $fn4=array_pop($u);
  111.  
  112.             echo json_encode(Array('ctype'=>$ctype, 'content'=>base64_encode($content), 'fname'=>$fn4, 'fpath'=>$fn));
  113.             break;
  114.  
  115.          case 'getdir' :
  116.  
  117.             ob_start();
  118.             showdir($_GET['fname']);
  119.             $ctype = 'getdir';
  120.             $content = ob_get_clean();
  121.  
  122.             echo json_encode(Array('ctype'=>$ctype, 'content'=>$content));
  123.             break;
  124.  
  125.         case 'getraw' :
  126.  
  127.            $fn= $_GET['fname'];
  128.            header('Content-type: text/plain');
  129.            echo file_get_contents($fn);
  130.            break;
  131.  
  132.         case 'savefile' :
  133.            
  134.            file_put_contents($_POST['fname'], $_POST['content']);
  135.            break;
  136.  
  137.         case 'newfile' :
  138.            if (file_exists($_GET['fname'])) break;
  139.            $f = fopen($_GET['fname'],'w');
  140.            fclose($f);
  141.       }
  142.  
  143.       exit();
  144.    }
  145.  
  146. ?>
  147.  
  148. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  149. <html xmlns="http://www.w3.org/1999/xhtml">
  150.  
  151. <head>
  152. <title> file browser </title>
  153.  
  154. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  155. <script type="text/javascript">
  156.  
  157. function decode_base64(s) {
  158.     var e={},i,k,v=[],r='',w=String.fromCharCode;
  159.     var n=[[65,91],[97,123],[48,58],[43,44],[47,48]];
  160.  
  161.     for(z in n){for(i=n[z][0];i<n[z][1];i++){v.push(w(i));}}
  162.     for(i=0;i<64;i++){e[v[i]]=i;}
  163.  
  164.     for(i=0;i<s.length;i+=72){
  165.     var b=0,c,x,l=0,o=s.substring(i,i+72);
  166.          for(x=0;x<o.length;x++){
  167.                 c=e[o.charAt(x)];b=(b<<6)+c;l+=6;
  168.                 while(l>=8){r+=w((b>>>(l-=8))%256);}
  169.          }
  170.     }
  171.     return r;
  172.     }
  173.  
  174.    function onSuccess(inf,block) {
  175.       if (inf.ctype=='getfile') {
  176.  
  177.          $('#messageload').html('');
  178.  
  179.          var tabs =  $('#filebrowse ul');
  180.          var l = tabs.children().length;
  181.          tabs.prepend($('<li/>').data('inf',l).data('filepath',inf.fpath).html(inf.fname)
  182.  
  183.      .append($('<span/>').text('r')
  184.  
  185.        .click ({file_path: inf.fpath}, function(ev) {
  186.  
  187.             open(ev.data.file_path.replace('<?php echo $_SERVER["DOCUMENT_ROOT"]  ?>',''));
  188.         })
  189.  
  190.      )
  191.  
  192.      .append($('<span/>').text('e')
  193.  
  194.         .click ({file_path: inf.fpath}, function(ev) {
  195.  
  196.              var ed = $('#filebrowse div.edd');
  197.  
  198.              if (ed.length==0) {
  199.                 ed = $('<div/>',{class:'edd'})
  200.                               .append($('<textarea/>',{style:'width:99%;height:500px'}))
  201.                               .append($('<a/>', {href:'#'}).text('save')
  202.                      )
  203.                     .appendTo($('#filebrowse'));
  204.              }
  205.  
  206.              $('#messageload').text('loading...please wait');
  207.  
  208.              ed.find('textarea').attr('disabled',true);
  209.              $.ajax( {
  210.                 type: 'GET',
  211.                 url: "<?php echo $_SERVER['PHP_SELF'];?>",
  212.                 data: {ajax:'', action: 'getraw', fname: $(this).parent().data('filepath') },
  213.                 success: function(inf) {
  214.  
  215.                    $('#messageload').text('loaded');
  216.                    ed.find('textarea').val(inf).removeAttr('disabled');
  217.  
  218.                    ed.find('a:contains("save")').unbind('click').click(
  219.  
  220.                             function() {
  221.                              $('#messageload').text('saving...please wait');
  222.  
  223.                              $.ajax ({
  224.                                 type: 'POST',
  225.  
  226.                                 url: "<?php echo $_SERVER['PHP_SELF'];?>?ajax&action=savefile",
  227.                                 data: {
  228.                                   fname: ev.data.file_path,    
  229.                                   content: $(this).parent().find('textarea').val()},
  230.  
  231.                                 success:  function() {
  232.                                    $('#messageload').text('saved');
  233.                                 },
  234.                                error: function() {
  235.                                    $('#messageload').text('error');
  236.                                }}
  237.                              )
  238.  
  239.                              return false;
  240.                            }
  241.                         )
  242.                 }
  243.              })
  244.              $('#filebrowse div[id!="messageload"]:visible').fadeOut('fast',
  245.  
  246.            function() {
  247.              ed.fadeIn('fast')});
  248.              
  249.            ev.stopPropagation();
  250.            return false;
  251.        }))
  252.  
  253.  
  254.      .append($('<span/>').text('x')
  255.  
  256.              .click ( function(ev) {
  257.                 var liinf = $(this).parent().data('inf');
  258.                 $('#filebrowse div').filter(
  259.                   function(index) {
  260.                      return (liinf===$(this).data('inf'));
  261.                   }
  262.                ).remove();
  263.                
  264.                var _selfp=$(this).parent().get(0);
  265.                $(this).parent().parent().children().filter(
  266.                      function(index) {
  267.                        return (this!==_selfp)
  268.                     }
  269.                  )
  270.                 .first().trigger('click');
  271.  
  272.                $(this).parent().remove();
  273.              
  274.              }
  275.               ))
  276.              .click (  function(ev) {
  277.              
  278.             var liinf = $(this).data('inf');
  279.  
  280.             $('#filebrowse div[id!="messageload"]:visible').fadeOut('fast');
  281.             $('#filebrowse div').filter(
  282.                function(index) {
  283.  
  284.                    return (liinf===$(this).data('inf'));
  285.                }
  286.             ).fadeIn('fast');
  287.          }));
  288.  
  289.          $('#filebrowse').append($('<div/>').data('inf',l));
  290.  
  291.          $("#filebrowse div[id!='messageload']").fadeOut('fast');
  292.          $('#filebrowse div:last').html(decode_base64(inf.content)).fadeIn('fast');
  293.        
  294.          
  295.       }
  296.  
  297.       if (inf.ctype=='getdir') {
  298.          $(block).html(inf.content);
  299.       }
  300.  
  301.       init();
  302.       busy=false;
  303.    }
  304.  
  305.    function ajax_get(action, fname, block) {
  306.  
  307.       //if (window.busy)
  308.       //   return;
  309.  
  310.       busy=true;
  311.  
  312.       if (!block)
  313.          block = $(document.body);
  314.  
  315.       switch (action) {
  316.  
  317.          case 'getfile':
  318.             var zht = $('#messageload').html();
  319.             $('#messageload').html(zht+fname+' loading, please wait...<br/>');
  320.             break;
  321.          
  322.          case 'getdir':
  323.             block.html('Loading...');
  324.             break;
  325.       }
  326.       $.ajax({
  327.           type: 'GET',
  328.           url:"<?=$_SERVER["PHP_SELF"]?>",
  329.           dataType: 'json',
  330.           data: {
  331.              ajax: '',
  332.              action: action,
  333.              fname: fname
  334.           },
  335.           success: function(inf) {
  336.              
  337.              onSuccess(inf, block);
  338.           }
  339.          
  340.       })
  341.    }
  342.  
  343.  
  344.    $(document).ready(init);
  345.  
  346.    function init() {
  347.       $('#dir_structure li.fl').children('a').unbind('click').click (
  348.        
  349.         function(ev) {
  350.            var fname = $(this).attr('href');
  351.            ajax_get('getfile', fname);
  352.  
  353.            
  354.            return false;
  355.            
  356.         } );
  357.  
  358.    
  359.  
  360.      $('#dir_structure li:not(.fl)').children('a').unbind('click').click(
  361.        
  362.         function(ev) {
  363.            var el = $('<ul/>');
  364.            $(this).parent().append(el);
  365.            var fname = $(this).attr('href');
  366.            ajax_get('getdir', fname, el);
  367.  
  368.            return false;
  369.         }
  370.      )
  371.         .unbind('contextmenu').bind('contextmenu', function(ev) {
  372.  
  373.           dir = $(this).attr('href');
  374.           console.log(dir);
  375.  
  376.           el = $('#contextmenu');
  377.           if (el.length==0) {
  378.              el = $('<div/>',{id:'contextmenu'}).prependTo($('body'))
  379.                .css({'position':'absolute','padding':20, 'border': 'solid 1px #444'} )
  380.                .css('background','white').css('display','none')
  381.                .append($('<a/>',{'href':'#'}).text('add new file'))
  382.  
  383.                .bind('mouseleave',function() {$(this).fadeOut('fast')});
  384.           }
  385.  
  386.           $('a',el).unbind('click').click({dir4:dir}, function(ev) {
  387.  
  388.                       if (elu=prompt('inp file name',
  389.                       ev.data.dir4+'/test_'+String(Math.ceil(Math.random()*8000))+'.txt')) {
  390.  
  391.                          $('#messageload').html(elu+' creating, please wait...');
  392.                          $.ajax({
  393.                             type:'GET',
  394.                             url: "<?php echo $_SERVER['PHP_SELF'];?>",
  395.                             data: { ajax:'',action: 'newfile', fname: elu},
  396.                             success: function() {
  397.                               $('#messageload').html(elu+' created');
  398.  
  399.                               ajax_get('getfile', elu);
  400.                             }
  401.                          })
  402.                       }
  403.  
  404.                       return false;
  405.                      
  406.                   })
  407.  
  408.           el.css('left',ev.pageX-10);
  409.           el.css('top', ev.pageY-el.height()/2);
  410.  
  411.           el.fadeIn('fast');
  412.  
  413.            return false;
  414.  
  415.         });
  416.  
  417.  
  418.  
  419.  
  420.         $("#dir_structure li a:contains('..up')").unbind('click').click (
  421.  
  422.           function(ev) {
  423.  
  424.            var el = $('#dir_structure');
  425.            
  426.            var fname = $(this).attr('href');
  427.            ajax_get('getdir', fname, el);
  428.  
  429.            return false;
  430.           }
  431.       );
  432.  
  433.       $("#dir_structure li ul li a:contains('..up'), a:contains('close..')")
  434.  
  435.        .html('close..')
  436.        .unbind('click').click (
  437.  
  438.           function(ev) {
  439.  
  440.              var _el = $(this);
  441.              $.when ($(this).parent().parent().slideUp('slow')).done(
  442.  
  443.                   function() {
  444.                      _el.parent().parent().remove();
  445.                   }
  446.              );
  447.  
  448.              return false;
  449.           }
  450.       );
  451.  
  452.    }
  453. </script>
  454.  
  455. <style type="text/css">
  456.    * {
  457.       margin:0;
  458.       padding:0;
  459.    }
  460.  
  461.    body {
  462.       font-family: verdana, sans-serif;
  463.       font-size: 1.2em;
  464.    }
  465.  
  466.    li {
  467.       list-style-type: none;
  468.    }
  469.  
  470.    a {
  471.       text-decoration: none;
  472.       color: #400;
  473.    }
  474.  
  475.    a:hover {
  476.       text-decoration: underline;
  477.    }
  478.  
  479.    #dir_structure {
  480.       padding: 10px;
  481.       border: solid 1px #eee;
  482.       background-color: #ffe;
  483.       margin:50px 0 0 20px;
  484.       float:left;
  485.       width:300px;
  486.       height:500px;
  487.       overflow: auto;
  488.    }
  489.  
  490.    #dir_structure li ul {
  491.       margin-left: 10px;
  492.       margin-bottom: 20px;
  493.    }
  494.    
  495.    .fl a{
  496.       color: #900;
  497.    }
  498.  
  499.    #filebrowse {
  500.      
  501.       margin:50px 0 0 350px;
  502.       background-color: #fffff0;
  503.       font-family: "courier new", monospace;
  504.       font-size: 12px;
  505.       padding: 5px;
  506.       border: solid 1px #aaa;
  507.       word-wrap:break-word;
  508.       overflow: auto;
  509.    }
  510.  
  511.    #filebrowse ul {
  512.  
  513.       margin-bottom:10px;
  514.       float:left;
  515.       width: 100%;
  516.    }
  517.  
  518.    #filebrowse ul li {
  519.  
  520.       float: left;
  521.       cursor:pointer;
  522.       margin: 0 5px 5px 0;
  523.       border: solid 1px #888;
  524.       border-radius: 7px;
  525.       padding: 4px 8px;
  526.  
  527.    }
  528.  
  529.    #filebrowse ul li:hover {
  530.      
  531.       background: #666;
  532.       color: white;
  533.    }
  534.  
  535.    #filebrowse ul li span {
  536.       padding-left:10px;
  537.       padding-right:10px;
  538.    }
  539.  
  540.    #filebrowse ul li span:hover {
  541.       color: red;
  542.    }
  543.  
  544.    #filebrowse div {
  545.       display: none;
  546.       float: left;
  547.       width: 100%;
  548.    }
  549.  
  550.    #filebrowse #messageload {
  551.       display: block;
  552.    }
  553.  
  554.    .edd {
  555.       position: relative;
  556.    }
  557.  
  558.    .edd a {
  559.       position: absolute;
  560.       display: block;
  561.       padding: 3px;
  562.       background: #444;
  563.       color: white;
  564.       top: 0;
  565.    }
  566.    
  567. </style>
  568.  
  569. </head>
  570. <body>
  571. <h3>browser editor (corrected)</h3>
  572. <ul id="dir_structure">
  573. <?php
  574.  
  575.  
  576.    showdir($dir);
  577.  
  578. ?>
  579. </ul>
  580.  
  581. <div id="filebrowse">
  582. <div id="messageload"></div>
  583. <ul></ul>
  584.  
  585. </div>
  586.  
  587. </body>
  588. </html>
Add Comment
Please, Sign In to add comment