/** * Get root dir * * @access public * @return string The root directory */ function GetFileBrowserRootDir() { static $root_dir; if (!isset($root_dir)) { $root_dir = trim($this->gadget->registry->fetch('root_dir')); $root_dir = JAWS_DATA . trim($root_dir, "\\/"); $root_dir = str_replace('..', '', $root_dir); require_once PEAR_PATH . 'File/Util.php'; $root_dir = File_Util::realpath($root_dir) . '/'; if (!File_Util::pathInRoot($root_dir, JAWS_DATA)) { Jaws_Error::Fatal(_t('FILEBROWSER_ERROR_DIRECTORY_DOES_NOT_EXISTS'), __FILE__, __LINE__); } } return $root_dir; }
/** * Creates a directory * * @access public * @param string $path Where to create it * @param string $dir_name Which name * @return bool Returns true if the directory was created, if not, returns false */ function MakeDir($path, $dir_name) { $path = trim($path, '/'); $path = str_replace('..', '', $path); $fModel = $this->gadget->model->load('Files'); $dir = $fModel->GetFileBrowserRootDir() . $path . '/' . $dir_name; require_once PEAR_PATH . 'File/Util.php'; $realpath = File_Util::realpath($dir); $blackList = explode(',', $this->gadget->registry->fetch('black_list')); $blackList = array_map('strtolower', $blackList); if (!File_Util::pathInRoot($realpath, $fModel->GetFileBrowserRootDir()) || in_array(strtolower(basename($realpath)), $blackList) || !Jaws_Utils::mkdir($realpath)) { $GLOBALS['app']->Session->PushLastResponse(_t('FILEBROWSER_ERROR_CANT_CREATE_DIRECTORY', $realpath), RESPONSE_ERROR); return false; } return true; }
function checkPermissions($file, $desc, $is_directory = FALSE) { clearstatcache(); if (!file_exists($file)) { if (!$is_directory) { // try to create the file ourselves then $fp = @fopen($file, 'w'); if (!$fp) { return "File '" . File_Util::realpath($file) . "' does not exist. Please create it (as a blank file) and try again."; } @fclose($fp); } else { if (!@mkdir($file)) { return "{$desc} does not exist. Please create it and try again."; } } } clearstatcache(); if (!is_writable($file)) { if (!stristr(PHP_OS, "win")) { // let's try to change the permissions ourselves @chmod($file, 0777); clearstatcache(); if (!is_writable($file)) { return "{$desc} is not writable"; } } else { return "{$desc} is not writable"; } } if (stristr(PHP_OS, "win")) { // need to check whether we can really create files in this directory or not // since is_writable() is not trustworthy on windows platforms if (is_dir($file)) { $fp = @fopen($file . '/dummy.txt', 'w'); if (!$fp) { return "{$desc} is not writable"; } @fwrite($fp, 'test'); @fclose($fp); // clean up after ourselves @unlink($file . '/dummy.txt'); } } return ""; }
/** * Get path relative to another path * * @static * @access public * @return string * @param string $path * @param string $root * @param string $separator */ function relativePath($path, $root, $separator = DIRECTORY_SEPARATOR) { $path = File_Util::realpath($path, $separator); $root = File_Util::realpath($root, $separator); $dirs = explode($separator, $path); $comp = explode($separator, $root); if (FILE_WIN32) { if (strcasecmp($dirs[0], $comp[0])) { return $path; } unset($dirs[0], $comp[0]); } foreach ($comp as $i => $part) { if (isset($dirs[$i]) && $part == $dirs[$i]) { unset($dirs[$i], $comp[$i]); } else { break; } } return str_repeat('..' . $separator, count($comp)) . implode($separator, $dirs); }
/** * @deprecated Use File_Util::realpath() instead. */ function realpath($path, $separator = DIRECTORY_SEPARATOR) { require_once 'File/Util.php'; return File_Util::realpath($path, $separator); }
/** * Uploads a new file * * @access public */ function UploadFile() { $this->gadget->CheckPermission('UploadFiles'); $fModel = $this->gadget->model->load('Files'); $fModelAdmin = $this->gadget->model->loadAdmin('Files'); $post = jaws()->request->fetch(array('path', 'file_title', 'file_description', 'file_fast_url', 'oldname', 'extra_params'), 'post'); $uploaddir = $fModel->GetFileBrowserRootDir() . $post['path']; require_once PEAR_PATH . 'File/Util.php'; $uploaddir = File_Util::realpath($uploaddir) . DIRECTORY_SEPARATOR; if (!File_Util::pathInRoot($uploaddir, $fModel->GetFileBrowserRootDir())) { $GLOBALS['app']->Session->PushLastResponse(_t('GLOBAL_ERROR_UPLOAD'), RESPONSE_ERROR); } else { $res = Jaws_Utils::UploadFiles($_FILES, $uploaddir, ''); if (Jaws_Error::IsError($res)) { $GLOBALS['app']->Session->PushLastResponse($res->getMessage(), RESPONSE_ERROR); } elseif (empty($res)) { $GLOBALS['app']->Session->PushLastResponse(_t('GLOBAL_ERROR_UPLOAD_4'), RESPONSE_ERROR); } else { $post['oldname'] = preg_replace('/[^[:alnum:]_\\.\\-]*/', '', $post['oldname']); if (!empty($post['oldname']) && $res['uploadfile'][0]['host_filename'] != $post['oldname']) { $fModelAdmin->Delete($post['path'], $post['oldname']); } $fModelAdmin->UpdateDBFileInfo($post['path'], $res['uploadfile'][0]['host_filename'], empty($post['file_title']) ? $res['uploadfile'][0]['user_filename'] : $post['file_title'], $post['file_description'], $post['file_fast_url'], $post['oldname']); } } if (empty($post['extra_params'])) { Jaws_Header::Location(BASE_SCRIPT . '?gadget=FileBrowser&action=Files&path=' . $post['path']); } else { Jaws_Header::Location(BASE_SCRIPT . '?gadget=FileBrowser&action=BrowseFile&path=' . $post['path'] . html_entity_decode($post['extra_params'])); } }
/** * Rename a given file or directory * * @access public * @param string $type file or dir * @param string $old_filename Filename to rename * @param string $new_filename New Filename * @return bool Returns file if file/directory was renamed without problems, if not, returns false */ function Rename($path, $old, $new) { $path = trim($path, '/'); $path = str_replace('..', '', $path); $fModel = $this->gadget->model->load('Files'); $oldfile = $fModel->GetFileBrowserRootDir() . $path . '/' . $old; $newfile = $fModel->GetFileBrowserRootDir() . $path . '/' . $new; require_once PEAR_PATH . 'File/Util.php'; $oldfile = File_Util::realpath($oldfile); $newfile = File_Util::realpath($newfile); $blackList = explode(',', $this->gadget->registry->fetch('black_list')); $blackList = array_map('strtolower', $blackList); if (!File_Util::pathInRoot($oldfile, $fModel->GetFileBrowserRootDir()) || !File_Util::pathInRoot($newfile, $fModel->GetFileBrowserRootDir()) || in_array(strtolower(basename($oldfile)), $blackList) || in_array(strtolower(basename($newfile)), $blackList)) { $GLOBALS['app']->Session->PushLastResponse(_t('FILEBROWSER_ERROR_CANT_RENAME', $old, $new), RESPONSE_ERROR); return false; } $return = @rename($oldfile, $newfile); if ($return) { $GLOBALS['app']->Session->PushLastResponse(_t('FILEBROWSER_RENAMED', $old, $new), RESPONSE_NOTICE); return true; } $msgError = _t('FILEBROWSER_ERROR_CANT_RENAME', $old, $new); $GLOBALS['app']->Session->PushLastResponse($msgError, RESPONSE_ERROR); return false; }