[8c9fcb2] | 1 | <?php |
---|
[560455a] | 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'); |
---|
[251bb977] | 14 | define('OG_REST_CMD_POWEROFF', 'poweroff'); |
---|
[da462c8] | 15 | define('OG_REST_CMD_REBOOT', 'reboot'); |
---|
[dc82754] | 16 | define('OG_REST_CMD_STOP', 'stop'); |
---|
[c9cfd44] | 17 | define('OG_REST_CMD_REFRESH', 'refresh'); |
---|
[db537e7] | 18 | define('OG_REST_CMD_HARDWARE', 'hardware'); |
---|
[96cced9] | 19 | define('OG_REST_CMD_SOFTWARE', 'software'); |
---|
[560455a] | 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'); |
---|
[e378505] | 28 | define('OG_REST_PARAM_STATE', 'state'); |
---|
[560455a] | 29 | |
---|
[31c92b6] | 30 | $conf_file = parse_ini_file(__DIR__ . '/../../etc/ogAdmRepo.cfg'); |
---|
[ef61154] | 31 | define('OG_REST_API_TOKEN', 'Authorization: ' . $conf_file['ApiToken']); |
---|
| 32 | |
---|
[41ceac0] | 33 | function common_request($command, $type, $data = null) { |
---|
[560455a] | 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); |
---|
[ef61154] | 41 | curl_setopt($curl, CURLOPT_HTTPHEADER, array( |
---|
| 42 | OG_REST_API_TOKEN, |
---|
| 43 | )); |
---|
[560455a] | 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) { |
---|
[4d7e35c] | 58 | syslog(LOG_ERR, 'error occured during curl exec. Additioanl info: ' . print_r($info, TRUE)); |
---|
[dc82754] | 59 | return 0; |
---|
[560455a] | 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; |
---|
[0b97d96] | 101 | $data = null; |
---|
[560455a] | 102 | break; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | $result = common_request(OG_REST_CMD_CLIENTS, $type, $data); |
---|
| 106 | |
---|
[0b97d96] | 107 | $trama_notificacion = ""; |
---|
| 108 | if (isset($result[OG_REST_PARAM_CLIENTS])) { |
---|
| 109 | foreach ($result[OG_REST_PARAM_CLIENTS] as $client) { |
---|
| 110 | $trama_notificacion .= $client[OG_REST_PARAM_ADDR].'/'. |
---|
| 111 | $client[OG_REST_PARAM_STATE].';'; |
---|
| 112 | } |
---|
[560455a] | 113 | } |
---|
| 114 | |
---|
| 115 | return $trama_notificacion; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | function wol($type_wol, $macs, $ips) { |
---|
| 119 | |
---|
| 120 | switch ($type_wol) { |
---|
| 121 | default: |
---|
| 122 | case 1: |
---|
| 123 | $wol = 'broadcast'; |
---|
| 124 | break; |
---|
| 125 | case 2: |
---|
| 126 | $wol = 'unicast'; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | $clients = array(); |
---|
| 130 | |
---|
| 131 | for($i=0; $i<count($macs); $i++) { |
---|
| 132 | $clients[] = array(OG_REST_PARAM_ADDR => $ips[$i], |
---|
| 133 | OG_REST_PARAM_MAC => $macs[$i]); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | $data = array(OG_REST_PARAM_TYPE => $wol, |
---|
| 137 | OG_REST_PARAM_CLIENTS => $clients); |
---|
| 138 | |
---|
| 139 | common_request(OG_REST_CMD_WOL, POST, $data); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | function session($string_ips, $params) { |
---|
| 143 | |
---|
| 144 | preg_match_all('!\d{1}!', $params, $matches); |
---|
| 145 | |
---|
| 146 | $ips = explode(';',$string_ips); |
---|
| 147 | $disk = $matches[0][0]; |
---|
| 148 | $part = $matches[0][1]; |
---|
| 149 | |
---|
| 150 | $data = array(OG_REST_PARAM_CLIENTS => $ips, |
---|
| 151 | OG_REST_PARAM_DISK => $disk, OG_REST_PARAM_PART => $part); |
---|
| 152 | |
---|
| 153 | common_request(OG_REST_CMD_SESSION, POST, $data); |
---|
| 154 | } |
---|
| 155 | |
---|
[251bb977] | 156 | function poweroff($string_ips) { |
---|
| 157 | |
---|
| 158 | $ips = explode(';',$string_ips); |
---|
| 159 | |
---|
| 160 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 161 | |
---|
| 162 | common_request(OG_REST_CMD_POWEROFF, POST, $data); |
---|
| 163 | } |
---|
| 164 | |
---|
[da462c8] | 165 | function reboot($string_ips) { |
---|
| 166 | |
---|
| 167 | $ips = explode(';',$string_ips); |
---|
| 168 | |
---|
| 169 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 170 | |
---|
| 171 | common_request(OG_REST_CMD_REBOOT, POST, $data); |
---|
| 172 | } |
---|
| 173 | |
---|
[dc82754] | 174 | function stop($string_ips) { |
---|
| 175 | |
---|
| 176 | $ips = explode(';',$string_ips); |
---|
| 177 | |
---|
| 178 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 179 | |
---|
| 180 | common_request(OG_REST_CMD_STOP, POST, $data); |
---|
| 181 | } |
---|
| 182 | |
---|
[c9cfd44] | 183 | function refresh($string_ips) { |
---|
| 184 | |
---|
| 185 | $ips = explode(';',$string_ips); |
---|
| 186 | |
---|
| 187 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 188 | |
---|
| 189 | common_request(OG_REST_CMD_REFRESH, POST, $data); |
---|
| 190 | } |
---|
| 191 | |
---|
[db537e7] | 192 | function hardware($string_ips) { |
---|
| 193 | |
---|
| 194 | $ips = explode(';',$string_ips); |
---|
| 195 | |
---|
| 196 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 197 | |
---|
| 198 | common_request(OG_REST_CMD_HARDWARE, POST, $data); |
---|
| 199 | } |
---|
| 200 | |
---|
[96cced9] | 201 | function software($string_ips) { |
---|
| 202 | |
---|
| 203 | $ips = explode(';',$string_ips); |
---|
| 204 | |
---|
| 205 | $data = array(OG_REST_PARAM_CLIENTS => $ips); |
---|
| 206 | |
---|
| 207 | common_request(OG_REST_CMD_SOFTWARE, POST, $data); |
---|
| 208 | } |
---|
| 209 | |
---|
[8c9fcb2] | 210 | /* |
---|
[357352b] | 211 | * @function multiRequest. |
---|
| 212 | * @param URLs array (may include header and POST data), cURL options array. |
---|
| 213 | * @return Array of arrays with JSON requests and response codes. |
---|
[04319fb] | 214 | * @warning Default options: does not verifying certificate, connection timeout 200 ms. |
---|
[357352b] | 215 | * @Date 2015-10-14 |
---|
[8c9fcb2] | 216 | */ |
---|
[42d268a4] | 217 | function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_TIMEOUT_MS => 500)) { |
---|
[8c9fcb2] | 218 | |
---|
| 219 | // array of curl handles |
---|
| 220 | $curly = array(); |
---|
[357352b] | 221 | // Data to be returned (response data and code) |
---|
[8c9fcb2] | 222 | $result = array(); |
---|
| 223 | |
---|
| 224 | // multi handle |
---|
| 225 | $mh = curl_multi_init(); |
---|
| 226 | |
---|
| 227 | // loop through $data and create curl handles |
---|
| 228 | // then add them to the multi-handle |
---|
| 229 | foreach ($data as $id => $d) { |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | $curly[$id] = curl_init(); |
---|
| 233 | |
---|
| 234 | $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; |
---|
[ba3c59d] | 235 | curl_setopt($curly[$id], CURLOPT_URL, $url); |
---|
[357352b] | 236 | // HTTP headers? |
---|
[69052958] | 237 | if (is_array($d) && !empty($d['header'])) { |
---|
[e46f7ce] | 238 | curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']); |
---|
[69052958] | 239 | } else { |
---|
| 240 | curl_setopt($curly[$id], CURLOPT_HEADER, 0); |
---|
| 241 | } |
---|
[8c9fcb2] | 242 | curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); |
---|
| 243 | |
---|
| 244 | // post? |
---|
| 245 | if (is_array($d)) { |
---|
| 246 | if (!empty($d['post'])) { |
---|
[ba3c59d] | 247 | curl_setopt($curly[$id], CURLOPT_POST, 1); |
---|
[8c9fcb2] | 248 | curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | // extra options? |
---|
| 253 | if (!empty($options)) { |
---|
| 254 | curl_setopt_array($curly[$id], $options); |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | curl_multi_add_handle($mh, $curly[$id]); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | // execute the handles |
---|
| 261 | $running = null; |
---|
| 262 | do { |
---|
| 263 | curl_multi_exec($mh, $running); |
---|
| 264 | } while($running > 0); |
---|
| 265 | |
---|
| 266 | |
---|
[357352b] | 267 | // Get content and HTTP code, and remove handles |
---|
[8c9fcb2] | 268 | foreach($curly as $id => $c) { |
---|
[357352b] | 269 | $result[$id]['data'] = curl_multi_getcontent($c); |
---|
| 270 | $result[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE); |
---|
[8c9fcb2] | 271 | curl_multi_remove_handle($mh, $c); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | // all done |
---|
| 275 | curl_multi_close($mh); |
---|
| 276 | |
---|
| 277 | return $result; |
---|
| 278 | } |
---|
[b6ec162] | 279 | |
---|