/** * Uploads a file if we have a valid upload * * @param Jelly $model * @param mixed $value * @param bool $loaded * @return string|NULL */ public function save($model, $value, $loaded) { $original = $model->get($this->name, FALSE); // Upload a file? if (is_array($value) and upload::valid($value)) { if (FALSE !== ($filename = upload::save($value, NULL, $this->path))) { // Chop off the original path $value = str_replace($this->path, '', $filename); // Ensure we have no leading slash if (is_string($value)) { $value = trim($value, '/'); } // Delete the old file if we need to if ($this->delete_old_file and $original != $this->default) { $path = $this->path . $original; if (file_exists($path)) { unlink($path); } } } else { $value = $this->default; } } return $value; }
public function attach_uploaded_file($field, $upload) { if (!upload::valid($upload)) { throw new Kohana_User_Exception('Not A Valid Upload', 'The supplied array did not specify a valid uploaded file'); } $this->remove_file($field); $this->files_pending_attachment[$field] = array($upload, 'upload'); }
protected function _save_img_bg($image) { if (!upload::valid($image) or !upload::type($image, array('jpg', 'jpeg', 'png', 'gif'))) { $this->session->set_flash('error_msg', 'Only upload file jpg , jpeg , png , gif'); url::redirect('admin_config'); } else { $directory = DOCROOT . 'themes/client/styleSIC/index/pics/'; if ($file = upload::save($image, NULL, $directory)) { $filename = 'bg_' . md5(rand(0, 999)) . time() . '.png'; Image::factory($file)->save($directory . $filename); // Delete the temporary file unlink($file); return $filename; } return FALSE; } }
/** * Uploads a file if we have a valid upload * * @param Jelly $model * @param mixed $value * @param bool $loaded * @return string|NULL */ public function save($model, $value, $loaded) { // Upload a file? if (is_array($value) and upload::valid($value)) { if (FALSE !== ($filename = upload::save($value, NULL, $this->path))) { // Chop off the original path $value = str_replace(realpath($this->path) . DIRECTORY_SEPARATOR, '', $filename); // Ensure we have no leading slash if (is_string($value)) { $value = trim($value, DIRECTORY_SEPARATOR); } } else { $value = $this->default; } } return $value; }
public function upload() { if (!$_FILES) { return; } $surfix = Kohana::config('torn')->surfix->temp; $surfix_len = utf8::strlen($surfix); foreach ($_POST as $key => $tmp_name) { if (utf8::substr($key, -$surfix_len) == $surfix) { $field = utf8::substr($key, 0, -$surfix_len); $this->parent->model->set($field, $tmp_name); } } $cache = Cache::instance(); foreach ($_FILES as $key => $upload) { $this->parent->model->set($key, $upload); if (!isset($this->parent->fields[$key]) or !$this->parent->fields[$key] instanceof Torn_Field_File) { continue; } if (upload::not_empty($upload) and upload::valid($upload)) { $seed = Arr::get($_POST, '__SEED__', md5(Request::current()->uri() . time())); $tmp_name = $seed . '-' . md5_file($upload['tmp_name']); if (upload::save($upload, $tmp_name, Kohana::$cache_dir) !== FALSE) { $timestamp = 24 * 60 * 60; $cache->set($tmp_name, array('upload' => $upload, 'timestamp' => $timestamp), $timestamp); $tmp_old_file = Arr::get($_POST, $key . $surfix); if (!empty($tmp_old_file) and file_exists(Kohana::$cache_dir . DIRECTORY_SEPARATOR . $tmp_old_file)) { try { unlink(Kohana::$cache_dir . DIRECTORY_SEPARATOR . $tmp_old_file); $cache->delete($tmp_old_file); } catch (Exception $e) { } } $this->parent->model->set($key, $tmp_name); } } } }
protected function _save_img_s3($image) { require Kohana::find_file('views/aws', 'init'); if (!upload::valid($image) or !upload::type($image, array('jpg', 'jpeg', 'png', 'gif'))) { $this->session->set_flash('error_msg', 'Only upload file jpg , jpeg , png , gif'); ///url::redirect('admin_config'); //die(); return FALSE; } else { $name = $image['name']; $size = $image['size']; $tmp = $image['tmp_name']; $actual_image_name = 'ans_' . md5($image['name'] . time()) . "." . 'png'; try { // Upload data. $result = $s3Client->putObject(array('Bucket' => $s3_bucket, 'Key' => 'answer/' . $actual_image_name, 'SourceFile' => $this->s3_resize($tmp, 593, 539), 'ACL' => 'public-read', 'ContentType' => 'image/png')); return $actual_image_name; } catch (S3Exception $e) { return FALSE; } return FALSE; } }
protected function _save_img_courses($image, $hd_id = '') { if (!upload::valid($image) or !upload::type($image, array('jpg', 'jpeg', 'png', 'gif'))) { $this->session->set_flash('error_msg', 'Only upload file jpg , jpeg , png , gif'); if ($hd_id) { url::redirect('admin_test/edit/' . $hd_id); } else { url::redirect('admin_test/create'); } } else { $directory = DOCROOT . 'uploads/courses_img/'; if ($file = upload::save($image, NULL, $directory)) { $filename = md5(rand(0, 999)) . '.png'; Image::factory($file)->resize(200, 200, Image::AUTO)->save($directory . $filename); // Delete the temporary file unlink($file); return $filename; } return FALSE; } }
/** * Add Edit decayimage */ public function index() { // The default decayimage thumb file name $default_decayimage_thumb = 'Question_icon_thumb.png'; $this->template->content = new View('decayimage/settings'); $this->template->content->title = Kohana::lang('decayimage.decayimage'); plugin::add_stylesheet('decayimage/css/decayimage'); // Setup and initialize form field names $form = array('action' => '', 'decayimage_id' => '', 'decayimage_image' => '', 'decayimage_file' => '', 'decayimage_thumb' => '', 'category_id' => ''); // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names $errors = $form; $form_error = FALSE; $form_saved = FALSE; $form_action = ""; $parents_array = array(); // Check, has the form been submitted, if so, setup validation if ($_POST) { $post = new Validation($_POST); $post->pre_filter('trim'); $post->add_callbacks('category_id', array($this, '_is_valid_category')); // if we have an action == 'a' but and a decayimage_id then what we really // mean is to perform and edit if ($post->action == 'a' && isset($post->category_id)) { $post->add_rules('category_id', 'required', 'numeric'); if ($post->validate() && ($decayimage = ORM::factory('decayimage')->where('category_id', $post->category_id)->find()) && $decayimage->loaded) { $post->decayimage_id = $decayimage->id; $post->action = 'e'; } } // Check for action if ($post->action == 'a') { // Create a new decayimage row $decayimage = new Decayimage_Model($post->decayimage_id); // Handle the case where we recieve new files if (upload::valid($_FILES['decayimage_file']) && strlen($_FILES['decayimage_file']['name']) && ($_FILES = Validation::factory($_FILES)->add_rules('decayimage_file', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[50K]')) && $_FILES->validate() && $post->validate()) { // Upload the file and create a thumb $modified_files = $this->_handle_new_decayimage_fileupload(0); if (!$modified_files) { $form_saved = false; $form_error = TRUE; $post->add_error('decayimage', Kohana::lang('decayimage.cant_upload_file')); } else { $decayimage->decayimage_image = $modified_files[0]; $decayimage->decayimage_thumb = $modified_files[1]; // Update the relevant decayimage from the db $decayimage->category_id = $post->category_id; $decayimage->save(); $form_saved = TRUE; $form_action = Kohana::lang('decayimage.added'); } } else { if ($post->add_rules('decayimage_thumb', 'required', 'length[5,255]') && $post->add_callbacks('decayimage_thumb', array($this, '_is_valid_decayimage_thumb')) && $post->validate()) { // Upload the file and create a thumb $decayimage->decayimage_thumb = $post->decayimage_thumb; // Update the relevant decayimage from the db $decayimage->category_id = $post->category_id; $decayimage->save(); $form_saved = TRUE; $form_action = Kohana::lang('decayimage.added'); } else { // There was an error in validation $form_error = TRUE; $form = arr::overwrite($form, $post->as_array()); $errors = arr::overwrite($errors, $post->errors('decayimage')); } } } elseif ($post->action == 'e') { // Setup validation for new $_FILES if (upload::valid($_FILES['decayimage_file']) && strlen($_FILES['decayimage_file']['name'])) { $_FILES = Validation::factory($_FILES)->add_rules('decayimage_file', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[50K]'); } else { $post->add_rules('decayimage_thumb', 'required', 'length[5,255]'); $post->add_callbacks('decayimage_thumb', array($this, '_is_valid_decayimage_thumb')); } // Validate all input $post->add_rules('decayimage_id', 'required', 'numeric'); $post->add_callbacks('decayimage_id', array($this, '_is_valid_decayimage_id')); if ($post->validate()) { // Get the relevant decayimage from the db $decayimage = new Decayimage_Model($post->decayimage_id); // If a file was uploaded we will need to convert it to an apropriate icon size if (upload::valid($_FILES['decayimage_file']) && strlen($_FILES['decayimage_file']['name']) && $_FILES->validate()) { $modified_files = $this->_handle_new_decayimage_fileupload($post->decayimage_id); if (!$modified_files) { $form_saved = false; $form_error = TRUE; $post->add_error('decayimage', Kohana::lang('decayimage.cant_upload_file')); } else { $decayimage->decayimage_image = $modified_files[0]; $decayimage->decayimage_thumb = $modified_files[1]; } } else { $decayimage->decayimage_thumb = $post->decayimage_thumb; } // Update the relevant decayimage from the db $decayimage->category_id = $post->category_id; $decayimage->save(); $form_saved = TRUE; $form_action = Kohana::lang('decayimage.updated'); } else { // There were errors $form_error = TRUE; } } elseif ($post->action == 'd') { // TODO: https://github.com/March-hare/decayimage/issues/3 // Make sure its not the Default entry $post->add_rules('decayimage_id', 'required', 'numeric'); if ($post->validate()) { $decayimage = ORM::factory('decayimage', $post->decayimage_id); if ($decayimage->decayimage_image != 'Question_icon.png') { $decayimage->delete(); } else { $form_error = TRUE; $post->add_error('decayimage', Kohana::lang('decayimage.cant_del_default')); } } else { $form_error = TRUE; } } elseif ($post->action == 'r') { // TODO: Revert to default decayimage action $decayimage = ORM::factory('decayimage')->where('category_id', 0)->find(); $decayimage->decayimage_image = 'Question_icon.png'; $decayimage->decayimage_thumb = 'Question_icon_thumb.png'; $decayimage->save(); } if ($form_error) { $form = arr::overwrite($form, $post->as_array()); $errors = arr::overwrite($errors, $post->errors('decayimage')); } } //get array of categories $categories = ORM::factory("category")->where("category_visible", "1")->find_all(); $cat_array[0] = Kohana::lang('decayimage.default_incident_icon'); foreach ($categories as $category) { $cat_array[$category->id] = $category->category_title; } //get array of decay images $decayimages = ORM::factory("decayimage")->find_all(); $decayimage_array = array(); foreach ($decayimages as $decayimage) { $decayimage_array[$decayimage->decayimage_thumb] = $decayimage->decayimage_thumb; } $this->template->content->form_action = $form_action; $this->template->content->errors = $errors; $this->template->content->cat_array = $cat_array; $this->template->content->decayimage_array = $decayimage_array; $this->template->content->url_site = url::site(); $this->template->content->default_decayimage_thumb = $default_decayimage_thumb; $this->template->content->decayimages = $decayimages; $this->template->content->form_error = $form_error; $this->template->content->form_saved = $form_saved; $this->template->js = new View('decayimage/settings_js'); $this->template->js->default_decayimage_thumb = $default_decayimage_thumb; }