]>
git.bts.cx Git - cx.git/blob - cx/cx.php
3 function cx_require(...$segments) {
4 array_unshift($segments, CX_PATH
);
5 require_once(join(DIRECTORY_SEPARATOR
, $segments));
8 define('CX_PATH', __DIR__
);
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');
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);
29 if (cx_setup_required()) {
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);
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'];
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);
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
64 $template_class = 'public';
65 $template_variables = [];
67 if (count($path_components) == 0) {
69 } else if (count($path_components) >= 1 && $path_components[0] == 'feed') {
70 header('Content-type: application/atom+xml;');
72 } else if (count($path_components) >= 1 && $path_components[0] == 'cx') {
73 if (count($path_components) >= 2 && $path_components[1] == 'login') {
74 if (cx_admin_logged_in()) {
75 cx_http_redirect(cx_url_admin('/'));
78 $username = cx_form_input_sanitized('id');
79 $password = cx_form_input_sanitized('password');
81 if ($username != null && $password != null && cx_admin_login($username, $password)) {
82 cx_http_redirect(cx_url_admin('/'));
86 $template_class = 'admin';
90 if (cx_admin_logged_in() == false) {
91 cx_http_redirect(cx_url_admin('/login/'));
94 if (count($path_components) >= 2 && $path_components[1] == 'logout') {
96 cx_http_redirect(cx_url_admin('/'));
98 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'add') {
99 $template_variables['post_id'] = '0';
100 $template_variables['post_title'] = '';
101 $template_variables['post_slug'] = '';
102 $template_variables['post_date'] = '';
103 $template_variables['post_data'] = '';
105 $template_class = 'admin';
107 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'edit') {
108 $post = cx_posts_find_post($_GET['id']);
110 $template_variables['post_id'] = $post->id
;
111 $template_variables['post_title'] = $post->title
;
112 $template_variables['post_slug'] = $post->slug
;
113 $template_variables['post_date'] = $post->date
;
114 $template_variables['post_data'] = $post->data
;
116 $template_class = 'admin';
118 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'update') {
119 $title = cx_form_input_sanitized('post_title');
120 $slug = cx_form_input_sanitized('post_slug');
121 if (isset($slug) == false) $slug = null;
122 $date = cx_form_input_sanitized_date_time('post_date');
123 if (isset($date) == false) $date = null;
124 $data = cx_form_input_sanitized('post_data');
126 if (isset($_GET['id']) == false or $_GET['id'] == 0) {
127 cx_posts_add_post(1, $title, $slug, $date, $data);
130 cx_posts_update_post($id, $title, $slug, $date, $data);
133 cx_http_redirect(cx_url_admin('/'));
135 } else if (count($path_components) >= 3 && $path_components[1] == 'posts' && $path_components[2] == 'delete') {
136 cx_posts_delete_post($_GET['id']);
137 cx_http_redirect(cx_url_admin('/'));
139 } else if (count($path_components) >= 3 && $path_components[1] == 'images' && $path_components[2] == 'add') {
140 $template_variables['image_id'] = '0';
141 $template_variables['image_alt_text'] = '';
143 $template_class = 'admin';
145 } else if (count($path_components) >= 3 && $path_components[1] == 'images' && $path_components[2] == 'update') {
146 $alt_text = cx_form_input_sanitized('image_alt_text');
148 $filename = $_FILES['image_file']['tmp_name'];
149 $original_filename = $_FILES['image_file']['name'];
151 cx_images_add_image(1, $alt_text, $filename, $original_filename);
153 cx_http_redirect(cx_url_admin('/'));
156 $template_class = 'admin';
161 } else if (count($path_components) >= 3) { // FIXME sometime, needs more flexibility...
162 $year = $path_components[0];
163 $month = $path_components[1];
164 $slug = $path_components[2];
167 $template_variables['post_id'] = cx_posts_find_post_id($slug);
170 if ($template != null) {
171 $output = cx_template_render($template_class, $template, $template_variables);
174 http_response_code(404);