Dotclear

source: plugins/pages/_public.php @ 2634:d60876d7e4e4

Revision 2634:d60876d7e4e4, 7.1 KB checked in by Dsls, 12 years ago (diff)

Use JSON instead of serialization, see #1249

RevLine 
[0]1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
[1179]6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
[0]7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14# Localized string we find in template
15__('Published on');
16__('This page\'s comments feed');
17
18require dirname(__FILE__).'/_widgets.php';
19
20class urlPages extends dcUrlHandlers
21{
22     public static function pages($args)
23     {
24          if ($args == '') {
25               # No page was specified.
26               self::p404();
27          }
28          else
29          {
30               $_ctx =& $GLOBALS['_ctx'];
31               $core =& $GLOBALS['core'];
[2566]32
[0]33               $core->blog->withoutPassword(false);
[2566]34
[306]35               $params = new ArrayObject(array(
36                    'post_type' => 'page',
37                    'post_url' => $args));
[2566]38
[306]39               $core->callBehavior('publicPagesBeforeGetPosts',$params,$args);
[2566]40
[0]41               $_ctx->posts = $core->blog->getPosts($params);
[2566]42
[0]43               $_ctx->comment_preview = new ArrayObject();
44               $_ctx->comment_preview['content'] = '';
45               $_ctx->comment_preview['rawcontent'] = '';
46               $_ctx->comment_preview['name'] = '';
47               $_ctx->comment_preview['mail'] = '';
48               $_ctx->comment_preview['site'] = '';
49               $_ctx->comment_preview['preview'] = false;
50               $_ctx->comment_preview['remember'] = false;
[2566]51
[0]52               $core->blog->withoutPassword(true);
[2566]53
54
[0]55               if ($_ctx->posts->isEmpty())
56               {
57                    # The specified page does not exist.
58                    self::p404();
59               }
60               else
61               {
62                    $post_id = $_ctx->posts->post_id;
63                    $post_password = $_ctx->posts->post_password;
[2566]64
[0]65                    # Password protected entry
66                    if ($post_password != '' && !$_ctx->preview)
67                    {
68                         # Get passwords cookie
69                         if (isset($_COOKIE['dc_passwd'])) {
[2634]70                              $pwd_cookie = json_decode($_COOKIE['dc_passwd']);
71                              if ($pwd_cookie === NULL) {
72                                   $pwd_cookie = array();
73                              } else {
74                                   $pwd_cookie = (array) $pwd_cookie;
75                              }
[0]76                         } else {
77                              $pwd_cookie = array();
78                         }
[2566]79
[0]80                         # Check for match
81                         if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
82                         || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password))
83                         {
84                              $pwd_cookie[$post_id] = $post_password;
[2634]85                              setcookie('dc_passwd',json_encode($pwd_cookie),0,'/');
[0]86                         }
87                         else
88                         {
89                              self::serveDocument('password-form.html','text/html',false);
90                              return;
91                         }
92                    }
[2566]93
[0]94                    $post_comment =
95                         isset($_POST['c_name']) && isset($_POST['c_mail']) &&
96                         isset($_POST['c_site']) && isset($_POST['c_content']) &&
97                         $_ctx->posts->commentsActive();
[2566]98
[0]99                    # Posting a comment
100                    if ($post_comment)
101                    {
102                         # Spam trap
103                         if (!empty($_POST['f_mail'])) {
104                              http::head(412,'Precondition Failed');
105                              header('Content-Type: text/plain');
106                              echo "So Long, and Thanks For All the Fish";
107                              # Exits immediately the application to preserve the server.
108                              exit;
109                         }
[2566]110
[0]111                         $name = $_POST['c_name'];
112                         $mail = $_POST['c_mail'];
113                         $site = $_POST['c_site'];
114                         $content = $_POST['c_content'];
115                         $preview = !empty($_POST['preview']);
[2566]116
[0]117                         if ($content != '')
118                         {
119                              if ($core->blog->settings->system->wiki_comments) {
120                                   $core->initWikiComment();
121                              } else {
122                                   $core->initWikiSimpleComment();
123                              }
124                              $content = $core->wikiTransform($content);
125                              $content = $core->HTMLfilter($content);
126                         }
[2566]127
[0]128                         $_ctx->comment_preview['content'] = $content;
129                         $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
130                         $_ctx->comment_preview['name'] = $name;
131                         $_ctx->comment_preview['mail'] = $mail;
132                         $_ctx->comment_preview['site'] = $site;
[2566]133
[0]134                         if ($preview)
135                         {
136                              # --BEHAVIOR-- publicBeforeCommentPreview
137                              $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
[2566]138
[0]139                              $_ctx->comment_preview['preview'] = true;
140                         }
141                         else
142                         {
143                              # Post the comment
144                              $cur = $core->con->openCursor($core->prefix.'comment');
145                              $cur->comment_author = $name;
146                              $cur->comment_site = html::clean($site);
147                              $cur->comment_email = html::clean($mail);
148                              $cur->comment_content = $content;
149                              $cur->post_id = $_ctx->posts->post_id;
150                              $cur->comment_status = $core->blog->settings->system->comments_pub ? 1 : -1;
151                              $cur->comment_ip = http::realIP();
[2566]152
[0]153                              $redir = $_ctx->posts->getURL();
[727]154                              $redir .= $core->blog->settings->system->url_scan == 'query_string' ? '&' : '?';
[2566]155
[0]156                              try
157                              {
158                                   if (!text::isEmail($cur->comment_email)) {
159                                        throw new Exception(__('You must provide a valid email address.'));
160                                   }
[2566]161
[0]162                                   # --BEHAVIOR-- publicBeforeCommentCreate
163                                   $core->callBehavior('publicBeforeCommentCreate',$cur);
[2566]164                                   if ($cur->post_id) {
[0]165                                        $comment_id = $core->blog->addComment($cur);
[2566]166
[0]167                                        # --BEHAVIOR-- publicAfterCommentCreate
168                                        $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
169                                   }
[2566]170
[0]171                                   if ($cur->comment_status == 1) {
172                                        $redir_arg = 'pub=1';
173                                   } else {
174                                        $redir_arg = 'pub=0';
175                                   }
[2566]176
[0]177                                   header('Location: '.$redir.$redir_arg);
178                              }
179                              catch (Exception $e)
180                              {
181                                   $_ctx->form_error = $e->getMessage();
182                                   $_ctx->form_error;
183                              }
184                         }
185                    }
[2566]186
[0]187                    # The entry
[1674]188                    if ($_ctx->posts->trackbacksActive()) {
189                         header('X-Pingback: '.$core->blog->url.$core->url->getURLFor("xmlrpc",$core->blog->id));
190                    }
[0]191                    $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
192                    self::serveDocument('page.html');
193               }
194          }
195     }
[2566]196
[0]197     public static function pagespreview($args)
198     {
199          $core = $GLOBALS['core'];
200          $_ctx = $GLOBALS['_ctx'];
[2566]201
[0]202          if (!preg_match('#^(.+?)/([0-9a-z]{40})/(.+?)$#',$args,$m)) {
203               # The specified Preview URL is malformed.
204               self::p404();
205          }
206          else
207          {
208               $user_id = $m[1];
209               $user_key = $m[2];
210               $post_url = $m[3];
211               if (!$core->auth->checkUser($user_id,null,$user_key)) {
212                    # The user has no access to the entry.
213                    self::p404();
214               }
215               else
216               {
217                    $_ctx->preview = true;
218                    self::pages($post_url);
219               }
220          }
221     }
222}
223
224class tplPages
225{
226     # Widget function
227     public static function pagesWidget($w)
228     {
229          global $core, $_ctx;
[2566]230
[945]231          if (($w->homeonly == 1 && $core->url->type != 'default') ||
232               ($w->homeonly == 2 && $core->url->type == 'default')) {
[0]233               return;
234          }
[2566]235
[0]236          $params['post_type'] = 'page';
237          $params['limit'] = abs((integer) $w->limit);
238          $params['no_content'] = true;
[911]239          $params['post_selected'] = false;
[2566]240
[0]241          $sort = $w->sortby;
242          if (!in_array($sort,array('post_title','post_position','post_dt'))) {
243               $sort = 'post_title';
244          }
[2566]245
[0]246          $order = $w->orderby;
247          if ($order != 'asc') {
248               $order = 'desc';
249          }
250          $params['order'] = $sort.' '.$order;
[2566]251
[0]252          $rs = $core->blog->getPosts($params);
[2566]253
[0]254          if ($rs->isEmpty()) {
255               return;
256          }
[2566]257
[0]258          $res =
[1063]259          ($w->content_only ? '' : '<div class="pages'.($w->class ? ' '.html::escapeHTML($w->class) : '').'">').
[0]260          ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
261          '<ul>';
[2566]262
[0]263          while ($rs->fetch()) {
264               $class = '';
265               if (($core->url->type == 'pages' && $_ctx->posts instanceof record && $_ctx->posts->post_id == $rs->post_id)) {
266                    $class = ' class="page-current"';
267               }
268               $res .= '<li'.$class.'><a href="'.$rs->getURL().'">'.
269               html::escapeHTML($rs->post_title).'</a></li>';
270          }
[2566]271
[1063]272          $res .= '</ul>'.($w->content_only ? '' : '</div>');
[2566]273
[0]274          return $res;
275     }
276}
Note: See TracBrowser for help on using the repository browser.

Sites map