Esempio n. 1
0
 /**
  * just a test method to upload a file and execute methods from
  * DataModel to check the result
  * @return void
  */
 public function testDataModelFilesMethods()
 {
     $api = new Api\Files();
     $fn = tempnam(sys_get_temp_dir(), 'cb_test');
     file_put_contents($fn, 'testing');
     $data = array('pid' => 1, 'localFile' => $fn, 'oid' => 1);
     $rez = $api->upload($data);
     $this->assertTrue($rez['success'], 'Upload test file failed: ' . $fn);
     $fileData = $rez['data']['file'];
     $id = $fileData['id'];
     $rez = DM\Files::getContentIds($id);
     $this->assertTrue(!empty($rez[$id]), 'Cant get content id');
     $rez = DM\Files::getContentPaths($id);
     $this->assertTrue(!empty($rez[$id]), 'Cant get content path');
     //delete the file from system and check if same actions return empty results
     $f = Objects::getCachedObject($id);
     $f->delete(true);
     // permanently delete
     unset($f);
     $rez = DM\Files::getContentIds($id);
     $this->assertTrue(empty($rez[$id]), 'Obtaining content id for a permanently deleted file');
     $rez = DM\Files::getContentPaths($id);
     $this->assertTrue(empty($rez[$id]), 'Obtaining content path for a permanently deleted file');
 }
Esempio n. 2
0
 /**
  * append file contents to content field for file records
  * @param  array &$records
  * @return void
  */
 protected function appendFileContents(&$records)
 {
     $fileRecords = array();
     foreach ($records as &$r) {
         if (!empty($r['template_type']) && $r['template_type'] == 'file') {
             $fileRecords[$r['id']] =& $r;
         }
     }
     if (!empty($fileRecords)) {
         $filesDir = Config::get('files_dir');
         $cpaths = DM\Files::getContentPaths(array_keys($fileRecords));
         foreach ($cpaths as $id => $cpath) {
             $r =& $fileRecords[$id];
             $filename = $filesDir . $cpath . '.gz';
             if (file_exists($filename)) {
                 $content = file_get_contents($filename);
                 $r['content'] .= "\n" . gzuncompress($content);
             }
             unset($content);
             unset($r);
         }
     }
 }
Esempio n. 3
0
 public function restoreVersion($id)
 {
     $rez = array('success' => true, 'data' => array('id' => 0, 'pid' => 0));
     $fileId = 0;
     //detect file id
     $version = DM\FilesVersions::read($id);
     if (!empty($version)) {
         $fileId = $version['file_id'];
         $rez['data']['id'] = $fileId;
     }
     //get its pid
     $r = DM\Tree::read($fileId);
     if (!empty($r['pid'])) {
         $rez['data']['pid'] = $r['pid'];
     }
     $this->saveCurrentVersion($fileId);
     DM\Files::delete($fileId);
     DM\Files::create(array('id' => $fileId, 'content_id' => $version['content_id'], 'date' => $version['date'], 'name' => $version['name'], 'cid' => $version['cid'], 'uid' => User::getId(), 'cdate' => $version['cdate'], 'udate' => $version['udate']));
     Objects::updateCaseUpdateInfo($id);
     Solr\Client::runCron();
     return $rez;
 }
Esempio n. 4
0
 /**
  * copy costom files data to targetId
  * @param  int  $targetId
  * @return void
  */
 protected function copyCustomDataTo($targetId)
 {
     DM\Files::copy($this->id, $targetId);
 }
Esempio n. 5
0
 /**
  * add attachment links below the comments body
  * @param  array reference $rez
  * @return void
  */
 protected static function addAttachmentLinks(&$rez)
 {
     //collect comment ids
     $ids = array();
     foreach ($rez['data'] as $d) {
         $ids[] = $d['id'];
     }
     if (empty($ids)) {
         return;
     }
     //select files for all loaded comments using a single solr request
     $params = array('system' => '[0 TO 2]', 'fq' => array('pid:(' . implode(' OR ', $ids) . ')', 'template_type:"file"'), 'fl' => 'id,pid,name,template_id', 'sort' => 'pid,cdate', 'rows' => 50, 'dir' => 'asc');
     $s = new \CB\Search();
     $sr = $s->query($params);
     $files = array();
     $fileIds = array();
     $fileTypes = array();
     foreach ($sr['data'] as $d) {
         $files[$d['pid']][] = $d;
         $fileIds[] = $d['id'];
     }
     //get file types
     if (!empty($fileIds)) {
         $fileTypes = DM\Files::getTypes($fileIds);
     }
     foreach ($rez['data'] as &$d) {
         if (empty($files[$d['id']])) {
             continue;
         }
         $links = array();
         foreach ($files[$d['id']] as $f) {
             $f['type'] = @$fileTypes[$f['id']];
             $links[] = static::getFileLink($f);
         }
         $d['files'] = '<ul class="comment-attachments"><li>' . implode('</li><li>', $links) . '</li></ul>';
     }
 }
Esempio n. 6
0
 public function rename($p)
 {
     $id = explode('/', $p['path']);
     $id = array_pop($id);
     $p['name'] = trim($p['name']);
     if (!is_numeric($id) || empty($p['name'])) {
         return array('success' => false);
     }
     /* check security access */
     if (!Security::canWrite($id)) {
         throw new \Exception(L\get('Access_denied'));
     }
     $p['name'] = Purify::filename($p['name']);
     $rez = array('success' => true, 'data' => array('id' => $id, 'pid' => null, 'newName' => $p['name']));
     $objectType = Objects::getType($id);
     if ($objectType == 'shortcut') {
         $r = DM\Tree::read($id);
         if (!empty($r['target_id'])) {
             $id = $r['target_id'];
             $objectType = Objects::getType($id);
         }
     }
     DM\Tree::update(array('id' => $id, 'name' => $p['name']));
     if ($objectType == 'file') {
         DM\Files::update(array('id' => $id, 'name' => $p['name']));
     }
     /*updating renamed document into solr directly (before runing background cron)
       so that it'll be displayed with new name without delay*/
     $solrClient = new Solr\Client();
     $solrClient->updateTree(array('id' => $id));
     //running background cron to index other nodes
     $solrClient->runBackgroundCron();
     $p['name'] = htmlspecialchars($p['name'], ENT_COMPAT);
     //get pid
     $r = DM\Tree::read($rez['data']['id']);
     if (!empty($r['pid'])) {
         $rez['data']['pid'] = $r['pid'];
     }
     return $rez;
 }