[532] | 1 | #!/usr/bin/env php |
---|
| 2 | <?php |
---|
| 3 | /* |
---|
| 4 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 5 | * For details visit the project site: <http://trac.strangecode.com/> |
---|
| 6 | * Copyright © 2015 Strangecode, LLC |
---|
| 7 | * |
---|
| 8 | * This program is free software: you can redistribute it and/or modify |
---|
| 9 | * it under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 11 | * (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | /* |
---|
| 23 | * Initialize tables managed by Codebase classes. |
---|
| 24 | * |
---|
| 25 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
[573] | 26 | * @version 2.0 |
---|
| 27 | * @since 05 Mar 2016 19:12:09 |
---|
[532] | 28 | */ |
---|
| 29 | |
---|
| 30 | /******************************************************************** |
---|
| 31 | * MAIN |
---|
| 32 | ********************************************************************/ |
---|
| 33 | |
---|
| 34 | // Process command line options. |
---|
[573] | 35 | $opt = getopt('fhc:'); |
---|
[532] | 36 | |
---|
[573] | 37 | // Get name of class to initialize (or 'all' for all). |
---|
[532] | 38 | $class = strtolower(end($_SERVER['argv'])); |
---|
| 39 | |
---|
| 40 | // Give them a fighting chance. Show the help message. |
---|
| 41 | if ($_SERVER['argc'] <= 1 || isset($opt['h'])) { |
---|
| 42 | help(); |
---|
| 43 | exit(1); |
---|
| 44 | } |
---|
| 45 | |
---|
[573] | 46 | if (isset($opt['c']) && is_file($opt['c'])) { |
---|
| 47 | require_once $opt['c']; |
---|
| 48 | $app->logMsg(sprintf('Loaded custom config file: %s', $opt['c']), LOG_INFO, __FILE__, __LINE__); |
---|
| 49 | } |
---|
| 50 | require_once dirname(__FILE__) . '/_config.inc.php'; |
---|
[533] | 51 | |
---|
[532] | 52 | if ('all' == $class) { |
---|
| 53 | initClassDB('acl'); |
---|
| 54 | initClassDB('auth'); |
---|
| 55 | initClassDB('lock'); |
---|
| 56 | initClassDB('prefs'); |
---|
| 57 | initClassDB('version'); |
---|
[573] | 58 | initClassDB('session'); |
---|
[532] | 59 | } else { |
---|
| 60 | initClassDB($class); |
---|
| 61 | } |
---|
| 62 | |
---|
[573] | 63 | $app->stop(); |
---|
| 64 | die; |
---|
[532] | 65 | |
---|
| 66 | /******************************************************************** |
---|
| 67 | * FUNCTIONS |
---|
| 68 | ********************************************************************/ |
---|
| 69 | |
---|
| 70 | /* |
---|
| 71 | * |
---|
| 72 | * |
---|
| 73 | * @access public |
---|
| 74 | * @param |
---|
| 75 | * @return |
---|
| 76 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 77 | * @version 1.0 |
---|
| 78 | * @since 12 Jul 2015 23:16:02 |
---|
| 79 | */ |
---|
| 80 | function help() |
---|
| 81 | { |
---|
[533] | 82 | global $cli_executed; |
---|
[532] | 83 | ?> |
---|
[533] | 84 | This script initializes DB tables managed by Codebase classes. If database schema is changed a log message will display. |
---|
[532] | 85 | |
---|
[533] | 86 | Usage: <?php echo $cli_executed; ?> [OPTIONS] CLASSNAME |
---|
[532] | 87 | |
---|
| 88 | OPTIONS |
---|
| 89 | |
---|
[573] | 90 | -c FILE Path to include custom site _config.inc.php file. |
---|
| 91 | -f Force recreation of tables, if they already exist. WARNING: this will delete existing records. |
---|
| 92 | -h Show this help message. |
---|
[532] | 93 | |
---|
| 94 | CLASSNAME is one of: |
---|
| 95 | |
---|
| 96 | all Special case: create tables for all classes. |
---|
| 97 | acl Create tables for ACL |
---|
| 98 | auth Create tables for Auth_SQL |
---|
| 99 | lock Create tables for Lock |
---|
| 100 | prefs Create tables for Prefs |
---|
| 101 | version Create tables for Version |
---|
[573] | 102 | session Create tables for DBSessionHandler |
---|
[532] | 103 | <?php |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /* |
---|
| 107 | * |
---|
| 108 | * |
---|
| 109 | * @access public |
---|
| 110 | * @param |
---|
| 111 | * @return |
---|
| 112 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 113 | * @version 1.0 |
---|
| 114 | * @since 12 Jul 2015 23:25:35 |
---|
| 115 | */ |
---|
| 116 | function initClassDB($class) |
---|
| 117 | { |
---|
| 118 | global $opt; |
---|
[573] | 119 | $app =& App::getInstance(); |
---|
| 120 | $db =& DB::getInstance(); |
---|
[532] | 121 | |
---|
[573] | 122 | $app->logMsg(sprintf('Running %s->initDB() on database %s', $class, $db->getParam('db_name')), LOG_INFO, __FILE__, __LINE__); |
---|
| 123 | |
---|
[532] | 124 | switch ($class) { |
---|
| 125 | case 'acl': |
---|
| 126 | // ACL! |
---|
[573] | 127 | require_once CODEBASE_PATH . '/lib/ACL.inc.php'; |
---|
[532] | 128 | $acl =& ACL::getInstance(); |
---|
| 129 | $acl->setParam(array('create_table' => true)); |
---|
| 130 | $acl->initDB(isset($opt['f'])); |
---|
| 131 | break; |
---|
| 132 | |
---|
| 133 | case 'auth': |
---|
| 134 | // Auth_SQL! |
---|
[573] | 135 | require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php'; |
---|
[532] | 136 | $auth = new Auth_SQL('codebase'); |
---|
[573] | 137 | $auth->setParam(array('create_table' => true)); |
---|
| 138 | if (!isset($opt['c'])) { |
---|
| 139 | // User didn't provide custom config. Use sane defaults. |
---|
| 140 | $auth->setParam(array('abuse_detection' => true)); |
---|
| 141 | } |
---|
[532] | 142 | $auth->initDB(isset($opt['f'])); |
---|
| 143 | break; |
---|
| 144 | |
---|
| 145 | case 'lock': |
---|
| 146 | // Lock! |
---|
[573] | 147 | require_once CODEBASE_PATH . '/lib/Lock.inc.php'; |
---|
[601] | 148 | require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php'; |
---|
[532] | 149 | $lock =& Lock::getInstance(new Auth_SQL('codebase')); |
---|
| 150 | $lock->setParam(array('create_table' => true)); |
---|
| 151 | $lock->initDB(isset($opt['f'])); |
---|
| 152 | break; |
---|
| 153 | |
---|
| 154 | case 'prefs': |
---|
| 155 | // Prefs! |
---|
[573] | 156 | require_once CODEBASE_PATH . '/lib/Prefs.inc.php'; |
---|
[532] | 157 | $prefs = new Prefs('codebase'); |
---|
| 158 | $prefs->setParam(array('create_table' => true)); |
---|
| 159 | $prefs->initDB(isset($opt['f'])); |
---|
| 160 | break; |
---|
| 161 | |
---|
| 162 | case 'version': |
---|
| 163 | // Version! |
---|
[573] | 164 | require_once CODEBASE_PATH . '/lib/Version.inc.php'; |
---|
[601] | 165 | require_once CODEBASE_PATH . '/lib/Auth_SQL.inc.php'; |
---|
[532] | 166 | $version = Version::getInstance(new Auth_SQL('codebase')); |
---|
| 167 | $version->setParam(array('create_table' => true)); |
---|
| 168 | $version->initDB(isset($opt['f'])); |
---|
| 169 | break; |
---|
| 170 | |
---|
[573] | 171 | case 'session': |
---|
| 172 | // DBSessionHandler! |
---|
| 173 | |
---|
| 174 | // We need to hack the app to allow sessions to run in a CLI. |
---|
| 175 | $app->stop(); |
---|
| 176 | $app->cli = false; |
---|
| 177 | $app->setParam(array('enable_session' => true)); |
---|
| 178 | $app->start(); |
---|
| 179 | |
---|
| 180 | if (!isset($opt['c'])) { |
---|
| 181 | // User didn't provide custom config. Use sane defaults. |
---|
| 182 | require_once CODEBASE_PATH . '/lib/DBSessionHandler.inc.php'; |
---|
| 183 | $db_session = new DBSessionHandler($app->db, array( |
---|
| 184 | 'db_table' => 'session_tbl', |
---|
| 185 | 'create_table' => true, |
---|
| 186 | )); |
---|
| 187 | $db_session->initDB(isset($opt['f'])); |
---|
| 188 | } else if (true === $app->getParam('enable_db_session_handler') && true === $app->getParam('enable_db') && isset($app->db_session)) { |
---|
| 189 | // Only init if db_session is enabled in config. |
---|
| 190 | $app->db_session->initDB(isset($opt['f'])); |
---|
| 191 | } |
---|
| 192 | break; |
---|
| 193 | |
---|
[532] | 194 | default: |
---|
[573] | 195 | echo "The class $class is not setup to initClassDB().\n"; |
---|
[532] | 196 | exit(1); |
---|
| 197 | } |
---|
| 198 | } |
---|