Ejemplo n.º 1
0
 private function _test_delimiters()
 {
     $file_options = global_file_options();
     $delimiter = $file_options->path_delimiter;
     $this->_check_equal(false, begins_with_delimiter('path/to/the/folder/'));
     $this->_check_equal(true, begins_with_delimiter($delimiter . 'path/to/the/folder/'));
     $this->_check_equal(true, ends_with_delimiter('path/to/the/folder' . $file_options->path_delimiter));
     $this->_check_equal(false, ends_with_delimiter('path/to/the/folder'));
     $this->_check_equal(true, begins_with_delimiter(ensure_begins_with_delimiter('path/to/the/folder/')));
     $this->_check_equal(true, begins_with_delimiter(ensure_begins_with_delimiter('/path/to/the/folder/')));
     $this->_check_equal(true, ends_with_delimiter(ensure_ends_with_delimiter('path/to/the/folder/')));
     $this->_check_equal(true, ends_with_delimiter(ensure_ends_with_delimiter('path/to/the/folder')));
 }
Ejemplo n.º 2
0
 /**
  * Move the file to the new location.
  * @param string $path
  * @param string $options Can be {@link Uploaded_file_unique_name} or {@link Uploaded_file_overwrite}.
  */
 public function move_to($path, $options = Uploaded_file_unique_name)
 {
     $final_name = $this->normalized_name;
     if ($options == Uploaded_file_unique_name || $this->current_name() != $path . $final_name) {
         if ($options == Uploaded_file_unique_name) {
             while (file_exists($path . $final_name)) {
                 $url = new FILE_URL($final_name);
                 $url->append_to_name('_' . uniqid(rand()));
                 $final_name = $url->as_text();
             }
         }
         ensure_path_exists($path);
         if (!file_exists($path)) {
             $this->raise("Could not create [{$path}] on server.", 'move_to', 'UPLOADED_FILE');
         } else {
             /* If the file has already been moved, use the normal move function to place it in the
                new directory. */
             if (isset($this->_final_name_and_path)) {
                 if ($options == Uploaded_file_overwrite && file_exists($path . $final_name)) {
                     unlink($path . $final_name);
                 }
                 rename($this->_final_name_and_path, $path . $final_name);
                 $this->processed = true;
             } else {
                 if (move_uploaded_file($this->temp_name, $path . $final_name)) {
                     $this->_final_name_and_path = $path . $final_name;
                     $opts = global_file_options();
                     chmod($this->_final_name_and_path, $opts->default_access_mode);
                     $this->processed = true;
                 }
             }
             if (!file_exists($path . $final_name)) {
                 $this->raise("Could not move [" . $this->current_name() . "] to [{$path}{$final_name}]", 'move_to', 'UPLOADED_FILE');
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Default options for this object.
  * @return FILE_OPTIONS
  */
 public function options()
 {
     return global_file_options();
 }
Ejemplo n.º 4
0
 public function __construct()
 {
     $this->date_time_toolkit = global_date_time_toolkit();
     $this->file_options = global_file_options();
     $this->url_options = global_url_options();
     $this->logs = new LOGGER_CONTAINER();
     parent::__construct();
     $this->auto_detect_os();
     /* server-local paths */
     $this->set_path(Folder_name_system_temp, temp_folder());
     $this->set_forced_root(Folder_name_system_temp, false);
     $this->set_path(Folder_name_logs, '/var/log');
     $this->set_forced_root(Folder_name_logs, false);
     /* URLs */
     $this->set_path(Folder_name_root, '/');
     $this->set_path(Folder_name_resources, '{' . Folder_name_root . '}');
     $this->set_path(Folder_name_apps, '{' . Folder_name_root . '}');
     $this->set_path(Folder_name_data, '{' . Folder_name_root . '}data');
     $this->set_path(Folder_name_pages, '{' . Folder_name_resources . '}');
     $this->set_path(Folder_name_functions, '{' . Folder_name_pages . '}');
     $this->set_path(Folder_name_icons, '{' . Folder_name_resources . '}icons');
     $this->set_path(Folder_name_styles, '{' . Folder_name_resources . '}styles');
     $this->set_path(Folder_name_themes, '{' . Folder_name_styles . '}themes');
     $this->set_path(Folder_name_scripts, '{' . Folder_name_resources . '}scripts');
     $this->set_extension(Folder_name_themes, 'css');
     $this->set_extension(Folder_name_styles, 'css');
     $this->set_extension(Folder_name_scripts, 'js');
     $this->set_extension(Folder_name_icons, 'png');
     /* Set up the path to the library. */
     $url = new FILE_URL(realpath(__FILE__));
     $url->strip_name();
     $url->go_back();
     $url->go_back();
     $this->library_path = $url->as_text();
 }
Ejemplo n.º 5
0
 /**
  * Save loaded image data to the given file name.
  * @param string $name Full path to the image file.
  * @param integer $type PHP image type constant specifying the type of image to create in that file.
  */
 public function save_to_file($name, $type = null)
 {
     if ($this->loaded() && !isset($type)) {
         $type = $this->properties->php_type;
     }
     $this->assert($this->saveable_to($type), 'Saving images to type [' . $this->properties->mime_type . '/' . $this->properties->php_type . '] is not supported.', 'save_to_file', 'IMAGE');
     $url = new FILE_URL($name);
     $url->ensure_path_exists();
     if (!isset($type)) {
         $type = $this->properties->php_type;
     }
     $opts = global_file_options();
     switch ($type) {
         case Image_type_GIF:
             imagegif($this->_data, $name);
             chmod($name, $opts->default_access_mode);
         case Image_type_JPG:
             imagejpeg($this->_data, $name);
             chmod($name, $opts->default_access_mode);
             break;
         case Image_type_PNG:
             imagepng($this->_data, $name);
             chmod($name, $opts->default_access_mode);
             break;
         default:
             $this->raise('saveable_to() claimed for (' . $this->properties->mime_type . '/' . $this->properties->php_type . ')', 'save_from_file', 'IMAGE');
     }
 }
Ejemplo n.º 6
0
 /**
  * Execute the 'file_callback' for each file entry.
  * The zip file is automatically closed when complete (the iterator returned by PHP is one-way).
  * @param WEBCORE_CALLBACK $file_callback Function prototype: function ({@link COMPRESSED_FILE} $archive, {@link COMPRESSED_FILE_ENTRY} $entry, {@link CALLBACK} $error_callback = null)
  * @param WEBCORE_CALLBACK $error_callback Function prototype: function ({@link COMPRESSED_FILE} $archive, string $msg, {@link COMPRESSED_FILE_ENTRY} $entry)
  */
 protected function _for_each($file_callback, $error_callback = null)
 {
     $opts = global_file_options();
     $file_num = 0;
     while ($zip_entry = zip_read($this->_handle)) {
         $size = zip_entry_filesize($zip_entry);
         if ($size > 0) {
             $file_num += 1;
             $entry = new ZIP_ENTRY($this, $this->_handle, $zip_entry, $opts);
             $entry->name = zip_entry_name($zip_entry);
             $entry->normalized_name = normalize_path($entry->name, $opts);
             $entry->number = $file_num;
             $entry->size = $size;
             $entry->compressed_size = zip_entry_compressedsize($zip_entry);
             $file_callback->execute(array($this, $entry, $error_callback));
         }
     }
     $this->close();
 }
Ejemplo n.º 7
0
/**
 * Return the list of files for the given path.
 * @param string $base_path Must be a full path.
 * @param string $path_to_prepend Prepended to each file.
 * @param bool $recurse If true, include all sub-folders.
 * @param FILE_OPTIONS $opts
 * @return string[]
 */
function file_list_for($base_path, $path_to_prepend = '', $recurse = false, $opts = NULL)
{
    if (!isset($opts)) {
        $opts = global_file_options();
    }
    $base_path = ensure_ends_with_delimiter($base_path, $opts);
    $Result = array();
    if ($handle = @opendir($base_path)) {
        while (($name = readdir($handle)) != false) {
            if ($name[0] != '.') {
                if (is_dir(join_paths($base_path, $name))) {
                    if ($recurse) {
                        $Result = array_merge($Result, file_list_for(join_paths($base_path, $name, $opts), join_paths($path_to_prepend, $name, $opts), $recurse, $opts));
                    }
                } else {
                    $Result[] = join_paths($path_to_prepend, $name);
                }
            }
        }
        closedir($handle);
    }
    return $Result;
}