/** * Short description of method * * @access public * @author Aurélien LEQUOY, <*****@*****.**> * @param string construct of controller * @return boolean Success * @access public */ final function __construct($controller, $action, $param) { $controller = Inflector::camelize($controller); if (AUTH_ACTIVE) { if (!IS_CLI) { if (!$GLOBALS['acl']->isAllowed($GLOBALS['auth']->getAccess(), $controller . "/" . $action)) { return; } } } $this->controller = $controller; $this->action = $action; $this->param = $param; $this->view = $action; $this->recursive = false; }
public function sql_save($data = null, $replace = false) { if ($replace) { $insert_or_replace = 'REPLACE'; } else { $insert_or_replace = 'INSERT'; } unset($this->error); $this->error = array(); if (count($this->_keys) === 0) { $this->unserializeKeys(); } $table = array_keys($data); $table = $table[0]; $keys = array_keys($data[$table]); $this->getInfosTable($table); $validation = new Validation($this); include_once APP_DIR . DS . "model" . DS . "Identifier" . ucwords(strtolower($this->_name)) . DS . $table . ".php"; $model_name = "Identifier" . Inflector::camelize($this->_name); $table2 = str_replace("-", "", $table); //$my_table = singleton::getInstance('glial\synapse\model\table\\'.$table2); $my_table = Singleton::getInstance('application\\model\\' . $model_name . '\\' . $table2); $validate = $my_table->validate; //debug($validate); foreach ($keys as $field) { if (!empty($validate[$field])) { foreach ($validate[$field] as $rule => $param) { if (!empty($rule)) { $elem['table'] = $table; $elem['field'] = $field; $elem['value'] = $data[$table][$field]; if (in_array("id", $keys, true)) { $elem['id'] = "AND id != " . $data[$table]['id']; } if (!empty($param[0])) { $msg_error = $param[0]; } else { $msg_error = NULL; } unset($param[0]); if (!empty($param)) { if (is_array($param)) { $nb_var = count($param); switch ($nb_var) { case 0: $return = $validation->{$rule}($elem); break; case 1: $return = $validation->{$rule}($elem, $param[1]); break; case 2: $return = $validation->{$rule}($elem, $param[1], $param[2]); break; case 3: $return = $validation->{$rule}($elem, $param[1], $param[2], $param[3]); break; } } else { $return = $validation->{$rule}($elem, $param); } } else { $return = $validation->{$rule}($elem); } if ($return === false) { //$this->error[$table][$field][] = __($param['message']); $this->error[$table][$field] = $msg_error; } } } } } unset($validation); $nb = count($keys); for ($i = 0; $i < $nb; $i++) { if (!in_array($keys[$i], $this->_table[$table]['field'])) { unset($data[$table][$keys[$i]]); unset($keys[$i]); } else { $data[$table][$keys[$i]] = $this->sql_real_escape_string($data[$table][$keys[$i]]); } } if (count($this->error) == 0) { if ($this->_history_active) { //traitement specifique if (strstr($this->_table_to_history, $table)) { if (in_array("id", $keys, true)) { $sql = "SELECT * FROM " . static::ESC . "" . $table . "" . static::ESC . " WHERE id ='" . $data[$table]['id'] . "'"; $res = $this->sql_query($sql); if ($this->sql_num_rows($res) === 1) { $before_update = $this->sql_to_array($res); //\history::insert($table, $data[$table]['id'], $param, $this->_history_type); } } } } if (in_array("id", $keys, true)) { $id = $data[$table]['id']; unset($data[$table]['id']); $str = array(); foreach ($keys as $key) { if ($key === 'id') { continue; } $str[] = "" . static::ESC . "" . $key . "" . static::ESC . " = '" . $data[$table][$key] . "'"; } $sql = "UPDATE " . static::ESC . "" . $table . "" . static::ESC . " SET " . implode(",", $str) . " WHERE id= " . $this->sql_real_escape_string($id) . ""; $this->sql_query($sql, $table, "UPDATE"); if ($this->query[$this->number_of_query - 1]['rows'] === 0) { $this->query[$this->number_of_query - 1]['last_id'] = $id; } // have to see if any problem in update /* if ($this->query[$this->number_of_query - 1]['rows'] == 0) { //$sql = "INSERT INTO ".static::ESC."".$table."".static::ESC." SET ".implode(",", $str).""; //$sql = "INSERT INTO ".static::ESC."".$table."".static::ESC." (".implode(",", $keys).") VALUES (".$this->sql_real_escape_string($id).",'".implode("','", $data[$table])."') --"; $sql = $insert_or_replace . " INTO " . static::ESC . "" . $table . "" . static::ESC . " SET id=" . $this->sql_real_escape_string($id) . " , " . implode(",", $str) . ""; //not supported by sybase A améliorer $this->sql_query($sql, $table, "INSERT"); }*/ } else { $sql = $insert_or_replace . " INTO " . static::ESC . "" . $table . "" . static::ESC . " (" . static::ESC . "" . implode("" . static::ESC . "," . static::ESC . "", $keys) . "" . static::ESC . ") VALUES ('" . implode("','", $data[$table]) . "') --"; //debug($sql); $this->sql_query($sql, $table, "INSERT"); } if (static::ESC === '`') { //case where ignore insert 0 line and we need the id inserted with these infos, focus on index unique $this->last_id = $this->query[$this->number_of_query - 1]['last_id']; if ($this->last_id == 0) { $sql = "SELECT id FROM " . static::ESC . "" . $table . "" . static::ESC . " WHERE 1=1 "; if (!empty($this->_keys[$table])) { foreach ($data[$table] as $key => $value) { //select only unique key if (in_array($key, $this->_keys[$table])) { $sql .= " AND " . static::ESC . "" . $key . "" . static::ESC . " = '" . $value . "' "; } } } //debug($sql); $res = $this->sql_query($sql, $table, "SELECT"); $tab = $this->sql_to_array($res); if (!empty($tab[0]['id'])) { $this->last_id = $tab[0]['id']; } else { $this->error[] = $sql; $this->error[] = "impossible to select the right row plz have a look on date('c')"; throw new \Exception('GLI-031 : Impossible to fine last id inserted in case of insert ignore'); } } } if ($this->_history_active) { //traitement specifique if (strstr($this->_table_to_history, $table)) { if (!empty($before_update)) { $param = \history::compare($before_update[0], $data[$table]); $id_table = $id; $type_query = 'UPDATE'; } else { $param = \history::compare(array(), $data[$table]); $id_table = $this->last_id; $type_query = 'INSERT'; } \history::insert($table, $id_table, $param, $this->_history_type, $this->_history_user, $type_query); $this->_history_type = HISTORY_TYPE; $this->_history_user = null; $this->last_id = $id_table; } } //return $this->query[$this->number_of_query-1]['last_id']; if (static::ESC === '"') { return true; } else { return $this->sql_insert_id(); } } else { return false; } }
function generate_model() { //php index.php administration generate_model $this->layout_name = false; $this->view = false; foreach ($this->di['db']->getAll() as $key) { $dbLink = $this->di['db']->sql($key); $tab_object = $dbLink->getListTable(); foreach ($tab_object['table'] as $table_name) { $table = $table_name; $model_name = "Identifier" . Inflector::camelize(str_replace('-', '_', $key)); $dir = APP_DIR . "/model/Identifier" . ucfirst(strtolower($key)); if (!is_dir($dir)) { mkdir($dir); } $file = $dir . "/" . strtolower($table) . ".php"; if (!file_exists($file)) { $fp = fopen($file, "w"); echo "model : " . $file . "\n"; $text = "<?php\n\nnamespace Application\\Model\\" . $model_name . ";\n"; $text .= "use \\Glial\\Synapse\\Model;\n"; $text .= "class " . $table . " extends Model\n{\nvar \$schema = \""; $create_table = $dbLink->getCreateTable($table); $des_table = $dbLink->getDescription($table); $i = 0; $data = array(); $field = array(); foreach ($des_table as $tab) { $field[] = "\"" . $tab[0] . "\""; $data[$table][$i]['field'] = $tab[0]; $data[$table][$i]['type'] = $tab[1]; $data[$table][$i]['length'] = $tab[2]; $i++; } $text .= $create_table; $text .= "\";\n\nvar \$field = array(" . implode(",", $field) . ");\n\nvar \$validate = array(\n"; foreach ($data[$table] as $field) { if ($field['field'] == "id") { continue; } if (mb_substr($field['field'], 0, 2) === "id") { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'reference_to' => array('The constraint to " . mb_substr($field['field'], 3) . ".id isn\\'t respected.','" . mb_substr($field['field'], 3) . "', 'id')\n\t),\n"; } elseif (mb_substr($field['field'], 0, 2) === "ip") { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'ip' => array('your IP is not valid')\n\t),\n"; } elseif ($field['field'] === "email") { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'email' => array('your email is not valid')\n\t),\n"; } else { if (mb_strstr($field['type'], "int")) { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'numeric' => array('This must be an int.')\n\t),\n"; } elseif (mb_stristr($field['type'], "datetime")) { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'dateTime' => array('This must be a datetime.')\n\t),\n"; } elseif (mb_stristr($field['type'], "time")) { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'time' => array('This must be a time.')\n\t),\n"; } elseif (mb_stristr($field['type'], "date")) { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'date' => array('This must be a date.')\n\t),\n"; } elseif (mb_stristr($field['type'], "float")) { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'decimal' => array('This must be a float.')\n\t),\n"; } elseif (mb_stristr($field['type'], "VARCHAR2")) { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'maxLength' => array('You execed the max length (" . $field['length'] . " chars)', " . $field['length'] . ")\n\t),\n"; } elseif (mb_stristr($field['type'], "NUMBER")) { $text .= "\t'" . $field['field'] . "' => array(\n\t\t'numeric' => array('This must be an int.')\n\t),\n"; } else { //$text .= "\t'" . $field['field'] . "' => array(\n\t\t'not_empty' => array('This field is requiered.')\n\t),\n"; } } } $text .= ");\n\nfunction get_validate()\n{\nreturn \$this->validate;\n}\n}\n"; fwrite($fp, $text); fclose($fp); unset($data); } } } }
} define('LINK', WWW_ROOT . "en" . "/"); } else { //mode with apache define('LINK', WWW_ROOT . I18n::Get() . "/"); if (AUTH_ACTIVE) { $auth = new Auth(); $auth->setInstance($_DB->sql(DB_DEFAULT), "user_main", array("login", "password")); $auth->setFctToHashCookie(function ($password) { return password_hash($password . $_SERVER['HTTP_USER_AGENT'] . $_SERVER['REMOTE_ADDR'], PASSWORD_DEFAULT); }); $auth->authenticate(false); FactoryController::addDi("auth", $auth); } ENVIRONEMENT ? $_DEBUG->save("User connexion") : ""; $_SYSTEM['controller'] = \Glial\Utility\Inflector::camelize($url['controller']); $_SYSTEM['action'] = $url['action']; $_SYSTEM['param'] = $url['param']; $acl = new Acl(CONFIG . "acl.config.ini"); FactoryController::addDi("acl", $acl); $js = new Javascript(); FactoryController::addDi("js", $js); if ($acl->checkIfResourceExist($_SYSTEM['controller'] . "/" . $_SYSTEM['action'])) { if (AUTH_ACTIVE) { if (!$acl->isAllowed($auth->getAccess(), $_SYSTEM['controller'] . "/" . $_SYSTEM['action'])) { if ($auth->getAccess() == 1) { $url = ROUTE_LOGIN; $msg = $_SYSTEM['controller'] . "/" . $_SYSTEM['action'] . "<br />" . __("You have to be registered to acces to this page"); } else { //die("here"); $url = ROUTE_DEFAULT;