Init(FILEMANAGER_TITLE); if (!defined(LINK_MAIN_R)) $this->title=$_SERVER['HTTP_HOST']; $this->file=get_invar('file', 'request'); $dir=get_invar('dir', 'request'); $this->dir=$this->correctDir($dir); $this->dirs=explode('/', $this->dir); $this->post_files=array(); $this->hidden_files=array('source_php', '.php', '.htaccess', 'data_save', 'data', 'Pear', '.ico', '.css'); $this->check_hide=true; } function correctDir($dir) { $dir=trim($dir); if (empty($dir) or $dir=='.') return '.'; $dir=str_replace('\\', '/', $dir); $dir='/'.$dir.'/'; $dir=str_replace('/./', '/', $dir); $dir=str_replace('/../', '/', $dir); $dir=ereg_replace('/{2,}', '/', $dir); $dir=ereg_replace('^/', '', $dir); $dir=ereg_replace('/$', '', $dir); $buf=explode('/', $dir); $n=count($buf); if ($n>1) { unset($buf[$n-1]); $this->up_dir=implode('/', $buf); } return $dir; } function editFile() { if (empty($this->dir)) $path=$this->file; else $path=$this->dir.'/'.$this->file; $buf=print_file($path); $buf=str_replace('&', '&', $buf); $buf=str_replace('<', '<', $buf); $buf=str_replace('>', '>', $buf); if ($path[0]=='.' and $path[1]=='/') $path=substr($path, 2); $this->title='Редактирование файла /'.$path; $string='
'; $this->body=$string; } function editFileSave() { if (empty($this->dir)) $path=$this->file; else $path=$this->dir.'/'.$this->file; savew($path, get_invar('text', 'post')); go_location($this->getMainUrl('&').'dir='.$this->dir); } function chmod() { $mod=get_invar('new_mod', 'request'); if (!empty($mod) and strlen($mod)>=3 and strlen($mod)<5) { if (strlen($mod)==3) $mod='0'+$mod; if (empty($this->dir)) $path=$this->file; else $path=$this->dir.'/'.$this->file; $mod=base_convert($mod, 8, 10); if (file_exists($path)) { if (!@chmod($path, (int)$mod)) { go_thanks('no_correct_rights', 'filemanager'); } } else { go_thanks('file_no_exists', 'filemanager'); } } else { go_thanks('no_correct_rights', 'filemanager'); } go_location($this->getMainUrl('&').'dir='.$this->dir); } function rename() { $new_file=get_invar('new_file', 'request'); if (empty($this->dir)) { $path=$this->file; $new_path=$new_file; } else { $path=$this->dir.'/'.$this->file; $new_path=$this->dir.'/'.$new_file; } if (file_exists($path)) { @rename($path, $new_path); } else { go_thanks('file_no_exists', 'filemanager'); } go_location($this->getMainUrl('&').'dir='.$this->dir); } function delete() { $files=get_invar('all_files', 'post'); if (!empty($files)) { $files=ereg_replace('^/', '', $files); $files=ereg_replace('/$', '', $files); $files=explode('""', $files); if (count($files)) { foreach($files as $file) { $file=str_replace('"', '', $file); if (empty($this->dir)) $path=$file; else $path=$this->dir.'/'.$file; if (empty($path)) continue; if (is_dir($path)) @rmdir($path); else @unlink($path); } } } go_location($this->getMainUrl('&').'dir='.$this->dir); } function addFiles() { for($i=1; $i<=FILEMANAGER_N_ADD_FILES; $i++) { $var='file'.$i; if (!empty($_FILES[$var]['name'])) { if (empty($this->dir)) $path=$_FILES[$var]['name']; else $path=$this->dir.'/'.$_FILES[$var]['name']; move_uploaded_file($_FILES[$var]['tmp_name'], $path); } } go_location($this->getMainUrl('&').'dir='.$this->dir); } function extEdit($file) { if (FILEMANAGER_EDIT_ALL) return true; $exts=array('wml', 'html', 'htm', 'xml', 'txt', 'csv', 'css', 'php', 'head', 'htaccess', 'dtd', 'js', 'pl', 'ini', 'c', 'cpp', 'tex', 'pas'); $buf=pathinfo($file); $ext=$buf['extension']; foreach($exts as $s) if (!strcasecmp($ext, $s)) return true; return false; } function createDir() { $new_dir=get_invar('new_dir', 'request'); if (empty($this->dir)) $path=$new_dir; else $path=$this->dir.'/'.$new_dir; if (!file_exists($path)) { @mkdir($path); } else { go_thanks('file_exists', 'filemanager'); } go_location($this->getMainUrl('&').'dir='.$this->dir); } function createFile() { $new_dir=get_invar('new_file', 'request'); if (empty($this->dir)) $path=$new_dir; else $path=$this->dir.'/'.$new_dir; if (!file_exists($path)) { savew($path, ''); } else { go_thanks('file_exists', 'filemanager'); } go_location($this->getMainUrl('&').'dir='.$this->dir); } function display() { switch ($this->action) { case 'add_files': $this->addFiles(); break; case 'create_dir': $this->createDir(); break; case 'create_file': $this->createFile(); break; case 'rename': $this->rename(); break; case 'delete': $this->delete(); break; case 'edit_file': { $this->editFile(); return; } break; case 'edit_file_save': $this->editFileSave(); break; case 'chmod': $this->chmod(); break; } $size=(int)ini_get('upload_max_filesize'); $string=''; if (!file_exists($this->dir)) { $this->body='Не существует директория «'.$this->dir.'».
'; return; } $string.=$this->getPath(); $paths=$this->getDir($this->dir); $string.='Путь: /
'; else $string.='Путь:'; $hdir='/'; $first=true; $n=count($buf)-1; $i=0; foreach($buf as $file) { $hdir.=$file.'/'; if ($first) $string.=' / '; if ($i==$n) $string.=$file.' / '; else $string.=''.$file.' / '; $first=false; $i++; } $string.='
'; return $string; } function getMainUrl($amp='&') { if (!defined(LINK_MAIN_R)) return '?'; else { return LINK_MAIN_R.'filemanager'.$amp; } } } function sort_files($a, $b) { $res=strcasecmp($a['type'], $b['type']); if (!$res) $res=strcasecmp($a['file'], $b['file']); return $res; } class Plugin { var $title; var $body; var $out; var $page; var $action; var $print; var $id; var $mode; var $prefs; function Init($title='') { $this->title=$title; $this->body=''; $this->id=get_invar('id', 'request'); $this->action=get_invar('action', 'request'); $this->mode=get_invar('mode', 'request'); $this->page=get_invar('page', 'request'); } function display(){} } function go_thanks($type='', $section='') { switch($type) { case 'no_correct_rights': $text='Не правильно установлены права на файл или директория.'; break; case 'no_file_exists': $text='Отсутствует выбранный вами файл или директория.'; break; case 'file_exists': $text='Директория или файл уже существует с таким именем.'; break; default: $text='Неопределенная ошибка.'; break; } main('Ошибка', $text); } function smartBytes($bytes) { if ($bytes<=0) return '0 КБ'; $kb=(int)($bytes/1024); $mb=$kb/1024; $gb=$mb/1024; if (!$kb) return 'менее 1 КБ'; if ($mb>0.1) { $size=sprintf('%.1f', $mb); $string=$size.' МБ'; } else { $size=sprintf('%u', $kb); $string=$size.' КБ'; } if ($gb>=1) { $size=sprintf('%.1f', $gb); $string=$size.' ГБ'; } return $string; } function smartSize($num) { $buf=strval($num); $n=strlen($buf); $res=''; $l=0; if ($n>3) { $k=intval($n/3); for($i=$n-1; $i>=0; $i--) { if ($l>2) { $res=' '.$res; $l=0; } $res=$buf[$i].$res; $l++; } } else $res=$buf; return $res; } function get_invar($name, $mas, $type='string', $protect=false) { $buf=''; switch($mas) { case 'get': if (isset($_GET[$name])) $buf=$_GET[$name]; break; case 'post': if (isset($_POST[$name])) $buf=$_POST[$name]; break; case 'request': if (isset($_REQUEST[$name])) $buf=$_REQUEST[$name]; break; case 'cookie': if (isset($_COOKIE[$name])) $buf=$_COOKIE[$name]; break; } if (($type=='string' or $type=='xml') and ini_get(magic_quotes_gpc)) { $buf=stripslashes($buf); } if ($type=='xml') { $buf=pre_xml($buf); $type='string'; } if ($protect) { $buf=str_replace('&', '&', $buf); $buf=str_replace('<', '<', $buf); $buf=str_replace('>', '>', $buf); } settype($buf, $type); return $buf; } function savew($file, $text, $check=false) { if ($check) { if (!file_exists($file)) return; } $h=fopen($file, 'w'); if ($h) { flock($h, LOCK_EX); fwrite($h , $text); flock($h, LOCK_UN); fclose($h); } return; } function savea($file, $text, $check=false) { if ($check) { if (!file_exists($file)) return false; } $h=fopen($file, 'a'); if ($h) { flock($h, LOCK_EX); fwrite($h , $text); flock($h, LOCK_UN); fclose($h); } else return false; return true; } function print_file($file) { $string=''; if (file_exists($file) and !is_dir($file)) $string=file_get_contents($file); return $string; } function go_location($loc) { header('Cache-Control: no-cache'); header('Pragma: no-cache'); header('Last-Modified: '. gmdate("D, d M Y H:i:s") .' GMT'); header('Location: '.$loc); exit(); } function headers_no_cache() { header("Expires: Tue, 1 Jan 1980 12:00:00 GMT"); header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); header("Cache-Control: no-cache"); header("Pragma: no-cache"); if (FILEMANAGER_GZIP_PAGE and function_exists('gzencode')) header('Content-Encoding: gzip'); } function main($title='', $body='') { $ff=new FileManager(); $ff->display(); if (empty($title) and empty($body)) { $title=$ff->title; $body=$ff->body; } $string='