Changeset 2655:a568d9e7d732 for inc/core
- Timestamp:
- 02/13/14 14:12:09 (12 years ago)
- Branch:
- twig
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.blog.php
r2650 r2655 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->in($params['user_id'])." "; 868 } 869 867 $not=""; 868 if (!empty($params['user_id_not'])) { 869 $not=" not"; 870 } 871 $strReq .= "AND U.user_id ".$not.$this->con->in($params['user_id'])." "; 872 } 873 870 874 if (isset($params['cat_id']) && $params['cat_id'] !== '') 871 875 { … … 888 892 $strReq .= 'AND '.$this->getPostsCategoryFilter($params['cat_url'],'cat_url').' '; 889 893 } 890 894 891 895 /* Other filters */ 892 896 if (isset($params['post_status'])) { 893 897 $strReq .= 'AND post_status = '.(integer) $params['post_status'].' '; 894 898 } 895 899 896 900 if (isset($params['post_selected'])) { 897 901 $strReq .= 'AND post_selected = '.(integer) $params['post_selected'].' '; 898 902 } 899 903 900 904 if (!empty($params['post_year'])) { 901 905 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y').' = '. 902 906 "'".sprintf('%04d',$params['post_year'])."' "; 903 907 } 904 908 905 909 if (!empty($params['post_month'])) { 906 910 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m').' = '. 907 911 "'".sprintf('%02d',$params['post_month'])."' "; 908 912 } 909 913 910 914 if (!empty($params['post_day'])) { 911 915 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d').' = '. 912 916 "'".sprintf('%02d',$params['post_day'])."' "; 913 917 } 914 918 915 919 if (!empty($params['post_lang'])) { 916 920 $strReq .= "AND P.post_lang = '".$this->con->escape($params['post_lang'])."' "; 917 921 } 918 922 919 923 if (!empty($params['search'])) 920 924 { 921 925 $words = text::splitWords($params['search']); 922 926 923 927 if (!empty($words)) 924 928 { … … 927 931 $this->core->callBehavior('corePostSearch',$this->core,array(&$words,&$strReq,&$params)); 928 932 } 929 933 930 934 if ($words) 931 935 { … … 937 941 } 938 942 } 939 943 940 944 if (isset($params['media'])) { 941 945 if ($params['media'] == '0') { 942 946 $strReq .= 'AND NOT '; 943 947 } else { 944 $strReq .= 'AND '; 948 $strReq .= 'AND '; 945 949 } 946 950 $strReq .= 'EXISTS (SELECT M.post_id FROM '.$this->prefix.'post_media M '. … … 968 972 $strReq .= $this->con->limit($params['limit']); 969 973 } 970 974 971 975 if (!empty($params['sql_only'])) { 972 976 return $strReq; … … 977 981 $rs->_nb_media = array(); 978 982 $rs->extend('rsExtPost'); 979 983 980 984 # --BEHAVIOR-- coreBlogGetPosts 981 985 $this->core->callBehavior('coreBlogGetPosts',$rs); 982 986 983 987 return $rs; 984 988 } 985 989 986 990 /** 987 991 Returns a record with post id, title and date for next or previous post 988 992 according to the post ID. 989 993 $dir could be 1 (next post) or -1 (previous post). 990 994 991 995 @param post_id <b>integer</b> Post ID 992 996 @param dir <b>integer</b> Search direction … … 999 1003 $dt = $post->post_dt; 1000 1004 $post_id = (integer) $post->post_id; 1001 1005 1002 1006 if($dir > 0) { 1003 1007 $sign = '>'; … … 1008 1012 $order = 'DESC'; 1009 1013 } 1010 1014 1011 1015 $params['post_type'] = $post->post_type; 1012 1016 $params['limit'] = 1; … … 1017 1021 " OR post_dt ".$sign." '".$this->con->escape($dt)."' ". 1018 1022 ') '; 1019 1023 1020 1024 if ($restrict_to_category) { 1021 1025 $params['sql'] .= $post->cat_id ? 'AND P.cat_id = '.(integer) $post->cat_id.' ' : 'AND P.cat_id IS NULL '; 1022 1026 } 1023 1027 1024 1028 if ($restrict_to_lang) { 1025 1029 $params['sql'] .= $post->post_lang ? 'AND P.post_lang = \''. $this->con->escape($post->post_lang) .'\' ': 'AND P.post_lang IS NULL '; 1026 1030 } 1027 1031 1028 1032 $rs = $this->getPosts($params); 1029 1033 1030 1034 if ($rs->isEmpty()) { 1031 1035 return null; 1032 1036 } 1033 1037 1034 1038 return $rs; 1035 1039 } 1036 1040 1037 1041 /** 1038 1042 Retrieves different languages and post count on blog, based on post_lang 1039 1043 field. <var>$params</var> is an array taking the following optionnal 1040 1044 parameters: 1041 1045 1042 1046 - post_type: Get only entries with given type (default "post", '' for no type) 1043 1047 - lang: retrieve post count for selected lang 1044 1048 - order: order statement (default post_lang DESC) 1045 1049 1046 1050 @param params <b>array</b> Parameters 1047 1051 @return record … … 1054 1058 "AND post_lang <> '' ". 1055 1059 "AND post_lang IS NOT NULL "; 1056 1060 1057 1061 if (!$this->core->auth->check('contentadmin',$this->id)) { 1058 1062 $strReq .= 'AND ((post_status = 1 '; 1059 1063 1060 1064 if ($this->without_password) { 1061 1065 $strReq .= 'AND post_password IS NULL '; 1062 1066 } 1063 1067 $strReq .= ') '; 1064 1068 1065 1069 if ($this->core->auth->userID()) { 1066 1070 $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."')"; … … 1069 1073 } 1070 1074 } 1071 1075 1072 1076 if (isset($params['post_type'])) { 1073 1077 if ($params['post_type'] != '') { … … 1077 1081 $strReq .= "AND post_type = 'post' "; 1078 1082 } 1079 1083 1080 1084 if (isset($params['lang'])) { 1081 1085 $strReq .= "AND post_lang = '".$this->con->escape($params['lang'])."' "; 1082 1086 } 1083 1087 1084 1088 $strReq .= 'GROUP BY post_lang '; 1085 1089 1086 1090 $order = 'desc'; 1087 1091 if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) { … … 1089 1093 } 1090 1094 $strReq .= 'ORDER BY post_lang '.$order.' '; 1091 1095 1092 1096 return $this->con->select($strReq); 1093 1097 } 1094 1098 1095 1099 /** 1096 1100 Returns a record with all distinct blog dates and post count. 1097 1101 <var>$params</var> is an array taking the following optionnal parameters: 1098 1102 1099 1103 - type: (day|month|year) Get days, months or years 1100 1104 - year: (integer) Get dates for given year … … 1107 1111 - previous: Get date before match 1108 1112 - order: Sort by date "ASC" or "DESC" 1109 1113 1110 1114 @param params <b>array</b> Parameters array 1111 1115 @return record … … 1126 1130 $dt_f .= ' 00:00:00'; 1127 1131 $dt_fc .= '000000'; 1128 1132 1129 1133 $cat_field = $catReq = $limit = ''; 1130 1134 1131 1135 if (isset($params['cat_id']) && $params['cat_id'] !== '') { 1132 1136 $catReq = 'AND P.cat_id = '.(integer) $params['cat_id'].' '; … … 1139 1143 $catReq = 'AND P.post_lang = \''. $params['post_lang'].'\' '; 1140 1144 } 1141 1145 1142 1146 $strReq = 'SELECT DISTINCT('.$this->con->dateFormat('post_dt',$dt_f).') AS dt '. 1143 1147 $cat_field. … … 1147 1151 "WHERE P.blog_id = '".$this->con->escape($this->id)."' ". 1148 1152 $catReq; 1149 1153 1150 1154 if (!$this->core->auth->check('contentadmin',$this->id)) { 1151 1155 $strReq .= 'AND ((post_status = 1 '; 1152 1156 1153 1157 if ($this->without_password) { 1154 1158 $strReq .= 'AND post_password IS NULL '; 1155 1159 } 1156 1160 $strReq .= ') '; 1157 1161 1158 1162 if ($this->core->auth->userID()) { 1159 1163 $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; … … 1162 1166 } 1163 1167 } 1164 1168 1165 1169 if (!empty($params['post_type'])) { 1166 1170 $strReq .= "AND post_type ".$this->con->in($params['post_type'])." "; … … 1168 1172 $strReq .= "AND post_type = 'post' "; 1169 1173 } 1170 1174 1171 1175 if (!empty($params['year'])) { 1172 1176 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y')." = '".sprintf('%04d',$params['year'])."' "; 1173 1177 } 1174 1178 1175 1179 if (!empty($params['month'])) { 1176 1180 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m')." = '".sprintf('%02d',$params['month'])."' "; 1177 1181 } 1178 1182 1179 1183 if (!empty($params['day'])) { 1180 1184 $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d')." = '".sprintf('%02d',$params['day'])."' "; 1181 1185 } 1182 1186 1183 1187 # Get next or previous date 1184 1188 if (!empty($params['next']) || !empty($params['previous'])) … … 1193 1197 $dt = $params['previous']; 1194 1198 } 1195 1199 1196 1200 $dt = date('YmdHis',strtotime($dt)); 1197 1201 1198 1202 $strReq .= 'AND '.$this->con->dateFormat('post_dt',$dt_fc).$pdir."'".$dt."' "; 1199 1203 $limit = $this->con->limit(1); 1200 1204 } 1201 1205 1202 1206 $strReq .= 'GROUP BY dt '.$cat_field; 1203 1207 1204 1208 $order = 'desc'; 1205 1209 if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) { 1206 1210 $order = $params['order']; 1207 1211 } 1208 1212 1209 1213 $strReq .= 1210 1214 'ORDER BY dt '.$order.' '. 1211 1215 $limit; 1212 1216 1213 1217 $rs = $this->con->select($strReq); 1214 1218 $rs->extend('rsExtDates'); 1215 1219 return $rs; 1216 1220 } 1217 1221 1218 1222 /** 1219 1223 Creates a new entry. Takes a cursor as input and returns the new entry 1220 1224 ID. 1221 1225 1222 1226 @param cur <b>cursor</b> Post cursor 1223 1227 @return <b>integer</b> New post ID … … 1228 1232 throw new Exception(__('You are not allowed to create an entry')); 1229 1233 } 1230 1234 1231 1235 $this->con->writeLock($this->prefix.'post'); 1232 1236 try … … 1235 1239 $rs = $this->con->select( 1236 1240 'SELECT MAX(post_id) '. 1237 'FROM '.$this->prefix.'post ' 1241 'FROM '.$this->prefix.'post ' 1238 1242 ); 1239 1243 1240 1244 $cur->post_id = (integer) $rs->f(0) + 1; 1241 1245 $cur->blog_id = (string) $this->id; … … 1243 1247 $cur->post_upddt = date('Y-m-d H:i:s'); 1244 1248 $cur->post_tz = $this->core->auth->getInfo('user_tz'); 1245 1249 1246 1250 # Post excerpt and content 1247 1251 $this->getPostContent($cur,$cur->post_id); 1248 1252 1249 1253 $this->getPostCursor($cur); 1250 1254 1251 1255 $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$cur->post_id); 1252 1256 1253 1257 if (!$this->core->auth->check('publish,contentadmin',$this->id)) { 1254 1258 $cur->post_status = -2; 1255 1259 } 1256 1260 1257 1261 # --BEHAVIOR-- coreBeforePostCreate 1258 1262 $this->core->callBehavior('coreBeforePostCreate',$this,$cur); 1259 1263 1260 1264 $cur->insert(); 1261 1265 $this->con->unlock(); … … 1266 1270 throw $e; 1267 1271 } 1268 1272 1269 1273 # --BEHAVIOR-- coreAfterPostCreate 1270 1274 $this->core->callBehavior('coreAfterPostCreate',$this,$cur); 1271 1275 1272 1276 $this->triggerBlog(); 1273 1277 1274 1278 return $cur->post_id; 1275 1279 } 1276 1280 1277 1281 /** 1278 1282 Updates an existing post. 1279 1283 1280 1284 @param id <b>integer</b> Post ID 1281 1285 @param cur <b>cursor</b> Post cursor … … 1286 1290 throw new Exception(__('You are not allowed to update entries')); 1287 1291 } 1288 1292 1289 1293 $id = (integer) $id; 1290 1294 1291 1295 if (empty($id)) { 1292 1296 throw new Exception(__('No such entry ID')); 1293 1297 } 1294 1298 1295 1299 # Post excerpt and content 1296 1300 $this->getPostContent($cur,$id); 1297 1301 1298 1302 $this->getPostCursor($cur); 1299 1303 1300 1304 if ($cur->post_url !== null) { 1301 1305 $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$id); 1302 1306 } 1303 1307 1304 1308 if (!$this->core->auth->check('publish,contentadmin',$this->id)) { 1305 1309 $cur->unsetField('post_status'); 1306 1310 } 1307 1311 1308 1312 $cur->post_upddt = date('Y-m-d H:i:s'); 1309 1313 1310 1314 #If user is only "usage", we need to check the post's owner 1311 1315 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1315 1319 'WHERE post_id = '.$id.' '. 1316 1320 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1317 1321 1318 1322 $rs = $this->con->select($strReq); 1319 1323 1320 1324 if ($rs->isEmpty()) { 1321 1325 throw new Exception(__('You are not allowed to edit this entry')); 1322 1326 } 1323 1327 } 1324 1328 1325 1329 # --BEHAVIOR-- coreBeforePostUpdate 1326 1330 $this->core->callBehavior('coreBeforePostUpdate',$this,$cur); 1327 1331 1328 1332 $cur->update('WHERE post_id = '.$id.' '); 1329 1333 1330 1334 # --BEHAVIOR-- coreAfterPostUpdate 1331 1335 $this->core->callBehavior('coreAfterPostUpdate',$this,$cur); 1332 1336 1333 1337 $this->triggerBlog(); 1334 1338 } 1335 1339 1336 1340 /** 1337 1341 Updates post status. 1338 1342 1339 1343 @param id <b>integer</b> Post ID 1340 1344 @param status <b>integer</b> Post status … … 1344 1348 $this->updPostsStatus($id,$status); 1345 1349 } 1346 1350 1347 1351 /** 1348 1352 Updates posts status. 1349 1353 1350 1354 @param ids <b>mixed</b> Post(s) ID(s) 1351 1355 @param status <b>integer</b> Post status … … 1356 1360 throw new Exception(__('You are not allowed to change this entry status')); 1357 1361 } 1358 1362 1359 1363 $posts_ids = dcUtils::cleanIds($ids); 1360 1364 $status = (integer) $status; 1361 1365 1362 1366 $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". 1363 1367 "AND post_id ".$this->con->in($posts_ids); 1364 1368 1365 1369 #If user can only publish, we need to check the post's owner 1366 1370 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1368 1372 $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1369 1373 } 1370 1374 1371 1375 $cur = $this->con->openCursor($this->prefix.'post'); 1372 1376 1373 1377 $cur->post_status = $status; 1374 1378 $cur->post_upddt = date('Y-m-d H:i:s'); 1375 1379 1376 1380 $cur->update($strReq); 1377 1381 $this->triggerBlog(); 1378 1382 } 1379 1383 1380 1384 /** 1381 1385 Updates post selection. 1382 1386 1383 1387 @param id <b>integer</b> Post ID 1384 1388 @param selected <b>integer</b> Is selected post … … 1388 1392 $this->updPostsSelected($id,$selected); 1389 1393 } 1390 1394 1391 1395 /** 1392 1396 Updates posts selection. 1393 1397 1394 1398 @param ids <b>mixed</b> Post(s) ID(s) 1395 1399 @param selected <b>integer</b> Is selected post(s) … … 1400 1404 throw new Exception(__('You are not allowed to change this entry category')); 1401 1405 } 1402 1406 1403 1407 $posts_ids = dcUtils::cleanIds($ids); 1404 1408 $selected = (boolean) $selected; 1405 1409 1406 1410 $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". 1407 1411 "AND post_id ".$this->con->in($posts_ids); 1408 1412 1409 1413 # If user is only usage, we need to check the post's owner 1410 1414 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1412 1416 $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1413 1417 } 1414 1418 1415 1419 $cur = $this->con->openCursor($this->prefix.'post'); 1416 1420 1417 1421 $cur->post_selected = (integer) $selected; 1418 1422 $cur->post_upddt = date('Y-m-d H:i:s'); 1419 1423 1420 1424 $cur->update($strReq); 1421 1425 $this->triggerBlog(); 1422 1426 } 1423 1427 1424 1428 /** 1425 1429 Updates post category. <var>$cat_id</var> can be null. 1426 1430 1427 1431 @param id <b>integer</b> Post ID 1428 1432 @param cat_id <b>integer</b> Category ID … … 1432 1436 $this->updPostsCategory($id,$cat_id); 1433 1437 } 1434 1438 1435 1439 /** 1436 1440 Updates posts category. <var>$cat_id</var> can be null. 1437 1441 1438 1442 @param ids <b>mixed</b> Post(s) ID(s) 1439 1443 @param cat_id <b>integer</b> Category ID … … 1444 1448 throw new Exception(__('You are not allowed to change this entry category')); 1445 1449 } 1446 1450 1447 1451 $posts_ids = dcUtils::cleanIds($ids); 1448 1452 $cat_id = (integer) $cat_id; 1449 1453 1450 1454 $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". 1451 1455 "AND post_id ".$this->con->in($posts_ids); 1452 1456 1453 1457 # If user is only usage, we need to check the post's owner 1454 1458 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1456 1460 $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1457 1461 } 1458 1462 1459 1463 $cur = $this->con->openCursor($this->prefix.'post'); 1460 1464 1461 1465 $cur->cat_id = ($cat_id ? $cat_id : null); 1462 1466 $cur->post_upddt = date('Y-m-d H:i:s'); 1463 1467 1464 1468 $cur->update($strReq); 1465 1469 $this->triggerBlog(); 1466 1470 } 1467 1471 1468 1472 /** 1469 1473 Updates posts category. <var>$new_cat_id</var> can be null. 1470 1474 1471 1475 @param old_cat_id <b>integer</b> Old category ID 1472 1476 @param new_cat_id <b>integer</b> New category ID … … 1477 1481 throw new Exception(__('You are not allowed to change entries category')); 1478 1482 } 1479 1483 1480 1484 $old_cat_id = (integer) $old_cat_id; 1481 1485 $new_cat_id = (integer) $new_cat_id; 1482 1486 1483 1487 $cur = $this->con->openCursor($this->prefix.'post'); 1484 1488 1485 1489 $cur->cat_id = ($new_cat_id ? $new_cat_id : null); 1486 1490 $cur->post_upddt = date('Y-m-d H:i:s'); 1487 1491 1488 1492 $cur->update( 1489 1493 'WHERE cat_id = '.$old_cat_id.' '. … … 1492 1496 $this->triggerBlog(); 1493 1497 } 1494 1498 1495 1499 /** 1496 1500 Deletes a post. 1497 1501 1498 1502 @param id <b>integer</b> Post ID 1499 1503 */ … … 1502 1506 $this->delPosts($id); 1503 1507 } 1504 1508 1505 1509 /** 1506 1510 Deletes multiple posts. 1507 1511 1508 1512 @param ids <b>mixed</b> Post(s) ID(s) 1509 1513 */ … … 1513 1517 throw new Exception(__('You are not allowed to delete entries')); 1514 1518 } 1515 1519 1516 1520 $posts_ids = dcUtils::cleanIds($ids); 1517 1521 1518 1522 if (empty($posts_ids)) { 1519 1523 throw new Exception(__('No such entry ID')); 1520 1524 } 1521 1525 1522 1526 $strReq = 'DELETE FROM '.$this->prefix.'post '. 1523 1527 "WHERE blog_id = '".$this->con->escape($this->id)."' ". 1524 1528 "AND post_id ".$this->con->in($posts_ids); 1525 1529 1526 1530 #If user can only delete, we need to check the post's owner 1527 1531 if (!$this->core->auth->check('contentadmin',$this->id)) … … 1529 1533 $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 1530 1534 } 1531 1535 1532 1536 $this->con->execute($strReq); 1533 1537 $this->triggerBlog(); 1534 1538 } 1535 1539 1536 1540 /** 1537 1541 Publishes all entries flaged as "scheduled". … … 1543 1547 'WHERE post_status = -1 '. 1544 1548 "AND blog_id = '".$this->con->escape($this->id)."' "; 1545 1549 1546 1550 $rs = $this->con->select($strReq); 1547 1551 1548 1552 $now = dt::toUTC(time()); 1549 1553 $to_change = new ArrayObject(); … … 1552 1556 return; 1553 1557 } 1554 1558 1555 1559 while ($rs->fetch()) 1556 1560 { 1557 1561 # Now timestamp with post timezone 1558 1562 $now_tz = $now + dt::getTimeOffset($rs->post_tz,$now); 1559 1563 1560 1564 # Post timestamp 1561 1565 $post_ts = strtotime($rs->post_dt); 1562 1566 1563 1567 # If now_tz >= post_ts, we publish the entry 1564 1568 if ($now_tz >= $post_ts) { … … 1582 1586 $this->core->callBehavior('coreAfterScheduledEntriesPublish',$this,$to_change); 1583 1587 } 1584 1585 } 1586 1588 1589 } 1590 1587 1591 /** 1588 1592 Retrieves all users having posts on current blog. 1589 1593 1590 1594 @param post_type <b>string</b> post_type filter (post) 1591 1595 @return record … … 1598 1602 'WHERE P.user_id = U.user_id '. 1599 1603 "AND blog_id = '".$this->con->escape($this->id)."' "; 1600 1604 1601 1605 if ($post_type) { 1602 1606 $strReq .= "AND post_type = '".$this->con->escape($post_type)."' "; 1603 1607 } 1604 1608 1605 1609 $strReq .= 'GROUP BY P.user_id, user_name, user_firstname, user_displayname, user_email '; 1606 1610 1607 1611 return $this->con->select($strReq); 1608 1612 } 1609 1613 1610 1614 private function getPostsCategoryFilter($arr,$field='cat_id') 1611 1615 { 1612 1616 $field = $field == 'cat_id' ? 'cat_id' : 'cat_url'; 1613 1617 1614 1618 $sub = array(); 1615 1619 $not = array(); 1616 1620 $queries = array(); 1617 1621 1618 1622 foreach ($arr as $v) 1619 1623 { … … 1622 1626 $id = array_shift($args); 1623 1627 $args = array_flip($args); 1624 1628 1625 1629 if (isset($args['not'])) { $not[$id] = 1; } 1626 1630 if (isset($args['sub'])) { $sub[$id] = 1; } … … 1636 1640 } 1637 1641 } 1638 1642 1639 1643 if (!empty($sub)) { 1640 1644 $rs = $this->con->select( … … 1643 1647 'AND '.$field.' '.$this->con->in(array_keys($sub)) 1644 1648 ); 1645 1649 1646 1650 while ($rs->fetch()) { 1647 1651 $queries[$rs->f($field)] = '(C.cat_lft BETWEEN '.$rs->cat_lft.' AND '.$rs->cat_rgt.')'; 1648 1652 } 1649 1653 } 1650 1654 1651 1655 # Create queries 1652 1656 $sql = array( … … 1654 1658 1 => array() # excluded categories 1655 1659 ); 1656 1660 1657 1661 foreach ($queries as $id => $q) { 1658 1662 $sql[(integer) isset($not[$id])][] = $q; 1659 1663 } 1660 1664 1661 1665 $sql[0] = implode(' OR ',$sql[0]); 1662 1666 $sql[1] = implode(' OR ',$sql[1]); 1663 1667 1664 1668 if ($sql[0]) { 1665 1669 $sql[0] = '('.$sql[0].')'; … … 1667 1671 unset($sql[0]); 1668 1672 } 1669 1673 1670 1674 if ($sql[1]) { 1671 1675 $sql[1] = '(P.cat_id IS NULL OR NOT('.$sql[1].'))'; … … 1673 1677 unset($sql[1]); 1674 1678 } 1675 1679 1676 1680 return implode(' AND ',$sql); 1677 1681 } 1678 1682 1679 1683 private function getPostCursor($cur,$post_id=null) 1680 1684 { … … 1682 1686 throw new Exception(__('No entry title')); 1683 1687 } 1684 1688 1685 1689 if ($cur->post_content == '') { 1686 1690 throw new Exception(__('No entry content')); 1687 1691 } 1688 1692 1689 1693 if ($cur->post_password === '') { 1690 1694 $cur->post_password = null; 1691 1695 } 1692 1696 1693 1697 if ($cur->post_dt == '') { 1694 1698 $offset = dt::getTimeOffset($this->core->auth->getInfo('user_tz')); … … 1696 1700 $cur->post_dt = date('Y-m-d H:i:00',$now); 1697 1701 } 1698 1702 1699 1703 $post_id = is_int($post_id) ? $post_id : $cur->post_id; 1700 1704 1701 1705 if ($cur->post_content_xhtml == '') { 1702 1706 throw new Exception(__('No entry content')); 1703 1707 } 1704 1708 1705 1709 # Words list 1706 1710 if ($cur->post_title !== null && $cur->post_excerpt_xhtml !== null … … 1711 1715 $cur->post_excerpt_xhtml.' '. 1712 1716 $cur->post_content_xhtml; 1713 1717 1714 1718 $cur->post_words = implode(' ',text::splitWords($words)); 1715 1719 } 1716 1720 } 1717 1721 1718 1722 private function getPostContent($cur,$post_id) 1719 1723 { … … 1722 1726 $post_content = $cur->post_content; 1723 1727 $post_content_xhtml = $cur->post_content_xhtml; 1724 1728 1725 1729 $this->setPostContent( 1726 1730 $post_id,$cur->post_format,$cur->post_lang, … … 1728 1732 $post_content,$post_content_xhtml 1729 1733 ); 1730 1734 1731 1735 $cur->post_excerpt = $post_excerpt; 1732 1736 $cur->post_excerpt_xhtml = $post_excerpt_xhtml; … … 1734 1738 $cur->post_content_xhtml = $post_content_xhtml; 1735 1739 } 1736 1740 1737 1741 /** 1738 1742 Creates post HTML content, taking format and lang into account. 1739 1743 1740 1744 @param post_id <b>integer</b> Post ID 1741 1745 @param format <b>string</b> Post format … … 1771 1775 } 1772 1776 } 1773 1777 1774 1778 if ($excerpt) { 1775 1779 $excerpt_xhtml = $this->core->callFormater($format,$excerpt); … … 1778 1782 $excerpt_xhtml = ''; 1779 1783 } 1780 1784 1781 1785 if ($content) { 1782 1786 $content_xhtml = $this->core->callFormater($format,$content); … … 1785 1789 $content_xhtml = ''; 1786 1790 } 1787 1791 1788 1792 # --BEHAVIOR-- coreAfterPostContentFormat 1789 1793 $this->core->callBehavior('coreAfterPostContentFormat',array( … … 1794 1798 )); 1795 1799 } 1796 1800 1797 1801 /** 1798 1802 Returns URL for a post according to blog setting <var>post_url_format</var>. 1799 1803 It will try to guess URL and append some figures if needed. 1800 1804 1801 1805 @param url <b>string</b> Origin URL, could be empty 1802 1806 @param post_dt <b>string</b> Post date (in YYYY-MM-DD HH:mm:ss) … … 1808 1812 { 1809 1813 $url = trim($url); 1810 1814 1811 1815 $url_patterns = array( 1812 1816 '{y}' => date('Y',strtotime($post_dt)), … … 1816 1820 '{id}' => (integer) $post_id 1817 1821 ); 1818 1822 1819 1823 # If URL is empty, we create a new one 1820 1824 if ($url == '') … … 1831 1835 $url = text::tidyURL($url); 1832 1836 } 1833 1837 1834 1838 # Let's check if URL is taken... 1835 1839 $strReq = 'SELECT post_url FROM '.$this->prefix.'post '. … … 1838 1842 "AND blog_id = '".$this->con->escape($this->id)."' ". 1839 1843 'ORDER BY post_url DESC'; 1840 1844 1841 1845 $rs = $this->con->select($strReq); 1842 1846 1843 1847 if (!$rs->isEmpty()) 1844 1848 { … … 1855 1859 "AND blog_id = '".$this->con->escape($this->id)."' ". 1856 1860 'ORDER BY post_url DESC '; 1857 1861 1858 1862 $rs = $this->con->select($strReq); 1859 1863 $a = array(); … … 1861 1865 $a[] = $rs->post_url; 1862 1866 } 1863 1867 1864 1868 natsort($a); 1865 1869 $t_url = end($a); 1866 1870 1867 1871 if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) { 1868 1872 $i = (integer) $m[2]; … … 1871 1875 $i = 1; 1872 1876 } 1873 1877 1874 1878 return $url.($i+1); 1875 1879 } 1876 1880 1877 1881 # URL is empty? 1878 1882 if ($url == '') { 1879 1883 throw new Exception(__('Empty entry URL')); 1880 1884 } 1881 1885 1882 1886 return $url; 1883 1887 } 1884 1888 //@} 1885 1889 1886 1890 /// @name Comments management methods 1887 1891 //@{ … … 1889 1893 Retrieves comments. <b>$params</b> is an array taking the following 1890 1894 optionnal parameters: 1891 1895 1892 1896 - no_content: Don't retrieve comment content 1893 - post_type: Get only entries with given type (default no type, array for many types) 1897 - post_type: Get only entries with given type (default no type, array for many types) 1894 1898 - post_id: (integer) Get comments belonging to given post_id 1895 1899 - cat_id: (integer or array) Get comments belonging to entries of given category ID … … 1907 1911 - limit: Limit parameter 1908 1912 - sql_only : return the sql request instead of results. Only ids are selected 1909 1913 1910 1914 @param params <b>array</b> Parameters 1911 1915 @param count_only <b>boolean</b> Only counts results … … 1918 1922 $strReq = 'SELECT count(comment_id) '; 1919 1923 } 1920 elseif (!empty($params['sql_only'])) 1924 elseif (!empty($params['sql_only'])) 1921 1925 { 1922 1926 $strReq = 'SELECT P.post_id '; … … 1929 1933 $content_req = 'comment_content, '; 1930 1934 } 1931 1935 1932 1936 if (!empty($params['columns']) && is_array($params['columns'])) { 1933 1937 $content_req .= implode(', ',$params['columns']).', '; 1934 1938 } 1935 1939 1936 1940 $strReq = 1937 1941 'SELECT C.comment_id, comment_dt, comment_tz, comment_upddt, '. … … 1942 1946 'P.post_dt, P.user_id, U.user_email, U.user_url '; 1943 1947 } 1944 1948 1945 1949 $strReq .= 1946 1950 'FROM '.$this->prefix.'comment C '. 1947 1951 'INNER JOIN '.$this->prefix.'post P ON C.post_id = P.post_id '. 1948 1952 'INNER JOIN '.$this->prefix.'user U ON P.user_id = U.user_id '; 1949 1953 1950 1954 if (!empty($params['from'])) { 1951 1955 $strReq .= $params['from'].' '; 1952 1956 } 1953 1957 1954 1958 $strReq .= 1955 1959 "WHERE P.blog_id = '".$this->con->escape($this->id)."' "; 1956 1960 1957 1961 if (!$this->core->auth->check('contentadmin',$this->id)) { 1958 1962 $strReq .= 'AND ((comment_status = 1 AND P.post_status = 1 '; 1959 1963 1960 1964 if ($this->without_password) { 1961 1965 $strReq .= 'AND post_password IS NULL '; 1962 1966 } 1963 1967 $strReq .= ') '; 1964 1968 1965 1969 if ($this->core->auth->userID()) { 1966 1970 $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; … … 1969 1973 } 1970 1974 } 1971 1975 1972 1976 if (!empty($params['post_type'])) 1973 1977 { 1974 1978 $strReq .= 'AND post_type '.$this->con->in($params['post_type']); 1975 1979 } 1976 1980 1977 1981 if (isset($params['post_id']) && $params['post_id'] !== '') { 1978 1982 $strReq .= 'AND P.post_id = '.(integer) $params['post_id'].' '; 1979 1983 } 1980 1984 1981 1985 if (isset($params['cat_id']) && $params['cat_id'] !== '') { 1982 1986 $strReq .= 'AND P.cat_id = '.(integer) $params['cat_id'].' '; 1983 1987 } 1984 1988 1985 1989 if (isset($params['comment_id']) && $params['comment_id'] !== '') { 1986 1990 if (is_array($params['comment_id'])) { … … 1991 1995 $strReq .= 'AND comment_id '.$this->con->in($params['comment_id']); 1992 1996 } 1993 1997 1994 1998 if (isset($params['comment_site'])) { 1995 1999 $comment_site = $this->con->escape(str_replace('*','%',$params['comment_site'])); 1996 2000 $strReq .= "AND comment_site LIKE '".$comment_site."' "; 1997 2001 } 1998 2002 1999 2003 if (isset($params['comment_status'])) { 2000 2004 $strReq .= 'AND comment_status = '.(integer) $params['comment_status'].' '; 2001 2005 } 2002 2006 2003 2007 if (!empty($params['comment_status_not'])) 2004 2008 { 2005 2009 $strReq .= 'AND comment_status <> '.(integer) $params['comment_status_not'].' '; 2006 2010 } 2007 2011 2008 2012 if (isset($params['comment_trackback'])) { 2009 2013 $strReq .= 'AND comment_trackback = '.(integer) (boolean) $params['comment_trackback'].' '; 2010 2014 } 2011 2015 2012 2016 if (isset($params['comment_ip'])) { 2013 2017 $comment_ip = $this->con->escape(str_replace('*','%',$params['comment_ip'])); 2014 2018 $strReq .= "AND comment_ip LIKE '".$comment_ip."' "; 2015 2019 } 2016 2020 2017 2021 if (isset($params['q_author'])) { 2018 2022 $q_author = $this->con->escape(str_replace('*','%',strtolower($params['q_author']))); 2019 2023 $strReq .= "AND LOWER(comment_author) LIKE '".$q_author."' "; 2020 2024 } 2021 2025 2022 2026 if (!empty($params['search'])) 2023 2027 { 2024 2028 $words = text::splitWords($params['search']); 2025 2029 2026 2030 if (!empty($words)) 2027 2031 { … … 2030 2034 $this->core->callBehavior('coreCommentSearch',$this->core,array(&$words,&$strReq,&$params)); 2031 2035 } 2032 2036 2033 2037 if ($words) 2034 2038 { … … 2040 2044 } 2041 2045 } 2042 2046 2043 2047 if (!empty($params['sql'])) { 2044 2048 $strReq .= $params['sql'].' '; 2045 2049 } 2046 2050 2047 2051 if (!$count_only) 2048 2052 { … … 2053 2057 } 2054 2058 } 2055 2059 2056 2060 if (!$count_only && !empty($params['limit'])) { 2057 2061 $strReq .= $this->con->limit($params['limit']); … … 2061 2065 return $strReq; 2062 2066 } 2063 2067 2064 2068 $rs = $this->con->select($strReq); 2065 2069 $rs->core = $this->core; 2066 2070 $rs->extend('rsExtComment'); 2067 2071 2068 2072 # --BEHAVIOR-- coreBlogGetComments 2069 2073 $this->core->callBehavior('coreBlogGetComments',$rs); 2070 2074 2071 2075 return $rs; 2072 2076 } 2073 2077 2074 2078 /** 2075 2079 Creates a new comment. Takes a cursor as input and returns the new comment 2076 2080 ID. 2077 2081 2078 2082 @param cur <b>cursor</b> Comment cursor 2079 2083 @return <b>integer</b> New comment ID … … 2087 2091 $rs = $this->con->select( 2088 2092 'SELECT MAX(comment_id) '. 2089 'FROM '.$this->prefix.'comment ' 2093 'FROM '.$this->prefix.'comment ' 2090 2094 ); 2091 2095 2092 2096 $cur->comment_id = (integer) $rs->f(0) + 1; 2093 2097 $cur->comment_upddt = date('Y-m-d H:i:s'); 2094 2098 2095 2099 $offset = dt::getTimeOffset($this->settings->system->blog_timezone); 2096 2100 $cur->comment_dt = date('Y-m-d H:i:s',time() + $offset); 2097 2101 $cur->comment_tz = $this->settings->system->blog_timezone; 2098 2102 2099 2103 $this->getCommentCursor($cur); 2100 2104 2101 2105 if ($cur->comment_ip === null) { 2102 2106 $cur->comment_ip = http::realIP(); 2103 2107 } 2104 2108 2105 2109 # --BEHAVIOR-- coreBeforeCommentCreate 2106 2110 $this->core->callBehavior('coreBeforeCommentCreate',$this,$cur); 2107 2111 2108 2112 $cur->insert(); 2109 2113 $this->con->unlock(); … … 2114 2118 throw $e; 2115 2119 } 2116 2120 2117 2121 # --BEHAVIOR-- coreAfterCommentCreate 2118 2122 $this->core->callBehavior('coreAfterCommentCreate',$this,$cur); 2119 2123 2120 2124 $this->triggerComment($cur->comment_id); 2121 2125 if ($cur->comment_status != -2) { 2122 2126 $this->triggerBlog(); 2123 } 2127 } 2124 2128 return $cur->comment_id; 2125 2129 } 2126 2130 2127 2131 /** 2128 2132 Updates an existing comment. 2129 2133 2130 2134 @param id <b>integer</b> Comment ID 2131 2135 @param cur <b>cursor</b> Comment cursor … … 2136 2140 throw new Exception(__('You are not allowed to update comments')); 2137 2141 } 2138 2142 2139 2143 $id = (integer) $id; 2140 2144 2141 2145 if (empty($id)) { 2142 2146 throw new Exception(__('No such comment ID')); 2143 2147 } 2144 2148 2145 2149 $rs = $this->getComments(array('comment_id' => $id)); 2146 2150 2147 2151 if ($rs->isEmpty()) { 2148 2152 throw new Exception(__('No such comment ID')); 2149 2153 } 2150 2154 2151 2155 #If user is only usage, we need to check the post's owner 2152 2156 if (!$this->core->auth->check('contentadmin',$this->id)) … … 2156 2160 } 2157 2161 } 2158 2162 2159 2163 $this->getCommentCursor($cur); 2160 2164 2161 2165 $cur->comment_upddt = date('Y-m-d H:i:s'); 2162 2166 2163 2167 if (!$this->core->auth->check('publish,contentadmin',$this->id)) { 2164 2168 $cur->unsetField('comment_status'); 2165 2169 } 2166 2170 2167 2171 # --BEHAVIOR-- coreBeforeCommentUpdate 2168 2172 $this->core->callBehavior('coreBeforeCommentUpdate',$this,$cur,$rs); 2169 2173 2170 2174 $cur->update('WHERE comment_id = '.$id.' '); 2171 2175 2172 2176 # --BEHAVIOR-- coreAfterCommentUpdate 2173 2177 $this->core->callBehavior('coreAfterCommentUpdate',$this,$cur,$rs); 2174 2178 2175 2179 $this->triggerComment($id); 2176 2180 $this->triggerBlog(); 2177 2181 } 2178 2182 2179 2183 /** 2180 2184 Updates comment status. 2181 2185 2182 2186 @param id <b>integer</b> Comment ID 2183 2187 @param status <b>integer</b> Comment status … … 2187 2191 $this->updCommentsStatus($id,$status); 2188 2192 } 2189 2193 2190 2194 /** 2191 2195 Updates comments status. 2192 2196 2193 2197 @param ids <b>mixed</b> Comment(s) ID(s) 2194 2198 @param status <b>integer</b> Comment status … … 2199 2203 throw new Exception(__("You are not allowed to change this comment's status")); 2200 2204 } 2201 2205 2202 2206 $co_ids = dcUtils::cleanIds($ids); 2203 2207 $status = (integer) $status; 2204 2205 $strReq = 2208 2209 $strReq = 2206 2210 'UPDATE '.$this->prefix.'comment '. 2207 2211 'SET comment_status = '.$status.' '; 2208 $strReq .= 2212 $strReq .= 2209 2213 'WHERE comment_id'.$this->con->in($co_ids). 2210 2214 'AND post_id in (SELECT tp.post_id '. … … 2213 2217 if (!$this->core->auth->check('contentadmin',$this->id)) 2214 2218 { 2215 $strReq .= 2219 $strReq .= 2216 2220 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 2217 2221 } … … 2221 2225 $this->triggerBlog(); 2222 2226 } 2223 2227 2224 2228 /** 2225 2229 Delete a comment 2226 2230 2227 2231 @param id <b>integer</b> Comment ID 2228 2232 */ … … 2231 2235 $this->delComments($id); 2232 2236 } 2233 2237 2234 2238 /** 2235 2239 Delete comments 2236 2240 2237 2241 @param ids <b>mixed</b> Comment(s) ID(s) 2238 2242 */ … … 2242 2246 throw new Exception(__('You are not allowed to delete comments')); 2243 2247 } 2244 2248 2245 2249 $co_ids = dcUtils::cleanIds($ids); 2246 2250 2247 2251 if (empty($co_ids)) { 2248 2252 throw new Exception(__('No such comment ID')); 2249 2253 } 2250 2254 2251 2255 # Retrieve posts affected by comments edition 2252 2256 $affected_posts = array(); … … 2256 2260 'WHERE comment_id'.$this->con->in($co_ids). 2257 2261 'GROUP BY post_id'; 2258 2262 2259 2263 $rs = $this->con->select($strReq); 2260 2264 2261 2265 while ($rs->fetch()) { 2262 2266 $affected_posts[] = (integer) $rs->post_id; 2263 2267 } 2264 2265 $strReq = 2268 2269 $strReq = 2266 2270 'DELETE FROM '.$this->prefix.'comment '. 2267 2271 'WHERE comment_id'.$this->con->in($co_ids).' '. … … 2272 2276 if (!$this->core->auth->check('contentadmin',$this->id)) 2273 2277 { 2274 $strReq .= 2278 $strReq .= 2275 2279 "AND tp.user_id = '".$this->con->escape($this->core->auth->userID())."' "; 2276 2280 } … … 2286 2290 throw new Exception(__('You are not allowed to delete comments')); 2287 2291 } 2288 2289 $strReq = 2292 2293 $strReq = 2290 2294 'DELETE FROM '.$this->prefix.'comment '. 2291 2295 'WHERE comment_status = -2 '. … … 2296 2300 if (!$this->core->auth->check('contentadmin',$this->id)) 2297 2301 { 2298 $strReq .= 2302 $strReq .= 2299 2303 "AND tp.user_id = '".$this->con->escape($this->core->auth->userID())."' "; 2300 2304 } … … 2303 2307 $this->triggerBlog(); 2304 2308 } 2305 2309 2306 2310 private function getCommentCursor($cur) 2307 2311 { … … 2309 2313 throw new Exception(__('You must provide a comment')); 2310 2314 } 2311 2315 2312 2316 if ($cur->comment_author !== null && $cur->comment_author == '') { 2313 2317 throw new Exception(__('You must provide an author name')); 2314 2318 } 2315 2319 2316 2320 if ($cur->comment_email != '' && !text::isEmail($cur->comment_email)) { 2317 2321 throw new Exception(__('Email address is not valid.')); 2318 2322 } 2319 2323 2320 2324 if ($cur->comment_site !== null && $cur->comment_site != '') { 2321 2325 if (!preg_match('|^http(s?)://|i',$cur->comment_site, $matches)) { … … 2325 2329 } 2326 2330 } 2327 2331 2328 2332 if ($cur->comment_status === null) { 2329 2333 $cur->comment_status = (integer) $this->settings->system->comments_pub; 2330 2334 } 2331 2335 2332 2336 # Words list 2333 2337 if ($cur->comment_content !== null)
Note: See TracChangeset
for help on using the changeset viewer.