PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/asosso/joomla15
PHP | 188 lines | 127 code | 19 blank | 42 comment | 34 complexity | bf282b85b5c5d2a98bee59abf2d1de8c MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id: file.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla
  5. * @subpackage Content
  6. * @copyright Copyright (C) 2005 - 2010 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. JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );
  126. // Set FTP credentials, if given
  127. jimport('joomla.client.helper');
  128. JClientHelper::setCredentialsFromRequest('ftp');
  129. // Get some data from the request
  130. $tmpl = JRequest::getCmd( 'tmpl' );
  131. $paths = JRequest::getVar( 'rm', array(), '', 'array' );
  132. $folder = JRequest::getVar( 'folder', '', '', 'path');
  133. // Initialize variables
  134. $msg = array();
  135. $ret = true;
  136. if (count($paths)) {
  137. foreach ($paths as $path)
  138. {
  139. if ($path !== JFile::makeSafe($path)) {
  140. JError::raiseWarning(100, JText::_('Unable to delete:').htmlspecialchars($path, ENT_COMPAT, 'UTF-8').' '.JText::_('WARNFILENAME'));
  141. continue;
  142. }
  143. $fullPath = JPath::clean(COM_MEDIA_BASE.DS.$folder.DS.$path);
  144. if (is_file($fullPath)) {
  145. $ret |= !JFile::delete($fullPath);
  146. } else if (is_dir($fullPath)) {
  147. $files = JFolder::files($fullPath, '.', true);
  148. $canDelete = true;
  149. foreach ($files as $file) {
  150. if ($file != 'index.html') {
  151. $canDelete = false;
  152. }
  153. }
  154. if ($canDelete) {
  155. $ret |= !JFolder::delete($fullPath);
  156. } else {
  157. JError::raiseWarning(100, JText::_('Unable to delete:').$fullPath.' '.JText::_('Not Empty!'));
  158. }
  159. }
  160. }
  161. }
  162. if ($tmpl == 'component') {
  163. // We are inside the iframe
  164. $mainframe->redirect('index.php?option=com_media&view=mediaList&folder='.$folder.'&tmpl=component');
  165. } else {
  166. $mainframe->redirect('index.php?option=com_media&folder='.$folder);
  167. }
  168. }
  169. }