1 | <?php |
---|
2 | # ***** BEGIN LICENSE BLOCK ***** |
---|
3 | # This file a plugin of DotClear. |
---|
4 | # Copyright (c) Marc Vachette. All rights |
---|
5 | # reserved. |
---|
6 | # |
---|
7 | #Subscription2 is free software; you can redistribute it and/or modify |
---|
8 | # it under the terms of the Creative Commons License "Attribution" |
---|
9 | # see the page http://creativecommons.org/licenses/by/2.0/ for more information |
---|
10 | # |
---|
11 | # Subscription is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # Creative Commons License for more details. |
---|
15 | # |
---|
16 | # ***** END LICENSE BLOCK ***** |
---|
17 | |
---|
18 | $core->url->register('subscription','subscribe','^subscribe(/(.*))?$',array('urlSubscription','create')); |
---|
19 | |
---|
20 | $core->tpl->addBlock('SubscriptionIf',array('tplSubscription','SubscriptionIf')); |
---|
21 | $core->tpl->addValue('SubscriptionURL',array('tplSubscription','SubscriptionURL')); |
---|
22 | $core->tpl->addValue('SubscriptionMsgSuccess',array('tplSubscription','SubscriptionMsgSuccess')); |
---|
23 | $core->tpl->addValue('SubscriptionMsgError',array('tplSubscription','SubscriptionMsgError')); |
---|
24 | $core->tpl->addValue('SubscriptionDomainName',array('tplSubscription','SubscriptionDomainName')); |
---|
25 | $core->tpl->addValue('SubscriptionPageTitle',array('tplSubscription','SubscriptionPageTitle')); |
---|
26 | $core->tpl->addValue('SubscriptionFormCaption',array('tplSubscription','SubscriptionFormCaption')); |
---|
27 | $core->tpl->addValue('SubscriptionName',array('tplSubscription','SubscriptionName')); |
---|
28 | $core->tpl->addValue('SubscriptionEmail',array('tplSubscription','SubscriptionEmail')); |
---|
29 | $core->tpl->addValue('SubscriptionLogin',array('tplSubscription','SubscriptionLogin')); |
---|
30 | $core->tpl->addValue('SubscriptionBlogName',array('tplSubscription','SubscriptionBlogName')); |
---|
31 | $core->tpl->addValue('SubscriptionBlogUrl',array('tplSubscription','SubscriptionBlogUrl')); |
---|
32 | |
---|
33 | |
---|
34 | class urlSubscription extends dcUrlHandlers { |
---|
35 | |
---|
36 | public static function getDomainName() { |
---|
37 | return $_SERVER['HTTP_HOST']; |
---|
38 | } |
---|
39 | |
---|
40 | public static function create($args) |
---|
41 | { |
---|
42 | global $core, $_ctx; |
---|
43 | |
---|
44 | if (!$core->blog->settings->blogs_folder_path) { |
---|
45 | self::p404(); |
---|
46 | exit; |
---|
47 | } |
---|
48 | |
---|
49 | $_ctx->subscription = new ArrayObject(array( |
---|
50 | 'name' => '', |
---|
51 | 'mail' => '', |
---|
52 | 'login' => '', |
---|
53 | 'password' => '', |
---|
54 | 'password_confirm' => '', |
---|
55 | 'blog_name' => '', |
---|
56 | 'blog_url' => '', |
---|
57 | 'created' => false, |
---|
58 | 'error' => false, |
---|
59 | 'error_msg' => '' |
---|
60 | )); |
---|
61 | |
---|
62 | $create_msg = |
---|
63 | isset($_POST['s_name']) && isset($_POST['s_mail']) && isset($_POST['s_login']) && |
---|
64 | isset($_POST['s_password']) && isset($_POST['s_password_confirm']) && |
---|
65 | isset($_POST['s_blog_name']) && isset($_POST['s_blog_url']); |
---|
66 | |
---|
67 | if ($args == 'created') |
---|
68 | { |
---|
69 | $_ctx->subscription['created'] = true; |
---|
70 | } |
---|
71 | elseif ($create_msg) |
---|
72 | { |
---|
73 | # Spam trap |
---|
74 | if (!empty($_POST['f_mail'])) { |
---|
75 | http::head(412,'Precondition Failed'); |
---|
76 | header('Content-Type: text/plain'); |
---|
77 | echo "So Long, and Thanks For All the Fish"; |
---|
78 | exit; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | try |
---|
83 | { |
---|
84 | $_ctx->subscription['name'] = preg_replace('/[\n\r]/','',$_POST['s_name']); |
---|
85 | $_ctx->subscription['mail'] = preg_replace('/[\n\r]/','',$_POST['s_mail']); |
---|
86 | $_ctx->subscription['login'] = preg_replace('/[\n\r]/','',$_POST['s_login']); |
---|
87 | $_ctx->subscription['password'] = preg_replace('/[\n\r]/','',$_POST['s_password']); |
---|
88 | $_ctx->subscription['password_confirm'] = preg_replace('/[\n\r]/','',$_POST['s_password_confirm']); |
---|
89 | $_ctx->subscription['blog_name'] = preg_replace('/[\n\r]/','',$_POST['s_blog_name']); |
---|
90 | $_ctx->subscription['blog_url'] = preg_replace('/[\n\r]/','',$_POST['s_blog_url']); |
---|
91 | |
---|
92 | # Checks provided fields |
---|
93 | if (empty($_POST['s_name'])) { |
---|
94 | throw new Exception(__('You must provide a name.')); |
---|
95 | } |
---|
96 | |
---|
97 | if (!text::isEmail($_POST['s_mail'])) { |
---|
98 | throw new Exception(__('You must provide a valid email address.')); |
---|
99 | } |
---|
100 | |
---|
101 | if (empty($_POST['s_login'])) { |
---|
102 | throw new Exception(__('You must provide a login.')); |
---|
103 | } |
---|
104 | |
---|
105 | if (empty($_POST['s_password'])) { |
---|
106 | throw new Exception(__('You must provide a password.')); |
---|
107 | } |
---|
108 | |
---|
109 | if ($_POST['s_password_confirm'] !== $_POST['s_password']) { |
---|
110 | throw new Exception(__("Passwords don't match")); |
---|
111 | } |
---|
112 | |
---|
113 | if (empty($_POST['s_blog_name'])) { |
---|
114 | throw new Exception(__('You must provide a name for you blog.')); |
---|
115 | } |
---|
116 | |
---|
117 | if (empty($_POST['s_blog_url'])) { |
---|
118 | throw new Exception(__('You must provide an url for your blog.')); |
---|
119 | } |
---|
120 | |
---|
121 | //creating user and blog |
---|
122 | $core->blog->settings->setNamespace('subscription2'); |
---|
123 | $blogs_folder_path = $core->blog->settings->get('blogs_folder_path'); |
---|
124 | $dotclear_folder_path = $core->blog->settings->get('dotclear_folder_path'); |
---|
125 | |
---|
126 | |
---|
127 | if($core->userExists($_ctx->subscription['login'])) { |
---|
128 | throw new Exception(__('User already exists')); |
---|
129 | } |
---|
130 | |
---|
131 | if($core->blogExists($_ctx->subscription['blog_url'])) { |
---|
132 | throw new Exception(__('A blog already exists at this URL')); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | //augmentation des droits TODO : changer admin par un recuperation de login |
---|
137 | $core->auth->checkUser('marc'); |
---|
138 | |
---|
139 | //user |
---|
140 | $cur = $core->con->openCursor($core->prefix.'user'); |
---|
141 | |
---|
142 | $cur->user_id = $_ctx->subscription['login']; |
---|
143 | $cur->user_super = 0; |
---|
144 | $cur->user_email = $_ctx->subscription['mail']; |
---|
145 | $cur->user_pwd = $_ctx->subscription['password']; |
---|
146 | |
---|
147 | if (!preg_match('/^[A-Za-z0-9._-]{2,}$/',$cur->user_id)) { |
---|
148 | throw new Exception(__('User ID must contain at least 2 characters using letters, numbers or symbols.')); |
---|
149 | } |
---|
150 | if ($cur->user_creadt === null) { |
---|
151 | $cur->user_creadt = array('NOW()'); |
---|
152 | } |
---|
153 | |
---|
154 | //$cur->insert(); |
---|
155 | $core->addUser($cur); |
---|
156 | |
---|
157 | |
---|
158 | //blog |
---|
159 | |
---|
160 | $root_url = 'http://'.$_ctx->subscription['blog_url'].'.'.self::getDomainName().'/'; |
---|
161 | |
---|
162 | $cur = $core->con->openCursor($core->prefix.'blog'); |
---|
163 | |
---|
164 | $cur->blog_id = $_ctx->subscription['blog_url']; |
---|
165 | $cur->blog_url = $root_url.'index.php/'; |
---|
166 | $cur->blog_name = $_ctx->subscription['blog_name']; |
---|
167 | |
---|
168 | $core->addBlog($cur); |
---|
169 | |
---|
170 | //permissions du blog |
---|
171 | |
---|
172 | $core->setUserBlogPermissions($_ctx->subscription['login'], $_ctx->subscription['blog_url'], array('admin'=>1, 'blogroll'=>1), true); |
---|
173 | |
---|
174 | $core->blogDefaults($cur->blog_id); |
---|
175 | |
---|
176 | $blog_settings = new dcSettings($core,$_ctx->subscription['blog_url']); |
---|
177 | $blog_settings->setNameSpace('system'); |
---|
178 | $blog_settings->put('lang',http::getAcceptLanguage()); |
---|
179 | |
---|
180 | $blog_settings->put('themes_path',$core->blog->themes_path); |
---|
181 | $blog_settings->put('themes_url',$core->blog->themes_path); //TODO : injection via la config |
---|
182 | |
---|
183 | //creating blog folder and index.php file |
---|
184 | $path = $blogs_folder_path.$_ctx->subscription['blog_url']; |
---|
185 | if(!file_exists($path)){ |
---|
186 | mkdir ($path); |
---|
187 | chmod ($path, 0755); |
---|
188 | mkdir ($path."/public"); |
---|
189 | chmod ($path."/public", 0755); |
---|
190 | } |
---|
191 | |
---|
192 | file_put_contents($path.'/index.php',"<?php\n\n". |
---|
193 | "define('DC_BLOG_ID','".$_ctx->subscription['blog_url']."'); # identifiant du blog\n". |
---|
194 | "require '".realpath($dotclear_folder_path.'/inc/public/prepend.php')."';\n\n?>"); //TODO : param dans la conf pour le path de dotclear |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | http::redirect($core->blog->url.$core->url->getBase('subscription2').'/created'); |
---|
199 | } |
---|
200 | catch (Exception $e) |
---|
201 | { |
---|
202 | $_ctx->subscription['error'] = true; |
---|
203 | $_ctx->subscription['error_msg'] = $e->getMessage(); |
---|
204 | } |
---|
205 | |
---|
206 | } |
---|
207 | |
---|
208 | $core->tpl->setPath($core->tpl->getPath(), dirname(__FILE__).'/default-templates'); |
---|
209 | self::serveDocument('new_blog.html'); |
---|
210 | exit; |
---|
211 | } |
---|
212 | |
---|
213 | } |
---|
214 | |
---|
215 | class tplSubscription |
---|
216 | { |
---|
217 | public static function SubscriptionIf($attr,$content) |
---|
218 | { |
---|
219 | $if = array(); |
---|
220 | |
---|
221 | $operator = isset($attr['operator']) ? $this->getOperator($attr['operator']) : '&&'; |
---|
222 | |
---|
223 | if (isset($attr['created'])) { |
---|
224 | $sign = (boolean) $attr['created'] ? '' : '!'; |
---|
225 | $if[] = $sign."\$_ctx->subscription['created']"; |
---|
226 | } |
---|
227 | |
---|
228 | if (isset($attr['error'])) { |
---|
229 | $sign = (boolean) $attr['error'] ? '' : '!'; |
---|
230 | $if[] = $sign."\$_ctx->subscription['error']"; |
---|
231 | } |
---|
232 | |
---|
233 | if (!empty($if)) { |
---|
234 | return '<?php if('.implode(' '.$operator.' ',$if).') : ?>'.$content.'<?php endif; ?>'; |
---|
235 | } else { |
---|
236 | return $content; |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | public static function SubscriptionURL($attr) |
---|
241 | { |
---|
242 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
243 | return '<?php echo '.sprintf($f,'$core->blog->url.$core->url->getBase("subscription2")').'; ?>'; |
---|
244 | } |
---|
245 | |
---|
246 | public static function SubscriptionMsgSuccess($attr) |
---|
247 | { |
---|
248 | /*return '<?php echo $core->blog->settings->cm_msg_success; ?>';*/ |
---|
249 | return '<?php echo "Yeah !"; ?>'; |
---|
250 | } |
---|
251 | |
---|
252 | public static function SubscriptionMsgError($attr) |
---|
253 | { |
---|
254 | //TODO : message d'erreur depuis la conf |
---|
255 | return '<?php echo html::escapeHTML($_ctx->subscription["error_msg"]); ?>'; |
---|
256 | /*return '<?php echo sprintf($core->blog->settings->subscription2_msg_error,html::escapeHTML($_ctx->subscription["error_msg"])); ?>';*/ |
---|
257 | } |
---|
258 | |
---|
259 | public static function SubscriptionDomainName() { |
---|
260 | $domain_name = urlSubscription::getDomainName(); |
---|
261 | return "<?php echo $domain_name; ?>"; |
---|
262 | } |
---|
263 | |
---|
264 | public static function SubscriptionPageTitle($attr) |
---|
265 | { |
---|
266 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
267 | return '<?php echo '.sprintf($f,'$core->blog->settings->subscription_page_title').'; ?>'; |
---|
268 | } |
---|
269 | |
---|
270 | public static function SubscriptionFormCaption($attr) |
---|
271 | { |
---|
272 | //TODO : titre du formulaire depuis la conf |
---|
273 | //$f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
274 | /*return '<?php echo '.sprintf($f,'$core->blog->settings->subscription_page_title').'; ?>';*/ |
---|
275 | } |
---|
276 | |
---|
277 | public static function SubscriptionName($attr) |
---|
278 | { |
---|
279 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
280 | return '<?php echo '.sprintf($f,'$_ctx->subscription["name"]').'; ?>'; |
---|
281 | } |
---|
282 | |
---|
283 | public static function SubscriptionEmail($attr) |
---|
284 | { |
---|
285 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
286 | return '<?php echo '.sprintf($f,'$_ctx->subscription["mail"]').'; ?>'; |
---|
287 | } |
---|
288 | |
---|
289 | public static function SubscriptionLogin($attr) |
---|
290 | { |
---|
291 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
292 | return '<?php echo '.sprintf($f,'$_ctx->subscription["login"]').'; ?>'; |
---|
293 | } |
---|
294 | |
---|
295 | public static function SubscriptionBlogName($attr) |
---|
296 | { |
---|
297 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
298 | return '<?php echo '.sprintf($f,'$_ctx->subscription["blog_name"]').'; ?>'; |
---|
299 | } |
---|
300 | |
---|
301 | public static function SubscriptionBlogUrl($attr) |
---|
302 | { |
---|
303 | $f = $GLOBALS['core']->tpl->getFilters($attr); |
---|
304 | return '<?php echo '.sprintf($f,'$_ctx->subscription["blog_url"]').'; ?>'; |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | ?> |
---|