/** * process any uploads * * @return void * @author Andy Bennett */ public static function upload_listener() { if (!Event::$data instanceof ORM) { throw new Exception('Model not passed to ' . __CLASS__ . '/' . __FUNCTION__); } $model = Event::$data; // do the uploading foreach ($_FILES as $file => $file_data) { if ($file_data['error'] == 0) { $udata = uploads::get_upload_data($file, $file_data); $n = str_replace('form_', '', $file); $id = (is_object($model->{$n}) and $model->{$n}->id > 0) ? $model->{$n}->id : 0; if (isset($udata['image_width']) and empty($udata['image_width'])) { unset($udata['image_width']); } if (isset($udata['image_height']) and empty($udata['image_height'])) { unset($udata['image_height']); } // if there is preview data set if (is_array($udata['preview'])) { // add the preview data into the upload table $preview = ORM::factory('upload'); foreach ($udata['preview'] as $k => $v) { if ($k == 'preview') { continue; } $preview->{$k} = $v; } $preview->parent_model = $model->object_name; $preview->parent_id = $model->id; $preview->save(); // and assign the id to the row $udata['preview_id'] = $preview->id; } unset($udata['preview']); $o = ORM::factory('upload', $id); if (!$o->loaded) { $o = ORM::factory('upload'); } foreach ($udata as $k => $v) { $o->{$k} = $v; } $o->parent_model = $model->object_name; $o->parent_id = $model->id; $o->save(); // insert the upload into the uploads database // $_POST[$file] = $o->id; $f = str_replace('form_', '', $file); $model->{$f} = $o->id; $model->save(); } } }
/** * get full information for an upload * * @param string $file * @param array $file_data * @return array * @author Andy Bennett */ function get_upload_data($file, $file_data, $save = true) { $filename = $save ? upload::save($file_data) : $file; if (!strlen($filename)) { throw new Exception("Empty filename", 1); } $pp = pathinfo($filename); $ext = strtolower($pp['extension']); $file_type = uploads::check_filetype($file_data['type'], $filename, $ext); $d = Kohana::config('upload.directory'); $upload_data['file_name'] = $pp['basename']; $upload_data['file_type'] = $file_type; $upload_data['file_path'] = $d; $upload_data['full_path'] = $filename; $upload_data['raw_name'] = $pp['filename']; $upload_data['orig_name'] = $file_data['name']; $upload_data['file_ext'] = '.' . strtolower($ext); $upload_data['file_size'] = $file_data['size']; $upload_data['is_image'] = file::is_image($file_type); $upload_data['is_video'] = 0; $upload_data['is_audio'] = 0; $upload_data['date_added'] = date('Y-m-d H:i:s'); $upload_data['preview'] = false; $driver = uploads::get_driver($upload_data['is_image'], $file_type, $ext); if ($driver !== false) { // Load the driver if (Kohana::auto_load($driver)) { // Initialize the driver $upload_driver = new $driver(); // Validate the driver if (!$upload_driver instanceof Uploader_Driver) { throw new Kohana_Exception('core.driver_implements', $driver, 'upload', 'Uploader_Driver'); } $upload_driver->generate_preview($upload_data, $filename, $ext); } } if ($upload_data['is_image']) { $properties = file::get_image_properties($filename); if (!empty($properties)) { $upload_data = array_merge($upload_data, $properties); } } return $upload_data; }
/** * Функция загрузки файлов * * @param (string) $field_name - им поля из формы загрузки файла * @param (string) $upload_dir - дарректория, в которую загружать файл * @param (array) $fileTypes - массив типов файлов, разрешенных для загрузки (по умолчанию FALSE) * * @return bool */ public function loadFile($field_name, $upload_dir, $fileTypes = false) { return uploads::uploadFile($field_name, $upload_dir, $fileTypes); }
/** * undocumented function * * @param string $path * @return void * @author Andy Bennett */ protected function render($path, $download = false, $orig_name = null) { Kohana::close_buffers(false); if (is_null($orig_name)) { $orig_name = basename($path); } $file_type = uploads::check_filetype(file::mime($path), $path); header('Content-type: ' . $file_type); if (!file::is_image($file_type) or $download) { header('Content-Disposition: attachment; filename="' . $orig_name . '"'); } header("Content-Length: " . filesize($path)); readfile($path); exit; }
/** * protected функция загружает логотип * @param (string) $field_name - им поля из формы загрузки файла * @param (string) $upload_dir - директория, в которую загружать файл * @return string (имя картинки) or false (если ф-я вернула false, можно получить ошибки из массива $arrErrors) */ protected function loadLogo($field_name, $upload_dir) { // пробуем загрузить файл if (uploads::uploadImage($field_name, $upload_dir)) { if (img::setParam(uploads::$arrUploadsSubj['file_name'], uploads::$arrUploadsSubj['upload_dir'])) { if (img::createThumbnail(100, 100)) { return true; } else { @unlink(uploads::$arrUploadsSubj['upload_dir'] . uploads::$arrUploadsSubj['file_name']); $this->arrErrors = ERROR_FILE_CREATE_THUMBNAIL; return false; } } else { $this->arrErrors = img::$arrErrors; return false; } } else { // если загрузить файл не удалось, устанавливаем ошибки $this->arrErrors = uploads::$arrErrors; return false; } }