Alternc  latest
Alternc logiel libre pour l'hébergement
Domain.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Domain Api of AlternC, used by alternc-api package
5  */
7 
8  protected $dom; // m_dom instance
9 
10  function __construct($service) {
11  global $dom;
12  parent::__construct($service);
13  $this->dom = $dom;
14  }
15 
16  /** API Method from legacy class method dom->get_domain_list()
17  * @param $options a hash with parameters transmitted to legacy call
18  * may be "uid" to only return domains for a specific user-id
19  * (if you are not admin, this WILL only list YOUR domains anyway)
20  * may be "offset" and/or "count" to do paging.
21  * @return Alternc_Api_Response whose content is the list of hosted domains on this server
22  * (no more details as of now)
23  */
24  function find($options) {
25  global $cuid;
26  $sql = "";
27  if ($this->isAdmin) {
28  if (isset($options["uid"])) {
29  $uid = intval($options["uid"]);
30  } else {
31  $uid = -1;
32  }
33  } else {
34  $uid = $cuid;
35  }
36  if ($uid != -1) {
37  $sql = " WHERE compte=$uid ";
38  } else {
39  $sql = "";
40  }
41  $stmt = $this->db->prepare("SELECT * FROM domaines $sql ORDER BY domaine");
42  $stmt->execute();
43  $result = array();
44  while ($me = $stmt->fetch(PDO::FETCH_OBJ)) {
45  $result[$me->domaine] = $me;
46  }
47  list($offset, $count) = $this->offsetAndCount($options, count($result));
48  if ($offset != -1 || $count != -1) {
49  $result = array_slice($result, $offset, $count);
50  }
51  return new Alternc_Api_Response(array("content" => $result));
52  }
53 
54  /** API Method from legacy class method dom->get_domain_all($dom)
55  * @param $options a hash with parameters transmitted to legacy call
56  * musr be the domain name $dom
57  * @return Alternc_Api_Response whose content is the list of domain info and subdomains
58  */
59  function get($options) {
60  global $cuid;
61  if ($this->isAdmin) {
62  if (isset($options["uid"])) {
63  $cuid = intval($options["uid"]);
64  }
65  }
66  $mandatory = array("dom");
67  $missing = "";
68  foreach ($mandatory as $key) {
69  if (!isset($options[$key])) {
70  $missing.=$key . " ";
71  }
72  }
73  if ($missing) {
74  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
75  }
76  $this->dom->lock();
77  $did = $this->dom->get_domain_all($options["dom"]);
78  $this->dom->unlock();
79  if (!$did) {
80  return $this->alterncLegacyErrorManager();
81  } else {
82  return new Alternc_Api_Response(array("content" => $did));
83  }
84  }
85 
86  /** API Method from legacy class method dom->add_domain()
87  * @param $options a hash with parameters transmitted to legacy call
88  * mandatory parameters: domain(str), dns(bool)
89  * non-mandatory: noerase(bool, only admins), force(bool, only admins), isslave(bool), slavedom(str)
90  * @return Alternc_Api_Response whose content is the newly created DOMAIN id
91  */
92  function add($options) {
93  $mandatory = array("domain", "dns");
94  $defaults = array("noerase" => false, "force" => false, "isslave" => false, "slavedom" => "");
95  $missing = "";
96  foreach ($mandatory as $key) {
97  if (!isset($options[$key])) {
98  $missing.=$key . " ";
99  }
100  }
101  if ($missing) {
102  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
103  }
104  foreach ($defaults as $key => $value) {
105  if (!isset($options[$key])) {
106  $options[$key] = $value;
107  }
108  }
109  if (!$this->isAdmin) { // only admin can change the options below:
110  $options["noerase"] = false;
111  $options["force"] = false;
112  }
113  $did = $this->dom->add_domain($options["domain"], $options["dns"], $options["noerase"], $options["force"], $options["isslave"], $options["slavedom"]);
114  if (!$did) {
115  return $this->alterncLegacyErrorManager();
116  } else {
117  return new Alternc_Api_Response(array("content" => $did));
118  }
119  }
120 
121  /** API Method from legacy class method dom->edit_domain()
122  * @param $options a hash with parameters transmitted to legacy call
123  * mandatory parameters: domain(str), dns(bool)
124  * non-mandatory: noerase(bool, only admins), force(bool, only admins), isslave(bool), slavedom(str)
125  * @return Alternc_Api_Response whose content is the newly created DOMAIN id
126  */
127  function update($options) {
128  $mandatory = array("domain", "dns", "gesmx");
129  $defaults = array("force" => false, "ttl" => 86400);
130  $missing = "";
131  foreach ($mandatory as $key) {
132  if (!isset($options[$key])) {
133  $missing.=$key . " ";
134  }
135  }
136  if ($missing) {
137  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_ARGUMENT, "message" => "Missing or invalid argument: " . $missing));
138  }
139  foreach ($defaults as $key => $value) {
140  if (!isset($options[$key])) {
141  $options[$key] = $value;
142  }
143  }
144  if (!$this->isAdmin) { // only admin can change the options below:
145  $options["force"] = false;
146  }
147  $did = $this->dom->edit_domain($options["domain"], $options["dns"], $options["gesmx"], $options["force"], $options["ttl"]);
148  if (!$did) {
149  return $this->alterncLegacyErrorManager();
150  } else {
151  return new Alternc_Api_Response(array("content" => $did));
152  }
153  }
154 
155  /** API Method from legacy class method dom->del_domain()
156  * @param $options a hash with parameters transmitted to legacy call
157  * mandatory parameters: domain
158  * @return Alternc_Api_Response TRUE if the domain has been marked for deletion.
159  */
160  function del($options) {
161  if (!isset($options["domain"])) {
162  return new Alternc_Api_Response(array("code" => self::ERR_ALTERNC_FUNCTION, "message" => "Missing or invalid argument: DOMAIN"));
163  }
164  $result = $this->dom->del_domain($options["domain"]);
165  if (!$result) {
166  return $this->alterncLegacyErrorManager();
167  } else {
168  return new Alternc_Api_Response(array("content" => true));
169  }
170  }
171 
172 }
173 
174 // class Alternc_Api_Object_Domain
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.
Domain Api of AlternC, used by alternc-api package.
Definition: Domain.php:6
update($options)
API Method from legacy class method dom->edit_domain()
Definition: Domain.php:127
del($options)
API Method from legacy class method dom->del_domain()
Definition: Domain.php:160
find($options)
API Method from legacy class method dom->get_domain_list()
Definition: Domain.php:24
add($options)
API Method from legacy class method dom->add_domain()
Definition: Domain.php:92
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