]> git.bts.cx Git - cx.git/blob - cx/cx.php
35b2722d4c400e145697a0ff683ed5e4830f02c4
[cx.git] / cx / cx.php
1 <?php
2
3 function cx_require(...$segments) {
4         array_unshift($segments, CX_PATH);
5         require_once(join(DIRECTORY_SEPARATOR, $segments));
6 }
7
8 define('CX_PATH', __DIR__);
9
10 cx_require('lib', 'admin.php');
11 cx_require('lib', 'form.php');
12 cx_require('lib', 'http.php');
13 cx_require('lib', 'images.php');
14 cx_require('lib', 'posts.php');
15 cx_require('lib', 'sessions.php');
16 cx_require('lib', 'setup.php');
17 cx_require('lib', 'site.php');
18 cx_require('lib', 'system.php');
19 cx_require('lib', 'template.php');
20 cx_require('lib', 'url.php');
21 cx_require('lib', 'user_data.php');
22 cx_require('lib', 'users.php');
23
24 function cx($db_path, $data_folder_path, $public_data_folder_path) {
25         define('CX_DATABASE_FILE', $db_path);
26         define('CX_USER_DATA_PATH', $data_folder_path);
27         define('CX_PUBLIC_USER_DATA_PATH', $public_data_folder_path);
28
29         if (cx_setup_required()) {
30                 cx_setup_run();
31
32                 require('../setup.php');
33                 $new_author = cx_users_add_user(CX_SETUP_USER, CX_SETUP_PASSWORD);
34                 $new_site = cx_sites_add_site(CX_SETUP_URL, CX_SETUP_TITLE, CX_SETUP_BYLINE, CX_SETUP_COPYRIGHT);
35                 cx_sites_site_add_user($new_site, $new_author, true);
36
37                 exit;
38         }
39
40         $path = '/';
41
42         if (isset($_SERVER['REQUEST_URI'])) {
43                 $route_details = parse_url($_SERVER['REQUEST_URI']);
44                 if (isset($route_details['path'])) {
45                         $path = $route_details['path'];
46                 }
47         }
48
49         $script_name = $_SERVER['SCRIPT_NAME'];
50         $script_name_len = strlen($script_name);
51         if (substr_compare($path, $script_name, 0, $script_name_len) == 0) {
52                 $path = substr($path, $script_name_len);
53         }
54
55         cx_route($path);
56 }
57
58 function cx_route($path) {
59         $path_components = explode('/', $path, 10);
60         $path_components = array_filter($path_components);
61         $path_components = array_values($path_components); // re-index
62
63         $template = null;
64         $template_class = 'public';
65         $template_variables = [];
66
67         if (count($path_components) == 0) {
68                 $template = 'list';
69                 $template_variables['page_number'] = 0;
70         } else if (count($path_components) >= 1 && $path_components[0] == 'feed') {
71                 header('Content-type: application/atom+xml;');
72                 $template = 'atom';
73         } else if (count($path_components) >= 1 && $path_components[0] == 'cx') {
74                 if (count($path_components) >= 2 && $path_components[1] == 'login') {
75                         if (cx_admin_logged_in()) {
76                                 cx_http_redirect(cx_url_admin('/'));
77                                 exit(0);
78                         } else {
79                                 $username = cx_form_input_sanitized('id');
80                                 $password = cx_form_input_sanitized('password');
81         
82                                 if ($username != null && $password != null && cx_admin_login($username, $password)) {
83                                         cx_http_redirect(cx_url_admin('/'));
84                                         exit(0);
85                                 }
86         
87                                 $template_class = 'admin';
88                                 $template = 'login';
89                         }
90                 } else {
91                         if (cx_admin_logged_in() == false) {
92                                 cx_http_redirect(cx_url_admin('/login/'));
93                                 exit(0);
94                         } else {
95                                 if (count($path_components) >= 2 && $path_components[1] == 'logout') {
96                                         cx_admin_logout();
97                                         cx_http_redirect(cx_url_admin('/'));
98                                         exit(0);
99                                 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'add') {
100                                         $template_variables['post_id'] = '0';
101                                         $template_variables['post_title'] = '';
102                                         $template_variables['post_slug'] = '';
103                                         $template_variables['post_date'] = '';
104                                         $template_variables['post_data'] = '';
105                                         $template_variables['post_is_page'] = false;
106                                         $template_variables['post_is_draft'] = true;
107
108                                         $template_class = 'admin';
109                                         $template = 'post';
110                                 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'edit') {
111                                         $post = cx_posts_find_post($_GET['id']);
112
113                                         $template_variables['post_id'] = $post->id;
114                                         $template_variables['post_title'] = $post->title;
115                                         $template_variables['post_slug'] = $post->slug;
116                                         $template_variables['post_date'] = $post->date;
117                                         $template_variables['post_data'] = $post->data;
118                                         $template_variables['post_is_page'] = $post->is_page;
119                                         $template_variables['post_is_draft'] = $post->is_draft;
120
121                                         $template_class = 'admin';
122                                         $template = 'post';
123                                 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'update') {
124                                         $title = cx_form_input_sanitized('post_title');
125                                         $slug = cx_form_input_sanitized('post_slug');
126                                         if (isset($slug) == false) $slug = null;
127                                         $date = cx_form_input_sanitized_date_time('post_date');
128                                         if (isset($date) == false) $date = null;
129                                         $is_page = cx_form_input_sanitized('post_is_page') == 'page';
130                                         $is_draft = cx_form_input_sanitized('post_is_draft') == 'draft';
131                                         $data = cx_form_input_sanitized_allowing_html('post_data');
132                                         
133                                         if (isset($_GET['id']) == false or $_GET['id'] == 0) {
134                                                 cx_posts_add_post(1, $title, $slug, $date, $is_page, $is_draft, $data);
135                                         } else {
136                                                 $id = $_GET['id'];
137                                                 cx_posts_update_post($id, $title, $slug, $date, $is_page, $is_draft, $data);
138                                         }
139                                         
140                                         cx_http_redirect(cx_url_admin('/'));
141                                         exit(0);
142                                 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'delete') {
143                                         cx_posts_delete_post($_GET['id']);
144                                         cx_http_redirect(cx_url_admin('/'));
145                                         exit(0);
146                                 } else if (count($path_components) >= 3 && $path_components[1] == 'images' && $path_components[2] == 'add') {
147                                         $template_variables['image_id'] = '0';
148                                         $template_variables['image_alt_text'] = '';
149                                         
150                                         $template_class = 'admin';
151                                         $template = 'image';
152                                 } else if (count($path_components) >= 3 && $path_components[1] == 'images' && $path_components[2] == 'update') {
153                                         $alt_text = cx_form_input_sanitized('image_alt_text');
154
155                                         $filename = $_FILES['image_file']['tmp_name'];
156                                         $original_filename = $_FILES['image_file']['name'];
157
158                                         cx_images_add_image(1, $alt_text, $filename, $original_filename);
159                                         
160                                         cx_http_redirect(cx_url_admin('/'));
161                                         exit(0);
162                                 } else {
163                                         $template_class = 'admin';
164                                         $template = 'main';
165                                 }
166                         }
167                 }
168         } else if (count($path_components) >= 2 && $path_components[0] == 'page') { // FIXME sometime, needs more flexibility...
169                 $page_number = $path_components[1];
170
171                 $template = 'list';
172                 $template_variables['page_number'] = $page_number;
173         } else {
174                 if (count($path_components) >= 1) {
175                         $slug = $path_components[0];
176                         $page_id = cx_posts_find_page_id($slug);
177                         if ($page_id) {
178                                 $template = 'post';
179                                 $template_variables['post_id'] = cx_posts_find_page_id($slug);
180                         }
181                 }
182
183                 if ($template == null && count($path_components) >= 3) { // FIXME sometime, needs more flexibility...
184                         $year = $path_components[0];
185                         $month = $path_components[1];
186                         $slug = $path_components[2];
187                         $post_id = cx_posts_find_article_id($slug);
188                         if ($post_id) {
189                                 $template = 'post';
190                                 $template_variables['post_id'] = cx_posts_find_article_id($slug);
191                         }
192                 }
193         }
194
195         if ($template != null) {
196                 $output = cx_template_render($template_class, $template, $template_variables);
197                 echo($output);
198         } else {
199                 http_response_code(404);
200                 exit(0);
201         }
202 }