PHP Classes

File: as_admintool_filemgr.php

Recommend this page to a friend!
  Classes of Alexander Selifonov   site administrator tool set   as_admintool_filemgr.php   Download  
File: as_admintool_filemgr.php
Role: Auxiliary script
Content type: text/plain
Description: Plugin module: file manager
Class: site administrator tool set
Web interface to manage site resources
Author: By
Last change: some changes
Date: 14 years ago
Size: 16,309 bytes
 

Contents

Class file image Download
<? /** * @package as_admintool * @desc as_admintool_filemgr.php - File manager plugin (upload/download/delete) * @author Alexander Selifonov <as-works@narod.ru> * @copyright Alexander Selifonov 2007 * @link http://as-works.narod.ru/en/php/ * @version 1.001.003 * modified 28.02.2008 (dd.mm.yyyy) * Read "as_admintool.htm" for detailed instructions */ define('ASADM_FILEMGR','filemgr'); // unique string ID for this plugin module define('ASADM_UPLOADSIZE','1048576'); // upload max file size define('ASADM_UPLOADFILES',2); // max files to upload per one POST # Register your plugin ! CAsAdminTool::RegisterPlugin(ASADM_FILEMGR,'AsAdm_filemgr_Form','AsAdm_filemgr_Exec','FileMgr_StdPost'); // interface localization strings if(empty($as_iface['folder'])) $as_iface['folder'] ='Folder'; if(empty($as_iface['filename'])) $as_iface['filename'] ='File name'; if(empty($as_iface['filesize'])) $as_iface['filesize'] ='Size'; if(empty($as_iface['filetime'])) $as_iface['filetime'] ='Date/time'; if(empty($as_iface['server_reply'])) $as_iface['server_reply'] ='Server reply'; if(empty($as_iface['upload'])) $as_iface['upload'] ='Upload'; // if(!isset($as_iface['chdir'])) $as_iface['chdir'] =''; // empty string - no button to chdir 'Change directory'; if(empty($as_iface['delete_files'])) $as_iface['delete_files'] ='Delete selected files'; if(empty($as_iface['confirm_delete'])) $as_iface['confirm_delete'] ='Selected files will be deleted. Confirm ?'; if(empty($as_iface['upload_files'])) $as_iface['upload_files'] ='Upload file(s)'; if(empty($as_iface['download_files'])) $as_iface['download_files'] ='Download file(s)'; if(empty($as_iface['deleting_error'])) $as_iface['deleting_error'] ='File deleting ERROR !'; if(empty($as_iface['upload_error'])) $as_iface['upload_error'] ='upload ERROR !'; if(empty($as_iface['file_deleted'])) $as_iface['file_deleted'] ='deleted'; if(empty($as_iface['file_uploaded'])) $as_iface['file_uploaded'] ='uploaded'; if(empty($as_iface['file_protected'])) $as_iface['file_protected'] ='File is protected !'; if(empty($as_iface['no_selected_files'])) $as_iface['no_selected_files'] ='No selected files for operation'; $asdt_fmgr_folder = false; $asdt_fmgr_protect = array(); # protected filenames (can't be deleted or rewritten by upload /** * AsAdm_filemgr_Form - function for drawing client interface page (FORM) * This function will be called when CAsAdmin::Draw() is drawing all pages. * param1-3 are the parameters You've passed to CAsAdminTool::AddPage() * @param array $pginfo : [0] - pageid, [1],[2] - parent table size (width,height) * @param array $param1 pass folder list to work with: [i][0] - real folder, [i][1] - visible title * @return none */ function AsAdm_filemgr_Form($pginfo,$param1=false, $param2=false, $param3=false) { global $as_dbengine, $as_iface, $as_cssclass, $asdt_result,$asdt_fmgr_folder; $pageid=isset($pginfo[0])? $pginfo[0]: 0; $lwidth = isset($pginfo[1])? $pginfo[1]: 800; $lheight = isset($pginfo[2])? $pginfo[2]: 600; $r_width = $lwidth-20; $up_height = ASADM_UPLOADFILES*24 + (ASADM_UPLOADFILES ? 60:0); // height for upload form $r_height = max(80, $lheight-195-$up_height); # calculated height of filelist area $self = $_SERVER['PHP_SELF']; static $js_drawn = false; if(!$js_drawn) { #<3> draw only once !!! $js_drawn = true; ?> <script language='javascript'> var ajax_filemgr_busy = false; var fmgr_folders = []; var delete_confirm = "<?=$as_iface['confirm_delete']?>"; var fmgr_result = []; <? $result= empty($asdt_result[$pageid]) ? '' : $asdt_result[$pageid]; echo "fmgr_result[$pageid] = '$result';\n"; ?> function FileMgrChkAll(chkobj,pageid) { var fm = asGetObj('asadt_filemgr_'+pageid); var chkt = chkobj.checked; var els = fm.elements; for(i=0; i<els.length; i++) { if(els[i].type=='checkbox' && els[i].name.substring(0,3)=='fl_') els[i].checked = chkt; } } function RefreshFileList(pageid,spl) { var sbody = "<tr><td class='<?=$as_cssclass['tdhead']?>' width=220><?=$as_iface['filename']?></td>"; sbody += "<td class='<?=$as_cssclass['tdhead']?>' width=120><?=$as_iface['filesize']?></td><td class='<?=$as_cssclass['tdhead']?>' width=120><?=$as_iface['filetime']?></td>"; sbody += "<td class='<?=$as_cssclass['tdhead']?>'><input type=checkbox name=chkall onClick='FileMgrChkAll(this,<?=$pageid?>)' value='1'></td>"; asGetObj('result_'+pageid).innerHTML = spl[1]; // passed action log sclass = '<?=$as_cssclass['troweven']?>'; for(ifl=2;ifl<spl.length;ifl++) { finfo = spl[ifl].split('^'); // 0-filename, 1-size, 2-modif-date/time if(finfo.length>2) { sclass = (sclass=='<?=$as_cssclass['troweven']?>')? '<?=$as_cssclass['trowodd']?>' : '<?=$as_cssclass['troweven']?>'; sbody +="<tr class='"+sclass+"'><td nowrap><b>"+finfo[0]+'</b></td><td nowrap align=right>'+finfo[1]+'</td>'; sbody +='<td nowrap align=center>'+finfo[2]+"</td><input type=hidden name='name_"+ifl+"' value='"+finfo[0]+"'><td><input name='fl_"+ifl+"' type='checkbox' value='1'></td></tr>"; } } asGetObj('filelist_'+pageid).innerHTML = "<table border=0 cellspacing=1 cellpadding=0>"+sbody+"</table>"; } function filemgrSetFolder(pageid) { if (ajax_filemgr_busy) return; var fm = asGetObj('asadt_filemgr_'+pageid); isel = fm.folder.selectedIndex; foldname = fm.folder.options[isel].value; var xmlreq = NewXMLHttpRequest(); if(!xmlreq) return false; ajax_filemgr_busy = true; asGetObj('result_'+pageid).innerHTML = '<?=$as_iface['msg_waiting']?>'; xmlreq.onreadystatechange= function() { //<3> if (xmlreq.readyState == 4) { //<3A> var resp = xmlreq.responseText; var spl = xmlreq.responseText.split("{|}"); delete xmlreq; asGetObj('result_'+pageid).innerHTML = ''; ajax_filemgr_busy = false; if(spl.length < 2 || spl[0].length>10) { asGetObj('result_'+pageid).innerHTML ='<?=$as_iface['msg_wrongreply']?> '+"<hr>"+resp; } else { RefreshFileList(pageid, spl); asGetObj('result_'+pageid).innerHTML = (fmgr_result[pageid]==undefined ? '': fmgr_result[pageid]); fmgr_result[pageid] = ''; } //<4> } //<3A> } //<3> xmlreq.open('POST','<?=$self?>',true); xmlreq.setRequestHeader("Content-Type", postcont); params = 'adm_action_type=<?=ASADM_FILEMGR?>&pageid=' + pageid + '&action=chdir&folder='+fm.folder.options[isel].value; xmlreq.send(params); return false; } function AsAdm_FilemgrDelete(pageid) { if (ajax_filemgr_busy) return; if(!confirm(delete_confirm)) return false; var fm = asGetObj('asadt_filemgr_'+pageid); var xmlreq = NewXMLHttpRequest(); if(!xmlreq) return false; ajax_filemgr_busy = true; asGetObj('result_'+pageid).innerHTML = '<?=$as_iface['msg_waiting']?>'; xmlreq.onreadystatechange= function() { //<3> if (xmlreq.readyState == 4) { //<3A> var spl = xmlreq.responseText.split("{|}"); delete xmlreq; ajax_filemgr_busy = false; if(spl.length < 2) { asGetObj('result_'+pageid).innerHTML ='<?=$as_iface['msg_wrongreply']?> '+spl[0]; } else { RefreshFileList(pageid,spl); } //<4> } //<3A> } //<3> xmlreq.open('POST','<?=$self?>',true); xmlreq.setRequestHeader("Content-Type", postcont); params = 'adm_action_type=<?=ASADM_FILEMGR?>&pageid=' + pageid + '&action=delete&'+ComputeParamString('asadt_filemgr_'+pageid); xmlreq.send(params); return false; } function AsAdm_DownloadFile(pageid) { var fm = asGetObj('asadt_filemgr_'+pageid); var els = fm.elements; for(i=0; i<els.length; i++) { if(els[i].type=='checkbox' && els[i].name.substring(0,3)=='fl_' && els[i].checked) { // create and open url for downloading file var fieldname = 'name_' + els[i].name.substring(3); eval('fl_name=fm.'+fieldname+'.value'); fullname = fm.folder.options[fm.folder.selectedIndex].value + encodeURIComponent(fl_name); //alert(fullname); var wnd=window.open('<?=$self?>?adm_action_type=filemgr&asadm_download='+fullname, "_blank","height=200,width=600,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0,top=40,left=40"); } } return false; } </script> <? } ?> <center> <table id='asadt_tbl_<?=$pageid?>'> <tr><form name='asadt_filemgr_<?=$pageid?>'> <input type='hidden' name='your_param1' value='<?=$param1?>'> <? if(is_array($param1)) { echo "<tr><td><b>{$as_iface['folder']}</b> <SELECT name='folder' onChange='filemgrSetFolder($pageid)'>"; for($kf=0;$kf<count($param1);$kf++) { $sdir = $stitle = $param1[$kf]; if(is_array($param1[$kf])) { $sdir = $param1[$kf][0]; $stitle = empty($param1[$kf][1])? $sdir : $param1[$kf][1]; } $sel = ($asdt_fmgr_folder===$sdir ? 'selected':''); echo "<OPTION value='$sdir' $sel>$stitle</OPTION>\n"; } echo "</SELECT>\n"; // if(!empty($as_iface['chdir'])) echo "&nbsp; <button name='chdir{$pageid}' class='{$as_cssclass['button']}' onClick='filemgrSetFolder($pageid)' >{$as_iface['chdir']}</button></td></tr>\n"; } $result = empty($asdt_result[$pageid])? '&nbsp;' : $asdt_result[$pageid]; ?> <tr><td><div id='filelist_<?=$pageid?>' class='<?=$as_cssclass['resultarea']?>' style='overflow:auto; width:<?=$r_width?>px; height:<?=$r_height?>'>files here...<div></td></tr> <tr><td> <? if(!empty($as_iface['download_files'])) { echo "<button name='download' class='button' onClick='AsAdm_DownloadFile($pageid)'>{$as_iface['download_files']}</button> &nbsp;"; } ?> <button class='button' name='fdelete' onClick='AsAdm_FilemgrDelete(<?=$pageid?>)'><?=$as_iface['delete_files']?></button> &nbsp; </td></tr></form></table> <? if(ASADM_UPLOADFILES >0) { ?> <hr><div id='asadt_upload_<?=$pageid?>' class='<?=$as_cssclass['resultarea']?>' style='overflow:auto; height:<?=$up_height?>px; width:<?=$r_width?>px; text-align:left'> <script language="javascript"> function FileMgrUpload(pageid) { var fmsrc = asGetObj('asadt_filemgr_'+pageid); var fmdest = asGetObj('asadt_upl_'+pageid); fmdest.folder.value = fmsrc.folder.options[fmsrc.folder.selectedIndex].value; // alert(fmdest.folder.value); return false; // debug return true; } </script> <table> <!-- Form for uploading files to server... --> <tr><FORM name='asadt_upl_<?=$pageid?>' method='POST' action='<?=$self?>' ENCTYPE="multipart/form-data" > <input type='hidden' name='adm_action_type' value='<?=ASADM_FILEMGR?>' /> <input type='hidden' name='filemgr_action' value='upload' /> <input type='hidden' name='pageid' value='<?=$pageid?>' /> <input type='hidden' name='as_stdpost' value='1' /> <input type='hidden' name='folder' value='' /> <input type='hidden' name='MAX_FILE_SIZE' value='<?=ASADM_UPLOADSIZE?>' /> <tr><td colspan='2'><b><?=$as_iface['upload_files']?> ...</b></td></tr> <? for($kup=1; $kup<=ASADM_UPLOADFILES; $kup++) { echo "<tr><td width=20><b>$kup</b>:</td><td><input type='FILE' class='{$as_cssclass['textfield']}' name='file{$kup}' style='width:320' /></td></tr>\n"; } ?> </tr> <tr><td>&nbsp;</td><td><input type='SUBMIT' name='submit' class='<?=$as_cssclass['button']?>' onClick='return FileMgrUpload(<?=$pageid?>)' value='<?=$as_iface['upload']?>' /></td></tr> </form></table></div></center> <? } ?> <div align=center> <table width='98%'> <tr><td><b><?=$as_iface['server_reply']?></b></td></tr> <tr><td><div id='result_<?=$pageid?>' class='<?=$as_cssclass['resultarea']?>' style='overflow:auto; height:80px; width:<?=$r_width?>px;'><?=$result?></div></td></tr> </tr> </table> </div> <? if(is_array($param1)) echo "<script language='Javascript'>filemgrSetFolder($pageid);</script>\n"; } /** * AsAdm_Filemgr_Exec - function that executes action on server and returns result string. * @param array $parms, decoded data passes in POST * @return 'delimited' result. I use a string '{|}' as a delimiter. */ function AsAdm_Filemgr_Exec($parms='') { global $as_dbengine, $as_iface, $as_admt_bckpfolder, $asdt_result; if(!empty($_GET['asadm_download'])) { // download file cmd $flname = $_GET['asadm_download']; $splt = split("[/\\]",$flname); $justfname = $splt[count($splt)-1]; $fh = fopen($flname,'r'); $fsize = filesize($flname); Header('Pragma: no-cache'); // avoid cached versions Header("Pragma: public"); Header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); Header('Content-Type: application/octet-stream'); // any binary file Header("Content-Length: $fsize"); Header("Content-Disposition: attachment; filename=\"$justfname\""); if($fh>0) { echo fread($fh,$fsize); fclose($fh); } exit; } $pageid = isset($parms['pageid'])? $parms['pageid'] : '1'; $action = isset($parms['action'])? $parms['action'] : 'chdir'; switch($action) { case 'chdir': # return file list for $folder $ret = "$pageid{|}".FlMgr_FileList($parms); break; case 'delete': #delete some files... $parms["file_<filename>"] = 1 $ret = "$pageid{|}".FlMgr_DeleteFiles($parms); } return $ret; } function FlMgr_FileList($parms) { global $asdt_result; $ret = ''; $folder = isset($parms['folder'])? $parms['folder'] : './'; $tflist = array(); if (($handle = @opendir($folder))) { while (($file = readdir($handle))) { if (is_file($folder.$file)) $tflist[] = $file; } closedir($handle); } natsort($tflist); // sort($tflist, SORT_STRING); foreach($tflist as $fkey=>$fname) { //$kk=0; $kk<count($tflist);$kk++) { $ftime = date ("Y-m-d H:i", filemtime($folder.$fname)); //tflist[$kk])); $fsize = number_format(filesize($folder.$fname)); //tflist[$kk])); $ret .= "{|}$fname^$fsize^$ftime"; } return $ret; } function FlMgr_DeleteFiles($parms) { global $asdt_fmgr_protect,$as_iface; $folder = isset($parms['folder'])? $parms['folder'] : './'; $ret = ''; $dlist = array(); foreach($parms as $vkey=>$vval) { if(substr($vkey,0,3)==='fl_' && $vval=='1') { $i = substr($vkey,3); $dlist[] = $parms['name_'.$i]; } } if(count($dlist)) { $msg = ''; for($kk=0;$kk<count($dlist);$kk++) { $msg .= ($msg==''? '':'<br>').$dlist[$kk].' : '; if(in_array($dlist[$kk],$asdt_fmgr_protect)) $msg .= $as_iface['file_protected']; else $msg .= (unlink($folder.$dlist[$kk]) ? $as_iface['file_deleted'] : $as_iface['deleting_error']); # $msg .= '<br>'.$dlist[$kk].' '.(true ? ' deleted':' deleting error!'); # emulate, no real seletion! (debug) } $ret = $msg.FlMgr_FileList($parms); # send new file list after deletion } else $ret = $as_iface['no_selected_files'].FlMgr_FileList($parms); return $ret; } /** * @desc FileMgr_StdPost() standart (non AJAX) POST handling. * Now used only for uploading files. */ function FileMgr_StdPost() { global $as_iface, $asdt_fmgr_folder, $asdt_fmgr_protect, $asdt_result; $pageid = isset($_POST['pageid'])? $_POST['pageid']: 0; $asdt_fmgr_folder = $folder = isset($_POST['folder'])? $_POST['folder']: ''; /* $_FILES['fileNN']['name'] - original filename $_FILES['fileNN']['tmp_name'] - temp.file path/name $_FILES['fileNN']['size'] - file size $_FILES['fileNN']['type'] - MIME-type ( "image/gif" ) $_FILES['fileNN']['error'] - error code */ $asdt_result[$pageid]= ''; for($kk=1; $kk<=ASADM_UPLOADFILES;$kk++) { if(empty($_FILES['file'.$kk]['name'])) { continue; } $fl = $_FILES['file'.$kk]; $to_name = ($folder==='./'?'':$folder) . $fl['name']; $tempname = $fl['tmp_name']; $errcode = $fl['error']; if(!empty($errcode)) { $asdt_result[$pageid] .= "{$fl['name']} - upload error: {$fl['error']}<br>"; } else { if(in_array($fl['name'],$asdt_fmgr_protect)) { # file in 'protected' list, don't upload it! $asdt_result[$pageid] .= "{$fl['name']} : {$as_iface['file_protected']}<br>"; unlink($tempname); } else { # if(@move_uploaded_file($tempname, $to_name) && is_file($to_name)) { // move_uploaded_file sucks ?! if(@copy($tempname, $to_name) && is_file($to_name)) { $asdt_result[$pageid] .= "$to_name : {$as_iface['file_uploaded']}, (" .number_format(filesize($to_name))." Bytes)<br>"; @unlink($tempname); } else $asdt_result[$pageid] .= "$to_name : {$as_iface['upload_error']} ($errcode)<br>"; } } } } ?>