PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/files/FilesController.php

https://code.google.com/p/ontowiki/
PHP | 380 lines | 277 code | 60 blank | 43 comment | 28 complexity | ae1b0df0138c3120266fd7411b692c3c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Controller for OntoWiki Filter Module
  4. *
  5. * @category OntoWiki
  6. * @package OntoWiki_extensions_components_files
  7. * @author Christoph Rie?&#x; <c.riess.dev@googlemail.com>
  8. * @author Norman Heino <norman.heino@gmail.com>
  9. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  10. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  11. * @version $Id: FilesController.php 4090 2009-08-19 22:10:54Z christian.wuerker $
  12. */
  13. class FilesController extends OntoWiki_Controller_Component
  14. {
  15. protected $_configModel;
  16. /**
  17. * Default action. Forwards to get action.
  18. */
  19. public function __call($action, $params)
  20. {
  21. $this->_forward('get', 'files');
  22. }
  23. public function deleteAction()
  24. {
  25. if ($this->_request->isPost()) {
  26. // delete file resources
  27. foreach ($this->_request->getPost('selectedFiles') as $fileUri) {
  28. $fileUri = rawurldecode($fileUri);
  29. $store = $this->_owApp->erfurt->getStore();
  30. // remove all statements from sysconfig
  31. $store->deleteMatchingStatements(
  32. (string) $this->_getConfigModelUri(),
  33. $fileUri ,
  34. null ,
  35. null
  36. );
  37. // remove file from file system
  38. $pathHashed = _OWROOT
  39. . $this->_privateConfig->path
  40. . DIRECTORY_SEPARATOR
  41. . md5($fileUri);
  42. unlink($pathHashed);
  43. }
  44. $url = new OntoWiki_Url(array('controller' => 'files', 'action' => 'manage'), array());
  45. $this->_redirect((string) $url);
  46. }
  47. }
  48. public function getAction()
  49. {
  50. $this->_helper->viewRenderer->setNoRender();
  51. $this->_helper->layout->disableLayout();
  52. // TODO: check acl
  53. $fileUri = $this->_config->urlBase . ltrim($this->_request->getPathInfo(), '/');
  54. $mimeProperty = $this->_privateConfig->mime->property;
  55. $store = $this->_owApp->erfurt->getStore();
  56. $query = new Erfurt_Sparql_SimpleQuery();
  57. $query->setProloguePart('SELECT DISTINCT ?mime_type')
  58. ->addFrom((string) $this->_getConfigModelUri())
  59. ->setWherePart('
  60. WHERE {
  61. <' . $fileUri . '> <' . $mimeProperty . '> ?mime_type.
  62. }');
  63. if ($result = $store->sparqlQuery($query, array('use_ac' => false))) {
  64. $mimeType = $result[0]['mime_type'];
  65. } else {
  66. $mimeType = 'text/plain';
  67. }
  68. $response = $this->getResponse();
  69. $response->setRawHeader('Content-Type:' . $mimeType);
  70. $pathHashed = _OWROOT
  71. . $this->_privateConfig->path
  72. . DIRECTORY_SEPARATOR
  73. . md5($fileUri);
  74. if (is_readable($pathHashed)) {
  75. $response->setBody(file_get_contents($pathHashed));
  76. }
  77. }
  78. public function manageAction()
  79. {
  80. $mimeProperty = $this->_privateConfig->mime->property;
  81. $fileClass = $this->_privateConfig->class;
  82. $fileModel = $this->_privateConfig->model;
  83. $store = $this->_owApp->erfurt->getStore();
  84. $query = new Erfurt_Sparql_SimpleQuery();
  85. $query->setProloguePart('SELECT DISTINCT ?mime_type ?uri')
  86. ->addFrom((string) $this->_getConfigModelUri())
  87. ->setWherePart('
  88. WHERE {
  89. ?uri a <' . $fileClass . '>.
  90. ?uri <' . $fileModel . '> <' . (string) $this->_owApp->selectedModel . '>.
  91. ?uri <' . $mimeProperty . '> ?mime_type.
  92. }')
  93. ->setOrderClause('?uri')
  94. ->setLimit(10); // TODO: paging
  95. if ($result = $store->sparqlQuery($query, array('use_ac' => false))) {
  96. $files = array();
  97. foreach ($result as $row) {
  98. if (is_readable($this->getFullPath($row['uri']))) {
  99. array_push($files, $row);
  100. }
  101. }
  102. $this->view->files = $files;
  103. } else {
  104. $this->view->files = array();
  105. }
  106. $this->view->placeholder('main.window.title')->set($this->_owApp->translate->_('File Manager'));
  107. OntoWiki_Navigation::disableNavigation();
  108. $toolbar = $this->_owApp->toolbar;
  109. $filePath = _OWROOT
  110. . rtrim($this->_privateConfig->path, '/')
  111. . DIRECTORY_SEPARATOR;
  112. $url = new OntoWiki_Url(array('controller' => 'files', 'action' => 'upload'), array());
  113. if (is_writable($filePath)) {
  114. $toolbar->appendButton(
  115. OntoWiki_Toolbar::DELETE,
  116. array('name' => 'Delete Files', 'class' => 'submit actionid', 'id' => 'filemanagement-delete')
  117. );
  118. $toolbar->appendButton(
  119. OntoWiki_Toolbar::ADD,
  120. array('name' => 'Upload File', 'class' => 'upload-file', 'url' => (string) $url)
  121. );
  122. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  123. } else {
  124. $msgString = sprintf(
  125. $this->_owApp->translate->_('Directory "%s" is not writeable. To upload files set it writable.'),
  126. rtrim($this->_privateConfig->path, '/') . DIRECTORY_SEPARATOR
  127. );
  128. $this->_owApp->appendMessage(
  129. new OntoWiki_Message($msgString, OntoWiki_Message::INFO)
  130. );
  131. }
  132. if (!defined('ONTOWIKI_REWRITE')) {
  133. $this->_owApp->appendMessage(
  134. new OntoWiki_Message('Rewrite mode is off. File URIs may not be accessible.', OntoWiki_Message::WARNING)
  135. );
  136. return;
  137. }
  138. $url->action = 'delete';
  139. $this->view->formActionUrl = (string) $url;
  140. $this->view->formMethod = 'post';
  141. $this->view->formClass = 'simple-input input-justify-left';
  142. $this->view->formName = 'filemanagement-delete';
  143. }
  144. public function uploadAction()
  145. {
  146. // default file URI
  147. $defaultUri = $this->_config->urlBase . 'files/';
  148. // store for sparql queries
  149. $store = $this->_owApp->erfurt->getStore();
  150. // DMS NS var
  151. $DMS_NS = $this->_privateConfig->DMS_NS;
  152. // check if DMS needs to be imported
  153. if ($store->isModelAvailable($DMS_NS) && $this->_privateConfig->import_DMS) {
  154. $this->_checkDMS();
  155. }
  156. $url = new OntoWiki_Url(array('controller' => 'files', 'action' => 'upload'), array());
  157. // check for POST'ed data
  158. if ($this->_request->isPost()) {
  159. if ($_FILES['upload']['error'] == UPLOAD_ERR_OK) {
  160. // upload ok, move file
  161. $fileUri = $this->_request->getPost('file_uri');
  162. $fileName = $_FILES['upload']['name'];
  163. $tmpName = $_FILES['upload']['tmp_name'];
  164. $mimeType = $_FILES['upload']['type'];
  165. // check for unchanged uri
  166. if ($fileUri == $defaultUri) {
  167. $fileUri = $defaultUri
  168. . 'file'
  169. . (count(scandir(_OWROOT . $this->_privateConfig->path)) - 2);
  170. }
  171. // build path
  172. $pathHashed = _OWROOT
  173. . $this->_privateConfig->path
  174. . DIRECTORY_SEPARATOR
  175. . md5($fileUri);
  176. // move file
  177. if (move_uploaded_file($tmpName, $pathHashed)) {
  178. $mimeProperty = $this->_privateConfig->mime->property;
  179. $fileClass = $this->_privateConfig->class;
  180. $fileModel = $this->_privateConfig->model;
  181. // use super class as default
  182. $fileClassLocal = 'http://xmlns.com/foaf/0.1/Document';
  183. // use mediaType-ontologie if available
  184. if ($store->isModelAvailable($DMS_NS)) {
  185. $allTypes = $store->sparqlQuery(
  186. Erfurt_Sparql_SimpleQuery::initWithString(
  187. 'SELECT * FROM <' . $DMS_NS . '>
  188. WHERE {
  189. ?type a <' . EF_OWL_CLASS . '> .
  190. OPTIONAL { ?type <' . $DMS_NS . 'mimeHint> ?mimeHint . }
  191. OPTIONAL { ?type <' . $DMS_NS . 'suffixHint> ?suffixHint . }
  192. } ORDER BY ?type'
  193. )
  194. );
  195. $mimeHintArray = array();
  196. $suffixHintArray = array();
  197. // check for better suited class
  198. foreach ($allTypes as $singleType) {
  199. if (!empty($singleType['mimeHint'])) {
  200. $mimeHintArray[$singleType['mimeHint']] = $singleType['type'];
  201. }
  202. if (!empty($singleType['suffixHint'])) {
  203. $suffixHintArray[$singleType['suffixHint']] = $singleType['type'];
  204. }
  205. }
  206. $suffixType = substr($fileName ,strrpos($fileName,'.'));
  207. if (array_key_exists($suffixType, $suffixHintArray)) {
  208. $fileClassLocal = $suffixHintArray[$suffixType];
  209. }
  210. if (array_key_exists($mimeType, $mimeHintArray)) {
  211. $fileClassLocal = $mimeHintArray[$mimeType];
  212. }
  213. }
  214. // add file resource as instance in local model
  215. $store->addStatement(
  216. (string) $this->_owApp->selectedModel ,
  217. $fileUri ,
  218. EF_RDF_TYPE ,
  219. array('value' => $fileClassLocal, 'type' => 'uri')
  220. );
  221. // add file resource as instance in system model
  222. $store->addStatement(
  223. (string) $this->_getConfigModelUri(),
  224. $fileUri ,
  225. EF_RDF_TYPE ,
  226. array('value' => $fileClass, 'type' => 'uri'),
  227. false
  228. );
  229. // add file resource mime type
  230. $store->addStatement(
  231. (string) $this->_getConfigModelUri(),
  232. $fileUri ,
  233. $mimeProperty ,
  234. array('value' => $mimeType, 'type' => 'literal') ,
  235. false
  236. );
  237. // add file resource model
  238. $store->addStatement(
  239. (string) $this->_getConfigModelUri(),
  240. $fileUri ,
  241. $fileModel ,
  242. array('value' => (string) $this->_owApp->selectedModel, 'type' => 'uri') ,
  243. false
  244. );
  245. $url->action = 'manage';
  246. $this->_redirect((string) $url);
  247. }
  248. } else {
  249. $this->_owApp->appendMessage(
  250. new OntoWiki_Message('Error during file upload.', OntoWiki_Message::ERROR)
  251. );
  252. }
  253. }
  254. // $this->_helper->viewRenderer->setNoRender();
  255. // $this->_helper->layout->disableLayout();
  256. $this->view->placeholder('main.window.title')->set($this->_owApp->translate->_('Upload File'));
  257. OntoWiki_Navigation::disableNavigation();
  258. $toolbar = $this->_owApp->toolbar;
  259. $url->action = 'manage';
  260. $toolbar->appendButton(OntoWiki_Toolbar::SUBMIT, array('name' => 'Upload File'))
  261. ->appendButton(OntoWiki_Toolbar::EDIT, array('name' => 'File Manager', 'class' => '', 'url' => (string) $url));
  262. $this->view->defaultUri = $defaultUri;
  263. $this->view->placeholder('main.window.toolbar')->set($toolbar);
  264. $url->action = 'upload';
  265. $this->view->formActionUrl = (string) $url;
  266. $this->view->formMethod = 'post';
  267. $this->view->formClass = 'simple-input input-justify-left';
  268. $this->view->formName = 'fileupload';
  269. $this->view->formEncoding = 'multipart/form-data';
  270. if (!is_writable($this->_privateConfig->path)) {
  271. $this->_owApp->appendMessage(
  272. new OntoWiki_Message('Uploads folder is not writable.', OntoWiki_Message::WARNING)
  273. );
  274. return;
  275. }
  276. // FIX: http://www.webmasterworld.com/macintosh_webmaster/3300569.htm
  277. header('Connection: close');
  278. }
  279. protected function getFullPath($fileUri)
  280. {
  281. $pathHashed = _OWROOT
  282. . $this->_privateConfig->path
  283. . DIRECTORY_SEPARATOR
  284. . md5($fileUri);
  285. return $pathHashed;
  286. }
  287. /**
  288. * method to check import of DMS Schema in current model
  289. */
  290. private function _checkDMS() {
  291. $store = $this->_owApp->erfurt->getStore();
  292. // checking if model is imported
  293. $allImports = $this->_owApp->selectedModel->sparqlQuery(
  294. Erfurt_Sparql_SimpleQuery::initWithString(
  295. 'SELECT *
  296. WHERE {
  297. <' . (string) $this->_owApp->selectedModel . '> <' . EF_OWL_IMPORTS . '> ?import .
  298. }'
  299. )
  300. );
  301. // import if missing
  302. if (!in_array(array('import' => $this->_privateConfig->DMS_NS), $allImports)) {
  303. $this->_owApp->selectedModel->addStatement(
  304. (string) $this->_owApp->selectedModel,
  305. EF_OWL_IMPORTS,
  306. array('value' => $this->_privateConfig->DMS_NS, 'type' => 'uri'),
  307. false
  308. );
  309. } else {
  310. // do nothing
  311. }
  312. }
  313. protected function _getConfigModelUri()
  314. {
  315. if (null === $this->_configModel) {
  316. $this->_configModel = Erfurt_App::getInstance()->getConfig()->sysont->modelUri;
  317. }
  318. return $this->_configModel;
  319. }
  320. }