Beispiel #1
0
 public static function get_writable_file($args, $extension = null)
 {
     if (is_string($args)) {
         $args = array('name' => $args);
     } else {
         if (!is_array($args)) {
             $args = array();
         }
     }
     $default_args = array('name' => '', 'extension' => '', 'create_new' => false, 'rename' => false, 'require_existing' => false, 'permissions' => 0644, 'path_permissions' => 0755, 'default_search_paths' => array('uploads_basedir', 'uploads_path', 'wp-content', 'abspath'), 'custom_search_paths' => array());
     $args = array_merge($default_args, $args);
     if (empty($args['name'])) {
         return new WP_Error('get_writable_file_no_name', 'The call to ITFileUtility::get_writable_file is missing the name attribute');
     }
     if (is_null($extension)) {
         $extension = $args['extension'];
     }
     if (!empty($extension) && !preg_match('/^\\./', $extension)) {
         $extension = ".{$extension}";
     }
     $base_path = ITFileUtility::get_writable_directory(array('permissions' => $args['path_permissions'], 'default_search_paths' => $args['default_search_paths'], 'custom_search_paths' => $args['custom_search_paths']));
     if (is_wp_error($base_path)) {
         return $base_path;
     }
     $name = $args['name'];
     if (preg_match('|/|', $name)) {
         $base_path .= '/' . dirname($name);
         $name = basename($name);
     }
     $file = "{$base_path}/{$name}{$extension}";
     if (is_file($file)) {
         if (true === $args['create_new']) {
             if (false === $args['rename']) {
                 return new WP_Error('get_writable_file_no_rename', 'Unable to create the named writable file');
             }
             $name = ITFileUtility::get_unique_name($base_path, $name, $extension);
             $file = "{$base_path}/{$name}";
         } else {
             if (is_writable($file)) {
                 return $file;
             }
             return new WP_Error('get_writable_file_cannot_write', 'Required file exists but is not writable');
         }
     } else {
         if (true === $args['require_existing']) {
             return new WP_Error('get_writable_file_does_not_exist', 'Required writable file does not exist');
         }
     }
     if (true === ITFileUtility::is_file_writable($file)) {
         @chmod($file, $args['permissions']);
         return $file;
     }
     return new WP_Error('get_writable_file_failed', 'Unable to create a writable file');
 }
Beispiel #2
0
 function create_zip($args = array())
 {
     if (empty($this->_path)) {
         return false;
     }
     if (is_string($args)) {
         $args = array('name' => $args);
     }
     if (!is_array($args)) {
         $args = array();
     }
     $default_args = array('file' => null, 'name' => 'temp', 'extension' => '.zip', 'disable_compression' => false, 'append_zip' => false, 'paths' => array());
     $args = array_merge($default_args, $args);
     if (!empty($args['file']) && is_file($args['file']) && !is_writable($args['file'])) {
         return new WP_Error('cannot_overwrite_existing_file', 'The requested zip file path to be used exists and cannot be overridden');
     }
     if (!empty($args['file'])) {
         if (!ITFileUtility::is_file_writable($args['file'])) {
             return new WP_Error('create_zip_file_not_writable', 'Requested zip file name is not writable');
         }
         $file = $args['file'];
     } else {
         $file = ITFileUtility::create_writable_file(array('name' => $args['name'], 'extension' => $args['extension'], 'custom_search_paths' => $args['paths']));
         if (is_wp_error($file)) {
             return $file;
         }
     }
     $result = false;
     if (true !== $this->_args['force_compatibility']) {
         $result = $this->_create_native_zip($file, $args);
     }
     if (true !== $result) {
         $result = $this->_create_compatibility_zip($file, $args);
     }
     if (is_wp_error($result)) {
         return $result;
     } else {
         if (true !== $result) {
             return new WP_Error('unknown_zip_failure', 'Zip file creation failed for an unknown reason');
         }
     }
     if (empty($args['file'])) {
         $this->_zip = $file;
     }
     return $file;
 }