Alpha Framework alpha--controller
[ class tree: alpha--controller ] [ index: alpha--controller ] [ all elements ]

Source for file CacheManager.php

Documentation is available at CacheManager.php

  1. <?php
  2.  
  3. // include the config file
  4. if(!isset($config)) {
  5.     require_once '../util/AlphaConfig.inc';
  6.     $config AlphaConfig::getInstance();
  7. }
  8.  
  9. require_once $config->get('sysRoot').'alpha/controller/AlphaController.inc';
  10. require_once $config->get('sysRoot').'alpha/util/AlphaFileUtils.inc';
  11. require_once $config->get('sysRoot').'alpha/controller/AlphaControllerInterface.inc';
  12. require_once $config->get('sysRoot').'alpha/view/AlphaView.inc';
  13.  
  14. /**
  15.  * 
  16.  * Controller used to clear out the CMS cache when required
  17.  * 
  18.  * @package alpha::controller
  19.  * @since 1.0
  20.  * @author John Collins <dev@alphaframework.org>
  21.  * @version $Id: CacheManager.php 1453 2011-12-04 15:12:54Z johnc $
  22.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  23.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  24.  *  All rights reserved.
  25.  * 
  26.  *  <pre>
  27.  *  Redistribution and use in source and binary forms, with or
  28.  *  without modification, are permitted provided that the
  29.  *  following conditions are met:
  30.  * 
  31.  *  * Redistributions of source code must retain the above
  32.  *    copyright notice, this list of conditions and the
  33.  *    following disclaimer.
  34.  *  * Redistributions in binary form must reproduce the above
  35.  *    copyright notice, this list of conditions and the
  36.  *    following disclaimer in the documentation and/or other
  37.  *    materials provided with the distribution.
  38.  *  * Neither the name of the Alpha Framework nor the names
  39.  *    of its contributors may be used to endorse or promote
  40.  *    products derived from this software without specific
  41.  *    prior written permission.
  42.  *   
  43.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  44.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  45.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  46.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  47.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  48.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  49.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  50.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  51.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  52.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  53.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  54.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  55.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56.  *  </pre>
  57.  *  
  58.  */
  59. class CacheManager extends AlphaController implements AlphaControllerInterface {
  60.     /**
  61.      * The root of the cache directory
  62.      * 
  63.      * @var string 
  64.      * @since 1.0
  65.      */
  66.     private $dataDir;
  67.     
  68.     /**
  69.      * Trace logger
  70.      * 
  71.      * @var Logger 
  72.      * @since 1.0
  73.      */
  74.     private static $logger null;
  75.     
  76.     /**
  77.      * constructor to set up the object
  78.      * 
  79.      * @since 1.0
  80.      */
  81.     public function __construct({
  82.         self::$logger new Logger('CacheManager');
  83.         self::$logger->debug('>>__construct()');
  84.         
  85.         global $config;
  86.         
  87.         // ensure that the super class constructor is called, indicating the rights group
  88.         parent::__construct('Admin');
  89.         
  90.         $this->setTitle('Cache Manager');
  91.         $this->dataDir  $config->get('sysRoot').'cache/';
  92.         
  93.         self::$logger->debug('<<__construct');
  94.     }
  95.     
  96.     /**
  97.      * Handle GET requests
  98.      * 
  99.      * @param array $params 
  100.      * @throws IllegalArguementException
  101.      * @since 1.0
  102.      */
  103.     public function doGET($params{
  104.         self::$logger->debug('>>doGET($params=['.var_export($paramstrue).'])');
  105.         
  106.         global $config;
  107.         
  108.         if(!is_array($params))
  109.             throw new IllegalArguementException('Bad $params ['.var_export($paramstrue).'] passed to doGET method!');
  110.         
  111.         
  112.         echo AlphaView::displayPageHead($this);
  113.         
  114.         echo '<h2>Listing contents of cache directory: '.$this->dataDir.'</h2>';
  115.         
  116.            $fileCount AlphaFileUtils::listDirectoryContents($this->dataDir);
  117.            
  118.            echo '<h2>Total of '.$fileCount.' files in the cache.</h2>';
  119.            
  120.            echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="post" name="clearForm" id="clearForm">';
  121.            echo '<input type="hidden" name="clearCache" id="clearCache" value="false"/>';
  122.            $js "$('#dialogDiv').text('Are you sure you want to delete all files in the cache?');
  123.                 $('#dialogDiv').dialog({
  124.                 buttons: {
  125.                     'OK': function(event, ui) {                        
  126.                         $('#clearCache').attr('value', 'true');
  127.                         $('#clearForm').submit();
  128.                     },
  129.                     'Cancel': function(event, ui) {
  130.                         $(this).dialog('close');
  131.                     }
  132.                 }
  133.             })
  134.             $('#dialogDiv').dialog('open');
  135.             return false;";
  136.         $button new Button($js"Clear cache""clearBut");
  137.            echo $button->render();
  138.            
  139.            echo AlphaView::renderSecurityFields();
  140.            echo '</form>';
  141.         
  142.         echo AlphaView::displayPageFoot($this);
  143.         
  144.         self::$logger->debug('<<doGET');
  145.     }
  146.     
  147.     /**
  148.      * Handle POST requests
  149.      * 
  150.      * @param array $params 
  151.      * @throws SecurityException
  152.      * @throws IllegalArguementException
  153.      * @since 1.0
  154.      */
  155.     public function doPOST($params{
  156.         self::$logger->debug('>>doPOST($params=['.var_export($paramstrue).'])');
  157.         
  158.         try {
  159.             // check the hidden security fields before accepting the form POST data
  160.             if(!$this->checkSecurityFields())
  161.                 throw new SecurityException('This page cannot accept post data from remote servers!');
  162.             
  163.             if(!is_array($params))
  164.                 throw new IllegalArguementException('Bad $params ['.var_export($paramstrue).'] passed to doPOST method!');
  165.  
  166.             if (isset($params['clearCache']&& $params['clearCache'== 'true'{
  167.                 try {
  168.                     AlphaFileUtils::deleteDirectoryContents($this->dataDir);
  169.                             
  170.                     $this->setStatusMessage(AlphaView::displayUpdateMessage('Cache contents deleted successfully.'));
  171.                     
  172.                     self::$logger->info('Cache contents deleted successfully by user ['.$_SESSION['currentUser']->get('displayName').'].');
  173.                 }catch (AlphaException $e{
  174.                     self::$logger->error($e->getMessage());
  175.                     $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
  176.                 }                
  177.             }
  178.             
  179.             $this->doGET($params);
  180.         }catch(SecurityException $e{
  181.             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
  182.             
  183.             self::$logger->warn($e->getMessage());
  184.         }catch(IllegalArguementException $e{
  185.             self::$logger->error($e->getMessage());
  186.             $this->setStatusMessage(AlphaView::displayErrorMessage($e->getMessage()));
  187.         }
  188.         
  189.         echo AlphaView::displayPageFoot($this);
  190.         self::$logger->debug('<<doPOST');
  191.     }
  192. }
  193.  
  194. // now build the new controller if this file is called directly
  195. if ('CacheManager.php' == basename($_SERVER['PHP_SELF'])) {
  196.     $controller new CacheManager();
  197.     
  198.     if(!empty($_POST)) {            
  199.         $controller->doPOST($_QUERY);
  200.     }else{
  201.         $controller->doGET($_GET);
  202.     }
  203. }
  204.  
  205. ?>

Documentation generated on Tue, 13 Dec 2011 20:26:34 +0000 by phpDocumentor 1.4.3