1 | <?php |
---|
2 | /* |
---|
3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
4 | * For details visit the project site: <http://trac.strangecode.com/> |
---|
5 | * Copyright © 2015 Strangecode, LLC |
---|
6 | * |
---|
7 | * This program is free software: you can redistribute it and/or modify |
---|
8 | * it under the terms of the GNU General Public License as published by |
---|
9 | * the Free Software Foundation, either version 3 of the License, or |
---|
10 | * (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | */ |
---|
20 | |
---|
21 | /* |
---|
22 | * Small config file for cli scripts in the codebase/bin directory. |
---|
23 | * |
---|
24 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
25 | * @version 1.0 |
---|
26 | * @since 12 Jul 2015 22:48:13 |
---|
27 | */ |
---|
28 | |
---|
29 | // The name of the CLI. |
---|
30 | $cli_executed = basename($_SERVER['argv'][0]); |
---|
31 | |
---|
32 | // Enforce "relaxed" directory location, if set by the called script. |
---|
33 | // This script can be executed in any directory that is an ancestor 2 directories above the db_auth file. |
---|
34 | if (!defined('COMMON_BASE')) { |
---|
35 | define('COMMON_BASE', realpath('.')); |
---|
36 | } |
---|
37 | |
---|
38 | // Use codebase files relative to this file. |
---|
39 | define('CODEBASE_PATH', realpath(dirname(__FILE__) . '/../')); |
---|
40 | |
---|
41 | $db_auth_file = false; |
---|
42 | if (!defined('_NODB')) { |
---|
43 | $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(COMMON_BASE)); |
---|
44 | $rii->setMaxDepth(2); |
---|
45 | foreach ($rii as $filename => $file) { |
---|
46 | if (in_array(basename($filename), ['db_auth.inc.php', 'db_auth.json'])) { |
---|
47 | $db_auth_file = $filename; |
---|
48 | break; |
---|
49 | } |
---|
50 | } |
---|
51 | if (!$db_auth_file) { |
---|
52 | printf("%s error: the current directory must be common site directory (i.e. the parent directory of the document root) AND a db_auth file must exist within two directory levels.\n", $cli_executed); |
---|
53 | exit(1); |
---|
54 | } |
---|
55 | if (fileowner($db_auth_file) != getmyuid()) { |
---|
56 | printf("%s error: you must execute this script as the owner of the web files.\n", $cli_executed); |
---|
57 | exit(1); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | // Set include path. |
---|
62 | ini_set('include_path', join(PATH_SEPARATOR, array_unique(array( |
---|
63 | '.', |
---|
64 | COMMON_BASE, |
---|
65 | dirname($db_auth_file) |
---|
66 | )))); |
---|
67 | |
---|
68 | /******************************************************************** |
---|
69 | * CONFIG |
---|
70 | ********************************************************************/ |
---|
71 | |
---|
72 | // Include core libraries: try local codebase path first, otherwise use global. |
---|
73 | (@include_once 'codebase/lib/App.inc.php') || require_once CODEBASE_PATH . '/lib/App.inc.php'; |
---|
74 | |
---|
75 | define('_CLI', true); |
---|
76 | $app =& App::getInstance('codebase'); |
---|
77 | $app->setParam(array( |
---|
78 | 'site_name' => 'codebase', |
---|
79 | 'site_email' => 'codebase@strangecode.com', |
---|
80 | 'enable_session' => false, |
---|
81 | 'enable_db' => defined('_NODB') ? false : true, |
---|
82 | 'db_create_tables' => true, |
---|
83 | 'db_auth_file' => $db_auth_file, |
---|
84 | 'db_always_debug' => false, |
---|
85 | 'db_debug' => true, |
---|
86 | 'db_die_on_failure' => true, |
---|
87 | 'display_errors' => true, |
---|
88 | 'error_reporting' => E_ALL, |
---|
89 | 'log_file_priority' => LOG_DEBUG, |
---|
90 | 'log_screen_priority' => LOG_INFO, |
---|
91 | 'log_directory' => (is_dir('./log') ? './log' : '/tmp'), // If ./log exists, use it, otherwise /tmp. |
---|
92 | 'log_filename' => 'site_log', |
---|
93 | )); |
---|
94 | |
---|
95 | if (false !== $db_auth_file && strtolower(getFilenameExtension($db_auth_file)) != 'json') { |
---|
96 | // DB auth file is not JSON, and will not be automatically loaded by $app's 'db_auth_file' parser. |
---|
97 | require_once $db_auth_file; |
---|
98 | } |
---|
99 | |
---|
100 | // Start application-based functionality: database, session, environment, ini setup, etc. |
---|
101 | // Most configuration parameters must be set before starting the App. |
---|
102 | $app->start(); |
---|
103 | |
---|
104 | if (!defined('_NODB')) { |
---|
105 | // Global DB object. Automatically pre-configured by $app->start(). |
---|
106 | $db =& DB::getInstance(); |
---|
107 | } |
---|