Dotclear

source: plugins/muppet/_public.php @ 2578

Revision 2578, 9.1 KB checked in by Osku, 13 years ago (diff)

grumpf

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
19class urlMuppet extends dcUrlHandlers
20{
21     public static function singlepost($args)
22     {
23          if ($args == '') {
24               # No page was specified.
25               self::p404();
26          }
27          else
28          {
29               $_ctx =& $GLOBALS['_ctx'];
30               $core =& $GLOBALS['core'];
31
32               $core->blog->withoutPassword(false);
33
34               $params = new ArrayObject();
35               $params['post_type'] = str_replace('preview', '', $core->url->type);
36               $params['post_url'] = $args;
37
38               $_ctx->posts = $core->blog->getPosts($params);
39
40               $_ctx->comment_preview = new ArrayObject();
41               $_ctx->comment_preview['content'] = '';
42               $_ctx->comment_preview['rawcontent'] = '';
43               $_ctx->comment_preview['name'] = '';
44               $_ctx->comment_preview['mail'] = '';
45               $_ctx->comment_preview['site'] = '';
46               $_ctx->comment_preview['preview'] = false;
47               $_ctx->comment_preview['remember'] = false;
48
49               $core->blog->withoutPassword(true);
50
51
52               if ($_ctx->posts->isEmpty())
53               {
54                    # The specified page does not exist.
55                    self::p404();
56               }
57               else
58               {
59                    $post_id = $_ctx->posts->post_id;
60                    $post_password = $_ctx->posts->post_password;
61
62                    # Password protected entry
63                    if ($post_password != '' && !$_ctx->preview)
64                    {
65                         # Get passwords cookie
66                         if (isset($_COOKIE['dc_passwd'])) {
67                              $pwd_cookie = unserialize($_COOKIE['dc_passwd']);
68                         } else {
69                              $pwd_cookie = array();
70                         }
71
72                         # Check for match
73                         if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
74                         || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password))
75                         {
76                              $pwd_cookie[$post_id] = $post_password;
77                              setcookie('dc_passwd',serialize($pwd_cookie),0,'/');
78                         }
79                         else
80                         {
81                              self::serveDocument('password-form.html','text/html',false);
82                              return;
83                         }
84                    }
85
86                    $post_comment =
87                         isset($_POST['c_name']) && isset($_POST['c_mail']) &&
88                         isset($_POST['c_site']) && isset($_POST['c_content']) &&
89                         $_ctx->posts->commentsActive();
90
91                    # Posting a comment
92                    if ($post_comment)
93                    {
94                         # Spam trap
95                         if (!empty($_POST['f_mail'])) {
96                              http::head(412,'Precondition Failed');
97                              header('Content-Type: text/plain');
98                              echo "So Long, and Thanks For All the Fish";
99                              # Exits immediately the application to preserve the server.
100                              exit;
101                         }
102
103                         $name = $_POST['c_name'];
104                         $mail = $_POST['c_mail'];
105                         $site = $_POST['c_site'];
106                         $content = $_POST['c_content'];
107                         $preview = !empty($_POST['preview']);
108
109                         if ($content != '')
110                         {
111                              if ($core->blog->settings->system->wiki_comments) {
112                                   $core->initWikiComment();
113                              } else {
114                                   $core->initWikiSimpleComment();
115                              }
116                              $content = $core->wikiTransform($content);
117                              $content = $core->HTMLfilter($content);
118                         }
119
120                         $_ctx->comment_preview['content'] = $content;
121                         $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
122                         $_ctx->comment_preview['name'] = $name;
123                         $_ctx->comment_preview['mail'] = $mail;
124                         $_ctx->comment_preview['site'] = $site;
125
126                         if ($preview)
127                         {
128                              # --BEHAVIOR-- publicBeforeCommentPreview
129                              $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
130
131                              $_ctx->comment_preview['preview'] = true;
132                         }
133                         else
134                         {
135                              # Post the comment
136                              $cur = $core->con->openCursor($core->prefix.'comment');
137                              $cur->comment_author = $name;
138                              $cur->comment_site = html::clean($site);
139                              $cur->comment_email = html::clean($mail);
140                              $cur->comment_content = $content;
141                              $cur->post_id = $_ctx->posts->post_id;
142                              $cur->comment_status = $core->blog->settings->system->comments_pub ? 1 : -1;
143                              $cur->comment_ip = http::realIP();
144
145                              $redir = $_ctx->posts->getURL();
146                              $redir .= strpos($redir,'?') !== false ? '&' : '?';
147
148                              try
149                              {
150                                   if (!text::isEmail($cur->comment_email)) {
151                                        throw new Exception(__('You must provide a valid email address.'));
152                                   }
153
154                                   # --BEHAVIOR-- publicBeforeCommentCreate
155                                   $core->callBehavior('publicBeforeCommentCreate',$cur);
156                                   if ($cur->post_id) {
157                                        $comment_id = $core->blog->addComment($cur);
158
159                                        # --BEHAVIOR-- publicAfterCommentCreate
160                                        $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
161                                   }
162
163                                   if ($cur->comment_status == 1) {
164                                        $redir_arg = 'pub=1';
165                                   } else {
166                                        $redir_arg = 'pub=0';
167                                   }
168
169                                   header('Location: '.$redir.$redir_arg);
170                              }
171                              catch (Exception $e)
172                              {
173                                   $_ctx->form_error = $e->getMessage();
174                                   $_ctx->form_error;
175                              }
176                         }
177                    }
178
179                    $mytpl = $params['post_type'];
180
181                    # The entry
182                    $tpl = 'single-'.$mytpl.'.html';
183                    if (!$core->tpl->getFilePath($tpl)) {
184                         $tpl = 'post.html';
185                    }
186                    self::serveDocument($tpl);
187               }
188          }
189     }
190
191     public static function singlepreview($args)
192     {
193          $core = $GLOBALS['core'];
194          $_ctx = $GLOBALS['_ctx'];
195
196          if (!preg_match('#^(.+?)/([0-9a-z]{40})/(.+?)$#',$args,$m)) {
197               # The specified Preview URL is malformed.
198               self::p404();
199          }
200          else
201          {
202               $user_id = $m[1];
203               $user_key = $m[2];
204               $post_url = $m[3];
205               if (!$core->auth->checkUser($user_id,null,$user_key)) {
206                    # The user has no access to the entry.
207                    self::p404();
208               }
209               else
210               {
211                    $_ctx->preview = true;
212                    self::singlepost($post_url);
213               }
214          }
215     }
216
217     public static function listpost($args)
218     {
219          $core = $GLOBALS['core'];
220          $_ctx = $GLOBALS['_ctx'];
221
222          $n = self::getPageNumber($args);
223
224          if ($args && !$n)
225          {
226               # "Then specified URL went unrecognized by all URL handlers and
227               # defaults to the home page, but is not a page number.
228               self::p404();
229          }
230          else
231          {
232               $params['post_type'] = substr($core->url->type, 0, -1);
233               if ($n) {
234                    $GLOBALS['_page_number'] = $n;
235               }
236
237               $mytpl = $params['post_type'];
238
239               $_ctx->posts = $core->blog->getPosts($params);
240
241               # The entry
242               $tpl = 'list-'.$mytpl.'.html';
243               if (!$core->tpl->getFilePath($tpl)) {
244                    $tpl = 'muppet-list.html';
245               }
246               self::serveDocument($tpl);
247          }
248     }
249}
250
251class widgetsMuppet
252{
253     public static function bestofWidget($w)
254     {
255          global $core;
256
257          if ($w->homeonly && $core->url->type != 'default') {
258               return;
259          }
260
261          $params = array(
262               'post_type' => $w->posttype,
263               'post_selected'=>true,
264               'no_content'=>true,
265               'order'=>'post_dt desc');
266
267          $rs = $core->blog->getPosts($params);
268
269          if ($rs->isEmpty()) {
270               return;
271          }
272
273          $res =
274          '<div class="selected">'.
275          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
276          '<ul>';
277
278          while ($rs->fetch()) {
279               $res .= ' <li><a href="'.$rs->getURL().'">'.html::escapeHTML($rs->post_title).'</a></li> ';
280          }
281
282          $res .= '</ul></div>';
283
284          return $res;
285     }
286
287     public static function lastpostsWidget($w)
288     {
289          global $core;
290
291          if ($w->homeonly && $core->url->type != 'default') {
292               return;
293          }
294
295          $params['post_type'] = $w->posttype;
296          $params['limit'] = abs((integer) $w->limit);
297          $params['order'] = 'post_id desc';
298          $params['no_content'] = true;
299
300          if ($w->category)
301          {
302               if ($w->category == 'null') {
303                    $params['sql'] = ' AND p.cat_id IS NULL ';
304               } elseif (is_numeric($w->category)) {
305                    $params['cat_id'] = (integer) $w->category;
306               } else {
307                    $params['cat_url'] = $w->category;
308               }
309          }
310
311          if ($w->tag)
312          {
313               $params['meta_id'] = $w->tag;
314               $rs = $core->meta->getPostsByMeta($params);
315          }
316          else
317          {
318               $rs = $core->blog->getPosts($params);
319          }
320
321          if ($rs->isEmpty()) {
322               return;
323          }
324
325          $res =
326          '<div class="lastposts">'.
327          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
328          '<ul>';
329
330          while ($rs->fetch()) {
331               $res .= '<li><a href="'.$rs->getURL().'">'.
332               html::escapeHTML($rs->post_title).'</a></li>';
333          }
334
335          $res .= '</ul></div>';
336
337          return $res;
338     }
339}
340
341class behaviorsMuppet
342{
343     public static function templateBeforeBlock($core,$b,$attr)
344     {
345          // Url->type : default, default-page, category, archive, tag, feed
346          if (($b == 'Entries' || $b == 'Comments' || $b == 'Archives') && !isset($attr['post_type']))
347          {
348               return
349               "<?php\n".
350               'if (!isset($params)) $params=array();'."\n".
351               'toolsmuppet::typesToInclude($core->url->type,$params);'."\n".
352               "?>\n";
353          }
354     }
355
356     public static function publicBeforeSearchCount($s_params)
357     {
358          global $core;
359          $types = muppet::getPostTypes();
360
361          if (!empty($types)) {
362               $post_types = array();
363
364               foreach ($types as $k => $v) {
365                    if ($v['integration'] === true) {
366                         $post_types[] = $k;
367                    }
368               }
369
370               if (count($post_types) > 0) {
371                    if (!isset($s_params['post_type'])) {
372                         $s_params['post_type']=array('post');
373                    }
374                    $s_params['post_type'] = array_merge($s_params['post_type'],$post_types);
375               }
376          }
377     }
378}
379?>
Note: See TracBrowser for help on using the repository browser.

Sites map