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