| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | // |
|---|
| 4 | // Open Web Analytics - An Open Source Web Analytics Framework |
|---|
| 5 | // |
|---|
| 6 | // Copyright 2006 Peter Adams. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html |
|---|
| 9 | // |
|---|
| 10 | // Unless required by applicable law or agreed to in writing, software |
|---|
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 13 | // See the License for the specific language governing permissions and |
|---|
| 14 | // limitations under the License. |
|---|
| 15 | // |
|---|
| 16 | // $Id$ |
|---|
| 17 | // |
|---|
| 18 | |
|---|
| 19 | require_once(OWA_BASE_DIR.'/owa_auth.php'); |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Abstract Controller Class |
|---|
| 24 | * |
|---|
| 25 | * @author Peter Adams <peter@openwebanalytics.com> |
|---|
| 26 | * @copyright Copyright © 2006 Peter Adams <peter@openwebanalytics.com> |
|---|
| 27 | * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 |
|---|
| 28 | * @category owa |
|---|
| 29 | * @package owa |
|---|
| 30 | * @version $Revision$ |
|---|
| 31 | * @since owa 1.0.0 |
|---|
| 32 | */ |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | class owa_controller extends owa_base { |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * Request Parameters passed in from caller |
|---|
| 39 | * |
|---|
| 40 | * @var array |
|---|
| 41 | */ |
|---|
| 42 | var $params = array(); |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Controller Type |
|---|
| 46 | * |
|---|
| 47 | * @var array |
|---|
| 48 | */ |
|---|
| 49 | var $type; |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Is the controller for an admin function |
|---|
| 53 | * |
|---|
| 54 | * @var boolean |
|---|
| 55 | */ |
|---|
| 56 | var $is_admin; |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * The priviledge level required to access this controller |
|---|
| 60 | * |
|---|
| 61 | * @var string |
|---|
| 62 | */ |
|---|
| 63 | var $priviledge_level; |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * The auth module to use for this controller |
|---|
| 67 | * This can be overriden by concrete controller classes |
|---|
| 68 | * otherwise value will be pulled from the base module's configuration |
|---|
| 69 | * |
|---|
| 70 | * @var string |
|---|
| 71 | */ |
|---|
| 72 | var $auth_module; |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * data validation control object |
|---|
| 76 | * |
|---|
| 77 | * @var Object |
|---|
| 78 | */ |
|---|
| 79 | var $v; |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * Data container |
|---|
| 83 | * |
|---|
| 84 | * @var Array |
|---|
| 85 | */ |
|---|
| 86 | var $data = array(); |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * Capability |
|---|
| 90 | * |
|---|
| 91 | * @var string |
|---|
| 92 | */ |
|---|
| 93 | var $capability; |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Available Views |
|---|
| 97 | * |
|---|
| 98 | * @var Array |
|---|
| 99 | */ |
|---|
| 100 | var $available_views = array(); |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Time period |
|---|
| 104 | * |
|---|
| 105 | * @var Object |
|---|
| 106 | */ |
|---|
| 107 | var $period; |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Dom id |
|---|
| 111 | * |
|---|
| 112 | * @var String |
|---|
| 113 | */ |
|---|
| 114 | var $dom_id; |
|---|
| 115 | |
|---|
| 116 | /** |
|---|
| 117 | * Flag for requiring authenciation before performing actions |
|---|
| 118 | * |
|---|
| 119 | * @var Bool |
|---|
| 120 | */ |
|---|
| 121 | var $authenticate_user; |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * PHP4 Constructor |
|---|
| 125 | * |
|---|
| 126 | * @param array $params |
|---|
| 127 | */ |
|---|
| 128 | function owa_controller($params) { |
|---|
| 129 | |
|---|
| 130 | return owa_controller::__construct($params); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * Constructor |
|---|
| 135 | * |
|---|
| 136 | * @param array $params |
|---|
| 137 | */ |
|---|
| 138 | function __construct($params) { |
|---|
| 139 | |
|---|
| 140 | // call parent constructor to setup objects. |
|---|
| 141 | parent::__construct(); |
|---|
| 142 | |
|---|
| 143 | // set request params |
|---|
| 144 | $this->params = $params; |
|---|
| 145 | |
|---|
| 146 | // sets the auth module. requires a configuration object. |
|---|
| 147 | $this->_setAuthModule(); |
|---|
| 148 | |
|---|
| 149 | return; |
|---|
| 150 | |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | /** |
|---|
| 154 | * Handles request from caller |
|---|
| 155 | * |
|---|
| 156 | */ |
|---|
| 157 | function doAction() { |
|---|
| 158 | |
|---|
| 159 | $this->e->debug('Performing Action: '.get_class($this)); |
|---|
| 160 | |
|---|
| 161 | // check if the schema needs to be updated and force the update |
|---|
| 162 | // not sure this should go here... |
|---|
| 163 | if ($this->is_admin === true): |
|---|
| 164 | // do not intercept if its the updatesApply action or else updates will never apply |
|---|
| 165 | if ($this->params['do'] != 'base.updatesApply'): |
|---|
| 166 | |
|---|
| 167 | $api = &owa_coreAPI::singleton(); |
|---|
| 168 | |
|---|
| 169 | if ($api->update_required === true): |
|---|
| 170 | $this->e->debug('Updates Required. Redirecting action.'); |
|---|
| 171 | $data = array(); |
|---|
| 172 | $data['view_method'] = 'redirect'; |
|---|
| 173 | $data['action'] = 'base.updates'; |
|---|
| 174 | return $data; |
|---|
| 175 | endif; |
|---|
| 176 | endif; |
|---|
| 177 | endif; |
|---|
| 178 | |
|---|
| 179 | /* CHECK USER FOR CAPABILITIES */ |
|---|
| 180 | if (!owa_coreAPI::isCurrentUserCapable($this->getRequiredCapability())): |
|---|
| 181 | |
|---|
| 182 | /* PERFORM AUTHENTICATION */ |
|---|
| 183 | // TODO: create authSingleton() to hold an array of multiple auth objects |
|---|
| 184 | // TODO: make auth object configurable by controller |
|---|
| 185 | |
|---|
| 186 | $auth = &owa_auth::get_instance(); |
|---|
| 187 | $status = $auth->authenticateUser(); |
|---|
| 188 | // if auth was not successful then return login view. |
|---|
| 189 | if ($status['auth_status'] != true): |
|---|
| 190 | //$data['view_method'] = 'delegate'; |
|---|
| 191 | $this->setView('base.login'); |
|---|
| 192 | $this->set('go', urlencode(owa_lib::get_current_url())); |
|---|
| 193 | $this->set('error_msg', $this->getMsg(2002)); |
|---|
| 194 | return $this->data; |
|---|
| 195 | else: |
|---|
| 196 | //check for needed capability again now that they are authenticated |
|---|
| 197 | if (!owa_coreAPI::isCurrentUserCapable($this->getRequiredCapability())): |
|---|
| 198 | $this->setView('base.error'); |
|---|
| 199 | $this->set('error_msg', $this->getMsg(2003)); |
|---|
| 200 | $this->set('go', urlencode(owa_lib::get_current_url())); |
|---|
| 201 | return $this->data; |
|---|
| 202 | endif; |
|---|
| 203 | endif; |
|---|
| 204 | endif; |
|---|
| 205 | |
|---|
| 206 | // set auth status |
|---|
| 207 | $this->set('auth_status', true); |
|---|
| 208 | //set request params |
|---|
| 209 | $this->set('params', $this->params); |
|---|
| 210 | // set site_id |
|---|
| 211 | $this->set('site_id', $this->get('site_id')); |
|---|
| 212 | |
|---|
| 213 | // set status msg - NEEDED HERE? doesnt owa_ view handle this? |
|---|
| 214 | if (!empty($this->params['status_code'])): |
|---|
| 215 | $this->data['status_msg'] = $this->getMsg($this->params['status_code']); |
|---|
| 216 | endif; |
|---|
| 217 | |
|---|
| 218 | // get error msg from error code passed on the query string from a redirect. |
|---|
| 219 | if (!empty($this->params['error_code'])): |
|---|
| 220 | $this->data['error_msg'] = $this->getMsg($this->params['error_code']); |
|---|
| 221 | endif; |
|---|
| 222 | |
|---|
| 223 | // check to see if the controller has created a validator |
|---|
| 224 | if (!empty($this->v)): |
|---|
| 225 | // if so do the validations required |
|---|
| 226 | $this->v->doValidations(); |
|---|
| 227 | //check for erros |
|---|
| 228 | if ($this->v->hasErrors == true): |
|---|
| 229 | // if errors, do the errorAction instead of the normal action |
|---|
| 230 | return $this->errorAction(); |
|---|
| 231 | endif; |
|---|
| 232 | endif; |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | /* PERFORM PRE ACTION */ |
|---|
| 236 | // often used by abstract descendant controllers to set various things |
|---|
| 237 | $this->pre(); |
|---|
| 238 | |
|---|
| 239 | /* PERFORM MAIN ACTION */ |
|---|
| 240 | // need to check ret for backwards compatability with older |
|---|
| 241 | // controllers that donot use $this->data |
|---|
| 242 | $ret = $this->action(); |
|---|
| 243 | |
|---|
| 244 | if (!empty($ret)): |
|---|
| 245 | $this->post(); |
|---|
| 246 | return $ret; |
|---|
| 247 | else: |
|---|
| 248 | $this->post(); |
|---|
| 249 | return $this->data; |
|---|
| 250 | endif; |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | function logEvent($event_type, $properties) { |
|---|
| 257 | |
|---|
| 258 | if (!class_exists('eventQueue')): |
|---|
| 259 | require_once(OWA_BASE_DIR.DIRECTORY_SEPARATOR.'eventQueue.php'); |
|---|
| 260 | endif; |
|---|
| 261 | |
|---|
| 262 | $eq = &eventQueue::get_instance(); |
|---|
| 263 | return $eq->log($properties, $event_type); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | function createValidator() { |
|---|
| 267 | |
|---|
| 268 | $this->v = owa_coreAPI::supportClassFactory('base', 'validator'); |
|---|
| 269 | |
|---|
| 270 | return; |
|---|
| 271 | |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | function addValidation($name, $value, $validation, $conf = array()) { |
|---|
| 275 | |
|---|
| 276 | if (empty($this->v)): |
|---|
| 277 | $this->createValidator(); |
|---|
| 278 | endif; |
|---|
| 279 | |
|---|
| 280 | return $this->v->addValidation($name, $value, $validation, $conf); |
|---|
| 281 | |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | function getValidationErrorMsgs() { |
|---|
| 285 | |
|---|
| 286 | return $this->v->getErrorMsgs(); |
|---|
| 287 | |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | function isAdmin() { |
|---|
| 291 | |
|---|
| 292 | if ($this->is_admin == true): |
|---|
| 293 | return true; |
|---|
| 294 | else: |
|---|
| 295 | return false; |
|---|
| 296 | endif; |
|---|
| 297 | |
|---|
| 298 | } |
|---|
| 299 | |
|---|
| 300 | function _setAuthModule() { |
|---|
| 301 | |
|---|
| 302 | if (empty($this->auth_module)): |
|---|
| 303 | $this->auth_module = $this->c->get('base', 'authentication'); |
|---|
| 304 | endif; |
|---|
| 305 | |
|---|
| 306 | return; |
|---|
| 307 | |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | // depricated |
|---|
| 311 | function _setCapability($capability) { |
|---|
| 312 | |
|---|
| 313 | $this->setRequiredCapability($capability); |
|---|
| 314 | |
|---|
| 315 | return; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | function setRequiredCapability($capability) { |
|---|
| 319 | |
|---|
| 320 | $this->capability = $capability; |
|---|
| 321 | return; |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | function getRequiredCapability() { |
|---|
| 325 | |
|---|
| 326 | return $this->capability; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | function getParam($name) { |
|---|
| 330 | |
|---|
| 331 | if (array_key_exists($name, $this->params)): |
|---|
| 332 | return $this->params[$name]; |
|---|
| 333 | else: |
|---|
| 334 | return false; |
|---|
| 335 | endif; |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | function get($name) { |
|---|
| 339 | |
|---|
| 340 | return $this->getParam($name); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | function pre() { |
|---|
| 344 | |
|---|
| 345 | return false; |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | function post() { |
|---|
| 349 | return false; |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | function getPeriod() { |
|---|
| 353 | |
|---|
| 354 | return $this->period; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | function setPeriod() { |
|---|
| 358 | |
|---|
| 359 | // set period |
|---|
| 360 | |
|---|
| 361 | $period = $this->makeTimePeriod($this->getParam('period'), $this->params); |
|---|
| 362 | |
|---|
| 363 | $this->period = $period; |
|---|
| 364 | $this->set('period', $this->getPeriod()); |
|---|
| 365 | $this->data['params'] = array_merge($this->data['params'], $period->getPeriodProperties()); |
|---|
| 366 | return; |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | function makeTimePeriod($time_period, $params = array()) { |
|---|
| 370 | |
|---|
| 371 | return owa_coreAPI::makeTimePeriod($time_period, $params); |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | function setView($view) { |
|---|
| 376 | $this->data['view'] = $view; |
|---|
| 377 | return; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | function setSubview($subview) { |
|---|
| 381 | $this->data['subview'] = $subview; |
|---|
| 382 | return; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | function setViewMethod($method = 'delegate') { |
|---|
| 386 | $this->data['view_method'] = $method; |
|---|
| 387 | return; |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | function setRedirectAction($do) { |
|---|
| 391 | $this->set('view_method', 'redirect'); |
|---|
| 392 | $this->set('do', $do); |
|---|
| 393 | return; |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | function setPagination($pagination, $name = 'pagination') { |
|---|
| 397 | $this->data[$name] = $pagination; |
|---|
| 398 | return; |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | function set($name, $value) { |
|---|
| 402 | |
|---|
| 403 | $this->data[$name] = $value; |
|---|
| 404 | return; |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | function setControllerType($string) { |
|---|
| 408 | |
|---|
| 409 | $this->type = $string; |
|---|
| 410 | return; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | function mergeParams($array) { |
|---|
| 414 | |
|---|
| 415 | $this->params = array_merge($this->params, $array); |
|---|
| 416 | return; |
|---|
| 417 | } |
|---|
| 418 | |
|---|
| 419 | } |
|---|
| 420 | |
|---|
| 421 | ?> |
|---|