Ejemplo n.º 1
0
 public function _upload_image(Validate $array, $input)
 {
     if ($array->errors()) {
         // Don't bother uploading
         return;
     }
     // Get the image from the array
     $image = $array[$input];
     if (!Upload::valid($image) or !Upload::not_empty($image)) {
         // No need to do anything right now
         return;
     }
     if (Upload::valid($image) and Upload::type($image, $this->types)) {
         $filename = strtolower(Text::random('alnum', 20)) . '.jpg';
         if ($file = Upload::save($image, NULL, $this->directory)) {
             Image::factory($file)->resize($this->width, $this->height, $this->resize)->save($this->directory . $filename);
             // Update the image filename
             $array[$input] = $filename;
             // Delete the temporary file
             unlink($file);
         } else {
             $array->error('image', 'failed');
         }
     } else {
         $array->error('image', 'valid');
     }
 }
Ejemplo n.º 2
0
 /**
  * Validation image file
  *
  * @return $this
  * @throws Exception
  */
 private function _validation()
 {
     if (!Upload::valid(Arr::get($_FILES, $this->_config['fn']))) {
         throw new Exception('Error field input name');
     }
     return Validation::factory($_FILES)->rule($this->_config['fn'], 'Upload::valid')->rule($this->_config['fn'], 'Upload::type', array(':value', array('jpg', 'jpeg', 'png', 'gif')))->rule($this->_config['fn'], 'Upload::size', array(':value', '10M'))->rule($this->_config['fn'], 'Upload::image');
 }
Ejemplo n.º 3
0
 /**
  * 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(realpath($this->path) . DIRECTORY_SEPARATOR, '', $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 = realpath($this->path) . $original;
                 if (file_exists($path)) {
                     unlink($path);
                 }
             }
         } else {
             $value = $this->default;
         }
     }
     return $value;
 }
Ejemplo n.º 4
0
 public static function validate_uploaded_image($image)
 {
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, array('jpg', 'jpeg', 'png', 'gif'))) {
         return FALSE;
     }
     return TRUE;
 }
Ejemplo n.º 5
0
 public function action_image()
 {
     if (Core::post('photo_delete') and Auth::instance()->get_user()->delete_image() == TRUE) {
         Alert::set(Alert::SUCCESS, __('Photo deleted.'));
         $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
     }
     // end of photo delete
     //get image
     $image = $_FILES['profile_image'];
     //file post
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, explode(',', core::config('image.allowed_formats'))) or !Upload::size($image, core::config('image.max_image_size') . 'M')) {
         if (Upload::not_empty($image) && !Upload::type($image, explode(',', core::config('image.allowed_formats')))) {
             Alert::set(Alert::ALERT, $image['name'] . ' ' . __('Is not valid format, please use one of this formats "jpg, jpeg, png"'));
             $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
         }
         if (!Upload::size($image, core::config('image.max_image_size') . 'M')) {
             Alert::set(Alert::ALERT, $image['name'] . ' ' . __('Is not of valid size. Size is limited on ' . core::config('general.max_image_size') . 'MB per image'));
             $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
         }
         Alert::set(Alert::ALERT, $image['name'] . ' ' . __('Image is not valid. Please try again.'));
         $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
     } else {
         if ($image != NULL) {
             $user = Auth::instance()->get_user();
             // saving/uploadng zip file to dir.
             $root = DOCROOT . 'images/users/';
             //root folder
             $image_name = $user->id_user . '.png';
             $width = core::config('image.width');
             // @TODO dynamic !?
             $height = core::config('image.height');
             // @TODO dynamic !?
             $image_quality = core::config('image.quality');
             // if folder does not exist, try to make it
             if (!is_dir($root) and !@mkdir($root, 0775, TRUE)) {
                 // mkdir not successful ?
                 Alert::set(Alert::ERROR, __('Image folder is missing and cannot be created with mkdir. Please correct to be able to upload images.'));
                 return FALSE;
                 // exit function
             }
             // save file to root folder, file, name, dir
             if ($file = Upload::save($image, $image_name, $root)) {
                 // resize uploaded image
                 Image::factory($file)->orientate()->resize($width, $height, Image::AUTO)->save($root . $image_name, $image_quality);
                 // update category info
                 $user->has_image = 1;
                 $user->last_modified = Date::unix2mysql();
                 $user->save();
                 Alert::set(Alert::SUCCESS, $image['name'] . ' ' . __('Image is uploaded.'));
             } else {
                 Alert::set(Alert::ERROR, $image['name'] . ' ' . __('Icon file could not been saved.'));
             }
             $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
         }
     }
 }
Ejemplo n.º 6
0
 /**
  * Logic to deal with uploading the image file and generating thumbnails according to
  * what has been specified in the $thumbnails array.
  *
  * @param   Jelly  $model
  * @param   mixed  $value
  * @param   bool   $loaded
  * @return  string|NULL
  */
 public function save($model, $value, $loaded)
 {
     $old_filename = $this->default;
     // 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);
             }
             // delete the old file
             $file_has_changed = $old_filename != $this->default;
             if ($file_has_changed and file_exists(realpath($this->path) . DIRECTORY_SEPARATOR . $old_filename)) {
                 unlink(realpath($this->path) . DIRECTORY_SEPARATOR . $old_filename);
             }
             // generate any thumbnails
             $source_file = $this->path . DIRECTORY_SEPARATOR . $value;
             foreach ($this->thumbnails as $thumbnail) {
                 $dest_path = realpath($thumbnail['path']) . DIRECTORY_SEPARATOR;
                 // Delete the old file
                 if ($file_has_changed and file_exists($dest_path . $old_filename)) {
                     unlink($dest_path . $old_filename);
                 }
                 $image = Image::factory($source_file);
                 if (isset($thumbnail['rotation'])) {
                     $image->rotate($thumbnail['rotation']);
                 }
                 $w = $thumbnail['dest_w'];
                 $h = $thumbnail['dest_h'];
                 switch ($thumbnail['resize_type']) {
                     case Jelly_Field_Image::RESIZE_TYPE_CROP:
                         $image->crop($w, $h);
                         break;
                     case Jelly_Field_Image::RESIZE_TYPE_FIT:
                         if ($image->width > $w || $image->height > $h) {
                             $image->resize($w, $h);
                         }
                         break;
                     case Jelly_Field_Image::RESIZE_TYPE_EXACT_FIT:
                         $image->resize($w, $h, Image::INVERSE)->crop($w, $h);
                         break;
                     case Jelly_Field_Image::RESIZE_TYPE_NONE:
                         // Do nothing - copy of the image will be saved below
                         break;
                 }
                 $image->save($dest_path . $value);
             }
         } else {
             $value = $old_filename;
         }
     }
     return $value;
 }
Ejemplo n.º 7
0
 /**
  * CRUD controller: CREATE
  */
 public function action_create()
 {
     $this->auto_render = FALSE;
     $this->template = View::factory('js');
     if (!isset($_FILES['image'])) {
         $this->template->content = json_encode('KO');
         return;
     }
     $image = $_FILES['image'];
     if (core::config('image.aws_s3_active')) {
         require_once Kohana::find_file('vendor', 'amazon-s3-php-class/S3', 'php');
         $s3 = new S3(core::config('image.aws_access_key'), core::config('image.aws_secret_key'));
     }
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, explode(',', core::config('image.allowed_formats'))) or !Upload::size($image, core::config('image.max_image_size') . 'M')) {
         if (Upload::not_empty($image) and !Upload::type($image, explode(',', core::config('image.allowed_formats')))) {
             $this->template->content = json_encode(array('msg' => $image['name'] . ' ' . sprintf(__('Is not valid format, please use one of this formats "%s"'), core::config('image.allowed_formats'))));
             return;
         }
         if (!Upload::size($image, core::config('image.max_image_size') . 'M')) {
             $this->template->content = json_encode(array('msg' => $image['name'] . ' ' . sprintf(__('Is not of valid size. Size is limited to %s MB per image'), core::config('image.max_image_size'))));
             return;
         }
         $this->template->content = json_encode(array('msg' => $image['name'] . ' ' . __('Image is not valid. Please try again.')));
         return;
     } elseif ($image != NULL) {
         // saving/uploading img file to dir.
         $path = 'images/cms/';
         $root = DOCROOT . $path;
         //root folder
         $image_name = URL::title(pathinfo($image['name'], PATHINFO_FILENAME));
         $image_name = Text::limit_chars(URL::title(pathinfo($image['name'], PATHINFO_FILENAME)), 200);
         $image_name = time() . '.' . $image_name;
         // if folder does not exist, try to make it
         if (!file_exists($root) and !@mkdir($root, 0775, true)) {
             // mkdir not successful ?
             $this->template->content = json_encode(array('msg' => __('Image folder is missing and cannot be created with mkdir. Please correct to be able to upload images.')));
             return;
             // exit function
         }
         // save file to root folder, file, name, dir
         if ($file = Upload::save($image, $image_name, $root)) {
             // put image to Amazon S3
             if (core::config('image.aws_s3_active')) {
                 $s3->putObject($s3->inputFile($file), core::config('image.aws_s3_bucket'), $path . $image_name, S3::ACL_PUBLIC_READ);
             }
             $this->template->content = json_encode(array('link' => Core::config('general.base_url') . $path . $image_name));
             return;
         } else {
             $this->template->content = json_encode(array('msg' => $image['name'] . ' ' . __('Image file could not been saved.')));
             return;
         }
         $this->template->content = json_encode(array('msg' => $image['name'] . ' ' . __('Image is not valid. Please try again.')));
     }
 }
Ejemplo n.º 8
0
 protected function _save_image($image)
 {
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, array('jpg', 'jpeg', 'png', 'gif'))) {
         return FALSE;
     }
     $directory = DOCROOT . '/public/media/image_product/';
     if ($file = Upload::save($image, NULL, $directory)) {
         $filename = strtolower(Text::random('alnum', 20)) . '.jpg';
         Image::factory($file)->resize(500, 500, Image::AUTO)->save($directory . $filename);
         // Delete the temporary file
         unlink($file);
         return $filename;
     }
     return FALSE;
 }
Ejemplo n.º 9
0
 /**
  * @return bool|string
  */
 private function save_image($image)
 {
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, array('jpg', 'jpeg', 'png', 'gif'))) {
         return FALSE;
     }
     $directory = DOCROOT . $this->prefix;
     if ($file = Upload::save($image, NULL, $directory)) {
         // Save the image.
         Image::factory($file)->resize($this->width(), $this->height())->save($directory . $this->_filename());
         // Delete the temporary file
         unlink($file);
         return TRUE;
     }
     return FALSE;
 }
Ejemplo n.º 10
0
 /**
  * Guess the type of the source:
  *
  *  - Upload_Source::TYPE_URL
  *  - Upload_Source::TYPE_STREAM
  *  - Upload_Source::TYPE_TEMP
  *  - Upload_Source::TYPE_FILE
  *  - Upload_Source::TYPE_UPLOAD
  *  - FALSE
  *
  * @param  mixed $source
  * @return string|boolean
  */
 public static function guess_type($source)
 {
     if (is_array($source)) {
         return (Upload::valid($source) and $source['error'] !== UPLOAD_ERR_NO_FILE) ? Upload_Source::TYPE_UPLOAD : FALSE;
     } elseif ($source == 'php://input') {
         return Upload_Source::TYPE_STREAM;
     } elseif (Valid::url($source)) {
         return Upload_Source::TYPE_URL;
     } elseif (substr_count($source, DIRECTORY_SEPARATOR) === 1) {
         return Upload_Source::TYPE_TEMP;
     } elseif (is_file($source)) {
         return Upload_Source::TYPE_FILE;
     } else {
         return FALSE;
     }
 }
Ejemplo n.º 11
0
 public function action_add()
 {
     $user_id = $this->user->id;
     if (empty($user_id)) {
         $this->redirect('/');
     }
     $article = new Model_Article();
     $article->title = Arr::get($_POST, 'title');
     $article->description = Arr::get($_POST, 'description');
     $article->text = Arr::get($_POST, 'text');
     $cover = Arr::get($_FILES, 'cover');
     $errors = FALSE;
     $table_values = array();
     if ($article->title != '') {
         $table_values['title'] = array('value' => $article->title);
     } else {
         $errors = TRUE;
     }
     if ($article->description != '') {
         $table_values['description'] = array('value' => $article->description);
     } else {
         $errors = TRUE;
     }
     if ($article->text != '') {
         $table_values['text'] = array('value' => $article->text);
     } else {
         $errors = TRUE;
     }
     if (!Upload::valid($cover) or !Upload::not_empty($cover) or !Upload::type($cover, array('jpg', 'jpeg', 'png')) or !Upload::size($cover, '10M')) {
         $table_values['cover'] = TRUE;
         $errors = TRUE;
     }
     if ($errors) {
         // $this->view["editor"] = View::factory('templates/articles/editor', array("storedNodes" => $table_values['text']['value']));
         $content = View::factory('templates/articles/new', $this->view);
         $this->template->content = View::factory("templates/articles/wrapper", array("active" => "newArticle", "content" => $content));
         return false;
     }
     // getting new name for cover
     $article->cover = $this->methods->save_cover($cover);
     $article->user_id = $user_id;
     $article->is_published = true;
     // FIXME изменить, когда будет доступны режимы публикации
     $article->insert();
     // redirect to new article
     $this->redirect('/article/' . $article->id);
 }
Ejemplo n.º 12
0
 public function action_upload()
 {
     $field = 'Filedata';
     if (($value = Arr::get($_FILES, $field, FALSE)) === FALSE) {
         $this->request->response = 'error';
         return;
     }
     if (!Upload::not_empty($value) or !Upload::valid($value)) {
         $this->request->response = 'error';
         return;
     }
     if ($tmp_name = Torn_Uploader::upload_to_cache($value, $field)) {
         $this->request->response = 'done;' . $tmp_name;
     } else {
         $this->request->response = 'error';
     }
 }
Ejemplo n.º 13
0
 /**
  * осуществляет проверку загружаемого изображения на jpg(jpeg)
  * копирует его в католог img, предварительно 
  * изменив размер до s - max(300*300) m  - max(800*800) схранив пропорции
  * @param $file    string  полное имя файла 
  * @return         mixed new file name or false(boolean)
  */
 public function load($file, $id = '')
 {
     if (!Upload::valid($file) || !Upload::not_empty($file) || !Upload::type($file, array('jpg', 'jpeg'))) {
         return false;
     }
     $dir = DOCROOT . self::IMAGE_DIR;
     if ($image = Upload::save($file, NULL, $dir)) {
         if ($id == '') {
             $id = Text::random('alnum', 32);
         }
         $name = $id . '.jpg';
         Image::factory($image)->resize(300, 300, Image::AUTO)->save($dir . 's_' . $name);
         Image::factory($image)->resize(800, 800, Image::AUTO)->save($dir . 'm_' . $name);
         unlink($image);
         return $name;
     }
     return false;
 }
Ejemplo n.º 14
0
 public function action_image()
 {
     //get image
     $image = $_FILES['profile_image'];
     //file post
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, explode(',', core::config('image.allowed_formats'))) or !Upload::size($image, core::config('image.max_image_size') . 'M')) {
         if (Upload::not_empty($image) && !Upload::type($image, explode(',', core::config('image.allowed_formats')))) {
             Alert::set(Alert::ALERT, $image['name'] . ' ' . __('Is not valid format, please use one of this formats "jpg, jpeg, png"'));
             $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
         }
         if (!Upload::size($image, core::config('image.max_image_size') . 'M')) {
             Alert::set(Alert::ALERT, $image['name'] . ' ' . __('Is not of valid size. Size is limited on ' . core::config('general.max_image_size') . 'MB per image'));
             $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
         }
         Alert::set(Alert::ALERT, $image['name'] . ' ' . __('Image is not valid. Please try again.'));
         $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
     } else {
         if ($image != NULL) {
             $user_id = Auth::instance()->get_user()->id_user;
             // saving/uploadng zip file to dir.
             $root = DOCROOT . 'images/users/';
             //root folder
             $image_name = $user_id . '.png';
             $width = core::config('image.width');
             // @TODO dynamic !?
             $height = core::config('image.height');
             // @TODO dynamic !?
             $image_quality = core::config('image.quality');
             // if folder doesnt exists
             if (!file_exists($root)) {
                 mkdir($root, 775, true);
             }
             // save file to root folder, file, name, dir
             if ($file = Upload::save($image, $image_name, $root)) {
                 // resize uploaded image
                 Image::factory($file)->resize($width, $height, Image::AUTO)->save($root . $image_name, $image_quality);
             }
             Alert::set(Alert::SUCCESS, $image['name'] . ' ' . __('Image is uploaded.'));
             $this->request->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'edit')));
         }
     }
 }
Ejemplo n.º 15
0
 protected function _save_image($image, $directory)
 {
     if (!Upload::valid($image) || !Upload::not_empty($image) || !Upload::type($image, array('jpg', 'jpeg', 'png', 'gif')) || !Upload::size($image, '2M')) {
         return false;
     }
     if (!is_dir($directory)) {
         mkdir($directory, 0777, true);
     }
     if ($file = Upload::save($image, NULL, $directory)) {
         try {
             $filename = Text::random('alnum', 20) . '.jpg';
             Image::factory($file)->save($directory . $filename);
             unlink($file);
             return $filename;
         } catch (ErrorException $e) {
             // ...
         }
     }
     return false;
 }
Ejemplo n.º 16
0
 public function action_add()
 {
     $file = $_FILES['image'];
     if (!Upload::valid($file)) {
         $ret = array('status' => 'error', 'msg' => '不是有效的文件');
         $this->content = json_encode($ret, JSON_UNESCAPED_UNICODE);
         return;
     } elseif (!Upload::not_empty($file)) {
         $ret = array('status' => 'error', 'msg' => '上传文件为空');
         $this->content = json_encode($ret, JSON_UNESCAPED_UNICODE);
         return;
     } elseif (!Upload::type($file, array('jpg', 'png'))) {
         $ret = array('status' => 'error', 'msg' => '文件格式只能为jpg,png');
         $this->content = json_encode($ret, JSON_UNESCAPED_UNICODE);
         return;
     } elseif (!Upload::size($file, '8M')) {
         $ret = array('status' => 'error', 'msg' => '文件大小不能超过8M');
         $this->content = json_encode($ret, JSON_UNESCAPED_UNICODE);
         return;
     }
     $this->_add($file);
     $this->redirect(Request::$referrer);
 }
Ejemplo n.º 17
0
 /**
  * returns true if file is of valid type.
  * Its used to check file sent to user from advert usercontact
  * @param array file
  * @return BOOL 
  */
 public static function is_valid_file($file)
 {
     //catch file
     $file = $_FILES['file'];
     //validate file
     if ($file !== NULL) {
         if (!Upload::valid($file) or !Upload::not_empty($file) or !Upload::type($file, array('jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx')) or !Upload::size($file, '3M')) {
             return FALSE;
         }
         return TRUE;
     }
 }
Ejemplo n.º 18
0
 /**
  * Загрузка файлов из массива
  * 
  * @param array $array
  * @return \DataSource_Hybrid_Document
  */
 public function read_files($array)
 {
     foreach ($this->section()->record()->fields() as $key => $field) {
         if (isset($array[$key]) and $field->family == DataSource_Hybrid_Field::FAMILY_FILE and Upload::valid($array[$key]) and Upload::not_empty($array[$key])) {
             $field->onReadDocumentValue($array, $this);
             unset($array[$field->name]);
         }
     }
     return $this;
 }
Ejemplo n.º 19
0
 protected function _save_image($image, $folderName)
 {
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, array('jpg', 'jpeg', 'png', 'gif'))) {
         return FALSE;
     }
     $directory = DOCROOT . 'assets/images/' . $folderName;
     if (!is_dir($directory)) {
         mkdir($directory, 0777);
     }
     if ($file = Upload::save($image, $image['name'], $directory)) {
         //            $filename = strtolower(Text::random('alnum', 20)).'.jpg';
         Image::factory($file)->save($directory . $image['name']);
         // Delete the temporary file
         //            unlink($file);
         return $image['name'];
     }
     return FALSE;
 }
Ejemplo n.º 20
0
 /**
  * uploads the given image to S3
  * @param  $_FILE $image 
  * @param  boolean $favicon set to true if image is a favicon
  * @return FALSE/string url        
  */
 public static function upload_image($image, $favicon = FALSE)
 {
     if ($favicon) {
         $allowed_formats = array('ico');
     } else {
         $allowed_formats = explode(',', core::config('image.allowed_formats'));
     }
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, $allowed_formats) or !Upload::size($image, core::config('image.max_image_size') . 'M')) {
         if (Upload::not_empty($image) && !Upload::type($image, explode(',', core::config('image.allowed_formats')))) {
             Alert::set(Alert::ALERT, $image['name'] . ' ' . sprintf(__('Is not valid format, please use one of this formats "%s"'), core::config('image.allowed_formats')));
             return FALSE;
         }
         if (!Upload::size($image, core::config('image.max_image_size') . 'M')) {
             Alert::set(Alert::ALERT, $image['name'] . ' ' . sprintf(__('Is not of valid size. Size is limited to %s MB per image'), core::config('general.max_image_size')));
             return FALSE;
         }
         if (!Upload::not_empty($image)) {
             return FALSE;
         }
     }
     if (!$favicon and core::config('image.disallow_nudes') and !Upload::not_nude_image($image)) {
         Alert::set(Alert::ALERT, $image['name'] . ' ' . __('Seems a nude picture so you cannot upload it'));
         return FALSE;
     }
     if ($image !== NULL) {
         $directory = DOCROOT . 'images/';
         if ($file = Upload::save($image, $image['name'], $directory)) {
             // put image to Amazon S3
             Core::S3_upload($directory . $image['name'], 'images/' . $image['name']);
         } else {
             Alert::set(Alert::ALERT, __('Something went wrong uploading your logo'));
             return FALSE;
         }
     }
     //try s3, if not normal
     if (($base = Core::S3_domain()) === FALSE) {
         $base = URL::base();
     }
     return $base . 'images/' . $image['name'];
 }
Ejemplo n.º 21
0
 /**
  * Test Upload::valid
  *
  * @test
  * @dataProvider provider_valid
  * @covers Upload::valid
  */
 public function test_valid($expected, $file)
 {
     $this->setEnvironment(array('_FILES' => array('unit_test' => $file)));
     $this->assertSame($expected, Upload::valid($_FILES['unit_test']));
 }
Ejemplo n.º 22
0
 /**
  * Check if file has been saved to a temporary persistent directory.
  *
  * @param   array    uploaded file data
  * @param   string   session key for persistent uploaded files
  * @return  boolean  TRUE if file temporary saved, FALSE if file not saved
  */
 public static function persistent_check(array &$file, $sess_key = NULL)
 {
     static $cache = array();
     $input_name = Arr::get($file, 'input_name');
     if (isset($cache[$input_name])) {
         return FALSE;
     }
     $cache[$input_name] = TRUE;
     $sess_key === NULL and $sess_key = Ku_Upload::persistent_key($input_name);
     $sess_file = NULL;
     // Get all of the session data as an array
     $_SESSION =& Session::instance()->as_array();
     if (!empty($_SESSION['persistents']) and is_array($_SESSION['persistents']) and isset($_SESSION['persistents'][$sess_key])) {
         $sess_file = $_SESSION['persistents'][$sess_key];
     }
     if ($sess_file) {
         if (parent::valid($file) and !parent::not_empty($file)) {
             if (isset($sess_file['persistent']) and isset($sess_file['persistent_key']) and $sess_file['persistent_key'] === $sess_key) {
                 $filename = $sess_file['persistent'];
                 // Make sure the directory ends with a slash
                 $directory = dirname($filename) . DIRECTORY_SEPARATOR;
                 // Delete old persistent files from this directory
                 Ku_Upload::persistent_gc($directory);
                 // Refresh timestamp in the file name
                 $new_filename = preg_replace('/persistent~\\d+~/', 'persistent~' . time() . '~', $filename);
                 if (file_exists($filename) and is_file($filename) and rename($filename, $new_filename)) {
                     $sess_file['persistent'] = $new_filename;
                     // Save information about temporary saved file in the session
                     $_SESSION['persistents'][$sess_key] = $sess_file;
                     // Update file
                     $file = $sess_file;
                     // Update $_FILES
                     $_FILES[$input_name] = $sess_file;
                     return TRUE;
                 }
             }
         }
         Ku_Upload::persistent_delete($sess_file);
     }
     return FALSE;
 }
Ejemplo n.º 23
0
 public function action_save()
 {
     if (!$this->user->can('Admin_Item_Shops_Save')) {
         throw HTTP_Exception::factory('403', 'Permission denied to view admin item shops save');
     }
     $this->view = NULL;
     $values = $this->request->post();
     if ($values['id'] == 0) {
         $values['id'] = NULL;
     }
     $id = $values['id'];
     $this->response->headers('Content-Type', 'application/json');
     try {
         // $base_dir = DOCROOT . 'assets' . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . 'items'
         // . DIRECTORY_SEPARATOR . 'shops' . DIRECTORY_SEPARATOR . 'npc' . DIRECTORY_SEPARATOR;
         // $values['npc_img'] = $base_dir . $values['npc_img'];
         $item = ORM::factory('Shop', $values['id']);
         if ($item->loaded()) {
             $img = $item->npc_img;
             $item->values($values, array('title', 'status', 'npc_text', 'stock_type', 'stock_cap'));
         } else {
             $img = NULL;
             $values['npc_img'] = 'tmp';
             $item->values($values, array('title', 'status', 'npc_img', 'npc_text', 'stock_type', 'stock_cap'));
         }
         $item->save();
         $file = array('status' => 'empty', 'msg' => '');
         if (isset($_FILES['image'])) {
             $image = $_FILES['image'];
             $cfg = Kohana::$config->load('items.npc.image');
             if ($img == NULL) {
                 $img = $image['name'];
             }
             if (!Upload::valid($image)) {
                 // error not valid upload
                 $file = array('status' => 'error', 'msg' => 'You did not provide a valid file to upload.');
             } elseif (!Upload::image($image, $cfg['width'], $cfg['height'], TRUE)) {
                 // not the right image dimensions
                 $file = array('status' => 'error', 'msg' => 'You need to provide a valid image (size: :width x :height.', array(':width' => $cfg['width'], ':height' => $cfg['height']));
             } else {
                 $msg = '';
                 if ($id != NULL and $img != NULL and file_exists(DOCROOT . 'media/image/npc/shop/' . $img)) {
                     $grave_dir = DOCROOT . 'media/graveyard/npc/shop/';
                     if (!is_dir($grave_dir)) {
                         mkdir($grave_dir, 0, TRUE);
                     }
                     // move the previously stored item to the graveyard
                     $new_name = Text::random('alnum', 4) . $img;
                     copy(DOCROOT . 'media/image/npc/shop/' . $img, $grave_dir . $new_name);
                     unlink(DOCROOT . 'media/image/npc/shop/' . $img);
                     $msg = 'The old image has been moved to the graveyard and renamed to ' . $new_name;
                 }
                 if (!is_dir(DOCROOT . 'media/image/npc/shop/')) {
                     mkdir(DOCROOT . 'media/image/npc/shop/', 0, TRUE);
                 }
                 $up = Upload::save($image, $image['name'], DOCROOT . 'media/image/npc/shop/');
                 // $up = Upload::save($image, DOCROOT . 'media/image/npc/shop/'.$image['name']);
                 if ($up != FALSE) {
                     $file['status'] = 'success';
                     $file['msg'] = 'You\'ve successfully uploaded your item image';
                     if (!empty($msg)) {
                         $file['msg'] .= '<br />' . $msg;
                     }
                     $item->npc_img = $image['name'];
                     $item->save();
                 } else {
                     $file = array('status' => 'error', 'msg' => 'There was an error uploading your file.');
                 }
             }
         }
         $data = array('action' => 'saved', 'type' => $id == NULL ? 'new' : 'update', 'file' => $file, 'row' => array($item->title, $item->stock_type, $item->status, $item->id));
         $this->response->body(json_encode($data));
     } catch (ORM_Validation_Exception $e) {
         $errors = array();
         $list = $e->errors('models');
         foreach ($list as $field => $er) {
             if (!is_array($er)) {
                 $er = array($er);
             }
             $errors[] = array('field' => $field, 'msg' => $er);
         }
         $this->response->body(json_encode(array('action' => 'error', 'errors' => $errors)));
     }
 }
Ejemplo n.º 24
0
 /**
  * Tests if assigned file data is valid, even if no file was uploaded.
  *
  *     $validate->rule('file', 'Ku_File::valid')
  *
  * @param   mixed    $_FILES item or filepath
  * @return  bool
  */
 public static function valid($file)
 {
     if (is_array($file)) {
         return Upload::valid($file);
     }
     if (empty($file) or basename($file) == $file) {
         return TRUE;
     }
     return (bool) is_file($file);
 }
Ejemplo n.º 25
0
 /**
  * save_product upload images with given path
  * 
  * @param  [array]  $file      [file $_FILE-s ]
  * @param  [string] $seotitle   [unique id, and folder name]
  * @return [bool]               [return true if 1 or more files uploaded, false otherwise]
  */
 public function save_product($file)
 {
     if (!Upload::valid($file) or !Upload::not_empty($file) or !Upload::type($file, explode(',', core::config('product.formats'))) or !Upload::size($file, core::config('product.max_size') . 'M')) {
         if (Upload::not_empty($file) && !Upload::type($file, explode(',', core::config('product.formats')))) {
             return Alert::set(Alert::ALERT, $file['name'] . ': ' . sprintf(__('This uploaded file is not of a valid format. Please use one of these formats: %s'), core::config('product.formats')));
         }
         if (!Upload::size($file, core::config('product.max_size') . 'M')) {
             return Alert::set(Alert::ALERT, $file['name'] . ': ' . sprintf(__("This uploaded file exceeds the allowable limit. Uploaded files cannot be larger than %s MB per product"), core::config('product.max_size')));
         }
     }
     if ($file !== NULL) {
         $directory = DOCROOT . '/data/';
         // make dir
         if (!is_dir($directory)) {
             // check if directory exists
             mkdir($directory, 0755, TRUE);
         }
         $product_format = strrchr($file['name'], '.');
         $encoded_name = md5($file['name'] . uniqid(mt_rand())) . $product_format;
         // d($product_format);
         if ($temp_file = Upload::save($file, $encoded_name, $directory, 775)) {
             return $encoded_name;
         } else {
             return FALSE;
         }
         // Delete the temporary file
     }
 }
Ejemplo n.º 26
0
 public function _update_avatar($user_id, $avatar)
 {
     if (!Upload::valid($avatar) or !Upload::not_empty($avatar) or !Upload::type($avatar, array('jpg', 'jpeg', 'png', 'gif'))) {
         return FALSE;
     } else {
         $dir = DOCROOT . 'public/upload/gfx/avatar/';
         if ($file = Upload::save($avatar, NULL, $dir)) {
             $filename = $user_id . '.jpg';
             Image::factory($file)->resize(200, 200, Image::AUTO)->save($dir . $filename);
             unlink($file);
             if (is_file($dir . $filename)) {
                 return TRUE;
             } else {
                 return FALSE;
             }
         }
     }
 }
Ejemplo n.º 27
0
 /**
  * save_image upload images with given path
  * 
  * @param array image
  * @return bool
  */
 public function save_image($image)
 {
     if (!$this->loaded()) {
         return FALSE;
     }
     $seotitle = $this->seotitle;
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, explode(',', core::config('image.allowed_formats'))) or !Upload::size($image, core::config('image.max_image_size') . 'M')) {
         if (Upload::not_empty($image) && !Upload::type($image, explode(',', core::config('image.allowed_formats')))) {
             Alert::set(Alert::ALERT, $image['name'] . ' ' . sprintf(__('Is not valid format, please use one of this formats "%s"'), core::config('image.allowed_formats')));
             return FALSE;
         }
         if (!Upload::size($image, core::config('image.max_image_size') . 'M')) {
             Alert::set(Alert::ALERT, $image['name'] . ' ' . sprintf(__('Is not of valid size. Size is limited to %s MB per image'), core::config('image.max_image_size')));
             return FALSE;
         }
         if (!Upload::not_empty($image)) {
             return FALSE;
         }
     }
     if (core::config('image.disallow_nudes') and !Upload::not_nude_image($image)) {
         Alert::set(Alert::ALERT, $image['name'] . ' ' . __('Seems a nude picture so you cannot upload it'));
         return FALSE;
     }
     if ($image !== NULL) {
         $path = $this->image_path();
         $directory = DOCROOT . $path;
         if ($file = Upload::save($image, NULL, $directory)) {
             return $this->save_image_file($file, $this->has_images + 1);
         } else {
             Alert::set(Alert::ALERT, __('Something went wrong with uploading pictures, please check format'));
             return FALSE;
         }
     }
 }
Ejemplo n.º 28
0
 public function _upload_file($file, $name, $director)
 {
     $directory = DOCROOT . $director . '/';
     if (!Upload::valid($file) or !Upload::not_empty($file)) {
         return FALSE;
     }
     $name = $this->_prepare_name_folder($name);
     if ($file = Upload::save($file, $name, $directory)) {
         return TRUE;
     }
 }
Ejemplo n.º 29
0
 public function action_icon()
 {
     //get icon
     if (isset($_FILES['category_icon'])) {
         $icon = $_FILES['category_icon'];
     } else {
         $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'index')));
     }
     $category = new Model_Category($this->request->param('id'));
     if (core::config('image.aws_s3_active')) {
         require_once Kohana::find_file('vendor', 'amazon-s3-php-class/S3', 'php');
         $s3 = new S3(core::config('image.aws_access_key'), core::config('image.aws_secret_key'));
     }
     if (core::post('icon_delete') and $category->delete_icon() == TRUE) {
         Alert::set(Alert::SUCCESS, __('Icon deleted.'));
         $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
     }
     // end of icon delete
     if (!Upload::valid($icon) or !Upload::not_empty($icon) or !Upload::type($icon, explode(',', core::config('image.allowed_formats'))) or !Upload::size($icon, core::config('image.max_image_size') . 'M')) {
         if (Upload::not_empty($icon) && !Upload::type($icon, explode(',', core::config('image.allowed_formats')))) {
             Alert::set(Alert::ALERT, $icon['name'] . ' ' . sprintf(__('Is not valid format, please use one of this formats "%s"'), core::config('image.allowed_formats')));
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
         }
         if (!Upload::size($icon, core::config('image.max_image_size') . 'M')) {
             Alert::set(Alert::ALERT, $icon['name'] . ' ' . sprintf(__('Is not of valid size. Size is limited to %s MB per image'), core::config('image.max_image_size')));
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
         }
         Alert::set(Alert::ALERT, $icon['name'] . ' ' . __('Image is not valid. Please try again.'));
         $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
     } else {
         if ($icon != NULL) {
             // saving/uploading img file to dir.
             $path = 'images/categories/';
             $root = DOCROOT . $path;
             //root folder
             $icon_name = $category->seoname . '.png';
             // if folder does not exist, try to make it
             if (!file_exists($root) and !@mkdir($root, 0775, true)) {
                 // mkdir not successful ?
                 Alert::set(Alert::ERROR, __('Image folder is missing and cannot be created with mkdir. Please correct to be able to upload images.'));
                 return;
                 // exit function
             }
             // save file to root folder, file, name, dir
             if ($file = Upload::save($icon, $icon_name, $root)) {
                 // put icon to Amazon S3
                 if (core::config('image.aws_s3_active')) {
                     $s3->putObject($s3->inputFile($file), core::config('image.aws_s3_bucket'), $path . $icon_name, S3::ACL_PUBLIC_READ);
                 }
                 // update category info
                 $category->has_image = 1;
                 $category->last_modified = Date::unix2mysql();
                 $category->save();
                 Alert::set(Alert::SUCCESS, $icon['name'] . ' ' . __('Icon is uploaded.'));
             } else {
                 Alert::set(Alert::ERROR, $icon['name'] . ' ' . __('Icon file could not been saved.'));
             }
             $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'update', 'id' => $category->id_category)));
         }
     }
 }
Ejemplo n.º 30
0
 /**
  * upload an image to the user
  * @param  file $image 
  * @return bool/message        
  */
 public function upload_image($image)
 {
     if (!$this->loaded()) {
         return FALSE;
     }
     if (core::config('image.aws_s3_active')) {
         require_once Kohana::find_file('vendor', 'amazon-s3-php-class/S3', 'php');
         $s3 = new S3(core::config('image.aws_access_key'), core::config('image.aws_secret_key'));
     }
     if (!Upload::valid($image) or !Upload::not_empty($image) or !Upload::type($image, explode(',', core::config('image.allowed_formats'))) or !Upload::size($image, core::config('image.max_image_size') . 'M')) {
         if (Upload::not_empty($image) && !Upload::type($image, explode(',', core::config('image.allowed_formats')))) {
             return $image['name'] . ' ' . sprintf(__('Is not valid format, please use one of this formats "%s"'), core::config('image.allowed_formats'));
         }
         if (!Upload::size($image, core::config('image.max_image_size') . 'M')) {
             return $image['name'] . ' ' . sprintf(__('Is not of valid size. Size is limited to %s MB per image'), core::config('image.max_image_size'));
         }
         return $image['name'] . ' ' . __('Image is not valid. Please try again.');
     } else {
         if ($image != NULL) {
             // saving/uploading zip file to dir.
             $path = 'images/users/';
             //root folder
             $root = DOCROOT . $path;
             //root folder
             $image_name = $this->id_user . '.png';
             $width = core::config('image.width');
             // @TODO dynamic !?
             $height = core::config('image.height');
             // @TODO dynamic !?
             $image_quality = core::config('image.quality');
             // if folder does not exist, try to make it
             if (!file_exists($root) and !@mkdir($root, 0775, true)) {
                 // mkdir not successful ?
                 return __('Image folder is missing and cannot be created with mkdir. Please correct to be able to upload images.');
             }
             // save file to root folder, file, name, dir
             if ($file = Upload::save($image, $image_name, $root)) {
                 // resize uploaded image
                 Image::factory($file)->orientate()->resize($width, $height, Image::AUTO)->save($root . $image_name, $image_quality);
                 // put image to Amazon S3
                 if (core::config('image.aws_s3_active')) {
                     $s3->putObject($s3->inputFile($file), core::config('image.aws_s3_bucket'), $path . $image_name, S3::ACL_PUBLIC_READ);
                 }
                 // update user info
                 $this->has_image = 1;
                 $this->last_modified = Date::unix2mysql();
                 try {
                     $this->save();
                     return TRUE;
                 } catch (Exception $e) {
                     return $e->getMessage();
                 }
             } else {
                 return $image['name'] . ' ' . __('Icon file could not been saved.');
             }
         }
     }
 }