chevereto-free/lib/G/G.php

155 lines
6.3 KiB
PHP
Raw Normal View History

2016-08-18 20:39:31 +00:00
<?php
/* --------------------------------------------------------------------
G\ library
2019-02-19 16:34:30 +00:00
https://g.chevereto.com
2016-08-18 20:39:31 +00:00
@author Rodolfo Berrios A. <http://rodolfoberrios.com/>
Copyright (c) Rodolfo Berrios <inbox@rodolfoberrios.com> All rights reserved.
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
Licensed under the MIT license
http://opensource.org/licenses/MIT
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
--------------------------------------------------------------------- */
2018-04-17 21:25:26 +00:00
2016-08-18 20:39:31 +00:00
namespace G;
2019-02-19 20:35:48 +00:00
if(!defined('access') or !access) die("This file cannot be directly accessed.");
2016-08-18 20:39:31 +00:00
2018-08-16 18:51:52 +00:00
define('G_VERSION', '1.0.42');
2016-08-18 20:39:31 +00:00
// Error reporting setup
2019-02-19 20:35:48 +00:00
@ini_set('log_errors', TRUE);
2016-08-18 20:39:31 +00:00
error_reporting(E_ALL ^ E_NOTICE);
2018-04-17 21:25:26 +00:00
// Set default locale
setlocale(LC_ALL, 'en_US.UTF8');
2016-08-18 20:39:31 +00:00
2018-04-17 21:25:26 +00:00
// Set encoding to UTF-8
@ini_set('default_charset', 'utf-8');
2016-08-18 20:39:31 +00:00
// Set G\ paths and files
2019-02-19 20:35:48 +00:00
define('G_ROOT_PATH', rtrim(str_replace('\\','/', dirname(dirname(__DIR__))), '/') . '/');
2016-08-18 20:39:31 +00:00
define('G_ROOT_PATH_RELATIVE', rtrim(dirname($_SERVER['SCRIPT_NAME']), '\/') . '/');
define('G_ROOT_LIB_PATH', G_ROOT_PATH . 'lib/');
define('G_PATH', G_ROOT_LIB_PATH . 'G/');
define('G_PATH_CLASSES', G_PATH . 'classes/');
define('G_FILE_FUNCTIONS', G_PATH . 'functions.php');
define('G_FILE_FUNCTIONS_RENDER', G_PATH . 'functions.render.php');
// Set app paths
2018-04-17 21:25:26 +00:00
define('G_APP_PATH', G_ROOT_PATH . 'app/');
2016-08-18 20:39:31 +00:00
define('G_APP_PATH_LIB', G_APP_PATH . 'lib/');
define('G_APP_PATH_ROUTES', G_APP_PATH . 'routes/');
define('G_APP_PATH_ROUTES_OVERRIDES', G_APP_PATH_ROUTES . 'overrides/');
define('G_APP_PATH_CLASSES', G_APP_PATH_LIB . 'classes/');
define('G_APP_FILE_FUNCTIONS', G_APP_PATH_LIB . 'functions.php');
define('G_APP_FILE_FUNCTIONS_RENDER', G_APP_PATH_LIB . 'functions.render.php');
define('G_APP_SETTINGS_FILE_ERROR', '<br />There are errors in the <strong>%%FILE%%</strong> file. Change the encodig to "UTF-8 without BOM" using Notepad++ or any similar code editor and remove any character before <span style="color: red;">&lt;?php</span>');
// Include the static app config file
(file_exists(G_APP_PATH . 'settings.php')) ? require_once(G_APP_PATH . 'settings.php') : die("G\: Can't find app/settings.php");
2019-02-19 20:35:48 +00:00
if(headers_sent()) die(str_replace('%%FILE%%', 'app/settings.php', G_APP_SETTINGS_FILE_ERROR)); // Stop on premature headers
2016-08-18 20:39:31 +00:00
2018-04-17 21:25:26 +00:00
// TZ failover
$tz = @date_default_timezone_get();
$dtz = @date_default_timezone_set($tz);
2019-02-19 20:35:48 +00:00
if(!$dtz && !@date_default_timezone_set('America/Santiago')) {
die(strtr('Invalid timezone identifier: %i. Configure php.ini with a valid timezone identifier %l', ['%i' => $tz, '%l' => 'http://php.net/manual/en/timezones.php']));
2018-04-17 21:25:26 +00:00
}
// Session hack
2019-02-19 20:35:48 +00:00
if($settings['session.save_path']) {
session_save_path($settings['session.save_path']);
2018-04-17 21:25:26 +00:00
}
// Can work with sessions?
2019-02-19 20:35:48 +00:00
if(!@session_start()) die("G\: Sessions are not working on this server (session_start).");
2018-04-17 21:25:26 +00:00
// Is session save path OK? (you won't believe how many people has session issues!)
2018-08-16 18:51:52 +00:00
$session_save_path = @realpath(session_save_path());
2019-02-19 20:35:48 +00:00
if($session_save_path) { // realpath on this needs pre-webroot directories access
foreach(['write'] as $k) {
$fn = 'is_' . $k . 'able';
if(!$fn($session_save_path)) $session_errors[] = $k;
}
if(isset($session_errors)) die(strtr("G\: Sessions are not working on this server due to missing %s permission on session save path (%f session.save_path).", ['%s' => implode('/', $session_errors), '%f' => $settings['session.save_path'] ? 'app/settings.php' : 'php.ini']));
2018-04-17 21:25:26 +00:00
}
// Are sessions working properly?
2019-02-19 20:35:48 +00:00
$_SESSION['G'] = TRUE;
if(!$_SESSION['G']) die("G\: Sessions are not working properly. Check for any conflicting server setting.");
2018-04-17 21:25:26 +00:00
// Set the starting execution time
define('G_APP_TIME_EXECUTION_START', microtime(true));
// Include G\ core functions
(file_exists(__DIR__ . '/functions.php')) ? require_once(__DIR__ . '/functions.php') : die("G\: Can't find <strong>" . __DIR__ . '/functions.php' . '</strong>. Make sure that this file exists.');
2019-02-19 20:35:48 +00:00
if(file_exists(__DIR__ . '/functions.render.php')) {
require_once(__DIR__ . '/functions.render.php');
2018-04-17 21:25:26 +00:00
}
2019-02-19 20:35:48 +00:00
if(isset($settings) && $settings['error_reporting'] === false) {
error_reporting(0);
2016-08-18 20:39:31 +00:00
}
// Set the default timezone
2019-02-19 20:35:48 +00:00
if(isset($settings['default_timezone']) && is_valid_timezone($settings['default_timezone'])) {
if(!@date_default_timezone_set($settings['default_timezone'])) {
die(strtr("G\: Can't set %s timezone on line %l", ['%s' => $settings['default_timezone'], '%l' => __LINE__ - 1]));
}
2016-08-18 20:39:31 +00:00
}
// Set the system environment
2019-02-19 20:35:48 +00:00
if(isset($settings['environment'])) {
define('G_APP_ENV', $settings['environment']);
2016-08-18 20:39:31 +00:00
}
// Set the HTTP definitions
define('G_HTTP_HOST', $_SERVER['HTTP_HOST']);
2019-02-19 20:35:48 +00:00
define('G_HTTP_PROTOCOL', 'http' . ((((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) || $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') || $settings['https']) ? 's' : NULL));
2018-08-16 18:51:52 +00:00
// La cumbia me divierte y mesita
2016-08-18 20:39:31 +00:00
// Fix some $_SERVER vars
$_SERVER['SCRIPT_FILENAME'] = forward_slash($_SERVER['SCRIPT_FILENAME']);
2018-04-17 21:25:26 +00:00
$_SERVER['SCRIPT_NAME'] = forward_slash($_SERVER['SCRIPT_NAME']);
2016-08-18 20:39:31 +00:00
// Fix CloudFlare REMOTE_ADDR
2019-02-19 20:35:48 +00:00
if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
2016-08-18 20:39:31 +00:00
}
// Inherit application definitions
2019-02-19 20:35:48 +00:00
if(file_exists(G_APP_PATH . 'app.php')) {
require_once(G_APP_PATH . 'app.php');
2016-08-18 20:39:31 +00:00
}
// Set the DB constants
2019-02-19 20:35:48 +00:00
foreach(['host', 'port', 'name', 'user', 'pass', 'driver', 'pdo_attrs'] as $k) {
define('G_APP_DB_' . strtoupper($k), isset($settings['db_' . $k]) ? (is_array($settings['db_' . $k]) ? serialize($settings['db_' . $k]) : $settings['db_' . $k]) : NULL);
2016-08-18 20:39:31 +00:00
}
// Include app functions
(file_exists(G_APP_FILE_FUNCTIONS)) ? require_once(G_APP_FILE_FUNCTIONS) : die("G\: Can't find <strong>" . G_APP_FILE_FUNCTIONS . '</strong>. Make sure that this file exists.');
2019-02-19 20:35:48 +00:00
if(file_exists(G_APP_FILE_FUNCTIONS_RENDER)) {
require_once(G_APP_FILE_FUNCTIONS_RENDER);
2016-08-18 20:39:31 +00:00
}
// Set the URLs
define("G_ROOT_URL", G_HTTP_PROTOCOL . "://".G_HTTP_HOST . G_ROOT_PATH_RELATIVE); // http(s)://www.mysite.com/chevereto/
define("G_ROOT_LIB_URL", absolute_to_url(G_ROOT_LIB_PATH));
define("G_APP_LIB_URL", absolute_to_url(G_APP_PATH_LIB));
// Define the app theme
define('G_APP_PATH_THEMES', G_APP_PATH . 'themes/');
2019-02-19 20:35:48 +00:00
if(!file_exists(G_APP_PATH_THEMES)) {
die("G\: Theme path doesn't exists!");
2016-08-18 20:39:31 +00:00
}
2019-02-19 20:35:48 +00:00
if(isset($settings['theme']) and file_exists(G_APP_PATH_THEMES . $settings['theme'])) {
define('G_APP_PATH_THEME', G_APP_PATH_THEMES . $settings['theme'].'/');
define('BASE_URL_THEME', absolute_to_url(G_APP_PATH_THEME));
}