Dotclear

source: plugins/muppet/_public.php @ 2718

Revision 2718, 13.1 KB checked in by Osku, 13 years ago (diff)

Muppet 1.1 - Ready for Dotaddict ?

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of muppet, a plugin for Dotclear 2.
5#
6# Copyright (c) 2010 Osku and contributors
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11#
12# -- END LICENSE BLOCK ------------------------------------
13if (!defined('DC_RC_PATH')) { return; }
14
15$core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
16$core->addBehavior('templateBeforeBlock',array('behaviorsMuppet','templateBeforeBlock'));
17$core->addBehavior('publicBeforeSearchCount',array('behaviorsMuppet','publicBeforeSearchCount'));
18$core->addBehavior('initCommentsWikibar', array('muppetPublicBehaviors','initCommentsWikibar'));
19$core->tpl->addValue('muppetFeedURL',array('muppetTpl','muppetFeedURL'));
20
21class muppetTpl 
22{
23     public static function muppetFeedURL($attr)
24     {
25          global $core, $_ctx;
26          $type = !empty($attr['type']) ? $attr['type'] : 'rss2';
27         
28          if (!preg_match('#^(rss2|atom)$#',$type)) {
29               $type = 'rss2';
30          }
31         
32          $f = $GLOBALS['core']->tpl->getFilters($attr);
33          return '<?php echo '.sprintf($f,'$core->blog->url.'.'$_ctx->muppet_feed."/'.$type.'"').'; ?>';
34     }
35}
36
37class muppetPublicBehaviors
38{
39     public static function initCommentsWikibar($modes)
40     {
41          $types = muppet::getPostTypes();
42          if (!empty($types))
43          {
44               foreach ($types as $k => $v)
45               {
46                    $modes[] = $k;
47               }
48          }
49     }
50}
51
52class urlMuppet extends dcUrlHandlers
53{
54     public static function singlepost($args)
55     {
56          if ($args == '') {
57               # No page was specified.
58               self::p404();
59          }
60          else
61          {
62               $_ctx =& $GLOBALS['_ctx'];
63               $core =& $GLOBALS['core'];
64
65               $core->blog->withoutPassword(false);
66
67               $params = new ArrayObject();
68               $params['post_type'] = str_replace('preview', '', $core->url->type);
69               $params['post_url'] = $args;
70
71               $_ctx->posts = $core->blog->getPosts($params);
72
73               $_ctx->comment_preview = new ArrayObject();
74               $_ctx->comment_preview['content'] = '';
75               $_ctx->comment_preview['rawcontent'] = '';
76               $_ctx->comment_preview['name'] = '';
77               $_ctx->comment_preview['mail'] = '';
78               $_ctx->comment_preview['site'] = '';
79               $_ctx->comment_preview['preview'] = false;
80               $_ctx->comment_preview['remember'] = false;
81
82               $core->blog->withoutPassword(true);
83
84
85               if ($_ctx->posts->isEmpty())
86               {
87                    # The specified page does not exist.
88                    self::p404();
89               }
90               else
91               {
92                    $post_id = $_ctx->posts->post_id;
93                    $post_password = $_ctx->posts->post_password;
94
95                    # Password protected entry
96                    if ($post_password != '' && !$_ctx->preview)
97                    {
98                         # Get passwords cookie
99                         if (isset($_COOKIE['dc_passwd'])) {
100                              $pwd_cookie = unserialize($_COOKIE['dc_passwd']);
101                         } else {
102                              $pwd_cookie = array();
103                         }
104
105                         # Check for match
106                         if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
107                         || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password))
108                         {
109                              $pwd_cookie[$post_id] = $post_password;
110                              setcookie('dc_passwd',serialize($pwd_cookie),0,'/');
111                         }
112                         else
113                         {
114                              self::serveDocument('password-form.html','text/html',false);
115                              return;
116                         }
117                    }
118
119                    $post_comment =
120                         isset($_POST['c_name']) && isset($_POST['c_mail']) &&
121                         isset($_POST['c_site']) && isset($_POST['c_content']) &&
122                         $_ctx->posts->commentsActive();
123
124                    # Posting a comment
125                    if ($post_comment)
126                    {
127                         # Spam trap
128                         if (!empty($_POST['f_mail'])) {
129                              http::head(412,'Precondition Failed');
130                              header('Content-Type: text/plain');
131                              echo "So Long, and Thanks For All the Fish";
132                              # Exits immediately the application to preserve the server.
133                              exit;
134                         }
135
136                         $name = $_POST['c_name'];
137                         $mail = $_POST['c_mail'];
138                         $site = $_POST['c_site'];
139                         $content = $_POST['c_content'];
140                         $preview = !empty($_POST['preview']);
141
142                         if ($content != '')
143                         {
144                              if ($core->blog->settings->system->wiki_comments) {
145                                   $core->initWikiComment();
146                              } else {
147                                   $core->initWikiSimpleComment();
148                              }
149                              $content = $core->wikiTransform($content);
150                              $content = $core->HTMLfilter($content);
151                         }
152
153                         $_ctx->comment_preview['content'] = $content;
154                         $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
155                         $_ctx->comment_preview['name'] = $name;
156                         $_ctx->comment_preview['mail'] = $mail;
157                         $_ctx->comment_preview['site'] = $site;
158
159                         if ($preview)
160                         {
161                              # --BEHAVIOR-- publicBeforeCommentPreview
162                              $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
163
164                              $_ctx->comment_preview['preview'] = true;
165                         }
166                         else
167                         {
168                              # Post the comment
169                              $cur = $core->con->openCursor($core->prefix.'comment');
170                              $cur->comment_author = $name;
171                              $cur->comment_site = html::clean($site);
172                              $cur->comment_email = html::clean($mail);
173                              $cur->comment_content = $content;
174                              $cur->post_id = $_ctx->posts->post_id;
175                              $cur->comment_status = $core->blog->settings->system->comments_pub ? 1 : -1;
176                              $cur->comment_ip = http::realIP();
177
178                              $redir = $_ctx->posts->getURL();
179                              $redir .= strpos($redir,'?') !== false ? '&' : '?';
180
181                              try
182                              {
183                                   if (!text::isEmail($cur->comment_email)) {
184                                        throw new Exception(__('You must provide a valid email address.'));
185                                   }
186
187                                   # --BEHAVIOR-- publicBeforeCommentCreate
188                                   $core->callBehavior('publicBeforeCommentCreate',$cur);
189                                   if ($cur->post_id) {
190                                        $comment_id = $core->blog->addComment($cur);
191
192                                        # --BEHAVIOR-- publicAfterCommentCreate
193                                        $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
194                                   }
195
196                                   if ($cur->comment_status == 1) {
197                                        $redir_arg = 'pub=1';
198                                   } else {
199                                        $redir_arg = 'pub=0';
200                                   }
201
202                                   header('Location: '.$redir.$redir_arg);
203                              }
204                              catch (Exception $e)
205                              {
206                                   $_ctx->form_error = $e->getMessage();
207                                   $_ctx->form_error;
208                              }
209                         }
210                    }
211
212                    $mytpl = $params['post_type'];
213
214                    # The entry
215                    $tpl = 'single-'.$mytpl.'.html';
216                    if (!$core->tpl->getFilePath($tpl)) {
217                         $tpl = 'post.html';
218                    }
219                    self::serveDocument($tpl);
220               }
221          }
222     }
223
224     public static function singlepreview($args)
225     {
226          $core = $GLOBALS['core'];
227          $_ctx = $GLOBALS['_ctx'];
228
229          if (!preg_match('#^(.+?)/([0-9a-z]{40})/(.+?)$#',$args,$m)) {
230               # The specified Preview URL is malformed.
231               self::p404();
232          }
233          else
234          {
235               $user_id = $m[1];
236               $user_key = $m[2];
237               $post_url = $m[3];
238               if (!$core->auth->checkUser($user_id,null,$user_key)) {
239                    # The user has no access to the entry.
240                    self::p404();
241               }
242               else
243               {
244                    $_ctx->preview = true;
245                    self::singlepost($post_url);
246               }
247          }
248     }
249
250     public static function listpost($args)
251     {
252          $core = $GLOBALS['core'];
253          $_ctx = $GLOBALS['_ctx'];
254
255          $n = self::getPageNumber($args);
256
257          if ($args && !$n)
258          {
259               # "Then specified URL went unrecognized by all URL handlers and
260               # defaults to the home page, but is not a page number.
261               self::p404();
262          }
263          else
264          {
265               // url->type : *s
266               $params['post_type'] = substr($core->url->type, 0, -1);
267               if ($n) {
268                    $GLOBALS['_page_number'] = $n;
269               }
270               
271               $mytpl = $params['post_type'];
272               $_ctx->muppet_feed = $core->url->getBase($mytpl.'_feed');
273               
274               $_ctx->posts = $core->blog->getPosts($params);
275               
276               # The list of entries
277               $tpl = 'list-'.$mytpl.'.html';
278               if (!$core->tpl->getFilePath($tpl)) {
279                    $tpl = 'muppet-list.html';
280               }
281               self::serveDocument($tpl);
282          }
283     }
284     
285     public static function mupFeed($args)
286     {
287          $core = $GLOBALS['core'];
288          $_ctx = $GLOBALS['_ctx'];
289         
290          if (!preg_match('#^(atom|rss2)(/comments)?$#',$args,$m))
291          {
292               self::p404();
293          }
294          else
295          {
296               $types = muppet::getPostTypes();
297               $type = $m[1];
298               $comments = !empty($m[2]);
299               
300               // url->type : *_feed
301               $params['post_type'] = substr($core->url->type, 0, -5);
302               $mytype = $params['post_type'];
303               
304               $_ctx->posts = $core->blog->getPosts($params);
305               
306               if ($_ctx->posts->isEmpty())
307               {
308                    # The specified tag does not exist.
309                    self::p404();
310               }
311               else
312               {
313                    $_ctx->muppet_feed = $core->url->getBase($core->url->type);
314                    $GLOBALS['_ctx']->feed_subtitle = ' - '.ucfirst($types[$mytype]['plural']) ;
315                   
316                    if ($type == 'atom') {
317                         $mime = 'application/atom+xml';
318                    } else {
319                         $mime = 'application/xml';
320                    }
321                   
322                    $tpl = $type;
323                    if ($comments) {
324                         $tpl .= '-comments';
325                         $GLOBALS['_ctx']->nb_comment_per_page = $GLOBALS['core']->blog->settings->system->nb_comment_per_feed;
326                    } else {
327                         $GLOBALS['_ctx']->nb_entry_per_page = $GLOBALS['core']->blog->settings->system->nb_post_per_feed;
328                         $GLOBALS['_ctx']->short_feed_items = $GLOBALS['core']->blog->settings->system->short_feed_items;
329                    }
330                    $tpl .= '.xml';
331                   
332                    self::serveDocument($tpl,$mime);
333               }
334          }
335     }
336     
337     public static function category($args)
338     {
339          $_ctx =& $GLOBALS['_ctx'];
340          $core =& $GLOBALS['core'];
341         
342          $n = self::getPageNumber($args);
343         
344          if ($args == '' && !$n) {
345               # No category was specified.
346               self::p404();
347          }
348          else
349          {
350               $params['cat_url'] = $args;
351               // Waiting ticket http://dev.dotclear.org/2.0/ticket/1090
352               //$params['post_type'] = 'post';
353               
354               $_ctx->categories = $core->blog->getCategories($params);
355               
356               if ($_ctx->categories->isEmpty()) {
357                    # The specified category does no exist.
358                    self::p404();
359               }
360               else
361               {
362                    if ($n) {
363                         $GLOBALS['_page_number'] = $n;
364                    }
365                    self::serveDocument('category.html');
366               }
367          }
368     }
369
370     public static function archive($args)
371     {
372          $_ctx =& $GLOBALS['_ctx'];
373          $core =& $GLOBALS['core'];
374         
375          $year = $month = $cat_url = null;
376          # Nothing or year and month
377          if ($args == '')
378          {
379               self::serveDocument('archive.html');
380          }
381          elseif (preg_match('|^/([0-9]{4})/([0-9]{2})$|',$args,$m))
382          {
383               $params['year'] = $m[1];
384               $params['month'] = $m[2];
385               $params['type'] = 'month';
386               // Waiting ticket http://dev.dotclear.org/2.0/ticket/1090
387               $types = muppet::getPostTypes();
388         
389               if (!empty($types)) {
390                    $post_types = array();
391               
392                    foreach ($types as $k => $v) {
393                         if ($v['integration'] === true) {
394                              $post_types[] = $k;
395                         }
396                    }
397                    $params['post_type'] = $post_types;
398                    $params['post_type'][] = 'post';
399               }
400               $_ctx->archives = $core->blog->getDates($params);
401               
402               if ($_ctx->archives->isEmpty()) {
403                    # There is no entries for the specified period.
404                    self::p404();
405               }
406               else
407               {
408                    self::serveDocument('archive_month.html');
409               }
410          }
411          else {
412               # The specified URL is not a date.
413               self::p404();
414          }
415     }
416}
417
418class widgetsMuppet
419{
420     public static function bestofWidget($w)
421     {
422          global $core;
423
424          if ($w->homeonly && $core->url->type != 'default') {
425               return;
426          }
427
428          $params = array(
429               'post_type' => $w->posttype,
430               'post_selected'=>true,
431               'no_content'=>true,
432               'order'=>'post_dt desc');
433
434          $rs = $core->blog->getPosts($params);
435
436          if ($rs->isEmpty()) {
437               return;
438          }
439
440          $res =
441          '<div class="selected">'.
442          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
443          '<ul>';
444
445          while ($rs->fetch()) {
446               $res .= ' <li><a href="'.$rs->getURL().'">'.html::escapeHTML($rs->post_title).'</a></li> ';
447          }
448
449          $res .= '</ul></div>';
450
451          return $res;
452     }
453
454     public static function lastpostsWidget($w)
455     {
456          global $core;
457
458          if ($w->homeonly && $core->url->type != 'default') {
459               return;
460          }
461
462          $params['post_type'] = $w->posttype;
463          $params['limit'] = abs((integer) $w->limit);
464          $params['order'] = 'post_id desc';
465          $params['no_content'] = true;
466
467          if ($w->category)
468          {
469               if ($w->category == 'null') {
470                    $params['sql'] = ' AND p.cat_id IS NULL ';
471               } elseif (is_numeric($w->category)) {
472                    $params['cat_id'] = (integer) $w->category;
473               } else {
474                    $params['cat_url'] = $w->category;
475               }
476          }
477
478          if ($w->tag)
479          {
480               $params['meta_id'] = $w->tag;
481               $rs = $core->meta->getPostsByMeta($params);
482          }
483          else
484          {
485               $rs = $core->blog->getPosts($params);
486          }
487
488          if ($rs->isEmpty()) {
489               return;
490          }
491
492          $res =
493          '<div class="lastposts">'.
494          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
495          '<ul>';
496
497          while ($rs->fetch()) {
498               $res .= '<li><a href="'.$rs->getURL().'">'.
499               html::escapeHTML($rs->post_title).'</a></li>';
500          }
501
502          $res .= '</ul>';
503
504          if ($core->url->getBase($w->posttype.'s') && !is_null($w->pagelink) && $w->pagelink !== '')
505          {
506               $res .=
507               '<p><strong><a href="'.$core->blog->url.$core->url->getBase($w->posttype.'s').'">'.
508               html::escapeHTML($w->pagelink).'</a></strong></p>';
509          }
510
511          $res .= '</div>';
512
513          return $res;
514     }
515}
516
517class behaviorsMuppet
518{
519     public static function templateBeforeBlock($core,$b,$attr)
520     {
521          // Url->type : default, default-page, category, archive, tag, feed
522          if (($b == 'Entries' || $b == 'Archives' || $b == 'ArchivePrevious' || $b =='ArchiveNext' ) && !isset($attr['post_type']))
523          {
524               return
525               "<?php\n".
526               'if (!isset($params)) $params=array();'."\n".
527               'toolsmuppet::typesToInclude($core->url->type,$params);'."\n".
528               "?>\n";
529          }
530     }
531
532     public static function publicBeforeSearchCount($s_params)
533     {
534          global $core;
535          $types = muppet::getPostTypes();
536
537          if (!empty($types)) {
538               $post_types = array();
539
540               foreach ($types as $k => $v) {
541                    if ($v['integration'] === true) {
542                         $post_types[] = $k;
543                    }
544               }
545
546               if (count($post_types) > 0) {
547                    if (!isset($s_params['post_type'])) {
548                         $s_params['post_type']=array('post');
549                    }
550                    $s_params['post_type'] = array_merge($s_params['post_type'],$post_types);
551               }
552          }
553     }
554}
555?>
Note: See TracBrowser for help on using the repository browser.

Sites map