Dotclear

source: inc/core/class.dc.blog.php @ 1610:2d5ebb2fcdfd

Revision 1610:2d5ebb2fcdfd, 61.2 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Fixes #472, <tpl:Categories> (new without_empty="0" attribute) and categories' widget ("Include empty categories" checkbox) may includes empty categories.

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14/**
15@ingroup DC_CORE
16@nosubgrouping
17@brief Dotclear blog class.
18
19Dotclear blog class instance is provided by dcCore $blog property.
20*/
21class dcBlog
22{
23     /** @var dcCore dcCore instance */
24     protected $core;
25     /** @var connection Database connection object */
26     public $con;
27     /** @var string Database table prefix */
28     public $prefix;
29     
30     /** @var string Blog ID */
31     public $id;
32     /** @var string Blog unique ID */
33     public $uid;
34     /** @var string Blog name */
35     public $name;
36     /** @var string Blog description */
37     public $desc;
38     /** @var string Blog URL */
39     public $url;
40     /** @var string Blog host */
41     public $host;
42     /** @var string Blog creation date */
43     public $creadt;
44     /** @var string Blog last update date */
45     public $upddt;
46     /** @var string Blog status */
47     public $status;
48     
49     /** @var dcSettings dcSettings object */
50     public $settings;
51     /** @var string Blog theme path */
52     public $themes_path;
53     /** @var string Blog public path */
54     public $public_path;
55     
56     private $post_status = array();
57     private $comment_status = array();
58     
59     private $categories;
60     
61     /** @var boolean Disallow entries password protection */
62     public $without_password = true;
63     
64     /**
65     Inits dcBlog object
66     
67     @param    core      <b>dcCore</b>       Dotclear core reference
68     @param    id        <b>string</b>       Blog ID
69     */
70     public function __construct($core, $id)
71     {
72          $this->con =& $core->con;
73          $this->prefix = $core->prefix;
74          $this->core =& $core;
75         
76          if (($b = $this->core->getBlog($id)) !== false)
77          {
78               $this->id = $id;
79               $this->uid = $b->blog_uid;
80               $this->name = $b->blog_name;
81               $this->desc = $b->blog_desc;
82               $this->url = $b->blog_url;
83               $this->host = http::getHostFromURL($this->url);
84               $this->creadt = strtotime($b->blog_creadt);
85               $this->upddt = strtotime($b->blog_upddt);
86               $this->status = $b->blog_status;
87               
88               $this->settings = new dcSettings($this->core,$this->id);
89               
90               $this->themes_path = path::fullFromRoot($this->settings->system->themes_path,DC_ROOT);
91               $this->public_path = path::fullFromRoot($this->settings->system->public_path,DC_ROOT);
92               
93               $this->post_status['-2'] = __('Pending');
94               $this->post_status['-1'] = __('Scheduled');
95               $this->post_status['0'] = __('Unpublished');
96               $this->post_status['1'] = __('Published');
97               
98               $this->comment_status['-2'] = __('Junk');
99               $this->comment_status['-1'] = __('Pending');
100               $this->comment_status['0'] = __('Unpublished');
101               $this->comment_status['1'] = __('Published');
102               
103               # --BEHAVIOR-- coreBlogConstruct
104               $this->core->callBehavior('coreBlogConstruct',$this);
105          }
106     }
107     
108     /// @name Common public methods
109     //@{
110     /**
111     Returns blog URL ending with a question mark.
112     */
113     public function getQmarkURL()
114     {
115          if (substr($this->url,-1) != '?') {
116               return $this->url.'?';
117          }
118         
119          return $this->url;
120     }
121     
122     /**
123     Returns an entry status name given to a code. Status are translated, never
124     use it for tests. If status code does not exist, returns <i>unpublished</i>.
125     
126     @param    s    <b>integer</b> Status code
127     @return   <b>string</b> Blog status name
128     */
129     public function getPostStatus($s)
130     {
131          if (isset($this->post_status[$s])) {
132               return $this->post_status[$s];
133          }
134          return $this->post_status['0'];
135     }
136     
137     /**
138     Returns an array of available entry status codes and names.
139     
140     @return   <b>array</b> Simple array with codes in keys and names in value
141     */
142     public function getAllPostStatus()
143     {
144          return $this->post_status;
145     }
146     
147     /**
148     Returns an array of available comment status codes and names.
149     
150     @return   <b>array</b> Simple array with codes in keys and names in value
151     */
152     public function getAllCommentStatus()
153     {
154          return $this->comment_status;
155     }
156     
157     /**
158     Disallows entries password protection. You need to set it to
159     <var>false</var> while serving a public blog.
160     
161     @param    v         <b>boolean</b>
162     */
163     public function withoutPassword($v)
164     {
165          $this->without_password = (boolean) $v;
166     }
167     //@}
168     
169     /// @name Triggers methods
170     //@{
171     /**
172     Updates blog last update date. Should be called every time you change
173     an element related to the blog.
174     */
175     public function triggerBlog()
176     {
177          $cur = $this->con->openCursor($this->prefix.'blog');
178         
179          $cur->blog_upddt = date('Y-m-d H:i:s');
180         
181          $cur->update("WHERE blog_id = '".$this->con->escape($this->id)."' ");
182         
183          # --BEHAVIOR-- coreBlogAfterTriggerBlog
184          $this->core->callBehavior('coreBlogAfterTriggerBlog',$cur);
185     }
186     
187     /**
188     Updates comment and trackback counters in post table. Should be called
189     every time a comment or trackback is added, removed or changed its status.
190     
191     @param    id        <b>integer</b>      Comment ID
192     @param    del       <b>boolean</b>      If comment is delete, set this to true
193     */
194     public function triggerComment($id,$del=false)
195     {
196          $this->triggerComments($id,$del);
197     }
198     
199     /**
200     Updates comments and trackbacks counters in post table. Should be called
201     every time comments or trackbacks are added, removed or changed there status.
202     
203     @param    ids       <b>mixed</b>        Comment(s) ID(s)
204     @param    del       <b>boolean</b>      If comment is delete, set this to true
205     */
206     public function triggerComments($ids,$del=false)
207     {
208          $co_ids = dcUtils::cleanIds($ids);
209         
210          # a) Retrieve posts affected by comments edition
211          $strReq = 
212               'SELECT post_id, comment_trackback '.
213               'FROM '.$this->prefix.'comment '.
214               'WHERE comment_id'.$this->con->in($co_ids).
215               'GROUP BY post_id,comment_trackback';
216         
217          $rs = $this->con->select($strReq);
218         
219          $a_ids = $a_tbs = array();
220          while ($rs->fetch()) {
221               $a_ids[] = (integer) $rs->post_id;
222               $a_tbs[] = (integer) $rs->comment_trackback;
223          }
224         
225          # b) Count comments of each posts previously retrieved
226          # Note that this does not return posts without comment
227          $strReq = 
228               'SELECT post_id, COUNT(post_id) AS nb_comment,comment_trackback '.
229               'FROM '.$this->prefix.'comment '.
230               'WHERE comment_status = 1 '.
231               (count($a_ids) > 0 ? 'AND post_id'.$this->con->in($a_ids) : ' ');
232         
233          if ($del) {
234               $strReq .= 
235                    'AND comment_id NOT'.$this->con->in($co_ids);
236          }
237         
238          $strReq .= 
239               'GROUP BY post_id,comment_trackback';
240         
241          $rs = $this->con->select($strReq);
242         
243          $b_ids = $b_tbs = $b_nbs = array();
244          while ($rs->fetch()) {
245               $b_ids[] = (integer) $rs->post_id;
246               $b_tbs[] = (integer) $rs->comment_trackback;
247               $b_nbs[] = (integer) $rs->nb_comment;
248          }
249         
250          # c) Update comments numbers on posts
251          # This compare previous requests to update also posts without comment
252          $cur = $this->con->openCursor($this->prefix.'post');
253         
254          foreach($a_ids as $a_key => $a_id)
255          {
256               $nb_comment = $nb_trackback = 0;
257               foreach($b_ids as $b_key => $b_id)
258               {
259                    if ($a_id != $b_id || $a_tbs[$a_key] != $b_tbs[$b_key]) {
260                         continue;
261                    }
262                   
263                    if ($b_tbs[$b_key]) {
264                         $nb_trackback = $b_nbs[$b_key];
265                    } else {
266                         $nb_comment = $b_nbs[$b_key];
267                    }
268               }
269               
270               if ($a_tbs[$a_key]) {
271                    $cur->nb_trackback = $nb_trackback;
272               } else {
273                    $cur->nb_comment = $nb_comment;
274               }
275               $cur->update('WHERE post_id = '.$a_id);
276          }
277     }
278     //@}
279     
280     /// @name Categories management methods
281     //@{
282     public function categories()
283     {
284          if (!($this->categories instanceof dcCategories)) {
285               $this->categories = new dcCategories($this->core);
286          }
287         
288          return $this->categories;
289     }
290     
291     /**
292     Retrieves categories. <var>$params</var> is an associative array which can
293     take the following parameters:
294     
295     - post_type: Get only entries with given type (default "post")
296     - cat_url: filter on cat_url field
297     - cat_id: filter on cat_id field
298     - start: start with a given category
299     - level: categories level to retrieve
300     
301     @param    params    <b>array</b>        Parameters
302     @return   <b>record</b>
303     */
304     public function getCategories($params=array())
305     {
306          $c_params = array();
307          if (isset($params['post_type'])) {
308               $c_params['post_type'] = $params['post_type'];
309               unset($params['post_type']);
310          }
311          $counter = $this->getCategoriesCounter($c_params);
312         
313          if (isset($params['without_empty']) && ($params['without_empty'] == false)) {
314               $without_empty = false;
315          } else {
316               $without_empty = $this->core->auth->userID() == false; # Get all categories if in admin display
317          }
318         
319          $start = isset($params['start']) ? (integer) $params['start'] : 0;
320          $l = isset($params['level']) ? (integer) $params['level'] : 0;
321         
322          $rs = $this->categories()->getChildren($start,null,'desc');
323         
324          # Get each categories total posts count
325          $data = array();
326          $stack = array();
327          $level = 0;
328          $cols = $rs->columns();
329          while ($rs->fetch())
330          {
331               $nb_post = isset($counter[$rs->cat_id]) ? (integer) $counter[$rs->cat_id] : 0;
332               
333               if ($rs->level > $level) {
334                    $nb_total = $nb_post;
335                    $stack[$rs->level] = (integer) $nb_post;
336               } elseif ($rs->level == $level) {
337                    $nb_total = $nb_post;
338                    $stack[$rs->level] += $nb_post;
339               } else {
340                    $nb_total = $stack[$rs->level+1] + $nb_post;
341                    if (isset($stack[$rs->level])) {
342                         $stack[$rs->level] += $nb_total;
343                    } else {
344                         $stack[$rs->level] = $nb_total;
345                    }
346                    unset($stack[$rs->level+1]);
347               }
348               
349               if ($nb_total == 0 && $without_empty) {
350                    continue;
351               }
352               
353               $level = $rs->level;
354               
355               $t = array();
356               foreach ($cols as $c) {
357                    $t[$c] = $rs->f($c);
358               }
359               $t['nb_post'] = $nb_post;
360               $t['nb_total'] = $nb_total;
361               
362               if ($l == 0 || ($l > 0 && $l == $rs->level)) {
363                    array_unshift($data,$t);
364               }
365          }
366         
367          # We need to apply filter after counting
368          if (isset($params['cat_id']) && $params['cat_id'] !== '')
369          {
370               $found = false;
371               foreach ($data as $v) {
372                    if ($v['cat_id'] == $params['cat_id']) {
373                         $found = true;
374                         $data = array($v);
375                         break;
376                    }
377               }
378               if (!$found) {
379                    $data = array();
380               }
381          }
382         
383          if (isset($params['cat_url']) && ($params['cat_url'] !== '') 
384               && !isset($params['cat_id']))
385          {
386               $found = false;
387               foreach ($data as $v) {
388                    if ($v['cat_url'] == $params['cat_url']) {
389                         $found = true;
390                         $data = array($v);
391                         break;
392                    }
393               }
394               if (!$found) {
395                    $data = array();
396               }
397          }
398         
399          return staticRecord::newFromArray($data);
400     }
401     
402     /**
403     Retrieves a category by its ID.
404     
405     @param    id        <b>integer</b>      Category ID
406     @return   <b>record</b>
407     */
408     public function getCategory($id)
409     {
410          return $this->getCategories(array('cat_id' => $id));
411     }
412     
413     /**
414     Retrieves parents of a given category.
415     
416     @param    id        <b>integer</b>      Category ID
417     @return   <b>record</b>
418     */
419     public function getCategoryParents($id)
420     {
421          return $this->categories()->getParents($id);
422     }
423     
424     /**
425     Retrieves first parent of a given category.
426     
427     @param    id        <b>integer</b>      Category ID
428     @return   <b>record</b>
429     */
430     public function getCategoryParent($id)
431     {
432          return $this->categories()->getParent($id);
433     }
434     
435     /**
436     Retrieves all category's first children
437     
438     @param    id        <b>integer</b>      Category ID
439     @return   <b>record</b>
440     */
441     public function getCategoryFirstChildren($id)
442     {
443          return $this->getCategories(array('start' => $id,'level' => $id == 0 ? 1 : 2));
444     }
445     
446     private function getCategoriesCounter($params=array())
447     {
448          $strReq =
449          'SELECT  C.cat_id, COUNT(P.post_id) AS nb_post '.
450          'FROM '.$this->prefix.'category AS C '.
451          'JOIN '.$this->prefix."post P ON (C.cat_id = P.cat_id AND P.blog_id = '".$this->con->escape($this->id)."' ) ".
452          "WHERE C.blog_id = '".$this->con->escape($this->id)."' ";
453         
454          if (!$this->core->auth->userID()) {
455               $strReq .= 'AND P.post_status = 1 ';
456          }
457         
458          if (!empty($params['post_type'])) {
459               $strReq .= 'AND P.post_type '.$this->con->in($params['post_type']);
460          }
461         
462          $strReq .= 'GROUP BY C.cat_id ';
463         
464          $rs = $this->con->select($strReq);
465          $counters = array();
466          while ($rs->fetch()) {
467               $counters[$rs->cat_id] = $rs->nb_post;
468          }
469         
470          return $counters;
471     }
472     
473     /**
474     Creates a new category. Takes a cursor as input and returns the new category
475     ID.
476     
477     @param    cur       <b>cursor</b>       Category cursor
478     @return   <b>integer</b>      New category ID
479     */
480     public function addCategory($cur,$parent=0)
481     {
482          if (!$this->core->auth->check('categories',$this->id)) {
483               throw new Exception(__('You are not allowed to add categories'));
484          }
485         
486          $url = array();
487          if ($parent != 0)
488          {
489               $rs = $this->getCategory($parent);
490               if ($rs->isEmpty()) {
491                    $url = array();
492               } else {
493                    $url[] = $rs->cat_url;
494               }
495          }
496         
497          if ($cur->cat_url == '') {
498               $url[] = text::tidyURL($cur->cat_title,false);
499          } else {
500               $url[] = $cur->cat_url;
501          }
502         
503          $cur->cat_url = implode('/',$url);
504         
505          $this->getCategoryCursor($cur);
506          $cur->blog_id = (string) $this->id;
507         
508          # --BEHAVIOR-- coreBeforeCategoryCreate
509          $this->core->callBehavior('coreBeforeCategoryCreate',$this,$cur);
510         
511          $this->categories()->addNode($cur,$parent);
512         
513          # --BEHAVIOR-- coreAfterCategoryCreate
514          $this->core->callBehavior('coreAfterCategoryCreate',$this,$cur);
515          $this->triggerBlog();
516         
517          return $cur->cat_id;
518     }
519     
520     /**
521     Updates an existing category.
522     
523     @param    id        <b>integer</b>      Category ID
524     @param    cur       <b>cursor</b>       Category cursor
525     */
526     public function updCategory($id,$cur)
527     {
528          if (!$this->core->auth->check('categories',$this->id)) {
529               throw new Exception(__('You are not allowed to update categories'));
530          }
531         
532          if ($cur->cat_url == '')
533          {
534               $url = array();
535               $rs = $this->categories()->getParents($id);
536               while ($rs->fetch()) {
537                    if ($rs->index() == $rs->count()-1) {
538                         $url[] = $rs->cat_url;
539                    }
540               }
541               
542               
543               $url[] = text::tidyURL($cur->cat_title,false);
544               $cur->cat_url = implode('/',$url);
545          }
546         
547          $this->getCategoryCursor($cur,$id);
548         
549          # --BEHAVIOR-- coreBeforeCategoryUpdate
550          $this->core->callBehavior('coreBeforeCategoryUpdate',$this,$cur);
551         
552          $cur->update(
553          'WHERE cat_id = '.(integer) $id.' '.
554          "AND blog_id = '".$this->con->escape($this->id)."' ");
555         
556          # --BEHAVIOR-- coreAfterCategoryUpdate
557          $this->core->callBehavior('coreAfterCategoryUpdate',$this,$cur);
558         
559          $this->triggerBlog();
560     }
561
562        /**
563        Set category position
564
565        @param  id              <b>integer</b>          Category ID
566        @param  left            <b>integer</b>          Category ID before
567        @param  right           <b>integer</b>          Category ID after
568        */
569        public function updCategoryPosition($id,$left,$right)
570        {
571                $this->categories()->updatePosition($id,$left,$right);
572                $this->triggerBlog();
573        }
574     
575     /**
576     DEPRECATED METHOD. Use dcBlog::setCategoryParent and dcBlog::moveCategory
577     instead.
578     
579     @param    id        <b>integer</b>      Category ID
580     @param    order     <b>integer</b>      Category position
581     */
582     public function updCategoryOrder($id,$order)
583     {
584          return;
585     }
586     
587     /**
588     Set a category parent
589     
590     @param    id        <b>integer</b>      Category ID
591     @param    parent    <b>integer</b>      Parent Category ID
592     */
593     public function setCategoryParent($id,$parent)
594     {
595          $this->categories()->setNodeParent($id,$parent);
596          $this->triggerBlog();
597     }
598     
599     /**
600     Set category position
601     
602     @param    id        <b>integer</b>      Category ID
603     @param    sibling   <b>integer</b>      Sibling Category ID
604     @param    move      <b>integer</b>      Order (before|after)
605     */
606     public function setCategoryPosition($id,$sibling,$move)
607     {
608          $this->categories()->setNodePosition($id,$sibling,$move);
609          $this->triggerBlog();
610     }
611     
612     /**
613     Deletes a category.
614     
615     @param    id        <b>integer</b>      Category ID
616     */
617     public function delCategory($id)
618     {
619          if (!$this->core->auth->check('categories',$this->id)) {
620               throw new Exception(__('You are not allowed to delete categories'));
621          }
622         
623          $strReq = 'SELECT COUNT(post_id) AS nb_post '.
624                    'FROM '.$this->prefix.'post '.
625                    'WHERE cat_id = '.(integer) $id.' '.
626                    "AND blog_id = '".$this->con->escape($this->id)."' ";
627         
628          $rs = $this->con->select($strReq);
629         
630          if ($rs->nb_post > 0) {
631               throw new Exception(__('This category is not empty.'));
632          }
633         
634          $this->categories()->deleteNode($id,true);
635          $this->triggerBlog();
636     }
637     
638     /**
639     Reset categories order and relocate them to first level
640     */
641     public function resetCategoriesOrder()
642     {
643          if (!$this->core->auth->check('categories',$this->id)) {
644               throw new Exception(__('You are not allowed to reset categories order'));
645          }
646         
647          $this->categories()->resetOrder();
648          $this->triggerBlog();
649     }
650     
651     private function checkCategory($title,$url,$id=null)
652     {
653          # Let's check if URL is taken...
654          $strReq = 
655               'SELECT cat_url FROM '.$this->prefix.'category '.
656               "WHERE cat_url = '".$this->con->escape($url)."' ".
657               ($id ? 'AND cat_id <> '.(integer) $id. ' ' : '').
658               "AND blog_id = '".$this->con->escape($this->id)."' ".
659               'ORDER BY cat_url DESC';
660         
661          $rs = $this->con->select($strReq);
662         
663          if (!$rs->isEmpty())
664          {
665               if ($this->con->driver() == 'mysql') {
666                    $clause = "REGEXP '^".$this->con->escape($url)."[0-9]+$'";
667               } elseif ($this->con->driver() == 'pgsql') {
668                    $clause = "~ '^".$this->con->escape($url)."[0-9]+$'";
669               } else {
670                    $clause = "LIKE '".$this->con->escape($url)."%'";
671               }
672               $strReq = 
673                    'SELECT cat_url FROM '.$this->prefix.'category '.
674                    "WHERE cat_url ".$clause.' '.
675                    ($id ? 'AND cat_id <> '.(integer) $id. ' ' : '').
676                    "AND blog_id = '".$this->con->escape($this->id)."' ".
677                    'ORDER BY cat_url DESC ';
678               
679               $rs = $this->con->select($strReq);
680               $a = array();
681               while ($rs->fetch()) {
682                    $a[] = $rs->cat_url;
683               }
684               
685               natsort($a);
686               $t_url = end($a);
687               
688               if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) {
689                    $i = (integer) $m[2];
690                    $url = $m[1];
691               } else {
692                    $i = 1;
693               }
694               
695               return $url.($i+1);
696          }
697         
698          # URL is empty?
699          if ($url == '') {
700               throw new Exception(__('Empty category URL'));
701          }
702         
703          return $url;
704     }
705     
706     private function getCategoryCursor($cur,$id=null)
707     {
708          if ($cur->cat_title == '') {
709               throw new Exception(__('You must provide a category title'));
710          }
711         
712          # If we don't have any cat_url, let's do one
713          if ($cur->cat_url == '') {
714               $cur->cat_url = text::tidyURL($cur->cat_title,false);
715          }
716         
717          # Still empty ?
718          if ($cur->cat_url == '') {
719               throw new Exception(__('You must provide a category URL'));
720          } else {
721               $cur->cat_url = text::tidyURL($cur->cat_url,true);
722          }
723         
724          # Check if title or url are unique
725          $cur->cat_url = $this->checkCategory($cur->cat_title,$cur->cat_url,$id);
726         
727          if ($cur->cat_desc !== null) {
728               $cur->cat_desc = $this->core->HTMLfilter($cur->cat_desc);
729          }
730     }
731     //@}
732     
733     /// @name Entries management methods
734     //@{
735     /**
736     Retrieves entries. <b>$params</b> is an array taking the following
737     optionnal parameters:
738     
739     - no_content: Don't retrieve entry content (excerpt and content)
740     - post_type: Get only entries with given type (default "post", array for many types and '' for no type)
741     - post_id: (integer) Get entry with given post_id
742     - post_url: Get entry with given post_url field
743     - user_id: (integer) Get entries belonging to given user ID
744     - cat_id: (string or array) Get entries belonging to given category ID
745     - cat_id_not: deprecated (use cat_id with "id ?not" instead)
746     - cat_url: (string or array) Get entries belonging to given category URL
747     - cat_url_not: deprecated (use cat_url with "url ?not" instead)
748     - post_status: (integer) Get entries with given post_status
749     - post_selected: (boolean) Get select flaged entries
750     - post_year: (integer) Get entries with given year
751     - post_month: (integer) Get entries with given month
752     - post_day: (integer) Get entries with given day
753     - post_lang: Get entries with given language code
754     - search: Get entries corresponding of the following search string
755     - columns: (array) More columns to retrieve
756     - sql: Append SQL string at the end of the query
757     - from: Append SQL string after "FROM" statement in query
758     - order: Order of results (default "ORDER BY post_dt DES")
759     - limit: Limit parameter
760     - sql_only : return the sql request instead of results. Only ids are selected
761     
762     Please note that on every cat_id or cat_url, you can add ?not to exclude
763     the category and ?sub to get subcategories.
764     
765     @param    params         <b>array</b>        Parameters
766     @param    count_only     <b>boolean</b>      Only counts results
767     @return   <b>record</b>  A record with some more capabilities or the SQL request
768     */
769     public function getPosts($params=array(),$count_only=false)
770     {
771          # --BEHAVIOR-- coreBlogBeforeGetPosts
772          $params = new ArrayObject($params);
773          $this->core->callBehavior('coreBlogBeforeGetPosts',$params);
774
775          if ($count_only)
776          {
777               $strReq = 'SELECT count(P.post_id) ';
778          }
779          elseif (!empty($params['sql_only'])) 
780          {
781               $strReq = 'SELECT P.post_id ';
782          }
783          else
784          {
785               if (!empty($params['no_content'])) {
786                    $content_req = '';
787               } else {
788                    $content_req =
789                    'post_excerpt, post_excerpt_xhtml, '.
790                    'post_content, post_content_xhtml, post_notes, ';
791               }
792               
793               if (!empty($params['columns']) && is_array($params['columns'])) {
794                    $content_req .= implode(', ',$params['columns']).', ';
795               }
796               
797               $strReq =
798               'SELECT P.post_id, P.blog_id, P.user_id, P.cat_id, post_dt, '.
799               'post_tz, post_creadt, post_upddt, post_format, post_password, '.
800               'post_url, post_lang, post_title, '.$content_req.
801               'post_type, post_meta, post_status, post_selected, post_position, '.
802               'post_open_comment, post_open_tb, nb_comment, nb_trackback, '.
803               'U.user_name, U.user_firstname, U.user_displayname, U.user_email, '.
804               'U.user_url, '.
805               'C.cat_title, C.cat_url, C.cat_desc ';
806          }
807         
808          $strReq .=
809          'FROM '.$this->prefix.'post P '.
810          'INNER JOIN '.$this->prefix.'user U ON U.user_id = P.user_id '.
811          'LEFT OUTER JOIN '.$this->prefix.'category C ON P.cat_id = C.cat_id ';
812         
813          if (!empty($params['from'])) {
814               $strReq .= $params['from'].' ';
815          }
816         
817          $strReq .=
818          "WHERE P.blog_id = '".$this->con->escape($this->id)."' ";
819         
820          if (!$this->core->auth->check('contentadmin',$this->id)) {
821               $strReq .= 'AND ((post_status = 1 ';
822               
823               if ($this->without_password) {
824                    $strReq .= 'AND post_password IS NULL ';
825               }
826               $strReq .= ') ';
827               
828               if ($this->core->auth->userID()) {
829                    $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')";
830               } else {
831                    $strReq .= ') ';
832               }
833          }
834         
835          #Adding parameters
836          if (isset($params['post_type']))
837          {
838               if (is_array($params['post_type']) || $params['post_type'] != '') {
839                    $strReq .= 'AND post_type '.$this->con->in($params['post_type']);
840               }
841          }
842          else
843          {
844               $strReq .= "AND post_type = 'post' ";
845          }
846         
847          if (isset($params['post_id']) && $params['post_id'] !== '') {
848               if (is_array($params['post_id'])) {
849                    array_walk($params['post_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
850               } else {
851                    $params['post_id'] = array((integer) $params['post_id']);
852               }
853               $strReq .= 'AND P.post_id '.$this->con->in($params['post_id']);
854          }
855         
856          if (isset($params['post_url']) && $params['post_url'] !== '') {
857               $strReq .= "AND post_url = '".$this->con->escape($params['post_url'])."' ";
858          }
859         
860          if (!empty($params['user_id'])) {
861               $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."' ";
862          }
863         
864          if (isset($params['cat_id']) && $params['cat_id'] !== '')
865          {
866               if (!is_array($params['cat_id'])) {
867                    $params['cat_id'] = array($params['cat_id']);
868               }
869               if (!empty($params['cat_id_not'])) {
870                    array_walk($params['cat_id'],create_function('&$v,$k','$v=$v." ?not";'));
871               }
872               $strReq .= 'AND '.$this->getPostsCategoryFilter($params['cat_id'],'cat_id').' ';
873          }
874          elseif (isset($params['cat_url']) && $params['cat_url'] !== '')
875          {
876               if (!is_array($params['cat_url'])) {
877                    $params['cat_url'] = array($params['cat_url']);
878               }
879               if (!empty($params['cat_url_not'])) {
880                    array_walk($params['cat_url'],create_function('&$v,$k','$v=$v." ?not";'));
881               }
882               $strReq .= 'AND '.$this->getPostsCategoryFilter($params['cat_url'],'cat_url').' ';
883          }
884         
885          /* Other filters */
886          if (isset($params['post_status'])) {
887               $strReq .= 'AND post_status = '.(integer) $params['post_status'].' ';
888          }
889         
890          if (isset($params['post_selected'])) {
891               $strReq .= 'AND post_selected = '.(integer) $params['post_selected'].' ';
892          }
893         
894          if (!empty($params['post_year'])) {
895               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y').' = '.
896               "'".sprintf('%04d',$params['post_year'])."' ";
897          }
898         
899          if (!empty($params['post_month'])) {
900               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m').' = '.
901               "'".sprintf('%02d',$params['post_month'])."' ";
902          }
903         
904          if (!empty($params['post_day'])) {
905               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d').' = '.
906               "'".sprintf('%02d',$params['post_day'])."' ";
907          }
908         
909          if (!empty($params['post_lang'])) {
910               $strReq .= "AND P.post_lang = '".$this->con->escape($params['post_lang'])."' ";
911          }
912         
913          if (!empty($params['search']))
914          {
915               $words = text::splitWords($params['search']);
916               
917               if (!empty($words))
918               {
919                    # --BEHAVIOR-- corePostSearch
920                    if ($this->core->hasBehavior('corePostSearch')) {
921                         $this->core->callBehavior('corePostSearch',$this->core,array(&$words,&$strReq,&$params));
922                    }
923                   
924                    if ($words)
925                    {
926                         foreach ($words as $i => $w) {
927                              $words[$i] = "post_words LIKE '%".$this->con->escape($w)."%'";
928                         }
929                         $strReq .= 'AND '.implode(' AND ',$words).' ';
930                    }
931               }
932          }
933         
934          if (!empty($params['sql'])) {
935               $strReq .= $params['sql'].' ';
936          }
937         
938          if (!$count_only)
939          {
940               if (!empty($params['order'])) {
941                    $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' ';
942               } else {
943                    $strReq .= 'ORDER BY post_dt DESC ';
944               }
945          }
946         
947          if (!$count_only && !empty($params['limit'])) {
948               $strReq .= $this->con->limit($params['limit']);
949          }
950         
951          if (!empty($params['sql_only'])) {
952               return $strReq;
953          }
954         
955          $rs = $this->con->select($strReq);
956          $rs->core = $this->core;
957          $rs->_nb_media = array();
958          $rs->extend('rsExtPost');
959         
960          # --BEHAVIOR-- coreBlogGetPosts
961          $this->core->callBehavior('coreBlogGetPosts',$rs);
962         
963          return $rs;
964     }
965     
966     /**
967     Returns a record with post id, title and date for next or previous post
968     according to the post ID.
969     $dir could be 1 (next post) or -1 (previous post).
970     
971     @param    post_id                  <b>integer</b>      Post ID
972     @param    dir                      <b>integer</b>      Search direction
973     @param    restrict_to_category     <b>boolean</b>      Restrict to post with same category
974     @param    restrict_to_lang         <b>boolean</b>      Restrict to post with same lang
975     @return   record
976     */
977     public function getNextPost($post,$dir,$restrict_to_category=false, $restrict_to_lang=false)
978     {
979          $dt = $post->post_dt;
980          $post_id = (integer) $post->post_id;
981         
982          if($dir > 0) {
983               $sign = '>';
984               $order = 'ASC';
985          }
986          else {
987               $sign = '<';
988               $order = 'DESC';
989          }
990         
991          $params['post_type'] = $post->post_type;
992          $params['limit'] = 1;
993          $params['order'] = 'post_dt '.$order.', P.post_id '.$order;
994          $params['sql'] =
995          'AND ( '.
996          "    (post_dt = '".$this->con->escape($dt)."' AND P.post_id ".$sign." ".$post_id.") ".
997          "    OR post_dt ".$sign." '".$this->con->escape($dt)."' ".
998          ') ';
999         
1000          if ($restrict_to_category) {
1001               $params['sql'] .= $post->cat_id ? 'AND P.cat_id = '.(integer) $post->cat_id.' ' : 'AND P.cat_id IS NULL ';
1002          }
1003         
1004          if ($restrict_to_lang) {
1005               $params['sql'] .= $post->post_lang ? 'AND P.post_lang = \''. $this->con->escape($post->post_lang) .'\' ': 'AND P.post_lang IS NULL ';
1006          }
1007         
1008          $rs = $this->getPosts($params);
1009         
1010          if ($rs->isEmpty()) {
1011               return null;
1012          }
1013         
1014          return $rs;
1015     }
1016     
1017     /**
1018     Retrieves different languages and post count on blog, based on post_lang
1019     field. <var>$params</var> is an array taking the following optionnal
1020     parameters:
1021     
1022     - post_type: Get only entries with given type (default "post", '' for no type)
1023     - lang: retrieve post count for selected lang
1024     - order: order statement (default post_lang DESC)
1025     
1026     @param    params    <b>array</b>        Parameters
1027     @return   record
1028     */
1029     public function getLangs($params=array())
1030     {
1031          $strReq = 'SELECT COUNT(post_id) as nb_post, post_lang '.
1032                    'FROM '.$this->prefix.'post '.
1033                    "WHERE blog_id = '".$this->con->escape($this->id)."' ".
1034                    "AND post_lang <> '' ".
1035                    "AND post_lang IS NOT NULL ";
1036         
1037          if (!$this->core->auth->check('contentadmin',$this->id)) {
1038               $strReq .= 'AND ((post_status = 1 ';
1039               
1040               if ($this->without_password) {
1041                    $strReq .= 'AND post_password IS NULL ';
1042               }
1043               $strReq .= ') ';
1044               
1045               if ($this->core->auth->userID()) {
1046                    $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."')";
1047               } else {
1048                    $strReq .= ') ';
1049               }
1050          }
1051         
1052          if (isset($params['post_type'])) {
1053               if ($params['post_type'] != '') {
1054                    $strReq .= "AND post_type = '".$this->con->escape($params['post_type'])."' ";
1055               }
1056          } else {
1057               $strReq .= "AND post_type = 'post' ";
1058          }
1059         
1060          if (isset($params['lang'])) {
1061               $strReq .= "AND post_lang = '".$this->con->escape($params['lang'])."' ";
1062          }
1063         
1064          $strReq .= 'GROUP BY post_lang ';
1065         
1066          $order = 'desc';
1067          if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) {
1068               $order = $params['order'];
1069          }
1070          $strReq .= 'ORDER BY post_lang '.$order.' ';
1071         
1072          return $this->con->select($strReq);
1073     }
1074     
1075     /**
1076     Returns a record with all distinct blog dates and post count.
1077     <var>$params</var> is an array taking the following optionnal parameters:
1078     
1079     - type: (day|month|year) Get days, months or years
1080     - year: (integer) Get dates for given year
1081     - month: (integer) Get dates for given month
1082     - day: (integer) Get dates for given day
1083     - cat_id: (integer) Category ID filter
1084     - cat_url: Category URL filter
1085     - post_lang: lang of the posts
1086     - next: Get date following match
1087     - previous: Get date before match
1088     - order: Sort by date "ASC" or "DESC"
1089     
1090     @param    params    <b>array</b>        Parameters array
1091     @return   record
1092     */
1093     public function getDates($params=array())
1094     {
1095          $dt_f = '%Y-%m-%d';
1096          $dt_fc = '%Y%m%d';
1097          if (isset($params['type'])) {
1098               if ($params['type'] == 'year') {
1099                    $dt_f = '%Y-01-01';
1100                    $dt_fc = '%Y0101';
1101               } elseif ($params['type'] == 'month') {
1102                    $dt_f = '%Y-%m-01';
1103                    $dt_fc = '%Y%m01';
1104               }
1105          }
1106          $dt_f .= ' 00:00:00';
1107          $dt_fc .= '000000';
1108         
1109          $cat_field = $catReq = $limit = '';
1110         
1111          if (isset($params['cat_id']) && $params['cat_id'] !== '') {
1112               $catReq = 'AND P.cat_id = '.(integer) $params['cat_id'].' ';
1113               $cat_field = ', C.cat_url ';
1114          } elseif (isset($params['cat_url']) && $params['cat_url'] !== '') {
1115               $catReq = "AND C.cat_url = '".$this->con->escape($params['cat_url'])."' ";
1116               $cat_field = ', C.cat_url ';
1117          }
1118          if (!empty($params['post_lang'])) {
1119               $catReq = 'AND P.post_lang = \''. $params['post_lang'].'\' ';
1120          }
1121         
1122          $strReq = 'SELECT DISTINCT('.$this->con->dateFormat('post_dt',$dt_f).') AS dt '.
1123                    $cat_field.
1124                    ',COUNT(P.post_id) AS nb_post '.
1125                    'FROM '.$this->prefix.'post P LEFT JOIN '.$this->prefix.'category C '.
1126                    'ON P.cat_id = C.cat_id '.
1127                    "WHERE P.blog_id = '".$this->con->escape($this->id)."' ".
1128                    $catReq;
1129         
1130          if (!$this->core->auth->check('contentadmin',$this->id)) {
1131               $strReq .= 'AND ((post_status = 1 ';
1132               
1133               if ($this->without_password) {
1134                    $strReq .= 'AND post_password IS NULL ';
1135               }
1136               $strReq .= ') ';
1137               
1138               if ($this->core->auth->userID()) {
1139                    $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')";
1140               } else {
1141                    $strReq .= ') ';
1142               }
1143          }
1144         
1145          if (!empty($params['post_type'])) {
1146               $strReq .= "AND post_type ".$this->con->in($params['post_type'])." ";
1147          } else {
1148               $strReq .= "AND post_type = 'post' ";
1149          }
1150         
1151          if (!empty($params['year'])) {
1152               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y')." = '".sprintf('%04d',$params['year'])."' ";
1153          }
1154         
1155          if (!empty($params['month'])) {
1156               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m')." = '".sprintf('%02d',$params['month'])."' ";
1157          }
1158         
1159          if (!empty($params['day'])) {
1160               $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d')." = '".sprintf('%02d',$params['day'])."' ";
1161          }
1162         
1163          # Get next or previous date
1164          if (!empty($params['next']) || !empty($params['previous']))
1165          {
1166               if (!empty($params['next'])) {
1167                    $pdir = ' > ';
1168                    $params['order'] = 'asc';
1169                    $dt = $params['next'];
1170               } else {
1171                    $pdir = ' < ';
1172                    $params['order'] = 'desc';
1173                    $dt = $params['previous'];
1174               }
1175               
1176               $dt = date('YmdHis',strtotime($dt));
1177               
1178               $strReq .= 'AND '.$this->con->dateFormat('post_dt',$dt_fc).$pdir."'".$dt."' ";
1179               $limit = $this->con->limit(1);
1180          }
1181         
1182          $strReq .= 'GROUP BY dt '.$cat_field;
1183         
1184          $order = 'desc';
1185          if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) {
1186               $order = $params['order'];
1187          }
1188         
1189          $strReq .=
1190          'ORDER BY dt '.$order.' '.
1191          $limit;
1192         
1193          $rs = $this->con->select($strReq);
1194          $rs->extend('rsExtDates');
1195          return $rs;
1196     }
1197     
1198     /**
1199     Creates a new entry. Takes a cursor as input and returns the new entry
1200     ID.
1201     
1202     @param    cur       <b>cursor</b>       Post cursor
1203     @return   <b>integer</b>      New post ID
1204     */
1205     public function addPost($cur)
1206     {
1207          if (!$this->core->auth->check('usage,contentadmin',$this->id)) {
1208               throw new Exception(__('You are not allowed to create an entry'));
1209          }
1210         
1211          $this->con->writeLock($this->prefix.'post');
1212          try
1213          {
1214               # Get ID
1215               $rs = $this->con->select(
1216                    'SELECT MAX(post_id) '.
1217                    'FROM '.$this->prefix.'post ' 
1218                    );
1219               
1220               $cur->post_id = (integer) $rs->f(0) + 1;
1221               $cur->blog_id = (string) $this->id;
1222               $cur->post_creadt = date('Y-m-d H:i:s');
1223               $cur->post_upddt = date('Y-m-d H:i:s');
1224               $cur->post_tz = $this->core->auth->getInfo('user_tz');
1225               
1226               # Post excerpt and content
1227               $this->getPostContent($cur,$cur->post_id);
1228               
1229               $this->getPostCursor($cur);
1230               
1231               $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$cur->post_id);
1232               
1233               if (!$this->core->auth->check('publish,contentadmin',$this->id)) {
1234                    $cur->post_status = -2;
1235               }
1236               
1237               # --BEHAVIOR-- coreBeforePostCreate
1238               $this->core->callBehavior('coreBeforePostCreate',$this,$cur);
1239               
1240               $cur->insert();
1241               $this->con->unlock();
1242          }
1243          catch (Exception $e)
1244          {
1245               $this->con->unlock();
1246               throw $e;
1247          }
1248         
1249          # --BEHAVIOR-- coreAfterPostCreate
1250          $this->core->callBehavior('coreAfterPostCreate',$this,$cur);
1251         
1252          $this->triggerBlog();
1253         
1254          return $cur->post_id;
1255     }
1256     
1257     /**
1258     Updates an existing post.
1259     
1260     @param    id        <b>integer</b>      Post ID
1261     @param    cur       <b>cursor</b>       Post cursor
1262     */
1263     public function updPost($id,$cur)
1264     {
1265          if (!$this->core->auth->check('usage,contentadmin',$this->id)) {
1266               throw new Exception(__('You are not allowed to update entries'));
1267          }
1268         
1269          $id = (integer) $id;
1270         
1271          if (empty($id)) {
1272               throw new Exception(__('No such entry ID'));
1273          }
1274         
1275          # Post excerpt and content
1276          $this->getPostContent($cur,$id);
1277         
1278          $this->getPostCursor($cur);
1279         
1280          if ($cur->post_url !== null) {
1281               $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$id);
1282          }
1283         
1284          if (!$this->core->auth->check('publish,contentadmin',$this->id)) {
1285               $cur->unsetField('post_status');
1286          }
1287         
1288          $cur->post_upddt = date('Y-m-d H:i:s');
1289         
1290          #If user is only "usage", we need to check the post's owner
1291          if (!$this->core->auth->check('contentadmin',$this->id))
1292          {
1293               $strReq = 'SELECT post_id '.
1294                         'FROM '.$this->prefix.'post '.
1295                         'WHERE post_id = '.$id.' '.
1296                         "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
1297               
1298               $rs = $this->con->select($strReq);
1299               
1300               if ($rs->isEmpty()) {
1301                    throw new Exception(__('You are not allowed to edit this entry'));
1302               }
1303          }
1304         
1305          # --BEHAVIOR-- coreBeforePostUpdate
1306          $this->core->callBehavior('coreBeforePostUpdate',$this,$cur);
1307         
1308          $cur->update('WHERE post_id = '.$id.' ');
1309         
1310          # --BEHAVIOR-- coreAfterPostUpdate
1311          $this->core->callBehavior('coreAfterPostUpdate',$this,$cur);
1312         
1313          $this->triggerBlog();
1314     }
1315     
1316     /**
1317     Updates post status.
1318     
1319     @param    id        <b>integer</b>      Post ID
1320     @param    status    <b>integer</b>      Post status
1321     */
1322     public function updPostStatus($id,$status)
1323     {
1324          $this->updPostsStatus($id,$status);
1325     }
1326     
1327     /**
1328     Updates posts status.
1329     
1330     @param    ids       <b>mixed</b>        Post(s) ID(s)
1331     @param    status    <b>integer</b>      Post status
1332     */
1333     public function updPostsStatus($ids,$status)
1334     {
1335          if (!$this->core->auth->check('publish,contentadmin',$this->id)) {
1336               throw new Exception(__('You are not allowed to change this entry status'));
1337          }
1338         
1339          $posts_ids = dcUtils::cleanIds($ids);
1340          $status = (integer) $status;
1341         
1342          $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ".
1343                    "AND post_id ".$this->con->in($posts_ids);
1344         
1345          #If user can only publish, we need to check the post's owner
1346          if (!$this->core->auth->check('contentadmin',$this->id))
1347          {
1348               $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
1349          }
1350         
1351          $cur = $this->con->openCursor($this->prefix.'post');
1352         
1353          $cur->post_status = $status;
1354          $cur->post_upddt = date('Y-m-d H:i:s');
1355         
1356          $cur->update($strReq);
1357          $this->triggerBlog();
1358     }
1359     
1360     /**
1361     Updates post selection.
1362     
1363     @param    id        <b>integer</b>      Post ID
1364     @param    selected  <b>integer</b>      Is selected post
1365     */
1366     public function updPostSelected($id,$selected)
1367     {
1368          $this->updPostsSelected($id,$selected);
1369     }
1370     
1371     /**
1372     Updates posts selection.
1373     
1374     @param    ids       <b>mixed</b>        Post(s) ID(s)
1375     @param    selected  <b>integer</b>      Is selected post(s)
1376     */
1377     public function updPostsSelected($ids,$selected)
1378     {
1379          if (!$this->core->auth->check('usage,contentadmin',$this->id)) {
1380               throw new Exception(__('You are not allowed to change this entry category'));
1381          }
1382         
1383          $posts_ids = dcUtils::cleanIds($ids);
1384          $selected = (boolean) $selected;
1385         
1386          $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ".
1387                    "AND post_id ".$this->con->in($posts_ids);
1388         
1389          # If user is only usage, we need to check the post's owner
1390          if (!$this->core->auth->check('contentadmin',$this->id))
1391          {
1392               $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
1393          }
1394         
1395          $cur = $this->con->openCursor($this->prefix.'post');
1396         
1397          $cur->post_selected = (integer) $selected;
1398          $cur->post_upddt = date('Y-m-d H:i:s');
1399         
1400          $cur->update($strReq);
1401          $this->triggerBlog();
1402     }
1403     
1404     /**
1405     Updates post category. <var>$cat_id</var> can be null.
1406     
1407     @param    id        <b>integer</b>      Post ID
1408     @param    cat_id    <b>integer</b>      Category ID
1409     */
1410     public function updPostCategory($id,$cat_id)
1411     {
1412          $this->updPostsCategory($id,$cat_id);
1413     }
1414     
1415     /**
1416     Updates posts category. <var>$cat_id</var> can be null.
1417     
1418     @param    ids       <b>mixed</b>        Post(s) ID(s)
1419     @param    cat_id    <b>integer</b>      Category ID
1420     */
1421     public function updPostsCategory($ids,$cat_id)
1422     {
1423          if (!$this->core->auth->check('usage,contentadmin',$this->id)) {
1424               throw new Exception(__('You are not allowed to change this entry category'));
1425          }
1426         
1427          $posts_ids = dcUtils::cleanIds($ids);
1428          $cat_id = (integer) $cat_id;
1429         
1430          $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ".
1431                    "AND post_id ".$this->con->in($posts_ids);
1432         
1433          # If user is only usage, we need to check the post's owner
1434          if (!$this->core->auth->check('contentadmin',$this->id))
1435          {
1436               $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
1437          }
1438         
1439          $cur = $this->con->openCursor($this->prefix.'post');
1440         
1441          $cur->cat_id = ($cat_id ? $cat_id : null);
1442          $cur->post_upddt = date('Y-m-d H:i:s');
1443         
1444          $cur->update($strReq);
1445          $this->triggerBlog();
1446     }
1447     
1448     /**
1449     Updates posts category. <var>$new_cat_id</var> can be null.
1450     
1451     @param    old_cat_id     <b>integer</b>      Old category ID
1452     @param    new_cat_id     <b>integer</b>      New category ID
1453     */
1454     public function changePostsCategory($old_cat_id,$new_cat_id)
1455     {
1456          if (!$this->core->auth->check('contentadmin,categories',$this->id)) {
1457               throw new Exception(__('You are not allowed to change entries category'));
1458          }
1459         
1460          $old_cat_id = (integer) $old_cat_id;
1461          $new_cat_id = (integer) $new_cat_id;
1462         
1463          $cur = $this->con->openCursor($this->prefix.'post');
1464         
1465          $cur->cat_id = ($new_cat_id ? $new_cat_id : null);
1466          $cur->post_upddt = date('Y-m-d H:i:s');
1467         
1468          $cur->update(
1469               'WHERE cat_id = '.$old_cat_id.' '.
1470               "AND blog_id = '".$this->con->escape($this->id)."' "
1471          );
1472          $this->triggerBlog();
1473     }
1474     
1475     /**
1476     Deletes a post.
1477     
1478     @param    id        <b>integer</b>      Post ID
1479     */
1480     public function delPost($id)
1481     {
1482          $this->delPosts($id);
1483     }
1484     
1485     /**
1486     Deletes multiple posts.
1487     
1488     @param    ids       <b>mixed</b>        Post(s) ID(s)
1489     */
1490     public function delPosts($ids)
1491     {
1492          if (!$this->core->auth->check('delete,contentadmin',$this->id)) {
1493               throw new Exception(__('You are not allowed to delete entries'));
1494          }
1495         
1496          $posts_ids = dcUtils::cleanIds($ids);
1497         
1498          if (empty($posts_ids)) {
1499               throw new Exception(__('No such entry ID'));
1500          }
1501         
1502          $strReq = 'DELETE FROM '.$this->prefix.'post '.
1503                    "WHERE blog_id = '".$this->con->escape($this->id)."' ".
1504                    "AND post_id ".$this->con->in($posts_ids);
1505         
1506          #If user can only delete, we need to check the post's owner
1507          if (!$this->core->auth->check('contentadmin',$this->id))
1508          {
1509               $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
1510          }
1511         
1512          $this->con->execute($strReq);
1513          $this->triggerBlog();
1514     }
1515     
1516     /**
1517     Publishes all entries flaged as "scheduled".
1518     */
1519     public function publishScheduledEntries()
1520     {
1521          $strReq = 'SELECT post_id, post_dt, post_tz '.
1522                    'FROM '.$this->prefix.'post '.
1523                    'WHERE post_status = -1 '.
1524                    "AND blog_id = '".$this->con->escape($this->id)."' ";
1525         
1526          $rs = $this->con->select($strReq);
1527         
1528          $now = dt::toUTC(time());
1529          $to_change = new ArrayObject();
1530
1531          if ($rs->isEmpty()) {
1532               return;
1533          }
1534         
1535          while ($rs->fetch())
1536          {
1537               # Now timestamp with post timezone
1538               $now_tz = $now + dt::getTimeOffset($rs->post_tz,$now);
1539               
1540               # Post timestamp
1541               $post_ts = strtotime($rs->post_dt);
1542               
1543               # If now_tz >= post_ts, we publish the entry
1544               if ($now_tz >= $post_ts) {
1545                    $to_change[] = (integer) $rs->post_id;
1546               }
1547          }
1548          if (count($to_change))
1549          {
1550               # --BEHAVIOR-- coreBeforeScheduledEntriesPublish
1551               $this->core->callBehavior('coreBeforeScheduledEntriesPublish',$this,$to_change);
1552
1553               $strReq =
1554               'UPDATE '.$this->prefix.'post SET '.
1555               'post_status = 1 '.
1556               "WHERE blog_id = '".$this->con->escape($this->id)."' ".
1557               'AND post_id '.$this->con->in((array)$to_change).' ';
1558               $this->con->execute($strReq);
1559               $this->triggerBlog();
1560
1561               # --BEHAVIOR-- coreAfterScheduledEntriesPublish
1562               $this->core->callBehavior('coreAfterScheduledEntriesPublish',$this,$to_change);
1563          }
1564         
1565     }
1566     
1567     /**
1568     Retrieves all users having posts on current blog.
1569     
1570     @param    post_type      <b>string</b>       post_type filter (post)
1571     @return   record
1572     */
1573     public function getPostsUsers($post_type='post')
1574     {
1575          $strReq = 'SELECT P.user_id, user_name, user_firstname, '.
1576                    'user_displayname, user_email '.
1577                    'FROM '.$this->prefix.'post P, '.$this->prefix.'user U '.
1578                    'WHERE P.user_id = U.user_id '.
1579                    "AND blog_id = '".$this->con->escape($this->id)."' ";
1580         
1581          if ($post_type) {
1582               $strReq .= "AND post_type = '".$this->con->escape($post_type)."' ";
1583          }
1584         
1585          $strReq .= 'GROUP BY P.user_id, user_name, user_firstname, user_displayname, user_email ';
1586         
1587          return $this->con->select($strReq);
1588     }
1589     
1590     private function getPostsCategoryFilter($arr,$field='cat_id')
1591     {
1592          $field = $field == 'cat_id' ? 'cat_id' : 'cat_url';
1593         
1594          $sub = array();
1595          $not = array();
1596          $queries = array();
1597         
1598          foreach ($arr as $v)
1599          {
1600               $v = trim($v);
1601               $args = preg_split('/\s*[?]\s*/',$v,-1,PREG_SPLIT_NO_EMPTY);
1602               $id = array_shift($args);
1603               $args = array_flip($args);
1604               
1605               if (isset($args['not'])) { $not[$id] = 1; }
1606               if (isset($args['sub'])) { $sub[$id] = 1; }
1607               if ($field == 'cat_id') {
1608                    if (preg_match('/^null$/i',$id)) {
1609                         $queries[$id] = 'P.cat_id IS NULL';
1610                    }
1611                    else {
1612                         $queries[$id] = 'P.cat_id = '.(integer) $id;
1613                    }
1614               } else {
1615                    $queries[$id] = "C.cat_url = '".$this->con->escape($id)."' ";
1616               }
1617          }
1618         
1619          if (!empty($sub)) {
1620               $rs = $this->con->select(
1621                    'SELECT cat_id, cat_url, cat_lft, cat_rgt FROM '.$this->prefix.'category '.
1622                    "WHERE blog_id = '".$this->con->escape($this->id)."' ".
1623                    'AND '.$field.' '.$this->con->in(array_keys($sub))
1624               );
1625               
1626               while ($rs->fetch()) {
1627                    $queries[$rs->f($field)] = '(C.cat_lft BETWEEN '.$rs->cat_lft.' AND '.$rs->cat_rgt.')';
1628               }
1629          }
1630         
1631          # Create queries
1632          $sql = array(
1633               0 => array(), # wanted categories
1634               1 => array()  # excluded categories
1635          );
1636         
1637          foreach ($queries as $id => $q) {
1638               $sql[(integer) isset($not[$id])][] = $q;
1639          }
1640         
1641          $sql[0] = implode(' OR ',$sql[0]);
1642          $sql[1] = implode(' OR ',$sql[1]);
1643         
1644          if ($sql[0]) {
1645               $sql[0] = '('.$sql[0].')';
1646          } else {
1647               unset($sql[0]);
1648          }
1649         
1650          if ($sql[1]) {
1651               $sql[1] = '(P.cat_id IS NULL OR NOT('.$sql[1].'))';
1652          } else {
1653               unset($sql[1]);
1654          }
1655         
1656          return implode(' AND ',$sql);
1657     }
1658     
1659     private function getPostCursor($cur,$post_id=null)
1660     {
1661          if ($cur->post_title == '') {
1662               throw new Exception(__('No entry title'));
1663          }
1664         
1665          if ($cur->post_content == '') {
1666               throw new Exception(__('No entry content'));
1667          }
1668         
1669          if ($cur->post_password === '') {
1670               $cur->post_password = null;
1671          }
1672         
1673          if ($cur->post_dt == '') {
1674               $offset = dt::getTimeOffset($this->core->auth->getInfo('user_tz'));
1675               $now = time() + $offset;
1676               $cur->post_dt = date('Y-m-d H:i:00',$now);
1677          }
1678         
1679          $post_id = is_int($post_id) ? $post_id : $cur->post_id;
1680         
1681          if ($cur->post_content_xhtml == '') {
1682               throw new Exception(__('No entry content'));
1683          }
1684         
1685          # Words list
1686          if ($cur->post_title !== null && $cur->post_excerpt_xhtml !== null
1687          && $cur->post_content_xhtml !== null)
1688          {
1689               $words =
1690               $cur->post_title.' '.
1691               $cur->post_excerpt_xhtml.' '.
1692               $cur->post_content_xhtml;
1693               
1694               $cur->post_words = implode(' ',text::splitWords($words));
1695          }
1696     }
1697     
1698     private function getPostContent($cur,$post_id)
1699     {
1700          $post_excerpt = $cur->post_excerpt;
1701          $post_excerpt_xhtml = $cur->post_excerpt_xhtml;
1702          $post_content = $cur->post_content;
1703          $post_content_xhtml = $cur->post_content_xhtml;
1704         
1705          $this->setPostContent(
1706               $post_id,$cur->post_format,$cur->post_lang,
1707               $post_excerpt,$post_excerpt_xhtml,
1708               $post_content,$post_content_xhtml
1709          );
1710         
1711          $cur->post_excerpt = $post_excerpt;
1712          $cur->post_excerpt_xhtml = $post_excerpt_xhtml;
1713          $cur->post_content = $post_content;
1714          $cur->post_content_xhtml = $post_content_xhtml;
1715     }
1716     
1717     /**
1718     Creates post HTML content, taking format and lang into account.
1719     
1720     @param         post_id        <b>integer</b>      Post ID
1721     @param         format         <b>string</b>       Post format
1722     @param         lang           <b>string</b>       Post lang
1723     @param         excerpt        <b>string</b>       Post excerpt
1724     @param[out]    excerpt_xhtml  <b>string</b>       Post excerpt HTML
1725     @param         content        <b>string</b>       Post content
1726     @param[out]    content_xhtml  <b>string</b>       Post content HTML
1727     */
1728     public function setPostContent($post_id,$format,$lang,&$excerpt,&$excerpt_xhtml,&$content,&$content_xhtml)
1729     {
1730          if ($format == 'wiki')
1731          {
1732               $this->core->initWikiPost();
1733               $this->core->wiki2xhtml->setOpt('note_prefix','pnote-'.$post_id);
1734               switch ($this->settings->system->note_title_tag) {
1735                    case 1:
1736                         $tag = 'h3';
1737                         break;
1738                    case 2:
1739                         $tag = 'p';
1740                         break;
1741                    default:
1742                         $tag = 'h4';
1743                         break;
1744               }
1745               $this->core->wiki2xhtml->setOpt('note_str','<div class="footnotes"><'.$tag.' class="footnotes-title">'.
1746                    __('Notes').'</'.$tag.'>%s</div>');
1747               $this->core->wiki2xhtml->setOpt('note_str_single','<div class="footnotes"><'.$tag.' class="footnotes-title">'.
1748                    __('Note').'</'.$tag.'>%s</div>');
1749               if (strpos($lang,'fr') === 0) {
1750                    $this->core->wiki2xhtml->setOpt('active_fr_syntax',1);
1751               }
1752          }
1753         
1754          if ($excerpt) {
1755               $excerpt_xhtml = $this->core->callFormater($format,$excerpt);
1756               $excerpt_xhtml = $this->core->HTMLfilter($excerpt_xhtml);
1757          } else {
1758               $excerpt_xhtml = '';
1759          }
1760         
1761          if ($content) {
1762               $content_xhtml = $this->core->callFormater($format,$content);
1763               $content_xhtml = $this->core->HTMLfilter($content_xhtml);
1764          } else {
1765               $content_xhtml = '';
1766          }
1767         
1768          # --BEHAVIOR-- coreAfterPostContentFormat
1769          $this->core->callBehavior('coreAfterPostContentFormat',array(
1770               'excerpt' => &$excerpt,
1771               'content' => &$content,
1772               'excerpt_xhtml' => &$excerpt_xhtml,
1773               'content_xhtml' => &$content_xhtml
1774          ));
1775     }
1776     
1777     /**
1778     Returns URL for a post according to blog setting <var>post_url_format</var>.
1779     It will try to guess URL and append some figures if needed.
1780     
1781     @param    url            <b>string</b>       Origin URL, could be empty
1782     @param    post_dt        <b>string</b>       Post date (in YYYY-MM-DD HH:mm:ss)
1783     @param    post_title     <b>string</b>       Post title
1784     @param    post_id        <b>integer</b>      Post ID
1785     @return   <b>string</b>  result URL
1786     */
1787     public function getPostURL($url,$post_dt,$post_title,$post_id)
1788     {
1789          $url = trim($url);
1790         
1791          $url_patterns = array(
1792          '{y}' => date('Y',strtotime($post_dt)),
1793          '{m}' => date('m',strtotime($post_dt)),
1794          '{d}' => date('d',strtotime($post_dt)),
1795          '{t}' => text::tidyURL($post_title),
1796          '{id}' => (integer) $post_id
1797          );
1798         
1799          # If URL is empty, we create a new one
1800          if ($url == '')
1801          {
1802               # Transform with format
1803               $url = str_replace(
1804                    array_keys($url_patterns),
1805                    array_values($url_patterns),
1806                    $this->settings->system->post_url_format
1807               );
1808          }
1809          else
1810          {
1811               $url = text::tidyURL($url);
1812          }
1813         
1814          # Let's check if URL is taken...
1815          $strReq = 'SELECT post_url FROM '.$this->prefix.'post '.
1816                    "WHERE post_url = '".$this->con->escape($url)."' ".
1817                    'AND post_id <> '.(integer) $post_id. ' '.
1818                    "AND blog_id = '".$this->con->escape($this->id)."' ".
1819                    'ORDER BY post_url DESC';
1820         
1821          $rs = $this->con->select($strReq);
1822         
1823          if (!$rs->isEmpty())
1824          {
1825               if ($this->con->driver() == 'mysql') {
1826                    $clause = "REGEXP '^".$this->con->escape($url)."[0-9]+$'";
1827               } elseif ($this->con->driver() == 'pgsql') {
1828                    $clause = "~ '^".$this->con->escape($url)."[0-9]+$'";
1829               } else {
1830                    $clause = "LIKE '".$this->con->escape($url)."%'";
1831               }
1832               $strReq = 'SELECT post_url FROM '.$this->prefix.'post '.
1833                         "WHERE post_url ".$clause.' '.
1834                         'AND post_id <> '.(integer) $post_id.' '.
1835                         "AND blog_id = '".$this->con->escape($this->id)."' ".
1836                         'ORDER BY post_url DESC ';
1837               
1838               $rs = $this->con->select($strReq);
1839               $a = array();
1840               while ($rs->fetch()) {
1841                    $a[] = $rs->post_url;
1842               }
1843               
1844               natsort($a);
1845               $t_url = end($a);
1846               
1847               if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) {
1848                    $i = (integer) $m[2];
1849                    $url = $m[1];
1850               } else {
1851                    $i = 1;
1852               }
1853               
1854               return $url.($i+1);
1855          }
1856         
1857          # URL is empty?
1858          if ($url == '') {
1859               throw new Exception(__('Empty entry URL'));
1860          }
1861         
1862          return $url;
1863     }
1864     //@}
1865     
1866     /// @name Comments management methods
1867     //@{
1868     /**
1869     Retrieves comments. <b>$params</b> is an array taking the following
1870     optionnal parameters:
1871     
1872     - no_content: Don't retrieve comment content
1873     - post_type: Get only entries with given type (default no type, array for many types)
1874     - post_id: (integer) Get comments belonging to given post_id
1875     - cat_id: (integer or array) Get comments belonging to entries of given category ID
1876     - comment_id: (integer) Get comment with given ID
1877     - comment_status: (integer) Get comments with given comment_status
1878     - comment_trackback: (integer) Get only comments (0) or trackbacks (1)
1879     - comment_ip: (string) Get comments with given IP address
1880     - post_url: Get entry with given post_url field
1881     - user_id: (integer) Get entries belonging to given user ID
1882     - q_author: Search comments by author
1883     - sql: Append SQL string at the end of the query
1884     - from: Append SQL string after "FROM" statement in query
1885     - order: Order of results (default "ORDER BY comment_dt DES")
1886     - limit: Limit parameter
1887     - sql_only : return the sql request instead of results. Only ids are selected
1888     
1889     @param    params         <b>array</b>        Parameters
1890     @param    count_only     <b>boolean</b>      Only counts results
1891     @return   <b>record</b>  A record with some more capabilities
1892     */
1893     public function getComments($params=array(),$count_only=false)
1894     {
1895          if ($count_only)
1896          {
1897               $strReq = 'SELECT count(comment_id) ';
1898          }
1899          elseif (!empty($params['sql_only'])) 
1900          {
1901               $strReq = 'SELECT P.post_id ';
1902          }
1903          else
1904          {
1905               if (!empty($params['no_content'])) {
1906                    $content_req = '';
1907               } else {
1908                    $content_req = 'comment_content, ';
1909               }
1910               
1911               if (!empty($params['columns']) && is_array($params['columns'])) {
1912                    $content_req .= implode(', ',$params['columns']).', ';
1913               }
1914               
1915               $strReq =
1916               'SELECT C.comment_id, comment_dt, comment_tz, comment_upddt, '.
1917               'comment_author, comment_email, comment_site, '.
1918               $content_req.' comment_trackback, comment_status, '.
1919               'comment_spam_status, comment_spam_filter, comment_ip, '.
1920               'P.post_title, P.post_url, P.post_id, P.post_password, P.post_type, '.
1921               'P.post_dt, P.user_id, U.user_email, U.user_url ';
1922          }
1923         
1924          $strReq .=
1925          'FROM '.$this->prefix.'comment C '.
1926          'INNER JOIN '.$this->prefix.'post P ON C.post_id = P.post_id '.
1927          'INNER JOIN '.$this->prefix.'user U ON P.user_id = U.user_id ';
1928         
1929          if (!empty($params['from'])) {
1930               $strReq .= $params['from'].' ';
1931          }
1932         
1933          $strReq .=
1934          "WHERE P.blog_id = '".$this->con->escape($this->id)."' ";
1935         
1936          if (!$this->core->auth->check('contentadmin',$this->id)) {
1937               $strReq .= 'AND ((comment_status = 1 AND P.post_status = 1 ';
1938               
1939               if ($this->without_password) {
1940                    $strReq .= 'AND post_password IS NULL ';
1941               }
1942               $strReq .= ') ';
1943               
1944               if ($this->core->auth->userID()) {
1945                    $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')";
1946               } else {
1947                    $strReq .= ') ';
1948               }
1949          }
1950         
1951          if (!empty($params['post_type']))
1952          {
1953               $strReq .= 'AND post_type '.$this->con->in($params['post_type']);
1954          }
1955         
1956          if (isset($params['post_id']) && $params['post_id'] !== '') {
1957               $strReq .= 'AND P.post_id = '.(integer) $params['post_id'].' ';
1958          }
1959         
1960          if (isset($params['cat_id']) && $params['cat_id'] !== '') {
1961               $strReq .= 'AND P.cat_id = '.(integer) $params['cat_id'].' ';
1962          }
1963         
1964          if (isset($params['comment_id']) && $params['comment_id'] !== '') {
1965               $strReq .= 'AND comment_id = '.(integer) $params['comment_id'].' ';
1966          }
1967         
1968          if (isset($params['comment_status'])) {
1969               $strReq .= 'AND comment_status = '.(integer) $params['comment_status'].' ';
1970          }
1971         
1972          if (!empty($params['comment_status_not']))
1973          {
1974               $strReq .= 'AND comment_status <> '.(integer) $params['comment_status_not'].' ';
1975          }
1976         
1977          if (isset($params['comment_trackback'])) {
1978               $strReq .= 'AND comment_trackback = '.(integer) (boolean) $params['comment_trackback'].' ';
1979          }
1980         
1981          if (isset($params['comment_ip'])) {
1982               $comment_ip = $this->con->escape(str_replace('*','%',$params['comment_ip']));
1983               $strReq .= "AND comment_ip LIKE '".$comment_ip."' ";
1984          }
1985         
1986          if (isset($params['q_author'])) {
1987               $q_author = $this->con->escape(str_replace('*','%',strtolower($params['q_author'])));
1988               $strReq .= "AND LOWER(comment_author) LIKE '".$q_author."' ";
1989          }
1990         
1991          if (!empty($params['search']))
1992          {
1993               $words = text::splitWords($params['search']);
1994               
1995               if (!empty($words))
1996               {
1997                    # --BEHAVIOR coreCommentSearch
1998                    if ($this->core->hasBehavior('coreCommentSearch')) {
1999                         $this->core->callBehavior('coreCommentSearch',$this->core,array(&$words,&$strReq,&$params));
2000                    }
2001                   
2002                    if ($words)
2003                    {
2004                         foreach ($words as $i => $w) {
2005                              $words[$i] = "comment_words LIKE '%".$this->con->escape($w)."%'";
2006                         }
2007                         $strReq .= 'AND '.implode(' AND ',$words).' ';
2008                    }
2009               }
2010          }
2011         
2012          if (!empty($params['sql'])) {
2013               $strReq .= $params['sql'].' ';
2014          }
2015         
2016          if (!$count_only)
2017          {
2018               if (!empty($params['order'])) {
2019                    $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' ';
2020               } else {
2021                    $strReq .= 'ORDER BY comment_dt DESC ';
2022               }
2023          }
2024         
2025          if (!$count_only && !empty($params['limit'])) {
2026               $strReq .= $this->con->limit($params['limit']);
2027          }
2028
2029          if (!empty($params['sql_only'])) {
2030               return $strReq;
2031          }
2032         
2033          $rs = $this->con->select($strReq);
2034          $rs->core = $this->core;
2035          $rs->extend('rsExtComment');
2036         
2037          # --BEHAVIOR-- coreBlogGetComments
2038          $this->core->callBehavior('coreBlogGetComments',$rs);
2039         
2040          return $rs;
2041     }
2042     
2043     /**
2044     Creates a new comment. Takes a cursor as input and returns the new comment
2045     ID.
2046     
2047     @param    cur       <b>cursor</b>       Comment cursor
2048     @return   <b>integer</b>      New comment ID
2049     */
2050     public function addComment($cur)
2051     {
2052          $this->con->writeLock($this->prefix.'comment');
2053          try
2054          {
2055               # Get ID
2056               $rs = $this->con->select(
2057                    'SELECT MAX(comment_id) '.
2058                    'FROM '.$this->prefix.'comment ' 
2059               );
2060               
2061               $cur->comment_id = (integer) $rs->f(0) + 1;
2062               $cur->comment_upddt = date('Y-m-d H:i:s');
2063               
2064               $offset = dt::getTimeOffset($this->settings->system->blog_timezone);
2065               $cur->comment_dt = date('Y-m-d H:i:s',time() + $offset);
2066               $cur->comment_tz = $this->settings->system->blog_timezone;
2067               
2068               $this->getCommentCursor($cur);
2069               
2070               if ($cur->comment_ip === null) {
2071                    $cur->comment_ip = http::realIP();
2072               }
2073               
2074               # --BEHAVIOR-- coreBeforeCommentCreate
2075               $this->core->callBehavior('coreBeforeCommentCreate',$this,$cur);
2076               
2077               $cur->insert();
2078               $this->con->unlock();
2079          }
2080          catch (Exception $e)
2081          {
2082               $this->con->unlock();
2083               throw $e;
2084          }
2085         
2086          # --BEHAVIOR-- coreAfterCommentCreate
2087          $this->core->callBehavior('coreAfterCommentCreate',$this,$cur);
2088         
2089          $this->triggerComment($cur->comment_id);
2090          if ($cur->comment_status != -2) {
2091               $this->triggerBlog();
2092          }   
2093          return $cur->comment_id;
2094     }
2095     
2096     /**
2097     Updates an existing comment.
2098     
2099     @param    id        <b>integer</b>      Comment ID
2100     @param    cur       <b>cursor</b>       Comment cursor
2101     */
2102     public function updComment($id,$cur)
2103     {
2104          if (!$this->core->auth->check('usage,contentadmin',$this->id)) {
2105               throw new Exception(__('You are not allowed to update comments'));
2106          }
2107         
2108          $id = (integer) $id;
2109         
2110          if (empty($id)) {
2111               throw new Exception(__('No such comment ID'));
2112          }
2113         
2114          $rs = $this->getComments(array('comment_id' => $id));
2115         
2116          if ($rs->isEmpty()) {
2117               throw new Exception(__('No such comment ID'));
2118          }
2119         
2120          #If user is only usage, we need to check the post's owner
2121          if (!$this->core->auth->check('contentadmin',$this->id))
2122          {
2123               if ($rs->user_id != $this->core->auth->userID()) {
2124                    throw new Exception(__('You are not allowed to update this comment'));
2125               }
2126          }
2127         
2128          $this->getCommentCursor($cur);
2129         
2130          $cur->comment_upddt = date('Y-m-d H:i:s');
2131         
2132          if (!$this->core->auth->check('publish,contentadmin',$this->id)) {
2133               $cur->unsetField('comment_status');
2134          }
2135         
2136          # --BEHAVIOR-- coreBeforeCommentUpdate
2137          $this->core->callBehavior('coreBeforeCommentUpdate',$this,$cur,$rs);
2138         
2139          $cur->update('WHERE comment_id = '.$id.' ');
2140         
2141          # --BEHAVIOR-- coreAfterCommentUpdate
2142          $this->core->callBehavior('coreAfterCommentUpdate',$this,$cur,$rs);
2143         
2144          $this->triggerComment($id);
2145          $this->triggerBlog();
2146     }
2147     
2148     /**
2149     Updates comment status.
2150     
2151     @param    id        <b>integer</b>      Comment ID
2152     @param    status    <b>integer</b>      Comment status
2153     */
2154     public function updCommentStatus($id,$status)
2155     {
2156          $this->updCommentsStatus($id,$status);
2157     }
2158     
2159     /**
2160     Updates comments status.
2161     
2162     @param    ids       <b>mixed</b>        Comment(s) ID(s)
2163     @param    status    <b>integer</b>      Comment status
2164     */
2165     public function updCommentsStatus($ids,$status)
2166     {
2167          if (!$this->core->auth->check('publish,contentadmin',$this->id)) {
2168               throw new Exception(__("You are not allowed to change this comment's status"));
2169          }
2170         
2171          $co_ids = dcUtils::cleanIds($ids);
2172          $status = (integer) $status;
2173         
2174          $strReq = 
2175               'UPDATE '.$this->prefix.'comment tc ';
2176         
2177          # mySQL uses "JOIN" synthax
2178          if ($this->con->driver() == 'mysql') {
2179               $strReq .= 
2180                    'JOIN '.$this->prefix.'post tp ON tc.post_id = tp.post_id ';
2181          }
2182         
2183          $strReq .= 
2184               'SET comment_status = '.$status.' ';
2185         
2186          # pgSQL uses "FROM" synthax
2187          if ($this->con->driver() != 'mysql') {
2188               $strReq .= 
2189                    'FROM '.$this->prefix.'post tp ';
2190          }
2191         
2192          $strReq .=
2193               "WHERE blog_id = '".$this->con->escape($this->id)."' ".
2194               'AND comment_id'.$this->con->in($co_ids);
2195         
2196          # add pgSQL "WHERE" clause
2197          if ($this->con->driver() != 'mysql') {
2198               $strReq .= 
2199                    'AND tc.post_id = tp.post_id ';
2200          }
2201         
2202          #If user is only usage, we need to check the post's owner
2203          if (!$this->core->auth->check('contentadmin',$this->id))
2204          {
2205               $strReq .= 
2206                    "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
2207          }
2208         
2209          $this->con->execute($strReq);
2210          $this->triggerComments($co_ids);
2211          $this->triggerBlog();
2212     }
2213     
2214     /**
2215     Delete a comment
2216     
2217     @param    id        <b>integer</b>      Comment ID
2218     */
2219     public function delComment($id)
2220     {
2221          $this->delComments($id);
2222     }
2223     
2224     /**
2225     Delete comments
2226     
2227     @param    ids       <b>mixed</b>        Comment(s) ID(s)
2228     */
2229     public function delComments($ids)
2230     {
2231          if (!$this->core->auth->check('delete,contentadmin',$this->id)) {
2232               throw new Exception(__('You are not allowed to delete comments'));
2233          }
2234         
2235          $co_ids = dcUtils::cleanIds($ids);
2236         
2237          if (empty($ids)) {
2238               throw new Exception(__('No such comment ID'));
2239          }
2240         
2241          # mySQL uses "INNER JOIN" synthax
2242          if ($this->con->driver() == 'mysql') {
2243               $strReq = 
2244                    'DELETE FROM tc '.
2245                    'USING '.$this->prefix.'comment tc '.
2246                    'INNER JOIN '.$this->prefix.'post tp ';
2247          }
2248          # pgSQL uses nothing special
2249          else {
2250               $strReq = 
2251                    'DELETE FROM '.$this->prefix.'comment tc '.
2252                    'USING '.$this->prefix.'post tp ';
2253          }
2254         
2255          $strReq .= 
2256               'WHERE tc.post_id = tp.post_id '.
2257               "AND tp.blog_id = '".$this->con->escape($this->id)."' ".
2258               'AND comment_id'.$this->con->in($co_ids);
2259         
2260          #If user can only delete, we need to check the post's owner
2261          if (!$this->core->auth->check('contentadmin',$this->id))
2262          {
2263               $strReq .= 
2264                    "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
2265          }
2266         
2267          $this->con->execute($strReq);
2268          $this->triggerComments($co_ids,true);
2269          $this->triggerBlog();
2270     }
2271
2272     public function delJunkComments()
2273     {
2274          if (!$this->core->auth->check('delete,contentadmin',$this->id)) {
2275               throw new Exception(__('You are not allowed to delete comments'));
2276          }
2277         
2278          # mySQL uses "INNER JOIN" synthax
2279          if ($this->con->driver() == 'mysql') {
2280               $strReq = 
2281                    'DELETE FROM tc '.
2282                    'USING '.$this->prefix.'comment tc '.
2283                    'INNER JOIN '.$this->prefix.'post tp ';
2284          }
2285          # pgSQL uses nothing special
2286          else {
2287               $strReq = 
2288                    'DELETE FROM '.$this->prefix.'comment tc '.
2289                    'USING '.$this->prefix.'post tp ';
2290          }
2291         
2292          $strReq .= 
2293               'WHERE tc.post_id = tp.post_id '.
2294               "AND tp.blog_id = '".$this->con->escape($this->id)."' ".
2295               'AND comment_status = -2';
2296         
2297          #If user can only delete, we need to check the post's owner
2298          if (!$this->core->auth->check('contentadmin',$this->id))
2299          {
2300               $strReq .= 
2301                    "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
2302          }
2303         
2304          $this->con->execute($strReq);
2305          $this->triggerBlog();
2306     }
2307     
2308     private function getCommentCursor($cur)
2309     {
2310          if ($cur->comment_content !== null && $cur->comment_content == '') {
2311               throw new Exception(__('You must provide a comment'));
2312          }
2313         
2314          if ($cur->comment_author !== null && $cur->comment_author == '') {
2315               throw new Exception(__('You must provide an author name'));
2316          }
2317         
2318          if ($cur->comment_email != '' && !text::isEmail($cur->comment_email)) {
2319               throw new Exception(__('Email address is not valid.'));
2320          }
2321         
2322          if ($cur->comment_site !== null && $cur->comment_site != '') {
2323               if (!preg_match('|^http(s?)://|',$cur->comment_site)) {
2324                    $cur->comment_site = 'http://'.$cur->comment_site;
2325               }
2326          }
2327         
2328          if ($cur->comment_status === null) {
2329               $cur->comment_status = (integer) $this->settings->system->comments_pub;
2330          }
2331         
2332          # Words list
2333          if ($cur->comment_content !== null)
2334          {
2335               $cur->comment_words = implode(' ',text::splitWords($cur->comment_content));
2336          }
2337     }
2338     //@}
2339}
2340?>
Note: See TracBrowser for help on using the repository browser.

Sites map