function cluster_call($event, $params=0) { global $db_central; $event=postgre_escape($event); $params=postgre_escape(serialize($params)); sql_query("select cluster_call($event, $params);", $db_central); }
function cache_remove($depend_id) { $pg_depend_id=postgre_escape($depend_id); sql_query("select cache_remove($pg_depend_id)"); }
function pg_modules_init() { global $modulekit; $plugins_db = array(); @($res = sql_query("select * from pg_modules")); // if table does not exist, create if ($res === false) { $res = sql_query("create table pg_modules ( id text not null, updates text[], primary key(id))"); } while ($elem = pg_fetch_assoc($res)) { $plugins_db[$elem['id']] = $elem; } foreach ($modulekit['order'] as $module_id) { // get current list of updates if (!isset($plugins_db[$module_id])) { $plugin_updates = array(); } else { $plugin_updates = pg_decode_array($plugins_db[$module_id]['updates']); } // build list of all updates -> array("20101010_1"=>array("sql")) // file_name->extensions $updates = array(); $dir = modulekit_file($module_id, "update/"); if (is_dir($dir)) { $d = opendir($dir); while ($f = readdir($d)) { if (substr($f, 0, 1) != ".") { $p = explode(".", $f); $updates[$p[0]][] = $p[1]; } } closedir($d); } ksort($updates); $list = modulekit_includes($module_id, "pgsql-functions"); // always reload functions sort($list); foreach ($list as $file) { debug("Module '{$module_id}', (re-)loading {$file}", "pg_modules"); sql_query(file_get_contents("{$file}")); } if (function_exists("{$module_id}_db_init") && !isset($plugins_db[$module_id])) { debug("Module '{$module_id}', calling db_init-function", "pg_modules"); call_user_func("{$module_id}_db_init"); } $list = modulekit_includes($module_id, "pgsql-init"); if (sizeof($list)) { // If plugin has never been loaded before, load db.sql if (!isset($plugins_db[$module_id])) { debug("Module '{$module_id}', initializing db", "pg_modules"); foreach ($list as $file) { sql_query(file_get_contents($file)); } } else { $updates_done = $plugin_updates; foreach ($updates as $update => $files) { if (!in_array($update, $updates_done)) { debug("Module '{$module_id}', loading update {$update}", "pg_modules"); if (in_array("sql", $files)) { sql_query(file_get_contents(modulekit_file($module_id, "update/{$update}.sql"))); } } } } } // save update information to database $pg_plugin = postgre_escape($module_id); $pg_updates = pg_encode_array(array_keys($updates), "text"); sql_query("delete from pg_modules where id={$pg_plugin}"); sql_query("insert into pg_modules values ({$pg_plugin}, {$pg_updates})"); } }
function match_to_sql($match, $table_def, $type="exact") { $not=""; $same="false"; switch($match[0]) { case "or": if(sizeof($match)==1) return "true"; $ret=array(); for($i=1; $i<sizeof($match); $i++) { $ret[]=match_to_sql($match[$i], $table_def, $type); } return "(".implode(") or (", $ret).")"; case "and": if(sizeof($match)==1) return "true"; $ret=array(); for($i=1; $i<sizeof($match); $i++) { $ret[]=match_to_sql($match[$i], $table_def, $type); } return "(".implode(") and (", $ret).")"; case "not": return "not ".match_to_sql($match[1], $table_def, $type); case "is not": $not="not"; case "is": switch($type) { case "index": $ret=array(); for($i=2; $i<sizeof($match); $i++) { $ret[]="osm_tags @> ".array_to_hstore(array($match[1]=>$match[$i])); } if($not) $ret[]="osm_tags ? ".postgre_escape($match[1]); return "$not (".implode(") or (", $ret).")"; default: $ret=array(); for($i=2; $i<sizeof($match); $i++) { $ret[]=postgre_escape($match[$i]); } if($not) $not="not osm_tags ? ".postgre_escape($match[1])." or not"; return "($not coalesce(osm_tags->".postgre_escape($match[1]).", '') in (".implode(", ", $ret)."))"; } case "~is not": $not="not"; case "~is": switch($type) { case "index": $ret=array(); for($i=2; $i<sizeof($match); $i++) { $ret[]=postgre_escape($match[$i]); } register_index($table_def['table'], $match[1], "tsvector", $table_def['id']); return "$not to_tsvector('simple', ".match_to_sql_colname($match[1], $table_def, $type).") @@ to_tsquery('simple', ".implode("||' | '||", $ret).")"; default: $ret=array(); for($i=2; $i<sizeof($match); $i++) { $ret[]=postgre_escape($match[$i]); } return "$not oneof_in(".match_to_sql_colname($match[1], $table_def, $type).", ARRAY[".implode(", ", $ret)."])"; } case "exist": return "osm_tags ? ".postgre_escape($match[1]); case "exist not": return "not osm_tags ? ".postgre_escape($match[1]); case ">=": $same="true"; case ">": $number=parse_number($match[2]); if($type=="index") { // for index-search we make an index every 100 // units and change the select-statement accordingly $same="true"; $number=pow(100, floor(log($number, 100)+0.000001)); register_index($table_def['table'], $match[1], "gteq", $table_def['id'], $number); $var="split_semicolon(".match_to_sql_colname($match[1], $table_def, $type).")"; } else { $var=match_to_sql_colname($match[1], $table_def, $type); } return "oneof_between($var, $number, $same, null, null)"; case "<=": $same="true"; case "<": $number=parse_number($match[2]); if($type=="index") { $same="true"; $number=pow(100, ceil(log($number, 100))); register_index($table_def['table'], $match[1], "lteq", $table_def['id'], $number); $var="split_semicolon(".match_to_sql_colname($match[1], $table_def, $type).")"; } else { $var=match_to_sql_colname($match[1], $table_def, $type); } return "oneof_between($var, null, null, $number, $same)"; case "true": return "true"; case "false": return "false"; default: print "invalid match! "; print_r($match); return "true"; } }
function category_restore($id, $param=array()) { global $db_central; global $current_user; if(!isset($param['version'])) return false; $cat=new category($id); $newest=$cat->get_newest_version(); $pg_id=postgre_escape($id); $pg_version=postgre_escape($param['version']); $pg_newest=postgre_escape($newest); $future=uniqid(); $pg_future=postgre_escape($future); // compile version tags $version_tags=new tags(); $version_tags->set("user", $current_user->username); $version_tags->set("date", Date("c")); $version_tags->set("msg", "Restore version '{$param['version']}'"); $pg_version_tags=array_to_hstore($version_tags->data()); $sql ="begin;"; $sql.="insert into category (select $pg_id, tags, $pg_future, Array[$pg_newest], $pg_version_tags from category where version=$pg_version);"; $sql.="insert into category_rule (select $pg_id, rule_id, tags, $pg_future from category_rule where version=$pg_version);"; $sql.="delete from category_current where category_id=$pg_id;"; $sql.="insert into category_current values ($pg_id, $pg_future, now());"; $sql.="commit;"; sql_query($sql, $db_central); return array("status"=>true, "version"=>$future); }
function __construct($param=0, $force_auth=0) { global $db_central; $this->authenticated=false; // anonymous user if(!$param) { $this->load_anonymous(); return; } // forced authentication (e.g. we found a valid auth_id) if($force_auth) { $this->username=$param['username']; $this->pg_username=postgre_escape($this->username); $this->auth_id=$param['auth_id']; $this->authenticated=true; } else { // Other methods for auth, e.g. OpenID $other_auth=null; call_hooks("user_is_valid", &$other_auth, $param); if($other_auth) { $this->username=$other_auth['username']; $this->pg_username=postgre_escape($other_auth['username']); $this->authenticated=true; } // also other auth methods did not work else { $this->username=$param['username']; $this->pg_username=postgre_escape($param['username']); } } // get user from database $res=sql_query("select * from user_list where username={$this->pg_username}", $db_central); // user does not exist -> anonymous if(!($elem=pg_fetch_assoc($res))) { $this->load_anonymous(); return; } // not authenticated yet, check password if(!$this->authenticated) { if($elem['md5_password']!=$param['md5_password']) { unset($this->username); unset($this->pg_username); $this->load_anonymous(); return; } } $this->authenticated=true; $this->tags=new tags(parse_hstore($elem['osm_tags'])); $this->create_auth(); }