33 if ( !defined(
'DATAFACE_PUBLIC_API_LOADED' ) ){
34 define(
'DATAFACE_PUBLIC_API_LOADED',
true);
50 function df_init($site_path, $dataface_url, $conf=null){
51 require_once dirname(__FILE__).
'/init.php';
52 init($site_path, $dataface_url);
55 import(
'Dataface/Application.php');
59 if ( df_get_file_system_version() != df_get_database_version() ){
62 throw new Exception($res->getMessage(), E_USER_ERROR);
70 function df_secure(&$records, $secure=
true){
71 foreach ($records as $record){
72 $record->secureDisplay = $secure;
76 if ( !function_exists(
'xmlentities') ){
77 function xmlentities($string) {
78 return str_replace ( array (
'&',
'"',
"'",
'<',
'>',
'' ), array (
'&' ,
'"',
''' ,
'<' ,
'>',
''' ), $string );
83 import(
'actions/install.php');
86 $res = $action->handle($params);
96 import(
'Dataface/QuickForm.php');
102 import(
'Dataface/QuickForm.php');
108 function &df_create_new_related_record_form(&$record, $relationshipName, $fieldNames=null){
109 import(
'Dataface/ShortRelatedRecordForm.php');
116 function &df_create_existing_related_record_form(&$record, $relationshpName){
117 import(
'Dataface/ExistingRelatedRecordForm.php');
125 function &df_create_import_form(&
$table, $relationshipName=null){
126 import(
'Dataface/ImportForm.php');
133 import(
'Dataface/SearchForm.php');
141 function &df_get_records(
$table, $query=null, $start=null, $limit=null, $preview=
true){
142 import(
'Dataface/QueryTool.php');
144 if ( $query === null and $start === null and $limit === null ){
147 if ( $query === null or !$query ) $query = array();
148 if ( $start !== null ) $query[
'-skip'] = $start;
149 if ( $limit !== null ) $query[
'-limit'] = $limit;
159 function &df_get_records_array(
$table, $query=null, $start=null, $limit=null, $preview=
true){
161 $it = df_get_records(
$table,$query,$start,$limit,$preview);
163 while ($it->hasNext()){
164 $records[] = $it->next();
169 function &df_get_related_records($query=array()){
170 if ( !isset($query[
'-relationship']) )
return PEAR::raiseError(
"No relationship specified");
172 $source = df_get_record($query[
'-table'],$query);
176 $relationship = $source->_table->getRelationship($query[
'-relationship']);
178 if ( isset( $query[
'-related:sort']) ){
179 $sortcols = explode(
',', trim($query[
'-related:sort']));
180 $sort_columns = array();
181 foreach ($sortcols as $sortcol){
182 $sortcol = trim($sortcol);
183 if (strlen($sortcol) === 0 )
continue;
184 $sortcol = explode(
' ', $sortcol);
185 if ( count($sortcol) > 1 ){
186 $sort_columns[$sortcol[0]] = strtolower($sortcol[1]);
188 $sort_columns[$sortcol[0]] =
'asc';
193 $sort_columns = array();
195 $sort_columns_arr = array();
196 foreach ( $sort_columns as $colkey=>$colorder) {
197 $sort_columns_arr[] =
'`'.$colkey.
'`'. $colorder;
199 if ( count($sort_columns_arr) > 0 ){
200 $sort_columns_str = implode(
', ',$sort_columns_arr);
202 $sort_columns_str = 0;
205 if ( isset($query[
'-related:search']) ){
207 foreach ($relationship->fields() as $rfield){
209 $rwhere[] =
'`'.str_replace(
'.',
'`.`',$rfield).
'` LIKE \'%'.addslashes($query[
'-related:search']).
'%\'';
211 $rwhere = implode(
' OR ', $rwhere);
215 $start = isset($query[
'-related:start']) ? $query[
'-related:start'] : 0;
216 $limit = isset($query[
'-related:limit']) ? $query[
'-related:limit'] : 30;
218 $out =& $source->getRelatedRecordObjects($query[
'-relationship'], $start, $limit, $rwhere, $sort_columns_str);
223 function df_singularize($label){
224 if ( preg_match(
'/s$/i', $label) ){
225 if ( preg_match(
'/ies$/i', $label) ){
226 return preg_replace(
'/ies$/i',
'y', $label);
227 }
else if ( preg_match(
'/([^aeiouy]{2})es$/i', $label)
228 and !preg_match(
'/[^l]les$/i', $label)
229 and !preg_match(
'/ees$/i', $label)
230 and !preg_match(
'/[aeoiuy][qwrtpsdfghjklzxcvbnm]es$/i', $label)
233 return preg_replace(
'/es$/',
'', $label);
235 return preg_replace(
'/s$/',
'', $label);
242 function df_append_query($url, $query){
243 if ( strpos($url,
'?') ===
false ){
246 foreach ($query as $k=>$v){
247 $url .=
'&'.urlencode($k).
'='.urlencode($v);
253 function df_clear_views(){
258 $res = mysql_query(
"show tables like 'dataface__view_%'", df_db());
260 while ( $row = mysql_fetch_row($res) ){
264 $sql =
"drop view `".implode(
'`,`', $views).
"`";
267 $res = mysql_query(
"drop view `".implode(
'`,`', $views).
"`", df_db());
268 if ( !$res )
throw new Exception(mysql_error(df_db()));
274 function df_clear_cache(){
275 $res = @mysql_query(
"truncate table __output_cache", df_db());
282 import(
'Dataface/Table.php');
287 function &df_get_record(
$table, $query, $io=null){
288 import(
'Dataface/Record.php');
289 import(
'Dataface/IO.php');
296 $query[
'-limit'] = 1;
297 if ( @$query[
'-cursor'] > 0 ){
298 $query[
'-skip'] = $query[
'-cursor'];
301 $res = $io->read($query, $record);
311 function &df_get_record_by_id($id){
312 import(
'Dataface/IO.php');
350 function df_parse_uri($uri){
352 if ( $cache === 0 ) $cache = array();
354 if ( !isset($cache[$uri]) ){
357 'relationship'=>null,
359 'related_where'=>null,
363 if ( strpos($uri,
'?') !==
false ){
364 list(
$table,$query) = explode(
'?', $uri);
366 if ( strpos($uri,
'#') !==
false ){
375 if ( strpos($query,
'#') !==
false )
376 list($query,
$fieldname) = explode(
'#', $query);
380 if ( strpos(
$table,
'://') !==
false ){
382 $out[
'action'] = $action;
388 $params = explode(
'&',$query);
390 foreach ($params as $param){
391 if ( !$param)
continue;
392 list($key,$val) = explode(
'=', $param);
393 $params2[trim(urldecode($key))] = trim(urldecode($val));
398 if ( !isset($relationship) ){
399 $out[
'query'] = $params2;
407 $out[
'relationship'] = $relationship;
408 $primary_params = array();
409 $related_params = array();
410 foreach ($params2 as $key=>$val){
411 @list($key1,$key2) = explode(
'::',$key);
412 if ( !isset($key2) ){
413 $primary_params[trim(urldecode($key1))] = trim(urldecode($val));
415 $related_params[trim(urldecode($key2))] = trim(urldecode($val));
419 if ( count($related_params) > 0 ){
421 foreach ($related_params as $k=>$v){
422 $sql[] =
"`{$k}`='{$v}'";
424 $sql = implode(
' and ', $sql);
425 $out[
'related_where'] = $sql;
428 $out[
'query'] = $primary_params;
439 if ( count($cache) > 200 ) array_shift($cache);
448 function df_get_selected_records($query){
449 if ( isset($query[
'--selected-ids']) ){
450 $selected = $query[
'--selected-ids'];
451 }
else if ( isset($query[
'-selected-ids']) ){
452 $selected = $query[
'-selected-ids'];
457 $ids = explode(
"\n", $selected);
459 foreach ($ids as $id){
460 $records[] = df_get_record_by_id($id);
465 function df_save_record(&$record,
$keys=null, $lang=null, $secure=
false){
466 import(
'Dataface/Record.php');
467 import(
'Dataface/IO.php');
470 if ( isset($lang) ) $io->lang = $lang;
471 $res = $io->write($record,
$keys, null, $secure);
478 function &df_get_valuelist(
$tablename, $valuelistname){
480 $vl =&
$table->getValuelist($valuelistname);
486 function &df_get_relationship_info(
$tablename, $relationshipname){
488 $relationship =
$table->getRelationship($relationshipname);
489 return $relationship;
493 function df_register_skin($name, $template_dir){
494 import(
'Dataface/SkinTool.php');
496 $st->register_skin($name, $template_dir);
500 function df_display($context, $template_name){
501 import(
'Dataface/SkinTool.php');
504 return $st->display($context, $template_name);
507 function df_config_get($varname){
509 return $app->_conf[$varname];
512 function df_config_set($varname, $value){
514 $app->_conf[$varname] = $value;
522 function df_query($sql, $lang=null, $as_array=
false, $enumerated=
false){
523 import(
'Dataface/DB.php');
525 return $db->query($sql,null,$lang,$as_array, $enumerated);
528 function df_insert_id(){
529 import(
'Dataface/DB.php');
531 return $db->insert_id();
534 function df_translate($id, $default=null, $params=array(), $lang=null){
538 function df_load_realm($realm, $lang=null){
542 function df_check_permission($permission, &$object, $params=array() ){
546 function df_permission_names_as_array(&$perms){
548 return $ptool->namesAsArray($perms);
551 function df_permission_names_as_string(&$perms){
553 return $ptool->namesAsString($perms);
556 function df_block($params){
558 $query =&
$app->getQuery();
561 else if ( isset($params[
'record']) )
$table = $params[
'record']->_table;
564 if ( isset($params[
'name']) ) $name = $params[
'name'];
565 else throw new Exception(
'No name specified for block.', E_USER_ERROR);
567 unset($params[
'name']); unset($params[
'table']);
569 return $table->displayBlock($name, $params);
573 function df_translation_warning(&$record, $language=null){
574 import(
'Dataface/TranslationTool.php');
576 $tt->printTranslationStatusAlert($record, $language);
579 function df_editable($content, $id){
581 return $skinTool->editable(array(
'id'=>$id), $content, $skinTool);
584 function df_offset($date){
586 return df_translate(
'scripts.global.MESSAGE_UNKNOWN',
'Unknown');
588 $date = strtotime($date);
589 $offset = (strftime(
"%j")+strftime(
"%Y")*365)-
590 (strftime(
"%j",$date)+strftime(
"%Y",$date)*365);
592 $offset = (strftime(
"%W")+strftime(
"%Y")*52)-
593 (strftime(
"%W",$date)+strftime(
"%Y",$date)*52);
594 $end=($offset!=0?($offset>1?$offset .
" weeks ago":
"a week ago"):
"Today");
596 $end=($offset!=0?($offset>1?
"$offset days ago":
"Yesterday"):
"Today");
597 return strftime(
"%A, %B %d, %Y",$date).
" - ". $end;
602 function &df_get($uri, $filter=null){
610 function df_set($uri, $value){
615 if ( !function_exists(
'array_merge_recursive_unique') ){
618 function array_merge_recursive_unique($array0, $array1){
619 $func = __FUNCTION__;
621 $arrays = func_get_args();
622 $keyarrs = array_map(
'array_keys', $arrays);
623 $keys = array_merge($keyarrs[0], $keyarrs[1]);
624 foreach (
$keys as $key){
625 foreach ( $arrays as $array ){
626 if ( array_key_exists($key, $array) ){
627 if ( is_array($array[$key]) ){
629 else $result[$key] = $array[$key];
641 function array_merge_recursive_unique2($array0, $array1)
643 $arrays = func_get_args();
651 foreach($arrays as $array) {
655 array_shift($remains);
658 if(is_array($array)) {
660 foreach($array as $key => $value) {
661 if(is_array($value)) {
664 foreach($remains as $remain) {
665 if(array_key_exists($key, $remain)) {
666 array_push($args, $remain[$key]);
670 if(count($args) > 2) {
672 $result[$key] = call_user_func_array(__FUNCTION__, $args);
674 foreach($value as $vkey => $vval) {
689 function df_is_logged_in(){
693 function df_absolute_url($url){
694 if ( !$url )
return $_SERVER[
'HOST_URI'];
695 else if ( $url{0} ==
'/' ){
697 }
else if ( preg_match(
'/http(s)?:\/\//', $url) ){
701 $site_url = DATAFACE_SITE_URL;
703 if ($site_url{0} ==
'/' ) $host_uri = $host_uri.$site_url;
704 else $host_uri = $host_uri.
'/'.$site_url;
707 return $host_uri.
'/'.$url;
717 function df_utc_offset(){
718 $diff = preg_replace(
'/^([+-])(\d{1,2})(\d{2,2})$/',
'$1$2:$3',date(
'O'));
724 function df_tz_or_offset(){
727 if ( $tz )
return $tz;
728 else return df_utc_offset();
746 function df_get_file_system_version(){
747 static $version = -1;
749 if ( $version == -1 ){
750 if ( file_exists(
'version.txt') ){
751 $varr = file(
'version.txt');
755 list($fs_version) = $varr;
761 $fs_version = explode(
' ', $fs_version);
762 $fs_version = intval($fs_version[count($fs_version)-1]);
763 $version = $fs_version;
765 if ( !$version )
return df_get_database_version();
775 function df_get_database_version($db=null){
776 if (!$db ) $db = df_db();
777 static $version = -1;
778 if ( $version == -1 ){
779 $sql =
"select `version` from dataface__version limit 1";
780 $res = @mysql_query($sql, $db);
782 $res = mysql_query(
"create table dataface__version ( `version` int(5) not null default 0)", $db);
783 if ( !$res )
throw new Exception(mysql_error($db), E_USER_ERROR);
785 $res = mysql_query(
"insert into dataface__version values ('0')", $db);
786 if ( !$res )
throw new Exception(mysql_error($db), E_USER_ERROR);
788 $res = mysql_query($sql, $db);
790 throw new Exception(mysql_error($db), E_USER_ERROR);
794 list($version) = mysql_fetch_row($res);
804 if ( is_array($sql) ){
805 foreach ($sql as $q){
810 $res = mysql_query($sql, df_db());
812 error_log(
"Error executing SQL: $sql");
813 error_log(mysql_error(df_db()));
814 throw new Exception(mysql_error(df_db()));
821 function df_IPv4To6($ip) {
822 if ( strpos($ip,
':') !==
false ){
823 if ( $ip ===
'::1' )
return 'fe80::1';
826 if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ===
true) {
827 if (strpos($ip,
'.') > 0) {
828 $ip = substr($ip, strrpos($ip,
':')+1);
833 $is_v4 = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
834 if (!$is_v4) {
return false; }
835 $iparr = array_pad(explode(
'.', $ip), 4, 0);
836 $Part7 = base_convert(($iparr[0] * 256) + $iparr[1], 10, 16);
837 $Part8 = base_convert(($iparr[2] * 256) + $iparr[3], 10, 16);
838 return '::ffff:'.$Part7.
':'.$Part8;