db.inc.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Database (MySQL) list management class
  4. *
  5. * @package siteadm
  6. */
  7. class db_manager extends db_object_manager
  8. {
  9. static protected $name = "db";
  10. }
  11. /**
  12. * User Database (MySQL)
  13. *
  14. * @package siteadm
  15. */
  16. class db extends db_object
  17. {
  18. static protected $_name = "db";
  19. static protected $_db_table = "db";
  20. static public $_f = array
  21. (
  22. "account_id" => array("type"=>"object", "otype"=>"account"),
  23. "dbname" => array("type"=>"string", "nonempty"=>true),
  24. "username" => array("type"=>"string", "nonempty"=>true),
  25. "password" => array("type"=>"string"),
  26. "quota" => array("type"=>"select", "list"=>array("10", "100", "1000", "10000")),
  27. "max_queries" => array("type"=>"numeric", "default"=>MYSQL_MAX_QUERIES),
  28. "max_user_connections" => array("type"=>"numeric", "default"=>MYSQL_MAX_USER_CONNECTIONS),
  29. "max_connections" => array("type"=>"numeric", "default"=>MYSQL_MAX_CONNECTIONS),
  30. "max_updates" => array("type"=>"numeric", "default"=>MYSQL_MAX_UPDATES),
  31. );
  32. // ACCESS
  33. /**
  34. * @return account
  35. */
  36. public function account()
  37. {
  38. return account()->get($this->account_id);
  39. }
  40. // PERM
  41. /**
  42. * @see db_object::insert_perm()
  43. */
  44. static public function insert_perm()
  45. {
  46. // Admin
  47. if (login()->perm("admin"))
  48. {
  49. return "admin";
  50. }
  51. // Manager
  52. elseif (login()->perm("manager"))
  53. {
  54. return "manager";
  55. }
  56. // User
  57. elseif (login()->id)
  58. {
  59. return "user";
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. }
  66. /**
  67. * @see db_object::update_perm()
  68. */
  69. public function update_perm()
  70. {
  71. // Admin
  72. if (login()->perm("admin"))
  73. {
  74. return "admin";
  75. }
  76. // Account Manager
  77. elseif (($account=$this->account()) && $account->manager_id == login()->id)
  78. {
  79. return "manager";
  80. }
  81. // User
  82. elseif ($this->account_id == login()->id)
  83. {
  84. return "user";
  85. }
  86. else
  87. {
  88. return false;
  89. }
  90. }
  91. // UPDATE
  92. /**
  93. * @see db_object::insert($infos)
  94. */
  95. public function insert($infos)
  96. {
  97. if (!($perm=static::insert_perm()) || !is_array($infos))
  98. return false;
  99. if ($perm != "admin" && ($perm != "manager" || !isset($infos["account_id"]) || !($account=account($infos["account_id"])) || $account->manager_id != login()->id))
  100. {
  101. $infos["account_id"] = login()->id;
  102. }
  103. return db_object::insert($infos);
  104. }
  105. /**
  106. * @see db_object::insert($update)
  107. */
  108. public function update($infos)
  109. {
  110. if (!($perm=$this->update_perm()) || !is_array($infos))
  111. return false;
  112. if ($infos["account_id"] && $perm != "admin" && ($perm != "manager" || !($account=account($infos["account_id"])) || $account->manager_id != login()->id))
  113. {
  114. unset($infos["account_id"]);
  115. }
  116. return db_object::update($infos);
  117. }
  118. // ROOT SCRIPTS
  119. function script_insert()
  120. {
  121. // Création user
  122. mysql_query("CREATE USER '$this->username'@'localhost' IDENTIFIED BY '$this->password'");
  123. echo mysql_error();
  124. // Création table
  125. mysql_query("CREATE DATABASE `$this->dbname`");
  126. // Droits de base pour user
  127. mysql_query("GRANT USAGE ON *.* TO '$this->username'@'localhost' IDENTIFIED BY '$this->password' WITH MAX_QUERIES_PER_HOUR $this->max_queries MAX_CONNECTIONS_PER_HOUR $this->max_connections MAX_UPDATES_PER_HOUR $this->max_updates MAX_USER_CONNECTIONS $this->max_user_connections; ");
  128. // Droits spécifiques pour user
  129. mysql_query("GRANT ALL PRIVILEGES ON `$this->dbname`. * TO '$this->username'@'localhost'");
  130. return true;
  131. }
  132. function script_update()
  133. {
  134. mysql_query("SET PASSWORD FOR '$this->username'@'localhost' = PASSWORD('$this->password')");
  135. return true;
  136. }
  137. function script_delete()
  138. {
  139. // Suppression privilèges
  140. $query_string = "REVOKE ALL PRIVILEGES, GRANT OPTION FROM '$this->username'@'localhost'";
  141. mysql_query($query_string);
  142. // Suppression user
  143. $query_string = "DROP USER '$this->username'@'localhost'";
  144. mysql_query($query_string);
  145. // Suppression table
  146. $query_string = "DROP DATABASE `$this->dbname`";
  147. mysql_query($query_string);
  148. return true;
  149. }
  150. }
  151. ?>