PageRenderTime 24ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/chrisinammo/arthurmcneil
PHP | 186 lines | 126 code | 18 blank | 42 comment | 34 complexity | a1e49db80793270cf13698172d66180a MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * @version $Id: file.php 10094 2008-03-02 04:35:10Z instance $
  4. * @package Joomla
  5. * @subpackage Content
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant to the
  9. * GNU General Public License, and as distributed it includes or is derivative
  10. * of works licensed under the GNU General Public License or other free or open
  11. * source software licenses. See COPYRIGHT.php for copyright notices and
  12. * details.
  13. */
  14. // Check to ensure this file is included in Joomla!
  15. defined('_JEXEC') or die( 'Restricted access' );
  16. jimport('joomla.filesystem.file');
  17. jimport('joomla.filesystem.folder');
  18. /**
  19. * Weblinks Weblink Controller
  20. *
  21. * @package Joomla
  22. * @subpackage Weblinks
  23. * @since 1.5
  24. */
  25. class MediaControllerFile extends MediaController
  26. {
  27. /**
  28. * Upload a file
  29. *
  30. * @since 1.5
  31. */
  32. function upload()
  33. {
  34. global $mainframe;
  35. // Check for request forgeries
  36. JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );
  37. $file = JRequest::getVar( 'Filedata', '', 'files', 'array' );
  38. $folder = JRequest::getVar( 'folder', '', '', 'path' );
  39. $format = JRequest::getVar( 'format', 'html', '', 'cmd');
  40. $return = JRequest::getVar( 'return-url', null, 'post', 'base64' );
  41. $err = null;
  42. // Set FTP credentials, if given
  43. jimport('joomla.client.helper');
  44. JClientHelper::setCredentialsFromRequest('ftp');
  45. // Make the filename safe
  46. jimport('joomla.filesystem.file');
  47. $file['name'] = JFile::makeSafe($file['name']);
  48. if (isset($file['name'])) {
  49. $filepath = JPath::clean(COM_MEDIA_BASE.DS.$folder.DS.strtolower($file['name']));
  50. if (!MediaHelper::canUpload( $file, $err )) {
  51. if ($format == 'json') {
  52. jimport('joomla.error.log');
  53. $log = &JLog::getInstance('upload.error.php');
  54. $log->addEntry(array('comment' => 'Invalid: '.$filepath.': '.$err));
  55. header('HTTP/1.0 415 Unsupported Media Type');
  56. jexit('Error. Unsupported Media Type!');
  57. } else {
  58. JError::raiseNotice(100, JText::_($err));
  59. // REDIRECT
  60. if ($return) {
  61. $mainframe->redirect(base64_decode($return).'&folder='.$folder);
  62. }
  63. return;
  64. }
  65. }
  66. if (JFile::exists($filepath)) {
  67. if ($format == 'json') {
  68. jimport('joomla.error.log');
  69. $log = &JLog::getInstance('upload.error.php');
  70. $log->addEntry(array('comment' => 'File already exists: '.$filepath));
  71. header('HTTP/1.0 409 Conflict');
  72. jexit('Error. File already exists');
  73. } else {
  74. JError::raiseNotice(100, JText::_('Error. File already exists'));
  75. // REDIRECT
  76. if ($return) {
  77. $mainframe->redirect(base64_decode($return).'&folder='.$folder);
  78. }
  79. return;
  80. }
  81. }
  82. if (!JFile::upload($file['tmp_name'], $filepath)) {
  83. if ($format == 'json') {
  84. jimport('joomla.error.log');
  85. $log = &JLog::getInstance('upload.error.php');
  86. $log->addEntry(array('comment' => 'Cannot upload: '.$filepath));
  87. header('HTTP/1.0 400 Bad Request');
  88. jexit('Error. Unable to upload file');
  89. } else {
  90. JError::raiseWarning(100, JText::_('Error. Unable to upload file'));
  91. // REDIRECT
  92. if ($return) {
  93. $mainframe->redirect(base64_decode($return).'&folder='.$folder);
  94. }
  95. return;
  96. }
  97. } else {
  98. if ($format == 'json') {
  99. jimport('joomla.error.log');
  100. $log = &JLog::getInstance();
  101. $log->addEntry(array('comment' => $folder));
  102. jexit('Upload complete');
  103. } else {
  104. $mainframe->enqueueMessage(JText::_('Upload complete'));
  105. // REDIRECT
  106. if ($return) {
  107. $mainframe->redirect(base64_decode($return).'&folder='.$folder);
  108. }
  109. return;
  110. }
  111. }
  112. } else {
  113. $mainframe->redirect('index.php', 'Invalid Request', 'error');
  114. }
  115. }
  116. /**
  117. * Deletes paths from the current path
  118. *
  119. * @param string $listFolder The image directory to delete a file from
  120. * @since 1.5
  121. */
  122. function delete()
  123. {
  124. global $mainframe;
  125. // Set FTP credentials, if given
  126. jimport('joomla.client.helper');
  127. JClientHelper::setCredentialsFromRequest('ftp');
  128. // Get some data from the request
  129. $tmpl = JRequest::getCmd( 'tmpl' );
  130. $paths = JRequest::getVar( 'rm', array(), '', 'array' );
  131. $folder = JRequest::getVar( 'folder', '', '', 'path');
  132. // Initialize variables
  133. $msg = array();
  134. $ret = true;
  135. if (count($paths)) {
  136. foreach ($paths as $path)
  137. {
  138. if ($path !== JFile::makeSafe($path)) {
  139. JError::raiseWarning(100, JText::_('Unable to delete:').htmlspecialchars($path, ENT_COMPAT, 'UTF-8').' '.JText::_('WARNFILENAME'));
  140. continue;
  141. }
  142. $fullPath = JPath::clean(COM_MEDIA_BASE.DS.$folder.DS.$path);
  143. if (is_file($fullPath)) {
  144. $ret |= !JFile::delete($fullPath);
  145. } else if (is_dir($fullPath)) {
  146. $files = JFolder::files($fullPath, '.', true);
  147. $canDelete = true;
  148. foreach ($files as $file) {
  149. if ($file != 'index.html') {
  150. $canDelete = false;
  151. }
  152. }
  153. if ($canDelete) {
  154. $ret |= !JFolder::delete($fullPath);
  155. } else {
  156. JError::raiseWarning(100, JText::_('Unable to delete:').$fullPath.' '.JText::_('Not Empty!'));
  157. }
  158. }
  159. }
  160. }
  161. if ($tmpl == 'component') {
  162. // We are inside the iframe
  163. $mainframe->redirect('index.php?option=com_media&view=mediaList&folder='.$folder.'&tmpl=component');
  164. } else {
  165. $mainframe->redirect('index.php?option=com_media&folder='.$folder);
  166. }
  167. }
  168. }