/** * Record to this file. * Does not open the logger if not already open. Directory must exist. * @param string $name */ public function set_file_name($fn) { if ($this->_file_name != $fn) { if ($this->is_open()) { $this->close(); } $url = new FILE_URL($fn); $url->ensure_file_exists(); $this->_file_handle = @fopen($fn, 'a'); if ($this->is_open()) { $this->_file_name = $fn; $this->_has_messages = false; } } }
protected function _process_for($name, $filename) { $this->_log("Processing [{$filename}] for [{$name}]", Msg_type_info); $this_url = new FILE_URL(__FILE__); $this_url->append_folder('data'); $this_url->append_folder('images'); $this_url->replace_name_and_extension($filename); if ($this->_use_internal_exif) { $image = new IMAGE(); } else { $image = new NON_INTERNAL_EXIF_IMAGE(); } $image->set_file($this_url->as_text(), true); if ($image->properties->time_created->is_valid()) { $this->_log('Extracted date/time: ' . $image->properties->time_created->as_iso(), Msg_type_info); } else { $this->_log('Could not extract date/time.', Msg_type_warning); } }
protected function _execute() { $this->_temp_path = $this->context->resolve_path_for_alias(Folder_name_system_temp, 'webcore-tests'); $this_url = new FILE_URL(__FILE__); $this_url->append_folder('data'); $this_url->append_folder('archives'); $this_url->replace_name_and_extension('oz_pics.zip'); $archive = new ARCHIVE($this_url->as_text()); $archive->for_each(new CALLBACK_METHOD('process_file', $this), new CALLBACK_METHOD('show_error', $this)); $zip = new ZIP_FILE($this_url->as_text()); $zip->open(); $zip->for_each(new CALLBACK_METHOD('process_file', $this), new CALLBACK_METHOD('show_error', $this)); $archive->extract_to($this->_temp_path, new CALLBACK_METHOD('show_error', $this)); }
/** * Apply post-validation operations to the object. * @param object $obj Object being validated. * @access private */ protected function _prepare_for_commit($obj) { parent::_prepare_for_commit($obj); /* Move the file to its final location and set the normalized file name. */ $file = $this->upload_file_for('file_name'); if (isset($file)) { $obj->file_name = $file->normalized_name; $url = new FILE_URL($obj->full_file_name()); /** @var UPLOAD_FILE_FIELD $file_name_field */ $file_name_field = $this->field_at('file_name'); $this->_move_uploaded_file($file_name_field, $file, $url->path()); } /* Create the thumbnail if needed. */ if ($obj->is_image && $this->value_for('create_thumbnail')) { $class_name = $this->app->final_class_name('THUMBNAIL_CREATOR', 'webcore/util/image.php'); /** @var THUMBNAIL_CREATOR $creator */ $creator = new $class_name($this->app); $creator->create_thumbnail_for($obj->full_file_name(), $this->value_for('thumbnail_size')); if ($creator->error_message) { $this->record_error('create_thumbnail', $creator->error_message); } } }
/** * 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'); } } } }
/** * File type icon. * Retrieves file type to icon mappings from the {@link APPLICATION::file_type_manager()}. * @param string $size The size of the icon to return. * @return string */ public function icon_as_html($size = One_hundred_px) { $ft = $this->app->file_type_manager(); $url = new FILE_URL($this->file_name); return $ft->icon_as_html($this->mime_type, $url->extension(), $size); }
/** * Full path to the install location of this application. * @return FILE_URL */ public function source_path() { $Result = new FILE_URL(realpath($this->_source_path())); $Result->strip_name(); $Result->go_back(); return $Result; }
/** * Draw information for a file as HTML. * Passed as a {@link CALLBACK} if the attachment is an {@link ARCHIVE}. * @param ARCHIVE $archive * @param COMPRESSED_FILE_ENTRY $entry * @param WEBCORE_CALLBACK $error_callback Function prototype: function ({@link COMPRESSED_FILE} $archive, string $msg, {@link COMPRESSED_FILE_ENTRY} $entry) * @access private */ public function list_file_as_html($archive, $entry, $error_callback = null) { $ft = $this->context->file_type_manager(); $url = new FILE_URL($entry->name); $icon_with_text = $this->context->get_icon_with_text($ft->icon_url('', $url->extension()), Sixteen_px, $entry->name); echo '<tr><td>' . $icon_with_text . '</td><td>' . file_size_as_text($entry->size) . '</td></tr>'; }
/** * Returns the thumbnail file name. * @param string $file_name * @return string * @access private */ protected function _thumbnail_name_for($file_name) { $url = new FILE_URL($file_name); $url->append_to_name('_tn'); return $url->as_text(); }
/** * Copy the object to the specified folder. * If both the source and target albums are {@link Album_location_type_local}, * and they are different folders, copy the pictures to the new folder. * @param ALBUM $folder * @param FOLDER_OPERATION_OPTIONS $options */ protected function _copy_to($folder, $options) { if ($options->update_now) { /** @var ALBUM $parent */ $parent = $this->parent_folder(); $old_location = $parent->location; $old_folder = url_to_folder($parent->picture_folder_url(true)); } parent::_copy_to($folder, $options); if ($options->update_now) { if ($old_location == Album_location_type_local && $folder->location == Album_location_type_local) { $new_folder = url_to_folder($folder->picture_folder_url(true)); if ($old_folder != $new_folder) { $old_url = new FILE_URL($old_folder); $old_url->replace_name($this->file_name); if ($old_url->extension() == '') { $old_url->replace_extension('jpg'); } $new_url = new FILE_URL($new_folder); $new_url->replace_name($this->file_name); if ($new_url->extension() == '') { $new_url->replace_extension('jpg'); } if (file_exists($old_url->as_text())) { ensure_path_exists($new_folder); log_message('Copied [' . $old_url->as_text() . '] to [' . $new_url->as_text() . ']', Msg_type_debug_info, Msg_channel_system); if (!copy($old_url->as_text(), $new_url->as_text())) { $this->raise('_copy_to', 'PICTURE', 'Could not copy main image for [' . $this->title_as_plain_text() . '].'); } } $old_url->append_to_name(Picture_thumbnail_suffix); $new_url->append_to_name(Picture_thumbnail_suffix); if (file_exists($old_url->as_text())) { ensure_path_exists($new_folder); log_message('Copied [' . $old_url->as_text() . '] to [' . $new_url->as_text() . ']', Msg_type_debug_info, Msg_channel_system); if (!copy($old_url->as_text(), $new_url->as_text())) { $this->raise('_copy_to', 'PICTURE', 'Could not copy thumbnail image for [' . $this->title_as_plain_text() . '].'); } } } } } }
/** * Extract the file to the given location. * If there is an error in the extraction, it is communicated to the optional 'error_callback' * instead of raising an exception (which stops the script). * @param string $path * @param WEBCORE_CALLBACK $error_callback */ public function extract_to($path, $error_callback = null) { $url = new FILE_URL($path); $url->append($this->normalized_name); ensure_path_exists($url->path()); $this->extracted_name = $url->as_text(); $this->_extract_to($this->extracted_name, $error_callback); }
/** * Return HTML for a file upload control. * If the file has already been uploaded and processed, the name is shown instead of a control * so that the user doesn't have to upload again if there were validation errors elsewhere. * @param string $id Name of field. * @param FORM_TEXT_CONTROL_OPTIONS $options * @return string */ public function file_as_html($id, $options = null) { /** @var UPLOAD_FILE_FIELD $field */ $field = $this->_field_at($id); if (!isset($options)) { $options = clone default_text_options(); } if (!isset($this->_num_controls[$id])) { $this->_num_controls[$id] = 0; } if ($field->is_processed($this->_num_controls[$id])) { $file = $field->file_at($this->_num_controls[$id]); $ft = $this->context->file_type_manager(); $url = new FILE_URL($file->name); $icon = $ft->icon_as_html($file->mime_type, $url->extension(), Sixteen_px); // TODO Wrap in an .input class container $Result = '<div class="detail">' . $icon . ' ' . $file->name . ' (' . file_size_as_text($file->size) . ")</div>"; $file_info = $file->store_to_text($id); $uploader = $this->_form->uploader(); $Result .= '<input type="hidden" name="' . $uploader->stored_info_name . '[]" value="' . $file_info . "\">\n"; if ($field->description) { $Result .= '<div class="description">' . $field->description . "</div>"; } } else { $max_size = $this->_form->max_upload_file_size(); if ($field->max_bytes) { $max_size = min($field->max_bytes, $max_size); } $desc = 'Maximum file size is ' . file_size_as_text($max_size) . '. '; $saved_desc = $options->extra_description; $options->extra_description .= $desc; $Result = $this->_text_line_as_html($id, 'file', $options); $options->extra_description = $saved_desc; } $this->_num_controls[$id] += 1; return $Result; }
/** * Return the server system temp directory. * @return string */ function temp_folder() { $name = tempnam('', 'WebCore'); unlink($name); $url = new FILE_URL($name); $url->strip_name(); return $url->as_text(); }
/** * Called once for each image in the archive. * @param COMPRESSED_FILE $archive * @param COMPRESSED_FILE_ENTRY $entry * @param WEBCORE_CALLBACK $error_callback * @access private */ public function process_image($archive, $entry, $error_callback) { log_open_block("Extracting [{$entry->name}]..."); $entry->extract_to($this->_target_directory, $error_callback); $class_name = $this->app->final_class_name('IMAGE', 'webcore/util/image.php'); /** @var IMAGE $img */ $img = new $class_name(); $img->set_file($entry->extracted_name, $this->read_exif); if ($img->loadable()) { $img->load_from_file(); $url = new FILE_URL($entry->extracted_name); if ($this->create_thumbnail) { if ($img->saveable()) { $url->append_to_name('_tn'); $thumbnail_name = $url->as_text(); $this->_log("Creating thumbnail...", Msg_type_info); $img->resize_to_fit($this->thumbnail_size, $this->thumbnail_size); $img->save_to_file($thumbnail_name); } else { $this->_log("Could not create thumbnail.", Msg_type_error); } } $pic_date = $this->default_date; if ($this->read_exif) { if ($img->properties->exists() && $img->properties->time_created->is_valid()) { $pic_date = $img->properties->time_created; } else { $this->_log("Could not read date from file (using default).", Msg_type_warning); } } $original_url = new FILE_URL($entry->name); $file_name_only = $original_url->name_without_extension(); $pic_title = $this->file_name_template; $pic_title = str_replace('{#}', $this->_num_pictures_imported + $this->starting_index, $pic_title); $pic_title = str_replace('{file}', $file_name_only, $pic_title); $pic = $this->_folder->new_object('picture'); $pic->title = $pic_title; $pic->file_name = $entry->normalized_name; $pic->date = $pic_date; $history_item = $pic->new_history_item(); $pic->store_if_different($history_item); $this->_num_pictures_imported = $this->_num_pictures_imported + 1; $this->_log("Created picture [{$pic->title}]", Msg_type_info); } else { $php_errormsg = null; @unlink($entry->extracted_name); if (isset($php_errormsg)) { $this->_log("This is not an image file (could not delete file on server): " . $php_errormsg, Msg_type_error); } else { $this->_log("This is not an image file (file was deleted on the server).", Msg_type_warning); } } log_close_block(); }