~ubuntu-branches/ubuntu/hardy/gallery2/hardy-security

« back to all changes in this revision

Viewing changes to modules/core/classes/helpers/GalleryPluginHelper_simple.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2006-04-16 16:42:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060416164235-8uy0u4bfjdxpge2o
Tags: 2.1.1-1
* New upstream release (Closes: #362936)
  + Bugfixes for Postgres7 (Closes: #359000, #362152)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 * $RCSfile: GalleryPluginHelper_simple.class,v $
4
4
 *
5
5
 * Gallery - a web based photo album viewer and editor
6
 
 * Copyright (C) 2000-2005 Bharat Mediratta
 
6
 * Copyright (C) 2000-2006 Bharat Mediratta
7
7
 *
8
8
 * This program is free software; you can redistribute it and/or modify
9
9
 * it under the terms of the GNU General Public License as published by
20
20
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
21
21
 */
22
22
/**
23
 
 * @version $Revision: 1.32 $ $Date: 2005/09/04 20:11:07 $
 
23
 * @version $Revision: 1.38 $ $Date: 2006/03/22 00:30:34 $
24
24
 * @package GalleryCore
25
25
 * @author Bharat Mediratta <bharat@menalto.com>
26
26
 */
39
39
     * @param string the plugin type (eg, module, theme, etc)
40
40
     * @param string the plugin id
41
41
     * @param bool should we ignore version mismatches (default: no)
 
42
     * @param bool should we return an error on version mismatches instead of redirecting
 
43
     *             to the upgrader?  (default: false)
42
44
     * @param int the depth of recursion (don't set this- it's used internally)
43
45
     * @return array object GalleryStatus a status code
44
46
     *               object the plugin
45
47
     * @static
46
48
     */
47
 
    function loadPlugin($pluginType, $pluginId, $ignoreVersionMismatch=false, $depth=0) {
 
49
    function loadPlugin($pluginType, $pluginId, $ignoreVersionMismatch=false,
 
50
                        $errorOnVersionMismatch=false, $depth=0) {
48
51
        global $gallery;
49
52
 
50
53
        if ($gallery->getDebug()) {
66
69
                break;
67
70
 
68
71
            default:
69
 
                return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
 
72
                return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
70
73
                                                  "pluginType = $pluginType"), null);
71
74
            }
72
75
 
75
78
                    $gallery->debug('Class not defined, trying to include it.');
76
79
                }
77
80
 
78
 
                $pluginBaseDir = dirname(__FILE__) . '/../../../../';
 
81
                $pluginBaseDir = GalleryCoreApi::getPluginBaseDir($pluginType, $pluginId);
79
82
                $pluginFile = sprintf('%ss/%s/%s.inc', $pluginType, $pluginId, $pluginType);
80
83
 
81
 
                GalleryCoreApi::relativeRequireOnce(
 
84
                GalleryCoreApi::requireOnce(
82
85
                    sprintf('modules/core/classes/%s.class', $pluginSuperClass));
83
86
 
84
 
                $platform = $gallery->getPlatform();
 
87
                $platform =& $gallery->getPlatform();
85
88
                if (!$platform->file_exists($pluginBaseDir . $pluginFile)) {
86
89
                    /*
87
90
                     * If we have a bad path, it may be because our cached plugin list is out of
94
97
                            array('type' => $pluginType,
95
98
                                  'itemId' => 'GalleryPluginHelper_fetchPluginStatus',
96
99
                                  'id' => '_all'));
97
 
                        list ($ret, $plugin) =
98
 
                            GalleryPluginHelper_simple::loadPlugin($pluginType, $pluginId,
99
 
                                                                   $ignoreVersionMismatch, $depth + 1);
100
 
                        if ($ret->isError()) {
 
100
                        list ($ret, $plugin) = GalleryPluginHelper_simple::loadPlugin(
 
101
                            $pluginType, $pluginId, $ignoreVersionMismatch,
 
102
                            $errorOnVersionMismatch, $depth + 1);
 
103
                        if ($ret) {
101
104
                            return array($ret->wrap(__FILE__, __LINE__), null);
102
105
                        }
103
106
                    } else {
104
 
                        return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
105
 
                                                      $pluginBaseDir . $pluginFile), null);
 
107
                        return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
 
108
                                                           $pluginBaseDir . $pluginFile), null);
106
109
                    }
107
110
                }
108
111
 
109
 
                GalleryCoreApi::relativeRequireOnce($pluginFile);
 
112
                GalleryCoreApi::requireOnce($pluginFile);
110
113
 
111
114
                if (!class_exists($pluginClass)) {
112
 
                    return array(GalleryStatus::error(ERROR_BAD_PLUGIN, __FILE__, __LINE__,
 
115
                    return array(GalleryCoreApi::error(ERROR_BAD_PLUGIN, __FILE__, __LINE__,
113
116
                                                      "Class $pluginClass does not exist"), null);
114
117
                }
115
118
            }
132
135
 
133
136
            /* Verify that the versions match */
134
137
            list ($ret, $status) = GalleryPluginHelper_simple::fetchPluginStatus($pluginType);
135
 
            if ($ret->isError()) {
 
138
            if ($ret) {
136
139
                return array($ret->wrap(__FILE__, __LINE__), null);
137
140
            }
138
141
 
147
150
 
148
151
                    /* Try to deactivate the plugin */
149
152
                    list ($ret, $redirectInfo) = $plugin->deactivate();
150
 
                    if ($ret->isError()) {
 
153
                    if ($ret) {
151
154
                        return array($ret->wrap(__FILE__, __LINE__), null);
152
155
                    }
153
156
 
154
 
                    if (!empty($redirectInfo)) {
 
157
                    if (!empty($redirectInfo) && !$errorOnVersionMismatch) {
155
158
                        /*
156
159
                         * We were unable to automatically deactivate the plugin!
157
160
                         * Forcibly abort this request and jump to the upgrader so this
158
161
                         * module can be upgraded.
159
162
                         */
160
 
                        $gallery->debug("Unable to deactivate $pluginId, jumping to upgrader.");
161
 
                        $gallery->debug_r($redirectInfo);
 
163
                        if ($gallery->getDebug()) {
 
164
                            $gallery->debug("Unable to deactivate $pluginId, jumping to upgrader.");
 
165
                            $gallery->debug_r($redirectInfo);
 
166
                        }
162
167
                        $urlGenerator =& $gallery->getUrlGenerator();
163
168
                        header('Location: ' .
164
169
                               $urlGenerator->getCurrentUrlDir(true) . 'upgrade/index.php');
166
171
                    }
167
172
 
168
173
                    /*  Return a handleable error */
169
 
                    return array(GalleryStatus::error(ERROR_PLUGIN_VERSION_MISMATCH,
 
174
                    return array(GalleryCoreApi::error(ERROR_PLUGIN_VERSION_MISMATCH,
170
175
                                                      __FILE__, __LINE__,
171
176
                                     sprintf('[%s] db: (v: %s core api: %s, %s api: %s) '.
172
177
                                             'code: (v: %s core api: %s, %s api: %s) ',
192
197
            }
193
198
        }
194
199
 
195
 
        return array(GalleryStatus::success(), $plugin);
 
200
        return array(null, $plugin);
196
201
    }
197
202
 
198
203
    /**
238
243
        }
239
244
 
240
245
        list ($ret, $params) =
241
 
            GalleryPluginHelper_simple::fetchAllParameters($pluginType, $pluginId, $itemId);
242
 
        if ($ret->isError()) {
 
246
            GalleryPluginHelper_simple::_fetchAllParameters($pluginType, $pluginId, $itemId);
 
247
        if ($ret) {
243
248
            return array($ret->wrap(__FILE__, __LINE__), null);
244
249
        }
245
250
 
246
251
        /* Return the value, or null if the param doesn't exist */
247
 
        if (!isset($params[$parameterName])) {
248
 
            return array(GalleryStatus::success(), null);
 
252
        if (!isset($params[$itemId][$parameterName])) {
 
253
            return array(null, null);
249
254
        } else {
250
 
            return array(GalleryStatus::success(), $params[$parameterName]);
 
255
            return array(null, $params[$itemId][$parameterName]);
251
256
        }
252
257
    }
253
258
 
256
261
     *
257
262
     * @param string the type of the plugin
258
263
     * @param string the id of the plugin
259
 
     * @param integer the id of item (or null for a global setting)
 
264
     * @param integer the id of item (or null/zero for a global setting)
260
265
     * @return array object GalleryStatus a status code
261
266
     *               array (parameterName => parameterValue)
262
267
     * @static
263
268
     */
264
269
    function fetchAllParameters($pluginType, $pluginId, $itemId=0) {
 
270
        /* Convert null to 0, just in case */
 
271
        if ($itemId == null) {
 
272
            $itemId = 0;
 
273
        }
 
274
        list ($ret, $params) =
 
275
            GalleryPluginHelper_simple::_fetchAllParameters($pluginType, $pluginId, $itemId);
 
276
        if ($ret) {
 
277
            return array($ret->wrap(__FILE__, __LINE__), null);
 
278
        }
 
279
        return array(null, $params[$itemId]);
 
280
    }
 
281
 
 
282
    /**
 
283
     * Get all the parameters for this plugin
 
284
     *
 
285
     * @param string the type of the plugin
 
286
     * @param string the id of the plugin
 
287
     * @param mixed int id of item (or null/zero for a global setting) or array of ids
 
288
     * @return array object GalleryStatus a status code
 
289
     *               array (itemId/zero => array(parameterName => parameterValue), ..)
 
290
     * @static
 
291
     */
 
292
    function _fetchAllParameters($pluginType, $pluginId, $itemId) {
265
293
        global $gallery;
266
294
        if (empty($pluginType) || empty($pluginId)) {
267
 
            return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
 
295
            return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
268
296
                                              sprintf("Missing pluginType [%s] or pluginId [%s]",
269
 
                                                      $pluginType,
270
 
                                                      $pluginId)),
 
297
                                                      $pluginType, $pluginId)),
271
298
                         null);
272
299
        }
273
300
 
276
303
            $itemId = 0;
277
304
        }
278
305
 
279
 
        $cacheKey = "GalleryPluginHelper::fetchAllParameters($pluginType, $pluginId, $itemId)";
280
 
        if (!GalleryDataCache::containsKey($cacheKey)) {
281
 
            $data =& GalleryDataCache::getFromDisk(array('type' => $pluginType,
282
 
                                                         'itemId' => $itemId,
283
 
                                                         'id' => $pluginId));
284
 
            if (!isset($data)) {
285
 
                $query = '
286
 
                  SELECT
287
 
                    [GalleryPluginParameterMap::parameterName],
288
 
                    [GalleryPluginParameterMap::parameterValue]
289
 
                  FROM
290
 
                    [GalleryPluginParameterMap]
291
 
                  WHERE
292
 
                    [GalleryPluginParameterMap::pluginType] = ?
293
 
                    AND
294
 
                    [GalleryPluginParameterMap::pluginId] = ?
295
 
                    AND
296
 
                    [GalleryPluginParameterMap::itemId] = ?
297
 
                  ';
298
 
 
299
 
                list ($ret, $searchResults) =
300
 
                    $gallery->search($query, array($pluginType, $pluginId, $itemId));
301
 
                if ($ret->isError()) {
302
 
                    return array($ret->wrap(__FILE__, __LINE__), null);
303
 
                }
304
 
 
305
 
                $data = array();
306
 
                while ($result = $searchResults->nextResult()) {
307
 
                    $data[$result[0]] = $result[1];
308
 
                }
309
 
 
310
 
                GalleryDataCache::putToDisk(array('type' => $pluginType,
311
 
                                                  'itemId' => $itemId,
312
 
                                                  'id' => $pluginId),
313
 
                                            $data);
314
 
            }
315
 
            GalleryDataCache::put($cacheKey, $data);
316
 
        } else {
317
 
            $data = GalleryDataCache::get($cacheKey);
318
 
        }
319
 
 
320
 
        return array(GalleryStatus::success(), $data);
 
306
        /* Check cache */
 
307
        $data = $queryIds = array();
 
308
        $itemIds = is_array($itemId) ? $itemId : array($itemId);
 
309
        $cacheBase = "GalleryPluginHelper::fetchAllParameters($pluginType, $pluginId, ";
 
310
        foreach ($itemIds as $itemId) {
 
311
            $cacheKey = $cacheBase . $itemId . ')';
 
312
            if (GalleryDataCache::containsKey($cacheKey)) {
 
313
                $data[$itemId] = GalleryDataCache::get($cacheKey);
 
314
                continue;
 
315
            }
 
316
            $cacheData =& GalleryDataCache::getFromDisk(
 
317
                array('type' => $pluginType, 'itemId' => $itemId, 'id' => $pluginId));
 
318
            if (isset($cacheData)) {
 
319
                $data[$itemId] = $cacheData;
 
320
                GalleryDataCache::put($cacheKey, $cacheData);
 
321
                continue;
 
322
            }
 
323
            $queryIds[] = (int)$itemId;
 
324
        }
 
325
        if (empty($queryIds)) {
 
326
            return array(null, $data);
 
327
        }
 
328
 
 
329
        $whereId = count($queryIds) > 1 ? 'IN (' . GalleryUtilities::makeMarkers($queryIds) . ')'
 
330
                                        : '= ?';
 
331
        $query = '
 
332
          SELECT
 
333
            [GalleryPluginParameterMap::itemId],
 
334
            [GalleryPluginParameterMap::parameterName],
 
335
            [GalleryPluginParameterMap::parameterValue]
 
336
          FROM
 
337
            [GalleryPluginParameterMap]
 
338
          WHERE
 
339
            [GalleryPluginParameterMap::pluginType] = ?
 
340
            AND
 
341
            [GalleryPluginParameterMap::pluginId] = ?
 
342
            AND
 
343
            [GalleryPluginParameterMap::itemId] ' . $whereId;
 
344
        array_unshift($queryIds, $pluginType, $pluginId);
 
345
 
 
346
        list ($ret, $searchResults) = $gallery->search($query, $queryIds);
 
347
        if ($ret) {
 
348
            return array($ret->wrap(__FILE__, __LINE__), null);
 
349
        }
 
350
 
 
351
        while ($result = $searchResults->nextResult()) {
 
352
            $data[$result[0]][$result[1]] = (string)$result[2];
 
353
        }
 
354
 
 
355
        foreach (array_slice($queryIds, 2) as $itemId) {
 
356
            if (!isset($data[$itemId])) {
 
357
                $data[$itemId] = array();
 
358
            }
 
359
            GalleryDataCache::putToDisk(array('type' => $pluginType,
 
360
                                              'itemId' => $itemId,
 
361
                                              'id' => $pluginId),
 
362
                                        $data[$itemId]);
 
363
            GalleryDataCache::put($cacheBase . $itemId . ')', $data[$itemId]);
 
364
        }
 
365
 
 
366
        return array(null, $data);
321
367
    }
322
368
 
323
369
    /**
343
389
            }
344
390
            if (!isset($plugins)) {
345
391
                list ($ret, $plugins) = GalleryPluginHelper_simple::fetchPluginList($pluginType);
346
 
                if ($ret->isError()) {
 
392
                if ($ret) {
347
393
                    return array($ret->wrap(__FILE__, __LINE__), null);
348
394
                }
349
395
 
350
 
                $platform = $gallery->getPlatform();
 
396
                $platform =& $gallery->getPlatform();
 
397
 
 
398
                $pluginBaseDirs = GalleryCoreApi::getPluginBaseDirs();
 
399
                $pluginBaseIndexFile = sprintf(
 
400
                    '%sindex.%ss', $gallery->getConfig('data.gallery.plugins'), $pluginType);
351
401
 
352
402
                /* Scan modules directory for installed modules */
353
403
                switch ($pluginType) {
354
404
                case 'module':
355
 
                    $pluginsDir = dirname(__FILE__) . '/../../../../modules/';
 
405
                    $pluginsDir = 'modules/';
356
406
                    $pluginFile = 'module.inc';
357
407
                    break;
358
408
 
359
409
                case 'theme':
360
 
                    $pluginsDir = dirname(__FILE__) . '/../../../../themes/';
 
410
                    $pluginsDir = 'themes/';
361
411
                    $pluginFile = 'theme.inc';
362
412
                    break;
363
413
 
364
414
                default:
365
 
                    return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
 
415
                    return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
366
416
                                                      sprintf("Wrong pluginType [%s]",
367
417
                                                              $pluginType)),
368
418
                                 null);
369
419
                }
370
420
 
371
 
                if ($dir = $platform->opendir($pluginsDir)) {
372
 
                    while ($pluginId = $platform->readdir($dir)) {
373
 
                        if ($pluginId{0} == '.') {
374
 
                            continue;
375
 
                        }
376
 
                        if (!$platform->is_dir($pluginsDir . $pluginId)) {
377
 
                            continue;
378
 
                        }
379
 
 
380
 
                        $path = $pluginsDir . $pluginId . '/' . $pluginFile;
381
 
                        if ($platform->file_exists($path)) {
382
 
                            $plugins[$pluginId]['available'] = 1;
383
 
 
384
 
                            $frameworkParams = array('version', 'callbacks', 'requiredCoreApi');
385
 
                            if ($pluginType == 'module') {
386
 
                                $frameworkParams[] = 'requiredModuleApi';
387
 
                            } else {
388
 
                                $frameworkParams[] = 'requiredThemeApi';
389
 
                            }
390
 
 
391
 
                            foreach ($frameworkParams as $paramName) {
392
 
                                list ($ret, $plugins[$pluginId][$paramName]) =
393
 
                                    GalleryPluginHelper_simple::getParameter(
394
 
                                        $pluginType, $pluginId, '_' . $paramName);
395
 
                                if ($ret->isError()) {
396
 
                                    return array($ret->wrap(__FILE__, __LINE__), null);
397
 
                                }
398
 
 
399
 
                                /* Separate out the major/minor version. */
400
 
                                if (!strncmp($paramName, 'required', 8)) {
401
 
                                    $tmp = split(',', $plugins[$pluginId][$paramName]);
402
 
                                    // DEBUG: remove these lines below when we
403
 
                                    // commit; make sure that we bump all module
404
 
                                    // versions instead so that the module
405
 
                                    // version reqs aren't empty
406
 
                                    if (empty($tmp) || count($tmp) < 2) {
407
 
                                        $tmp = array(-1, -1);
408
 
                                    }
409
 
                                    $plugins[$pluginId][$paramName] =
410
 
                                        array((int)$tmp[0], (int)$tmp[1]);
411
 
                                }
412
 
                            }
413
 
                        }
 
421
                $frameworkParams = array('version', 'callbacks', 'requiredCoreApi');
 
422
                $frameworkParams[] =
 
423
                    ($pluginType == 'module') ? 'requiredModuleApi' : 'requiredThemeApi';
 
424
 
 
425
                foreach ($pluginBaseDirs as $pluginBaseDir) {
 
426
                    if ($dir = $platform->opendir($pluginBaseDir . $pluginsDir)) {
 
427
                        while ($pluginId = $platform->readdir($dir)) {
 
428
                            if ($pluginId{0} == '.') {
 
429
                                continue;
 
430
                            }
 
431
                            if (!$platform->is_dir($pluginBaseDir . $pluginsDir . $pluginId)) {
 
432
                                continue;
 
433
                            }
 
434
 
 
435
                            $path = $pluginBaseDir . $pluginsDir . $pluginId . '/' . $pluginFile;
 
436
                            if ($platform->file_exists($path)) {
 
437
                                $plugins[$pluginId]['available'] = 1;
 
438
 
 
439
                                foreach ($frameworkParams as $paramName) {
 
440
                                    list ($ret, $plugins[$pluginId][$paramName]) =
 
441
                                        GalleryPluginHelper_simple::getParameter(
 
442
                                            $pluginType, $pluginId, '_' . $paramName);
 
443
                                    if ($ret) {
 
444
                                        return array($ret->wrap(__FILE__, __LINE__), null);
 
445
                                    }
 
446
 
 
447
                                    /* Separate out the major/minor version. */
 
448
                                    if (!strncmp($paramName, 'required', 8)) {
 
449
                                        $tmp = explode(',', $plugins[$pluginId][$paramName]);
 
450
                                        $plugins[$pluginId][$paramName] = (count($tmp) < 2)
 
451
                                            ? array(-1, -1) : array((int)$tmp[0], (int)$tmp[1]);
 
452
                                    }
 
453
                                }
 
454
                            }
 
455
                        }
 
456
                        $platform->closedir($dir);
414
457
                    }
415
 
                    $platform->closedir($dir);
416
458
                }
417
459
 
418
460
                /* Find and remove plugins that are active, but not installed */
419
461
                foreach ($plugins as $pluginId => $pluginStatus) {
420
462
                    if (!isset($pluginStatus['available'])) {
421
 
                        $gallery->debug("Plugin $pluginId no longer installed");
 
463
                        if ($gallery->getDebug()) {
 
464
                            $gallery->debug("Plugin $pluginId no longer installed");
 
465
                        }
422
466
                        unset($plugins[$pluginId]);
423
467
                    }
424
468
                }
429
473
                    $plugins['core']['available'] = 1;
430
474
                }
431
475
 
432
 
                GalleryDataCache::putToDisk(array('type' => $pluginType,
433
 
                                                  'itemId' => 'GalleryPluginHelper_fetchPluginStatus',
434
 
                                                  'id' => '_all'),
435
 
                                            $plugins);
 
476
                GalleryDataCache::putToDisk(
 
477
                        array('type' => $pluginType,
 
478
                              'itemId' => 'GalleryPluginHelper_fetchPluginStatus',
 
479
                              'id' => '_all'),
 
480
                        $plugins);
436
481
            }
437
482
            GalleryDataCache::put($cacheKey, $plugins);
438
483
        } else {
439
484
            $plugins = GalleryDataCache::get($cacheKey);
440
485
        }
441
486
 
442
 
        return array(GalleryStatus::success(), $plugins);
 
487
        return array(null, $plugins);
443
488
    }
444
489
 
445
490
    /**
454
499
        global $gallery;
455
500
 
456
501
        if (empty($pluginType)) {
457
 
            return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
 
502
            return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__,
458
503
                                              "Missing plugin type"),
459
504
                         null);
460
505
        }
472
517
            ';
473
518
 
474
519
            list ($ret, $searchResults) = $gallery->search($query, array($pluginType));
475
 
            if ($ret->isError()) {
 
520
            if ($ret) {
476
521
                return array($ret->wrap(__FILE__, __LINE__), null);
477
522
            }
478
523
 
485
530
            $data = GalleryDataCache::get($cacheKey);
486
531
        }
487
532
 
488
 
        return array(GalleryStatus::success(), $data);
 
533
        return array(null, $data);
 
534
    }
 
535
 
 
536
    /**
 
537
     * Returns an array of directories that can contain plugins.
 
538
     *
 
539
     * This function should only be used in special circumstances,
 
540
     * for example when a list of all plugins needs to be made.
 
541
     * Currently it returns gallery2/ and gallery2/plugins/.
 
542
     */
 
543
     function getPluginBaseDirs() {
 
544
        static $data;
 
545
        if (!isset($data)) {
 
546
            global $gallery;
 
547
            $data = array();
 
548
            $data['base'] = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/';
 
549
 
 
550
            $pluginsPath = $gallery->getConfig('data.gallery.plugins');
 
551
            if (file_exists($pluginsPath)) {
 
552
                $data['g2data'] = $pluginsPath;
 
553
            }
 
554
        }
 
555
        return $data;
 
556
    }
 
557
 
 
558
    /**
 
559
     * Returns the base directory of the specified plugin.
 
560
     *
 
561
     * Modules should never assume the filesystem location of any module, not
 
562
     * even the core module. Use this function to get the base directory of a
 
563
     * module. Possible base directories are usually gallery2/ and gallery2/plugins.
 
564
     * The complete list can be read with GalleryCoreApi::getPluginBaseDirs().
 
565
     *
 
566
     * @param string 'module' or 'theme'
 
567
     * @param string the plugin ID
 
568
     * @param bool force index to be reread from the filesystem
 
569
     * @return string plugin base directory
 
570
     */
 
571
    function getPluginBaseDir($pluginType, $pluginId, $clearCache=false) {
 
572
        static $baseDirs;
 
573
        if (!isset($baseDirs[$pluginId . $pluginType]) || $clearCache) {
 
574
            global $gallery;
 
575
            $defaultPluginBasePath = dirname(__FILE__) . '/../../../../';
 
576
            $pluginDataDir = $defaultPluginBasePath . $gallery->getConfig('plugins.dirname') . '/';
 
577
 
 
578
            if (GalleryCoreApi::isPluginInDefaultLocation($pluginType, $pluginId, $clearCache)) {
 
579
                $baseDirs[$pluginId . $pluginType] = $defaultPluginBasePath;
 
580
            } else {
 
581
                $baseDirs[$pluginId . $pluginType] = $pluginDataDir;
 
582
            }
 
583
        }
 
584
        return $baseDirs[$pluginId . $pluginType];
 
585
    }
 
586
 
 
587
    /**
 
588
     * Indicates whether the specified plugin is in the default plugin directory.
 
589
     *
 
590
     * Sometimes it's useful to know just whether a plugin is in its default
 
591
     * directory or not, without actually getting any information about its path.
 
592
     * It is used for rewriting URLs in the UrlGenerator.
 
593
     *
 
594
     * @param string 'module' or 'theme'
 
595
     * @param string the plugin ID
 
596
     * @param bool force index to be reread from the filesystem
 
597
     * @return boolean
 
598
     */
 
599
    function isPluginInDefaultLocation($pluginType, $pluginId, $clearCache=false) {
 
600
        static $pluginIndex;
 
601
 
 
602
        /* Don't we need to know if the index file has changed to keep the cache updated? */
 
603
        if (!isset($pluginIndex[$pluginType]) || $clearCache) {
 
604
            global $gallery;
 
605
            $platform =& $gallery->getPlatform();
 
606
            $pluginDataDir =
 
607
                dirname(__FILE__) . '/../../../../' . $gallery->getConfig('plugins.dirname');
 
608
            $pluginIndexFile = sprintf('%s/index.%ss', $pluginDataDir, $pluginType);
 
609
            $pluginIndex[$pluginType] = array();
 
610
 
 
611
            /*
 
612
             * If the config file doesn't exist, install is in progress. In that
 
613
             * case we assume that all plugins are in their default directories.
 
614
             */
 
615
            if (!$platform->file_exists(dirname(__FILE__) . '/../../../../config.php')
 
616
                    || !$platform->file_exists($pluginIndexFile)) {
 
617
                return true;
 
618
            }
 
619
            $dataDirPluginList = $platform->file($pluginIndexFile);
 
620
 
 
621
            foreach($dataDirPluginList as $line) {
 
622
                $pluginIndex[$pluginType][rtrim($line)] = 1;
 
623
            }
 
624
        }
 
625
 
 
626
        if (isset($pluginIndex[$pluginType][$pluginId])) {
 
627
            return false;
 
628
        } else {
 
629
            return true;
 
630
        }
489
631
    }
490
632
}
491
633
?>