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: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
59: class CacheController extends Controller implements ControllerInterface
60: {
61: 62: 63: 64: 65: 66: 67:
68: private $dataDir;
69:
70: 71: 72: 73: 74: 75: 76:
77: private static $logger = null;
78:
79: 80: 81: 82: 83:
84: public function __construct()
85: {
86: self::$logger = new Logger('CacheManager');
87: self::$logger->debug('>>__construct()');
88:
89: $config = ConfigProvider::getInstance();
90:
91:
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: 102: 103: 104: 105: 106: 107: 108: 109: 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: 182: 183: 184: 185: 186: 187: 188: 189: 190: 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:
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: