Dotclear

source: plugins/agora/_public.php @ 2141

Revision 2141, 53.3 KB checked in by Osku, 14 years ago (diff)

Plugin agora version 0.5.3 : long time ago since last commit :) ~ please test massively

  • post_type change from threadpost to thread .
  • admin available: manage threads and messages.
  • new widgets: last threads, selected threads, last messages, subscribe, navigation, etc.
  • Thread can be created without category.
  • i18n: french available.
  • fixes

Thank you to report anything

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-2010 Osku ,Tomtom and contributors
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11#
12# -- END LICENSE BLOCK ------------------------------------
13
14if (!defined('DC_RC_PATH')) { return; }
15
16$core->addBehavior('publicBeforeDocument',array('agorapublicBehaviors','autoLogIn'));
17$core->addBehavior('publicBeforeDocument',array('agorapublicBehaviors','cleanSession'));
18
19//Admin announce set
20$core->tpl->addValue('agoraAnnounce',array('agoraTemplate','agoraAnnounce'));
21
22// URLs
23$core->tpl->addValue('agoraURL',array('agoraTemplate','agoraURL'));
24$core->tpl->addValue('registerURL',array('agoraTemplate','registerURL'));
25$core->tpl->addValue('loginURL',array('agoraTemplate','loginURL'));
26$core->tpl->addValue('profileURL',array('agoraTemplate','profileURL'));
27$core->tpl->addValue('logoutURL',array('agoraTemplate','logoutURL'));
28$core->tpl->addValue('AgoraFeedURL',array('agoraTemplate','AgoraFeedURL'));
29$core->tpl->addValue('placeFeedURL',array('agoraTemplate','placeFeedURL'));
30
31
32// Register page
33$core->tpl->addBlock('IfRegisterPreview',array('agoraTemplate','IfRegisterPreview'));
34$core->tpl->addValue('RegisterPreviewLogin',array('agoraTemplate','RegisterPreviewLogin'));
35$core->tpl->addValue('RegisterPreviewEmail',array('agoraTemplate','RegisterPreviewEmail'));
36
37// places loop
38$core->tpl->addValue('placeURL',array('agoraTemplate','placeURL'));
39$core->tpl->addValue('placeThreadsNumber',array('agoraTemplate','placeThreadsNumber'));
40$core->tpl->addValue('placeAnswersNumber',array('agoraTemplate','placeAnswersNumber'));
41$core->tpl->addValue('placeNewThreadLink',array('agoraTemplate','placeNewThreadLink'));
42$core->tpl->addValue('placeID',array('agoraTemplate','placeID'));
43$core->tpl->addValue('placeSpacer',array('agoraTemplate','placeSpacer'));
44$core->tpl->addBlock('placeComboSelected',array('agoraTemplate','placeComboSelected'));
45
46// Pagination plus (getMessages)
47$core->tpl->addBlock('agoPagination',array('agoraTemplate','agoPagination'));
48$core->tpl->addValue('agoPaginationCounter',array('agoraTemplate','agoPaginationCounter'));
49$core->tpl->addValue('agoPaginationCurrent',array('agoraTemplate','agoPaginationCurrent'));
50$core->tpl->addBlock('agoPaginationIf',array('agoraTemplate','agoPaginationIf'));
51$core->tpl->addValue('agoPaginationURL',array('agoraTemplate','agoPaginationURL'));
52
53// Thread loop
54//$core->tpl->addBlock('ForumEntries',array('agoraTemplate','ForumEntries'));
55$core->tpl->addValue('EntryIfClosed',array('agoraTemplate','EntryIfClosed'));
56$core->tpl->addValue('EntryMessageCount',array('agoraTemplate','EntryMessageCount'));
57$core->tpl->addValue('EntryCreaDate',array('agoraTemplate','EntryCreaDate'));
58$core->tpl->addValue('EntryUpdDate',array('agoraTemplate','EntryUpdDate'));
59// Thread loop, place context
60$core->tpl->addBlock('IfThreadPreview',array('agoraTemplate','IfThreadPreview'));
61$core->tpl->addValue('ThreadPreviewTitle',array('agoraTemplate','ThreadPreviewTitle'));
62$core->tpl->addValue('ThreadPreviewContent',array('agoraTemplate','ThreadPreviewContent'));
63//$core->tpl->addValue('ThreadURL',array('agoraTemplate','ThreadURL'));
64$core->tpl->addValue('ThreadCategoryURL',array('agoraTemplate','ThreadCategoryURL'));
65$core->tpl->addValue('MessageThreadURL',array('agoraTemplate','MessageThreadURL'));
66$core->tpl->addValue('ThreadProfileUserID',array('agoraTemplate','ThreadProfileUserID'));
67$core->tpl->addBlock('ThreadComboSelected',array('agoraTemplate','ThreadComboSelected'));
68// Thread loop, thread context
69$core->tpl->addBlock('IfAnswerPreview',array('agoraTemplate','IfAnswerPreview'));
70$core->tpl->addValue('AnswerPreviewContent',array('agoraTemplate','AnswerPreviewContent'));
71$core->tpl->addBlock('IfEditPreview',array('agoraTemplate','IfEditPreview'));
72//$core->tpl->addBlock('IfIsThread',array('agoraTemplate','IfIsThread'));
73$core->tpl->addValue('PostEditTitle',array('agoraTemplate','PostEditTitle'));
74$core->tpl->addValue('PostEditContent',array('agoraTemplate','PostEditContent'));
75$core->tpl->addValue('AnswerOrderNumber',array('agoraTemplate','AnswerOrderNumber'));
76$core->tpl->addBlock('SysIfThreadUpdated',array('agoraTemplate','SysIfThreadUpdated'));
77// Tread action modo suffixe
78$core->tpl->addValue('ModerationDeleteThread',array('agoraTemplate','ModerationDeleteThread'));
79$core->tpl->addValue('ModerationEditThread',array('agoraTemplate','ModerationEditThread'));
80$core->tpl->addValue('ModerationDeleteMessage',array('agoraTemplate','ModerationDeleteMessage'));
81$core->tpl->addValue('ModerationEditMessage',array('agoraTemplate','ModerationEditMessage'));
82$core->tpl->addValue('ModerationPin',array('agoraTemplate','ModerationPin'));
83$core->tpl->addValue('ModerationUnpin',array('agoraTemplate','ModerationUnpin'));
84$core->tpl->addValue('ModerationClose',array('agoraTemplate','ModerationClose'));
85$core->tpl->addValue('ModerationOpen',array('agoraTemplate','ModerationOpen'));
86
87// Messages = answers to threads
88$core->tpl->addBlock('Messages',array('agoraTemplate','Messages'));
89$core->tpl->addBlock('MessagesHeader',array('agoraTemplate','MessagesHeader'));
90$core->tpl->addBlock('MessagesFooter',array('agoraTemplate','MessagesFooter'));
91$core->tpl->addValue('MessageIfFirst',array('agoraTemplate','MessageIfFirst'));
92$core->tpl->addValue('MessageIfOdd',array('agoraTemplate','MessageIfOdd'));
93$core->tpl->addValue('MessageContent',array('agoraTemplate','MessageContent'));
94$core->tpl->addValue('MessageID',array('agoraTemplate','MessageID'));
95$core->tpl->addValue('MessageOrderNumber',array('agoraTemplate','MessageOrderNumber'));
96$core->tpl->addValue('MessageAuthorID',array('agoraTemplate','MessageAuthorID'));
97$core->tpl->addValue('MessageAuthorCommonName',array('agoraTemplate','MessageAuthorCommonName'));
98$core->tpl->addValue('MessageDate',array('agoraTemplate','MessageDate'));
99$core->tpl->addValue('MessageTime',array('agoraTemplate','MessageTime'));
100$core->tpl->addBlock('IfMessagePreview',array('agoraTemplate','IfMessagePreview'));
101$core->tpl->addValue('MessagePreviewContent',array('agoraTemplate','MessagePreviewContent'));
102$core->tpl->addValue('MessageEditContent',array('agoraTemplate','MessageEditContent'));
103$core->tpl->addValue('MessageProfileUserID',array('agoraTemplate','MessageProfileUserID'));
104$core->tpl->addValue('MessageEntryTitle',array('agoraTemplate','MessageEntryTitle'));
105$core->tpl->addValue('MessageFeedID',array('agoraTemplate','MessageFeedID'));
106//$core->tpl->addValue('',array('agoraTemplate',''));
107//$core->tpl->addValue('',array('agoraTemplate',''));
108
109// User
110$core->tpl->addBlock('authForm',array('agoraTemplate','authForm'));
111$core->tpl->addBlock('notauthForm',array('agoraTemplate','notauthForm'));
112$core->tpl->addValue('PublicUserID',array('agoraTemplate','PublicUserID'));
113$core->tpl->addValue('PublicUserDisplayName',array('agoraTemplate','PublicUserDisplayName'));
114$core->tpl->addBlock('userIsModo',array('agoraTemplate','userIsModo'));
115$core->tpl->addValue('ProfileUserID',array('agoraTemplate','ProfileUserID'));
116$core->tpl->addValue('ProfileUserDisplayName',array('agoraTemplate','ProfileUserDisplayName'));
117$core->tpl->addValue('ProfileUserURL',array('agoraTemplate','ProfileUserURL'));
118$core->tpl->addValue('ProfileUserEmail',array('agoraTemplate','ProfileUserEmail'));
119$core->tpl->addValue('ProfileUserCreaDate',array('agoraTemplate','ProfileUserCreaDate'));
120$core->tpl->addValue('ProfileUserUpdDate',array('agoraTemplate','ProfileUserUpdDate'));
121
122//$core->tpl->addBlock('',array('agoraTemplate',''));
123//$core->tpl->addValue('',array('agoraTemplate',''));
124
125
126global $_ctx;
127
128$_ctx->agora = new agora($core);
129$_ctx->log = new dcLog($core);
130
131class agorapublicBehaviors
132{
133     public static function autoLogIn()
134     {
135          global $core, $_ctx;
136
137          $core->session = new sessionDB(
138               $core->con,
139               $core->prefix.'session',
140               'dc_agora_sess_'.$core->blog->id,
141               ''
142          );
143
144          if (isset($_COOKIE['dc_agora_sess_'.$core->blog->id]))
145          {
146               # If we have a session we launch it now
147               if (!$core->auth->checkSession())
148               {
149                    # Avoid loop caused by old cookie
150                    $p = $core->session->getCookieParameters(false,-600);
151                    $p[3] = '/';
152                    call_user_func_array('setcookie',$p);
153               }
154          }
155
156          if (!isset($_SESSION['sess_user_id']))
157          {
158               if (isset($_COOKIE['dc_agora_'.$core->blog->id])
159               && strlen($_COOKIE['dc_agora_'.$core->blog->id]) == 104)
160               {
161                    # If we have a remember cookie, go through auth process with key
162                    $login = substr($_COOKIE['dc_agora_'.$core->blog->id],40);
163                    $login = @unpack('a32',@pack('H*',$login));
164                    if (is_array($login))
165                    {
166                         $login = $login[1];
167                         $key = substr($_COOKIE['dc_agora_'.$core->blog->id],0,40);
168                         $passwd = null;
169                    }
170                    else
171                    {
172                         $login = null;
173                    }
174                   
175                    $_ctx->agora->userlogIn($login,$passwd,$key);
176               }
177          }
178
179          return;
180     }
181
182     public static function cleanSession()
183     {
184          global $core;
185
186          $strReq = 'DELETE FROM '.$core->prefix.'session '.
187                    "WHERE ses_time < ".(time() - 3600*24*14);
188
189          $core->con->execute($strReq);
190     }
191}
192
193
194class urlAgora extends dcUrlHandlers
195{
196     public static function recover($args)
197     {
198          // forum/recover : set a recovery key
199          // forum/recover/blabla : create a newpassword and send it to user mailbox
200         
201          global $core, $_ctx;
202         
203          $recover = $core->auth->allowPassChange() && !empty($_REQUEST['recover']);
204          $akey = ($core->auth->allowPassChange() && !empty($args)) ? $args : null;
205         
206          $user_id = $user_pwd = $user_key = $user_email = null;
207         
208          $_ctx->agora_recovery = new ArrayObject();
209          $_ctx->agora_recovery['msg'] = '';
210         
211          # Recover password
212          if ($recover && !empty($_POST['user_id']) && !empty($_POST['user_email']))
213          {
214               $user_id = !empty($_POST['user_id']) ? $_POST['user_id'] : null;
215               $user_email = !empty($_POST['user_email']) ? $_POST['user_email'] : '';
216               try
217               {
218                    $recover_key = $core->auth->setRecoverKey($user_id,$user_email);
219                    $_ctx->agora->sendRecoveryEmail($mail,$recover_key);
220                    http::head(200,'OK');
221                    header('Content-Type: text/html');
222                    echo sprintf(__('The e-mail was sent successfully to %s.'),'<strong>'.$user_email.'</strong>');
223               }
224               
225               catch (Exception $e)
226               {
227                    $_ctx->form_error = $e->getMessage();
228               }
229          }
230          elseif ($akey)
231          {
232               try
233               {
234                    $recover_res = $core->auth->recoverUserPassword($akey);
235                    $_ctx->agora->sendNewPasswordEmail($recover_res['user_email'],$recover_res['user_id'],$recover_res['new_pass']);
236                    http::head(200,'OK');
237                    header('Content-Type: text/plain');
238                    echo __('Your new password is in your mailbox.');
239               }
240               
241               catch (Exception $e)
242               {
243                    $_ctx->form_error = $e->getMessage();
244               }
245         
246          }
247         
248     $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
249     self::serveDocument('recovery.html');
250     return;
251     }
252
253     public static function register($args)
254     {
255          // URL forum/register : create the user but without any perm
256          // forum/register/?key=12345678 : end of registration : add perm 'member'
257          global $core, $_ctx;
258         
259          $_ctx->agora_register = new ArrayObject();
260          $_ctx->agora_register['login'] = '';
261          $_ctx->agora_register['email'] = '';
262          $_ctx->agora_register['preview'] = false;
263          $_ctx->agora_register['key'] = false;
264          $_ctx->agora_register['pwd'] = '';
265         
266          $url = $core->blog->url.$core->url->getBase("agora");
267         
268          $register = isset($_POST['ru_login']) && isset($_POST['ru_email']);
269          $key =  !empty($_GET['key']) ? $_GET['key'] : null;
270         
271          if ($register)
272          {
273               // Spam trap
274               if (!isset($_POST['email2']) || $_POST['email2'] !== '') {
275                    http::head('412');
276                    header('Content-Type: text/plain');
277                    echo "So Long, and Thanks For All the Fish";
278                    return;
279               }
280               
281               // new password from clearbricks/common/lib.crypt.php
282               $pwd = crypt::createPassword();
283               $login = trim($_POST['ru_login']);
284               $mail = trim($_POST['ru_email']); 
285               $register_preview = !empty($_POST['preview']);
286               
287               $_ctx->agora_register['login'] = $login;
288               $_ctx->agora_register['email'] = $mail;
289               $_ctx->agora_register['pwd'] = $pwd;
290               
291               if ($register_preview)
292               {
293                    # --BEHAVIOR-- publicBeforeSignUp
294                    $core->callBehavior('publicBeforeSignUp',$_ctx->agora_register);
295                    $_ctx->agora_register['preview'] = true;
296               }
297               else
298               {
299                    $cur = $core->con->openCursor($core->prefix.'user');
300                    $cur->user_id = $login;
301                    $cur->user_email = html::clean($mail);
302                    $cur->user_pwd = $pwd;
303                    $cur->user_lang = $core->blog->settings->lang;
304                    $cur->user_tz = $core->blog->settings->blog_timezone;
305                    $cur->user_default_blog = $core->blog->id;
306                    //$redir = http::getSelfURI();
307                    //$redir .= strpos($redir,'?') !== false ? '&' : '?';
308                   
309                    try
310                    {
311                         if (!text::isEmail($cur->user_email))
312                         {
313                              throw new Exception(__('You must provide a valid email'));
314                         }
315                         
316                         if ($core->getUsers(array('user_id' => $cur->user_id),true)->f(0) > 0) 
317                         {
318                              throw new Exception(sprintf(__('User "%s" already exists.'),html::escapeHTML($cur->user_id)));
319                         }
320                         
321                         # --BEHAVIOR-- publicBeforeUserCreate
322                         $core->callBehavior('publicBeforeUserCreate',$cur);
323                         
324                         $user_id = $core->auth->sudo(array($core,'addUser'),$cur);
325                         $_ctx->agora->sendActivationEmail($mail,$user_id,$pwd);
326                         # --BEHAVIOR-- publicAfterUserCreate
327                         $core->callBehavior('publicAfterUserCreate',$cur,$user_id);
328                         
329                         header('Content-Type: text/html; charset=UTF-8');
330                         http::head(201,'Created');
331                         header('Content-Type: text/html');
332                         header("Refresh: 5;URL=$url");
333                         echo sprintf(__('User %s successfully created. You will receive an email to activate your account.'),'<strong>'.$user_id.'</strong>');
334                         return;
335                         
336                    }
337               
338                    catch (Exception $e)
339                    {
340                         $_ctx->form_error = $e->getMessage();
341                    }
342
343               }
344          }
345         
346          if($key)
347          {
348               if (preg_match('/^[a-fA-F\d]{32}$/',$key))
349               {
350                    try
351                    {
352                         $_ctx->unregistred_user = $_ctx->agora->getUnregistredUser($key);
353                         // throw Exception if invalid key ...
354                         
355                         $user_id = $_ctx->unregistred_user['user_id'];
356                         $user_status = $_ctx->unregistred_user['user_status'];
357                         
358                         // http://dev.dotclear.org/2.0/browser/trunk/inc/core/class.dc.core.php#L684
359
360                         if ($_ctx->agora->isMember($user_id) === true)
361                         {
362                              // User has permission "member of agora"
363                              throw new Exception(sprintf(__('User %s is already registred. You can log in.'),html::escapeHTML($user_id)));
364                         }
365                         else
366                         {
367                              $perm = array('member' => '');
368                              $core->auth->sudo(array($core,'setUserBlogPermissions'),$user_id,$core->blog->id,$perm);
369                              http::head(200,'OK');
370                              header('Content-Type: text/html');
371                              echo sprintf(__('User %s is now registred. You can now log in.'),'<strong>'.$user_id.'</strong>');
372                              return;
373                         }
374                    }
375                    catch (Exception $e)
376                    {
377                         $_ctx->form_error = $e->getMessage();
378                    }
379               }
380               else
381               {
382                    $_ctx->form_error = __('This is a wrong registration URL. Registration failed.');
383               }
384          }
385         
386          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
387          self::serveDocument('register.html','text/html',false);
388          return;
389     }
390     
391     public static function login($args)
392     {
393          // Ajouter un test sur les conditions générales ...
394          // module de password recovery : envoi d'un email avec mot de passe généré auto..
395          // URL forum/login : login user
396         
397          global $core, $_ctx;
398          $url = $core->blog->url.$core->url->getBase("agora");
399
400          if (!isset($_SESSION['sess_user_id']))
401          {
402               $login = isset($_POST['li_login']) && isset($_POST['li_pwd']);
403
404               if ($login)
405               {
406                    $login = trim($_POST['li_login']);
407                    $pwd = trim($_POST['li_pwd']);
408                    //$redir .= strpos($redir,'?') !== false ? '&' : '?';
409
410                    try
411                    {
412                         $user_id = $_ctx->agora->userlogIn($login,$pwd);
413                         http::redirect($core->blog->url.$core->url->getBase('agora'));
414                         return;
415                    }
416
417                    catch (Exception $e)
418                    {
419                         $_ctx->form_error = $e->getMessage(); 
420                    }
421               }
422               $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
423               self::serveDocument('login.html');
424               return;
425          }
426          else
427          {
428               //http::head(100,'Continue');
429               header('Location: '.$url);
430               return;
431          }
432     }
433
434     public static function logout($args)
435     {
436          // URL forum/logout : logout user without template
437         
438          global $core;
439         
440          if (isset($_SESSION['sess_user_id']))
441          {
442               $_SESSION['sess_user_id'] = null;
443               $core->session->destroy();
444               
445               if (isset($_COOKIE['dc_agora_'.$core->blog->id]))
446               {
447                    unset($_COOKIE['dc_agora_'.$this->core->blog->id]);
448                    setcookie('dc_agora_'.$core->blog->id,false,-600);
449               }
450               //what about comment_info cookie ?
451          }
452         
453          http::redirect($core->blog->url.$core->url->getBase('agora'));
454          return;
455     }
456
457     public static function userlist($args)
458     {
459       //todo
460     }
461
462     public static function profile($args)
463     {
464          // URL forum/profile/batman : edit/view profile ..
465          // URL forum/profile/batman/ban : ban user  ..
466         
467          global $core, $_ctx;
468         
469          if (($args == '') || (!is_string($args)))
470          {
471               self::p404();
472          }
473          else 
474          {
475               $user_id = ($core->auth->userID() != false && isset($_SESSION['sess_user_id'])) ? $core->auth->userID() : '';
476               //$_ctx->users->user_id = $args;
477               $_ctx->users = $_ctx->agora->getUser($args);
478               if ($_ctx->users->isEmpty()) {
479                    self::p404();
480               }
481               
482               $_ctx->profile_user = new ArrayObject();
483               
484               $_ctx->profile_user['pseudo'] = $_ctx->users->user_displayname;
485               $_ctx->profile_user['email'] = $_ctx->users->user_email;
486               $_ctx->profile_user['url'] = $_ctx->users->user_url;
487               $_ctx->profile_user['status'] = $_ctx->users->user_status;
488               $_ctx->profile_user['pwd'] = '';
489               $_ctx->profile_user['msg'] = '';
490               
491               //$_ctx->agora->isModerator($user_id) === false
492               if ($args != $user_id)
493               {
494                    $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
495                    self::serveDocument('profile.html','text/html',false);
496                    return;
497               }
498               
499               if (!empty($_POST['submit']))
500               {
501                    $_ctx->profile_user['pseudo'] = trim($_POST['li_pseudo']);;
502                    $_ctx->profile_user['email']  = trim($_POST['li_email']);
503                    $_ctx->profile_user['url']  = trim($_POST['li_url']);
504                    $_ctx->profile_user['pwd'] = trim($_POST['li_pwd']);
505                    $_ctx->profile_user['pwd2'] = trim($_POST['li_pwd2']);
506                    $redir = $redir = $core->blog->url.$core->url->getBase("profile").'/'.$args;
507                    $redir .= strpos($redir,'?') !== false ? '&' : '?';
508                   
509                     if (empty($_ctx->form_error))
510                    {
511                         try
512                         {
513                         
514                              if (!empty($_POST['li_pwd']))
515                              {
516                                   if (empty($_POST['li_pwd2']))
517                                   {
518                                         throw new Exception(__('You must confirm your password'));
519                                   }
520                                   elseif ($_POST['li_pwd'] != $_POST['li_pwd2'])
521                                   {
522                                         throw new Exception(__('Please, check your password. Passwords don\'t match'));
523                                        }
524                                   else {
525                                        $cur->user_pwd = $_ctx->profile_user['pwd'];
526                                   }
527                              }
528                   
529                              if (empty($_ctx->profile_user['email']) ||
530                              !text::isEmail($_ctx->profile_user['email']))
531                              {
532                                   throw new Exception(__('You must provide a valid email'));
533                              }
534                         
535                              $cur = $core->con->openCursor($core->prefix.'user');
536                              $cur->user_email = $_ctx->profile_user['email'];
537                              $cur->user_displayname = $_ctx->profile_user['pseudo'];
538                              $cur->user_url = $_ctx->profile_user['url'];
539                              if (!empty($_ctx->profile_user['pwd']))
540                              {
541                                   $cur->user_pwd =  $_ctx->profile_user['pwd'];
542                              }
543                             
544                              # --BEHAVIOR-- publicBeforeUserCreate
545                              $core->callBehavior('publicBeforeUserUpdate',$cur,$user_id);
546                             
547                              $id = $core->auth->sudo(array($core,'updUser'),$user_id,$cur);
548                             
549                              $_ctx->profile_user['msg'] = sprintf(__('User %s successfully updated.'),'<strong>'.$args.'</strong>');
550                              # --BEHAVIOR-- publicAfterUserCreate
551                              $core->callBehavior('publicAfterUserUpdate',$cur,$id);
552                             
553                              if (!empty($id))
554                              {
555                                   $redir_arg = 'updated=1';
556                              }
557                              else
558                              {
559                                   $redir_arg = 'error=1';
560                              }
561                             
562                              header('Location: '.$redir.$redir_arg);
563                         }
564                         catch (Exception $e)
565                         {
566                              $_ctx->form_error = $e->getMessage();
567                         }
568                         
569                    }
570               }
571          }
572         
573          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
574          self::serveDocument('profile_me.html','text/html',false);
575          return;
576     }
577
578     public static function old_forum($args)
579     {
580          // URL forum/ : home of the forum : see categories aka places
581         
582          global $core, $_ctx;
583         
584          //getCategoriesPlus ...
585          $params['without_empty'] = false;
586          $_ctx->categories = $_ctx->agora->getCategoriesPlus($params);
587          //die(var_dump($_ctx->categories));
588         
589          if (empty($_GET['q'])) {
590               $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
591               self::serveDocument('forum.html','text/html',false);
592               return;
593          } else {
594                self::fsearch();
595          }
596     }
597     
598     public static function agora($args)
599     {
600          global $core;
601         
602          $n = self::getPageNumber($args);
603         
604          if ($args && !$n)
605          {
606               # "Then specified URL went unrecognized by all URL handlers and
607               # defaults to the home page, but is not a page number.
608               self::p404();
609          }
610          else
611          {
612               if ($n) {
613                    $GLOBALS['_page_number'] = $n;
614                    $core->url->type = $n > 1 ? 'agora-page' : 'agora';
615               }
616               
617               if (empty($_GET['q'])) {
618                    $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
619                    self::serveDocument('agora.html','text/html',false);
620               } else {
621                    self::search();
622               }
623          }
624     }
625
626     public static function fsearch()
627     {
628          global $core;
629         
630          $GLOBALS['_fsearch'] = !empty($_GET['q']) ? rawurldecode($_GET['q']) : '';
631          if ($GLOBALS['_fsearch']) {
632               $GLOBALS['_fsearch_count'] = $_ctx->agora->getPostsPlus(array('search' => $GLOBALS['_fsearch']),true)->f(0);
633          }
634         
635          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
636          self::serveDocument('forum_search.html');
637          return;
638     }
639
640     public static function place($args)
641     {
642          // URL forum/sub/sub_url : view threads of a place
643          // URL forum/sub/sub_url /newthread/ : write a new thread in the category
644         
645          global $core, $_ctx;
646         
647          $core->addBehavior('coreInitWikiPost',array('agoraBehaviors','coreInitWikiPost'));
648          //$core->addBehavior('coreBeforePostCreate',array('agoraBehaviors','coreBeforePostCreate'));
649         
650          $n = self::getPageNumber($args);
651         
652          if ($args == '' && !$n) {
653               self::p404();
654          }
655          //$params['without_empty'] = false;
656          $params['cat_url'] = $args;
657          $params['post_type'] = 'thread';
658          //$params['thread_id'] = '';
659         
660          //$_ctx->categories = $_ctx->agora->getCategoriesPlus($params);
661          $_ctx->categories = $core->blog->getCategories($params);
662         
663          if ($_ctx->categories->isEmpty())
664          {
665               self::p404();
666          }
667         
668          if ($n) {
669               $GLOBALS['_page_number'] = $n;
670          }
671         
672          $user_id = $core->auth->userID();
673         
674          $_ctx->thread_preview = new ArrayObject();
675          $_ctx->thread_preview['title'] = '';
676          $_ctx->thread_preview['content'] = '';
677          $_ctx->thread_preview['rawcontent'] = '';
678          $_ctx->thread_preview['preview'] = false;
679         
680          $thread_new = isset($_POST['t_content']) && isset($_POST['t_title']);
681         
682          //Setting for quick new thread ?
683         
684          if ($thread_new && ($_ctx->agora->isMember($user_id) === true))
685          {
686               $title = $_POST['t_title'];
687               $content = $_POST['t_content'];
688               $preview = !empty($_POST['preview']);
689               
690               if ($content != '')
691               { 
692                    $core->initWikiPost();
693                    /// coreInitWikiPost
694                    $content = $core->wikiTransform($content);
695                    $content = $core->HTMLfilter($content);
696               }
697               
698               $_ctx->thread_preview['title'] = $title ;
699               $_ctx->thread_preview['content'] = $content;
700               $_ctx->thread_preview['rawcontent'] = $_POST['t_content'];
701               
702               if ($preview)
703               {
704                    # --BEHAVIOR-- publicBeforePostPreview
705                    $core->callBehavior('publicBeforeThreadPreview',$_ctx->thread_preview);
706                   
707                    $_ctx->thread_preview['preview'] = true;
708               }
709               
710               else
711               {
712                    $cur = $core->con->openCursor($core->prefix.'post');
713                    $cur->user_id = $core->auth->userID() ;
714                    $cur->cat_id = $_ctx->categories->cat_id;
715                    $cur->post_title = $title;
716                    $cur->post_format = 'wiki';
717                    $cur->post_status = 1;
718                    $cur->post_lang = $core->auth->getInfo('user_lang');
719                    $cur->post_content = $_POST['t_content'];
720                    $cur->post_type = 'thread';
721                    $cur->post_open_comment = 1;
722                   
723                    // thread_id : (new field in base ): link between posts of a same thread
724                    //$cur->thread_id = '';
725               
726                    $redir = $core->blog->url.$core->url->getBase("place").'/'.$_ctx->categories->cat_url;
727                    $redir .= strpos($redir,'?') !== false ? '&' : '?';
728               
729                    try
730                    {
731                         # --BEHAVIOR-- publicBeforePostCreate
732                         $core->callBehavior('publicBeforeThreadCreate',$cur);
733                   
734                         $post_id = $core->auth->sudo(array($core->blog,'addPost'),$cur);
735                   
736                         # --BEHAVIOR-- publicAfterPostCreate
737                         $core->callBehavior('publicAfterThreadCreate',$cur,$post_id);
738                   
739                         $redir_arg = 'pub=1';
740                   
741                         header('Location: '.$redir.$redir_arg);
742                         return;
743                    }
744         
745                    catch (Exception $e)
746                    {
747                         $_ctx->form_error = $e->getMessage();
748                    }
749               }
750          }
751          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
752          self::serveDocument('place.html','text/html',false);
753          return;
754     }
755
756     public static function newthread($args)
757     {
758          global $core, $_ctx;
759          $user_id = ($core->auth->userID() != false && isset($_SESSION['sess_user_id'])) ? $core->auth->userID() : '';
760         
761          if ($args) {$args =  substr($args,1);}
762
763          $core->addBehavior('coreInitWikiPost',array('agoraBehaviors','coreInitWikiPost'));
764         
765          if (/*$args == '' ||*/ !$core->auth->userID()) {
766               self::p404();
767          }
768         
769          $params['cat_url'] = $args;
770         
771          //$_ctx->categories = $_ctx->agora->getCategoriesPlus($params);
772          $_ctx->categories = $core->blog->getCategories($params);
773         
774          if ($_ctx->categories->isEmpty())
775          {
776               //self::p404();
777          }
778         
779          $_ctx->thread_preview = new ArrayObject();
780          $_ctx->thread_preview['title'] = '';
781          $_ctx->thread_preview['content'] = '';
782          $_ctx->thread_preview['rawcontent'] = '';
783          $_ctx->thread_preview['preview'] = false;
784          $_ctx->thread_preview['cat'] = ($_ctx->categories->isEmpty()) ? '' : $_ctx->categories->cat_id;
785          $_ctx->thread_preview['not_empty'] = ( $args == '' ) ? false : true;
786         
787          $thread_new = isset($_POST['t_content']) && isset($_POST['t_title']);
788         
789          if ($thread_new && ($_ctx->agora->isMember($user_id) === true))
790          {
791               $title = $_POST['t_title'];
792               $content = $_POST['t_content'];
793               $preview = !empty($_POST['preview']);
794               
795               if ($content != '')
796               { 
797                    $core->initWikiPost();
798                    /// coreInitWikiPost
799                    $content = $core->wikiTransform($content);
800                    $content = $core->HTMLfilter($content);
801               }
802               
803               $_ctx->thread_preview['title'] = $title ;
804               $_ctx->thread_preview['content'] = $content;
805               $_ctx->thread_preview['rawcontent'] = $_POST['t_content'];
806               $_ctx->thread_preview['cat'] = (integer) $_POST['t_cat'];
807               $_ctx->thread_preview['not_empty'] = true;
808               
809               if ($preview)
810               {
811                    # --BEHAVIOR-- publicBeforePostPreview
812                    $core->callBehavior('publicBeforeThreadPreview',$_ctx->thread_preview);
813                   
814                    $_ctx->thread_preview['preview'] = true;
815               }
816               
817               else
818               {
819                    $cur = $core->con->openCursor($core->prefix.'post');
820                    # Magic tweak doesn't work here
821                    //$core->blog->settings->system->post_url_format = "{id}";
822                    $cur->user_id = $user_id;
823                    $cur->cat_id = ((integer) $_POST['t_cat']) ? (integer) $_POST['t_cat'] : null;
824                    $cur->post_title = $title;
825                    $offset = dt::getTimeOffset($core->blog->settings->blog_timezone);
826                    $cur->post_dt = date('Y-m-d H:i:s',time() + $offset);
827                    $cur->post_format = 'wiki';
828                    $cur->post_status = 1;
829                    $cur->post_lang = $core->auth->getInfo('user_lang');
830                    $cur->post_content = $_POST['t_content'];
831                    $cur->post_type = 'thread';
832                    $cur->post_open_comment = 1;
833                    $redir = $core->blog->url.$core->url->getBase("thread").'/';
834                    //$redir .= strpos($redir,'?') !== false ? '&' : '?';
835               
836                    try
837                    {
838                         # --BEHAVIOR-- publicBeforeThreadCreate
839                         $core->callBehavior('publicBeforeThreadCreate',$cur);
840                   
841                         $post_id = $core->auth->sudo(array($core->blog,'addPost'),$cur);
842                         //$meta = new dcMeta($core);
843                         //$meta->setPostMeta($post_id,'nb_messages',1);
844                   
845                         # --BEHAVIOR-- publicAfterThreadCreate
846                         $core->callBehavior('publicAfterThreadCreate',$cur,$post_id);
847                   
848                         $redir .= $cur->post_url;
849                         $redir .= strpos($redir,'?') !== false ? '&' : '?';
850                         $redir_arg = 'pub=1';
851                   
852                         header('Location: '.$redir.$redir_arg);
853                         return;
854                    }
855         
856                    catch (Exception $e)
857                    {
858                         $_ctx->form_error = $e->getMessage();
859                    }
860               }
861          }
862          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
863          self::serveDocument('newthread.html','text/html',false);
864          return;
865     }
866
867     public static function thread($args)
868     {
869          global $core, $_ctx;
870         
871          $core->addBehavior('coreInitWikiPost',array('agoraBehaviors','coreInitWikiPost'));
872          /*
873          URL forum/thread/id : view a full thread (first and answers) serve a template
874          Moderator :
875          URL forum/thread/id(& or ?)action=pin : marks as selected
876          URL forum/thread/id(& or ?)action=unpin : marks as unselected
877          URL forum/thread/id(& or ?)action=close : close the thead : thread->commentsActive : false
878          URL forum/thread/id(& or ?)action=open : open the thead : thread->commentsActive : true
879          */
880          //$n = self::getPageNumber($args);
881         
882          if ($args == ''){// && !$n) {
883               self::p404();
884          }
885          $core->blog->withoutPassword(false);
886          //if ($n) {
887          //   $GLOBALS['_page_number'] = $n;
888          //}
889         
890          $user_id = ($core->auth->userID() != false && isset($_SESSION['sess_user_id'])) ? $core->auth->userID() : '';
891          $action =  !empty($_GET['action']) ? $_GET['action'] : null;
892         
893          $params = new ArrayObject();
894          $params['post_url'] = $args;
895          $params['post_type'] = 'thread';
896         
897          //$_ctx->posts = $_ctx->agora->getPostsPlus($params);
898          $_ctx->posts = $core->blog->getPosts($params);
899         
900          if ($_ctx->posts->isEmpty() )//|| $_ctx->posts->thread_id != '')
901          {
902               self::p404();
903          }
904         
905          /*$thread_id = $_ctx->posts->post_id;
906          $_ctx->post_preview = new ArrayObject();
907          $_ctx->post_preview['content'] = '';
908          $_ctx->post_preview['title'] = '';
909          $_ctx->post_preview['rawcontent'] = '';
910          $_ctx->post_preview['preview'] = false;*/
911          //$_ctx->nb_message_per_page = $core->blog->settings->agora_nb_msg_per_page_per_thread;
912
913          $post_id = $_ctx->posts->post_id;
914          $post_password = $_ctx->posts->post_password;
915         
916          # Password protected entry
917          if ($post_password != '' && !$_ctx->preview)
918          {
919               # Get passwords cookie
920               if (isset($_COOKIE['dc_passwd'])) {
921                    $pwd_cookie = unserialize($_COOKIE['dc_passwd']);
922               } else {
923                    $pwd_cookie = array();
924               }
925     
926               # Check for match
927               if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
928               || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password))
929               {
930                    $pwd_cookie[$post_id] = $post_password;
931                    setcookie('dc_passwd',serialize($pwd_cookie),0,'/');
932               }
933               else
934               {
935                    self::serveDocument('password-form.html','text/html',false);
936                    return;
937               }
938          }
939
940          $_ctx->message_preview = new ArrayObject();
941          $_ctx->message_preview['content'] = '';
942          $_ctx->message_preview['title'] = '';
943          $_ctx->message_preview['rawcontent'] = '';
944          $_ctx->message_preview['preview'] = false;
945         
946          // Mark as selected or unselected
947          if ($_ctx->agora->isModerator($user_id) === true && 
948          (($action == 'pin') || ($action == 'unpin')))
949          {
950               $redir = $core->blog->url.$core->url->getBase("thread").'/'.$_ctx->posts->post_url;
951               $redir .= strpos($redir,'?') !== false ? '&' : '?';
952               
953               try
954               {
955                    $core->auth->sudo(array($core->blog,'updPostSelected'),$_ctx->posts->post_id,$action == 'pin');
956                   
957                    $redir_arg = $action;
958                    $redir_arg .= '=1';
959                   
960                    header('Location: '.$redir.$redir_arg);
961                    return;
962               }
963               
964               catch (Exception $e)
965               {
966                    $_ctx->form_error = $e->getMessage();
967               }
968          }
969         
970          // Mark as selected or unselected - open or close thread
971          if ($_ctx->agora->isModerator($user_id) === true && 
972          (($action == 'close') || ($action == 'open')))
973          {
974               $redir = $core->blog->url.$core->url->getBase("thread").'/'.$_ctx->posts->post_url;
975               $redir .= strpos($redir,'?') !== false ? '&' : '?';
976               
977               try
978               {
979                    $core->auth->sudo(array($_ctx->agora,'updPostClosed'),$_ctx->posts->post_id,$action == 'open');
980                   
981                    $redir_arg = $action;
982                    $redir_arg .= '=1';
983                   
984                    header('Location: '.$redir.$redir_arg);
985                    return;
986               }
987               
988               catch (Exception $e)
989               {
990                    $_ctx->form_error = $e->getMessage();
991               }
992          }
993         
994          // Quick Answer
995          // In comments ?
996          if ($_ctx->agora->isMember($user_id) === true)
997          {
998               $thread_message = (isset($_POST['p_content']) && $_ctx->posts->commentsActive());
999               
1000               if ($thread_message)
1001               {
1002                    $content = $_POST['p_content'];
1003                    $preview = !empty($_POST['preview']);
1004                   
1005                    if ($content != '')
1006                    { 
1007                         $core->initWikiPost();
1008                         /// coreInitWikiPost
1009                         $content = $core->wikiTransform($content);
1010                         $content = $core->HTMLfilter($content);
1011                    }
1012                   
1013                    $_ctx->message_preview['content'] = $content;
1014                    $_ctx->message_preview['rawcontent'] = $_POST['p_content'];
1015                   
1016                    if ($preview)
1017                    {
1018                         # --BEHAVIOR-- publicBeforePostPreview
1019                         $core->callBehavior('publicBeforeMessagePreview',$_ctx->message_preview);
1020                         
1021                         $_ctx->message_preview['preview'] = true;
1022                    }
1023                   
1024                    else
1025                    {
1026                         /*$cur = $core->con->openCursor($core->prefix.'post');
1027                         $cur->user_id = $user_id;
1028                         $cur->cat_id = $_ctx->posts->cat_id;
1029                         $cur->post_format =  'wiki';
1030                         $cur->post_status =  1;
1031                         $cur->post_lang = $core->auth->getInfo('user_lang');
1032                         $cur->post_title = $_ctx->posts->post_title;
1033                         $cur->post_content = $_POST['p_content'];
1034                         $cur->post_type =  'thread';*/
1035                         $cur = $core->con->openCursor($core->prefix.'message');
1036                         $cur->user_id = $user_id;
1037                         $cur->message_format = 'wiki';
1038                         $cur->message_content = $_POST['p_content'];
1039                         $offset = dt::getTimeOffset($core->blog->settings->blog_timezone);
1040                         $cur->message_dt = date('Y-m-d H:i:s',time() + $offset);
1041                         //$cur->message_dt = date('Y-m-d H:i:s');
1042                         $cur->post_id = $_ctx->posts->post_id;
1043                         $cur->message_status =  1 ;
1044                         
1045                         $redir = $_ctx->posts->getURL();
1046                         $redir .= strpos($redir,'?') !== false ? '&' : '?';
1047                         
1048                         // thread_id : new field in base : link between posts of a same thread
1049                         //$cur->thread_id = $_ctx->posts->post_id;
1050                         
1051                         //$redir = $core->blog->url.$core->url->getBase("thread").'/'.$_ctx->posts->post_url;
1052                         //$redir .= strpos($redir,'?') !== false ? '&' : '?';
1053                         
1054                         //die(var_dump($cur));
1055                         
1056                         try
1057                         {
1058                              # --BEHAVIOR-- publicBeforePostCreate
1059                              $core->callBehavior('publicBeforeMessageCreate',$cur);
1060
1061                              //$post_id = $core->auth->sudo(array($core->blog,'addPost'),$cur);
1062                              //$comment_id = $core->blog->addComment($cur);
1063                              # update nb_comment (used as nb_answers for the thread)
1064                              //die($user_id);
1065
1066                              $message_id = $core->auth->sudo(array($_ctx->agora,'addMessage'),$cur);
1067               
1068                              # --BEHAVIOR-- publicAfterPostCreate
1069                              $core->callBehavior('publicAfterMessageCreate',$cur,$message_id);
1070                             
1071                              $_ctx->agora->triggerThread($_ctx->posts->post_id);
1072                             
1073                              $redir_arg = 'pub=1';
1074                             
1075                              header('Location: '.$redir.$redir_arg);
1076                              return;
1077                         }
1078                         
1079                         catch (Exception $e)
1080                         {
1081                              $_ctx->form_error = $e->getMessage();
1082                         }
1083                    }
1084               }
1085          }
1086         
1087          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
1088          self::serveDocument('thread.html','text/html',false);
1089          return;
1090     }
1091
1092     public static function threadpreview($args)
1093     {
1094          $core = $GLOBALS['core'];
1095          $_ctx = $GLOBALS['_ctx'];
1096         
1097          if (!preg_match('#^(.+?)/([0-9a-z]{40})/(.+?)$#',$args,$m)) {
1098               # The specified Preview URL is malformed.
1099               self::p404();
1100          }
1101          else
1102          {
1103               $user_id = $m[1];
1104               $user_key = $m[2];
1105               $post_url = $m[3];
1106               if (!$core->auth->checkUser($user_id,null,$user_key)) {
1107                    # The user has no access to the entry.
1108                    self::p404();
1109               }
1110               else
1111               {
1112                    $_ctx->preview = true;
1113                    self::thread($post_url);
1114               }
1115          }
1116     }
1117     
1118     public static function removethread($args)
1119     {
1120          global $core, $_ctx;
1121          $user_id = $core->auth->userID();
1122         
1123          if ($_ctx->agora->isModerator($user_id) === false)
1124          {
1125               self::p404();
1126          }
1127
1128          $post_id = $args;
1129         
1130          if (!is_numeric($post_id))
1131          {
1132               self::p404();
1133          }
1134         
1135          $params['post_id'] = $args;
1136          $params['no_content'] = true;
1137          $params['post_type'] = 'thread';
1138          //$_ctx->posts = $_ctx->agora->getPostsPlus($params);
1139          $_ctx->posts = $core->blog->getPosts($params);
1140
1141          if ($_ctx->posts->isEmpty() )
1142          {
1143               self::p404();
1144          }
1145         
1146          //$redir = $core->blog->url.$core->url->getBase("thread").'/'.$_ctx->posts->post_url;
1147          $redir = $core->blog->url.$core->url->getBase("place").'/'.$_ctx->posts->cat_url;
1148
1149          $redir .= strpos($redir,'?') !== false ? '&' : '?';
1150         
1151          try
1152          {
1153               # --BEHAVIOR-- publicBeforePostDelete
1154               $core->callBehavior('publicBeforePostDelete',$post_id);
1155               
1156               $core->auth->sudo(array($core->blog,'delPost'),$post_id);
1157               # update nb_comment (used as nb_answers for the thread)
1158               //$_ctx->agora->triggerThread($thread_id);
1159               
1160               # --BEHAVIOR-- publicAfterPostDelete
1161               $core->callBehavior('publicAfterPostDelete',$post_id);
1162               
1163               $redir_arg = 'del=1';
1164               
1165               header('Location: '.$redir.$redir_arg);
1166               return;
1167          }
1168         
1169          catch (Exception $e)
1170          {
1171               $_ctx->form_error = $e->getMessage();
1172          }
1173               
1174     }
1175     
1176     public static function editthread($args)
1177     {
1178          global $core, $_ctx;
1179         
1180          $core->addBehavior('coreInitWikiPost',array('agoraBehaviors','coreInitWikiPost'));
1181          $user_id = $core->auth->userID();
1182         
1183          if ($_ctx->agora->isModerator($user_id) === false)
1184          {
1185               self::p404();
1186          }
1187         
1188          $params['post_id'] = $args ;
1189          $params['post_type'] = 'thread';
1190          $_ctx->posts = $core->blog->getPosts($params);
1191
1192          if ($_ctx->posts->isEmpty() )
1193          {
1194               self::p404();
1195          }
1196
1197          $_ctx->thread_preview = new ArrayObject();
1198          $_ctx->thread_preview['content'] = '';
1199          $_ctx->thread_preview['title'] = '';
1200          $_ctx->thread_preview['rawcontent'] = '';
1201          $_ctx->thread_preview['preview'] = false;
1202          $_ctx->thread_preview['cat'] = $_ctx->posts->cat_id;
1203          $_ctx->thread_preview['not_empty'] = ( $args == '' ) ? false : true;
1204
1205          $p_content = $_ctx->posts->post_content;
1206          $p_title = $_ctx->posts->post_title;
1207         
1208          $_ctx->thread_preview['rawcontent'] = $p_content;
1209          $_ctx->thread_preview['title'] = $p_title;
1210         
1211          $edit_post = isset($_POST['ed_content']) &&  isset($_POST['ed_title']);
1212         
1213          if ($edit_post)
1214          {
1215               $content = isset($_POST['ed_content'])? $_POST['ed_content'] : '';
1216               $title = isset($_POST['ed_title'])? $_POST['ed_title'] : '';
1217               $preview = !empty($_POST['preview']);
1218         
1219               if ($content != '')
1220               { 
1221                    $core->initWikiPost();
1222                    /// coreInitWikiPost
1223                    $content = $core->wikiTransform($content);
1224                    $content = $core->HTMLfilter($content);
1225               }
1226               
1227               if ($title != '')
1228               { 
1229                    //$title = $core->HTMLfilter($title);
1230               }
1231               
1232               $_ctx->thread_preview['content'] = $content;
1233               $_ctx->thread_preview['rawcontent'] =  $_POST['ed_content'];
1234               $_ctx->thread_preview['title'] = $_POST['ed_title'];
1235               $_ctx->thread_preview['cat'] = $_POST['ed_cat'];
1236               $_ctx->thread_preview['not_empty'] = true;
1237               
1238               if ($preview)
1239               {
1240                    # --BEHAVIOR-- publicBeforePostReview
1241                    $core->callBehavior('publicBeforePostReview',$_ctx->post_preview);
1242               
1243                    $_ctx->thread_preview['preview'] = true;
1244               }
1245               else
1246               {
1247                    $post_id = $args;
1248                    $cur = $core->con->openCursor($core->prefix.'post');
1249                    $cur->post_id = $post_id;
1250                    $cur->post_title = isset($_POST['ed_title'])? $_POST['ed_title'] : $_ctx->posts->post_title;
1251                    $cur->post_content = isset($_POST['ed_content'])? $_POST['ed_content'] : $p_content;
1252                    $cur->cat_id = isset($_POST['ed_cat'])? $_POST['ed_cat'] : $_ctx->posts->cat_id;
1253                    $cur->post_format =  'wiki';
1254                   
1255                    $redir = $core->blog->url.$core->url->getBase("thread").'/'.$_ctx->posts->post_url;
1256                   
1257                    /*else
1258                    {
1259                         //Ugly
1260                         $params['post_id'] = $_ctx->posts->thread_id;
1261                         $params['no_content'] = true;
1262                         $_ctx->posts2 = $_ctx->agora->getPostsPlus($params);
1263                         $redir = $core->blog->url.$core->url->getBase("thread").'/'.$_ctx->posts2->post_url;
1264                    }*/
1265                    $redir .= strpos($redir,'?') !== false ? '&' : '?';
1266                   
1267                    try
1268                    {
1269                         # --BEHAVIOR-- publicBeforePostUpdate
1270                         $core->callBehavior('publicBeforePostUpdate',$cur,$post_id );
1271                   
1272                         $core->auth->sudo(array($core->blog,'updPost'),$post_id,$cur);
1273                   
1274                         # --BEHAVIOR-- publicAfterPostUpdate
1275                         $core->callBehavior('publicAfterPostUpdate',$cur,$post_id);
1276                   
1277                         $redir_arg = 'edt=1';
1278                   
1279                         header('Location: '.$redir.$redir_arg);
1280                         return;
1281                    }
1282               
1283                    catch (Exception $e)
1284                    {
1285                         $_ctx->form_error = $e->getMessage();
1286                    }
1287               
1288               }
1289          }
1290          # The entry
1291          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
1292          self::serveDocument('editpost.html','text/html',false);
1293          return;
1294     }
1295
1296     public static function removemessage($args)
1297     {
1298          global $core, $_ctx;
1299          $user_id = $core->auth->userID();
1300         
1301          if ($_ctx->agora->isModerator($user_id) === false)
1302          {
1303               self::p404();
1304          }
1305
1306          $message_id = $args;
1307         
1308          if (!is_numeric($message_id))
1309          {
1310               self::p404();
1311          }
1312         
1313          $params['message_id'] = $args;
1314          $params['no_content'] = true;
1315
1316          //$_ctx->posts = $_ctx->agora->getPostsPlus($params);
1317          $_ctx->messages = $_ctx->agora->getMessages($params);
1318
1319          if ($_ctx->messages->isEmpty() )
1320          {
1321               self::p404();
1322          }
1323
1324          $redir = $core->blog->url.$core->url->getBase("thread").'/'.$_ctx->messages->post_url;
1325
1326          $redir .= strpos($redir,'?') !== false ? '&' : '?';
1327         
1328          try
1329          {
1330               # --BEHAVIOR-- publicBeforeMessageDelete
1331               $core->callBehavior('publicBeforeMessageDelete',$message_id);
1332
1333               $core->auth->sudo(array($_ctx->agora,'delMessage'),$message_id);
1334               # update nb_comment (used as nb_answers for the thread)
1335               //$_ctx->agora->triggerThread($thread_id);
1336               
1337               # --BEHAVIOR-- publicAfterMessageDelete
1338               $core->callBehavior('publicAfterMessageDelete',$message_id);
1339               
1340               $redir_arg = 'del=1';
1341               
1342               header('Location: '.$redir.$redir_arg);
1343
1344               return;
1345          }
1346         
1347          catch (Exception $e)
1348          {
1349               throw new Exception($e->getMessage());
1350          }
1351               
1352     }
1353     
1354     public static function editmessage($args)
1355     {
1356          global $core, $_ctx;
1357         
1358          $core->addBehavior('coreInitWikiPost',array('agoraBehaviors','coreInitWikiPost'));
1359          $user_id = $core->auth->userID();
1360         
1361          if ($_ctx->agora->isModerator($user_id) === false)
1362          {
1363               self::p404();
1364          }
1365         
1366          $params['message_id'] = $args ;
1367          $_ctx->messages = $_ctx->agora->getMessages($params);
1368
1369          if ($_ctx->messages->isEmpty() )
1370          {
1371               self::p404();
1372          }
1373
1374          $_ctx->message_preview = new ArrayObject();
1375          $_ctx->message_preview['content'] = '';
1376          $_ctx->message_preview['rawcontent'] = '';
1377          $_ctx->message_preview['preview'] = false;
1378
1379          $m_content = $_ctx->messages->message_content;
1380         
1381          $_ctx->message_preview['rawcontent'] = $m_content;
1382         
1383          $edit_message = isset($_POST['ed_content_m']);
1384         
1385          if ($edit_message)
1386          {
1387               $content = isset($_POST['ed_content_m'])? $_POST['ed_content_m'] : '';
1388
1389               $preview = !empty($_POST['preview']);
1390         
1391               if ($content != '')
1392               { 
1393                    $core->initWikiPost();
1394                    /// coreInitWikiPost
1395                    $content = $core->wikiTransform($content);
1396                    $content = $core->HTMLfilter($content);
1397               }
1398               
1399               $_ctx->message_preview['content'] = $content;
1400               $_ctx->message_preview['rawcontent'] =  $_POST['ed_content_m'];
1401               
1402               if ($preview)
1403               {
1404                    # --BEHAVIOR-- publicBeforePostReview
1405                    $core->callBehavior('publicBeforeMessagePreview',$_ctx->message_preview);
1406               
1407                    $_ctx->message_preview['preview'] = true;
1408               }
1409               else
1410               {
1411                    $message_id = $args;
1412                    $cur = $core->con->openCursor($core->prefix.'message');
1413                    $cur->message_id = $message_id;
1414                    $cur->message_content = isset($_POST['ed_content_m'])? $_POST['ed_content_m'] : $m_content;
1415                    $cur->message_format =  'wiki';
1416                   
1417                    $redir = $core->blog->url.$core->url->getBase("thread").'/'.$_ctx->messages->post_url;
1418                   
1419                    /*else
1420                    {
1421                         //Ugly
1422                         $params['post_id'] = $_ctx->posts->thread_id;
1423                         $params['no_content'] = true;
1424                         $_ctx->posts2 = $_ctx->agora->getPostsPlus($params);
1425                         $redir = $core->blog->url.$core->url->getBase("thread").'/'.$_ctx->posts2->post_url;
1426                    }*/
1427                    $redir .= strpos($redir,'?') !== false ? '&' : '?';
1428                   
1429                    try
1430                    {
1431                         # --BEHAVIOR-- publicBeforePostUpdate
1432                         $core->callBehavior('publicBeforeMessageUpdate',$cur,$message_id );
1433                   
1434                         $core->auth->sudo(array($_ctx->agora,'updMessage'),$message_id,$cur);
1435                   
1436                         # --BEHAVIOR-- publicAfterPostUpdate
1437                         $core->callBehavior('publicAfterMessageUpdate',$cur,$message_id);
1438                   
1439                         $redir_arg = 'edm=1';
1440                   
1441                         header('Location: '.$redir.$redir_arg);
1442                         return;
1443                    }
1444               
1445                    catch (Exception $e)
1446                    {
1447                         $_ctx->form_error = $e->getMessage();
1448                    }
1449               
1450               }
1451          }
1452          # The entry
1453          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
1454          self::serveDocument('editmessage.html','text/html',false);
1455          return;
1456     }
1457
1458     public static function feed($args)
1459     { // need review
1460          global $core, $_ctx;
1461         
1462          $type = null;
1463          $messages = false;
1464          $cat_url = false;
1465          $post_id = null;
1466          $params = array();
1467          $subtitle = '';
1468         
1469          $mime = 'application/xml';
1470         
1471          //$_ctx =& $GLOBALS['_ctx'];
1472          //$core =& $GLOBALS['core'];
1473         
1474          if (preg_match('!^([a-z]{2}(-[a-z]{2})?)/(.*)$!',$args,$m)) {
1475               $params['lang'] = $m[1];
1476               $args = $m[3];
1477
1478               $_ctx->langs = $core->blog->getLangs($params);
1479         
1480               if ($_ctx->langs->isEmpty()) {
1481                    self::p404();
1482               } else {
1483                    $_ctx->cur_lang = $m[1];
1484               }
1485          }
1486
1487          if (preg_match('#^(atom|rss2)/messages/([0-9]+)$#',$args,$m))
1488          {
1489               # Thread messages feed
1490               $type = $m[1];
1491               $messages = true;
1492               $post_id = (integer) $m[2];
1493          }
1494          elseif (preg_match('#^(?:place/(.+)/)?(atom|rss2)(/messages)?$#',$args,$m))
1495          {
1496               # All posts or comments feed
1497               $type = $m[2];
1498               $messages = !empty($m[3]);
1499               if (!empty($m[1])) {
1500                    $cat_url = $m[1];
1501               }
1502          }
1503          else
1504          {
1505               self::p404();
1506               return;
1507          }
1508         
1509          if ($cat_url)
1510          {
1511               $params['cat_url'] = $cat_url;
1512               $params['post_type'] = 'thread';
1513               //$params['threads_only'] = true;
1514               $_ctx->categories = $core->blog->getCategories($params);
1515
1516               if ($_ctx->categories->isEmpty()) {          //die ('coucou 1');           
1517                    self::p404();
1518               }
1519               
1520               $subtitle = ' - '.$_ctx->categories->cat_title;
1521          }
1522          elseif ($post_id)
1523          {
1524               $params['post_id'] = $post_id;
1525               $params['post_type'] = 'thread';
1526               //$_ctx->posts = $_ctx->agora->getPostsPlus($params);
1527               $_ctx->posts = $core->blog->getPosts($params);
1528               
1529               if ($_ctx->posts->isEmpty()) { 
1530                    self::p404();
1531               }
1532               //die($_ctx->posts->post_content);
1533               $subtitle = ' - '.$_ctx->posts->post_title;
1534          }
1535         
1536          $tpl = 'agora-'.$type;
1537          if ($messages) {
1538               $tpl .= '-messages';
1539               //$_ctx->nb_comment_per_page = $core->blog->settings->nb_comment_per_feed;
1540               $_ctx->nb_message_per_page = $core->blog->settings->nb_message_per_feed;
1541          } else {
1542               $_ctx->nb_entry_per_page = $core->blog->settings->nb_post_per_feed;
1543               $_ctx->short_feed_items = $core->blog->settings->short_feed_items;
1544          }
1545          $tpl .= '.xml';
1546         
1547          if ($type == 'atom') {
1548               $mime = 'application/atom+xml';
1549          }
1550         
1551          $_ctx->feed_subtitle = $subtitle;
1552          header('X-Robots-Tag: '.context::robotsPolicy($core->blog->settings->robots_policy,''));
1553          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
1554          self::serveDocument($tpl,$mime);
1555          return;
1556     }
1557}
1558
1559class widgetsAgora
1560{
1561     public static function memberWidget($w)
1562     {
1563          global $core;
1564         
1565          if ($core->url->type != 'place' && $core->url->type != 'thread' 
1566               && $core->url->type != 'agora' && $core->url->type != 'agora-page') {
1567               return;
1568          }
1569         
1570          $user_displayname = ($core->auth->getInfo('user_displayname') == '' )? $core->auth->userID() : $core->auth->getInfo('user_displayname');
1571         
1572          $content = 
1573               '<li><a href="'.$core->blog->url.$core->url->getBase("agora").'">'.__('Home').'</a></li>';
1574          $content .=
1575               ($core->auth->userID() != false && isset($_SESSION['sess_user_id'])) ?
1576               '<li><a href="'.$core->blog->url.$core->url->getBase("newthread").'">'.__('New thread').'</a></li>'.
1577               '<li><a href="'.$core->blog->url.$core->url->getBase("profile").'/'.$core->auth->userID().'"><strong>'.$user_displayname.'</strong></a></li>'.
1578               '<li><a href="'.$core->blog->url.$core->url->getBase("logout").'">'.__('Logout').'</a></li>' : 
1579               '<li><a href="'.$core->blog->url.$core->url->getBase("login").'">'.__('Login').'</a></li>'.
1580               '<li><a href="'.$core->blog->url.$core->url->getBase("register").'">'.__('Register').'</a></li>';
1581         
1582          return
1583          '<div class="agorabox">'.
1584          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
1585          '<ul>'.
1586          $content.
1587          '</ul>'.
1588          '</div>';
1589     }
1590     
1591     public static function moderateWidget($w)
1592     {
1593          global $core, $_ctx;
1594         
1595          if ($core->url->type != 'thread') {
1596               return;
1597          }
1598         
1599          $url = $core->blog->url.$core->url->getBase("thread")."/".$_ctx->posts->post_url;
1600          $url .= strpos($core->blog->url,'?') !== false ? '&' : '?';
1601          $openclose = $_ctx->posts->post_open_comment ? 
1602               '<li><a href="'.$url.'action=close'.'">'.__('Close the thread').'</a></li>' : 
1603               '<li><a href="'.$url.'action=open'.'">'.__('Open the thread').'</a></li>';
1604               
1605          $pinunpin = $_ctx->posts->post_selected ? 
1606               '<li><a href="'.$url.'action=unpin'.'">'.__('Unpin the thread').'</a></li>' : 
1607               '<li><a href="'.$url.'action=pin'.'">'.__('Pin the thread').'</a></li>';
1608         
1609          $res  = 
1610               (($core->auth->userID() != false) && $_ctx->agora->isModerator($core->auth->userID()) === true) ?
1611               '<div class="agoramodobox">'.
1612               ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
1613               '<ul>'.
1614               $openclose.
1615               $pinunpin.
1616               '</ul>'.
1617               '</div>' :'';
1618         
1619          return $res;
1620     }
1621
1622     public static function categoriesWidget($w)
1623     {
1624          global $core;
1625
1626          if ($core->url->type != 'place' && $core->url->type != 'thread' 
1627               && $core->url->type != 'agora' && $core->url->type != 'agora-page') {
1628               return;
1629          }
1630         
1631          $rs = $core->blog->getCategories(array('post_type'=>'thread'));
1632          if ($rs->isEmpty()) {
1633               return;
1634          }
1635         
1636          $res =
1637          '<div class="places">'.
1638          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '');
1639         
1640          $ref_level = $level = $rs->level-1;
1641          while ($rs->fetch())
1642          {
1643               $class = '';
1644               if (($core->url->type == 'place' && $_ctx->categories instanceof record && $_ctx->categories->cat_id == $rs->cat_id)
1645               || ($core->url->type == 'thread' && $_ctx->posts instanceof record && $_ctx->posts->cat_id == $rs->cat_id)) {
1646                    $class = ' class="place-current"';
1647               }
1648               
1649               if ($rs->level > $level) {
1650                    $res .= str_repeat('<ul><li'.$class.'>',$rs->level - $level);
1651               } elseif ($rs->level < $level) {
1652                    $res .= str_repeat('</li></ul>',-($rs->level - $level));
1653               }
1654               
1655               if ($rs->level <= $level) {
1656                    $res .= '</li><li'.$class.'>';
1657               }
1658               
1659               $res .=
1660               '<a href="'.$core->blog->url.$core->url->getBase('place').'/'.
1661               $rs->cat_url.'">'.
1662               html::escapeHTML($rs->cat_title).'</a>'.
1663               ($w->postcount ? ' ('.$rs->nb_post.')' : '');
1664               
1665               
1666               $level = $rs->level;
1667          }
1668         
1669          if ($ref_level - $level < 0) {
1670               $res .= str_repeat('</li></ul>',-($ref_level - $level));
1671          }
1672          $res .= '</div>';
1673         
1674          return $res;
1675     }
1676
1677     public static function bestofWidget($w)
1678     {
1679          global $core;
1680         
1681          if ($w->homeonly && $core->url->type != 'agora') {
1682               return;
1683          }
1684         
1685          $params = array(
1686               'post_type' => 'thread',
1687               'post_selected'=>true,
1688               'no_content'=>true,
1689               'order'=>'post_dt desc');
1690         
1691          $rs = $core->blog->getPosts($params);
1692         
1693          if ($rs->isEmpty()) {
1694               return;
1695          }
1696         
1697          $res =
1698          '<div class="selected">'.
1699          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
1700          '<ul>';
1701         
1702          while ($rs->fetch()) {
1703               $res .= ' <li><a href="'.$rs->getURL().'">'.html::escapeHTML($rs->post_title).'</a></li> ';
1704          }
1705         
1706          $res .= '</ul></div>';
1707         
1708          return $res;
1709     }
1710
1711     public static function lastthreadsWidget($w)
1712     {
1713          global $core;
1714         
1715          if ($w->homeonly && $core->url->type != 'agora') {
1716               return;
1717          }
1718         
1719          $params['post_type'] = 'thread';
1720          $params['limit'] = abs((integer) $w->limit);
1721          $params['order'] = 'post_dt desc';
1722          $params['no_content'] = true;
1723         
1724          if ($w->category)
1725          {
1726               if ($w->category == 'null') {
1727                    $params['sql'] = ' AND p.cat_id IS NULL ';
1728               } elseif (is_numeric($w->category)) {
1729                    $params['cat_id'] = (integer) $w->category;
1730               } else {
1731                    $params['cat_url'] = $w->category;
1732               }
1733          }
1734         
1735          $rs = $core->blog->getPosts($params);
1736         
1737          if ($rs->isEmpty()) {
1738               return;
1739          }
1740         
1741          $res =
1742          '<div class="lastthreads">'.
1743          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
1744          '<ul>';
1745         
1746          while ($rs->fetch()) {
1747               $res .= '<li><a href="'.$rs->getURL().'">'.
1748               html::escapeHTML($rs->post_title).'</a></li>';
1749          }
1750         
1751          $res .= '</ul></div>';
1752         
1753          return $res;
1754     }
1755
1756     public static function lastmessagesWidget($w)
1757     {
1758          global $core, $_ctx;
1759         
1760          if ($w->homeonly && $core->url->type != 'agora') {
1761               return;
1762          }
1763         
1764          $params['limit'] = abs((integer) $w->limit);
1765          //$params['order'] = 'message_dt desc';
1766          if ($w->category)
1767          {
1768               if ($w->category == 'null') {
1769                    $params['sql'] = ' AND p.cat_id IS NULL ';
1770               } elseif (is_numeric($w->category)) {
1771                    $params['cat_id'] = (integer) $w->category;
1772               } else {
1773                    $params['cat_url'] = $w->category;
1774               }
1775          }
1776          $agora = new agora($core);
1777          $rs = $agora->getMessages($params);
1778
1779          if ($rs->isEmpty()) {
1780               return;
1781          }
1782         
1783          $res = '<div class="lastmessages">'.
1784          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
1785          '<ul>';
1786         
1787          while ($rs->fetch())
1788          {
1789               $res .= '<li><a href="'.$rs->getThreadURL().'#m'.$rs->message_id.'">'.
1790               html::escapeHTML($rs->post_title).' - '.
1791               html::escapeHTML($rs->getAuthorCN()).
1792               '</a></li>';
1793          }
1794         
1795          $res .= '</ul></div>';
1796         
1797          return $res;
1798     }
1799
1800     public static function subscribeWidget($w)
1801     {
1802          global $core, $_ctx;
1803
1804          if ($w->homeonly && $core->url->type != 'agora') {
1805               return;
1806          }
1807         
1808          $type = ($w->type == 'atom' || $w->type == 'rss2') ? $w->type : 'rss2';
1809          $mime = $type == 'rss2' ? 'application/rss+xml' : 'application/atom+xml';
1810         
1811          $p_title = __('This agora\'s threads %s feed');
1812          $c_title = __('This agora\'s messages %s feed');
1813         
1814          $res =
1815          '<div class="syndicate">'.
1816          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
1817          '<ul>';
1818         
1819          $res .=
1820          '<li><a type="'.$mime.'" '.
1821          'href="'.$core->blog->url.$core->url->getBase('agora_feed').'/'.$type.'" '.
1822          'title="'.sprintf($p_title,($type == 'atom' ? 'Atom' : 'RSS')).'" class="feed">'.
1823          __('Threads feed').'</a></li>'.
1824          '<li><a type="'.$mime.'" '.
1825          'href="'.$core->blog->url.$core->url->getBase('agora_feed').'/'.$type.'/messages" '.
1826          'title="'.sprintf($c_title,($type == 'atom' ? 'Atom' : 'RSS')).'" class="feed">'.
1827          __('Messages feed').'</a></li>';
1828         
1829          $res .= '</ul></div>';
1830         
1831          return $res;
1832     }
1833}
1834?>
Note: See TracBrowser for help on using the repository browser.

Sites map