[ Index ] |
|
Code source de XOOPS 2.0.17.1 |
1 <?php 2 // $Id: xoopsblock.php 922 2007-08-04 07:06:50Z 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 require_once XOOPS_ROOT_PATH."/kernel/object.php"; 36 37 class XoopsBlock extends XoopsObject 38 { 39 var $db; 40 41 function XoopsBlock($id = null) 42 { 43 $this->db =& Database::getInstance(); 44 $this->initVar('bid', XOBJ_DTYPE_INT, null, false); 45 $this->initVar('mid', XOBJ_DTYPE_INT, 0, false); 46 $this->initVar('func_num', XOBJ_DTYPE_INT, 0, false); 47 $this->initVar('options', XOBJ_DTYPE_TXTBOX, null, false, 255); 48 $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150); 49 //$this->initVar('position', XOBJ_DTYPE_INT, 0, false); 50 $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 150); 51 $this->initVar('content', XOBJ_DTYPE_TXTAREA, null, false); 52 $this->initVar('side', XOBJ_DTYPE_INT, 0, false); 53 $this->initVar('weight', XOBJ_DTYPE_INT, 0, false); 54 $this->initVar('visible', XOBJ_DTYPE_INT, 0, false); 55 $this->initVar('block_type', XOBJ_DTYPE_OTHER, null, false); 56 $this->initVar('c_type', XOBJ_DTYPE_OTHER, null, false); 57 $this->initVar('isactive', XOBJ_DTYPE_INT, null, false); 58 59 $this->initVar('dirname', XOBJ_DTYPE_TXTBOX, null, false, 50); 60 $this->initVar('func_file', XOBJ_DTYPE_TXTBOX, null, false, 50); 61 $this->initVar('show_func', XOBJ_DTYPE_TXTBOX, null, false, 50); 62 $this->initVar('edit_func', XOBJ_DTYPE_TXTBOX, null, false, 50); 63 64 $this->initVar('template', XOBJ_DTYPE_OTHER, null, false); 65 $this->initVar('bcachetime', XOBJ_DTYPE_INT, 0, false); 66 $this->initVar('last_modified', XOBJ_DTYPE_INT, 0, false); 67 68 if ( !empty($id) ) { 69 if ( is_array($id) ) { 70 $this->assignVars($id); 71 } else { 72 $this->load(intval($id)); 73 } 74 } 75 } 76 77 function load($id) 78 { 79 $sql = 'SELECT * FROM '.$this->db->prefix('newblocks').' WHERE bid = '.$id; 80 $arr = $this->db->fetchArray($this->db->query($sql)); 81 $this->assignVars($arr); 82 } 83 84 function store() 85 { 86 if ( !$this->cleanVars() ) { 87 return false; 88 } 89 foreach ( $this->cleanVars as $k=>$v ) { 90 ${$k} = $v; 91 } 92 if ( empty($bid) ) { 93 $bid = $this->db->genId($this->db->prefix("newblocks")."_bid_seq"); 94 $sql = sprintf("INSERT INTO %s (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type, c_type, isactive, dirname, func_file, show_func, edit_func, template, bcachetime, last_modified) VALUES (%u, %u, %u, %s, %s, %s, %s, %u, %u, %u, %s, %s, %u, %s, %s, %s, %s, %s, %u, %u)", $this->db->prefix('newblocks'), $bid, $mid, $func_num, $this->db->quoteString($options), $this->db->quoteString($name), $this->db->quoteString($title), $this->db->quoteString($content), $side, $weight, $visible, $this->db->quoteString($block_type), $this->db->quoteString($c_type), 1, $this->db->quoteString($dirname), $this->db->quoteString($func_file), $this->db->quoteString($show_func), $this->db->quoteString($edit_func), $this->db->quoteString($template), $bcachetime, time()); 95 } else { 96 $sql = "UPDATE ".$this->db->prefix("newblocks")." SET options=".$this->db->quoteString($options); 97 // a custom block needs its own name 98 if ( $block_type == "C" ) { 99 $sql .= ", name=".$this->db->quoteString($name); 100 } 101 $sql .= ", isactive=".$isactive.", title=".$this->db->quoteString($title).", content=".$this->db->quoteString($content).", side=".$side.", weight=".$weight.", visible=".$visible.", c_type=".$this->db->quoteString($c_type).", template=".$this->db->quoteString($template).", bcachetime=".$bcachetime.", last_modified=".time()." WHERE bid=".$bid; 102 } 103 if ( !$this->db->query($sql) ) { 104 $this->setErrors("Could not save block data into database"); 105 return false; 106 } 107 if ( empty($bid) ) { 108 $bid = $this->db->getInsertId(); 109 } 110 return $bid; 111 } 112 113 function delete() 114 { 115 $sql = sprintf("DELETE FROM %s WHERE bid = %u", $this->db->prefix('newblocks'), $this->getVar('bid')); 116 if ( !$this->db->query($sql) ) { 117 return false; 118 } 119 $sql = sprintf("DELETE FROM %s WHERE gperm_name = 'block_read' AND gperm_itemid = %u AND gperm_modid = 1", $this->db->prefix('group_permission'), $this->getVar('bid')); 120 $this->db->query($sql); 121 $sql = sprintf("DELETE FROM %s WHERE block_id = %u", $this->db->prefix('block_module_link'), $this->getVar('bid')); 122 $this->db->query($sql); 123 return true; 124 } 125 126 /** 127 * do stripslashes/htmlspecialchars according to the needed output 128 * 129 * @param $format output use: S for Show and E for Edit 130 * @param $c_type type of block content 131 * @returns string 132 */ 133 function getContent($format = 'S', $c_type = 'T') 134 { 135 switch ( $format ) { 136 case 'S': 137 // check the type of content 138 // H : custom HTML block 139 // P : custom PHP block 140 // S : use text sanitizater (smilies enabled) 141 // T : use text sanitizater (smilies disabled) 142 if ( $c_type == 'H' ) { 143 return str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N')); 144 } elseif ( $c_type == 'P' ) { 145 ob_start(); 146 echo eval($this->getVar('content', 'N')); 147 $content = ob_get_contents(); 148 ob_end_clean(); 149 return str_replace('{X_SITEURL}', XOOPS_URL.'/', $content); 150 } elseif ( $c_type == 'S' ) { 151 $myts =& MyTextSanitizer::getInstance(); 152 $content = str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N')); 153 return $myts->displayTarea($content, 1, 1); 154 } else { 155 $myts =& MyTextSanitizer::getInstance(); 156 $content = str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N')); 157 return $myts->displayTarea($content, 1, 0); 158 } 159 break; 160 case 'E': 161 return $this->getVar('content', 'E'); 162 break; 163 default: 164 return $this->getVar('content', 'N'); 165 break; 166 } 167 } 168 169 function buildBlock() 170 { 171 global $xoopsConfig, $xoopsOption; 172 $block = array(); 173 // M for module block, S for system block C for Custom 174 if ( $this->getVar("block_type") != "C" ) { 175 // get block display function 176 $show_func = $this->getVar('show_func'); 177 if ( !$show_func ) { 178 return false; 179 } 180 // must get lang files b4 execution of the function 181 if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('func_file')) ) { 182 if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php") ) { 183 include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php"; 184 } elseif ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php") ) { 185 include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php"; 186 } 187 include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('func_file'); 188 $options = explode("|", $this->getVar("options")); 189 if ( function_exists($show_func) ) { 190 // execute the function 191 $block = $show_func($options); 192 if ( !$block ) { 193 return false; 194 } 195 } else { 196 return false; 197 } 198 } else { 199 return false; 200 } 201 } else { 202 // it is a custom block, so just return the contents 203 $block['content'] = $this->getContent("S",$this->getVar("c_type")); 204 if (empty($block['content'])) { 205 return false; 206 } 207 } 208 return $block; 209 } 210 211 /* 212 * Aligns the content of a block 213 * If position is 0, content in DB is positioned 214 * before the original content 215 * If position is 1, content in DB is positioned 216 * after the original content 217 */ 218 function buildContent($position,$content="",$contentdb="") 219 { 220 if ( $position == 0 ) { 221 $ret = $contentdb.$content; 222 } elseif ( $position == 1 ) { 223 $ret = $content.$contentdb; 224 } 225 return $ret; 226 } 227 228 function buildTitle($originaltitle, $newtitle="") 229 { 230 if ($newtitle != "") { 231 $ret = $newtitle; 232 } else { 233 $ret = $originaltitle; 234 } 235 return $ret; 236 } 237 238 function isCustom() 239 { 240 if ( $this->getVar("block_type") == "C" ) { 241 return true; 242 } 243 return false; 244 } 245 246 /** 247 * gets html form for editting block options 248 * 249 */ 250 function getOptions() 251 { 252 global $xoopsConfig; 253 if ( $this->getVar("block_type") != "C" ) { 254 $edit_func = $this->getVar('edit_func'); 255 if ( !$edit_func ) { 256 return false; 257 } 258 if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/blocks/".$this->getVar('func_file')) ) { 259 if ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php") ) { 260 include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/".$xoopsConfig['language']."/blocks.php"; 261 } elseif ( file_exists(XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php") ) { 262 include_once XOOPS_ROOT_PATH."/modules/".$this->getVar('dirname')."/language/english/blocks.php"; 263 } 264 include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/blocks/'.$this->getVar('func_file'); 265 $options = explode("|", $this->getVar("options")); 266 $edit_form = $edit_func($options); 267 if ( !$edit_form ) { 268 return false; 269 } 270 return $edit_form; 271 } else { 272 return false; 273 } 274 } else { 275 return false; 276 } 277 } 278 279 /** 280 * get all the blocks that match the supplied parameters 281 * @param $side 0: sideblock - left 282 * 1: sideblock - right 283 * 2: sideblock - left and right 284 * 3: centerblock - left 285 * 4: centerblock - right 286 * 5: centerblock - center 287 * 6: centerblock - left, right, center 288 * @param $groupid groupid (can be an array) 289 * @param $visible 0: not visible 1: visible 290 * @param $orderby order of the blocks 291 * @returns array of block objects 292 */ 293 function getAllBlocksByGroup($groupid, $asobject=true, $side=null, $visible=null, $orderby="b.weight,b.bid", $isactive=1) 294 { 295 $db =& Database::getInstance(); 296 $ret = array(); 297 if ( !$asobject ) { 298 $sql = "SELECT b.bid "; 299 } else { 300 $sql = "SELECT b.* "; 301 } 302 $sql .= "FROM ".$db->prefix("newblocks")." b LEFT JOIN ".$db->prefix("group_permission")." l ON l.gperm_itemid=b.bid WHERE gperm_name = 'block_read' AND gperm_modid = 1"; 303 if ( is_array($groupid) ) { 304 $sql .= " AND (l.gperm_groupid=".$groupid[0].""; 305 $size = count($groupid); 306 if ( $size > 1 ) { 307 for ( $i = 1; $i < $size; $i++ ) { 308 $sql .= " OR l.gperm_groupid=".$groupid[$i].""; 309 } 310 } 311 $sql .= ")"; 312 } else { 313 $sql .= " AND l.gperm_groupid=".$groupid.""; 314 } 315 $sql .= " AND b.isactive=".$isactive; 316 if ( isset($side) ) { 317 // get both sides in sidebox? (some themes need this) 318 if ( $side == XOOPS_SIDEBLOCK_BOTH ) { 319 $side = "(b.side=0 OR b.side=1)"; 320 } elseif ( $side == XOOPS_CENTERBLOCK_ALL ) { 321 $side = "(b.side=3 OR b.side=4 OR b.side=5 OR b.side=7 OR b.side=8 OR b.side=9 )"; 322 } else { 323 $side = "b.side=".$side; 324 } 325 $sql .= " AND ".$side; 326 } 327 if ( isset($visible) ) { 328 $sql .= " AND b.visible=$visible"; 329 } 330 $sql .= " ORDER BY $orderby"; 331 $result = $db->query($sql); 332 $added = array(); 333 while ( $myrow = $db->fetchArray($result) ) { 334 if ( !in_array($myrow['bid'], $added) ) { 335 if (!$asobject) { 336 $ret[] = $myrow['bid']; 337 } else { 338 $ret[] = new XoopsBlock($myrow); 339 } 340 array_push($added, $myrow['bid']); 341 } 342 } 343 //echo $sql; 344 return $ret; 345 } 346 347 function getAllBlocks($rettype="object", $side=null, $visible=null, $orderby="side,weight,bid", $isactive=1) 348 { 349 $db =& Database::getInstance(); 350 $ret = array(); 351 $where_query = " WHERE isactive=".$isactive; 352 if ( isset($side) ) { 353 // get both sides in sidebox? (some themes need this) 354 if ( $side == 2 ) { 355 $side = "(side=0 OR side=1)"; 356 } elseif ( $side == 6 ) { 357 $side = "(side=3 OR side=4 OR side=5 OR side=7 OR side=8 OR side=9)"; 358 } else { 359 $side = "side=".$side; 360 } 361 $where_query .= " AND ".$side; 362 } 363 if ( isset($visible) ) { 364 $where_query .= " AND visible=$visible"; 365 } 366 $where_query .= " ORDER BY $orderby"; 367 switch ($rettype) { 368 case "object": 369 $sql = "SELECT * FROM ".$db->prefix("newblocks")."".$where_query; 370 $result = $db->query($sql); 371 while ( $myrow = $db->fetchArray($result) ) { 372 $ret[] = new XoopsBlock($myrow); 373 } 374 break; 375 case "list": 376 $sql = "SELECT * FROM ".$db->prefix("newblocks")."".$where_query; 377 $result = $db->query($sql); 378 while ( $myrow = $db->fetchArray($result) ) { 379 $block = new XoopsBlock($myrow); 380 $name = ($block->getVar("block_type") != "C") ? $block->getVar("name") : $block->getVar("title"); 381 $ret[$block->getVar("bid")] = $name; 382 } 383 break; 384 case "id": 385 $sql = "SELECT bid FROM ".$db->prefix("newblocks")."".$where_query; 386 $result = $db->query($sql); 387 while ( $myrow = $db->fetchArray($result) ) { 388 $ret[] = $myrow['bid']; 389 } 390 break; 391 } 392 //echo $sql; 393 return $ret; 394 } 395 396 function getByModule($moduleid, $asobject=true) 397 { 398 $db =& Database::getInstance(); 399 if ( $asobject == true ) { 400 $sql = $sql = "SELECT * FROM ".$db->prefix("newblocks")." WHERE mid=".$moduleid.""; 401 } else { 402 $sql = "SELECT bid FROM ".$db->prefix("newblocks")." WHERE mid=".$moduleid.""; 403 } 404 $result = $db->query($sql); 405 $ret = array(); 406 while( $myrow = $db->fetchArray($result) ) { 407 if ( $asobject ) { 408 $ret[] = new XoopsBlock($myrow); 409 } else { 410 $ret[] = $myrow['bid']; 411 } 412 } 413 return $ret; 414 } 415 416 function getAllByGroupModule($groupid, $module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1) 417 { 418 $db =& Database::getInstance(); 419 $ret = array(); 420 $sql = "SELECT DISTINCT gperm_itemid FROM ".$db->prefix('group_permission')." WHERE gperm_name = 'block_read' AND gperm_modid = 1"; 421 if ( is_array($groupid) ) { 422 $sql .= ' AND gperm_groupid IN ('.implode(',', $groupid).')'; 423 } else { 424 if (intval($groupid) > 0) { 425 $sql .= ' AND gperm_groupid='.$groupid; 426 } 427 } 428 $result = $db->query($sql); 429 $blockids = array(); 430 while ( $myrow = $db->fetchArray($result) ) { 431 $blockids[] = $myrow['gperm_itemid']; 432 } 433 if (!empty($blockids)) { 434 $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid'; 435 $sql .= ' AND b.isactive='.$isactive; 436 if (isset($visible)) { 437 $sql .= ' AND b.visible='.intval($visible); 438 } 439 $module_id = intval($module_id); 440 if (!empty($module_id)) { 441 $sql .= ' AND m.module_id IN (0,'.$module_id; 442 if ($toponlyblock) { 443 $sql .= ',-1'; 444 } 445 $sql .= ')'; 446 } else { 447 if ($toponlyblock) { 448 $sql .= ' AND m.module_id IN (0,-1)'; 449 } else { 450 $sql .= ' AND m.module_id=0'; 451 } 452 } 453 $sql .= ' AND b.bid IN ('.implode(',', $blockids).')'; 454 $sql .= ' ORDER BY '.$orderby; 455 $result = $db->query($sql); 456 while ( $myrow = $db->fetchArray($result) ) { 457 $block =& new XoopsBlock($myrow); 458 $ret[$myrow['bid']] =& $block; 459 unset($block); 460 } 461 } 462 return $ret; 463 } 464 465 function getNonGroupedBlocks($module_id=0, $toponlyblock=false, $visible=null, $orderby='b.weight,b.bid', $isactive=1) 466 { 467 $db =& Database::getInstance(); 468 $ret = array(); 469 $bids = array(); 470 $sql = "SELECT DISTINCT(bid) from ".$db->prefix('newblocks'); 471 if ($result = $db->query($sql)) { 472 while ( $myrow = $db->fetchArray($result) ) { 473 $bids[] = $myrow['bid']; 474 } 475 } 476 $sql = "SELECT DISTINCT(p.gperm_itemid) from ".$db->prefix('group_permission')." p, ".$db->prefix('groups')." g WHERE g.groupid=p.gperm_groupid AND p.gperm_name='block_read'"; 477 $grouped = array(); 478 if ($result = $db->query($sql)) { 479 while ( $myrow = $db->fetchArray($result) ) { 480 $grouped[] = $myrow['gperm_itemid']; 481 } 482 } 483 $non_grouped = array_diff($bids, $grouped); 484 if (!empty($non_grouped)) { 485 $sql = 'SELECT b.* FROM '.$db->prefix('newblocks').' b, '.$db->prefix('block_module_link').' m WHERE m.block_id=b.bid'; 486 $sql .= ' AND b.isactive='.$isactive; 487 if (isset($visible)) { 488 $sql .= ' AND b.visible='.intval($visible); 489 } 490 $module_id = intval($module_id); 491 if (!empty($module_id)) { 492 $sql .= ' AND m.module_id IN (0,'.$module_id; 493 if ($toponlyblock) { 494 $sql .= ',-1'; 495 } 496 $sql .= ')'; 497 } else { 498 if ($toponlyblock) { 499 $sql .= ' AND m.module_id IN (0,-1)'; 500 } else { 501 $sql .= ' AND m.module_id=0'; 502 } 503 } 504 $sql .= ' AND b.bid IN ('.implode(',', $non_grouped).')'; 505 $sql .= ' ORDER BY '.$orderby; 506 $result = $db->query($sql); 507 while ( $myrow = $db->fetchArray($result) ) { 508 $block =& new XoopsBlock($myrow); 509 $ret[$myrow['bid']] =& $block; 510 unset($block); 511 } 512 } 513 return $ret; 514 } 515 516 function countSimilarBlocks($moduleId, $funcNum, $showFunc = null) 517 { 518 $funcNum = intval($funcNum); 519 $moduleId = intval($moduleId); 520 if ($funcNum < 1 || $moduleId < 1) { 521 // invalid query 522 return 0; 523 } 524 $db =& Database::getInstance(); 525 if (isset($showFunc)) { 526 // showFunc is set for more strict comparison 527 $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d AND show_func = %s", $db->prefix('newblocks'), $moduleId, $funcNum, $db->quoteString(trim($showFunc))); 528 } else { 529 $sql = sprintf("SELECT COUNT(*) FROM %s WHERE mid = %d AND func_num = %d", $db->prefix('newblocks'), $moduleId, $funcNum); 530 } 531 if (!$result = $db->query($sql)) { 532 return 0; 533 } 534 list($count) = $db->fetchRow($result); 535 return $count; 536 } 537 } 538 ?>
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 |
![]() |