2020-06-29 16:59:37 +02:00
< ? php
ini_set ( 'display_errors' , 1 );
ini_set ( 'display_startup_errors' , 1 );
error_reporting ( E_ALL );
class Initialize {
/**
* A list of classes that are always included .
*
* @ since $ { current_version }
* @ return object A single instance of this class .
*/
const BASES = array (
" authentication " ,
" common " ,
" data " ,
" events " ,
2020-08-20 04:14:12 +02:00
" options " ,
" permissions " ,
2020-06-29 16:59:37 +02:00
" update " ,
);
2020-07-24 17:40:00 +02:00
const EXTENSIONS = array (
" curl " ,
" json " ,
" mbstring " ,
);
2020-06-29 16:59:37 +02:00
const PATHS = array (
2020-07-24 17:40:00 +02:00
" BASE_PATH " ,
2020-06-29 16:59:37 +02:00
" COMPONENTS " ,
" DATA " ,
" PLUGINS " ,
" SESSIONS_PATH " ,
" THEMES " ,
" UPLOAD_CACHE " ,
2020-07-24 17:40:00 +02:00
" WORKSPACE " ,
2020-06-29 16:59:37 +02:00
);
2020-07-24 17:40:00 +02:00
2020-06-29 16:59:37 +02:00
protected static $instance = null ;
function __construct () {
2020-07-13 06:21:06 +02:00
$config = realpath ( dirname ( __FILE__ ) . " /../../config.php " );
2020-07-24 17:40:00 +02:00
$installing = self :: is_installing ();
2020-06-29 16:59:37 +02:00
2020-07-24 17:40:00 +02:00
if ( ! $installing ) {
2020-06-29 16:59:37 +02:00
2020-07-13 06:21:06 +02:00
if ( ( ! file_exists ( $config ) || ! is_readable ( $config ) ) ) {
$message = " Error loading config file. " ;
2020-06-29 16:59:37 +02:00
2020-07-13 06:21:06 +02:00
if ( ! file_exists ( $config ) ) {
$message = " Error, could not find config file. If you have not installed Codiad please go to <a href='./install'>/install/</a> to complete the installation. " ;
} elseif ( ! is_readable ( $config ) ) {
$message = " Error config file is not readable, please check permissions. " ;
}
echo $message ;
exit ();
} else {
2020-06-29 16:59:37 +02:00
2020-07-13 06:21:06 +02:00
require_once ( $config );
2020-06-29 16:59:37 +02:00
}
}
$bases = self :: BASES ;
2020-07-27 19:20:12 +02:00
$this -> register_constants ();
2020-06-29 16:59:37 +02:00
foreach ( $bases as $base ) {
$name = strtolower ( $base );
$class = ucfirst ( $base );
require_once ( COMPONENTS . " / $name /class. $name .php " );
}
2020-07-27 19:20:12 +02:00
$this -> register_globals ();
2020-07-24 17:40:00 +02:00
if ( ! $installing ) {
$this -> check_extensions ();
$this -> check_paths ();
}
}
public static function check_extensions () {
$extensions = self :: EXTENSIONS ;
$pass = true ;
2020-06-29 16:59:37 +02:00
2020-07-24 17:40:00 +02:00
foreach ( $extensions as $extension ) {
if ( extension_loaded ( $extension ) ) {
} else {
$pass = false ;
break ;
}
}
return $pass ;
2020-06-29 16:59:37 +02:00
}
2020-07-24 17:40:00 +02:00
public static function check_paths () {
2020-06-29 16:59:37 +02:00
$paths = self :: PATHS ;
2020-07-24 17:40:00 +02:00
$pass = true ;
2020-06-29 16:59:37 +02:00
foreach ( $paths as $path ) {
2020-07-24 17:40:00 +02:00
if ( is_dir ( constant ( $path ) ) ) {
if ( ! is_writable ( constant ( $path ) ) ) {
$pass = false ;
break ;
}
} else {
2020-06-29 16:59:37 +02:00
mkdir ( constant ( $path ) );
}
}
2020-07-24 17:40:00 +02:00
return $pass ;
2020-06-29 16:59:37 +02:00
}
/**
* Return an instance of this class .
*
* @ since $ { current_version }
* @ return object A single instance of this class .
*/
public static function get_instance () {
if ( null == self :: $instance ) {
self :: $instance = new self ;
}
return self :: $instance ;
}
2020-07-13 06:21:06 +02:00
public static function is_installing () {
return ! ( strpos ( $_SERVER [ " SCRIPT_NAME " ], " install " ) === false );
}
2020-06-29 16:59:37 +02:00
function register_constants () {
if ( ! defined ( 'AUTH_TYPE' ) ) {
define ( 'AUTH_TYPE' , " default " );
}
if ( ! defined ( 'BASE_PATH' ) ) {
$base_path = rtrim ( realpath ( __DIR__ . " /../../ " ), '/' );
define ( 'BASE_PATH' , $base_path );
}
if ( ! defined ( 'COMPONENTS' ) ) {
define ( 'COMPONENTS' , BASE_PATH . '/components' );
}
if ( ! defined ( 'DATA' ) ) {
define ( 'DATA' , BASE_PATH . '/data' );
}
if ( ! defined ( 'PLUGINS' ) ) {
define ( 'PLUGINS' , BASE_PATH . '/plugins' );
}
if ( ! defined ( 'SESSIONS_PATH' ) ) {
define ( 'SESSIONS_PATH' , BASE_PATH . '/data/sessions' );
}
if ( ! defined ( 'SITE_NAME' ) ) {
define ( 'SITE_NAME' , " Codiad " );
}
2020-07-13 06:21:06 +02:00
if ( ! defined ( 'THEME' ) ) {
define ( " THEME " , " default " );
}
2020-06-29 16:59:37 +02:00
if ( ! defined ( 'THEMES' ) ) {
define ( " THEMES " , BASE_PATH . " /themes " );
}
if ( ! defined ( 'UPLOAD_CACHE' ) ) {
if ( ! is_dir ( sys_get_temp_dir () ) ) {
define ( " UPLOAD_CACHE " , DATA . " /uploads " );
} else {
define ( " UPLOAD_CACHE " , rtrim ( sys_get_temp_dir (), " / " ) );
}
}
2020-07-24 17:40:00 +02:00
if ( ! defined ( 'WORKSPACE' ) ) {
define ( " WORKSPACE " , BASE_PATH . " /workspace " );
}
2020-06-29 16:59:37 +02:00
}
function register_globals () {
2020-08-20 04:14:12 +02:00
$GLOBALS [ " data " ] = Data :: get_instance ();
2020-06-29 16:59:37 +02:00
}
}
?>