Dotclear

source: plugins/agora/_public.php @ 1882

Revision 1882, 46.1 KB checked in by Osku, 14 years ago (diff)

agora - still in progress

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

Sites map