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