';
- //$html.=$this->input('table', $class, 'hidden');
+ function render_insert_inputs($class,$columns,$selectbox,$current,$hidecols,$update) {
+ $html = '';
foreach($columns as $column) {
$html.=T($class).':
: ';
$name="values[$class][".$column['Field'].'][]';
@@ -299,26 +389,71 @@ EOF;
}
$html.='
';
}
+ return $html;
+ }
- if($multi_insert) {
- //TODO, move to separate JS file
- $html.=<<
-
-
- +
-EOF;
+ function render_insert_form_multi($array) {
+ $html = '';
+ $head=false;
+
+ foreach($array as $key => $args) {
+ $parts=array('inputs');
+ if(!$head) { $head = true;
+ $parts[]='head';
+ }
+ if(!isset($array[$key+1])) {
+ $parts[]='foot';
+ $hr = '';
+ } else $hr = '
';
+ //$args[] = false;
+ $args[] = $parts;
+ $html .= call_user_func_array(array($this, 'render_insert_form'), $args);
+ $html .= $hr;
+ }
+ return $html;
+ }
+
+ function render_insert_form($class, $columns, $selectbox=array(), $current=false, $hidecols=false, $action=false, $multi_insert=true, $parts=false) {
+ $html = '';
+ //print_r($parts);
+ //echo(''); print_r($selectbox);
+ //echo(''); print_r($current);
+ $update = false;
+ if(is_array($current)) {
+ $update = true;
+ $current = array_shift($current);
}
- $btn = is_array($current) ? 'UPDATE' : 'INSERT'; //TODO: $current may be set even when inserting...
- $html.=$this->input(false, $btn, 'submit');
- $html.='';
+ if(!is_array($hidecols)) $hidecols = array();
+ $hidecols = array_merge($hidecols, array('item_author', 'item_valid_from', 'item_valid_till')); //TODO Autodetect
+
+ if(!is_array($parts) || in_array('head', $parts)) {
+ $action = $action ? " action='$action'" : false;
+ $html.="
';
+ }
return $html;
}
}
@@ -331,7 +466,7 @@ EOF;
*/
class Sklad_DB extends PDO {
function __construct() {
- $this->lms = new Sklad_LMS();
+ $this->auth = new Sklad_Auth();
parent::__construct(
DB_DSN, DB_USER, DB_PASS,
@@ -343,6 +478,10 @@ class Sklad_DB extends PDO {
return preg_replace('(^.|.$)', '', $this->quote($str)); //TODO HACK
}
+ function quote_identifier($str) {
+ return '`'.$this->escape($str).'`'; //TODO HACK
+ }
+
function build_query_select($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false, $order=false, $suffix_id='_id') {
//Configuration
$join = array(
@@ -353,13 +492,16 @@ class Sklad_DB extends PDO {
'item' => array('item_id','item_serial','model_name','model_barcode','model_descript','producer_name','vendor_name')
); //TODO Autodetect
+ //Init
+ if(is_array($where)) foreach($where as $key => $value) $where[$key] = $key.' '.$value; //TODO: escape SQLi!!!
+
//Escaping
$class = $this->escape($class);
//SELECT
- $sql="SELECT * FROM $class\n";
+ $sql="SELECT * FROM `$class`\n";
//JOIN
- if(isset($join[$class])) foreach($join[$class] as $j) $sql .= "LEFT JOIN $j USING($j$suffix_id)\n";
+ 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);
@@ -421,10 +563,14 @@ class Sklad_DB extends PDO {
return $indexed;
}
- function get_columns($class) {
+ function get_columns($class,$disable_cols=array()) { //TODO: Not sure if compatible with non-MySQL DBs
$class = $this->escape($class);
$sql = "SHOW COLUMNS FROM $class;";
- return $this->safe_query_fetch($sql);
+ $columns = $this->safe_query_fetch($sql);
+ /*foreach($columns as $colk => $col) foreach($col as $key => $val) {
+ if(in_array($col['Field'],$disable_cols)) $columns[$colk]['Extra']='auto_increment';
+ }*/
+ return $columns;
}
function columns_get_selectbox($columns, $class=false, $suffix_id='_id', $suffix_name='_name') {
@@ -445,12 +591,12 @@ class Sklad_DB extends PDO {
return array_filter($selectbox, 'ksort');
}
- function map_unique($key, $value, $select, $table) { //TODO: Guess $select and $table if not passed
+ function map_unique($key, $value, $select, $table, $fatal=true) { //TODO: Guess $select and $table if not passed
$history = $this->contains_history($table) ? " AND ${table}_valid_till=0" : '';
$value=$this->quote($value);
$sql = "SELECT $select FROM $table WHERE $key=$value$history LIMIT 1;"; //TODO use build_query_select()!!!
$result = $this->safe_query_fetch($sql);
- if(isset($result[0][$select])) return $result[0][$select]; else die(trigger_error(T('Record not found!'))); //TODO post_redirect_get...
+ if(isset($result[0][$select])) return $result[0][$select]; else if($fatal) die(trigger_error(T('Record not found!'))); //TODO post_redirect_get...
}
function contains_history($table) {
@@ -481,7 +627,7 @@ class Sklad_DB extends PDO {
$sql .= " WHERE ${table}_valid_till=0 AND (";
$or = '';
foreach($values as $row) {
- $sql .= $or.' '.$table.'_id='.$row[$table.'_id'];
+ $sql .= $or.' '.$table.'_id='.$this->quote($row[$table.'_id']);
$or = ' OR';
}
$sql .= " );\n\n";
@@ -506,8 +652,8 @@ class Sklad_DB extends PDO {
$row_quoted[$column] = '0';
break;
case $table.'_author':
- $row_quoted[$column] = $this->lms->get_authorized_user_id();
- //die($this->lms->get_authorized_user_id().'=USER');
+ $row_quoted[$column] = $this->auth->get_user_id();
+ //die($this->auth->get_user_id().'=USER');
break;
}
}
@@ -578,7 +724,7 @@ class Sklad_UI {
}
function render_items($class, $id=false, $limit=false, $offset=0, $where=false, $search=false, $history=false) {
- return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false));
+ return $this->html->render_item_table($this->db->get_listing($class, $id, $limit, $offset, $where, $search, $history, false),$class);
}
function render_form_add($class) {
@@ -603,6 +749,7 @@ class Sklad_UI {
$html.= '-';
$html.= $this->html->link('>>', "$class/$id_next/");
$html.= '
';
+ $html.=''.$this->html->render_barcode(BARCODE_PREFIX.strtoupper("$class/$id")).'';
$html.= $this->html->link('edit', "$class/$id/edit/");
if($this->db->contains_history($class)) $html.= ' ][ '.$this->html->link('history', "$class/$id/history/");
return $html;
@@ -631,32 +778,33 @@ class Sklad_UI {
if($edit) {
$html.= $this->render_form_edit($class, $id);
$action = $_SERVER['SCRIPT_NAME']."/$class/$id/delete";
- $html.= "';
+ $html.=$this->html->form($action,'POST',array(
+ array(false,'DELETE','submit'),
+ array('sure', false, 'checkbox', false, false, 'sure?')
+ ));
$action = $_SERVER['SCRIPT_NAME']."/$class/$id/image";
- $html.= "';
+ $html.=$this->html->form($action,'POST',array(
+ array('image', false, 'file', false, 'size="30"'),
+ array(false, 'IMAGE', 'submit')
+ ), "enctype='multipart/form-data'");
}
return $html;
}
function check_auth() {
- new HTTP_Auth('SkladovejSystem', true, array($this->db->lms,'check_auth'));
+ new HTTP_Auth('SkladovejSystem', true, array($this->db->auth,'check_auth'));
}
- function post_redirect_get($location, $message='', $error=false) {
- $url_args = $message != '' ? '?message='.urlencode(T($message)) : '';
+ function post_redirect_get($location, $message='', $error=false, $translate=true) {
+ $messaget = $translate ? T($message) : $message;
+ $url_args = $messaget != '' ? '?message='.urlencode($messaget) : '';
$location = $this->html->internal_url($location).$url_args;
header('Location: '.$location);
if($error) trigger_error($message);
$location=htmlspecialchars($location);
die(
"".
- T($message)."
Location: $location"
+ $messaget."
Location: $location"
);
}
@@ -672,6 +820,15 @@ class Sklad_UI {
return $out;
}
+ function check_input_validity($field, $value='', $ruleset=0) {
+ $rules = array(0 => array(
+ 'model_barcode' => '/./',
+ 'item_serial' => '/./'
+ ));
+ if(isset($rules[$ruleset][$field]) && !preg_match($rules[$ruleset][$field], trim($value))) return false;
+ return true;
+ }
+
function process_http_request_post($action=false, $class=false, $id=false, $force_redirect=false) {
if($_SERVER['REQUEST_METHOD'] != 'POST') return;
//echo(''); //DEBUG (maybe todo remove), HEADERS ALREADY SENT!!!!
@@ -686,7 +843,14 @@ class Sklad_UI {
$values=array();
foreach($_POST['values'] as $table => $columns) {
foreach($columns as $column => $ids) {
- foreach($ids as $id => $val) $values[$table][$id][$column] = $val;
+ foreach($ids as $id => $val) {
+ $values[$table][$id][$column] = trim($val);
+ if(!$this->check_input_validity($column,$val)) {
+ $message = "Spatny vstup: $column [$id] = \"$val\"; ". //XSS
+ $this->html->link('GO BACK', 'javascript:history.back()', false, false);
+ $this->post_redirect_get('', $message, false, false);
+ }
+ }
}
}
//die(print_r($values));
@@ -737,13 +901,12 @@ class Sklad_UI {
}
$PATH_INFO=@trim($_SERVER[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:
$PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
+ //Sephirot:
if(!isset($PATH_CHUNKS[1])) $PATH_CHUNKS[1]='';
- switch($PATH_CHUNKS[1]) {
+ if($_SERVER['REQUEST_METHOD'] != 'POST' && $PATH_CHUNKS[1]!='barcode') //TODO: tyhle podminky naznacujou, ze je v navrhu nejaka drobna nedomyslenost...
+ echo $this->html->header($PATH_INFO,$this->db->auth->get_user());
+ switch($PATH_CHUNKS[1]) { //TODO: Move some branches to plugins if possible
case 'test': //test
die('Tell me why you cry');
break;
@@ -752,8 +915,12 @@ class Sklad_UI {
$assistant_vars['SUBPATH'] = array_slice($PATH_CHUNKS, 3);
$assistant_vars['URL_INTERNAL'] = 'assistant/'.$PATH_CHUNKS[2];
$assistant_vars['URL'] = $_SERVER['SCRIPT_NAME'].'/'.$assistant_vars['URL_INTERNAL'];
+ $assistant_vars['ASSISTANT'] = $PATH_CHUNKS[2];
echo $this->safe_include(DIR_ASSISTANTS,$PATH_CHUNKS[2],$assistant_vars);
break;
+ case 'barcode': //barcode
+ Barcode::download_barcode(implode('/',array_slice($PATH_CHUNKS, 2)));
+ break;
default: //?
$search = (isset($_GET['q']) && trim($_GET['q']) != '') ? trim($_GET['q']) : false;
$class = (isset($PATH_CHUNKS[1]) && $PATH_CHUNKS[1] != '') ? $PATH_CHUNKS[1] : 'item';
@@ -777,7 +944,7 @@ class Sklad_UI {
$history = $PATH_CHUNKS[3] == 'history' ? true : false;
$limit = (int) (isset($PATH_CHUNKS[3]) ? $PATH_CHUNKS[3] : '0');
$offset = (int) (isset($PATH_CHUNKS[4]) ? $PATH_CHUNKS[4] : '0');
- $where = false; //TODO get from URL
+ $where = @is_array($_GET['where']) ? $_GET['where'] : false;
echo $this->render_items($class, $id, $limit, $offset, $where, $search, $history);
echo $this->render_listing_extensions($class, $id, $limit, $offset, $edit);
//print_r(array("",$_SERVER));
@@ -793,4 +960,4 @@ class Sklad_UI {
$sklad = new Sklad_UI();
$sklad->process_http_request();
-echo("
");
+echo('
');