1 | <?php |
---|
2 | |
---|
3 | define('OG_REST_URL', 'http://127.0.0.1:8888/'); |
---|
4 | |
---|
5 | define('GET', 1); |
---|
6 | define('POST', 2); |
---|
7 | define('CUSTOM', 3); |
---|
8 | |
---|
9 | define('OG_REST_CMD_CLIENTS', 'clients'); |
---|
10 | define('OG_REST_CMD_WOL', 'wol'); |
---|
11 | define('OG_REST_CMD_SESSION', 'session'); |
---|
12 | define('OG_REST_CMD_RUN', 'shell/run'); |
---|
13 | define('OG_REST_CMD_OUTPUT', 'shell/output'); |
---|
14 | define('OG_REST_CMD_POWEROFF', 'poweroff'); |
---|
15 | define('OG_REST_CMD_REBOOT', 'reboot'); |
---|
16 | define('OG_REST_CMD_STOP', 'stop'); |
---|
17 | define('OG_REST_CMD_REFRESH', 'refresh'); |
---|
18 | define('OG_REST_CMD_HARDWARE', 'hardware'); |
---|
19 | define('OG_REST_CMD_SOFTWARE', 'software'); |
---|
20 | |
---|
21 | define('OG_REST_PARAM_CLIENTS', 'clients'); |
---|
22 | define('OG_REST_PARAM_ADDR', 'addr'); |
---|
23 | define('OG_REST_PARAM_MAC', 'mac'); |
---|
24 | define('OG_REST_PARAM_DISK', 'disk'); |
---|
25 | define('OG_REST_PARAM_PART', 'partition'); |
---|
26 | define('OG_REST_PARAM_RUN', 'run'); |
---|
27 | define('OG_REST_PARAM_TYPE', 'type'); |
---|
28 | define('OG_REST_PARAM_STATE', 'state'); |
---|
29 | |
---|
30 | $conf_file = parse_ini_file(__DIR__ . '/../../etc/ogAdmRepo.cfg'); |
---|
31 | define('OG_REST_API_TOKEN', 'Authorization: ' . $conf_file['ApiToken']); |
---|
32 | |
---|
33 | function common_request($command, $type, $data = null) { |
---|
34 | |
---|
35 | $json = json_encode($data); |
---|
36 | |
---|
37 | $service_url = OG_REST_URL.$command; |
---|
38 | |
---|
39 | $curl = curl_init($service_url); |
---|
40 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
---|
41 | curl_setopt($curl, CURLOPT_HTTPHEADER, array( |
---|
42 | OG_REST_API_TOKEN, |
---|
43 | )); |
---|
44 | |
---|
45 | switch ($type) { |
---|
46 | default: |
---|
47 | case GET: |
---|
48 | break; |
---|
49 | case POST: |
---|
50 | curl_setopt($curl, CURLOPT_POST, true); |
---|
51 | curl_setopt($curl, CURLOPT_POSTFIELDS, $json); |
---|
52 | } |
---|
53 | |
---|
54 | $curl_response = curl_exec($curl); |
---|
55 | $info = curl_getinfo($curl); |
---|
56 | |
---|
57 | if ($curl_response === false || $info['http_code'] != 200) { |
---|
58 | syslog(LOG_ERR, 'error occured during curl exec. Additioanl info: ' . var_export($info)); |
---|
59 | return 0; |
---|
60 | } |
---|
61 | |
---|
62 | curl_close($curl); |
---|
63 | |
---|
64 | syslog(LOG_INFO, 'response '.$command.' ok!'); |
---|
65 | |
---|
66 | return json_decode($curl_response, true); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | function shell($case, $string_ips, $command) { |
---|
71 | |
---|
72 | $ips = explode(';',$string_ips); |
---|
73 | |
---|
74 | switch ($case) { |
---|
75 | case 1: |
---|
76 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
77 | OG_REST_PARAM_RUN => $command); |
---|
78 | $command = OG_REST_CMD_RUN; |
---|
79 | break; |
---|
80 | default: |
---|
81 | case 2: |
---|
82 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
83 | $command = OG_REST_CMD_OUTPUT; |
---|
84 | } |
---|
85 | |
---|
86 | $result = common_request($command, POST, |
---|
87 | $data)[OG_REST_PARAM_CLIENTS][0]['output']; |
---|
88 | |
---|
89 | return (is_null($result) ? '1' : $result); |
---|
90 | } |
---|
91 | |
---|
92 | function clients($case, $ips) { |
---|
93 | |
---|
94 | switch ($case) { |
---|
95 | case 1: |
---|
96 | $type = POST; |
---|
97 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
98 | break; |
---|
99 | case 2: |
---|
100 | $type = GET; |
---|
101 | break; |
---|
102 | } |
---|
103 | |
---|
104 | $result = common_request(OG_REST_CMD_CLIENTS, $type, $data); |
---|
105 | |
---|
106 | foreach ($result[OG_REST_PARAM_CLIENTS] as $client) { |
---|
107 | $trama_notificacion = $trama_notificacion.$client[OG_REST_PARAM_ADDR].'/'. |
---|
108 | $client[OG_REST_PARAM_STATE].';'; |
---|
109 | } |
---|
110 | |
---|
111 | return $trama_notificacion; |
---|
112 | } |
---|
113 | |
---|
114 | function wol($type_wol, $macs, $ips) { |
---|
115 | |
---|
116 | switch ($type_wol) { |
---|
117 | default: |
---|
118 | case 1: |
---|
119 | $wol = 'broadcast'; |
---|
120 | break; |
---|
121 | case 2: |
---|
122 | $wol = 'unicast'; |
---|
123 | } |
---|
124 | |
---|
125 | $clients = array(); |
---|
126 | |
---|
127 | for($i=0; $i<count($macs); $i++) { |
---|
128 | $clients[] = array(OG_REST_PARAM_ADDR => $ips[$i], |
---|
129 | OG_REST_PARAM_MAC => $macs[$i]); |
---|
130 | } |
---|
131 | |
---|
132 | $data = array(OG_REST_PARAM_TYPE => $wol, |
---|
133 | OG_REST_PARAM_CLIENTS => $clients); |
---|
134 | |
---|
135 | common_request(OG_REST_CMD_WOL, POST, $data); |
---|
136 | } |
---|
137 | |
---|
138 | function session($string_ips, $params) { |
---|
139 | |
---|
140 | preg_match_all('!\d{1}!', $params, $matches); |
---|
141 | |
---|
142 | $ips = explode(';',$string_ips); |
---|
143 | $disk = $matches[0][0]; |
---|
144 | $part = $matches[0][1]; |
---|
145 | |
---|
146 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
147 | OG_REST_PARAM_DISK => $disk, OG_REST_PARAM_PART => $part); |
---|
148 | |
---|
149 | common_request(OG_REST_CMD_SESSION, POST, $data); |
---|
150 | } |
---|
151 | |
---|
152 | function poweroff($string_ips) { |
---|
153 | |
---|
154 | $ips = explode(';',$string_ips); |
---|
155 | |
---|
156 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
157 | |
---|
158 | common_request(OG_REST_CMD_POWEROFF, POST, $data); |
---|
159 | } |
---|
160 | |
---|
161 | function reboot($string_ips) { |
---|
162 | |
---|
163 | $ips = explode(';',$string_ips); |
---|
164 | |
---|
165 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
166 | |
---|
167 | common_request(OG_REST_CMD_REBOOT, POST, $data); |
---|
168 | } |
---|
169 | |
---|
170 | function stop($string_ips) { |
---|
171 | |
---|
172 | $ips = explode(';',$string_ips); |
---|
173 | |
---|
174 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
175 | |
---|
176 | common_request(OG_REST_CMD_STOP, POST, $data); |
---|
177 | } |
---|
178 | |
---|
179 | function refresh($string_ips) { |
---|
180 | |
---|
181 | $ips = explode(';',$string_ips); |
---|
182 | |
---|
183 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
184 | |
---|
185 | common_request(OG_REST_CMD_REFRESH, POST, $data); |
---|
186 | } |
---|
187 | |
---|
188 | function hardware($string_ips) { |
---|
189 | |
---|
190 | $ips = explode(';',$string_ips); |
---|
191 | |
---|
192 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
193 | |
---|
194 | common_request(OG_REST_CMD_HARDWARE, POST, $data); |
---|
195 | } |
---|
196 | |
---|
197 | function software($string_ips) { |
---|
198 | |
---|
199 | $ips = explode(';',$string_ips); |
---|
200 | |
---|
201 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
202 | |
---|
203 | common_request(OG_REST_CMD_SOFTWARE, POST, $data); |
---|
204 | } |
---|
205 | |
---|
206 | /* |
---|
207 | * @function multiRequest. |
---|
208 | * @param URLs array (may include header and POST data), cURL options array. |
---|
209 | * @return Array of arrays with JSON requests and response codes. |
---|
210 | * @warning Default options: does not verifying certificate, connection timeout 200 ms. |
---|
211 | * @Date 2015-10-14 |
---|
212 | */ |
---|
213 | function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT_MS => 500)) { |
---|
214 | |
---|
215 | // array of curl handles |
---|
216 | $curly = array(); |
---|
217 | // Data to be returned (response data and code) |
---|
218 | $result = array(); |
---|
219 | |
---|
220 | // multi handle |
---|
221 | $mh = curl_multi_init(); |
---|
222 | |
---|
223 | // loop through $data and create curl handles |
---|
224 | // then add them to the multi-handle |
---|
225 | foreach ($data as $id => $d) { |
---|
226 | |
---|
227 | |
---|
228 | $curly[$id] = curl_init(); |
---|
229 | |
---|
230 | $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; |
---|
231 | curl_setopt($curly[$id], CURLOPT_URL, $url); |
---|
232 | // HTTP headers? |
---|
233 | if (is_array($d) && !empty($d['header'])) { |
---|
234 | curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']); |
---|
235 | } else { |
---|
236 | curl_setopt($curly[$id], CURLOPT_HEADER, 0); |
---|
237 | } |
---|
238 | curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); |
---|
239 | |
---|
240 | // post? |
---|
241 | if (is_array($d)) { |
---|
242 | if (!empty($d['post'])) { |
---|
243 | curl_setopt($curly[$id], CURLOPT_POST, 1); |
---|
244 | curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | // extra options? |
---|
249 | if (!empty($options)) { |
---|
250 | curl_setopt_array($curly[$id], $options); |
---|
251 | } |
---|
252 | |
---|
253 | curl_multi_add_handle($mh, $curly[$id]); |
---|
254 | } |
---|
255 | |
---|
256 | // execute the handles |
---|
257 | $running = null; |
---|
258 | do { |
---|
259 | curl_multi_exec($mh, $running); |
---|
260 | } while($running > 0); |
---|
261 | |
---|
262 | |
---|
263 | // Get content and HTTP code, and remove handles |
---|
264 | foreach($curly as $id => $c) { |
---|
265 | $result[$id]['data'] = curl_multi_getcontent($c); |
---|
266 | $result[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE); |
---|
267 | curl_multi_remove_handle($mh, $c); |
---|
268 | } |
---|
269 | |
---|
270 | // all done |
---|
271 | curl_multi_close($mh); |
---|
272 | |
---|
273 | return $result; |
---|
274 | } |
---|
275 | |
---|