source: bureau/class/m_mysql.php @ 1

Revision 1, 10.6 KB checked in by anarcat, 7 years ago (diff)

[project @ Tailorization of alternc]
Tailorization of alternc

Import of the upstream sources from

Repository: /cvs
Module: alternc
Revision: 2003-03-26 17:41:29 by root

Original author: tailor@cvs
Date: 2005-08-30 16:57:40

Line 
1<?php
2/*
3 $Id: m_mysql.php,v 1.1.1.1 2003/03/26 17:41:29 root Exp $
4 ----------------------------------------------------------------------
5 AlternC - Web Hosting System
6 Copyright (C) 2002 by the AlternC Development Team.
7 http://alternc.org/
8 ----------------------------------------------------------------------
9 Based on:
10 Valentin Lacambre's web hosting softwares: http://altern.org/
11 ----------------------------------------------------------------------
12 LICENSE
13
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License (GPL)
16 as published by the Free Software Foundation; either version 2
17 of the License, or (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 GNU General Public License for more details.
23
24 To read the license please visit http://www.gnu.org/copyleft/gpl.html
25 ----------------------------------------------------------------------
26 Original Author of file:
27 Purpose of file:
28 ----------------------------------------------------------------------
29*/
30class m_mysql {
31
32  var $uid=0;         /* Membre dont on souhaite gérer les comptes mysql */
33
34  var $alternc_quota_name="mysql";
35
36  var $server="localhost";
37
38  /*---------------------------------------------------------------------------*/
39  /**
40     m_mysql([$mid]) Constructeur de la classe m_mysql, initialise le membre concerne
41  */
42  function m_mysql($membre=0) {
43    $this->uid=$membre;
44  }
45
46  /*---------------------------------------------------------------------------*/
47  /**
48     get_dblist() retourne la liste des bases configurées pour le membre.
49     @return array tableau de tableaux associatifs contenant
50       "db" => nom de la base "bck" => "mode de backup" "dir" => "Répertoire de backup
51       "size" => Size of the database (in bytes)
52       retourne false si l'utilisateur n'a aucune base de données
53  */
54  function get_dblist() {
55    global $db,$err,$bro;
56    $err->log("mysql","get_dblist");
57    $db->query("SELECT pass,db, bck_mode, bck_dir FROM db WHERE uid=".$this->uid.";");
58    if (!$db->num_rows()) {
59      return false;
60    }
61    $c=array();
62    while ($db->next_record()) {
63      $size=$bro->fsize("/var/alternc/db/".$db->f("db")."/");
64      list($dbu,$dbn)=explode("_",$db->f("db"));
65      $c[]=array("db"=>$db->f("db"), "name"=>$dbn,"bck"=>$db->f("bck_mode"), "dir"=>$db->f("bck_dir"), "size"=>$size, "pass"=>$db->f("pass"));
66    }
67    return $c;
68  }
69
70  /*---------------------------------------------------------------------------*/
71  /**
72     add_db($dbn) Create a new database for the current user.
73     @return true if the database $user_$db has been successfully created, or false if
74     an error occured, such as over quota user.
75  */
76  function add_db($dbn) {
77    global $db,$err,$quota,$mem;
78    $err->log("mysql","add_db",$dbn);
79    if (!$quota->cancreate("mysql")) {
80      $err->raise("mysql",1);
81      return false;
82    }
83    if (!ereg("^[0-9a-z]*",$dbn)) {
84      $err->raise("mysql",2);
85      return false;
86    }
87    $dbname=$mem->user["login"].($dbn?"_":"").$dbn;
88    $db->query("SELECT * FROM db WHERE db='$dbname';");
89    if ($db->num_rows()) {
90      $err->raise("mysql",3);
91      return false;
92    }
93    // find the login/pass for this user :
94    $db->query("SELECT login,pass FROM db WHERE uid='".$this->uid."' LIMIT 0,1;");
95    if (!$db->num_rows()) {
96      $lo=$mem->user["login"];
97      $pa="";
98    } else {
99      $db->next_record();
100      $lo=addslashes($db->f("login"));
101      $pa=addslashes($db->f("pass"));
102    }
103    // Ok, database does not exist, quota is ok and dbname is compliant. Let's proceed
104    $db->query("INSERT INTO db (uid,login,pass,db,bck_mode) VALUES ('".$this->uid."','$lo','$pa','$dbname',0);");
105    $db->query("INSERT INTO mysql.db (Host, Db, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Grant_priv, References_priv, Index_priv, Alter_priv) VALUES ('localhost','$dbname','$lo','Y','Y','Y','Y','Y','Y','N','Y','Y','Y');");
106    $db->query("CREATE DATABASE $dbname;");
107    exec("/usr/lib/alternc/db_create ".$this->uid." /var/alternc/db/$dbname");
108    $db->query("FLUSH PRIVILEGES;");
109    $quota->inc("mysql");
110    return true;
111  }
112
113  /*---------------------------------------------------------------------------*/
114  /**
115     del_db($dbn) Delete a database for the current user.
116     @return true if the database $user_$db has been successfully deleted, or false if
117     an error occured, such as db does not exist.
118  */
119  function del_db($dbn) {
120    global $db,$err,$quota,$mem;
121    $err->log("mysql","del_db",$dbn);
122    if (!ereg("^[0-9a-z]*$",$dbn)) {
123      $err->raise("mysql",2);
124      return false;
125    }
126    $dbname=$mem->user["login"].($dbn?"_":"").$dbn;
127    $db->query("SELECT * FROM db WHERE db='$dbname';");
128    if (!$db->num_rows()) {
129      $err->raise("mysql",4);
130      return false;
131    }
132    // Ok, database exists and dbname is compliant. Let's proceed
133    $db->query("DELETE FROM db WHERE uid='".$this->uid."' AND db='$dbname';");
134    $db->query("DELETE FROM mysql.db WHERE db='$dbname';");
135    $db->query("DROP DATABASE $dbname;");
136    $quota->dec("mysql");
137    $db->query("SELECT COUNT(*) AS cnt FROM db WHERE uid='".$this->uid."';");
138    $db->next_record();
139    if ($db->f("cnt")==0) {
140      $db->query("DELETE FROM mysql.user WHERE User='".$mem->user["login"]."';");
141    }
142    $db->query("FLUSH PRIVILEGES;");
143    return true;
144  }
145 
146  /*---------------------------------------------------------------------------*/
147  /** put_mysql_backup() Set the backup parameters for db $db
148      @param $db string database name
149      @param $bck_mode integer Backup mode (0 = none 1 = daily 2 = weekly)
150      @param $bck_history integer How many backup should we keep ?
151      @param $bck_gzip boolean Shall we compress the backup ?
152      @param $bck_dir string Directory relative to the user account where the backup will be stored
153      @return boolean true if the backup parameters has been successfully changed, false if not.
154  */
155  function put_mysql_backup($dbn,$bck_mode,$bck_history,$bck_gzip,$bck_dir) {
156    global $db,$err,$mem,$bro;
157    $err->log("mysql","put_mysql_backup");
158    if (!ereg("^[0-9a-z]*$",$dbn)) {
159      $err->raise("mysql",2);
160      return false;
161    }
162    $dbname=$mem->user["login"].($dbn?"_":"").$dbn;
163    $db->query("SELECT * FROM db WHERE uid=".$this->uid." AND db='$dbname';");
164    if (!$db->num_rows()) {
165      $err->raise("mysql",4);
166      return false;
167    }
168    $db->next_record();
169    $login=$db->f("login");
170    $bck_mode=intval($bck_mode);
171    $bck_history=intval($bck_history);
172    if ($bck_gzip)
173      $bck_gzip="1";
174    else
175      $bck_gzip="0";
176    if (!$bck_mode)
177      $bck_mode="0";
178    if (!$bck_history) {
179      $err->raise("mysql",5);
180      return false;
181    }
182    if (($bck_dir=$bro->convertabsolute($bck_dir,0))===false) { // return a full path or FALSE
183      $err->raise("mysql",6);
184      return false;
185    }
186    $db->query("UPDATE db SET bck_mode=$bck_mode, bck_history=$bck_history, bck_gzip=$bck_gzip, bck_dir='$bck_dir' WHERE uid=".$this->uid." AND db='$dbname';");
187    return true;
188  }
189
190  /*---------------------------------------------------------------------------*/
191  /** put_mysql_details($password) Change the password of the user
192      @param $password string new password (cleartext)
193      @return boolean true if the password has been successfully changed, false else.
194  */
195  function put_mysql_details($password) {
196    global $db,$err,$mem;
197    $err->log("mysql","put_mysql_details");
198    $db->query("SELECT * FROM db WHERE uid=".$this->uid.";");
199    if (!$db->num_rows()) {
200      $err->raise("mysql",7);
201      return false;
202    }
203    if (strlen($password)>16) {
204      $err->raise("mysql",8);
205      return false;
206    }
207    $login=$mem->user["login"];
208    $db->query("UPDATE db SET pass='$password' WHERE uid=".$this->uid.";");
209    $db->query("UPDATE mysql.user SET password=PASSWORD('$password') WHERE User='$login';");
210    $db->query("FLUSH PRIVILEGES;");
211    return true;
212  }
213
214  /* ****************************************************************************
215     new_mysql($password) Cree un nouveau compte mysql au membre
216     $password est le mot de passe (en clair)
217  *****************************************************************************/
218  function new_mysql($password) {
219    global $db,$err,$mem;
220    $err->log("mysql","new_mysql");
221    if (strlen($password)>16) {
222      $err->raise("mysql",2);
223      return false;
224    }
225    $db->query("SELECT * FROM db WHERE uid=".$this->uid.";");
226    if ($db->num_rows()) {
227      $err->raise("mysql",8);
228      return false;
229    }
230    $login=$mem->user["login"];
231    // OK, creation now...
232    $db->query("INSERT INTO db (uid,login,pass,db) VALUES ('".$this->uid."','".$mem->user["login"]."','$password','".$mem->user["login"]."');");
233    $db->query("INSERT INTO mysql.user (Host,User,Password) VALUES ('localhost','".$mem->user["login"]."',PASSWORD('$password'));");
234    $db->query("INSERT INTO mysql.db (Host, Db, User, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Grant_priv, References_priv, Index_priv, Alter_priv) VALUES ('localhost','".$mem->user["login"]."','".$mem->user["login"]."','Y','Y','Y','Y','Y','Y','N','Y','Y','Y');");
235    $db->query("CREATE DATABASE ".$mem->user["login"].";");
236    exec("/usr/lib/alternc/db_create ".$this->uid." /var/alternc/db/".$mem->user["login"]);
237    $db->query("FLUSH PRIVILEGES;");
238    return true;
239  }
240
241
242
243
244  function restore($file,$stdout,$r) {
245    global $err,$bro,$mem;
246    if (!($fi=$bro->convertabsolute($file,0))) {
247      $err->raise("mysql",7);
248    }
249    if (substr($fi,-3)==".gz") {
250      $exe="/bin/gzip -d -c <\"$fi\" | /usr/bin/mysql -u".$r["login"]." -p".$r["pass"]." ".$r["login"];
251    } else {
252      $exe="/usr/bin/mysql -u".$r["login"]." -p".$r["pass"]." ".$r["login"]." <".$fi;
253    }
254    if ($stdout) {
255      passthru($exe);
256    } else {
257      exec ($exe);
258    }
259    return true;
260  }
261
262  /* TODO / TO BE IMPLEMENTED : */
263
264  function add_member($id) {
265    global $db,$err;
266    $err->log("mysql","add_member");
267    $db->query("SELECT login FROM membres WHERE uid=$id;");
268    $db->next_record();
269    $login=$db->f("login");
270    $db->query("INSERT INTO db (uid,login,pass,enabled) VALUES ($id,'$login','',0);");
271    return true;
272  }
273
274  function del_member($id) {
275    global $db,$err;
276    $err->log("mysql","del_member");
277    $db->query("SELECT login FROM membres WHERE uid=$id;");
278    $db->next_record();
279    $login=$db->f("login");
280    $this->delete_mysql($id);
281    $db->query("DELETE FROM db WHERE login='$login';");
282    return true;
283  }
284
285} /* Class m_mysql */
286
287?>
Note: See TracBrowser for help on using the repository browser.