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