source: trunk/bin/_config.inc.php @ 572

Last change on this file since 572 was 572, checked in by anonymous, 7 years ago

Added option to skip using a database by testing for defined(_NODB)

File size: 3.7 KB
Line 
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.
34if (!defined('COMMON_BASE')) {
35    define('COMMON_BASE', realpath('.'));
36}
37
38// Use codebase files relative to this file.
39define('CODEBASE_PATH', realpath(dirname(__FILE__) . '/../'));
40
41
42$db_auth_file = false;
43if (!defined('_NODB')) {
44    $rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(COMMON_BASE));
45    $rii->setMaxDepth(2);
46    foreach ($rii as $filename => $file) {
47        if (in_array(basename($filename), ['db_auth.inc.php', 'db_auth.json'])) {
48            $db_auth_file = $filename;
49            break;
50        }
51    }
52    if (!$db_auth_file) {
53        die(sprintf("%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));
54    }
55    if (fileowner($db_auth_file) != getmyuid()) {
56        die(sprintf("%s error: you must execute this script as the owner of the web files.\n", $cli_executed));
57    }
58}
59
60// Set include path.
61ini_set('include_path', get_include_path() . PATH_SEPARATOR . COMMON_BASE);
62
63/********************************************************************
64* CONFIG
65********************************************************************/
66
67// Include core libraries.
68require_once CODEBASE_PATH . '/lib/App.inc.php';
69require_once CODEBASE_PATH . '/lib/Utilities.inc.php';
70
71define('_CLI', true);
72$app =& App::getInstance('codebase');
73$app->setParam(array(
74    'site_name' => 'codebase',
75    'site_email' => 'codebase@strangecode.com',
76    'enable_session' => false,
77    'enable_db' => defined('_NODB') ? false : true,
78    'db_create_tables' => true,
79    'db_auth_file' => $db_auth_file,
80    'db_always_debug' => false,
81    'db_debug' => true,
82    'db_die_on_failure' => true,
83    'display_errors' => true,
84    'error_reporting' => E_ALL,
85    'log_file_priority' => LOG_DEBUG,
86    'log_screen_priority' => LOG_INFO,
87    'log_directory' => (is_dir('./log') ? './log' : '/tmp'), // If ./log exists, use it, otherwise /tmp.
88    'log_filename' => 'site_log',
89));
90
91if (false !== $db_auth_file && strtolower(getFilenameExtension($db_auth_file)) != 'json') {
92    // DB auth file is not JSON, and will not be automatically loaded by $app's 'db_auth_file' parser.
93    require_once $db_auth_file;
94}
95
96// Start application-based functionality: database, session, environment, ini setup, etc.
97// Most configuration parameters must be set before starting the App.
98$app->start();
99
100if (!defined('_NODB')) {
101    // Global DB object. Automatically pre-configured by $app->start().
102    $db =& DB::getInstance();
103}
Note: See TracBrowser for help on using the repository browser.