Changeset 2609:c26642f775e2 for inc/core
- 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. - Location:
- inc/core
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.blog.php
r2468 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 … … 81 81 $this->desc = $b->blog_desc; 82 82 $this->url = $b->blog_url; 83 $this->host = preg_replace('|^([a-z]{3,}://)(.*?)/.*$|','$1$2',$this->url);83 $this->host = http::getHostFromURL($this->url); 84 84 $this->creadt = strtotime($b->blog_creadt); 85 85 $this->upddt = strtotime($b->blog_upddt); … … 963 963 $strReq .= 'ORDER BY post_dt DESC '; 964 964 } 965 if (!empty($params['limit'])) { 965 } 966 967 if (!$count_only && !empty($params['limit'])) { 966 968 $strReq .= $this->con->limit($params['limit']); 967 969 } 968 }969 970 970 971 if (!empty($params['sql_only'])) { … … 1893 1894 - post_id: (integer) Get comments belonging to given post_id 1894 1895 - cat_id: (integer or array) Get comments belonging to entries of given category ID 1895 - comment_id: (integer ) Get comment with given ID1896 - comment_id: (integer or array) Get comment with given ID (or IDs) 1896 1897 - comment_site: (string) Get comments with given comment_site 1897 1898 - comment_status: (integer) Get comments with given comment_status … … 1983 1984 1984 1985 if (isset($params['comment_id']) && $params['comment_id'] !== '') { 1985 $strReq .= 'AND comment_id = '.(integer) $params['comment_id'].' '; 1986 if (is_array($params['comment_id'])) { 1987 array_walk($params['comment_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); 1988 } else { 1989 $params['comment_id'] = array((integer) $params['comment_id']); 1990 } 1991 $strReq .= 'AND comment_id '.$this->con->in($params['comment_id']); 1986 1992 } 1987 1993 … … 2379 2385 //@} 2380 2386 } 2381 ?> -
inc/core/class.dc.blog.php
r2567 r2609 27 27 /** @var string Database table prefix */ 28 28 public $prefix; 29 29 30 30 /** @var string Blog ID */ 31 31 public $id; … … 46 46 /** @var string Blog status */ 47 47 public $status; 48 48 49 49 /** @var dcSettings dcSettings object */ 50 50 public $settings; … … 53 53 /** @var string Blog public path */ 54 54 public $public_path; 55 55 56 56 private $post_status = array(); 57 57 private $comment_status = array(); 58 58 59 59 private $categories; 60 60 61 61 /** @var boolean Disallow entries password protection */ 62 62 public $without_password = true; 63 63 64 64 /** 65 65 Inits dcBlog object 66 66 67 67 @param core <b>dcCore</b> Dotclear core reference 68 68 @param id <b>string</b> Blog ID … … 73 73 $this->prefix = $core->prefix; 74 74 $this->core =& $core; 75 75 76 76 if (($b = $this->core->getBlog($id)) !== false) 77 77 { … … 85 85 $this->upddt = strtotime($b->blog_upddt); 86 86 $this->status = $b->blog_status; 87 87 88 88 $this->settings = new dcSettings($this->core,$this->id); 89 89 90 90 $this->themes_path = path::fullFromRoot($this->settings->system->themes_path,DC_ROOT); 91 91 $this->public_path = path::fullFromRoot($this->settings->system->public_path,DC_ROOT); 92 92 93 93 $this->post_status['-2'] = __('Pending'); 94 94 $this->post_status['-1'] = __('Scheduled'); 95 95 $this->post_status['0'] = __('Unpublished'); 96 96 $this->post_status['1'] = __('Published'); 97 97 98 98 $this->comment_status['-2'] = __('Junk'); 99 99 $this->comment_status['-1'] = __('Pending'); 100 100 $this->comment_status['0'] = __('Unpublished'); 101 101 $this->comment_status['1'] = __('Published'); 102 102 103 103 # --BEHAVIOR-- coreBlogConstruct 104 104 $this->core->callBehavior('coreBlogConstruct',$this); 105 105 } 106 106 } 107 107 108 108 /// @name Common public methods 109 109 //@{ … … 116 116 return $this->url.'?'; 117 117 } 118 118 119 119 return $this->url; 120 120 } 121 121 122 122 /** 123 123 Returns an entry status name given to a code. Status are translated, never 124 124 use it for tests. If status code does not exist, returns <i>unpublished</i>. 125 125 126 126 @param s <b>integer</b> Status code 127 127 @return <b>string</b> Blog status name … … 134 134 return $this->post_status['0']; 135 135 } 136 136 137 137 /** 138 138 Returns an array of available entry status codes and names. 139 139 140 140 @return <b>array</b> Simple array with codes in keys and names in value 141 141 */ … … 144 144 return $this->post_status; 145 145 } 146 146 147 147 /** 148 148 Returns an array of available comment status codes and names. 149 149 150 150 @return <b>array</b> Simple array with codes in keys and names in value 151 151 */ … … 154 154 return $this->comment_status; 155 155 } 156 156 157 157 /** 158 158 Disallows entries password protection. You need to set it to 159 159 <var>false</var> while serving a public blog. 160 160 161 161 @param v <b>boolean</b> 162 162 */ … … 166 166 } 167 167 //@} 168 168 169 169 /// @name Triggers methods 170 170 //@{ … … 176 176 { 177 177 $cur = $this->con->openCursor($this->prefix.'blog'); 178 178 179 179 $cur->blog_upddt = date('Y-m-d H:i:s'); 180 180 181 181 $cur->update("WHERE blog_id = '".$this->con->escape($this->id)."' "); 182 182 183 183 # --BEHAVIOR-- coreBlogAfterTriggerBlog 184 184 $this->core->callBehavior('coreBlogAfterTriggerBlog',$cur); 185 185 } 186 186 187 187 /** 188 188 Updates comment and trackback counters in post table. Should be called 189 189 every time a comment or trackback is added, removed or changed its status. 190 190 191 191 @param id <b>integer</b> Comment ID 192 192 @param del <b>boolean</b> If comment is delete, set this to true … … 196 196 $this->triggerComments($id,$del); 197 197 } 198 198 199 199 /** 200 200 Updates comments and trackbacks counters in post table. Should be called 201 201 every time comments or trackbacks are added, removed or changed their status. 202 202 203 203 @param ids <b>mixed</b> Comment(s) ID(s) 204 204 @param del <b>boolean</b> If comment is delete, set this to true … … 208 208 { 209 209 $comments_ids = dcUtils::cleanIds($ids); 210 210 211 211 # Get posts affected by comments edition 212 212 if (empty($affected_posts)) { 213 $strReq = 213 $strReq = 214 214 'SELECT post_id '. 215 215 'FROM '.$this->prefix.'comment '. 216 216 'WHERE comment_id'.$this->con->in($comments_ids). 217 217 'GROUP BY post_id'; 218 218 219 219 $rs = $this->con->select($strReq); 220 220 221 221 $affected_posts = array(); 222 222 while ($rs->fetch()) { … … 224 224 } 225 225 } 226 226 227 227 if (!is_array($affected_posts) || empty($affected_posts)) { 228 228 return; 229 229 } 230 230 231 231 # Count number of comments if exists for affected posts 232 $strReq = 232 $strReq = 233 233 'SELECT post_id, COUNT(post_id) AS nb_comment, comment_trackback '. 234 234 'FROM '.$this->prefix.'comment '. … … 236 236 'AND post_id'.$this->con->in($affected_posts). 237 237 'GROUP BY post_id,comment_trackback'; 238 238 239 239 $rs = $this->con->select($strReq); 240 240 241 241 $posts = array(); 242 242 while ($rs->fetch()) { … … 247 247 } 248 248 } 249 249 250 250 # Update number of comments on affected posts 251 251 $cur = $this->con->openCursor($this->prefix.'post'); … … 253 253 { 254 254 $cur->clean(); 255 255 256 256 if (!array_key_exists($post_id,$posts)) { 257 257 $cur->nb_trackback = 0; … … 261 261 $cur->nb_comment = empty($posts[$post_id]['comment']) ? 0 : $posts[$post_id]['comment']; 262 262 } 263 263 264 264 $cur->update('WHERE post_id = '.$post_id); 265 265 } 266 266 } 267 267 //@} 268 268 269 269 /// @name Categories management methods 270 270 //@{ … … 274 274 $this->categories = new dcCategories($this->core); 275 275 } 276 276 277 277 return $this->categories; 278 278 } 279 279 280 280 /** 281 281 Retrieves categories. <var>$params</var> is an associative array which can 282 282 take the following parameters: 283 283 284 284 - post_type: Get only entries with given type (default "post") 285 285 - cat_url: filter on cat_url field … … 287 287 - start: start with a given category 288 288 - level: categories level to retrieve 289 289 290 290 @param params <b>array</b> Parameters 291 291 @return <b>record</b> … … 299 299 } 300 300 $counter = $this->getCategoriesCounter($c_params); 301 301 302 302 if (isset($params['without_empty']) && ($params['without_empty'] == false)) { 303 303 $without_empty = false; … … 305 305 $without_empty = $this->core->auth->userID() == false; # Get all categories if in admin display 306 306 } 307 307 308 308 $start = isset($params['start']) ? (integer) $params['start'] : 0; 309 309 $l = isset($params['level']) ? (integer) $params['level'] : 0; 310 310 311 311 $rs = $this->categories()->getChildren($start,null,'desc'); 312 312 313 313 # Get each categories total posts count 314 314 $data = array(); … … 319 319 { 320 320 $nb_post = isset($counter[$rs->cat_id]) ? (integer) $counter[$rs->cat_id] : 0; 321 321 322 322 if ($rs->level > $level) { 323 323 $nb_total = $nb_post; … … 335 335 unset($stack[$rs->level+1]); 336 336 } 337 337 338 338 if ($nb_total == 0 && $without_empty) { 339 339 continue; 340 340 } 341 341 342 342 $level = $rs->level; 343 343 344 344 $t = array(); 345 345 foreach ($cols as $c) { … … 348 348 $t['nb_post'] = $nb_post; 349 349 $t['nb_total'] = $nb_total; 350 350 351 351 if ($l == 0 || ($l > 0 && $l == $rs->level)) { 352 352 array_unshift($data,$t); 353 353 } 354 354 } 355 355 356 356 # We need to apply filter after counting 357 357 if (isset($params['cat_id']) && $params['cat_id'] !== '') … … 369 369 } 370 370 } 371 372 if (isset($params['cat_url']) && ($params['cat_url'] !== '') 371 372 if (isset($params['cat_url']) && ($params['cat_url'] !== '') 373 373 && !isset($params['cat_id'])) 374 374 { … … 385 385 } 386 386 } 387 387 388 388 return staticRecord::newFromArray($data); 389 389 } 390 390 391 391 /** 392 392 Retrieves a category by its ID. 393 393 394 394 @param id <b>integer</b> Category ID 395 395 @return <b>record</b> … … 399 399 return $this->getCategories(array('cat_id' => $id)); 400 400 } 401 401 402 402 /** 403 403 Retrieves parents of a given category. 404 404 405 405 @param id <b>integer</b> Category ID 406 406 @return <b>record</b> … … 410 410 return $this->categories()->getParents($id); 411 411 } 412 412 413 413 /** 414 414 Retrieves first parent of a given category. 415 415 416 416 @param id <b>integer</b> Category ID 417 417 @return <b>record</b> … … 421 421 return $this->categories()->getParent($id); 422 422 } 423 423 424 424 /** 425 425 Retrieves all category's first children 426 426 427 427 @param id <b>integer</b> Category ID 428 428 @return <b>record</b> … … 432 432 return $this->getCategories(array('start' => $id,'level' => $id == 0 ? 1 : 2)); 433 433 } 434 434 435 435 private function getCategoriesCounter($params=array()) 436 436 { … … 440 440 'JOIN '.$this->prefix."post P ON (C.cat_id = P.cat_id AND P.blog_id = '".$this->con->escape($this->id)."' ) ". 441 441 "WHERE C.blog_id = '".$this->con->escape($this->id)."' "; 442 442 443 443 if (!$this->core->auth->userID()) { 444 444 $strReq .= 'AND P.post_status = 1 '; 445 445 } 446 446 447 447 if (!empty($params['post_type'])) { 448 448 $strReq .= 'AND P.post_type '.$this->con->in($params['post_type']); 449 449 } 450 450 451 451 $strReq .= 'GROUP BY C.cat_id '; 452 452 453 453 $rs = $this->con->select($strReq); 454 454 $counters = array(); … … 456 456 $counters[$rs->cat_id] = $rs->nb_post; 457 457 } 458 458 459 459 return $counters; 460 460 } 461 461 462 462 /** 463 463 Creates a new category. Takes a cursor as input and returns the new category 464 464 ID. 465 465 466 466 @param cur <b>cursor</b> Category cursor 467 467 @return <b>integer</b> New category ID … … 472 472 throw new Exception(__('You are not allowed to add categories')); 473 473 } 474 474 475 475 $url = array(); 476 476 if ($parent != 0) … … 483 483 } 484 484 } 485 485 486 486 if ($cur->cat_url == '') { 487 487 $url[] = text::tidyURL($cur->cat_title,false); … … 489 489 $url[] = $cur->cat_url; 490 490 } 491 491 492 492 $cur->cat_url = implode('/',$url); 493 493 494 494 $this->getCategoryCursor($cur); 495 495 $cur->blog_id = (string) $this->id; 496 496 497 497 # --BEHAVIOR-- coreBeforeCategoryCreate 498 498 $this->core->callBehavior('coreBeforeCategoryCreate',$this,$cur); 499 499 500 500 $id = $this->categories()->addNode($cur,$parent); 501 501 # Update category's cursor … … 505 505 $cur->cat_rgt = $rs->cat_rgt; 506 506 } 507 507 508 508 # --BEHAVIOR-- coreAfterCategoryCreate 509 509 $this->core->callBehavior('coreAfterCategoryCreate',$this,$cur); 510 510 $this->triggerBlog(); 511 511 512 512 return $cur->cat_id; 513 513 } 514 514 515 515 /** 516 516 Updates an existing category. 517 517 518 518 @param id <b>integer</b> Category ID 519 519 @param cur <b>cursor</b> Category cursor … … 524 524 throw new Exception(__('You are not allowed to update categories')); 525 525 } 526 526 527 527 if ($cur->cat_url == '') 528 528 { … … 534 534 } 535 535 } 536 537 536 537 538 538 $url[] = text::tidyURL($cur->cat_title,false); 539 539 $cur->cat_url = implode('/',$url); 540 540 } 541 541 542 542 $this->getCategoryCursor($cur,$id); 543 543 544 544 # --BEHAVIOR-- coreBeforeCategoryUpdate 545 545 $this->core->callBehavior('coreBeforeCategoryUpdate',$this,$cur); 546 546 547 547 $cur->update( 548 548 'WHERE cat_id = '.(integer) $id.' '. 549 549 "AND blog_id = '".$this->con->escape($this->id)."' "); 550 550 551 551 # --BEHAVIOR-- coreAfterCategoryUpdate 552 552 $this->core->callBehavior('coreAfterCategoryUpdate',$this,$cur); 553 553 554 554 $this->triggerBlog(); 555 555 } … … 567 567 $this->triggerBlog(); 568 568 } 569 569 570 570 /** 571 571 DEPRECATED METHOD. Use dcBlog::setCategoryParent and dcBlog::moveCategory 572 572 instead. 573 573 574 574 @param id <b>integer</b> Category ID 575 575 @param order <b>integer</b> Category position … … 579 579 return; 580 580 } 581 581 582 582 /** 583 583 Set a category parent 584 584 585 585 @param id <b>integer</b> Category ID 586 586 @param parent <b>integer</b> Parent Category ID … … 591 591 $this->triggerBlog(); 592 592 } 593 593 594 594 /** 595 595 Set category position 596 596 597 597 @param id <b>integer</b> Category ID 598 598 @param sibling <b>integer</b> Sibling Category ID … … 604 604 $this->triggerBlog(); 605 605 } 606 606 607 607 /** 608 608 Deletes a category. 609 609 610 610 @param id <b>integer</b> Category ID 611 611 */ … … 615 615 throw new Exception(__('You are not allowed to delete categories')); 616 616 } 617 617 618 618 $strReq = 'SELECT COUNT(post_id) AS nb_post '. 619 619 'FROM '.$this->prefix.'post '. 620 620 'WHERE cat_id = '.(integer) $id.' '. 621 621 "AND blog_id = '".$this->con->escape($this->id)."' "; 622 622 623 623 $rs = $this->con->select($strReq); 624 624 625 625 if ($rs->nb_post > 0) { 626 626 throw new Exception(__('This category is not empty.')); 627 627 } 628 628 629 629 $this->categories()->deleteNode($id,true); 630 630 $this->triggerBlog(); 631 631 } 632 632 633 633 /** 634 634 Reset categories order and relocate them to first level … … 639 639 throw new Exception(__('You are not allowed to reset categories order')); 640 640 } 641 641 642 642 $this->categories()->resetOrder(); 643 643 $this->triggerBlog(); 644 644 } 645 645 646 646 private function checkCategory($title,$url,$id=null) 647 647 { 648 648 # Let's check if URL is taken... 649 $strReq = 649 $strReq = 650 650 'SELECT cat_url FROM '.$this->prefix.'category '. 651 651 "WHERE cat_url = '".$this->con->escape($url)."' ". … … 653 653 "AND blog_id = '".$this->con->escape($this->id)."' ". 654 654 'ORDER BY cat_url DESC'; 655 655 656 656 $rs = $this->con->select($strReq); 657 657 658 658 if (!$rs->isEmpty()) 659 659 { … … 665 665 $clause = "LIKE '".$this->con->escape($url)."%'"; 666 666 } 667 $strReq = 667 $strReq = 668 668 'SELECT cat_url FROM '.$this->prefix.'category '. 669 669 "WHERE cat_url ".$clause.' '. … … 671 671 "AND blog_id = '".$this->con->escape($this->id)."' ". 672 672 'ORDER BY cat_url DESC '; 673 673 674 674 $rs = $this->con->select($strReq); 675 675 $a = array(); … … 677 677 $a[] = $rs->cat_url; 678 678 } 679 679 680 680 natsort($a); 681 681 $t_url = end($a); 682 682 683 683 if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) { 684 684 $i = (integer) $m[2]; … … 687 687 $i = 1; 688 688 } 689 689 690 690 return $url.($i+1); 691 691 } 692 692 693 693 # URL is empty? 694 694 if ($url == '') { 695 695 throw new Exception(__('Empty category URL')); 696 696 } 697 697 698 698 return $url; 699 699 } 700 700 701 701 private function getCategoryCursor($cur,$id=null) 702 702 { … … 704 704 throw new Exception(__('You must provide a category title')); 705 705 } 706 706 707 707 # If we don't have any cat_url, let's do one 708 708 if ($cur->cat_url == '') { 709 709 $cur->cat_url = text::tidyURL($cur->cat_title,false); 710 710 } 711 711 712 712 # Still empty ? 713 713 if ($cur->cat_url == '') { … … 716 716 $cur->cat_url = text::tidyURL($cur->cat_url,true); 717 717 } 718 718 719 719 # Check if title or url are unique 720 720 $cur->cat_url = $this->checkCategory($cur->cat_title,$cur->cat_url,$id); 721 721 722 722 if ($cur->cat_desc !== null) { 723 723 $cur->cat_desc = $this->core->HTMLfilter($cur->cat_desc); … … 725 725 } 726 726 //@} 727 727 728 728 /// @name Entries management methods 729 729 //@{ … … 731 731 Retrieves entries. <b>$params</b> is an array taking the following 732 732 optionnal parameters: 733 733 734 734 - no_content: Don't retrieve entry content (excerpt and content) 735 735 - post_type: Get only entries with given type (default "post", array for many types and '' for no type) … … 755 755 - sql_only : return the sql request instead of results. Only ids are selected 756 756 - exclude_post_id : (integer or array) Exclude entries with given post_id 757 757 758 758 Please note that on every cat_id or cat_url, you can add ?not to exclude 759 759 the category and ?sub to get subcategories. 760 760 761 761 @param params <b>array</b> Parameters 762 762 @param count_only <b>boolean</b> Only counts results … … 773 773 $strReq = 'SELECT count(DISTINCT P.post_id) '; 774 774 } 775 elseif (!empty($params['sql_only'])) 775 elseif (!empty($params['sql_only'])) 776 776 { 777 777 $strReq = 'SELECT P.post_id '; … … 786 786 'post_content, post_content_xhtml, post_notes, '; 787 787 } 788 788 789 789 if (!empty($params['columns']) && is_array($params['columns'])) { 790 790 $content_req .= implode(', ',$params['columns']).', '; 791 791 } 792 792 793 793 794 794 $strReq = … … 802 802 'C.cat_title, C.cat_url, C.cat_desc '; 803 803 } 804 804 805 805 $strReq .= 806 806 'FROM '.$this->prefix.'post P '. … … 811 811 $strReq .= $params['from'].' '; 812 812 } 813 813 814 814 $strReq .= 815 815 "WHERE P.blog_id = '".$this->con->escape($this->id)."' "; 816 816 817 817 if (!$this->core->auth->check('contentadmin',$this->id)) { 818 818 $strReq .= 'AND ((post_status = 1 '; 819 819 820 820 if ($this->without_password) { 821 821 $strReq .= 'AND post_password IS NULL '; 822 822 } 823 823 $strReq .= ') '; 824 824 825 825 if ($this->core->auth->userID()) { 826 826 $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; … … 829 829 } 830 830 } 831 831 832 832 #Adding parameters 833 833 if (isset($params['post_type'])) … … 841 841 $strReq .= "AND post_type = 'post' "; 842 842 } 843 843 844 844 if (isset($params['post_id']) && $params['post_id'] !== '') { 845 845 if (is_array($params['post_id'])) { … … 850 850 $strReq .= 'AND P.post_id '.$this->con->in($params['post_id']); 851 851 } 852 852 853 853 if (isset($params['exclude_post_id']) && $params['exclude_post_id'] !== '') { 854 854 if (is_array($params['exclude_post_id'])) { … … 859 859 $strReq .= 'AND P.post_id NOT '.$this->con->in($params['exclude_post_id']); 860 860 } 861 861 862 862 if (isset($params['post_url']) && $params['post_url'] !== '') { 863 863 $strReq .= "AND post_url = '".$this->con->escape($params['post_url'])."' "; 864 864 } 865 865 866 866 if (!empty($params['user_id'])) { 867 $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."'";868 } 869 867 $strReq .= "AND U.user_id ".$this->con->in($params['user_id'])." "; 868 } 869 870 870 if (isset($params['cat_id']) && $params['cat_id'] !== '') 871 871 { … … 888 888 $strReq .= 'AND '.$this->getPostsCategoryFilter($params['cat_url'],'cat_url').' '; 889 889 } 890 890 891 891 /* Other filters */ 892 892 if (isset($params['post_status'])) { 893 893 $strReq .= 'AND post_status = '.(integer) $params['post_status'].' '; 894 894 } 895 895 896 896 if (isset($params['post_selected'])) { 897 897 $strReq .= 'AND post_selected = '.(integer) $params['post_selected'].' '; 898 898 } 899 899 900 900 if (!empty($params['post_year'])) { 901 901 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y').' = '. 902 902 "'".sprintf('%04d',$params['post_year'])."' "; 903 903 } 904 904 905 905 if (!empty($params['post_month'])) { 906 906 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m').' = '. 907 907 "'".sprintf('%02d',$params['post_month'])."' "; 908 908 } 909 909 910 910 if (!empty($params['post_day'])) { 911 911 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d').' = '. 912 912 "'".sprintf('%02d',$params['post_day'])."' "; 913 913 } 914 914 915 915 if (!empty($params['post_lang'])) { 916 916 $strReq .= "AND P.post_lang = '".$this->con->escape($params['post_lang'])."' "; 917 917 } 918 918 919 919 if (!empty($params['search'])) 920 920 { 921 921 $words = text::splitWords($params['search']); 922 922 923 923 if (!empty($words)) 924 924 { … … 927 927 $this->core->callBehavior('corePostSearch',$this->core,array(&$words,&$strReq,&$params)); 928 928 } 929 929 930 930 if ($words) 931 931 { … … 937 937 } 938 938 } 939 939 940 940 if (isset($params['media'])) { 941 941 if ($params['media'] == '0') { 942 942 $strReq .= 'AND NOT '; 943 943 } else { 944 $strReq .= 'AND '; 944 $strReq .= 'AND '; 945 945 } 946 946 $strReq .= 'EXISTS (SELECT M.post_id FROM '.$this->prefix.'post_media M '. … … 966 966 967 967 if (!$count_only && !empty($params['limit'])) { 968 $strReq .= $this->con->limit($params['limit']);969 }970 968 $strReq .= $this->con->limit($params['limit']); 969 } 970 971 971 if (!empty($params['sql_only'])) { 972 972 return $strReq; … … 977 977 $rs->_nb_media = array(); 978 978 $rs->extend('rsExtPost'); 979 979 980 980 # --BEHAVIOR-- coreBlogGetPosts 981 981 $this->core->callBehavior('coreBlogGetPosts',$rs); 982 982 983 983 return $rs; 984 984 } 985 985 986 986 /** 987 987 Returns a record with post id, title and date for next or previous post 988 988 according to the post ID. 989 989 $dir could be 1 (next post) or -1 (previous post). 990 990 991 991 @param post_id <b>integer</b> Post ID 992 992 @param dir <b>integer</b> Search direction … … 999 999 $dt = $post->post_dt; 1000 1000 $post_id = (integer) $post->post_id; 1001 1001 1002 1002 if($dir > 0) { 1003 1003 $sign = '>'; … … 1008 1008 $order = 'DESC'; 1009 1009 } 1010 1010 1011 1011 $params['post_type'] = $post->post_type; 1012 1012 $params['limit'] = 1; … … 1017 1017 " OR post_dt ".$sign." '".$this->con->escape($dt)."' ". 1018 1018 ') '; 1019 1019 1020 1020 if ($restrict_to_category) { 1021 1021 $params['sql'] .= $post->cat_id ? 'AND P.cat_id = '.(integer) $post->cat_id.' ' : 'AND P.cat_id IS NULL '; 1022 1022 } 1023 1023 1024 1024 if ($restrict_to_lang) { 1025 1025 $params['sql'] .= $post->post_lang ? 'AND P.post_lang = \''. $this->con->escape($post->post_lang) .'\' ': 'AND P.post_lang IS NULL '; 1026 1026 } 1027 1027 1028 1028 $rs = $this->getPosts($params); 1029 1029 1030 1030 if ($rs->isEmpty()) { 1031 1031 return null; 1032 1032 } 1033 1033 1034 1034 return $rs; 1035 1035 } 1036 1036 1037 1037 /** 1038 1038 Retrieves different languages and post count on blog, based on post_lang 1039 1039 field. <var>$params</var> is an array taking the following optionnal 1040 1040 parameters: 1041 1041 1042 1042 - post_type: Get only entries with given type (default "post", '' for no type) 1043 1043 - lang: retrieve post count for selected lang 1044 1044 - order: order statement (default post_lang DESC) 1045 1045 1046 1046 @param params <b>array</b> Parameters 1047 1047 @return record … … 1054 1054 "AND post_lang <> '' ". 1055 1055 "AND post_lang IS NOT NULL "; 1056 1056 1057 1057 if (!$this->core->auth->check('contentadmin',$this->id)) { 1058 1058 $strReq .= 'AND ((post_status = 1 '; 1059 1059 1060 1060 if ($this->without_password) { 1061 1061 $strReq .= 'AND post_password IS NULL '; 1062 1062 } 1063 1063 $strReq .= ') '; 1064 1064 1065 1065 if ($this->core->auth->userID()) { 1066 1066 $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."')"; … … 1069 1069 } 1070 1070 } 1071 1071 1072 1072 if (isset($params['post_type'])) { 1073 1073 if ($params['post_type'] != '') { … … 1077 1077 $strReq .= "AND post_type = 'post' "; 1078 1078 } 1079 1079 1080 1080 if (isset($params['lang'])) { 1081 1081 $strReq .= "AND post_lang = '".$this->con->escape($params['lang'])."' "; 1082 1082 } 1083 1083 1084 1084 $strReq .= 'GROUP BY post_lang '; 1085 1085 1086 1086 $order = 'desc'; 1087 1087 if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) { … … 1089 1089 } 1090 1090 $strReq .= 'ORDER BY post_lang '.$order.' '; 1091 1091 1092 1092 return $this->con->select($strReq); 1093 1093 } 1094 1094 1095 1095 /** 1096 1096 Returns a record with all distinct blog dates and post count. 1097 1097 <var>$params</var> is an array taking the following optionnal parameters: 1098 1098 1099 1099 - type: (day|month|year) Get days, months or years 1100 1100 - year: (integer) Get dates for given year … … 1107 1107 - previous: Get date before match 1108 1108 - order: Sort by date "ASC" or "DESC" 1109 1109 1110 1110 @param params <b>array</b> Parameters array 1111 1111 @return record … … 1126 1126 $dt_f .= ' 00:00:00'; 1127 1127 $dt_fc .= '000000'; 1128 1128 1129 1129 $cat_field = $catReq = $limit = ''; 1130 1130 1131 1131 if (isset($params['cat_id']) && $params['cat_id'] !== '') { 1132 1132 $catReq = 'AND P.cat_id = '.(integer) $params['cat_id'].' '; … … 1139 1139 $catReq = 'AND P.post_lang = \''. $params['post_lang'].'\' '; 1140 1140 } 1141 1141 1142 1142 $strReq = 'SELECT DISTINCT('.$this->con->dateFormat('post_dt',$dt_f).') AS dt '. 1143 1143 $cat_field. … … 1147 1147 "WHERE P.blog_id = '".$this->con->escape($this->id)."' ". 1148 1148 $catReq; 1149 1149 1150 1150 if (!$this->core->auth->check('contentadmin',$this->id)) { 1151 1151 $strReq .= 'AND ((post_status = 1 '; 1152 1152 1153 1153 if ($this->without_password) { 1154 1154 $strReq .= 'AND post_password IS NULL '; 1155 1155 } 1156 1156 $strReq .= ') '; 1157 1157 1158 1158 if ($this->core->auth->userID()) { 1159 1159 $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; … … 1162 1162 } 1163 1163 } 1164 1164 1165 1165 if (!empty($params['post_type'])) { 1166 1166 $strReq .= "AND post_type ".$this->con->in($params['post_type'])." "; … … 1168 1168 $strReq .= "AND post_type = 'post' "; 1169 1169 } 1170 1170 1171 1171 if (!empty($params['year'])) { 1172 1172 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y')." = '".sprintf('%04d',$params['year'])."' "; 1173 1173 } 1174 1174 1175 1175 if (!empty($params['month'])) { 1176 1176 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m')." = '".sprintf('%02d',$params['month'])."' "; 1177 1177 } 1178 1178 1179 1179 if (!empty($params['day'])) { 1180 1180 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d')." = '".sprintf('%02d',$params['day'])."' "; 1181 1181 } 1182 1182 1183 1183 # Get next or previous date 1184 1184 if (!empty($params['next']) || !empty($params['previous'])) … … 1193 1193 $dt = $params['previous']; 1194 1194 } 1195 1195 1196 1196 $dt = date('YmdHis',strtotime($dt)); 1197 1197 1198 1198 $strReq .= 'AND '.$this->con->dateFormat('post_dt',$dt_fc).$pdir."'".$dt."' "; 1199 1199 $limit = $this->con->limit(1); 1200 1200 } 1201 1201 1202 1202 $strReq .= 'GROUP BY dt '.$cat_field; 1203 1203 1204 1204 $order = 'desc'; 1205 1205 if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) { 1206 1206 $order = $params['order']; 1207 1207 } 1208 1208 1209 1209 $strReq .= 1210 1210 'ORDER BY dt '.$order.' '. 1211 1211 $limit; 1212 1212 1213 1213 $rs = $this->con->select($strReq); 1214 1214 $rs->extend('rsExtDates'); 1215 1215 return $rs; 1216 1216 } 1217 1217 1218 1218 /** 1219 1219 Creates a new entry. Takes a cursor as input and returns the new entry 1220 1220 ID. 1221 1221 1222 1222 @param cur <b>cursor</b> Post cursor 1223 1223 @return <b>integer</b> New post ID … … 1228 1228 throw new Exception(__('You are not allowed to create an entry')); 1229 1229 } 1230 1230 1231 1231 $this->con->writeLock($this->prefix.'post'); 1232 1232 try … … 1235 1235 $rs = $this->con->select( 1236 1236 'SELECT MAX(post_id) '. 1237 'FROM '.$this->prefix.'post ' 1237 'FROM '.$this->prefix.'post ' 1238 1238 ); 1239 1239 1240 1240 $cur->post_id = (integer) $rs->f(0) + 1; 1241 1241 $cur->blog_id = (string) $this->id; … … 1243 1243 $cur->post_upddt = date('Y-m-d H:i:s'); 1244 1244 $cur->post_tz = $this->core->auth->getInfo('user_tz'); 1245 1245 1246 1246 # Post excerpt and content 1247 1247 $this->getPostContent($cur,$cur->post_id); 1248 1248 1249 1249 $this->getPostCursor($cur); 1250 1250 1251 1251 $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$cur->post_id); 1252 1252 1253 1253 if (!$this->core->auth->check('publish,contentadmin',$this->id)) { 1254 1254 $cur->post_status = -2; 1255 1255 } 1256 1256 1257 1257 # --BEHAVIOR-- coreBeforePostCreate 1258 1258 $this->core->callBehavior('coreBeforePostCreate',$this,$cur); 1259 1259 1260 1260 $cur->insert(); 1261 1261 $this->con->unlock(); … … 1266 1266 throw $e; 1267 1267 } 1268 1268 1269 1269 # --BEHAVIOR-- coreAfterPostCreate 1270 1270 $this->core->callBehavior('coreAfterPostCreate',$this,$cur); 1271 1271 1272 1272 $this->triggerBlog(); 1273 1273 1274 1274 return $cur->post_id; 1275 1275 } 1276 1276 1277 1277 /** 1278 1278 Updates an existing post. 1279 1279 1280 1280 @param id <b>integer</b> Post ID 1281 1281 @param cur <b>cursor</b> Post cursor … … 1286 1286 throw new Exception(__('You are not allowed to update entries')); 1287 1287 } 1288 1288 1289 1289 $id = (integer) $id; 1290 1290 1291 1291 if (empty($id)) { 1292 1292 throw new Exception(__('No such entry ID')); 1293 1293 } 1294 1294 1295 1295 # Post excerpt and content 1296 1296 $this->getPostContent($cur,$id); 1297 1297 1298 1298 $this->getPostCursor($cur); 1299 1299 1300 1300 if ($cur->post_url !== null) { 1301 1301 $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$id); 1302 1302 } 1303 1303 1304 1304 if (!$this->core->auth->check('publish,contentadmin',$this->id)) { 1305 1305 $cur->unsetField('post_status'); 1306 1306 } 1307 1307 1308 1308 $cur->post_upddt = date('Y-m-d H:i:s'); 1309 1309 1310 1310 #If user is only "usage", we need to check the post's owner 1311 1311 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1315 1315 'WHERE post_id = '.$id.' '. 1316 1316 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1317 1317 1318 1318 $rs = $this->con->select($strReq); 1319 1319 1320 1320 if ($rs->isEmpty()) { 1321 1321 throw new Exception(__('You are not allowed to edit this entry')); 1322 1322 } 1323 1323 } 1324 1324 1325 1325 # --BEHAVIOR-- coreBeforePostUpdate 1326 1326 $this->core->callBehavior('coreBeforePostUpdate',$this,$cur); 1327 1327 1328 1328 $cur->update('WHERE post_id = '.$id.' '); 1329 1329 1330 1330 # --BEHAVIOR-- coreAfterPostUpdate 1331 1331 $this->core->callBehavior('coreAfterPostUpdate',$this,$cur); 1332 1332 1333 1333 $this->triggerBlog(); 1334 1334 } 1335 1335 1336 1336 /** 1337 1337 Updates post status. 1338 1338 1339 1339 @param id <b>integer</b> Post ID 1340 1340 @param status <b>integer</b> Post status … … 1344 1344 $this->updPostsStatus($id,$status); 1345 1345 } 1346 1346 1347 1347 /** 1348 1348 Updates posts status. 1349 1349 1350 1350 @param ids <b>mixed</b> Post(s) ID(s) 1351 1351 @param status <b>integer</b> Post status … … 1356 1356 throw new Exception(__('You are not allowed to change this entry status')); 1357 1357 } 1358 1358 1359 1359 $posts_ids = dcUtils::cleanIds($ids); 1360 1360 $status = (integer) $status; 1361 1361 1362 1362 $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". 1363 1363 "AND post_id ".$this->con->in($posts_ids); 1364 1364 1365 1365 #If user can only publish, we need to check the post's owner 1366 1366 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1368 1368 $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1369 1369 } 1370 1370 1371 1371 $cur = $this->con->openCursor($this->prefix.'post'); 1372 1372 1373 1373 $cur->post_status = $status; 1374 1374 $cur->post_upddt = date('Y-m-d H:i:s'); 1375 1375 1376 1376 $cur->update($strReq); 1377 1377 $this->triggerBlog(); 1378 1378 } 1379 1379 1380 1380 /** 1381 1381 Updates post selection. 1382 1382 1383 1383 @param id <b>integer</b> Post ID 1384 1384 @param selected <b>integer</b> Is selected post … … 1388 1388 $this->updPostsSelected($id,$selected); 1389 1389 } 1390 1390 1391 1391 /** 1392 1392 Updates posts selection. 1393 1393 1394 1394 @param ids <b>mixed</b> Post(s) ID(s) 1395 1395 @param selected <b>integer</b> Is selected post(s) … … 1400 1400 throw new Exception(__('You are not allowed to change this entry category')); 1401 1401 } 1402 1402 1403 1403 $posts_ids = dcUtils::cleanIds($ids); 1404 1404 $selected = (boolean) $selected; 1405 1405 1406 1406 $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". 1407 1407 "AND post_id ".$this->con->in($posts_ids); 1408 1408 1409 1409 # If user is only usage, we need to check the post's owner 1410 1410 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1412 1412 $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1413 1413 } 1414 1414 1415 1415 $cur = $this->con->openCursor($this->prefix.'post'); 1416 1416 1417 1417 $cur->post_selected = (integer) $selected; 1418 1418 $cur->post_upddt = date('Y-m-d H:i:s'); 1419 1419 1420 1420 $cur->update($strReq); 1421 1421 $this->triggerBlog(); 1422 1422 } 1423 1423 1424 1424 /** 1425 1425 Updates post category. <var>$cat_id</var> can be null. 1426 1426 1427 1427 @param id <b>integer</b> Post ID 1428 1428 @param cat_id <b>integer</b> Category ID … … 1432 1432 $this->updPostsCategory($id,$cat_id); 1433 1433 } 1434 1434 1435 1435 /** 1436 1436 Updates posts category. <var>$cat_id</var> can be null. 1437 1437 1438 1438 @param ids <b>mixed</b> Post(s) ID(s) 1439 1439 @param cat_id <b>integer</b> Category ID … … 1444 1444 throw new Exception(__('You are not allowed to change this entry category')); 1445 1445 } 1446 1446 1447 1447 $posts_ids = dcUtils::cleanIds($ids); 1448 1448 $cat_id = (integer) $cat_id; 1449 1449 1450 1450 $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". 1451 1451 "AND post_id ".$this->con->in($posts_ids); 1452 1452 1453 1453 # If user is only usage, we need to check the post's owner 1454 1454 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1456 1456 $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1457 1457 } 1458 1458 1459 1459 $cur = $this->con->openCursor($this->prefix.'post'); 1460 1460 1461 1461 $cur->cat_id = ($cat_id ? $cat_id : null); 1462 1462 $cur->post_upddt = date('Y-m-d H:i:s'); 1463 1463 1464 1464 $cur->update($strReq); 1465 1465 $this->triggerBlog(); 1466 1466 } 1467 1467 1468 1468 /** 1469 1469 Updates posts category. <var>$new_cat_id</var> can be null. 1470 1470 1471 1471 @param old_cat_id <b>integer</b> Old category ID 1472 1472 @param new_cat_id <b>integer</b> New category ID … … 1477 1477 throw new Exception(__('You are not allowed to change entries category')); 1478 1478 } 1479 1479 1480 1480 $old_cat_id = (integer) $old_cat_id; 1481 1481 $new_cat_id = (integer) $new_cat_id; 1482 1482 1483 1483 $cur = $this->con->openCursor($this->prefix.'post'); 1484 1484 1485 1485 $cur->cat_id = ($new_cat_id ? $new_cat_id : null); 1486 1486 $cur->post_upddt = date('Y-m-d H:i:s'); 1487 1487 1488 1488 $cur->update( 1489 1489 'WHERE cat_id = '.$old_cat_id.' '. … … 1492 1492 $this->triggerBlog(); 1493 1493 } 1494 1494 1495 1495 /** 1496 1496 Deletes a post. 1497 1497 1498 1498 @param id <b>integer</b> Post ID 1499 1499 */ … … 1502 1502 $this->delPosts($id); 1503 1503 } 1504 1504 1505 1505 /** 1506 1506 Deletes multiple posts. 1507 1507 1508 1508 @param ids <b>mixed</b> Post(s) ID(s) 1509 1509 */ … … 1513 1513 throw new Exception(__('You are not allowed to delete entries')); 1514 1514 } 1515 1515 1516 1516 $posts_ids = dcUtils::cleanIds($ids); 1517 1517 1518 1518 if (empty($posts_ids)) { 1519 1519 throw new Exception(__('No such entry ID')); 1520 1520 } 1521 1521 1522 1522 $strReq = 'DELETE FROM '.$this->prefix.'post '. 1523 1523 "WHERE blog_id = '".$this->con->escape($this->id)."' ". 1524 1524 "AND post_id ".$this->con->in($posts_ids); 1525 1525 1526 1526 #If user can only delete, we need to check the post's owner 1527 1527 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1529 1529 $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1530 1530 } 1531 1531 1532 1532 $this->con->execute($strReq); 1533 1533 $this->triggerBlog(); 1534 1534 } 1535 1535 1536 1536 /** 1537 1537 Publishes all entries flaged as "scheduled". … … 1543 1543 'WHERE post_status = -1 '. 1544 1544 "AND blog_id = '".$this->con->escape($this->id)."' "; 1545 1545 1546 1546 $rs = $this->con->select($strReq); 1547 1547 1548 1548 $now = dt::toUTC(time()); 1549 1549 $to_change = new ArrayObject(); … … 1552 1552 return; 1553 1553 } 1554 1554 1555 1555 while ($rs->fetch()) 1556 1556 { 1557 1557 # Now timestamp with post timezone 1558 1558 $now_tz = $now + dt::getTimeOffset($rs->post_tz,$now); 1559 1559 1560 1560 # Post timestamp 1561 1561 $post_ts = strtotime($rs->post_dt); 1562 1562 1563 1563 # If now_tz >= post_ts, we publish the entry 1564 1564 if ($now_tz >= $post_ts) { … … 1582 1582 $this->core->callBehavior('coreAfterScheduledEntriesPublish',$this,$to_change); 1583 1583 } 1584 1585 } 1586 1584 1585 } 1586 1587 1587 /** 1588 1588 Retrieves all users having posts on current blog. 1589 1589 1590 1590 @param post_type <b>string</b> post_type filter (post) 1591 1591 @return record … … 1598 1598 'WHERE P.user_id = U.user_id '. 1599 1599 "AND blog_id = '".$this->con->escape($this->id)."' "; 1600 1600 1601 1601 if ($post_type) { 1602 1602 $strReq .= "AND post_type = '".$this->con->escape($post_type)."' "; 1603 1603 } 1604 1604 1605 1605 $strReq .= 'GROUP BY P.user_id, user_name, user_firstname, user_displayname, user_email '; 1606 1606 1607 1607 return $this->con->select($strReq); 1608 1608 } 1609 1609 1610 1610 private function getPostsCategoryFilter($arr,$field='cat_id') 1611 1611 { 1612 1612 $field = $field == 'cat_id' ? 'cat_id' : 'cat_url'; 1613 1613 1614 1614 $sub = array(); 1615 1615 $not = array(); 1616 1616 $queries = array(); 1617 1617 1618 1618 foreach ($arr as $v) 1619 1619 { … … 1622 1622 $id = array_shift($args); 1623 1623 $args = array_flip($args); 1624 1624 1625 1625 if (isset($args['not'])) { $not[$id] = 1; } 1626 1626 if (isset($args['sub'])) { $sub[$id] = 1; } … … 1636 1636 } 1637 1637 } 1638 1638 1639 1639 if (!empty($sub)) { 1640 1640 $rs = $this->con->select( … … 1643 1643 'AND '.$field.' '.$this->con->in(array_keys($sub)) 1644 1644 ); 1645 1645 1646 1646 while ($rs->fetch()) { 1647 1647 $queries[$rs->f($field)] = '(C.cat_lft BETWEEN '.$rs->cat_lft.' AND '.$rs->cat_rgt.')'; 1648 1648 } 1649 1649 } 1650 1650 1651 1651 # Create queries 1652 1652 $sql = array( … … 1654 1654 1 => array() # excluded categories 1655 1655 ); 1656 1656 1657 1657 foreach ($queries as $id => $q) { 1658 1658 $sql[(integer) isset($not[$id])][] = $q; 1659 1659 } 1660 1660 1661 1661 $sql[0] = implode(' OR ',$sql[0]); 1662 1662 $sql[1] = implode(' OR ',$sql[1]); 1663 1663 1664 1664 if ($sql[0]) { 1665 1665 $sql[0] = '('.$sql[0].')'; … … 1667 1667 unset($sql[0]); 1668 1668 } 1669 1669 1670 1670 if ($sql[1]) { 1671 1671 $sql[1] = '(P.cat_id IS NULL OR NOT('.$sql[1].'))'; … … 1673 1673 unset($sql[1]); 1674 1674 } 1675 1675 1676 1676 return implode(' AND ',$sql); 1677 1677 } 1678 1678 1679 1679 private function getPostCursor($cur,$post_id=null) 1680 1680 { … … 1682 1682 throw new Exception(__('No entry title')); 1683 1683 } 1684 1684 1685 1685 if ($cur->post_content == '') { 1686 1686 throw new Exception(__('No entry content')); 1687 1687 } 1688 1688 1689 1689 if ($cur->post_password === '') { 1690 1690 $cur->post_password = null; 1691 1691 } 1692 1692 1693 1693 if ($cur->post_dt == '') { 1694 1694 $offset = dt::getTimeOffset($this->core->auth->getInfo('user_tz')); … … 1696 1696 $cur->post_dt = date('Y-m-d H:i:00',$now); 1697 1697 } 1698 1698 1699 1699 $post_id = is_int($post_id) ? $post_id : $cur->post_id; 1700 1700 1701 1701 if ($cur->post_content_xhtml == '') { 1702 1702 throw new Exception(__('No entry content')); 1703 1703 } 1704 1704 1705 1705 # Words list 1706 1706 if ($cur->post_title !== null && $cur->post_excerpt_xhtml !== null … … 1711 1711 $cur->post_excerpt_xhtml.' '. 1712 1712 $cur->post_content_xhtml; 1713 1713 1714 1714 $cur->post_words = implode(' ',text::splitWords($words)); 1715 1715 } 1716 1716 } 1717 1717 1718 1718 private function getPostContent($cur,$post_id) 1719 1719 { … … 1722 1722 $post_content = $cur->post_content; 1723 1723 $post_content_xhtml = $cur->post_content_xhtml; 1724 1724 1725 1725 $this->setPostContent( 1726 1726 $post_id,$cur->post_format,$cur->post_lang, … … 1728 1728 $post_content,$post_content_xhtml 1729 1729 ); 1730 1730 1731 1731 $cur->post_excerpt = $post_excerpt; 1732 1732 $cur->post_excerpt_xhtml = $post_excerpt_xhtml; … … 1734 1734 $cur->post_content_xhtml = $post_content_xhtml; 1735 1735 } 1736 1736 1737 1737 /** 1738 1738 Creates post HTML content, taking format and lang into account. 1739 1739 1740 1740 @param post_id <b>integer</b> Post ID 1741 1741 @param format <b>string</b> Post format … … 1771 1771 } 1772 1772 } 1773 1773 1774 1774 if ($excerpt) { 1775 1775 $excerpt_xhtml = $this->core->callFormater($format,$excerpt); … … 1778 1778 $excerpt_xhtml = ''; 1779 1779 } 1780 1780 1781 1781 if ($content) { 1782 1782 $content_xhtml = $this->core->callFormater($format,$content); … … 1785 1785 $content_xhtml = ''; 1786 1786 } 1787 1787 1788 1788 # --BEHAVIOR-- coreAfterPostContentFormat 1789 1789 $this->core->callBehavior('coreAfterPostContentFormat',array( … … 1794 1794 )); 1795 1795 } 1796 1796 1797 1797 /** 1798 1798 Returns URL for a post according to blog setting <var>post_url_format</var>. 1799 1799 It will try to guess URL and append some figures if needed. 1800 1800 1801 1801 @param url <b>string</b> Origin URL, could be empty 1802 1802 @param post_dt <b>string</b> Post date (in YYYY-MM-DD HH:mm:ss) … … 1808 1808 { 1809 1809 $url = trim($url); 1810 1810 1811 1811 $url_patterns = array( 1812 1812 '{y}' => date('Y',strtotime($post_dt)), … … 1816 1816 '{id}' => (integer) $post_id 1817 1817 ); 1818 1818 1819 1819 # If URL is empty, we create a new one 1820 1820 if ($url == '') … … 1831 1831 $url = text::tidyURL($url); 1832 1832 } 1833 1833 1834 1834 # Let's check if URL is taken... 1835 1835 $strReq = 'SELECT post_url FROM '.$this->prefix.'post '. … … 1838 1838 "AND blog_id = '".$this->con->escape($this->id)."' ". 1839 1839 'ORDER BY post_url DESC'; 1840 1840 1841 1841 $rs = $this->con->select($strReq); 1842 1842 1843 1843 if (!$rs->isEmpty()) 1844 1844 { … … 1855 1855 "AND blog_id = '".$this->con->escape($this->id)."' ". 1856 1856 'ORDER BY post_url DESC '; 1857 1857 1858 1858 $rs = $this->con->select($strReq); 1859 1859 $a = array(); … … 1861 1861 $a[] = $rs->post_url; 1862 1862 } 1863 1863 1864 1864 natsort($a); 1865 1865 $t_url = end($a); 1866 1866 1867 1867 if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) { 1868 1868 $i = (integer) $m[2]; … … 1871 1871 $i = 1; 1872 1872 } 1873 1873 1874 1874 return $url.($i+1); 1875 1875 } 1876 1876 1877 1877 # URL is empty? 1878 1878 if ($url == '') { 1879 1879 throw new Exception(__('Empty entry URL')); 1880 1880 } 1881 1881 1882 1882 return $url; 1883 1883 } 1884 1884 //@} 1885 1885 1886 1886 /// @name Comments management methods 1887 1887 //@{ … … 1889 1889 Retrieves comments. <b>$params</b> is an array taking the following 1890 1890 optionnal parameters: 1891 1891 1892 1892 - no_content: Don't retrieve comment content 1893 - post_type: Get only entries with given type (default no type, array for many types) 1893 - post_type: Get only entries with given type (default no type, array for many types) 1894 1894 - post_id: (integer) Get comments belonging to given post_id 1895 1895 - cat_id: (integer or array) Get comments belonging to entries of given category ID … … 1907 1907 - limit: Limit parameter 1908 1908 - sql_only : return the sql request instead of results. Only ids are selected 1909 1909 1910 1910 @param params <b>array</b> Parameters 1911 1911 @param count_only <b>boolean</b> Only counts results … … 1918 1918 $strReq = 'SELECT count(comment_id) '; 1919 1919 } 1920 elseif (!empty($params['sql_only'])) 1920 elseif (!empty($params['sql_only'])) 1921 1921 { 1922 1922 $strReq = 'SELECT P.post_id '; … … 1929 1929 $content_req = 'comment_content, '; 1930 1930 } 1931 1931 1932 1932 if (!empty($params['columns']) && is_array($params['columns'])) { 1933 1933 $content_req .= implode(', ',$params['columns']).', '; 1934 1934 } 1935 1935 1936 1936 $strReq = 1937 1937 'SELECT C.comment_id, comment_dt, comment_tz, comment_upddt, '. … … 1942 1942 'P.post_dt, P.user_id, U.user_email, U.user_url '; 1943 1943 } 1944 1944 1945 1945 $strReq .= 1946 1946 'FROM '.$this->prefix.'comment C '. 1947 1947 'INNER JOIN '.$this->prefix.'post P ON C.post_id = P.post_id '. 1948 1948 'INNER JOIN '.$this->prefix.'user U ON P.user_id = U.user_id '; 1949 1949 1950 1950 if (!empty($params['from'])) { 1951 1951 $strReq .= $params['from'].' '; 1952 1952 } 1953 1953 1954 1954 $strReq .= 1955 1955 "WHERE P.blog_id = '".$this->con->escape($this->id)."' "; 1956 1956 1957 1957 if (!$this->core->auth->check('contentadmin',$this->id)) { 1958 1958 $strReq .= 'AND ((comment_status = 1 AND P.post_status = 1 '; 1959 1959 1960 1960 if ($this->without_password) { 1961 1961 $strReq .= 'AND post_password IS NULL '; 1962 1962 } 1963 1963 $strReq .= ') '; 1964 1964 1965 1965 if ($this->core->auth->userID()) { 1966 1966 $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; … … 1969 1969 } 1970 1970 } 1971 1971 1972 1972 if (!empty($params['post_type'])) 1973 1973 { 1974 1974 $strReq .= 'AND post_type '.$this->con->in($params['post_type']); 1975 1975 } 1976 1976 1977 1977 if (isset($params['post_id']) && $params['post_id'] !== '') { 1978 1978 $strReq .= 'AND P.post_id = '.(integer) $params['post_id'].' '; 1979 1979 } 1980 1980 1981 1981 if (isset($params['cat_id']) && $params['cat_id'] !== '') { 1982 1982 $strReq .= 'AND P.cat_id = '.(integer) $params['cat_id'].' '; 1983 1983 } 1984 1984 1985 1985 if (isset($params['comment_id']) && $params['comment_id'] !== '') { 1986 1986 if (is_array($params['comment_id'])) { … … 1991 1991 $strReq .= 'AND comment_id '.$this->con->in($params['comment_id']); 1992 1992 } 1993 1993 1994 1994 if (isset($params['comment_site'])) { 1995 1995 $comment_site = $this->con->escape(str_replace('*','%',$params['comment_site'])); 1996 1996 $strReq .= "AND comment_site LIKE '".$comment_site."' "; 1997 1997 } 1998 1998 1999 1999 if (isset($params['comment_status'])) { 2000 2000 $strReq .= 'AND comment_status = '.(integer) $params['comment_status'].' '; 2001 2001 } 2002 2002 2003 2003 if (!empty($params['comment_status_not'])) 2004 2004 { 2005 2005 $strReq .= 'AND comment_status <> '.(integer) $params['comment_status_not'].' '; 2006 2006 } 2007 2007 2008 2008 if (isset($params['comment_trackback'])) { 2009 2009 $strReq .= 'AND comment_trackback = '.(integer) (boolean) $params['comment_trackback'].' '; 2010 2010 } 2011 2011 2012 2012 if (isset($params['comment_ip'])) { 2013 2013 $comment_ip = $this->con->escape(str_replace('*','%',$params['comment_ip'])); 2014 2014 $strReq .= "AND comment_ip LIKE '".$comment_ip."' "; 2015 2015 } 2016 2016 2017 2017 if (isset($params['q_author'])) { 2018 2018 $q_author = $this->con->escape(str_replace('*','%',strtolower($params['q_author']))); 2019 2019 $strReq .= "AND LOWER(comment_author) LIKE '".$q_author."' "; 2020 2020 } 2021 2021 2022 2022 if (!empty($params['search'])) 2023 2023 { 2024 2024 $words = text::splitWords($params['search']); 2025 2025 2026 2026 if (!empty($words)) 2027 2027 { … … 2030 2030 $this->core->callBehavior('coreCommentSearch',$this->core,array(&$words,&$strReq,&$params)); 2031 2031 } 2032 2032 2033 2033 if ($words) 2034 2034 { … … 2040 2040 } 2041 2041 } 2042 2042 2043 2043 if (!empty($params['sql'])) { 2044 2044 $strReq .= $params['sql'].' '; 2045 2045 } 2046 2046 2047 2047 if (!$count_only) 2048 2048 { … … 2053 2053 } 2054 2054 } 2055 2055 2056 2056 if (!$count_only && !empty($params['limit'])) { 2057 2057 $strReq .= $this->con->limit($params['limit']); … … 2061 2061 return $strReq; 2062 2062 } 2063 2063 2064 2064 $rs = $this->con->select($strReq); 2065 2065 $rs->core = $this->core; 2066 2066 $rs->extend('rsExtComment'); 2067 2067 2068 2068 # --BEHAVIOR-- coreBlogGetComments 2069 2069 $this->core->callBehavior('coreBlogGetComments',$rs); 2070 2070 2071 2071 return $rs; 2072 2072 } 2073 2073 2074 2074 /** 2075 2075 Creates a new comment. Takes a cursor as input and returns the new comment 2076 2076 ID. 2077 2077 2078 2078 @param cur <b>cursor</b> Comment cursor 2079 2079 @return <b>integer</b> New comment ID … … 2087 2087 $rs = $this->con->select( 2088 2088 'SELECT MAX(comment_id) '. 2089 'FROM '.$this->prefix.'comment ' 2089 'FROM '.$this->prefix.'comment ' 2090 2090 ); 2091 2091 2092 2092 $cur->comment_id = (integer) $rs->f(0) + 1; 2093 2093 $cur->comment_upddt = date('Y-m-d H:i:s'); 2094 2094 2095 2095 $offset = dt::getTimeOffset($this->settings->system->blog_timezone); 2096 2096 $cur->comment_dt = date('Y-m-d H:i:s',time() + $offset); 2097 2097 $cur->comment_tz = $this->settings->system->blog_timezone; 2098 2098 2099 2099 $this->getCommentCursor($cur); 2100 2100 2101 2101 if ($cur->comment_ip === null) { 2102 2102 $cur->comment_ip = http::realIP(); 2103 2103 } 2104 2104 2105 2105 # --BEHAVIOR-- coreBeforeCommentCreate 2106 2106 $this->core->callBehavior('coreBeforeCommentCreate',$this,$cur); 2107 2107 2108 2108 $cur->insert(); 2109 2109 $this->con->unlock(); … … 2114 2114 throw $e; 2115 2115 } 2116 2116 2117 2117 # --BEHAVIOR-- coreAfterCommentCreate 2118 2118 $this->core->callBehavior('coreAfterCommentCreate',$this,$cur); 2119 2119 2120 2120 $this->triggerComment($cur->comment_id); 2121 2121 if ($cur->comment_status != -2) { 2122 2122 $this->triggerBlog(); 2123 } 2123 } 2124 2124 return $cur->comment_id; 2125 2125 } 2126 2126 2127 2127 /** 2128 2128 Updates an existing comment. 2129 2129 2130 2130 @param id <b>integer</b> Comment ID 2131 2131 @param cur <b>cursor</b> Comment cursor … … 2136 2136 throw new Exception(__('You are not allowed to update comments')); 2137 2137 } 2138 2138 2139 2139 $id = (integer) $id; 2140 2140 2141 2141 if (empty($id)) { 2142 2142 throw new Exception(__('No such comment ID')); 2143 2143 } 2144 2144 2145 2145 $rs = $this->getComments(array('comment_id' => $id)); 2146 2146 2147 2147 if ($rs->isEmpty()) { 2148 2148 throw new Exception(__('No such comment ID')); 2149 2149 } 2150 2150 2151 2151 #If user is only usage, we need to check the post's owner 2152 2152 if (!$this->core->auth->check('contentadmin',$this->id)) … … 2156 2156 } 2157 2157 } 2158 2158 2159 2159 $this->getCommentCursor($cur); 2160 2160 2161 2161 $cur->comment_upddt = date('Y-m-d H:i:s'); 2162 2162 2163 2163 if (!$this->core->auth->check('publish,contentadmin',$this->id)) { 2164 2164 $cur->unsetField('comment_status'); 2165 2165 } 2166 2166 2167 2167 # --BEHAVIOR-- coreBeforeCommentUpdate 2168 2168 $this->core->callBehavior('coreBeforeCommentUpdate',$this,$cur,$rs); 2169 2169 2170 2170 $cur->update('WHERE comment_id = '.$id.' '); 2171 2171 2172 2172 # --BEHAVIOR-- coreAfterCommentUpdate 2173 2173 $this->core->callBehavior('coreAfterCommentUpdate',$this,$cur,$rs); 2174 2174 2175 2175 $this->triggerComment($id); 2176 2176 $this->triggerBlog(); 2177 2177 } 2178 2178 2179 2179 /** 2180 2180 Updates comment status. 2181 2181 2182 2182 @param id <b>integer</b> Comment ID 2183 2183 @param status <b>integer</b> Comment status … … 2187 2187 $this->updCommentsStatus($id,$status); 2188 2188 } 2189 2189 2190 2190 /** 2191 2191 Updates comments status. 2192 2192 2193 2193 @param ids <b>mixed</b> Comment(s) ID(s) 2194 2194 @param status <b>integer</b> Comment status … … 2199 2199 throw new Exception(__("You are not allowed to change this comment's status")); 2200 2200 } 2201 2201 2202 2202 $co_ids = dcUtils::cleanIds($ids); 2203 2203 $status = (integer) $status; 2204 2205 $strReq = 2204 2205 $strReq = 2206 2206 'UPDATE '.$this->prefix.'comment tc '; 2207 2207 2208 2208 # mySQL uses "JOIN" synthax 2209 2209 if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { 2210 $strReq .= 2210 $strReq .= 2211 2211 'JOIN '.$this->prefix.'post tp ON tc.post_id = tp.post_id '; 2212 2212 } 2213 2214 $strReq .= 2213 2214 $strReq .= 2215 2215 'SET comment_status = '.$status.' '; 2216 2216 2217 2217 # pgSQL uses "FROM" synthax 2218 2218 if ($this->con->driver() != 'mysql' && $this->con->driver() != 'mysqli') { 2219 $strReq .= 2219 $strReq .= 2220 2220 'FROM '.$this->prefix.'post tp '; 2221 2221 } 2222 2222 2223 2223 $strReq .= 2224 2224 "WHERE blog_id = '".$this->con->escape($this->id)."' ". 2225 2225 'AND comment_id'.$this->con->in($co_ids); 2226 2226 2227 2227 # add pgSQL "WHERE" clause 2228 2228 if ($this->con->driver() != 'mysql' && $this->con->driver() != 'mysqli') { 2229 $strReq .= 2229 $strReq .= 2230 2230 'AND tc.post_id = tp.post_id '; 2231 2231 } 2232 2232 2233 2233 #If user is only usage, we need to check the post's owner 2234 2234 if (!$this->core->auth->check('contentadmin',$this->id)) 2235 2235 { 2236 $strReq .= 2236 $strReq .= 2237 2237 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 2238 2238 } 2239 2239 2240 2240 $this->con->execute($strReq); 2241 2241 $this->triggerComments($co_ids); 2242 2242 $this->triggerBlog(); 2243 2243 } 2244 2244 2245 2245 /** 2246 2246 Delete a comment 2247 2247 2248 2248 @param id <b>integer</b> Comment ID 2249 2249 */ … … 2252 2252 $this->delComments($id); 2253 2253 } 2254 2254 2255 2255 /** 2256 2256 Delete comments 2257 2257 2258 2258 @param ids <b>mixed</b> Comment(s) ID(s) 2259 2259 */ … … 2263 2263 throw new Exception(__('You are not allowed to delete comments')); 2264 2264 } 2265 2265 2266 2266 $co_ids = dcUtils::cleanIds($ids); 2267 2267 2268 2268 if (empty($co_ids)) { 2269 2269 throw new Exception(__('No such comment ID')); 2270 2270 } 2271 2271 2272 2272 # Retrieve posts affected by comments edition 2273 2273 $affected_posts = array(); … … 2277 2277 'WHERE comment_id'.$this->con->in($co_ids). 2278 2278 'GROUP BY post_id'; 2279 2279 2280 2280 $rs = $this->con->select($strReq); 2281 2281 2282 2282 while ($rs->fetch()) { 2283 2283 $affected_posts[] = (integer) $rs->post_id; 2284 2284 } 2285 2285 2286 2286 # mySQL uses "INNER JOIN" synthax 2287 2287 if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { 2288 $strReq = 2288 $strReq = 2289 2289 'DELETE FROM tc '. 2290 2290 'USING '.$this->prefix.'comment tc '. … … 2293 2293 # pgSQL uses nothing special 2294 2294 else { 2295 $strReq = 2295 $strReq = 2296 2296 'DELETE FROM '.$this->prefix.'comment tc '. 2297 2297 'USING '.$this->prefix.'post tp '; 2298 2298 } 2299 2300 $strReq .= 2299 2300 $strReq .= 2301 2301 'WHERE tc.post_id = tp.post_id '. 2302 2302 "AND tp.blog_id = '".$this->con->escape($this->id)."' ". 2303 2303 'AND comment_id'.$this->con->in($co_ids); 2304 2304 2305 2305 #If user can only delete, we need to check the post's owner 2306 2306 if (!$this->core->auth->check('contentadmin',$this->id)) 2307 2307 { 2308 $strReq .= 2308 $strReq .= 2309 2309 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 2310 2310 } 2311 2311 2312 2312 $this->con->execute($strReq); 2313 2313 $this->triggerComments($co_ids, true, $affected_posts); … … 2320 2320 throw new Exception(__('You are not allowed to delete comments')); 2321 2321 } 2322 2322 2323 2323 # mySQL uses "INNER JOIN" synthax 2324 2324 if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { 2325 $strReq = 2325 $strReq = 2326 2326 'DELETE FROM tc '. 2327 2327 'USING '.$this->prefix.'comment tc '. … … 2330 2330 # pgSQL uses nothing special 2331 2331 else { 2332 $strReq = 2332 $strReq = 2333 2333 'DELETE FROM '.$this->prefix.'comment tc '. 2334 2334 'USING '.$this->prefix.'post tp '; 2335 2335 } 2336 2337 $strReq .= 2336 2337 $strReq .= 2338 2338 'WHERE tc.post_id = tp.post_id '. 2339 2339 "AND tp.blog_id = '".$this->con->escape($this->id)."' ". 2340 2340 'AND comment_status = -2'; 2341 2341 2342 2342 #If user can only delete, we need to check the post's owner 2343 2343 if (!$this->core->auth->check('contentadmin',$this->id)) 2344 2344 { 2345 $strReq .= 2345 $strReq .= 2346 2346 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 2347 2347 } 2348 2348 2349 2349 $this->con->execute($strReq); 2350 2350 $this->triggerBlog(); 2351 2351 } 2352 2352 2353 2353 private function getCommentCursor($cur) 2354 2354 { … … 2356 2356 throw new Exception(__('You must provide a comment')); 2357 2357 } 2358 2358 2359 2359 if ($cur->comment_author !== null && $cur->comment_author == '') { 2360 2360 throw new Exception(__('You must provide an author name')); 2361 2361 } 2362 2362 2363 2363 if ($cur->comment_email != '' && !text::isEmail($cur->comment_email)) { 2364 2364 throw new Exception(__('Email address is not valid.')); 2365 2365 } 2366 2366 2367 2367 if ($cur->comment_site !== null && $cur->comment_site != '') { 2368 2368 if (!preg_match('|^http(s?)://|i',$cur->comment_site, $matches)) { … … 2372 2372 } 2373 2373 } 2374 2374 2375 2375 if ($cur->comment_status === null) { 2376 2376 $cur->comment_status = (integer) $this->settings->system->comments_pub; 2377 2377 } 2378 2378 2379 2379 # Words list 2380 2380 if ($cur->comment_content !== null) -
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.