root/trunk/owa_caller.php

Revision 558, 13.9 kB (checked in by padams, 3 days ago)

fixed several bugs in serviceUser
renamed 'Administrator' role to 'admin' to maintain backwards compatability
fixed bogus error msg when applying module updates successfully

Line 
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
19include_once('owa_env.php');
20require_once('owa_requestContainer.php');
21require_once(OWA_BASE_DIR.'/owa_auth.php');
22require_once(OWA_BASE_DIR.'/owa_base.php');
23require_once(OWA_BASE_DIR.'/owa_coreAPI.php');
24
25/**
26 * Abstract Caller class used to build application specific invocation classes
27 *
28 * @author      Peter Adams <peter@openwebanalytics.com>
29 * @copyright   Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
30 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
31 * @category    owa
32 * @package     owa
33 * @version             $Revision$           
34 * @since               owa 1.0.0
35 */
36class owa_caller extends owa_base {
37       
38        /**
39         * Request Params from get or post
40         *
41         * @var array
42         */
43        var $params;
44       
45        /**
46         * Core API
47         *
48         * @var object
49         */
50        var $api;
51       
52        var $start_time;
53       
54        var $end_time;
55       
56        var $update_required;
57       
58        var $service;
59               
60        /**
61         * PHP4 Constructor
62         *
63         */
64         
65        function owa_caller($config) {
66       
67                register_shutdown_function(array(&$this, "__destruct"));
68                return $this->__construct($config);
69               
70        }
71       
72        /**
73         * Constructor
74         *
75         * @param array $config
76         * @return owa_caller
77         */
78        function __construct($config) {
79               
80                // Start time
81                $this->start_time = owa_lib::microtime_float();
82               
83                /* LOAD CONFIG FILE */
84               
85                $file = OWA_BASE_DIR.DIRECTORY_SEPARATOR.'conf'.DIRECTORY_SEPARATOR.'owa-config.php';
86               
87                if (file_exists($file)):
88                        $config_file_exists = true;
89                        include($file);
90                else:
91                        $config_file_exists = false;
92                        //$this->e->debug("I can't find your configuration file...assuming that you didn't create one.");
93                endif;
94               
95                /* SETUP STORAGE ENGINE */
96               
97                // Must be called before any entities are created
98               
99                if (!defined('OWA_DB_TYPE')):
100                        owa_coreAPI::setupStorageEngine($config['db_type']);
101                else:
102                        owa_coreAPI::setupStorageEngine(OWA_DB_TYPE);
103                endif;
104               
105                /* SETUP CONFIGURATION AND ERROR LOGGER */
106               
107                // Parent Constructor. Sets default config entity and error logger
108                $this->owa_base();
109               
110                // Log version debug
111                $this->e->debug(sprintf('*** Starting Open Web Analytics v%s. Running under PHP v%s (%s) ***', OWA_VERSION, PHP_VERSION, PHP_OS));
112                       
113                // Backtrace. handy for debugging who called OWA       
114                //$bt = debug_backtrace();
115                //$this->e->debug($bt[4]);
116               
117                /* APPLY CONFIGURATION FILE OVERRIDES */
118               
119                if ($config_file_exists == true):
120                       
121                        /* ERROR LOGGING */
122               
123                        // Looks for log level constant
124                        if (defined('OWA_ERROR_LOG_LEVEL')):
125                                $this->c->set('base', 'error_log_level', OWA_ERROR_LOG_LEVEL);
126                        endif;
127               
128                        /* PHP ERROR LOGGING */
129                       
130                        if (OWA_LOG_PHP_ERRORS === true):
131                                $this->e->logPhpErrors();
132                        endif;
133                       
134                        /* CONFIGURATION ID */
135                       
136                        if (defined('OWA_CONFIGURATION_ID')):
137                                $this->c->set('base', 'configuration_id', OWA_CONFIGURATION_ID);
138                        endif;
139                       
140                        /* OBJECT CACHING */
141               
142                        // Looks for object cache config constant
143                        // must comebefore user db values are fetched from db
144                        if (defined('OWA_CACHE_OBJECTS')):
145                                $this->c->set('base', 'cache_objects', OWA_CACHE_OBJECTS);
146                        endif;
147                       
148                endif;
149                                       
150                /* DATABASE CONFIGURATION */
151               
152                // Can either be set by calling application or the config file.
153                // This needs to come before the fetch of user overrides from the DB
154                // Constants defined in the config file have the final word
155                // values passed from calling application must be applied prior
156                // to the rest of the caller's overrides
157               
158                if (!defined('OWA_DB_TYPE')):
159                        define('OWA_DB_TYPE', $config['db_type']);
160                endif;
161       
162                $this->c->set('base', 'db_type', OWA_DB_TYPE);
163                               
164                if (!defined('OWA_DB_NAME')):
165                        define('OWA_DB_NAME',  $config['db_name']);
166                endif;
167               
168                $this->c->set('base', 'db_name', OWA_DB_NAME);
169                               
170                if (!defined('OWA_DB_HOST')):
171                        define('OWA_DB_HOST',  $config['db_host']);
172                endif;         
173               
174                $this->c->set('base', 'db_host', OWA_DB_HOST);
175                               
176                if (!defined('OWA_DB_USER')):
177                        define('OWA_DB_USER',  $config['db_user']);
178                endif;
179               
180                $this->c->set('base', 'db_user', OWA_DB_USER);
181               
182                if (!defined('OWA_DB_PASSWORD')):
183                        define('OWA_DB_PASSWORD',  $config['db_password']);
184                endif;
185               
186                $this->c->set('base', 'db_password', OWA_DB_PASSWORD);
187               
188                                       
189                /* APPLY USER CONFIGURATION OVERRIDES FROM DATABASE */
190               
191                if (!defined('OWA_CONFIG_DO_NOT_FETCH_FROM_DB')):
192                        $this->c->set('base', 'do_not_fetch_config_from_db', $config['do_not_fetch_config_from_db']);
193                else:
194                        $this->c->set('base', 'do_not_fetch_config_from_db', OWA_CONFIG_DO_NOT_FETCH_FROM_DB);
195                endif;         
196               
197                // Applies config from db or cache
198                // check here is needed for installs when the configuration table does not exist.
199                if ($this->c->get('base', 'do_not_fetch_config_from_db') != true):
200                        $this->c->load($this->c->get('base', 'configuration_id'));
201                endif;
202
203                /* APPLY CALLER CONFIGURATION OVERRIDES */
204               
205                // overrides all default and user config values except defined in the config file
206                // must come after user overides are applied
207                // This will apply configuration overirdes that are specified by the calling application.
208                // This is usually used by plugins to setup integration specific configuration values.
209               
210                $this->c->applyModuleOverrides('base', $config);
211               
212                $this->e->debug('Caller configuration overrides applied.');
213               
214                /* SET ERROR HANDLER */
215               
216                // Looks for log handler constant from config file otherwise respects
217                // user and caller overrides
218                if (defined('OWA_ERROR_HANDLER')):
219                        $this->c->set('base', 'error_handler', OWA_ERROR_HANDLER);
220                endif;
221               
222                // Sets the correct mode of the error logger now that final config values are in place
223                // This will flush buffered msgs that were thrown up untill this point
224                $this->e->setHandler($this->c->get('base', 'error_handler'));
225               
226               
227                /* SETUP REQUEST CONTAINER */
228                $this->params = &owa_requestContainer::getInstance();
229                //print_r($this->params);
230               
231                /**
232                 * @todo This needs to be refactored into stateless api calls
233                 */
234                $this->api = &owa_coreAPI::singleton();
235                $this->api->caller_config_overrides = $config;
236                // should only be called once to load all modules
237                $this->api->setupFramework();
238                // check for required schema updates and sets update flag
239                // this is needed if the calling application or plugin needs to check for updates
240                if (!empty($this->api->modules_needing_updates)):
241                        $this->update_required = true;
242                endif;
243               
244                /* SET SITE ID */
245                // needed in standalone installs where site_id is not set in config file.
246                if (!empty($this->params['site_id'])):
247                        $this->c->set('base', 'site_id', $this->params['site_id']);
248                endif;
249               
250                // re-fetch the array now that overrides have been applied.
251                // needed for backwards compatability
252                $this->config = $this->c->fetch('base');
253               
254                /* LOAD SERVICE LAYER */
255                $this->service = owa_coreAPI::serviceSingleton();
256               
257                return;
258       
259        }
260       
261        function handleRequestFromUrl()  {
262               
263                //$this->params = owa_lib::getRequestParams();
264                return $this->handleRequest();
265               
266        }
267       
268        /**
269         * Logs a Page Request
270         *
271         * @param array $app_params     This is an array of application specific request params
272         */
273        function log($caller_params = '') {
274               
275                return $this->logEvent('base.processRequest', $caller_params);
276               
277        }
278       
279        /**
280         * Logs an event to the event queue
281         *
282         * This function sets the action to be perfromed, santizes,
283         * and adds all of PHP's $_SERVER vars to the $caller_params.
284         * $_REQUEST vars are already added to $this->params in the constructor.
285         *
286         * @param array $caller_params
287         * @param string $event_type
288         * @return boolean
289         */
290        function logEvent($event_type, $caller_params = '') {
291               
292                if ($this->c->get('base', 'error_log_level') > 9):
293                        $this->e->debug(print_r($this->e->backtrace(), true));
294                endif;
295               
296                //change config value to incomming site_id
297                if(!empty($caller_params['site_id'])):
298                        $this->config['site_id'] = $caller_params['site_id'];
299                        $this->c->set('base', 'site_id', $caller_params['site_id']);
300                else:
301                        $caller_params['site_id'] = $this->c->get('base', 'site_id');
302                endif;
303               
304                // do not log if the request is from a reserved IP
305                // ips = $this->c->get('base', 'log_not_log_ips');
306                //      ...
307               
308                // do not log if the do not log param is set by caller.
309                if ($this->params['do_not_log'] == true):
310                        return false;
311                endif;
312               
313                $params = array();
314                // Add PHP's $_SERVER scope variables to event properties
315                $params['server'] = $_SERVER;
316               
317                // Apply caller's params to event properties
318                if (!empty($caller_params)):
319                        $params['caller'] = $caller_params;
320                endif;
321               
322                // set controller to invoke
323                $params['action'] = $event_type;
324               
325                // Filter input
326                $params = owa_lib::inputFilter($params);
327               
328                //Load browscap
329                $bcap = owa_coreAPI::supportClassFactory('base', 'browscap', $params['server']['HTTP_USER_AGENT']);
330               
331                //$bcap = new owa_browscap($params['server']['HTTP_USER_AGENT']);  ///!
332               
333                // Abort if the request is from a robot
334                if ($this->config['log_robots'] != true):
335                        if ($bcap->robotCheck() == true):
336                                $this->e->debug("ABORTING: request appears to be from a robot");
337                                return;
338                        endif;
339                endif;
340               
341                // Fetch browser capabilities and and apply to event params
342                $params['browscap'] = get_object_vars($bcap->browser);
343       
344                return $this->handleRequest($params);
345               
346        }
347       
348        /**
349         * Logs event params taken from request scope (url, cookies, etc.).
350         * Takes event type from url.
351         *
352         * @return unknown
353         */
354        function logEventFromUrl($caller_params) {
355               
356                // keeps php executing even if the client closes the connection
357                ignore_user_abort(true);
358               
359                //$clean_params = owa_lib::inputFilter($caller_params);
360               
361                $striped_params = owa_lib::stripParams($caller_params);
362                $params =array();
363                // Apply caller specific params
364                        foreach ($striped_params as $k => $v) {
365                               
366                                $params[$k] = base64_decode(urldecode($v));
367                               
368                        }
369                       
370                //$this->e->debug('logEventFromUrl decoded params: '. print_r($params, true));
371               
372                return $this->logEvent($params['action'], $params);
373               
374        }
375       
376        function requestTag($site_id) {
377               
378                return $api->requestTag($site_id);
379               
380        }
381       
382        function placeHelperPageTags($echo = true) {
383               
384                $params = array();
385                $params['view'] = 'base.helperPageTags';
386                $params['view_method'] = 'delegate';
387               
388                if ($echo == false):
389                        //return $this->handleHelperPageTagsRequest();
390                        return $this->handleRequest($params);
391                else:
392                        echo $this->handleRequest($params);
393                        return;
394                endif;
395               
396        }
397       
398        function handleHelperPageTagsRequest() {
399       
400                $params = array();
401                $params['view'] = 'base.helperPageTags';
402                $params['view_method'] = 'delegate';
403                return $this->handleRequest($params);
404       
405        }
406       
407
408        /**
409         * Authenticated Rendering of view
410         *
411         * @param array $caller_data
412         * @depricated
413         * @return string
414         */
415        function renderView($data) {
416       
417                $view =  $this->api->moduleFactory($data['view'], 'View', $this->params);
418               
419                //perfrom authentication
420                $auth = &owa_auth::get_instance();
421                $auth_data = $auth->authenticateUser($view->priviledge_level);
422       
423                // if auth was success then procead to assemble view.
424                if ($auth_data['auth_status'] == true):
425       
426                        return $view->assembleView($data);
427                else: 
428                        //$this->e->debug('RenderView: '.print_r($data, true));
429                        return $this->api->displayView($auth_data);
430                endif;
431               
432        }
433       
434        /**
435         * Displays a View without user authentication. Takes array of data as input
436         * @depricated
437         * @param array $data
438         */
439        function displayView($data) {
440               
441                $view =  $this->api->moduleFactory($data['view'], 'View', $this->params);
442               
443                return $view->assembleView($data);
444               
445        }
446       
447
448       
449        /**
450         * Handles OWA internal page/action requests
451         *
452         * @return unknown
453         */
454        function handleRequest($caller_params = null) {
455               
456                static $init;
457               
458                // Override request parsms with those passed by caller
459                if (!empty($caller_params)):
460                        $this->params = array_merge($this->params, $caller_params);
461                endif;
462               
463                if ($init != true):
464                        $this->e->debug('Request Params: '. print_r($this->params, true));
465                endif;
466                               
467                if (!empty($this->params['action'])):
468                        $result = owa_coreAPI::performAction($this->params['action'], $this->params);
469                        unset($this->params['action']);
470                       
471                elseif (!empty($this->params['do'])):
472                        $result = owa_coreAPI::performAction($this->params['do'], $this->params);       
473                        //unset($this->params['action']);
474                       
475                elseif ($this->params['view']):
476                        // its a view request so the only data is in whats in the params
477                        $result = owa_coreAPI::displayView($this->params);
478                        unset($this->params['view']);
479                       
480                else:
481                        print "Caller: No view or action param found. I'm not sure what to do here.";
482                        return;
483                       
484                endif;
485               
486                $init = true;
487               
488                return $result;
489        }
490       
491        function handleSpecialActionRequest() {
492               
493                if(isset($_GET['owa_specialAction'])):
494                        $this->e->debug("special action received");
495                        echo $this->handleRequestFromUrl();
496                        exit;
497                elseif(isset($_GET['owa_logAction'])):
498                        $this->e->debug("log action received");
499                        $this->config['delay_first_hit'] = false;
500                        $this->c->set('base', 'delay_first_hit', false);
501                        echo $this->logEventFromUrl($_GET);
502                        exit;
503                else:
504                        return;
505                endif;
506
507        }
508       
509        function __destruct() {
510               
511                $this->end_time = owa_lib::microtime_float();
512                $total_time = $this->end_time - $this->start_time;
513                $this->e->debug(sprintf('Total session time: %s',$total_time));
514                $this->e->debug("goodbye from OWA");
515               
516                return;
517        }
518       
519}
520
521?>
Note: See TracBrowser for help on using the browser.