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 <<<EOF
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<input type="submit" value="search items" />
</form -->
</div>
+<div style="background-color:#FFDDDD;">
+ <font color="red">$message</font>
+</div>
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 "<a href='$link'>$title</a>";
}
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, $id_suffix='_id') {
+ //Configuration
$join = array(
'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
'model' => array('category', 'producer')
$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";
+ //JOIN
if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$id_suffix)\n";
+ //WHERE/REGEXP
if($search) {
$search = $this->quote($search);
if(!isset($search_fields[$class])) {
$sql .= 'WHERE FALSE ';
foreach($search_fields[$class] as $column) $sql .= "OR $column REGEXP $search ";
} elseif($id) $sql .= "WHERE $class$id_suffix = $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.'_id';
+ $sql .= "ORDER BY $order";
+
return $sql;
}
function safe_query($sql) {
$result = $this->query($sql);
if(!$result) {
- trigger_error('<font color=red><b>QUERY FAILED:</b><pre>'.$sql.'</pre></font>');
- die();
+ $error = $this->errorInfo();
+ die(trigger_error("<font color=red><b>QUERY FAILED ($error[0]): </b>$error[2]<br /><br /><b>QUERY:</b>\n<pre>$sql</pre></font>"));
}
return $result;
}
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).'.<br />'.
- '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,$vars=array(),$ext='.inc.php') {
function process_http_request_post($action=false, $class=false, $id=false) {
if($_SERVER['REQUEST_METHOD'] != 'POST') return;
- echo('<pre>'); //DEBUG (maybe todo remove)
+ //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
//SephirPOST:
$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).'.<br />'.
+ '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
$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 :-*');
}
$PATH_INFO=@trim($_SERVER[PATH_INFO]);
- echo $this->html->header($PATH_INFO);
+ if($_SERVER['REQUEST_METHOD'] != 'POST') echo $this->html->header($PATH_INFO);
//Sephirot: