Exemple #1
0
 /**
  * twig custom filter for convert date to jalai with custom format like php date func format
  */
 public function twig_filter_readableSize()
 {
     return new \Twig_SimpleFilter('readableSize', function ($_string, $_type = 'file', $_emptyTxt = null) {
         return \lib\utility\Upload::readableSize($_string, $_type, $_emptyTxt);
     });
 }
Exemple #2
0
 /**
  * force browser to download file
  * @param  [type] $_filepath [description]
  * @return [type]           [description]
  */
 public static function download($_filePath, $_fileName = null, $_fileMime = null)
 {
     // Quick check to verify that the file exists
     if (!file_exists($_filePath)) {
         // return false;
     }
     if (!$_fileName) {
         $_fileName = basename($_filePath);
     }
     if (!$_fileMime) {
         // get file mime from upload library
         $_fileMime = \lib\utility\Upload::extCheck($_filePath);
         $_fileMime = $_fileMime['mime'];
     }
     // Force the download
     header('Pragma: public');
     header('Expires: 0');
     header('Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0');
     header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($_filePath)) . ' GMT');
     header("Content-disposition: attachment; filename=\"" . basename($_fileName) . "\"");
     header('Content-Length: ' . filesize($_filePath));
     header('Content-Transfer-Encoding: binary');
     // Generate the server headers to force the download process
     if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
         header("Content-Type: " . $_fileMime . ";");
     } else {
         header("Content-Type: " . $_fileMime, true, 200);
         header('Cache-Control: private', false);
         header('Accept-Ranges: bytes');
         header('Connection: close');
     }
     readfile($_filePath);
     exit;
 }
Exemple #3
0
 public function view_child()
 {
     $mytable = $this->cpModule('table');
     $mychild = $this->child();
     // $this->global->js       = array($this->url->myStatic.'js/cp/medium-editor.min.js');
     $this->data->enum = \lib\sql\getTable::enumValues('posts');
     switch ($mytable) {
         case 'posts':
             // show list of tags
             $this->data->tagList = $this->model()->sp_term_list();
             // for each type of post
             switch ($this->cpModule('raw')) {
                 case 'pages':
                     $this->data->parentList = $this->model()->sp_parent_list();
                     break;
                 case 'attachments':
                     $this->data->maxSize = \lib\utility\Upload::max_file_upload_in_bytes();
                     // $this->include->uploader      = true;
                     // array_push($this->global->js, $this->url->myStatic.'js/cp/uploader.js');
                     $this->data->catList = $this->model()->sp_cats('filecat');
                     $this->data->catListSelected = $this->model()->sp_cats('filecat', true);
                     break;
                 case 'books':
                     $this->data->catList = $this->model()->sp_cats('bookcat');
                     $this->data->catListSelected = $this->model()->sp_cats('bookcat', true);
                     $this->data->parentList = $this->model()->sp_parent_list(true, 'book');
                     break;
                 case 'socialnetwork':
                     $this->data->catList = null;
                     break;
                 default:
                     $this->data->catList = $this->model()->sp_cats();
                     break;
             }
             break;
         default:
             switch ($this->cpModule('raw')) {
                 case 'categories':
                 case 'filecategories':
                 case 'bookcategories':
                     $this->data->parentList = $this->model()->sp_category_list($this->cpModule('type'));
                     break;
             }
             $this->data->field_list = \lib\sql\getTable::get($mytable);
             $myform = $this->createform('@' . db_name . '.' . $mytable, $this->data->child);
             break;
     }
     // if module for users then fill permission list
     if ($this->cpModule('raw') === 'users') {
         $myPermList = $this->data->form->users->user_permission;
         $myPermList->type('select');
         // get list of permissions
         foreach ($this->model()->permList() as $value) {
             $myPermList->child()->value($value)->label(T_($value));
         }
     }
     if ($mychild === 'edit') {
         $this->data->datarow = $this->model()->datarow($mytable, null, true);
         if (isset($this->data->datarow['post_meta'])) {
             $this->data->datarow['meta'] = json_decode($this->data->datarow['post_meta'], true);
         }
         // var_dump($this->data->datarow['meta']);
         if ($this->cpModule('raw') === 'attachments') {
             if (isset($this->data->datarow['meta']['slug'])) {
                 $this->data->datarow['post_slug'] = $this->data->datarow['meta']['slug'];
             }
         }
         if ($mytable === 'posts') {
             // $this->data->datarow['post_content'] .= '<img src="/static/images/logo.png" />';
             // var_dump($this->data->datarow['post_content']);
             $url = $this->data->datarow['post_url'];
             $this->data->datarow['cat_url'] = substr($url, 0, strrpos($url, '/'));
         }
     }
 }
Exemple #4
0
 /**
  * Upload file to project files and database
  * @return [type] [description]
  */
 function sp_attachment_add()
 {
     $FOLDER_SIZE = 1000;
     // 1. check upload process and validate it
     $invalid = utility\Upload::invalid('upfile');
     if ($invalid) {
         debug::property('status', 'fail');
         debug::property('error', $invalid);
         $this->_processor(['force_json' => true, 'not_redirect' => true]);
         return false;
     }
     // 2. Generate file_id, folder_id and url
     $qry_count = $this->sql()->table('posts')->where('post_type', 'attachment')->select('id')->num();
     $folder_prefix = "files/";
     $folder_id = $folder_prefix . ceil(($qry_count + 1) / $FOLDER_SIZE);
     $file_id = $qry_count % $FOLDER_SIZE + 1;
     $url_full = "{$folder_id}/{$file_id}-" . utility\Upload::$fileFullName;
     // 3. Check for record exist in db or not
     $qry_count = $this->sql()->table('posts')->where('post_slug', utility\Upload::$fileMd5)->select('id');
     if ($qry_count->num()) {
         $id = $qry_count->assoc('id');
         debug::property('status', 'fail');
         $link = '<a target="_blank" href=/attachments/edit=' . $id . '>' . T_('Duplicate - File exist') . '</a>';
         debug::property('error', $link);
         $this->_processor(['force_json' => true, 'not_redirect' => true]);
         return false;
     }
     // 4. transfer file to project folder with new name
     if (!utility\Upload::transfer($url_full, $folder_id)) {
         debug::property('status', 'fail');
         debug::property('error', T_('Fail on tranfering file'));
         $this->_processor(['force_json' => true, 'not_redirect' => true]);
         return false;
     }
     $file_ext = utility\Upload::$fileExt;
     $url_thumb = null;
     $url_normal = null;
     switch ($file_ext) {
         case 'jpg':
         case 'jpeg':
         case 'png':
         case 'gif':
             $extlen = strlen(utility\Upload::$fileExt);
             $url_file = substr($url_full, 0, -$extlen - 1);
             $url_thumb = $url_file . '-thumb.' . utility\Upload::$fileExt;
             $url_normal = $url_file . '-normal.' . utility\Upload::$fileExt;
             // var_dump($thumb_url);
             // exit();
             utility\Image::load($url_full);
             utility\Image::thumb(600, 400);
             utility\Image::save($url_normal);
             utility\Image::thumb(150, 150);
             utility\Image::save($url_thumb);
             break;
     }
     // 5. get filemeta data
     $file_meta = ['mime' => utility\Upload::$fileMime, 'type' => utility\Upload::$fileType, 'size' => utility\Upload::$fileSize, 'ext' => $file_ext, 'url' => $url_full, 'thumb' => $url_thumb, 'normal' => $url_normal];
     $page_url = $file_meta['type'] . '/' . substr($url_full, strlen($folder_prefix));
     if (strpos($file_meta['mime'], 'image') !== false) {
         list($file_meta['width'], $file_meta['height']) = getimagesize($url_full);
     }
     $file_meta = json_encode($file_meta);
     // var_dump($file_meta);exit();
     // 6. add uploaded file record to db
     $qry = $this->sql();
     $qry = $qry->table('posts')->set('post_title', utility\Upload::$fileName)->set('post_slug', utility\Upload::$fileMd5)->set('post_meta', $file_meta)->set('post_type', 'attachment')->set('post_url', $page_url)->set('user_id', $this->login('id'))->set('post_status', 'draft')->set('post_publishdate', date('Y-m-d H:i:s'));
     $qry = $qry->insert();
     $post_new_id = $qry->LAST_INSERT_ID();
     // 7. commit all changes or rollback and remove file
     // ======================================================
     // you can manage next event with one of these variables,
     // commit for successfull and rollback for failed
     // if query run without error means commit
     $this->commit(function ($_id) {
         debug::property('status', 'ok');
         $link = '<a target="_blank" href=/attachments/edit=' . $_id . '>' . T_('Edit') . '</a>';
         debug::property('edit', $link);
     }, $post_new_id);
     // if a query has error or any error occour in any part of codes, run roolback
     $this->rollback(function () {
         debug::property('status', 'fail');
         debug::property('error', T_('Error'));
         // remove file if has problem
     });
     $this->_processor(['force_json' => true, 'not_redirect' => true]);
 }