update copyright year
[GitHub/Stricted/speedport-hybrid-php-api.git] / lib / trait / System.class.php
CommitLineData
9b926efe
S
1<?php
2/**
3 * @author Jan Altensen (Stricted)
4 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
dd76f288 5 * @copyright 2015-2016 Jan Altensen (Stricted)
9b926efe
S
6 */
7trait System {
8 /**
105717c9 9 * get uptime based on last reboot
9b926efe
S
10 *
11 * @return string
12 */
13 public function getUptime () {
105717c9
S
14 $lastReboot = $this->getLastReboot();
15
16 $dtF = new DateTime("@0");
17 $dtT = new DateTime("@".$lastReboot);
9b926efe 18
105717c9 19 return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
9b926efe
S
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();
e94573dd 33 $data = $this->sendRequest($path, $fields, true);
9b926efe
S
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
105717c9
S
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
9b926efe
S
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');
e94573dd 80 $data = $this->sendRequest($path, $fields, true);
9b926efe
S
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');
e94573dd 96 $data = $this->sendRequest($path, $fields, true);
9b926efe
S
97
98 return $data['body'];
99 }
e9631169
S
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');
e94573dd 111 $data = $this->sendEncryptedRequest($path, $fields, true);
e9631169
S
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 }
89d43347 128}