root/trunk/module.inc

Revision 558, 12.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 2008 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
19require_once('owa_env.php');
20require_once(OWA_BASE_CLASSES_DIR.'owa_php.php');
21
22/**
23 * OWA Singleton.
24 *
25 * Creates instance of OWA that can be called from within Gallery.
26 * All configuration taken from Gallery directly.
27 */
28function owa_factory($params = array()) {
29       
30        static $owa;
31
32        if(!empty($owa)):
33                return $owa;
34        else:
35       
36                // globals
37                global $gallery;
38               
39                // init the configuration array for this caller
40                $owa_config = $params;
41               
42                // OWA DATABASE CONFIGURATION
43                // Will use Gallery config unless there is a config file present.
44                // OWA uses this to setup it's own DB connection seperate from the one
45                // that Gallery uses.
46               
47                // Get Gallery's DB configuration
48                $db_config = $gallery->getConfig('storage.config');
49                        $gallery->debug($db_config['type']);
50                // set OWA's database configuration values
51                if ($db_config['type'] === 'mysqli'):
52                        $owa_config['db_type'] = 'mysql';
53                else:
54                        $owa_config['db_type'] = $db_config['type'];
55                endif;
56                $gallery->debug($owa_config['db_type']);
57                $owa_config['db_name'] = $db_config['database'];
58                $owa_config['db_host'] = $db_config['hostname'];
59                $owa_config['db_user'] = $db_config['username'];
60                $owa_config['db_password'] = $db_config['password'];
61                       
62                //$gallery_base_url = $gallery->getConfig('galleryBaseUrl');
63                $urlgenerator = $gallery->getUrlGenerator();
64                $gallery_base_url = $urlgenerator->getCurrentUrlDir();
65               
66                // Public folder URL
67                define('OWA_PUBLIC_URL', $gallery_base_url.'modules/owa/public/');
68               
69                // Gallery specific config overrides array
70               
71                $owa_config['report_wrapper'] = 'wrapper_gallery2.tpl';
72                $owa_config['images_url'] = OWA_PUBLIC_URL.'i/';
73                $owa_config['images_absolute_url'] = $owa_config['images_url'];
74                $owa_config['main_url'] = $gallery_base_url.'main.php?g2_view=core.SiteAdmin&g2_subView=owa.owaGeneric';
75                $owa_config['main_absolute_url'] = $owa_config['main_url'];
76                $owa_config['action_url'] = $gallery_base_url.'main.php?g2_view=owa.owaAction&owa_specialAction';
77                $owa_config['log_url'] = $gallery_base_url.'main.php?g2_view=owa.owaAction&owa_logAction=1';
78                $owa_config['link_template'] = '%s&%s';
79                //$owa_config['authentication'] = 'gallery';
80                $owa_config['site_id'] = md5($gallery_base_url);
81                $owa_config['query_string_filters'] = 'g2_fromNavId';
82                $owa_config['is_embedded'] = 'true';
83               
84                $gallery->debug('hello from owa');
85
86                // create owa instance
87                $owa = new owa_php($owa_config);
88                $gallery->debug('new owa instance created');
89               
90                return $owa;
91               
92        endif;
93
94}
95
96/**
97 * Sets OWA priviledge info for current gallery user
98 */
99function owa_set_priviledges() {
100
101        global $gallery;
102       
103        // get Gallery's active user
104        $u = $gallery->getActiveUser();
105       
106        // create instance of OWA
107        $owa = owa_factory();
108       
109        //set user level. Needed for OWA's auth module.
110       
111        // check to see if user is a guest or not
112        list ($ret, $user) = GalleryCoreApi::isAnonymousUser();
113       
114        if ($user == true):
115       
116                $level = 'everyone';
117               
118        else:
119                // check to see if the user is a site admin. important becasue we might not want
120                // to log such users activities.
121                list ($ret, $admin) = GalleryCoreApi::isUserInSiteAdminGroup();
122       
123                if ($admin = true):
124                        $level = 'admin';
125                else:
126                        $level = 'viewer';
127                endif;
128               
129        endif;         
130       
131        // preemptively set the current user info and mark as authenticated so that
132        // downstream controllers don't have to authenticate
133        $cu =&owa_coreAPI::getCurrentUser();
134        $cu->setUserData('user_id', $u->id);
135        $cu->setUserData('email_address', $u->email);
136        $cu->setUserData('real_name', $u->fullname);
137        $cu->setRole($level);
138        $cu->setAuthStatus(true);
139       
140        /*
141                       
142        $owa->params['caller']['gallery']['user_data'] = array(
143       
144                                        'user_level'    => $level,
145                                        'user_ID'               => $u->id,
146                                        'user_login'    => $u->userName,
147                                        'user_email'    => $u->email,
148                                        'user_identity' => $u->fullname,
149                                        );
150                                       
151                                        $owa->params['u'] = $u->userName;
152                                        $owa->params['p'] = 'xxxxxxx';
153
154        */
155       
156        return;
157}
158
159
160
161/**
162 * OWA Gallery Module
163 *
164 * Integrates OWA with Gallery 2.2 or later
165 *
166 * @package owa
167 * @author Peter Adams <peter@openwebanalytics.com>
168 * @version $Revision$ $Date: $
169 */
170class owaModule extends GalleryModule {
171
172        function owaModule() {
173        global $gallery;
174
175        $this->setId('owa');
176        $this->setName($gallery->i18n('Open Web Analytics for Gallery'));
177        $this->setDescription($gallery->i18n('Adds web analytics capabilities to Gallery.'));
178        $this->setVersion('1.0.0');
179        $this->setGroup('OWA', $gallery->i18n('Open Web Analytics'));
180        $this->setCallbacks('');
181        $this->setRequiredCoreApi(array(7, 18));
182        $this->setRequiredModuleApi(array(3, 4));
183        $this->setCallbacks('getSiteAdminViews|getSystemLinks');
184       
185        return;
186    }
187   
188   
189        /**
190         * Main OWA logging method
191         *
192         * Using getSystemLinks as a callback because it is called on every request.
193         */
194        function getSystemLinks() {
195               
196                global $gallery;
197               
198               
199        if (GalleryUtilities::hasRequestVariable('view')):
200                $viewName = GalleryUtilities::getRequestVariables('view');
201               
202               
203                // ensure this is not a Gallery admin screen
204                if ($viewName == "core.SiteAdmin" || $viewName == "core.ItemAdmin"):
205                        return;
206                else:
207                       
208                        // get instance of owa
209                                $owa = owa_factory();
210                               
211                                // set user priviledges of the request for OWA to log
212                                owa_set_priviledges();
213               
214                                // Setup OWA request params
215                                $params = array();
216                               
217                                // get user gallery's object to set request variables
218                                //$params['user_name']= $u->userName;
219                                //$params['user_email'] = $u->email;   
220                       
221                                // get information on current view     
222                                list ($ret, $view) = GalleryView::loadView($viewName);
223                                list ($ret, $page_type) = $view->getViewDescription();
224                                $params['page_type'] = $page_type;
225               
226                                //Log request is for an item, get item details
227                                if (GalleryUtilities::hasRequestVariable('itemId')):
228                                        //Lookup item from view
229                                        list ($rest, $item) = $view->getItem();
230                                        $params['page_title'] = $item->title;
231                                else:
232                                        $params['page_title'] = $page_type;                     
233                                endif;
234                               
235                                // is RSS page type
236                               
237                                if (($viewName == "rss.Render") || ($viewName == "rss.SimpleRender")):
238                                        $params['page_type'] = 'feed';
239                                        $params['is_feedreader'] = true;
240                                        $params['feed_format'] = $_GET['feed'];
241                                endif;
242                                               
243                                // log request
244                               
245                                //print_r($owa->config);
246                               
247                                $owa->log($params);
248                        endif; 
249                endif;
250                               
251                return;
252       
253        }
254       
255       
256        /**
257         * Check to see if OWA is installed and activated
258         *
259         */
260        function owa_isActivated() {
261       
262                list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'owa');
263               
264                if (!empty($params)):
265                        return true;
266                else:
267                        return false;
268                endif;
269        }
270       
271       
272        /**
273     * @see GalleryModule::getSiteAdminViews
274     */
275    function getSiteAdminViews() {
276       
277        global $gallery;
278       
279        // this is needed becasue on the plugins page this callback is triggered
280        // whether then plugin is active or not for some reason.
281        //if ($this->owa_isActivated()):
282                        // get OWA instance
283                //      $owa = owa_factory();
284                        // set user priviledges of the request for OWA
285                //      owa_set_priviledges();
286                //endif;
287                                       
288                $data = array(array('name' => $this->translate('Dashboard'), 'view' => 'owa.owaDashboard'),
289                                          array('name' => $this->translate('Admin Settings'), 'view' => 'owa.owaOptions'));                 
290                return array(null, $data);
291    }
292   
293    /**
294     * Module specific logic for install
295     *
296     * @see GalleryModule::install
297     */
298    function upgrade($currentVersion, $statusMonitor) {
299               
300                global $gallery;
301               
302                $owa_config = array();
303                $owa_config['do_not_fetch_config_from_db'] = true;             
304                $owa = owa_factory($owa_config);
305                                // set user priviledges of the request for OWA to log
306                owa_set_priviledges();
307               
308                //get the base gallery url
309                $urlgenerator = $gallery->getUrlGenerator();
310                $site_url = $urlgenerator->getCurrentUrlDir();
311               
312                //Config('galleryBaseUrl');
313                       
314        $params = array('site_id' => md5($site_url),
315                                        'name' => 'Gallery',
316                                        'domain' => $site_url,
317                                        'description' => '',
318                                        'do' => 'base.installEmbedded');
319                                       
320        $page = $owa->handleRequest($params);
321       
322                return null;
323    }
324
325
326}
327
328/**
329 * OWA Gallery Views
330 *
331 * Enables OWA to be embedded as a Gallery's site admin screen
332 */
333class owaOptionsView extends GalleryView {
334
335        /**
336     * @see GalleryView::loadTemplate
337     */
338    function loadTemplate(&$template, &$form) {
339
340                $owa = owa_factory();
341               
342                owa_set_priviledges();
343               
344                $params = array();
345               
346                 if (empty($owa->params['do'])):
347                if (empty($owa->params['view'])):               
348                                $params['view'] = 'base.options';
349                        endif;
350                endif;
351                       
352                $page = $owa->handleRequest($params);
353                $template->setVariable('owa', array('content' => $page));
354                return array(null, array('body' => 'modules/owa/modules/base/templates/gallery.tpl'));
355    }
356   
357    /**
358     * Does this view change any data? Only controllers should change data, but AJAX and some
359     * immediate views are handled in views in Gallery.
360     * @return bool true if the view changes data
361     */
362    function isControllerLike() {
363                return true;
364    }
365
366               
367}
368
369/**
370 * OWA Gallery Views
371 *
372 *
373 */
374class owaDashboardView extends GalleryView {
375
376        /**
377     * @see GalleryView::loadTemplate
378     */
379    function loadTemplate(&$template, &$form) {
380
381                $owa = owa_factory();
382               
383                owa_set_priviledges();
384               
385                $params = array();
386                //$params['view'] = 'base.report';
387                $params['action'] = 'base.reportDashboard';
388                $params['period'] = 'today';     
389                $page = $owa->handleRequest($params);
390                $template->setVariable('owa', array('content' => $page));
391                return array(null, array('body' => 'modules/owa/modules/base/templates/gallery.tpl'));
392    }
393               
394}
395
396class owaGenericView extends GalleryView {
397
398        /**
399     * @see GalleryView::loadTemplate
400     */
401    function loadTemplate(&$template, &$form) {
402
403                $owa = owa_factory();
404               
405                owa_set_priviledges();
406               
407                $page = $owa->handleRequest();
408                $template->setVariable('owa', array('content' => $page));
409                return array(null, array('body' => 'modules/owa/modules/base/templates/gallery.tpl'));
410    }
411       
412        /**
413     * Does this view change any data? Only controllers should change data, but AJAX and some
414     * immediate views are handled in views in Gallery.
415     * @return bool true if the view changes data
416     */
417    function isControllerLike() {
418                return true;
419    }
420               
421}
422
423
424GalleryCoreApi::requireOnce('modules/core/classes/GalleryController.class');
425class owaControlController extends GalleryController {
426
427        /**
428     * @see GalleryController::handleRequest
429     */
430    function handleRequest($form) {
431   
432        $result = array('delegate' => array('view' => 'owa.owaGeneric'),
433                        'status' => 1, 'error' => '');
434        return array(null, $result);
435   
436    }
437
438}
439
440
441/**
442 * Handles OWA's special action requests
443 *
444 */
445class owaActionView extends GalleryView {
446
447
448        /**
449     * @see GalleryView::isImmediate
450     */
451    function isImmediate() {
452                return true;
453    }
454
455        /**
456         * Method called when view is set to render immeadiately.
457         * This will bypass Gallery's global templating allowing
458         * the view to send output directly to the browser.
459         */
460        function renderImmediate($status, $error) {
461       
462                global $gallery;
463                $owa = owa_factory();
464               
465                $gallery->debug('hello from owaAction');
466                owa_set_priviledges();
467               
468                $owa->handleSpecialActionRequest();
469               
470                return null;
471    }
472
473       
474}
475
476/**
477 * Gallery Template Callback for OWA footer elements
478 *
479 * This class is packaged here for convienence only but could also be
480 * put in Callbacks.inc.
481 */
482class owaCallbacks {
483       
484        function callback($params, &$smarty, $callback, $userId=null) {
485                /* 1. Identify the exact callback */
486                switch ($callback) {
487                case 'pagetags':
488               
489                $viewName = GalleryUtilities::getRequestVariables('view');
490                // ensure this is not a Gallery admin screen
491               
492                if ($viewName == "core.SiteAdmin" || $viewName == "core.ItemAdmin"):
493                        return;
494                else:
495                                /* 2. Load the requested data */
496                               
497                                $owa = owa_factory();
498                                $tags = $owa->placeHelperPageTags(false);
499                               
500                                /* 3. Assign the requested data to a template variable */
501                                $block =& $smarty->_tpl_vars['block'];
502                               
503                                /* By convention, put the data into $block[$moduleId] (in this case, moduleId is 'owa') */
504                                $block['owa']['pagetags'] = array('owaData' => $tags,
505                                                           'randomNumber' => rand()); // You can put any data into the template variable...
506                endif;
507               
508                break;
509                       
510                        case 'SomeOtherCallbackName':
511                ;
512                break;
513            default:
514                ;
515        }
516
517                return null;
518        }
519}
520
521
522?>
Note: See TracBrowser for help on using the browser.