示例#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');
 }