Ejemplo n.º 1
0
 /**
  * Create a folder -- and all necessary parent folders.
  *
  * @param   string   $path  A path to create from the base path.
  * @param   integer  $mode  Directory permissions to set for folders created. 0755 by default.
  *
  * @return  boolean  True if successful.
  *
  * @since   11.1
  */
 public static function create($path = '', $mode = 0755)
 {
     static $nested = 0;
     //if (Zend_Registry::isRegistered('logger')):
     //   $logger = Zend_Registry::get('logger');
     //endif;
     // Check to make sure the path valid and clean
     $path = App_Filesystem_Path::clean($path);
     // Check if parent dir exists
     $parent = dirname($path);
     if (!self::exists($parent)) {
         // Prevent infinite loops!
         $nested++;
         if ($nested > 20 || $parent == $path) {
             //$logger->getLog('genmodel')->log(__METHOD__ . ': Infinite loop detected', Zend_Log::WARN);
             $nested--;
             return false;
         }
         // Create the parent directory
         if (self::create($parent, $mode) !== true) {
             // JFolder::create throws an error
             $nested--;
             return false;
         }
         // OK, parent directory has been created
         $nested--;
     }
     // Check if dir already exists
     if (self::exists($path)) {
         return true;
     }
     // Check for safe mode
     // We need to get and explode the open_basedir paths
     $obd = ini_get('open_basedir');
     // If open_basedir is set we need to get the open_basedir that the path is in
     if ($obd != null) {
         if (IS_WIN) {
             $obdSeparator = ";";
         } else {
             $obdSeparator = ":";
         }
         // Create the array of open_basedir paths
         $obdArray = explode($obdSeparator, $obd);
         $inBaseDir = false;
         // Iterate through open_basedir paths looking for a match
         foreach ($obdArray as $test) {
             $test = App_Filesystem_Path::clean($test);
             if (strpos($path, $test) === 0) {
                 $inBaseDir = true;
                 break;
             }
         }
         if ($inBaseDir == false) {
             // Return false for JFolder::create because the path to be created is not in open_basedir
             //$logger->getLog('genmodel')->log(__METHOD__ . ': Path not in open_basedir paths', Zend_Log::WARN);
             return false;
         }
     }
     // First set umask
     $origmask = @umask(0);
     // Create the path
     if (!($ret = @mkdir($path, $mode))) {
         @umask($origmask);
         // $logger->getLog('genmodel')->log(__METHOD__ . ': Could not create directory ' . 'Path: ' . $path, Zend_Log::WARN);
         return false;
     }
     // Reset umask
     @umask($origmask);
     return $ret;
 }
Ejemplo n.º 2
0
 /**
  * Write contents to a file
  *
  * @param   string   $file         The full file path
  * @param   string   &$buffer      The buffer to write
  * @param   boolean  $use_streams  Use streams
  *
  * @return  boolean  True on success
  *
  * @since   11.1
  */
 public static function write($file, &$buffer, $use_streams = false)
 {
     @set_time_limit(ini_get('max_execution_time'));
     if (Zend_Registry::isRegistered('logger')) {
         $logger = Zend_Registry::get('logger');
     }
     // If the destination directory doesn't exist we need to create it
     if (!file_exists(dirname($file))) {
         if (App_Filesystem_Folder::create(dirname($file)) == false) {
             return false;
         }
     }
     if ($use_streams) {
         $stream = new App_Filesystem_Stream();
         // Beef up the chunk size to a meg
         $stream->set('chunksize', 1024 * 1024);
         if (!$stream->writeFile($file, $buffer)) {
             $logger->getLog('filesystem')->log(sprintf(__METHOD__ . ': :write(%1$s): %2$s', $file, $stream->getError()), Zend_Log::WARN);
             return false;
         }
         return true;
     } else {
         $file = App_Filesystem_Path::clean($file);
         return is_int(file_put_contents($file, $buffer)) ? true : false;
     }
 }