1 | <?php |
---|
2 | # ***** BEGIN LICENSE BLOCK ***** |
---|
3 | # This file is part of DotClear Preview plugin. |
---|
4 | # Copyright (c) 2008 Bruno Hondelatte, and contributors. |
---|
5 | # Many, many thanks to Olivier Meunier and the Dotclear Team. |
---|
6 | # All rights reserved. |
---|
7 | # |
---|
8 | # Preview plugin for DC2 is free sofwtare; you can redistribute it and/or modify |
---|
9 | # it under the terms of the GNU General Public License as published by |
---|
10 | # the Free Software Foundation; either version 2 of the License, or |
---|
11 | # (at your option) any later version. |
---|
12 | # |
---|
13 | # DotClear is distributed in the hope that it will be useful, |
---|
14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | # GNU General Public License for more details. |
---|
17 | # |
---|
18 | # You should have received a copy of the GNU General Public License |
---|
19 | # along with DotClear; if not, write to the Free Software |
---|
20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | # |
---|
22 | # ***** END LICENSE BLOCK ***** |
---|
23 | if (!defined('DC_RC_PATH')) { return; } |
---|
24 | |
---|
25 | require dirname(__FILE__).'/_widgets.php'; |
---|
26 | $core->addBehavior('publicPrepend',array('PreviewBehavior','publicPrepend')); |
---|
27 | |
---|
28 | $core->tpl->addValue('EntryIfOffline',array('tplPreview','EntryIfOffline')); |
---|
29 | |
---|
30 | class urlPreview extends dcUrlHandlers |
---|
31 | { |
---|
32 | public static function post($args) |
---|
33 | { |
---|
34 | if ($args == '') { |
---|
35 | self::p404(); |
---|
36 | } |
---|
37 | |
---|
38 | $_ctx =& $GLOBALS['_ctx']; |
---|
39 | $core =& $GLOBALS['core']; |
---|
40 | |
---|
41 | if (isset($_COOKIE[DC_SESSION_NAME])) |
---|
42 | { |
---|
43 | $core->session->start(); |
---|
44 | $core->auth->checkUser($_SESSION['sess_user_id']); |
---|
45 | } |
---|
46 | |
---|
47 | $core->blog->withoutPassword(false); |
---|
48 | |
---|
49 | $params = new ArrayObject(); |
---|
50 | $params['post_url'] = $args; |
---|
51 | |
---|
52 | $_ctx->posts = $core->blog->getPosts($params); |
---|
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 | } |
---|
71 | |
---|
72 | # The entry |
---|
73 | self::serveDocument('post.html'); |
---|
74 | exit; |
---|
75 | } |
---|
76 | |
---|
77 | public static function login($args) |
---|
78 | { |
---|
79 | global $core; |
---|
80 | if (!isset($_POST['user_id']) || !isset($_POST['user_pwd'])) { |
---|
81 | header('Location: '.$core->blog->url); |
---|
82 | return; |
---|
83 | } |
---|
84 | $user_id=$_POST['user_id']; |
---|
85 | $user_pwd = $_POST['user_pwd']; |
---|
86 | if ($core->auth->checkUser($user_id,$user_pwd) === true) { |
---|
87 | $core->session->start(); |
---|
88 | $_SESSION['sess_user_id'] = $user_id; |
---|
89 | $_SESSION['sess_browser_uid'] = http::browserUID(DC_MASTER_KEY); |
---|
90 | |
---|
91 | if (!empty($_POST['blog'])) { |
---|
92 | $_SESSION['sess_blog_id'] = $_POST['blog']; |
---|
93 | } |
---|
94 | if (!empty($_POST['user_remember'])) |
---|
95 | { |
---|
96 | $cookie_admin = |
---|
97 | http::browserUID(DC_MASTER_KEY.$user_id.crypt::hmac(DC_MASTER_KEY,$user_pwd)). |
---|
98 | bin2hex(pack('a32',$user_id)); |
---|
99 | |
---|
100 | setcookie('dc_admin',$cookie_admin,strtotime('+15 days'),'','',DC_ADMIN_SSL); |
---|
101 | } |
---|
102 | header('Location: '.$core->blog->url); |
---|
103 | return; |
---|
104 | }; |
---|
105 | |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | class PreviewBehavior { |
---|
111 | public static function publicPrepend(&$core) { |
---|
112 | if (isset($_COOKIE[DC_SESSION_NAME])) |
---|
113 | { |
---|
114 | $core->session->start(); |
---|
115 | $core->auth->checkUser($_SESSION['sess_user_id']); |
---|
116 | } |
---|
117 | |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | class tplPreview { |
---|
122 | public static function EntryIfOffline($attr) { |
---|
123 | $ret = isset($attr['return']) ? $attr['return'] : 'offline'; |
---|
124 | $ret = html::escapeHTML($ret); |
---|
125 | |
---|
126 | return '<?php if ($_ctx->posts->post_status != 1) { '. "echo '".addslashes($ret)."'; } ?".">"; |
---|
127 | } |
---|
128 | |
---|
129 | public static function authWidget(&$w) { |
---|
130 | global $core; |
---|
131 | $title = $w->title ? html::escapeHTML($w->title) : __('Connection'); |
---|
132 | $is_authenticated=isset($_COOKIE[DC_SESSION_NAME]); |
---|
133 | |
---|
134 | $res = '<div id="auth">'. |
---|
135 | '<h2>'.$title.'</h2>'; |
---|
136 | |
---|
137 | if (!$is_authenticated) { |
---|
138 | $res .= '<form action="'.$core->blog->url.'login" method="post">'. |
---|
139 | '<p><label>'.__('Login').' '. |
---|
140 | form::field("user_id",20,32).'</label></p>'. |
---|
141 | '<p><label>'.__('Password').' '. |
---|
142 | form::password("user_pwd",20,255).'</label></p>'. |
---|
143 | '<p><input type="submit" value="'.__('login').'" tabindex="3" />'. |
---|
144 | '</form>'; |
---|
145 | } else { |
---|
146 | $res .= "You are authenticated"; |
---|
147 | } |
---|
148 | $res .= '</div>'; |
---|
149 | return $res; |
---|
150 | |
---|
151 | } |
---|
152 | } |
---|