$value) $new[utf8_decode($key)] = utf8_decode($value); return $new; } function array_to_utf8_encode($arr) { $new = array(); foreach($arr as $key => $value) $new[utf8_encode($key)] = utf8_encode($value); return $new; } function html2db($text,$charset=null) { global $html_charset,$db_charset; if (!$charset) $charset=$db_charset; if (($charset=='utf8' && $html_charset=='utf-8') || ($charset=='latin1' && $html_charset=='iso-8859-1')) return $text; if ($charset=='utf8' && $html_charset=='iso-8859-1') return utf8_encode($text); die('character convertion not implemented'); } function esc_js_str($text) { return "'".str_replace(array("\n\r","\r\n","\n","\r"),"'+\"\\n\"+'",str_replace("'","\\'",$text))."'"; } function esc_only($text,$nullable=true,$charset=null) { $text=trim($text); if (strlen($text)==0 && $nullable) return 'NULL'; return mysql_real_escape_string(html2db($text,$charset)); } function esc_str($text,$nullable=true,$charset=null) { $text=trim($text); if (strlen($text)==0 && $nullable) return 'NULL'; return "'".mysql_real_escape_string(html2db($text,$charset))."'"; } function esc_float($text,$nullable=true,$charset=null) { $text=trim($text); if (strlen($text)==0 && $nullable) return 'NULL'; $text=0.0 + str_replace(',','.',str_replace('.','',$text)); return mysql_real_escape_string(html2db($text,$charset)); } function esc_bool($text) { $text=strtolower(trim($text)); if (strlen($text)==0 || $text=='0' || strtolower(substr($text,0,1))=='n') return 'NULL'; return '1'; } function esc_id($text) { if (!$text) return 'NULL'; return (0+mysql_real_escape_string($text)); } function esc_date($text) { $text=MySQLTABLE::datum2date(strtolower(trim($text))); if ($text) return "'".$text."'"; return 'NULL'; } function db_error_report($query=null,$stop_on_errors=null) { global $silent_errors, $send_error_to,$halt_on_errors; if (!$silent_errors) echo $query.'
Database Query Error!
'.mysql_error().'
ORIGINAL QUERY:
'.$query; if ($send_error_to) mail($send_error_to,'TOPOI-DB-Error',mysql_error()."\n\n".$query); if ($halt_on_errors) exit; } function db_query($query,$stop_on_errors=null) { $result = mysql_query($query); if (mysql_error()) db_error_report($query); return $result; } function db_value($query,$stop_on_errors=null) { $result = mysql_query($query); if (mysql_error()) db_error_report($query); if ($result===FALSE) return null; if (mysql_num_rows($result)==0) { mysql_free_result($result); return null; } $row = mysql_fetch_row($result); mysql_free_result($result); return db2html($row[0]); } function db_values($query) { $values = array(); $result = mysql_query($query); if (mysql_error()) db_error_report($query); if ($result===FALSE) return null; if (mysql_num_rows($result)==0) { mysql_free_result($result); return $values; } while($row = mysql_fetch_row($result)) { array_push($values,db2html($row[0])); } mysql_free_result($result); return $values; } function db_indexed_values($query) { $values = array(); $result = mysql_query($query); if (mysql_error()) db_error_report($query); if ($result===FALSE) return null; if (mysql_num_rows($result)==0) { mysql_free_result($result); return $values; } while($row = mysql_fetch_row($result)) { $values[db2html($row[0])]=db2html($row[1]); } mysql_free_result($result); return $values; } function db_first_row($query) { $result = mysql_query($query); if (mysql_error()) db_error_report($query); if ($result===FALSE) return false; if (mysql_num_rows($result)==0) { mysql_free_result($result); return array(); } $row = mysql_fetch_assoc($result); mysql_free_result($result); return db2html_array($row); } function db_rows($query) { $rows=array(); $result = mysql_query($query); if (mysql_error()) db_error_report($query); if ($result===FALSE) { db_error_report($query); return $rows; } if (mysql_num_rows($result)==0) { mysql_free_result($result); return $rows; } while ($row = mysql_fetch_assoc($result)) array_push($rows,db2html_array($row)); mysql_free_result($result); return $rows; } function db_indexed_rows($query,$index_field='id') { $rows=array(); $result = mysql_query($query); if (mysql_error()) db_error_report($query); if ($result===FALSE) { echo mysql_error(); exit; return $rows; } if (mysql_num_rows($result)==0) { mysql_free_result($result); return $rows; } while ($row = mysql_fetch_assoc($result)) $rows[db2html($row[$index_field])]=db2html_array($row); mysql_free_result($result); return $rows; } function db_grouped_rows($query,$index_field_1='id') { $rows=array(); $result = mysql_query($query); if (mysql_error()) db_error_report($query); if ($result===FALSE) { echo mysql_error(); exit; return $rows; } if (mysql_num_rows($result)==0) { mysql_free_result($result); return $rows; } while ($row = mysql_fetch_assoc($result)) { $idx1 = db2html($row[$index_field_1]); if (!(isset($rows[$idx1]) && is_array($rows[$idx1]))) $rows[$idx1] = array(); array_push($rows[$idx1],db2html_array($row)); } mysql_free_result($result); return $rows; } function db_grouped2_rows($query,$index_field_1='id',$index_field_2='id') { $rows=array(); $result = mysql_query($query); if (mysql_error()) db_error_report($query); if ($result===FALSE) { echo mysql_error(); exit; return $rows; } if (mysql_num_rows($result)==0) { mysql_free_result($result); return $rows; } while ($row = mysql_fetch_assoc($result)) { $idx1 = db2html($row[$index_field_1]); if (!(isset($rows[$idx1]) && is_array($rows[$idx1]))) $rows[$idx1] = array(); $idx2 = db2html($row[$index_field_2]); if (!(isset($rows[$idx1][$idx2]) && is_array($rows[$idx1][$idx2]))) $rows[$idx1][$idx2] = array(); array_push($rows[$idx1][$idx2],db2html_array($row)); } mysql_free_result($result); return $rows; } function db_grouped3_rows($query,$index_field_1='id',$index_field_2='id',$index_field_3='id') { $rows=array(); $result = mysql_query($query); if (mysql_error()) db_error_report($query); if ($result===FALSE) { echo mysql_error(); exit; return $rows; } if (mysql_num_rows($result)==0) { mysql_free_result($result); return $rows; } while ($row = mysql_fetch_assoc($result)) { $idx1 = db2html($row[$index_field_1]); if (!(isset($rows[$idx1]) && is_array($rows[$idx1]))) $rows[$idx1] = array(); $idx2 = db2html($row[$index_field_2]); if (!(isset($rows[$idx1][$idx2]) && is_array($rows[$idx1][$idx2]))) $rows[$idx1][$idx2] = array(); $idx3 = db2html($row[$index_field_3]); if (!(isset($rows[$idx1][$idx2][$idx3]) && is_array($rows[$idx1][$idx2][$idx3]))) $rows[$idx1][$idx2][$idx3] = array(); array_push($rows[$idx1][$idx2][$idx3],db2html_array($row)); } mysql_free_result($result); return $rows; } function redaktionstext($name) { return db_value('SELECT `'.$name.'` FROM redaktionstexte LIMIT 1'); } class MySQLTable { /*** INTERNAL PROPERTIES (do not change!) ***/ var $table = ''; var $lang = 'de'; var $initialized = false; var $executed = false; var $queried_fields = false; var $field_maxlength = array(); var $col_descr = array(); var $editable_fields = array(); var $field2name = array(); var $allowed_field_values = array(); /*** METHOD DEFINITIONS ***/ function connect($host,$user,$pw,$db) { global $MySQLTable_db_link; $MySQLTable_db_link = mysql_connect($host, $user, $pw) or die("Keine Verbindung zum Datenbankserver möglich: " . mysql_error()); mysql_set_charset('utf8', $MySQLTable_db_link); mysql_select_db($db) or die('Auswahl der Datenbank "'.$db.'" ist fehlgeschlagen.'); } function MySQLTable($table,$id='et') { global $html_charset; $this->html_charset = $html_charset; $this->table = $table; $this->name = $table; $this->id = $id; $this->magic_quotes=get_magic_quotes_gpc(); /*** read mysql field description from table ***/ $rc = mysql_query('SHOW FULL COLUMNS FROM '.$this->table); if (!$rc) return false; $this->col_descr=array(); while($row = mysql_fetch_assoc($rc)) { $this->col_descr[$row['Field']]=$row; $collation = explode('_',$row['Collation']); $this->col_descr[$row['Field']]['Charset']=$collation[0]; } mysql_free_result($rc); /*** if not editable fields are specified than using all ***/ $this->editable_fields = array(); foreach($this->col_descr as $field => $descr) { array_push($this->editable_fields,$field); $this->field2name[$field]=$field; } $this->index = db_indexed_rows('SHOW INDEX FROM `'.$table.'`','Column_name'); } function insert_input($row,$prefix=null,$alt_form_fields=null) { global $form_field_prefix; if ($prefix === null) $prefix=$form_field_prefix; if ($alt_form_fields===null) $form_fields=$GLOBALS['form_fields']; else $form_fields=$alt_form_fields; if ($this->field2name['form_content_id']) { $form_content_id=form_content_id($row); $old = db_first_row('SELECT * FROM '.$this->table.' WHERE form_content_id='.esc_str($form_content_id).' LIMIT 1'); if ($old && is_array($old) && sizeof($old)) return $old; $row[$prefix.'form_content_id'] = $form_content_id; array_push($form_fields,$prefix.'form_content_id'); } if ($this->field2name['erstellt_am']) { $row[$prefix.'erstellt_am'] = date('Y-m-d H:i:s'); array_push($form_fields,$prefix.'erstellt_am'); } $fields = array(); $values = array(); foreach(array_keys($this->col_descr) as $field) { // if (array_key_exists($prefix.$field,$row) && in_array($prefix.$field,$form_fields) && in_array($field,$this->editable_fields) && $field!='id') { if (in_array($prefix.$field,$form_fields) && in_array($field,$this->editable_fields) && $field!='id') { array_push($fields,$field); $value = $row[$prefix.$field]; array_push($values,$this->field_sql_value($field,$value)); } } $query = 'INSERT INTO '.$this->table.' (`'.join('`,`',$fields).'`) VALUES ('.join(',',$values).')'; db_query($query); $return_id = mysql_insert_id(); $_REQUEST[$prefix.'id'] = $return_id; return $return_id; } function save_input($row,$prefix=null,$alt_form_fields=null) { global $form_field_prefix; if ($alt_form_fields===null) $form_fields=$GLOBALS['form_fields']; else $form_fields=$alt_form_fields; if ($prefix === null) $prefix=$form_field_prefix; if (!$row[$prefix.'id']) { return $this->insert_input($row,$prefix); } else return $this->update_input($row,$prefix); } function update_input($row,$prefix=null,$alt_form_fields=null) { global $form_field_prefix; if ($prefix === null) $prefix=$form_field_prefix; if ($alt_form_fields===null) $form_fields=$GLOBALS['form_fields']; else $form_fields=$alt_form_fields; if (!$row[$prefix.'id']) die('Coudn\'t update without IDentifier '); if ($this->field2name['modifiziert_am']) { $row[$prefix.'modifiziert_am'] = date('Y-m-d H:i:s'); array_push($form_fields,$prefix.'modifiziert_am'); } $set = array(); foreach(array_keys($this->col_descr) as $field) { // if (array_key_exists($prefix.$field,$row) && in_array($prefix.$field,$form_fields) && in_array($field,$this->editable_fields) && $field!='id') { if (in_array($prefix.$field,$form_fields) && in_array($field,$this->editable_fields) && $field!='id') { array_push($set,$field.'='.$this->field_sql_value($field,$row[$prefix.$field])); } } if (sizeof($set) > 0) { $query = 'UPDATE '.$this->table.' SET '.implode(',',$set).' WHERE id='.$row[$prefix.'id'].' LIMIT 1'; //if ($this->table=='teilnahme') {print_r($_REQUEST); exit;} // if ($this->table=='teilnahme') {echo $query; exit;} db_query($query); } return $row[$prefix.'id']; } function update($row,$where,$prefix=null) { $set = array(); foreach(array_keys($this->col_descr) as $field) { if (array_key_exists($field,$row) && in_array($field,$this->editable_fields)) { array_push($set,$field.'='.$this->field_sql_value($field,$row[$prefix.$field])); } } if (sizeof($set) > 0) { $query = 'UPDATE '.$this->table.' SET '.implode(',',$set).' WHERE '.$where; return db_query($query); } return 0; } function field_sql_value($field, $value) { $descr = $this->col_descr[$field]; $charset = $descr['Charset']; if (strpos($field,'email')!==FALSE) $value = str_replace(';',',',$value); $type = $descr['Type']; $nullable = ($descr['Null'] != 'NO'); if ($type=='tinyint(1)') { if ($value) return 1; else return 'NULL'; } else if (substr($type,0,3)=='int' || substr($type,0,8)=='smallint' || substr($type,0,7)=='tinyint' || substr($type,0,5)=='float' || substr($type,0,7)=='decimal') { $arg = explode(' ',trim($value)); $value = $arg[0]; if ($this->lang == 'de') $value = str_replace(',','.',str_replace('.','',$value)); if ($nullable && $value == 0) return 'NULL'; return (0.0+$value); } else if (substr($type,0,4)=='text' || substr($type,0,8)=='longtext') { return esc_str($value,$nullable,$charset); } else if (substr($type,0,7)=='varchar' || substr($type,0,4)=='char') { return esc_str($value,$nullable,$charset); } else if (substr($type,0,4)=='date') { if ($this->lang == 'de') return esc_str($this->datum2date($value),$nullable,$charset); return esc_str($this->datum2date($value),$nullable,$charset); } } function datum2date($value) { if ($value=='0000-00-00 00:00:00' || $value=='0000-00-00') return ''; if ($value===null || strlen(trim($value))==0) return ''; $value=trim($value); if ($value=='now' || $value=='heute' || $value=='n' || $value=='h' || $value=='today' || $value=='t' ) return date('Y-m-d H:i:s'); if ($value=='y' || $value=='g' ) return date('Y-m-d H:i:s',strtotime('-1 day')); $m = array(); if (preg_match('/((\+|\-)\s*\d+)\s*m/',$value,$m)) return date('Y-m-d H:i:s',strtotime($m[1].' months')); if (preg_match('/((\+|\-)\s*\d+)\s*w/',$value,$m)) return date('Y-m-d H:i:s',strtotime($m[1].' weeks')); if (preg_match('/((\+|\-)\s*\d+)/',$value,$m)) return date('Y-m-d H:i:s',strtotime($m[1].' days')); if (preg_match('/^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$/',$value)) return $value; if (preg_match('/^\\d{14}$/',$value)) return substr($stamp,0,4).'.'.substr($stamp,4,2).'.'.substr($stamp,6,2).' '. substr($stamp,8,2).':'.substr($stamp,10,2).':'.substr($stamp,12,2); $Y=0;$m=1;$d=1;$H=0;$i=0;$s=0;$date_not_found=false; $match=array(); if (preg_match('/^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})?/',$value,$match)) { $d=$match[1]; $m=$match[2]; $Y=$match[3]; if (!$Y) $Y=date('Y'); else if ($Y<100) $Y+=2000; $value=substr($value,strlen($match[0])); } else { $Y=date('Y');$m=date('m');$d=date('d'); $date_not_found=true; } $match=array(); if (preg_match('/^\\s*(\\d{1,2})(:(\\d{1,2})(:(\\d{1,2}))?)?$/',$value,$match)) { $H=0+$match[1];$i=0+$match[3];$s=0+$match[5]; } else if ($date_not_found) { return ''; } return sprintf('%04d-%02d-%02d %02d:%02d:%02d',$Y,$m,$d,$H,$i,$s); } } ?>