mecanisme d'upgrade
authorroot <root@dj3c1t.net>
Sun, 3 Nov 2013 00:39:13 +0000 (01:39 +0100)
committerroot <root@dj3c1t.net>
Sun, 3 Nov 2013 00:39:13 +0000 (01:39 +0100)
20 files changed:
mw/app/controllers/install/index.php
mw/app/controllers/upgrade/index.php [new file with mode: 0644]
mw/app/controllers/upgrade/user.php [new file with mode: 0644]
mw/app/data/modules/share/mw_data_users_sessions.php
mw/app/data/mw_data.php [deleted file]
mw/app/data/mw_sgbd.php [deleted file]
mw/app/data/upgrades/xml/mtweb_xml_version_0_11_2.php [new file with mode: 0644]
mw/app/init/0300_data.php
mw/app/out/default/js/actions/upgrade.js [new file with mode: 0644]
mw/app/out/default/layouts/upgrade.xml [new file with mode: 0644]
mw/app/out/default/upgrade.php [new file with mode: 0644]
mw/app/out/default/views/upgrade/colonne.php [new file with mode: 0644]
mw/app/out/default/views/upgrade/footer.php [new file with mode: 0644]
mw/app/out/default/views/upgrade/head.php [new file with mode: 0644]
mw/app/out/default/views/upgrade/header.php [new file with mode: 0644]
mw/app/out/default/views/upgrade/index.php [new file with mode: 0644]
mw/env/modules/mw_env_data.php
mw/env/modules/mw_env_data_upgrade.php [new file with mode: 0644]
mw/env/modules/mw_env_webmaster.php [new file with mode: 0644]
mw/mw_app.php

index 8cc63f0..c47b231 100644 (file)
             $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 (file)
index 0000000..ecbb576
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+  class mw_upgrade_index extends mw_controller{
+
+    function validate(){
+      $env = $this->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 (file)
index 0000000..68dce71
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+  class mw_upgrade_user extends mw_controller{
+
+    function validate(){
+      $env = $this->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 &ecirc;tes maintenant identifi&eacute; 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);
+    }
+
+  }
index 9a787a0..df81d46 100644 (file)
@@ -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;
         }
 
     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;
     }
 
     # ----------------------------------------------------------------------------------------
       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 (file)
index 233fe50..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-<?php
-
-  class mw_data extends empty_class{
-
-    function call_default($inst, $method_name, $arguments){
-      return $this->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 (file)
index c3ca66e..0000000
+++ /dev/null
@@ -1,152 +0,0 @@
-<?php
-
-  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/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 (file)
index 0000000..9bac3d0
--- /dev/null
@@ -0,0 +1,273 @@
+<?php
+
+  class mtweb_xml_version_0_11_2 extends mw_data_version{
+
+    function version(){
+      return "0.11.2";
+    }
+
+    function do_upgrade(){
+
+      $env = $this->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;
+    }
+
+  }
index 9d5e2f9..fd785f7 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 
-  if(!class_exists("mw_sgbd")) require_once $this->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 (file)
index 0000000..ca05ce5
--- /dev/null
@@ -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 (file)
index 0000000..a41df3e
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<layout>
+
+  <upgrade page="upgrade.php">
+    <index content="views/upgrade/index.php" />
+  </upgrade>
+
+</layout>
\ 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 (file)
index 0000000..5c90c3b
--- /dev/null
@@ -0,0 +1,36 @@
+<!doctype html>
+<html>
+  <head>
+<?php require $this->out_file("views/upgrade/head.php"); ?>
+  </head>
+  <body>
+
+    <header id="main_header">
+      <div class="content">
+<?php require $this->out_file("views/upgrade/header.php"); ?>
+     </div>
+    </header>
+
+    <div id="main">
+      <div class="content">
+
+        <div id="colonne" class="admin">
+<?php require $this->out_file("views/upgrade/colonne.php"); ?>
+        </div>
+
+        <div id="center">
+<?php require $this->out_file("views/messages.php"); ?>
+<?php if($layout["content"] && $this->out_file_exists($layout["content"])) require $this->out_file($layout["content"]); ?>
+        </div>
+
+      </div>
+    </div>
+
+    <footer id="main_footer">
+      <div class="content">
+<?php require $this->out_file("views/upgrade/footer.php"); ?>
+     </div>
+    </footer>
+
+  </body>
+</html>
diff --git a/mw/app/out/default/views/upgrade/colonne.php b/mw/app/out/default/views/upgrade/colonne.php
new file mode 100644 (file)
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 (file)
index 0000000..f1fb6f3
--- /dev/null
@@ -0,0 +1,3 @@
+<ul>
+  <li><a href="http://mtweb.dj3c1t.com/">mtweb</a></li>
+</ul>
diff --git a/mw/app/out/default/views/upgrade/head.php b/mw/app/out/default/views/upgrade/head.php
new file mode 100644 (file)
index 0000000..0f9cce0
--- /dev/null
@@ -0,0 +1,43 @@
+    <meta charset="UTF-8">
+
+    <title>Mtweb - upgrade</title>
+
+    <!--[if lt IE 9]>\r
+    <script type="text/javascript" src="<?php echo $this->out_url("js/html5.js"); ?>"></script>\r
+    <![endif]-->
+
+    <script type="text/javascript"> mw_site_url = "<?php echo $this->path("web") ?>"; </script>
+    <script type="text/javascript" src="<?php echo $this->out_url("js/md5.js"); ?>"></script>
+    <?php foreach($this->js_files() as $js_file) : ?>
+    <script type="text/javascript" src="<?php echo $js_file; ?>"></script>
+    <?php endforeach; ?>
+
+    <?php foreach($this->css_files() as $css_file) : ?>
+    <link rel="stylesheet" type="text/css" href="<?php echo $css_file; ?>" />
+    <?php endforeach; ?>
+
+    <?php if($this->out_file_exists("js/script.js")) : ?>
+    <script type="text/javascript" src="<?php echo $this->out_url("js/script.js"); ?>"></script>
+    <?php endif; ?>
+    <?php if($this->out_file_exists("js/actions/".$this->etat("mod").".js")) : ?>
+    <script type="text/javascript" src="<?php echo $this->out_url("js/actions/".$this->etat("mod").".js"); ?>"></script>
+    <?php endif; ?>
+    <?php if($this->out_file_exists("js/actions/".$this->etat("mod")."_".$this->etat("controller").".js")) : ?>
+    <script type="text/javascript" src="<?php echo $this->out_url("js/actions/".$this->etat("mod")."_".$this->etat("controller").".js"); ?>"></script>
+    <?php endif; ?>
+    <?php if($this->out_file_exists("js/actions/".$this->etat("mod")."_".$this->etat("controller")."_".$this->etat("action").".js")) : ?>
+    <script type="text/javascript" src="<?php echo $this->out_url("js/actions/".$this->etat("mod")."_".$this->etat("controller")."_".$this->etat("action").".js"); ?>"></script>
+    <?php endif; ?>
+
+    <?php if($this->out_file_exists("css/style.css")) : ?>
+    <link rel="stylesheet" type="text/css" href="<?php echo $this->out_url("css/style.css"); ?>" />
+    <?php endif; ?>
+    <?php if($this->out_file_exists("css/actions/".$this->etat("mod").".css")) : ?>
+    <link rel="stylesheet" type="text/css" href="<?php echo $this->out_url("css/actions/".$this->etat("mod").".css"); ?>" />
+    <?php endif; ?>
+    <?php if($this->out_file_exists("css/actions/".$this->etat("mod")."_".$this->etat("controller").".css")) : ?>
+    <link rel="stylesheet" type="text/css" href="<?php echo $this->out_url("css/actions/".$this->etat("mod")."_".$this->etat("controller").".css"); ?>" />
+    <?php endif; ?>
+    <?php if($this->out_file_exists("css/actions/".$this->etat("mod")."_".$this->etat("controller")."_".$this->etat("action").".css")) : ?>
+    <link rel="stylesheet" type="text/css" href="<?php echo $this->out_url("css/actions/".$this->etat("mod")."_".$this->etat("controller")."_".$this->etat("action").".css"); ?>" />
+    <?php endif; ?>
\ 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 (file)
index 0000000..c3d5e22
--- /dev/null
@@ -0,0 +1 @@
+<h1>Mtweb - upgrade</h1>
\ 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 (file)
index 0000000..46137c1
--- /dev/null
@@ -0,0 +1,64 @@
+<h2>Mise à jour des données</h2>
+
+<p>
+  La base de données doit être mise à jour pour
+  <strong>mtweb.<?php echo $this->out["env_version"]; ?></strong>.
+</p>
+<br /><br />
+
+<?php
+
+  if(!($user = $this->user())) :
+  $data = $this->data();
+  $app_session_key = $data->app_session_key();
+
+?>
+
+<form id="login_form"
+      action="<?php echo $this->url("upgrade/user/login"); ?>"
+      method="post">
+  <fieldset>
+    <legend>Identification</legend>
+    <p>
+      Identifiez-vous avec l'utilisateur que vous avez créé pendant l'installation du site.
+    </p>
+    <input type="hidden" name="pass" value="" />
+    <ul>
+      <li>
+        <label for="login">login</label>
+        <div class="form_input">
+          <input type="text" id="login" name="login" size="14" maxlength="25" />
+        </div>
+      </li>
+      <li>
+        <label for="password">pass</label>
+        <div class="form_input">
+          <input type="password" id="password" name="password" size="14" maxlength="16" />
+        </div>
+      </li>
+      <li>
+        <div class="form_single_button">
+          <input type="submit" value="Login" onclick="prepare_password('<?php echo $_SESSION[$app_session_key]["id"]; ?>')" />
+        </div>
+      </li>
+    </ul>
+  </fieldset>
+</form>
+
+<?php else : ?>
+
+<p>
+  <strong>Cette opération est risquée</strong>.
+</p>
+<br />
+<pre class="debug">faites une sauvegarde de vos données
+avant de lancer la mise à jour de la base</pre>
+<br />
+<br />
+<ul class="buttons">
+  <li><a href="<?php echo $this->url("upgrade/index/confirm_upgrade"); ?>"
+         onclick="return confirm('Lancer l\'upgrade ?')">Lancer la mise à jour</a></li>
+  <li><a href="<?php echo $this->url("upgrade/user/logout"); ?>">Annuler</a></li>
+</ul>
+
+<?php endif; ?>
index fb1a5bd..e6cb000 100644 (file)
 
   }
 
+  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 (file)
index 0000000..8a3f0c1
--- /dev/null
@@ -0,0 +1,162 @@
+<?php
+
+  class mw_env_data_upgrade extends mw_env{
+
+    var $data_upgrades;
+
+    function init_data_upgrades(){
+      $data_upgrade = new mw_data_upgrade($this);
+      $impl_dir = ($this->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 (file)
index 0000000..d1ceabc
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+  class mw_env_webmaster extends mw_env{
+
+    function load_webmaster_session(){
+      $data = $this->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);
+    }
+
+  }
index 990d7a7..5349b79 100644 (file)
@@ -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 = "";
       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;
       }
         }
       }
       else{
-        if(!$this->DO_SETUP){
+        if(!$this->DO_INSTALL){
           $this->error("fichier config.php manquant");
           return false;
         }
       $env->load_versions();
       $env->load_config($this->bdd, $this->config);
       $env->init();
+      $env->init_data_upgrades();
       return true;
     }
 
       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){