| 1 | <?php |
| 2 | /** |
| 3 | * @author Jan Altensen (Stricted) |
| 4 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 5 | * @copyright 2015-2016 Jan Altensen (Stricted) |
| 6 | */ |
| 7 | trait System { |
| 8 | /** |
| 9 | * get uptime based on last reboot |
| 10 | * |
| 11 | * @return string |
| 12 | */ |
| 13 | public function getUptime () { |
| 14 | $lastReboot = $this->getLastReboot(); |
| 15 | |
| 16 | $dtF = new DateTime("@0"); |
| 17 | $dtT = new DateTime("@".$lastReboot); |
| 18 | |
| 19 | return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds'); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * return the given json as array |
| 24 | * |
| 25 | * @param string $file |
| 26 | * @return array |
| 27 | */ |
| 28 | public function getData ($file) { |
| 29 | if ($file != 'Status') $this->checkLogin(); |
| 30 | |
| 31 | $path = 'data/'.$file.'.json'; |
| 32 | $fields = array(); |
| 33 | $data = $this->sendRequest($path, $fields, true); |
| 34 | |
| 35 | return $data['body']; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * get the router syslog |
| 40 | * |
| 41 | * @return array |
| 42 | */ |
| 43 | public function getSyslog() { |
| 44 | $data = $this->getData('SystemMessages'); |
| 45 | $data = $this->getValues($data); |
| 46 | |
| 47 | if (isset($data['addmessage'])) { |
| 48 | return $data['addmessage']; |
| 49 | } |
| 50 | else { |
| 51 | return array(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * get Last Reboot time |
| 57 | * |
| 58 | * @return int |
| 59 | */ |
| 60 | public function getLastReboot () { |
| 61 | $response = $this->sendRequest("data/Reboot.json"); |
| 62 | $response = $this->getValues($response); |
| 63 | |
| 64 | $lastReboot = time() - strtotime($response['reboot_date']." ".$response['reboot_time']); |
| 65 | |
| 66 | return $lastReboot; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * reset the router to Factory Default |
| 71 | * not tested |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | public function resetToFactoryDefault () { |
| 76 | $this->checkLogin(); |
| 77 | |
| 78 | $path = 'data/resetAllSetting.json'; |
| 79 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true'); |
| 80 | $data = $this->sendRequest($path, $fields, true); |
| 81 | |
| 82 | return $data['body']; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * check if firmware is actual |
| 88 | * |
| 89 | * @return array |
| 90 | */ |
| 91 | public function checkFirmware () { |
| 92 | $this->checkLogin(); |
| 93 | |
| 94 | $path = 'data/checkfirmware.json'; |
| 95 | $fields = array('checkfirmware' => 'true'); |
| 96 | $data = $this->sendRequest($path, $fields, true); |
| 97 | |
| 98 | return $data['body']; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * reboot the router |
| 103 | * |
| 104 | * @return boolean |
| 105 | */ |
| 106 | public function reboot () { |
| 107 | $this->checkLogin(); |
| 108 | |
| 109 | $path = 'data/Reboot.json'; |
| 110 | $fields = array('csrf_token' => $this->token, 'reboot_device' => 'true'); |
| 111 | $data = $this->sendEncryptedRequest($path, $fields, true); |
| 112 | $data = $this->getValues($data['body']); |
| 113 | |
| 114 | if ($data['status'] == 'ok') { |
| 115 | // reset challenge and session |
| 116 | $this->challenge = ''; |
| 117 | $this->cookie = ''; |
| 118 | $this->token = ''; |
| 119 | $this->derivedk = ''; |
| 120 | |
| 121 | // throw an exception because router is unavailable for other tasks |
| 122 | // like $this->logout() or $this->checkLogin |
| 123 | throw new RebootException('Router Reboot'); |
| 124 | } |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | } |