source: temp/test-xoops.ec-cube.net/html/kernel/module.php @ 405

Revision 405, 20.2 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: module.php,v 1.5 2006/05/01 02:37:28 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31
32if (!defined('XOOPS_ROOT_PATH')) {
33    exit();
34}
35
36/**
37 * A Module
38 *
39 * @package     kernel
40 *
41 * @author      Kazumi Ono  <[email protected]>
42 * @copyright   (c) 2000-2003 The Xoops Project - www.xoops.org
43 */
44class XoopsModule extends XoopsObject
45{
46    /**
47     * @var string
48     */
49    var $modinfo;
50    /**
51     * @var string
52     */
53    var $adminmenu;
54
55    /**
56     * Constructor
57     */
58    function XoopsModule()
59    {
60        $this->XoopsObject();
61        $this->initVar('mid', XOBJ_DTYPE_INT, null, false);
62        $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
63        $this->initVar('version', XOBJ_DTYPE_INT, 100, false);
64        $this->initVar('last_update', XOBJ_DTYPE_INT, null, false);
65        $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
66        $this->initVar('isactive', XOBJ_DTYPE_INT, 1, false);
67        $this->initVar('dirname', XOBJ_DTYPE_OTHER, null, true);
68        $this->initVar('hasmain', XOBJ_DTYPE_INT, 0, false);
69        $this->initVar('hasadmin', XOBJ_DTYPE_INT, 0, false);
70        $this->initVar('hassearch', XOBJ_DTYPE_INT, 0, false);
71        $this->initVar('hasconfig', XOBJ_DTYPE_INT, 0, false);
72        $this->initVar('hascomments', XOBJ_DTYPE_INT, 0, false);
73        // RMV-NOTIFY
74        $this->initVar('hasnotification', XOBJ_DTYPE_INT, 0, false);
75    }
76
77    /**
78     * Load module info
79     *
80     * @param   string  $dirname    Directory Name
81     * @param   boolean $verbose
82     **/
83    function loadInfoAsVar($dirname, $verbose = true)
84    {
85        if ( !isset($this->modinfo) ) {
86            $this->loadInfo($dirname, $verbose);
87        }
88        $this->setVar('name', $this->modinfo['name'], true);
89        $this->setVar('version', round(100 * $this->modinfo['version']));
90        $this->setVar('dirname', $this->modinfo['dirname'], true);
91        $hasmain = (isset($this->modinfo['hasMain']) && $this->modinfo['hasMain'] == 1) ? 1 : 0;
92        $hasadmin = (isset($this->modinfo['hasAdmin']) && $this->modinfo['hasAdmin'] == 1) ? 1 : 0;
93        $hassearch = (isset($this->modinfo['hasSearch']) && $this->modinfo['hasSearch'] == 1) ? 1 : 0;
94        $hasconfig = ((isset($this->modinfo['config']) && is_array($this->modinfo['config'])) || !empty($this->modinfo['hasComments'])) ? 1 : 0;
95        $hascomments = (isset($this->modinfo['hasComments']) && $this->modinfo['hasComments'] == 1) ? 1 : 0;
96        // RMV-NOTIFY
97        $hasnotification = (isset($this->modinfo['hasNotification']) && $this->modinfo['hasNotification'] == 1) ? 1 : 0;
98        $this->setVar('hasmain', $hasmain);
99        $this->setVar('hasadmin', $hasadmin);
100        $this->setVar('hassearch', $hassearch);
101        $this->setVar('hasconfig', $hasconfig);
102        $this->setVar('hascomments', $hascomments);
103        // RMV-NOTIFY
104        $this->setVar('hasnotification', $hasnotification);
105    }
106
107    /**
108     * Get module info
109     *
110     * @param   string  $name
111     * @return  array|string    Array of module information.
112     *          If {@link $name} is set, returns a singel module information item as string.
113     **/
114    function &getInfo($name=null)
115    {
116        if ( !isset($this->modinfo) ) {
117            $this->loadInfo($this->getVar('dirname'));
118        }
119        if ( isset($name) ) {
120            if ( isset($this->modinfo[$name]) ) {
121                return $this->modinfo[$name];
122            }
123            $ret = false;
124            return $ret;
125        }
126        return $this->modinfo;
127    }
128
129    /**
130     * Get a link to the modules main page
131     *
132     * @return  string  FALSE on fail
133     */
134    function mainLink()
135    {
136        if ( $this->getVar('hasmain') == 1 ) {
137            $ret = '<a href="'.XOOPS_URL.'/modules/'.$this->getVar('dirname').'/">'.$this->getVar('name').'</a>';
138            return $ret;
139        }
140        return false;
141    }
142
143    /**
144     * Get links to the subpages
145     *
146     * @return  string
147     */
148    function &subLink()
149    {
150        $ret = array();
151        if ( $this->getInfo('sub') && is_array($this->getInfo('sub')) ) {
152            foreach ( $this->getInfo('sub') as $submenu ) {
153                $ret[] = array('name' => $submenu['name'], 'url' => $submenu['url']);
154            }
155        }
156        return $ret;
157    }
158
159    /**
160     * Load the admin menu for the module
161     */
162    function loadAdminMenu()
163    {
164        if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && file_exists(XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/'.$this->getInfo('adminmenu'))) {
165            include XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/'.$this->getInfo('adminmenu');
166            $this->adminmenu =& $adminmenu;
167        }
168    }
169
170    /**
171     * Get the admin menu for the module
172     *
173     * @return  string
174     */
175    function &getAdminMenu()
176    {
177        if ( !isset($this->adminmenu) ) {
178            $this->loadAdminMenu();
179        }
180        return $this->adminmenu;
181    }
182
183    /**
184     * Load the module info for this module
185     *
186     * @param   string  $dirname    Module directory
187     * @param   bool    $verbose    Give an error on fail?
188     */
189    function loadInfo($dirname, $verbose = true)
190    {
191        global $xoopsConfig;
192        if (file_exists(XOOPS_ROOT_PATH.'/modules/'.$dirname.'/language/'.$xoopsConfig['language'].'/modinfo.php')) {
193            include_once XOOPS_ROOT_PATH.'/modules/'.$dirname.'/language/'.$xoopsConfig['language'].'/modinfo.php';
194        } elseif (file_exists(XOOPS_ROOT_PATH.'/modules/'.$dirname.'/language/english/modinfo.php')) {
195            include_once XOOPS_ROOT_PATH.'/modules/'.$dirname.'/language/english/modinfo.php';
196        }
197        if (file_exists(XOOPS_ROOT_PATH.'/modules/'.$dirname.'/xoops_version.php')) {
198            include XOOPS_ROOT_PATH.'/modules/'.$dirname.'/xoops_version.php';
199        } else {
200            if (false != $verbose) {
201                echo "Module File for $dirname Not Found!";
202            }
203            return;
204        }
205        $this->modinfo =& $modversion;
206    }
207
208    /**
209     * Search contents within a module
210     *
211     * @param   string  $term
212     * @param   string  $andor  'AND' or 'OR'
213     * @param   integer $limit
214     * @param   integer $offset
215     * @param   integer $userid
216     * @return  mixed   Search result.
217     **/
218    function &search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
219    {
220        $ret = false;
221        if ($this->getVar('hassearch') != 1) {
222            return $ret;
223        }
224        $search =& $this->getInfo('search');
225        if ($this->getVar('hassearch') != 1 || !isset($search['file']) || !isset($search['func']) || $search['func'] == '' || $search['file'] == '') {
226            return $ret;
227        }
228        if (file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname').'/'.$search['file'])) {
229            include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/'.$search['file'];
230        } else {
231            return $ret;
232        }
233        if (function_exists($search['func'])) {
234            $func = $search['func'];
235            $ret = $func($term, $andor, $limit, $offset, $userid);
236        }
237        return $ret;
238    }
239
240    /**#@+
241     * For backward compatibility only!
242     * @deprecated
243     */
244    function mid()
245    {
246        return $this->getVar('mid');
247    }
248    function dirname()
249    {
250        return $this->getVar('dirname');
251    }
252    function name()
253    {
254        return $this->getVar('name');
255    }
256    function &getByDirName($dirname)
257    {
258        $modhandler =& xoops_gethandler('module');
259        $ret =& $modhandler->getByDirname($dirname);
260        return $ret;
261    }
262    /**#@-*/
263}
264
265
266/**
267 * XOOPS module handler class.
268 *
269 * This class is responsible for providing data access mechanisms to the data source
270 * of XOOPS module class objects.
271 *
272 * @package     kernel
273 *
274 * @author      Kazumi Ono  <[email protected]>
275 * @copyright   (c) 2000-2003 The Xoops Project - www.xoops.org
276 */
277class XoopsModuleHandler extends XoopsObjectHandler
278{
279    /**
280     * holds an array of cached module references, indexed by module id
281     *
282     * @var    array
283     * @access private
284     */
285    var $_cachedModule_mid = array();
286
287    /**
288     * holds an array of cached module references, indexed by module dirname
289     *
290     * @var    array
291     * @access private
292     */
293    var $_cachedModule_dirname = array();
294
295    /**
296     * Create a new {@link XoopsModule} object
297     *
298     * @param   boolean     $isNew   Flag the new object as "new"
299     * @return  object
300     **/
301    function &create($isNew = true)
302    {
303        $module =& new XoopsModule();
304        if ($isNew) {
305            $module->setNew();
306        }
307        return $module;
308    }
309
310    /**
311     * Load a module from the database
312     *
313     * @param   int     $id     ID of the module
314     *
315     * @return  object  FALSE on fail
316     */
317    function &get($id)
318    {
319        static $_cachedModule_dirname;
320        static $_cachedModule_mid;
321        $ret = false;
322        $id = intval($id);
323        if ($id > 0) {
324            if (!empty($_cachedModule_mid[$id])) {
325                return $_cachedModule_mid[$id];
326            } else {
327                $sql = 'SELECT * FROM '.$this->db->prefix('modules').' WHERE mid = '.$id;
328                if ($result = $this->db->query($sql)) {
329                    $numrows = $this->db->getRowsNum($result);
330                    if ($numrows == 1) {
331                        $module =& new XoopsModule();
332                        $myrow = $this->db->fetchArray($result);
333                        $module->assignVars($myrow);
334                        $_cachedModule_mid[$id] =& $module;
335                        $_cachedModule_dirname[$module->getVar('dirname')] =& $module;
336                        $ret =& $module;
337                    }
338                }
339            }
340        }
341        return $ret;
342    }
343
344    /**
345     * Load a module by its dirname
346     *
347     * @param   string  $dirname
348     *
349     * @return  object  FALSE on fail
350     */
351    function &getByDirname($dirname)
352    {
353        static $_cachedModule_mid;
354        static $_cachedModule_dirname;
355        $ret = false;
356        if (!empty($_cachedModule_dirname[$dirname])) {
357            $ret = $_cachedModule_dirname[$dirname];
358        } else {
359            $sql = "SELECT * FROM ".$this->db->prefix('modules')." WHERE dirname = '".trim($dirname)."'";
360            if ($result = $this->db->query($sql)) {
361                $numrows = $this->db->getRowsNum($result);
362                if ($numrows == 1) {
363                    $module =& new XoopsModule();
364                    $myrow = $this->db->fetchArray($result);
365                    $module->assignVars($myrow);
366                    $_cachedModule_dirname[$dirname] =& $module;
367                    $_cachedModule_mid[$module->getVar('mid')] =& $module;
368                    $ret =& $module;
369                }
370            }
371        }
372        return $ret;
373    }
374
375    /**
376     * Write a module to the database
377     *
378     * @param   object  &$module reference to a {@link XoopsModule}
379     * @return  bool
380     **/
381    function insert(&$module)
382    {
383        if (strtolower(get_class($module)) != 'xoopsmodule') {
384            return false;
385        }
386        if (!$module->isDirty()) {
387            return true;
388        }
389        if (!$module->cleanVars()) {
390            return false;
391        }
392        foreach ($module->cleanVars as $k => $v) {
393            ${$k} = $v;
394        }
395        if ($module->isNew()) {
396            $mid = $this->db->genId('modules_mid_seq');
397            $sql = sprintf("INSERT INTO %s (mid, name, version, last_update, weight, isactive, dirname, hasmain, hasadmin, hassearch, hasconfig, hascomments, hasnotification) VALUES (%u, %s, %u, %u, %u, %u, %s, %u, %u, %u, %u, %u, %u)", $this->db->prefix('modules'), $mid, $this->db->quoteString($name), $version, time(), $weight, 1, $this->db->quoteString($dirname), $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification);
398        } else {
399            $sql = sprintf("UPDATE %s SET name = %s, dirname = %s, version = %u, last_update = %u, weight = %u, isactive = %u, hasmain = %u, hasadmin = %u, hassearch = %u, hasconfig = %u, hascomments = %u, hasnotification = %u WHERE mid = %u", $this->db->prefix('modules'), $this->db->quoteString($name), $this->db->quoteString($dirname), $version, time(), $weight, $isactive, $hasmain, $hasadmin, $hassearch, $hasconfig, $hascomments, $hasnotification, $mid);
400        }
401        if (!$result = $this->db->query($sql)) {
402            return false;
403        }
404        if (empty($mid)) {
405            $mid = $this->db->getInsertId();
406        }
407        $module->assignVar('mid', $mid);
408        if (!empty($this->_cachedModule_dirname[$dirname])) {
409            unset ($this->_cachedModule_dirname[$dirname]);
410        }
411        if (!empty($this->_cachedModule_mid[$mid])) {
412            unset ($this->_cachedModule_mid[$mid]);
413        }
414        return true;
415    }
416
417    /**
418     * Delete a module from the database
419     *
420     * @param   object  &$module
421     * @return  bool
422     **/
423    function delete(&$module)
424    {
425        if (strtolower(get_class($module)) != 'xoopsmodule') {
426            return false;
427        }
428        $sql = sprintf("DELETE FROM %s WHERE mid = %u", $this->db->prefix('modules'), $module->getVar('mid'));
429        if ( !$result = $this->db->query($sql) ) {
430            return false;
431        }
432        // delete admin permissions assigned for this module
433        $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_admin' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
434        $this->db->query($sql);
435        // delete read permissions assigned for this module
436        $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_read' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
437        $this->db->query($sql);
438
439        $sql = sprintf("SELECT block_id FROM %s WHERE module_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid'));
440        if ($result = $this->db->query($sql)) {
441            $block_id_arr = array();
442            while ($myrow = $this->db->fetchArray($result))
443{
444                array_push($block_id_arr, $myrow['block_id']);
445            }
446        }
447        // loop through block_id_arr
448        if (isset($block_id_arr)) {
449            foreach ($block_id_arr as $i) {
450                $sql = sprintf("SELECT block_id FROM %s WHERE module_id != %u AND block_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
451                if ($result2 = $this->db->query($sql)) {
452                    if (0 < $this->db->getRowsNum($result2)) {
453                    // this block has other entries, so delete the entry for this module
454                        $sql = sprintf("DELETE FROM %s WHERE (module_id = %u) AND (block_id = %u)", $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
455                        $this->db->query($sql);
456                    } else {
457                    // this block doesnt have other entries, so disable the block and let it show on top page only. otherwise, this block will not display anymore on block admin page!
458                        $sql = sprintf("UPDATE %s SET visible = 0 WHERE bid = %u", $this->db->prefix('newblocks'), $i);
459                        $this->db->query($sql);
460                        $sql = sprintf("UPDATE %s SET module_id = -1 WHERE module_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid'));
461                        $this->db->query($sql);
462                    }
463                }
464            }
465        }
466
467        if (!empty($this->_cachedModule_dirname[$module->getVar('dirname')])) {
468            unset ($this->_cachedModule_dirname[$module->getVar('dirname')]);
469        }
470        if (!empty($this->_cachedModule_mid[$module->getVar('mid')])) {
471            unset ($this->_cachedModule_mid[$module->getVar('mid')]);
472        }
473        return true;
474    }
475
476    /**
477     * Load some modules
478     *
479     * @param   object  $criteria   {@link CriteriaElement}
480     * @param   boolean $id_as_key  Use the ID as key into the array
481     * @return  array
482     **/
483    function &getObjects($criteria = null, $id_as_key = false)
484    {
485        $ret = array();
486        $limit = $start = 0;
487        $sql = 'SELECT * FROM '.$this->db->prefix('modules');
488        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
489            $sql .= ' '.$criteria->renderWhere();
490            $sql .= ' ORDER BY weight '.$criteria->getOrder().', mid ASC';
491            $limit = $criteria->getLimit();
492            $start = $criteria->getStart();
493        }
494        $result = $this->db->query($sql, $limit, $start);
495        if (!$result) {
496            return $ret;
497        }
498        while ($myrow = $this->db->fetchArray($result)) {
499            $module =& new XoopsModule();
500            $module->assignVars($myrow);
501            if (!$id_as_key) {
502                $ret[] =& $module;
503            } else {
504                $ret[$myrow['mid']] =& $module;
505            }
506            unset($module);
507        }
508        return $ret;
509    }
510
511    /**
512     * Count some modules
513     *
514     * @param   object  $criteria   {@link CriteriaElement}
515     * @return  int
516     **/
517    function getCount($criteria = null)
518    {
519        $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('modules');
520        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
521            $sql .= ' '.$criteria->renderWhere();
522        }
523        if (!$result =& $this->db->query($sql)) {
524            return 0;
525        }
526        list($count) = $this->db->fetchRow($result);
527        return $count;
528    }
529
530    /**
531     * returns an array of module names
532     *
533     * @param   bool    $criteria
534     * @param   boolean $dirname_as_key
535     *      if true, array keys will be module directory names
536     *      if false, array keys will be module id
537     * @return  array
538     **/
539    function &getList($criteria = null, $dirname_as_key = false)
540    {
541        $ret = array();
542        $modules =& $this->getObjects($criteria, true);
543        foreach (array_keys($modules) as $i) {
544            if (!$dirname_as_key) {
545                $ret[$i] =& $modules[$i]->getVar('name');
546            } else {
547                $ret[$modules[$i]->getVar('dirname')] =& $modules[$i]->getVar('name');
548            }
549        }
550        return $ret;
551    }
552}
553?>
Note: See TracBrowser for help on using the repository browser.