Dotclear

source: plugins/agora/inc/class.agora.php @ 1885

Revision 1885, 26.1 KB checked in by Osku, 14 years ago (diff)

agora - mini commit

  • Property svn:executable set to *
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of agora, a plugin for Dotclear 2.
5#
6# Copyright (c) 2009 Osku , Tomtom and contributors
7## Licensed under the GPL version 2.0 license.
8# A copy of this license is available in LICENSE file or at
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK ------------------------------------
12
13class agora 
14{
15     private $user_status = array();
16     private $message_status = array();
17     
18     public function __construct($core)
19     {
20          $this->con =& $core->con;
21          $this->prefix =& $core->prefix;
22          $this->core =& $core;
23         
24          $this->user_status['-1'] = __('pending');
25          $this->user_status['0'] = __('suspended');
26          $this->user_status['1'] = __('active');
27         
28          $this->message_status['-2'] = __('junk');
29          $this->message_status['-1'] = __('pending');
30          $this->message_status['0'] = __('unpublished');
31          $this->message_status['1'] = __('published');
32         
33          $this->core->auth = new dcPublicAuth($core);
34          $this->core->log = new dcLog($core);
35     }
36     
37     public function getUser($id)
38     {
39          $params['user_id'] = $id;
40         
41          return $this->getUsers($params);
42     }
43     
44     public function getUsers($params=array(),$count_only=false)
45     {
46          if ($count_only)
47          {
48               $strReq =
49               'SELECT count(U.user_id) '.
50               'FROM '.$this->prefix.'user U '.
51               'WHERE NULL IS NULL ';
52          }
53          else
54          {
55               $strReq =
56               'SELECT U.user_id,user_super,user_status,user_pwd,user_name,'.
57               'user_firstname,user_displayname,user_email,user_url,'.
58               'user_desc, user_lang,user_tz, user_post_status,user_options, '.
59               'user_creadt, user_upddt, '.
60               'count(P.post_id) AS nb_post '.
61               'FROM '.$this->prefix.'user U '.
62                    'LEFT JOIN '.$this->prefix.'post P ON U.user_id = P.user_id '.
63               'WHERE NULL IS NULL ';
64          }
65         
66          if (!empty($params['q'])) {
67               $q = $this->con->escape(str_replace('*','%',strtolower($params['q'])));
68               $strReq .= 'AND ('.
69                    "LOWER(U.user_id) LIKE '".$q."' ".
70                    "OR LOWER(user_name) LIKE '".$q."' ".
71                    "OR LOWER(user_firstname) LIKE '".$q."' ".
72                    ') ';
73          }
74         
75          if (!empty($params['user_id'])) {
76               $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."' ";
77          }
78         
79          if (!$count_only) {
80               $strReq .= 'GROUP BY U.user_id,user_super,user_status,user_pwd,user_name,'.
81               'user_firstname,user_displayname,user_email,user_url,'.
82               'user_desc, user_lang,user_tz,user_post_status,user_options ';
83               
84               if (!empty($params['order']) && !$count_only) {
85                    $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' ';
86               } else {
87                    $strReq .= 'ORDER BY U.user_id ASC ';
88               }
89          }
90         
91          if (!$count_only && !empty($params['limit'])) {
92               $strReq .= $this->con->limit($params['limit']);
93          }
94         
95          $rs = $this->con->select($strReq);
96          $rs->user_creadt = strtotime($rs->user_creadt);
97          $rs->user_upddt = strtotime($rs->user_upddt);
98          $rs->extend('rsExtUser');
99          return $rs;
100     }
101     
102     public function getLogsLastVisit($params=array(), $count_only=false)
103     {
104          $params['log_msg'] = 'lastvisit';
105          $rs = $this->core->log->getLogs($params,$count_only);
106          return $rs;
107     }
108
109     public function getLastVisitUser($user_id)
110     {
111          $params['user_id'] = $user_id;
112          $rs = $this->getLogsLastVisit($params);
113          return $rs;
114     }
115
116     public function getAllUserStatus()
117     {
118          return $this->user_status;
119     }
120     
121     public function getUserStatus($s)
122     {
123          if (isset($this->user_status[$s])) {
124               return $this->user_status[$s];
125          }
126          return $this->user_status['1'];
127     }
128     
129     public function getUnregistredUser($recover_key)
130     {
131          $strReq = 'SELECT user_id, user_status '.
132          'FROM '.$this->prefix.'user U '.
133          "WHERE user_recover_key = '".$this->con->escape($recover_key)."' ";
134
135          $rs = $this->con->select($strReq);
136         
137          if ($rs->isEmpty()) {
138               throw new Exception(__('This is a wrong registration URL. Registration failed.'));
139          }
140
141          $cur = $this->con->openCursor($this->prefix.'user');
142          $cur->user_recover_key = null;
143         
144          $cur->update("WHERE user_recover_key = '".$this->con->escape($recover_key)."'");
145         
146          return array('user_status' => $rs->user_status, 'user_id' => $rs->user_id);
147     }
148     
149     public function userlogIn($login,$passwd,$key = '')
150     {
151          $key = empty($key) ? null : $key;
152         
153          //if (!$this->core->auth->checkUser($login,$passwd,$key) || (!$this->isMember($login)))
154          // As dcAuth checkUser through findUserBlog need a 'usage' perm, we use dcPublicAuth::checkUser
155          if (empty($passwd))
156          {
157               throw new Exception(__('Cannot login. Empty password.'));
158          }
159         
160          if (!$this->core->auth->checkPublicUser($login,$passwd,$key))
161          {
162               throw new Exception(__('Cannot login. Check.'));
163          }
164          elseif ($this->isMember($login) === false)
165          {
166               throw new Exception(__('User is not a member of forum'));
167          }
168          else
169          {
170               $this->core->session->start();
171               $_SESSION['sess_user_id'] = $login;
172               $_SESSION['sess_browser_uid'] = http::browserUID(DC_MASTER_KEY);
173               $_SESSION['sess_blog_id'] = $this->core->blog->id;
174               $_SESSION['sess_user_lastseen'] = $this->getLastVisitUser($login);
175               $_SESSION['sess_forum'] = 1;
176               if (isset($_POST['li_remember'])) {
177                    $cookie_forum =
178                    http::browserUID(DC_MASTER_KEY.$login.crypt::hmac(DC_MASTER_KEY,$passwd)).
179                    bin2hex(pack('a32',$login));
180                    setcookie('dc_forum_'.$this->core->blog->id,$cookie_forum,strtotime('+15 days'));
181               }
182               
183               // later, we may set the cookie for comments...
184               //$name = (string)dcUtils::getUserCN($this->core->auth->userID(),$this->core->auth->getInfo('user_name'),$this->core->auth->getInfo('user_firstname'),$this->core->auth->getInfo('user_displayname'));
185               //$mail = $this->core->auth->getInfo('user_email');
186               //$site = $this->core->auth->getInfo('user_url');
187               //setrawcookie('comment_info',rawurlencode($name."\n".$mail."\n".$site),strtotime('+30 days'));
188
189               return $login;
190          }
191         
192     }
193
194     public function sendActivationEmail($mail,$user_id,$pwd)
195     {
196          $key = $this->core->auth->setRecoverKey($user_id,$mail);
197          $link = $this->core->blog->url.$this->core->url->getBase('register');
198          $link .= strpos($link,'?') !== false ? '&' : '?';
199          $url_forum = $this->core->url->getBase('forum');
200          $url_login = $this->core->url->getBase('login');
201          $sub = __('Account confirmation request on Agora');
202          $msg = 
203          sprintf(__('Welcome to the forum of %s'),$this->core->blog->name)."\n".
204          __('To activate your account and verify your e-mail address, please click on the following link:').
205          "\n\n".
206          $link.'key='.$key.
207          "\n\n".
208          __('Your indormations:').
209          sprintf(__('Login: %s'),$user_id)."\n".
210          sprintf(__('Password: %s'),$pwd)."\n".
211          __('Agora connection:').
212          $url_login.
213          "\n\n".
214          __('If you have received this mail in error, you do not need to take any action to cancel the account.').
215          __('The account will not be activated, and you will not receive any further emails.').
216          __('If clicking the link above does not work, copy and paste the URL in a new browser window instead.').
217          "\n\n".
218          __('Thank you for particape to our agora.').
219          "\n\n".
220          __('This is a post-only mailing. Replies to this message are not monitored or answered.').
221          "\n\n";   
222
223          $this->sendEmail($mail,$sub,$msg);
224     }
225
226     public function sendRecoveryEmail($mail,$key)
227     {
228         
229          $this->sendEmail($mail,$sub,$msg);
230     }
231     
232     public function sendNewPasswordEmail($mail,$user_id,$pwd)
233     {
234         
235          $this->sendEmail($mail,$sub,$msg);
236     }
237     
238     protected function sendEmail($dest,$sub,$msg)
239     {
240          $headers = array(
241          'From: '.mail::B64Header($this->core->blog->name).' forum <no-reply@'.str_replace('http://','',http::getHost()).' >',
242          'Content-Type: text/plain; charset=UTF-8;',
243          'X-Originating-IP: '.http::realIP(),
244          'X-Mailer: Dotclear',
245          'X-Blog-Id: '.mail::B64Header($this->core->blog->id),
246          'X-Blog-Name: '.mail::B64Header($this->core->blog->name),
247          'X-Blog-Url: '.mail::B64Header($this->core->blog->url)
248          );
249         
250          $sub = '['.$this->core->blog->name.'] '.$sub;
251          $sub = mail::B64Header($sub);
252         
253          mail::sendMail($dest,$sub,$msg,$headers);
254     }
255
256     private function getPostsCategoryFilter($arr,$field='cat_id')
257     {
258          $field = $field == 'cat_id' ? 'cat_id' : 'cat_url';
259         
260          $sub = array();
261          $not = array();
262          $queries = array();
263         
264          foreach ($arr as $v)
265          {
266               $v = trim($v);
267               $args = preg_split('/\s*[?]\s*/',$v,-1,PREG_SPLIT_NO_EMPTY);
268               $id = array_shift($args);
269               $args = array_flip($args);
270               
271               if (isset($args['not'])) { $not[$id] = 1; }
272               if (isset($args['sub'])) { $sub[$id] = 1; }
273               if ($field == 'cat_id') {
274                    $queries[$id] = 'P.cat_id = '.(integer) $id;
275               } else {
276                    $queries[$id] = "C.cat_url = '".$this->con->escape($id)."' ";
277               }
278          }
279         
280          if (!empty($sub)) {
281               $rs = $this->con->select(
282                    'SELECT cat_id, cat_url, cat_lft, cat_rgt FROM '.$this->prefix.'category '.
283                    "WHERE blog_id = '".$this->con->escape($this->id)."' ".
284                    'AND '.$field.' '.$this->con->in(array_keys($sub))
285               );
286               
287               while ($rs->fetch()) {
288                    $queries[$rs->f($field)] = '(C.cat_lft BETWEEN '.$rs->cat_lft.' AND '.$rs->cat_rgt.')';
289               }
290          }
291         
292          # Create queries
293          $sql = array(
294               0 => array(), # wanted categories
295               1 => array()  # excluded categories
296          );
297         
298          foreach ($queries as $id => $q) {
299               $sql[(integer) isset($not[$id])][] = $q;
300          }
301         
302          $sql[0] = implode(' OR ',$sql[0]);
303          $sql[1] = implode(' OR ',$sql[1]);
304         
305          if ($sql[0]) {
306               $sql[0] = '('.$sql[0].')';
307          } else {
308               unset($sql[0]);
309          }
310         
311          if ($sql[1]) {
312               $sql[1] = '(P.cat_id IS NULL OR NOT('.$sql[1].'))';
313          } else {
314               unset($sql[1]);
315          }
316         
317          return implode(' AND ',$sql);
318     }
319
320     public function triggerThread($id)
321     {
322          /*$strReq = 'SELECT COUNT(post_id) '.
323                    'FROM '.$this->prefix.'post '.
324                    'WHERE thread_id = '.(integer) $id.' '.
325                    'AND post_status = 1 ';
326         
327          $rs = $this->con->select($strReq);*/
328         
329          $cur = $this->con->openCursor($this->prefix.'post');
330         
331          /*if ($rs->isEmpty()) {
332               return;
333          }
334          */
335          //$cur->nb_comment = (integer) $rs->f(0);
336          $cur->post_dt = date('Y-m-d H:i:s');
337         
338          $cur->update('WHERE post_id = '.(integer) $id);
339     }
340
341     public function getThreadURL($rs)
342     {
343          $thread_id = $rs->thread_id;
344         
345          $strReq = 'SELECT post_url '.
346                    'FROM '.$this->prefix.'post '.
347                    'WHERE post_id = '.(integer) $thread_id.' ';
348         
349          $rs = $this->con->select($strReq);
350         
351          if ($rs->isEmpty()) {
352               return;
353          }
354         
355          return $rs->post_url;
356     }
357
358     /**
359     Retrieves categories. <var>$params</var> is an associative array which can
360     take the following parameters:
361     
362     - post_type: Get only entries with given type (default "post")
363     - cat_url: filter on cat_url field
364     - cat_id: filter on cat_id field
365     - start: start with a given category
366     - level: categories level to retrieve
367     - with_empty: filter empty categories
368     
369     @param    params    <b>array</b>        Parameters
370     @return   <b>record</b>
371     */
372     public function getCategoriesPlus($params=array())
373     {
374          // From /inc/core/class.dc.blog.php getCategories
375          //Just authorize Empty Categories
376          $c_params = array();
377          if (isset($params['post_type'])) {
378               $c_params['post_type'] = $params['post_type'];
379               unset($params['post_type']);
380          }
381          $counter = $this->getCategoriesCounter($c_params);
382          $counter2 = $this->getCategoriesCounter($c_params,true);
383         
384          //$without_empty = isset($params['without_empty']) ? (bool) $params['without_empty'] : ($this->core->auth->userID() == false);
385          //$with_empty = isset($params['with_empty']) ? (bool) $params['with_empty'] : ($this->core->auth->userID() == false);
386          //if (isset($params['with_empty'])) //&& ($params['with_empty'])))
387          //{
388          //   $with_empty = true;
389          //} else {
390          //   $with_empty = $this->core->auth->userID() != false; # For public display   $this->core->auth->userID() !=
391          //}
392         
393          $start = isset($params['start']) ? (integer) $params['start'] : 0;
394          $l = isset($params['level']) ? (integer) $params['level'] : 0;
395         
396          $rs = $this->core->blog->categories()->getChildren($start,null,'desc');
397         
398          # Get each categories total posts count
399          $data = array();
400          $stack = array();
401          $stack2 = array();
402          $level = 0;
403          $cols = $rs->columns();
404          while ($rs->fetch())
405          {
406               $nb_post = isset($counter[$rs->cat_id]) ? (integer) $counter[$rs->cat_id] : 0;
407               $nb_answer = isset($counter2[$rs->cat_id]) ? (integer) $counter2[$rs->cat_id] : 0;
408               
409               if ($rs->level > $level) {
410                    $nb_total = $nb_post;
411                    $stack[$rs->level] = (integer) $nb_post;
412                    $nb_total2 = $nb_answer;
413                    $stack2[$rs->level] = (integer) $nb_answer;
414               } elseif ($rs->level == $level) {
415                    $nb_total = $nb_post;
416                    $stack[$rs->level] += $nb_post;
417                    $nb_total2 = $nb_answer;
418                    $stack2[$rs->level] += $nb_answer;
419               } else {
420                    $nb_total = $stack[$rs->level+1] + $nb_post;
421                    $nb_total2 = $stack2[$rs->level+1] + $nb_answer;
422                    if (isset($stack[$rs->level])) {
423                         $stack[$rs->level] += $nb_total;
424                         $stack2[$rs->level] += $nb_answer;
425                    } else {
426                         $stack[$rs->level] = $nb_total;
427                         $stack2[$rs->level] = $nb_total2;
428                    }
429                    unset($stack[$rs->level+1]);
430                    unset($stack2[$rs->level+1]);
431               }
432               
433               //if (($nb_total == 0) && true) {
434               //   continue;
435               //}
436               
437               $level = $rs->level;
438               
439               $t = array();
440               foreach ($cols as $c) {
441                    $t[$c] = $rs->f($c);
442               }
443               $t['nb_post'] = $nb_post;
444               $t['nb_total'] = $nb_total;
445               $t['nb_answer'] = $nb_answer;
446               $t['nb_total2'] = $nb_total2;
447               
448               if ($l == 0 || ($l > 0 && $l == $rs->level)) {
449                    array_unshift($data,$t);
450               }
451          }
452         
453          # We need to apply filter after counting
454          if (!empty($params['cat_id']))
455          {
456               $found = false;
457               foreach ($data as $v) {
458                    if ($v['cat_id'] == $params['cat_id']) {
459                         $found = true;
460                         $data = array($v);
461                         break;
462                    }
463               }
464               if (!$found) {
465                    $data = array();
466               }
467          }
468         
469          if (!empty($params['cat_url']) && empty($params['cat_id']))
470          {
471               $found = false;
472               foreach ($data as $v) {
473                    if ($v['cat_url'] == $params['cat_url']) {
474                         $found = true;
475                         $data = array($v);
476                         break;
477                    }
478               }
479               if (!$found) {
480                    $data = array();
481               }
482          }
483         
484          return staticRecord::newFromArray($data);
485     }
486
487     private function getCategoriesCounter($params=array(),$bis=false)
488     {
489          $strReq =
490          'SELECT  C.cat_id, COUNT(P.post_id) AS nb_post, SUM(P.nb_comment) AS nb_answer '.
491          'FROM '.$this->prefix.'category AS C '.
492          'JOIN '.$this->prefix."post P ON (C.cat_id = P.cat_id AND P.blog_id = '".$this->con->escape($this->core->blog->id)."' ) ".
493          "WHERE C.blog_id = '".$this->con->escape($this->core->blog->id)."' ";
494         
495          if (!$this->core->auth->userID()) {
496               $strReq .= 'AND P.post_status = 1 ';
497          }
498         
499          if (!empty($params['post_type'])) {
500               $strReq .= "AND post_type = '".$this->con->escape($params['post_type'])."' ";
501          }
502          else {
503               $strReq .= "AND post_type = 'threadpost' ";
504          }
505         
506          //$strReq .= 'AND P.thread_id is NULL ';
507         
508          $strReq .= 'GROUP BY C.cat_id ';
509         
510          $rs = $this->con->select($strReq);
511          $counters = array();
512          $counters2 = array();
513          while ($rs->fetch()) {
514               $counters[$rs->cat_id] = $rs->nb_post;
515               $counters2[$rs->cat_id] = $rs->nb_answer;
516          }
517         
518          if ($bis) {
519               return $counters2;
520          } else {
521               return $counters;
522          }
523     }
524
525     public function getCategoryFirstChildren($id)
526     {
527          return $this->getCategoriesPlus(array('start' => $id,'level' => $id == 0 ? 1 : 2));
528     }
529
530     public function updPostClosed($id,$closed)
531     {
532          if (!$this->core->auth->check('usage,contentadmin',$this->core->blog->id)) {
533               throw new Exception(__('You are not allowed to close this thread'));
534          }
535         
536          $id = (integer) $id;
537          $closed = (boolean) $closed;
538         
539          # If user is only usage, we need to check the post's owner
540          if (!$this->core->auth->check('contentadmin',$this->core->blog->id))
541          {
542               $strReq = 'SELECT post_id '.
543                         'FROM '.$this->prefix.'post '.
544                         'WHERE post_id = '.$id.' '.
545                         "AND blog_id = '".$this->con->escape($this->core->blog->id)."' ".
546                         "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
547               
548               $rs = $this->con->select($strReq);
549               
550               if ($rs->isEmpty()) {
551                    throw new Exception(__('You are not allowed to mark this entry as closed'));
552               }
553          }
554         
555          $cur = $this->con->openCursor($this->prefix.'post');
556         
557          $cur->post_open_comment = (integer) $closed;
558          $cur->post_upddt = date('Y-m-d H:i:s');
559         
560          $cur->update(
561               'WHERE post_id = '.$id.' '.
562               "AND blog_id = '".$this->con->escape($this->core->blog->id)."' "
563          );
564          $this->core->blog->triggerBlog();
565     }
566
567     public function isMember($user_id)
568     {
569          return $this->hasUserPerm($user_id,'member');
570     }
571     
572     public function isModerator($user_id)
573     {
574          return $this->hasUserPerm($user_id,'moderator');
575     }
576
577     public function hasUserPerm($user_id,$perm)
578     {
579          $res = $this->core->getUserPermissions($user_id);
580          $blog_id = $this->core->blog->id;
581         
582          if (!empty($res))
583          {
584               if (array_key_exists($perm,$res[$blog_id]['p'])) {
585                    return true;
586               }
587          }
588          return false;
589     }
590     
591     public function getAllMessageStatus()
592     {
593          return $this->message_status;
594     }
595     
596     public function triggerMessage($id,$del=false)
597     {
598          global $core;
599          $id = (integer) $id;
600         
601          $strReq = 'SELECT post_id '.
602                    'FROM '.$this->prefix.'message '.
603                    'WHERE message_id = '.$id.' ';
604         
605          $rs = $this->con->select($strReq);
606
607          if ($rs->isEmpty()) {
608               return;
609          }
610         
611          $post_id = $rs->post_id;
612         
613          $strReq = 'SELECT COUNT(post_id) '.
614                    'FROM '.$this->prefix.'message '.
615                    'WHERE post_id = '.(integer) $post_id.' '.
616                    'AND message_status = 1 ';
617         
618          if ($del) {
619               $strReq .= 'AND message_id <> '.$id.' ';
620          }
621         
622          $rs = $this->con->select($strReq);
623         
624          if ($rs->isEmpty()) {
625               return;
626          }
627          else {
628               $nb = $rs->f(0);         
629          }
630         
631          $meta = new dcMeta($core);
632          $meta->delPostMeta($post_id,'nb_messages');
633          $meta->setPostMeta($post_id,'nb_messages',$nb);
634         
635     }
636     
637     public function getMessages($params=array(),$count_only=false)
638     {
639          if ($count_only)
640          {
641               $strReq = 'SELECT count(message_id) ';
642          }
643          else
644          {
645               if (!empty($params['no_content'])) {
646                    $content_req = '';
647               } else {
648                    $content_req =
649                    'message_content, message_content_xhtml, message_notes, ';
650               }
651               
652               if (!empty($params['columns']) && is_array($params['columns'])) {
653                    $content_req .= implode(', ',$params['columns']).', ';
654               }
655               
656               $strReq =
657               'SELECT message_id,M.post_id, M.user_id, message_dt, '.
658               'message_tz, message_upddt, message_format, '.
659               $content_req.' message_status, '.
660               'P.post_title, P.post_url, P.post_type, P.post_dt, './/P.user_id, '.
661               //'U.user_name, U.user_firstname, U.user_displayname, U.user_email, '.
662               //'U.user_url, '.
663               'V.user_name, V.user_firstname, V.user_displayname, V.user_email, '.
664               'V.user_url, '.
665               'C.cat_title, C.cat_url, C.cat_desc ';
666               
667          }
668         
669          $strReq .=
670          'FROM '.$this->prefix.'message M '.
671          'INNER JOIN '.$this->prefix.'post P ON P.post_id = M.post_id '.
672          //'INNER JOIN '.$this->prefix.'user U ON U.user_id = M.user_id '.
673          'LEFT OUTER JOIN '.$this->prefix.'category C ON P.cat_id = C.cat_id '.
674          'LEFT OUTER JOIN '.$this->prefix.'user V ON M.user_id = V.user_id ';
675         
676          if (!empty($params['from'])) {
677               $strReq .= $params['from'].' ';
678          }
679         
680          $strReq .=
681          "WHERE P.blog_id = '".$this->con->escape($this->core->blog->id)."' ";
682         
683          if (!$this->core->auth->check('contentadmin',$this->core->blog->id)) {
684               $strReq .= 'AND ((message_status = 1 AND P.post_status = 1 ';
685               
686               $strReq .= ') ';
687               
688               if ($this->core->auth->userID()) {
689                    $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')";
690               } else {
691                    $strReq .= ') ';
692               }
693          }
694         
695          if (!empty($params['post_type']))
696          {
697               if (is_array($params['post_type']) && !empty($params['post_type'])) {
698                    $strReq .= 'AND post_type '.$this->con->in($params['post_type']);
699               } else {
700                    $strReq .= "AND post_type = '".$this->con->escape($params['post_type'])."' ";
701               }
702          }
703         
704          if (!empty($params['post_id'])) {
705               $strReq .= 'AND P.post_id = '.(integer) $params['post_id'].' ';
706          }
707         
708          if (!empty($params['cat_id'])) {
709               $strReq .= 'AND P.cat_id = '.(integer) $params['cat_id'].' ';
710          }
711         
712          if (!empty($params['message_id'])) {
713               $strReq .= 'AND message_id = '.(integer) $params['message_id'].' ';
714          }
715         
716          if (isset($params['message_status'])) {
717               $strReq .= 'AND message_status = '.(integer) $params['message_status'].' ';
718          }
719         
720          if (!empty($params['message_status_not']))
721          {
722               $strReq .= 'AND message_status <> '.(integer) $params['message_status_not'].' ';
723          }
724         
725
726          if (isset($params['q_author'])) {
727               $q_author = $this->con->escape(str_replace('*','%',strtolower($params['q_author'])));
728               $strReq .= "AND LOWER(comment_author) LIKE '".$q_author."' ";
729          }
730         
731          if (!empty($params['search']))
732          {
733               $words = text::splitWords($params['search']);
734               
735               if (!empty($words))
736               {
737                    # --BEHAVIOR coreCommentSearch
738                    if ($this->core->hasBehavior('coreMessageSearch')) {
739                         $this->core->callBehavior('coreMessageSearch',$this->core,array(&$words,&$strReq,&$params));
740                    }
741                   
742                    if ($words)
743                    {
744                         foreach ($words as $i => $w) {
745                              $words[$i] = "message_words LIKE '%".$this->con->escape($w)."%'";
746                         }
747                         $strReq .= 'AND '.implode(' AND ',$words).' ';
748                    }
749               }
750          }
751         
752          if (!empty($params['sql'])) {
753               $strReq .= $params['sql'].' ';
754          }
755         
756          if (!$count_only)
757          {
758               if (!empty($params['order'])) {
759                    $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' ';
760               } else {
761                    $strReq .= 'ORDER BY message_dt DESC ';
762               }
763          }
764         
765          if (!$count_only && !empty($params['limit'])) {
766               $strReq .= $this->con->limit($params['limit']);
767          }
768         
769          $rs = $this->con->select($strReq);
770          $rs->core = $this->core;
771          $rs->extend('rsExtMessage');
772         
773          # --BEHAVIOR-- coreBlogGetComments
774          $this->core->callBehavior('agoraBlogGetMessages',$rs);
775         
776          return $rs;
777     }
778     
779     public function addMessage($cur)
780     {
781          if (!$this->core->auth->check('usage,contentadmin',$this->core->blog->id)) {
782               throw new Exception(__('You are not allowed to create an message'));
783          }
784         
785          $this->con->writeLock($this->prefix.'message');
786          try
787          {
788               # Get ID
789               $rs = $this->con->select(
790                    'SELECT MAX(message_id) '.
791                    'FROM '.$this->prefix.'message '
792                    );
793               
794               $cur->message_id = (integer) $rs->f(0) + 1;
795               $cur->message_upddt = date('Y-m-d H:i:s');
796
797               $offset = dt::getTimeOffset($this->core->blog->settings->blog_timezone);
798               $cur->message_dt = date('Y-m-d H:i:s',time() + $offset);
799               $cur->message_tz = $this->core->blog->settings->blog_timezone;
800               
801               # Post excerpt and content
802               $this->getMessageContent($cur,$cur->message_id);
803               
804               $this->getMessageCursor($cur);
805               
806               if (!$this->core->auth->check('publish,contentadmin',$this->core->blog->id)) {
807                    $cur->message_status = -2;
808               }
809               //die(var_dump($cur->message_words));
810               $cur->insert();
811               $this->con->unlock();
812          }
813          catch (Exception $e)
814          {
815               $this->con->unlock();
816               throw $e;
817          }
818          $this->triggerMessage($cur->message_id);
819          $this->core->blog->triggerBlog();
820         
821          return $cur->message_id;
822     }
823     
824     public function updMessage($id,$cur)
825     {
826          if (!$this->core->auth->check('usage,contentadmin',$this->core->blog->id)) {
827               throw new Exception(__('You are not allowed to update comments'));
828          }
829         
830          $id = (integer) $id;
831         
832          if (empty($id)) {
833               throw new Exception(__('No such message ID'));
834          }
835         
836          $rs = $this->getMessages(array('message_id' => $id));
837         
838          if ($rs->isEmpty()) {
839               throw new Exception(__('No such message ID'));
840          }
841         
842          #If user is only usage, we need to check the post's owner
843          if (!$this->core->auth->check('contentadmin',$this->core->blog->id))
844          {
845               if ($rs->user_id != $this->core->auth->userID()) {
846                    throw new Exception(__('You are not allowed to update this message'));
847               }
848          }
849         
850          $this->getMessageContent($cur,$cur->message_id);
851         
852          $this->getMessageCursor($cur);
853         
854          $cur->message_upddt = date('Y-m-d H:i:s');
855         
856          if (!$this->core->auth->check('publish,contentadmin',$this->core->blog->id)) {
857               $cur->unsetField('message_status');
858          }
859         
860          # --BEHAVIOR-- coreBeforeCommentUpdate
861          $this->core->callBehavior('coreBeforeMessageUpdate',$this,$cur,$rs);
862         
863          $cur->update('WHERE message_id = '.$id.' ');
864         
865          # --BEHAVIOR-- coreAfterCommentUpdate
866          $this->core->callBehavior('coreAfterMessageUpdate',$this,$cur,$rs);
867         
868          $this->triggerMessage($id);
869          $this->core->blog->triggerBlog();
870     }
871     
872     public function updMessageStatus($id,$status)
873     {
874          if (!$this->core->auth->check('publish,contentadmin',$this->core->blog->id)) {
875               throw new Exception(__("You are not allowed to change this message's status"));
876          }
877         
878          $cur = $this->con->openCursor($this->prefix.'message');
879          $cur->message_status = (integer) $status;
880          $this->updMessage($id,$cur);
881     }
882     
883     public function delMessage($id)
884     {
885          if (!$this->core->auth->check('delete,contentadmin',$this->core->blog->id)) {
886               throw new Exception(__('You are not allowed to delete messages'));
887          }
888         
889          $id = (integer) $id;
890         
891          if (empty($id)) {
892               throw new Exception(__('No such message ID'));
893          }
894         
895          #If user can only delete, we need to check the post's owner
896          if (!$this->core->auth->check('contentadmin',$this->core->blog->id))
897          {
898               $strReq = 'SELECT P.post_id '.
899                         'FROM '.$this->prefix.'post P, '.$this->prefix.'message M '.
900                         'WHERE P.post_id = M.post_id '.
901                         "AND P.blog_id = '".$this->con->escape($this->core->blog->id)."' ".
902                         'AND message_id = '.$id.' '.
903                         "AND user_id = '".$this->con->escape($this->core->auth->userID())."' ";
904               
905               $rs = $this->con->select($strReq);
906               
907               if ($rs->isEmpty()) {
908                    throw new Exception(__('You are not allowed to delete this comment'));
909               }
910          }
911         
912          $strReq = 'DELETE FROM '.$this->prefix.'message '.
913                    'WHERE message_id = '.$id.' ';
914         
915          $this->triggerMessage($id,true);
916          $this->con->execute($strReq);
917          $this->core->blog->triggerBlog();
918     }
919     
920     private function getMessageCursor($cur,$message_id=null)
921     {
922          if ($cur->message_content == '') {
923               throw new Exception(__('No message content'));
924          }
925         
926          $message_id = is_int($message_id) ? $message_id : $cur->message_id;
927         
928          if ($cur->message_content_xhtml == '') {
929               throw new Exception(__('No message content xhtml'));
930          }
931         
932          # Words list
933          if ($cur->message_content_xhtml !== null)
934          {
935               $words = $cur->message_content_xhtml;
936               
937               $cur->message_words = implode(' ',text::splitWords($words));
938          }
939     }
940     
941
942     private function getMessageContent($cur,$message_id)
943     {
944          $message_content = $cur->message_content;
945          $message_content_xhtml = $cur->message_content_xhtml;
946                    //die(var_dump('error'.$message_content));
947
948          $this->setMessageContent(
949               $message_id,$cur->message_format,
950               $message_content,$message_content_xhtml
951          );
952//die(var_dump('errorse&nbsp;:'.$message_content_xhtml));
953
954          $cur->message_content = $message_content;
955          $cur->message_content_xhtml = $message_content_xhtml;
956     }
957
958     public function setMessageContent($message_id,$format,&$content,&$content_xhtml)
959     {
960          if ($content) {
961               $content_xhtml = $this->core->callFormater($format,$content);
962               $content_xhtml = $this->core->HTMLfilter($content_xhtml);
963          } else {
964               $content_xhtml = '';
965          }
966          # --BEHAVIOR-- coreAfterPostContentFormat
967          $this->core->callBehavior('coreAfterMessageContentFormat',array(
968               'content' => &$content,
969               'content_xhtml' => &$content_xhtml
970          ));
971
972     }
973}
974?>
Note: See TracBrowser for help on using the repository browser.

Sites map