Dotclear

source: plugins/private/_public.php @ 2613

Revision 2613, 6.3 KB checked in by JcDenis, 13 years ago (diff)

Private mode : version 1.5 - Cleaner release

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('PrivateReqPage',array('tplPrivate','PrivateReqPage'));
17$core->tpl->addValue('PrivateMsg',array('tplPrivate','PrivateMsg'));
18
19$s = $core->blog->settings->private;
20         
21if ($s->private_flag) {
22     $core->addBehavior('publicBeforeDocument',array('urlPrivate','privateHandler'));
23}
24
25if ($s->private_conauto_flag) {
26     $core->addBehavior('publicPrivateFormAfterContent',array('behaviorsPrivate','publicPrivateFormAfterContent'));
27}
28
29$core->addBehavior('publicPrivateFormBeforeContent',array('behaviorsPrivate','publicPrivateFormBeforeContent'));
30
31/**
32*
33*/
34class urlPrivate extends dcUrlHandlers
35{
36     /**
37     *
38     */
39     public static function feedXslt($args)
40     {
41          self::serveDocument('rss2.xsl','text/xml');
42     }   
43     
44     /**
45     *
46     */
47     public static function publicFeed($args)
48     {
49          #Don't reinvent the wheel - take a look to dcUrlHandlers/feed
50          global $core,$_ctx;
51
52          $type = null;
53          $params = array();
54          $mime = 'application/xml';
55         
56          if (preg_match('#^(atom|rss2)$#',$args,$m)) {
57               # Atom or RSS2 ?
58               $type = $m[0];
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->system->robots_policy,''));
69          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
70          self::serveDocument($tpl,$mime);
71         
72          return;
73     }
74
75     /**
76     *
77     */
78     public static function callbackfoo($args)
79     {
80          #Woohoo :)
81          return;
82     }
83
84     /**
85     *
86     */
87     public static function privateHandler($args)
88     {
89          global $core,$_ctx;
90
91          #New temporary urlHandlers
92          $urlp = new urlHandler();
93          $urlp->mode = $core->url->mode;
94          $urlp->registerDefault(array('urlPrivate','callbackfoo'));
95          foreach ($core->url->getTypes() as $k => $v) {
96               $urlp->register($k,$v['url'],$v['representation'],array('urlPrivate','callbackfoo'));
97          }
98         
99          #Find type
100          $urlp->getDocument();
101          $type = $urlp->type;
102          unset($urlp);
103         
104          #Looking for a new template (private.html)
105          $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates');
106         
107          #Load password from configuration
108          $password = $core->blog->settings->private->blog_private_pwd;
109         
110          #Define allowed url->type
111          $allowed_types = new ArrayObject(array('feed','xslt','tag_feed','pubfeed','spamfeed','hamfeed','trackback','preview','pagespreview','contactme'));
112          $core->callBehavior('initPrivateMode',$allowed_types);
113
114          #Generic behavior
115          $core->callBehavior('initPrivateHandler',$core);
116         
117          #Let's go : define a new session and start it
118          if (!isset($session)) {
119               $session = new sessionDB(
120                    $core->con,
121                    $core->prefix.'session',
122                    'dc_privateblog_sess_'.$core->blog->id,
123                    '/'
124               );
125               $session->start();
126          }
127
128          if (in_array($type,(array)$allowed_types)) {
129               return;
130          } else {
131               #Add cookie test (automatic login)
132               $cookiepass = "dc_privateblog_cookie_".$core->blog->id;
133               
134               if (!empty($_COOKIE[$cookiepass])) {
135                    $cookiepassvalue = (($_COOKIE[$cookiepass]) == $password);
136               } 
137               else {
138                    $cookiepassvalue = false;
139               }
140               
141               #Let's rumble session, cookies & conf :)
142               if (!isset($_SESSION['sess_blog_private']) || $_SESSION['sess_blog_private'] == "") {
143                    if ($cookiepassvalue != false) {
144                         $_SESSION['sess_blog_private'] = $_COOKIE[$cookiepass];
145                    }
146                    if (!empty($_POST['private_pass'])) {
147                         if (md5($_POST['private_pass']) == $password) {
148                              $_SESSION['sess_blog_private'] = md5($_POST['private_pass']);
149                             
150                              if (!empty($_POST['pass_remember'])) {
151                                   setcookie($cookiepass,md5($_POST['private_pass']),time() + 31536000,'/');
152                              }
153                              return;
154                         }
155                         $_ctx->form_error = __('Wrong password');
156                    }
157                    $session->destroy();
158                    self::serveDocument('private.html','text/html',false);
159                    # --BEHAVIOR-- publicAfterDocument
160                    $core->callBehavior('publicAfterDocument',$core);
161                    exit;
162               }
163               elseif ($_SESSION['sess_blog_private'] != $password) {
164                    $session->destroy();
165                    self::serveDocument('private.html','text/html',false);
166                    # --BEHAVIOR-- publicAfterDocument
167                    $core->callBehavior('publicAfterDocument',$core);
168                    exit;
169               }
170               elseif (isset($_POST['blogout'])) {
171                    $session->destroy();
172                    setcookie($cookiepass,'ciao',time() - 86400,'/');
173                    $_ctx->form_error = __('You are now disconnected.');
174                    self::serveDocument('private.html','text/html',false);
175                    # --BEHAVIOR-- publicAfterDocument
176                    $core->callBehavior('publicAfterDocument',$core);
177                    exit;
178               }
179               return;
180          }
181     }
182}
183
184/**
185*
186*/
187class behaviorsPrivate
188{
189     /**
190     *
191     */
192     public static function publicPrivateFormBeforeContent($core)
193     {
194          echo $core->blog->settings->private->message;
195     }
196     
197     /**
198     *
199     */
200     public static function publicPrivateFormAfterContent($core)
201     {
202          echo '<p><label class="classic">'.
203               form::checkbox(array('pass_remember'),1).' '.
204               __('Enable automatic connection').'</label></p>';
205     }
206}
207
208/**
209*
210*/
211class tplPrivate
212{
213     /**
214     *
215     */
216     public static function PrivateMsg($attr)
217     {
218          $f = $GLOBALS['core']->tpl->getFilters($attr);
219          return '<?php echo '.sprintf($f,'$GLOBALS[\'core\']->blog->settings->private->message').'; ?>';
220     }
221
222     /**
223     *
224     */
225     public static function PrivateReqPage($attr)
226     {
227          return  '<?php echo(isset($_SERVER[\'REQUEST_URI\'])
228               ? html::escapeHTML($_SERVER[\'REQUEST_URI\'])
229               : $core->blog->url); ?>' ;
230     }
231}
232
233/**
234*
235*/
236class widgetsPrivage
237{
238     /**
239     *
240     */
241     public static function widgetLogout($w) 
242     {
243          if ($GLOBALS['core']->blog->settings->private->private_flag)
244          {
245               if ($w->homeonly && $core->url->type != 'default') {
246                    return;
247               }
248
249               $res = '<div class="blogout">'.
250                    ($w->title ? '<h2>'.html::escapeHTML($w->title).'</h2>' : '').
251                    ($w->text ? $w->text : '').
252                    '<form action="'.$GLOBALS['core']->blog->url.'" method="post">'.
253                    '<p class="buttons">'.
254                    '<input type="hidden" name="blogout" id="blogout" value="" />'.
255                    '<input type="submit" value="'.html::escapeHTML($w->label).'" class="logout" /></p>'.
256                    '</form></div>';
257               return $res;
258          }
259     }
260}
261?>
Note: See TracBrowser for help on using the repository browser.

Sites map