Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.05 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class UniFi {
  5. public $user="";
  6. public $password="";
  7. public $site="";
  8. public $baseurl="";
  9. public $controller="";
  10. public $is_loggedin=false;
  11. private $cookies="/tmp/unify";
  12. public $debug=false;
  13. //function __construct($user="",$password="",$baseurl="",$site="",$controller="") {
  14. function __construct($cInfo) {
  15. $this->user = $cInfo->ctrlUsername;
  16. $this->password = $cInfo->ctrlPassword;
  17. $this->baseurl = $cInfo->ctrlBaseURL;
  18. $this->site = $cInfo->ctrlSite;
  19. $this->controller = $cInfo->ctrlVersion;
  20. }
  21. function __destruct() {
  22. if ($this->is_loggedin) {
  23. $this->logout();
  24. }
  25. }
  26.  
  27. function write_array($array) {
  28. echo "<pre>";
  29. print_r($array);
  30. echo "</pre>";
  31. }
  32.  
  33. /*
  34. Login to Unifi Controller
  35. */
  36. public function login() {
  37. $this->cookies="";
  38. $ch=$this->get_curl_obj();
  39. curl_setopt($ch, CURLOPT_HEADER, 1);
  40. if(intval($this->controller) >= 4) {
  41. //Controller 4
  42. curl_setopt($ch, CURLOPT_REFERER, $this->baseurl."/login");
  43. curl_setopt($ch, CURLOPT_URL, $this->baseurl."/api/login");
  44. curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode(array("username" => $this->user, "password" => $this->password)).":");
  45. } else {
  46. //Controller 3
  47. curl_setopt($ch, CURLOPT_URL, $this->baseurl."/login");
  48. curl_setopt($ch, CURLOPT_POSTFIELDS,"login=login&username=".$this->user."&password=".$this->password);
  49. }
  50. if ($this->debug == true) {
  51. curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
  52. }
  53. $content=curl_exec($ch);
  54. if ($this->debug == true) {
  55. print "<pre>";
  56. print "-----LOGIN-------------\n<br>\n";
  57. print_r (curl_getinfo($ch));
  58. print "\n<br>\n---------------------\n<br>\n";
  59. print $url."\n<br>\n";
  60. print $data."\n<br>\n";
  61. print "---------------------\n<br>\n";
  62. //print $content."\n<br>\n";
  63. print "---------------------\n<br>\n";
  64. print "</pre>";
  65. }
  66. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  67. $body = trim(substr($content, $header_size));
  68. $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  69.  
  70. if(curl_exec($ch) === false) {
  71. error_log('curl error: ' . curl_error($ch));
  72. }
  73. curl_close ($ch);
  74. preg_match_all('|Set-Cookie: (.*);|U', substr($content, 0, $header_size), $results);
  75.  
  76. if (isset($results[1])) {
  77. $this->cookies = implode(';', $results[1]);
  78. if (!empty($body)) {
  79. if (($code >= 200) && ($code < 400)) {
  80. if (strpos($this->cookies,"unifises") !== FALSE) {
  81.  
  82. $this->is_loggedin=true;
  83. }
  84. }
  85. if($code === 400) {
  86. error_log('we have received an HTTP response status: 400. Probably a controller login failure');
  87. }
  88. }
  89. }
  90. return $this->is_loggedin;
  91. }
  92. /*
  93. Logout from Unifi Controller
  94. */
  95. public function logout() {
  96. if (!$this->is_loggedin) return false;
  97. $return=false;
  98. $content=$this->exec_curl($this->baseurl."/logout");
  99. $this->is_loggedin=false;
  100. $this->cookies="";
  101. return $return;
  102. }
  103. /*
  104. Authorize a MAC address
  105. parameter <MAC address>,<minutes until expires from now>
  106. return true on success
  107. */
  108. public function authorize_guest($mac,$minutes) {
  109. $mac=strtolower($mac);
  110. if (!$this->is_loggedin) return false;
  111. $return=false;
  112. $json=json_encode(array('cmd' => 'authorize-guest', 'mac' => $mac, 'minutes' => $minutes));
  113. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/cmd/stamgr","json=".$json);
  114. $content_decoded=json_decode($content);
  115. if (isset($content_decoded->meta->rc)) {
  116. if ($content_decoded->meta->rc == "ok") {
  117. $return=true;
  118. }
  119. }
  120. return $return;
  121. }
  122. /*
  123. unauthorize a MAC address
  124. parameter <MAC address>
  125. return true on success
  126. */
  127. public function unauthorize_guest($mac) {
  128. $mac=strtolower($mac);
  129. if (!$this->is_loggedin) return false;
  130. $return=false;
  131. $json=json_encode(array('cmd' => 'unauthorize-guest', 'mac' => $mac));
  132. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/cmd/stamgr","json=".$json);
  133. $content_decoded=json_decode($content);
  134. if (isset($content_decoded->meta->rc)) {
  135. if ($content_decoded->meta->rc == "ok") {
  136. $return=true;
  137. }
  138. }
  139. return $return;
  140. }
  141. /*
  142. reconnect a client
  143. parameter <MAC address>
  144. return true on success
  145. */
  146. public function reconnect_sta($mac) {
  147. $mac=strtolower($mac);
  148. if (!$this->is_loggedin) return false;
  149. $return=false;
  150. $json=json_encode(array('cmd' => 'kick-sta', 'mac' => $mac));
  151. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/cmd/stamgr","json=".$json);
  152. $content_decoded=json_decode($content);
  153. if (isset($content_decoded->meta->rc)) {
  154. if ($content_decoded->meta->rc == "ok") {
  155. $return=true;
  156. }
  157. }
  158. return $return;
  159. }
  160. /*
  161. block a client
  162. parameter <MAC address>
  163. return true on success
  164. */
  165. public function block_sta($mac) {
  166. $mac=strtolower($mac);
  167. if (!$this->is_loggedin) return false;
  168. $return=false;
  169. $json=json_encode(array('cmd' => 'block-sta', 'mac' => $mac));
  170. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/cmd/stamgr","json=".$json);
  171. $content_decoded=json_decode($content);
  172. if (isset($content_decoded->meta->rc)) {
  173. if ($content_decoded->meta->rc == "ok") {
  174. $return=true;
  175. }
  176. }
  177. return $return;
  178. }
  179. /*
  180. unblock a client
  181. parameter <MAC address>
  182. return true on success
  183. */
  184. public function unblock_sta($mac) {
  185. $mac=strtolower($mac);
  186. if (!$this->is_loggedin) return false;
  187. $return=false;
  188. $json=json_encode(array('cmd' => 'unblock-sta', 'mac' => $mac));
  189. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/cmd/stamgr","json=".$json);
  190. $content_decoded=json_decode($content);
  191. if (isset($content_decoded->meta->rc)) {
  192. if ($content_decoded->meta->rc == "ok") {
  193. $return=true;
  194. }
  195. }
  196. return $return;
  197. }
  198. /*
  199. hourly stats method for a site
  200. parameter <start>
  201. parameter <end>
  202. defaults to the past 7*24 hours
  203. */
  204. public function stat_hourly_site($start = NULL, $end = NULL) {
  205. $return=array();
  206. if (!$this->is_loggedin) return false;
  207. $return=array();
  208. $end = is_null($end) ? ((time())*1000) : $end;
  209. $start = is_null($start) ? $end-604800000 : $start;
  210. $json=json_encode(array('attrs' => array('bytes', 'num_sta', 'time'), 'start' => $start, 'end' => $end));
  211. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/report/hourly.site","json=".$json);
  212. $content_decoded=json_decode($content);
  213. if (isset($content_decoded->meta->rc)) {
  214. if ($content_decoded->meta->rc == "ok") {
  215. if (is_array($content_decoded->data)) {
  216. foreach ($content_decoded->data as $test) {
  217. $return[]=$test;
  218. }
  219. }
  220. }
  221. }
  222. return $return;
  223. }
  224. /*
  225. hourly stats method for all access points
  226. parameter <start>
  227. parameter <end>
  228. defaults to the past 7*24 hours, but unifi controller does not
  229. keep these stats longer than 5 hours (controller v 4.6.6)
  230. */
  231. public function stat_hourly_aps($start = NULL, $end = NULL) {
  232. $return=array();
  233. if (!$this->is_loggedin) return false;
  234. $return=array();
  235. $end = is_null($end) ? ((time())*1000) : $end;
  236. $start = is_null($start) ? $end-604800000 : $start;
  237. $json=json_encode(array('attrs' => array('bytes', 'num_sta', 'time'), 'start' => $start, 'end' => $end));
  238. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/report/hourly.ap","json=".$json);
  239. $content_decoded=json_decode($content);
  240. if (isset($content_decoded->meta->rc)) {
  241. if ($content_decoded->meta->rc == "ok") {
  242. if (is_array($content_decoded->data)) {
  243. foreach ($content_decoded->data as $test) {
  244. $return[]=$test;
  245. }
  246. }
  247. }
  248. }
  249. return $return;
  250. }
  251. /*
  252. show all login sessions
  253. parameter <start>
  254. parameter <end>
  255. default start value is 7 days ago
  256. */
  257. public function stat_sessions($start = NULL, $end = NULL) {
  258. $return=array();
  259. if (!$this->is_loggedin) return false;
  260. $return=array();
  261. //$end = is_null($end) ? ((time()-(time() % 3600))) : $end; // why did we need this floor calc?
  262. $end = is_null($end) ? time() : $end;
  263. $start = is_null($start) ? $end-604800 : $start;
  264. $json=json_encode(array('type'=> 'all', 'start' => $start, 'end' => $end));
  265. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/session","json=".$json);
  266. $content_decoded=json_decode($content);
  267. if (isset($content_decoded->meta->rc)) {
  268. if ($content_decoded->meta->rc == "ok") {
  269. if (is_array($content_decoded->data)) {
  270. foreach ($content_decoded->data as $session) {
  271. $return[]=$session;
  272. }
  273. }
  274. }
  275. }
  276. return $return;
  277. }
  278. /*
  279. show all authorizations
  280. parameter <start>
  281. parameter <end>
  282. defaults to the past 7*24 hours
  283. */
  284. public function stat_auths($start = NULL, $end = NULL) {
  285. $return=array();
  286. if (!$this->is_loggedin) return false;
  287. $return=array();
  288. //$end = is_null($end) ? ((time()-(time() % 3600))) : $end; // why did we need this floor calc?
  289. $end = is_null($end) ? time() : $end;
  290. $start = is_null($start) ? $end-604800 : $start;
  291. $json=json_encode(array('start' => $start, 'end' => $end));
  292. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/authorization","json=".$json);
  293. $content_decoded=json_decode($content);
  294. if (isset($content_decoded->meta->rc)) {
  295. if ($content_decoded->meta->rc == "ok") {
  296. if (is_array($content_decoded->data)) {
  297. foreach ($content_decoded->data as $auth) {
  298. $return[]=$auth;
  299. }
  300. }
  301. }
  302. }
  303. return $return;
  304. }
  305. /*
  306. daily stats method
  307. parameter <start>
  308. parameter <end>
  309. defaults to the past 30*7*24 hours
  310. */
  311. public function stat_daily_site($start = NULL, $end = NULL) {
  312. $return=array();
  313. if (!$this->is_loggedin) return false;
  314. $return=array();
  315. $end = is_null($end) ? ((time()-(time() % 3600))*1000) : $end;
  316. $start = is_null($start) ? $end-18144000000 : $start;
  317. $json=json_encode(array('attrs' => array('bytes', 'num_sta', 'time'), 'start' => $start, 'end' => $end));
  318. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/report/daily.site","json=".$json);
  319. $content_decoded=json_decode($content);
  320. if (isset($content_decoded->meta->rc)) {
  321. if ($content_decoded->meta->rc == "ok") {
  322. if (is_array($content_decoded->data)) {
  323. foreach ($content_decoded->data as $test) {
  324. $return[]=$test;
  325. }
  326. }
  327. }
  328. }
  329. return $return;
  330. }
  331.  
  332. /*
  333. get details of all clients ever connected to the site
  334. json parameters {type: "all", conn: "all", within: "24"}
  335. Note: within only selects the clients that were online in that period.
  336. Stats per client remain the same
  337. */
  338. public function stat_allusers($historyhours = NULL) {
  339. $return=array();
  340. if (!$this->is_loggedin) return false;
  341. $return=array();
  342. // default to 1 year of history
  343. $historyhours = is_null($historyhours) ? 8760 : $historyhours;
  344. $json=json_encode(array('type' => 'all', 'conn' => 'all', 'within' => $historyhours));
  345. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/alluser","json=".$json);
  346. $content_decoded=json_decode($content);
  347. if (isset($content_decoded->meta->rc)) {
  348. if ($content_decoded->meta->rc == "ok") {
  349. if (is_array($content_decoded->data)) {
  350. foreach ($content_decoded->data as $stats) {
  351. $return[]=$stats;
  352. }
  353. }
  354. }
  355. }
  356. return $return;
  357. }
  358. /*
  359. list guests
  360. returns a array of guest objects
  361. */
  362. public function list_guests() {
  363. $return=array();
  364. if (!$this->is_loggedin) return $return;
  365. $return=array();
  366. $json=json_encode(array());
  367. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/guest","json=".$json);
  368. $content_decoded=json_decode($content);
  369. if (isset($content_decoded->meta->rc)) {
  370. if ($content_decoded->meta->rc == "ok") {
  371. if (is_array($content_decoded->data)) {
  372. foreach ($content_decoded->data as $guest) {
  373. $return[]=$guest;
  374. }
  375. }
  376. }
  377. }
  378. return $return;
  379. }
  380. /*
  381. list clients
  382. returns a array of client objects
  383. */
  384. public function list_clients() {
  385. $return=array();
  386. if (!$this->is_loggedin) {
  387. echo "not logged in?";
  388. return $return;
  389. }
  390. $return=array();
  391. $json=json_encode(array());
  392. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/sta","json=".$json);
  393. $content_decoded=json_decode($content);
  394. if (isset($content_decoded->meta->rc)) {
  395. if ($content_decoded->meta->rc == "ok") {
  396. if (is_array($content_decoded->data)) {
  397. foreach ($content_decoded->data as $client) {
  398. $return[]=$client;
  399. }
  400. }
  401. }
  402. }
  403. return $return;
  404. }
  405. /*
  406. list health metrics
  407. returns a array of health metric objects
  408. */
  409. public function list_health() {
  410. $return=array();
  411. if (!$this->is_loggedin) return $return;
  412. $return=array();
  413. $json=json_encode(array());
  414. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/health","json=".$json);
  415. $content_decoded=json_decode($content);
  416. if (isset($content_decoded->meta->rc)) {
  417. if ($content_decoded->meta->rc == "ok") {
  418. if (is_array($content_decoded->data)) {
  419. foreach ($content_decoded->data as $health) {
  420. $return[]=$health;
  421. }
  422. }
  423. }
  424. }
  425. return $return;
  426. }
  427. /*
  428. list users
  429. returns a array of known user objects
  430. */
  431. public function list_users() {
  432. $return=array();
  433. if (!$this->is_loggedin) return $return;
  434. $return=array();
  435. $json=json_encode(array());
  436. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/list/user","json=".$json);
  437. $content_decoded=json_decode($content);
  438. if (isset($content_decoded->meta->rc)) {
  439. if ($content_decoded->meta->rc == "ok") {
  440. if (is_array($content_decoded->data)) {
  441. foreach ($content_decoded->data as $user) {
  442. $return[]=$user;
  443. }
  444. }
  445. }
  446. }
  447. return $return;
  448. }
  449. /*
  450. list access points
  451. returns a array of known access point objects
  452. */
  453. public function list_aps() {
  454. $return=array();
  455. if (!$this->is_loggedin) return $return;
  456. $return=array();
  457. $json=json_encode(array());
  458. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/device","json=".$json);
  459. $content_decoded=json_decode($content);
  460. if (isset($content_decoded->meta->rc)) {
  461. if ($content_decoded->meta->rc == "ok") {
  462. if (is_array($content_decoded->data)) {
  463. foreach ($content_decoded->data as $ap) {
  464. $return[]=$ap;
  465. }
  466. }
  467. }
  468. }
  469. return $return;
  470. }
  471. public function list_devices($adoptedsite) {
  472. $return=array();
  473. if (!$this->is_loggedin) return $return;
  474. $return=array();
  475. $json=json_encode(array());
  476. $content=$this->exec_curl($this->baseurl."/api/s/".$adoptedsite."/stat/device","json=".$json);
  477. $content_decoded=json_decode($content);
  478. if (isset($content_decoded->meta->rc)) {
  479. if ($content_decoded->meta->rc == "ok") {
  480. if (is_array($content_decoded->data)) {
  481. foreach ($content_decoded->data as $ap) {
  482. $return[]=$ap;
  483. }
  484. }
  485. }
  486. }
  487. return $return;
  488. }
  489. /*
  490. list rogue access points
  491. returns a array of known roque access point objects
  492. json parameter <within> {within: "<hoursback eg 24>"} is optional
  493. */
  494. public function list_rogueaps() {
  495. $return=array();
  496. if (!$this->is_loggedin) return $return;
  497. $return=array();
  498. $json=json_encode(array('within' => '24'));
  499. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/rogueap","json=".$json);
  500. $content_decoded=json_decode($content);
  501. if (isset($content_decoded->meta->rc)) {
  502. if ($content_decoded->meta->rc == "ok") {
  503. if (is_array($content_decoded->data)) {
  504. foreach ($content_decoded->data as $rogues) {
  505. $return[]=$rogues;
  506. }
  507. }
  508. }
  509. }
  510. return $return;
  511. }
  512. /*
  513. list sites
  514. returns a list sites hosted on this controller with some details
  515. */
  516. public function list_sites() {
  517. $return=array();
  518. if (!$this->is_loggedin) return $return;
  519. $return=array();
  520. $content=$this->exec_curl($this->baseurl."/api/self/sites");
  521. $content_decoded=json_decode($content);
  522. if (isset($content_decoded->meta->rc)) {
  523. if ($content_decoded->meta->rc == "ok") {
  524. if (is_array($content_decoded->data)) {
  525. foreach ($content_decoded->data as $rogues) {
  526. $return[]=$rogues;
  527. }
  528. }
  529. }
  530. }
  531. return $return;
  532. }
  533. /*
  534. list site settings
  535. returns a array of the site configuration settings
  536. */
  537. public function list_settings() {
  538. $return=array();
  539. if (!$this->is_loggedin) return $return;
  540. $return=array();
  541. $json=json_encode(array());
  542. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/get/setting","json=".$json);
  543. $content_decoded=json_decode($content);
  544. if (isset($content_decoded->meta->rc)) {
  545. if ($content_decoded->meta->rc == "ok") {
  546. if (is_array($content_decoded->data)) {
  547. foreach ($content_decoded->data as $setting) {
  548. $return[]=$setting;
  549. }
  550. }
  551. }
  552. }
  553. return $return;
  554. }
  555. /*
  556. reboot an access point
  557. parameter <MAC address>
  558. return true on success
  559. */
  560. public function restart_ap($mac) {
  561. $mac=strtolower($mac);
  562. if (!$this->is_loggedin) return false;
  563. $return=false;
  564. $json=json_encode(array('cmd' => 'restart', 'mac' => $mac));
  565. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/cmd/devmgr","json=".$json);
  566. $content_decoded=json_decode($content);
  567. if (isset($content_decoded->meta->rc)) {
  568. if ($content_decoded->meta->rc == "ok") {
  569. $return=true;
  570. }
  571. }
  572. return $return;
  573. }
  574. /*
  575. start flashing LED of an access point for locating purposes
  576. parameter <MAC address>
  577. return true on success
  578. */
  579. public function set_locate_ap($mac) {
  580. $mac=strtolower($mac);
  581. if (!$this->is_loggedin) return false;
  582. $return=false;
  583. $json=json_encode(array('cmd' => 'set-locate', 'mac' => $mac));
  584. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/cmd/devmgr","json=".$json);
  585. $content_decoded=json_decode($content);
  586. if (isset($content_decoded->meta->rc)) {
  587. if ($content_decoded->meta->rc == "ok") {
  588. $return=true;
  589. }
  590. }
  591. return $return;
  592. }
  593.  
  594. public function adopt_ap($mac,$adoptsite) {
  595. $mac=strtolower($mac);
  596. if (!$this->is_loggedin) return false;
  597. $return=false;
  598. $json=json_encode(array('cmd' => 'adopt', 'mac' => $mac));
  599. $content=$this->exec_curl($this->baseurl."/api/s/".$adoptsite."/cmd/devmgr","json=".$json);
  600. $content_decoded=json_decode($content);
  601. if (isset($content_decoded->meta->rc)) {
  602. if ($content_decoded->meta->rc == "ok") {
  603. $return=true;
  604. }
  605. }
  606. return $return;
  607. }
  608. public function upgrade_device($mac,$adoptsite) {
  609. $mac=strtolower($mac);
  610. if (!$this->is_loggedin) return false;
  611. $return=false;
  612. $json=json_encode(array('cmd' => 'upgrade', 'mac' => $mac));
  613. $content=$this->exec_curl($this->baseurl."/api/s/".$adoptsite."/cmd/devmgr","json=".$json);
  614. $content_decoded=json_decode($content);
  615. if (isset($content_decoded->meta->rc)) {
  616. if ($content_decoded->meta->rc == "ok") {
  617. $return=true;
  618. }
  619. }
  620. return $return;
  621. }
  622. /*
  623. set access point radio settings
  624. parameter <ap_id> <radio>(default=ng) <channel> <ht>(default=20) <tx_power_mode> <tx_power>(default=0)
  625. return true on success
  626. */
  627. public function set_ap_radiosettings($ap_id, $radio, $channel, $ht, $tx_power_mode, $tx_power) {
  628. if (!$this->is_loggedin) return false;
  629. $return=false;
  630. $jsonsettings=json_encode(array('radio' => $radio, 'channel' => $channel, 'ht' => $ht, 'tx_power_mode' => $tx_power_mode, 'tx_power' =>$tx_power));
  631. $json='{"radio_table": ['.$jsonsettings.']}';
  632. error_log("de json data die wordt gepost: ".$json);
  633. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/upd/device/".$ap_id,"json=".$json);
  634. $content_decoded=json_decode($content);
  635. if (isset($content_decoded->meta->rc)) {
  636. if ($content_decoded->meta->rc == "ok") {
  637. $return=true;
  638. }
  639. }
  640. return $return;
  641. }
  642. /*
  643. set guest login settings
  644. parameters <portal_enabled> <portal_customized> <redirect_enabled> <redirect_url> <x_password> <expire_number> <expire_unit> <site_id>
  645. both portal parameters are set to the same value!
  646. return true on success
  647. */
  648. public function set_guestlogin_settings($portal_enabled, $portal_customized, $redirect_enabled, $redirect_url, $x_password, $expire_number, $expire_unit, $site_id) {
  649. if (!$this->is_loggedin) return false;
  650. $return=false;
  651. $json=json_encode(array( 'portal_enabled' => $portal_enabled, 'portal_customized' => $portal_customized,
  652. 'redirect_enabled' => $redirect_enabled, 'redirect_url' => $redirect_url,
  653. 'x_password' => $x_password, 'expire_number' => $expire_number,
  654. 'expire_unit' => $expire_unit, 'site_id' => $site_id), JSON_UNESCAPED_SLASHES);
  655. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/set/setting/guest_access","json=".$json);
  656. $content_decoded=json_decode($content);
  657. if (isset($content_decoded->meta->rc)) {
  658. if ($content_decoded->meta->rc == "ok") {
  659. $return=true;
  660. }
  661. }
  662. return $return;
  663. }
  664. /*
  665. rename access point
  666. parameter <ap_id> <apname>
  667. return true on success
  668. */
  669. public function rename_ap_($ap_id, $apname) {
  670. if (!$this->is_loggedin) return false;
  671. $return=false;
  672. $json=json_encode(array('name' => $apname));
  673. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/upd/device/".$ap_id,"json=".$json);
  674. $content_decoded=json_decode($content);
  675. if (isset($content_decoded->meta->rc)) {
  676. if ($content_decoded->meta->rc == "ok") {
  677. $return=true;
  678. }
  679. }
  680. return $return;
  681. }
  682. /*
  683. set wlan settings
  684. parameter <wlan_id> <name> <x_passphrase>
  685. return true on success
  686. */
  687. public function set_wlansettings($wlan_id, $name, $x_passphrase) {
  688. if (!$this->is_loggedin) return false;
  689. $return=false;
  690. $json=json_encode(array('name' => $name, 'x_passphrase' => $x_passphrase));
  691. error_log("de json data die wordt gepost: ".$json);
  692. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/upd/wlanconf/".$wlan_id,"json=".$json);
  693. $content_decoded=json_decode($content);
  694. if (isset($content_decoded->meta->rc)) {
  695. if ($content_decoded->meta->rc == "ok") {
  696. $return=true;
  697. }
  698. }
  699. return $return;
  700. }
  701. /*
  702. start flashing LED of an access point for locating purposes
  703. parameter <MAC address>
  704. return true on success
  705. */
  706. public function unset_locate_ap($mac) {
  707. $mac=strtolower($mac);
  708. if (!$this->is_loggedin) return false;
  709. $return=false;
  710. $json=json_encode(array('cmd' => 'unset-locate', 'mac' => $mac));
  711. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/cmd/devmgr","json=".$json);
  712. $content_decoded=json_decode($content);
  713. if (isset($content_decoded->meta->rc)) {
  714. if ($content_decoded->meta->rc == "ok") {
  715. $return=true;
  716. }
  717. }
  718. return $return;
  719. }
  720. /*
  721. switch LEDs of all the access points ON
  722. return true on success
  723. */
  724. public function site_ledson() {
  725. if (!$this->is_loggedin) return false;
  726. $return=false;
  727. $json=json_encode(array('led_enabled' => true));
  728. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/set/setting/mgmt","json=".$json);
  729. $content_decoded=json_decode($content);
  730. if (isset($content_decoded->meta->rc)) {
  731. if ($content_decoded->meta->rc == "ok") {
  732. $return=true;
  733. }
  734. }
  735. return $return;
  736. }
  737. /*
  738. switch LEDs of all the access points OFF
  739. return true on success
  740. */
  741. public function site_ledsoff() {
  742. if (!$this->is_loggedin) return false;
  743. $return=false;
  744. $json=json_encode(array('led_enabled' => false));
  745. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/set/setting/mgmt","json=".$json);
  746. $content_decoded=json_decode($content);
  747. if (isset($content_decoded->meta->rc)) {
  748. if ($content_decoded->meta->rc == "ok") {
  749. $return=true;
  750. }
  751. }
  752. return $return;
  753. }
  754.  
  755. /*
  756. list events
  757. returns a array of known events
  758. */
  759. public function list_events() {
  760. $return=array();
  761. if (!$this->is_loggedin) return $return;
  762. $return=array();
  763. $json=json_encode(array());
  764. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/event","json=".$json);
  765. $content_decoded=json_decode($content);
  766. if (isset($content_decoded->meta->rc)) {
  767. if ($content_decoded->meta->rc == "ok") {
  768. if (is_array($content_decoded->data)) {
  769. foreach ($content_decoded->data as $event) {
  770. $return[]=$event;
  771. }
  772. }
  773. }
  774. }
  775. return $return;
  776. }
  777. /*
  778. list wireless settings
  779. returns a array of wireless networks and settings
  780. */
  781. public function list_wlanconf() {
  782. $return=array();
  783. if (!$this->is_loggedin) return $return;
  784. $return=array();
  785. $json=json_encode(array());
  786. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/list/wlanconf","json=".$json);
  787. $content_decoded=json_decode($content);
  788. if (isset($content_decoded->meta->rc)) {
  789. if ($content_decoded->meta->rc == "ok") {
  790. if (is_array($content_decoded->data)) {
  791. foreach ($content_decoded->data as $wlan) {
  792. $return[]=$wlan;
  793. }
  794. }
  795. }
  796. }
  797. return $return;
  798. }
  799. /*
  800. list alarms
  801. returns a array of known alarms
  802. */
  803. public function list_alarms() {
  804. $return=array();
  805. if (!$this->is_loggedin) return $return;
  806. $return=array();
  807. $json=json_encode(array());
  808. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/list/alarm","json=".$json);
  809. $content_decoded=json_decode($content);
  810. if (isset($content_decoded->meta->rc)) {
  811. if ($content_decoded->meta->rc == "ok") {
  812. if (is_array($content_decoded->data)) {
  813. foreach ($content_decoded->data as $alarm) {
  814. $return[]=$alarm;
  815. }
  816. }
  817. }
  818. }
  819. return $return;
  820. }
  821. /*
  822. list vouchers
  823. returns a array of voucher objects
  824. */
  825. public function get_vouchers($create_time="") {
  826. $return=array();
  827. if (!$this->is_loggedin) return $return;
  828. $return=array();
  829. $json=json_encode(array());
  830. if (trim($create_time) != "") {
  831. $json=json_encode(array('create_time' => $create_time));
  832. }
  833. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/stat/voucher","json=".$json);
  834. $content_decoded=json_decode($content);
  835. if (isset($content_decoded->meta->rc)) {
  836. if ($content_decoded->meta->rc == "ok") {
  837. if (is_array($content_decoded->data)) {
  838. foreach ($content_decoded->data as $voucher) {
  839. $return[]=$voucher;
  840. }
  841. }
  842. }
  843. }
  844. return $return;
  845. }
  846. /*
  847. create voucher(s)
  848. parameter <minutes>,<number_of_vouchers_to_create>,<note>,<up>,<down>,<mb>
  849. returns a array of vouchers codes (Note: without the "-" in the middle)
  850. */
  851. public function create_voucher($minutes,$number_of_vouchers_to_create=1,$note="",$up=0,$down=0,$Mbytes=0) {
  852. $return=array();
  853. if (!$this->is_loggedin) return $return;
  854. $json=array('cmd' => 'create-voucher', 'expire' => $minutes, 'n' => $number_of_vouchers_to_create);
  855. if (trim($note) != "") {
  856. $json[]=array('note'=>$note);
  857. }
  858. if ($up > 0) {
  859. $json[]=array('up'=>$up);
  860. }
  861. if ($down > 0) {
  862. $json[]=array('down'=>$down);
  863. }
  864. if ($Mbytes > 0) {
  865. $json[]=array('bytes'=>$Mbytes);
  866. }
  867. // json_encode here
  868. $content=$this->exec_curl($this->baseurl."/api/s/".$this->site."/cmd/hotspot","json={".$json."}");
  869. $content_decoded=json_decode($content);
  870. if ($content_decoded->meta->rc == "ok") {
  871. if (is_array($content_decoded->data)) {
  872. $obj=$content_decoded->data[0];
  873. foreach ($this->get_vouchers($obj->create_time) as $voucher) {
  874. $return[]=$voucher->code;
  875. }
  876. }
  877. }
  878. return $return;
  879. }
  880. private function exec_curl($url,$data="") {
  881. $ch=$this->get_curl_obj();
  882. curl_setopt($ch, CURLOPT_URL, $url);
  883. if (trim($data) != "") {
  884. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  885. } else {
  886. curl_setopt($ch, CURLOPT_POST, FALSE);
  887. }
  888. $content=curl_exec($ch);
  889. if ($this->debug == true) {
  890. print "<pre>";
  891. print "---------------------\n<br>\n";
  892. print_r (curl_getinfo($ch));
  893. print "\n<br>\n---------------------\n<br>\n";
  894. print $url."\n<br>\n";
  895. print $data."\n<br>\n";
  896. print "---------------------\n<br>\n";
  897. print $content."\n<br>\n";
  898. print "---------------------\n<br>\n";
  899. print "</pre>";
  900. }
  901. if(curl_exec($ch) === false) {
  902. error_log('curl error: ' . curl_error($ch));
  903. }
  904. curl_close ($ch);
  905. return $content;
  906. }
  907. private function get_curl_obj() {
  908. $ch = curl_init();
  909. curl_setopt($ch, CURLOPT_POST, TRUE);
  910. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  911. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  912. curl_setopt($ch , CURLOPT_RETURNTRANSFER, TRUE);
  913. if ($this->debug == true) {
  914. curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
  915. }
  916. if ($this->cookies != "") {
  917. curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
  918. }
  919. return $ch;
  920. }
  921. }
  922. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement