PHP Classes

File: LEMP/lemp.php

Recommend this page to a friend!
  Classes of David Duong   Usermanage   LEMP/lemp.php   Download  
File: LEMP/lemp.php
Role: Class source
Content type: text/plain
Description: Core Class
Class: Usermanage
Puzzle like modular authentication class
Author: By
Last change:
Date: 20 years ago
Size: 30,807 bytes
 

Contents

Class file image Download
<?php // ---------------------------------------------------------------------- // LEMP - Lightweight Efficient Modular PHP Application Framework // @copyright 2002-2003 The CGI Open Source Federation. (http://cosf.sf.net) // @version $Id: lemp.php,v 1.23 2004/05/16 20:59:11 cogs Exp $ // ---------------------------------------------------------------------- // LICENSE // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License (GPL) // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have recieved the lincence included with the disribution. // If not visit http://www.gnu.org/copyleft/gpl.html // ---------------------------------------------------------------------- // Shorthand constants define ("LEMP_PATH_TO_LIBRARY", dirname (__FILE__)); // You could also want to replace the following if-else block with // define ('LEMP_PATH_TO_CONFIG', 'path_to_config_file'); // but you will have to do this every time you update LEMP if (file_exists(LEMP_PATH_TO_LIBRARY . '/../config.php')) { define ('LEMP_PATH_TO_CONFIG', realpath (LEMP_PATH_TO_LIBRARY . '/../config.php')); } elseif (file_exists(LEMP_PATH_TO_LIBRARY . '/config.php')) { define ('LEMP_PATH_TO_CONFIG', realpath (LEMP_PATH_TO_LIBRARY . '/config.php')); } else { define ('LEMP_PATH_TO_CONFIG', ''); } // Some usefull constants... define ("ENDL", "\n"); define ("ENDLB", "<br>\n"); // Variable types define ("LEMP_NULL", 0); define ("LEMP_BOOL", 1); define ("LEMP_SCALAR", 10); define ("LEMP_INT", 11); define ("LEMP_COUNT", 11); define ("LEMP_STRING", 12); define ("LEMP_FIELD", 12); define ("LEMP_TEXT", 13); // Multi-Line Text define ("LEMP_HTML", 14); define ("LEMP_XML", 15); define ("LEMP_ARRAY", 100); define ("LEMP_ARRAY_NUM", 101); define ("LEMP_ARRAY_ASSOC", 102); define ("LEMP_ARRAY_BOTH", 103); define ("LEMP_MULTI_ARRAY", 110); define ("LEMP_MULTI_ARRAY_NUM", 111); define ("LEMP_MULTI_ARRAY_ASSOC", 112); define ("LEMP_MULTI_ARRAY_BOTH", 113); define ("LEMP_OBJECT", 200); $LEMP_error_log = array (); $LEMP_config_vars = array (); /** * LEMP is a mini-framework that can help developers focus on their * concept. It is extended by modules to form the base and provide * functions like database interaction, which could also be extended * by other modules like user management. */ class LEMP { var $db; // The DB object used by LEMP to interact with the databse. var $input; // Is equal to $_REQUEST var $in; // Reference to $input /** * LEMP::LEMP() LEMP's constructor. It will set up the variables that LEMP needs to * run. * * @access private * @return void */ function LEMP () { // Create database object $this->db = &create_obj ('db_handle'); $this->input = $_REQUEST; $this->in = &$this->input; } /** * LEMP::dbconnect() alias of db_handle::dbconnect() */ function dbconnect () { return $this->db->connect(); } /** * LEMP::dbclose() alias of db_handle::dbclose() */ function dbclose () { return $this->db->close(); } /** * LEMP::dbquery() alias of db_handle::dbquery() */ function dbquery ($query) { return $this->db->query ($query); } /** * LEMP::dbfetch() alias of db_handle::dbfetch() */ function dbfetch ($result, $result_type = 103) { return $this->db->fetch ($result, $result_type); } /** * LEMP::dbquery_fetch() alias of db_handle::dbquery_fetch() */ function dbquery_fetch ($query, $result_type = 103) { return $this->db->query_fetch ($query, $result_type); } /** * LEMP::dbnum_rows() alias of db_handle::dbnum_rows() */ function dbnum_rows ($result) { return $this->db->num_rows ($result); } } /** * Database functions that LEMP uses. */ class db_handle { var $db_link; // The DB link /** * db_handle::db_handle() db_handle's constructor. * * @access private * @return void */ function db_handle () { $this->db_link = false; } /** * db_handle::connect() Object connects and selects database * (settings are set in config.php file), or returns the database * link a connection exists. * * @return bool Successful | Not Successful */ function connect () { $db = get_config('db'); // Get the database configuration // If there already is a link exit the function if ($this->db_link !== false) { return true; } if ($db['type'] == 'mysql') { // Determine handler // Connect to mysql database if mysql database is used $this->db_link = mysql_connect($db['host'], $db['username'], $db['password']); if (!$this->db_link) { return error_add ('DB Error: Could not connect to mysql host:' . $db['host'], __FILE__, __LINE__, true); } // Select database $selected = mysql_select_db ($db['database'], $this->db_link); if (!$selected) { return error_add ('DB Error: Could not select database:' . $db['database'], __FILE__, __LINE__, true); } } else { // If database type is not mysql, trigger any external hooks $result = trigger_event ('db_handle:connect:alternate'); if (is_null ($result)) { return error_add ('Config Error: Unknown database type', __FILE__, __LINE__, true); } } return true; } /** * db_handle::close() Closes database connection and destroys link handler. * * @return bool Successful | Not Successful */ function close () { $db = get_config('db'); // Determine handler if ($db['type'] == 'mysql') { // Close link to mysql database if mysql database is used $return = mysql_close ($this->db_link); } else { // If database type is not mysql, trigger any external hooks $result = trigger_event ('db_handle:close:alternate', array($this->db_link)); if (is_null ($result)) { return error_add ('Config Error: Unknown database type', __FILE__, __LINE__, true); } } // Reset $this->db_link $this->db_link = false; return true; } /** * db_handle::query() Sends queries to database. * * @param string $query SQL query to send to SQL database. * @return resource */ function query ($query) { // If there already is a link exit the function if (!$this->db_link) { $this->connect(); } // Get the $db['type'] value from config $db_type = get_config('db', 'type'); // Determine handler if ($db_type == 'mysql') { // Close link to mysql database if mysql database is used $return = mysql_query ($query, $this->db_link); } else { // If database type is not mysql, trigger any external hooks $return = trigger_event ('db_handle:query:alternate', array($query, $this->db_link)); if (is_null ($result)) { return error_add ('Config Error: Unknown database type', __FILE__, __LINE__, true); } } if (!$return) { $error_str = "Error performing SQL Query: <p><pre>$query</pre></p>"; if (function_exists($db_type . '_error')) { $error_str .= "\n<p><pre>Error Message: " . call_user_func ("{$db_type}_error", $this->db_link) . "</pre></p>"; } return error_add ($error_str, __FILE__, __LINE__, true); } return $return; } /** * db_handle::fetch() Fetches data from the result associated with the specified result identifier. * Subsequent calls to dbfetch() would return the next row in the result set, or FALSE if there are no more rows. * Simulates *sql_result functions when LEMP_SCALAR is passed for $result_type * * @param resource $result SQL result to use. * @param const $result_type (Default LEMP_ARRAY_BOTH) Takes the following values: LEMP_ASSOC, LEMP_NUM, LEMP_BOTH, * LEMP_SCALAR (simulates [database[_fetch]]_result), and LEMP_OBJECT * @return mixed return depends on $result_type */ function fetch ($result, $result_type = 103) { static $row; $db = get_config('db'); // Make sure that the result is valid if (is_bool($result) || empty($result)) { return error_add('Invalid Arguments: Supplied argument is not a valid result resource', __FILE__, __LINE__, true); } // Determine if result is multi if ($result_type >= LEMP_MULTI_ARRAY && $result_type <= LEMP_MULTI_ARRAY_BOTH) { // If it is, set result type $multi_array = true; $result_type -= 10; } else { $multi_array = false; } do { // Determine handler if ($db['type'] == 'mysql') { // Convert lemp result types to mysql $result_type = ($result_type == LEMP_ARRAY_ASSOC)? MYSQL_ASSOC : ($result_type == LEMP_ARRAY_NUM)? MYSQL_NUM : ($result_type == LEMP_ARRAY_BOTH)? MYSQL_BOTH : $result_type; // Fetch from mysql database if mysql database is used if ($result_type == LEMP_OBJECT) { $return = mysql_fetch_object ($result); } elseif ($result_type == LEMP_SCALAR) { $return = mysql_fetch_array ($result, MYSQL_NUM); if ($return !== false) { $return = $return[0]; } $return = stripslashes ($return); } else { $return = mysql_fetch_array ($result, $result_type); } } else { // If database type is not mysql, trigger any external hooks $return = trigger_event ('db_handle:fetch:alternate', array($result, $row[$result], $result_type, $this->db_link)); if (is_null ($return)) { return error_add ('Config Error: Unknown database type', __FILE__, __LINE__, true); } } // Apply Strip Slashes to results if (is_array($return)) { $striped_return = array (); foreach ($return as $column => $value) { // TODO: Replace $column with table schema in UM_Dbdat $striped_return[$column] = stripslashes($value); } $return = $striped_return; } if ($multi_array == true && $return !== false) { $multi_return[] = $return; } $row[$result]++; } while ($return !== false && $multi_array == true); return ($multi_array == true)? $multi_return: $return; } /** * db_handle::num_rows() Get number of rows in result. This command is only valid for SELECT statements. * * @param resource $result returns the number of rows in a result set or false on error * @return mixed */ function num_rows ($result) { $db = get_config('db'); if ($db['type'] == 'mysql') { $return = mysql_num_rows($result); } else { $result = trigger_event ('db_handle:num_rows:alternate', array($result, $this->db_link)); if (is_null ($result)) { return error_add ('Config Error: Unknown database type', __FILE__, __LINE__, true); } } if ($return === false) { error_add ('Error retrieving the number of rows in result set', __FILE__, __LINE__, true); } return $return; } /** * db_handle::query_fetch() Sends SQL query and fetches results. * * @param mixed $query Either an sql select statement or array($select, $from, [$where], [$limit]) * @param const $result_type See db_handle::fetch() * @param integer $start_row See db_handle::fetch() * @param string $field See db_handle::fetch() * @return See db_handle::fetch() */ function query_fetch ($query, $result_type = 113) { if (($result_type > LEMP_MULTI_ARRAY_BOTH || $result_type < LEMP_ARRAY) && $result_type != LEMP_SCALAR) { return error_add ('dbquery_fetch only accepts: LEMP_MULTI_ARRAY*, LEMP_ARRAY*, or LEMP_SCALAR not ' . $query, __FILE__, __LINE__, true); } if (is_array($query)) { $parts = $query; $query = "SELECT {$parts[0]} FROM {$parts[1]}"; if (isset($parts[2])) { $query .= ' WHERE ' . $parts[2]; } if (isset($parts[3])) { $query .= ' LIMIT ' . $parts[3]; } $query .= ';'; } $result = $this->query ($query); return ($result !== false)? $this->fetch ($result, $result_type) : false; } } /** * Functions used in LEMP */ /** * error_add() Adds an error/warning/notice to LEMP's internal error handler. * * You can retrieve all errors with error_get_all (). * * @param mixed $error_name What just happened? * @param string $errfile In what file was error_add called? (Use __FILE__) * @param string $errline In what line was error_add called? (Use __LINE__) * @param bool $script_error Is this not a user error? * @param string $details Extended information * @return mixed */ function error_add ($error_name, $errfile = '', $errline = 0, $script_error = false, $details = '') { global $LEMP_error_log; if (empty ($error_name)) { return true; } if ($script_error == false) { $script_error = ($errfile != '')? true : false; } $can_write = false; if ($script_error && is_writable(LEMP_PATH_TO_LIBRARY . '/logs/error_log.txt')) { $can_write = true; $error_message = ''; } if (!is_array($error_name)) { $error_name = array($error_name); } foreach ($error_name as $name) { if ($can_write) { if ($errfile == '') { $errfile = 'unknown'; } $error_message .= <<<EOL <pre> File: $errfile<br> Line: $errline<br> Message: "$name"<br> Details: "$details"<br> </pre> EOL; } $LEMP_error_log[] = array('name' => $name, 'file' => $errfile, 'line' => $errline, 'script_error' => $script_error, 'details' => $details); } if ($can_write && $error_message != '') { $error_log = fopen (UM_LEMP_PATH_TO_LIBRARY . '/logs/error_log.txt' , 'a+'); fwrite ($error_log, $error_message); fclose ($error_log); } return false; } /** * error_get_all() Retrieve all errors encountered by script. * * @param const $return_as Return as LEMP_ARRAY, LEMP_STRING, LEMP_HTML or LEMP_INT (number of errors). * @return mixed */ function error_get_all ($return_as = 14) { global $LEMP_error_log; $config_config = get_config ('config'); $config_errors = get_config ('errors'); $count = count ($LEMP_error_log); if ($count == 0) { $error_string = 'NONE'; } elseif ($return_as == LEMP_HTML || $return_as == LEMP_STRING || $return_as == LEMP_ERROR) { $error_string = ''; foreach ($LEMP_error_log as $error_info) { if ($error_info['script_error'] && !$config_config['debug']) { continue; } if ($config_config['debug'] && $error_info['script_error']) { $error_string .= ENDL . "File: {$error_info['file']}, line: {$error_info['line']}, message: \"{$error_info['name']}\", details: \"{$error_info['details']}\"\n"; } else { $error_string .= ENDL . $error_info['name']; } } if ($error_string == '') { $error_string = 'NONE'; } } if ($return_as == LEMP_STRING) { return $error_string; } elseif ($return_as == LEMP_HTML) { return nl2br($error_string); } elseif ($return_as == LEMP_INT) { return $count; } elseif ($return_as == LEMP_ARRAY_NUM || $return_as == LEMP_ARRAY) { return $LEMP_error_log; } elseif ($config_config['allow_terminate'] && $return_as == LEMP_ERROR) { exit (nl2br($error_string)); } } /** * init_config() config database's constructor. It load the initial config file * * @access private * @return void */ function init_config () { if (LEMP_PATH_TO_CONFIG == '') { return error_add ('Could not find config.php', __FILE__, __LINE__, true); } global $LEMP_config_vars; $config_vars['static'] = array ('db'); $config_vars['extendible'] = array ('objects', 'config', 'extensions', 'tables', 'trigger', 'config_vars', 'static', 'extendible'); require_once (LEMP_PATH_TO_CONFIG); if ($db['type'] != 'mysql') { require (LEMP_PATH_TO_LIBRARY . "/db/{$db['type']}_handler.php"); } $LEMP_config_vars = compact ($config_vars['static'], $config_vars['extendible']); if (isset ($LEMP_config_vars['extensions']['config'])) { add_config ($LEMP_config_vars['extensions']['config']); unset ($LEMP_config_vars['extensions']['config']); } } /** * Fetches data from a config file. * ie get_config ('tables', 'table_name', 'column') * * Using get_config to retrieve table information is discouraged, used get_table_columns instead. * @return mixed */ function get_config () { $num_args = func_num_args (); // if (($num_args == 3 || $num_args == 2) && func_get_arg(0) == 'tables') { // return get_table_cols (func_get_arg(1), @func _get_arg(2)); // } global $LEMP_config_vars; if (empty ($LEMP_config_vars)) { init_config (); } if ($num_args > 0) { if ($num_args > 1) { $current_location = $LEMP_config_vars[func_get_arg(0)][func_get_arg(1)]; for($i = 2; $i < $num_args; $i++) { $current_location = $current_location[func_get_arg($i)]; } } else { $current_location = $LEMP_config_vars[func_get_arg(0)]; } return $current_location; } else { return $LEMP_config_vars; } } /** * Fetches table data from the config database. * * @param string $table_name The table name. * @param string $columns The column names to fetch. If left empty, all column names are returned. * @return mixed */ function get_table_columns ($table_name, $columns = array ()) { global $LEMP_config_vars; if (empty ($LEMP_config_vars)) { init_config (); } if (is_array ($LEMP_config_vars['tables'][$table_name])) { foreach ($LEMP_config_vars['tables'][$table_name] as $key => $column) { if (is_int($key) || empty ($column)) { if (empty ($column) && $key{0} == '_') { $LEMP_config_vars['tables'][$table_name][$column] = substr($key, 1); } else { $LEMP_config_vars['tables'][$table_name][$column] = $column; } if (is_int($key)) { unset ($LEMP_config_vars['tables'][$table_name][$key]); } } } } if (!empty ($columns)) { if (is_string($columns)) { if (empty ($LEMP_config_vars['tables'][$table_name][$columns]) && $columns{0} != '_') { return $columns; } return $LEMP_config_vars['tables'][$table_name][$column]; } else { foreach ($columns as $column) { $table[$column] = $LEMP_config_vars['tables'][$table_name][$column]; } return $table; } } else { return $LEMP_config_vars['tables'][$table_name]; } } /** * Adds a config file (no config files are not changed). * * @param mixed $config_files Array (or string) of config file(s)'s location * @return void */ function add_config ($config_files) { global $LEMP_config_vars; static $included_config_exts = array (); if (empty ($LEMP_config_vars)) { init_config (); } if (is_string ($config_files) && (!in_array($config_files, $included_config_exts))) { $config_files = array ($config_files); } elseif (is_array ($config_files)) { $config_files = array_diff ($config_files, $included_config_exts); } else { return; } if (empty($config_files)) { return; } // Make sure that variables like $db are availabe within the scope. if (is_array($LEMP_config_vars['config_vars']['static'])) { foreach ($LEMP_config_vars['config_vars']['static'] as $static_var) { $$static_var = $LEMP_config_vars[$static_var]; } } // Include any config extensions while (count ($config_files) > 0) { foreach ($config_files as $config_key => $config_ext) { if (file_exists($config_ext)) { include_once ($config_ext); } elseif (file_exists(LEMP_PATH_TO_LIBRARY . '/' . $config_ext)) { include_once (LEMP_PATH_TO_LIBRARY . '/' . $config_ext); } $included_config_exts[] = $config_ext; unset ($config_files[$config_key]); if (isset ($extensions['config'])) { if (is_string ($extensions['config']) && !in_array($extensions['config'], $included_config_exts)) { $config_files[] = $extensions['config']; } else { $extensions['config'] = array_diff ($extensions['config'], $included_config_exts, $config_files); $config_files = array_merge ($extensions['config'], $config_files); } unset ($extensions['config']); } } } if (isset($config_vars['static'])) { $LEMP_config_vars['config_vars']['static'] = array_merge ($LEMP_config_vars['config_vars']['static'], $config_vars['static']); array_unique ($LEMP_config_vars['config_vars']['static']); unset ($config_vars['static']); } if (isset($config_vars['extendible'])) { $LEMP_config_vars['config_vars']['extendible'] = array_merge ($LEMP_config_vars['config_vars']['extendible'], $config_vars['extendible']); array_unique ($LEMP_config_vars['config_vars']['static']); unset ($config_vars['extendible']); } $LEMP_config_vars = array_merge_recursive ($LEMP_config_vars, compact ($LEMP_config_vars['config_vars']['extendible'])); return; } /** * create_obj() Creates or returns a LEMP object. * * @param mixed $object Object schema or module name. * @param array $args This is sent to the object's constructor, this is merged with * the args set in the object schema. * @return object */ function &create_obj ($object = 'default', $args = array ()) { static $objects = array(); static $auto_name = array (); if (empty ($object)) { return add_error ('Object name must be specified for create_obj', __FILE__, __LINE__); } if (is_string ($object)) { if ($object == 'default') { $object = get_config ('objects', 'default'); if (empty ($object)) { $object == 'LEMP'; } } if ($object == 'db_handle' || $object == 'LEMP' || $object == 'lemp') { $objects[$object] = new $object; return $objects[$object]; } else { $object_name = $object; $object = get_config ('objects', $object); $object['name'] = $object_name; $object['class'] = $object_name; } } if ($object['name'] != '' && isset($objects[$object['name']])) { return $objects[$object['name']]; } if ($object['name'] == '') { $object['name'] = 'Nameless object instance #' . ++$auto_name['nameless']; } elseif ($object['mode'] == 'multi') { $object['name'] = "{$object['name']} object instance #" . ++$auto_name[$object['name']]; } if (!empty ($object['file']) && $object['file'] != 'lemp.php') { load_module ($object['name']); } if (empty ($object['class'])) { if (!empty($object['file']) && !empty($object['name'])) { list ($name) = explode ('.', $object['file']); if ($name == $object['name']) { $object['class'] = $object['name']; } } } if (!class_exists($object['class'])) { return error_add ("Class ({$object['name']}) does not exist", __FILE__, __LINE__); } if (isset ($object['args'])) { $args = array_merge($args, $object['args']); } $objects[$object['name']] = new $object['class'] ($args); // $objects[$object['name']] = (!empty($object['args']))? new $object['class'] ($object['args']) : new $object['class']; return $objects[$object['name']]; } /** * load_module() Loads a module. * * @access private * @param string $object_name * @return bool Successful | Not Successful */ function load_module ($module_name = '') { static $loaded = array ('lemp', 'LEMP'); if (empty ($module_name) || !is_string ($module_name)) { return error_add("Invalid parameter for load_module ($module_name)", __FILE__, __LINE__); } elseif (in_array ($module_name, $loaded)) { return true; // Should give out a notice that module is already loaded. } // NOTE: $object is short for object schema and is not an actual object $object = (is_array($module_name))? $module_name : get_config ('objects', $module_name); if (!is_array($object)) { return error_add("Can not find module schematics for load_module ($module_name)", __FILE__, __LINE__); } if (!empty($object['parent']) && is_array ($object['parent'])) { $object_parents = array_diff($object['parent'], $loaded); foreach ($object_parents as $object_parent) { load_module ($object_parent); } } if (file_exists (LEMP_PATH_TO_LIBRARY . '/' . $object['file'])) { $include_path = LEMP_PATH_TO_LIBRARY . '/' . $object['file']; } elseif (file_exists ($object['file'])) { $include_path = $object['file']; } else { return error_add ("Could not locate class file {$object['file']} of module $module_name)", __FILE__, __LINE__); } include_once ($include_path); $loaded[] = $module_name; return true; } /** * trigger_event() Calls hooks, and passes some information. * * Examples assume that you are using the user manage module. * * ex 1) If you want to add a row to a table when a user logs in, you * would first create an entry $trigger['hooks']['login'] = 'mymodulename_login' in the * config, then create a function called mymodulename_login. LEMP calls * trigger_event('login') whenever a user logs in. $trigger['hooks']['login'] could also * be an array. * * ex 2) If you want to add a row to a table when a user logs in, or joins, you * would first create an entry $trigger['prefixes'][] = 'mymodulename_login' in the * config, then create the functions called mymodulename_login, mymodulename_create_user. * LEMP calls trigger_event('login') whenever a user logs in and * trigger_event('create_user') when a user joins. * * You will have to look at LEMP's sourcecode to find when events are triggered (pre or * post events). * * @param string $event What just happened? * @param array $args This is passed to the hooks * @param bool $limit Set to 1 if you only want one event to be called * @return mixed If limit is set to false, trigger_event will return an array containing the return of all the events called. * If limit is set to true, trigger_event will call one only event, and return with the event's return. */ function trigger_event ($event, $args = array(), $limit = false) { static $trigger = ''; if ($trigger == '') { $trigger = get_config ('trigger'); } if (empty ($trigger) || (empty ($trigger[$event]['hooks']) && empty ($trigger[$event]['include']))) { return; } if (is_array($trigger[$event]['include'])) { foreach ($trigger[$event]['include'] as $include) { if (file_exists($include)) { include_once ($include); } elseif (file_exists(LEMP_PATH_TO_LIBRARY . "/$include")) { include_once (LEMP_PATH_TO_LIBRARY . "/$include"); } } } if ($limit) { if (is_string($trigger[$event]['hooks']) && function_exists ($trigger[$event]['hooks'])) { return call_user_func ($trigger[$event]['hooks'], $args); } elseif (is_array($trigger[$event]['hooks']) && function_exists ($trigger[$event]['hooks'][0])) { return call_user_func ($trigger[$event]['hooks'][0], $args); } } else { $return = array(); if (is_array($trigger[$event]['hooks'])) { foreach ($trigger[$event]['hooks'] as $hook) { if (function_exists ($hook)) { $return[] = call_user_func ($hook, $args); } } } return $return; } } ?>