23 if ( !function_exists(
'sys_get_temp_dir') )
27 function sys_get_temp_dir()
30 if ( !empty($_ENV[
'TMP']) )
32 return realpath( $_ENV[
'TMP'] );
34 else if ( !empty($_ENV[
'TMPDIR']) )
36 return realpath( $_ENV[
'TMPDIR'] );
38 else if ( !empty($_ENV[
'TEMP']) )
40 return realpath( $_ENV[
'TEMP'] );
48 $temp_file = tempnam( md5(uniqid(rand(), TRUE)),
'' );
51 $temp_dir = realpath( dirname($temp_file) );
63 require_once dirname(__FILE__).
"/../config.inc.php";
64 import(
'Dataface/PermissionsTool.php');
65 import(
'Dataface/LanguageTool.php');
66 define(
'DATAFACE_STRICT_PERMISSIONS', 100);
167 private $pageTitle = null;
172 var $sessionCookieKey;
178 var $autoSession =
false;
183 var $_url_filters = array();
188 var $_tables = array();
194 var $tableIndex = array();
237 var $queryTool = null;
242 var $currentRecord = null;
247 var $recordContext = null;
260 var $locations = null;
269 var $eventListeners = array();
275 'show_result_stats'=>1,
277 'show_result_controller'=>1,
278 'show_table_tabs'=>1,
279 'show_actions_menu'=>1,
281 'show_tables_menu'=>1,
283 'show_record_actions'=>1,
284 'show_recent_records_menu'=>1,
285 'show_bread_crumbs' => 1,
286 'show_record_tabs' => 1,
287 'show_record_tree' => 1,
288 'list_view_scroll_horizontal'=>1,
289 'list_view_scroll_vertical'=>1
298 var $tableNamesUsed = array();
303 var $main_content_only =
false;
325 var $messages = array();
330 var $debugLog = array();
335 var $authenticationTool = null;
343 var $headContent=array();
350 var $mysqlVersion = null;
377 var $_locales = array(
393 var $_languages = array(
424 if ( isset($this->_languages[$langCode]) ){
425 return $this->_languages[$langCode];
450 if ( isset($this->_locales[$locale]) )
return $this->_locales[$locale];
452 list($langCode) = explode(
'_', $locale);
487 $langs = array_keys($this->_conf[
'languages']);
488 if ( @$this->_conf[
'default_language'] ) $langs[] = $this->_conf[
'default_language'];
489 else $langs[] =
'en';
491 foreach ($langs as $lang){
494 return array_keys(
$out);
516 function db(){
return $this->_db;}
527 $this->sessionCookieKey = md5(DATAFACE_SITE_URL.
'#'.__FILE__);
528 $this->_baseUrl =
$_SERVER[
'PHP_SELF'];
529 if ( !is_array($conf) ) $conf = array();
530 if ( is_readable(DATAFACE_SITE_PATH.
'/conf.ini') ){
531 $conf = array_merge(parse_ini_file(DATAFACE_SITE_PATH.
'/conf.ini',
true), $conf);
532 if ( @$conf[
'__include__'] ){
533 $includes = array_map(
'trim',explode(
',', $conf[
'__include__']));
534 foreach ($includes as $i){
535 if ( is_readable($i) ){
536 $conf = array_merge($conf, parse_ini_file($i,
true));
544 if ( !isset( $conf[
'_tables'] ) ){
545 throw new Exception(
'Error loading config file. No tables specified.', E_USER_ERROR);
551 if ( isset( $conf[
'db'] ) and is_resource($conf[
'db']) ){
552 $this->_db = $conf[
'db'];
554 if ( !isset( $conf[
'_database'] ) ){
555 throw new Exception(
'Error loading config file. No database specified.', E_USER_ERROR);
558 $dbinfo =& $conf[
'_database'];
559 if ( !is_array( $dbinfo ) || !isset($dbinfo[
'host']) || !isset( $dbinfo[
'user'] ) || !isset( $dbinfo[
'password'] ) || !isset( $dbinfo[
'name'] ) ){
560 throw new Exception(
'Error loading config file. The database information was not entered correctly.<br>
561 Please enter the database information int its own section of the config file as follows:<br>
568 </pre>', E_USER_ERROR);
571 if ( @$dbinfo[
'persistent'] ){
572 $this->_db = mysql_pconnect( $dbinfo[
'host'], $dbinfo[
'user'], $dbinfo[
'password'] );
574 $this->_db = mysql_connect( $dbinfo[
'host'], $dbinfo[
'user'], $dbinfo[
'password'] );
577 throw new Exception(
'Error connecting to the database: '.mysql_error());
580 $this->mysqlVersion = mysql_get_server_info($this->_db);
581 mysql_select_db( $dbinfo[
'name'] ) or die(
"Could not select DB: ".mysql_error($this->_db));
583 if ( !defined(
'DATAFACE_DB_HANDLE') ) define(
'DATAFACE_DB_HANDLE', $this->_db);
586 if ( !is_array( $conf[
'_tables'] ) ){
587 throw new Exception(
"<pre>
588 Error reading table information from the config file. Please enter the table information in its own section
589 of the ini file as follows:
591 table1 = Table 1 Label
592 table2 = Table 2 Label
597 $this->_tables = $conf[
'_tables'];
601 if ( count($this->_tables) <= 10 ){
602 $this->prefs[
'horizontal_tables_menu'] = 1;
606 register_shutdown_function(array(&$this,
'_cleanup'));
609 if ( DATAFACE_EXTENSION_LOADED_MEMCACHE ){
610 if ( isset($conf[
'_memcache']) ){
611 if ( !isset($conf[
'_memcache'][
'host']) ){
612 $conf[
'_memcache'][
'host'] =
'localhost';
614 if ( !isset($conf[
'_memcache'][
'port']) ){
615 $conf[
'_memcache'][
'port'] = 11211;
617 $this->memcache =
new Memcache;
618 $this->memcache->connect($conf[
'_memcache'][
'host'], $conf[
'_memcache'][
'port']) or die (
"Could not connect to memcache on port 11211");
625 $this->_conf = $conf;
627 if ( !isset($this->_conf[
'_disallowed_tables']) ){
628 $this->_conf[
'_disallowed_tables'] = array();
631 $this->_conf[
'_disallowed_tables'][
'history'] =
'/__history$/';
632 $this->_conf[
'_disallowed_tables'][
'cache'] =
'__output_cache';
633 $this->_conf[
'_disallowed_tables'][
'dataface'] =
'/^dataface__/';
634 if ( !@$this->_conf[
'_modules'] or !is_array($this->_conf[
'_modules']) ){
635 $this->_conf[
'_modules'] = array();
639 $mods = array(
'modules_XataJax'=>
'modules/XataJax/XataJax.php');
640 foreach ($this->_conf[
'_modules'] as $k=>$v){
643 $this->_conf[
'_modules'] = $mods;
646 if ( isset($this->_conf[
'_modules']) and count($this->_conf[
'_modules'])>0 ){
647 import(
'Dataface/ModuleTool.php');
650 if ( isset($this->_conf[
'languages']) ){
651 $this->_conf[
'language_labels'] = $this->_conf[
'languages'];
652 foreach ( array_keys($this->_conf[
'language_labels']) as $lang_code){
653 $this->_conf[
'languages'][$lang_code] = $lang_code;
657 if ( @$this->_conf[
'support_transactions'] ){
659 @mysql_query(
'SET AUTOCOMMIT=0', $this->_db);
660 @mysql_query(
'START TRANSACTION', $this->_db);
663 if ( !isset($this->_conf[
'default_ie']) ) $this->_conf[
'default_ie'] =
'ISO-8859-1';
664 if ( !isset($this->_conf[
'default_oe']) ) $this->_conf[
'default_oe'] =
'ISO-8859-1';
665 if ( isset( $this->_conf[
'multilingual_content']) || isset($this->_conf[
'languages']) ){
666 $this->_conf[
'oe'] =
'UTF-8';
667 $this->_conf[
'ie'] =
'UTF-8';
669 if (function_exists(
'mb_substr') ){
671 ini_set(
'mbstring.internal_encoding',
'UTF-8');
673 ini_set(
'mbstring.func_overload', 7);
677 if ( !isset($this->_conf[
'languages']) ){
678 $this->_conf[
'languages'] = array(
'en'=>
'English');
680 if ( !isset($this->_conf[
'default_language']) ){
681 if ( count($this->_conf[
'languages']) > 0 )
682 $this->_conf[
'default_language'] = reset($this->_conf[
'languages']);
685 $this->_conf[
'default_language'] =
'en';
690 $this->_conf[
'oe'] = $this->_conf[
'default_oe'];
691 $this->_conf[
'ie'] = $this->_conf[
'default_ie'];
694 if ( $this->_conf[
'oe'] ==
'UTF-8' ){
695 $res = mysql_query(
'set character_set_results = \'utf8\'', $this->_db);
696 mysql_query(
"SET NAMES utf8", $this->_db);
698 if ( $this->_conf[
'ie'] ==
'UTF-8' ){
699 $res = mysql_query(
'set character_set_client = \'utf8\'', $this->_db);
704 if ( isset($this->_conf[
'use_cache']) and $this->_conf[
'use_cache'] and !defined(
'DATAFACE_USE_CACHE') ){
705 define(
'DATAFACE_USE_CACHE',
true);
708 if ( isset($this->_conf[
'debug']) and $this->_conf[
'debug'] and !defined(
'DATAFACE_DEBUG') ){
709 define(
'DATAFACE_DEBUG',
true);
710 }
else if ( !defined(
'DATAFACE_DEBUG') ){
711 define(
'DATAFACE_DEBUG',
false);
714 if ( !@$this->_conf[
'config_storage'] ) $this->_conf[
'config_storage'] = DATAFACE_DEFAULT_CONFIG_STORAGE;
719 if ( !isset($this->_conf[
'garbage_collector_threshold']) ){
725 $this->_conf[
'garbage_collector_threshold'] = 10*60;
728 if ( !isset($this->_conf[
'multilingual_content']) ) $this->_conf[
'multilingual_content'] =
false;
735 if ( !isset($this->_conf[
'cookie_prefix']) ) $this->_conf[
'cookie_prefix'] =
'dataface__';
737 if ( !isset($this->_conf[
'security_level']) ){
741 $this->_conf[
'security_level'] = 0;
745 if ( !isset($this->_conf[
'default_action']) ){
748 $this->_conf[
'default_action'] =
'list';
751 if ( !isset($this->_conf[
'default_browse_action']) ){
752 $this->_conf[
'default_browse_action'] =
'view';
756 if ( !isset($this->_conf[
'default_mode'] ) ) $this->_conf[
'default_mode'] =
'list';
758 if ( !isset($this->_conf[
'default_limit']) ){
759 $this->_conf[
'default_limit'] = 30;
762 if ( !isset($this->_conf[
'default_table'] ) ){
764 foreach ($this->_tables as $key=>$value){
765 $this->_conf[
'default_table'] = $key;
771 if ( !isset($this->_conf[
'auto_load_results']) ) $this->_conf[
'auto_load_results'] =
false;
773 if ( !isset( $this->_conf[
'cache_dir'] ) ){
774 if ( ini_get(
'upload_tmp_dir') ) $this->_conf[
'cache_dir'] = ini_get(
'upload_tmp_dir');
775 else $this->_conf[
'cache_dir'] =
'/tmp';
778 if ( !isset( $this->_conf[
'default_table_role'] ) ){
781 $this->_conf[
'default_table_role'] =
'NO ACCESS';
783 $this->_conf[
'default_table_role'] =
'ADMIN';
788 if ( !isset( $this->_conf[
'default_field_role'] ) ){
790 $this->_conf[
'default_field_role'] =
'NO ACCESS';
792 $this->_conf[
'default_field_role'] =
'ADMIN';
797 if ( !isset( $this->_conf[
'default_relationship_role'] ) ){
799 $this->_conf[
'default_relationship_role'] =
'READ ONLY';
801 $this->_conf[
'default_relationship_role'] =
'ADMIN';
806 if ( !isset( $this->_conf[
'languages'] ) ) $this->_conf[
'languages'] = array(
'en');
807 else if ( !is_array($this->_conf[
'languages']) ) $this->_conf[
'languages'] = array($this->_conf[
'languages']);
809 if ( isset($this->_conf[
'_language_codes']) ){
810 $this->_languages = array_merge($this->_languages, $this->_conf[
'_language_codes']);
812 if ( isset($this->_conf[
'_locales']) ){
813 $this->_locales = array_merge($this->_locales, $this->_conf[
'_locales']);
819 if ( !isset( $this->_conf[
'default_language'] ) ) $this->_conf[
'default_language'] =
'en';
820 $prefix = $this->_conf[
'cookie_prefix'];
822 if ( isset($_REQUEST[
'--lang']) ){
823 $_REQUEST[
'--lang'] = basename($_REQUEST[
'--lang']);
824 $this->_conf[
'lang'] = $_REQUEST[
'--lang'];
825 }
else if ( isset( $_REQUEST[
'-lang'] ) ){
826 $_REQUEST[
'-lang'] = basename($_REQUEST[
'-lang']);
827 $this->_conf[
'lang'] = $_REQUEST[
'-lang'];
828 if ( @$_COOKIE[$prefix.
'lang'] !== $_REQUEST[
'-lang'] ){
829 setcookie($prefix.
'lang', $_REQUEST[
'-lang'], null,
'/');
831 }
else if (isset( $_COOKIE[$prefix.
'lang']) ){
832 $this->_conf[
'lang'] = $_COOKIE[$prefix.
'lang'];
834 import(
'I18Nv2/I18Nv2.php');
835 $negotiator = I18Nv2::createNegotiator($this->_conf[
'default_language'],
'UTF-8');
837 $negotiator->getLocaleMatch(
838 $this->getAvailableLanguages()
841 setcookie($prefix.
'lang', $this->_conf[
'lang'], null,
'/');
844 $this->_conf[
'lang'] = basename($this->_conf[
'lang']);
848 if ( isset($_REQUEST[
'-usage_mode'] )){
849 $this->_conf[
'usage_mode'] = $_REQUEST[
'-usage_mode'];
850 if (@$_COOKIE[$prefix.
'usage_mode'] !== $_REQUEST[
'-usage_mode']){
851 setcookie($prefix.
'usage_mode', $_REQUEST[
'-usage_mode'], null,
'/');
853 }
else if ( isset( $_COOKIE[$prefix.
'usage_mode'] ) ){
854 $this->_conf[
'usage_mode'] = $_COOKIE[$prefix.
'usage_mode'];
855 }
else if ( !isset($this->_conf[
'usage_mode']) ){
856 $this->_conf[
'usage_mode'] =
'view';
859 define(
'DATAFACE_USAGE_MODE', $this->_conf[
'usage_mode']);
861 if ( @$this->_conf[
'enable_workflow'] ){
862 import(
'Dataface/WorkflowTool.php');
870 if ( isset($_REQUEST[
'__keys__']) and is_array($_REQUEST[
'__keys__']) ){
871 $query = $_REQUEST[
'__keys__'];
872 foreach ( array_keys($_REQUEST) as $key ){
873 if ( $key{0} ==
'-' and !in_array($key, array(
'-search',
'-cursor',
'-skip',
'-limit'))){
874 $query[$key] = $_REQUEST[$key];
878 $query = array_merge($_GET, $_POST);
880 if ( @$query[
'-action'] ){
881 $query[
'-action'] = trim($query[
'-action']);
882 if ( !preg_match(
'/^[a-zA-Z0-9_]+$/', $query[
'-action']) ){
883 throw new Exception(
"Illegal action name.");
885 $query[
'-action'] = basename($query[
'-action']);
887 if ( @$query[
'-table'] ){
888 $query[
'-table'] = trim($query[
'-table']);
889 if ( !preg_match(
'/^[a-zA-Z0-9_]+$/', $query[
'-table']) ){
890 throw new Exception(
"Illegal table name.");
892 $query[
'-table'] = basename($query[
'-table']);
894 if ( @$query[
'-lang'] ){
895 $query[
'-lang'] = trim($query[
'-lang']);
896 if ( !preg_match(
'/^[a-zA-Z0-9]{2}$/', $query[
'-lang']) ){
897 throw new Exception(
"Illegal language code: ".$query[
'-lang']);
899 $query[
'-lang'] = basename($query[
'-lang']);
902 if ( @$query[
'--lang'] ){
903 $query[
'--lang'] = trim($query[
'--lang']);
904 if ( !preg_match(
'/^[a-zA-Z0-9]{2}$/', $query[
'--lang']) ){
905 throw new Exception(
"Illegal language code: ".$query[
'--lang']);
907 $query[
'--lang'] = basename($query[
'--lang']);
910 if ( @$query[
'-theme'] ){
911 $query[
'-theme'] = trim($query[
'-theme']);
912 if ( !preg_match(
'/^[a-zA-Z0-9_]+$/', $query[
'-theme']) ){
913 throw new Exception(
"Illegal theme name.");
915 $query[
'-theme'] = basename($query[
'-theme']);
918 if ( @$query[
'-cursor']){
919 $query[
'-cursor'] = intval($query[
'-cursor']);
921 if ( @$query[
'-limit'] ){
922 $query[
'-limit'] = intval($query[
'-limit']);
924 if ( @$query[
'-skip'] ){
925 $query[
'-skip'] = intval($query[
'-skip']);
927 if ( @$query[
'-related-limit'] ){
928 $query[
'-related-limit'] = intval($query[
'-related-limit']);
930 if ( @$query[
'-relationship'] ){
931 if ( !preg_match(
'/^[a-zA-Z0-9_]+$/', $query[
'-relationship']) ){
932 throw new Exception(
"Illegal relationship name.");
939 $this->rawQuery = $query;
941 if ( !isset( $query[
'-table'] ) ) $query[
'-table'] = $this->_conf[
'default_table'];
942 $this->_currentTable = $query[
'-table'];
945 if ( !@$query[
'-action'] ) {
946 $query[
'-action'] = $this->_conf[
'default_action'];
947 $this->_conf[
'using_default_action'] =
true;
950 $query[
'--original_action'] = $query[
'-action'];
951 if ( $query[
'-action'] ==
'browse') {
952 if ( isset($query[
'-relationship']) ){
953 $query[
'-action'] =
'related_records_list';
954 }
else if ( isset($query[
'-new']) and $query[
'-new']) {
955 $query[
'-action'] =
'new';
957 $query[
'-action'] = $this->_conf[
'default_browse_action'];
959 }
else if ( $query[
'-action'] ==
'find_list' ){
960 $query[
'-action'] =
'list';
962 if ( !isset( $query[
'-cursor'] ) ) $query[
'-cursor'] = 0;
963 if ( !isset( $query[
'-skip'] ) ) $query[
'-skip'] = 0;
964 if ( !isset( $query[
'-limit'] ) ) $query[
'-limit'] = $this->_conf[
'default_limit'];
966 if ( !isset( $query[
'-mode'] ) ) $query[
'-mode'] = $this->_conf[
'default_mode'];
967 $this->_query =& $query;
970 if ( isset( $query[
'--msg'] ) ) {
971 $query[
'--msg'] = preg_replace(
'#<[^>]*>#',
'', $query[
'--msg']);
972 if ( preg_match(
'/^@@$/', $query[
'--msg']) ){
974 if ( @$_SESSION[
'--msg'] ){
976 unset($_SESSION[
'--msg']);
987 if ( isset($query[
'--error']) and trim($query[
'--error']) ){
988 $query[
'--error'] = preg_replace(
'#<[^>]*>#',
'', $query[
'--error']);
993 if ( isset($query[
'-theme']) ){
994 if ( !isset($this->_conf[
'_themes']) ) $this->_conf[
'_themes'] = array();
995 $this->_conf[
'_themes'][basename($query[
'-theme'])] =
'themes/'.basename($query[
'-theme']);
999 if ( isset($query[
'--default-preview-length']) ){
1000 $len = intval($query[
'--default-preview-length']);
1001 if ( $len > 0 && !defined(
'XATAFACE_DEFAULT_PREVIEW_LENGTH') ){
1002 define(
'XATAFACE_DEFAULT_PREVIEW_LENGTH', $len);
1030 static $instance = array();
1032 if ( !isset( $instance[0] ) ){
1034 if ( !defined(
'DATAFACE_APPLICATION_LOADED') ){
1035 define(
'DATAFACE_APPLICATION_LOADED',
true);
1039 return $instance[0];
1056 static $loaded =
false;
1060 if ( isset($del) and method_exists($del,
'conf') ){
1061 $conf = $del->conf();
1062 if ( !is_array($conf) )
throw new Exception(
"The Application Delegate class defined a method 'conf' that must return an array, but returns something else.", E_USER_ERROR);
1063 foreach ( $conf as $key=>$val){
1064 if ( isset($this->_conf[$key]) ){
1065 if ( is_array($this->_conf[$key]) and is_array($val) ){
1066 $this->_conf[$key] = array_merge($this->_conf[$key], $val);
1068 $this->_conf[$key] = $val;
1071 $this->_conf[$key] = $val;
1088 if ( !isset($this->mysqlVersion) ){
1089 $this->mysqlVersion = mysql_get_server_info($this->_db);
1091 list($mv) = explode(
'.',$this->mysqlVersion);
1096 if ( isset($this->pageTitle) ){
1097 return $this->pageTitle;
1104 $this->pageTitle = $title;
1119 $title =
'Dataface Application';
1121 if ( isset($this->_conf[
'title']) ) {
1123 $title = $this->
parseString($this->_conf[
'title']);
1124 }
catch (Exception $ex){
1125 $title = $this->_conf[
'title'];
1128 if ( ($record = $this->
getRecord()) && $query[
'-mode'] ==
'browse' ){
1129 return $record->getTitle().
' - '.$title;
1132 return $tableLabel.
' - '.$title;
1167 return $this->_query;
1194 if ( isset( $this->_query[
'-'.$key] ) ){
1195 return $this->_query[
'-'.$key];
1207 if ( $this->queryTool === null ){
1208 import(
'Dataface/QueryTool.php');
1211 return $this->queryTool;
1261 if ( $this->currentRecord === null ){
1263 if ( @$query[
'--no-query'] ){
1268 if ( isset($_REQUEST[
'__keys__']) and is_array($_REQUEST[
'__keys__']) ){
1269 foreach ($_REQUEST[
'__keys__'] as $key=>$val) $q[$key] =
'='.$val;
1270 $this->currentRecord = df_get_record($query[
'-table'], $q);
1271 }
else if ( isset($_REQUEST[
'-__keys__']) and is_array($_REQUEST[
'-__keys__']) ){
1272 foreach ($_REQUEST[
'-__keys__'] as $key=>$val) $q[$key] =
'='.$val;
1273 $this->currentRecord = df_get_record($query[
'-table'], $q);
1274 }
else if ( isset($_REQUEST[
'--__keys__']) and is_array($_REQUEST[
'--__keys__']) ){
1275 foreach ($_REQUEST[
'--__keys__'] as $key=>$val) $q[$key] =
'='.$val;
1276 $this->currentRecord = df_get_record($query[
'-table'], $q);
1277 }
else if ( isset($_REQUEST[
'--recordid']) ){
1278 $this->currentRecord = df_get_record_by_id($_REQUEST[
'--recordid']);
1279 }
else if ( isset($_REQUEST[
'-recordid']) ){
1280 $this->currentRecord = df_get_record_by_id($_REQUEST[
'-recordid']);
1283 $this->currentRecord = $rs->loadCurrent();
1285 if ( $this->currentRecord === null ) $this->currentRecord = -1;
1287 if ( $this->currentRecord === -1 || !$this->currentRecord )
return $null;
1288 return $this->currentRecord;
1303 if ( !isset($this->recordContext) ){
1304 $this->recordContext = array();
1306 if ( @$query[
'-portal-context'] ){
1307 $rrec = df_get_record_by_id($query[
'-portal-context']);
1311 if ( is_a($rrec,
'Dataface_RelatedRecord') ){
1312 $destRecords = $rrec->toRecords();
1313 foreach ($destRecords as $destRec){
1314 $this->recordContext[$destRec->getId()] = $rrec;
1321 foreach ($this->recordContext as $rrec)
return $rrec;
1323 return @$this->recordContext[$id];
1342 foreach ($destRecords as $destRec){
1343 $this->recordContext[$destRec->getId()] = $rec;
1360 $this->recordContext = array();
1362 foreach ($contextMasks as $k=>$v){
1363 unset($contextMasks[$k]);
1375 return ( $this->currentRecord !== null);
1386 import(
'Dataface/ActionTool.php');
1388 return $actionTool->getAction(array(
'name'=>$this->_query[
'-action']));
1430 if ( !isset($action) ){
1433 if ( !isset($action) or !is_array($action)){
1434 if ( @$this->_conf[
'default_search_target'] )
return $this->_conf[
'default_search_target'];
1442 $tableDel =
$table->getDelegate();
1443 $method =
'getSearchTarget';
1444 if ( isset($tableDel) and method_exists($tableDel, $method) ){
1445 return $tableDel->$method($action);
1449 if ( isset($appDel) and method_exists($appDel, $method) ){
1450 return $appDel->$method($action);
1454 if ( @$action[
'search_target'] ){
1455 return $action[
'search_target'];
1457 if ( @$this->_conf[
'default_search_target'] )
return $this->_conf[
'default_search_target'];
1492 $_SESSION[
'--msg'] = $str;
1505 setcookie($this->sessionCookieKey, 1, 0, DATAFACE_SITE_URL);
1518 setcookie($this->sessionCookieKey, 1, time()-3600*25, DATAFACE_SITE_URL);
1530 return @$_COOKIE[$this->sessionCookieKey];
1545 if ( defined(
'XATAFACE_NO_SESSION') and XATAFACE_NO_SESSION )
return;
1550 if ( session_id() ==
"" ){
1551 if ( !isset($conf) ){
1552 if ( isset($this->_conf[
'_auth']) ) $conf = $this->_conf[
'_auth'];
1553 else $conf = array();
1557 if ( isset($delegate) and method_exists($delegate,
'startSession') ){
1558 $delegate->startSession($conf);
1562 $parts = parse_url(DATAFACE_SITE_URL);
1563 $cookie_path = $parts[
'path'];
1564 if ( isset($conf[
'cookie_path']) ){
1565 $cookie_path = $conf[
'cookie_path'];
1566 if ( substr($cookie_path,0,4) ==
'php:' ){
1567 $cookie_path_expr = substr($cookie_path,4);
1568 eval(
'$cookie_path = '.$cookie_path_expr.
';');
1572 if (strlen($cookie_path)==0) $cookie_path =
'/';
1573 if ( $cookie_path{strlen($cookie_path)-1} !=
'/' ) $cookie_path .=
'/';
1576 $cookie_timeout = (isset($conf[
'session_timeout']) ? intval($conf[
'session_timeout']) : 24*60*60);
1584 $garbage_timeout = $cookie_timeout + 600;
1587 session_set_cookie_params($cookie_timeout, $cookie_path);
1591 ini_set(
'session.gc_maxlifetime', $garbage_timeout);
1592 if ( isset($conf[
'session_timeout']) and ini_get(
'session.save_handler') ==
'files' ){
1599 strstr(strtoupper(substr(@
$_SERVER[
"OS"], 0, 3)),
"WIN") ?
1600 $sep =
"\\" : $sep =
"/";
1601 $sessdir = session_save_path();
1603 if (strpos($sessdir,
";") !== FALSE){
1604 $levels = substr($sessdir, 0, strpos($sessdir,
";")).
';';
1605 $sessdir = substr($sessdir, strpos($sessdir,
";")+1);
1607 if ( !$sessdir ) $sessdir = sys_get_temp_dir();
1608 if ( $sessdir and $sessdir{strlen($sessdir)-1} ==
'/' ) $sessdir = substr($sessdir,0, strlen($sessdir)-1);
1610 if ( @$conf[
'subdir'] ) $subdir = $conf[
'subdir'];
1611 else $subdir = md5(DATAFACE_SITE_PATH);
1612 if ( !$subdir ) $subdir =
'dataface';
1613 $sessdir .=
"/".$subdir;
1616 if (!is_dir($sessdir)) {
1617 $res = @mkdir($sessdir, 0777);
1619 error_log(
"Failed to create session directory '$sessdir' to store session files in ".__FILE__.
" on line ".__LINE__);
1623 if (is_dir($sessdir) ){
1624 session_save_path($sessdir);
1629 if ( !@$conf[
'session_name'] ){
1630 $conf[
'session_name'] = md5(DATAFACE_SITE_PATH);
1633 if ( @$conf[
'session_name'] ) session_name($conf[
'session_name']);
1636 header(
'P3P: CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"');
1639 if ( isset($_COOKIE[session_name()]) ){
1640 setcookie(session_name(), $_COOKIE[session_name()], time() + $cookie_timeout, $cookie_path);
1647 if ( isset( $_SESSION[
'--msg'] ) ){
1649 unset($_SESSION[
'--msg']);
1658 function writeSessionData(){
1660 if ( isset($this->locations) ) $_SESSION[
'locations'] = serialize($this->locations);
1666 function encodeLocation($url){
1667 if ( !isset($this->locations) and isset($_SESSION[
'locations']) ) $this->locations = unserialize($_SESSION[
'locations']);
1668 else if ( !isset($this->locations) ) $this->locations = array();
1670 $this->locations[$key] = $url;
1677 function decodeLocation($key){
1678 if ( !isset($this->locations) and isset($_SESSION[
'locations']) ) $this->locations = unserialize($_SESSION[
'locations']);
1679 else if ( !isset($this->locations) ) $this->locations = array();
1681 if ( isset($this->locations[$key]) ){
1682 $url = $this->locations[$key];
1683 unset($this->locations[$key]);
1701 if ( !isset($this->authenticationTool) ){
1703 if ( isset($this->_conf[
'_auth']) ){
1704 import(
'Dataface/AuthenticationTool.php');
1711 return $this->authenticationTool;
1753 $this->headContent[] = $content;
1779 $override = array();
1780 if ( isset($del) and method_exists($del,
'getNavItem') ){
1782 $override = $del->getNavItem($key, $label?$label:$key);
1783 }
catch (Exception $ex){}
1785 if ( !isset($override) ){
1788 return array_merge(array(
1789 'href'=> DATAFACE_SITE_HREF.
'?-table='.urlencode($key),
1790 'label'=> $label ? $label:$key,
1813 if ( isset($del) and method_exists($del,
'isNavItemSelected') ){
1815 return $del->isNavItemSelected($key);
1816 }
catch (Exception $ex){}
1819 return ($query[
'-table'] == $key);
1835 $this->errors[] = $err;
1856 return $this->errors;
1868 $this->messages[] = $msg;
1883 if ( trim(@$_SESSION[
'msg']) ){
1884 array_push($this->messages, $_SESSION[
'msg']);
1885 unset($_SESSION[
'msg']);
1887 $msgs = $this->messages;
1889 if ( @$response[
'--msg'] ){
1890 array_push($msgs, $response[
'--msg']);
1901 $this->messages = array();
1909 $count = count($this->messages);
1911 if ( @$response[
'--msg'] ) $count++;
1927 static $response = 0;
1929 $response = array(
'--msg'=>
'');
1959 foreach ($listeners as $listener){
1960 $res = call_user_func($listener, $params);
1977 if ( !isset($this->eventListeners[$name]) ) $this->eventListeners[$name] = array();
1978 $this->eventListeners[$name][] = $callback;
1989 if ( isset($this->eventListeners[$name]) ){
1990 $listeners =& $this->eventListeners[$name];
1991 foreach ( $listeners as $key=>$listener ){
1992 if ( $listener == $callback ) unset($listeners[$key]);
2003 if ( !isset($name) )
return $this->eventListeners;
2004 else if (isset($this->eventListeners[$name])){
2005 return $this->eventListeners[$name];
2055 if ( !$disableCache and (@$_GET[
'-action'] !=
'getBlob') and isset( $this->_conf[
'_output_cache'] ) and @$this->_conf[
'_output_cache'][
'enabled'] and count($_POST) == 0){
2056 import(
'Dataface/OutputCache.php');
2061 import(
'Dataface/ActionTool.php');
2062 import(
'Dataface/PermissionsTool.php');
2063 import(
'Dataface/Table.php');
2065 if ( isset($this->_conf[
'_modules']) and count($this->_conf[
'_modules']) > 0 ){
2067 foreach ($this->_conf[
'_modules'] as $modname=>$modpath){
2068 $mt->loadModule($modname);
2073 $this->
fireEvent(
'beforeHandleRequest');
2075 if ( isset($applicationDelegate) and method_exists($applicationDelegate,
'beforeHandleRequest') ){
2077 $applicationDelegate->beforeHandleRequest();
2090 if ( isset($this->_conf[
'_prefs']) and is_array($this->_conf[
'_prefs']) ){
2091 $this->prefs = array_merge($this->prefs,$this->_conf[
'_prefs']);
2093 if ( @$this->_conf[
'hide_nav_menu'] ){
2094 $this->prefs[
'show_tables_menu'] = 0;
2097 if ( @$this->_conf[
'hide_view_tabs'] ){
2098 $this->prefs[
'show_table_tabs'] = 0;
2101 if ( @$this->_conf[
'hide_result_controller'] ){
2102 $this->prefs[
'show_result_controller'] = 0;
2105 if ( @$this->_conf[
'hide_table_result_stats'] ){
2106 $this->prefs[
'show_result_stats'] = 0;
2109 if ( @$this->_conf[
'hide_search'] ){
2110 $this->prefs[
'show_search'] = 0;
2113 if ( !isset($this->prefs[
'disable_ajax_record_details']) ){
2114 $this->prefs[
'disable_ajax_record_details'] = 1;
2117 if ( $query[
'-action'] ==
'login_prompt' ) $this->prefs[
'no_history'] = 1;
2120 if ( isset($applicationDelegate) and method_exists($applicationDelegate,
'getPreferences') ){
2121 $this->prefs = array_merge($this->prefs, $applicationDelegate->getPreferences());
2123 $this->prefs = array_map(
'intval', $this->prefs);
2126 $disallowed =
false;
2127 if ( isset($this->_conf[
'_disallowed_tables']) ){
2128 foreach ( $this->_conf[
'_disallowed_tables'] as $name=>$pattern ){
2129 if ( $pattern{0} ==
'/' and preg_match($pattern, $query[
'-table']) ){
2132 }
else if ( $pattern == $query[
'-table'] ){
2139 if ( $disallowed and isset($this->_conf[
'_allowed_tables']) ){
2140 foreach ($this->_conf[
'_allowed_tables'] as $name=>$pattern ){
2141 if ( $pattern{0} ==
'/' and preg_match($pattern, $query[
'-table']) ){
2142 $disallowed =
false;
2144 }
else if ( $pattern == $query[
'-table'] ){
2145 $disallowed =
false;
2156 "Permission Denied. This table has been disallowed in the conf.ini file",
2158 "Permission denied because this table has been disallowed in the conf.ini file '"
2173 'table'=>$query[
'-table'],
2174 'name'=>$query[
'-action']);
2175 if ( strpos($query[
'-action'],
'custom_') === 0 ){
2177 'name' => $query[
'-action'],
2178 'page' => substr($query[
'-action'], 7),
2179 'permission' =>
'view',
2184 $action = $actionTool->getAction($params);
2185 if ( is_array($action) and @$action[
'related'] and @$query[
'-relationship'] and preg_match(
'/relationships\.ini/', @$action[
'allow_override']) ){
2187 $raction =
$table->getRelationshipsAsActions(array(), $query[
'-relationship']);
2188 if ( is_array($raction) ){
2189 $action = array_merge($action,$raction);
2192 if ( is_array($action) and isset($action[
'delegate']) ){
2193 $params[
'name'] = $query[
'-action'] = $action[
'delegate'];
2194 $tmp = $actionTool->getActions($params);
2199 if ( is_array($action) and isset($action[
'auth_type']) ){
2201 $authTool->setAuthType($action[
'auth_type']);
2215 "Permission Denied. No action found in strict permissions mode",
2217 "Permission denied for action '".
2219 "'. No entry for this action was found in the actions.ini file.
2220 You are currently using strict permissions mode which requires that you define all actions that you want to use in the actions.ini file with appropriate permissions information.",
2222 array(
'action'=>$query[
'-action'])
2229 $action = array(
'name'=>$query[
'-action'],
'label'=>$query[
'-action']);
2234 $delegate =
$table->getDelegate();
2236 if ( method_exists($delegate,
'handleRequest') ){
2237 $result = $delegate->handleRequest();
2246 if ( isset($action[
'mode']) and $action[
'mode'] ) $query[
'-mode'] = $action[
'mode'];
2249 if ( isset($action[
'custom']) ){
2250 $locations = array( DATAFACE_PATH.
'/actions/custom.php'=>
'dataface_actions_custom');
2252 $locations = array();
2254 $locations[
Dataface_Table::getBasePath($query[
'-table']).
'/tables/'.basename($query[
'-table']).
'/actions/'.basename($query[
'-action']).
'.php' ] =
'tables_'.$query[
'-table'].
'_actions_'.$query[
'-action'];
2255 $locations[ DATAFACE_SITE_PATH.
'/actions/'.basename($query[
'-action']).
'.php' ] =
'actions_'.$query[
'-action'];
2257 if ( isset($this->_conf[
'_modules']) and count($this->_conf[
'_modules']) > 0 ){
2259 foreach ($this->_conf[
'_modules'] as $modname=>$modpath){
2260 $mt->loadModule($modname);
2261 if ( $modpath{0} ==
'/' )
2262 $locations[ dirname($modpath).
'/actions/'.basename($query[
'-action']).
'.php' ] =
'actions_'.$query[
'-action'];
2264 $locations[ DATAFACE_SITE_PATH.
'/'.dirname($modpath).
'/actions/'.basename($query[
'-action']).
'.php' ] =
'actions_'.$query[
'-action'];
2265 $locations[ DATAFACE_PATH.
'/'.dirname($modpath).
'/actions/'.basename($query[
'-action']).
'.php' ] =
'actions_'.$query[
'-action'];
2270 $locations[ DATAFACE_PATH.
'/actions/'.basename($query[
'-action']).
'.php' ] =
'dataface_actions_'.$query[
'-action'];
2271 $locations[ DATAFACE_PATH.
'/actions/default.php' ] =
'dataface_actions_default';
2274 $doParams = array(
'action'=>&$action);
2278 foreach ($locations as $handlerPath=>$handlerClassName){
2279 if ( is_readable($handlerPath) ){
2280 import($handlerPath);
2281 $handler =
new $handlerClassName;
2283 if ( is_array($action) and @$action[
'related'] and @$query[
'-relationship'] ){
2284 $params[
'relationship'] = $query[
'-relationship'];
2286 if ( !
PEAR::isError($action) and method_exists($handler,
'getPermissions') ){
2294 $permissions = $handler->getPermissions($params);
2306 if ( isset($action[
'permission']) && !(isset($permissions[$action[
'permission']]) and $permissions[$action[
'permission']]) ){
2309 "Permission Denied for action.",
2311 "Permission to perform action '".
2314 Requires permission '".
2315 $action[
'permission'].
2316 "' but only granted '".
2325 if ( method_exists($handler,
'handle') ){
2328 $result = $handler->handle($doParams);
2340 throw new Exception(df_translate(
'scripts.Dataface.Application.handleRequest.NO_HANDLER_FOUND',
"No handler found for request. This should never happen because, at the very least, the default handler at dataface/actions/default.php should be called. Check the permissions on dataface/actions/default.php to make sure that it is readable by the web server."), E_USER_ERROR);
2362 function display($main_content_only=
false, $disableCache=
false){
2364 foreach ($this->_tables as $key=>$value){
2365 $this->_tables[$key] = $this->_conf[
'_tables'][$key] = df_translate(
'tables.'.$key.
'.label', $value);
2368 $this->main_content_only = $main_content_only;
2372 if ( isset($this->_conf[
'disable_session_ip_check']) and !@$this->_conf[
'disable_session_ip_check'] ){
2373 if ( !@$_SESSION[
'XATAFACE_REMOTE_ADDR'] ){
2374 $_SESSION[
'XATAFACE_REMOTE_ADDR'] = df_IPv4To6(
$_SERVER[
'REMOTE_ADDR']);
2376 $ipAddressError = null;
2377 if ( df_IPv4To6($_SESSION[
'XATAFACE_REMOTE_ADDR']) != df_IPv4To6(
$_SERVER[
'REMOTE_ADDR']) ){
2379 "Session address does not match the remote address. Possible hacking attempt. Session address was '%s', User address was '%s'",
2380 htmlspecialchars(df_IPv4To6($_SESSION[
'XATAFACE_REMOTE_ADDR'])),
2381 htmlspecialchars(df_IPv4To6(
$_SERVER[
'REMOTE_ADDR']))
2387 if ( !@$_SESSION[
'XATAFACE_REMOTE_ADDR'] ){
2388 $_SESSION[
'XATAFACE_REMOTE_ADDR'] = df_IPv4To6(
$_SERVER[
'REMOTE_ADDR']);
2394 if ( !(defined(
'XATAFACE_DISABLE_AUTH') and XATAFACE_DISABLE_AUTH) and isset($this->_conf[
'_auth']) ){
2397 $loginPrompt =
false;
2398 $permissionDenied =
false;
2399 $permissionError =
'';
2404 $auth_result = $authTool->authenticate();
2408 $loginPrompt =
true;
2409 $loginError = $auth_result->getMessage();
2410 }
else if ( $authTool->isLoggedIn() ){
2418 $permissionDenied =
true;
2419 $permissionError =
$result->getMessage();
2421 }
else if ( isset($this->_conf[
'_auth'][
'require_login']) and $this->_conf[
'_auth'][
'require_login'] ){
2424 $loginPrompt =
true;
2435 $loginPrompt =
true;
2439 if ( $loginPrompt ){
2443 $authTool->showLoginPrompt($loginError);
2444 }
else if ($permissionDenied) {
2448 if ( $query[
'--original_action'] ==
'browse' and $query[
'-action'] !=
'view' ){
2452 header(
"HTTP/1.1 403 Permission Denied");
2453 df_display(array(),
'Dataface_Permission_Denied.html');
2458 throw new Exception(
$result->toString().$result->getDebugInfo(), E_USER_ERROR);
2468 if ( $query[
'--original_action'] ==
'browse' and $query[
'-action'] !=
'view' ){
2472 header(
"HTTP/1.1 403 Permission Denied");
2473 df_display(array(),
'Dataface_Permission_Denied.html');
2476 throw new Exception(
$result->toString().$result->getDebugInfo(), E_USER_ERROR);
2491 function _handleGetBlob($request){
2492 import(
'Dataface/Application/blob.php');
2580 if ( strpos($expression,
'"') !==
false ){
2581 throw new Exception(
2583 'scripts.Dataface.Application.parseString.ERROR_PARSING_EXPRESSION_DBL_QUOTE',
2584 "Invalid expression (possible hacking attempt in Dataface_Application::eval(). Expression cannot include double quotes '\"', but recieved '".$expression.
"'.",
2585 array(
'expression'=>$expression))
2589 $site_url = DATAFACE_SITE_URL;
2590 $site_href = DATAFACE_SITE_HREF;
2591 $dataface_url = DATAFACE_URL;
2592 $table = $this->_currentTable;
2595 throw new Exception($tableObj->getMessage(), $tableObj->getCode());
2599 $resultSet =
$app->getResultSet();
2600 if ( isset($context[
'record']) ){
2602 $record = $context[
'record'];
2604 $record =
$app->getRecord();
2607 if ( isset($context[
'relationship']) ){
2610 if ( is_string($context[
'relationship']) ){
2611 $relationship = $tableObj->getRelationship($context[
'relationship']);
2613 $relationship = null;
2616 $relationship = $context[
'relationship'];
2620 if ( !@
$app->_conf[
'debug'] ){
2621 @eval(
'$parsed = "'.$expression.
'";');
2623 eval(
'$parsed = "'.$expression.
'";');
2626 if ( !isset( $parsed ) ){
2627 throw new Exception(df_translate(
'scripts.Dataface.Application.parseString.ERROR_PARSING_EXPRESSION',
"Error parsing expression '$expression'. ", array(
'expression'=>$expression)), E_USER_ERROR);
2638 function _parsePregMatch($matches){
2639 extract($this->_parseStringContext);
2640 if ( !@$this->_conf[
'debug'] ){
2641 return @eval(
'return '.$matches[1].$matches[2].
';');
2643 return eval(
'return '.$matches[1].$matches[2].
';');
2716 $site_url = DATAFACE_SITE_URL;
2717 $site_href = DATAFACE_SITE_HREF;
2718 $dataface_url = DATAFACE_URL;
2719 $table = $this->_currentTable;
2721 if (
PEAR::isError($tableObj) )
throw new Exception($tableObj->getMessage(), $tableObj->getCode());
2724 $resultSet =
$app->getResultSet();
2725 if ( isset($context[
'record']) ) $record = $context[
'record'];
2726 else $record =
$app->getRecord();
2728 if ( isset($context[
'relationship']) ){
2730 if ( is_string($context[
'relationship']) ){
2731 $relationship = $tableObj->getRelationship($context[
'relationship']);
2733 $relationship = null;
2736 $relationship = $context[
'relationship'];
2740 if ( !@$this->_conf[
'debug'] ){
2741 return @eval(
'return ('.$condition.
');');
2743 error_log($condition);
2744 return eval(
'return ('.$condition.
');');
2776 function url($query, $useContext=
true, $forceContext=
false){
2777 import(
'Dataface/LinkTool.php');
2778 return Dataface_LinkTool::buildLInk($query, $useContext, $forceContext);
2821 $this->_url_filters[] = $filter;
2835 if ( !preg_match(
'/[&\?]-table/i', $url ) ){
2836 if ( preg_match(
'/\?/i', $url ) ){
2837 $url .=
'&-table='.$this->_currentTable;
2839 $url .=
'?-table='.$this->_currentTable;
2843 foreach ($this->_url_filters as $filter){
2844 $url = call_user_func($filter, $url);
2873 if ( isset($this->redirectHandler) and method_exists(
'redirect', $this->redirectHandler) ){
2874 $this->redirectHandler->redirect($url);
2877 header(
'Location: '.$url);
2906 if ( $this->delegate === -1 ){
2907 $delegate_path = DATAFACE_SITE_PATH.
'/conf/ApplicationDelegate.php';
2908 if ( is_readable($delegate_path) ){
2909 import($delegate_path);
2910 $this->delegate =
new conf_ApplicationDelegate();
2912 $this->delegate = null;
2915 return $this->delegate;
2956 if ( @$query[
'-relationship'] ){
2957 $params[
'relationship'] = $query[
'-relationship'];
2959 if ( $record and is_a($record,
'Dataface_Record') ){
2985 $result = (isset($perms[$perm]) and $perms[$perm]);
3011 if ( @$this->_conf[
'metadata_enabled'] ){
3013 $metadataTool->updateWorkflowTable(
$tablename);
3030 function _parseRelatedBlobRequest($request){
3031 import(
'Dataface/Application/blob.php');
3065 if ( !isset( $this->_customPages ) ){
3066 $this->_customPages = array();
3067 $path = DATAFACE_SITE_PATH.
'/pages/';
3068 if ( is_dir(
$path) ){
3069 if ( $dh = opendir(
$path) ){
3070 while ( ( $file = readdir($dh) ) !==
false ){
3071 if ( preg_match(
'/\.php$/', $file) ){
3072 list($name) = explode(
'.', $file);
3075 $this->_customPages[$name] =
$path.$file;
3081 return $this->_customPages;
3093 return $pages[$name];
3105 $name = str_replace(
'_',
' ', $name);
3106 return ucwords($name);
3121 function addDebugInfo($info){
3122 $this->debugLog[] = $info;
3130 function displayDebugInfo(){
3131 echo
'<ul class="debug-info"><li>
3132 '; echo implode(
'</li><li>', $this->debugLog);
3139 function _cleanup(){
3140 if ( session_id() !=
"" ){
3141 $this->writeSessionData();
3143 if ( @$this->_conf[
'support_transactions'] ){
3144 @mysql_query(
'COMMIT', $this->_db);
3164 public function __construct($url, $code = 0, Exception $previous = null ){