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