]> git.bts.cx Git - cx.git/blob - cx/cx.php
Hide date on pages
[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 (cx_template_has_content('public', $path)) {
71                 cx_template_output_content('public', $path);
72                 exit(0);
73         } else if (cx_template_has_content('admin', $path)) {
74                 cx_template_output_content('admin', $path);
75                 exit(0);
76         } else if (count($path_components) >= 1 && $path_components[0] == 'feed') {
77                 header('Content-type: application/atom+xml;');
78                 $template = 'atom';
79         } else if (count($path_components) >= 1 && $path_components[0] == 'cx') {
80                 if (count($path_components) >= 2 && $path_components[1] == 'login') {
81                         if (cx_admin_logged_in()) {
82                                 cx_http_redirect(cx_url_admin('/'));
83                                 exit(0);
84                         } else {
85                                 $username = cx_form_input_sanitized('id');
86                                 $password = cx_form_input_sanitized('password');
87         
88                                 if ($username != null && $password != null && cx_admin_login($username, $password)) {
89                                         cx_http_redirect(cx_url_admin('/'));
90                                         exit(0);
91                                 }
92         
93                                 $template_class = 'admin';
94                                 $template = 'login';
95                         }
96                 } else {
97                         if (cx_admin_logged_in() == false) {
98                                 cx_http_redirect(cx_url_admin('/login/'));
99                                 exit(0);
100                         } else {
101                                 if (count($path_components) >= 2 && $path_components[1] == 'logout') {
102                                         cx_admin_logout();
103                                         cx_http_redirect(cx_url_admin('/'));
104                                         exit(0);
105                                 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'add') {
106                                         $template_variables['post_id'] = '0';
107                                         $template_variables['post_title'] = '';
108                                         $template_variables['post_slug'] = '';
109                                         $template_variables['post_date'] = '';
110                                         $template_variables['post_data'] = '';
111                                         $template_variables['post_is_page'] = false;
112                                         $template_variables['post_is_draft'] = true;
113                                         $template_variables['post_nav_index'] = '';
114
115                                         $template_class = 'admin';
116                                         $template = 'post';
117                                 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'edit') {
118                                         $post = cx_posts_find_post($_GET['id']);
119
120                                         $template_variables['post_id'] = $post->id;
121                                         $template_variables['post_title'] = $post->title;
122                                         $template_variables['post_slug'] = $post->slug;
123                                         $template_variables['post_date'] = $post->date;
124                                         $template_variables['post_data'] = $post->data;
125                                         $template_variables['post_is_page'] = $post->is_page;
126                                         $template_variables['post_is_draft'] = $post->is_draft;
127                                         $template_variables['post_nav_index'] = $post->nav_index;
128
129                                         $template_class = 'admin';
130                                         $template = 'post';
131                                 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'update') {
132                                         $title = cx_form_input_sanitized('post_title');
133                                         $slug = cx_form_input_sanitized('post_slug');
134                                         if (isset($slug) == false) $slug = null;
135                                         $date = cx_form_input_sanitized_date_time('post_date');
136                                         if (isset($date) == false) $date = null;
137                                         $is_page = cx_form_input_sanitized('post_is_page') == 'page';
138                                         $is_draft = cx_form_input_sanitized('post_is_draft') == 'draft';
139                                         $nav_index = cx_form_input_sanitized('post_nav_index');
140                                         $data = cx_form_input_sanitized_allowing_html('post_data');
141                                         
142                                         if (isset($_GET['id']) == false or $_GET['id'] == 0) {
143                                                 cx_posts_add_post(1, $title, $slug, $date, $is_page, $is_draft, $nav_index, $data);
144                                         } else {
145                                                 $id = $_GET['id'];
146                                                 cx_posts_update_post($id, $title, $slug, $date, $is_page, $is_draft, $nav_index, $data);
147                                         }
148                                         
149                                         cx_http_redirect(cx_url_admin('/'));
150                                         exit(0);
151                                 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'delete') {
152                                         cx_posts_delete_post($_GET['id']);
153                                         cx_http_redirect(cx_url_admin('/'));
154                                         exit(0);
155                                 } else if (count($path_components) >= 3 && $path_components[1] == 'images' && $path_components[2] == 'add') {
156                                         $template_variables['image_id'] = '0';
157                                         $template_variables['image_alt_text'] = '';
158                                         
159                                         $template_class = 'admin';
160                                         $template = 'image';
161                                 } else if (count($path_components) >= 3 && $path_components[1] == 'images' && $path_components[2] == 'update') {
162                                         $alt_text = cx_form_input_sanitized('image_alt_text');
163
164                                         $filename = $_FILES['image_file']['tmp_name'];
165                                         $original_filename = $_FILES['image_file']['name'];
166
167                                         cx_images_add_image(1, $alt_text, $filename, $original_filename);
168                                         
169                                         cx_http_redirect(cx_url_admin('/'));
170                                         exit(0);
171                                 } else {
172                                         $template_class = 'admin';
173                                         $template = 'main';
174                                 }
175                         }
176                 }
177         } else if (count($path_components) >= 2 && $path_components[0] == 'page') { // FIXME sometime, needs more flexibility...
178                 $page_number = $path_components[1];
179
180                 $template = 'list';
181                 $template_variables['page_number'] = $page_number;
182         } else {
183                 if (count($path_components) >= 1) {
184                         $slug = $path_components[0];
185                         $page_id = cx_posts_find_page_id($slug);
186                         if ($page_id) {
187                                 $template = 'post';
188                                 $template_variables['post_id'] = $page_id;
189                         }
190                 }
191
192                 if ($template == null && count($path_components) >= 3) { // FIXME sometime, needs more flexibility...
193                         $year = $path_components[0];
194                         $month = $path_components[1];
195                         $slug = $path_components[2];
196                         $post_id = cx_posts_find_article_id($slug);
197                         if ($post_id) {
198                                 $template = 'post';
199                                 $template_variables['post_id'] = $post_id;
200                         }
201                 }
202         }
203
204         if ($template != null) {
205                 $output = cx_template_render($template_class, $template, $template_variables);
206                 echo($output);
207         } else {
208                 http_response_code(404);
209                 exit(0);
210         }
211 }