source: trunk/bin/_config.inc.php

Last change on this file was 766, checked in by anonymous, 2 years ago

Only source local codebase if version is > 2.1.0

File size: 4.0 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_name = 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$db_auth_file = false;
42if (!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_name);
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_name);
57        exit(1);
58    }
59}
60
61// Set include path.
62ini_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.
73if (file_exists('codebase/docs/version.txt') && version_compare(file_get_contents('codebase/docs/version.txt'), '2.1.0', '>')) {
74    require_once 'codebase/lib/App.inc.php';
75} else {
76    require_once CODEBASE_PATH . '/lib/App.inc.php';
77}
78
79define('_CLI', true);
80$app =& App::getInstance('codebase');
81$app->setParam(array(
82    'site_name' => 'codebase',
83    'site_email' => 'codebase@strangecode.com',
84    'enable_session' => false,
85    'enable_db' => defined('_NODB') ? false : true,
86    'db_create_tables' => true,
87    'db_auth_file' => $db_auth_file,
88    'db_always_debug' => false,
89    'db_debug' => true,
90    'db_die_on_failure' => true,
91    'display_errors' => true,
92    'error_reporting' => E_ALL,
93    'log_file_priority' => LOG_DEBUG,
94    'log_screen_priority' => LOG_INFO,
95    'log_directory' => (is_dir('./log') ? './log' : '/tmp'), // If ./log exists, use it, otherwise /tmp.
96    'log_filename' => 'site_log',
97));
98
99if (false !== $db_auth_file && strtolower(getFilenameExtension($db_auth_file)) != 'json') {
100    // DB auth file is not JSON, and will not be automatically loaded by $app's 'db_auth_file' parser.
101    require_once $db_auth_file;
102}
103
104// Start application-based functionality: database, session, environment, ini setup, etc.
105// Most configuration parameters must be set before starting the App.
106$app->start();
107
108if (!defined('_NODB')) {
109    // Global DB object. Automatically pre-configured by $app->start().
110    $db =& DB::getInstance();
111}
Note: See TracBrowser for help on using the repository browser.