#!/usr/bin/env php * Copyright © 2015 Strangecode, LLC * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* * Initialize tables managed by Codebase classes. * * @author Quinn Comendant * @version 2.0 * @since 05 Mar 2016 19:12:09 */ /******************************************************************** * MAIN ********************************************************************/ // Process command line options. $opt = getopt('fhc:'); // Get name of class to initialize (or 'all' for all). $class = strtolower(end($_SERVER['argv'])); // Give them a fighting chance. Show the help message. if ($_SERVER['argc'] <= 1 || isset($opt['h'])) { help(); exit(1); } if (isset($opt['c']) && is_file($opt['c'])) { require_once $opt['c']; $app->logMsg(sprintf('Loaded custom config file: %s', $opt['c']), LOG_INFO, __FILE__, __LINE__); } require_once dirname(__FILE__) . '/_config.inc.php'; if ('all' == $class) { initClassDB('acl'); initClassDB('auth'); initClassDB('lock'); initClassDB('prefs'); initClassDB('version'); initClassDB('session'); } else { initClassDB($class); } $app->stop(); die; /******************************************************************** * FUNCTIONS ********************************************************************/ /* * * * @access public * @param * @return * @author Quinn Comendant * @version 1.0 * @since 12 Jul 2015 23:16:02 */ function help() { global $cli_executed; ?> This script initializes DB tables managed by Codebase classes. If database schema is changed a log message will display. Usage: [OPTIONS] CLASSNAME OPTIONS -c FILE Path to include custom site _config.inc.php file. -f Force recreation of tables, if they already exist. WARNING: this will delete existing records. -h Show this help message. CLASSNAME is one of: all Special case: create tables for all classes. acl Create tables for ACL auth Create tables for Auth_SQL lock Create tables for Lock prefs Create tables for Prefs version Create tables for Version session Create tables for DBSessionHandler * @version 1.0 * @since 12 Jul 2015 23:25:35 */ function initClassDB($class) { global $opt; $app =& App::getInstance(); $db =& DB::getInstance(); $app->logMsg(sprintf('Running %s->initDB() on database %s', $class, $db->getParam('db_name')), LOG_INFO, __FILE__, __LINE__); switch ($class) { case 'acl': // ACL! require_once CODEBASE_PATH . '/lib/ACL.inc.php'; $acl =& ACL::getInstance(); $acl->setParam(array('create_table' => true)); $acl->initDB(isset($opt['f'])); break; case 'auth': // Auth_SQL! require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php'; $auth = new Auth_SQL('codebase'); $auth->setParam(array('create_table' => true)); if (!isset($opt['c'])) { // User didn't provide custom config. Use sane defaults. $auth->setParam(array('abuse_detection' => true)); } $auth->initDB(isset($opt['f'])); break; case 'lock': // Lock! require_once CODEBASE_PATH . '/lib/Lock.inc.php'; require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php'; $lock =& Lock::getInstance(new Auth_SQL('codebase')); $lock->setParam(array('create_table' => true)); $lock->initDB(isset($opt['f'])); break; case 'prefs': // Prefs! require_once CODEBASE_PATH . '/lib/Prefs.inc.php'; $prefs = new Prefs('codebase'); $prefs->setParam(array('create_table' => true)); $prefs->initDB(isset($opt['f'])); break; case 'version': // Version! require_once CODEBASE_PATH . '/lib/Version.inc.php'; require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php'; $version = Version::getInstance(new Auth_SQL('codebase')); $version->setParam(array('create_table' => true)); $version->initDB(isset($opt['f'])); break; case 'session': // DBSessionHandler! if (version_compare(PHP_VERSION, '7.0.0', '<')) { // We need to hack the app to allow sessions to run in a CLI. $app->stop(); $app->cli = false; $app->setParam(array('enable_session' => true)); $app->start(); } if (!isset($opt['c'])) { // User didn't provide custom config. Use sane defaults. require_once CODEBASE_PATH . '/lib/DBSessionHandler.inc.php'; // Creating a session here causes a “Session save handler cannot be changed after headers have already been sent” warning. This can be ignored. $db_session = new DBSessionHandler($app->db, array( 'db_table' => 'session_tbl', 'create_table' => true, )); $db_session->initDB(isset($opt['f'])); } else if (true === $app->getParam('enable_db_session_handler') && true === $app->getParam('enable_db') && isset($app->db_session)) { // Only init if db_session is enabled in config. $app->db_session->initDB(isset($opt['f'])); } break; default: echo "The class $class is not setup to initClassDB().\n"; exit(1); } }