X-Git-Url: https://git.harvie.cz/?a=blobdiff_plain;f=index.php;h=1138613d9e158f4649b245150dce6af9a213d8bc;hb=eb5795b64941a6b7dfd1b1fdf92a85943302b2fe;hp=8d6ff1460af8ab9350aea7d8cd06d3772ca7c5bd;hpb=7d20381fb0c10040979e12615c8584fbe86f2651;p=mirrors%2FSokoMan.git diff --git a/index.php b/index.php index 8d6ff14..1138613 100755 --- a/index.php +++ b/index.php @@ -33,7 +33,8 @@ class Sklad_HTML { function header($title='') { $home = URL_HOME; $script = $_SERVER['SCRIPT_NAME']; - $search = @trim($_GET['q']); + $search = htmlspecialchars(@trim($_GET['q'])); + $message = htmlspecialchars(@trim($_GET['message'])); return << @@ -53,6 +54,9 @@ class Sklad_HTML { +
+ $message +
EOF; } @@ -80,8 +84,12 @@ EOF; return $html; } + function internal_url($link) { + return $_SERVER['SCRIPT_NAME'].'/'.$link; + } + function link($title='n/a', $link='#void', $internal=true) { - if($internal) $link = $_SERVER['SCRIPT_NAME'].'/'.$link; + if($internal) $link = $this->internal_url($link); return "$title"; } @@ -242,8 +250,8 @@ class Sklad_DB extends PDO { return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK } - function build_query_select($class, $id=false, $limit=false, $offset=0, $search=false, $id_suffix='_id') { - $class = $this->escape($class); + function build_query_select($class, $id=false, $limit=false, $offset=0, $search=false, $order=false, $suffix_id='_id') { + //Configuration $join = array( 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'), 'model' => array('category', 'producer') @@ -251,8 +259,15 @@ class Sklad_DB extends PDO { $search_fields = array( 'item' => array('item_id','model_name','model_barcode','model_descript','producer_name','vendor_name') ); + + //Escaping + $class = $this->escape($class); + + //SELECT $sql="SELECT * FROM $class\n"; - if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$id_suffix)\n"; + //JOIN + if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$suffix_id)\n"; + //WHERE/REGEXP if($search) { $search = $this->quote($search); if(!isset($search_fields[$class])) { @@ -261,20 +276,25 @@ class Sklad_DB extends PDO { } $sql .= 'WHERE FALSE '; foreach($search_fields[$class] as $column) $sql .= "OR $column REGEXP $search "; - } elseif($id) $sql .= "WHERE $class$id_suffix = $id\n"; + } elseif($id) $sql .= "WHERE $class$suffix_id = $id\n"; + //LIMIT/OFFSET if($limit) { $limit = $this->escape((int)$limit); $offset = $this->escape((int)$offset); $sql .= "LIMIT $offset,$limit\n"; } + //ORDER + if(!$order) $order = $class.$suffix_id; + $sql .= "ORDER BY $order"; + return $sql; } function safe_query($sql) { $result = $this->query($sql); if(!$result) { - trigger_error('QUERY FAILED:
'.$sql.'
'); - die(); + $error = $this->errorInfo(); + die(trigger_error("QUERY FAILED ($error[0],$error[1]): $error[2]

QUERY:\n
$sql
")); } return $result; } @@ -300,7 +320,7 @@ class Sklad_DB extends PDO { if($class && $column['Field'] == $class.$suffix_id) continue; if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue; $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']); - $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table;"; + $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table;"; //TODO: tabulka nemusi obsahovat *_name!!! momentalne se to tyka jen tabulky user (a item - u ty to nevadi)! $result=$this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC); foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name]; } @@ -438,17 +458,17 @@ class Sklad_UI { new HTTP_Auth('SkladovejSystem', true, array($this->db->lms,'check_auth')); } - function post_redirect_get($last, $next) { //TODO prepracovat, tohle je uplna picovina... - //header('Location: '.$_SERVER['REQUEST_URI']); //TODO redirect (need templating system or ob_start() first!!!) - echo 'Hotovo. Poslední vložený záznam naleznete '.$this->html->link('zde', $last).'.
'. - 'Další záznam přidáte '.$this->html->link('zde', $next).'.'; - die(); + function post_redirect_get($location, $message='') { + $location = $this->html->internal_url($location).'?message='.urlencode($message); + header('Location: '.$location); + die("Location: $location"); } - function safe_include($dir,$name,$ext='.inc.php') { + function safe_include($dir,$name,$vars=array(),$ext='.inc.php') { if(preg_match('/[^a-zA-Z0-9-]/',$name)) die(trigger_error('SAFE INCLUDE: Securityfuck.')); $filename="$dir/$name$ext"; if(!is_file($filename)) die(trigger_error('SAFE INCLUDE: Fuckfound.')); + foreach($vars as $var => $val) $$var=$val; ob_start(); include($filename); $out=ob_get_contents(); @@ -458,7 +478,7 @@ class Sklad_UI { function process_http_request_post($action=false, $class=false, $id=false) { if($_SERVER['REQUEST_METHOD'] != 'POST') return; - echo('
'); //DEBUG (maybe todo remove)
+		//echo('
'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
 
 		//SephirPOST:
 
@@ -484,12 +504,16 @@ class Sklad_UI {
 				$table='item';
 				//print_r($values); //debug
 				$last = $this->db->insert_or_update_multitab($values);
-				$this->post_redirect_get("$table/$last/", "$table/new/");
+				$last = "$table/$last/";
+				$next = "$table/new/";
+				echo 'Hotovo. Poslední vložený záznam naleznete '.$this->html->link('zde', $last).'.
'. + 'Další záznam přidáte '.$this->html->link('zde', $next).'.'; + die(); break; case 'delete': if(!isset($_POST['sure']) || !$_POST['sure']) die(trigger_error('Sure user expected :-)')); $this->db->delete($class, $id); - die("Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-("); //TODO REDIRECT + $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-("); break; case 'image': $image_classes = array('model'); //TODO, use this more widely across the code @@ -497,9 +521,9 @@ class Sklad_UI { $image_destination = DIR_IMAGES."/$class/$id.jpg"; if($_FILES['image']['name'] == '') die(trigger_error('Kazde neco se musi nejak jmenovat!')); if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) { - chmod ($image_destination, 0664); - die('Obrazek se naladoval :)'); //TODO REDIRECT - } else die(trigger_error('Soubor se nenahral :(')); + chmod ($image_destination, 0664); + $this->post_redirect_get("$class/$id", 'Obrazek se naladoval :)'); + } else die(trigger_error('Soubor se nenahral :(')); break; default: trigger_error('Nothin\' to do here my cutie :-*'); @@ -518,7 +542,7 @@ class Sklad_UI { } $PATH_INFO=@trim($_SERVER[PATH_INFO]); - echo $this->html->header($PATH_INFO); + if($_SERVER['REQUEST_METHOD'] != 'POST') echo $this->html->header($PATH_INFO); //TODO tahle podminka naznacuje ze je v navrhu nejaka drobna nedomyslenost... //Sephirot: @@ -529,7 +553,8 @@ class Sklad_UI { die('Tell me why you cry'); break; case 'assistant': //assistant - echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2]); + $assistant_vars['step'] = isset($PATH_CHUNKS[3]) && is_numeric($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false; + echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2],$assistant_vars); break; default: //? $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false;