Source for file removefolder.php

Documentation is available at removefolder.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Installation
  4.  * @subpackage  Controller
  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. /**
  13.  * Controller class to set the FTP data for the Joomla Installer.
  14.  *
  15.  * @package     Joomla.Installation
  16.  * @subpackage  Controller
  17.  * @since       3.1
  18.  */
  19. {
  20.     /**
  21.      * Execute the controller.
  22.      *
  23.      * @return  void 
  24.      *
  25.      * @since   3.1
  26.      */
  27.     public function execute()
  28.     {
  29.         // Get the application
  30.         /* @var InstallationApplicationWeb $app */
  31.         $app $this->getApplication();
  32.  
  33.         // Check for request forgeries.
  34.         JSession::checkToken(or $app->sendJsonResponse(new Exception(JText::_('JINVALID_TOKEN')403));
  35.  
  36.         $path JPATH_INSTALLATION;
  37.  
  38.         // Check whether the folder still exists
  39.         if (!file_exists($path))
  40.         {
  41.             $app->sendJsonResponse(new Exception(JText::sprintf('INSTL_COMPLETE_ERROR_FOLDER_ALREADY_REMOVED')500));
  42.         }
  43.  
  44.         // Check whether we need to use FTP
  45.         $useFTP false;
  46.         if ((file_exists($path&& !is_writable($path)))
  47.         {
  48.             $useFTP true;
  49.         }
  50.  
  51.         // Check for safe mode
  52.         if (ini_get('safe_mode'))
  53.         {
  54.             $useFTP true;
  55.         }
  56.  
  57.         // Enable/Disable override
  58.         if (!isset($options->ftpEnable|| ($options->ftpEnable != 1))
  59.         {
  60.             $useFTP false;
  61.         }
  62.  
  63.         if ($useFTP == true)
  64.         {
  65.             // Connect the FTP client
  66.             $ftp JClientFtp::getInstance($options->ftp_host$options->ftp_port);
  67.             $ftp->login($options->ftp_user$options->ftp_pass);
  68.  
  69.             // Translate path for the FTP account
  70.             $file JPath::clean(str_replace(JPATH_CONFIGURATION$options->ftp_root$path)'/');
  71.             $return $ftp->delete($file);
  72.  
  73.             // Delete the extra XML file while we're at it
  74.             if ($return)
  75.             {
  76.                 $file JPath::clean($options->ftp_root '/joomla.xml');
  77.                 if (file_exists($file))
  78.                 {
  79.                     $return $ftp->delete($file);
  80.                 }
  81.             }
  82.  
  83.             // Rename the robots.txt.dist file to robots.txt
  84.             if ($return)
  85.             {
  86.                 $robotsFile JPath::clean($options->ftp_root '/robots.txt');
  87.                 $distFile JPath::clean($options->ftp_root '/robots.txt.dist');
  88.  
  89.                 if (!file_exists($robotsFile&& file_exists($distFile))
  90.                 {
  91.                     $return $ftp->rename($distFile$robotsFile);
  92.                 }
  93.             }
  94.  
  95.             $ftp->quit();
  96.         }
  97.         else
  98.         {
  99.             /*
  100.              * Try to delete the folder.
  101.              * We use output buffering so that any error message echoed JFolder::delete
  102.              * doesn't land in our JSON output.
  103.              */
  104.             ob_start();
  105.             $return JFolder::delete($path&& (!file_exists(JPATH_ROOT '/joomla.xml'|| JFile::delete(JPATH_ROOT '/joomla.xml'));
  106.  
  107.             // Rename the robots.txt.dist file if robots.txt doesn't exist
  108.             if ($return && !file_exists(JPATH_ROOT '/robots.txt'&& file_exists(JPATH_ROOT '/robots.txt.dist'))
  109.             {
  110.                 $return JFile::move(JPATH_ROOT '/robots.txt.dist'JPATH_ROOT '/robots.txt');
  111.             }
  112.  
  113.             ob_end_clean();
  114.         }
  115.  
  116.         // If an error was encountered return an error.
  117.         if (!$return)
  118.         {
  119.             $app->sendJsonResponse(new Exception(JText::_('INSTL_COMPLETE_ERROR_FOLDER_DELETE')500));
  120.         }
  121.  
  122.         // Create a response body.
  123.         $r new stdClass;
  124.         $r->text JText::_('INSTL_COMPLETE_FOLDER_REMOVED');
  125.  
  126.         /*
  127.          * Send the response
  128.          * This is a hack since by now, the rest of the folder is deleted and we can't make a new request
  129.          */
  130.         $this->sendJsonResponse($r);
  131.     }
  132.  
  133.     /**
  134.      * Method to send a JSON response. The data parameter
  135.      * can be a Exception object for when an error has occurred or
  136.      * a stdClass for a good response.
  137.      *
  138.      * @param   mixed  $response  stdClass on success, Exception on failure.
  139.      *
  140.      * @return  void 
  141.      *
  142.      * @since   3.1
  143.      */
  144.     public function sendJsonResponse($response)
  145.     {
  146.         // Check if we need to send an error code.
  147.         if ($response instanceof Exception)
  148.         {
  149.             // Send the appropriate error code response.
  150.             $this->setHeader('status'$response->getCode());
  151.             $this->setHeader('Content-Type''application/json; charset=utf-8');
  152.             $this->sendHeaders();
  153.         }
  154.  
  155.         // Send the JSON response.
  156.         JLoader::register('InstallationResponseJson'__FILE__);
  157.         echo json_encode(new InstallationResponseJson($response));
  158.  
  159.         // Close the application.
  160.         exit;
  161.     }
  162. }
  163.  
  164. /**
  165.  * JSON Response class for the Joomla Installer.
  166.  *
  167.  * @package     Joomla.Installation
  168.  * @subpackage  Response
  169.  * @since       3.1
  170.  */
  171. {
  172.     /**
  173.      * Constructor for the JSON response
  174.      *
  175.      * @param   mixed  $data  Exception if there is an error, otherwise, the session data
  176.      *
  177.      * @since   3.1
  178.      */
  179.     public function __construct($data)
  180.     {
  181.         // The old token is invalid so send a new one.
  182.         $this->token JSession::getFormToken(true);
  183.  
  184.         // Get the language and send it's tag along
  185.         $this->lang JFactory::getLanguage()->getTag();
  186.  
  187.         // Get the message queue
  188.         $messages JFactory::getApplication()->getMessageQueue();
  189.  
  190.         // Build the sorted message list
  191.         if (is_array($messages&& count($messages))
  192.         {
  193.             foreach ($messages as $msg)
  194.             {
  195.                 if (isset($msg['type']&& isset($msg['message']))
  196.                 {
  197.                     $lists[$msg['type']][$msg['message'];
  198.                 }
  199.             }
  200.         }
  201.  
  202.         // If messages exist add them to the output
  203.         if (isset($lists&& is_array($lists))
  204.         {
  205.             $this->messages $lists;
  206.         }
  207.  
  208.         // Check if we are dealing with an error.
  209.         if ($data instanceof Exception)
  210.         {
  211.             // Prepare the error response.
  212.             $this->error   true;
  213.             $this->header  JText::_('INSTL_HEADER_ERROR');
  214.             $this->message $data->getMessage();
  215.         }
  216.         else
  217.         {
  218.             // Prepare the response data.
  219.             $this->error false;
  220.             $this->data  $data;
  221.         }
  222.     }
  223. }

Documentation generated on Tue, 19 Nov 2013 15:11:47 +0100 by phpDocumentor 1.4.3