From 441212b3fdf07852be95c4a59315aa39a7264249 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 3 Nov 2013 01:39:13 +0100 Subject: [PATCH] mecanisme d'upgrade --- mw/app/controllers/install/index.php | 4 + mw/app/controllers/upgrade/index.php | 45 ++++ mw/app/controllers/upgrade/user.php | 33 +++ .../data/modules/share/mw_data_users_sessions.php | 18 +- mw/app/data/mw_data.php | 11 - mw/app/data/mw_sgbd.php | 152 ------------ .../data/upgrades/xml/mtweb_xml_version_0_11_2.php | 273 +++++++++++++++++++++ mw/app/init/0300_data.php | 4 - mw/app/out/default/js/actions/upgrade.js | 5 + mw/app/out/default/layouts/upgrade.xml | 8 + mw/app/out/default/upgrade.php | 36 +++ mw/app/out/default/views/upgrade/colonne.php | 0 mw/app/out/default/views/upgrade/footer.php | 3 + mw/app/out/default/views/upgrade/head.php | 43 ++++ mw/app/out/default/views/upgrade/header.php | 1 + mw/app/out/default/views/upgrade/index.php | 64 +++++ mw/env/modules/mw_env_data.php | 157 ++++++++++++ mw/env/modules/mw_env_data_upgrade.php | 162 ++++++++++++ mw/env/modules/mw_env_webmaster.php | 38 +++ mw/mw_app.php | 60 +++-- 20 files changed, 921 insertions(+), 196 deletions(-) create mode 100644 mw/app/controllers/upgrade/index.php create mode 100644 mw/app/controllers/upgrade/user.php delete mode 100644 mw/app/data/mw_data.php delete mode 100644 mw/app/data/mw_sgbd.php create mode 100644 mw/app/data/upgrades/xml/mtweb_xml_version_0_11_2.php create mode 100644 mw/app/out/default/js/actions/upgrade.js create mode 100644 mw/app/out/default/layouts/upgrade.xml create mode 100644 mw/app/out/default/upgrade.php create mode 100644 mw/app/out/default/views/upgrade/colonne.php create mode 100644 mw/app/out/default/views/upgrade/footer.php create mode 100644 mw/app/out/default/views/upgrade/head.php create mode 100644 mw/app/out/default/views/upgrade/header.php create mode 100644 mw/app/out/default/views/upgrade/index.php create mode 100644 mw/env/modules/mw_env_data_upgrade.php create mode 100644 mw/env/modules/mw_env_webmaster.php diff --git a/mw/app/controllers/install/index.php b/mw/app/controllers/install/index.php index 8cc63f0..c47b231 100644 --- a/mw/app/controllers/install/index.php +++ b/mw/app/controllers/install/index.php @@ -128,6 +128,10 @@ $env->erreur("la base de données a été installée mais impossible d'ajouter l'administrateur"); return; } + if(!$env->set_webmaster_user($admin["login"], md5($admin["password"]))){ + $env->erreur("la base de données a été installée mais impossible d'enregistrer le webmaster"); + return; + } } if(!$env->messages()){ if(!$content = @file_get_contents($template_file)){ diff --git a/mw/app/controllers/upgrade/index.php b/mw/app/controllers/upgrade/index.php new file mode 100644 index 0000000..ecbb576 --- /dev/null +++ b/mw/app/controllers/upgrade/index.php @@ -0,0 +1,45 @@ +env(); + $env->load_webmaster_session(); + return true; + } + + function index(){ + $env = $this->env(); + $data = $env->data(); + $env->set_out("data_version", $data->version("mtweb")); + $env->set_out("env_version", $env->version("mtweb")); + } + + function confirm_upgrade(){ + $env = $this->env(); + if(!($user = $env->user())){ + $env->redirect($env->url("upgrade"), "", 0); + return; + } + if(!($webmaster_user = $env->get_webmaster_user())){ + $env->erreur("impossible de lire le user webmaster"); + return; + } + if( + ($user["login"] != $webmaster_user["login"]) + || ($user["password"] != $webmaster_user["password"]) + ){ + $env->erreur("identification incorrecte"); + return; + } + if(($res = $env->do_data_upgrade()) !== true){ + $env->erreur("Erreur durant l'upgrade: ".$res); + return; + } + $env->redirect( + $env->url(), + "La base a été mise à jour" + ); + } + + } diff --git a/mw/app/controllers/upgrade/user.php b/mw/app/controllers/upgrade/user.php new file mode 100644 index 0000000..68dce71 --- /dev/null +++ b/mw/app/controllers/upgrade/user.php @@ -0,0 +1,33 @@ +env(); + $env->load_webmaster_session(); + return true; + } + + function login(){ + $env = $this->env(); + if($_POST){ + $data = $env->data(); + $message = "Identifiants incorrects"; + if($user = $env->webmaster_login(trim($_POST['login']), trim($_POST['pass']))){ + $message = "Vous êtes maintenant identifié en tant que ".$user['login']; + } + $env->redirect( + $env->url("upgrade"), + $message + ); + } + } + + function logout(){ + $env = $this->env(); + $data = $env->data(); + $data->logout(); + $env->redirect($env->url("upgrade"), "", 0); + } + + } diff --git a/mw/app/data/modules/share/mw_data_users_sessions.php b/mw/app/data/modules/share/mw_data_users_sessions.php index 9a787a0..df81d46 100644 --- a/mw/app/data/modules/share/mw_data_users_sessions.php +++ b/mw/app/data/modules/share/mw_data_users_sessions.php @@ -4,8 +4,12 @@ var $user; - function login($login, $password){ - if(($user = $this->user($login)) !== false){ + function login($login, $password, $reference_user = null){ + if(($user = isset($reference_user) ? $reference_user : $this->user($login)) !== false){ + if(isset($reference_user) && $login != $reference_user["login"]){ + $this->clear_session(); + return array(); + } if($this->password_ok($user, $password)){ if(!$this->set_session($user)) $user = false; } @@ -32,11 +36,13 @@ function password_ok($user, $password){ if(!$user) return false; - return + $OK = (isset($_SESSION[$this->app_session_key()]["id"])) && (isset($_SESSION[$this->app_session_key()]["ip"])) && (strcmp(md5($user["password"].$_SESSION[$this->app_session_key()]["id"]), $password) == 0) - && ($_SESSION[$this->app_session_key()]["ip"] == $_SERVER["REMOTE_ADDR"]); + && ($_SESSION[$this->app_session_key()]["ip"] == $_SERVER["REMOTE_ADDR"]) + ; + return $OK; } # ---------------------------------------------------------------------------------------- @@ -48,12 +54,12 @@ return "mw_".str_replace("/", "_", $env->path("web")); } - function load_session(){ + function load_session($reference_user = null){ @session_start(); if(!isset($_SESSION[$this->app_session_key()]["id"])) $this->clear_session(); $user = array(); if(isset($_SESSION[$this->app_session_key()]["user"])){ - $user = $this->user($_SESSION[$this->app_session_key()]["user"]); + $user = isset($reference_user) ? $reference_user : $this->user($_SESSION[$this->app_session_key()]["user"]); } elseif(isset($_COOKIE[$this->app_session_key()."_user"]) && isset($_COOKIE[$this->app_session_key()."_pass"])){ if($user = $this->user($_COOKIE[$this->app_session_key()."_user"])){ diff --git a/mw/app/data/mw_data.php b/mw/app/data/mw_data.php deleted file mode 100644 index 233fe50..0000000 --- a/mw/app/data/mw_data.php +++ /dev/null @@ -1,11 +0,0 @@ -call_data_auto_crud($method_name, $arguments); - } - - } - -?> \ No newline at end of file diff --git a/mw/app/data/mw_sgbd.php b/mw/app/data/mw_sgbd.php deleted file mode 100644 index c3ca66e..0000000 --- a/mw/app/data/mw_sgbd.php +++ /dev/null @@ -1,152 +0,0 @@ -sgbd_impl = $sgbd_impl; - $this->env = $env; - } - - function extention_ok(){ - return $this->sgbd_impl->extention_ok($this->env); - } - - function authentication_required(){ - return $this->sgbd_impl->authentication_required(); - } - - function connect($host, $base, $user, $password){ - return $this->sgbd_impl->connect($host, $base, $user, $password); - } - - function select_db($db_name){ - return $this->sgbd_impl->select_db($db_name); - } - - # --------------------------------------------------------------------------------- - # SQL - # - - function desc_table($table_name){ - if(!method_exists($this->sgbd_impl, "desc_table")) return false; - return $this->sgbd_impl->desc_table( - ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? - str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $table_name) - : $table_name - ); - } - - function table_exists($table_name){ - if(!method_exists($this->sgbd_impl, "table_exists")) return false; - return $this->sgbd_impl->table_exists( - ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? - str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $table_name) - : $table_name - ); - } - - function field_exists($table_name, $field_name){ - if(!method_exists($this->sgbd_impl, "field_exists")) return false; - return $this->sgbd_impl->field_exists( - ( - $prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? - str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $table_name) - : $table_name, - $field_name - ); - } - - function query($sql){ - if(!method_exists($this->sgbd_impl, "query")) return false; - return $this->sgbd_impl->query( - ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? - str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $sql) - : $sql - ); - } - - function insert_id(){ - if(!method_exists($this->sgbd_impl, "insert_id")) return false; - return $this->sgbd_impl->insert_id(); - } - - function fetch_assoc($rst){ - if(!method_exists($this->sgbd_impl, "fetch_assoc")) return false; - return $this->sgbd_impl->fetch_assoc($rst); - } - - function free_result($rst){ - if(!method_exists($this->sgbd_impl, "")) return false; - return $this->sgbd_impl->free_result($rst); - } - - function close(){ - if(!method_exists($this->sgbd_impl, "")) return false; - return $this->sgbd_impl->close(); - } - - # --------------------------------------------------------------------------------- - # XML - # - - function data_exists($data_path){ - if(!method_exists($this->sgbd_impl, "data_exists")) return false; - return $this->sgbd_impl->data_exists($data_path); - } - - function create_data($data_path){ - if(!method_exists($this->sgbd_impl, "create_data")) return false; - return $this->sgbd_impl->create_data($data_path); - } - - function get_data($data_path, $data_id){ - if(!method_exists($this->sgbd_impl, "get_data")) return false; - return $this->sgbd_impl->get_data($data_path, $data_id); - } - - function open_data($data_path, $FETCH = true){ - if(!method_exists($this->sgbd_impl, "open_data")) return false; - return $this->sgbd_impl->open_data($data_path, $FETCH); - } - - function fetch_data($dh){ - if(!method_exists($this->sgbd_impl, "fetch_data")) return false; - return $this->sgbd_impl->fetch_data($dh); - } - - function add_data($data_path, $data, $index = null){ - if(!method_exists($this->sgbd_impl, "add_data")) return false; - return $this->sgbd_impl->add_data($data_path, $data, $index); - } - - function last_index($dh){ - if(!method_exists($this->sgbd_impl, "last_index")) return false; - return $this->sgbd_impl->last_index($dh); - } - - function set_data($data_path, $data_id, $data){ - if(!method_exists($this->sgbd_impl, "set_data")) return false; - return $this->sgbd_impl->set_data($data_path, $data_id, $data); - } - - function del_data($data_path, $data_id){ - if(!method_exists($this->sgbd_impl, "del_data")) return false; - return $this->sgbd_impl->del_data($data_path, $data_id); - } - - function close_data($dh){ - if(!method_exists($this->sgbd_impl, "close_data")) return false; - return $this->sgbd_impl->close_data($dh); - } - - function remove_data($data_path){ - if(!method_exists($this->sgbd_impl, "remove_data")) return false; - return $this->sgbd_impl->remove_data($data_path); - } - - } - -?> \ No newline at end of file diff --git a/mw/app/data/upgrades/xml/mtweb_xml_version_0_11_2.php b/mw/app/data/upgrades/xml/mtweb_xml_version_0_11_2.php new file mode 100644 index 0000000..9bac3d0 --- /dev/null +++ b/mw/app/data/upgrades/xml/mtweb_xml_version_0_11_2.php @@ -0,0 +1,273 @@ +env(); + $data = $env->data(); + $sgbd = $data->sgbd(); + + /* ---------------------------------------------------------------------- */ + /* action_status */ + /* devient actions_roles */ + /* element id_status devient id_role */ + + if(!$sgbd->data_exists("actions_roles")){ + if(!$sgbd->create_data("actions_roles")){ + return "impossible de créer la table actions_roles"; + } + } + if($sgbd->data_exists("action_status")){ + $action_status = array(); + if($rst = $sgbd->open_data("action_status")){ + while($v_rst = $sgbd->fetch_data($rst)){ + if(isset($v_rst)){ + $action_status[$v_rst["id"]] = $v_rst; + } + else{ + $action_status = false; + break; + } + } + $sgbd->close_data($rst); + } + else{ + return "impossible d'ouvrir la table action_status, dans ".get_class($this); + } + if($action_status === false){ + return "impossible de lire la table action_status, dans ".get_class($this); + } + if($action_status){ + foreach($action_status as $status){ + if( + !$sgbd->add_data( + "actions_roles", + array( + "action" => $status["action"], + "id_role" => $status["id_status"] + ) + ) + ){ + return "erreur lors de la migration de action_status vers actions_roles"; + } + } + } + if(!$sgbd->remove_data("action_status")){ + return "impossible de supprimer la table action_status"; + } + } + + + /* ---------------------------------------------------------------------- */ + /* config */ + /* elements key et value */ + /* */ + + $OK = true; + if($rst = $sgbd->open_data("config")){ + while($v_rst = $sgbd->fetch_data($rst)){ + if(isset($v_rst)){ + if(!isset($v_rst["key"]) || !isset($v_rst["value"])){ + $config_key = ""; + $config_value = ""; + foreach($v_rst as $key => $value){ + if($key != "id"){ + $config_key = $key; + $config_value = $value; + break; + } + } + if($config_key){ + if( + !$sgbd->set_data( + "config", + $v_rst["id"], + array( + "key" => $config_key, + "value" => $config_value + ) + ) + ){ + $OK = false; + break; + } + } + } + } + else{ + $OK = false; + break; + } + } + $sgbd->close_data($rst); + } + else{ + return "impossible d'ouvrir la table config, dans ".get_class($this); + } + if(!$OK){ + return "impossible de mettre à jour la table config, dans ".get_class($this); + } + + /* ---------------------------------------------------------------------- */ + /* user_status */ + /* devient roles */ + /* */ + if(!$sgbd->data_exists("roles")){ + if(!$sgbd->create_data("roles")){ + return "impossible de creer la table roles, dans ".get_class($this); + } + + $ERROR = false; + if(!$ERROR){ + $res = $sgbd->add_data( + "roles", + array( + "nom" => "guest", + "intitule" => "invité" + ), + 0 + ); + if(!isset($res)) $ERROR = true; + } + if(!$ERROR) if( + !$sgbd->add_data( + "roles", + array( + "nom" => "admin", + "intitule" => "administrateur" + ) + ) + ) $ERROR = true; + if(!$ERROR) if( + !$sgbd->add_data( + "roles", + array( + "nom" => "membre", + "intitule" => "membre" + ) + ) + ) $ERROR = true; + if(!$ERROR) if( + !$sgbd->add_data( + "roles", + array( + "nom" => "webmaster", + "intitule" => "webmaster" + ) + ) + ) $ERROR = true; + + if($ERROR){ + return "impossible de remplir la table roles, dans ".get_class($this); + } + + } + if($sgbd->data_exists("user_status")){ + if(!$sgbd->remove_data("user_status")){ + return "impossible de supprimer la table user_status"; + } + } + + /* ---------------------------------------------------------------------- */ + /* nouvelle table users_roles */ + /* importer users.status dans users_roles.id_role */ + + if(!$sgbd->data_exists("users_roles")){ + if(!$sgbd->create_data("users_roles")){ + return "impossible de creer la table users_roles, dans ".get_class($this); + } + $OK = true; + if($rst = $sgbd->open_data("users")){ + while($v_rst = $sgbd->fetch_data($rst)){ + if(isset($v_rst)){ + if(isset($v_rst["status"])){ + if( + !$sgbd->add_data( + "users_roles", + array( + "id_user" => $v_rst["id"], + "id_role" => $v_rst["status"] + ) + ) + ){ + $OK = false; + break; + } + } + } + else{ + $OK = false; + break; + } + } + $sgbd->close_data($rst); + } + else{ + return "impossible d'ouvrir la table users, dans ".get_class($this); + } + if(!$OK){ + return "impossible de remplir la table users_roles, dans ".get_class($this); + } + } + + /* ---------------------------------------------------------------------- */ + /* nouvelle table versions */ + /* */ + + if(!$sgbd->data_exists("versions")){ + if(!$sgbd->create_data("versions")){ + return "impossible de creer la table versions, dans ".get_class($this); + } + } + + /* ---------------------------------------------------------------------- */ + /* mise a jour de la version des donnees */ + /* */ + + if(!($env_version = $env->version("mtweb"))){ + return "impossible de lire la version du code, dans ".get_class($this); + } + if( + $data_version = $data->data_read( + array( + "table_name" => "versions", + "index_name" => "application", + "index_value" => "mtweb" + ) + ) + ){ + if( + !$sgbd->set_data( + "versions", + $data_version["id"], + array( + "version" => $this->version() + ) + ) + ){ + return "impossible de mettre à jour la version, dans ".get_class($this); + } + } + else{ + if( + !$sgbd->add_data( + "versions", + array( + "application" => "mtweb", + "version" => $this->version() + ) + ) + ){ + return "impossible d'ajouter la version, dans ".get_class($this); + } + } + + return true; + } + + } diff --git a/mw/app/init/0300_data.php b/mw/app/init/0300_data.php index 9d5e2f9..fd785f7 100644 --- a/mw/app/init/0300_data.php +++ b/mw/app/init/0300_data.php @@ -1,7 +1,5 @@ app_file("data/mw_sgbd.php"); - if(!class_exists("mw_data")) require_once $this->app_file("data/mw_data.php"); if(($plugins = $this->plugins("DESC")) === false){ $this->erreur("Impossible de lire les plugins pour charger les modules de donnees", true); } @@ -45,5 +43,3 @@ } $data->set_env($this); $this->set_data($data); - -?> \ No newline at end of file diff --git a/mw/app/out/default/js/actions/upgrade.js b/mw/app/out/default/js/actions/upgrade.js new file mode 100644 index 0000000..ca05ce5 --- /dev/null +++ b/mw/app/out/default/js/actions/upgrade.js @@ -0,0 +1,5 @@ +function prepare_password(session_id){ + document.forms["login_form"].pass.value = MD5(MD5(document.forms["login_form"].password.value) + session_id); + document.forms['login_form'].password.value = ""; + return true; +} diff --git a/mw/app/out/default/layouts/upgrade.xml b/mw/app/out/default/layouts/upgrade.xml new file mode 100644 index 0000000..a41df3e --- /dev/null +++ b/mw/app/out/default/layouts/upgrade.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/mw/app/out/default/upgrade.php b/mw/app/out/default/upgrade.php new file mode 100644 index 0000000..5c90c3b --- /dev/null +++ b/mw/app/out/default/upgrade.php @@ -0,0 +1,36 @@ + + + +out_file("views/upgrade/head.php"); ?> + + + +
+
+out_file("views/upgrade/header.php"); ?> +
+
+ +
+
+ +
+out_file("views/upgrade/colonne.php"); ?> +
+ +
+out_file("views/messages.php"); ?> +out_file_exists($layout["content"])) require $this->out_file($layout["content"]); ?> +
+ +
+
+ + + + + diff --git a/mw/app/out/default/views/upgrade/colonne.php b/mw/app/out/default/views/upgrade/colonne.php new file mode 100644 index 0000000..e69de29 diff --git a/mw/app/out/default/views/upgrade/footer.php b/mw/app/out/default/views/upgrade/footer.php new file mode 100644 index 0000000..f1fb6f3 --- /dev/null +++ b/mw/app/out/default/views/upgrade/footer.php @@ -0,0 +1,3 @@ + diff --git a/mw/app/out/default/views/upgrade/head.php b/mw/app/out/default/views/upgrade/head.php new file mode 100644 index 0000000..0f9cce0 --- /dev/null +++ b/mw/app/out/default/views/upgrade/head.php @@ -0,0 +1,43 @@ + + + Mtweb - upgrade + + + + + + js_files() as $js_file) : ?> + + + + css_files() as $css_file) : ?> + + + + out_file_exists("js/script.js")) : ?> + + + out_file_exists("js/actions/".$this->etat("mod").".js")) : ?> + + + out_file_exists("js/actions/".$this->etat("mod")."_".$this->etat("controller").".js")) : ?> + + + out_file_exists("js/actions/".$this->etat("mod")."_".$this->etat("controller")."_".$this->etat("action").".js")) : ?> + + + + out_file_exists("css/style.css")) : ?> + " /> + + out_file_exists("css/actions/".$this->etat("mod").".css")) : ?> + etat("mod").".css"); ?>" /> + + out_file_exists("css/actions/".$this->etat("mod")."_".$this->etat("controller").".css")) : ?> + etat("mod")."_".$this->etat("controller").".css"); ?>" /> + + out_file_exists("css/actions/".$this->etat("mod")."_".$this->etat("controller")."_".$this->etat("action").".css")) : ?> + etat("mod")."_".$this->etat("controller")."_".$this->etat("action").".css"); ?>" /> + \ No newline at end of file diff --git a/mw/app/out/default/views/upgrade/header.php b/mw/app/out/default/views/upgrade/header.php new file mode 100644 index 0000000..c3d5e22 --- /dev/null +++ b/mw/app/out/default/views/upgrade/header.php @@ -0,0 +1 @@ +

Mtweb - upgrade

\ No newline at end of file diff --git a/mw/app/out/default/views/upgrade/index.php b/mw/app/out/default/views/upgrade/index.php new file mode 100644 index 0000000..46137c1 --- /dev/null +++ b/mw/app/out/default/views/upgrade/index.php @@ -0,0 +1,64 @@ +

Mise à jour des données

+ +

+ La base de données doit être mise à jour pour + mtweb.out["env_version"]; ?>. +

+

+ +user())) : + $data = $this->data(); + $app_session_key = $data->app_session_key(); + +?> + +
" + method="post"> +
+ Identification +

+ Identifiez-vous avec l'utilisateur que vous avez créé pendant l'installation du site. +

+ +
    +
  • + +
    + +
    +
  • +
  • + +
    + +
    +
  • +
  • +
    + ')" /> +
    +
  • +
+
+
+ + + +

+ Cette opération est risquée. +

+
+
faites une sauvegarde de vos données
+avant de lancer la mise à jour de la base
+
+
+ + + diff --git a/mw/env/modules/mw_env_data.php b/mw/env/modules/mw_env_data.php index fb1a5bd..e6cb000 100644 --- a/mw/env/modules/mw_env_data.php +++ b/mw/env/modules/mw_env_data.php @@ -14,4 +14,161 @@ } + class mw_data extends empty_class{ + + function call_default($inst, $method_name, $arguments){ + return $this->call_data_auto_crud($method_name, $arguments); + } + + } + + class mw_sgbd{ + + var $sgbd_impl; + var $env; + + function mw_sgbd($sgbd_impl, $env){ + $this->sgbd_impl = $sgbd_impl; + $this->env = $env; + } + + function extention_ok(){ + return $this->sgbd_impl->extention_ok($this->env); + } + + function authentication_required(){ + return $this->sgbd_impl->authentication_required(); + } + + function connect($host, $base, $user, $password){ + return $this->sgbd_impl->connect($host, $base, $user, $password); + } + + function select_db($db_name){ + return $this->sgbd_impl->select_db($db_name); + } + + # --------------------------------------------------------------------------------- + # SQL + # + + function desc_table($table_name){ + if(!method_exists($this->sgbd_impl, "desc_table")) return false; + return $this->sgbd_impl->desc_table( + ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? + str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $table_name) + : $table_name + ); + } + + function table_exists($table_name){ + if(!method_exists($this->sgbd_impl, "table_exists")) return false; + return $this->sgbd_impl->table_exists( + ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? + str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $table_name) + : $table_name + ); + } + + function field_exists($table_name, $field_name){ + if(!method_exists($this->sgbd_impl, "field_exists")) return false; + return $this->sgbd_impl->field_exists( + ( + $prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? + str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $table_name) + : $table_name, + $field_name + ); + } + + function query($sql){ + if(!method_exists($this->sgbd_impl, "query")) return false; + return $this->sgbd_impl->query( + ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ? + str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $sql) + : $sql + ); + } + + function insert_id(){ + if(!method_exists($this->sgbd_impl, "insert_id")) return false; + return $this->sgbd_impl->insert_id(); + } + + function fetch_assoc($rst){ + if(!method_exists($this->sgbd_impl, "fetch_assoc")) return false; + return $this->sgbd_impl->fetch_assoc($rst); + } + + function free_result($rst){ + if(!method_exists($this->sgbd_impl, "")) return false; + return $this->sgbd_impl->free_result($rst); + } + + function close(){ + if(!method_exists($this->sgbd_impl, "")) return false; + return $this->sgbd_impl->close(); + } + + # --------------------------------------------------------------------------------- + # XML + # + + function data_exists($data_path){ + if(!method_exists($this->sgbd_impl, "data_exists")) return false; + return $this->sgbd_impl->data_exists($data_path); + } + + function create_data($data_path){ + if(!method_exists($this->sgbd_impl, "create_data")) return false; + return $this->sgbd_impl->create_data($data_path); + } + + function get_data($data_path, $data_id){ + if(!method_exists($this->sgbd_impl, "get_data")) return false; + return $this->sgbd_impl->get_data($data_path, $data_id); + } + + function open_data($data_path, $FETCH = true){ + if(!method_exists($this->sgbd_impl, "open_data")) return false; + return $this->sgbd_impl->open_data($data_path, $FETCH); + } + + function fetch_data($dh){ + if(!method_exists($this->sgbd_impl, "fetch_data")) return false; + return $this->sgbd_impl->fetch_data($dh); + } + + function add_data($data_path, $data, $index = null){ + if(!method_exists($this->sgbd_impl, "add_data")) return false; + return $this->sgbd_impl->add_data($data_path, $data, $index); + } + + function last_index($dh){ + if(!method_exists($this->sgbd_impl, "last_index")) return false; + return $this->sgbd_impl->last_index($dh); + } + + function set_data($data_path, $data_id, $data){ + if(!method_exists($this->sgbd_impl, "set_data")) return false; + return $this->sgbd_impl->set_data($data_path, $data_id, $data); + } + + function del_data($data_path, $data_id){ + if(!method_exists($this->sgbd_impl, "del_data")) return false; + return $this->sgbd_impl->del_data($data_path, $data_id); + } + + function close_data($dh){ + if(!method_exists($this->sgbd_impl, "close_data")) return false; + return $this->sgbd_impl->close_data($dh); + } + + function remove_data($data_path){ + if(!method_exists($this->sgbd_impl, "remove_data")) return false; + return $this->sgbd_impl->remove_data($data_path); + } + + } + ?> \ No newline at end of file diff --git a/mw/env/modules/mw_env_data_upgrade.php b/mw/env/modules/mw_env_data_upgrade.php new file mode 100644 index 0000000..8a3f0c1 --- /dev/null +++ b/mw/env/modules/mw_env_data_upgrade.php @@ -0,0 +1,162 @@ +bdd("sgbd") == "xml" ? "xml" : "sql")."/"; + $versions_dir = $this->path("mw_dir")."app/data/upgrades/".$impl_dir; + if(!is_dir($versions_dir)){ + return false; + } + if(!$data_upgrade->load_versions("mtweb", $versions_dir)){ + return false; + } + if(!isset($this->data_upgrades)) $this->data_upgrades = array(); + $this->data_upgrades[] = $data_upgrade; + return true; + } + + function data_upgrade_required(){ + if(!isset($this->data_upgrades)) return false; + foreach($this->data_upgrades as $data_upgrade){ + if($data_upgrade->upgrade_required()){ + return true; + } + } + return false; + } + + function do_data_upgrade(){ + if(!isset($this->data_upgrades)) return false; + foreach($this->data_upgrades as $data_upgrade){ + if($data_upgrade->upgrade_required() && ($res = $data_upgrade->do_upgrade()) !== true){ + return $res; + } + } + return true; + } + + } + + class mw_data_upgrade{ + + var $versions; + var $env; + + function mw_data_upgrade($env){ + $this->env = $env; + } + + function env(){ + return isset($this->env) ? $this->env : false; + } + + function load_versions($application_name, $versions_dir){ + if($dh = opendir($versions_dir)){ + $versions_dir .= $versions_dir && substr($versions_dir, -1) != "/" ? "/" : ""; + while(($file = readdir($dh)) !== false){ + if( + substr($file, 0, 1) != "." + && !is_dir($versions_dir.$file) + && strcmp(substr($file, -4), ".php") == 0 + ) $this->load_version($application_name, $versions_dir, $file); + } + closedir($dh); + } + else return false; + return true; + } + + function load_version($application_name, $versions_dir, $version_file){ + if( + !$version_file + || !file_exists($versions_dir.$version_file) + || (substr($version_file, 0, 1) == ".") + || is_dir($versions_dir.$version_file) + || (substr($version_file, -4) != ".php") + ) return false; + $class_name = substr($version_file, 0, -4); + if(!class_exists($class_name)) require_once $versions_dir.$version_file; + if(!class_exists($class_name)) return false; + $version = new $class_name($this->env(), $application_name); + return $this->add_version($version); + } + + function add_version($version){ + if(!$version->version()) return false; + if(!isset($this->versions)) $this->versions = array(); + $tmp = array(); + $INSERTED = false; + foreach($this->versions as $_version){ + if(version_compare($version->version(), $_version->version()) == 0){ + $INSERTED = true; + } + elseif(version_compare($version->version(), $_version->version()) < 0){ + $tmp[] = $version; + $tmp[] = $_version; + $INSERTED = true; + } + else{ + $tmp[] = $_version; + } + } + if(!$INSERTED) $tmp[] = $version; + $this->versions = $tmp; + return true; + } + + function upgrade_required(){ + if(!isset($this->versions)) return false; + foreach($this->versions as $version_number => $version){ + if($version->upgrade_required()) return true; + } + return false; + } + + function do_upgrade(){ + if(!isset($this->versions)) return true; + foreach($this->versions as $version_number => $version){ + if($version->upgrade_required() && ($res = $version->do_upgrade()) !== true){ + return $res; + } + } + return true; + } + + } + + class mw_data_version{ + + var $env; + var $application_name; + + function version(){ + return false; + } + + function mw_data_version($env, $application_name){ + $this->env = $env; + $this->application_name = $application_name; + } + + function env(){ + return isset($this->env) ? $this->env : false; + } + + function upgrade_required(){ + if(!isset($this->application_name)) return false; + $env = $this->env(); + $data = $env->data(); + if(!($env_version = $env->version($this->application_name))) return false; + if(!($data_version = $data->version($this->application_name))) return true; + return version_compare($data_version, $env_version) < 0; + } + + function do_upgrade(){ + return true; + } + + } diff --git a/mw/env/modules/mw_env_webmaster.php b/mw/env/modules/mw_env_webmaster.php new file mode 100644 index 0000000..d1ceabc --- /dev/null +++ b/mw/env/modules/mw_env_webmaster.php @@ -0,0 +1,38 @@ +data(); + if(!($webmaster_user = $this->get_webmaster_user())) return array(); + return $data->load_session($webmaster_user); + } + + function get_webmaster_user(){ + if(!file_exists($this->get_webmaster_user_file())) return array(); + if(!($content = @file_get_contents($this->get_webmaster_user_file()))) return false; + $v_content = explode(":", $content); + if(count($v_content) != 2) return false; + return array( + "login" => trim($v_content[0]), + "password" => trim($v_content[1]) + ); + } + + function set_webmaster_user($login, $password){ + return @file_put_contents($this->get_webmaster_user_file(), $login.":".$password); + } + + function get_webmaster_user_file(){ + return $this->path("content")."data/.webmaster"; + } + + function webmaster_login($login, $password){ + $data = $this->data(); + if(!($webmaster_user = $this->get_webmaster_user())){ + return false; + } + return $data->login($login, $password, $webmaster_user); + } + + } diff --git a/mw/mw_app.php b/mw/mw_app.php index 990d7a7..5349b79 100644 --- a/mw/mw_app.php +++ b/mw/mw_app.php @@ -9,10 +9,10 @@ var $config; var $bdd; var $error; - var $DO_SETUP; + var $DO_INSTALL; - function mw_app($path_file, $DO_SETUP = false){ - $this->DO_SETUP = $DO_SETUP; + function mw_app($path_file, $DO_INSTALL = false){ + $this->DO_INSTALL = $DO_INSTALL; $this->path_file = $path_file; $this->pathes = array(); $this->config_file = ""; @@ -39,19 +39,14 @@ if(!$this->init_config()) return $this->get_error(); if(!$this->init_env()) return $this->get_error(); if(!$this->config_file){ - if($this->DO_SETUP){ - $this->setup(); + if($this->DO_INSTALL){ + $this->install(); exit; } return $this->get_error(); } $env = $this->env(); - if(!$env->version("mtweb")){ - $this->error("impossible de lire la version du code"); - return $this->get_error(); - } - $data = $env->data(); - if($data->version("mtweb") != $env->version("mtweb")){ + if($env->data_upgrade_required()){ $this->upgrade(); exit; } @@ -128,7 +123,7 @@ } } else{ - if(!$this->DO_SETUP){ + if(!$this->DO_INSTALL){ $this->error("fichier config.php manquant"); return false; } @@ -165,6 +160,7 @@ $env->load_versions(); $env->load_config($this->bdd, $this->config); $env->init(); + $env->init_data_upgrades(); return true; } @@ -175,24 +171,42 @@ return true; } - function display(){ + function run_mod($mod_name, $valid_role = true){ $env = $this->env(); - if($env->etat_is_valid()){ - $template = $env->get_template(); - $layout = $env->init_layout(); - $template->render_layout($layout); + $etat = false; + if(isset($_GET[$env->param("e")])){ + $etat = $env->valid_etat($_GET[$env->param("e")]); + if(!$etat || ($etat["mod"] != $mod_name)){ + $etat = false; + } + } + if(!$etat){ + $etat = $env->valid_etat($mod_name); } + if(!$etat) return false; + $env->run($etat, array(), $valid_role); + return true; } - function setup(){ - $env = $this->env(); - $etat = isset($_GET[$env->param("e")]) ? $_GET[$env->param("e")] : "install"; - $env->run($etat, array(), false); - $this->display(); + function install(){ + if($this->run_mod("install", false)){ + $this->display(); + } } function upgrade(){ - debug("la base de données doit être mise à jour"); + if($this->run_mod("upgrade", false)){ + $this->display(); + } + } + + function display(){ + $env = $this->env(); + if($env->etat_is_valid()){ + $template = $env->get_template(); + $layout = $env->init_layout(); + $template->render_layout($layout); + } } function error($content){ -- 2.1.4