* @copyright (c) 2008-13 by Ralf Becker * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @version $Id$ */ /** * Filemanage user interface class */ class filemanager_ui { /** * Methods callable via menuaction * * @var array */ var $public_functions = array( 'index' => true, 'file' => true, ); /** * Views available from plugins * * @var array */ public static $views = array( 'filemanager_ui::listview' => 'Listview', ); public static $views_init = false; /** * vfs namespace for document merge properties * */ public static $merge_prop_namespace = ''; /** * Constructor * */ function __construct() { // strip slashes from _GET parameters, if someone still has magic_quotes_gpc on if (get_magic_quotes_gpc() && $_GET) { $_GET = etemplate::array_stripslashes($_GET); } // do we have root rights if (egw_session::appsession('is_root','filemanager')) { egw_vfs::$is_root = true; } self::init_views(); self::$merge_prop_namespace = egw_vfs::DEFAULT_PROP_NAMESPACE.$GLOBALS['egw_info']['flags']['currentapp']; } /** * Initialise and return available views * * @return array with method => label pairs */ public static function init_views() { if (!self::$views_init) { // translate our labels foreach(self::$views as $method => &$label) { $label = lang($label); } // search for plugins with additional filemanager views foreach($GLOBALS['egw']->hooks->process('filemanager_views') as $app => $views) { if ($views) self::$views += $views; } self::$views_init = true; } return self::$views; } /** * Get active view * * @return string */ public static function get_view() { $view =& egw_cache::getSession('filemanager', 'view'); if (isset($_GET['view'])) { $view = $_GET['view']; } if (!isset(self::$views[$view])) { reset(self::$views); $view = key(self::$views); } return $view; } /** * Context menu * * @return array */ private static function get_actions() { $actions = array( 'open' => array( 'caption' => lang('Open'), 'icon' => '', 'group' => $group=1, 'allowOnMultiple' => false, 'onExecute' => 'javaScript:app.filemanager.open', 'default' => true ), 'saveas' => array( 'caption' => lang('Save as'), 'group' => $group, 'allowOnMultiple' => false, 'icon' => 'filesave', 'onExecute' => 'javaScript:app.filemanager.force_download', 'disableClass' => 'isDir', ), 'edit' => array( 'caption' => lang('Edit settings'), 'group' => $group, 'allowOnMultiple' => false, 'onExecute' => 'javaScript:app.filemanager.editprefs', ), 'mail' => array( 'caption' => lang('Mail files'), 'icon' => 'filemanager/mail_post_to', 'group' => $group, 'onExecute' => 'javaScript:app.filemanager.mail', ), 'copy' => array( 'caption' => lang('Copy'), 'group' => ++$group, 'onExecute' => 'javaScript:app.filemanager.clipboard', ), 'add' => array( 'caption' => lang('Add to clipboard'), 'group' => $group, 'icon' => 'copy', 'onExecute' => 'javaScript:app.filemanager.clipboard', ), 'cut' => array( 'caption' => lang('Cut'), 'group' => $group, 'onExecute' => 'javaScript:app.filemanager.clipboard', ), 'documents' => filemanager_merge::document_action( $GLOBALS['egw_info']['user']['preferences']['filemanager']['document_dir'], ++$group, 'Insert in document', 'document_', $GLOBALS['egw_info']['user']['preferences']['filemanager']['default_document'] ), 'delete' => array( 'caption' => lang('Delete'), 'group' => ++$group, 'confirm' => 'Delete these files or directories?', 'onExecute' => 'javaScript:app.filemanager.action', ), // DRAG and DROP events 'file_drag' => array( 'dragType' => 'file', 'type' => 'drag', 'onExecute' => 'javaScript:app.filemanager.drag' ), 'file_drop_move' => array( 'icon' => 'stylite/move', 'acceptedTypes' => 'file', 'caption' => lang('Move into folder'), 'type' => 'drop', 'onExecute' => 'javaScript:app.filemanager.drop', 'default' => true ), 'file_drop_copy' => array( 'icon' => 'stylite/edit_copy', 'acceptedTypes' => 'file', 'caption' => lang('Copy into folder'), 'type' => 'drop', 'onExecute' => 'javaScript:app.filemanager.drop' ) ); if (!isset($GLOBALS['egw_info']['user']['apps']['felamimail'])) { unset($actions['mail']); } return $actions; } /** * Get mergeapp property for given path * * @param string $path * @param string $scope='self' (default) or 'parents' * $scope == 'self' query only the given path * $scope == 'parents' query only path parents for property (first parent in hierarchy upwards wins) * * @return string merge application or NULL if no property found */ private static function get_mergeapp($path, $scope='self') { $app = null; switch($scope) { case 'self': $props = egw_vfs::propfind($path, self::$merge_prop_namespace); $app = empty($props) ? null : $props[0]['val']; break; case 'parents': // search for props in parent directories $currentpath = $path; while($dir = egw_vfs::dirname($currentpath)) { $props = egw_vfs::propfind($dir, self::$merge_prop_namespace); if(!empty($props)) { // found prop in parent directory return $app = $props[0]['val']; } $currentpath = $dir; } break; } return $app; } /** * Main filemanager page * * @param array $content=null * @param string $msg=null */ function index(array $content=null,$msg=null) { if (!is_array($content)) { $content = array( 'nm' => egw_session::appsession('index','filemanager'), ); if (!is_array($content['nm'])) { $content['nm'] = array( 'get_rows' => 'filemanager.filemanager_ui.get_rows', // I method/callback to request the data for the rows eg. 'notes.bo.get_rows' 'filter' => '1', // current dir only 'no_filter2' => True, // I disable the 2. filter (params are the same as for filter) 'no_cat' => True, // I disable the cat-selectbox 'lettersearch' => True, // I show a lettersearch 'searchletter' => false, // I0 active letter of the lettersearch or false for [all] 'start' => 0, // IO position in list 'order' => 'name', // IO name of the column to sort after (optional for the sortheaders) 'sort' => 'ASC', // IO direction of the sort: 'ASC' or 'DESC' 'default_cols' => '!comment,ctime', // I columns to use if there's no user or default pref (! as first char uses all but the named columns), default all columns 'csv_fields' => false, // I false=disable csv export, true or unset=enable it with auto-detected fieldnames, //or array with name=>label or name=>array('label'=>label,'type'=>type) pairs (type is a eT widget-type) 'actions' => self::get_actions(), 'row_id' => 'path', 'row_modified' => 'mtime', 'parent_id' => 'dir', 'is_parent' => 'mime', 'is_parent_value'=> egw_vfs::DIR_MIME_TYPE, 'header_left' => 'filemanager.index.header_left', ); $content['nm']['path'] = self::get_home_dir(); } $content['nm']['home_dir'] = self::get_home_dir(); if (isset($_GET['msg'])) $msg = $_GET['msg']; // switch to projectmanager folders if (isset($_GET['pm_id'])) { $_GET['path'] = '/apps/projectmanager'.((int)$_GET['pm_id'] ? '/'.(int)$_GET['pm_id'] : ''); } if (isset($_GET['path']) && ($path = $_GET['path'])) { switch($path) { case '..': $path = egw_vfs::dirname($content['nm']['path']); break; case '~': $path = self::get_home_dir(); break; } if ($path[0] == '/' && egw_vfs::stat($path,true) && egw_vfs::is_dir($path) && egw_vfs::check_access($path,egw_vfs::READABLE)) { $content['nm']['path'] = $path; } else { $msg .= lang('The requested path %1 is not available.',egw_vfs::decodePath($path)); } // reset lettersearch as it confuses users (they think the dir is empty) $content['nm']['searchletter'] = false; // switch recusive display off if (!$content['nm']['filter']) $content['nm']['filter'] = '1'; } } $view = self::get_view(); if (strpos($view,'::') !== false && version_compare(PHP_VERSION,'5.3.0','<')) { $view = explode('::',$view); } call_user_func($view,$content,$msg); } /** * Make the current user (vfs) root * * The user/pw is either the setup config user or a specially configured vfs_root user * * @param string $user='' setup config user to become root or '' to log off as root * @param string $password=null setup config password to become root * @param boolean &$is_setup=null on return true if authenticated user is setup config user, false otherwise * @return boolean true is root user given, false otherwise (including logout / empty $user) */ protected function sudo($user='',$password=null,&$is_setup=null) { if (!$user) { $is_root = $is_setup = false; } else { // config user & password $is_setup = egw_session::user_pw_hash($user,$password) === $GLOBALS['egw_info']['server']['config_hash']; // or vfs root user from setup >> configuration $is_root = $is_setup || $GLOBALS['egw_info']['server']['vfs_root_user'] && in_array($user,preg_split('/, */',$GLOBALS['egw_info']['server']['vfs_root_user'])) && $GLOBALS['egw']->auth->authenticate($user, $password, 'text'); } //echo "

".__METHOD__."('$user','$password',$is_setup) user_pw_hash(...)='".egw_session::user_pw_hash($user,$password)."', config_hash='{$GLOBALS['egw_info']['server']['config_hash']}' --> returning ".array2string($is_root)."

\n"; egw_session::appsession('is_setup','filemanager',$is_setup); return egw_session::appsession('is_root','filemanager',egw_vfs::$is_root = $is_root); } /** * Filemanager listview * * @param array $content=null * @param string $msg=null */ function listview(array $content=null,$msg=null) { require_once EGW_INCLUDE_ROOT.'/etemplate/inc/class.etemplate.inc.php'; $tpl = new etemplate_new('filemanager.index'); $content['nm']['msg'] = $msg; if (($content['nm']['action'] || $content['nm']['rows']) && (empty($content['button']) || !isset($content['button']))) { if ($content['nm']['action']) { $content['nm']['msg'] = self::action($content['nm']['action'],$content['nm']['selected'],$content['nm']['path']); // clean up after action unset($content['nm']['selected']); // reset any occasion where action may be stored, as it may be ressurected out of the helpers by etemplate, which is quite unconvenient in case of action delete if (isset($content['nm']['action'])) unset($content['nm']['action']); if (isset($content['nm']['nm_action'])) unset($content['nm']['nm_action']); if (isset($content['nm_action'])) unset($content['nm_action']); // we dont use ['nm']['rows']['delete'], so unset it, if it is present if (isset($content['nm']['rows']['delete'])) unset($content['nm']['rows']['delete']); } elseif($content['nm']['rows']['delete']) { $content['nm']['msg'] = self::action('delete',array_keys($content['nm']['rows']['delete']),$content['nm']['path']); // clean up after action unset($content['nm']['rows']['delete']); // reset any occasion where action may be stored, as we use ['nm']['rows']['delete'] anyhow // we clean this up, as it may be ressurected out of the helpers by etemplate, which is quite unconvenient in case of action delete if (isset($content['nm']['action'])) unset($content['nm']['action']); if (isset($content['nm']['nm_action'])) unset($content['nm']['nm_action']); if (isset($content['nm_action'])) unset($content['nm_action']); if (isset($content['nm']['selected'])) unset($content['nm']['selected']); } unset($content['nm']['rows']); egw_session::appsession('index','filemanager',$content['nm']); } // be tolerant with (in previous versions) not correct urlencoded pathes if ($content['nm']['path'][0] == '/' && !egw_vfs::stat($content['nm']['path'],true) && egw_vfs::stat(urldecode($content['nm']['path']))) { $content['nm']['path'] = urldecode($content['nm']['path']); } if ($content['button']) { if ($content['button']) { list($button) = each($content['button']); unset($content['button']); } switch($button) { case 'upload': if (!$content['upload']) { $content['nm']['msg'] = lang('You need to select some files first!'); break; } $upload_success = $upload_failure = array(); foreach(isset($content['upload'][0]) ? $content['upload'] : array($content['upload']) as $upload) { // encode chars which special meaning in url/vfs (some like / get removed!) $to = egw_vfs::concat($content['nm']['path'],egw_vfs::encodePathComponent($upload['name'])); if ($upload && (egw_vfs::is_writable($content['nm']['path']) || egw_vfs::is_writable($to)) && copy($upload['tmp_name'],egw_vfs::PREFIX.$to)) { $upload_success[] = $upload['name']; } else { $upload_failure[] = $upload['name']; } } $content['nm']['msg'] = ''; if ($upload_success) { $content['nm']['msg'] = count($upload_success) == 1 && !$upload_failure ? lang('File successful uploaded.') : lang('%1 successful uploaded.',implode(', ',$upload_success)); } if ($upload_failure) { $content['nm']['msg'] .= ($upload_success ? "\n" : '').lang('Error uploading file!')."\n".etemplate::max_upload_size_message(); } break; } } $readonlys['button[mailpaste]'] = !isset($GLOBALS['egw_info']['user']['apps']['felamimail']); $sel_options['filter'] = array( '1' => 'Current directory', '2' => 'Directories sorted in', '3' => 'Show hidden files', '' => 'Files from subdirectories', ); $tpl->setElementAttribute('nm', 'onfiledrop', 'app.filemanager.filedrop'); $tpl->setElementAttribute('nm[upload]', 'onFinish', 'app.filemanager.upload'); $tpl->setElementAttribute('nm[upload]', 'multiple', true); $tpl->exec('filemanager.filemanager_ui.index',$content,$sel_options,$readonlys,array('nm' => $content['nm'])); } /** * Get the configured start directory for the current user * * @return string */ static function get_home_dir() { $start = '/home/'.$GLOBALS['egw_info']['user']['account_lid']; // check if user specified a valid startpath in his prefs --> use it if (($path = $GLOBALS['egw_info']['user']['preferences']['filemanager']['startfolder']) && $path[0] == '/' && egw_vfs::is_dir($path) && egw_vfs::check_access($path, egw_vfs::READABLE)) { $start = $path; } return $start; } /** * Run a certain action with the selected file * * @param string $action * @param array $selected selected pathes * @param mixed $dir=null current directory * @param int &$errs=null on return number of errors * @param int &$dirs=null on return number of dirs deleted * @param int &$files=null on return number of files deleted * @return string success or failure message displayed to the user */ static private function action($action,$selected,$dir=null,&$errs=null,&$files=null,&$dirs=null) { if (!count($selected)) { return lang('You need to select some files first!'); } $errs = $dirs = $files = 0; switch($action) { case 'mail': throw new egw_exception_assertion_failed('Implemented on clientside!'); case 'delete': return self::do_delete($selected,$errs,$files,$dirs); case 'copy': foreach($selected as $path) { if (!egw_vfs::is_dir($path)) { $to = egw_vfs::concat($dir,egw_vfs::basename($path)); if ($path != $to && egw_vfs::copy($path,$to)) { ++$files; } else { ++$errs; } } else { $len = strlen(dirname($path)); foreach(egw_vfs::find($path) as $p) { $to = $dir.substr($p,$len); if ($to == $p) // cant copy into itself! { ++$errs; continue; } if (($is_dir = egw_vfs::is_dir($p)) && egw_vfs::mkdir($to,null,STREAM_MKDIR_RECURSIVE)) { ++$dirs; } elseif(!$is_dir && egw_vfs::copy($p,$to)) { ++$files; } else { ++$errs; } } } } if ($errs) { return lang('%1 errors copying (%2 diretories and %3 files copied)!',$errs,$dirs,$files); } return $dirs ? lang('%1 directories and %2 files copied.',$dirs,$files) : lang('%1 files copied.',$files); case 'move': foreach($selected as $path) { $to = egw_vfs::is_dir($dir) || count($selected) > 1 ? egw_vfs::concat($dir,egw_vfs::basename($path)) : $dir; if ($path != $to && egw_vfs::rename($path,$to)) { ++$files; } else { ++$errs; } } if ($errs) { return lang('%1 errors moving (%2 files moved)!',$errs,$files); } return lang('%1 files moved.',$files); case 'symlink': // symlink given files to $dir foreach((array)$selected as $target) { $link = egw_vfs::concat($dir, egw_vfs::basename($target)); if ($target[0] != '/') $target = egw_vfs::concat($dir, $target); if (!egw_vfs::stat($target)) { return lang('Link target %1 not found!', egw_vfs::decodePath($target)); } if ($target != $link && egw_vfs::symlink($target, $link)) { ++$files; } else { ++$errs; } } if (count((array)$selected) == 1) { return $files ? lang('Symlink to %1 created.', egw_vfs::decodePath($target)) : lang('Error creating symlink to target %1!', egw_vfs::decodePath($target)); } $ret = lang('%1 elements linked.', $files); if ($errs) { $ret = lang('%1 errors linking (%2)!',$errs, $ret); } return $ret;//." egw_vfs::symlink('$target', '$link')"; case 'createdir': $dst = egw_vfs::concat($dir, is_array($selected) ? $selected[0] : $selected); if (egw_vfs::mkdir($dst, null, STREAM_MKDIR_RECURSIVE)) { return lang("Directory successfully created."); } return lang("Error while creating directory."); default: list($action, $settings) = explode('_', $action, 2); switch($action) { case 'document': if (!$settings) $settings = $GLOBALS['egw_info']['user']['preferences']['filemanager']['default_document']; $document_merge = new filemanager_merge(egw_vfs::decodePath($dir)); $msg = $document_merge->download($settings, $selected, '', $GLOBALS['egw_info']['user']['preferences']['filemanager']['document_dir']); if($msg) return $msg; $failed = count($selected); return false; } } return "Unknown action '$action'!"; } /** * Delete selected files and return success or error message * * @param array $selected * @param int &$errs=null on return number of errors * @param int &$dirs=null on return number of dirs deleted * @param int &$files=null on return number of files deleted * @return string */ public static function do_delete(array $selected, &$errs=null, &$dirs=null, &$files=null) { $dirs = $files = $errs = 0; // we first delete all selected links (and files) // feeding the links to dirs to egw_vfs::find() deletes the content of the dirs, not just the link! foreach($selected as $key => $path) { if (!egw_vfs::is_dir($path) || egw_vfs::is_link($path)) { if (egw_vfs::unlink($path)) { ++$files; } else { ++$errs; } unset($selected[$key]); } } if ($selected) // somethings left to delete { // some precaution to never allow to (recursivly) remove /, /apps or /home foreach((array)$selected as $path) { if (preg_match('/^\/?(home|apps|)\/*$/',$path)) { $errs++; return lang("Cautiously rejecting to remove folder '%1'!",egw_vfs::decodePath($path)); } } // now we use find to loop through all files and dirs: (selected only contains dirs now) // - depth=true to get first the files and then the dir containing it // - hidden=true to also return hidden files (eg. Thumbs.db), as we cant delete non-empty dirs foreach(egw_vfs::find($selected,array('depth'=>true,'hidden'=>true)) as $path) { if (($is_dir = egw_vfs::is_dir($path) && !egw_vfs::is_link($path)) && egw_vfs::rmdir($path,0)) { ++$dirs; } elseif (!$is_dir && egw_vfs::unlink($path)) { ++$files; } else { ++$errs; } } } if ($errs) { return lang('%1 errors deleteting (%2 directories and %3 files deleted)!',$errs,$dirs,$files); } if ($dirs) { return lang('%1 directories and %2 files deleted.',$dirs,$files); } return $files == 1 ? lang('File deleted.') : lang('%1 files deleted.',$files); } /** * Callback to fetch the rows for the nextmatch widget * * @param array $query * @param array &$rows * @param array &$readonlys */ function get_rows($query,&$rows,&$readonlys) { // show projectmanager sidebox for projectmanager path if (substr($query['path'],0,20) == '/apps/projectmanager' && isset($GLOBALS['egw_info']['user']['apps']['projectmanager'])) { $GLOBALS['egw_info']['flags']['currentapp'] = 'projectmanager'; } // do NOT store query, if hierarchical data / children are requested if (!$query['csv_export']) { egw_session::appsession('index','filemanager',$query); } // be tolerant with (in previous versions) not correct urlencoded pathes if (!egw_vfs::stat($query['path'],true) && egw_vfs::stat(urldecode($query['path']))) { $query['path'] = urldecode($query['path']); } if (!egw_vfs::stat($query['path'],true) || !egw_vfs::is_dir($query['path']) || !egw_vfs::check_access($query['path'],egw_vfs::READABLE)) { // we will leave here, since we are not allowed, or the location does not exist. Index must handle that, and give // an appropriate message egw::redirect_link('/index.php',array('menuaction'=>'filemanager.filemanager_ui.index', 'path' => self::get_home_dir(), 'msg' => lang('The requested path %1 is not available.',egw_vfs::decodePath($query['path'])), )); } $rows = $dir_is_writable = array(); if($query['searchletter'] && !empty($query['search'])) { $namefilter = '/^'.$query['searchletter'].'.*'.str_replace(array('\\?','\\*'),array('.{1}','.*'),preg_quote($query['search'])).'/i'; if ($query['searchletter'] == strtolower($query['search'][0])) { $namefilter = '/^('.$query['searchletter'].'.*'.str_replace(array('\\?','\\*'),array('.{1}','.*'),preg_quote($query['search'])).'|'. str_replace(array('\\?','\\*'),array('.{1}','.*'),preg_quote($query['search'])).')/i'; } } elseif ($query['searchletter']) { $namefilter = '/^'.$query['searchletter'].'/i'; } elseif(!empty($query['search'])) { $namefilter = '/'.str_replace(array('\\?','\\*'),array('.{1}','.*'),preg_quote($query['search'])).'/i'; } foreach(egw_vfs::find(!empty($query['col_filter']['dir']) ? $query['col_filter']['dir'] : $query['path'],array( 'mindepth' => 1, 'maxdepth' => $query['filter'] ? (int)(boolean)$query['filter'] : null, 'dirsontop' => $query['filter'] <= 1, 'type' => $query['filter'] ? null : 'f', 'order' => $query['order'], 'sort' => $query['sort'], 'limit' => (int)$query['num_rows'].','.(int)$query['start'], 'need_mime' => true, 'name_preg' => $namefilter, 'hidden' => $query['filter'] == 3, ),true) as $path => $row) { //echo $path; _debug_array($row); $dir = dirname($path); if (!isset($dir_is_writable[$dir])) { $dir_is_writable[$dir] = egw_vfs::is_writable($dir); } $path_quoted = str_replace(array('"',"'"),array('"',"\\'"),$path); if (!$dir_is_writable[$dir]) { $readonlys["delete[$path_quoted]"] = true; // no rights to delete the file } if (egw_vfs::is_dir($path)) { $readonlys["mail[$path_quoted]"] = true; $row['class'] = 'isDir'; } elseif (!egw_vfs::is_readable($path) || !$GLOBALS['egw_info']['user']['apps']['felamimail']) { $readonlys["mail[$path_quoted]"] = true; } $row['download_url'] = egw_vfs::download_url($path); $rows[++$n] = $row; $path2n[$path] = $n; } // query comments and cf's for the displayed rows $cols_to_show = explode(',',$GLOBALS['egw_info']['user']['preferences']['filemanager']['nextmatch-filemanager.index.rows']); $cfs = config::get_customfields('filemanager'); $all_cfs = in_array('customfields',$cols_to_show) && $cols_to_show[count($cols_to_show)-1][0] != '#'; if ($path2n && (in_array('comment',$cols_to_show) || in_array('customfields',$cols_to_show)) && ($path2props = egw_vfs::propfind(array_keys($path2n)))) { foreach($path2props as $path => $props) { unset($row); // fixes a weird problem with php5.1, does NOT happen with php5.2 $row =& $rows[$path2n[$path]]; if ( !is_array($props) ) continue; foreach($props as $prop) { if (!$all_cfs && $prop['name'][0] == '#' && !in_array($prop['name'],$cols_to_show)) continue; $row[$prop['name']] = strlen($prop['val']) < 64 ? $prop['val'] : substr($prop['val'],0,64).' ...'; } } } // tell client-side if directory is writeable or not $response = egw_json_response::get(); $response->call('app.filemanager.set_readonly', $query['path'], !egw_vfs::is_writable($query['path'])); //_debug_array($readonlys); if ($GLOBALS['egw_info']['flags']['currentapp'] == 'projectmanager') { $GLOBALS['egw_info']['flags']['app_header'] = lang('Projectmanager').' - '.lang('Filemanager'); // we need our app.css file if (!file_exists(EGW_SERVER_ROOT.($css_file='/filemanager/templates/'.$GLOBALS['egw_info']['server']['template_set'].'/app.css'))) { $css_file = '/filemanager/templates/default/app.css'; } $GLOBALS['egw_info']['flags']['css'] .= "\n\t\t\n\t\t".''."\n\t\t