/** * Require existence of a directory * * @since ADD MVC 0.5 */ public static function require_dir_exists($dir, $writable = null) { if (!file_exists($dir)) { throw new e_system("Directory {$dir} is not existing", add::config()); } if (!is_writable($dir)) { throw new e_system("Directory {$dir} is not writable", add::config()); } }
/** * The pre-view process * * @since ADD MVC 0.0 */ public function process() { # Required Directories self::require_dir_exists(add::config()->incs_dir); self::require_dir_exists(add::config()->caches_dir, true); $this->require_dir($this->view()->compile_dir, true); $this->require_dir($this->view()->cache_dir, true); # Other essential directories $this->check_dir_exists(add::config()->classes_dir); $this->check_dir_exists(add::config()->assets_dir); $this->check_dir_exists(add::config()->css_dir); $this->check_dir_exists(add::config()->js_dir); }
/** * Smarty construct * * @since ADD MVC 0.0 */ public function __construct() { $C = add::config(); parent::__construct(); if (is_array($C->views_dir)) { $this->setTemplateDir(array_merge($C->views_dir, array($C->add_dir . '/views/'))); } else { $this->setTemplateDir(array($C->views_dir, $C->add_dir . '/views/')); } $this->compile_dir = $C->caches_dir . '/smarty_compile/'; $this->config_dir = $C->configs_dir . '/smarty/'; $this->cache_dir = $C->caches_dir . '/smarty_cache/'; }
/** * Login * * @param array $gpc - contains $gpc['username'] and $gpc['password'] * */ public function process_mode_login($gpc) { # validation on controller if (empty($gpc['username'])) { throw new e_user_input("Blank username"); } if (empty($gpc['password'])) { throw new e_user_input("Blank password"); } # login the session user class if (member::login($gpc['username'], $gpc['password']) instanceof member) { add::redirect(add::config()->path); } }
/** * Smarty construct * * @since ADD MVC 0.0 */ public function __construct() { $C = add::config(); parent::__construct(); if (is_array($C->views_dir)) { $this->setTemplateDir(array_merge($C->views_dir, array($C->add_dir . '/views/'))); } else { $this->setTemplateDir(array($C->views_dir, $C->add_dir . '/views/')); } $this->compile_dir = $C->caches_dir . '/smarty_compile/'; $this->config_dir = $C->configs_dir . '/smarty/'; $this->cache_dir = $C->caches_dir . '/smarty_cache/'; if (add::is_development()) { foreach (array($this->compile_dir, $this->cache_dir) as $dir) { if (!is_dir($dir)) { $this->_dir_perms = 0777; } else { if (!is_writable($dir)) { # Do something } } } } }
/** * Settings and stuffs * * @since ADD MVC 0.8-data_mining */ public function __construct() { if (!isset($this->cookie_dir)) { $this->cookie_dir = add::config()->caches_dir . '/' . preg_replace('/\\W+/', '_', get_called_class()) . '.class.cookies'; } if ($this->enable_cache) { $this->cache_dir = add::config()->caches_dir . '/' . preg_replace('/\\W+/', '_', get_called_class()) . '.class.cache'; if (!file_exists($this->cache_dir)) { mkdir($this->cache_dir, 0777); } } $this->set_curl_options(); if ($this->curl_options[CURLOPT_HTTPPROXYTUNNEL] && empty($this->curl_options[CURLOPT_PROXY]) && !empty($this->proxies) && is_array($this->proxies)) { $this->curl_options[CURLOPT_PROXY] = $this->proxies[array_rand($this->proxies)]; $this->cookie_dir = add::config()->caches_dir . '/cookies_' . preg_replace('/\\W+/', '_', __FILE__) . "_" . preg_replace("/\\W+/", "_", $this->curl_options[CURLOPT_PROXY]); } }
/** * login_redirect() * * @since ADD MVC 0.0 */ static function login_redirect() { add::redirect(add::config()->path . 'login?redirect=' . urlencode($_SERVER['REQUEST_URI'])); }
/** * is_developer() * * Checks if the user is developer according to his/her IP * * @since ADD MVC 0.7.2 */ public static function is_developer() { return in_array(current_user_ip(), add::config()->developer_ips) || current_ip_in_network(); }
/** * is_developer() * * Checks if the user is developer according to his/her IP * * @since ADD MVC 0.7.2 */ public static function is_developer() { # Fix for issue #6 if (current_ip_in_network()) { return true; } if (isset(add::config()->developer_ips)) { return in_array(current_user_ip(), (array) add::config()->developer_ips); } else { return false; } }
public function fetch_view() { $this->view()->assign('C', add::config()); return $this->view()->fetch(static::view_filepath()); }
} if (!isset($C)) { die("No config found"); } require $C->add_dir . '/classes/add.class.php'; $GLOBALS[add::CONFIG_VARNAME] = add::config($C); spl_autoload_register('add::load_class'); set_exception_handler('add::handle_exception'); set_error_handler('add::handle_error'); register_shutdown_function('add::handle_shutdown'); $C->incs_dir = $C->root_dir . '/includes'; $C->classes_dirs = array_merge(array($C->incs_dir . '/classes', $C->add_dir . '/classes'), isset($C->classes_dirs) && is_array($C->classes_dirs) ? $C->classes_dirs : array()); $C->configs_dir = $C->incs_dir . '/configs'; $C->views_dir = $C->incs_dir . '/views'; $C->caches_dir = $C->incs_dir . '/caches'; add::environment_status(add::config()->environment_status); if (add::is_development() && !is_writeable($C->caches_dir)) { $C->caches_dir = sys_get_temp_dir() . '/add_mvc_caches'; if (!file_exists($C->caches_dir)) { mkdir($C->caches_dir, 0700); } } $C->assets_dir = $C->root_dir . '/assets'; $C->images_dir = $C->assets_dir . '/images'; $C->css_dir = $C->assets_dir . '/css'; $C->js_dir = $C->assets_dir . '/js'; $C->domain = ($C->sub_domain ? "{$C->sub_domain}." : "") . $C->super_domain; $C->base_url = "http://{$C->domain}" . $C->path; set_include_path($C->incs_dir); /** * assets
<?php require '../config.php'; require_once "{$C->add_dir}/init.php"; add::config()->environment_status = 'live'; $email = $_GET['email']; if ($email) { e_add::$email_addresses = $email; $exceptions = array('e_developer', 'e_syntax', 'e_hack', 'e_spam', 'e_system'); $rand_excemption = $exceptions[array_rand($exceptions)]; throw new $rand_excemption('ERROR! ' . $rand_excemption); } ?> <form method="GET"> <input type="text" name="email" /> <input type="submit" /> </form>
/** * Handle exception * */ public function handle_exception() { $this->message = str_replace(add::config()->root_dir, '< mvc-path >', $this->data->message); return parent::handle_exception(); }
<?php require 'config.php'; #echo "ADD DIR: ".$C->add_dir; require $C->add_dir . '/init.php'; #date_default_timezone_set(add::config()->default_timezone); if (add::config()->debug_sql && add::is_development() && add::is_developer()) { locust_mule_db::singleton()->debug = true; }
*/ $C->assets_path = $C->base_url . 'assets/'; $C->css_path = $C->assets_path . 'css/'; $C->js_path = $C->assets_path . 'js/'; $C->images_path = $C->assets_path . 'images/'; $C->assets_libs_path = $C->assets_path . 'libs/'; /** * No errors if live * * @since ADD MVC 0.7.2 */ if (add::is_live()) { error_reporting(0); } else { error_reporting(E_ALL); /** * When development, record the time spent on script execution * * @since ADD MVC 0.7.2 */ if (add::is_development()) { $GLOBALS['add_mvc_root_timer'] = add_development_timer::start("Framework Configuration"); add::config()->root_timer = $GLOBALS['add_mvc_root_timer']; } } add::load_functions('common'); /** * Libraries */ add::load_lib('adodb'); add::load_lib('smarty');
/** * cache path of the current url * * @since ADD MVC 0.5 */ public function cache_path() { # Fix https://code.google.com/p/add-mvc-framework/issues/detail?id=152 if (!$this->enable_cache) { return null; } if (empty($this->cache_dir)) { $this->cache_dir = add::config()->caches_dir . '/' . preg_replace('/\\W+/', '_', get_called_class()) . '.class.cache'; if (!file_exists($this->cache_dir)) { mkdir($this->cache_dir, 0777); } } # ^ Fix https://code.google.com/p/add-mvc-framework/issues/detail?id=152 e_developer::assert($this->cache_dir, "Cache directory variable is blank"); $cache_file_name = sha1($this->url); if ($this->curl_options[CURLOPT_HTTPPROXYTUNNEL]) { $cache_file_name .= sha1($this->curl_options[CURLOPT_PROXY]); } return $this->cache_dir . '/' . $cache_file_name; }
/** * environment check: is_development() * * @since ADD MVC 0.7 */ public static function is_development() { return add::config()->environment_status === 'development'; }
/** * Returns the file and line number of the caller of the function * @since ADD MVC 0.0 */ static function caller_file_line() { $backtraces = debug_backtrace(); foreach ($backtraces as $backtrace) { $is_trace_class_debug = $backtrace['class'] == __CLASS__ || is_subclass_of($backtrace['class'], __CLASS__); if (empty($backtrace['class']) || !$is_trace_class_debug) { break; } $file_line = $backtrace['file'] . ':' . $backtrace['line']; } if (!$file_line) { return "Unknown Location"; } $file_line = preg_replace('/^' . preg_quote(add::config()->root_dir, '/') . '\\//', '', $file_line); return $file_line; }
/** * * * */ public function Connect() { return $this->adodb->Connect(add::config()->mysql_host, add::config()->mysql_username, add::config()->mysql_password, add::config()->mysql_db_name); }
* To be used to check if PHP Version is correct * @since ADD MVC 0.1 */ DEFINE('ADD_MIN_PHP_VERSION', '5.3.8'); if (version_compare(phpversion(), ADD_MIN_PHP_VERSION) === -1) { die("ADD MVC Error: PHP version must be at least " . ADD_MIN_PHP_VERSION . " or higher!"); } if (!isset($C)) { $C = new STDClass(); } # Sets the add_dir if not set. And it may be smart not to let the user(developer) set this on the first place if (empty($C->add_dir)) { $C->add_dir = realpath(dirname(__FILE__)); } require $C->add_dir . '/classes/add.class.php'; $GLOBALS[add::CONFIG_VARNAME] = add::config($C); if (php_sapi_name() == "cli") { add::content_type('text/plain'); } # Set the handlers spl_autoload_register('add::load_class'); set_exception_handler('add::handle_exception'); set_error_handler('add::handle_error'); register_shutdown_function('add::handle_shutdown'); # Set the includes dir if (!isset($C->incs_dir)) { $C->incs_dir = $C->root_dir . '/includes'; } # Merge config declared class directories $C->classes_dirs = array_merge(array($C->incs_dir . '/classes'), isset($C->classes_dirs) ? is_array($C->classes_dirs) ? $C->classes_dirs : (array) $C->classes_dirs : array(), array($C->add_dir . '/classes')); # Set these rarely used directory variables
/** * login_redirect() * extend to change * @since ADD MVC 0.0 */ static function login_redirect() { add::redirect(add::config()->path . static::LOGIN_PAGE . "?redirect=" . urlencode($_SERVER['REQUEST_URI'])); }
static function singleton() { return add::config(); }
public static function load_classes() { static::load_dir_classes(add::config()->add_dir . '/classes'); }
/** * Autoload class function * * @param string $classname * * @todo on 1.0, move the ifs() and deprecate filename without .class. * @author albertdiones@gmail.com * @since ADD MVC 0.0 * @version 1.0 */ static function load_class($classname) { global $C; if (class_exists('e_developer', false)) { e_developer::assert($classname, "Blank classname"); } $incs_class_filepath = add::config()->classes_dir . "/{$classname}.class.php"; if (file_exists($incs_class_filepath)) { $class_filepath = $incs_class_filepath; } # Backward support to 0.0 if (empty($class_filepath)) { $incs_class_filepath = add::config()->classes_dir . "/{$classname}.php"; if (file_exists($incs_class_filepath)) { $class_filepath = $incs_class_filepath; trigger_error("{$class_filepath} format is deprecated", E_USER_NOTICE); } } if (empty($class_filepath)) { $class_filepath_wildcard = $C->incs_dir . '/classes/*/' . $classname . '.class.php'; $class_filepath_search = glob($class_filepath_wildcard); # Backward support to 0.0 if (!$class_filepath_search) { $class_filepath_wildcard = $C->incs_dir . '/classes/*/' . $classname . '.php'; if ($class_filepath_search = glob($class_filepath_wildcard)) { trigger_error("{$class_filepath_search[0]} format is deprecated", E_USER_NOTICE); } } if (!$class_filepath_search) { $class_filepath_wildcard = $C->add_dir . '/classes/*/' . $classname . '.class.php'; $class_filepath_search = glob($class_filepath_wildcard); } # Backward support to 0.0 if (!$class_filepath_search) { $class_filepath_wildcard = $C->add_dir . '/classes/*/' . $classname . '.php'; if ($class_filepath_search = glob($class_filepath_wildcard)) { trigger_error("{$class_filepath_search[0]} format is deprecated", E_USER_NOTICE); } } if ($class_filepath_search) { $class_filepath = $class_filepath_search[0]; } } # Backward support to 0.2 if (empty($class_filepath)) { $class_filepath = $C->add_dir . '/classes/' . $classname . '.php'; if (file_exists($class_filepath)) { trigger_error("{$class_filepath} format is deprecated", E_USER_NOTICE); } if (!file_exists($class_filepath)) { $class_filepath = null; } } /*if (empty($class_filepath)) { $e_class = 'e_developer'; if (class_exists($e_class)) { throw new $e_class("$classname not found",array($C->add_dir,add::config()->classes_dir)); } else { throw new Exception("$classname not found" . $C->add_dir . " " . add::config()->classes_dir ); } }*/ if ($class_filepath) { include_once $class_filepath; } if ($class_filepath && !class_exists($classname) && !interface_exists($classname)) { $e_class = 'e_developer'; if (class_exists($e_class)) { throw new $e_class("{$classname} not found from {$class_filepath}"); } else { throw new Exception("{$classname} not found from {$class_filepath}"); } } return class_exists($classname); }
/** * meta keywords * * @since ADD MVC 0.1, ctrl_tpl_page 0.1 * @deprecated use the view instead */ public function meta_keywords() { return isset(add::config()->default_meta_keywords) ? add::config()->default_meta_keywords : null; }
/** * Returns the file and line number of the caller of the function * @since ADD MVC 0.0 */ static function caller_file_line() { $backtrace = static::caller_backtrace(); if (!$backtrace) { return "Unknown Location"; } $file_line = $backtrace['file'] . ':' . $backtrace['line']; $file_line = preg_replace('/^' . preg_quote(add::config()->root_dir, '/') . '\\//', '', $file_line); return $file_line; }
/** * is_developer() * * Checks if the user is developer according to his/her IP * * @since ADD MVC 0.7.2 */ public static function is_developer() { /** * @see http://code.google.com/p/add-mvc-framework/issues/detail?id=39 */ if (php_sapi_name() == "cli") { return true; } # Fix for issue #6 if (current_ip_in_network()) { return true; } if (isset(add::config()->developer_ips)) { return in_array(current_user_ip(), (array) add::config()->developer_ips); } else { return false; } }
/** * Smarty construct * * @since ADD MVC 0.7 */ public function __construct() { parent::__construct(); $this->setTemplateDir(array(add::config()->views_dir . '/exceptions', add::config()->add_dir . '/views/exceptions')); }
/** * Prints the config value * * @since ADD MVC 0.7.4 */ public static function print_config($field, $boolean = false) { $value = isset(add::config()->{$field}) ? add::config()->{$field} : null; $label = "config - {$field}"; if ($boolean) { $value = (bool) $value; $label .= " declared"; } static::print_data($label, $value); }
/** * Handling Exceptions * * @since ADD MVC 0.7 */ public function handle_sensitive_exception($user_message = "An error has occured") { if (add::is_development()) { $this->print_exception(); die; } else { $this->mail(); $this->view()->assign('exception', $this); $this->view()->assign('C', add::config()); $this->display_view(); } }