Dotclear

source: plugins/pages/_public.php @ 2778:fe7e025273c4

Revision 2778:fe7e025273c4, 7.8 KB checked in by franck <carnet.franck.paul@…>, 11 years ago (diff)

Add "offline" option on widgets, closes #1945

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
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'];
32
33               $core->blog->withoutPassword(false);
34
35               $params = new ArrayObject(array(
36                    'post_type' => 'page',
37                    'post_url' => $args));
38
39               $core->callBehavior('publicPagesBeforeGetPosts',$params,$args);
40
41               $_ctx->posts = $core->blog->getPosts($params);
42
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;
51
52               $core->blog->withoutPassword(true);
53
54
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;
64
65                    # Password protected entry
66                    if ($post_password != '' && !$_ctx->preview)
67                    {
68                         # Get passwords cookie
69                         if (isset($_COOKIE['dc_passwd'])) {
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                              }
76                         } else {
77                              $pwd_cookie = array();
78                         }
79
80                         # Check for match
81                         # Note: We must prefix post_id key with '#'' in pwd_cookie array in order to avoid integer conversion
82                         # because MyArray["12345"] is treated as MyArray[12345]
83                         if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
84                         || (isset($pwd_cookie['#'.$post_id]) && $pwd_cookie['#'.$post_id] == $post_password))
85                         {
86                              $pwd_cookie['#'.$post_id] = $post_password;
87                              setcookie('dc_passwd',json_encode($pwd_cookie),0,'/');
88                         }
89                         else
90                         {
91                              self::serveDocument('password-form.html','text/html',false);
92                              return;
93                         }
94                    }
95
96                    $post_comment =
97                         isset($_POST['c_name']) && isset($_POST['c_mail']) &&
98                         isset($_POST['c_site']) && isset($_POST['c_content']) &&
99                         $_ctx->posts->commentsActive();
100
101                    # Posting a comment
102                    if ($post_comment)
103                    {
104                         # Spam trap
105                         if (!empty($_POST['f_mail'])) {
106                              http::head(412,'Precondition Failed');
107                              header('Content-Type: text/plain');
108                              echo "So Long, and Thanks For All the Fish";
109                              # Exits immediately the application to preserve the server.
110                              exit;
111                         }
112
113                         $name = $_POST['c_name'];
114                         $mail = $_POST['c_mail'];
115                         $site = $_POST['c_site'];
116                         $content = $_POST['c_content'];
117                         $preview = !empty($_POST['preview']);
118
119                         if ($content != '')
120                         {
121                              # --BEHAVIOR-- publicBeforeCommentTransform
122                              $buffer = $core->callBehavior('publicBeforeCommentTransform',$content);
123                              if ($buffer != '') {
124                                   $content = $buffer;
125                              } else {
126                                   if ($core->blog->settings->system->wiki_comments) {
127                                        $core->initWikiComment();
128                                   } else {
129                                        $core->initWikiSimpleComment();
130                                   }
131                                   $content = $core->wikiTransform($content);
132                              }
133                              $content = $core->HTMLfilter($content);
134                         }
135
136                         $_ctx->comment_preview['content'] = $content;
137                         $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
138                         $_ctx->comment_preview['name'] = $name;
139                         $_ctx->comment_preview['mail'] = $mail;
140                         $_ctx->comment_preview['site'] = $site;
141
142                         if ($preview)
143                         {
144                              # --BEHAVIOR-- publicBeforeCommentPreview
145                              $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
146
147                              $_ctx->comment_preview['preview'] = true;
148                         }
149                         else
150                         {
151                              # Post the comment
152                              $cur = $core->con->openCursor($core->prefix.'comment');
153                              $cur->comment_author = $name;
154                              $cur->comment_site = html::clean($site);
155                              $cur->comment_email = html::clean($mail);
156                              $cur->comment_content = $content;
157                              $cur->post_id = $_ctx->posts->post_id;
158                              $cur->comment_status = $core->blog->settings->system->comments_pub ? 1 : -1;
159                              $cur->comment_ip = http::realIP();
160
161                              $redir = $_ctx->posts->getURL();
162                              $redir .= $core->blog->settings->system->url_scan == 'query_string' ? '&' : '?';
163
164                              try
165                              {
166                                   if (!text::isEmail($cur->comment_email)) {
167                                        throw new Exception(__('You must provide a valid email address.'));
168                                   }
169
170                                   # --BEHAVIOR-- publicBeforeCommentCreate
171                                   $core->callBehavior('publicBeforeCommentCreate',$cur);
172                                   if ($cur->post_id) {
173                                        $comment_id = $core->blog->addComment($cur);
174
175                                        # --BEHAVIOR-- publicAfterCommentCreate
176                                        $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
177                                   }
178
179                                   if ($cur->comment_status == 1) {
180                                        $redir_arg = 'pub=1';
181                                   } else {
182                                        $redir_arg = 'pub=0';
183                                   }
184
185                                   header('Location: '.$redir.$redir_arg);
186                              }
187                              catch (Exception $e)
188                              {
189                                   $_ctx->form_error = $e->getMessage();
190                                   $_ctx->form_error;
191                              }
192                         }
193                    }
194
195                    # The entry
196                    if ($_ctx->posts->trackbacksActive()) {
197                         header('X-Pingback: '.$core->blog->url.$core->url->getURLFor("xmlrpc",$core->blog->id));
198                    }
199
200                    $tplset = $core->themes->moduleInfo($core->blog->settings->system->theme,'tplset');
201                    if (!empty($tplset) && is_dir(dirname(__FILE__).'/default-templates/'.$tplset)) {
202                         $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates/'.$tplset);
203                    } else {
204                         $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates/'.DC_DEFAULT_TPLSET);
205                    }
206                    self::serveDocument('page.html');
207               }
208          }
209     }
210
211     public static function pagespreview($args)
212     {
213          $core = $GLOBALS['core'];
214          $_ctx = $GLOBALS['_ctx'];
215
216          if (!preg_match('#^(.+?)/([0-9a-z]{40})/(.+?)$#',$args,$m)) {
217               # The specified Preview URL is malformed.
218               self::p404();
219          }
220          else
221          {
222               $user_id = $m[1];
223               $user_key = $m[2];
224               $post_url = $m[3];
225               if (!$core->auth->checkUser($user_id,null,$user_key)) {
226                    # The user has no access to the entry.
227                    self::p404();
228               }
229               else
230               {
231                    $_ctx->preview = true;
232                    self::pages($post_url);
233               }
234          }
235     }
236}
237
238class tplPages
239{
240     # Widget function
241     public static function pagesWidget($w)
242     {
243          global $core, $_ctx;
244
245          if ($w->offline)
246               return;
247
248          if (($w->homeonly == 1 && $core->url->type != 'default') ||
249               ($w->homeonly == 2 && $core->url->type == 'default')) {
250               return;
251          }
252
253          $params['post_type'] = 'page';
254          $params['limit'] = abs((integer) $w->limit);
255          $params['no_content'] = true;
256          $params['post_selected'] = false;
257
258          $sort = $w->sortby;
259          if (!in_array($sort,array('post_title','post_position','post_dt'))) {
260               $sort = 'post_title';
261          }
262
263          $order = $w->orderby;
264          if ($order != 'asc') {
265               $order = 'desc';
266          }
267          $params['order'] = $sort.' '.$order;
268
269          $rs = $core->blog->getPosts($params);
270
271          if ($rs->isEmpty()) {
272               return;
273          }
274
275          $res = ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '').'<ul>';
276
277          while ($rs->fetch()) {
278               $class = '';
279               if (($core->url->type == 'pages' && $_ctx->posts instanceof record && $_ctx->posts->post_id == $rs->post_id)) {
280                    $class = ' class="page-current"';
281               }
282               $res .= '<li'.$class.'><a href="'.$rs->getURL().'">'.
283               html::escapeHTML($rs->post_title).'</a></li>';
284          }
285
286          $res .= '</ul>';
287
288          return $w->renderDiv($w->content_only,'pages '.$w->class,'',$res);
289     }
290}
Note: See TracBrowser for help on using the repository browser.

Sites map