public function action_index() { $value = $_FILES['upload']; if (is_array($value) and Ku_Upload::valid($value) and Ku_Upload::not_empty($value)) { $md5 = md5($value['name']); $save_path = DOCROOT . 'upload' . DIRECTORY_SEPARATOR . 'editor' . DIRECTORY_SEPARATOR . str_pad($this->site_id, 2, '0', STR_PAD_LEFT) . DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . substr($md5, 0, 2) . DIRECTORY_SEPARATOR . substr($md5, 2, 2) . DIRECTORY_SEPARATOR; Ku_Dir::make_writable($save_path); $filename = Ku_File::safe_name($value['name'], TRUE, $this->max_filename_length); $prefix = uniqid() . '_'; while (file_exists($save_path . $prefix . $filename)) { $prefix = uniqid() . '_'; } $filename = Ku_Upload::save($value, $prefix . $filename, $save_path); $filename = 'upload' . str_replace(array(realpath(DOCROOT . 'upload'), DIRECTORY_SEPARATOR), array('', '/'), $filename); if (!$filename) { Kohana::$log->add(Log::ERROR, 'Exception occurred: :exception. [:file][:line] ', array(':file' => Debug::path(__FILE__), ':line' => __LINE__, ':exception' => 'File not saved')); } echo str_replace(array('{FUNCTION}', '{SRC}'), array(Request::initial()->query('CKEditorFuncNum'), URL::base() . $filename), $this->template); } }
private function write_to_file($file_name, $str) { $file_name = str_replace('/', DIRECTORY_SEPARATOR, $file_name); if (strpos($file_name, DOCROOT) !== 0) { $file_name = DOCROOT . $file_name; } $dirname = dirname($file_name); if (!file_exists($dirname)) { Ku_Dir::make($dirname); } Ku_Dir::make_writable($dirname); $handle = fopen($file_name, 'w'); fwrite($handle, $str); fclose($handle); }
/** * Generate thumb. * * @param string group of thumb config * @param string path to source file * @param boolean force process even if the thumb file already exists * @return string|boolean Realpath of created file or FALSE if failure */ public static function create($group, $file, $force = FALSE) { if (empty($file)) { return FALSE; } $file = str_replace('/', DIRECTORY_SEPARATOR, $file); if (strpos($file, '.' . DIRECTORY_SEPARATOR) !== FALSE) { // File is invalid: "./" and "../" not allowed return FALSE; } self::$_config === NULL and self::_load_config(); self::$_route_tpl === NULL and self::_set_route_tpl(); $config = self::$_config->get($group); if (!$config) { return FALSE; } // Detect realpath for base group path $path = rtrim(Arr::get($config, 'path', ''), '/'); if (!$path) { $realpath = realpath(self::$docroot); } else { if ($realpath = realpath($path)) { // Path finded } else { if ($realpath = realpath(self::$docroot . $path)) { // Path finded } else { if ($realpath = realpath(DOCROOT . $path)) { // Path finded } else { // Path not exists return FALSE; } } } } $path = $realpath; // Detect realpath for src file if ($realpath = realpath($file)) { // File finded } else { if ($realpath = realpath(self::$docroot . $file)) { // File finded } else { if ($realpath = realpath(DOCROOT . $file)) { // File finded } else { // File not found return FALSE; } } } $file = $realpath; if (is_file($file) and is_dir($path) and strpos($file, $path) === 0) { $thumb_path = self::$docroot . sprintf(self::$_route_tpl, $group, str_replace($path, '', $file)); $thumb_path = str_replace('/', DIRECTORY_SEPARATOR, $thumb_path); if ($force === TRUE or !is_file($thumb_path)) { $img = Image::factory($file); foreach ($config as $key => $params) { switch ($key) { case 'resize': $params += array('width' => NULL, 'height' => NULL, 'master' => NULL); $img->resize($params['width'], $params['height'], $params['master']); break; case 'crop': $params += array('width' => NULL, 'height' => NULL, 'offset_x' => NULL, 'offset_y' => NULL); $img->crop($params['width'], $params['height'], $params['offset_x'], $params['offset_y']); break; default: if (is_callable($key)) { call_user_func($key, $img, $params); } } } // Make directory writable Ku_Dir::make_writable(dirname($thumb_path)); // Save file by requsted path if ($img->save($thumb_path, Arr::get($config, 'quality', 90)) === FALSE) { return FALSE; } } return realpath($thumb_path); } return FALSE; }
/** * Saves file and returns file name * * @param string $field File field name * @param mixed $value File field value * @return string */ public function file_save($field, $value) { $this->_check_file_field($field); $config = $this->_file_fields[$field]; $base_path = $this->file_path($field, ''); // Upload a file? if (is_array($value) and Ku_Upload::valid($value) and Ku_Upload::not_empty($value)) { // Get path to save file $sub_dir = $this->file_sub_dir($field, $value['name']); $save_path = $base_path . $sub_dir; // Create and make directory writable Ku_Dir::make_writable($base_path . $sub_dir, $config['dir_chmod']); // Generate safe filename $filename = Ku_File::safe_name($value['name'], TRUE, $config['max_filename_length']); $prefix = ''; if ($config['force_unique_prefix']) { // Make unique filename $prefix = uniqid() . '_'; } while (file_exists($save_path . $prefix . $filename)) { // Make unique filename to prevent override existing file $prefix = uniqid() . '_'; } $filename = $prefix . $filename; $filename = Ku_Upload::save($value, $filename, $save_path, $config['file_chmod']); if (!$filename) { throw new Kohana_Exception('File :filename not saved to a field :field of model :model', array(':filename' => $value['name'], ':field' => $field, ':model' => $this->_orm->object_name())); } } elseif (is_string($value) and is_file($value)) { // Test allowed source directories if (!is_array($config['allowed_src_dirs']) or empty($config['allowed_src_dirs'])) { throw new Kohana_Exception('Field :field of model :model has no allowed source directories', array(':field' => $field, ':model' => $this->_orm->object_name())); } foreach ($config['allowed_src_dirs'] as $dir) { if (strpos(realpath($value), realpath($dir)) === 0) { // Allowed directory found $allowed_dir = $dir; break; } } if (!isset($allowed_dir)) { // Allowed directory not found throw new Kohana_Exception('File :filename is not in the allowed source directory of field :field of model :model', array(':filename' => Debug::path($value), ':field' => $field, ':model' => $this->_orm->object_name())); } // Get path to save file $sub_dir = $this->file_sub_dir($field, basename($value)); $save_path = $base_path . $sub_dir; // Create and make directory writable Ku_Dir::make_writable($base_path . $sub_dir, Arr::get($config, 'dir_chmod')); // Generate safe filename $filename = Ku_File::safe_name(basename($value), TRUE, $config['max_filename_length']); if ($value !== $save_path . $filename) { $prefix = ''; if ($config['force_unique_prefix']) { // Make unique filename $prefix = uniqid() . '_'; } while (file_exists($save_path . $prefix . $filename)) { // Make unique filename to prevent override existing file $prefix = uniqid() . '_'; } $filename = $prefix . $filename; if (rename($value, $save_path . $filename)) { $filename = $save_path . $filename; } else { // File not saved throw new Kohana_Exception('File :filename not saved to a field :field of model :model', array(':filename' => Debug::path($value), ':field' => $field, ':model' => $this->_orm->object_name())); } } else { $filename = $value; } } else { throw new Kohana_Exception('Invalid file parameter :value for field :field of model :model', array(':value' => (string) $value, ':field' => $field, ':model' => $this->_orm->object_name())); } if (!empty($filename)) { try { chmod($filename, $config['file_chmod']); } catch (Exception $e) { Kohana::$log->add(Log::ERROR, 'Exception occurred: :exception. [:file][:line] ', array(':file' => Debug::path(__FILE__), ':line' => __LINE__, ':exception' => $e->getMessage())); } // Save only path relative base path $save_value = $sub_dir . basename($filename); $save_value = ltrim(str_replace('\\', '/', $save_value), '/'); // Assign ORM field $this->_orm->{$field} = $save_value; } return $filename; }
<?php defined('SYSPATH') or die('No direct script access.'); $config = array('file' => array('driver' => 'file', 'cache_dir' => APPPATH . 'cache/other', 'default_expire' => 3600, 'ignore_on_delete' => array('.gitignore', '.git', '.svn')), 'struct' => array('driver' => 'file', 'cache_dir' => APPPATH . 'cache/struct', 'default_expire' => 3600, 'ignore_on_delete' => array('.gitignore', '.git', '.svn')), 'page-helper' => array('driver' => 'file', 'cache_dir' => APPPATH . 'cache/page-helper', 'default_expire' => 3600, 'ignore_on_delete' => array('.gitignore', '.git', '.svn')), 'sites' => array('driver' => 'file', 'cache_dir' => APPPATH . 'cache/sites', 'default_expire' => 3600, 'ignore_on_delete' => array('.gitignore', '.git', '.svn')), 'properties' => array('driver' => 'file', 'cache_dir' => APPPATH . 'cache/properties', 'default_expire' => 3600, 'ignore_on_delete' => array('.gitignore', '.git', '.svn'))); foreach ($config as $item) { if ($item['driver'] === 'file' and !is_dir($item['cache_dir'])) { Ku_Dir::make_writable($item['cache_dir']); } } return $config;