Changeset 2521:d11c986607d2
- Timestamp:
- 11/08/13 08:14:28 (12 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
inc/core/class.dc.blog.php
r2318 r2521 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 867 $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."' "; 868 868 } 869 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 '. … … 964 964 } 965 965 } 966 966 967 967 if (!$count_only && !empty($params['limit'])) { 968 968 $strReq .= $this->con->limit($params['limit']); 969 969 } 970 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 1896 - comment_id: (integer ) Get comment with given ID1896 - comment_id: (integer or array) Get comment with given ID (or IDs) 1897 1897 - comment_site: (string) Get comments with given comment_site 1898 1898 - comment_status: (integer) Get comments with given comment_status … … 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 $strReq .= 'AND comment_id = '.(integer) $params['comment_id'].' '; 1987 } 1988 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']); 1992 } 1993 1989 1994 if (isset($params['comment_site'])) { 1990 1995 $comment_site = $this->con->escape(str_replace('*','%',$params['comment_site'])); 1991 1996 $strReq .= "AND comment_site LIKE '".$comment_site."' "; 1992 1997 } 1993 1998 1994 1999 if (isset($params['comment_status'])) { 1995 2000 $strReq .= 'AND comment_status = '.(integer) $params['comment_status'].' '; 1996 2001 } 1997 2002 1998 2003 if (!empty($params['comment_status_not'])) 1999 2004 { 2000 2005 $strReq .= 'AND comment_status <> '.(integer) $params['comment_status_not'].' '; 2001 2006 } 2002 2007 2003 2008 if (isset($params['comment_trackback'])) { 2004 2009 $strReq .= 'AND comment_trackback = '.(integer) (boolean) $params['comment_trackback'].' '; 2005 2010 } 2006 2011 2007 2012 if (isset($params['comment_ip'])) { 2008 2013 $comment_ip = $this->con->escape(str_replace('*','%',$params['comment_ip'])); 2009 2014 $strReq .= "AND comment_ip LIKE '".$comment_ip."' "; 2010 2015 } 2011 2016 2012 2017 if (isset($params['q_author'])) { 2013 2018 $q_author = $this->con->escape(str_replace('*','%',strtolower($params['q_author']))); 2014 2019 $strReq .= "AND LOWER(comment_author) LIKE '".$q_author."' "; 2015 2020 } 2016 2021 2017 2022 if (!empty($params['search'])) 2018 2023 { 2019 2024 $words = text::splitWords($params['search']); 2020 2025 2021 2026 if (!empty($words)) 2022 2027 { … … 2025 2030 $this->core->callBehavior('coreCommentSearch',$this->core,array(&$words,&$strReq,&$params)); 2026 2031 } 2027 2032 2028 2033 if ($words) 2029 2034 { … … 2035 2040 } 2036 2041 } 2037 2042 2038 2043 if (!empty($params['sql'])) { 2039 2044 $strReq .= $params['sql'].' '; 2040 2045 } 2041 2046 2042 2047 if (!$count_only) 2043 2048 { … … 2048 2053 } 2049 2054 } 2050 2055 2051 2056 if (!$count_only && !empty($params['limit'])) { 2052 2057 $strReq .= $this->con->limit($params['limit']); … … 2056 2061 return $strReq; 2057 2062 } 2058 2063 2059 2064 $rs = $this->con->select($strReq); 2060 2065 $rs->core = $this->core; 2061 2066 $rs->extend('rsExtComment'); 2062 2067 2063 2068 # --BEHAVIOR-- coreBlogGetComments 2064 2069 $this->core->callBehavior('coreBlogGetComments',$rs); 2065 2070 2066 2071 return $rs; 2067 2072 } 2068 2073 2069 2074 /** 2070 2075 Creates a new comment. Takes a cursor as input and returns the new comment 2071 2076 ID. 2072 2077 2073 2078 @param cur <b>cursor</b> Comment cursor 2074 2079 @return <b>integer</b> New comment ID … … 2082 2087 $rs = $this->con->select( 2083 2088 'SELECT MAX(comment_id) '. 2084 'FROM '.$this->prefix.'comment ' 2089 'FROM '.$this->prefix.'comment ' 2085 2090 ); 2086 2091 2087 2092 $cur->comment_id = (integer) $rs->f(0) + 1; 2088 2093 $cur->comment_upddt = date('Y-m-d H:i:s'); 2089 2094 2090 2095 $offset = dt::getTimeOffset($this->settings->system->blog_timezone); 2091 2096 $cur->comment_dt = date('Y-m-d H:i:s',time() + $offset); 2092 2097 $cur->comment_tz = $this->settings->system->blog_timezone; 2093 2098 2094 2099 $this->getCommentCursor($cur); 2095 2100 2096 2101 if ($cur->comment_ip === null) { 2097 2102 $cur->comment_ip = http::realIP(); 2098 2103 } 2099 2104 2100 2105 # --BEHAVIOR-- coreBeforeCommentCreate 2101 2106 $this->core->callBehavior('coreBeforeCommentCreate',$this,$cur); 2102 2107 2103 2108 $cur->insert(); 2104 2109 $this->con->unlock(); … … 2109 2114 throw $e; 2110 2115 } 2111 2116 2112 2117 # --BEHAVIOR-- coreAfterCommentCreate 2113 2118 $this->core->callBehavior('coreAfterCommentCreate',$this,$cur); 2114 2119 2115 2120 $this->triggerComment($cur->comment_id); 2116 2121 if ($cur->comment_status != -2) { 2117 2122 $this->triggerBlog(); 2118 } 2123 } 2119 2124 return $cur->comment_id; 2120 2125 } 2121 2126 2122 2127 /** 2123 2128 Updates an existing comment. 2124 2129 2125 2130 @param id <b>integer</b> Comment ID 2126 2131 @param cur <b>cursor</b> Comment cursor … … 2131 2136 throw new Exception(__('You are not allowed to update comments')); 2132 2137 } 2133 2138 2134 2139 $id = (integer) $id; 2135 2140 2136 2141 if (empty($id)) { 2137 2142 throw new Exception(__('No such comment ID')); 2138 2143 } 2139 2144 2140 2145 $rs = $this->getComments(array('comment_id' => $id)); 2141 2146 2142 2147 if ($rs->isEmpty()) { 2143 2148 throw new Exception(__('No such comment ID')); 2144 2149 } 2145 2150 2146 2151 #If user is only usage, we need to check the post's owner 2147 2152 if (!$this->core->auth->check('contentadmin',$this->id)) … … 2151 2156 } 2152 2157 } 2153 2158 2154 2159 $this->getCommentCursor($cur); 2155 2160 2156 2161 $cur->comment_upddt = date('Y-m-d H:i:s'); 2157 2162 2158 2163 if (!$this->core->auth->check('publish,contentadmin',$this->id)) { 2159 2164 $cur->unsetField('comment_status'); 2160 2165 } 2161 2166 2162 2167 # --BEHAVIOR-- coreBeforeCommentUpdate 2163 2168 $this->core->callBehavior('coreBeforeCommentUpdate',$this,$cur,$rs); 2164 2169 2165 2170 $cur->update('WHERE comment_id = '.$id.' '); 2166 2171 2167 2172 # --BEHAVIOR-- coreAfterCommentUpdate 2168 2173 $this->core->callBehavior('coreAfterCommentUpdate',$this,$cur,$rs); 2169 2174 2170 2175 $this->triggerComment($id); 2171 2176 $this->triggerBlog(); 2172 2177 } 2173 2178 2174 2179 /** 2175 2180 Updates comment status. 2176 2181 2177 2182 @param id <b>integer</b> Comment ID 2178 2183 @param status <b>integer</b> Comment status … … 2182 2187 $this->updCommentsStatus($id,$status); 2183 2188 } 2184 2189 2185 2190 /** 2186 2191 Updates comments status. 2187 2192 2188 2193 @param ids <b>mixed</b> Comment(s) ID(s) 2189 2194 @param status <b>integer</b> Comment status … … 2194 2199 throw new Exception(__("You are not allowed to change this comment's status")); 2195 2200 } 2196 2201 2197 2202 $co_ids = dcUtils::cleanIds($ids); 2198 2203 $status = (integer) $status; 2199 2200 $strReq = 2204 2205 $strReq = 2201 2206 'UPDATE '.$this->prefix.'comment tc '; 2202 2207 2203 2208 # mySQL uses "JOIN" synthax 2204 2209 if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { 2205 $strReq .= 2210 $strReq .= 2206 2211 'JOIN '.$this->prefix.'post tp ON tc.post_id = tp.post_id '; 2207 2212 } 2208 2209 $strReq .= 2213 2214 $strReq .= 2210 2215 'SET comment_status = '.$status.' '; 2211 2216 2212 2217 # pgSQL uses "FROM" synthax 2213 2218 if ($this->con->driver() != 'mysql' && $this->con->driver() != 'mysqli') { 2214 $strReq .= 2219 $strReq .= 2215 2220 'FROM '.$this->prefix.'post tp '; 2216 2221 } 2217 2222 2218 2223 $strReq .= 2219 2224 "WHERE blog_id = '".$this->con->escape($this->id)."' ". 2220 2225 'AND comment_id'.$this->con->in($co_ids); 2221 2226 2222 2227 # add pgSQL "WHERE" clause 2223 2228 if ($this->con->driver() != 'mysql' && $this->con->driver() != 'mysqli') { 2224 $strReq .= 2229 $strReq .= 2225 2230 'AND tc.post_id = tp.post_id '; 2226 2231 } 2227 2232 2228 2233 #If user is only usage, we need to check the post's owner 2229 2234 if (!$this->core->auth->check('contentadmin',$this->id)) 2230 2235 { 2231 $strReq .= 2236 $strReq .= 2232 2237 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 2233 2238 } 2234 2239 2235 2240 $this->con->execute($strReq); 2236 2241 $this->triggerComments($co_ids); 2237 2242 $this->triggerBlog(); 2238 2243 } 2239 2244 2240 2245 /** 2241 2246 Delete a comment 2242 2247 2243 2248 @param id <b>integer</b> Comment ID 2244 2249 */ … … 2247 2252 $this->delComments($id); 2248 2253 } 2249 2254 2250 2255 /** 2251 2256 Delete comments 2252 2257 2253 2258 @param ids <b>mixed</b> Comment(s) ID(s) 2254 2259 */ … … 2258 2263 throw new Exception(__('You are not allowed to delete comments')); 2259 2264 } 2260 2265 2261 2266 $co_ids = dcUtils::cleanIds($ids); 2262 2267 2263 2268 if (empty($co_ids)) { 2264 2269 throw new Exception(__('No such comment ID')); 2265 2270 } 2266 2271 2267 2272 # Retrieve posts affected by comments edition 2268 2273 $affected_posts = array(); … … 2272 2277 'WHERE comment_id'.$this->con->in($co_ids). 2273 2278 'GROUP BY post_id'; 2274 2279 2275 2280 $rs = $this->con->select($strReq); 2276 2281 2277 2282 while ($rs->fetch()) { 2278 2283 $affected_posts[] = (integer) $rs->post_id; 2279 2284 } 2280 2285 2281 2286 # mySQL uses "INNER JOIN" synthax 2282 2287 if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { 2283 $strReq = 2288 $strReq = 2284 2289 'DELETE FROM tc '. 2285 2290 'USING '.$this->prefix.'comment tc '. … … 2288 2293 # pgSQL uses nothing special 2289 2294 else { 2290 $strReq = 2295 $strReq = 2291 2296 'DELETE FROM '.$this->prefix.'comment tc '. 2292 2297 'USING '.$this->prefix.'post tp '; 2293 2298 } 2294 2295 $strReq .= 2299 2300 $strReq .= 2296 2301 'WHERE tc.post_id = tp.post_id '. 2297 2302 "AND tp.blog_id = '".$this->con->escape($this->id)."' ". 2298 2303 'AND comment_id'.$this->con->in($co_ids); 2299 2304 2300 2305 #If user can only delete, we need to check the post's owner 2301 2306 if (!$this->core->auth->check('contentadmin',$this->id)) 2302 2307 { 2303 $strReq .= 2308 $strReq .= 2304 2309 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 2305 2310 } 2306 2311 2307 2312 $this->con->execute($strReq); 2308 2313 $this->triggerComments($co_ids, true, $affected_posts); … … 2315 2320 throw new Exception(__('You are not allowed to delete comments')); 2316 2321 } 2317 2322 2318 2323 # mySQL uses "INNER JOIN" synthax 2319 2324 if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { 2320 $strReq = 2325 $strReq = 2321 2326 'DELETE FROM tc '. 2322 2327 'USING '.$this->prefix.'comment tc '. … … 2325 2330 # pgSQL uses nothing special 2326 2331 else { 2327 $strReq = 2332 $strReq = 2328 2333 'DELETE FROM '.$this->prefix.'comment tc '. 2329 2334 'USING '.$this->prefix.'post tp '; 2330 2335 } 2331 2332 $strReq .= 2336 2337 $strReq .= 2333 2338 'WHERE tc.post_id = tp.post_id '. 2334 2339 "AND tp.blog_id = '".$this->con->escape($this->id)."' ". 2335 2340 'AND comment_status = -2'; 2336 2341 2337 2342 #If user can only delete, we need to check the post's owner 2338 2343 if (!$this->core->auth->check('contentadmin',$this->id)) 2339 2344 { 2340 $strReq .= 2345 $strReq .= 2341 2346 "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; 2342 2347 } 2343 2348 2344 2349 $this->con->execute($strReq); 2345 2350 $this->triggerBlog(); 2346 2351 } 2347 2352 2348 2353 private function getCommentCursor($cur) 2349 2354 { … … 2351 2356 throw new Exception(__('You must provide a comment')); 2352 2357 } 2353 2358 2354 2359 if ($cur->comment_author !== null && $cur->comment_author == '') { 2355 2360 throw new Exception(__('You must provide an author name')); 2356 2361 } 2357 2362 2358 2363 if ($cur->comment_email != '' && !text::isEmail($cur->comment_email)) { 2359 2364 throw new Exception(__('Email address is not valid.')); 2360 2365 } 2361 2366 2362 2367 if ($cur->comment_site !== null && $cur->comment_site != '') { 2363 2368 if (!preg_match('|^http(s?)://|i',$cur->comment_site, $matches)) { … … 2367 2372 } 2368 2373 } 2369 2374 2370 2375 if ($cur->comment_status === null) { 2371 2376 $cur->comment_status = (integer) $this->settings->system->comments_pub; 2372 2377 } 2373 2378 2374 2379 # Words list 2375 2380 if ($cur->comment_content !== null)
Note: See TracChangeset
for help on using the changeset viewer.