/** * login function * login using $username and $password, and optional addinational $options * @author albertdiones@gmail.com * * @since ADD MVC 0.0 */ public static function login() { $args = func_get_args(); $username = $args[0]; $password = $args[1]; $group = $args[2]; if (!$username) { throw new e_user_input("Input your " . $this->term_username()); } if (!$password) { throw new e_user_input("Input your " . $this->term_password()); } $ldap_result = static::ldap()->bind($username, $password); if (!$ldap_result) { //debug::var_dump(static::TABLE,static::get_value_index_field($username),$username,get_called_class()); throw new e_user_input("Failed to login: Invalid " . self::term_username() . " and " . self::term_password()); } if ($group) { # You have to declare ::validate_group_membership() on the late static class $late_static_class = get_called_class(); e_developer::assert(method_exists($late_static_class, 'validate_group_membership'), $late_static_class . '::validate_group_membership() function is not declared'); if (!static::validate_group_membership($username, $group)) { throw new e_user_input("Failed to login: invalid group"); } } $member = static::singleton(); $member->username = $username; $member->group = $group; return $member; }
/** * Gets an instance that represent the row from the table * @param array $pks an array of pks produced by model_multi_pk::serialize_pk_array * @version 0.1 * @since version 0.1 */ public static function get_instance($pks) { $args = func_get_args(); if (count($pks) === 1) { e_developer::assert(is_array($pks), "Multiple PK must be used on " . get_called_class() . "::" . __FUNCTION__ . " " . json_encode($pks)); } return call_user_func_array(array('parent', 'get_instance'), $args); }
/** * Here we change the path to layouts/path * * @since ADD MVC 0.10 */ public function getAttributes($compiler, $void) { $result = call_user_func_array('parent::' . __FUNCTION__, func_get_args()); $result['file'] = preg_replace('/^(\\\'|\\")/', '$0includes/', $result['file']); $result['file'] = preg_replace('/(\\\'|\\")$/', '.tpl$0', $result['file']); e_developer::assert($compiler->smarty->TemplateExists(trim($result['file'], '\'"')), "{$result['file']} Does not exist!"); return $result; }
/** * Deprecated function * @param string $table * @param string $field * @param string $field_value * @deprecated use get_row_instance() */ static function db_row_array($table, $field, $field_value) { e_developer::assert(is_object(static::db()), get_called_class() . ' $D is not an object'); $row = static::db()->getRow("\r\n SELECT * FROM " . static::db()->meta_quote($table) . "\r\n WHERE " . static::db()->meta_quote($field) . " = " . static::db()->quote($field_value)); return $row; }
/** * 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"); } # Iterate it through classes directories foreach (add::config()->classes_dirs as $classes_dir) { # Load it from the application's class dir $class_filepath_wildcard = "{$classes_dir}/{,*/}{$classname}.class.php"; $class_filepath_search = glob($class_filepath_wildcard, GLOB_BRACE); # Backward support to 0.2 (without .class) if (!$class_filepath_search) { $class_filepath_wildcard = "{$classes_dir}/{,*/}{$classname}.php"; $class_filepath_search = glob($class_filepath_wildcard, GLOB_BRACE); } if ($class_filepath_search) { $class_filepath = $class_filepath_search[0]; break; } } if (!empty($class_filepath)) { $class_loaded = (include_once $class_filepath); $class_loaded &= class_exists($classname) || interface_exists($classname); } else { $class_loaded = false; } # Check if the class is actually loaded if (!empty($class_filepath) && !$class_loaded) { $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}"); } } # ADD MVC Class Loaded Event if ($class_loaded && method_exists($classname, '__add_loaded')) { $classname::__add_loaded(); } return $class_loaded; }
/** * Logs string to the log file * * @param string $string the string to log * * @since ADD MVC 0.5 */ public function log($string) { e_developer::assert(method_exists($this, 'log_filepath'), __CLASS__ . " log file is not set"); file_put_contents($this->log_filepath(), $string . "\r\n", FILE_APPEND); }
/** * recursive compact($array_keys) * Returns an array of GPC from the $array_keys * * @since ADD MVC 0.1, ctrl_tpl_page 0.1 */ public static function recursive_compact($gpc_array_keys) { $compact_array = array(); foreach ($gpc_array_keys as $gpc_key => $array_keys) { e_developer::assert(isset($GLOBALS[$gpc_key]), "Invalid GPC key {$gpc_key}"); $gpc_array = $GLOBALS[$gpc_key]; foreach ($array_keys as $array_key) { e_developer::assert(is_scalar($array_key), "Invalid GPC array key {$array_key}"); $compact_array[$array_key] = empty($gpc_array[$array_key]) ? null : $gpc_array[$array_key]; } } return $compact_array; }
/** * set_password * @param string $password * @param string $confirm_password * @since ADD MVC 0.3 */ public function set_password($password, $confirm_password = false) { e_developer::assert(is_string($password), "Password argument is not string", func_get_args()); if ($confirm_password !== false && $password !== $confirm_password) { throw new e_user("Password confirmation incorrect"); } $this->{static::PASSWORD_FIELD} = $this->hash_password($password); }
/** * 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); }
/** * cache path of the current url * * @since ADD MVC 0.5 */ public function cache_path() { e_developer::assert($this->cache_dir, "Cache directory 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; }
/** * Accepts 2 dimensional array of keys to be fetched from global variables * * Returns a multi dimension array of the global variables value of $gpc_array_keys * * @param array $gpc_array_keys - 2 dimension array of keys * * @see ctrl_abstract::process_mode() * * <code> * if (!$_GET['foo']) { * add:redirect('?foo=bar'); * } * debug::var_dump($_GET, ctrl_abstract::recursive_compact( array( '_GET' => array('foo') ) )); * </code> * * @since ADD MVC 0.1, ctrl_tpl_page 0.1 */ public static function recursive_compact($gpc_array_keys) { $compact_array = array(); # Magic quotes backward support https://code.google.com/p/add-mvc-framework/issues/detail?id=118 $magic_quotes_on = get_magic_quotes_gpc() && ($real_gpcs = array('_GET', '_POST', '_COOKIE', '_REQUEST')); foreach ($gpc_array_keys as $gpc_key => $array_keys) { e_developer::assert(isset($GLOBALS[$gpc_key]), "Invalid GPC key {$gpc_key}"); $gpc_array = $GLOBALS[$gpc_key]; foreach ($array_keys as $array_key) { e_developer::assert(is_scalar($array_key), "Invalid GPC array key {$array_key}"); $compact_array[$array_key] = empty($gpc_array[$array_key]) ? null : $gpc_array[$array_key]; } # stripslahes if magic quotes is on https://code.google.com/p/add-mvc-framework/issues/detail?id=118 if ($magic_quotes_on && in_array($gpc_key, $real_gpcs)) { array_walk_recursive($compact_array, function (&$value, $key) { $value = stripslashes($value); }); } } return $compact_array; }
/** * 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; }
/** * cache path of the current url * * @since ADD MVC 0.5 */ public function cache_path() { e_developer::assert($this->cache_dir, "Cache directory is blank"); return $this->cache_dir . '/' . sha1($this->url); }