Changeset 2609:c26642f775e2 for inc/core/class.dc.core.php
- Timestamp:
- 12/10/13 09:00:09 (12 years ago)
- Branch:
- twig
- Parents:
- 2468:d7fda5a0bd39 (diff), 2608:3365c9df16a6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.core.php
r2313 r2609 4 4 # This file is part of Dotclear 2. 5 5 # 6 # Copyright (c) 2003-201 1Olivier Meunier & Association Dotclear6 # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear 7 7 # Licensed under the GPL version 2.0 license. 8 8 # See LICENSE file or … … 40 40 public $log; ///< <b>dcLog</b> dcLog object 41 41 public $tpl; ///< <b>Twig_Environment</b> Twig_Environment object 42 public $stime; ///< <b>float</b> starting time 42 43 43 44 private $versions = null; … … 60 61 public function __construct($driver, $host, $db, $user, $password, $prefix, $persist) 61 62 { 63 if (defined('DC_START_TIME')) { 64 $this->stime=DC_START_TIME; 65 } else { 66 $this->stime = microtime(true); 67 } 68 62 69 $this->con = dbLayer::init($driver,$host,$db,$user,$password,$persist); 63 70 … … 88 95 $this->url = new dcUrlHandlers(); 89 96 90 $this->plugins = new dc Modules($this);97 $this->plugins = new dcPlugins($this); 91 98 92 99 $this->rest = new dcRestServer($this); … … 1019 1026 private function getBlogCursor($cur) 1020 1027 { 1021 if ($cur->blog_id !== null 1022 && !preg_match('/^[A-Za-z0-9._-]{2,}$/',$cur->blog_id)) { 1028 if (($cur->blog_id !== null 1029 && !preg_match('/^[A-Za-z0-9._-]{2,}$/',$cur->blog_id)) || 1030 (!$cur->blog_id)) { 1023 1031 throw new Exception(__('Blog ID must contain at least 2 characters using letters, numbers or symbols.')); 1024 1032 } 1025 1033 1026 if ($cur->blog_name !== null && $cur->blog_name == '') { 1034 if (($cur->blog_name !== null && $cur->blog_name == '') || 1035 (!$cur->blog_name)) { 1027 1036 throw new Exception(__('No blog name')); 1028 1037 } 1029 1038 1030 if ($cur->blog_url !== null && $cur->blog_url == '') { 1039 if (($cur->blog_url !== null && $cur->blog_url == '') || 1040 (!$cur->blog_url)) { 1031 1041 throw new Exception(__('No blog URL')); 1032 1042 } … … 1343 1353 array('media_img_title_pattern','string','Title ;; Date(%b %Y) ;; separator(, )', 1344 1354 'Pattern to set image title when you insert it in a post'), 1355 array('nb_post_for_home','integer',20, 1356 'Number of entries on first home page'), 1345 1357 array('nb_post_per_page','integer',20, 1346 'Number of entries on home page and category pages'),1358 'Number of entries on home pages and category pages'), 1347 1359 array('nb_post_per_feed','integer',20, 1348 1360 'Number of entries on feeds'), … … 1506 1518 } 1507 1519 } 1520 1521 /** 1522 Return elapsed time since script has been started 1523 @param $mtime <b>float</b> timestamp (microtime format) to evaluate delta from 1524 current time is taken if null 1525 @return <b>float</b> elapsed time 1526 */ 1527 public function getElapsedTime ($mtime=null) { 1528 if ($mtime !== null) { 1529 return $mtime-$this->stime; 1530 } else { 1531 return microtime(true)-$this->stime; 1532 } 1533 } 1508 1534 //@} 1535 1536 1537 1509 1538 } 1510 ?> -
inc/core/class.dc.core.php
r2596 r2609 39 39 public $rest; ///< <b>dcRestServer</b> dcRestServer object 40 40 public $log; ///< <b>dcLog</b> dcLog object 41 public $tpl; ///< <b>Twig_Environment</b> Twig_Environment object 41 42 public $stime; ///< <b>float</b> starting time 42 43 43 44 private $versions = null; 44 45 private $formaters = array(); 45 46 private $behaviors = array(); 46 47 private $post_types = array(); 47 48 48 49 /** 49 50 dcCore constructor inits everything related to Dotclear. It takes arguments 50 51 to init database connection. 51 52 52 53 @param driver <b>string</b> Database driver name 53 54 @param host <b>string</b> Database hostname … … 67 68 68 69 $this->con = dbLayer::init($driver,$host,$db,$user,$password,$persist); 69 70 70 71 # define weak_locks for mysql 71 72 if ($this->con instanceof mysqlConnection) { … … 74 75 mysqliConnection::$weak_locks = true; 75 76 } 76 77 77 78 # define searchpath for postgresql 78 79 if ($this->con instanceof pgsqlConnection) … … 86 87 } 87 88 } 88 89 89 90 $this->prefix = $prefix; 90 91 91 92 $this->error = new dcError(); 92 93 $this->auth = $this->authInstance(); 93 94 $this->session = new sessionDB($this->con,$this->prefix.'session',DC_SESSION_NAME,'',null,DC_ADMIN_SSL); 94 95 $this->url = new dcUrlHandlers(); 95 96 96 97 $this->plugins = new dcPlugins($this); 97 98 98 99 $this->rest = new dcRestServer($this); 99 100 100 101 $this->meta = new dcMeta($this); 101 102 102 103 $this->log = new dcLog($this); 103 104 104 105 $this->addFormater('xhtml', create_function('$s','return $s;')); 105 106 $this->addFormater('wiki', array($this,'wikiTransform')); 106 } 107 107 $this->loadTemplateEnvironment(); 108 } 109 108 110 private function authInstance() 109 111 { … … 115 117 $c = DC_AUTH_CLASS; 116 118 } 117 119 118 120 if (!class_exists($c)) { 119 121 throw new Exception('Authentication class '.$c.' does not exist.'); 120 122 } 121 123 122 124 if ($c != 'dcAuth' && !is_subclass_of($c,'dcAuth')) { 123 125 throw new Exception('Authentication class '.$c.' does not inherit dcAuth.'); 124 126 } 125 127 126 128 return new $c($this); 127 129 } 128 129 130 131 /** 132 Create template environment (Twig_Environment instance) 133 134 default-templates path must be added from admin|public/prepend.php with: 135 $core->tpl->getLoader()->addPath('PATH_TO/default-templates'); 136 Selected theme path must be added with: 137 $core->tpl->getLoader()->prependPath('PATH_TO/MY_THEME'); 138 */ 139 public function loadTemplateEnvironment() 140 { 141 $cache_dir = path::real(DC_TPL_CACHE.'/twtpl',false); 142 if (!is_dir($cache_dir)) { 143 try { 144 files::makeDir($cache_dir); 145 } catch (Exception $e) { 146 $cache_dir = false; 147 } 148 } 149 150 $this->tpl = new Twig_Environment( 151 new Twig_Loader_Filesystem(dirname(__FILE__).'/../swf'), 152 array( 153 'auto_reload' => true, 154 'autoescape' => false, 155 'base_template_class' => 'Twig_Template', 156 'cache' => $cache_dir, 157 'charset' => 'UTF-8', 158 'debug' => DC_DEBUG, 159 'optimizations' => -1, 160 'strict_variables' => 0 //DC_DEBUG // Please fix undefined variables! 161 ) 162 ); 163 $this->tpl->addExtension(new dcFormExtension($this)); 164 $this->tpl->addExtension(new dcTabExtension($this)); 165 } 166 130 167 /// @name Blog init methods 131 168 //@{ 132 169 /** 133 170 Sets a blog to use in <var>blog</var> property. 134 171 135 172 @param id <b>string</b> Blog ID 136 173 */ … … 139 176 $this->blog = new dcBlog($this, $id); 140 177 } 141 178 142 179 /** 143 180 Unsets <var>blog</var> property. … … 148 185 } 149 186 //@} 150 151 187 188 152 189 /// @name Blog status methods 153 190 //@{ 154 191 /** 155 192 Returns an array of available blog status codes and names. 156 193 157 194 @return <b>array</b> Simple array with codes in keys and names in value 158 195 */ … … 165 202 ); 166 203 } 167 204 168 205 /** 169 206 Returns a blog status name given to a code. This is intended to be 170 207 human-readable and will be translated, so never use it for tests. 171 208 If status code does not exist, returns <i>offline</i>. 172 209 173 210 @param s <b>integer</b> Status code 174 211 @return <b>string</b> Blog status name … … 183 220 } 184 221 //@} 185 222 186 223 /// @name Admin nonce secret methods 187 224 //@{ 188 225 189 226 public function getNonce() 190 227 { 191 228 return crypt::hmac(DC_MASTER_KEY,session_id()); 192 229 } 193 230 194 231 public function checkNonce($secret) 195 232 { … … 197 234 return false; 198 235 } 199 236 200 237 return $secret == crypt::hmac(DC_MASTER_KEY,session_id()); 201 238 } 202 239 203 240 public function formNonce() 204 241 { … … 206 243 return; 207 244 } 208 245 209 246 return form::hidden(array('xd_check'),$this->getNonce()); 210 247 } 211 248 //@} 212 213 249 250 214 251 /// @name Text Formatters methods 215 252 //@{ … … 218 255 transform text. The function must be a valid callback and takes one 219 256 argument: the string to transform. It returns the transformed string. 220 257 221 258 @param name <b>string</b> Formater name 222 259 @param func <b>callback</b> Function to use, must be a valid and callable callback … … 228 265 } 229 266 } 230 267 231 268 /** 232 269 Returns formaters list. 233 270 234 271 @return <b>array</b> An array of formaters names in values. 235 272 */ … … 238 275 return array_keys($this->formaters); 239 276 } 240 277 241 278 /** 242 279 If <var>$name</var> is a valid formater, it returns <var>$str</var> 243 280 transformed using that formater. 244 281 245 282 @param name <b>string</b> Formater name 246 283 @param str <b>string</b> String to transform … … 252 289 return call_user_func($this->formaters[$name],$str); 253 290 } 254 291 255 292 return $str; 256 293 } 257 294 //@} 258 259 295 296 260 297 /// @name Behaviors methods 261 298 //@{ … … 263 300 Adds a new behavior to behaviors stack. <var>$func</var> must be a valid 264 301 and callable callback. 265 302 266 303 @param behavior <b>string</b> Behavior name 267 304 @param func <b>callback</b> Function to call … … 273 310 } 274 311 } 275 312 276 313 /** 277 314 Tests if a particular behavior exists in behaviors stack. 278 315 279 316 @param behavior <b>string</b> Behavior name 280 317 @return <b>boolean</b> … … 284 321 return isset($this->behaviors[$behavior]); 285 322 } 286 323 287 324 /** 288 325 Get behaviors stack (or part of). 289 326 290 327 @param behavior <b>string</b> Behavior name 291 328 @return <b>array</b> … … 294 331 { 295 332 if (empty($this->behaviors)) return null; 296 333 297 334 if ($behavior == '') { 298 335 return $this->behaviors; … … 300 337 return $this->behaviors[$behavior]; 301 338 } 302 339 303 340 return array(); 304 341 } 305 342 306 343 /** 307 344 Calls every function in behaviors stack for a given behavior and returns 308 345 concatened result of each function. 309 346 310 347 Every parameters added after <var>$behavior</var> will be pass to 311 348 behavior calls. 312 349 313 350 @param behavior <b>string</b> Behavior name 314 351 @return <b>string</b> Behavior concatened result … … 320 357 $args = func_get_args(); 321 358 array_shift($args); 322 359 323 360 $res = ''; 324 361 325 362 foreach ($this->behaviors[$behavior] as $f) { 326 363 $res .= call_user_func_array($f,$args); 327 364 } 328 365 329 366 return $res; 330 367 } 331 368 } 332 369 //@} 333 370 334 371 /// @name Post types URLs management 335 372 //@{ … … 339 376 $type = 'post'; 340 377 } 341 378 342 379 $url = sprintf($this->post_types[$type]['admin_url'],$post_id); 343 380 return $escaped ? html::escapeURL($url) : $url; 344 381 } 345 382 346 383 public function getPostPublicURL($type,$post_url,$escaped=true) 347 384 { … … 349 386 $type = 'post'; 350 387 } 351 388 352 389 $url = sprintf($this->post_types[$type]['public_url'],$post_url); 353 390 return $escaped ? html::escapeURL($url) : $url; 354 391 } 355 392 356 393 public function setPostType($type,$admin_url,$public_url,$label='') 357 394 { … … 362 399 ); 363 400 } 364 401 365 402 public function getPostTypes() 366 403 { … … 368 405 } 369 406 //@} 370 407 371 408 /// @name Versions management methods 372 409 //@{ 373 410 /** 374 411 Returns a given $module version. 375 412 376 413 @param module <b>string</b> Module name 377 414 @return <b>string</b> Module version … … 384 421 $strReq = 'SELECT module, version FROM '.$this->prefix.'version'; 385 422 $rs = $this->con->select($strReq); 386 423 387 424 while ($rs->fetch()) { 388 425 $this->versions[$rs->module] = $rs->version; 389 426 } 390 427 } 391 428 392 429 if (isset($this->versions[$module])) { 393 430 return $this->versions[$module]; … … 396 433 } 397 434 } 398 435 399 436 /** 400 437 Sets $version to given $module. 401 438 402 439 @param module <b>string</b> Module name 403 440 @param version <b>string</b> Module version … … 406 443 { 407 444 $cur_version = $this->getVersion($module); 408 445 409 446 $cur = $this->con->openCursor($this->prefix.'version'); 410 447 $cur->module = (string) $module; 411 448 $cur->version = (string) $version; 412 449 413 450 if ($cur_version === null) { 414 451 $cur->insert(); … … 416 453 $cur->update("WHERE module='".$this->con->escape($module)."'"); 417 454 } 418 455 419 456 $this->versions[$module] = $version; 420 457 } 421 458 422 459 /** 423 460 Removes given $module version entry. 424 461 425 462 @param module <b>string</b> Module name 426 463 */ … … 430 467 'DELETE FROM '.$this->prefix.'version '. 431 468 "WHERE module = '".$this->con->escape($module)."' "; 432 469 433 470 $this->con->execute($strReq); 434 471 435 472 if (is_array($this->versions)) { 436 473 unset($this->versions[$module]); 437 474 } 438 475 } 439 476 440 477 //@} 441 478 442 479 /// @name Users management methods 443 480 //@{ 444 481 /** 445 482 Returns a user by its ID. 446 483 447 484 @param id <b>string</b> User ID 448 485 @return <b>record</b> … … 451 488 { 452 489 $params['user_id'] = $id; 453 490 454 491 return $this->getUsers($params); 455 492 } 456 493 457 494 /** 458 495 Returns a users list. <b>$params</b> is an array with the following 459 496 optionnal parameters: 460 497 461 498 - <var>q</var>: search string (on user_id, user_name, user_firstname) 462 499 - <var>user_id</var>: user ID 463 500 - <var>order</var>: ORDER BY clause (default: user_id ASC) 464 501 - <var>limit</var>: LIMIT clause (should be an array ![limit,offset]) 465 502 466 503 @param params <b>array</b> Parameters 467 504 @param count_only <b>boolean</b> Only counts results … … 488 525 'WHERE NULL IS NULL '; 489 526 } 490 527 491 528 if (!empty($params['q'])) { 492 529 $q = $this->con->escape(str_replace('*','%',strtolower($params['q']))); … … 497 534 ') '; 498 535 } 499 536 500 537 if (!empty($params['user_id'])) { 501 538 $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."' "; 502 539 } 503 540 504 541 if (!$count_only) { 505 542 $strReq .= 'GROUP BY U.user_id,user_super,user_status,user_pwd,user_change_pwd,'. 506 543 'user_name,user_firstname,user_displayname,user_email,user_url,'. 507 544 'user_desc, user_lang,user_tz,user_post_status,user_options '; 508 545 509 546 if (!empty($params['order']) && !$count_only) { 510 547 $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; … … 513 550 } 514 551 } 515 552 516 553 if (!$count_only && !empty($params['limit'])) { 517 554 $strReq .= $this->con->limit($params['limit']); 518 555 } 519 556 520 557 $rs = $this->con->select($strReq); 521 558 $rs->extend('rsExtUser'); 522 559 return $rs; 523 560 } 524 561 525 562 /** 526 563 Create a new user. Takes a cursor as input and returns the new user ID. 527 564 528 565 @param cur <b>cursor</b> User cursor 529 566 @return <b>string</b> … … 534 571 throw new Exception(__('You are not an administrator')); 535 572 } 536 573 537 574 if ($cur->user_id == '') { 538 575 throw new Exception(__('No user ID given')); 539 576 } 540 577 541 578 if ($cur->user_pwd == '') { 542 579 throw new Exception(__('No password given')); 543 580 } 544 581 545 582 $this->getUserCursor($cur); 546 583 547 584 if ($cur->user_creadt === null) { 548 585 $cur->user_creadt = date('Y-m-d H:i:s'); 549 586 } 550 587 551 588 $cur->insert(); 552 589 553 590 $this->auth->afterAddUser($cur); 554 591 555 592 return $cur->user_id; 556 593 } 557 594 558 595 /** 559 596 Updates an existing user. Returns the user ID. 560 597 561 598 @param id <b>string</b> User ID 562 599 @param cur <b>cursor</b> User cursor … … 566 603 { 567 604 $this->getUserCursor($cur); 568 605 569 606 if (($cur->user_id !== null || $id != $this->auth->userID()) && 570 607 !$this->auth->isSuperAdmin()) { 571 608 throw new Exception(__('You are not an administrator')); 572 609 } 573 610 574 611 $cur->update("WHERE user_id = '".$this->con->escape($id)."' "); 575 612 576 613 $this->auth->afterUpdUser($id,$cur); 577 614 578 615 if ($cur->user_id !== null) { 579 616 $id = $cur->user_id; 580 617 } 581 618 582 619 # Updating all user's blogs 583 620 $rs = $this->con->select( … … 585 622 "WHERE user_id = '".$this->con->escape($id)."' " 586 623 ); 587 624 588 625 while ($rs->fetch()) { 589 626 $b = new dcBlog($this,$rs->blog_id); … … 591 628 unset($b); 592 629 } 593 630 594 631 return $id; 595 632 } 596 633 597 634 /** 598 635 Deletes a user. 599 636 600 637 @param id <b>string</b> User ID 601 638 */ … … 605 642 throw new Exception(__('You are not an administrator')); 606 643 } 607 644 608 645 if ($id == $this->auth->userID()) { 609 646 return; 610 647 } 611 648 612 649 $rs = $this->getUser($id); 613 650 614 651 if ($rs->nb_post > 0) { 615 652 return; 616 653 } 617 654 618 655 $strReq = 'DELETE FROM '.$this->prefix.'user '. 619 656 "WHERE user_id = '".$this->con->escape($id)."' "; 620 657 621 658 $this->con->execute($strReq); 622 659 623 660 $this->auth->afterDelUser($id); 624 661 } 625 662 626 663 /** 627 664 Checks whether a user exists. 628 665 629 666 @param id <b>string</b> User ID 630 667 @return <b>boolean</b> … … 635 672 'FROM '.$this->prefix.'user '. 636 673 "WHERE user_id = '".$this->con->escape($id)."' "; 637 674 638 675 $rs = $this->con->select($strReq); 639 676 640 677 return !$rs->isEmpty(); 641 678 } 642 679 643 680 /** 644 681 Returns all user permissions as an array which looks like: 645 682 646 683 - [blog_id] 647 684 - [name] => Blog name … … 650 687 - [permission] => true 651 688 - ... 652 689 653 690 @param id <b>string</b> User ID 654 691 @return <b>array</b> … … 660 697 'INNER JOIN '.$this->prefix.'blog B ON P.blog_id = B.blog_id '. 661 698 "WHERE user_id = '".$this->con->escape($id)."' "; 662 699 663 700 $rs = $this->con->select($strReq); 664 701 665 702 $res = array(); 666 703 667 704 while ($rs->fetch()) 668 705 { … … 673 710 ); 674 711 } 675 712 676 713 return $res; 677 714 } 678 715 679 716 /** 680 717 Sets user permissions. The <var>$perms</var> array looks like: 681 718 682 719 - [blog_id] => '|perm1|perm2|' 683 720 - ... 684 721 685 722 @param id <b>string</b> User ID 686 723 @param perms <b>array</b> Permissions array … … 691 728 throw new Exception(__('You are not an administrator')); 692 729 } 693 730 694 731 $strReq = 'DELETE FROM '.$this->prefix.'permissions '. 695 732 "WHERE user_id = '".$this->con->escape($id)."' "; 696 733 697 734 $this->con->execute($strReq); 698 735 699 736 foreach ($perms as $blog_id => $p) { 700 737 $this->setUserBlogPermissions($id, $blog_id, $p, false); 701 738 } 702 739 } 703 740 704 741 /** 705 742 Sets user permissions for a given blog. <var>$perms</var> is an array with 706 743 permissions in values 707 744 708 745 @param id <b>string</b> User ID 709 746 @param blog_id <b>string</b> Blog ID … … 716 753 throw new Exception(__('You are not an administrator')); 717 754 } 718 755 719 756 $no_perm = empty($perms); 720 757 721 758 $perms = '|'.implode('|',array_keys($perms)).'|'; 722 759 723 760 $cur = $this->con->openCursor($this->prefix.'permissions'); 724 761 725 762 $cur->user_id = (string) $id; 726 763 $cur->blog_id = (string) $blog_id; 727 764 $cur->permissions = $perms; 728 765 729 766 if ($delete_first || $no_perm) 730 767 { … … 732 769 "WHERE blog_id = '".$this->con->escape($blog_id)."' ". 733 770 "AND user_id = '".$this->con->escape($id)."' "; 734 771 735 772 $this->con->execute($strReq); 736 773 } 737 774 738 775 if (!$no_perm) { 739 776 $cur->insert(); 740 777 } 741 778 } 742 779 743 780 /** 744 781 Sets a user default blog. This blog will be selected when user log in. 745 782 746 783 @param id <b>string</b> User ID 747 784 @param blog_id <b>string</b> Blog ID … … 750 787 { 751 788 $cur = $this->con->openCursor($this->prefix.'user'); 752 789 753 790 $cur->user_default_blog = (string) $blog_id; 754 791 755 792 $cur->update("WHERE user_id = '".$this->con->escape($id)."'"); 756 793 } 757 794 758 795 private function getUserCursor($cur) 759 796 { … … 762 799 throw new Exception(__('User ID must contain at least 2 characters using letters, numbers or symbols.')); 763 800 } 764 801 765 802 if ($cur->user_url !== null && $cur->user_url != '') { 766 803 if (!preg_match('|^http(s?)://|',$cur->user_url)) { … … 768 805 } 769 806 } 770 807 771 808 if ($cur->isField('user_pwd')) { 772 809 if (strlen($cur->user_pwd) < 6) { … … 775 812 $cur->user_pwd = crypt::hmac(DC_MASTER_KEY,$cur->user_pwd); 776 813 } 777 814 778 815 if ($cur->user_lang !== null && !preg_match('/^[a-z]{2}(-[a-z]{2})?$/',$cur->user_lang)) { 779 816 throw new Exception(__('Invalid user language code')); 780 817 } 781 818 782 819 if ($cur->user_upddt === null) { 783 820 $cur->user_upddt = date('Y-m-d H:i:s'); 784 821 } 785 822 786 823 if ($cur->user_options !== null) { 787 824 $cur->user_options = serialize((array) $cur->user_options); 788 825 } 789 826 } 790 827 791 828 /** 792 829 Returns user default settings in an associative array with setting names in 793 830 keys. 794 831 795 832 @return <b>array</b> 796 833 */ … … 804 841 } 805 842 //@} 806 843 807 844 /// @name Blog management methods 808 845 //@{ 809 846 /** 810 847 Returns all blog permissions (users) as an array which looks like: 811 848 812 849 - [user_id] 813 850 - [name] => User name … … 818 855 - [permission] => true 819 856 - ... 820 857 821 858 @param id <b>string</b> Blog ID 822 859 @param with_super <b>boolean</b> Includes super admins in result … … 831 868 'JOIN '.$this->prefix.'permissions P ON U.user_id = P.user_id '. 832 869 "WHERE blog_id = '".$this->con->escape($id)."' "; 833 870 834 871 if ($with_super) { 835 872 $strReq .= … … 840 877 'WHERE user_super = 1 '; 841 878 } 842 879 843 880 $rs = $this->con->select($strReq); 844 881 845 882 $res = array(); 846 883 847 884 while ($rs->fetch()) 848 885 { … … 856 893 ); 857 894 } 858 895 859 896 return $res; 860 897 } 861 898 862 899 /** 863 900 Returns a blog of given ID. 864 901 865 902 @param id <b>string</b> Blog ID 866 903 @return <b>record</b> … … 869 906 { 870 907 $blog = $this->getBlogs(array('blog_id'=>$id)); 871 908 872 909 if ($blog->isEmpty()) { 873 910 return false; 874 911 } 875 912 876 913 return $blog; 877 914 } 878 915 879 916 /** 880 917 Returns a record of blogs. <b>$params</b> is an array with the following 881 918 optionnal parameters: 882 919 883 920 - <var>blog_id</var>: Blog ID 884 921 - <var>q</var>: Search string on blog_id, blog_name and blog_url 885 922 - <var>limit</var>: limit results 886 923 887 924 @param params <b>array</b> Parameters 888 925 @param count_only <b>boolean</b> Count only results … … 893 930 $join = ''; // %1$s 894 931 $where = ''; // %2$s 895 932 896 933 if ($count_only) 897 934 { … … 911 948 'WHERE NULL IS NULL '. 912 949 '%2$s '; 913 950 914 951 if (!empty($params['order'])) { 915 952 $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; … … 917 954 $strReq .= 'ORDER BY B.blog_id ASC '; 918 955 } 919 956 920 957 if (!empty($params['limit'])) { 921 958 $strReq .= $this->con->limit($params['limit']); 922 959 } 923 960 } 924 961 925 962 if ($this->auth->userID() && !$this->auth->isSuperAdmin()) 926 963 { … … 933 970 $where = 'AND blog_status IN (1,0) '; 934 971 } 935 972 936 973 if (!empty($params['blog_id'])) { 937 974 $where .= "AND B.blog_id = '".$this->con->escape($params['blog_id'])."' "; 938 975 } 939 976 940 977 if (!empty($params['q'])) { 941 978 $params['q'] = strtolower(str_replace('*','%',$params['q'])); … … 947 984 ') '; 948 985 } 949 986 950 987 $strReq = sprintf($strReq,$join,$where); 951 988 return $this->con->select($strReq); 952 989 } 953 990 954 991 /** 955 992 Creates a new blog. 956 993 957 994 @param cur <b>cursor</b> Blog cursor 958 995 */ … … 962 999 throw new Exception(__('You are not an administrator')); 963 1000 } 964 1001 965 1002 $this->getBlogCursor($cur); 966 1003 967 1004 $cur->blog_creadt = date('Y-m-d H:i:s'); 968 1005 $cur->blog_upddt = date('Y-m-d H:i:s'); 969 1006 $cur->blog_uid = md5(uniqid()); 970 1007 971 1008 $cur->insert(); 972 1009 } 973 1010 974 1011 /** 975 1012 Updates a given blog. 976 1013 977 1014 @param id <b>string</b> Blog ID 978 1015 @param cur <b>cursor</b> Blog cursor … … 981 1018 { 982 1019 $this->getBlogCursor($cur); 983 1020 984 1021 $cur->blog_upddt = date('Y-m-d H:i:s'); 985 1022 986 1023 $cur->update("WHERE blog_id = '".$this->con->escape($id)."'"); 987 1024 } 988 1025 989 1026 private function getBlogCursor($cur) 990 1027 { … … 992 1029 && !preg_match('/^[A-Za-z0-9._-]{2,}$/',$cur->blog_id)) || 993 1030 (!$cur->blog_id)) { 994 throw new Exception(__('Blog ID must contain at least 2 characters using letters, numbers or symbols.')); 995 } 996 1031 throw new Exception(__('Blog ID must contain at least 2 characters using letters, numbers or symbols.')); 1032 } 1033 997 1034 if (($cur->blog_name !== null && $cur->blog_name == '') || 998 1035 (!$cur->blog_name)) { 999 1036 throw new Exception(__('No blog name')); 1000 1037 } 1001 1038 1002 1039 if (($cur->blog_url !== null && $cur->blog_url == '') || 1003 1040 (!$cur->blog_url)) { 1004 1041 throw new Exception(__('No blog URL')); 1005 1042 } 1006 1043 1007 1044 if ($cur->blog_desc !== null) { 1008 1045 $cur->blog_desc = html::clean($cur->blog_desc); 1009 1046 } 1010 1047 } 1011 1048 1012 1049 /** 1013 1050 Removes a given blog. 1014 1051 @warning This will remove everything related to the blog (posts, 1015 1052 categories, comments, links...) 1016 1053 1017 1054 @param id <b>string</b> Blog ID 1018 1055 */ … … 1022 1059 throw new Exception(__('You are not an administrator')); 1023 1060 } 1024 1061 1025 1062 $strReq = 'DELETE FROM '.$this->prefix.'blog '. 1026 1063 "WHERE blog_id = '".$this->con->escape($id)."' "; 1027 1064 1028 1065 $this->con->execute($strReq); 1029 1066 } 1030 1067 1031 1068 /** 1032 1069 Checks if a blog exist. 1033 1070 1034 1071 @param id <b>string</b> Blog ID 1035 1072 @return <b>boolean</b> … … 1040 1077 'FROM '.$this->prefix.'blog '. 1041 1078 "WHERE blog_id = '".$this->con->escape($id)."' "; 1042 1079 1043 1080 $rs = $this->con->select($strReq); 1044 1081 1045 1082 return !$rs->isEmpty(); 1046 1083 } 1047 1084 1048 1085 /** 1049 1086 Count posts on a blog 1050 1087 1051 1088 @param id <b>string</b> Blog ID 1052 1089 @param type <b>string</b> Post type … … 1058 1095 'FROM '.$this->prefix.'post '. 1059 1096 "WHERE blog_id = '".$this->con->escape($id)."' "; 1060 1097 1061 1098 if ($type) { 1062 1099 $strReq .= "AND post_type = '".$this->con->escape($type)."' "; 1063 1100 } 1064 1101 1065 1102 return $this->con->select($strReq)->f(0); 1066 1103 } 1067 1104 //@} 1068 1105 1069 1106 /// @name HTML Filter methods 1070 1107 //@{ … … 1073 1110 tidy extension is present). If <b>enable_html_filter</b> blog setting is 1074 1111 false, returns not filtered string. 1075 1112 1076 1113 @param str <b>string</b> String to filter 1077 1114 @return <b>string</b> Filtered string. … … 1082 1119 return $str; 1083 1120 } 1084 1121 1085 1122 $filter = new htmlFilter; 1086 1123 $str = trim($filter->apply($str)); … … 1088 1125 } 1089 1126 //@} 1090 1127 1091 1128 /// @name wiki2xhtml methods 1092 1129 //@{ … … 1095 1132 $this->wiki2xhtml = new wiki2xhtml; 1096 1133 } 1097 1134 1098 1135 /** 1099 1136 Returns a transformed string with wiki2xhtml. 1100 1137 1101 1138 @param str <b>string</b> String to transform 1102 1139 @return <b>string</b> Transformed string … … 1109 1146 return $this->wiki2xhtml->transform($str); 1110 1147 } 1111 1148 1112 1149 /** 1113 1150 Inits <var>wiki2xhtml</var> property for blog post. … … 1116 1153 { 1117 1154 $this->initWiki(); 1118 1155 1119 1156 $this->wiki2xhtml->setOpts(array( 1120 1157 'active_title' => 1, … … 1148 1185 'note_str' => '<div class="footnotes"><h4>Notes</h4>%s</div>' 1149 1186 )); 1150 1187 1151 1188 $this->wiki2xhtml->registerFunction('url:post',array($this,'wikiPostLink')); 1152 1189 1153 1190 # --BEHAVIOR-- coreWikiPostInit 1154 1191 $this->callBehavior('coreInitWikiPost',$this->wiki2xhtml); 1155 1192 } 1156 1193 1157 1194 /** 1158 1195 Inits <var>wiki2xhtml</var> property for simple blog comment (basic syntax). … … 1161 1198 { 1162 1199 $this->initWiki(); 1163 1200 1164 1201 $this->wiki2xhtml->setOpts(array( 1165 1202 'active_title' => 0, … … 1190 1227 'active_fr_syntax' => 0 1191 1228 )); 1192 1229 1193 1230 # --BEHAVIOR-- coreInitWikiSimpleComment 1194 1231 $this->callBehavior('coreInitWikiSimpleComment',$this->wiki2xhtml); 1195 1232 } 1196 1233 1197 1234 /** 1198 1235 Inits <var>wiki2xhtml</var> property for blog comment. … … 1201 1238 { 1202 1239 $this->initWiki(); 1203 1240 1204 1241 $this->wiki2xhtml->setOpts(array( 1205 1242 'active_title' => 0, … … 1230 1267 'active_fr_syntax' => 0 1231 1268 )); 1232 1269 1233 1270 # --BEHAVIOR-- coreInitWikiComment 1234 1271 $this->callBehavior('coreInitWikiComment',$this->wiki2xhtml); 1235 1272 } 1236 1273 1237 1274 public function wikiPostLink($url,$content) 1238 1275 { 1239 if (!($this->blog instanceof dcBlog)) { 1276 if (!($this->blog instanceof dcBlog)) { 1240 1277 return array(); 1241 1278 } 1242 1279 1243 1280 $post_id = abs((integer) substr($url,5)); 1244 1281 if (!$post_id) { 1245 1282 return array(); 1246 1283 } 1247 1284 1248 1285 $post = $this->blog->getPosts(array('post_id'=>$post_id)); 1249 1286 if ($post->isEmpty()) { 1250 1287 return array(); 1251 1288 } 1252 1289 1253 1290 $res = array('url' => $post->getURL()); 1254 1291 $post_title = $post->post_title; 1255 1292 1256 1293 if ($content != $url) { 1257 1294 $res['title'] = html::escapeHTML($post->post_title); 1258 1295 } 1259 1296 1260 1297 if ($content == '' || $content == $url) { 1261 1298 $res['content'] = html::escapeHTML($post->post_title); 1262 1299 } 1263 1300 1264 1301 if ($post->post_lang) { 1265 1302 $res['lang'] = $post->post_lang; 1266 1303 } 1267 1304 1268 1305 return $res; 1269 1306 } 1270 1307 //@} 1271 1308 1272 1309 /// @name Maintenance methods 1273 1310 //@{ … … 1275 1312 Creates default settings for active blog. Optionnal parameter 1276 1313 <var>defaults</var> replaces default params while needed. 1277 1314 1278 1315 @param defaults <b>array</b> Default parameters 1279 1316 */ … … 1360 1397 ); 1361 1398 } 1362 1399 1363 1400 $settings = new dcSettings($this,null); 1364 1401 $settings->addNamespace('system'); 1365 1402 1366 1403 foreach ($defaults as $v) { 1367 1404 $settings->system->put($v[0],$v[2],$v[1],$v[3],false,true); 1368 1405 } 1369 1406 } 1370 1407 1371 1408 /** 1372 1409 Recreates entries search engine index. 1373 1410 1374 1411 @param start <b>integer</b> Start entry index 1375 1412 @param limit <b>integer</b> Number of entry to index 1376 1413 1377 1414 @return <b>integer</b> <var>$start</var> and <var>$limit</var> sum 1378 1415 */ … … 1383 1420 $rs = $this->con->select($strReq); 1384 1421 $count = $rs->f(0); 1385 1422 1386 1423 $strReq = 'SELECT post_id, post_title, post_excerpt_xhtml, post_content_xhtml '. 1387 1424 'FROM '.$this->prefix.'post '; 1388 1425 1389 1426 if ($start !== null && $limit !== null) { 1390 1427 $strReq .= $this->con->limit($start,$limit); 1391 1428 } 1392 1429 1393 1430 $rs = $this->con->select($strReq,true); 1394 1431 1395 1432 $cur = $this->con->openCursor($this->prefix.'post'); 1396 1433 1397 1434 while ($rs->fetch()) 1398 1435 { 1399 1436 $words = $rs->post_title.' '. $rs->post_excerpt_xhtml.' '. 1400 1437 $rs->post_content_xhtml; 1401 1438 1402 1439 $cur->post_words = implode(' ',text::splitWords($words)); 1403 1440 $cur->update('WHERE post_id = '.(integer) $rs->post_id); 1404 1441 $cur->clean(); 1405 1442 } 1406 1443 1407 1444 if ($start+$limit > $count) { 1408 1445 return null; … … 1410 1447 return $start+$limit; 1411 1448 } 1412 1449 1413 1450 /** 1414 1451 Recreates comments search engine index. 1415 1452 1416 1453 @param start <b>integer</b> Start comment index 1417 1454 @param limit <b>integer</b> Number of comments to index 1418 1455 1419 1456 @return <b>integer</b> <var>$start</var> and <var>$limit</var> sum 1420 1457 */ … … 1425 1462 $rs = $this->con->select($strReq); 1426 1463 $count = $rs->f(0); 1427 1464 1428 1465 $strReq = 'SELECT comment_id, comment_content '. 1429 1466 'FROM '.$this->prefix.'comment '; 1430 1467 1431 1468 if ($start !== null && $limit !== null) { 1432 1469 $strReq .= $this->con->limit($start,$limit); 1433 1470 } 1434 1471 1435 1472 $rs = $this->con->select($strReq); 1436 1473 1437 1474 $cur = $this->con->openCursor($this->prefix.'comment'); 1438 1475 1439 1476 while ($rs->fetch()) 1440 1477 { … … 1443 1480 $cur->clean(); 1444 1481 } 1445 1482 1446 1483 if ($start+$limit > $count) { 1447 1484 return null; … … 1449 1486 return $start+$limit; 1450 1487 } 1451 1488 1452 1489 /** 1453 1490 Reinits nb_comment and nb_trackback in post table. … … 1455 1492 public function countAllComments() 1456 1493 { 1457 1494 1458 1495 $updCommentReq = 'UPDATE '.$this->prefix.'post P '. 1459 1496 'SET nb_comment = ('. … … 1471 1508 $this->con->execute($updTrackbackReq); 1472 1509 } 1473 1510 1474 1511 /** 1475 1512 Empty templates cache directory
Note: See TracChangeset
for help on using the changeset viewer.