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