create.php 18.9 KB
Newer Older
1 2
<?php
// Dobrado Content Management System
3
// Copyright (C) 2018 Malcolm Blaney
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

function create_tables() {
  $mysqli = connect_db();

21 22 23 24 25 26 27 28 29 30 31
  $query = 'CREATE TABLE IF NOT EXISTS users (' .
    'user VARCHAR(50) NOT NULL,' .
    'password VARCHAR(123) NOT NULL,' .
    'email VARCHAR(100),' .
    'system_group VARCHAR(50),' .
    'confirmed TINYINT(1),' .
    'confirm_id VARCHAR(32),' .
    'registration_time INT(10) UNSIGNED,' .
    'from_guest VARCHAR(20),' .
    'active TINYINT(1),' .
    'PRIMARY KEY(user)' .
32 33
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
34
    error_log('create users: ' . $mysqli->error);
35 36
  }

37 38 39 40 41 42 43 44 45 46 47
  $query = 'CREATE TABLE IF NOT EXISTS modules (' .
    'user VARCHAR(50) NOT NULL,' .
    'page VARCHAR(200) NOT NULL,' .
    'box_id INT UNSIGNED NOT NULL AUTO_INCREMENT,' .
    'label VARCHAR(50) NOT NULL,' .
    'class TEXT,' .
    'box_order INT UNSIGNED,' .
    'placement ENUM("header", "left", "middle", ' .
      '"right", "footer", "outside") NOT NULL,' .
    'deleted TINYINT(1),' .
    'PRIMARY KEY(user, box_id, page(50))' .
48 49
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
50
    error_log('create modules: ' . $mysqli->error);
51 52
  }

53 54 55 56 57 58 59 60 61 62 63 64 65
  $query = 'CREATE TABLE IF NOT EXISTS modules_history (' .
    'user VARCHAR(50) NOT NULL,' .
    'page VARCHAR(200) NOT NULL,' .
    'box_id INT UNSIGNED NOT NULL AUTO_INCREMENT,' .
    'label VARCHAR(50) NOT NULL,' .
    'class TEXT,' .
    'box_order INT UNSIGNED,' .
    'placement ENUM("header", "left", "middle", ' .
      '"right", "footer", "outside") NOT NULL,' .
    'action ENUM("add", "delete", "move") NOT NULL,' .
    'modified_by VARCHAR(50),' .
    'timestamp INT(10) UNSIGNED NOT NULL,' .
    'PRIMARY KEY(user, box_id, page(50), timestamp)' .
66 67
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
68
    error_log('create modules_history: ' . $mysqli->error);
69 70
  }

71 72 73 74 75 76 77
  $query = 'CREATE TABLE IF NOT EXISTS box_style (' .
    'user VARCHAR(50) NOT NULL,' .
    'media VARCHAR(200) NOT NULL,' .
    'selector VARCHAR(200) NOT NULL,' .
    'property VARCHAR(200) NOT NULL,' .
    'value TEXT,' .
    'PRIMARY KEY(user, media(50), selector(50), property(50))' .
78 79
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
80
    error_log('create box_style: ' . $mysqli->error);
81 82
  }

83 84 85 86 87 88 89 90
  $query = 'CREATE TABLE IF NOT EXISTS page_style (' .
    'user VARCHAR(50) NOT NULL,' .
    'name VARCHAR(200) NOT NULL,' .
    'media VARCHAR(200) NOT NULL,' .
    'selector VARCHAR(200) NOT NULL,' .
    'property VARCHAR(200) NOT NULL,' .
    'value TEXT,' .
    'PRIMARY KEY(user, name(50), media(50), selector(50), property(50))' .
91 92
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
93
    error_log('create page_style: ' . $mysqli->error);
94 95
  }

96 97 98 99 100 101
  $query = 'CREATE TABLE IF NOT EXISTS site_style (' .
    'user VARCHAR(50) NOT NULL,' .
    'media VARCHAR(200) NOT NULL,' .
    'selector VARCHAR(200) NOT NULL,' .
    'property VARCHAR(200) NOT NULL,' .
    'value TEXT,' .
102
    'PRIMARY KEY(media(50), selector(100), property(50))' .
103 104
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
105
    error_log('create site_style: ' . $mysqli->error);
106 107
  }

108 109 110 111 112 113 114 115
  $query = 'CREATE TABLE IF NOT EXISTS installed_modules (' .
    'user VARCHAR(50) NOT NULL,' .
    'label VARCHAR(50) NOT NULL,' .
    'version VARCHAR(50),' .
    'title VARCHAR(50) NOT NULL,' .
    'description TEXT,' .
    'display TINYINT(1),' .
    'PRIMARY KEY(label)' .
116 117
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
118
    error_log('create installed_modules: ' . $mysqli->error);
119 120
  }

121 122 123 124 125 126 127 128
  $query = 'CREATE TABLE IF NOT EXISTS user_permission (' .
    'user VARCHAR(50) NOT NULL,' .
    'page VARCHAR(200) NOT NULL,' .
    'visitor VARCHAR(50) NOT NULL,' .
    'edit TINYINT(1),' .
    'copy TINYINT(1),' .
    'view TINYINT(1),' .
    'PRIMARY KEY(user, page(50), visitor)' .
129 130
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
131
    error_log('create user_permission: ' . $mysqli->error);
132 133
  }

134 135 136 137 138 139 140 141
  $query = 'CREATE TABLE IF NOT EXISTS group_permission (' .
    'user VARCHAR(50) NOT NULL,' .
    'page VARCHAR(200) NOT NULL,' .
    'name VARCHAR(250) NOT NULL,' .
    'edit TINYINT(1),' .
    'copy TINYINT(1),' .
    'view TINYINT(1),' .
    'PRIMARY KEY(user, page(50), name(50))' .
142 143
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
144
    error_log('create group_permission: ' . $mysqli->error);
145 146
  }

147
  $query = 'CREATE TABLE IF NOT EXISTS group_names (' .
148 149 150 151
    'user VARCHAR(50) NOT NULL,' .
    'name VARCHAR(250) NOT NULL,' .
    'visitor VARCHAR(50) NOT NULL,' .
    'edit_group TINYINT(1),' .
152
    'PRIMARY KEY(user, name(100), visitor)' .
153 154
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
155
    error_log('create group_names: ' . $mysqli->error);
156 157
  }

158 159 160 161 162
  $query = 'CREATE TABLE IF NOT EXISTS published (' .
    'user VARCHAR(50) NOT NULL,' .
    'page VARCHAR(200) NOT NULL,' .
    'published TINYINT(1) NOT NULL,' .
    'PRIMARY KEY(user, page)' .
163 164
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
165
    error_log('create published: ' . $mysqli->error);
166 167
  }

168 169 170 171 172 173 174
  $query = 'CREATE TABLE IF NOT EXISTS api (' .
    'user VARCHAR(50) NOT NULL,' .
    'box_id INT UNSIGNED NOT NULL,' .
    'label VARCHAR(50) NOT NULL,' .
    'feed_id INT UNSIGNED NOT NULL,' .
    'api_key INT UNSIGNED NOT NULL,' .
    'PRIMARY KEY(user, box_id)' .
175 176
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
177
    error_log('create api: ' . $mysqli->error);
178 179
  }

180 181 182 183 184 185 186 187 188 189 190
  $query = 'CREATE TABLE IF NOT EXISTS notify (' .
    'user VARCHAR(50) NOT NULL,' .
    'page VARCHAR(200),' .
    'box_id INT UNSIGNED NOT NULL,' .
    'label VARCHAR(50) NOT NULL,' .
    'action ENUM("add", "comment", "delete", "edit", "feed", "invite", ' .
      '"message", "move", "share", "star", "system", "spark", "tag") ' .
      'NOT NULL,' .
    'timestamp INT(10) UNSIGNED NOT NULL,' .
    'public TINYINT(1),' .
    'PRIMARY KEY(user, box_id, page(50), action)' .
191 192
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
193
    error_log('create notify: ' . $mysqli->error);
194 195
  }

196 197 198 199 200 201 202 203
  $query = 'CREATE TABLE IF NOT EXISTS page_updates (' .
    'user VARCHAR(50) NOT NULL,' .
    'page VARCHAR(200),' .
    'action ENUM("add", "comment", "delete", "edit", "feed", "invite", ' .
      '"message", "move", "share", "star", "system", "spark", "tag") ' .
      'NOT NULL,' .
    'timestamp INT(10) UNSIGNED NOT NULL,' .
    'PRIMARY KEY(user, page(50), action)' .
204 205
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
206
    error_log('create page_updates: ' . $mysqli->error);
207 208
  }

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  $query = 'CREATE TABLE IF NOT EXISTS config (' .
    'user VARCHAR(50) NOT NULL,' .
    'analytics TEXT,' .
    'cron INT UNSIGNED,' .
    'development_mode TINYINT(1),' .
    'fancy_url TINYINT(1),' .
    'guest_allowed TINYINT(1),' .
    'login_page VARCHAR(200),' .
    'max_file_size INT UNSIGNED,' .
    'max_upload INT UNSIGNED,' .
    'permalink_default VARCHAR(200),' .
    'permalink_order INT UNSIGNED,' .
    'private_path VARCHAR(200),' .
    'secure TINYINT(1),' .
    'server_name VARCHAR(200),' .
    'theme VARCHAR(200),' .
    'timezone VARCHAR(50),' .
    'title VARCHAR(200),' .
    'title_includes_page TINYINT(1),' .
    'unavailable VARCHAR(200),' .
    'PRIMARY KEY(user)' .
230 231
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
232
    error_log('create config: ' . $mysqli->error);
233 234
  }

235 236 237 238 239 240
  $query = 'CREATE TABLE IF NOT EXISTS log (' .
    'user VARCHAR(50),' .
    'visitor VARCHAR(50),' .
    'page VARCHAR(200),' .
    'timestamp INT(10) UNSIGNED NOT NULL,' .
    'message TEXT' .
241 242
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
243
    error_log('create log: ' . $mysqli->error);
244 245
  }

246 247 248 249 250
  $query = 'CREATE TABLE IF NOT EXISTS template (' .
    'label VARCHAR(200) NOT NULL,' .
    'system_group VARCHAR(50),' .
    'content TEXT,' .
    'PRIMARY KEY(label, system_group)' .
251 252
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
253
    error_log('create template: ' . $mysqli->error);
254 255
  }

256 257 258 259
  $query = 'CREATE TABLE IF NOT EXISTS template_description (' .
    'label VARCHAR(200) NOT NULL,' .
    'description TEXT,' .
    'PRIMARY KEY(label)' .
260 261
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
262
    error_log('create template_description: ' . $mysqli->error);
263 264
  }

265 266 267 268 269
  $query = 'CREATE TABLE IF NOT EXISTS session (' .
    'user VARCHAR(50) NOT NULL,' .
    'series VARCHAR(32) NOT NULL,' .
    'token VARCHAR(32) NOT NULL,' .
    'PRIMARY KEY(user, series, token)' .
270 271
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
272
    error_log('create session: ' . $mysqli->error);
273
  }
274

275 276 277 278 279 280 281
  $query = 'CREATE TABLE IF NOT EXISTS nickname (' .
    'name VARCHAR(200) NOT NULL,' .
    'url VARCHAR(200) NOT NULL,' .
    'photo VARCHAR(200),' .
    'nickname VARCHAR(200),' .
    'reachable TINYINT(1),' .
    'PRIMARY KEY(url)' .
282 283
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
284
    error_log('create nickname: ' . $mysqli->error);
285
  }
286

287 288 289 290
  $query = 'CREATE TABLE IF NOT EXISTS settings (' .
    'user VARCHAR(50) NOT NULL,' .
    'label VARCHAR(50) NOT NULL,' .
    'name VARCHAR(200) NOT NULL,' .
291
    'value TEXT,' .
292
    'PRIMARY KEY(user, label, name(50))' .
293 294
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
295
    error_log('create settings: ' . $mysqli->error);
296 297
  }

298 299 300 301 302 303 304
  $query = 'CREATE TABLE IF NOT EXISTS setting_types (' .
    'label VARCHAR(50) NOT NULL,' .
    'name VARCHAR(200) NOT NULL,' .
    'value TEXT,' .
    'input_type ENUM("checkbox", "radio", "date", "password", "select", ' .
      '"text", "textarea") NOT NULL,' .
    'input_info TEXT,' .
305
    'PRIMARY KEY(label, name(50))' .
306 307
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
308
    error_log('create setting_types: ' . $mysqli->error);
309 310
  }

311 312
  $query = 'CREATE TABLE IF NOT EXISTS auth_codes (' .
    'me VARCHAR(200) NOT NULL,' .
313
    'code VARCHAR(64) NOT NULL,' .
314 315
    'client_id VARCHAR(200) NOT NULL,' .
    'redirect_uri VARCHAR(200) NOT NULL,' .
316 317
    'scope VARCHAR(100) NOT NULL,' .
    'response_type ENUM("id", "code") NOT NULL,' .
318
    'timestamp INT(10) UNSIGNED NOT NULL,' .
319 320
    'PRIMARY KEY(client_id(100), redirect_uri(100))' .
    ') ENGINE=MyISAM';
321 322 323 324
  if (!$mysqli->query($query)) {
    error_log('create auth_codes: ' . $mysqli->error);
  }

325 326 327 328 329 330 331 332 333 334 335
  $query = 'CREATE TABLE IF NOT EXISTS access_tokens (' .
    'me VARCHAR(200) NOT NULL,' .
    'token VARCHAR(64) NOT NULL,' .
    'client_id VARCHAR(200) NOT NULL,' .
    'scope VARCHAR(100) NOT NULL,' .
    'PRIMARY KEY(token)' .
    ') ENGINE=MyISAM';
  if (!$mysqli->query($query)) {
    error_log('create access_tokens: ' . $mysqli->error);
  }

336
  $mysqli->close();
337
}
338 339 340 341 342 343 344 345 346 347 348

function create_site_style() {
  $mysqli = connect_db();

  // This is a default media query for mobile devices. Note '.dobrado-mobile'
  // visibility is used by javascript to do some dynamic configuration.
  $media = '@media screen and (max-device-width: 480px)';

  // Add some site wide style rules that aren't set by modules.
  $site_style = ['"","body","margin","0"',
                 '"","body","padding","0"',
349 350
                 '"","body","color","#222222"',
                 '"","body","font-family","Arial, sans-serif"',
351 352 353 354
                 '"","iframe","max-width","100%"',
                 '"","img","max-width","100%"',
                 '"","img","height","auto"',
                 '"","img","border-style","none"',
355 356 357 358 359 360 361 362
                 '"","input[type=text]","padding","4px"',
                 '"","input[type=text]","border","1px solid #aaaaaa"',
                 '"","input[type=text]","border-radius","3px"',
                 '"","input[type=text]:focus","border-color","#32a3cf"',
                 '"","input[type=password]","padding","4px"',
                 '"","input[type=password]","border","1px solid #aaaaaa"',
                 '"","input[type=password]","border-radius","3px"',
                 '"","input[type=password]:focus","border-color","#32a3cf"',
363 364 365 366
                 '"","textarea","padding","4px"',
                 '"","textarea","border","1px solid #aaaaaa"',
                 '"","textarea","border-radius","3px"',
                 '"","textarea:focus","border-color","#32a3cf"',
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
                 '"",".form-spacing","margin-top","4px"',
                 '"",".form-spacing","margin-bottom","4px"',
                 '"",".form-spacing","clear","both"',
                 '"",".form-spacing > input[type=text]","height","25px"',
                 '"",".form-spacing > input[type=text]","width","300px"',
                 '"",".form-spacing > input[type=password]","height","25px"',
                 '"",".form-spacing > input[type=password]","width","300px"',
                 '"",".form-spacing > input[type=checkbox]","height","20px"',
                 '"",".form-spacing > input","max-width","100%"',
                 '"",".form-spacing > textarea","height","75px"',
                 '"",".form-spacing > textarea","width","300px"',
                 '"",".form-spacing > textarea","max-width","100%"',
                 '"",".form-spacing > select","height","30px"',
                 '"",".form-spacing > label","float","left"',
                 '"",".form-spacing > label","margin-top","0.3em"',
                 '"",".form-spacing > label","margin-right","0.3em"',
                 '"",".form-spacing > label","text-align","right"',
                 '"",".hidden","display","none"',
                 '"",".show","display","block"',
                 '"",".main > .header","min-height","100px"',
                 '"",".main > .left","position","relative"',
                 '"",".main > .left","left","0"',
                 '"",".main > .left","top","0"',
                 '"",".main > .left","width","0"',
                 '"",".main > .left","float","left"',
                 '"",".main > .left","min-height","100px"',
                 '"",".main > .right","position","relative"',
                 '"",".main > .right","right","0"',
                 '"",".main > .right","top","0"',
                 '"",".main > .right","width","0"',
                 '"",".main > .right","float","right"',
                 '"",".main > .right","min-height","100px"',
                 '"",".main > .middle","position","relative"',
                 '"",".main > .middle","width","760px"',
                 '"",".main > .middle","margin-left","auto"',
                 '"",".main > .middle","margin-right","auto"',
                 '"",".main > .middle","min-height","350px"',
                 '"",".main > .footer","clear","both"',
                 '"",".main > .footer","min-height","100px"',
                 '"",".dobrado-editable","min-height","100px"',
                 '"",".dobrado-editable.border","border","1px dashed #828282"',
                 '"",".cke_skin_kama a.cke_button_extended .cke_icon",' .
                   '"background-image","url(\"/images/extended.png\")"',
                 '"",".cke_skin_kama a.cke_button_extended .cke_icon",' .
                   '"background-position","center"',
412 413 414
                 '"",".photo-hidden img","display","none"',
                 '"",".photo-list img","width","45%"',
                 '"",".photo-list img","margin-right","10px"',
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
                 '"",".ui-sortable-placeholder","height","50px"',
                 '"",".ui-sortable-placeholder","border",' .
                   '"4px dashed #828282"',
                 '"",".ui-layout-resizer","background-color","#dddddd"',
                 '"",".ui-dialog","box-shadow",' .
                   '"20px 20px 10px rgba(0, 0, 0, 0.3)"',
                 '"",".ui-dialog","max-width","100%"',
                 '"",".no-close .ui-dialog-titlebar-close","display","none"',
                 '"",".ui-button-text-only .ui-button-text",' .
                   '"padding","0.1em 0.6em"',
                 '"",".ui-autocomplete","max-height","300px"',
                 '"",".ui-autocomplete","max-width","200px"',
                 '"",".ui-autocomplete","overflow-y","auto"',
                 '"",".ui-autocomplete","overflow-x","hidden"',
                 '"",".control .ui-button-icon-only","height","28px"',
                 '"",".control .ui-button-icon-only","width","28px"',
                 '"",".ui-tabs","padding","0"',
                 '"",".ui-tabs-nav li a","padding","0.3em 0.6em"',
                 '"",".ui-accordion-header a","padding","0.3em"',
                 '"",".ui-dialog-content","padding","0.5em 0"',
435
                 '"",".ui-spinner input[type=text]","border","none"',
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
                 '"",".ui-selectmenu-menu","position","fixed"',
                 '"",".ui-menu .ui-state-active","font-weight","normal"',
                 '"",".horizontal-menu:after","clear","both"',
                 '"",".horizontal-menu:after","content","\".\""',
                 '"",".horizontal-menu:after","display","block"',
                 '"",".horizontal-menu:after","height","0"',
                 '"",".horizontal-menu:after","line-height","0"',
                 '"",".horizontal-menu:after","visibility","hidden"',
                 '"",".horizontal-menu > .ui-menu-item","display",' .
                   '"inline-block"',
                 '"",".horizontal-menu > .ui-menu-item","float","left"',
                 '"",".horizontal-menu > .ui-menu-item","margin","0 15px"',
                 '"",".horizontal-menu > .ui-menu-item","padding","0"',
                 '"",".horizontal-menu > .ui-menu-item","width","auto"',
                 '"",".horizontal-menu > .ui-menu-item .ui-icon",' .
                   '"margin-top","0.3em"',
                 '"","indie-action","display","none"',
                 '"","indie-action","white-space","nowrap"',
                 '"","indie-action .ui-icon","display","inline-block"',
                 '"","indie-action .ui-icon","height","14px"',
456 457
                 '"","#indie-action-handler","width","220px"',
                 '"","#indie-action-submit","margin-left","10px"',
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
                 '"","#indie-action-twitter","margin-left","10px"',
                 '"","#indie-action-facebook","margin-left","10px"',
                 '"",".indie-config .ui-icon","display","inline-block"',
                 '"",".indie-config .ui-icon","height","14px"',
                 '"",".indie-config","white-space","nowrap"',
                 '"",".indie-config-settings","margin-left","10px"',
                 '"","#indie-config-ok","float","right"',
                 '"",".dobrado-mobile","display","none"',
                 '"' . $media . '",".dobrado-mobile","display","block"'];
  $values = '';
  foreach ($site_style as $style) {
    if ($values !== '') $values .= ',';
    $values .= '("admin",' . $style . ')';
  }
  $query = 'INSERT INTO site_style VALUES ' . $values;
  if (!$mysqli->query($query)) {
    log_db('start insert site_style: ' . $mysqli->error);
  }
  $mysqli->close();
}