Example #1
0
 public function upload_attaches()
 {
     $model = $this->Register['ModManager']->getModelInstance('ShopAttaches');
     $model->deleteNotRelated();
     $this->counter = false;
     if (!$this->ACL->turn(array($this->module, 'edit_products'), false)) {
         $this->showAjaxResponse(array('errors' => __('Permission denied'), 'result' => '0'));
     }
     $attachModel = $this->Register['ModManager']->getModelInstance($this->module . 'Attaches');
     $errors = '';
     if (!empty($_FILES) && is_array($_FILES)) {
         $cnt = 0;
         $new_file_size = 0;
         foreach ($_FILES as $name => $file) {
             if (preg_match('#^attach\\d+$#', $name)) {
                 $cnt++;
                 $new_file_size += $file['size'];
             }
         }
     }
     if ($cnt > Config::read('max_attaches', $this->module)) {
         $errors .= '<li>' . sprintf(__('You can upload only %s file(s)'), Config::read('max_attaches', $this->module)) . '</li>';
     }
     if (!empty($_SESSION['user']['id'])) {
         $old_files_size = $attachModel->getUserOveralFilesSize($_SESSION['user']['id']);
         $overal_files_size = intval($new_file_size) + $old_files_size;
         $max_overal_size = Config::read('max_all_attaches_size', $this->module) * 1024 * 1024;
     } else {
         $overal_files_size = intval($new_file_size);
         $max_overal_size = Config::read('max_guest_attaches_size', $this->module) * 1024 * 1024;
     }
     if ($overal_files_size > $max_overal_size) {
         $errors .= '<li>' . sprintf(__('Max overal files size is %s Mb'), $max_overal_size / 1024 / 1024) . '</li>';
     }
     $errors .= $this->Register['Validate']->check($this->Register['action']);
     if (!empty($errors)) {
         $this->showAjaxResponse(array('errors' => $this->Register['DocParser']->wrapErrors($errors), 'result' => '0'));
     }
     $attaches = downloadAtomAttaches($this->module);
     if ($attaches) {
         $last_ids = '';
         foreach ($attaches as $k => $attach) {
             $last_ids += ($k > 0 ? ', ' : '') . $attach['id'];
         }
         if ($this->Log) {
             $this->Log->write('add attach[s](' . $this->module . ')', 'id[s](' . $last_ids . ')');
         }
     }
     $this->showAjaxResponse($attaches);
 }
Example #2
0
 public function upload_attaches()
 {
     $this->counter = false;
     $key = $this->module === 'forum' ? 'add_posts' : 'add_materials';
     if (!$this->ACL->turn(array($this->module, $key), false) || !$this->ACL->turn(array($this->module, 'use_attaches'), false)) {
         $this->showAjaxResponse(array('errors' => $this->Register['DocParser']->wrapErrors(__('Permission denied')), 'result' => '0'));
     }
     $attachModel = $this->Register['ModManager']->getModelInstance($this->module . 'Attaches');
     $errors = $this->Register['Validate']->check($this->Register['action']);
     if (!empty($_FILES) && is_array($_FILES)) {
         $cnt = 0;
         $new_file_size = 0;
         foreach ($_FILES as $name => $file) {
             if (preg_match('#^attach\\d+$#', $name)) {
                 $cnt++;
                 $new_file_size += $file['size'];
             }
         }
     }
     if ($cnt > Config::read('max_attaches', $this->module)) {
         $errors[] = sprintf(__('You can upload only %s file(s)'), Config::read('max_attaches', $this->module));
     }
     if (!empty($_SESSION['user']['id'])) {
         $old_files_size = $attachModel->getUserOveralFilesSize($_SESSION['user']['id']);
         $overal_files_size = intval($new_file_size) + $old_files_size;
         $max_overal_size = Config::read('max_all_attaches_size', $this->module) * 1024 * 1024;
     } else {
         $overal_files_size = intval($new_file_size);
         $max_overal_size = Config::read('max_guest_attaches_size', $this->module) * 1024 * 1024;
     }
     if ($overal_files_size > $max_overal_size) {
         $errors[] = sprintf(__('Max overal files size is %s Mb'), $max_overal_size / 1024 / 1024);
     }
     if (!empty($errors)) {
         $this->showAjaxResponse(array('errors' => $this->Register['DocParser']->wrapErrors($errors), 'result' => '0'));
     }
     $attaches = downloadAtomAttaches($this->module);
     $this->showAjaxResponse($attaches);
 }
 public function beforeSaveContent($content)
 {
     $attrObj = $this->parent;
     switch ($attrObj->getType()) {
         case 'text':
         case 'textarea':
         default:
             $this->content = trim($content);
             break;
         case 'checkbox':
             $this->content = !empty($content) ? '1' : '0';
             break;
         case 'select':
             if ($attrObj->getParams()) {
                 foreach ($attrObj->getParams() as $v) {
                     if ($v === trim($content)) {
                         $this->content = trim($content);
                         break 2;
                     }
                 }
             }
             throw new Exception('Unavailable point "' . h(trim($content)) . '" is selected for "' . h($attrObj->getTitle()) . '" attribute.');
             break;
         case 'image':
             if (!empty($_POST['attributes'][$attrObj->getTitle() . '_delete'])) {
                 if ($this->old_content) {
                     $path = ROOT . '/sys/files/shop/' . $this->old_content;
                     if (file_exists($path)) {
                         unlink($path);
                     }
                     $this->content = '';
                 }
             }
             if (!empty($content) && is_array($content)) {
                 $Register = Register::getInstance();
                 $params = $attrObj->getParams();
                 $_FILES['attach1'] = $content;
                 $Register['Validate']->setRules(array('upload_image' => array('files__attach1' => array('type' => 'image', 'max_size' => !empty($params['max_size']) ? intval($params['max_size']) : Config::read('max_attaches_size', 'shop')))));
                 $errors = $Register['Validate']->check('upload_image');
                 if (!empty($errors)) {
                     throw new Exception($errors);
                 }
                 $uploaded = downloadAtomAttaches('shop');
                 if (!empty($uploaded[0]) && !empty($uploaded[0]['filename'])) {
                     $this->content = $uploaded[0]['filename'];
                 }
             }
             break;
     }
 }