Beispiel #1
0
 /**
  * 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;
 }
Beispiel #2
0
 /**
  * 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;
 }
Beispiel #3
0
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 "";
}
 /**
  * Switch File Extension
  *
  * @static
  * @access  public
  * @return  string|array
  * @param   string|array    $filename
  * @param   string          $to new file extension
  * @param   string          $from change only files with this extension
  * @param   bool            $reverse change only files not having $from extension
  */
 function switchExt($filename, $to, $from = null, $reverse = false)
 {
     if (is_array($filename)) {
         foreach ($filename as $key => $file) {
             $filename[$key] = File_Util::switchExt($file, $to, $from);
         }
         return $filename;
     }
     if ($len = strlen($from)) {
         $ext = substr($filename, -$len - 1);
         $cfn = FILE_WIN32 ? 'strcasecmp' : 'strcmp';
         if (!$reverse == $cfn($ext, '.' . $from)) {
             return $filename;
         }
         return substr($filename, 0, -$len - 1) . '.' . $to;
     }
     if ($pos = strpos($filename, '.')) {
         return substr($filename, 0, $pos) . '.' . $to;
     }
     return $filename . '.' . $to;
 }
Beispiel #5
0
 /**
  * @deprecated      Use File_Util::realpath() instead.
  */
 function realpath($path, $separator = DIRECTORY_SEPARATOR)
 {
     require_once 'File/Util.php';
     return File_Util::realpath($path, $separator);
 }
 /**
  * Пометка письма как обработанное	 
  */
 function markAnsweredMessage()
 {
     $fName = File_Util::tmpFile(ANSWERED_DIR);
     $res = File::write($fName, $this->_message, FILE_MODE_WRITE);
     if (!PEAR::isError($res)) {
         imap_delete($this->_mailBox, $this->_messageNum);
     }
 }
Beispiel #7
0
 /**
  * Generate the temporary ssh_askpass script
  *
  * @param string $password The password to use for login.
  *
  * @return void
  */
 protected function sshAskPass($password)
 {
     ($this->ssh_askpass_scripts =& File_Util::tmpFile()) . "";
     $askpass_data = 'echo ' . $password . ' < /dev/null';
     File::write($this->ssh_askpass_scripts, $askpass_data);
     File::closeAll();
     chmod($this->ssh_askpass_scripts, 0700);
 }
Beispiel #8
0
 /**
  * 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']));
     }
 }
Beispiel #9
0
 /**
  * Constructor
  *
  * @param   mixed   $db         SQLiteDatabase のインスタンス or データベースのパス
  * @param   array   $options    キャッシュ設定オプション (optional)
  * @throws  Cache_SQLiteException
  * @access  public
  */
 public function __construct($db = ':memory:', $options = array())
 {
     // エラー管理オブジェクトを用意
     $this->_stack = PEAR_ErrorStack::singleton('Cache_SQLite');
     // オプションを設定
     $constOptions = array('debug', 'strict', 'autoCreateTable', 'autoVacuum');
     foreach ($this->_defaults as $key => $value) {
         $propName = '_' . $key;
         $this->{$propName} = $value;
         if (isset($options[$key])) {
             if ($key == 'table') {
                 $this->{$propName} = (string) $options[$key];
             } elseif (in_array($key, $constOptions)) {
                 $this->{$propName} = (bool) $options[$key];
             } else {
                 $this->setOption($key, $options[$key]);
             }
         }
     }
     $this->_tableQuoted = self::_quoteIdentifier($this->_table);
     $this->_validFromat = self::SERIALIZE_PHP | self::ENCODE_BASE64 | self::ENCODE_ZLIB_BINARY;
     // データベースをオープン
     $errmsg = '';
     if ($db instanceof SQLiteDatabase) {
         $this->_db = $db;
     } else {
         if ($db == ':memory:' || File_Util::isAbsolute($db)) {
             $path = $db;
         } else {
             $path = File_Util::realPath($db);
         }
         $this->_db = new SQLiteDatabase($path, CACHE_SQLITE_FILE_MODE, $errmsg);
     }
     if ($errmsg == '') {
         $this->_inTransaction = false;
         if ($this->_autoCreateTable) {
             $this->_checkTable();
         }
     } else {
         $params = array('message' => $errmsg);
         $msg = 'sqlite_open() failed: %message%';
         $this->_stack->push(self::ERR_CRITICAL, 'error', $params, $msg);
         throw new Cache_SQLiteException('Cache_SQLite sqlite_open() failed: {$errmsg}', self::ERR_CRITICAL);
     }
 }
Beispiel #10
0
 /**
  * List Directory
  * 
  * The final argument, $cb, is a callback that either evaluates to true or
  * false and performs a filter operation, or it can also modify the 
  * directory/file names returned.  To achieve the latter effect use as 
  * follows:
  * 
  * <code>
  * <?php
  * function uc(&$filename) {
  *     $filename = strtoupper($filename);
  *     return true;
  * }
  * $entries = File_Util::listDir('.', FILE_LIST_ALL, FILE_SORT_NONE, 'uc');
  * foreach ($entries as $e) {
  *     echo $e->name, "\n";
  * }
  * ?>
  * </code>
  * 
  * @static
  * @access  public
  * @return  array
  * @param   string  $path
  * @param   int     $list
  * @param   int     $sort
  * @param   mixed   $cb
  */
 function listDir($path, $list = FILE_LIST_ALL, $sort = FILE_SORT_NONE, $cb = null)
 {
     if (!strlen($path) || !is_dir($path)) {
         return null;
     }
     $entries = array();
     for ($dir = dir($path); false !== ($entry = $dir->read());) {
         if ($list & FILE_LIST_DOTS || $entry[0] !== '.') {
             $isRef = $entry === '.' || $entry === '..';
             $isDir = $isRef || is_dir($path . '/' . $entry);
             if ((!$isDir && $list & FILE_LIST_FILES || $isDir && $list & FILE_LIST_DIRS) && (!is_callable($cb) || call_user_func_array($cb, array(&$entry)))) {
                 $entries[] = (object) array('name' => $entry, 'size' => $isDir ? null : filesize($path . '/' . $entry), 'date' => filemtime($path . '/' . $entry));
             }
         }
     }
     $dir->close();
     if ($sort) {
         $entries = File_Util::sortFiles($entries, $sort);
     }
     return $entries;
 }
Beispiel #11
0
 /**
  * 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;
 }
Beispiel #12
0
/**
 * 実在しない(かもしれない)ファイルの絶対パスを取得する
 *
 * @param   string $path
 * @return  string
 */
function p2_realpath($path)
{
    if (file_exists($path)) {
        return realpath($path);
    }
    return File_Util::realPath($path);
}
Beispiel #13
0
function getPermissionError($file, $desc, $is_directory, $exists)
{
    $error = '';
    if ($is_directory) {
        $title = 'Directory';
    } else {
        $title = 'File';
    }
    $error = "{$title} <b>'" . File_Util::realPath($file) . ($is_directory ? '/' : '') . "'</b> ";
    if (!$exists) {
        $error .= "does not exist. Please create the {$title} and reload this page.";
    } else {
        $error .= "is not writeable. Please change this {$title} to be writeable by the web server.";
    }
    return $error;
}
Beispiel #14
0
/**
 * @return  void
 */
function _prepareFileSession()
{
    global $_conf;
    // セッションデータ保存ディレクトリを設定
    if ($_conf['session_save'] == 'p2' and session_module_name() == 'files') {
        // $_conf['data_dir'] を絶対パスに変換する
        define('P2_DATA_DIR_REAL_PATH', File_Util::realPath($_conf['data_dir']));
        $_conf['session_dir'] = P2_DATA_DIR_REAL_PATH . DIRECTORY_SEPARATOR . 'session';
    }
    if (!is_dir($_conf['session_dir'])) {
        require_once P2_LIB_DIR . '/FileCtl.php';
        FileCtl::mkdirFor($_conf['session_dir'] . '/dummy_filename');
    }
    if (!is_writable($_conf['session_dir'])) {
        die(sprintf('p2 error: セッションデータ保存ディレクトリ (%s) に書き込み権限がありません。', hs($_conf['session_dir'])));
    }
    session_save_path($_conf['session_dir']);
    // session.save_path のパスの深さが2より大きいとガーベッジコレクションが行われないので
    // 自前でガーベッジコレクションする
    P2Util::session_gc();
}
Beispiel #15
0
/**
 * 実在しない(かもしれない)ファイルの絶対パスを取得する
 *
 * @param   string $path
 * @return  string
 */
function p2_realpath($path)
{
    if (file_exists($path)) {
        return realpath($path);
    }
    if (!class_exists('File_Util', false)) {
        require 'File/Util.php';
    }
    return File_Util::realPath($path);
}