| [ Index ] |
|
Code source de XOOPS 2.0.17.1 |
1 <?php 2 // $Id: module.php 988 2007-08-13 07:36:31Z 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 if (strtolower(get_class($module)) != 'xoopsmodule') { 386 return false; 387 } 388 if (!$module->isDirty()) { 389 return true; 390 } 391 if (!$module->cleanVars()) { 392 return false; 393 } 394 foreach ($module->cleanVars as $k => $v) { 395 ${$k} = $v; 396 } 397 if ($module->isNew()) { 398 $mid = $this->db->genId('modules_mid_seq'); 399 $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); 400 } else { 401 $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); 402 } 403 if (!$result = $this->db->query($sql)) { 404 return false; 405 } 406 if (empty($mid)) { 407 $mid = $this->db->getInsertId(); 408 } 409 $module->assignVar('mid', $mid); 410 if (!empty($this->_cachedModule_dirname[$dirname])) { 411 unset ($this->_cachedModule_dirname[$dirname]); 412 } 413 if (!empty($this->_cachedModule_mid[$mid])) { 414 unset ($this->_cachedModule_mid[$mid]); 415 } 416 return true; 417 } 418 419 /** 420 * Delete a module from the database 421 * 422 * @param object &$module 423 * @return bool 424 **/ 425 function delete(&$module) 426 { 427 if (strtolower(get_class($module)) != 'xoopsmodule') { 428 return false; 429 } 430 $sql = sprintf("DELETE FROM %s WHERE mid = %u", $this->db->prefix('modules'), $module->getVar('mid')); 431 if ( !$result = $this->db->query($sql) ) { 432 return false; 433 } 434 // delete admin permissions assigned for this module 435 $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_admin' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid')); 436 $this->db->query($sql); 437 // delete read permissions assigned for this module 438 $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'module_read' AND gperm_itemid = %u", $this->db->prefix('group_permission'), $module->getVar('mid')); 439 $this->db->query($sql); 440 441 $sql = sprintf("SELECT block_id FROM %s WHERE module_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid')); 442 if ($result = $this->db->query($sql)) { 443 $block_id_arr = array(); 444 while ($myrow = $this->db->fetchArray($result)) 445 { 446 array_push($block_id_arr, $myrow['block_id']); 447 } 448 } 449 // loop through block_id_arr 450 if (isset($block_id_arr)) { 451 foreach ($block_id_arr as $i) { 452 $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); 453 if ($result2 = $this->db->query($sql)) { 454 if (0 < $this->db->getRowsNum($result2)) { 455 // this block has other entries, so delete the entry for this module 456 $sql = sprintf("DELETE FROM %s WHERE (module_id = %u) AND (block_id = %u)", $this->db->prefix('block_module_link'), $module->getVar('mid'), $i); 457 $this->db->query($sql); 458 } else { 459 // 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! 460 $sql = sprintf("UPDATE %s SET visible = 0 WHERE bid = %u", $this->db->prefix('newblocks'), $i); 461 $this->db->query($sql); 462 $sql = sprintf("UPDATE %s SET module_id = -1 WHERE module_id = %u", $this->db->prefix('block_module_link'), $module->getVar('mid')); 463 $this->db->query($sql); 464 } 465 } 466 } 467 } 468 469 if (!empty($this->_cachedModule_dirname[$module->getVar('dirname')])) { 470 unset ($this->_cachedModule_dirname[$module->getVar('dirname')]); 471 } 472 if (!empty($this->_cachedModule_mid[$module->getVar('mid')])) { 473 unset ($this->_cachedModule_mid[$module->getVar('mid')]); 474 } 475 return true; 476 } 477 478 /** 479 * Load some modules 480 * 481 * @param object $criteria {@link CriteriaElement} 482 * @param boolean $id_as_key Use the ID as key into the array 483 * @return array 484 **/ 485 function getObjects($criteria = null, $id_as_key = false) 486 { 487 $ret = array(); 488 $limit = $start = 0; 489 $sql = 'SELECT * FROM '.$this->db->prefix('modules'); 490 if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { 491 $sql .= ' '.$criteria->renderWhere(); 492 $sql .= ' ORDER BY weight '.$criteria->getOrder().', mid ASC'; 493 $limit = $criteria->getLimit(); 494 $start = $criteria->getStart(); 495 } 496 $result = $this->db->query($sql, $limit, $start); 497 if (!$result) { 498 return $ret; 499 } 500 while ($myrow = $this->db->fetchArray($result)) { 501 $module = new XoopsModule(); 502 $module->assignVars($myrow); 503 if (!$id_as_key) { 504 $ret[] =& $module; 505 } else { 506 $ret[$myrow['mid']] =& $module; 507 } 508 unset($module); 509 } 510 return $ret; 511 } 512 513 /** 514 * Count some modules 515 * 516 * @param object $criteria {@link CriteriaElement} 517 * @return int 518 **/ 519 function getCount($criteria = null) 520 { 521 $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('modules'); 522 if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { 523 $sql .= ' '.$criteria->renderWhere(); 524 } 525 if (!$result = $this->db->query($sql)) { 526 return 0; 527 } 528 list($count) = $this->db->fetchRow($result); 529 return $count; 530 } 531 532 /** 533 * returns an array of module names 534 * 535 * @param bool $criteria 536 * @param boolean $dirname_as_key 537 * if true, array keys will be module directory names 538 * if false, array keys will be module id 539 * @return array 540 **/ 541 function getList($criteria = null, $dirname_as_key = false) 542 { 543 $ret = array(); 544 $modules =& $this->getObjects($criteria, true); 545 foreach (array_keys($modules) as $i) { 546 if (!$dirname_as_key) { 547 $ret[$i] = $modules[$i]->getVar('name'); 548 } else { 549 $ret[$modules[$i]->getVar('dirname')] = $modules[$i]->getVar('name'); 550 } 551 } 552 return $ret; 553 } 554 } 555 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Nov 25 11:44:32 2007 | par Balluche grâce à PHPXref 0.7 |
|