Alternc  latest
Alternc logiel libre pour l'hébergement
Mysql.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Account Api of AlternC, used by alternc-api package
5  */
7 
8  protected $mysql;
9 
10  function __construct($service) {
11  global $mysql;
12  parent::__construct($service);
13  $this->mysql = $mysql;
14  }
15 
16  /** API Method from legacy class method admin->add_mem()
17  * @param $options a hash with parameters transmitted to legacy call
18  * mandatory parameters: login, pass, nom, prenom, mail,
19  * non-mandatory: canpass, type, duration, notes, force, create_dom, db_server_id
20  * @return Alternc_Api_Response whose content is the newly created UID
21  */
22  function add($options) {
23  $mandatory = array("login", "pass", "nom", "prenom", "mail");
24  $defaults = array("canpass" => 1, "type" => "default", "duration" => 0, "notes" => "", "force" => 0, "create_dom" => "");
25  $missing = "";
26  foreach ($mandatory as $key) {
27  if (!isset($options[$key])) {
28  $missing.=$key . " ";
29  }
30  }
31  if ($missing) {
32  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
33  }
34  foreach ($defaults as $key => $value) {
35  if (!isset($options[$key])) {
36  $options[$key] = $value;
37  }
38  }
39  if (!isset($options["db_server_id"])) {
40  $stmt = $this->db->prepare("SELECT MIN(db_servers.id) AS id FROM db_servers;");
41  $stmt->execute();
42  $me = $stmt->fetch(PDO::FETCH_OBJ);
43  $options["db_server_id"] = $me->id;
44  }
45  $uid = $this->admin->add_mem($options["login"], $options["pass"], $options["nom"], $options["prenom"], $options["mail"], $options["canpass"], $options["type"], $options["duration"], $options["notes"], $options["force"], $options["create_dom"], $options["db_server_id"]);
46  if (!$uid) {
47  return $this->alterncLegacyErrorManager();
48  } else {
49  return new Alternc_Api_Response(array("content" => $uid));
50  }
51  }
52 
53  /** API Method from legacy class method admin->update_mem()
54  * @param $options a hash with parameters transmitted to legacy call
55  * mandatory parameters: nom, prenom, mail,
56  * non-mandatory: pass, canpass, type, duration, notes, force, create_dom, db_server_id
57  * @return Alternc_Api_Response whose content is the updated UID
58  */
59  function update($options) {
60  $defaults = array("nom", "prenom", "mail", "canpass", "enabled", "type", "duration", "notes");
61  if (!isset($options["uid"])) {
62  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: UID"));
63  }
64  $uid = intval($options["uid"]);
65  $old = $this->admin->get($uid);
66  if (!$old) {
67  return new Alternc_Api_Response(array("code" => self::ERR_NOT_FOUND, "message" => "User not found"));
68  }
69 
70  foreach ($defaults as $key) {
71  if (!isset($options[$key])) {
72  $options[$key] = $old[$key];
73  }
74  }
75  if (!isset($options["pass"]))
76  $options["pass"] = "";
77  $uid = $this->admin->update_mem($uid, $options["mail"], $options["nom"], $options["prenom"], $options["pass"], $options["enabled"], $options["canpass"], $options["type"], $options["duration"], $options["notes"]);
78  if (!$uid) {
79  return $this->alterncLegacyErrorManager();
80  } else {
81  return new Alternc_Api_Response(array("content" => $uid));
82  }
83  }
84 
85  /** API Method from legacy class method admin->del_mem()
86  * @param $options a hash with parameters transmitted to legacy call
87  * mandatory parameters: uid
88  * @return Alternc_Api_Response TRUE if the account has been deleted.
89  */
90  function del($options) {
91  if (!isset($options["uid"])) {
92  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
93  }
94  $result = $this->admin->del_mem(intval($options["uid"]));
95  if (!$result) {
96  return $this->alterncLegacyErrorManager();
97  } else {
98  return new Alternc_Api_Response(array("content" => true));
99  }
100  }
101 
102  /** API Method from legacy class method admin->lock_mem()
103  * @param $options a hash with parameters transmitted to legacy call
104  * mandatory parameters: uid
105  * @return Alternc_Api_Response TRUE if the account has been locked
106  */
107  function lock($options) {
108  if (!isset($options["uid"])) {
109  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
110  }
111  $result = $this->admin->lock_mem(intval($options["uid"]));
112  if (!$result) {
113  return $this->alterncLegacyErrorManager();
114  } else {
115  return new Alternc_Api_Response(array("content" => true));
116  }
117  }
118 
119  /** API Method from legacy class method admin->unlock_mem()
120  * @param $options a hash with parameters transmitted to legacy call
121  * mandatory parameters: uid
122  * @return Alternc_Api_Response TRUE if the account has been unlocked
123  */
124  function unlock($options) {
125  if (!isset($options["uid"])) {
126  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
127  }
128  $result = $this->admin->unlock_mem(intval($options["uid"]));
129  if (!$result) {
130  return $this->alterncLegacyErrorManager();
131  } else {
132  return new Alternc_Api_Response(array("content" => true));
133  }
134  }
135 
136  /** API Method from legacy class method admin->normal2su()
137  * @param $options a hash with parameters transmitted to legacy call
138  * mandatory parameters: uid
139  * @return Alternc_Api_Response TRUE if the account has been set to be an administator
140  */
141  function setAdmin($options) {
142  if (!isset($options["uid"])) {
143  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
144  }
145  $result = $this->admin->normal2su(intval($options["uid"]));
146  if (!$result) {
147  return $this->alterncLegacyErrorManager();
148  } else {
149  return new Alternc_Api_Response(array("content" => true));
150  }
151  }
152 
153  /** API Method from legacy class method admin->su2normal()
154  * @param $options a hash with parameters transmitted to legacy call
155  * mandatory parameters: uid
156  * @return Alternc_Api_Response TRUE if the account has been set to NOT be an administrator
157  */
158  function unsetAdmin($options) {
159  if (!isset($options["uid"])) {
160  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: UID"));
161  }
162  $result = $this->admin->su2normal(intval($options["uid"]));
163  if (!$result) {
164  return $this->alterncLegacyErrorManager();
165  } else {
166  return new Alternc_Api_Response(array("content" => true));
167  }
168  }
169 
170  /** API Method from legacy class method admin->get_list()
171  * @param $options a hash with parameters transmitted to legacy call
172  * non-mandatory parameters: ONE OF:
173  * uid(strict), login(like %%), domain(like %%), creator(strict, by uid),
174  * Any of: offset(int=0), count(int=+inf)
175  * @return Alternc_Api_Response An array with all matching users informations as hashes
176  */
177  function find($options) {
178  $result = false;
179  if (!$result && isset($options["uid"])) {
180  $result = $this->admin->get(intval($options["uid"]));
181  if ($result)
182  $result = array($result);
183  }
184  if (!$result && isset($options["login"])) {
185  $result = $this->admin->get_list(1/* ALL */, "", $options["login"], "login");
186  }
187  if (!$result && isset($options["domain"])) {
188  $result = $this->admin->get_list(1/* ALL */, "", $options["domain"], "domaine");
189  }
190  if (!$result && isset($options["creator"])) {
191  $result = $this->admin->get_list(1/* ALL */, intval($options["creator"]));
192  }
193  if (!$result) { // everybody
194  $result = $this->admin->get_list(1/* ALL */, "");
195  }
196 
197  if (!$result) {
198  return $this->alterncLegacyErrorManager();
199  } else {
200  list($offset, $count) = $this->offsetAndCount($options, count($result));
201  if ($offset != -1 || $count != -1) {
202  $result = array_slice($result, $offset, $count);
203  }
204  return new Alternc_Api_Response(array("content" => $result));
205  }
206  }
207 
208 }
209 
210 // class Alternc_Api_Object_Mysql
Any Legacy AlternC Api should use this class as a parent to be able to handle properly the access rig...
Definition: Legacyobject.php:9
alterncLegacyErrorManager()
return a proper Alternc_Api_Response from an error class and error string from AlternC legacy class
offsetAndCount($options, $max)
ensure that offset & count are set properly from $options.
Account Api of AlternC, used by alternc-api package.
Definition: Mysql.php:6
unlock($options)
API Method from legacy class method admin->unlock_mem()
Definition: Mysql.php:124
find($options)
API Method from legacy class method admin->get_list()
Definition: Mysql.php:177
lock($options)
API Method from legacy class method admin->lock_mem()
Definition: Mysql.php:107
update($options)
API Method from legacy class method admin->update_mem()
Definition: Mysql.php:59
setAdmin($options)
API Method from legacy class method admin->normal2su()
Definition: Mysql.php:141
unsetAdmin($options)
API Method from legacy class method admin->su2normal()
Definition: Mysql.php:158
del($options)
API Method from legacy class method admin->del_mem()
Definition: Mysql.php:90
add($options)
API Method from legacy class method admin->add_mem()
Definition: Mysql.php:22
__construct($service)
Definition: Mysql.php:10
Standard Response object for the AlternC API.
Definition: Response.php:7
$value
$uid
if(empty($_POST['key'])||empty($_POST['val'])) $key
Definition: tempovars.php:14