1 | <?php |
---|
2 | |
---|
3 | |
---|
4 | class Subscription |
---|
5 | { |
---|
6 | |
---|
7 | public static function subscribe(ArrayObject $subscription) |
---|
8 | { |
---|
9 | global $core; |
---|
10 | |
---|
11 | if($core->userExists($subscription['login'])) { |
---|
12 | throw new Exception(__('User already exists')); |
---|
13 | } |
---|
14 | |
---|
15 | if($core->blogExists($subscription['blog_url'])) { |
---|
16 | throw new Exception(__('A blog already exists at this URL')); |
---|
17 | } |
---|
18 | |
---|
19 | self::_createUser($subscription); |
---|
20 | self::_createBlog($subscription); |
---|
21 | } |
---|
22 | |
---|
23 | protected static function _createUser(ArrayObject $subscription) |
---|
24 | { |
---|
25 | global $core; |
---|
26 | |
---|
27 | //augmentation des droits pour pouvoir ajouter le user |
---|
28 | //@TODO : changer admin par un recuperation de login |
---|
29 | $core->auth->checkUser('mvachette'); |
---|
30 | |
---|
31 | //user |
---|
32 | $cur = $core->con->openCursor($core->prefix.'user'); |
---|
33 | |
---|
34 | $cur->user_id = $subscription['login']; |
---|
35 | $cur->user_super = 0; |
---|
36 | $cur->user_email = $subscription['mail']; |
---|
37 | $cur->user_pwd = $subscription['password']; |
---|
38 | |
---|
39 | if (!preg_match('/^[A-Za-z0-9._-]{2,}$/',$cur->user_id)) { |
---|
40 | throw new Exception(__('User ID must contain at least 2 characters using letters, numbers or symbols.')); |
---|
41 | } |
---|
42 | |
---|
43 | if ($cur->user_creadt === null) { |
---|
44 | $cur->user_creadt = array('NOW()'); |
---|
45 | } |
---|
46 | |
---|
47 | $core->addUser($cur); |
---|
48 | } |
---|
49 | |
---|
50 | protected static function _createBlog(ArrayObject $subscription) |
---|
51 | { |
---|
52 | global $core; |
---|
53 | |
---|
54 | //settings |
---|
55 | $core->blog->settings->setNamespace('subscription'); |
---|
56 | $blogs_folder_path = $core->blog->settings->blogs_folder_path; |
---|
57 | $dotclear_folder_path = $core->blog->settings->dotclear_folder_path; |
---|
58 | |
---|
59 | //blog |
---|
60 | $root_url = 'http://'.$subscription['blog_url'].'.'.urlSubscription::getDomainName().'/'; |
---|
61 | |
---|
62 | $cur = $core->con->openCursor($core->prefix.'blog'); |
---|
63 | |
---|
64 | $cur->blog_id = $subscription['blog_url']; |
---|
65 | $cur->blog_url = $root_url.'index.php/'; |
---|
66 | $cur->blog_name = $subscription['blog_name']; |
---|
67 | |
---|
68 | $core->addBlog($cur); |
---|
69 | |
---|
70 | //permissions du blog |
---|
71 | $core->setUserBlogPermissions($subscription['login'], $subscription['blog_url'], array('admin' => 1, 'blogroll' => 1), true); |
---|
72 | |
---|
73 | $core->blogDefaults($cur->blog_id); |
---|
74 | |
---|
75 | $blog_settings = new dcSettings($core,$subscription['blog_url']); |
---|
76 | $blog_settings->setNameSpace('system'); |
---|
77 | $blog_settings->put('lang',http::getAcceptLanguage()); |
---|
78 | |
---|
79 | $blog_settings->put('themes_path',$core->blog->themes_path); |
---|
80 | $blog_settings->put('themes_url',$core->blog->themes_path); //TODO : injection via la config |
---|
81 | |
---|
82 | //creating blog folder and index.php file |
---|
83 | $path = $blogs_folder_path.$subscription['blog_url']; |
---|
84 | mkdir ($path); |
---|
85 | chmod ($path, 0755); |
---|
86 | mkdir ($path."/public"); |
---|
87 | chmod ($path."/public", 0755); |
---|
88 | |
---|
89 | file_put_contents($path.'/index.php',"<?php\n\n". |
---|
90 | "define('DC_BLOG_ID','".$subscription['blog_url']."'); # identifiant du blog\n". |
---|
91 | "require '".realpath($dotclear_folder_path.'/inc/public/prepend.php')."';\n\n?>"); //TODO : param dans la conf pour le path de dotclear |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | } |
---|