reorganisation init
[mtweb] / mw / env / modules / mw_env_sgbds.php
1 <?php
2
3   class mw_env_sgbds extends mw_env{
4
5     private $sgbds;
6
7     public function sgbds(){
8       if(isset($this->sgbds)) return $this->sgbds;
9       $this->sgbds = array();
10       $impls_dir = $this->path("mw_dir")."env/sgbd";
11       if(!file_exists($impls_dir) || !is_dir($impls_dir)){
12         return false;
13       }
14       if($dh = opendir($impls_dir)){
15         $OK = true;
16         while($OK && ($impl_file = readdir($dh)) !== false){
17           if(substr($impl_file, 0 ,1) !== "." && substr($impl_file, -4) == ".php"){
18             require_once $impls_dir."/".$impl_file;
19             if(class_exists($class_name = substr($impl_file, 0, -4))){
20               if(
21                     method_exists($class_name, "name")
22                 &&  method_exists($class_name, "extention_ok")
23               ){
24                 $impl = new $class_name($this);
25                 if($impl->extention_ok($this)) $this->sgbds[$class_name] = $impl;
26               }
27             }
28           }
29         }
30       }
31       else{
32         return false;
33       }
34       return $this->sgbds;
35     }
36
37     public function load_sgbd(){
38       if(($data = $this->data()) && $this->bdd("sgbd")){
39         $sgbd_impl_file = $this->path("mw_dir")."env/sgbd/mw_".$this->bdd("sgbd").".php";
40         if(!file_exists($sgbd_impl_file)){
41           $this->erreur("Impossible de trouver le fichier d'implementation du sgbd ".$this->bdd("sgbd"), true);
42         }
43         $sgbd_impl = "mw_".$this->bdd("sgbd");
44         if(!class_exists($sgbd_impl)) require_once $sgbd_impl_file;
45         if(!class_exists($sgbd_impl)){
46           $this->erreur("Impossible de trouver la classe d'implementation du sgbd ".$this->bdd("sgbd"), true);
47         }
48         $sgbd = new $sgbd_impl(
49           $this,
50           array(
51             "host" => $this->bdd("host"),
52             "base" => $this->bdd("base"),
53             "user" => $this->bdd("user"),
54             "password" => $this->bdd("password")
55           )
56         );
57         if(!$sgbd->extention_ok()){
58           $this->erreur("L'extention php ".$this->bdd("sgbd")." n'est pas install&eacute;e", true);
59         }
60         $data->set_sgbd($sgbd);
61       }
62     }
63
64   }
65
66   // -------------------------------------------------------------------------------------------
67   //                                                                               class mw_sgbd
68   //
69
70   abstract class mw_sgbd{
71
72     public $env;
73     public $link;
74     public $host;
75     public $base;
76     public $user;
77     public $password;
78     public $EXTENTION_OK;
79
80     public function __construct($env, $params = array()){
81       $this->env = $env;
82       $default_params = $this->default_params();
83       $params = $this->prepare_params($params);
84       $this->host = isset($params["host"]) ? $params["host"] : $default_params["host"];
85       $this->base = isset($params["base"]) ? $params["base"] : $default_params["base"];
86       $this->user = isset($params["user"]) ? $params["user"] : $default_params["user"];
87       $this->password = isset($params["password"]) ? $params["password"] : $default_params["password"];
88       $this->EXTENTION_OK = $this->validate_extention();
89     }
90
91     public function name(){
92       return "";
93     }
94
95     public function default_params(){
96       return array(
97         "host" => "",
98         "base" => "",
99         "user" => "",
100         "password" => ""
101       );
102     }
103
104     public function prepare_params($params){
105       return $params;
106     }
107
108     public function validate_extention(){
109       return false;
110     }
111
112     public function authentication_required(){
113       return false;
114     }
115
116     public function get_link(){
117       return $this->link;
118     }
119
120     public function extention_ok(){
121       return $this->EXTENTION_OK;
122     }
123
124     public function replace_prefixes($content){
125       return (
126         ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ?
127           str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $content)
128         : $content
129       );
130     }
131
132   }