Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • ActiveRecordController
  • ArticleController
  • AttachmentController
  • CacheController
  • Controller
  • DEnumController
  • ExcelController
  • FeedController
  • GenSecureQueryStringController
  • ImageController
  • IndexController
  • InstallController
  • ListActiveRecordsController
  • LogController
  • LoginController
  • LogoutController
  • MetricController
  • PhpinfoController
  • RecordSelectorController
  • SearchController
  • SequenceController
  • TagController

Interfaces

  • ControllerInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: namespace Alpha\Controller;
  4: 
  5: use Alpha\Util\Logging\Logger;
  6: use Alpha\Util\Config\ConfigProvider;
  7: use Alpha\Util\File\FileUtils;
  8: use Alpha\Util\Security\SecurityUtils;
  9: use Alpha\Util\Http\Response;
 10: use Alpha\Util\Http\Session\SessionProviderFactory;
 11: use Alpha\Exception\IllegalArguementException;
 12: use Alpha\Exception\SecurityException;
 13: use Alpha\Exception\AlphaException;
 14: use Alpha\View\View;
 15: use Alpha\View\Widget\Button;
 16: 
 17: /**
 18:  * Controller used to clear out the CMS cache when required.
 19:  *
 20:  * @since 1.0
 21:  *
 22:  * @author John Collins <dev@alphaframework.org>
 23:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 24:  * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
 25:  * All rights reserved.
 26:  *
 27:  * <pre>
 28:  * Redistribution and use in source and binary forms, with or
 29:  * without modification, are permitted provided that the
 30:  * following conditions are met:
 31:  *
 32:  * * Redistributions of source code must retain the above
 33:  *   copyright notice, this list of conditions and the
 34:  *   following disclaimer.
 35:  * * Redistributions in binary form must reproduce the above
 36:  *   copyright notice, this list of conditions and the
 37:  *   following disclaimer in the documentation and/or other
 38:  *   materials provided with the distribution.
 39:  * * Neither the name of the Alpha Framework nor the names
 40:  *   of its contributors may be used to endorse or promote
 41:  *   products derived from this software without specific
 42:  *   prior written permission.
 43:  *
 44:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 45:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 46:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 47:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 48:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 49:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 50:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 51:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 52:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 53:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 54:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 55:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 56:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 57:  * </pre>
 58:  */
 59: class CacheController extends Controller implements ControllerInterface
 60: {
 61:     /**
 62:      * The root of the cache directory.
 63:      *
 64:      * @var string
 65:      *
 66:      * @since 1.0
 67:      */
 68:     private $dataDir;
 69: 
 70:     /**
 71:      * Trace logger.
 72:      *
 73:      * @var Alpha\Util\Logging\Logger
 74:      *
 75:      * @since 1.0
 76:      */
 77:     private static $logger = null;
 78: 
 79:     /**
 80:      * constructor to set up the object.
 81:      *
 82:      * @since 1.0
 83:      */
 84:     public function __construct()
 85:     {
 86:         self::$logger = new Logger('CacheManager');
 87:         self::$logger->debug('>>__construct()');
 88: 
 89:         $config = ConfigProvider::getInstance();
 90: 
 91:         // ensure that the super class constructor is called, indicating the rights group
 92:         parent::__construct('Admin');
 93: 
 94:         $this->setTitle('Cache Manager');
 95:         $this->dataDir = $config->get('app.file.store.dir').'cache/';
 96: 
 97:         self::$logger->debug('<<__construct');
 98:     }
 99: 
100:     /**
101:      * Handle GET requests.
102:      *
103:      * @param Alpha\Util\Http\Response $request
104:      *
105:      * @throws Alpha\Exception\IllegalArguementException
106:      *
107:      * @return Alpha\Util\Http\Response
108:      *
109:      * @since 1.0
110:      */
111:     public function doGET($request)
112:     {
113:         self::$logger->debug('>>doGET($request=['.var_export($request, true).'])');
114: 
115:         $params = $request->getParams();
116: 
117:         $config = ConfigProvider::getInstance();
118: 
119:         if (!is_array($params)) {
120:             throw new IllegalArguementException('Bad $params ['.var_export($params, true).'] passed to doGET method!');
121:         }
122: 
123:         $body = View::displayPageHead($this);
124: 
125:         $message = $this->getStatusMessage();
126:         if (!empty($message)) {
127:             $body .= $message;
128:         }
129: 
130:         $body .= '<h3>Listing contents of cache directory: '.$this->dataDir.'</h3>';
131: 
132:         $fileList = '';
133:         $fileCount = FileUtils::listDirectoryContents($this->dataDir, $fileList, 0, array('.htaccess'));
134:         $body .= $fileList;
135: 
136:         $body .= '<h3>Total of '.$fileCount.' files in the cache.</h3>';
137: 
138:         $body .= '<form action="'.$request->getURI().'" method="post" name="clearForm" id="clearForm">';
139:         $fieldname = ($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('clearCache')) : 'clearCache');
140:         $body .= '<input type="hidden" name="'.$fieldname.'" id="'.$fieldname.'" value="false"/>';
141:         $js = "if(window.jQuery) {
142:                     BootstrapDialog.show({
143:                         title: 'Confirmation',
144:                         message: 'Are you sure you want to delete all files in the cache?',
145:                         buttons: [
146:                             {
147:                                 icon: 'glyphicon glyphicon-remove',
148:                                 label: 'Cancel',
149:                                 cssClass: 'btn btn-default btn-xs',
150:                                 action: function(dialogItself){
151:                                     dialogItself.close();
152:                                 }
153:                             },
154:                             {
155:                                 icon: 'glyphicon glyphicon-ok',
156:                                 label: 'Okay',
157:                                 cssClass: 'btn btn-default btn-xs',
158:                                 action: function(dialogItself) {
159:                                     $('[id=\"".$fieldname."\"]').attr('value', 'true');
160:                                     $('#clearForm').submit();
161:                                     dialogItself.close();
162:                                 }
163:                             }
164:                         ]
165:                     });
166:                 }";
167:         $button = new Button($js, 'Clear cache', 'clearBut');
168:         $body .= $button->render();
169: 
170:         $body .= View::renderSecurityFields();
171:         $body .= '</form>';
172: 
173:         $body .= View::displayPageFoot($this);
174: 
175:         self::$logger->debug('<<doGET');
176: 
177:         return new Response(200, $body, array('Content-Type' => 'text/html'));
178:     }
179: 
180:     /**
181:      * Handle POST requests.
182:      *
183:      * @param Alpha\Util\Http\Response $request
184:      *
185:      * @throws Alpha\Exception\SecurityException
186:      * @throws Alpha\Exception\IllegalArguementException
187:      *
188:      * @return Alpha\Util\Http\Response
189:      *
190:      * @since 1.0
191:      */
192:     public function doPOST($request)
193:     {
194:         self::$logger->debug('>>doPOST($request=['.var_export($request, true).'])');
195: 
196:         $params = $request->getParams();
197: 
198:         try {
199:             // check the hidden security fields before accepting the form POST data
200:             if (!$this->checkSecurityFields()) {
201:                 throw new SecurityException('This page cannot accept post data from remote servers!');
202:             }
203: 
204:             if (!is_array($params)) {
205:                 throw new IllegalArguementException('Bad $params ['.var_export($params, true).'] passed to doPOST method!');
206:             }
207: 
208:             if (isset($params['clearCache']) && $params['clearCache'] == 'true') {
209:                 try {
210:                     FileUtils::deleteDirectoryContents($this->dataDir, array('.htaccess','html','images','pdf','xls'));
211: 
212:                     $this->setStatusMessage(View::displayUpdateMessage('Cache contents deleted successfully.'));
213: 
214:                     $config = ConfigProvider::getInstance();
215:                     $sessionProvider = $config->get('session.provider.name');
216:                     $session = SessionProviderFactory::getInstance($sessionProvider);
217: 
218:                     self::$logger->info('Cache contents deleted successfully by user ['.$session->get('currentUser')->get('displayName').'].');
219:                 } catch (AlphaException $e) {
220:                     self::$logger->error($e->getMessage());
221:                     $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
222:                 }
223:             }
224: 
225:             return $this->doGET($request);
226:         } catch (SecurityException $e) {
227:             $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
228: 
229:             self::$logger->warn($e->getMessage());
230:         } catch (IllegalArguementException $e) {
231:             self::$logger->error($e->getMessage());
232:             $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
233:         }
234: 
235:         $body = View::displayPageHead($this);
236: 
237:         $message = $this->getStatusMessage();
238:         if (!empty($message)) {
239:             $body .= $message;
240:         }
241: 
242:         $body .= View::displayPageFoot($this);
243:         self::$logger->debug('<<doPOST');
244: 
245:         return new Response(200, $body, array('Content-Type' => 'text/html'));
246:     }
247: }
248: 
Alpha Framework 2.0.4 API Documentation API documentation generated by ApiGen 2.8.0