Esempio n. 1
0
 /**
  * Process resource library insert 
  * @param array $table_cfg
  * @param array $data_row
  * @param array $parent_vals
  * @return array
  */
 private function _do_all_resources($table_cfg, $data, $parent_vals)
 {
     $records = array();
     foreach ($data['rows'] as $row) {
         if ($row['type']) {
             if ($row['source_url']) {
                 $records[$row['type']]['source_url'][] = $row['source_url'];
             } else {
                 if ($row['source_path']) {
                     $records[$row['type']]['source_path'][] = $row['source_path'];
                 } else {
                     if ($row['html_code']) {
                         $records[$row['type']]['html_code'][] = $row['html_code'];
                     }
                 }
             }
         }
     }
     foreach ($records as $type => $sources) {
         $rm = new AResourceManager();
         $rm->setType($type);
         //delete all resource of the type from library
         $object_name = $table_cfg['special_relation']['object_name'];
         $object_id = $parent_vals[$table_cfg['special_relation']['object_id']];
         $resources = $rm->unmapAndDeleteResources($object_name, $object_id, $type);
         //ad new media sources
         if ($sources['source_url']) {
             $fl = new AFile();
             foreach ($sources['source_url'] as $source) {
                 $image_basename = basename($source);
                 $target = DIR_RESOURCE . $rm->getTypeDir() . '/' . $image_basename;
                 if (!is_dir(DIR_RESOURCE . $rm->getTypeDir())) {
                     @mkdir(DIR_RESOURCE . $rm->getTypeDir(), 0777);
                 }
                 if (($file = $fl->downloadFile($source)) === false) {
                     $this->_status2array('error', "Unable to download file from {$source}");
                     continue;
                 }
                 if (!$fl->writeDownloadToFile($file, $target)) {
                     $this->_status2array('error', "Unable to save download to {$target}");
                     continue;
                 }
                 if (!$this->_create_resource($rm, $object_name, $object_id, $image_basename)) {
                     $this->_status2array('error', "Unable to create new media resource type {$type} for {$image_basename}");
                     continue;
                 }
             }
         }
         if ($sources['source_path']) {
             foreach ($sources['source_path'] as $source) {
                 $image_basename = basename($source);
                 $target = DIR_RESOURCE . $rm->getTypeDir() . '/' . $image_basename;
                 if (!is_dir(DIR_RESOURCE . $rm->getTypeDir())) {
                     @mkdir(DIR_RESOURCE . $rm->getTypeDir(), 0777);
                 }
                 if (!copy($source, $target)) {
                     $this->_status2array('error', "Unable to copy {$source} to {$target}");
                     continue;
                 }
                 if (!$this->_create_resource($rm, $object_name, $object_id, $image_basename)) {
                     $this->_status2array('error', "Unable to create new media resource for {$image_basename}");
                     continue;
                 }
             }
         }
         if ($sources['html_code']) {
             foreach ($sources['html_code'] as $code) {
                 if (!$this->_create_resource($rm, $object_name, $object_id, '', $code)) {
                     $this->_status2array('error', "Unable to create new HTML code media resource type {$type}");
                     continue;
                 }
             }
         }
     }
     return array();
 }
 public function get_resource_preview()
 {
     //init controller data
     $this->extensions->hk_InitData($this, __FUNCTION__);
     $rm = new AResourceManager();
     $result = $rm->getResource($this->request->get['resource_id'], $this->language->getContentLanguageID());
     if (!empty($result)) {
         $rm->setType($result['type_name']);
         if (!empty($result['resource_code'])) {
             if (strpos($result['resource_code'], "http") === 0) {
                 $this->redirect($result['resource_code']);
             } else {
                 $this->response->setOutput($result['resource_code']);
             }
         } else {
             $file_path = DIR_RESOURCE . $rm->getTypeDir() . $result['resource_path'];
             $result['name'] = pathinfo($result['name'], PATHINFO_FILENAME);
             if (file_exists($file_path) && ($fd = fopen($file_path, "r"))) {
                 $fsize = filesize($file_path);
                 $path_parts = pathinfo($file_path);
                 $this->response->addHeader('Content-type: application/octet-stream');
                 $this->response->addHeader("Content-Disposition: filename=\"" . $result['name'] . '.' . $path_parts["extension"] . "\"");
                 $this->response->addHeader("Content-length: " . $fsize);
                 $this->response->addHeader("Cache-control: private");
                 //use this to open files directly
                 $buffer = '';
                 while (!feof($fd)) {
                     $buffer .= fread($fd, 32768);
                 }
                 $this->response->setOutput($buffer);
             } else {
                 $this->response->setOutput($this->language->get('text_no_resources'));
             }
             fclose($fd);
         }
     } else {
         $this->response->setOutput($this->language->get('text_no_resources'));
     }
 }
Esempio n. 3
0
 public function get_resource_preview()
 {
     $rm = new AResourceManager();
     $result = $rm->getResource($this->request->get['resource_id'], $this->config->get('storefront_language_id'));
     if (!empty($result)) {
         $rm->setType($result['type_name']);
         if (!empty($result['resource_code'])) {
             if (strpos($result['resource_code'], "http") === 0) {
                 $this->redirect($result['resource_code']);
             } else {
                 $this->response->setOutput($result['resource_code']);
             }
         } else {
             $file_path = DIR_RESOURCE . $rm->getTypeDir() . $result['resource_path'];
             $result['name'] = pathinfo($result['name'], PATHINFO_FILENAME);
             if (file_exists($file_path) && ($fd = fopen($file_path, "r"))) {
                 $fsize = filesize($file_path);
                 $path_parts = pathinfo($file_path);
                 $this->response->addHeader('Content-type: ' . mime_content_type($path_parts["basename"]));
                 $this->response->addHeader("Content-Disposition: filename=\"" . $result['name'] . '.' . $path_parts["extension"] . "\"");
                 $this->response->addHeader("Content-length: {$fsize}");
                 $this->response->addHeader("Cache-control: private");
                 //use this to open files directly
                 $buffer = '';
                 while (!feof($fd)) {
                     $buffer .= fread($fd, 32768);
                 }
                 $this->response->setOutput($buffer);
             } else {
                 $this->response->setOutput($this->language->get('text_no_resources'));
             }
             fclose($fd);
         }
     } else {
         $this->response->setOutput($this->language->get('text_no_resources'));
     }
 }