dfdb74f9b76f12d355a3655c00f9784f218debc9
[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   }
38
39   // -------------------------------------------------------------------------------------------
40   //                                                                               class mw_sgbd
41   //
42
43   abstract class mw_sgbd{
44
45     public $env;
46     public $link;
47     public $host;
48     public $base;
49     public $user;
50     public $password;
51     public $EXTENTION_OK;
52
53     public function __construct($env, $params = array()){
54       $this->env = $env;
55       $default_params = $this->default_params();
56       $params = $this->prepare_params($params);
57       $this->host = isset($params["host"]) ? $params["host"] : $default_params["host"];
58       $this->base = isset($params["base"]) ? $params["base"] : $default_params["base"];
59       $this->user = isset($params["user"]) ? $params["user"] : $default_params["user"];
60       $this->password = isset($params["password"]) ? $params["password"] : $default_params["password"];
61       $this->EXTENTION_OK = $this->validate_extention();
62     }
63
64     public function name(){
65       return "";
66     }
67
68     public function default_params(){
69       return array(
70         "host" => "",
71         "base" => "",
72         "user" => "",
73         "password" => ""
74       );
75     }
76
77     public function prepare_params($params){
78       return $params;
79     }
80
81     public function validate_extention(){
82       return false;
83     }
84
85     public function authentication_required(){
86       return false;
87     }
88
89     public function get_link(){
90       return $this->link;
91     }
92
93     public function extention_ok(){
94       return $this->EXTENTION_OK;
95     }
96
97     public function replace_prefixes($content){
98       return (
99         ($prefix_codes = array_keys($this->env->bdd("table_prefix"))) ?
100           str_replace($prefix_codes, array_values($this->env->bdd("table_prefix")), $content)
101         : $content
102       );
103     }
104
105   }