Dotclear

source: plugins/splitPost/_public.php @ 1464

Revision 1464, 6.8 KB checked in by Tomtom33, 14 years ago (diff)

spliPost 0.3 :

  • Adds plugin configuration in blog pref panel
  • Adds auto insert post pagination
  • Fixes regex's pattern bug
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of splitPost, a plugin for Dotclear.
4#
5# Copyright (c) 2009 Tomtom
6# http://blog.zenstyle.fr/
7#
8# Licensed under the GPL version 2.0 license.
9# A copy of this license is available in LICENSE file or at
10# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
11# -- END LICENSE BLOCK ------------------------------------
12
13if (!defined('DC_RC_PATH')) { return; }
14
15$core->tpl->addValue('PostPagination',array('splitPostTpl','PostPagination'));
16$core->tpl->addValue('EntryContent',array('splitPostTpl','EntryContent'));
17
18$core->url->register('post','post','^post/(.+)$',array('splitPostUrl','post'));
19
20$core->addBehavior('coreBlogGetPosts',array('splitPostBehaviors','coreBlogGetPosts'));
21
22class splitPostUrl extends dcUrlHandlers
23{
24     public static function post($args)
25     {
26          if ($args == '') {
27               self::p404();
28          }
29         
30          $_ctx = $GLOBALS['_ctx'];
31          $core = $GLOBALS['core'];
32         
33          $core->blog->withoutPassword(false);
34         
35          $args = preg_split('#/page/#',$args);
36         
37          $params = new ArrayObject();
38          $params['post_url'] = $args[0];
39         
40          $_ctx->posts = $core->blog->getPosts($params);
41         
42          # Post pages
43          $_ctx->post_page_count = count(preg_split($core->post_page_pattern,$_ctx->posts->post_content_xhtml));
44          $_ctx->post_page_current = isset($args[1]) ? (int) $args[1] : null;
45         
46          if (
47               $_ctx->post_page_current === 0 ||
48               $_ctx->post_page_current > $_ctx->post_page_count
49          ) {
50               self::p404();
51               exit;
52          }
53         
54          $_ctx->comment_preview = new ArrayObject();
55          $_ctx->comment_preview['content'] = '';
56          $_ctx->comment_preview['rawcontent'] = '';
57          $_ctx->comment_preview['name'] = '';
58          $_ctx->comment_preview['mail'] = '';
59          $_ctx->comment_preview['site'] = '';
60          $_ctx->comment_preview['preview'] = false;
61          $_ctx->comment_preview['remember'] = false;
62         
63          $core->blog->withoutPassword(true);
64         
65         
66          if ($_ctx->posts->isEmpty())
67          { 
68               # No entry
69               self::p404();
70               exit;
71          }
72         
73          $post_id = $_ctx->posts->post_id;
74          $post_password = $_ctx->posts->post_password;
75         
76          # Password protected entry
77          if ($post_password != '')
78          {
79               # Get passwords cookie
80               if (isset($_COOKIE['dc_passwd'])) {
81                    $pwd_cookie = unserialize($_COOKIE['dc_passwd']);
82               } else {
83                    $pwd_cookie = array();
84               }
85               
86               # Check for match
87               if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
88               || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password))
89               {
90                    $pwd_cookie[$post_id] = $post_password;
91                    setcookie('dc_passwd',serialize($pwd_cookie),0,'/');
92               }
93               else
94               {
95                    self::serveDocument('password-form.html','text/html',false);
96                    exit;
97               }
98          }
99         
100          $post_comment =
101               isset($_POST['c_name']) && isset($_POST['c_mail']) &&
102               isset($_POST['c_site']) && isset($_POST['c_content']) &&
103               $_ctx->posts->commentsActive();
104         
105          # Posting a comment
106          if ($post_comment)
107          {
108               # Spam trap
109               if (!empty($_POST['f_mail'])) {
110                    http::head(412,'Precondition Failed');
111                    header('Content-Type: text/plain');
112                    echo "So Long, and Thanks For All the Fish";
113                    exit;
114               }
115               
116               $name = $_POST['c_name'];
117               $mail = $_POST['c_mail'];
118               $site = $_POST['c_site'];
119               $content = $_POST['c_content'];
120               $preview = !empty($_POST['preview']);
121               
122               if ($content != '')
123               {
124                    if ($core->blog->settings->wiki_comments) {
125                         $core->initWikiComment();
126                    } else {
127                         $core->initWikiSimpleComment();
128                    }
129                    $content = $core->wikiTransform($content);
130                    $content = $core->HTMLfilter($content);
131               }
132               
133               $_ctx->comment_preview['content'] = $content;
134               $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
135               $_ctx->comment_preview['name'] = $name;
136               $_ctx->comment_preview['mail'] = $mail;
137               $_ctx->comment_preview['site'] = $site;
138               
139               if ($preview)
140               {
141                    # --BEHAVIOR-- publicBeforeCommentPreview
142                    $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
143                   
144                    $_ctx->comment_preview['preview'] = true;
145               }
146               else
147               {
148                    # Post the comment
149                    $cur = $core->con->openCursor($core->prefix.'comment');
150                    $cur->comment_author = $name;
151                    $cur->comment_site = html::clean($site);
152                    $cur->comment_email = html::clean($mail);
153                    $cur->comment_content = $content;
154                    $cur->post_id = $_ctx->posts->post_id;
155                    $cur->comment_status = $core->blog->settings->comments_pub ? 1 : -1;
156                    $cur->comment_ip = http::realIP();
157                   
158                    $redir = $_ctx->posts->getURL();
159                    $redir .= strpos($redir,'?') !== false ? '&' : '?';
160                   
161                    try
162                    {
163                         if (!text::isEmail($cur->comment_email)) {
164                              throw new Exception(__('You must provide a valid email address.'));
165                         }
166
167                         # --BEHAVIOR-- publicBeforeCommentCreate
168                         $core->callBehavior('publicBeforeCommentCreate',$cur);
169                         if ($cur->post_id) {                         
170                              $comment_id = $core->blog->addComment($cur);
171                         
172                              # --BEHAVIOR-- publicAfterCommentCreate
173                              $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
174                         }
175                         
176                         if ($cur->comment_status == 1) {
177                              $redir_arg = 'pub=1';
178                         } else {
179                              $redir_arg = 'pub=0';
180                         }
181                         
182                         header('Location: '.$redir.$redir_arg);
183                         exit;
184                    }
185                    catch (Exception $e)
186                    {
187                         $_ctx->form_error = $e->getMessage();
188                         $_ctx->form_error;
189                    }
190               }
191          }
192         
193          # The entry
194          self::serveDocument('post.html');
195     }
196}
197
198class splitPostTpl
199{
200     public static function EntryContent($attr)
201     {
202          $urls = '0';
203          if (!empty($attr['absolute_urls'])) {
204               $urls = '1';
205          }
206         
207          $f = $GLOBALS['core']->tpl->getFilters($attr);
208         
209          $res = '';
210         
211         
212          if (!empty($attr['full'])) {
213               $res = '<?php echo '.sprintf($f,
214                    '$_ctx->posts->getExcerpt('.$urls.')." ".$_ctx->posts->getContent('.$urls.')').'; ?>';
215          } else {
216               $res = '<?php echo '.sprintf($f,'$_ctx->posts->getContent('.$urls.')').'; ?>';
217          }
218         
219          $res .= "<?php if (\$core->blog->settings->splitpost_auto_insert) : ?>\n";
220          $res .= splitPostTpl::PostPagination($attr);
221          $res .= "<?php endif; ?>\n";
222
223          return $res;
224     }
225     
226     public static function PostPagination($attr)
227     {
228          $f = $GLOBALS['core']->tpl->getFilters($attr);
229         
230          $max = isset($attr['max']) ? (int) $attr['max'] : 20;
231         
232          $p = "\$params = array();\n";
233          if (isset($attr['current'])) {
234               $p .= "\$params['current'] = '".$attr['current']."';\n";
235          }
236          if (isset($attr['prev'])) {
237               $p .= "\$params['prev'] = '".$attr['prev']."';\n";
238          }
239          if (isset($attr['next'])) {
240               $p .= "\$params['next'] = '".$attr['next']."';\n";
241          }
242         
243         
244          $res = "<?php\n";
245          $res .= $p;
246          $res .= "\$pager = new splitPostPager(\$_ctx->post_page_current,\$_ctx->post_page_count,".$max.");\n";
247          $res .= "\$pager->init(\$params);\n";
248          $res .= "echo ".sprintf($f,'$pager->getLinks()').";\n";
249          $res .= "?>\n";
250         
251          return $res;
252     }
253}
254
255?>
Note: See TracBrowser for help on using the repository browser.

Sites map