PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/pastor399/newcastleunifc
PHP | 182 lines | 120 code | 29 blank | 33 comment | 7 complexity | 9e9e7f181ad99923a2408d05c01f379d MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_media
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 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. * File Media Controller
  14. *
  15. * @package Joomla.Administrator
  16. * @subpackage com_media
  17. * @since 1.6
  18. */
  19. class MediaControllerFile extends JControllerLegacy
  20. {
  21. /**
  22. * Upload a file
  23. *
  24. * @return void
  25. *
  26. * @since 1.5
  27. */
  28. function upload()
  29. {
  30. $params = JComponentHelper::getParams('com_media');
  31. // Check for request forgeries
  32. if (!JSession::checkToken('request'))
  33. {
  34. $response = array(
  35. 'status' => '0',
  36. 'error' => JText::_('JINVALID_TOKEN')
  37. );
  38. echo json_encode($response);
  39. return;
  40. }
  41. // Get the user
  42. $user = JFactory::getUser();
  43. JLog::addLogger(array('text_file' => 'upload.error.php'), JLog::ALL, array('upload'));
  44. // Get some data from the request
  45. $file = $this->input->files->get('Filedata', '', 'array');
  46. $folder = $this->input->get('folder', '', 'path');
  47. if (
  48. $_SERVER['CONTENT_LENGTH']>($params->get('upload_maxsize', 0) * 1024 * 1024) ||
  49. $_SERVER['CONTENT_LENGTH']>(int)(ini_get('upload_max_filesize'))* 1024 * 1024 ||
  50. $_SERVER['CONTENT_LENGTH']>(int)(ini_get('post_max_size'))* 1024 * 1024 ||
  51. $_SERVER['CONTENT_LENGTH']>(int)(ini_get('memory_limit'))* 1024 * 1024
  52. )
  53. {
  54. $response = array(
  55. 'status' => '0',
  56. 'error' => JText::_('COM_MEDIA_ERROR_WARNFILETOOLARGE')
  57. );
  58. echo json_encode($response);
  59. return;
  60. }
  61. // Set FTP credentials, if given
  62. JClientHelper::setCredentialsFromRequest('ftp');
  63. // Make the filename safe
  64. $file['name'] = JFile::makeSafe($file['name']);
  65. if (isset($file['name']))
  66. {
  67. // The request is valid
  68. $err = null;
  69. $filepath = JPath::clean(COM_MEDIA_BASE . '/' . $folder . '/' . strtolower($file['name']));
  70. if (!MediaHelper::canUpload($file, $err))
  71. {
  72. JLog::add('Invalid: ' . $filepath . ': ' . $err, JLog::INFO, 'upload');
  73. $response = array(
  74. 'status' => '0',
  75. 'error' => JText::_($err)
  76. );
  77. echo json_encode($response);
  78. return;
  79. }
  80. // Trigger the onContentBeforeSave event.
  81. JPluginHelper::importPlugin('content');
  82. $dispatcher = JEventDispatcher::getInstance();
  83. $object_file = new JObject($file);
  84. $object_file->filepath = $filepath;
  85. $result = $dispatcher->trigger('onContentBeforeSave', array('com_media.file', &$object_file));
  86. if (in_array(false, $result, true))
  87. {
  88. // There are some errors in the plugins
  89. JLog::add('Errors before save: ' . $object_file->filepath . ' : ' . implode(', ', $object_file->getErrors()), JLog::INFO, 'upload');
  90. $response = array(
  91. 'status' => '0',
  92. 'error' => JText::plural('COM_MEDIA_ERROR_BEFORE_SAVE', count($errors = $object_file->getErrors()), implode('<br />', $errors))
  93. );
  94. echo json_encode($response);
  95. return;
  96. }
  97. if (JFile::exists($object_file->filepath))
  98. {
  99. // File exists
  100. JLog::add('File exists: ' . $object_file->filepath . ' by user_id ' . $user->id, JLog::INFO, 'upload');
  101. $response = array(
  102. 'status' => '0',
  103. 'error' => JText::_('COM_MEDIA_ERROR_FILE_EXISTS')
  104. );
  105. echo json_encode($response);
  106. return;
  107. }
  108. elseif (!$user->authorise('core.create', 'com_media'))
  109. {
  110. // File does not exist and user is not authorised to create
  111. JLog::add('Create not permitted: ' . $object_file->filepath . ' by user_id ' . $user->id, JLog::INFO, 'upload');
  112. $response = array(
  113. 'status' => '0',
  114. 'error' => JText::_('COM_MEDIA_ERROR_CREATE_NOT_PERMITTED')
  115. );
  116. echo json_encode($response);
  117. return;
  118. }
  119. if (!JFile::upload($object_file->tmp_name, $object_file->filepath))
  120. {
  121. // Error in upload
  122. JLog::add('Error on upload: ' . $object_file->filepath, JLog::INFO, 'upload');
  123. $response = array(
  124. 'status' => '0',
  125. 'error' => JText::_('COM_MEDIA_ERROR_UNABLE_TO_UPLOAD_FILE')
  126. );
  127. echo json_encode($response);
  128. return;
  129. }
  130. else
  131. {
  132. // Trigger the onContentAfterSave event.
  133. $dispatcher->trigger('onContentAfterSave', array('com_media.file', &$object_file, true));
  134. JLog::add($folder, JLog::INFO, 'upload');
  135. $response = array(
  136. 'status' => '1',
  137. 'error' => JText::sprintf('COM_MEDIA_UPLOAD_COMPLETE', substr($object_file->filepath, strlen(COM_MEDIA_BASE)))
  138. );
  139. echo json_encode($response);
  140. return;
  141. }
  142. }
  143. else
  144. {
  145. $response = array(
  146. 'status' => '0',
  147. 'error' => JText::_('COM_MEDIA_ERROR_BAD_REQUEST')
  148. );
  149. echo json_encode($response);
  150. return;
  151. }
  152. }
  153. }