コード例 #1
0
 /**
  * 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) {
             $tmp_file = $file_data['tmp_name'];
             $filename = $file_data['name'];
             $udata = fileinfo::get($tmp_file, $filename);
             $udata['file_name'] = filestore::put($file_data, $filename);
             preview::generate($udata);
             $n = str_replace('form_', '', $file);
             $id = (is_object($model->{$n}) and $model->{$n}->id > 0) ? $model->{$n}->id : 0;
             if (isset($udata['image_width']) && empty($udata['image_width'])) {
                 unset($udata['image_width']);
             }
             if (isset($udata['image_height']) && 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();
         }
     }
 }
コード例 #2
0
 /**
  * get the data about the image
  *
  * @param integer $image (optional - if not passed, used uri segment 3)
  * @return object
  * @author Andy Bennett
  */
 protected function get_image_row($image = false)
 {
     // use URI segment 3 if no id is passed
     if (!$image) {
         $image = end(URI::instance()->segment_array());
     }
     // strip any file extensions (actually strip anything that isn't a number)
     $image = pathinfo(Kohana::instance()->uri->string(), PATHINFO_FILENAME);
     // make sure the id is numeric - if not exit
     if (!preg_match('/[0-9]+/i', $image)) {
         exit;
     }
     // get the ORM object
     $row = ORM::factory($this->setup['model'] . '_upload', $image);
     // make sure the object has been loaded properly and the parent model matches
     if (!$row->loaded) {
         Kohana::log('error', 'File model row error: ' . $image);
         throw new Kohana_404_Exception($image, 'common/error_404');
     }
     if ($row->parent_model != $this->setup['model']) {
         Kohana::log('error', 'File model error: ' . $row->id);
         throw new Kohana_403_Exception($row->id, 'common/error_403');
     }
     // make sure the object's status is true
     if (!$row->parent->status and !User::instance()->is_moderator()) {
         Kohana::log('error', 'File status: ' . $row->id);
         throw new Kohana_403_Exception($row->id, 'common/error_403');
     }
     // $data = array( 'action' => URI::instance()->segment(2), 'name' => $this->setup['name'], 'role' => User::instance()->get_role() );
     // Event::run( 'acl.check', $data );
     $row->full_path = Kohana::config('upload.directory') . '/' . $row->file_name;
     if (!file_exists($row->full_path)) {
         $this->delete_fullpath = $row->full_path;
         file_put_contents($row->full_path, filestore::get($row->file_name));
     } else {
         if (!file_exists($row->full_path) and isset($row->file_data) and !empty($row->file_data)) {
             file_put_contents($row->full_path, $row->file_data);
         }
     }
     if (!file_exists($row->full_path)) {
         Kohana::log('error', 'Invalid file path: ' . $row->full_path);
         throw new Kohana_404_Exception($row->orig_name, 'common/error_404');
     }
     return $row;
 }