Alternc  latest
Alternc logiel libre pour l'hébergement
 All Classes Namespaces Files Functions Variables Pages
m_mysql Class Reference

Public Member Functions

 __construct ()
 Constructor m_mysql([$mid]) Constructeur de la classe m_mysql, initialise le membre concerne. More...
 
 reload_dbus ()
 
 list_db_servers ()
 
 hook_menu ()
 
 alternc_password_policy ()
 Password kind used in this class (hook for admin class) More...
 
 get_dblist ()
 Get the list of the database for the current user. More...
 
 php_myadmin_connect ()
 Get the login and password of the special user able to connect to phpmyadmin. More...
 
 get_mysql_details ($dbn)
 Returns the details of a user's database. More...
 
 add_db ($dbn)
 Create a new database for the current user. More...
 
 del_db ($dbname)
 Delete a database for the current user. More...
 
 put_mysql_backup ($dbn, $bck_mode, $bck_history, $bck_gzip, $bck_dir)
 Set the backup parameters for the database $db. More...
 
 put_mysql_details ($password)
 Change the password of the user in MySQL. More...
 
 grant ($base, $user, $rights=null, $pass=null, $table= '*')
 Function used to grant SQL rights to users: :database : database user : rights to apply ( optional, every rights apply given if missing : user password ( optional, if not given the pass stays the same, else it takes the new value ) : sql tables to apply rights. More...
 
 restore ($file, $stdout, $id)
 Restore a sql database. More...
 
 get_db_size ($dbname)
 Get the size of a database. More...
 
 get_userslist ($all=null)
 Returns the list of database users of an account. More...
 
 get_defaultsparam ($dbn)
 
 add_user ($usern, $password, $passconf)
 Create a new user in MySQL rights tables. More...
 
 change_user_password ($usern, $password, $passconf)
 Change a user's MySQL password. More...
 
 del_user ($user, $all=false, $caller_is_deldb=false)
 Delete a user in MySQL rights tables. More...
 
 get_user_dblist ($user)
 Return the list of the database rights of user $user. More...
 
 set_user_rights ($user, $dbn, $rights)
 Set the access rights of user $user to database $dbn to be rights $rights. More...
 
 available_sql_rights ()
 list of all possible SQL rights More...
 
 hook_lxc_params ($params)
 Hook function called by the lxc class to set mysql_host and port parameters private. More...
 
 hook_quota_get ()
 Hook function called by the quota class to compute user used quota Returns the used quota for the $name service for the current user. More...
 
 alternc_add_member ()
 Hook function called when a user is created. More...
 
 alternc_del_member ()
 Hook function called when a user is deleted. More...
 
 alternc_del_session ()
 Hook function called when a user is logged out. More...
 
 alternc_export_conf ()
 Exports all the mysql information of an account private EXPERIMENTAL 'sid' function ;) More...
 
 alternc_export_data ($dir)
 Exports all the mysql databases a of give account to $dir directory private EXPERIMENTAL 'sid' function ;) More...
 
 get_dbus_size ($db_name, $db_host, $db_login, $db_password, $db_client)
 Return the size of each databases in a SQL Host given in parameter. More...
 

Public Attributes

 $dbus
 

Detailed Description

Definition at line 58 of file m_mysql.php.

Constructor & Destructor Documentation

m_mysql::__construct ( )

Constructor m_mysql([$mid]) Constructeur de la classe m_mysql, initialise le membre concerne.

Definition at line 67 of file m_mysql.php.

References $cuid, and variable_get().

67  {
68  global $cuid;
69  if (!empty($cuid)) {
70  $this->dbus = new DB_users();
71  }
72  variable_get('sql_allow_users_backups', 1, 'Set 1 to allow users to configure backup of their databases, 0 if you want do disable this feature. Warning: it will not stop configured backup made by sqlbackup.sh');
73  }
$cuid
Definition: bootstrap.php:43
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
MySQL user database management for AlternC.
Definition: m_mysql.php:27

Member Function Documentation

m_mysql::add_db (   $dbn)

Create a new database for the current user.

Parameters
$dbnstring Database name ($user_$dbn is the mysql db name)
Returns
boolean if the database $user_$db has been successfully created, or FALSE if an error occured, such as over quota user.

Definition at line 218 of file m_mysql.php.

References $c, $cuid, $db, $len, $msg, $passwd_classcount, $password, add_user(), create_pass(), grant(), and variable_get().

218  {
219  global $db, $msg, $quota, $cuid, $admin;
220  $msg->log("mysql", "add_db", $dbn);
221  $password_user = "";
222  if (!$quota->cancreate("mysql")) {
223  $msg->raise("ERROR", "mysql", _("Your databases quota is over. You cannot create more databases"));
224  return false;
225  }
226  $pos = strpos($dbn, '_');
227  if ($pos === false) {
228  $dbname = $dbn;
229  } else {
230  $dbncomp = explode('_', $dbn);
231  $dbname = $dbn;
232  $dbn = $dbncomp[1];
233  if (empty($dbn)) { // If nothing after the '_'
234  $msg->raise("ERROR", "mysql", _("Database can't have empty suffix"));
235  return false;
236  }
237  }
238  if (!preg_match("#^[0-9a-z]*$#", $dbn)) {
239  $msg->raise("ERROR", "mysql", _("Database name can contain only letters and numbers"));
240  return false;
241  }
242 
243  $len=variable_get("sql_max_database_length", 64);
244  if (strlen($dbname) > $len) {
245  $msg->raise("ERROR", "mysql", _("Database name cannot exceed %d characters"), $len);
246  return false;
247  }
248  $db->query("SELECT * FROM db WHERE db= ? ;", array($dbname));
249  if ($db->num_rows()) {
250  $msg->raise("ERROR", "mysql", _("Database %s already exists"), $dbn);
251  return false;
252  }
253 
254  // We prevent the automatic creation of user account longer than the max allowed lenght of a MySQL username
255  $len=variable_get('sql_max_username_length', NULL);
256  if (strlen($dbname) <= $len) {
257  $db->query("SELECT name from dbusers where name= ? and enable='ACTIVATED' ;", array($dbname));
258  if (!$db->num_rows()) {
259  // We get the password complexity set in the policy and ensure we have that complexity in the create_pass() call
260  $c=$admin->listPasswordPolicies();
261  $passwd_classcount = $c['mysql']['classcount'];
262 
263  $password_user = create_pass(10, $passwd_classcount);
264  if ($this->add_user($dbn, $password_user, $password_user)) {
265  $msg->raise("INFO", "mysql", "L'utilisateur '$dbname' a été créé et les droits sur cette base de données lui ont été attribué.");
266  } else {
267  $msg->raise("ALERT", "mysql", "L'utilisateur '$dbname' n'a pas pu être créé.<br>Allez à la page 'Utilisateurs Mysql' pour en créer manuellement.<br>Et n'oubliez pas de lui donner les droits sur la base de données.");
268  }
269  }
270  } else {
271  $msg->raise("ALERT", "mysql", "L'utilisateur '$dbname' n'a pas été automatiquement créé car il dépasse la limite de taille pour les utilisateurs qui est à $len<br>Allez à la page 'Utilisateurs Mysql' pour en créer un avec le nom que vous voulez.<br>Et n'oubliez pas de lui donner les droits sur la base de données.");
272  }
273 
274  // checking for the phpmyadmin user
275  $db->query("SELECT * FROM dbusers WHERE uid= ? AND enable='ADMIN';", array($cuid));
276  if ($db->num_rows()) {
277  $db->next_record();
278  $myadm = $db->f("name");
279  $password = $db->f("password");
280  } else {
281  $msg->raise("ERROR", "mysql", _("There is a problem with the special PhpMyAdmin user. Contact the administrator"));
282  return false;
283  }
284 
285  // Grant the special user every rights.
286  if ($this->dbus->exec("CREATE DATABASE $dbname;")) { // secured: dbname is checked against ^[0-9a-z]*$
287  $msg->log("mysql", "add_db_succes", $dbn);
288  // Ok, database does not exist, quota is ok and dbname is compliant. Let's proceed
289  $db->query("INSERT INTO db (uid,login,pass,db,bck_mode) VALUES (?, ?, ?, ? ,0)", array($cuid, $myadm, $password, $dbname));
290  $dbuser = $dbname;
291  $dbname = str_replace('_', '\_', $dbname);
292  $this->grant($dbname, $myadm, "ALL PRIVILEGES", $password);
293  if (!empty($password_user)) {
294  $this->grant($dbname, $dbuser, "ALL PRIVILEGES", $password_user);
295  }
296  $this->dbus->query("FLUSH PRIVILEGES;");
297  return true;
298  } else {
299  $msg->log("mysql", "add_db", $dbn);
300  $msg->raise("ERROR", "mysql", _("An error occured. The database could not be created"));
301  return false;
302  }
303  }
create_pass($length=10, $classcount=3)
Create a password compatible with the password policy.
Definition: functions.php:877
add_user($usern, $password, $passconf)
Create a new user in MySQL rights tables.
Definition: m_mysql.php:649
global $db
Definition: bootstrap.php:22
$len
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
$password
Definition: bootstrap.php:85
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
$c
Definition: bootstrap.php:47
$passwd_classcount
Definition: aws_pass.php:58
grant($base, $user, $rights=null, $pass=null, $table= '*')
Function used to grant SQL rights to users: :database : database user : rights to apply ( optional...
Definition: m_mysql.php:447
m_mysql::add_user (   $usern,
  $password,
  $passconf 
)

Create a new user in MySQL rights tables.

Parameters
$usernthe username (we will add _[alternc-account] to it)
string$passwordThe password for this username
string$passconfThe password confirmation
Returns
boolean if the user has been created in MySQL or FALSE if an error occurred

Definition at line 649 of file m_mysql.php.

References $cuid, $db, $len, $login, $mem, $msg, $password, $user, grant(), and variable_get().

Referenced by add_db().

649  {
650  global $db, $msg, $mem, $cuid, $admin;
651  $msg->log("mysql", "add_user", $usern);
652 
653  $usern = trim($usern);
654  $login = $mem->user["login"];
655  if ($login != $usern) {
656  $user = $login . "_" . $usern;
657  } else {
658  $user = $usern;
659  }
660  if (!$usern) {
661  $msg->raise("ALERT", "mysql", _("The username is mandatory"));
662  return false;
663  }
664  if (!$password) {
665  $msg->raise("ALERT", "mysql", _("The password is mandatory"));
666  return false;
667  }
668  if (!preg_match("#^[0-9a-z]#", $usern)) {
669  $msg->raise("ERROR", "mysql", _("The username can contain only letters and numbers"));
670  return false;
671  }
672 
673  // We check the length of the COMPLETE username, not only the part after _
674  $len=variable_get("sql_max_username_length", 16);
675  if (strlen($user) > $len) {
676  $msg->raise("ERROR", "mysql", _("MySQL username cannot exceed %d characters"), $len);
677  return false;
678  }
679  $db->query("SELECT * FROM dbusers WHERE name= ? ;", array($user));
680  if ($db->num_rows()) {
681  $msg->raise("ERROR", "mysql", _("The database user already exists"));
682  return false;
683  }
684  if ($password != $passconf || !$password) {
685  $msg->raise("ERROR", "mysql", _("The passwords do not match"));
686  return false;
687  }
688 
689  // Check this password against the password policy using common API :
690  if (is_callable(array($admin, "checkPolicy"))) {
691  if (!$admin->checkPolicy("mysql", $user, $password)) {
692  return false; // The error has been raised by checkPolicy()
693  }
694  }
695 
696  // We add him to the user table
697  $db->query("INSERT INTO dbusers (uid,name,password,enable) VALUES( ?, ?, ?, 'ACTIVATED');", array($cuid, $user, $password));
698 
699  $this->grant("*", $user, "USAGE", $password);
700  return true;
701  }
$user
Definition: bootstrap.php:84
$login
global $db
Definition: bootstrap.php:22
$len
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
$password
Definition: bootstrap.php:85
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
$mem
Definition: bootstrap.php:71
grant($base, $user, $rights=null, $pass=null, $table= '*')
Function used to grant SQL rights to users: :database : database user : rights to apply ( optional...
Definition: m_mysql.php:447
m_mysql::alternc_add_member ( )

Hook function called when a user is created.

AlternC's standard function that create a member private

Definition at line 951 of file m_mysql.php.

References $cuid, $db, $mem, $msg, $password, and create_pass().

951  {
952  global $db, $msg, $cuid, $mem;
953  $msg->log("mysql", "alternc_add_member");
954  // checking for the phpmyadmin user
955  $db->query("SELECT name,password FROM dbusers WHERE uid= ? AND Type='ADMIN';", array($cuid));
956  if ($db->num_rows()) {
957  $myadm = $db->f("name");
958  $password = $db->f("password");
959  } else {
960  $myadm = $cuid . "_myadm";
962  }
963 
964  $db->query("INSERT INTO dbusers (uid,name,password,enable) VALUES (?, ?, ?, 'ADMIN');", array($cuid, $myadm, $password));
965 
966  return true;
967  }
create_pass($length=10, $classcount=3)
Create a password compatible with the password policy.
Definition: functions.php:877
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
$password
Definition: bootstrap.php:85
$mem
Definition: bootstrap.php:71
m_mysql::alternc_del_member ( )

Hook function called when a user is deleted.

AlternC's standard function that delete a member private

Definition at line 975 of file m_mysql.php.

References $c, $d, $i, $msg, del_db(), del_user(), get_dblist(), and get_userslist().

975  {
976  global $msg;
977  $msg->log("mysql", "alternc_del_member");
978  $c = $this->get_dblist();
979  if (is_array($c)) {
980  for ($i = 0; $i < count($c); $i++) {
981  $this->del_db($c[$i]["name"]);
982  }
983  }
984  $d = $this->get_userslist(1);
985  if (!empty($d)) {
986  for ($i = 0; $i < count($d); $i++) {
987  $this->del_user($d[$i]["name"], 1,true);
988  }
989  }
990  return true;
991  }
del_db($dbname)
Delete a database for the current user.
Definition: m_mysql.php:312
get_dblist()
Get the list of the database for the current user.
Definition: m_mysql.php:138
del_user($user, $all=false, $caller_is_deldb=false)
Delete a user in MySQL rights tables.
Definition: m_mysql.php:739
get_userslist($all=null)
Returns the list of database users of an account.
Definition: m_mysql.php:565
$i
$msg
Definition: config.php:155
$c
Definition: bootstrap.php:47
$d
m_mysql::alternc_del_session ( )

Hook function called when a user is logged out.

We just remove the cookie created in admin/sql_admin.php a private

Definition at line 999 of file m_mysql.php.

999  {
1000  $_SESSION['PMA_single_signon_user'] = '';
1001  $_SESSION['PMA_single_signon_password'] = '';
1002  $_SESSION['PMA_single_signon_host'] = '';
1003  }
m_mysql::alternc_export_conf ( )

Exports all the mysql information of an account private EXPERIMENTAL 'sid' function ;)

Definition at line 1011 of file m_mysql.php.

References $cuid, $db, and $msg.

1011  {
1012  // TODO don't work with separated sql server for dbusers
1013  global $db, $msg, $cuid;
1014  $msg->log("mysql", "export");
1015  $db->query("SELECT login, pass, db, bck_mode, bck_dir, bck_history, bck_gzip FROM db WHERE uid= ? ;", array($cuid));
1016  $str = "";
1017  if ($db->next_record()) {
1018  $str.=" <sql>\n";
1019  $str.=" <login>" . $db->Record["login"] . "</login>\n";
1020  $str.=" <pass>" . $db->Record["pass"] . "</pass>\n";
1021  do {
1022  $filename = $tmpdir . "/mysql." . $db->Record["db"] . ".sql.gz"; // FIXME not used
1023  $str.=" <database>" . ($db->Record["db"]) . "</database>\n";
1024  $str.=" <password>" . ($db->Record["pass"]) . "</password>\n";
1025  if ($s["bck_mode"] != 0) { // FIXME what is $s ?
1026  $str.=" <backup-mode>" . ($db->Record["bck_mode"]) . "</backup-mode>\n";
1027  $str.=" <backup-dir>" . ($db->Record["bck_dir"]) . "</backup-dir>\n";
1028  $str.=" <backup-history>" . ($db->Record["bck_history"]) . "</backup-history>\n";
1029  $str.=" <backup-gzip>" . ($db->Record["bck_gzip"]) . "</backup-gzip>\n";
1030  }
1031  } while ($db->next_record());
1032  $str.=" </sql>\n";
1033  }
1034  return $str;
1035  }
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_mysql::alternc_export_data (   $dir)

Exports all the mysql databases a of give account to $dir directory private EXPERIMENTAL 'sid' function ;)

Definition at line 1043 of file m_mysql.php.

References $cuid, $db, and $msg.

1043  {
1044  global $db, $msg, $cuid;
1045  $msg->log("mysql", "export_data");
1046  $db->query("SELECT db.login, db.pass, db.db, dbusers.name FROM db,dbusers WHERE db.uid= ? AND dbusers.uid=db.uid;", array($cuid));
1047  $dir.="sql/";
1048  if (!is_dir($dir)) {
1049  if (!mkdir($dir)) {
1050  $msg->raise("ERROR", 'mysql', _("The directory could not be created"));
1051  }
1052  }
1053  // on exporte toutes les bases utilisateur.
1054  while ($db->next_record()) {
1055  $filename = $dir . "mysql." . $db->Record["db"] . "." . date("H:i:s") . ".sql.gz";
1056  exec("/usr/bin/mysqldump --defaults-file=/etc/alternc/my.cnf --add-drop-table --allow-keywords -Q -f -q -a -e " . escapeshellarg($db->Record["db"]) . " |/bin/gzip >" . escapeshellarg($filename));
1057  }
1058  }
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_mysql::alternc_password_policy ( )

Password kind used in this class (hook for admin class)

Definition at line 126 of file m_mysql.php.

126  {
127  return array("mysql" => "MySQL users");
128  }
m_mysql::available_sql_rights ( )

list of all possible SQL rights

Definition at line 904 of file m_mysql.php.

904  {
905  return Array('select', 'insert', 'update', 'delete', 'create', 'drop', 'references', 'index', 'alter', 'create_tmp', 'lock', 'create_view', 'show_view', 'create_routine', 'alter_routine', 'execute', 'event', 'trigger');
906  }
m_mysql::change_user_password (   $usern,
  $password,
  $passconf 
)

Change a user's MySQL password.

Parameters
$usernthe username
$passwordThe password for this username
$passconfThe password confirmation
Returns
boolean if the password has been changed in MySQL or FALSE if an error occurred

Definition at line 711 of file m_mysql.php.

References $cuid, $db, $msg, and $password.

711  {
712  global $db, $msg, $cuid, $admin;
713  $msg->log("mysql", "change_user_pass", $usern);
714 
715  $usern = trim($usern);
716  if ($password != $passconf || !$password) {
717  $msg->raise("ERROR", "mysql", _("The passwords do not match"));
718  return false;
719  }
720 
721  // Check this password against the password policy using common API :
722  if (is_callable(array($admin, "checkPolicy"))) {
723  if (!$admin->checkPolicy("mysql", $usern, $password)) {
724  return false; // The error has been raised by checkPolicy()
725  }
726  }
727  $this->dbus->query("SET PASSWORD FOR " . $db->quote($usern) . "@" . $db->quote($this->dbus->Client) . " = PASSWORD(?);", array($password));
728  $db->query("UPDATE dbusers set password= ? where name= ? and uid= ? ;", array($password, $usern, $cuid));
729  return true;
730  }
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
$password
Definition: bootstrap.php:85
m_mysql::del_db (   $dbname)

Delete a database for the current user.

Parameters
$dbnamestring Name of the database to delete. The db name is $user_$dbn
Returns
boolean if the database $user_$db has been successfully deleted, or FALSE if an error occured, such as db does not exist.

Definition at line 312 of file m_mysql.php.

References $cuid, $db, $msg, and del_user().

Referenced by alternc_del_member().

312  {
313  global $db, $msg, $cuid;
314  $msg->log("mysql", "del_db", $dbname);
315  $db->query("SELECT uid FROM db WHERE db= ?;", array($dbname));
316  if (!$db->next_record()) {
317  $msg->raise("ERROR", "mysql", _("The database was not found. I can't delete it"));
318  return false;
319  }
320 
321  // Ok, database exists and dbname is compliant. Let's proceed
322  $db->query("DELETE FROM size_db WHERE db ?;", array($dbname));
323  $db->query("DELETE FROM db WHERE uid= ? AND db= ? ;", array($cuid, $dbname));
324  $this->dbus->query("DROP DATABASE $dbname;");
325 
326  $db_esc = str_replace('_', '\_', $dbname);
327  $this->dbus->query("DELETE FROM mysql.db WHERE Db= ? ;", array($db_esc));
328 
329  // We test if the user created with the database is associated with more than 1 database.
330  $this->dbus->query("select User from mysql.db where User= ? ;", array($dbname));
331  if (($this->dbus->num_rows()) == 0) {
332  // If not we can delete it.
333  $this->del_user($dbname, false, true );
334  }
335  return true;
336  }
del_user($user, $all=false, $caller_is_deldb=false)
Delete a user in MySQL rights tables.
Definition: m_mysql.php:739
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_mysql::del_user (   $user,
  $all = false,
  $caller_is_deldb = false 
)

Delete a user in MySQL rights tables.

Parameters
$userthe username (we will add "[alternc-account]_" to it) to delete
integer$all
Returns
boolean if the user has been deleted in MySQL or FALSE if an error occurred

Definition at line 739 of file m_mysql.php.

References $cuid, $db, $login, $msg, and $user.

Referenced by alternc_del_member(), and del_db().

739  {
740  global $db, $msg, $cuid;
741  $msg->log("mysql", "del_user", $user);
742  if (!preg_match("#^[0-9a-z]#", $user)) {
743  $msg->raise("ERROR", "mysql", _("The username can contain only letters and numbers"));
744  return false;
745  }
746  if (!$all) {
747  $db->query("SELECT name FROM dbusers WHERE name= ? and enable not in ('ADMIN','HIDDEN');", array($user));
748  } else {
749  $db->query("SELECT name FROM dbusers WHERE uid= ? ;", array($cuid));
750  }
751 
752  if (!$db->num_rows()) {
753  if (! $caller_is_deldb )
754  $msg->raise("ERROR", "mysql", _("The username was not found"));
755 
756  return false;
757  }
758  $db->next_record();
759  $login = $db->f("name");
760 
761  // Ok, database exists and dbname is compliant. Let's proceed
762  $this->dbus->query("REVOKE ALL PRIVILEGES ON *.* FROM " . $db->quote($user) . "@" . $db->quote($this->dbus->Client) . ";");
763  $this->dbus->query("DELETE FROM mysql.db WHERE User= ? AND Host= ? ;", array($user, $this->dbus->Client));
764  $this->dbus->query("DELETE FROM mysql.user WHERE User= ? AND Host= ? ;", array($user, $this->dbus->Client));
765  $this->dbus->query("FLUSH PRIVILEGES");
766 
767  $db->query("DELETE FROM dbusers WHERE uid= ? AND name= ? ;", array($cuid, $user));
768 
769  if ( $caller_is_deldb )
770  $msg->raise("INFO", "mysql", _("The user '%s' has been successfully deleted"), $user);
771 
772  return true;
773  }
$user
Definition: bootstrap.php:84
$login
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_mysql::get_db_size (   $dbname)

Get the size of a database.

Parameters
$dbnamename of the database
Returns
integer database size private

Definition at line 549 of file m_mysql.php.

Referenced by get_mysql_details().

549  {
550  $this->dbus->query("SHOW TABLE STATUS FROM $dbname;");
551  $size = 0;
552  while ($this->dbus->next_record()) {
553  $size += $this->dbus->f('Data_length') + $this->dbus->f('Index_length');
554  if ($this->dbus->f('Engine') != 'InnoDB') {
555  $size += $this->dbus->f('Data_free');
556  }
557  }
558  return $size;
559  }
m_mysql::get_dblist ( )

Get the list of the database for the current user.

Returns
array returns an associative array as follow :
"db" => database name "bck" => backup mode for this db "dir" => Backup folder. Returns an array (empty) if no databases

Definition at line 138 of file m_mysql.php.

References $bro, $c, $cuid, $db, $msg, and split_mysql_database_name().

Referenced by alternc_del_member(), get_user_dblist(), and hook_quota_get().

138  {
139  global $db, $msg, $bro, $cuid;
140  $msg->log("mysql", "get_dblist");
141  $db->free();
142  $db->query("SELECT login,pass,db, bck_mode, bck_dir FROM db WHERE uid= ? ORDER BY db;", array($cuid));
143  $c = array();
144  while ($db->next_record()) {
145  list($dbu, $dbn) = split_mysql_database_name($db->f("db"));
146  $c[] = array("db" => $db->f("db"), "name" => $db->f('db'), "bck" => $db->f("bck_mode"), "dir" => $db->f("bck_dir"), "login" => $db->f("login"), "pass" => $db->f("pass"));
147  }
148  return $c;
149  }
$bro
Definition: bootstrap.php:151
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
split_mysql_database_name($dbname)
split mysql database name between username and custom database name
Definition: functions.php:549
$c
Definition: bootstrap.php:47
m_mysql::get_dbus_size (   $db_name,
  $db_host,
  $db_login,
  $db_password,
  $db_client 
)

Return the size of each databases in a SQL Host given in parameter.

Parameters
$db_namethe human name of the host
$db_hostthe host hosting the SQL databases
$db_loginthe login to access the SQL db
$db_passwordthe password to access the SQL db
$db_clientthe client to access the SQL db
Returns
an array associating the name of the databases to their sizes : array(dbname=>size)

Definition at line 1070 of file m_mysql.php.

References $c, $msg, and $res.

1070  {
1071  global $msg;
1072  $msg->log("mysql", "get_dbus_size", $db_host);
1073 
1074  $this->dbus = new DB_Sql("mysql",$db_host,$db_login,$db_password);
1075 
1076  $this->dbus->query("SHOW DATABASES;");
1077  $alldb=array();
1078  while ($this->dbus->next_record()) {
1079  $alldb[] = $this->dbus->f("Database");
1080  }
1081 
1082  $res = array();
1083  foreach($alldb as $dbname) {
1084  $c = $this->dbus->query("SHOW TABLE STATUS FROM $dbname;");
1085  $size = 0;
1086  while ($this->dbus->next_record()) {
1087  $size+=$this->dbus->f("Data_length") + $this->dbus->f("Index_length");
1088  }
1089  $res["$dbname"] = "$size";
1090  }
1091  return $res;
1092  }
$res
Definition: index.php:125
$msg
Definition: config.php:155
$c
Definition: bootstrap.php:47
Mysql Database class.
Definition: db_mysql.php:27
m_mysql::get_defaultsparam (   $dbn)

Definition at line 588 of file m_mysql.php.

References $cuid, $db, $msg, and $r.

588  {
589  global $db, $msg, $cuid;
590  $msg->log("mysql", "getdefaults");
591 
592  $dbu = $dbn;
593  $r = array();
594  $dbn = str_replace('_', '\_', $dbn);
595  $this->dbus->query("Select * from mysql.db where Db= ? and User!= ? ;", array($dbn, $cuid."_myadm"));
596 
597  if (!$this->dbus->num_rows()) {
598  $msg->raise("ERROR", "mysql",_("Database not found"));
599  return false;
600  }
601 
602  $listRights = array('Select', 'Insert', 'Update', 'Delete', 'Create', 'Drop', 'References', 'Index', 'Alter', 'Create_tmp_table', 'Lock_tables', 'Create_view', 'Show_view', 'Create_routine', 'Alter_routine', 'Execute', 'Event', 'Trigger');
603  while ($this->dbus->next_record()) {
604  // rTmp is the array where we put the informations from each loop, added to array $r
605  $rTmp = array();
606  $variable = $this->dbus->Record;
607 
608  $dbu = $variable['User'];
609 
610  $rTmp['Host'] = $this->dbus->f('Host');
611  $rTmp['Rights']='All';
612 
613  foreach ($listRights as $v) {
614  $right = $v."_priv";
615  if ($this->dbus->f($right) !== "Y") {
616  $rTmp['Rights'] = 'NotAll';
617  break;
618  }
619  }
620 
621  if (!$db->query("SELECT name,password from dbusers where name= ? ;", array($dbu))) {
622  $msg->raise("ERROR", "mysql",_("Database not found")." (3)");
623  return false;
624  }
625 
626  if (!$db->num_rows()) {
627  $msg->raise("ERROR", "mysql",_("Database not found")." (4)");
628  return false;
629  }
630 
631  $db->next_record();
632  $rTmp['user'] = $db->f('name');
633  $rTmp['password'] = $db->f('password');
634 
635  $r[] = $rTmp;
636 
637  } // endwhile
638  return $r;
639  }
global $db
Definition: bootstrap.php:22
$r
Definition: aws_add.php:75
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_mysql::get_mysql_details (   $dbn)

Returns the details of a user's database.

$dbn is the name of the database (after the _) or nothing for the database "$user"

Returns
string returns an associative array as follow : "db" => Name of the database "bck" => Current bckup mode "dir" => Backup directory "size" => Size of the database (in bytes) "pass" => Password of the user "history" => Number of backup we keep "gzip" => Does we compress the dumps ? Returns FALSE if the user has no database of if the database does not exist.

Definition at line 188 of file m_mysql.php.

References $cuid, $db, $msg, $root, get_db_size(), getuserpath(), and split_mysql_database_name().

Referenced by restore().

188  {
189  global $db, $msg, $cuid;
190  $root = getuserpath();
191  $msg->log("mysql", "get_mysql_details");
192  $pos = strpos($dbn, '_');
193  if ($pos === false) {
194  $dbname = $dbn;
195  } else {
196  $dbncomp = explode('_', $dbn);
197  $dbname = $dbn;
198  $dbn = $dbncomp[1];
199  }
200  $size = $this->get_db_size($dbname);
201  $db->query("SELECT login,pass,db, bck_mode, bck_gzip, bck_dir, bck_history FROM db WHERE uid= ? AND db= ?;", array($cuid, $dbname));
202  if (!$db->num_rows()) {
203  $msg->raise("ERROR", "mysql", _("Database %s not found"), $dbn);
204  return array("enabled" => false);
205  }
206  $db->next_record();
207  list($dbu, $dbn) = split_mysql_database_name($db->f("db"));
208  return array("enabled" => true, "login" => $db->f("login"), "db" => $db->f("db"), "name" => $dbn, "bck" => $db->f("bck_mode"), "dir" => substr($db->f("bck_dir"), strlen($root)), "size" => $size, "pass" => $db->f("pass"), "history" => $db->f("bck_history"), "gzip" => $db->f("bck_gzip"));
209  }
global $db
Definition: bootstrap.php:22
getuserpath($user=null)
get the home of the user
Definition: functions.php:329
$root
Definition: bootstrap.php:20
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
get_db_size($dbname)
Get the size of a database.
Definition: m_mysql.php:549
split_mysql_database_name($dbname)
split mysql database name between username and custom database name
Definition: functions.php:549
m_mysql::get_user_dblist (   $user)

Return the list of the database rights of user $user.

Parameters
$userthe username
Returns
array An array of database name and rights

Definition at line 781 of file m_mysql.php.

References $db, $msg, $r, $tab, $user, and get_dblist().

781  {
782  global $db, $msg;
783 
784  $this->dbus->query("SELECT * FROM mysql.user WHERE User= ? AND Host= ? ;", array($user, $this->dbus->Client));
785  if (!$this->dbus->next_record()) {
786  $msg->raise("ERROR", 'mysql', _("This user does not exist in the MySQL/User database"));
787  return false;
788  }
789 
790  $r = array();
791  $db->free();
792  $dblist = $this->get_dblist();
793  foreach ($dblist as $tab) {
794  $dbname = str_replace('_', '\_', $tab["db"]);
795  $this->dbus->query("SELECT * FROM mysql.db WHERE User= ? AND Host= ? AND Db= ? ;", array($user, $this->dbus->Client, $dbname));
796  if ($this->dbus->next_record()) {
797  $r[] = array("db" => $tab["db"], "select" => $this->dbus->f("Select_priv"), "insert" => $this->dbus->f("Insert_priv"), "update" => $this->dbus->f("Update_priv"), "delete" => $this->dbus->f("Delete_priv"), "create" => $this->dbus->f("Create_priv"), "drop" => $this->dbus->f("Drop_priv"), "references" => $this->dbus->f("References_priv"), "index" => $this->dbus->f("Index_priv"), "alter" => $this->dbus->f("Alter_priv"), "create_tmp" => $this->dbus->f("Create_tmp_table_priv"), "lock" => $this->dbus->f("Lock_tables_priv"),
798  "create_view" => $this->dbus->f("Create_view_priv"),
799  "show_view" => $this->dbus->f("Show_view_priv"),
800  "create_routine" => $this->dbus->f("Create_routine_priv"),
801  "alter_routine" => $this->dbus->f("Alter_routine_priv"),
802  "execute" => $this->dbus->f("Execute_priv"),
803  "event" => $this->dbus->f("Event_priv"),
804  "trigger" => $this->dbus->f("Trigger_priv")
805  );
806  } else {
807  $r[] = array("db" => $tab['db'], "select" => "N", "insert" => "N", "update" => "N", "delete" => "N", "create" => "N", "drop" => "N", "references" => "N", "index" => "N", "alter" => "N", "create_tmp" => "N", "lock" => "N", "create_view" => "N", "show_view" => "N", "create_routine" => "N", "alter_routine" => "N", "execute" => "N", "event" => "N", "trigger" => "N");
808  }
809  }
810  return $r;
811  }
$user
Definition: bootstrap.php:84
get_dblist()
Get the list of the database for the current user.
Definition: m_mysql.php:138
global $db
Definition: bootstrap.php:22
$r
Definition: aws_add.php:75
$msg
Definition: config.php:155
m_mysql::get_userslist (   $all = null)

Returns the list of database users of an account.

Definition at line 565 of file m_mysql.php.

References $c, $cuid, $db, and $msg.

Referenced by alternc_del_member().

565  {
566  global $db, $msg, $cuid;
567  $msg->log("mysql", "get_userslist");
568  $c = array();
569  if (!$all) {
570  $db->query("SELECT name FROM dbusers WHERE uid= ? and enable not in ('ADMIN','HIDDEN') ORDER BY name;", array($cuid));
571  } else {
572  $db->query("SELECT name FROM dbusers WHERE uid= ? ORDER BY name;", array($cuid));
573  }
574  while ($db->next_record()) {
575  $pos = strpos($db->f("name"), "_");
576  if ($pos === false) {
577  $c[] = array("name" => ($db->f("name")));
578  } else {
579  $c[] = array("name" => ($db->f("name")));
580  //$c[]=array("name"=>substr($db->f("name"),strpos($db->f("name"),"_")+1));
581  }
582  }
583 
584  return $c;
585  }
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
$c
Definition: bootstrap.php:47
m_mysql::grant (   $base,
  $user,
  $rights = null,
  $pass = null,
  $table = '*' 
)

Function used to grant SQL rights to users: :database : database user : rights to apply ( optional, every rights apply given if missing : user password ( optional, if not given the pass stays the same, else it takes the new value ) : sql tables to apply rights.

Definition at line 447 of file m_mysql.php.

References $db, $msg, $user, elseif, and null.

Referenced by add_db(), add_user(), and set_user_rights().

447  {
448  global $msg, $db;
449  $msg->log("mysql", "grant", $base . "-" . $rights . "-" . $user);
450 
451  if (!preg_match("#^[0-9a-z_\\*\\\\]*$#", $base)) {
452  $msg->raise("ERROR", "mysql", _("Database name can contain only letters and numbers"));
453  return false;
454  } elseif (!$this->dbus->query("select db from db where db= ?;", array($base))) {
455  $msg->raise("ERROR", "mysql", _("Database not found"));
456  return false;
457  }
458 
459  if ($rights == null) {
460  $rights = 'ALL PRIVILEGES';
461  } elseif (!preg_match("#^[a-zA-Z,\s]*$#", $rights)) {
462  $msg->raise("ERROR", "mysql", _("Databases rights are not correct"));
463  return false;
464  }
465 
466  if (!preg_match("#^[0-9a-z]#", $user)) {
467  $msg->raise("ERROR", "mysql", _("The username can contain only letters and numbers."));
468  return false;
469  }
470  $db->query("select name from dbusers where name= ? ;", array($user));
471 
472  if (!$db->num_rows()) {
473  $msg->raise("ERROR", "mysql", _("Database user not found"));
474  return false;
475  }
476 
477  $grant = "grant " . $rights . " on `" . $base . "`." . $table . " to " . $db->quote($user) . "@" . $db->quote($this->dbus->Client);
478 
479  if ($pass) {
480  $grant .= " identified by " . $db->quote($pass) . ";";
481  } else {
482  $grant .= ";";
483  }
484 
485  if (!$this->dbus->query($grant)) {
486  $msg->raise("ERROR", "mysql", _("Could not grant rights"));
487  return false;
488  }
489  return true;
490  }
$user
Definition: bootstrap.php:84
global $db
Definition: bootstrap.php:22
PR null
Definition: lang-css.js:1
$msg
Definition: config.php:155
if(empty($site_name)) elseif($piwik->site_add($site_name, $site_urls))
m_mysql::hook_lxc_params (   $params)

Hook function called by the lxc class to set mysql_host and port parameters private.

Definition at line 914 of file m_mysql.php.

References $msg, and $p.

914  {
915  global $msg;
916  $msg->log("mysql", "alternc_get_quota");
917  $p = array();
918  if (isset($this->dbus["Host"]) && $this->dbus["Host"] != "") {
919  $p["mysql_host"] = $this->dbus["Host"];
920  $p["mysql_port"] = 3306;
921  }
922  return $p;
923  }
$msg
Definition: config.php:155
$p
Definition: bro_editor.php:46
m_mysql::hook_menu ( )

Definition at line 92 of file m_mysql.php.

References $q.

92  {
93  global $quota;
94  $q = $quota->getquota("mysql");
95 
96  $obj = array(
97  'title' => _("MySQL"),
98  'ico' => 'images/mysql.png',
99  'link' => 'toggle',
100  'pos' => 100,
101  'links' => array(),
102  );
103 
104  $obj['links'][] = array(
105  'txt' => _("Databases"),
106  'url' => "sql_list.php",
107  );
108  $obj['links'][] = array(
109  'txt' => _("MySQL Users"),
110  'url' => "sql_users_list.php",
111  );
112  if ($q["u"] > 0) {
113  $obj['links'][] = array(
114  'txt' => _("PhpMyAdmin"),
115  'url' => "sql_pma_sso.php",
116  'target' => '_blank',
117  );
118  }
119  return $obj;
120  }
m_mysql::hook_quota_get ( )

Hook function called by the quota class to compute user used quota Returns the used quota for the $name service for the current user.

Parameters
$namestring name of the quota
Returns
integer the number of service used or false if an error occured private

Definition at line 933 of file m_mysql.php.

References $c, $mem, $msg, $q, and get_dblist().

933  {
934  global $msg, $mem, $quota;
935  $msg->log("mysql", "alternc_get_quota");
936  $q = Array("name" => "mysql", "description" => _("MySQL Databases"), "used" => 0);
937  $c = $this->get_dblist();
938  if (is_array($c)) {
939  $q['used'] = count($c);
940  $q['sizeondisk'] = $quota->get_size_db_sum_user($mem->user["login"])/1024;
941  }
942  return $q;
943  }
get_dblist()
Get the list of the database for the current user.
Definition: m_mysql.php:138
$msg
Definition: config.php:155
$c
Definition: bootstrap.php:47
$mem
Definition: bootstrap.php:71
m_mysql::list_db_servers ( )

Definition at line 81 of file m_mysql.php.

References $c, and $db.

81  {
82  global $db;
83  $db->query("select d.*, IFNULL(count(m.uid),0) as nb_users from db_servers d left join membres m on d.id = m.db_server_id group by d.id,m.db_server_id order by d.name,d.id;");
84  $c = array();
85  while ($db->next_record()) {
86  $c[] = $db->Record;
87  }
88  return $c;
89  }
global $db
Definition: bootstrap.php:22
$c
Definition: bootstrap.php:47
m_mysql::php_myadmin_connect ( )

Get the login and password of the special user able to connect to phpmyadmin.

Returns
array returns an associative array with login and password Returns FALSE if error

Definition at line 157 of file m_mysql.php.

References $cuid, $db, $info, and $msg.

157  {
158  global $db, $cuid, $msg;
159  $msg->log("mysql", "php_myadmin_connect");
160  $db->query("SELECT dbu.name,dbu.password, dbs.host FROM dbusers dbu, db_servers dbs, membres m WHERE dbu.uid= ? and enable='ADMIN' and dbs.id=m.db_server_id and m.uid= ? ;", array($cuid, $cuid));
161  if (!$db->num_rows()) {
162  $msg->raise("ERROR", "mysql", _("Cannot connect to PhpMyAdmin"));
163  return false;
164  }
165  $db->next_record();
166  $info = array(
167  "login" => $db->f("name"),
168  "pass" => $db->f("password"),
169  "host" => $db->f("host")
170  );
171  return $info;
172  }
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
m_mysql::put_mysql_backup (   $dbn,
  $bck_mode,
  $bck_history,
  $bck_gzip,
  $bck_dir 
)

Set the backup parameters for the database $db.

Parameters
$dbnstring database name
$bck_modeinteger Backup mode (0 = none 1 = daily 2 = weekly)
$bck_historyinteger How many backup should we keep ?
$bck_gzipboolean shall we compress the backup ?
$bck_dirstring Directory relative to the user account where the backup will be stored
Returns
boolean true if the backup parameters has been successfully changed, false if not.

Definition at line 348 of file m_mysql.php.

References $bro, $cuid, $db, $msg, and variable_get().

348  {
349  global $db, $msg, $bro, $cuid;
350  $msg->log("mysql", "put_mysql_backup");
351 
352  if (!variable_get('sql_allow_users_backups')) {
353  $msg->raise("ERROR", "mysql", _("User aren't allowed to configure their backups"));
354  return false;
355  }
356 
357  $pos = strpos($dbn, '_');
358  if ($pos === false) {
359  $dbname = $dbn;
360  } else {
361  $dbncomp = explode('_', $dbn);
362  $dbname = $dbn;
363  $dbn = $dbncomp[1];
364  }
365  if (!preg_match("#^[0-9a-z]*$#", $dbn)) {
366  $msg->raise("ERROR", "mysql", _("Database name can contain only letters and numbers"));
367  return false;
368  }
369  $db->query("SELECT * FROM db WHERE uid= ? AND db= ? ;", array($cuid, $dbname));
370  if (!$db->num_rows()) {
371  $msg->raise("ERROR", "mysql", _("Database %s not found"), $dbn);
372  return false;
373  }
374  $db->next_record();
375  $bck_mode = intval($bck_mode);
376  $bck_history = intval($bck_history);
377  if ($bck_gzip) {
378  $bck_gzip = "1";
379  } else {
380  $bck_gzip = "0";
381  }
382  if (!$bck_mode) {
383  $bck_mode = "0";
384  }
385  if (!$bck_history) {
386  $msg->raise("ALERT", "mysql", _("You have to choose how many backups you want to keep"));
387  return false;
388  }
389  if (($bck_dir = $bro->convertabsolute($bck_dir, 0)) === false) { // return a full path or FALSE
390  $msg->raise("ERROR", "mysql", _("Directory does not exist"));
391  return false;
392  }
393  $db->query("UPDATE db SET bck_mode= ? , bck_history= ?, bck_gzip= ?, bck_dir= ? WHERE uid= ? AND db= ? ;", array($bck_mode, $bck_history, $bck_gzip, $bck_dir, $cuid, $dbname));
394  return true;
395  }
$bro
Definition: bootstrap.php:151
global $db
Definition: bootstrap.php:22
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
m_mysql::put_mysql_details (   $password)

Change the password of the user in MySQL.

Parameters
$passwordstring new password (cleartext)
Returns
boolean TRUE if the password has been successfully changed, FALSE else.

Definition at line 403 of file m_mysql.php.

References $cuid, $db, $len, $login, $msg, $password, and variable_get().

403  {
404  global $db, $msg, $cuid, $admin;
405  $msg->log("mysql", "put_mysql_details");
406  $db->query("SELECT * FROM db WHERE uid= ?;", array($cuid));
407  if (!$db->num_rows()) {
408  $msg->raise("ERROR", "mysql", _("Database not found"));
409  return false;
410  }
411  $db->next_record();
412  $login = $db->f("login");
413 
414  if (!$password) {
415  $msg->raise("ERROR", "mysql", _("The password is mandatory"));
416  return false;
417  }
418 
419  $len=variable_get("sql_max_username_length", 16);
420  if (strlen($password) > $len) {
421  $msg->raise("ERROR", "mysql", _("MySQL password cannot exceed %d characters"), $len);
422  return false;
423  }
424 
425  // Check this password against the password policy using common API :
426  if (is_callable(array($admin, "checkPolicy"))) {
427  if (!$admin->checkPolicy("mysql", $login, $password)) {
428  return false; // The error has been raised by checkPolicy()
429  }
430  }
431 
432  // Update all the "pass" fields for this user :
433  $db->query("UPDATE db SET pass= ? WHERE uid= ?;", array($password, $cuid));
434  $this->dbus->query("SET PASSWORD FOR " .$login . "@" . $this->dbus->Client . " = PASSWORD(?);", array($password));
435  return true;
436  }
$login
global $db
Definition: bootstrap.php:22
$len
$msg
Definition: config.php:155
$cuid
Definition: bootstrap.php:43
$password
Definition: bootstrap.php:85
variable_get($name, $default=null, $createit_comment=null)
Return a persistent variable.
Definition: variables.php:85
m_mysql::reload_dbus ( )

Definition at line 76 of file m_mysql.php.

76  {
77  $this->dbus = new DB_users();
78  }
MySQL user database management for AlternC.
Definition: m_mysql.php:27
m_mysql::restore (   $file,
  $stdout,
  $id 
)

Restore a sql database.

Parameters
$filestring The filename, relative to the user root dir, which contains a sql dump
$stdoutboolean shall-we dump the error to stdout ?
$idinteger The ID of the database to dump to.
Returns
boolean TRUE if the database has been restored, or FALSE if an error occurred

Definition at line 500 of file m_mysql.php.

References $bro, $id, $msg, $r, elseif, and get_mysql_details().

500  {
501  global $msg, $bro;
502  if (empty($file)) {
503  $msg->raise("ERROR", "mysql", _("No file specified"));
504  return false;
505  }
506  if (!$r = $this->get_mysql_details($id)) {
507  return false;
508  }
509  if (!($fi = $bro->convertabsolute($file, 0))) {
510  $msg->raise("ERROR", "mysql", _("File not found"));
511  return false;
512  }
513  if (!file_exists($fi)) {
514  $msg->raise("ERROR", "mysql", _("File not found"));
515  return false;
516  }
517 
518  if (substr($fi, -3) == ".gz") {
519  $exe = "/bin/gzip -d -c <" . escapeshellarg($fi) . " | /usr/bin/mysql -h" . escapeshellarg($this->dbus->Host) . " -u" . escapeshellarg($r["login"]) . " -p" . escapeshellarg($r["pass"]) . " " . escapeshellarg($r["db"]);
520  } elseif (substr($fi, -4) == ".bz2") {
521  $exe = "/usr/bin/bunzip2 -d -c <" . escapeshellarg($fi) . " | /usr/bin/mysql -h" . escapeshellarg($this->dbus->Host) . " -u" . escapeshellarg($r["login"]) . " -p" . escapeshellarg($r["pass"]) . " " . escapeshellarg($r["db"]);
522  } else {
523  $exe = "/usr/bin/mysql -h" . escapeshellarg($this->dbus->Host) . " -u" . escapeshellarg($r["login"]) . " -p" . escapeshellarg($r["pass"]) . " " . escapeshellarg($r["db"]) . " <" . escapeshellarg($fi);
524  }
525  $exe .= " 2>&1";
526 
527  echo "<code><pre>";
528  $ret = 0;
529  if ($stdout) {
530  passthru($exe, $ret);
531  } else {
532  exec($exe, $ret);
533  }
534  echo "</pre></code>";
535  if ($ret != 0) {
536  return false;
537  } else {
538  return true;
539  }
540  }
$bro
Definition: bootstrap.php:151
$r
Definition: aws_add.php:75
$msg
Definition: config.php:155
if(empty($site_name)) elseif($piwik->site_add($site_name, $site_urls))
get_mysql_details($dbn)
Returns the details of a user's database.
Definition: m_mysql.php:188
if(!isset($is_include)) if(!$key &&!$crt) $id
m_mysql::set_user_rights (   $user,
  $dbn,
  $rights 
)

Set the access rights of user $user to database $dbn to be rights $rights.

Parameters
$userthe username to give rights to
$dbnThe database to give rights to
$rightsThe rights as an array of MySQL keywords (insert, select ...)
Returns
boolean TRUE if the rights has been applied or FALSE if an error occurred

Definition at line 821 of file m_mysql.php.

References $i, $msg, $user, and grant().

821  {
822  global $msg;
823  $msg->log("mysql", "set_user_rights");
824 
825  // We generate the rights array depending on the rights list:
826  $strrights = "";
827  for ($i = 0; $i < count($rights); $i++) {
828  switch ($rights[$i]) {
829  case "select":
830  $strrights.="SELECT,";
831  break;
832  case "insert":
833  $strrights.="INSERT,";
834  break;
835  case "update":
836  $strrights.="UPDATE,";
837  break;
838  case "delete":
839  $strrights.="DELETE,";
840  break;
841  case "create":
842  $strrights.="CREATE,";
843  break;
844  case "drop":
845  $strrights.="DROP,";
846  break;
847  case "references":
848  $strrights.="REFERENCES,";
849  break;
850  case "index":
851  $strrights.="INDEX,";
852  break;
853  case "alter":
854  $strrights.="ALTER,";
855  break;
856  case "create_tmp":
857  $strrights.="CREATE TEMPORARY TABLES,";
858  break;
859  case "lock":
860  $strrights.="LOCK TABLES,";
861  break;
862  case "create_view":
863  $strrights.="CREATE VIEW,";
864  break;
865  case "show_view":
866  $strrights.="SHOW VIEW,";
867  break;
868  case "create_routine":
869  $strrights.="CREATE ROUTINE,";
870  break;
871  case "alter_routine":
872  $strrights.="ALTER ROUTINE,";
873  break;
874  case "execute":
875  $strrights.="EXECUTE,";
876  break;
877  case "event":
878  $strrights.="EVENT,";
879  break;
880  case "trigger":
881  $strrights.="TRIGGER,";
882  break;
883  }
884  }
885 
886  // We reset all user rights on this DB :
887  $dbname = str_replace('_', '\_', $dbn);
888  $this->dbus->query("SELECT * FROM mysql.db WHERE User = ? AND Db = ?;", array($user, $dbname));
889 
890  if ($this->dbus->num_rows()) {
891  $this->dbus->query("REVOKE ALL PRIVILEGES ON `".$dbname."`.* FROM ".$this->dbus->quote($user)."@" . $this->dbus->quote($this->dbus->Client) . ";");
892  }
893  if ($strrights) {
894  $strrights = substr($strrights, 0, strlen($strrights) - 1);
895  $this->grant($dbname, $user, $strrights);
896  }
897  $this->dbus->query("FLUSH PRIVILEGES");
898  return TRUE;
899  }
$user
Definition: bootstrap.php:84
$i
$msg
Definition: config.php:155
grant($base, $user, $rights=null, $pass=null, $table= '*')
Function used to grant SQL rights to users: :database : database user : rights to apply ( optional...
Definition: m_mysql.php:447

Member Data Documentation

m_mysql::$dbus

Definition at line 60 of file m_mysql.php.


The documentation for this class was generated from the following file: