Documentation is available at functions.php
- <?php
- function core_start() {
- //retrieves form data for use as global variables
- //NOTE: may not be used any more - look for these three variables in executed scripts...
- if( !isset( $GLOBALS['core'] ) ) { $GLOBALS['core'] = new core; }
- if($_GET['action'] == "activate" ){
- activate_module( $_GET['value'] );
- }
- else if( $_GET['action'] == "deactivate" ) {
- deactivate_module( $_GET['value'] );
- }
- else if( $_GET['action'] == "uninstall" ) {
- uninstall_module( $_GET['value'] );
- }
- system_start();
- }
- function verify_core() {
- //verifies that the core has been set up and is working properly
- if( !isset($GLOBALS['core']) && !$GLOBALS['core'] = new core ) {
- return false;
- }
- global $db;
- $return_bool = true;
- $db->hide_errors();
- //locate module registry
- $db->get_results("SELECT * FROM module_registry");
- if( !$db->col_info ) { $return_bool = false; }
- //locate core settings
- $db->get_results("SELECT * FROM core_settings");
- if( !$db->col_info ) { $return_bool = false; }
- $db->show_errors();
- //verify the data integrity if the databases are set up
- if( $return_bool ) {
- $installed = retrieve_installed_modules();
- $available = retrieve_available_modules();
- $root = get_root();
- //install any new modules if the databases are up and running
- if( count($installed) < count($available) && count($available) ) {
- $installed_location = array();
- if( count($installed) ) {
- foreach( $installed as $module ) {
- array_push( $installed_location, $module['module_location'] );
- }
- }
- $uninstalled_modules = array();
- foreach( $available as $module_location ) {
- if( !in_array( $module_location, $installed_location ) ) {
- array_push( $uninstalled_modules, $module_location );
- }
- }
- if( count($uninstalled_modules) ) {
- foreach( $uninstalled_modules as $module ) {
- install_module( $module, false );
- }
- $installed = retrieve_installed_modules();
- }
- }
- //remove any modules from registry whose files are missing on the server
- if( count($installed) > count($available) && count($installed) ) {
- $damaged_modules = array();
- foreach( $installed as $module ){
- if( !in_array( $module['module_name'], $available ) ) {
- uninstall_module( $module['module_name'], false );
- }
- }
- $installed = retrieve_installed_modules();
- $available = retrieve_available_modules();
- }
- //execute verification function of any active modules, deactivate if problem
- if( count($installed) ) {
- foreach( $installed as $module ){
- include_once $root."Modules/".$module['module_location']."/Includes/module_functions.php";
- if( function_exists( $module['module_location'].'_verify_module' ) ) {
- eval('$valid = '.$module['module_location'].'_verify_module();');
- }
- if( !$valid ) { deactivate_module( $module['module_name'] ); }
- }
- }
- }
- return $return_bool;
- }
- function initialize_core() {
- function allowed() { return true; }
- //re-initializes the core
- global $db;
- //drop existing tables if they're there
- $db->hide_errors();
- $db->get_results("SELECT * FROM module_registry");
- if( !$db->col_info ) {
- $db->query("
- CREATE TABLE module_registry
- (
- id int(3),
- module_name char(30),
- module_description text,
- module_activated bool,
- module_icon text,
- module_page char(30),
- module_location char(30),
- module_object_types text
- )
- ");
- }
- $db->get_results("SELECT * FROM core_settings");
- if( !$db->col_info ) {
- $db->query("
- CREATE TABLE core_settings
- (
- name char(40),
- value text
- )
- ");
- }
- $db->show_errors();
- //initialize tables
- $object_types = array(
- 'table_name' => 'object_types',
- 'table' => array ( 'id' => 'int', 'name' => 'char(30)', 'description' => 'text', 'table_name' => 'text', 'class_name'=>'char(30)', 'icon' => 'text' )
- );
- insert_db_table( $object_types );
- $relationship_types = array(
- 'table_name' => 'relationship_types',
- 'table' => array ( 'id' => 'int', 'name_foreward' => 'text', 'name_reverse' => 'text', 'description' => 'text', 'icon'=>'text' )
- );
- insert_db_table( $relationship_types );
- $relationships = array(
- 'table_name' => 'relationships',
- 'table' => array ( 'id' => 'int', 'typeID' => 'int', 'primaryID' => 'int', 'secondaryID' => 'int' )
- );
- insert_db_table( $relationships );
- //insert initial settings
- $db->query( "
- INSERT INTO core_settings
- VALUES ( 'core_counter', 7 )
- " );
- $db->query( "
- INSERT INTO core_settings
- VALUES ( 'module_counter', 1 )
- " );
- $db->query( "
- INSERT INTO core_settings
- (name) VALUES ( 'system_module_name' )
- " );
- $db->query( "
- INSERT INTO core_settings
- (name) VALUES ( 'system_module_version' )
- " );
- $db->query( "
- INSERT INTO core_settings
- (name, value) VALUES ( 'start_page', 'core.php' )
- " );
- $GLOBALS['core'] = new core;
- return verify_core();
- }
- function retrieve_installed_modules() {
- //returns a list of modules in the registry
- global $db;
- $db->hide_errors();
- $return_array = $db->get_results( "SELECT * FROM module_registry", ARRAY_A );
- $db->show_errors();
- return $return_array;
- }
- function retrieve_available_modules() {
- //returns a list of modules in the modules folder
- $root = get_root();
- // scan the modules folder for subfolders, return a list of such folders
- $return_array = array();
- if ($handle = opendir($root.'Modules')) {
- while (false !== ($file = readdir($handle))) {
- if ($file != "." && $file != ".." && is_dir( $root.'Modules/'.$file ) ) {
- array_push( $return_array, $file );
- }
- }
- closedir($handle);
- }
- return $return_array;
- }
- function core_get_active_modules( $field = NULL ) {
- //returns a list of active modules (modules being executed by the script)
- global $db;
- $selectField = $field ? ( "id, ".$field ) : "*";
- $db->hide_errors();
- $return_array = $db->get_results( "SELECT ".$selectField." FROM module_registry WHERE module_activated = 1", ARRAY_A );
- $db->show_errors();
- return $return_array;
- }
- function core_satisfied( &$constraint, &$object ) {
- //returns true if the given object satisfies the given constraint
- /*
- * NOTE: i'm going to ignore encapsulation here out of laziness...
- * this function probably doesn't work at all right now, but i'm going to leave it
- * when you come back - check the test_object_constraint and test_relationship_constraint functions
- */
- $satisfied = true;
- //if constraint object type isn't defined or if it is the same as the object
- if( $constraint->object_constraints) {
- if( !in_array( 'type', $constraint->object_constraints[0] ) || ( in_array( 'type', $constraint->object_constraints[0] ) && test_object_constraint( $object->type->type_name, $constraint->object_constraints[0]['operator'], $constraint->object_constraints[0]['value'] ) ) ) {
- foreach( $constraint->object_constraints as $object_constraint ) {
- $field = $object_constraint['attribute'];
- if( $field != 'type' ) {
- $satisfied = test_object_constraint( $object->$field, $object_constraint['operator'], $object_constraint['value'] );
- }
- }
- } else if( in_array( 'type', $constraint->object_constraints[0] ) ) {
- return false;
- }
- }
- if( $satisfied && count($constraint->relationship_constraints) ) {
- if( $relationships = get_relationships( $object->id ) ) {
- //NOTE: this tests all the relationships at once rather than iteratively as objects are
- $satisfied = $constraint->test_relationship_constraints( $object );
- } else {
- return false;
- }
- }
- if( $satisfied ) {
- return true;
- } else {
- return false;
- }
- }
- function test_object_constraint( $attribute, $operator, $value ) {
- //executes a constraint test for object constraints - used in place of eval for security
- if( $operator == "=" ) {
- return $attribute == $value;
- } else if( $operator == "!=" ) {
- return $attribute != $value;
- } else if( $operator == ">" ) {
- return $attribute > $value;
- } else if( $operator == "<" ) {
- return $attribute < $value;
- } else if( $operator == ">=" ) {
- return $attribute >= $value;
- } else if( $operator == "<=" ) {
- return $attribute <= $value;
- }
- }
- function test_relationship_constraint( $relationships, &$constraintFull ) {
- //executes a constraint test for relationship constraints
- $returnArray;
- $validRelationships = array();
- foreach( $constraintFull->relationship_constraints as $constraint ) {
- if( $constraint['type'] ) {
- //if the relationship type is specified but doesn't exist return false
- if( count($relationships) ) { foreach( $relationships as $relationship ) {
- if( $relationship->typeID == $constraint['type'] ) {
- array_push( $validRelationships, $relationship );
- }
- } }
- if( !count($validRelationships) ) {
- return false;
- }
- }
- if( $constraint['position'] == "primary" ) {
- $testBool = false;
- foreach( $validRelationships as $validRelationship ) {
- $relatedObject = $validRelationship->secondaryID;
- //test if the related constraint is satisfied by the related object
- if( $constraint['related'] && $relatedObject && core_satisfied( $constraint['related'], $relatedObject ) ) {
- $testBool = true;
- }
- if( $relatedObject && !$constraint['related'] ) {$testBool = true;}
- }
- if( !$testBool ) { return false; }
- } else if( $constraint['position'] == "secondary" ) {
- $testBool = false;
- foreach( $validRelationships as $validRelationship ) {
- $relatedObject = $validRelationship->primaryID;
- //test if the related constraint is satisfied by the related object
- if( $constraint['related'] && $relatedObject && core_satisfied( $constraint['related'], $relatedObject ) ) {
- $testBool = true;
- }
- if( $relatedObject && !$constraint['related'] ) {$testBool = true;}
- }
- if( !$testBool ) { return false; }
- } else if( $constraint['related'] && ( !$constraint['position'] || $constraint['position'] == "either" ) ) {
- $testBool = false;
- foreach( $validRelationships as $validRelationship ) {
- $relatedPrimaryObject = $validRelationship->primaryID;
- $relatedSecondaryObject = $validRelationship->primaryID;
- //test if the related constraint is satisfied by the related object
- if( core_satisfied( $constraint['related'], $relatedPrimaryObject ) ) {
- $testBool = true;
- } else if( core_satisfied( $constraint['related'], $relatedSecondaryObject ) ) {
- $testBool = true;
- }
- }
- if( !$testBool ) { return false; }
- }
- }
- return true;
- }
- function install_module( $modulePath, $activate = true ) {
- //enters the module's information into the module registry
- global $db;
- $root = get_root();
- $module_activation = $activate;
- //include the module's function file
- if( file_exists( $root."Modules/".$modulePath."/Includes/module_functions.php" ) ) {
- include_once $root."Modules/".$modulePath."/Includes/module_functions.php";
- //get the module's information
- if( function_exists( $modulePath.'_module_info' ) ) {
- eval('$module_info = '.$modulePath.'_module_info();');
- }
- //insert the module into the registry and run self activation function if active
- $module_ID = $db->get_var( "SELECT value FROM core_settings WHERE name = 'module_counter'" );
- $db->query( "
- INSERT INTO module_registry ( id, module_name, module_description, module_activated, module_icon, module_page, module_location, module_object_types )
- VALUES ( '".$module_ID."', '".$module_info['module_name']."', '".$module_info['module_description']."', '".$module_activation."', '".$module_info['module_icon']."', '".$module_info['module_page']."', '".$module_info['module_location']."', '".serialize($module_info['module_object_types'])."' )
- " );
- $db->query( "UPDATE core_settings SET value = '".($module_ID + 1)."' WHERE name = 'module_counter'" );
- if( $activate && function_exists( $modulePath.'_activate_module' ) ){
- eval( $modulePath.'_activate_module();');
- }
- //special processes for system modules
- if( $module_info['system_module'] ) {
- if( $module_info['system_module'] > $db->get_var( "SELECT value FROM core_settings WHERE name = 'system_module_version'" ) ) {
- deactivate_module( "system" );
- }
- $db->query( "UPDATE core_settings SET value = '".$module_info['module_name']."' WHERE name = 'system_module_name'" );
- $db->query( "UPDATE core_settings SET value = '".$module_info['system_module']."' WHERE name = 'system_module_version'" );
- activate_module( $module_info['module_name'] );
- }
- }
- }
- function activate_module( $moduleName ) {
- //activates the module
- global $db;
- $root = $_SERVER['DOCUMENT_ROOT'].'/pi/';
- //retrieve the module's information
- $moduleInfo = reset($db->get_results("SELECT * FROM module_registry WHERE module_name = '".$moduleName."'", ARRAY_A ));
- //if the module isn't active, activate it, otherwise return true
- if( !$moduleInfo['module_activated'] ) {
- //include the module's functions
- include_once $root."Modules/".$moduleInfo['module_location']."/Includes/module_functions.php";
- //tell the module to activate itself
- if( function_exists( $moduleInfo['module_location'].'_activate_module' ) ) {
- $evalString = '$module_activated = '.$moduleInfo['module_location'].'_activate_module();';
- eval( $evalString );
- }
- //if that went ok, update the registry to show the module is active
- if( $module_activated ){
- $db->query( "UPDATE module_registry SET module_activated = '1' WHERE module_name = '".$moduleName."'" );
- return true;
- }
- //if not, return false
- return false;
- }
- return true;
- }
- function deactivate_module( $moduleName ) {
- //deactivates the specified module
- global $db;
- $root = get_root();
- //retrieve the module's information
- $moduleInfo = $db->get_results("SELECT module_activated, module_location FROM module_registry WHERE module_name = '".$moduleName."'", ARRAY_A );
- //if the module is active, deactivate it, otherwise return true
- if( $moduleInfo[0]['module_activated'] ) {
- //include the module's functions
- include_once $root."Modules/".$moduleInfo[0]['module_location']."/Includes/module_functions.php";
- //tell the module to activate itself
- if( function_exists( $moduleInfo[0]['module_location'].'_deactivate_module' ) ) {
- eval('$module_activated = '.$moduleInfo[0]['module_location'].'_deactivate_module();');
- }
- //if that went ok, update the registry to show the module is active
- if( $module_activated ){
- $db->query( "UPDATE module_registry SET module_activated = '0' WHERE module_name = '".$moduleName."'" );
- return true;
- }
- //if not, return false
- return false;
- }
- return true;
- }
- function uninstall_module( $moduleName, $backupFiles = true ) {
- //removes the specified module from the Modules folder (if backup specified moves it to the core/backup folder
- global $db;
- $root = get_root();
- //remove the module from the registry
- $modulePath = $db->get_var( "SELECT module_location FROM module_registry WHERE module_name = '".$moduleName."'" );
- $db->query( "DELETE FROM module_registry WHERE module_name = '".$moduleName."'" );
- //remove the module's files if they exist, backing up if specified
- if( file_exists( $root."Modules/".$modulePath ) ) {
- if( $backupFiles ) {
- copy_folder( $root."Modules/".$modulePath, "./Core_Resources/Backup_Data/Modules/".$modulePath );
- }
- delete_directory( $root."Modules/".$modulePath );
- }
- }
- function get_core_settings() {
- //returns an array of all defined core settings
- global $db;
- return $db->get_results( "SELECT name, value FROM core_settings", ARRAY_A );
- }
- function delete_directory( $path ) {
- //deletes the given folder and recursively removes any subfolders and files
- if ($handle = opendir($path)) {
- while (false !== ($file = readdir($handle))) {
- if ($file != "." && $file != ".." ) {
- if( is_dir( $path."/".$file ) ) {
- delete_directory( $path."/".$file );
- } else {
- unlink( $path."/".$file );
- }
- }
- }
- closedir($handle);
- }
- rmdir( $path );
- }
- function copy_folder( $srcPath, $destPath ) {
- //copies the specified folder to the specified path (recursive)
- mkdir( $destPath );
- if ($handle = opendir($srcPath)) {
- while (false !== ($file = readdir($handle))) {
- if ($file != "." && $file != ".." ) {
- if( is_dir( $srcPath."/".$file ) ) {
- copy_folder( $srcPath."/".$file, $destPath."/".$file );
- } else {
- copy( $srcPath."/".$file, $destPath."/".$file );
- }
- }
- }
- closedir($handle);
- }
- }
- function get_root() {
- //returns the site's root directory
- return $_SERVER['DOCUMENT_ROOT'].'/pi/';
- }
- function in_array_deep( $needle, $haystack, $searchkeys = false ) {
- //searches the array for the value recursively
- if( count( $haystack && !$searchkeys ) ) {
- foreach( $haystack as $hay ) {
- if( is_array( $hay) ) {
- if( in_array_deep( $needle, $hay, $searchkeys ) ) { return true; }
- } else if( $hay == $needle ) { return true; }
- }
- } else if( count( $haystack && $searchkeys ) ) {
- foreach( $haystack as $hay ) {
- if( is_array( $hay) ) {
- if( in_array_deep( $needle, $hay, $searchkeys ) ) { return true; }
- } else if( key($haystack) == $needle ) { return true; }
- }
- }
- return false;
- }
- function stripslashes_deep($value) {
- //removes slashes recursively
- $value = is_array($value) ?
- array_map('stripslashes_deep', $value) :
- stripslashes($value);
- return $value;
- }
- function addslashes_deep($value) {
- //adds slashes to any suspicious characters
- $value = is_array($value) ?
- array_map('addslashes_deep', $value) :
- addslashes($value);
- return $value;
- }
- function clean( $value ) {
- //ensures that the given value is safe to put in a mySQL statement
- //NOTE: this may need to remove code capable of inserting malicious javascript, but for now i'll leave it alone
- return addslashes($value);
- }
- function unclean( $value ) {
- return stripslashes( $value );
- }
- function toHTML( $value ) {
- if( !value ) { return $value; }
- else if( is_array( $value ) ) { return stripslashes_deep($value); }
- else { return stripslashes( $value ); }
- }
- function toDbDate( $string = NULL ) {
- if( $string ) {
- return date( 'Y-m-d H:i:s', strtotime(stripslashes($string)) );
- }
- }
- function toPhpDate( $string = NULL ) {
- if( $string ) {
- return date( 'D M d, Y', strtotime(stripslashes($string)) );
- }
- }
- function file_to_html( $file ) {
- //converts a string describing a file's location on the server to a string describing the file's location on the internet
- return str_replace( $_SERVER['DOCUMENT_ROOT'], $_SERVER['HTTP_HOST'], $file);
- }
- function html_to_file( $html ) {
- return str_replace( $_SERVER['HTTP_HOST'], $_SERVER['DOCUMENT_ROOT'], $html );
- }
- function format_path( $path ) {
- //convert all slashes to foreward slashes
- $path = str_replace( '\\', '/', $path );
- //change all file references to double foreward slash
- if( strpos( $path, '://' ) === false && strpos( $path, ':/' ) !== false ) {
- $path = str_replace( ':/', '://', $path );
- }
- if( !strstr( $path, ':' ) ) {
- $path = $_SERVER['DOCUMENT_ROOT'].'/'.ltrim( $path, '/' );
- }
- return $path;
- }
- function ifile_exists( $path ) {
- //tests if the given file exists - case insensitive
- //if the file exists it returns the file's name
- $pathParts = pathinfo( $path );
- if ($handle = opendir( $pathParts['dirname'] )) {
- while (false !== ($file = readdir($handle))) {
- if( !strcasecmp( $pathParts['basename'], $file ) && !is_dir( $pathParts['dirname']."/".$file ) ) { return $file; }
- }
- closedir( $handle );
- }
- return false;
- }
- if (!function_exists('array_diff_key'))
- {
- /**
- * Computes the difference of arrays using keys for comparison
- *
- * @param array $valuesBase Base elements for comparison, associative
- * @param array $valuesComp[,..] Comparison elements, associative
- *
- * @param array Elements, not existing in comparison element, associative
- */
- function array_diff_key(){
- $argCount = func_num_args();
- $argValues = func_get_args();
- $valuesDiff = array();
- if ($argCount < 2) { return false; }
- foreach ($argValues as $argParam) {
- if (!is_array($argParam)) { $argParam = array(); }
- }
- if( is_array( $argValues[0] ) ) {
- foreach ($argValues[0] as $valueKey => $valueData){
- for ($i = 1; $i < $argCount; $i++) {
- if (isset($argValues[$i][$valueKey])) { continue 2; }
- }
- $valuesDiff[$valueKey] = $valueData;
- }
- }
- return $valuesDiff;
- }
- }
- function array_merge_withKeys() {
- $argCount = func_num_args();
- $argValues = func_get_args();
- $returnArray = array();
- if ($argCount < 2) { return $argValues[0]; }
- foreach ($argValues as $argParam) {
- if( is_array( $argParam ) ) {
- foreach( $argParam as $key => $value ) {
- $returnArray[$key] = $value;
- }
- }
- }
- return $returnArray;
- }
- function insert_db_table( $fields ) {
- if( validate_db_table( $fields ) ) {
- return true;
- } else {
- global $db;
- $queryString = "CREATE TABLE ".$fields['table_name']."( ";
- foreach( $fields['table'] as $field => $fieldType ) {
- $queryString .= $field." ".$fieldType.",";
- }
- $queryString = rtrim($queryString, ",").")";
- $db->query( $queryString );
- }
- return validate_db_table( $fields );
- }
- function validate_db_table( $fields ) {
- global $db;
- //get the db results
- $db->hide_errors();
- $exist = $db->get_results("SELECT * FROM ".$fields['table_name'] );
- $db->show_errors();
- if( $db->col_info ) {
- //if the table exists
- foreach( $db->get_col_info( 'name' ) as $existingField ) {
- //and all the specified fields exist
- if( !isset($fields['table'][$existingField]) ) {
- return false;
- }
- }
- } else {
- //if not return false
- return false;
- }
- //return true
- return true;
- }
- function insert_object_type( $fields ) {
- global $db;
- if( !$existing = $db->get_var( "SELECT id FROM object_types WHERE name = '".$fields['name']."'" ) ) {
- $counter = $db->get_var( "SELECT value FROM system_settings WHERE name = 'object_type_counter'" );
- $db->query( "UPDATE system_settings SET value = '".($counter + 1)."' WHERE name = 'object_type_counter'" );
- $db->query( "
- INSERT INTO object_types (id, name, description, table_name, class_name, icon )
- VALUES ( '".$counter."', '".$fields['name']."', '".$fields['description']."', '".$fields['table_name']."', '".$fields['class_name']."', '' )
- " );
- return $counter;
- } else {
- return $existing;
- }
- }
- function insert_relationship_type( $fields ) {
- global $db;
- if( !$existing = $db->get_var( "SELECT id FROM object_types WHERE name = '".$fields['name']."'" ) ) {
- $counter = $db->get_var( "SELECT value FROM system_settings WHERE name = 'object_type_counter'" );
- $db->query( "UPDATE system_settings SET value = '".($counter + 1)."' WHERE name = 'object_type_counter'" );
- $db->query( "
- INSERT INTO relationship_types (id, name_foreward, name_reverse, description, icon )
- VALUES ( '".$counter."', '".$fields['name_foreward']."', '".$fields['name_reverse']."', '".$fields['description']."', '' )
- " );
- return $counter;
- } else {
- return $existing;
- }
- }
- function new_constraint( $valueArray) {
- $constraint =& new constraint;
- $constraint->verify_set( $valueArray );
- return $constraint->get_attribute( 'id' );
- }
- function to_phone( $input ) {
- return $input;
- }
- ?>
Documentation generated on Tue, 24 May 2005 03:57:57 -0400 by phpDocumentor 1.3.0RC3