Source for file file.json.php

Documentation is available at file.json.php

  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.  
  10. defined('_JEXEC'or die;
  11.  
  12. jimport('joomla.filesystem.file');
  13. jimport('joomla.filesystem.folder');
  14.  
  15. /**
  16.  * File Media Controller
  17.  *
  18.  * @package     Joomla.Administrator
  19.  * @subpackage  com_media
  20.  * @since       1.6
  21.  */
  22. {
  23.     /**
  24.      * Upload a file
  25.      *
  26.      * @return  void 
  27.      *
  28.      * @since   1.5
  29.      */
  30.     function upload()
  31.     {
  32.         $params JComponentHelper::getParams('com_media');
  33.  
  34.         // Check for request forgeries
  35.         if (!JSession::checkToken('request'))
  36.         {
  37.             $response array(
  38.                 'status' => '0',
  39.                 'error' => JText::_('JINVALID_TOKEN')
  40.             );
  41.             echo json_encode($response);
  42.             return;
  43.         }
  44.  
  45.         // Get the user
  46.         $user  JFactory::getUser();
  47.         JLog::addLogger(array('text_file' => 'upload.error.php')JLog::ALLarray('upload'));
  48.  
  49.         // Get some data from the request
  50.         $file   $this->input->files->get('Filedata''''array');
  51.         $folder $this->input->get('folder''''path');
  52.  
  53.         if (
  54.             $_SERVER['CONTENT_LENGTH'($params->get('upload_maxsize'01024 1024||
  55.             $_SERVER['CONTENT_LENGTH'> (int) (ini_get('upload_max_filesize')) 1024 1024 ||
  56.             $_SERVER['CONTENT_LENGTH'> (int) (ini_get('post_max_size')) 1024 1024 ||
  57.             $_SERVER['CONTENT_LENGTH'> (int) (ini_get('memory_limit')) 1024 1024
  58.         )
  59.         {
  60.             $response array(
  61.                 'status' => '0',
  62.                 'error' => JText::_('COM_MEDIA_ERROR_WARNFILETOOLARGE')
  63.             );
  64.             echo json_encode($response);
  65.             return;
  66.         }
  67.  
  68.         // Set FTP credentials, if given
  69.         JClientHelper::setCredentialsFromRequest('ftp');
  70.  
  71.         // Make the filename safe
  72.         $file['name'JFile::makeSafe($file['name']);
  73.  
  74.         if (isset($file['name']))
  75.         {
  76.             // The request is valid
  77.             $err null;
  78.  
  79.             $filepath JPath::clean(COM_MEDIA_BASE '/' $folder '/' strtolower($file['name']));
  80.  
  81.             if (!MediaHelper::canUpload($file$err))
  82.             {
  83.                 JLog::add('Invalid: ' $filepath ': ' $errJLog::INFO'upload');
  84.  
  85.                 $response array(
  86.                     'status' => '0',
  87.                     'error' => JText::_($err)
  88.                 );
  89.  
  90.                 echo json_encode($response);
  91.                 return;
  92.             }
  93.  
  94.             // Trigger the onContentBeforeSave event.
  95.             JPluginHelper::importPlugin('content');
  96.             $dispatcher    JEventDispatcher::getInstance();
  97.             $object_file new JObject($file);
  98.             $object_file->filepath $filepath;
  99.             $result $dispatcher->trigger('onContentBeforeSave'array('com_media.file'&$object_filetrue));
  100.  
  101.             if (in_array(false$resulttrue))
  102.             {
  103.                 // There are some errors in the plugins
  104.                 JLog::add('Errors before save: ' $object_file->filepath ' : ' implode(', '$object_file->getErrors())JLog::INFO'upload');
  105.  
  106.                 $response array(
  107.                     'status' => '0',
  108.                     'error' => JText::plural('COM_MEDIA_ERROR_BEFORE_SAVE'count($errors $object_file->getErrors())implode('<br />'$errors))
  109.                 );
  110.  
  111.                 echo json_encode($response);
  112.                 return;
  113.             }
  114.  
  115.             if (JFile::exists($object_file->filepath))
  116.             {
  117.                 // File exists
  118.                 JLog::add('File exists: ' $object_file->filepath ' by user_id ' $user->idJLog::INFO'upload');
  119.  
  120.                 $response array(
  121.                     'status' => '0',
  122.                     'error' => JText::_('COM_MEDIA_ERROR_FILE_EXISTS')
  123.                 );
  124.  
  125.                 echo json_encode($response);
  126.                 return;
  127.             }
  128.             elseif (!$user->authorise('core.create''com_media'))
  129.             {
  130.                 // File does not exist and user is not authorised to create
  131.                 JLog::add('Create not permitted: ' $object_file->filepath ' by user_id ' $user->idJLog::INFO'upload');
  132.  
  133.                 $response array(
  134.                     'status' => '0',
  135.                     'error' => JText::_('COM_MEDIA_ERROR_CREATE_NOT_PERMITTED')
  136.                 );
  137.  
  138.                 echo json_encode($response);
  139.                 return;
  140.             }
  141.  
  142.             if (!JFile::upload($object_file->tmp_name$object_file->filepath))
  143.             {
  144.                 // Error in upload
  145.                 JLog::add('Error on upload: ' $object_file->filepathJLog::INFO'upload');
  146.  
  147.                 $response array(
  148.                     'status' => '0',
  149.                     'error' => JText::_('COM_MEDIA_ERROR_UNABLE_TO_UPLOAD_FILE')
  150.                 );
  151.  
  152.                 echo json_encode($response);
  153.                 return;
  154.             }
  155.             else
  156.             {
  157.                 // Trigger the onContentAfterSave event.
  158.                 $dispatcher->trigger('onContentAfterSave'array('com_media.file'&$object_filetrue));
  159.                 JLog::add($folderJLog::INFO'upload');
  160.  
  161.                 $response array(
  162.                     'status' => '1',
  163.                     'error' => JText::sprintf('COM_MEDIA_UPLOAD_COMPLETE'substr($object_file->filepathstrlen(COM_MEDIA_BASE)))
  164.                 );
  165.  
  166.                 echo json_encode($response);
  167.                 return;
  168.             }
  169.         }
  170.         else
  171.         {
  172.             $response array(
  173.                 'status' => '0',
  174.                 'error' => JText::_('COM_MEDIA_ERROR_BAD_REQUEST')
  175.             );
  176.  
  177.             echo json_encode($response);
  178.             return;
  179.         }
  180.     }
  181. }

Documentation generated on Tue, 19 Nov 2013 15:03:01 +0100 by phpDocumentor 1.4.3