home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / kernel / module.php < prev    next >
Encoding:
PHP Script  |  2008-02-11  |  20.4 KB  |  561 lines

  1. <?php
  2. // $Id: module.php 1290 2008-02-10 13:09:25Z phppp $
  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.  
  32. if (!defined('XOOPS_ROOT_PATH')) {
  33.     exit();
  34. }
  35.  
  36. /**
  37.  * A Module
  38.  *
  39.  * @package        kernel
  40.  *
  41.  * @author        Kazumi Ono     <onokazu@xoops.org>
  42.  * @copyright    (c) 2000-2003 The Xoops Project - www.xoops.org
  43.  */
  44. class 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', intval(100 * ($this->modinfo['version']+0.001)), true);
  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.             $return = false;
  124.             return $return;
  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_once 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 false;
  204.            }
  205.         $this->modinfo =& $modversion;
  206.  
  207.         return true;
  208.     }
  209.  
  210.     /**
  211.      * Search contents within a module
  212.      *
  213.      * @param   string  $term
  214.      * @param   string  $andor  'AND' or 'OR'
  215.      * @param   integer $limit
  216.      * @param   integer $offset
  217.      * @param   integer $userid
  218.      * @return  mixed   Search result.
  219.      **/
  220.     function search($term = '', $andor = 'AND', $limit = 0, $offset = 0, $userid = 0)
  221.     {
  222.         if ($this->getVar('hassearch') != 1) {
  223.             return false;
  224.         }
  225.         $search =& $this->getInfo('search');
  226.         if ($this->getVar('hassearch') != 1 || !isset($search['file']) || !isset($search['func']) || $search['func'] == '' || $search['file'] == '') {
  227.             return false;
  228.         }
  229.         if (file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname').'/'.$search['file'])) {
  230.             include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/'.$search['file'];
  231.         } else {
  232.             return false;
  233.         }
  234.         if (function_exists($search['func'])) {
  235.             $func = $search['func'];
  236.             return $func($term, $andor, $limit, $offset, $userid);
  237.         }
  238.         return false;
  239.     }
  240.  
  241.     /**#@+
  242.      * For backward compatibility only!
  243.      * @deprecated
  244.      */
  245.     function mid()
  246.     {
  247.         return $this->getVar('mid');
  248.     }
  249.     function dirname()
  250.     {
  251.         return $this->getVar('dirname');
  252.     }
  253.     function name()
  254.     {
  255.         return $this->getVar('name');
  256.     }
  257.     function &getByDirName($dirname)
  258.     {
  259.         $modhandler =& xoops_gethandler('module');
  260.         $inst =& $modhandler->getByDirname($dirname);
  261.         return $inst;
  262.     }
  263.     /**#@-*/
  264. }
  265.  
  266.  
  267. /**
  268.  * XOOPS module handler class.
  269.  *
  270.  * This class is responsible for providing data access mechanisms to the data source
  271.  * of XOOPS module class objects.
  272.  *
  273.  * @package        kernel
  274.  *
  275.  * @author        Kazumi Ono     <onokazu@xoops.org>
  276.  * @copyright    (c) 2000-2003 The Xoops Project - www.xoops.org
  277.  */
  278. class XoopsModuleHandler extends XoopsObjectHandler
  279. {
  280.     /**
  281.      * holds an array of cached module references, indexed by module id
  282.      *
  283.      * @var    array
  284.      * @access private
  285.      */
  286.     var $_cachedModule_mid = array();
  287.  
  288.     /**
  289.      * holds an array of cached module references, indexed by module dirname
  290.      *
  291.      * @var    array
  292.      * @access private
  293.      */
  294.     var $_cachedModule_dirname = array();
  295.  
  296.     /**
  297.      * Create a new {@link XoopsModule} object
  298.      *
  299.      * @param   boolean     $isNew   Flag the new object as "new"
  300.      * @return  object
  301.      **/
  302.     function &create($isNew = true)
  303.     {
  304.         $module = new XoopsModule();
  305.         if ($isNew) {
  306.             $module->setNew();
  307.         }
  308.         return $module;
  309.     }
  310.  
  311.     /**
  312.      * Load a module from the database
  313.      *
  314.      * @param    int     $id     ID of the module
  315.      *
  316.      * @return    object  FALSE on fail
  317.      */
  318.     function &get($id)
  319.     {
  320.         static $_cachedModule_dirname;
  321.         static $_cachedModule_mid;
  322.         $id = intval($id);
  323.         $module = false;
  324.         if ($id > 0) {
  325.             if (!empty($_cachedModule_mid[$id])) {
  326.                 return $_cachedModule_mid[$id];
  327.             } else {
  328.                   $sql = 'SELECT * FROM '.$this->db->prefix('modules').' WHERE mid = '.$id;
  329.                   if (!$result = $this->db->query($sql)) {
  330.                       return $module;
  331.                   }
  332.                   $numrows = $this->db->getRowsNum($result);
  333.                   if ($numrows == 1) {
  334.                       $module = new XoopsModule();
  335.                       $myrow = $this->db->fetchArray($result);
  336.                       $module->assignVars($myrow);
  337.                     $_cachedModule_mid[$id] =& $module;
  338.                     $_cachedModule_dirname[$module->getVar('dirname')] =& $module;
  339.                       return $module;
  340.                   }
  341.             }
  342.         }
  343.         return $module;
  344.     }
  345.  
  346.     /**
  347.      * Load a module by its dirname
  348.      *
  349.      * @param    string  $dirname
  350.      *
  351.      * @return    object  FALSE on fail
  352.      */
  353.     function &getByDirname($dirname)
  354.     {
  355.         static $_cachedModule_mid;
  356.         static $_cachedModule_dirname;
  357.         if (!empty($_cachedModule_dirname[$dirname])) {
  358.             return $_cachedModule_dirname[$dirname];
  359.         } else {
  360.             $module = false;
  361.             $sql = "SELECT * FROM ".$this->db->prefix('modules')." WHERE dirname = '".trim($dirname)."'";
  362.             if (!$result = $this->db->query($sql)) {
  363.                 return $module;
  364.             }
  365.             $numrows = $this->db->getRowsNum($result);
  366.             if ($numrows == 1) {
  367.                 $module = new XoopsModule();
  368.                 $myrow = $this->db->fetchArray($result);
  369.                 $module->assignVars($myrow);
  370.                 $_cachedModule_dirname[$dirname] =& $module;
  371.                 $_cachedModule_mid[$module->getVar('mid')] =& $module;
  372.             }
  373.             return $module;
  374.         }
  375.     }
  376.  
  377.     /**
  378.      * Write a module to the database
  379.      *
  380.      * @param   object  &$module reference to a {@link XoopsModule}
  381.      * @return  bool
  382.      **/
  383.     function insert(&$module)
  384.     {
  385.         /**
  386.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  387.         */
  388.         if (!is_a($module, 'xoopsmodule')) {
  389.             return false;
  390.         }
  391.         if (!$module->isDirty()) {
  392.             return true;
  393.         }
  394.         if (!$module->cleanVars()) {
  395.             return false;
  396.         }
  397.         foreach ($module->cleanVars as $k => $v) {
  398.             ${$k} = $v;
  399.         }
  400.         if ($module->isNew()) {
  401.             $mid = $this->db->genId('modules_mid_seq');
  402.             $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);
  403.         } else {
  404.             $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);
  405.         }
  406.         if (!$result = $this->db->query($sql)) {
  407.             return false;
  408.         }
  409.         if (empty($mid)) {
  410.             $mid = $this->db->getInsertId();
  411.         }
  412.         $module->assignVar('mid', $mid);
  413.         if (!empty($this->_cachedModule_dirname[$dirname])) {
  414.             unset ($this->_cachedModule_dirname[$dirname]);
  415.         }
  416.         if (!empty($this->_cachedModule_mid[$mid])) {
  417.             unset ($this->_cachedModule_mid[$mid]);
  418.         }
  419.         return true;
  420.     }
  421.  
  422.     /**
  423.      * Delete a module from the database
  424.      *
  425.      * @param   object  &$module
  426.      * @return  bool
  427.      **/
  428.     function delete(&$module)
  429.     {
  430.         /**
  431.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  432.         */
  433.         if (!is_a($module, 'xoopsmodule')) {
  434.             return false;
  435.         }
  436.         $sql = sprintf("DELETE FROM %s WHERE mid = %u", $this->db->prefix('modules'), $module->getVar('mid'));
  437.         if ( !$result = $this->db->query($sql) ) {
  438.             return false;
  439.         }
  440.         // delete admin permissions assigned for this module
  441.         $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_admin' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
  442.         $this->db->query($sql);
  443.         // delete read permissions assigned for this module
  444.         $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_read' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid'));
  445.         $this->db->query($sql);
  446.  
  447.         $sql = sprintf("SELECT block_id FROM %s WHERE module_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid'));
  448.         if ($result = $this->db->query($sql)) {
  449.             $block_id_arr = array();
  450.             while ($myrow = $this->db->fetchArray($result))
  451. {
  452.                 array_push($block_id_arr, $myrow['block_id']);
  453.             }
  454.         }
  455.         // loop through block_id_arr
  456.         if (isset($block_id_arr)) {
  457.             foreach ($block_id_arr as $i) {
  458.                 $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);
  459.                 if ($result2 = $this->db->query($sql)) {
  460.                     if (0 < $this->db->getRowsNum($result2)) {
  461.                     // this block has other entries, so delete the entry for this module
  462.                         $sql = sprintf("DELETE FROM %s WHERE (module_id = %u) AND (block_id = %u)", $this->db->prefix('block_module_link'), $module->getVar('mid'), $i);
  463.                         $this->db->query($sql);
  464.                     } else {
  465.                     // 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!
  466.                         $sql = sprintf("UPDATE %s SET visible = 0 WHERE bid = %u", $this->db->prefix('newblocks'), $i);
  467.                         $this->db->query($sql);
  468.                         $sql = sprintf("UPDATE %s SET module_id = -1 WHERE module_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid'));
  469.                         $this->db->query($sql);
  470.                     }
  471.                 }
  472.             }
  473.         }
  474.  
  475.         if (!empty($this->_cachedModule_dirname[$module->getVar('dirname')])) {
  476.             unset ($this->_cachedModule_dirname[$module->getVar('dirname')]);
  477.         }
  478.         if (!empty($this->_cachedModule_mid[$module->getVar('mid')])) {
  479.             unset ($this->_cachedModule_mid[$module->getVar('mid')]);
  480.         }
  481.         return true;
  482.     }
  483.  
  484.     /**
  485.      * Load some modules
  486.      *
  487.      * @param   object  $criteria   {@link CriteriaElement}
  488.      * @param   boolean $id_as_key  Use the ID as key into the array
  489.      * @return  array
  490.      **/
  491.     function getObjects($criteria = null, $id_as_key = false)
  492.     {
  493.         $ret = array();
  494.         $limit = $start = 0;
  495.         $sql = 'SELECT * FROM '.$this->db->prefix('modules');
  496.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  497.             $sql .= ' '.$criteria->renderWhere();
  498.             $sql .= ' ORDER BY weight '.$criteria->getOrder().', mid ASC';
  499.             $limit = $criteria->getLimit();
  500.             $start = $criteria->getStart();
  501.         }
  502.         $result = $this->db->query($sql, $limit, $start);
  503.         if (!$result) {
  504.             return $ret;
  505.         }
  506.         while ($myrow = $this->db->fetchArray($result)) {
  507.             $module = new XoopsModule();
  508.             $module->assignVars($myrow);
  509.             if (!$id_as_key) {
  510.                 $ret[] =& $module;
  511.             } else {
  512.                 $ret[$myrow['mid']] =& $module;
  513.             }
  514.             unset($module);
  515.         }
  516.         return $ret;
  517.     }
  518.  
  519.     /**
  520.      * Count some modules
  521.      *
  522.      * @param   object  $criteria   {@link CriteriaElement}
  523.      * @return  int
  524.      **/
  525.     function getCount($criteria = null)
  526.     {
  527.         $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('modules');
  528.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  529.             $sql .= ' '.$criteria->renderWhere();
  530.         }
  531.         if (!$result = $this->db->query($sql)) {
  532.             return 0;
  533.         }
  534.         list($count) = $this->db->fetchRow($result);
  535.         return $count;
  536.     }
  537.  
  538.     /**
  539.      * returns an array of module names
  540.      *
  541.      * @param   bool    $criteria
  542.      * @param   boolean $dirname_as_key
  543.      *      if true, array keys will be module directory names
  544.      *      if false, array keys will be module id
  545.      * @return  array
  546.      **/
  547.     function getList($criteria = null, $dirname_as_key = false)
  548.     {
  549.         $ret = array();
  550.         $modules = $this->getObjects($criteria, true);
  551.         foreach (array_keys($modules) as $i) {
  552.             if (!$dirname_as_key) {
  553.                 $ret[$i] = $modules[$i]->getVar('name');
  554.             } else {
  555.                 $ret[$modules[$i]->getVar('dirname')] = $modules[$i]->getVar('name');
  556.             }
  557.         }
  558.         return $ret;
  559.     }
  560. }
  561. ?>