Source for file functions.php

Documentation is available at functions.php

  1. <?php
  2.  
  3.  
  4. function core_start() {
  5.  
  6. //retrieves form data for use as global variables
  7.  
  8. //NOTE: may not be used any more - look for these three variables in executed scripts...
  9.  
  10.  
  11.  
  12. if( !isset( $GLOBALS['core'] ) ) { $GLOBALS['core'] = new core; }
  13.  
  14.  
  15. if($_GET['action'] == "activate" ){
  16.  
  17. activate_module( $_GET['value'] );
  18.  
  19. }
  20.  
  21. else if( $_GET['action'] == "deactivate" ) {
  22.  
  23. deactivate_module( $_GET['value'] );
  24.  
  25. }
  26.  
  27. else if( $_GET['action'] == "uninstall" ) {
  28.  
  29. uninstall_module( $_GET['value'] );
  30.  
  31. }
  32.  
  33.  
  34. system_start();
  35.  
  36. }
  37.  
  38.  
  39.  
  40. function verify_core() {
  41.  
  42. //verifies that the core has been set up and is working properly
  43.  
  44.  
  45.  
  46. if( !isset($GLOBALS['core']) && !$GLOBALS['core'] = new core ) {
  47.  
  48. return false;
  49.  
  50. }
  51.  
  52.  
  53.  
  54. global $db;
  55.  
  56.  
  57.  
  58. $return_bool = true;
  59.  
  60. $db->hide_errors();
  61.  
  62.  
  63. //locate module registry
  64.  
  65. $db->get_results("SELECT * FROM module_registry");
  66.  
  67. if( !$db->col_info ) { $return_bool = false; }
  68.  
  69.  
  70.  
  71. //locate core settings
  72.  
  73. $db->get_results("SELECT * FROM core_settings");
  74.  
  75. if( !$db->col_info ) { $return_bool = false; }
  76.  
  77.  
  78. $db->show_errors();
  79.  
  80.  
  81.  
  82. //verify the data integrity if the databases are set up
  83.  
  84. if( $return_bool ) {
  85.  
  86. $installed = retrieve_installed_modules();
  87.  
  88. $available = retrieve_available_modules();
  89.  
  90. $root = get_root();
  91.  
  92.  
  93.  
  94. //install any new modules if the databases are up and running
  95.  
  96. if( count($installed) < count($available) && count($available) ) {
  97.  
  98. $installed_location = array();
  99.  
  100. if( count($installed) ) {
  101.  
  102. foreach( $installed as $module ) {
  103.  
  104. array_push( $installed_location, $module['module_location'] );
  105.  
  106. }
  107.  
  108. }
  109.  
  110. $uninstalled_modules = array();
  111.  
  112. foreach( $available as $module_location ) {
  113.  
  114. if( !in_array( $module_location, $installed_location ) ) {
  115.  
  116. array_push( $uninstalled_modules, $module_location );
  117.  
  118. }
  119.  
  120. }
  121.  
  122. if( count($uninstalled_modules) ) {
  123.  
  124. foreach( $uninstalled_modules as $module ) {
  125.  
  126. install_module( $module, false );
  127.  
  128. }
  129.  
  130. $installed = retrieve_installed_modules();
  131.  
  132. }
  133.  
  134. }
  135.  
  136.  
  137.  
  138. //remove any modules from registry whose files are missing on the server
  139.  
  140. if( count($installed) > count($available) && count($installed) ) {
  141.  
  142. $damaged_modules = array();
  143.  
  144. foreach( $installed as $module ){
  145.  
  146. if( !in_array( $module['module_name'], $available ) ) {
  147.  
  148. uninstall_module( $module['module_name'], false );
  149.  
  150. }
  151.  
  152. }
  153.  
  154. $installed = retrieve_installed_modules();
  155.  
  156. $available = retrieve_available_modules();
  157.  
  158. }
  159.  
  160.  
  161.  
  162. //execute verification function of any active modules, deactivate if problem
  163.  
  164. if( count($installed) ) {
  165.  
  166. foreach( $installed as $module ){
  167.  
  168. include_once $root."Modules/".$module['module_location']."/Includes/module_functions.php";
  169.  
  170. if( function_exists( $module['module_location'].'_verify_module' ) ) {
  171.  
  172. eval('$valid = '.$module['module_location'].'_verify_module();');
  173.  
  174. }
  175.  
  176. if( !$valid ) { deactivate_module( $module['module_name'] ); }
  177.  
  178. }
  179.  
  180. }
  181.  
  182. }
  183.  
  184. return $return_bool;
  185.  
  186. }
  187.  
  188.  
  189.  
  190. function initialize_core() {
  191.  
  192. function allowed() { return true; }
  193.  
  194.  
  195. //re-initializes the core
  196.  
  197. global $db;
  198.  
  199.  
  200.  
  201. //drop existing tables if they're there
  202.  
  203. $db->hide_errors();
  204.  
  205.  
  206. $db->get_results("SELECT * FROM module_registry");
  207.  
  208. if( !$db->col_info ) {
  209.  
  210. $db->query("
  211.  
  212. CREATE TABLE module_registry
  213.  
  214. (
  215.  
  216. id int(3),
  217.  
  218. module_name char(30),
  219.  
  220. module_description text,
  221.  
  222. module_activated bool,
  223.  
  224. module_icon text,
  225.  
  226. module_page char(30),
  227.  
  228. module_location char(30),
  229.  
  230. module_object_types text
  231.  
  232. )
  233.  
  234. ");
  235.  
  236. }
  237.  
  238.  
  239. $db->get_results("SELECT * FROM core_settings");
  240.  
  241. if( !$db->col_info ) {
  242.  
  243. $db->query("
  244.  
  245. CREATE TABLE core_settings
  246.  
  247. (
  248.  
  249. name char(40),
  250.  
  251. value text
  252.  
  253. )
  254.  
  255. ");
  256.  
  257. }
  258.  
  259. $db->show_errors();
  260.  
  261.  
  262. //initialize tables
  263.  
  264. $object_types = array(
  265.  
  266. 'table_name' => 'object_types',
  267.  
  268. 'table' => array ( 'id' => 'int', 'name' => 'char(30)', 'description' => 'text', 'table_name' => 'text', 'class_name'=>'char(30)', 'icon' => 'text' )
  269.  
  270. );
  271.  
  272. insert_db_table( $object_types );
  273.  
  274.  
  275.  
  276. $relationship_types = array(
  277.  
  278. 'table_name' => 'relationship_types',
  279.  
  280. 'table' => array ( 'id' => 'int', 'name_foreward' => 'text', 'name_reverse' => 'text', 'description' => 'text', 'icon'=>'text' )
  281.  
  282. );
  283.  
  284. insert_db_table( $relationship_types );
  285.  
  286.  
  287.  
  288. $relationships = array(
  289.  
  290. 'table_name' => 'relationships',
  291.  
  292. 'table' => array ( 'id' => 'int', 'typeID' => 'int', 'primaryID' => 'int', 'secondaryID' => 'int' )
  293.  
  294. );
  295.  
  296. insert_db_table( $relationships );
  297.  
  298.  
  299.  
  300. //insert initial settings
  301.  
  302. $db->query( "
  303.  
  304. INSERT INTO core_settings
  305.  
  306. VALUES ( 'core_counter', 7 )
  307.  
  308. " );
  309.  
  310.  
  311. $db->query( "
  312.  
  313. INSERT INTO core_settings
  314.  
  315. VALUES ( 'module_counter', 1 )
  316.  
  317. " );
  318.  
  319. $db->query( "
  320.  
  321. INSERT INTO core_settings
  322.  
  323. (name) VALUES ( 'system_module_name' )
  324.  
  325. " );
  326.  
  327. $db->query( "
  328.  
  329. INSERT INTO core_settings
  330.  
  331. (name) VALUES ( 'system_module_version' )
  332.  
  333. " );
  334.  
  335. $db->query( "
  336.  
  337. INSERT INTO core_settings
  338.  
  339. (name, value) VALUES ( 'start_page', 'core.php' )
  340.  
  341. " );
  342.  
  343.  
  344.  
  345. $GLOBALS['core'] = new core;
  346.  
  347.  
  348. return verify_core();
  349.  
  350. }
  351.  
  352.  
  353.  
  354. function retrieve_installed_modules() {
  355.  
  356. //returns a list of modules in the registry
  357.  
  358. global $db;
  359.  
  360. $db->hide_errors();
  361.  
  362. $return_array = $db->get_results( "SELECT * FROM module_registry", ARRAY_A );
  363.  
  364. $db->show_errors();
  365.  
  366. return $return_array;
  367.  
  368. }
  369.  
  370.  
  371.  
  372. function retrieve_available_modules() {
  373.  
  374. //returns a list of modules in the modules folder
  375.  
  376. $root = get_root();
  377.  
  378.  
  379. // scan the modules folder for subfolders, return a list of such folders
  380.  
  381. $return_array = array();
  382.  
  383. if ($handle = opendir($root.'Modules')) {
  384.  
  385. while (false !== ($file = readdir($handle))) {
  386.  
  387. if ($file != "." && $file != ".." && is_dir( $root.'Modules/'.$file ) ) {
  388.  
  389. array_push( $return_array, $file );
  390.  
  391. }
  392.  
  393. }
  394.  
  395. closedir($handle);
  396.  
  397. }
  398.  
  399. return $return_array;
  400.  
  401. }
  402.  
  403.  
  404.  
  405. function core_get_active_modules( $field = NULL ) {
  406.  
  407. //returns a list of active modules (modules being executed by the script)
  408.  
  409. global $db;
  410.  
  411. $selectField = $field ? ( "id, ".$field ) : "*";
  412.  
  413. $db->hide_errors();
  414.  
  415. $return_array = $db->get_results( "SELECT ".$selectField." FROM module_registry WHERE module_activated = 1", ARRAY_A );
  416.  
  417. $db->show_errors();
  418.  
  419. return $return_array;
  420.  
  421. }
  422.  
  423. function core_satisfied( &$constraint, &$object ) {
  424.  
  425. //returns true if the given object satisfies the given constraint
  426.  
  427. /*
  428.  
  429. * NOTE: i'm going to ignore encapsulation here out of laziness...
  430.  
  431. * this function probably doesn't work at all right now, but i'm going to leave it
  432.  
  433. * when you come back - check the test_object_constraint and test_relationship_constraint functions
  434.  
  435. */
  436.  
  437.  
  438. $satisfied = true;
  439.  
  440. //if constraint object type isn't defined or if it is the same as the object
  441.  
  442. if( $constraint->object_constraints) {
  443.  
  444. 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'] ) ) ) {
  445.  
  446. foreach( $constraint->object_constraints as $object_constraint ) {
  447.  
  448. $field = $object_constraint['attribute'];
  449.  
  450. if( $field != 'type' ) {
  451.  
  452. $satisfied = test_object_constraint( $object->$field, $object_constraint['operator'], $object_constraint['value'] );
  453.  
  454. }
  455.  
  456. }
  457.  
  458. } else if( in_array( 'type', $constraint->object_constraints[0] ) ) {
  459.  
  460. return false;
  461.  
  462. }
  463.  
  464. }
  465.  
  466. if( $satisfied && count($constraint->relationship_constraints) ) {
  467.  
  468. if( $relationships = get_relationships( $object->id ) ) {
  469.  
  470. //NOTE: this tests all the relationships at once rather than iteratively as objects are
  471.  
  472. $satisfied = $constraint->test_relationship_constraints( $object );
  473.  
  474. } else {
  475.  
  476. return false;
  477.  
  478. }
  479.  
  480. }
  481.  
  482. if( $satisfied ) {
  483.  
  484. return true;
  485.  
  486. } else {
  487.  
  488. return false;
  489.  
  490. }
  491.  
  492. }
  493.  
  494.  
  495.  
  496. function test_object_constraint( $attribute, $operator, $value ) {
  497.  
  498. //executes a constraint test for object constraints - used in place of eval for security
  499.  
  500.  
  501. if( $operator == "=" ) {
  502.  
  503. return $attribute == $value;
  504.  
  505. } else if( $operator == "!=" ) {
  506.  
  507. return $attribute != $value;
  508.  
  509. } else if( $operator == ">" ) {
  510.  
  511. return $attribute > $value;
  512.  
  513. } else if( $operator == "<" ) {
  514.  
  515. return $attribute < $value;
  516.  
  517. } else if( $operator == ">=" ) {
  518.  
  519. return $attribute >= $value;
  520.  
  521. } else if( $operator == "<=" ) {
  522.  
  523. return $attribute <= $value;
  524.  
  525. }
  526.  
  527. }
  528.  
  529.  
  530.  
  531. function test_relationship_constraint( $relationships, &$constraintFull ) {
  532.  
  533. //executes a constraint test for relationship constraints
  534.  
  535. $returnArray;
  536.  
  537. $validRelationships = array();
  538.  
  539. foreach( $constraintFull->relationship_constraints as $constraint ) {
  540.  
  541. if( $constraint['type'] ) {
  542.  
  543. //if the relationship type is specified but doesn't exist return false
  544.  
  545. if( count($relationships) ) { foreach( $relationships as $relationship ) {
  546.  
  547. if( $relationship->typeID == $constraint['type'] ) {
  548.  
  549. array_push( $validRelationships, $relationship );
  550.  
  551. }
  552.  
  553. } }
  554.  
  555. if( !count($validRelationships) ) {
  556.  
  557. return false;
  558.  
  559. }
  560.  
  561. }
  562.  
  563. if( $constraint['position'] == "primary" ) {
  564.  
  565. $testBool = false;
  566.  
  567.  
  568. foreach( $validRelationships as $validRelationship ) {
  569.  
  570. $relatedObject = $validRelationship->secondaryID;
  571.  
  572. //test if the related constraint is satisfied by the related object
  573.  
  574. if( $constraint['related'] && $relatedObject && core_satisfied( $constraint['related'], $relatedObject ) ) {
  575.  
  576. $testBool = true;
  577.  
  578. }
  579.  
  580. if( $relatedObject && !$constraint['related'] ) {$testBool = true;}
  581.  
  582. }
  583.  
  584. if( !$testBool ) { return false; }
  585.  
  586. } else if( $constraint['position'] == "secondary" ) {
  587.  
  588. $testBool = false;
  589.  
  590.  
  591. foreach( $validRelationships as $validRelationship ) {
  592.  
  593. $relatedObject = $validRelationship->primaryID;
  594.  
  595. //test if the related constraint is satisfied by the related object
  596.  
  597. if( $constraint['related'] && $relatedObject && core_satisfied( $constraint['related'], $relatedObject ) ) {
  598.  
  599. $testBool = true;
  600.  
  601. }
  602.  
  603. if( $relatedObject && !$constraint['related'] ) {$testBool = true;}
  604.  
  605. }
  606.  
  607. if( !$testBool ) { return false; }
  608.  
  609. } else if( $constraint['related'] && ( !$constraint['position'] || $constraint['position'] == "either" ) ) {
  610.  
  611. $testBool = false;
  612.  
  613.  
  614. foreach( $validRelationships as $validRelationship ) {
  615.  
  616. $relatedPrimaryObject = $validRelationship->primaryID;
  617.  
  618. $relatedSecondaryObject = $validRelationship->primaryID;
  619.  
  620.  
  621.  
  622. //test if the related constraint is satisfied by the related object
  623.  
  624. if( core_satisfied( $constraint['related'], $relatedPrimaryObject ) ) {
  625.  
  626. $testBool = true;
  627.  
  628. } else if( core_satisfied( $constraint['related'], $relatedSecondaryObject ) ) {
  629.  
  630. $testBool = true;
  631.  
  632. }
  633.  
  634. }
  635.  
  636. if( !$testBool ) { return false; }
  637.  
  638. }
  639.  
  640. }
  641.  
  642. return true;
  643.  
  644. }
  645.  
  646. function install_module( $modulePath, $activate = true ) {
  647.  
  648. //enters the module's information into the module registry
  649.  
  650. global $db;
  651.  
  652. $root = get_root();
  653.  
  654. $module_activation = $activate;
  655.  
  656.  
  657.  
  658. //include the module's function file
  659. if( file_exists( $root."Modules/".$modulePath."/Includes/module_functions.php" ) ) {
  660. include_once $root."Modules/".$modulePath."/Includes/module_functions.php";
  661.  
  662. //get the module's information
  663. if( function_exists( $modulePath.'_module_info' ) ) {
  664. eval('$module_info = '.$modulePath.'_module_info();');
  665. }
  666. //insert the module into the registry and run self activation function if active
  667. $module_ID = $db->get_var( "SELECT value FROM core_settings WHERE name = 'module_counter'" );
  668. $db->query( "
  669. INSERT INTO module_registry ( id, module_name, module_description, module_activated, module_icon, module_page, module_location, module_object_types )
  670. 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'])."' )
  671. " );
  672. $db->query( "UPDATE core_settings SET value = '".($module_ID + 1)."' WHERE name = 'module_counter'" );
  673. if( $activate && function_exists( $modulePath.'_activate_module' ) ){
  674. eval( $modulePath.'_activate_module();');
  675. }
  676. //special processes for system modules
  677. if( $module_info['system_module'] ) {
  678. if( $module_info['system_module'] > $db->get_var( "SELECT value FROM core_settings WHERE name = 'system_module_version'" ) ) {
  679. deactivate_module( "system" );
  680. }
  681. $db->query( "UPDATE core_settings SET value = '".$module_info['module_name']."' WHERE name = 'system_module_name'" );
  682. $db->query( "UPDATE core_settings SET value = '".$module_info['system_module']."' WHERE name = 'system_module_version'" );
  683. activate_module( $module_info['module_name'] );
  684. }
  685. }
  686.  
  687. }
  688.  
  689.  
  690.  
  691. function activate_module( $moduleName ) {
  692.  
  693. //activates the module
  694.  
  695. global $db;
  696.  
  697.  
  698.  
  699. $root = $_SERVER['DOCUMENT_ROOT'].'/pi/';
  700.  
  701.  
  702.  
  703. //retrieve the module's information
  704.  
  705. $moduleInfo = reset($db->get_results("SELECT * FROM module_registry WHERE module_name = '".$moduleName."'", ARRAY_A ));
  706.  
  707.  
  708.  
  709. //if the module isn't active, activate it, otherwise return true
  710.  
  711. if( !$moduleInfo['module_activated'] ) {
  712.  
  713. //include the module's functions
  714.  
  715. include_once $root."Modules/".$moduleInfo['module_location']."/Includes/module_functions.php";
  716.  
  717.  
  718. //tell the module to activate itself
  719.  
  720. if( function_exists( $moduleInfo['module_location'].'_activate_module' ) ) {
  721.  
  722. $evalString = '$module_activated = '.$moduleInfo['module_location'].'_activate_module();';
  723.  
  724. eval( $evalString );
  725.  
  726. }
  727.  
  728. //if that went ok, update the registry to show the module is active
  729.  
  730. if( $module_activated ){
  731.  
  732. $db->query( "UPDATE module_registry SET module_activated = '1' WHERE module_name = '".$moduleName."'" );
  733.  
  734. return true;
  735.  
  736. }
  737.  
  738.  
  739. //if not, return false
  740.  
  741. return false;
  742.  
  743. }
  744.  
  745. return true;
  746.  
  747. }
  748.  
  749.  
  750.  
  751. function deactivate_module( $moduleName ) {
  752.  
  753. //deactivates the specified module
  754.  
  755. global $db;
  756.  
  757. $root = get_root();
  758.  
  759.  
  760.  
  761. //retrieve the module's information
  762.  
  763. $moduleInfo = $db->get_results("SELECT module_activated, module_location FROM module_registry WHERE module_name = '".$moduleName."'", ARRAY_A );
  764.  
  765.  
  766.  
  767. //if the module is active, deactivate it, otherwise return true
  768.  
  769. if( $moduleInfo[0]['module_activated'] ) {
  770.  
  771. //include the module's functions
  772.  
  773. include_once $root."Modules/".$moduleInfo[0]['module_location']."/Includes/module_functions.php";
  774.  
  775.  
  776. //tell the module to activate itself
  777.  
  778. if( function_exists( $moduleInfo[0]['module_location'].'_deactivate_module' ) ) {
  779.  
  780. eval('$module_activated = '.$moduleInfo[0]['module_location'].'_deactivate_module();');
  781.  
  782. }
  783.  
  784.  
  785. //if that went ok, update the registry to show the module is active
  786.  
  787. if( $module_activated ){
  788.  
  789. $db->query( "UPDATE module_registry SET module_activated = '0' WHERE module_name = '".$moduleName."'" );
  790.  
  791. return true;
  792.  
  793. }
  794.  
  795.  
  796. //if not, return false
  797.  
  798. return false;
  799.  
  800. }
  801.  
  802. return true;
  803.  
  804. }
  805.  
  806.  
  807.  
  808. function uninstall_module( $moduleName, $backupFiles = true ) {
  809.  
  810. //removes the specified module from the Modules folder (if backup specified moves it to the core/backup folder
  811.  
  812. global $db;
  813.  
  814. $root = get_root();
  815.  
  816.  
  817. //remove the module from the registry
  818.  
  819. $modulePath = $db->get_var( "SELECT module_location FROM module_registry WHERE module_name = '".$moduleName."'" );
  820.  
  821. $db->query( "DELETE FROM module_registry WHERE module_name = '".$moduleName."'" );
  822.  
  823.  
  824. //remove the module's files if they exist, backing up if specified
  825.  
  826. if( file_exists( $root."Modules/".$modulePath ) ) {
  827.  
  828. if( $backupFiles ) {
  829.  
  830. copy_folder( $root."Modules/".$modulePath, "./Core_Resources/Backup_Data/Modules/".$modulePath );
  831.  
  832. }
  833.  
  834. delete_directory( $root."Modules/".$modulePath );
  835.  
  836. }
  837.  
  838. }
  839.  
  840.  
  841.  
  842. function get_core_settings() {
  843.  
  844. //returns an array of all defined core settings
  845.  
  846. global $db;
  847.  
  848.  
  849.  
  850. return $db->get_results( "SELECT name, value FROM core_settings", ARRAY_A );
  851.  
  852. }
  853.  
  854.  
  855.  
  856. function delete_directory( $path ) {
  857.  
  858. //deletes the given folder and recursively removes any subfolders and files
  859.  
  860. if ($handle = opendir($path)) {
  861.  
  862. while (false !== ($file = readdir($handle))) {
  863.  
  864. if ($file != "." && $file != ".." ) {
  865.  
  866. if( is_dir( $path."/".$file ) ) {
  867.  
  868. delete_directory( $path."/".$file );
  869.  
  870. } else {
  871.  
  872. unlink( $path."/".$file );
  873.  
  874. }
  875.  
  876. }
  877.  
  878. }
  879.  
  880. closedir($handle);
  881.  
  882. }
  883.  
  884. rmdir( $path );
  885.  
  886. }
  887.  
  888.  
  889.  
  890. function copy_folder( $srcPath, $destPath ) {
  891.  
  892. //copies the specified folder to the specified path (recursive)
  893.  
  894. mkdir( $destPath );
  895.  
  896. if ($handle = opendir($srcPath)) {
  897.  
  898. while (false !== ($file = readdir($handle))) {
  899.  
  900. if ($file != "." && $file != ".." ) {
  901.  
  902. if( is_dir( $srcPath."/".$file ) ) {
  903.  
  904. copy_folder( $srcPath."/".$file, $destPath."/".$file );
  905.  
  906. } else {
  907.  
  908. copy( $srcPath."/".$file, $destPath."/".$file );
  909.  
  910. }
  911.  
  912. }
  913.  
  914. }
  915.  
  916. closedir($handle);
  917.  
  918. }
  919.  
  920. }
  921.  
  922.  
  923.  
  924. function get_root() {
  925.  
  926. //returns the site's root directory
  927.  
  928. return $_SERVER['DOCUMENT_ROOT'].'/pi/';
  929.  
  930. }
  931.  
  932.  
  933.  
  934. function in_array_deep( $needle, $haystack, $searchkeys = false ) {
  935.  
  936. //searches the array for the value recursively
  937.  
  938. if( count( $haystack && !$searchkeys ) ) {
  939.  
  940. foreach( $haystack as $hay ) {
  941.  
  942. if( is_array( $hay) ) {
  943.  
  944. if( in_array_deep( $needle, $hay, $searchkeys ) ) { return true; }
  945.  
  946. } else if( $hay == $needle ) { return true; }
  947.  
  948. }
  949.  
  950. } else if( count( $haystack && $searchkeys ) ) {
  951.  
  952. foreach( $haystack as $hay ) {
  953.  
  954. if( is_array( $hay) ) {
  955.  
  956. if( in_array_deep( $needle, $hay, $searchkeys ) ) { return true; }
  957.  
  958. } else if( key($haystack) == $needle ) { return true; }
  959.  
  960. }
  961.  
  962. }
  963.  
  964.  
  965.  
  966.  
  967. return false;
  968.  
  969. }
  970.  
  971. function stripslashes_deep($value) {
  972.  
  973. //removes slashes recursively
  974.  
  975. $value = is_array($value) ?
  976.  
  977. array_map('stripslashes_deep', $value) :
  978.  
  979. stripslashes($value);
  980.  
  981. return $value;
  982.  
  983. }
  984.  
  985.  
  986.  
  987. function addslashes_deep($value) {
  988.  
  989. //adds slashes to any suspicious characters
  990.  
  991. $value = is_array($value) ?
  992.  
  993. array_map('addslashes_deep', $value) :
  994.  
  995. addslashes($value);
  996.  
  997. return $value;
  998.  
  999. }
  1000.  
  1001.  
  1002.  
  1003. function clean( $value ) {
  1004.  
  1005. //ensures that the given value is safe to put in a mySQL statement
  1006.  
  1007. //NOTE: this may need to remove code capable of inserting malicious javascript, but for now i'll leave it alone
  1008.  
  1009. return addslashes($value);
  1010.  
  1011. }
  1012.  
  1013.  
  1014.  
  1015. function unclean( $value ) {
  1016.  
  1017. return stripslashes( $value );
  1018.  
  1019. }
  1020.  
  1021.  
  1022.  
  1023. function toHTML( $value ) {
  1024.  
  1025. if( !value ) { return $value; }
  1026.  
  1027. else if( is_array( $value ) ) { return stripslashes_deep($value); }
  1028.  
  1029. else { return stripslashes( $value ); }
  1030.  
  1031. }
  1032.  
  1033.  
  1034.  
  1035. function toDbDate( $string = NULL ) {
  1036.  
  1037. if( $string ) {
  1038.  
  1039. return date( 'Y-m-d H:i:s', strtotime(stripslashes($string)) );
  1040.  
  1041. }
  1042.  
  1043. }
  1044.  
  1045.  
  1046.  
  1047. function toPhpDate( $string = NULL ) {
  1048.  
  1049. if( $string ) {
  1050.  
  1051. return date( 'D M d, Y', strtotime(stripslashes($string)) );
  1052.  
  1053. }
  1054.  
  1055. }
  1056.  
  1057.  
  1058.  
  1059. function file_to_html( $file ) {
  1060.  
  1061. //converts a string describing a file's location on the server to a string describing the file's location on the internet
  1062.  
  1063. return str_replace( $_SERVER['DOCUMENT_ROOT'], $_SERVER['HTTP_HOST'], $file);
  1064.  
  1065. }
  1066.  
  1067.  
  1068.  
  1069. function html_to_file( $html ) {
  1070.  
  1071. return str_replace( $_SERVER['HTTP_HOST'], $_SERVER['DOCUMENT_ROOT'], $html );
  1072.  
  1073. }
  1074.  
  1075.  
  1076.  
  1077. function format_path( $path ) {
  1078.  
  1079. //convert all slashes to foreward slashes
  1080.  
  1081. $path = str_replace( '\\', '/', $path );
  1082.  
  1083.  
  1084. //change all file references to double foreward slash
  1085.  
  1086. if( strpos( $path, '://' ) === false && strpos( $path, ':/' ) !== false ) {
  1087.  
  1088. $path = str_replace( ':/', '://', $path );
  1089.  
  1090. }
  1091.  
  1092.  
  1093.  
  1094. if( !strstr( $path, ':' ) ) {
  1095.  
  1096. $path = $_SERVER['DOCUMENT_ROOT'].'/'.ltrim( $path, '/' );
  1097.  
  1098. }
  1099.  
  1100.  
  1101.  
  1102. return $path;
  1103.  
  1104. }
  1105.  
  1106.  
  1107.  
  1108. function ifile_exists( $path ) {
  1109.  
  1110. //tests if the given file exists - case insensitive
  1111.  
  1112. //if the file exists it returns the file's name
  1113.  
  1114. $pathParts = pathinfo( $path );
  1115.  
  1116.  
  1117. if ($handle = opendir( $pathParts['dirname'] )) {
  1118.  
  1119. while (false !== ($file = readdir($handle))) {
  1120.  
  1121. if( !strcasecmp( $pathParts['basename'], $file ) && !is_dir( $pathParts['dirname']."/".$file ) ) { return $file; }
  1122.  
  1123. }
  1124.  
  1125. closedir( $handle );
  1126.  
  1127. }
  1128.  
  1129.  
  1130. return false;
  1131.  
  1132. }
  1133.  
  1134.  
  1135.  
  1136. if (!function_exists('array_diff_key'))
  1137.  
  1138. {
  1139.  
  1140. /**
  1141. * Computes the difference of arrays using keys for comparison
  1142. *
  1143. * @param array $valuesBase Base elements for comparison, associative
  1144. * @param array $valuesComp[,..] Comparison elements, associative
  1145. *
  1146. * @param array Elements, not existing in comparison element, associative
  1147. */
  1148.  
  1149. function array_diff_key(){
  1150.  
  1151. $argCount = func_num_args();
  1152.  
  1153. $argValues = func_get_args();
  1154.  
  1155. $valuesDiff = array();
  1156.  
  1157. if ($argCount < 2) { return false; }
  1158.  
  1159. foreach ($argValues as $argParam) {
  1160.  
  1161. if (!is_array($argParam)) { $argParam = array(); }
  1162.  
  1163. }
  1164.  
  1165. if( is_array( $argValues[0] ) ) {
  1166.  
  1167. foreach ($argValues[0] as $valueKey => $valueData){
  1168.  
  1169. for ($i = 1; $i < $argCount; $i++) {
  1170.  
  1171. if (isset($argValues[$i][$valueKey])) { continue 2; }
  1172.  
  1173. }
  1174.  
  1175. $valuesDiff[$valueKey] = $valueData;
  1176.  
  1177. }
  1178.  
  1179. }
  1180.  
  1181. return $valuesDiff;
  1182.  
  1183. }
  1184.  
  1185. }
  1186.  
  1187.  
  1188.  
  1189. function array_merge_withKeys() {
  1190.  
  1191. $argCount = func_num_args();
  1192.  
  1193. $argValues = func_get_args();
  1194.  
  1195. $returnArray = array();
  1196.  
  1197.  
  1198. if ($argCount < 2) { return $argValues[0]; }
  1199.  
  1200.  
  1201. foreach ($argValues as $argParam) {
  1202.  
  1203. if( is_array( $argParam ) ) {
  1204.  
  1205. foreach( $argParam as $key => $value ) {
  1206.  
  1207. $returnArray[$key] = $value;
  1208.  
  1209. }
  1210.  
  1211. }
  1212.  
  1213. }
  1214.  
  1215.  
  1216. return $returnArray;
  1217.  
  1218. }
  1219.  
  1220.  
  1221.  
  1222. function insert_db_table( $fields ) {
  1223.  
  1224. if( validate_db_table( $fields ) ) {
  1225.  
  1226. return true;
  1227.  
  1228. } else {
  1229.  
  1230. global $db;
  1231.  
  1232.  
  1233. $queryString = "CREATE TABLE ".$fields['table_name']."( ";
  1234.  
  1235.  
  1236. foreach( $fields['table'] as $field => $fieldType ) {
  1237.  
  1238. $queryString .= $field." ".$fieldType.",";
  1239.  
  1240. }
  1241.  
  1242.  
  1243.  
  1244. $queryString = rtrim($queryString, ",").")";
  1245.  
  1246. $db->query( $queryString );
  1247.  
  1248. }
  1249.  
  1250.  
  1251. return validate_db_table( $fields );
  1252.  
  1253. }
  1254.  
  1255.  
  1256.  
  1257. function validate_db_table( $fields ) {
  1258.  
  1259. global $db;
  1260.  
  1261.  
  1262.  
  1263. //get the db results
  1264.  
  1265. $db->hide_errors();
  1266.  
  1267. $exist = $db->get_results("SELECT * FROM ".$fields['table_name'] );
  1268.  
  1269. $db->show_errors();
  1270.  
  1271.  
  1272. if( $db->col_info ) {
  1273.  
  1274. //if the table exists
  1275.  
  1276. foreach( $db->get_col_info( 'name' ) as $existingField ) {
  1277.  
  1278. //and all the specified fields exist
  1279.  
  1280. if( !isset($fields['table'][$existingField]) ) {
  1281.  
  1282. return false;
  1283.  
  1284. }
  1285.  
  1286. }
  1287.  
  1288. } else {
  1289.  
  1290. //if not return false
  1291.  
  1292. return false;
  1293.  
  1294. }
  1295.  
  1296.  
  1297. //return true
  1298.  
  1299. return true;
  1300.  
  1301. }
  1302.  
  1303.  
  1304.  
  1305. function insert_object_type( $fields ) {
  1306.  
  1307. global $db;
  1308.  
  1309.  
  1310. if( !$existing = $db->get_var( "SELECT id FROM object_types WHERE name = '".$fields['name']."'" ) ) {
  1311.  
  1312. $counter = $db->get_var( "SELECT value FROM system_settings WHERE name = 'object_type_counter'" );
  1313.  
  1314. $db->query( "UPDATE system_settings SET value = '".($counter + 1)."' WHERE name = 'object_type_counter'" );
  1315.  
  1316. $db->query( "
  1317.  
  1318. INSERT INTO object_types (id, name, description, table_name, class_name, icon )
  1319.  
  1320. VALUES ( '".$counter."', '".$fields['name']."', '".$fields['description']."', '".$fields['table_name']."', '".$fields['class_name']."', '' )
  1321.  
  1322. " );
  1323.  
  1324. return $counter;
  1325.  
  1326. } else {
  1327.  
  1328. return $existing;
  1329.  
  1330. }
  1331.  
  1332. }
  1333.  
  1334.  
  1335.  
  1336. function insert_relationship_type( $fields ) {
  1337.  
  1338. global $db;
  1339.  
  1340.  
  1341. if( !$existing = $db->get_var( "SELECT id FROM object_types WHERE name = '".$fields['name']."'" ) ) {
  1342.  
  1343. $counter = $db->get_var( "SELECT value FROM system_settings WHERE name = 'object_type_counter'" );
  1344.  
  1345. $db->query( "UPDATE system_settings SET value = '".($counter + 1)."' WHERE name = 'object_type_counter'" );
  1346.  
  1347. $db->query( "
  1348.  
  1349. INSERT INTO relationship_types (id, name_foreward, name_reverse, description, icon )
  1350.  
  1351. VALUES ( '".$counter."', '".$fields['name_foreward']."', '".$fields['name_reverse']."', '".$fields['description']."', '' )
  1352.  
  1353. " );
  1354.  
  1355. return $counter;
  1356.  
  1357. } else {
  1358.  
  1359. return $existing;
  1360.  
  1361. }
  1362.  
  1363. }
  1364.  
  1365.  
  1366.  
  1367. function new_constraint( $valueArray) {
  1368.  
  1369. $constraint =& new constraint;
  1370.  
  1371. $constraint->verify_set( $valueArray );
  1372.  
  1373. return $constraint->get_attribute( 'id' );
  1374.  
  1375. }
  1376.  
  1377.  
  1378.  
  1379. function to_phone( $input ) {
  1380.  
  1381. return $input;
  1382.  
  1383. }
  1384.  
  1385.  
  1386.  
  1387. ?>

Documentation generated on Tue, 24 May 2005 03:57:57 -0400 by phpDocumentor 1.3.0RC3