PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/joomla/administrator/components/com_media/controllers/file.php

https://gitlab.com/ricardosanchez/prueba
PHP | 311 lines | 172 code | 64 blank | 75 comment | 30 complexity | 7379e966f9b52a1eb0c44f55a0e362eb MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_media
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. jimport('joomla.filesystem.file');
  11. jimport('joomla.filesystem.folder');
  12. /**
  13. * Media File Controller
  14. *
  15. * @since 1.5
  16. */
  17. class MediaControllerFile extends JControllerLegacy
  18. {
  19. /**
  20. * The folder we are uploading into
  21. *
  22. * @var string
  23. */
  24. protected $folder = '';
  25. /**
  26. * Upload one or more files
  27. *
  28. * @return boolean
  29. *
  30. * @since 1.5
  31. */
  32. public function upload()
  33. {
  34. // Check for request forgeries
  35. JSession::checkToken('request') or jexit(JText::_('JINVALID_TOKEN'));
  36. $params = JComponentHelper::getParams('com_media');
  37. // Get some data from the request
  38. $files = $this->input->files->get('Filedata', '', 'array');
  39. $return = JFactory::getSession()->get('com_media.return_url');
  40. $this->folder = $this->input->get('folder', '', 'path');
  41. // Don't redirect to an external URL.
  42. if (!JUri::isInternal($return))
  43. {
  44. $return = '';
  45. }
  46. // Set the redirect
  47. if ($return)
  48. {
  49. $this->setRedirect($return . '&folder=' . $this->folder);
  50. }
  51. else
  52. {
  53. $this->setRedirect('index.php?option=com_media&folder=' . $this->folder);
  54. }
  55. // Authorize the user
  56. if (!$this->authoriseUser('create'))
  57. {
  58. return false;
  59. }
  60. // Total length of post back data in bytes.
  61. $contentLength = (int) $_SERVER['CONTENT_LENGTH'];
  62. // Instantiate the media helper
  63. $mediaHelper = new JHelperMedia;
  64. // Maximum allowed size of post back data in MB.
  65. $postMaxSize = $mediaHelper->toBytes(ini_get('post_max_size'));
  66. // Maximum allowed size of script execution in MB.
  67. $memoryLimit = $mediaHelper->toBytes(ini_get('memory_limit'));
  68. // Check for the total size of post back data.
  69. if (($postMaxSize > 0 && $contentLength > $postMaxSize)
  70. || ($memoryLimit != -1 && $contentLength > $memoryLimit))
  71. {
  72. JError::raiseWarning(100, JText::_('COM_MEDIA_ERROR_WARNUPLOADTOOLARGE'));
  73. return false;
  74. }
  75. $uploadMaxSize = $params->get('upload_maxsize', 0) * 1024 * 1024;
  76. $uploadMaxFileSize = $mediaHelper->toBytes(ini_get('upload_max_filesize'));
  77. // Perform basic checks on file info before attempting anything
  78. foreach ($files as &$file)
  79. {
  80. $file['name'] = JFile::makeSafe($file['name']);
  81. $file['filepath'] = JPath::clean(implode(DIRECTORY_SEPARATOR, array(COM_MEDIA_BASE, $this->folder, $file['name'])));
  82. if (($file['error'] == 1)
  83. || ($uploadMaxSize > 0 && $file['size'] > $uploadMaxSize)
  84. || ($uploadMaxFileSize > 0 && $file['size'] > $uploadMaxFileSize))
  85. {
  86. // File size exceed either 'upload_max_filesize' or 'upload_maxsize'.
  87. JError::raiseWarning(100, JText::_('COM_MEDIA_ERROR_WARNFILETOOLARGE'));
  88. return false;
  89. }
  90. if (JFile::exists($file['filepath']))
  91. {
  92. // A file with this name already exists
  93. JError::raiseWarning(100, JText::_('COM_MEDIA_ERROR_FILE_EXISTS'));
  94. return false;
  95. }
  96. if (!isset($file['name']))
  97. {
  98. // No filename (after the name was cleaned by JFile::makeSafe)
  99. $this->setRedirect('index.php', JText::_('COM_MEDIA_INVALID_REQUEST'), 'error');
  100. return false;
  101. }
  102. }
  103. // Set FTP credentials, if given
  104. JClientHelper::setCredentialsFromRequest('ftp');
  105. JPluginHelper::importPlugin('content');
  106. $dispatcher = JEventDispatcher::getInstance();
  107. foreach ($files as &$file)
  108. {
  109. // The request is valid
  110. $err = null;
  111. if (!MediaHelper::canUpload($file, $err))
  112. {
  113. // The file can't be uploaded
  114. return false;
  115. }
  116. // Trigger the onContentBeforeSave event.
  117. $object_file = new JObject($file);
  118. $result = $dispatcher->trigger('onContentBeforeSave', array('com_media.file', &$object_file, true));
  119. if (in_array(false, $result, true))
  120. {
  121. // There are some errors in the plugins
  122. JError::raiseWarning(100, JText::plural('COM_MEDIA_ERROR_BEFORE_SAVE', count($errors = $object_file->getErrors()), implode('<br />', $errors)));
  123. return false;
  124. }
  125. if (!JFile::upload($object_file->tmp_name, $object_file->filepath))
  126. {
  127. // Error in upload
  128. JError::raiseWarning(100, JText::_('COM_MEDIA_ERROR_UNABLE_TO_UPLOAD_FILE'));
  129. return false;
  130. }
  131. // Trigger the onContentAfterSave event.
  132. $dispatcher->trigger('onContentAfterSave', array('com_media.file', &$object_file, true));
  133. $this->setMessage(JText::sprintf('COM_MEDIA_UPLOAD_COMPLETE', substr($object_file->filepath, strlen(COM_MEDIA_BASE))));
  134. }
  135. return true;
  136. }
  137. /**
  138. * Check that the user is authorized to perform this action
  139. *
  140. * @param string $action - the action to be peformed (create or delete)
  141. *
  142. * @return boolean
  143. *
  144. * @since 1.6
  145. */
  146. protected function authoriseUser($action)
  147. {
  148. if (!JFactory::getUser()->authorise('core.' . strtolower($action), 'com_media'))
  149. {
  150. // User is not authorised
  151. JError::raiseWarning(403, JText::_('JLIB_APPLICATION_ERROR_' . strtoupper($action) . '_NOT_PERMITTED'));
  152. return false;
  153. }
  154. return true;
  155. }
  156. /**
  157. * Deletes paths from the current path
  158. *
  159. * @return boolean
  160. *
  161. * @since 1.5
  162. */
  163. public function delete()
  164. {
  165. JSession::checkToken('request') or jexit(JText::_('JINVALID_TOKEN'));
  166. // Get some data from the request
  167. $tmpl = $this->input->get('tmpl');
  168. $paths = $this->input->get('rm', array(), 'array');
  169. $folder = $this->input->get('folder', '', 'path');
  170. $redirect = 'index.php?option=com_media&folder=' . $folder;
  171. if ($tmpl == 'component')
  172. {
  173. // We are inside the iframe
  174. $redirect .= '&view=mediaList&tmpl=component';
  175. }
  176. $this->setRedirect($redirect);
  177. // Nothing to delete
  178. if (empty($paths))
  179. {
  180. return true;
  181. }
  182. // Authorize the user
  183. if (!$this->authoriseUser('delete'))
  184. {
  185. return false;
  186. }
  187. // Set FTP credentials, if given
  188. JClientHelper::setCredentialsFromRequest('ftp');
  189. JPluginHelper::importPlugin('content');
  190. $dispatcher = JEventDispatcher::getInstance();
  191. $ret = true;
  192. foreach ($paths as $path)
  193. {
  194. if ($path !== JFile::makeSafe($path))
  195. {
  196. // Filename is not safe
  197. $filename = htmlspecialchars($path, ENT_COMPAT, 'UTF-8');
  198. JError::raiseWarning(100, JText::sprintf('COM_MEDIA_ERROR_UNABLE_TO_DELETE_FILE_WARNFILENAME', substr($filename, strlen(COM_MEDIA_BASE))));
  199. continue;
  200. }
  201. $fullPath = JPath::clean(implode(DIRECTORY_SEPARATOR, array(COM_MEDIA_BASE, $folder, $path)));
  202. $object_file = new JObject(array('filepath' => $fullPath));
  203. if (is_file($object_file->filepath))
  204. {
  205. // Trigger the onContentBeforeDelete event.
  206. $result = $dispatcher->trigger('onContentBeforeDelete', array('com_media.file', &$object_file));
  207. if (in_array(false, $result, true))
  208. {
  209. // There are some errors in the plugins
  210. $errors = $object_file->getErrors();
  211. JError::raiseWarning(100, JText::plural('COM_MEDIA_ERROR_BEFORE_DELETE', count($errors), implode('<br />', $errors)));
  212. continue;
  213. }
  214. $ret &= JFile::delete($object_file->filepath);
  215. // Trigger the onContentAfterDelete event.
  216. $dispatcher->trigger('onContentAfterDelete', array('com_media.file', &$object_file));
  217. $this->setMessage(JText::sprintf('COM_MEDIA_DELETE_COMPLETE', substr($object_file->filepath, strlen(COM_MEDIA_BASE))));
  218. continue;
  219. }
  220. if (is_dir($object_file->filepath))
  221. {
  222. $contents = JFolder::files($object_file->filepath, '.', true, false, array('.svn', 'CVS', '.DS_Store', '__MACOSX', 'index.html'));
  223. if (!empty($contents))
  224. {
  225. // This makes no sense...
  226. $folderPath = substr($object_file->filepath, strlen(COM_MEDIA_BASE));
  227. JError::raiseWarning(100, JText::sprintf('COM_MEDIA_ERROR_UNABLE_TO_DELETE_FOLDER_NOT_EMPTY', $folderPath));
  228. continue;
  229. }
  230. // Trigger the onContentBeforeDelete event.
  231. $result = $dispatcher->trigger('onContentBeforeDelete', array('com_media.folder', &$object_file));
  232. if (in_array(false, $result, true))
  233. {
  234. // There are some errors in the plugins
  235. $errors = $object_file->getErrors();
  236. JError::raiseWarning(100, JText::plural('COM_MEDIA_ERROR_BEFORE_DELETE', count($errors), implode('<br />', $errors)));
  237. continue;
  238. }
  239. $ret &= JFolder::delete($object_file->filepath);
  240. // Trigger the onContentAfterDelete event.
  241. $dispatcher->trigger('onContentAfterDelete', array('com_media.folder', &$object_file));
  242. $this->setMessage(JText::sprintf('COM_MEDIA_DELETE_COMPLETE', substr($object_file->filepath, strlen(COM_MEDIA_BASE))));
  243. }
  244. }
  245. return $ret;
  246. }
  247. }