* @license PHP License * @package WB * @subpackage content */ /** * Load required class */ WBClass::load('WBContent' , 'WBVFS_File'); /** * Content component: Manage VFS * * @version 1.3.0 * @package WB * @subpackage content */ class WBContent_VFS extends WBContent { /** * my parameter list * @var array */ protected $config = array( 'process' => 'show', 'dir' => 0, 'user' => 0, 'requiredgroup' => 'vfs', 'translation' => 0, 'mimemajor' => '', 'clause' => array(), 'order' => '' ); /** * file object * @var WBVFS_File */ protected $file; /** * explorer object * @var WBVFS_Explorer */ protected $expl; /** * chunk class * @var WBVFS_ChunkUpload */ protected $chunk; /** * References * @var WBDatasource_Reference */ private $ref; /** * run * * run component * * @return array parameter list */ public function run() { $this->ref = WBClass::create('WBDatasource_Reference'); $this->ref->setNamespace('wb')->setTag('vfsfile'); $config = $this->config; foreach ($config as $k => &$v) { if (is_array($v)) { $v = ''; } } // display media file if ($this->config['process'] == 'display') { $this->tmpl->addGLobalVars($config, 'config_'); $this->displayVFSFile(); return $this->config; } if (!$this->isUserInGroup($this->config['requiredgroup'])) { $this->tmpl->addGLobalVars($config, 'config_'); $this->loadTemplates('anon'); $fid = 17; return $this->config; } $this->startVFS(); $dir = $this->config['dir']; if (empty($dir) || $dir === '0') { $dir = 0; } else { // verify dir WBClass::load('WBException_Datasource'); try { $node = $this->expl->getDir($dir); } catch (WBException_Datasource $e) { if ($e->getCode() != 'WBDatasource_Tree.1') { throw $e; } $dir = 0; } } $this->config['dir'] = $dir; $cwd = array(); switch ($this->config['process']) { case 'chunkinit': $this->chunkInit(); break; case 'chunkupload': $this->chunkUpload(); break; case 'chunkimport': $this->chunkImport(); break; case 'upload': $this->processForm('upload'); $this->loadTemplates('show'); break; case 'mkdir': $this->processForm('mkdir'); $this->loadTemplates('show'); break; case 'rmdir': if ($this->req->get('force', 'no') != 'yes') { $dirs = $this->expl->lsDir($dir); $cwd['dir_count'] = count($dirs); $files = $this->file->listDir($dir); $cwd['file_count'] = count($files); $this->loadTemplates('rmdir'); break; } $parent = $this->expl->getParentDir($dir); $this->expl->rmDir($dir); $dir = $parent['id']; $this->config['dir'] = $dir; $this->loadTemplates('show'); break; case 'exec': $id = $this->req->get('id'); $file = $this->file->loadByObscureId($id); if (!$file->isOK()) { $this->loadTemplates('show'); break; } $dir = $file->getDirId(); $cmd = strtolower($this->req->get('cmd', null)); $arg = $this->req->get('cmdarg', array()); if ($cmd == 'save') { $save = array(); foreach (array('name', 'description') as $k) { if (isset($arg[$k])) { if (get_magic_quotes_gpc()) { $arg[$k] = stripslashes($arg[$k]); } $save[$k] = strval(substr($arg[$k], 0, 240)) . ''; } } if (!empty($save)) { $file->save($save); } $this->loadTemplates('show'); break; } // execute command for mime type $mimeHdl = WBClass::create('WBVFS_Mime_' . ucfirst($file->getMime())); /** @var $mimeHdl WBVFS_Mime */ $mimeHdl->setVirtualFile($file); $mimeHdl->execute($cmd, $arg); $file = $mimeHdl->getVirtualFile(); $this->loadTemplates('show'); break; case 'rm': $id = $this->req->get('id'); $file = $this->file->loadByObscureId($id); if (!$file->isOK()) { $this->loadTemplates('show'); break; } $dir = $file->getDirId(); $this->config['dir'] = $dir; if ($this->req->get('force', 'no') != 'yes') { $this->loadTemplates('rm'); $this->tmpl->addGlobalVars($file->get()); break; } $file->delete(); $this->loadTemplates('show'); break; case 'mvdir': case 'mvfile': $gotodes = intval($this->req->get('gotodes', 0)); if (1 > $gotodes) { $src = $this->req->get('src'); if (0 < count($src)) { $dir = $src[0]; if ('mvfile' == $this->config['process']) { $file = $this->file->loadByObscureId($src[0]); $dir = $file->getDirId(); } } } $this->processForm($this->config['process']); $this->loadTemplates('show'); break; case 'show'; $this->loadTemplates('show'); break; case 'find': $this->loadTemplates('find'); $query = $this->req->get('query', ''); $this->tmpl->addGlobalVar('find_query', $query); $this->find($dir, $query); break; case 'file'; $this->loadTemplates('file'); break; case 'dir'; $this->loadTemplates('dir'); break; } $cwd = array_merge($cwd, $this->expl->getDir($dir)); if (empty($cwd['id'])) { $cwd['id'] = "0"; } $cwd['parent'] = $this->expl->getParentDirId($dir); $cwd['path'] = $this->expl->getPath($dir, true); // list parent folders if ($this->tmpl->exists('parent_list_entry')) { $parents = $this->expl->getParent($dir); $this->tmpl->addRows('parent_list_entry', $parents); $cwd['parent_count'] = count($parents); } // list sub folders if ($this->tmpl->exists('dir_list_entry')) { $dirs = $this->getDirList($dir); $this->tmpl->addRows('dir_list_entry', $dirs); $cwd['dir_count'] = count($dirs); } // list files in folder if ($this->tmpl->exists('file_list_entry')) { $files = $this->getFileList($dir); $this->tmpl->addRows('file_list_entry', $files); $cwd['file_count'] = count($files); } // display upload form if ($this->tmpl->exists('formupload')) { $form = $this->getForm('upload'); $this->renderForm($form); } // display mkdir form if ($this->tmpl->exists('formmkdir')) { $form = $this->getForm('mkdir'); $this->renderForm($form); } $config = $this->config; foreach ($config as $k => &$v) { if (is_array($v)) { $v = ''; } } $this->tmpl->addGlobalVars($config, 'config_'); $this->tmpl->addGlobalVars($cwd, 'cwd_'); return $this->config; } /** * Get list of subfolders * * @see WBVFS_Explorer::lsDir() * @param string $dir folder id * @return array $dirs */ protected function getDirList($dir) { return $this->expl->lsDir($dir); } /** * Get list of subfolders * * @see WBVFS_File::listDir() * @param string $dir folder id * @return array $files */ protected function getFileList($dir) { $list = $this->file->listDir($dir); if ('reverse' == $this->config['order']) { $list = array_reverse($list); } foreach ($list as &$l) { $this->injectReferences($l); } return $list; } /** * Init chunked uploads * * Create one or more upload ids for chunked file upload */ private function chunkInit() { $data = json_decode($this->req->get("data", ""), true); $upload = array(); $this->startChunkUploader(); foreach ($data as $d) { try{ $d['uploadid'] = $this->chunk->create($d); } catch(WBException $e) { $d['uploadid'] = false; $d['error'] = $e->getCode(); } $upload[] = $d; } $this->sendJSONResponse($upload); } /** * Upload file's chunk * * Receive uploaded file and add it as chunk */ private function chunkUpload() { $this->startChunkUploader(); $this->chunk->loadById($this->req->get('uploadid')); $pos = $this->chunk->add(-1, file_get_contents($_FILES['file']['tmp_name'])); $data = array( 'uploadid' => $this->chunk->getId(), 'position' => $pos ); $this->sendJSONResponse($data); } /** * Import chunk * * Use request parameter: uploadid and name to create VFS File */ private function chunkImport() { $this->startChunkUploader(); $this->chunk->loadById($this->req->get('uploadid')); $this->file->setCurrentDir($this->config['dir']); try{ $this->chunk->import($this->file, $this->req->get('name')); $data = array( 'uploadid' => $this->chunk->getId(), 'vfsfile' => $this->file->getId() ); $this->chunk->rm(); } catch(WBException $e) { $data = array( 'uploadid' => $this->chunk->getId(), 'vfsfile' => false, 'error' => $e->getCode() ); } $this->sendJSONResponse($data); } /** * Use dir of matching file * * load first matching file and select folder * * @see WBVFS_File::loadByName() * @param string $dir directory id * @param string $query pattern of filename */ private function find(&$dir, $query) { $this->startVFS(); $this->file = $this->file->loadByName($query, $this->config['user']); if (!$this->file->isOK()) { return; } $dir = $this->file->getDirId(); } /** * display single media file * */ private function displayVFSFile() { $clause = array(); if (!empty($this->config['clause']) && is_array($this->config['clause'])) { $clause = $this->config['clause']; } if (empty($this->config['__path'])) { $this->loadTemplates('displayList'); // list recent files $this->table = WBClass::create('WBDatasource_Table'); $opt = array( 'limit' => 100, 'order' => array( 'field' => 'created', 'asc' => 0 ) ); // description is required $clause[] = array( 'field' => 'description', 'relation' => 'not', 'value' => '' ); if (!empty($this->config['mimemajor'])) { $clause[] = array( 'field' => 'mimemajor', 'value' => $this->config['mimemajor'] ); } $list = $this->table->get('vfsfile', null, null, $clause, $opt); $data = array( 'file_count' => count($list) ); $this->tmpl->addGlobalVars($data); if (!$this->tmpl->exists('file_list_entry')) { return; } $this->tmpl->addRows('file_list_entry', $list); return; } $this->file = WBClass::create('WBVFS_File'); $this->file = $this->file->loadByObscureId($this->config['__path'][0]); if (!$this->file->isOK()) { $this->loadTemplates('displayInvalid'); return; } $this->loadTemplates('display'); $data = array( 'vfsdid' => $this->file->getDirId(), 'mimemajor' => $this->file->getMime(WBVFS_File::MIME_MAJOR), 'mimeminor' => $this->file->getMime(WBVFS_File::MIME_MINOR), 'name' => $this->file->getName(), 'name_uri' => urlencode($this->file->getName()), 'name_slashes' => addslashes($this->file->getName()), 'description' => $this->file->getDescription(), 'obscureid' => $this->file->getObscureId(), 'uri' => $this->file->getUri(), 'url' => $this->file->getUrl(null, true), 'url_download' => $this->file->getUrl('application', true), 'url_image' => $this->file->getUrl('image', true), 'server' => $this->file->getServer(), 'size' => $this->file->getSize(), 'views' => $this->file->getViews(), 'md5' => $this->file->getMd5(), 'reference_count' => 0 ); $this->injectReferences($data); $this->tmpl->addGlobalVars($data); // mangle bubble values if (empty($this->bubbles)) { return; } $search = array(); $replace = array(); foreach ($data as $key => $value) { $search[] = '%' . $key; $replace[] = $value; } foreach ($this->bubbles as $name => &$bubble) { $bubble = str_replace($search, $replace, $bubble); } } /** * Inject reference count data * * @todo re-implement add gallery support for references * @param array $data of vfsfile */ private function injectReferences(&$data) { $ref = $this->ref->get($data['id']); $data['reference_count'] = count($ref); if ($this->tmpl->exists('reference_list_entry')) { $this->tmpl->addRows('reference_list_entry', $ref); } } /** * receive output * * fetch output of this content component * * @return string */ public function getString() { return $this->tmpl->getParsedTemplate('snippet'); } /** * mkdir validated * * @param patForms $form * @param array $values * @return bool always false */ public function onMkdirValid($form, $values) { $this->expl->mkDir($this->config['dir'], $values['dirname']); return false; } /** * move folders * * move given folders to new destination * * @param patForms $form * @param array $values * @return bool always false */ public function onMvdirValid($form, $values) { foreach ($values['src'] as $src) { if (0 == $values['des']) { $values['des'] = null; } $this->expl->mvDir($src, $values['des']); } return false; } /** * move files * * move all files to a single folder * * @param patForms $form * @param array $values * @return bool always false */ public function onMvfileValid($form, $values) { $save = array('dir' => $values['des']); foreach ($values['src'] as $src) { $file = $this->file->loadByObscureId($src); $file->save($save); } return false; } /** * Upload was valid * * Don't do a thing * * @param patForms $form * @param array $values * @return bool always false */ public function onUploadValid($form, $values) { return false; } /** * start VFS objects * * */ protected function startVFS() { if ($this->file) { return; } if (empty($this->config['user'])) { $this->config['user'] = $this->user->getId(); } // populte user mapping $this->sess->set('vfsfile_user', $this->config['user']); $param = array( 'user' => $this->config['user'] ); $this->file = WBClass::create('WBVFS_File', $param); $this->expl = WBClass::create('WBVFS_Explorer', $param); $this->file->switchTranslation($this->config['translation']); $this->expl->switchTranslation($this->config['translation']); } /** * Create chunk uploader object * * Set user to current user */ private function startChunkUploader() { if ($this->chunk) { return; } $this->chunk = WBClass::create('WBVFS_ChunkUpload'); $this->chunk->setUser($this->user->getId()); } /** * Send response as JSON data * * Pack data in JSON string and send as response. * CAUTION: this method will exit the programme */ private function sendJSONResponse($data) { $json = array( 'data' => $data ); $res = WBClass::create('WBResponse'); /** @var $res WBResponse */ $res->add(json_encode($json)); $res->addHeader('Content-Type', 'text/json'); $res->send($this->req); exit(0); } /** * location of form config * * Return sub directory where form element definitions are located * * @return string folder */ protected function getFormConfigDir() { return 'vfs'; } /** * get element definition for this form * * Use parent's method, but add destination folder for Vfsfile-elements * * @param string $name */ public function getFormElementList($name) { $elements = parent::getFormElementList($name); /* * Simply copy src input values to list of allowed values * The VFSIsDir / VFSIsFile rule does the rest */ if (isset($elements['src'])) { $values = array(); $src = $this->req->get('src', array()); foreach ($src as $s) { $values[] = array( 'value' => $s, 'lable' => $s ); } $elements['src']['attributes']['values'] = $values; } foreach ($elements as &$e) { // add destination folder for new files if ($e['type'] == 'Vfsfile') { $e['attributes']['des'] = $this->config['dir']; $e['attributes']['user'] = $this->config['user']; } } return $elements; } /** * Add form rules to just created form * * For form: "mvdir" and "mvfile" * - verify that destination is a proper folder * - Verify that destination folder is not in source list * - verify that sources are either files or folders * * @param patForms $form object * @param string name of the xml- and template-filename * @param array $elements list of elements current form * @return bool true on success */ protected function insertFormRules(&$form, $name, $elements) { $ruleVfs = null; switch ($name) { case 'mvfile': // rule: VFSIsFile $ruleVfs = patForms::createRule('VFSIsFile'); // fall through case 'mvdir': // rule: VFSIsDir if (!$ruleVfs) { $ruleVfs = patForms::createRule('VFSIsDir'); } $config = array( 'user' => $this->config['user'] ); $ruleVfs->setConfig($config); $src = $form->getElementByName('src'); $src->addRule($ruleVfs); // rule: InOther $ruleInOther = patForms::createRule('InOther'); $config = array( 'in' => 'no', 'other' => $src ); $ruleInOther->setConfig($config); $form->getElementByName('des')->addRule($ruleInOther); break; default: break; } return true; } } ?>