Dotclear

source: plugins/private/_public.php @ 2475

Revision 2475, 6.3 KB checked in by Osku, 13 years ago (diff)

private mode : update to last current version

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of Private mode, a plugin for Dotclear 2.
5#
6# Copyright (c) 2008-2010 Osku and contributors
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#
12# -- END LICENSE BLOCK ------------------------------------
13
14if (!defined('DC_RC_PATH')) { return; }
15
16$core->tpl->addValue('PrivatePageTitle',array('tplPrivate','PrivatePageTitle'));
17$core->tpl->addValue('PrivateMsg',array('tplPrivate','PrivateMsg'));
18$core->tpl->addValue('PrivateReqPage',array('tplPrivate','PrivateReqPage'));
19$core->tpl->addBlock('IfPrivateMsgError',array('tplPrivate','IfPrivateMsgError'));
20$core->tpl->addValue('PrivatePassRemember',array('tplPrivate','PrivatePassRemember'));
21$core->tpl->addValue('PrivateMsgError',array('tplPrivate','PrivateMsgError'));
22
23$s = privateSettings($core);
24         
25if ($s->private_flag)
26{
27     $core->addBehavior('publicBeforeDocument',array('urlPrivate','privateHandler'));
28}
29
30class urlPrivate extends dcUrlHandlers
31{
32     public static function privateFeed($args)
33     {
34          self::feed($args);
35     }
36     
37     public static function publicFeed($args)
38     {
39          global $core,$_ctx;
40         
41          $type = null;
42          $params = array();
43          $mime = 'application/xml';
44         
45          if (preg_match('#^rss2/xslt$#',$args,$m))
46          {
47               # RSS XSLT stylesheet
48               self::serveDocument('rss2.xsl','text/xml');
49          }
50          elseif (preg_match('#^(atom|rss2)/comments/([0-9]+)$#',$args,$m))
51          {
52               # Post comments feed
53               $type = $m[1];
54          }
55          elseif (preg_match('#^(?:category/(.+)/)?(atom|rss2)(/comments)?$#',$args,$m))
56          {
57               # All posts or comments feed
58               $type = $m[2];
59          }
60         
61          $tpl =  $type == '' ? 'atom' : $type;
62          $tpl .= '-pv.xml';
63         
64          if ($type == 'atom') {
65               $mime = 'application/atom+xml';
66          }
67         
68          header('X-Robots-Tag: '.context::robotsPolicy($core->blog->settings->robots_policy,''));
69          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
70          self::serveDocument($tpl,$mime);
71         
72          return;
73     }
74
75     public static function callbackfoo($args)
76     {
77          return;
78     }
79
80     public static function privateHandler($args)
81     {
82          global $core,$_ctx;
83
84          $urlp = new urlHandler();
85          $urlp->mode = $core->url->mode;
86          $urlp->registerDefault(array('urlPrivate','callbackfoo'));
87          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
88          $s = privateSettings($core);
89          $password = $s->blog_private_pwd;
90          $allowed_types = array('feed','pubfeed','spamfeed','hamfeed','trackback','preview','pagespreview','contactme');
91
92          #agora trick ?
93          if ($core->auth->userID()) 
94          {
95               return;
96          }
97         
98          if (!isset($session))
99          {
100               $session = new sessionDB(
101                    $core->con,
102                    $core->prefix.'session',
103                    'dc_privateblog_sess_'.$core->blog->id,
104                    '/'
105               );
106               $session->start();
107          }
108
109          foreach ($core->url->getTypes() as $k=>$v)
110          {
111               $urlp->register($k,$v['url'],$v['representation'],array('urlPrivate','callbackfoo'));
112          }
113
114          $urlp->getDocument();
115          $type = $urlp->type;
116          unset($urlp);
117
118          if (in_array($type,$allowed_types)) 
119          {
120               return;
121          }
122          else
123          {
124               // Add cookie test (automatic login)
125               $cookiepass = "dc_privateblog_cookie_".$core->blog->id;
126               
127               if (!empty($_COOKIE[$cookiepass])) 
128               {
129                    $cookiepassvalue = (($_COOKIE[$cookiepass]) == $password);
130               } 
131               else 
132               {
133                    $cookiepassvalue = false;
134               }
135               
136               if (!isset($_SESSION['sess_blog_private']) || $_SESSION['sess_blog_private'] == "")
137               {
138                    if ($cookiepassvalue != false) 
139                    {
140                         $_SESSION['sess_blog_private'] = $_COOKIE[$cookiepass];
141                         return;
142                    }
143                    if (!empty($_POST['private_pass'])) 
144                    {
145                         if (md5($_POST['private_pass']) == $password)
146                         {
147                              $_SESSION['sess_blog_private'] = md5($_POST['private_pass']);
148                             
149                              if (!empty($_POST['pass_remember'])) 
150                              {
151                                   setcookie($cookiepass,md5($_POST['private_pass']),time() + 31536000,'/');
152                              }
153                              return;
154                         }
155                         $_ctx->blogpass_error = __('Wrong password');
156                    }
157                    $session->destroy();
158                    self::serveDocument('private.html','text/html',false);
159                    exit;
160               }
161               elseif ($_SESSION['sess_blog_private'] != $password)
162               {
163                    $session->destroy();
164                    $_ctx->blogpass_error = __('Wrong password');
165                    self::serveDocument('private.html','text/html',false);
166                    exit;
167               }
168               elseif (isset($_POST['blogout']))
169               {
170                    $session->destroy();
171                    setcookie($cookiepass,'ciao',time() - 86400,'/');
172                    $_ctx->blogpass_error = __('Disconnected');
173                    self::serveDocument('private.html','text/html',false);
174                    exit;
175               }
176               return;
177          }
178     }
179}
180
181class tplPrivate
182{
183     public static function PrivatePageTitle($attr)
184     {
185          global $core;
186          $f = $core->tpl->getFilters($attr);
187          return '<?php echo '.sprintf($f,'privateSettings($core)->private_page_title').'; ?>';
188     }
189
190     public static function PrivateMsg($attr)
191     {
192          global $core;
193          $f = $core->tpl->getFilters($attr);
194          return '<?php echo '.sprintf($f,'privateSettings($core)->private_page_message').'; ?>';
195     }
196
197     public static function PrivateReqPage($attr)
198     {
199          return 
200          '<?php echo(isset($_SERVER[\'REQUEST_URI\'])
201          ? html::escapeHTML($_SERVER[\'REQUEST_URI\'])
202          : $core->blog->url); ?>' ;
203     }
204
205     public static function IfPrivateMsgError($attr,$content)
206     {
207          return
208          '<?php if ($_ctx->blogpass_error !== null) : ?>'.
209          $content.
210          '<?php endif; ?>';
211     }
212
213     public static function PrivateMsgError($attr)
214     {
215          return 
216          '<?php if ($_ctx->blogpass_error !== null)
217          { echo $_ctx->blogpass_error; } ?>';
218     }
219
220     public static function PrivatePassRemember($attr)
221     {
222          global $core;
223          $s = privateSettings($core);
224          if ($s->private_conauto_flag)
225          {
226               $res = '<p><label class="classic">'.
227                    form::checkbox(array('pass_remember'),1).' '.
228                    __('Enable automatic connection').'</label></p>';
229               return $res;
230          }
231          else
232          {
233               return;
234          }
235     }
236}
237
238class widgetsPrivage
239{
240     public static function widgetLogout($w) 
241     {
242          global $core;
243          $s = privateSettings($core);
244
245          if ($w->homeonly && $core->url->type != 'default') {
246               return;
247          }
248
249          if ($s->private_flag)
250          {
251               $res = '<div class="blogout">'.
252                    ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
253                    ($w->text ? $w->text : '').
254                    '<form action="'.$core->blog->url.'" method="post">'.
255                    '<p class="buttons">'.
256                    '<input type="hidden" name="blogout" id="blogout" value="" />'.
257                    '<input type="submit" value="'.html::escapeHTML($w->label).'" class="logout" /></p>'.
258                    '</form></div>';
259               return $res;
260          }
261          else
262          {
263               return;
264          }
265     }
266}
267?>
Note: See TracBrowser for help on using the repository browser.

Sites map