Knihovny presunuty do ./lib
[mirrors/SokoMan.git] / index.php
... / ...
CommitLineData
1<?php
2/*
3 * SkladovySystem - Storage management system compatible with LMS
4 * Copyright (C) 2011 Tomas Mudrunka
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20require_once('sklad.conf.php');
21set_include_path(DIR_LIB.PATH_SEPARATOR.get_include_path());
22
23require_once('Sklad_LMS-fake.class.php');
24require_once('HTTP_Auth.class.php');
25
26/**
27* Trida poskytuje vseobecne funkce pro generovani HTML kodu
28*
29* Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
30*
31* @package HTML
32* @author Tomas Mudrunka
33*/
34class HTML {
35 function row($row) {
36 $html='<tr>';
37 foreach($row as $var) {
38 if(trim($var) == '') $var = '&nbsp;';
39 $html.="<td>$var</td>";
40 }
41 $html.='</tr>';
42 return $html;
43 }
44
45 function table(&$table, $params='border=1') {
46 $html="<table $params>";
47 $header=true;
48 foreach($table as $row) {
49 if($header) {
50 $html.=$this->row(array_keys($row));
51 $header=false;
52 }
53 $html.=$this->row($row);
54 }
55 $html.='</table>';
56 return $html;
57 }
58
59 function link($title='n/a', $link='#void', $internal=true) {
60 if($internal) $link = $this->internal_url($link);
61 return "<a href='$link'>$title</a>";
62 }
63
64 function img($src='#void', $title='img') {
65 return "<img src='$src' alt='$title' title='$title' width=64 />";
66 }
67
68 function input($name=false, $value=false, $type='text', $placeholder=false, $options=false) {
69 $html = "<input type='$type' ";
70 if($name) $html.= "name='$name' ";
71 if(!is_bool($value)) $html.= "value='$value' ";
72 if($options) $html.= "$options ";
73 if($placeholder) $html.= "placeholder='$placeholder' ";
74 $html .= '/>';
75 return $html;
76 }
77
78 function select($name, $selectbox, $default=false) {
79 //echo('<pre>'); print_r($selectbox);
80 $html = "<select name='$name'>";
81
82 if($default) {
83 $value=$default; $title=$selectbox[$value];
84 $html .= "<option value='$value'>$value :: $title</option>";
85 unset($selectbox[$value]);
86 }
87 foreach($selectbox as $value => $title) {
88 $html .= "<option value='$value'>$value :: $title</option>";
89 }
90 $html .= "</select>";
91 return $html;
92 }
93}
94
95/**
96* Trida poskytuje podpurne funkce pro generovani HTML kodu specificke pro sklad
97*
98* Tato trida by nemela sama nic vypisovat (vyjma chybovych a debugovacich hlasek)!
99*
100* @package Sklad_HTML
101* @author Tomas Mudrunka
102*/
103class Sklad_HTML extends HTML {
104 function header($title='') {
105 $home = URL_HOME;
106 $script = $_SERVER['SCRIPT_NAME'];
107 $search = htmlspecialchars(@trim($_GET['q']));
108 $message = strip_tags(@trim($_GET['message']),'<a><b><u><i>');
109 return <<<EOF
110<head>
111 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
112</head>
113<h1><a href="$script/">SystémSklad</a><small>$title</small></h1>
114
115<style type="text/css">
116.menu li {
117 float: left;
118 padding: 0.2em;
119}
120
121.menu * li {
122 float: none;
123}
124
125.menu * menu {
126 position: absolute;
127 padding: 0.2em;
128}
129
130.menu, .menu * menu {
131 list-style: none;
132}
133
134.menu * menu {
135 border: 1px solid orange;
136 display: none;
137 margin: 0;
138}
139
140.menu li:hover menu, .menu li:hover {
141 display: block;
142 background-color: yellow;
143}
144
145</style>
146
147<div>
148 <menu class="menu">
149 <li><a href="?logout">Logout</a></li>
150 <li><a href="$script/">Home</a></li>
151 <li><a href="#">Assistants</a>
152 <menu>
153 <li><a href="$script/assistant/stats">stats</a></li>
154 <li><a href="$script/assistant/store">store</a></li>
155 <li><a href="$script/assistant/store-single">store-single</a></li>
156 <li><a href="$script/assistant/dispose">dispose</a></li>
157 <li><a href="$script/assistant/sell">sell</a></li>
158 <li>&darr;&darr; BETA &darr;&darr;</li>
159 </menu>
160 </li>
161 <li><a href="#">List</a>
162 <menu>
163 <li><a href="$script/item">item</a></li>
164 <li><a href="$script/model">model</a></li>
165 <li><a href="$script/category">category</a></li>
166 <li><a href="$script/producer">producer</a></li>
167 <li><a href="$script/vendor">vendor</a></li>
168 <li><a href="$script/room">room</a></li>
169 <li><a href="$script/status">status</a></li>
170 </menu>
171 </li>
172 <li><a href="#">New</a>
173 <menu>
174 <li><a href="$script/item/new">item</a></li>
175 <li><a href="$script/model/new">model</a></li>
176 <li><a href="$script/category/new">category</a></li>
177 <li><a href="$script/producer/new">producer</a></li>
178 <li><a href="$script/vendor/new">vendor</a></li>
179 <li><a href="$script/room/new">room</a></li>
180 <li><a href="$script/status/new">status</a></li>
181 </menu>
182 </li>
183 </menu>
184
185 <div style="float: right;">
186 <form action="$script/assistant/go" method="GET" style="float: left;"><!-- TODO: Display only when go plugin available -->
187 <input type="text" name="q" placeholder="smart id..." />
188 <input type="submit" value="go" />
189 </form>
190 <form action="?" method="GET" style="float: left;">
191 <input type="text" name="q" placeholder="regexp..." value="$search" />
192 <input type="submit" value="filter" />
193 </form>
194 <!-- form action="$script/" method="GET">
195 <input type="text" name="q" placeholder="regexp..." value="$search" />
196 <input type="submit" value="search items" />
197 </form -->
198 </div>
199</div>
200<hr style="clear: both;" />
201<div style="background-color:#FFDDDD;">
202 <font color="red">$message</font>
203</div>
204EOF;
205 }
206
207 function internal_url($link) {
208 return $_SERVER['SCRIPT_NAME'].'/'.$link;
209 }
210
211 function table_add_images(&$table) {
212 $image = array('model_id');
213 foreach($table as $id => $row) {
214 foreach($image as $column) if(isset($table[$id][$column])) {
215 $type = @array_shift(preg_split('/_/', $column));
216 $src=URL_IMAGES."/$type/".$table[$id][$column].'.jpg';
217 $table[$id][$type.'_image']=$this->img($src, $table[$id][$column]);
218 }
219 }
220 }
221
222 function table_collapse(&$table) {
223 $collapse = array(
224 'item_id' => 'item_id',
225 'model_id' => 'model_name',
226 'category_id' => 'category_name',
227 'producer_id' => 'producer_name',
228 'vendor_id' => 'vendor_name',
229 'room_id' => 'room_name',
230 'status_id' => 'status_name',
231 );
232 foreach($table as $id => $row) {
233 foreach($collapse as $link => $title)
234 if(isset($table[$id][$link]) && isset($row[$title])) {
235 $type = @array_shift(preg_split('/_/', $link));
236 if($link != $title) unset($table[$id][$link]);
237 $table[$id][$title]=$this->link($row[$title], $type.'/'.$row[$link].'/');
238 }
239 }
240 }
241
242 function table_sort(&$table) {
243 $precedence = array('item_id', 'model_image', 'model_name','model_descript','category_name','status_name','room_name');
244 $table_sorted = array();
245 foreach($table as $id => $row) {
246 $table_sorted[$id] = array();
247 foreach($precedence as $column) if(isset($table[$id][$column])) {
248 $table_sorted[$id][$column]=$table[$id][$column];
249 unset($table[$id][$column]);
250 }
251 $table_sorted[$id]=array_merge($table_sorted[$id],$table[$id]);
252 }
253 $table = $table_sorted;
254 }
255
256 function render_item_table($table) {
257 $this->table_add_images($table);
258 $this->table_collapse($table);
259 $this->table_sort($table);
260 return $this->table($table);
261 }
262
263 function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true) {
264 //echo('<pre>'); print_r($selectbox);
265 //echo('<pre>'); print_r($current);
266 $update = false;
267 if(is_array($current)) {
268 $update = true;
269 $current = array_shift($current);
270 }
271
272 if(!is_array($hidecols)) $hidecols = array('item_author', 'item_valid_from', 'item_valid_till'); //TODO Autodetect
273
274 $action = $action ? " action='$action'" : false;
275 $html="<form$action method='POST'>";
276 if($multi_insert) $html.='<div name="input_set" style="float:left; border:1px solid grey;">';
277 //$html.=$this->input('table', $class, 'hidden');
278 foreach($columns as $column) {
279 $html.=$class.':<b>'.$column['Field'].'</b>: ';
280 $name="values[$class][".$column['Field'].'][]';
281 $val = $update && isset($current[$column['Field']]) ? $current[$column['Field']] : false;
282 switch(true) {
283 case (preg_match('/auto_increment/', $column['Extra']) || in_array($column['Field'], $hidecols)):
284 if(!$val) $val = '';
285 $html.=$this->input($name, $val, 'hidden');
286 $html.=$val.'(AUTO)';
287 break;
288 case isset($selectbox[$column['Field']]):
289 $html.=$this->select($name,$selectbox[$column['Field']],$val);
290 break;
291 default:
292 $html.=$this->input($name, $val);
293 break;
294 }
295 $html.='<br />';
296 }
297
298 if($multi_insert) {
299 //TODO, move to separate JS file
300 $html.=<<<EOF
301 </div>
302 <span name="input_set_next"></span><br style="clear:both" />
303 <script>
304 function duplicate_element(what, where) {
305 document.getElementsByName(where)[0].outerHTML =
306 document.getElementsByName(what)[0].outerHTML
307 + document.getElementsByName(where)[0].outerHTML;
308 }
309 </script>
310 <a href='#' onClick="duplicate_element('input_set', 'input_set_next')">+</a>
311EOF;
312 }
313
314 $btn = is_array($current) ? 'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting...
315 $html.=$this->input(false, $btn, 'submit');
316 $html.='</form>';
317 return $html;
318 }
319}
320
321/**
322* Trida poskytuje rozhrani k databazi skladu
323*
324* @package Sklad_DB
325* @author Tomas Mudrunka
326*/
327class Sklad_DB extends PDO {
328 function __construct() {
329 $this->lms = new Sklad_LMS();
330
331 parent::__construct(
332 DB_DSN, DB_USER, DB_PASS,
333 array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") //Force UTF8 for MySQL
334 );
335 }
336
337 function escape($str) {
338 return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
339 }
340
341 function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') {
342 //Configuration
343 $join = array(
344 'item' => array('model', 'category', 'producer', 'vendor', 'room', 'status'),
345 'model' => array('category', 'producer')
346 ); //TODO Autodetect using foreign keys?
347 $search_fields = array(
348 'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name')
349 ); //TODO Autodetect
350
351 //Escaping
352 $class = $this->escape($class);
353
354 //SELECT
355 $sql="SELECT * FROM $class\n";
356 //JOIN
357 if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$suffix_id)\n";
358 //WHERE/REGEXP
359 if($search) {
360 $search = $this->quote($search);
361 if(!isset($search_fields[$class])) $this->post_redirect_get($class, "Ve tride $class zatim vyhledavat nemozno :-(");
362 $sql_search = '';
363 foreach($search_fields[$class] as $column) $sql_search .= "OR $column REGEXP $search ";
364 $where[] = "FALSE $sql_search";
365 } elseif($id) $where[] = "$class$suffix_id = $id";
366 if(!$history && $this->contains_history($class)) $where[] = $class.'_valid_till=0';
367
368 if($where) $sql .= 'WHERE ('.implode(') AND (', $where).")\n";
369 //ORDER
370 if(!$order) $order = $class.$suffix_id;
371 if($this->contains_history($class)) $order .= ",${class}_valid_from DESC";
372 $sql .= "ORDER BY $order\n";
373 //LIMIT/OFFSET
374 if($limit) {
375 $limit = $this->escape((int)$limit);
376 $offset = $this->escape((int)$offset);
377 $sql .= "LIMIT $offset,$limit\n";
378 }
379
380 return $sql;
381 }
382
383 function safe_query($sql, $fatal=true) {
384 $result = $this->query($sql);
385 if(!$result) {
386 $error = $this->errorInfo();
387 trigger_error("<font color=red><b>QUERY FAILED ($error[0],$error[1]): </b>$error[2]<br /><br /><b>QUERY:</b>\n<pre>$sql</pre></font>");
388 if($fatal) die();
389 }
390 return $result;
391 }
392
393 function get_listing($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $indexed=array(), $suffix_id='_id') {
394 $sql = $this->build_query_select($class, $id, $limit, $offset, $where, $search, $history);
395 $result = $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC);
396 if(!$result || !is_array($indexed)) return $result;
397
398 foreach($result as $key => $row) $indexed[$row[$class.$suffix_id]]=$row;
399 return $indexed;
400 }
401
402 function get_columns($class) {
403 $class = $this->escape($class);
404 $sql = "SHOW COLUMNS FROM $class;";
405 return $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC);
406 }
407
408 function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
409 $selectbox=array();
410 foreach($columns as $column) {
411 if($column['Field'] == 'user_id') continue; //TODO HACK Blacklist: tabulka nemusi obsahovat *_name!!! momentalne se to tyka jen tabulky user (a item - u ty to nevadi)!
412 if($class && $column['Field'] == $class.$suffix_id) continue;
413 if(!preg_match('/'.$suffix_id.'$/', $column['Field'])) continue;
414 $table=preg_replace('/'.$suffix_id.'$/','',$column['Field']);
415
416 $history = $this->contains_history($table) ? " WHERE ${table}_valid_till=0" : '';
417 $sql = "SELECT $table$suffix_id, $table$suffix_name FROM $table$history;"; //TODO use build_query_select()!!!
418 $result = $this->safe_query($sql, false);
419 if(!$result) continue;
420 $result = $result->fetchAll(PDO::FETCH_ASSOC);
421 foreach($result as $row) $selectbox[$table.$suffix_id][$row[$table.$suffix_id]]=$row[$table.$suffix_name];
422 }
423 //echo('<pre>'); print_r($selectbox);
424 return array_filter($selectbox, 'ksort');
425 }
426
427 function map_unique($key, $value, $select, $table) { //TODO: Guess $select and $table if not passed
428 $history = $this->contains_history($table) ? " AND ${table}_valid_till=0" : '';
429 $value=$this->quote($value);
430 $sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!!
431 $result = $this->safe_query($sql)->fetchAll(PDO::FETCH_ASSOC);
432 if(isset($result[0][$select])) return $result[0][$select]; else die(trigger_error('Položka nenalezena!')); //TODO post_redirect_get...
433 }
434
435 function contains_history($table) {
436 $history_tables = array('item'); //TODO Autodetect
437 return in_array($table, $history_tables);
438 }
439
440 function build_query_insert($table, $values, $replace=true, $suffix_id='_id') {
441 //Init
442 $history = $this->contains_history($table);
443
444 //Escaping
445 $table = $this->escape($table);
446
447 //Get list of POSTed columns
448 $columns_array = array_map(array($this,'escape'), array_keys($values[0]));
449 $columns = implode(',',$columns_array);
450
451 //Build query
452 $sql = '';
453 //echo('<pre>'); die(print_r($values));
454
455 if($history) {
456 $history_update=false; foreach($values as $row) if(is_numeric($row[$table.'_id'])) $history_update=true;
457 if($history_update) {
458 $sql .= "UPDATE $table";
459 $sql .= " SET ${table}_valid_till=NOW()";
460 $sql .= " WHERE ${table}_valid_till=0 AND (";
461 $or = '';
462 foreach($values as $row) {
463 $sql .= $or.' '.$table.'_id='.$row[$table.'_id'];
464 $or = ' OR';
465 }
466 $sql .= " );\n\n";
467 $replace = false;
468 }
469 }
470
471 //Insert into table (columns)
472 $sql .= "INSERT INTO $table ($columns) VALUES ";
473
474 //Values (a,b,c),(d,e,f)
475 $comma='';
476 foreach($values as $row) {
477 $row_quoted = array_map(array($this,'quote'), $row); //Check
478 if($history) {
479 foreach($row as $column => $value) {
480 switch($column) {
481 case $table.'_valid_from':
482 $row_quoted[$column] = 'NOW()';
483 break;
484 case $table.'_valid_till':
485 $row_quoted[$column] = '0';
486 break;
487 case $table.'_author':
488 $row_quoted[$column] = $this->lms->get_authorized_user_id();
489 //die($this->lms->get_authorized_user_id().'=USER');
490 break;
491 }
492 }
493 }
494 $sql .= $comma.'('.implode(',',$row_quoted).')';
495 $comma = ',';
496 }
497
498 //On duplicate key
499 if($replace) {
500 foreach($columns_array as $col) {
501 if($col == $table.'_id' || $col == $table.'_valid_till') continue;
502 $on_duplicate[] = "$col=VALUES($col)";
503 }
504 $sql .= "\nON DUPLICATE KEY UPDATE ".implode(',', $on_duplicate);
505 }
506
507 //Terminate
508 $sql .= ';';
509 return $sql;
510 }
511
512 function insert_or_update($table, $values, $replace=true) {
513 $sql = $this->build_query_insert($table, $values, $replace);
514 $this->safe_query($sql);
515 return $this->lastInsertId();
516 }
517
518 function insert_or_update_multitab($values, $replace=true) {
519 $last=false;
520 foreach($values as $table => $rows) $last = $this->insert_or_update($table, $rows, $replace);
521 return $last;
522 }
523
524 function delete($table, $id, $suffix_id='_id') {
525 if($this->contains_history($table)) return false;
526 $key = $this->escape($table.$suffix_id);
527 $table = $this->escape($table);
528 $id = $this->quote($id);
529 return $this->safe_query("DELETE FROM $table WHERE $key = $id LIMIT 1;");
530 }
531}
532
533/**
534* Trida poskytuje high-level rozhrani k databazi skladu
535*
536* @package Sklad_DB_Abstract
537* @author Tomas Mudrunka
538*/
539class Sklad_DB_Abstract extends Sklad_DB {
540 //TODO Code
541}
542
543/**
544* Trida implementuje uzivatelske rozhrani skladu
545*
546* Example usage:
547* $sklad = new Sklad_UI();
548* $sklad->process_http_request();
549*
550* @package Sklad_UI
551* @author Tomas Mudrunka
552*/
553class Sklad_UI {
554 function __construct() {
555 $this->db = new Sklad_DB();
556 $this->html = new Sklad_HTML();
557 }
558
559 function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) {
560 return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false));
561 }
562
563 function render_form_add($class) {
564 $columns = $this->db->get_columns($class);
565 $selectbox = $this->db->columns_get_selectbox($columns, $class);
566 return $this->html->render_insert_form($class, $columns, $selectbox);
567 }
568
569 function render_form_edit($class, $id) {
570 $columns = $this->db->get_columns($class);
571 $selectbox = $this->db->columns_get_selectbox($columns, $class);
572 $current = $this->db->get_listing($class, $id, 1);
573 return $this->html->render_insert_form($class, $columns, $selectbox, $current);
574 }
575
576 function render_single_record_details($class, $id) {
577 $id_next = $id + 1;
578 $id_prev = $id - 1 > 0 ? $id - 1 : 0;
579 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
580 $html='';
581 $html.= $this->html->link('<<', "$class/$id_prev/");
582 $html.= '-';
583 $html.= $this->html->link('>>', "$class/$id_next/");
584 $html.= '<br />';
585 $html.= $this->html->link('edit', "$class/$id/edit/");
586 if($this->db->contains_history($class)) $html.= ' ][ '.$this->html->link('history', "$class/$id/history/");
587 return $html;
588 }
589
590 function render_listing_navigation($class, $id, $limit, $offset) {
591 $offset_next = $offset + $limit;
592 $offset_prev = $offset - $limit > 0 ? $offset - $limit : 0;
593 $get = $_SERVER['QUERY_STRING'] != '' ? '?'.$_SERVER['QUERY_STRING'] : '';
594 $html='';
595 $html.= $this->html->link('<<', "$class/$id/$limit/$offset_prev/$get");
596 $html.= '-';
597 $html.= $this->html->link('>>', "$class/$id/$limit/$offset_next/$get");
598 $html.= '<br />';
599 $html.= $this->html->link('new', "$class/new/$get");
600 return $html;
601 }
602
603 function render_listing_extensions($class, $id, $limit, $offset, $edit=false) {
604 $html='';
605 if(is_numeric($id)) {
606 $html.=$this->render_single_record_details($class, $id);
607 } else {
608 $html.=$this->render_listing_navigation($class, '*', $limit, $offset);
609 }
610 if($edit) {
611 $html.= $this->render_form_edit($class, $id);
612 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
613 $html.= "<form action='$action' method='POST'>";
614 $html.= $this->html->input(false, 'DELETE', 'submit');
615 $html.= 'sure?'.$this->html->input('sure', false, 'checkbox');
616 $html.= '</form>';
617 $action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
618 $html.= "<form action='$action' method='POST' enctype='multipart/form-data'>";
619 $html.= $this->html->input('image', false, 'file', false, 'size="30"');
620 $html.= $this->html->input(false, 'IMAGE', 'submit');
621 $html.='</form>';
622 }
623 return $html;
624 }
625
626 function check_auth() {
627 new HTTP_Auth('SkladovejSystem', true, array($this->db->lms,'check_auth'));
628 }
629
630 function post_redirect_get($location, $message='', $error=false) {
631 $url_args = $message != '' ? '?message='.urlencode($message) : '';
632 $location = $this->html->internal_url($location).$url_args;
633 header('Location: '.$location);
634 if($error) trigger_error($message);
635 $location=htmlspecialchars($location);
636 die(
637 "<meta http-equiv='refresh' content='0; url=$location'>".
638 "Location: <a href='$location'>$location</a>"
639 );
640 }
641
642 function safe_include($dir,$name,$vars=array(),$ext='.inc.php') {
643 if(preg_match('/[^a-zA-Z0-9-]/',$name)) $this->post_redirect_get('', 'SAFE INCLUDE: Securityfuck.', true);
644 $filename="$dir/$name$ext";
645 if(!is_file($filename)) $this->post_redirect_get('', 'SAFE INCLUDE: Fuckfound.', true);
646 foreach($vars as $var => $val) $$var=$val;
647 ob_start();
648 include($filename);
649 $out=ob_get_contents();
650 ob_end_clean();
651 return $out;
652 }
653
654 function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) {
655 if($_SERVER['REQUEST_METHOD'] != 'POST') return;
656 //echo('<pre>'); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
657
658 //SephirPOST:
659
660 /* Tenhle foreach() prekopiruje promenne
661 * z: $_POST['values'][$table][$column][$id];
662 * do: $values[$table][$id][$column]
663 */
664 if(isset($_POST['values'])) {
665 $values=array();
666 foreach($_POST['values'] as $table => $columns) {
667 foreach($columns as $column => $ids) {
668 foreach($ids as $id => $val) $values[$table][$id][$column] = $val;
669 }
670 }
671 //die(print_r($values));
672 }
673
674 if($action) switch($action) {
675 case 'new':
676 $replace = false;
677 case 'edit':
678 if(!isset($replace)) $replace = true;
679 $table = $class ? $class : 'item';
680 //print_r($values); //debug
681 $last = $this->db->insert_or_update_multitab($values, $replace);
682 $last = $force_redirect ? $force_redirect."?last=$last" : "$table/$last/";
683 $next = "$table/new/";
684 $message = $force_redirect ? '' : 'Hotovo. Další záznam přidáte '.$this->html->link('zde', $next).'.';
685 $this->post_redirect_get($last, $message);
686 break;
687 case 'delete':
688 if(!isset($_POST['sure']) || !$_POST['sure']) $this->post_redirect_get("$class/$id/edit", 'Sure user expected :-)');
689 $this->db->delete($class, $id) || $this->post_redirect_get("$class/$id/edit", "V tabulce $class jentak neco mazat nebudes chlapecku :-P");
690 $this->post_redirect_get("$class", "Neco (pravdepodobne /$class/$id) bylo asi smazano. Fnuk :'-(");
691 break;
692 case 'image':
693 $image_classes = array('model'); //TODO, use this more widely across the code
694 if(!in_array($class, $image_classes)) $this->post_redirect_get("$class/$id/edit", "Nekdo nechce k DB Tride '$class' prirazovat obrazky!");
695 $image_destination = DIR_IMAGES."/$class/$id.jpg";
696 if($_FILES['image']['name'] == '') $this->post_redirect_get("$class/$id/edit", 'Kazde neco se musi nejak jmenovat!', true);
697 if(move_uploaded_file($_FILES['image']['tmp_name'], $image_destination)) {
698 chmod ($image_destination, 0664);
699 $this->post_redirect_get("$class/$id", 'Obrazek se naladoval :)');
700 } else $this->post_redirect_get("$class/$id/edit", 'Soubor se nenahral :(', true);
701 break;
702 default:
703 trigger_error('Nothin\' to do here my cutie :-*');
704 break;
705 }
706
707 die('POSTed pyčo!');
708 }
709
710 function process_http_request() {
711 $this->check_auth();
712
713 @ini_set('magic_quotes_gpc' , 'off');
714 if(get_magic_quotes_gpc()) {
715 die(trigger_error("Error: magic_quotes_gpc needs to be disabled! F00K!"));
716 }
717
718 $PATH_INFO=@trim($_SERVER[PATH_INFO]);
719 if($_SERVER['REQUEST_METHOD'] != 'POST') echo $this->html->header($PATH_INFO); //TODO tahle podminka naznacuje ze je v navrhu nejaka drobna nedomyslenost...
720
721
722 //Sephirot:
723 $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
724 if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
725 switch($PATH_CHUNKS[1]) {
726 case 'test': //test
727 die('Tell me why you cry');
728 break;
729 case 'assistant': //assistant
730 $PATH_CHUNKS[3] = isset($PATH_CHUNKS[3]) ? trim($PATH_CHUNKS[3]) : false;
731 $assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3);
732 $assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2];
733 $assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL'];
734 echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2],$assistant_vars);
735 break;
736 default: //?
737 $search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false;
738 $class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item';
739 if(!isset($PATH_CHUNKS[2])) $PATH_CHUNKS[2]='';
740 switch($PATH_CHUNKS[2]) {
741 case 'new': //?/new
742 $this->process_http_request_post($PATH_CHUNKS[2], $class);
743 echo $this->render_form_add($class);
744 break;
745 default: //?/?
746 $id = (isset($PATH_CHUNKS[2]) && is_numeric($PATH_CHUNKS[2]) ? (int) $PATH_CHUNKS[2] : false);
747 if(!isset($PATH_CHUNKS[3])) $PATH_CHUNKS[3]='';
748 $edit=false;
749 switch($PATH_CHUNKS[3]) {
750 case 'edit': //?/?/edit
751 case 'image': //?/?/image
752 case 'delete': //?/?/delete
753 $this->process_http_request_post($PATH_CHUNKS[3], $class, $id);
754 $edit=true;
755 default: //?/?/?
756 $history = $PATH_CHUNKS[3] == 'history' ? true : false;
757 $limit = (int) (isset($PATH_CHUNKS[3]) ? $PATH_CHUNKS[3] : '0');
758 $offset = (int) (isset($PATH_CHUNKS[4]) ? $PATH_CHUNKS[4] : '0');
759 $where = false; //TODO get from URL
760 echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history);
761 echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
762 //print_r(array("<pre>",$_SERVER));
763 break;
764 }
765 break;
766 }
767 break;
768 }
769 }
770}
771
772$sklad = new Sklad_UI();
773$sklad->process_http_request();
774
775echo("<hr/>");
This page took 0.197737 seconds and 4 git commands to generate.