@@ -248,7 +248,7 @@ |
||
248 | 248 | /** |
249 | 249 | * alias of stop(); |
250 | 250 | * |
251 | - * @return boolean |
|
251 | + * @return boolean|null |
|
252 | 252 | */ |
253 | 253 | public function kill( $_signal = SIGKILL, $_wait = false ) { |
254 | 254 | return $this->stop( $_signal, $_wait ); |
@@ -11,47 +11,47 @@ discard block |
||
11 | 11 | const FUNCTION_NOT_CALLABLE = 10; |
12 | 12 | const COULD_NOT_FORK = 15; |
13 | 13 | |
14 | - /** |
|
15 | - * possible errors |
|
16 | - * |
|
17 | - * @var array |
|
18 | - */ |
|
14 | + /** |
|
15 | + * possible errors |
|
16 | + * |
|
17 | + * @var array |
|
18 | + */ |
|
19 | 19 | private $errors = array( |
20 | 20 | Thread::FUNCTION_NOT_CALLABLE => 'You must specify a valid function name that can be called from the current scope.', |
21 | 21 | Thread::COULD_NOT_FORK => 'pcntl_fork() returned a status of -1. No new process was created', |
22 | 22 | ); |
23 | 23 | |
24 | - /** |
|
25 | - * callback for the function that should |
|
26 | - * run as a separate thread |
|
27 | - * |
|
28 | - * @var callback |
|
29 | - */ |
|
24 | + /** |
|
25 | + * callback for the function that should |
|
26 | + * run as a separate thread |
|
27 | + * |
|
28 | + * @var callback |
|
29 | + */ |
|
30 | 30 | protected $runnable; |
31 | 31 | |
32 | - /** |
|
33 | - * holds the current process id |
|
34 | - * |
|
35 | - * @var integer |
|
36 | - */ |
|
32 | + /** |
|
33 | + * holds the current process id |
|
34 | + * |
|
35 | + * @var integer |
|
36 | + */ |
|
37 | 37 | private $pid; |
38 | 38 | |
39 | - /** |
|
40 | - * holds the exit code after the child dies |
|
41 | - */ |
|
39 | + /** |
|
40 | + * holds the exit code after the child dies |
|
41 | + */ |
|
42 | 42 | private $exitCode = -1; |
43 | 43 | |
44 | 44 | /** |
45 | - * holds type - needed for fping |
|
46 | - */ |
|
47 | - public $stype = "ping"; |
|
45 | + * holds type - needed for fping |
|
46 | + */ |
|
47 | + public $stype = "ping"; |
|
48 | 48 | |
49 | - /** |
|
50 | - * checks if threading is supported by the current |
|
51 | - * PHP configuration |
|
52 | - * |
|
53 | - * @return boolean |
|
54 | - */ |
|
49 | + /** |
|
50 | + * checks if threading is supported by the current |
|
51 | + * PHP configuration |
|
52 | + * |
|
53 | + * @return boolean |
|
54 | + */ |
|
55 | 55 | public static function available() { |
56 | 56 | $required_functions = array( |
57 | 57 | 'pcntl_fork', |
@@ -66,24 +66,24 @@ discard block |
||
66 | 66 | return true; |
67 | 67 | } |
68 | 68 | |
69 | - /** |
|
70 | - * class constructor - you can pass |
|
71 | - * the callback function as an argument |
|
72 | - * |
|
73 | - * @param callback $_runnable |
|
74 | - */ |
|
69 | + /** |
|
70 | + * class constructor - you can pass |
|
71 | + * the callback function as an argument |
|
72 | + * |
|
73 | + * @param callback $_runnable |
|
74 | + */ |
|
75 | 75 | public function __construct( $_runnable = null ) { |
76 | - if( $_runnable !== null ) { |
|
77 | - $this->setRunnable( $_runnable ); |
|
78 | - } |
|
76 | + if( $_runnable !== null ) { |
|
77 | + $this->setRunnable( $_runnable ); |
|
78 | + } |
|
79 | 79 | } |
80 | 80 | |
81 | - /** |
|
82 | - * sets the callback |
|
83 | - * |
|
84 | - * @param callback $_runnable |
|
85 | - * @return callback |
|
86 | - */ |
|
81 | + /** |
|
82 | + * sets the callback |
|
83 | + * |
|
84 | + * @param callback $_runnable |
|
85 | + * @return callback |
|
86 | + */ |
|
87 | 87 | public function setRunnable( $_runnable ) { |
88 | 88 | if( self::runnableOk( $_runnable ) ) { |
89 | 89 | $this->runnable = $_runnable; |
@@ -93,43 +93,43 @@ discard block |
||
93 | 93 | } |
94 | 94 | } |
95 | 95 | |
96 | - /** |
|
97 | - * gets the callback |
|
98 | - * |
|
99 | - * @return callback |
|
100 | - */ |
|
96 | + /** |
|
97 | + * gets the callback |
|
98 | + * |
|
99 | + * @return callback |
|
100 | + */ |
|
101 | 101 | public function getRunnable() { |
102 | 102 | return $this->runnable; |
103 | 103 | } |
104 | 104 | |
105 | - /** |
|
106 | - * checks if the callback is ok (the function/method |
|
107 | - * actually exists and is runnable from the current |
|
108 | - * context) |
|
109 | - * |
|
110 | - * can be called statically |
|
111 | - * |
|
112 | - * @param callback $_runnable |
|
113 | - * @return boolean |
|
114 | - */ |
|
105 | + /** |
|
106 | + * checks if the callback is ok (the function/method |
|
107 | + * actually exists and is runnable from the current |
|
108 | + * context) |
|
109 | + * |
|
110 | + * can be called statically |
|
111 | + * |
|
112 | + * @param callback $_runnable |
|
113 | + * @return boolean |
|
114 | + */ |
|
115 | 115 | public static function runnableOk( $_runnable ) { |
116 | 116 | return ( function_exists( $_runnable ) && is_callable( $_runnable ) ); |
117 | 117 | } |
118 | 118 | |
119 | - /** |
|
120 | - * returns the process id (pid) of the simulated thread |
|
121 | - * |
|
122 | - * @return int |
|
123 | - */ |
|
119 | + /** |
|
120 | + * returns the process id (pid) of the simulated thread |
|
121 | + * |
|
122 | + * @return int |
|
123 | + */ |
|
124 | 124 | public function getPid() { |
125 | 125 | return $this->pid; |
126 | 126 | } |
127 | 127 | |
128 | - /** |
|
129 | - * checks if the child thread is alive |
|
130 | - * |
|
131 | - * @return boolean |
|
132 | - */ |
|
128 | + /** |
|
129 | + * checks if the child thread is alive |
|
130 | + * |
|
131 | + * @return boolean |
|
132 | + */ |
|
133 | 133 | public function isAlive() { |
134 | 134 | $pid = pcntl_waitpid( $this->pid, $status, WNOHANG ); |
135 | 135 | |
@@ -143,22 +143,22 @@ discard block |
||
143 | 143 | } |
144 | 144 | } |
145 | 145 | |
146 | - /** |
|
147 | - * return exit code of child (-1 if child is still alive) |
|
148 | - * |
|
149 | - * @return int |
|
150 | - */ |
|
146 | + /** |
|
147 | + * return exit code of child (-1 if child is still alive) |
|
148 | + * |
|
149 | + * @return int |
|
150 | + */ |
|
151 | 151 | public function getExitCode() { |
152 | 152 | $this->isAlive(); |
153 | 153 | return $this->exitCode; |
154 | 154 | } |
155 | 155 | |
156 | - /** |
|
157 | - * starts the thread, all the parameters are |
|
158 | - * passed to the callback function |
|
159 | - * |
|
160 | - * @return void |
|
161 | - */ |
|
156 | + /** |
|
157 | + * starts the thread, all the parameters are |
|
158 | + * passed to the callback function |
|
159 | + * |
|
160 | + * @return void |
|
161 | + */ |
|
162 | 162 | public function start() { |
163 | 163 | $pid = @ pcntl_fork(); |
164 | 164 | if( $pid == -1 ) { |
@@ -183,59 +183,59 @@ discard block |
||
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
186 | - /** |
|
187 | - * starts the thread, all the parameters are |
|
188 | - * passed to the callback function |
|
189 | - * |
|
190 | - * modification for fping threading for cron scanning |
|
191 | - * |
|
192 | - * @return void |
|
193 | - */ |
|
186 | + /** |
|
187 | + * starts the thread, all the parameters are |
|
188 | + * passed to the callback function |
|
189 | + * |
|
190 | + * modification for fping threading for cron scanning |
|
191 | + * |
|
192 | + * @return void |
|
193 | + */ |
|
194 | 194 | public function start_fping() { |
195 | - $status = 0; |
|
196 | - $results = null; |
|
197 | - $pid = pcntl_fork(); |
|
195 | + $status = 0; |
|
196 | + $results = null; |
|
197 | + $pid = pcntl_fork(); |
|
198 | 198 | |
199 | - if( $pid == -1 ) { //error forking, no child is created |
|
200 | - throw new Exception( $this->getError( Thread::COULD_NOT_FORK ), Thread::COULD_NOT_FORK ); |
|
201 | - }else if ( $pid ) {// parent |
|
202 | - $this->pid = $pid; |
|
199 | + if( $pid == -1 ) { //error forking, no child is created |
|
200 | + throw new Exception( $this->getError( Thread::COULD_NOT_FORK ), Thread::COULD_NOT_FORK ); |
|
201 | + }else if ( $pid ) {// parent |
|
202 | + $this->pid = $pid; |
|
203 | 203 | |
204 | - } else { // child |
|
205 | - $this->pid = posix_getpid();//pid (child) |
|
206 | - $this->ppid = posix_getppid();//pid (parent) |
|
204 | + } else { // child |
|
205 | + $this->pid = posix_getpid();//pid (child) |
|
206 | + $this->ppid = posix_getppid();//pid (parent) |
|
207 | 207 | |
208 | - pcntl_signal( SIGTERM, array( $this, 'signalHandler' ) ); |
|
209 | - $array_args = func_get_args(); |
|
210 | - if ( !empty( $array_args ) ) { |
|
211 | - $results = call_user_func_array( $this->runnable, $array_args ); |
|
212 | - }else{ |
|
213 | - $results = call_user_func( $this->runnable ); |
|
214 | - } |
|
208 | + pcntl_signal( SIGTERM, array( $this, 'signalHandler' ) ); |
|
209 | + $array_args = func_get_args(); |
|
210 | + if ( !empty( $array_args ) ) { |
|
211 | + $results = call_user_func_array( $this->runnable, $array_args ); |
|
212 | + }else{ |
|
213 | + $results = call_user_func( $this->runnable ); |
|
214 | + } |
|
215 | 215 | |
216 | - $pipe = "/tmp/pipe_".$this->pid;//pid is known by parent |
|
216 | + $pipe = "/tmp/pipe_".$this->pid;//pid is known by parent |
|
217 | 217 | |
218 | - if(!file_exists($pipe)) {//child talks to parent using this pipe |
|
219 | - umask(0); |
|
220 | - posix_mkfifo($pipe, 0600); |
|
221 | - } |
|
222 | - //we have to open the pipe and send the data serialized |
|
223 | - $pipe_descriptor = fopen($pipe, 'w'); |
|
224 | - fwrite($pipe_descriptor, serialize( $results ) ); |
|
218 | + if(!file_exists($pipe)) {//child talks to parent using this pipe |
|
219 | + umask(0); |
|
220 | + posix_mkfifo($pipe, 0600); |
|
221 | + } |
|
222 | + //we have to open the pipe and send the data serialized |
|
223 | + $pipe_descriptor = fopen($pipe, 'w'); |
|
224 | + fwrite($pipe_descriptor, serialize( $results ) ); |
|
225 | 225 | |
226 | - //and kill the child using posix_kill ( exit(0) duplicates headers!! ) |
|
227 | - posix_kill( $this->pid , SIGKILL); |
|
228 | - exit(0); |
|
229 | - } |
|
226 | + //and kill the child using posix_kill ( exit(0) duplicates headers!! ) |
|
227 | + posix_kill( $this->pid , SIGKILL); |
|
228 | + exit(0); |
|
229 | + } |
|
230 | 230 | } |
231 | 231 | |
232 | - /** |
|
233 | - * attempts to stop the thread |
|
234 | - * returns true on success and false otherwise |
|
235 | - * |
|
236 | - * @param integer $_signal - SIGKILL/SIGTERM |
|
237 | - * @param boolean $_wait |
|
238 | - */ |
|
232 | + /** |
|
233 | + * attempts to stop the thread |
|
234 | + * returns true on success and false otherwise |
|
235 | + * |
|
236 | + * @param integer $_signal - SIGKILL/SIGTERM |
|
237 | + * @param boolean $_wait |
|
238 | + */ |
|
239 | 239 | public function stop( $_signal = SIGKILL, $_wait = false ) { |
240 | 240 | if( $this->isAlive() ) { |
241 | 241 | posix_kill( $this->pid, $_signal ); |
@@ -245,22 +245,22 @@ discard block |
||
245 | 245 | } |
246 | 246 | } |
247 | 247 | |
248 | - /** |
|
249 | - * alias of stop(); |
|
250 | - * |
|
251 | - * @return boolean |
|
252 | - */ |
|
248 | + /** |
|
249 | + * alias of stop(); |
|
250 | + * |
|
251 | + * @return boolean |
|
252 | + */ |
|
253 | 253 | public function kill( $_signal = SIGKILL, $_wait = false ) { |
254 | 254 | return $this->stop( $_signal, $_wait ); |
255 | 255 | } |
256 | 256 | |
257 | - /** |
|
258 | - * gets the error's message based on |
|
259 | - * its id |
|
260 | - * |
|
261 | - * @param integer $_code |
|
262 | - * @return string |
|
263 | - */ |
|
257 | + /** |
|
258 | + * gets the error's message based on |
|
259 | + * its id |
|
260 | + * |
|
261 | + * @param integer $_code |
|
262 | + * @return string |
|
263 | + */ |
|
264 | 264 | public function getError( $_code ) { |
265 | 265 | if ( isset( $this->errors[$_code] ) ) { |
266 | 266 | return $this->errors[$_code]; |
@@ -270,11 +270,11 @@ discard block |
||
270 | 270 | } |
271 | 271 | } |
272 | 272 | |
273 | - /** |
|
274 | - * signal handler |
|
275 | - * |
|
276 | - * @param integer $_signal |
|
277 | - */ |
|
273 | + /** |
|
274 | + * signal handler |
|
275 | + * |
|
276 | + * @param integer $_signal |
|
277 | + */ |
|
278 | 278 | protected function signalHandler( $_signal ) { |
279 | 279 | switch( $_signal ) { |
280 | 280 | case SIGTERM: |
@@ -133,10 +133,13 @@ discard block |
||
133 | 133 | public function isAlive() { |
134 | 134 | $pid = pcntl_waitpid( $this->pid, $status, WNOHANG ); |
135 | 135 | |
136 | - if ($pid === 0) { // child is still alive |
|
136 | + if ($pid === 0) { |
|
137 | +// child is still alive |
|
137 | 138 | return true; |
138 | - } else { |
|
139 | - if (pcntl_wifexited($status) && $this->exitCode == -1) { // normal exit |
|
139 | + } |
|
140 | + else { |
|
141 | + if (pcntl_wifexited($status) && $this->exitCode == -1) { |
|
142 | +// normal exit |
|
140 | 143 | $this->exitCode = pcntl_wexitstatus($status); |
141 | 144 | } |
142 | 145 | return false; |
@@ -196,12 +199,17 @@ discard block |
||
196 | 199 | $results = null; |
197 | 200 | $pid = pcntl_fork(); |
198 | 201 | |
199 | - if( $pid == -1 ) { //error forking, no child is created |
|
202 | + if( $pid == -1 ) { |
|
203 | +//error forking, no child is created |
|
200 | 204 | throw new Exception( $this->getError( Thread::COULD_NOT_FORK ), Thread::COULD_NOT_FORK ); |
201 | - }else if ( $pid ) {// parent |
|
205 | + } |
|
206 | + else if ( $pid ) { |
|
207 | +// parent |
|
202 | 208 | $this->pid = $pid; |
203 | 209 | |
204 | - } else { // child |
|
210 | + } |
|
211 | + else { |
|
212 | +// child |
|
205 | 213 | $this->pid = posix_getpid();//pid (child) |
206 | 214 | $this->ppid = posix_getppid();//pid (parent) |
207 | 215 | |
@@ -209,13 +217,15 @@ discard block |
||
209 | 217 | $array_args = func_get_args(); |
210 | 218 | if ( !empty( $array_args ) ) { |
211 | 219 | $results = call_user_func_array( $this->runnable, $array_args ); |
212 | - }else{ |
|
220 | + } |
|
221 | + else { |
|
213 | 222 | $results = call_user_func( $this->runnable ); |
214 | 223 | } |
215 | 224 | |
216 | 225 | $pipe = "/tmp/pipe_".$this->pid;//pid is known by parent |
217 | 226 | |
218 | - if(!file_exists($pipe)) {//child talks to parent using this pipe |
|
227 | + if(!file_exists($pipe)) { |
|
228 | +//child talks to parent using this pipe |
|
219 | 229 | umask(0); |
220 | 230 | posix_mkfifo($pipe, 0600); |
221 | 231 | } |
@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | * |
17 | 17 | * @var array |
18 | 18 | */ |
19 | - private $errors = array( |
|
19 | + private $errors = array ( |
|
20 | 20 | Thread::FUNCTION_NOT_CALLABLE => 'You must specify a valid function name that can be called from the current scope.', |
21 | 21 | Thread::COULD_NOT_FORK => 'pcntl_fork() returned a status of -1. No new process was created', |
22 | 22 | ); |
@@ -52,13 +52,13 @@ discard block |
||
52 | 52 | * |
53 | 53 | * @return boolean |
54 | 54 | */ |
55 | - public static function available() { |
|
56 | - $required_functions = array( |
|
55 | + public static function available () { |
|
56 | + $required_functions = array ( |
|
57 | 57 | 'pcntl_fork', |
58 | 58 | ); |
59 | 59 | |
60 | - foreach( $required_functions as $function ) { |
|
61 | - if ( !function_exists( $function ) ) { |
|
60 | + foreach ($required_functions as $function) { |
|
61 | + if (!function_exists ($function)) { |
|
62 | 62 | return false; |
63 | 63 | } |
64 | 64 | } |
@@ -72,9 +72,9 @@ discard block |
||
72 | 72 | * |
73 | 73 | * @param callback $_runnable |
74 | 74 | */ |
75 | - public function __construct( $_runnable = null ) { |
|
76 | - if( $_runnable !== null ) { |
|
77 | - $this->setRunnable( $_runnable ); |
|
75 | + public function __construct ($_runnable = null) { |
|
76 | + if ($_runnable !== null) { |
|
77 | + $this->setRunnable ($_runnable); |
|
78 | 78 | } |
79 | 79 | } |
80 | 80 | |
@@ -84,12 +84,12 @@ discard block |
||
84 | 84 | * @param callback $_runnable |
85 | 85 | * @return callback |
86 | 86 | */ |
87 | - public function setRunnable( $_runnable ) { |
|
88 | - if( self::runnableOk( $_runnable ) ) { |
|
87 | + public function setRunnable ($_runnable) { |
|
88 | + if (self::runnableOk ($_runnable)) { |
|
89 | 89 | $this->runnable = $_runnable; |
90 | 90 | } |
91 | 91 | else { |
92 | - throw new Exception( $this->getError( Thread::FUNCTION_NOT_CALLABLE ), Thread::FUNCTION_NOT_CALLABLE ); |
|
92 | + throw new Exception ($this->getError (Thread::FUNCTION_NOT_CALLABLE), Thread::FUNCTION_NOT_CALLABLE); |
|
93 | 93 | } |
94 | 94 | } |
95 | 95 | |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | * |
99 | 99 | * @return callback |
100 | 100 | */ |
101 | - public function getRunnable() { |
|
101 | + public function getRunnable () { |
|
102 | 102 | return $this->runnable; |
103 | 103 | } |
104 | 104 | |
@@ -112,8 +112,8 @@ discard block |
||
112 | 112 | * @param callback $_runnable |
113 | 113 | * @return boolean |
114 | 114 | */ |
115 | - public static function runnableOk( $_runnable ) { |
|
116 | - return ( function_exists( $_runnable ) && is_callable( $_runnable ) ); |
|
115 | + public static function runnableOk ($_runnable) { |
|
116 | + return (function_exists ($_runnable) && is_callable ($_runnable)); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | /** |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | * |
122 | 122 | * @return int |
123 | 123 | */ |
124 | - public function getPid() { |
|
124 | + public function getPid () { |
|
125 | 125 | return $this->pid; |
126 | 126 | } |
127 | 127 | |
@@ -130,14 +130,14 @@ discard block |
||
130 | 130 | * |
131 | 131 | * @return boolean |
132 | 132 | */ |
133 | - public function isAlive() { |
|
134 | - $pid = pcntl_waitpid( $this->pid, $status, WNOHANG ); |
|
133 | + public function isAlive () { |
|
134 | + $pid = pcntl_waitpid ($this->pid, $status, WNOHANG); |
|
135 | 135 | |
136 | 136 | if ($pid === 0) { // child is still alive |
137 | 137 | return true; |
138 | 138 | } else { |
139 | - if (pcntl_wifexited($status) && $this->exitCode == -1) { // normal exit |
|
140 | - $this->exitCode = pcntl_wexitstatus($status); |
|
139 | + if (pcntl_wifexited ($status) && $this->exitCode == -1) { // normal exit |
|
140 | + $this->exitCode = pcntl_wexitstatus ($status); |
|
141 | 141 | } |
142 | 142 | return false; |
143 | 143 | } |
@@ -148,8 +148,8 @@ discard block |
||
148 | 148 | * |
149 | 149 | * @return int |
150 | 150 | */ |
151 | - public function getExitCode() { |
|
152 | - $this->isAlive(); |
|
151 | + public function getExitCode () { |
|
152 | + $this->isAlive (); |
|
153 | 153 | return $this->exitCode; |
154 | 154 | } |
155 | 155 | |
@@ -159,27 +159,27 @@ discard block |
||
159 | 159 | * |
160 | 160 | * @return void |
161 | 161 | */ |
162 | - public function start() { |
|
163 | - $pid = @ pcntl_fork(); |
|
164 | - if( $pid == -1 ) { |
|
165 | - throw new Exception( $this->getError( Thread::COULD_NOT_FORK ), Thread::COULD_NOT_FORK ); |
|
162 | + public function start () { |
|
163 | + $pid = @ pcntl_fork (); |
|
164 | + if ($pid == -1) { |
|
165 | + throw new Exception ($this->getError (Thread::COULD_NOT_FORK), Thread::COULD_NOT_FORK); |
|
166 | 166 | } |
167 | - if( $pid ) { |
|
167 | + if ($pid) { |
|
168 | 168 | // parent |
169 | 169 | $this->pid = $pid; |
170 | 170 | } |
171 | 171 | else { |
172 | 172 | // child |
173 | - pcntl_signal( SIGTERM, array( $this, 'signalHandler' ) ); |
|
174 | - $arguments = func_get_args(); |
|
175 | - if ( !empty( $arguments ) ) { |
|
176 | - call_user_func_array( $this->runnable, $arguments ); |
|
173 | + pcntl_signal (SIGTERM, array ($this, 'signalHandler')); |
|
174 | + $arguments = func_get_args (); |
|
175 | + if (!empty($arguments)) { |
|
176 | + call_user_func_array ($this->runnable, $arguments); |
|
177 | 177 | } |
178 | 178 | else { |
179 | - call_user_func( $this->runnable ); |
|
179 | + call_user_func ($this->runnable); |
|
180 | 180 | } |
181 | 181 | |
182 | - exit( 0 ); |
|
182 | + exit(0); |
|
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
@@ -191,40 +191,40 @@ discard block |
||
191 | 191 | * |
192 | 192 | * @return void |
193 | 193 | */ |
194 | - public function start_fping() { |
|
194 | + public function start_fping () { |
|
195 | 195 | $status = 0; |
196 | 196 | $results = null; |
197 | - $pid = pcntl_fork(); |
|
197 | + $pid = pcntl_fork (); |
|
198 | 198 | |
199 | - if( $pid == -1 ) { //error forking, no child is created |
|
200 | - throw new Exception( $this->getError( Thread::COULD_NOT_FORK ), Thread::COULD_NOT_FORK ); |
|
201 | - }else if ( $pid ) {// parent |
|
199 | + if ($pid == -1) { //error forking, no child is created |
|
200 | + throw new Exception ($this->getError (Thread::COULD_NOT_FORK), Thread::COULD_NOT_FORK); |
|
201 | + } else if ($pid) {// parent |
|
202 | 202 | $this->pid = $pid; |
203 | 203 | |
204 | 204 | } else { // child |
205 | - $this->pid = posix_getpid();//pid (child) |
|
206 | - $this->ppid = posix_getppid();//pid (parent) |
|
205 | + $this->pid = posix_getpid (); //pid (child) |
|
206 | + $this->ppid = posix_getppid (); //pid (parent) |
|
207 | 207 | |
208 | - pcntl_signal( SIGTERM, array( $this, 'signalHandler' ) ); |
|
209 | - $array_args = func_get_args(); |
|
210 | - if ( !empty( $array_args ) ) { |
|
211 | - $results = call_user_func_array( $this->runnable, $array_args ); |
|
212 | - }else{ |
|
213 | - $results = call_user_func( $this->runnable ); |
|
208 | + pcntl_signal (SIGTERM, array ($this, 'signalHandler')); |
|
209 | + $array_args = func_get_args (); |
|
210 | + if (!empty($array_args)) { |
|
211 | + $results = call_user_func_array ($this->runnable, $array_args); |
|
212 | + } else { |
|
213 | + $results = call_user_func ($this->runnable); |
|
214 | 214 | } |
215 | 215 | |
216 | - $pipe = "/tmp/pipe_".$this->pid;//pid is known by parent |
|
216 | + $pipe = "/tmp/pipe_".$this->pid; //pid is known by parent |
|
217 | 217 | |
218 | - if(!file_exists($pipe)) {//child talks to parent using this pipe |
|
219 | - umask(0); |
|
220 | - posix_mkfifo($pipe, 0600); |
|
218 | + if (!file_exists ($pipe)) {//child talks to parent using this pipe |
|
219 | + umask (0); |
|
220 | + posix_mkfifo ($pipe, 0600); |
|
221 | 221 | } |
222 | 222 | //we have to open the pipe and send the data serialized |
223 | - $pipe_descriptor = fopen($pipe, 'w'); |
|
224 | - fwrite($pipe_descriptor, serialize( $results ) ); |
|
223 | + $pipe_descriptor = fopen ($pipe, 'w'); |
|
224 | + fwrite ($pipe_descriptor, serialize ($results)); |
|
225 | 225 | |
226 | 226 | //and kill the child using posix_kill ( exit(0) duplicates headers!! ) |
227 | - posix_kill( $this->pid , SIGKILL); |
|
227 | + posix_kill ($this->pid, SIGKILL); |
|
228 | 228 | exit(0); |
229 | 229 | } |
230 | 230 | } |
@@ -236,11 +236,11 @@ discard block |
||
236 | 236 | * @param integer $_signal - SIGKILL/SIGTERM |
237 | 237 | * @param boolean $_wait |
238 | 238 | */ |
239 | - public function stop( $_signal = SIGKILL, $_wait = false ) { |
|
240 | - if( $this->isAlive() ) { |
|
241 | - posix_kill( $this->pid, $_signal ); |
|
242 | - if( $_wait ) { |
|
243 | - pcntl_waitpid( $this->pid, $status = 0 ); |
|
239 | + public function stop ($_signal = SIGKILL, $_wait = false) { |
|
240 | + if ($this->isAlive ()) { |
|
241 | + posix_kill ($this->pid, $_signal); |
|
242 | + if ($_wait) { |
|
243 | + pcntl_waitpid ($this->pid, $status = 0); |
|
244 | 244 | } |
245 | 245 | } |
246 | 246 | } |
@@ -250,8 +250,8 @@ discard block |
||
250 | 250 | * |
251 | 251 | * @return boolean |
252 | 252 | */ |
253 | - public function kill( $_signal = SIGKILL, $_wait = false ) { |
|
254 | - return $this->stop( $_signal, $_wait ); |
|
253 | + public function kill ($_signal = SIGKILL, $_wait = false) { |
|
254 | + return $this->stop ($_signal, $_wait); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | /** |
@@ -261,12 +261,12 @@ discard block |
||
261 | 261 | * @param integer $_code |
262 | 262 | * @return string |
263 | 263 | */ |
264 | - public function getError( $_code ) { |
|
265 | - if ( isset( $this->errors[$_code] ) ) { |
|
264 | + public function getError ($_code) { |
|
265 | + if (isset($this->errors[$_code])) { |
|
266 | 266 | return $this->errors[$_code]; |
267 | 267 | } |
268 | 268 | else { |
269 | - return 'No such error code ' . $_code . '! Quit inventing errors!!!'; |
|
269 | + return 'No such error code '.$_code.'! Quit inventing errors!!!'; |
|
270 | 270 | } |
271 | 271 | } |
272 | 272 | |
@@ -275,10 +275,10 @@ discard block |
||
275 | 275 | * |
276 | 276 | * @param integer $_signal |
277 | 277 | */ |
278 | - protected function signalHandler( $_signal ) { |
|
279 | - switch( $_signal ) { |
|
278 | + protected function signalHandler ($_signal) { |
|
279 | + switch ($_signal) { |
|
280 | 280 | case SIGTERM: |
281 | - exit( 0 ); |
|
281 | + exit(0); |
|
282 | 282 | break; |
283 | 283 | } |
284 | 284 | } |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | if($argv[1]!="update"&&$argv[1]!="discovery") { die(json_encode(array("status"=>1, "error"=>"Invalid scan type!"))); } |
53 | 53 | //verify cidr |
54 | 54 | if(!is_numeric($argv[2])) { |
55 | - if($Subnets->verify_cidr_address($argv[2])!==true) { die(json_encode(array("status"=>1, "error"=>"Invalid subnet CIDR address provided"))); } |
|
55 | + if($Subnets->verify_cidr_address($argv[2])!==true) { die(json_encode(array("status"=>1, "error"=>"Invalid subnet CIDR address provided"))); } |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
@@ -64,103 +64,103 @@ discard block |
||
64 | 64 | |
65 | 65 | # fping |
66 | 66 | if($Scan->settings->scanPingType=="fping" && $argv[1]=="discovery") { |
67 | - # fetch subnet |
|
68 | - $subnet = $Subnets->fetch_subnet(null, $argv[2]); |
|
69 | - $subnet!==false ? : die(json_encode(array("status"=>1, "error"=>"Invalid subnet ID provided"))); |
|
70 | - |
|
71 | - //set exit flag to true |
|
72 | - $Scan->ping_set_exit(false); |
|
73 | - |
|
74 | - # set cidr |
|
75 | - $subnet_cidr = $Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask; |
|
76 | - # execute |
|
77 | - $retval = $Scan->ping_address_method_fping_subnet ($subnet_cidr); |
|
78 | - |
|
79 | - # errors |
|
80 | - if($retval==3) { die(json_encode(array("status"=>1, "error"=>"invalid command line arguments"))); } |
|
81 | - if($retval==4) { die(json_encode(array("status"=>1, "error"=>"system call failure"))); } |
|
82 | - |
|
83 | - # parse result |
|
84 | - if(sizeof(@$Scan->fping_result)==0) { die(json_encode(array("status"=>0, "values"=>array("alive"=>null)))); } |
|
85 | - else { |
|
86 | - //check each line |
|
87 | - foreach($Scan->fping_result as $l) { |
|
88 | - //split |
|
89 | - $field = array_filter(explode(" ", $l)); |
|
90 | - //create result |
|
91 | - $out['alive'][] = $Subnets->transform_to_decimal($field[0]); |
|
92 | - } |
|
93 | - } |
|
67 | + # fetch subnet |
|
68 | + $subnet = $Subnets->fetch_subnet(null, $argv[2]); |
|
69 | + $subnet!==false ? : die(json_encode(array("status"=>1, "error"=>"Invalid subnet ID provided"))); |
|
70 | + |
|
71 | + //set exit flag to true |
|
72 | + $Scan->ping_set_exit(false); |
|
73 | + |
|
74 | + # set cidr |
|
75 | + $subnet_cidr = $Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask; |
|
76 | + # execute |
|
77 | + $retval = $Scan->ping_address_method_fping_subnet ($subnet_cidr); |
|
78 | + |
|
79 | + # errors |
|
80 | + if($retval==3) { die(json_encode(array("status"=>1, "error"=>"invalid command line arguments"))); } |
|
81 | + if($retval==4) { die(json_encode(array("status"=>1, "error"=>"system call failure"))); } |
|
82 | + |
|
83 | + # parse result |
|
84 | + if(sizeof(@$Scan->fping_result)==0) { die(json_encode(array("status"=>0, "values"=>array("alive"=>null)))); } |
|
85 | + else { |
|
86 | + //check each line |
|
87 | + foreach($Scan->fping_result as $l) { |
|
88 | + //split |
|
89 | + $field = array_filter(explode(" ", $l)); |
|
90 | + //create result |
|
91 | + $out['alive'][] = $Subnets->transform_to_decimal($field[0]); |
|
92 | + } |
|
93 | + } |
|
94 | 94 | } |
95 | 95 | # fping - status update |
96 | 96 | elseif($Scan->settings->scanPingType=="fping") { |
97 | - # fetch subnet |
|
98 | - $subnet = $Subnets->fetch_subnet(null, $argv[2]); |
|
99 | - $subnet!==false ? : die(json_encode(array("status"=>1, "error"=>"Invalid subnet ID provided"))); |
|
100 | - |
|
101 | - //set exit flag to true |
|
102 | - $Scan->ping_set_exit(false); |
|
103 | - |
|
104 | - # set cidr |
|
105 | - $subnet_cidr = $Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask; |
|
106 | - # execute |
|
107 | - $retval = $Scan->ping_address_method_fping_subnet ($subnet_cidr); |
|
108 | - |
|
109 | - # errors |
|
110 | - if($retval==3) { die(json_encode(array("status"=>1, "error"=>"invalid command line arguments"))); } |
|
111 | - if($retval==4) { die(json_encode(array("status"=>1, "error"=>"system call failure"))); } |
|
112 | - |
|
113 | - # parse result |
|
114 | - if(sizeof(@$Scan->fping_result)==0) { die(json_encode(array("status"=>0, "values"=>array("alive"=>null)))); } |
|
115 | - else { |
|
116 | - //check each line |
|
117 | - foreach($Scan->fping_result as $l) { |
|
118 | - //split |
|
119 | - $field = array_filter(explode(" ", $l)); |
|
120 | - //create result |
|
121 | - $out['alive'][] = $Subnets->transform_to_decimal($field[0]); |
|
122 | - } |
|
123 | - } |
|
97 | + # fetch subnet |
|
98 | + $subnet = $Subnets->fetch_subnet(null, $argv[2]); |
|
99 | + $subnet!==false ? : die(json_encode(array("status"=>1, "error"=>"Invalid subnet ID provided"))); |
|
100 | + |
|
101 | + //set exit flag to true |
|
102 | + $Scan->ping_set_exit(false); |
|
103 | + |
|
104 | + # set cidr |
|
105 | + $subnet_cidr = $Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask; |
|
106 | + # execute |
|
107 | + $retval = $Scan->ping_address_method_fping_subnet ($subnet_cidr); |
|
108 | + |
|
109 | + # errors |
|
110 | + if($retval==3) { die(json_encode(array("status"=>1, "error"=>"invalid command line arguments"))); } |
|
111 | + if($retval==4) { die(json_encode(array("status"=>1, "error"=>"system call failure"))); } |
|
112 | + |
|
113 | + # parse result |
|
114 | + if(sizeof(@$Scan->fping_result)==0) { die(json_encode(array("status"=>0, "values"=>array("alive"=>null)))); } |
|
115 | + else { |
|
116 | + //check each line |
|
117 | + foreach($Scan->fping_result as $l) { |
|
118 | + //split |
|
119 | + $field = array_filter(explode(" ", $l)); |
|
120 | + //create result |
|
121 | + $out['alive'][] = $Subnets->transform_to_decimal($field[0]); |
|
122 | + } |
|
123 | + } |
|
124 | 124 | } |
125 | 125 | # pear / ping |
126 | 126 | else { |
127 | - # Create array of addresses to scan |
|
128 | - $scan_addresses = $Scan->prepare_addresses_to_scan ($argv[1], $argv[2]); |
|
129 | - |
|
130 | - $z = 0; //addresses array index |
|
131 | - |
|
132 | - //run per MAX_THREADS |
|
133 | - for ($m=0; $m<=sizeof($scan_addresses); $m += $Scan->settings->scanMaxThreads) { |
|
134 | - // create threads |
|
135 | - $threads = array(); |
|
136 | - // fork processes |
|
137 | - for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($scan_addresses); $i++) { |
|
138 | - //only if index exists! |
|
139 | - if(isset($scan_addresses[$z])) { |
|
140 | - //start new thread |
|
141 | - $threads[$z] = new Thread( "ping_address" ); |
|
142 | - $threads[$z]->start( $Subnets->transform_to_dotted($scan_addresses[$z], true) ); |
|
143 | - |
|
144 | - $z++; //next index |
|
145 | - } |
|
146 | - } |
|
147 | - // wait for all the threads to finish |
|
148 | - while( !empty( $threads ) ) { |
|
149 | - foreach( $threads as $index => $thread ) { |
|
150 | - if( ! $thread->isAlive() ) { |
|
151 | - //online, save to array |
|
152 | - if($thread->getExitCode() == 0) { $out['alive'][] = $scan_addresses[$index]; } |
|
153 | - //ok, but offline |
|
154 | - elseif($thread->getExitCode() == 1 || $thread->getExitCode() == 2) { $out['dead'][] = $scan_addresses[$index];} |
|
155 | - //error |
|
156 | - else { $out['error'][] = $scan_addresses[$index]; } |
|
157 | - //remove thread |
|
158 | - unset( $threads[$index] ); |
|
159 | - } |
|
160 | - } |
|
161 | - usleep(100000); |
|
162 | - } |
|
163 | - } |
|
127 | + # Create array of addresses to scan |
|
128 | + $scan_addresses = $Scan->prepare_addresses_to_scan ($argv[1], $argv[2]); |
|
129 | + |
|
130 | + $z = 0; //addresses array index |
|
131 | + |
|
132 | + //run per MAX_THREADS |
|
133 | + for ($m=0; $m<=sizeof($scan_addresses); $m += $Scan->settings->scanMaxThreads) { |
|
134 | + // create threads |
|
135 | + $threads = array(); |
|
136 | + // fork processes |
|
137 | + for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($scan_addresses); $i++) { |
|
138 | + //only if index exists! |
|
139 | + if(isset($scan_addresses[$z])) { |
|
140 | + //start new thread |
|
141 | + $threads[$z] = new Thread( "ping_address" ); |
|
142 | + $threads[$z]->start( $Subnets->transform_to_dotted($scan_addresses[$z], true) ); |
|
143 | + |
|
144 | + $z++; //next index |
|
145 | + } |
|
146 | + } |
|
147 | + // wait for all the threads to finish |
|
148 | + while( !empty( $threads ) ) { |
|
149 | + foreach( $threads as $index => $thread ) { |
|
150 | + if( ! $thread->isAlive() ) { |
|
151 | + //online, save to array |
|
152 | + if($thread->getExitCode() == 0) { $out['alive'][] = $scan_addresses[$index]; } |
|
153 | + //ok, but offline |
|
154 | + elseif($thread->getExitCode() == 1 || $thread->getExitCode() == 2) { $out['dead'][] = $scan_addresses[$index];} |
|
155 | + //error |
|
156 | + else { $out['error'][] = $scan_addresses[$index]; } |
|
157 | + //remove thread |
|
158 | + unset( $threads[$index] ); |
|
159 | + } |
|
160 | + } |
|
161 | + usleep(100000); |
|
162 | + } |
|
163 | + } |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | # compose result - ok |
@@ -27,32 +27,32 @@ discard block |
||
27 | 27 | */ |
28 | 28 | |
29 | 29 | /* functions */ |
30 | -require( dirname(__FILE__) . '/../../functions/functions.php'); |
|
31 | -require( dirname(__FILE__) . '/../../functions/classes/class.Thread.php'); |
|
30 | +require(dirname (__FILE__).'/../../functions/functions.php'); |
|
31 | +require(dirname (__FILE__).'/../../functions/classes/class.Thread.php'); |
|
32 | 32 | |
33 | 33 | # initialize user object |
34 | -$Database = new Database_PDO; |
|
35 | -$Subnets = new Subnets ($Database); |
|
36 | -$Scan = new Scan ($Database); |
|
34 | +$Database = new Database_PDO; |
|
35 | +$Subnets = new Subnets ($Database); |
|
36 | +$Scan = new Scan ($Database); |
|
37 | 37 | |
38 | 38 | //set exit flag to true |
39 | -$Scan->ping_set_exit(true); |
|
39 | +$Scan->ping_set_exit (true); |
|
40 | 40 | |
41 | 41 | /** |
42 | 42 | * Input checks |
43 | 43 | */ |
44 | 44 | |
45 | 45 | //script can only be run from cli |
46 | -if(php_sapi_name()!="cli") { die(json_encode(array("status"=>1, "error"=>"This script can only be run from cli!"))); } |
|
46 | +if (php_sapi_name () != "cli") { die(json_encode (array ("status"=>1, "error"=>"This script can only be run from cli!"))); } |
|
47 | 47 | //check input parameters |
48 | -if(!isset($argv[1]) || !isset($argv[2])) { die(json_encode(array("status"=>1, "error"=>"Missing required input parameters"))); } |
|
48 | +if (!isset($argv[1]) || !isset($argv[2])) { die(json_encode (array ("status"=>1, "error"=>"Missing required input parameters"))); } |
|
49 | 49 | // test to see if threading is available |
50 | -if( !Thread::available() ) { die(json_encode(array("status"=>1, "error"=>"Threading is required for scanning subnets. Please recompile PHP with pcntl extension"))); } |
|
50 | +if (!Thread::available ()) { die(json_encode (array ("status"=>1, "error"=>"Threading is required for scanning subnets. Please recompile PHP with pcntl extension"))); } |
|
51 | 51 | //check script |
52 | -if($argv[1]!="update"&&$argv[1]!="discovery") { die(json_encode(array("status"=>1, "error"=>"Invalid scan type!"))); } |
|
52 | +if ($argv[1] != "update" && $argv[1] != "discovery") { die(json_encode (array ("status"=>1, "error"=>"Invalid scan type!"))); } |
|
53 | 53 | //verify cidr |
54 | -if(!is_numeric($argv[2])) { |
|
55 | - if($Subnets->verify_cidr_address($argv[2])!==true) { die(json_encode(array("status"=>1, "error"=>"Invalid subnet CIDR address provided"))); } |
|
54 | +if (!is_numeric ($argv[2])) { |
|
55 | + if ($Subnets->verify_cidr_address ($argv[2]) !== true) { die(json_encode (array ("status"=>1, "error"=>"Invalid subnet CIDR address provided"))); } |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | /** |
@@ -63,62 +63,62 @@ discard block |
||
63 | 63 | |
64 | 64 | |
65 | 65 | # fping |
66 | -if($Scan->settings->scanPingType=="fping" && $argv[1]=="discovery") { |
|
66 | +if ($Scan->settings->scanPingType == "fping" && $argv[1] == "discovery") { |
|
67 | 67 | # fetch subnet |
68 | - $subnet = $Subnets->fetch_subnet(null, $argv[2]); |
|
69 | - $subnet!==false ? : die(json_encode(array("status"=>1, "error"=>"Invalid subnet ID provided"))); |
|
68 | + $subnet = $Subnets->fetch_subnet (null, $argv[2]); |
|
69 | + $subnet !== false ? : die(json_encode (array ("status"=>1, "error"=>"Invalid subnet ID provided"))); |
|
70 | 70 | |
71 | 71 | //set exit flag to true |
72 | - $Scan->ping_set_exit(false); |
|
72 | + $Scan->ping_set_exit (false); |
|
73 | 73 | |
74 | 74 | # set cidr |
75 | - $subnet_cidr = $Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask; |
|
75 | + $subnet_cidr = $Subnets->transform_to_dotted ($subnet->subnet)."/".$subnet->mask; |
|
76 | 76 | # execute |
77 | 77 | $retval = $Scan->ping_address_method_fping_subnet ($subnet_cidr); |
78 | 78 | |
79 | 79 | # errors |
80 | - if($retval==3) { die(json_encode(array("status"=>1, "error"=>"invalid command line arguments"))); } |
|
81 | - if($retval==4) { die(json_encode(array("status"=>1, "error"=>"system call failure"))); } |
|
80 | + if ($retval == 3) { die(json_encode (array ("status"=>1, "error"=>"invalid command line arguments"))); } |
|
81 | + if ($retval == 4) { die(json_encode (array ("status"=>1, "error"=>"system call failure"))); } |
|
82 | 82 | |
83 | 83 | # parse result |
84 | - if(sizeof(@$Scan->fping_result)==0) { die(json_encode(array("status"=>0, "values"=>array("alive"=>null)))); } |
|
84 | + if (sizeof (@$Scan->fping_result) == 0) { die(json_encode (array ("status"=>0, "values"=>array ("alive"=>null)))); } |
|
85 | 85 | else { |
86 | 86 | //check each line |
87 | - foreach($Scan->fping_result as $l) { |
|
87 | + foreach ($Scan->fping_result as $l) { |
|
88 | 88 | //split |
89 | - $field = array_filter(explode(" ", $l)); |
|
89 | + $field = array_filter (explode (" ", $l)); |
|
90 | 90 | //create result |
91 | - $out['alive'][] = $Subnets->transform_to_decimal($field[0]); |
|
91 | + $out['alive'][] = $Subnets->transform_to_decimal ($field[0]); |
|
92 | 92 | } |
93 | 93 | } |
94 | 94 | } |
95 | 95 | # fping - status update |
96 | -elseif($Scan->settings->scanPingType=="fping") { |
|
96 | +elseif ($Scan->settings->scanPingType == "fping") { |
|
97 | 97 | # fetch subnet |
98 | - $subnet = $Subnets->fetch_subnet(null, $argv[2]); |
|
99 | - $subnet!==false ? : die(json_encode(array("status"=>1, "error"=>"Invalid subnet ID provided"))); |
|
98 | + $subnet = $Subnets->fetch_subnet (null, $argv[2]); |
|
99 | + $subnet !== false ? : die(json_encode (array ("status"=>1, "error"=>"Invalid subnet ID provided"))); |
|
100 | 100 | |
101 | 101 | //set exit flag to true |
102 | - $Scan->ping_set_exit(false); |
|
102 | + $Scan->ping_set_exit (false); |
|
103 | 103 | |
104 | 104 | # set cidr |
105 | - $subnet_cidr = $Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask; |
|
105 | + $subnet_cidr = $Subnets->transform_to_dotted ($subnet->subnet)."/".$subnet->mask; |
|
106 | 106 | # execute |
107 | 107 | $retval = $Scan->ping_address_method_fping_subnet ($subnet_cidr); |
108 | 108 | |
109 | 109 | # errors |
110 | - if($retval==3) { die(json_encode(array("status"=>1, "error"=>"invalid command line arguments"))); } |
|
111 | - if($retval==4) { die(json_encode(array("status"=>1, "error"=>"system call failure"))); } |
|
110 | + if ($retval == 3) { die(json_encode (array ("status"=>1, "error"=>"invalid command line arguments"))); } |
|
111 | + if ($retval == 4) { die(json_encode (array ("status"=>1, "error"=>"system call failure"))); } |
|
112 | 112 | |
113 | 113 | # parse result |
114 | - if(sizeof(@$Scan->fping_result)==0) { die(json_encode(array("status"=>0, "values"=>array("alive"=>null)))); } |
|
114 | + if (sizeof (@$Scan->fping_result) == 0) { die(json_encode (array ("status"=>0, "values"=>array ("alive"=>null)))); } |
|
115 | 115 | else { |
116 | 116 | //check each line |
117 | - foreach($Scan->fping_result as $l) { |
|
117 | + foreach ($Scan->fping_result as $l) { |
|
118 | 118 | //split |
119 | - $field = array_filter(explode(" ", $l)); |
|
119 | + $field = array_filter (explode (" ", $l)); |
|
120 | 120 | //create result |
121 | - $out['alive'][] = $Subnets->transform_to_decimal($field[0]); |
|
121 | + $out['alive'][] = $Subnets->transform_to_decimal ($field[0]); |
|
122 | 122 | } |
123 | 123 | } |
124 | 124 | } |
@@ -127,38 +127,38 @@ discard block |
||
127 | 127 | # Create array of addresses to scan |
128 | 128 | $scan_addresses = $Scan->prepare_addresses_to_scan ($argv[1], $argv[2]); |
129 | 129 | |
130 | - $z = 0; //addresses array index |
|
130 | + $z = 0; //addresses array index |
|
131 | 131 | |
132 | 132 | //run per MAX_THREADS |
133 | - for ($m=0; $m<=sizeof($scan_addresses); $m += $Scan->settings->scanMaxThreads) { |
|
133 | + for ($m = 0; $m <= sizeof ($scan_addresses); $m += $Scan->settings->scanMaxThreads) { |
|
134 | 134 | // create threads |
135 | - $threads = array(); |
|
135 | + $threads = array (); |
|
136 | 136 | // fork processes |
137 | - for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($scan_addresses); $i++) { |
|
137 | + for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof ($scan_addresses); $i++) { |
|
138 | 138 | //only if index exists! |
139 | - if(isset($scan_addresses[$z])) { |
|
139 | + if (isset($scan_addresses[$z])) { |
|
140 | 140 | //start new thread |
141 | - $threads[$z] = new Thread( "ping_address" ); |
|
142 | - $threads[$z]->start( $Subnets->transform_to_dotted($scan_addresses[$z], true) ); |
|
141 | + $threads[$z] = new Thread ("ping_address"); |
|
142 | + $threads[$z]->start ($Subnets->transform_to_dotted ($scan_addresses[$z], true)); |
|
143 | 143 | |
144 | - $z++; //next index |
|
144 | + $z++; //next index |
|
145 | 145 | } |
146 | 146 | } |
147 | 147 | // wait for all the threads to finish |
148 | - while( !empty( $threads ) ) { |
|
149 | - foreach( $threads as $index => $thread ) { |
|
150 | - if( ! $thread->isAlive() ) { |
|
148 | + while (!empty($threads)) { |
|
149 | + foreach ($threads as $index => $thread) { |
|
150 | + if (!$thread->isAlive ()) { |
|
151 | 151 | //online, save to array |
152 | - if($thread->getExitCode() == 0) { $out['alive'][] = $scan_addresses[$index]; } |
|
152 | + if ($thread->getExitCode () == 0) { $out['alive'][] = $scan_addresses[$index]; } |
|
153 | 153 | //ok, but offline |
154 | - elseif($thread->getExitCode() == 1 || $thread->getExitCode() == 2) { $out['dead'][] = $scan_addresses[$index];} |
|
154 | + elseif ($thread->getExitCode () == 1 || $thread->getExitCode () == 2) { $out['dead'][] = $scan_addresses[$index]; } |
|
155 | 155 | //error |
156 | - else { $out['error'][] = $scan_addresses[$index]; } |
|
156 | + else { $out['error'][] = $scan_addresses[$index]; } |
|
157 | 157 | //remove thread |
158 | - unset( $threads[$index] ); |
|
158 | + unset($threads[$index]); |
|
159 | 159 | } |
160 | 160 | } |
161 | - usleep(100000); |
|
161 | + usleep (100000); |
|
162 | 162 | } |
163 | 163 | } |
164 | 164 | } |
@@ -168,8 +168,8 @@ discard block |
||
168 | 168 | $result['values'] = @$out; |
169 | 169 | |
170 | 170 | # save to json |
171 | -$out = json_encode(@$result); |
|
171 | +$out = json_encode (@$result); |
|
172 | 172 | |
173 | 173 | # print result |
174 | -print_r($out); |
|
174 | +print_r ($out); |
|
175 | 175 | ?> |
176 | 176 | \ No newline at end of file |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | |
62 | 62 | //reset array, set each IP together with port |
63 | 63 | foreach($scan_addresses as $k=>$v) { |
64 | - foreach($ports as $p) { |
|
65 | - $addresses[] = array("ip"=>$v, "port"=>$p); |
|
66 | - } |
|
64 | + foreach($ports as $p) { |
|
65 | + $addresses[] = array("ip"=>$v, "port"=>$p); |
|
66 | + } |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | |
@@ -73,22 +73,22 @@ discard block |
||
73 | 73 | $threads = array(); |
74 | 74 | //fork processes |
75 | 75 | for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($addresses); $i++) { |
76 | - //only if index exists! |
|
77 | - if(isset($addresses[$z])) { |
|
78 | - //start new thread |
|
76 | + //only if index exists! |
|
77 | + if(isset($addresses[$z])) { |
|
78 | + //start new thread |
|
79 | 79 | $threads[$z] = new Thread( 'telnet_address' ); |
80 | 80 | $threads[$z]->start( $Subnets->transform_to_dotted($addresses[$z]['ip']), $addresses[$z]['port'], 2); |
81 | 81 | $z++; //next index |
82 | - } |
|
82 | + } |
|
83 | 83 | } |
84 | 84 | //wait for all the threads to finish |
85 | 85 | while( !empty( $threads ) ) { |
86 | 86 | foreach( $threads as $index => $thread ) { |
87 | 87 | if( ! $thread->isAlive() ) { |
88 | - //online, save to array |
|
89 | - if($thread->getExitCode() == 0) { $out['alive'][$addresses[$index]['ip']][] = $addresses[$index]['port']; } |
|
90 | - //ok, but offline |
|
91 | - else { $out['dead'][$addresses[$index]['ip']][] = $addresses[$index]['port'];} |
|
88 | + //online, save to array |
|
89 | + if($thread->getExitCode() == 0) { $out['alive'][$addresses[$index]['ip']][] = $addresses[$index]['port']; } |
|
90 | + //ok, but offline |
|
91 | + else { $out['dead'][$addresses[$index]['ip']][] = $addresses[$index]['port'];} |
|
92 | 92 | //remove thread |
93 | 93 | unset( $threads[$index] ); |
94 | 94 | } |
@@ -21,28 +21,28 @@ discard block |
||
21 | 21 | */ |
22 | 22 | |
23 | 23 | /* functions */ |
24 | -require( dirname(__FILE__) . '/../../functions/functions.php'); |
|
25 | -require( dirname(__FILE__) . '/../../functions/classes/class.Thread.php'); |
|
24 | +require(dirname (__FILE__).'/../../functions/functions.php'); |
|
25 | +require(dirname (__FILE__).'/../../functions/classes/class.Thread.php'); |
|
26 | 26 | |
27 | 27 | # initialize user object |
28 | 28 | $Database = new Database_PDO; |
29 | -$Subnets = new Subnets ($Database); |
|
29 | +$Subnets = new Subnets ($Database); |
|
30 | 30 | $Addresses = new Addresses ($Database); |
31 | -$Scan = new Scan ($Database); |
|
31 | +$Scan = new Scan ($Database); |
|
32 | 32 | |
33 | 33 | //set exit flag to true |
34 | -$Scan->ping_set_exit(true); |
|
34 | +$Scan->ping_set_exit (true); |
|
35 | 35 | |
36 | 36 | /** |
37 | 37 | * Input checks |
38 | 38 | */ |
39 | 39 | |
40 | 40 | //script can only be run from cli |
41 | -if(php_sapi_name()!="cli") { die(json_encode(array("status"=>1, "error"=>"This script can only be run from cli!"))); } |
|
41 | +if (php_sapi_name () != "cli") { die(json_encode (array ("status"=>1, "error"=>"This script can only be run from cli!"))); } |
|
42 | 42 | //check input parameters |
43 | -if(!isset($argv[1]) || !isset($argv[2])) { die(json_encode(array("status"=>1, "error"=>"Missing required input parameters"))); } |
|
43 | +if (!isset($argv[1]) || !isset($argv[2])) { die(json_encode (array ("status"=>1, "error"=>"Missing required input parameters"))); } |
|
44 | 44 | // test to see if threading is available |
45 | -if( !Thread::available() ) { die(json_encode(array("status"=>1, "error"=>"Threading is required for scanning subnets. Please recompile PHP with pcntl extension"))); } |
|
45 | +if (!Thread::available ()) { die(json_encode (array ("status"=>1, "error"=>"Threading is required for scanning subnets. Please recompile PHP with pcntl extension"))); } |
|
46 | 46 | |
47 | 47 | /** |
48 | 48 | * Create array of addresses to scan |
@@ -50,50 +50,50 @@ discard block |
||
50 | 50 | $scan_addresses = $Scan->prepare_addresses_to_scan ("discovery", $argv[1]); |
51 | 51 | |
52 | 52 | |
53 | -$z = 0; //addresses array index |
|
53 | +$z = 0; //addresses array index |
|
54 | 54 | |
55 | 55 | /* |
56 | 56 | test |
57 | 57 | */ |
58 | -$ports = explode(";", $argv[2]); |
|
58 | +$ports = explode (";", $argv[2]); |
|
59 | 59 | |
60 | -$out = array(); |
|
60 | +$out = array (); |
|
61 | 61 | |
62 | 62 | //reset array, set each IP together with port |
63 | -foreach($scan_addresses as $k=>$v) { |
|
64 | - foreach($ports as $p) { |
|
65 | - $addresses[] = array("ip"=>$v, "port"=>$p); |
|
63 | +foreach ($scan_addresses as $k=>$v) { |
|
64 | + foreach ($ports as $p) { |
|
65 | + $addresses[] = array ("ip"=>$v, "port"=>$p); |
|
66 | 66 | } |
67 | 67 | } |
68 | 68 | |
69 | 69 | |
70 | 70 | # run per MAX_THREADS |
71 | -for ($m=0; $m<=sizeof($addresses); $m += $Scan->settings->scanMaxThreads) { |
|
71 | +for ($m = 0; $m <= sizeof ($addresses); $m += $Scan->settings->scanMaxThreads) { |
|
72 | 72 | //create threads |
73 | - $threads = array(); |
|
73 | + $threads = array (); |
|
74 | 74 | //fork processes |
75 | - for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($addresses); $i++) { |
|
75 | + for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof ($addresses); $i++) { |
|
76 | 76 | //only if index exists! |
77 | - if(isset($addresses[$z])) { |
|
77 | + if (isset($addresses[$z])) { |
|
78 | 78 | //start new thread |
79 | - $threads[$z] = new Thread( 'telnet_address' ); |
|
80 | - $threads[$z]->start( $Subnets->transform_to_dotted($addresses[$z]['ip']), $addresses[$z]['port'], 2); |
|
81 | - $z++; //next index |
|
79 | + $threads[$z] = new Thread ('telnet_address'); |
|
80 | + $threads[$z]->start ($Subnets->transform_to_dotted ($addresses[$z]['ip']), $addresses[$z]['port'], 2); |
|
81 | + $z++; //next index |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 | //wait for all the threads to finish |
85 | - while( !empty( $threads ) ) { |
|
86 | - foreach( $threads as $index => $thread ) { |
|
87 | - if( ! $thread->isAlive() ) { |
|
85 | + while (!empty($threads)) { |
|
86 | + foreach ($threads as $index => $thread) { |
|
87 | + if (!$thread->isAlive ()) { |
|
88 | 88 | //online, save to array |
89 | - if($thread->getExitCode() == 0) { $out['alive'][$addresses[$index]['ip']][] = $addresses[$index]['port']; } |
|
89 | + if ($thread->getExitCode () == 0) { $out['alive'][$addresses[$index]['ip']][] = $addresses[$index]['port']; } |
|
90 | 90 | //ok, but offline |
91 | - else { $out['dead'][$addresses[$index]['ip']][] = $addresses[$index]['port'];} |
|
91 | + else { $out['dead'][$addresses[$index]['ip']][] = $addresses[$index]['port']; } |
|
92 | 92 | //remove thread |
93 | - unset( $threads[$index] ); |
|
93 | + unset($threads[$index]); |
|
94 | 94 | } |
95 | 95 | } |
96 | - usleep(100000); |
|
96 | + usleep (100000); |
|
97 | 97 | } |
98 | 98 | } |
99 | 99 | |
@@ -102,8 +102,8 @@ discard block |
||
102 | 102 | $result['values'] = @$out; |
103 | 103 | |
104 | 104 | # save to json |
105 | -$out = json_encode(@$result); |
|
105 | +$out = json_encode (@$result); |
|
106 | 106 | |
107 | 107 | # print result |
108 | -print_r($out); |
|
108 | +print_r ($out); |
|
109 | 109 | ?> |
110 | 110 | \ No newline at end of file |
@@ -84,32 +84,32 @@ discard block |
||
84 | 84 | //fetch all addresses that need to be checked |
85 | 85 | foreach($scan_subnets as $s) { |
86 | 86 | |
87 | - // if subnet has slaves dont check it |
|
88 | - if ($Subnets->has_slaves ($s->id) === false) { |
|
89 | - |
|
90 | - $subnet_addresses = $Addresses->fetch_subnet_addresses ($s->id); |
|
91 | - //set array for fping |
|
92 | - if($Scan->icmp_type=="fping") { |
|
93 | - $subnets[] = array("id"=>$s->id, "cidr"=>$Subnets->transform_to_dotted($s->subnet)."/".$s->mask); |
|
94 | - } |
|
95 | - //save addresses |
|
96 | - if(sizeof($subnet_addresses)>1) { |
|
97 | - foreach($subnet_addresses as $a) { |
|
98 | - //ignore excludePing |
|
99 | - if($a->excludePing!=1) { |
|
100 | - //create different array for fping |
|
101 | - if($Scan->icmp_type=="fping") { |
|
102 | - $addresses2[$s->id][$a->id] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); //used for status check |
|
103 | - $addresses[$s->id][$a->id] = $a->ip_addr; //used for alive check |
|
104 | - } |
|
105 | - else { |
|
106 | - $addresses[] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - } |
|
87 | + // if subnet has slaves dont check it |
|
88 | + if ($Subnets->has_slaves ($s->id) === false) { |
|
89 | + |
|
90 | + $subnet_addresses = $Addresses->fetch_subnet_addresses ($s->id); |
|
91 | + //set array for fping |
|
92 | + if($Scan->icmp_type=="fping") { |
|
93 | + $subnets[] = array("id"=>$s->id, "cidr"=>$Subnets->transform_to_dotted($s->subnet)."/".$s->mask); |
|
94 | + } |
|
95 | + //save addresses |
|
96 | + if(sizeof($subnet_addresses)>1) { |
|
97 | + foreach($subnet_addresses as $a) { |
|
98 | + //ignore excludePing |
|
99 | + if($a->excludePing!=1) { |
|
100 | + //create different array for fping |
|
101 | + if($Scan->icmp_type=="fping") { |
|
102 | + $addresses2[$s->id][$a->id] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); //used for status check |
|
103 | + $addresses[$s->id][$a->id] = $a->ip_addr; //used for alive check |
|
104 | + } |
|
105 | + else { |
|
106 | + $addresses[] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + } |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | |
@@ -124,126 +124,126 @@ discard block |
||
124 | 124 | |
125 | 125 | //different scan for fping |
126 | 126 | if($Scan->icmp_type=="fping") { |
127 | - //run per MAX_THREADS |
|
128 | - for ($m=0; $m<=sizeof($subnets); $m += $Scan->settings->scanMaxThreads) { |
|
129 | - // create threads |
|
130 | - $threads = array(); |
|
131 | - //fork processes |
|
132 | - for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($subnets); $i++) { |
|
133 | - //only if index exists! |
|
134 | - if(isset($subnets[$z])) { |
|
135 | - //start new thread |
|
136 | - $threads[$z] = new Thread( 'fping_subnet' ); |
|
137 | - $threads[$z]->start_fping( $subnets[$z]['cidr'] ); |
|
138 | - $z++; //next index |
|
139 | - } |
|
140 | - } |
|
141 | - // wait for all the threads to finish |
|
142 | - while( !empty( $threads ) ) { |
|
143 | - foreach($threads as $index => $thread) { |
|
144 | - $child_pipe = "/tmp/pipe_".$thread->getPid(); |
|
145 | - |
|
146 | - if (file_exists($child_pipe)) { |
|
147 | - $file_descriptor = fopen( $child_pipe, "r"); |
|
148 | - $child_response = ""; |
|
149 | - while (!feof($file_descriptor)) { |
|
150 | - $child_response .= fread($file_descriptor, 8192); |
|
151 | - } |
|
152 | - //we have the child data in the parent, but serialized: |
|
153 | - $child_response = unserialize( $child_response ); |
|
154 | - //store |
|
155 | - $subnets[$index]['result'] = $child_response; |
|
156 | - |
|
157 | - //now, child is dead, and parent close the pipe |
|
158 | - unlink( $child_pipe ); |
|
159 | - unset($threads[$index]); |
|
160 | - } |
|
161 | - } |
|
162 | - usleep(200000); |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - //now we must remove all non-existing hosts |
|
167 | - foreach($subnets as $sk=>$s) { |
|
168 | - if(sizeof(@$s['result'])>0 && isset($addresses[$s['id']])) { |
|
169 | - //loop addresses |
|
170 | - foreach($addresses[$s['id']] as $ak=>$a) { |
|
171 | - //offline host |
|
172 | - if(array_search($Subnets->transform_to_dotted($a), $subnets[$sk]['result'])===false) { |
|
173 | - // new change = null |
|
174 | - $addresses2[$s['id']][$ak]['lastSeenNew'] = NULL; |
|
175 | - //save to out array |
|
127 | + //run per MAX_THREADS |
|
128 | + for ($m=0; $m<=sizeof($subnets); $m += $Scan->settings->scanMaxThreads) { |
|
129 | + // create threads |
|
130 | + $threads = array(); |
|
131 | + //fork processes |
|
132 | + for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($subnets); $i++) { |
|
133 | + //only if index exists! |
|
134 | + if(isset($subnets[$z])) { |
|
135 | + //start new thread |
|
136 | + $threads[$z] = new Thread( 'fping_subnet' ); |
|
137 | + $threads[$z]->start_fping( $subnets[$z]['cidr'] ); |
|
138 | + $z++; //next index |
|
139 | + } |
|
140 | + } |
|
141 | + // wait for all the threads to finish |
|
142 | + while( !empty( $threads ) ) { |
|
143 | + foreach($threads as $index => $thread) { |
|
144 | + $child_pipe = "/tmp/pipe_".$thread->getPid(); |
|
145 | + |
|
146 | + if (file_exists($child_pipe)) { |
|
147 | + $file_descriptor = fopen( $child_pipe, "r"); |
|
148 | + $child_response = ""; |
|
149 | + while (!feof($file_descriptor)) { |
|
150 | + $child_response .= fread($file_descriptor, 8192); |
|
151 | + } |
|
152 | + //we have the child data in the parent, but serialized: |
|
153 | + $child_response = unserialize( $child_response ); |
|
154 | + //store |
|
155 | + $subnets[$index]['result'] = $child_response; |
|
156 | + |
|
157 | + //now, child is dead, and parent close the pipe |
|
158 | + unlink( $child_pipe ); |
|
159 | + unset($threads[$index]); |
|
160 | + } |
|
161 | + } |
|
162 | + usleep(200000); |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + //now we must remove all non-existing hosts |
|
167 | + foreach($subnets as $sk=>$s) { |
|
168 | + if(sizeof(@$s['result'])>0 && isset($addresses[$s['id']])) { |
|
169 | + //loop addresses |
|
170 | + foreach($addresses[$s['id']] as $ak=>$a) { |
|
171 | + //offline host |
|
172 | + if(array_search($Subnets->transform_to_dotted($a), $subnets[$sk]['result'])===false) { |
|
173 | + // new change = null |
|
174 | + $addresses2[$s['id']][$ak]['lastSeenNew'] = NULL; |
|
175 | + //save to out array |
|
176 | 176 | $address_change[] = $addresses2[$s['id']][$ak]; |
177 | - } |
|
178 | - //online host |
|
179 | - else { |
|
180 | - // new change = now |
|
181 | - $addresses2[$s['id']][$ak]['lastSeenNew'] = $nowdate; |
|
182 | - //save to out array |
|
177 | + } |
|
178 | + //online host |
|
179 | + else { |
|
180 | + // new change = now |
|
181 | + $addresses2[$s['id']][$ak]['lastSeenNew'] = $nowdate; |
|
182 | + //save to out array |
|
183 | 183 | $address_change[] = $addresses2[$s['id']][$ak]; |
184 | 184 | //update status |
185 | 185 | $Scan->ping_update_lastseen ($addresses2[$s['id']][$ak]['id'], $nowdate); |
186 | - } |
|
187 | - } |
|
188 | - } |
|
189 | - } |
|
186 | + } |
|
187 | + } |
|
188 | + } |
|
189 | + } |
|
190 | 190 | } |
191 | 191 | //ping, pear |
192 | 192 | else { |
193 | - //run per MAX_THREADS |
|
194 | - for ($m=0; $m<=sizeof($addresses); $m += $Scan->settings->scanMaxThreads) { |
|
195 | - // create threads |
|
196 | - $threads = array(); |
|
197 | - //fork processes |
|
198 | - for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($addresses); $i++) { |
|
199 | - //only if index exists! |
|
200 | - if(isset($addresses[$z])) { |
|
201 | - //start new thread |
|
202 | - $threads[$z] = new Thread( 'ping_address' ); |
|
203 | - $threads[$z]->start($Subnets->transform_to_dotted($addresses[$z]['ip_addr'])); |
|
204 | - $z++; //next index |
|
205 | - } |
|
206 | - } |
|
207 | - // wait for all the threads to finish |
|
208 | - while( !empty( $threads ) ) { |
|
209 | - foreach( $threads as $index => $thread ) { |
|
210 | - if( ! $thread->isAlive() ) { |
|
211 | - //online |
|
212 | - if($thread->getExitCode() == 0) { |
|
213 | - // set new available time |
|
214 | - $addresses[$index]['lastSeenNew'] = $nowdate; |
|
193 | + //run per MAX_THREADS |
|
194 | + for ($m=0; $m<=sizeof($addresses); $m += $Scan->settings->scanMaxThreads) { |
|
195 | + // create threads |
|
196 | + $threads = array(); |
|
197 | + //fork processes |
|
198 | + for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($addresses); $i++) { |
|
199 | + //only if index exists! |
|
200 | + if(isset($addresses[$z])) { |
|
201 | + //start new thread |
|
202 | + $threads[$z] = new Thread( 'ping_address' ); |
|
203 | + $threads[$z]->start($Subnets->transform_to_dotted($addresses[$z]['ip_addr'])); |
|
204 | + $z++; //next index |
|
205 | + } |
|
206 | + } |
|
207 | + // wait for all the threads to finish |
|
208 | + while( !empty( $threads ) ) { |
|
209 | + foreach( $threads as $index => $thread ) { |
|
210 | + if( ! $thread->isAlive() ) { |
|
211 | + //online |
|
212 | + if($thread->getExitCode() == 0) { |
|
213 | + // set new available time |
|
214 | + $addresses[$index]['lastSeenNew'] = $nowdate; |
|
215 | 215 | $address_change[$index] = $addresses[$index]; //change to online |
216 | - } |
|
217 | - //offline |
|
218 | - else { |
|
219 | - // set nw online |
|
220 | - $addresses[$index]['lastSeenNew'] = NULL; |
|
216 | + } |
|
217 | + //offline |
|
218 | + else { |
|
219 | + // set nw online |
|
220 | + $addresses[$index]['lastSeenNew'] = NULL; |
|
221 | 221 | $address_change[$index] = $addresses[$index]; //change to online |
222 | - } |
|
223 | - //save exit code for host |
|
224 | - $addresses[$index]['newStatus'] = $thread->getExitCode(); |
|
225 | - //remove thread |
|
226 | - unset( $threads[$index] ); |
|
227 | - } |
|
228 | - } |
|
229 | - usleep(200000); |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - //update statuses for online |
|
234 | - |
|
235 | - # re-initialize classes |
|
236 | - $Database = new Database_PDO; |
|
237 | - $Scan = new Scan ($Database, $Subnets->settings); |
|
238 | - // reset debugging |
|
239 | - $Scan->reset_debugging(false); |
|
240 | - |
|
241 | - # update all active statuses |
|
242 | - foreach($addresses as $k=>$a) { |
|
243 | - if($a['newStatus']==0) { |
|
244 | - $Scan->ping_update_lastseen ($a['id'], $nowdate); |
|
245 | - } |
|
246 | - } |
|
222 | + } |
|
223 | + //save exit code for host |
|
224 | + $addresses[$index]['newStatus'] = $thread->getExitCode(); |
|
225 | + //remove thread |
|
226 | + unset( $threads[$index] ); |
|
227 | + } |
|
228 | + } |
|
229 | + usleep(200000); |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + //update statuses for online |
|
234 | + |
|
235 | + # re-initialize classes |
|
236 | + $Database = new Database_PDO; |
|
237 | + $Scan = new Scan ($Database, $Subnets->settings); |
|
238 | + // reset debugging |
|
239 | + $Scan->reset_debugging(false); |
|
240 | + |
|
241 | + # update all active statuses |
|
242 | + foreach($addresses as $k=>$a) { |
|
243 | + if($a['newStatus']==0) { |
|
244 | + $Scan->ping_update_lastseen ($a['id'], $nowdate); |
|
245 | + } |
|
246 | + } |
|
247 | 247 | } |
248 | 248 | |
249 | 249 | |
@@ -293,125 +293,125 @@ discard block |
||
293 | 293 | # all done, mail diff? |
294 | 294 | if(sizeof($address_change)>0 && $send_mail) { |
295 | 295 | |
296 | - # remove old classes |
|
297 | - unset($Database, $Subnets, $Addresses, $Tools, $Scan, $Result); |
|
298 | - |
|
299 | - $Database = new Database_PDO; |
|
300 | - $Subnets = new Subnets ($Database); |
|
301 | - $Addresses = new Addresses ($Database); |
|
302 | - $Tools = new Tools ($Database); |
|
303 | - $Scan = new Scan ($Database); |
|
304 | - $Result = new Result(); |
|
305 | - |
|
306 | - // set exit flag to true |
|
307 | - $Scan->ping_set_exit(true); |
|
308 | - // set debugging |
|
309 | - $Scan->reset_debugging(false); |
|
310 | - |
|
311 | - |
|
312 | - # check for recipients |
|
313 | - foreach($Tools->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
|
314 | - if($admin->mailNotify=="Yes") { |
|
315 | - $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
316 | - } |
|
317 | - } |
|
318 | - # none? |
|
319 | - if(!isset($recepients)) { die(); } |
|
320 | - |
|
321 | - # fetch mailer settings |
|
322 | - $mail_settings = $Tools->fetch_object("settingsMail", "id", 1); |
|
323 | - # fake user object, needed for create_link |
|
324 | - $User = new StdClass(); |
|
325 | - @$User->settings->prettyLinks = $Scan->settings->prettyLinks; |
|
326 | - |
|
327 | - # initialize mailer |
|
328 | - $phpipam_mail = new phpipam_mail($Scan->settings, $mail_settings); |
|
329 | - $phpipam_mail->initialize_mailer(); |
|
330 | - |
|
331 | - // set subject |
|
332 | - $subject = "phpIPAM IP state change ".$nowdate; |
|
333 | - |
|
334 | - //html |
|
335 | - $content[] = "<h3>phpIPAM host changes</h3>"; |
|
336 | - $content[] = "<table style='margin-left:10px;margin-top:5px;width:auto;padding:0px;border-collapse:collapse;border:1px solid gray;'>"; |
|
337 | - $content[] = "<tr>"; |
|
338 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>IP</th>"; |
|
339 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Description</th>"; |
|
340 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Hostname</th>"; |
|
341 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Subnet</th>"; |
|
342 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Section</th>"; |
|
343 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>last seen</th>"; |
|
344 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>old status</th>"; |
|
345 | - $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>new status</th>"; |
|
346 | - $content[] = "</tr>"; |
|
347 | - |
|
348 | - //plain |
|
349 | - $content_plain[] = "phpIPAM host changes \r\n------------------------------"; |
|
350 | - |
|
351 | - //Changes |
|
352 | - foreach($address_change as $change) { |
|
353 | - //reformat statuses |
|
354 | - if($change['oldStatus'] == 0) { |
|
355 | - $oldStatus = "<font style='color:#04B486'>Online</font>"; |
|
356 | - $newStatus = "<font style='color:#DF0101'>Offline</font>"; |
|
357 | - } |
|
358 | - else { |
|
359 | - $oldStatus = "<font style='color:#DF0101'>Offline</font>"; |
|
360 | - $newStatus = "<font style='color:#04B486'>Online</font>"; |
|
361 | - } |
|
362 | - |
|
363 | - //set subnet |
|
364 | - $subnet = $Subnets->fetch_subnet(null, $change['subnetId']); |
|
365 | - //set section |
|
366 | - $section = $Tools->fetch_object("sections", "id", $subnet->sectionId); |
|
367 | - //ago |
|
368 | - if(is_null($change['lastSeen']) || $change['lastSeen']=="0000-00-00 00:00:00") { |
|
369 | - $ago = "never"; |
|
370 | - } else { |
|
371 | - $timeDiff = $now - strtotime($change['lastSeen']); |
|
372 | - $ago = $change['lastSeen']." (".$Result->sec2hms($timeDiff)." ago)"; |
|
373 | - } |
|
374 | - |
|
375 | - //content |
|
376 | - $content[] = "<tr>"; |
|
377 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($change['ip_addr'])."</a></td>"; |
|
378 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[description]</td>"; |
|
379 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[dns_name]</td>"; |
|
380 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask." - ".$subnet->description."</a></td>"; |
|
381 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id)."'>$section->name $section->description</a></td>"; |
|
382 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$ago</td>"; |
|
383 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$oldStatus</td>"; |
|
384 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$newStatus</td>"; |
|
385 | - $content[] = "</tr>"; |
|
386 | - |
|
387 | - //plain content |
|
388 | - $content_plain[] = "\t * ".$Subnets->transform_to_dotted($change['ip_addr'])." (".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask.")\r\n \t ".strip_tags($oldStatus)." => ".strip_tags($newStatus); |
|
389 | - |
|
390 | - } |
|
391 | - $content[] = "</table>"; |
|
392 | - |
|
393 | - |
|
394 | - # set content |
|
395 | - $content = $phpipam_mail->generate_message (implode("\r\n", $content)); |
|
396 | - $content_plain = implode("\r\n",$content_plain); |
|
397 | - |
|
398 | - # try to send |
|
399 | - try { |
|
400 | - $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
401 | - //add all admins to CC |
|
402 | - foreach($recepients as $admin) { |
|
403 | - $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
404 | - } |
|
405 | - $phpipam_mail->Php_mailer->Subject = $subject; |
|
406 | - $phpipam_mail->Php_mailer->msgHTML($content); |
|
407 | - $phpipam_mail->Php_mailer->AltBody = $content_plain; |
|
408 | - //send |
|
409 | - $phpipam_mail->Php_mailer->send(); |
|
410 | - } catch (phpmailerException $e) { |
|
411 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
412 | - } catch (Exception $e) { |
|
413 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
414 | - } |
|
296 | + # remove old classes |
|
297 | + unset($Database, $Subnets, $Addresses, $Tools, $Scan, $Result); |
|
298 | + |
|
299 | + $Database = new Database_PDO; |
|
300 | + $Subnets = new Subnets ($Database); |
|
301 | + $Addresses = new Addresses ($Database); |
|
302 | + $Tools = new Tools ($Database); |
|
303 | + $Scan = new Scan ($Database); |
|
304 | + $Result = new Result(); |
|
305 | + |
|
306 | + // set exit flag to true |
|
307 | + $Scan->ping_set_exit(true); |
|
308 | + // set debugging |
|
309 | + $Scan->reset_debugging(false); |
|
310 | + |
|
311 | + |
|
312 | + # check for recipients |
|
313 | + foreach($Tools->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
|
314 | + if($admin->mailNotify=="Yes") { |
|
315 | + $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
316 | + } |
|
317 | + } |
|
318 | + # none? |
|
319 | + if(!isset($recepients)) { die(); } |
|
320 | + |
|
321 | + # fetch mailer settings |
|
322 | + $mail_settings = $Tools->fetch_object("settingsMail", "id", 1); |
|
323 | + # fake user object, needed for create_link |
|
324 | + $User = new StdClass(); |
|
325 | + @$User->settings->prettyLinks = $Scan->settings->prettyLinks; |
|
326 | + |
|
327 | + # initialize mailer |
|
328 | + $phpipam_mail = new phpipam_mail($Scan->settings, $mail_settings); |
|
329 | + $phpipam_mail->initialize_mailer(); |
|
330 | + |
|
331 | + // set subject |
|
332 | + $subject = "phpIPAM IP state change ".$nowdate; |
|
333 | + |
|
334 | + //html |
|
335 | + $content[] = "<h3>phpIPAM host changes</h3>"; |
|
336 | + $content[] = "<table style='margin-left:10px;margin-top:5px;width:auto;padding:0px;border-collapse:collapse;border:1px solid gray;'>"; |
|
337 | + $content[] = "<tr>"; |
|
338 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>IP</th>"; |
|
339 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Description</th>"; |
|
340 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Hostname</th>"; |
|
341 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Subnet</th>"; |
|
342 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>Section</th>"; |
|
343 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>last seen</th>"; |
|
344 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>old status</th>"; |
|
345 | + $content[] = " <th style='padding:3px 8px;border:1px solid silver;border-bottom:2px solid gray;'>new status</th>"; |
|
346 | + $content[] = "</tr>"; |
|
347 | + |
|
348 | + //plain |
|
349 | + $content_plain[] = "phpIPAM host changes \r\n------------------------------"; |
|
350 | + |
|
351 | + //Changes |
|
352 | + foreach($address_change as $change) { |
|
353 | + //reformat statuses |
|
354 | + if($change['oldStatus'] == 0) { |
|
355 | + $oldStatus = "<font style='color:#04B486'>Online</font>"; |
|
356 | + $newStatus = "<font style='color:#DF0101'>Offline</font>"; |
|
357 | + } |
|
358 | + else { |
|
359 | + $oldStatus = "<font style='color:#DF0101'>Offline</font>"; |
|
360 | + $newStatus = "<font style='color:#04B486'>Online</font>"; |
|
361 | + } |
|
362 | + |
|
363 | + //set subnet |
|
364 | + $subnet = $Subnets->fetch_subnet(null, $change['subnetId']); |
|
365 | + //set section |
|
366 | + $section = $Tools->fetch_object("sections", "id", $subnet->sectionId); |
|
367 | + //ago |
|
368 | + if(is_null($change['lastSeen']) || $change['lastSeen']=="0000-00-00 00:00:00") { |
|
369 | + $ago = "never"; |
|
370 | + } else { |
|
371 | + $timeDiff = $now - strtotime($change['lastSeen']); |
|
372 | + $ago = $change['lastSeen']." (".$Result->sec2hms($timeDiff)." ago)"; |
|
373 | + } |
|
374 | + |
|
375 | + //content |
|
376 | + $content[] = "<tr>"; |
|
377 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($change['ip_addr'])."</a></td>"; |
|
378 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[description]</td>"; |
|
379 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[dns_name]</td>"; |
|
380 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask." - ".$subnet->description."</a></td>"; |
|
381 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id)."'>$section->name $section->description</a></td>"; |
|
382 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$ago</td>"; |
|
383 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$oldStatus</td>"; |
|
384 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$newStatus</td>"; |
|
385 | + $content[] = "</tr>"; |
|
386 | + |
|
387 | + //plain content |
|
388 | + $content_plain[] = "\t * ".$Subnets->transform_to_dotted($change['ip_addr'])." (".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask.")\r\n \t ".strip_tags($oldStatus)." => ".strip_tags($newStatus); |
|
389 | + |
|
390 | + } |
|
391 | + $content[] = "</table>"; |
|
392 | + |
|
393 | + |
|
394 | + # set content |
|
395 | + $content = $phpipam_mail->generate_message (implode("\r\n", $content)); |
|
396 | + $content_plain = implode("\r\n",$content_plain); |
|
397 | + |
|
398 | + # try to send |
|
399 | + try { |
|
400 | + $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
401 | + //add all admins to CC |
|
402 | + foreach($recepients as $admin) { |
|
403 | + $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
404 | + } |
|
405 | + $phpipam_mail->Php_mailer->Subject = $subject; |
|
406 | + $phpipam_mail->Php_mailer->msgHTML($content); |
|
407 | + $phpipam_mail->Php_mailer->AltBody = $content_plain; |
|
408 | + //send |
|
409 | + $phpipam_mail->Php_mailer->send(); |
|
410 | + } catch (phpmailerException $e) { |
|
411 | + $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
412 | + } catch (Exception $e) { |
|
413 | + $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
414 | + } |
|
415 | 415 | } |
416 | 416 | |
417 | 417 | ?> |
418 | 418 | \ No newline at end of file |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | |
90 | 90 | $subnet_addresses = $Addresses->fetch_subnet_addresses ($s->id); |
91 | 91 | //set array for fping |
92 | - if($Scan->icmp_type=="fping") { |
|
92 | + if($Scan->icmp_type=="fping") { |
|
93 | 93 | $subnets[] = array("id"=>$s->id, "cidr"=>$Subnets->transform_to_dotted($s->subnet)."/".$s->mask); |
94 | 94 | } |
95 | 95 | //save addresses |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | //ignore excludePing |
99 | 99 | if($a->excludePing!=1) { |
100 | 100 | //create different array for fping |
101 | - if($Scan->icmp_type=="fping") { |
|
101 | + if($Scan->icmp_type=="fping") { |
|
102 | 102 | $addresses2[$s->id][$a->id] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); //used for status check |
103 | 103 | $addresses[$s->id][$a->id] = $a->ip_addr; //used for alive check |
104 | 104 | } |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | // now offline, and diff > offline period, do checks |
270 | 270 | elseif($change['lastSeenNew']==NULL && $deviceDiff >= (int) $statuses[1]) { |
271 | 271 | // if not already reported |
272 | - if ($deviceDiff <= ((int) $statuses[1] + $agentDiff)) { |
|
272 | + if ($deviceDiff <= ((int) $statuses[1] + $agentDiff)) { |
|
273 | 273 | $address_change[$k]['oldStatus'] = 0; |
274 | 274 | $address_change[$k]['newStatus'] = 2; |
275 | 275 | } |
@@ -367,7 +367,8 @@ discard block |
||
367 | 367 | //ago |
368 | 368 | if(is_null($change['lastSeen']) || $change['lastSeen']=="0000-00-00 00:00:00") { |
369 | 369 | $ago = "never"; |
370 | - } else { |
|
370 | + } |
|
371 | + else { |
|
371 | 372 | $timeDiff = $now - strtotime($change['lastSeen']); |
372 | 373 | $ago = $change['lastSeen']." (".$Result->sec2hms($timeDiff)." ago)"; |
373 | 374 | } |
@@ -31,79 +31,79 @@ discard block |
||
31 | 31 | |
32 | 32 | |
33 | 33 | # include required scripts |
34 | -require( dirname(__FILE__) . '/../functions.php' ); |
|
35 | -require( dirname(__FILE__) . '/../../functions/classes/class.Thread.php'); |
|
34 | +require(dirname (__FILE__).'/../functions.php'); |
|
35 | +require(dirname (__FILE__).'/../../functions/classes/class.Thread.php'); |
|
36 | 36 | |
37 | 37 | # initialize objects |
38 | 38 | $Database = new Database_PDO; |
39 | -$Subnets = new Subnets ($Database); |
|
39 | +$Subnets = new Subnets ($Database); |
|
40 | 40 | $Addresses = new Addresses ($Database); |
41 | -$Tools = new Tools ($Database); |
|
42 | -$Scan = new Scan ($Database); |
|
43 | -$Result = new Result(); |
|
41 | +$Tools = new Tools ($Database); |
|
42 | +$Scan = new Scan ($Database); |
|
43 | +$Result = new Result (); |
|
44 | 44 | |
45 | 45 | // set exit flag to true |
46 | -$Scan->ping_set_exit(true); |
|
46 | +$Scan->ping_set_exit (true); |
|
47 | 47 | // set debugging |
48 | -$Scan->reset_debugging(false); |
|
48 | +$Scan->reset_debugging (false); |
|
49 | 49 | // fetch agent |
50 | -$agent = $Tools->fetch_object("scanAgents", "id", 1); |
|
50 | +$agent = $Tools->fetch_object ("scanAgents", "id", 1); |
|
51 | 51 | // change scan type? |
52 | 52 | // $Scan->reset_scan_method ("fping"); |
53 | 53 | // set ping statuses |
54 | -$statuses = explode(";", $Scan->settings->pingStatus); |
|
54 | +$statuses = explode (";", $Scan->settings->pingStatus); |
|
55 | 55 | // set mail override flag |
56 | 56 | $send_mail = true; |
57 | 57 | |
58 | 58 | // response for mailing |
59 | -$address_change = array(); // Array with differences, can be used to email to admins |
|
59 | +$address_change = array (); // Array with differences, can be used to email to admins |
|
60 | 60 | |
61 | 61 | // set now for whole script |
62 | -$now = time(); |
|
62 | +$now = time (); |
|
63 | 63 | $nowdate = date ("Y-m-d H:i:s"); |
64 | 64 | |
65 | 65 | |
66 | 66 | // script can only be run from cli |
67 | -if(php_sapi_name()!="cli") { die("This script can only be run from cli!"); } |
|
67 | +if (php_sapi_name () != "cli") { die("This script can only be run from cli!"); } |
|
68 | 68 | // test to see if threading is available |
69 | -if(!Thread::available()) { die("Threading is required for scanning subnets. Please recompile PHP with pcntl extension"); } |
|
69 | +if (!Thread::available ()) { die("Threading is required for scanning subnets. Please recompile PHP with pcntl extension"); } |
|
70 | 70 | // verify ping path |
71 | -if ($Scan->icmp_type=="ping") { |
|
72 | -if(!file_exists($Scan->settings->scanPingPath)) { die("Invalid ping path!"); } |
|
71 | +if ($Scan->icmp_type == "ping") { |
|
72 | +if (!file_exists ($Scan->settings->scanPingPath)) { die("Invalid ping path!"); } |
|
73 | 73 | } |
74 | 74 | // verify fping path |
75 | -if ($Scan->icmp_type=="fping") { |
|
76 | -if(!file_exists($Scan->settings->scanFPingPath)){ die("Invalid fping path!"); } |
|
75 | +if ($Scan->icmp_type == "fping") { |
|
76 | +if (!file_exists ($Scan->settings->scanFPingPath)) { die("Invalid fping path!"); } |
|
77 | 77 | } |
78 | 78 | |
79 | 79 | |
80 | 80 | //first fetch all subnets to be scanned |
81 | 81 | $scan_subnets = $Subnets->fetch_all_subnets_for_pingCheck (1); |
82 | -if($Scan->debugging) { print_r($scan_subnets); } |
|
83 | -if($scan_subnets===false) { die("No subnets are marked for checking status updates"); } |
|
82 | +if ($Scan->debugging) { print_r ($scan_subnets); } |
|
83 | +if ($scan_subnets === false) { die("No subnets are marked for checking status updates"); } |
|
84 | 84 | //fetch all addresses that need to be checked |
85 | -foreach($scan_subnets as $s) { |
|
85 | +foreach ($scan_subnets as $s) { |
|
86 | 86 | |
87 | 87 | // if subnet has slaves dont check it |
88 | 88 | if ($Subnets->has_slaves ($s->id) === false) { |
89 | 89 | |
90 | 90 | $subnet_addresses = $Addresses->fetch_subnet_addresses ($s->id); |
91 | 91 | //set array for fping |
92 | - if($Scan->icmp_type=="fping") { |
|
93 | - $subnets[] = array("id"=>$s->id, "cidr"=>$Subnets->transform_to_dotted($s->subnet)."/".$s->mask); |
|
92 | + if ($Scan->icmp_type == "fping") { |
|
93 | + $subnets[] = array ("id"=>$s->id, "cidr"=>$Subnets->transform_to_dotted ($s->subnet)."/".$s->mask); |
|
94 | 94 | } |
95 | 95 | //save addresses |
96 | - if(sizeof($subnet_addresses)>1) { |
|
97 | - foreach($subnet_addresses as $a) { |
|
96 | + if (sizeof ($subnet_addresses) > 1) { |
|
97 | + foreach ($subnet_addresses as $a) { |
|
98 | 98 | //ignore excludePing |
99 | - if($a->excludePing!=1) { |
|
99 | + if ($a->excludePing != 1) { |
|
100 | 100 | //create different array for fping |
101 | - if($Scan->icmp_type=="fping") { |
|
102 | - $addresses2[$s->id][$a->id] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); //used for status check |
|
103 | - $addresses[$s->id][$a->id] = $a->ip_addr; //used for alive check |
|
101 | + if ($Scan->icmp_type == "fping") { |
|
102 | + $addresses2[$s->id][$a->id] = array ("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); //used for status check |
|
103 | + $addresses[$s->id][$a->id] = $a->ip_addr; //used for alive check |
|
104 | 104 | } |
105 | 105 | else { |
106 | - $addresses[] = array("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); |
|
106 | + $addresses[] = array ("id"=>$a->id, "ip_addr"=>$a->ip_addr, "description"=>$a->description, "dns_name"=>$a->dns_name, "subnetId"=>$a->subnetId, "lastSeenOld"=>$a->lastSeen, "lastSeen"=>$a->lastSeen); |
|
107 | 107 | } |
108 | 108 | } |
109 | 109 | } |
@@ -113,63 +113,63 @@ discard block |
||
113 | 113 | } |
114 | 114 | |
115 | 115 | |
116 | -if($Scan->debugging) { print "Using $Scan->icmp_type\n--------------------\n\n";print_r($addresses); } |
|
116 | +if ($Scan->debugging) { print "Using $Scan->icmp_type\n--------------------\n\n"; print_r ($addresses); } |
|
117 | 117 | //if none die |
118 | -if(!isset($addresses)) { die("No addresses to check"); } |
|
118 | +if (!isset($addresses)) { die("No addresses to check"); } |
|
119 | 119 | |
120 | 120 | |
121 | 121 | /* scan */ |
122 | 122 | |
123 | -$z = 0; //addresses array index |
|
123 | +$z = 0; //addresses array index |
|
124 | 124 | |
125 | 125 | //different scan for fping |
126 | -if($Scan->icmp_type=="fping") { |
|
126 | +if ($Scan->icmp_type == "fping") { |
|
127 | 127 | //run per MAX_THREADS |
128 | - for ($m=0; $m<=sizeof($subnets); $m += $Scan->settings->scanMaxThreads) { |
|
128 | + for ($m = 0; $m <= sizeof ($subnets); $m += $Scan->settings->scanMaxThreads) { |
|
129 | 129 | // create threads |
130 | - $threads = array(); |
|
130 | + $threads = array (); |
|
131 | 131 | //fork processes |
132 | - for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($subnets); $i++) { |
|
132 | + for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof ($subnets); $i++) { |
|
133 | 133 | //only if index exists! |
134 | - if(isset($subnets[$z])) { |
|
134 | + if (isset($subnets[$z])) { |
|
135 | 135 | //start new thread |
136 | - $threads[$z] = new Thread( 'fping_subnet' ); |
|
137 | - $threads[$z]->start_fping( $subnets[$z]['cidr'] ); |
|
138 | - $z++; //next index |
|
136 | + $threads[$z] = new Thread ('fping_subnet'); |
|
137 | + $threads[$z]->start_fping ($subnets[$z]['cidr']); |
|
138 | + $z++; //next index |
|
139 | 139 | } |
140 | 140 | } |
141 | 141 | // wait for all the threads to finish |
142 | - while( !empty( $threads ) ) { |
|
143 | - foreach($threads as $index => $thread) { |
|
144 | - $child_pipe = "/tmp/pipe_".$thread->getPid(); |
|
142 | + while (!empty($threads)) { |
|
143 | + foreach ($threads as $index => $thread) { |
|
144 | + $child_pipe = "/tmp/pipe_".$thread->getPid (); |
|
145 | 145 | |
146 | - if (file_exists($child_pipe)) { |
|
147 | - $file_descriptor = fopen( $child_pipe, "r"); |
|
146 | + if (file_exists ($child_pipe)) { |
|
147 | + $file_descriptor = fopen ($child_pipe, "r"); |
|
148 | 148 | $child_response = ""; |
149 | - while (!feof($file_descriptor)) { |
|
150 | - $child_response .= fread($file_descriptor, 8192); |
|
149 | + while (!feof ($file_descriptor)) { |
|
150 | + $child_response .= fread ($file_descriptor, 8192); |
|
151 | 151 | } |
152 | 152 | //we have the child data in the parent, but serialized: |
153 | - $child_response = unserialize( $child_response ); |
|
153 | + $child_response = unserialize ($child_response); |
|
154 | 154 | //store |
155 | 155 | $subnets[$index]['result'] = $child_response; |
156 | 156 | |
157 | 157 | //now, child is dead, and parent close the pipe |
158 | - unlink( $child_pipe ); |
|
158 | + unlink ($child_pipe); |
|
159 | 159 | unset($threads[$index]); |
160 | 160 | } |
161 | 161 | } |
162 | - usleep(200000); |
|
162 | + usleep (200000); |
|
163 | 163 | } |
164 | 164 | } |
165 | 165 | |
166 | 166 | //now we must remove all non-existing hosts |
167 | - foreach($subnets as $sk=>$s) { |
|
168 | - if(sizeof(@$s['result'])>0 && isset($addresses[$s['id']])) { |
|
167 | + foreach ($subnets as $sk=>$s) { |
|
168 | + if (sizeof (@$s['result']) > 0 && isset($addresses[$s['id']])) { |
|
169 | 169 | //loop addresses |
170 | - foreach($addresses[$s['id']] as $ak=>$a) { |
|
170 | + foreach ($addresses[$s['id']] as $ak=>$a) { |
|
171 | 171 | //offline host |
172 | - if(array_search($Subnets->transform_to_dotted($a), $subnets[$sk]['result'])===false) { |
|
172 | + if (array_search ($Subnets->transform_to_dotted ($a), $subnets[$sk]['result']) === false) { |
|
173 | 173 | // new change = null |
174 | 174 | $addresses2[$s['id']][$ak]['lastSeenNew'] = NULL; |
175 | 175 | //save to out array |
@@ -191,42 +191,42 @@ discard block |
||
191 | 191 | //ping, pear |
192 | 192 | else { |
193 | 193 | //run per MAX_THREADS |
194 | - for ($m=0; $m<=sizeof($addresses); $m += $Scan->settings->scanMaxThreads) { |
|
194 | + for ($m = 0; $m <= sizeof ($addresses); $m += $Scan->settings->scanMaxThreads) { |
|
195 | 195 | // create threads |
196 | - $threads = array(); |
|
196 | + $threads = array (); |
|
197 | 197 | //fork processes |
198 | - for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof($addresses); $i++) { |
|
198 | + for ($i = 0; $i <= $Scan->settings->scanMaxThreads && $i <= sizeof ($addresses); $i++) { |
|
199 | 199 | //only if index exists! |
200 | - if(isset($addresses[$z])) { |
|
200 | + if (isset($addresses[$z])) { |
|
201 | 201 | //start new thread |
202 | - $threads[$z] = new Thread( 'ping_address' ); |
|
203 | - $threads[$z]->start($Subnets->transform_to_dotted($addresses[$z]['ip_addr'])); |
|
204 | - $z++; //next index |
|
202 | + $threads[$z] = new Thread ('ping_address'); |
|
203 | + $threads[$z]->start ($Subnets->transform_to_dotted ($addresses[$z]['ip_addr'])); |
|
204 | + $z++; //next index |
|
205 | 205 | } |
206 | 206 | } |
207 | 207 | // wait for all the threads to finish |
208 | - while( !empty( $threads ) ) { |
|
209 | - foreach( $threads as $index => $thread ) { |
|
210 | - if( ! $thread->isAlive() ) { |
|
208 | + while (!empty($threads)) { |
|
209 | + foreach ($threads as $index => $thread) { |
|
210 | + if (!$thread->isAlive ()) { |
|
211 | 211 | //online |
212 | - if($thread->getExitCode() == 0) { |
|
212 | + if ($thread->getExitCode () == 0) { |
|
213 | 213 | // set new available time |
214 | - $addresses[$index]['lastSeenNew'] = $nowdate; |
|
215 | - $address_change[$index] = $addresses[$index]; //change to online |
|
214 | + $addresses[$index]['lastSeenNew'] = $nowdate; |
|
215 | + $address_change[$index] = $addresses[$index]; //change to online |
|
216 | 216 | } |
217 | 217 | //offline |
218 | 218 | else { |
219 | 219 | // set nw online |
220 | - $addresses[$index]['lastSeenNew'] = NULL; |
|
221 | - $address_change[$index] = $addresses[$index]; //change to online |
|
220 | + $addresses[$index]['lastSeenNew'] = NULL; |
|
221 | + $address_change[$index] = $addresses[$index]; //change to online |
|
222 | 222 | } |
223 | 223 | //save exit code for host |
224 | - $addresses[$index]['newStatus'] = $thread->getExitCode(); |
|
224 | + $addresses[$index]['newStatus'] = $thread->getExitCode (); |
|
225 | 225 | //remove thread |
226 | - unset( $threads[$index] ); |
|
226 | + unset($threads[$index]); |
|
227 | 227 | } |
228 | 228 | } |
229 | - usleep(200000); |
|
229 | + usleep (200000); |
|
230 | 230 | } |
231 | 231 | } |
232 | 232 | |
@@ -236,11 +236,11 @@ discard block |
||
236 | 236 | $Database = new Database_PDO; |
237 | 237 | $Scan = new Scan ($Database, $Subnets->settings); |
238 | 238 | // reset debugging |
239 | - $Scan->reset_debugging(false); |
|
239 | + $Scan->reset_debugging (false); |
|
240 | 240 | |
241 | 241 | # update all active statuses |
242 | - foreach($addresses as $k=>$a) { |
|
243 | - if($a['newStatus']==0) { |
|
242 | + foreach ($addresses as $k=>$a) { |
|
243 | + if ($a['newStatus'] == 0) { |
|
244 | 244 | $Scan->ping_update_lastseen ($a['id'], $nowdate); |
245 | 245 | } |
246 | 246 | } |
@@ -255,21 +255,21 @@ discard block |
||
255 | 255 | // loop |
256 | 256 | foreach ($address_change as $k=>$change) { |
257 | 257 | // null old - set to epoch time |
258 | - if (strtotime($change['lastSeenOld'])===false) { $change['lastSeenOld'] = date("Y-m-d H:i:s", 0); } |
|
258 | + if (strtotime ($change['lastSeenOld']) === false) { $change['lastSeenOld'] = date ("Y-m-d H:i:s", 0); } |
|
259 | 259 | |
260 | 260 | // set general diffs |
261 | - $deviceDiff = $now - strtotime($change['lastSeenOld']); // now - device last seen |
|
262 | - $agentDiff = $now - strtotime($agent->last_access); // now - last agent check |
|
261 | + $deviceDiff = $now - strtotime ($change['lastSeenOld']); // now - device last seen |
|
262 | + $agentDiff = $now - strtotime ($agent->last_access); // now - last agent check |
|
263 | 263 | |
264 | 264 | // if now online and old offline send mail |
265 | - if ($change['lastSeenNew']!=NULL && $deviceDiff >= (int) $statuses[1]) { |
|
265 | + if ($change['lastSeenNew'] != NULL && $deviceDiff >= (int) $statuses[1]) { |
|
266 | 266 | $address_change[$k]['oldStatus'] = 2; |
267 | 267 | $address_change[$k]['newStatus'] = 0; |
268 | 268 | } |
269 | 269 | // now offline, and diff > offline period, do checks |
270 | - elseif($change['lastSeenNew']==NULL && $deviceDiff >= (int) $statuses[1]) { |
|
270 | + elseif ($change['lastSeenNew'] == NULL && $deviceDiff >= (int) $statuses[1]) { |
|
271 | 271 | // if not already reported |
272 | - if ($deviceDiff <= ((int) $statuses[1] + $agentDiff)) { |
|
272 | + if ($deviceDiff <= ((int) $statuses[1] + $agentDiff)) { |
|
273 | 273 | $address_change[$k]['oldStatus'] = 0; |
274 | 274 | $address_change[$k]['newStatus'] = 2; |
275 | 275 | } |
@@ -288,48 +288,48 @@ discard block |
||
288 | 288 | |
289 | 289 | |
290 | 290 | # print change |
291 | -if($Scan->debugging) { print "\nAddress changes:\n----------\n"; print_r($address_change); } |
|
291 | +if ($Scan->debugging) { print "\nAddress changes:\n----------\n"; print_r ($address_change); } |
|
292 | 292 | |
293 | 293 | # all done, mail diff? |
294 | -if(sizeof($address_change)>0 && $send_mail) { |
|
294 | +if (sizeof ($address_change) > 0 && $send_mail) { |
|
295 | 295 | |
296 | 296 | # remove old classes |
297 | 297 | unset($Database, $Subnets, $Addresses, $Tools, $Scan, $Result); |
298 | 298 | |
299 | 299 | $Database = new Database_PDO; |
300 | - $Subnets = new Subnets ($Database); |
|
300 | + $Subnets = new Subnets ($Database); |
|
301 | 301 | $Addresses = new Addresses ($Database); |
302 | - $Tools = new Tools ($Database); |
|
303 | - $Scan = new Scan ($Database); |
|
304 | - $Result = new Result(); |
|
302 | + $Tools = new Tools ($Database); |
|
303 | + $Scan = new Scan ($Database); |
|
304 | + $Result = new Result (); |
|
305 | 305 | |
306 | 306 | // set exit flag to true |
307 | - $Scan->ping_set_exit(true); |
|
307 | + $Scan->ping_set_exit (true); |
|
308 | 308 | // set debugging |
309 | - $Scan->reset_debugging(false); |
|
309 | + $Scan->reset_debugging (false); |
|
310 | 310 | |
311 | 311 | |
312 | 312 | # check for recipients |
313 | - foreach($Tools->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
|
314 | - if($admin->mailNotify=="Yes") { |
|
315 | - $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
313 | + foreach ($Tools->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
|
314 | + if ($admin->mailNotify == "Yes") { |
|
315 | + $recepients[] = array ("name"=>$admin->real_name, "email"=>$admin->email); |
|
316 | 316 | } |
317 | 317 | } |
318 | 318 | # none? |
319 | - if(!isset($recepients)) { die(); } |
|
319 | + if (!isset($recepients)) { die(); } |
|
320 | 320 | |
321 | 321 | # fetch mailer settings |
322 | - $mail_settings = $Tools->fetch_object("settingsMail", "id", 1); |
|
322 | + $mail_settings = $Tools->fetch_object ("settingsMail", "id", 1); |
|
323 | 323 | # fake user object, needed for create_link |
324 | - $User = new StdClass(); |
|
324 | + $User = new StdClass (); |
|
325 | 325 | @$User->settings->prettyLinks = $Scan->settings->prettyLinks; |
326 | 326 | |
327 | 327 | # initialize mailer |
328 | - $phpipam_mail = new phpipam_mail($Scan->settings, $mail_settings); |
|
329 | - $phpipam_mail->initialize_mailer(); |
|
328 | + $phpipam_mail = new phpipam_mail ($Scan->settings, $mail_settings); |
|
329 | + $phpipam_mail->initialize_mailer (); |
|
330 | 330 | |
331 | 331 | // set subject |
332 | - $subject = "phpIPAM IP state change ".$nowdate; |
|
332 | + $subject = "phpIPAM IP state change ".$nowdate; |
|
333 | 333 | |
334 | 334 | //html |
335 | 335 | $content[] = "<h3>phpIPAM host changes</h3>"; |
@@ -349,9 +349,9 @@ discard block |
||
349 | 349 | $content_plain[] = "phpIPAM host changes \r\n------------------------------"; |
350 | 350 | |
351 | 351 | //Changes |
352 | - foreach($address_change as $change) { |
|
352 | + foreach ($address_change as $change) { |
|
353 | 353 | //reformat statuses |
354 | - if($change['oldStatus'] == 0) { |
|
354 | + if ($change['oldStatus'] == 0) { |
|
355 | 355 | $oldStatus = "<font style='color:#04B486'>Online</font>"; |
356 | 356 | $newStatus = "<font style='color:#DF0101'>Offline</font>"; |
357 | 357 | } |
@@ -361,56 +361,56 @@ discard block |
||
361 | 361 | } |
362 | 362 | |
363 | 363 | //set subnet |
364 | - $subnet = $Subnets->fetch_subnet(null, $change['subnetId']); |
|
364 | + $subnet = $Subnets->fetch_subnet (null, $change['subnetId']); |
|
365 | 365 | //set section |
366 | - $section = $Tools->fetch_object("sections", "id", $subnet->sectionId); |
|
366 | + $section = $Tools->fetch_object ("sections", "id", $subnet->sectionId); |
|
367 | 367 | //ago |
368 | - if(is_null($change['lastSeen']) || $change['lastSeen']=="0000-00-00 00:00:00") { |
|
369 | - $ago = "never"; |
|
368 | + if (is_null ($change['lastSeen']) || $change['lastSeen'] == "0000-00-00 00:00:00") { |
|
369 | + $ago = "never"; |
|
370 | 370 | } else { |
371 | - $timeDiff = $now - strtotime($change['lastSeen']); |
|
372 | - $ago = $change['lastSeen']." (".$Result->sec2hms($timeDiff)." ago)"; |
|
371 | + $timeDiff = $now - strtotime ($change['lastSeen']); |
|
372 | + $ago = $change['lastSeen']." (".$Result->sec2hms ($timeDiff)." ago)"; |
|
373 | 373 | } |
374 | 374 | |
375 | 375 | //content |
376 | 376 | $content[] = "<tr>"; |
377 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($change['ip_addr'])."</a></td>"; |
|
377 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim ($Scan->settings->siteURL, "/")."".create_link ("subnets", $section->id, $subnet->id)."'>".$Subnets->transform_to_dotted ($change['ip_addr'])."</a></td>"; |
|
378 | 378 | $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[description]</td>"; |
379 | 379 | $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$change[dns_name]</td>"; |
380 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id,$subnet->id)."'>".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask." - ".$subnet->description."</a></td>"; |
|
381 | - $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim($Scan->settings->siteURL, "/")."".create_link("subnets",$section->id)."'>$section->name $section->description</a></td>"; |
|
380 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim ($Scan->settings->siteURL, "/")."".create_link ("subnets", $section->id, $subnet->id)."'>".$Subnets->transform_to_dotted ($subnet->subnet)."/".$subnet->mask." - ".$subnet->description."</a></td>"; |
|
381 | + $content[] = " <td style='padding:3px 8px;border:1px solid silver;'><a href='".rtrim ($Scan->settings->siteURL, "/")."".create_link ("subnets", $section->id)."'>$section->name $section->description</a></td>"; |
|
382 | 382 | $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$ago</td>"; |
383 | 383 | $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$oldStatus</td>"; |
384 | 384 | $content[] = " <td style='padding:3px 8px;border:1px solid silver;'>$newStatus</td>"; |
385 | 385 | $content[] = "</tr>"; |
386 | 386 | |
387 | 387 | //plain content |
388 | - $content_plain[] = "\t * ".$Subnets->transform_to_dotted($change['ip_addr'])." (".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask.")\r\n \t ".strip_tags($oldStatus)." => ".strip_tags($newStatus); |
|
388 | + $content_plain[] = "\t * ".$Subnets->transform_to_dotted ($change['ip_addr'])." (".$Subnets->transform_to_dotted ($subnet->subnet)."/".$subnet->mask.")\r\n \t ".strip_tags ($oldStatus)." => ".strip_tags ($newStatus); |
|
389 | 389 | |
390 | 390 | } |
391 | 391 | $content[] = "</table>"; |
392 | 392 | |
393 | 393 | |
394 | 394 | # set content |
395 | - $content = $phpipam_mail->generate_message (implode("\r\n", $content)); |
|
396 | - $content_plain = implode("\r\n",$content_plain); |
|
395 | + $content = $phpipam_mail->generate_message (implode ("\r\n", $content)); |
|
396 | + $content_plain = implode ("\r\n", $content_plain); |
|
397 | 397 | |
398 | 398 | # try to send |
399 | 399 | try { |
400 | - $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
400 | + $phpipam_mail->Php_mailer->setFrom ($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
401 | 401 | //add all admins to CC |
402 | - foreach($recepients as $admin) { |
|
403 | - $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
402 | + foreach ($recepients as $admin) { |
|
403 | + $phpipam_mail->Php_mailer->addAddress (addslashes ($admin['email']), addslashes ($admin['name'])); |
|
404 | 404 | } |
405 | 405 | $phpipam_mail->Php_mailer->Subject = $subject; |
406 | - $phpipam_mail->Php_mailer->msgHTML($content); |
|
406 | + $phpipam_mail->Php_mailer->msgHTML ($content); |
|
407 | 407 | $phpipam_mail->Php_mailer->AltBody = $content_plain; |
408 | 408 | //send |
409 | - $phpipam_mail->Php_mailer->send(); |
|
409 | + $phpipam_mail->Php_mailer->send (); |
|
410 | 410 | } catch (phpmailerException $e) { |
411 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
411 | + $Result->show_cli ("Mailer Error: ".$e->errorMessage (), true); |
|
412 | 412 | } catch (Exception $e) { |
413 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
413 | + $Result->show_cli ("Mailer Error: ".$e->errorMessage (), true); |
|
414 | 414 | } |
415 | 415 | } |
416 | 416 |
@@ -27,17 +27,17 @@ discard block |
||
27 | 27 | // check if argv[1] provided, if not check readline support and wait for user pass |
28 | 28 | if (isset($argv[1])) { $password = $argv[1]; } |
29 | 29 | else { |
30 | - // get available extensions |
|
31 | - $available_extensions = get_loaded_extensions(); |
|
32 | - // not in array |
|
33 | - if (!in_array("readline", $available_extensions)) { $Result->show_cli("readline php extension is required.\nOr provide password as first argument", true); } |
|
34 | - else { |
|
35 | - // read password |
|
36 | - $line = readline("Enter password: "); |
|
37 | - readline_add_history($line); |
|
38 | - // save |
|
39 | - $password = array_pop(readline_list_history()); |
|
40 | - } |
|
30 | + // get available extensions |
|
31 | + $available_extensions = get_loaded_extensions(); |
|
32 | + // not in array |
|
33 | + if (!in_array("readline", $available_extensions)) { $Result->show_cli("readline php extension is required.\nOr provide password as first argument", true); } |
|
34 | + else { |
|
35 | + // read password |
|
36 | + $line = readline("Enter password: "); |
|
37 | + readline_add_history($line); |
|
38 | + // save |
|
39 | + $password = array_pop(readline_list_history()); |
|
40 | + } |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | // validate password |
@@ -50,8 +50,8 @@ discard block |
||
50 | 50 | |
51 | 51 | // set update array |
52 | 52 | $values = array("id"=>1, |
53 | - "password"=>$password_crypted |
|
54 | - ); |
|
53 | + "password"=>$password_crypted |
|
54 | + ); |
|
55 | 55 | |
56 | 56 | // update password |
57 | 57 | if(!$Admin->object_modify("users", "edit", "id", $values)) { $Result->show_cli("Failed to update Admin password", false); } |
@@ -60,10 +60,10 @@ discard block |
||
60 | 60 | |
61 | 61 | // debug ? |
62 | 62 | if ($debugging || $fail) { |
63 | - $Result->show_cli("---------"); |
|
64 | - $Result->show_cli("Crypt type: ".$crypt_type); |
|
65 | - $Result->show_cli("Password: ".$password_crypted); |
|
66 | - $Result->show_cli("---------"); |
|
63 | + $Result->show_cli("---------"); |
|
64 | + $Result->show_cli("Crypt type: ".$crypt_type); |
|
65 | + $Result->show_cli("Password: ".$password_crypted); |
|
66 | + $Result->show_cli("---------"); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | // fail |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | |
74 | 74 | # check for recipients |
75 | 75 | foreach($Admin->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
76 | - $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
76 | + $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
77 | 77 | } |
78 | 78 | # none? |
79 | 79 | if(!isset($recepients)) { die(); } |
@@ -104,20 +104,20 @@ discard block |
||
104 | 104 | |
105 | 105 | # try to send |
106 | 106 | try { |
107 | - $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
108 | - //add all admins to CC |
|
109 | - foreach($recepients as $admin) { |
|
110 | - $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
111 | - } |
|
112 | - $phpipam_mail->Php_mailer->Subject = $subject; |
|
113 | - $phpipam_mail->Php_mailer->msgHTML($content); |
|
114 | - $phpipam_mail->Php_mailer->AltBody = $content_plain; |
|
115 | - //send |
|
116 | - $phpipam_mail->Php_mailer->send(); |
|
107 | + $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
108 | + //add all admins to CC |
|
109 | + foreach($recepients as $admin) { |
|
110 | + $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
111 | + } |
|
112 | + $phpipam_mail->Php_mailer->Subject = $subject; |
|
113 | + $phpipam_mail->Php_mailer->msgHTML($content); |
|
114 | + $phpipam_mail->Php_mailer->AltBody = $content_plain; |
|
115 | + //send |
|
116 | + $phpipam_mail->Php_mailer->send(); |
|
117 | 117 | } catch (phpmailerException $e) { |
118 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
118 | + $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
119 | 119 | } catch (Exception $e) { |
120 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
120 | + $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | ?> |
124 | 124 | \ No newline at end of file |
@@ -8,40 +8,40 @@ discard block |
||
8 | 8 | */ |
9 | 9 | |
10 | 10 | # include required scripts |
11 | -require( dirname(__FILE__) . '/../functions.php' ); |
|
11 | +require(dirname (__FILE__).'/../functions.php'); |
|
12 | 12 | |
13 | 13 | # set debugging |
14 | 14 | $debugging = false; |
15 | -$fail = false; |
|
15 | +$fail = false; |
|
16 | 16 | |
17 | 17 | # initialize objects |
18 | -$Database = new Database_PDO; |
|
19 | -$Admin = new Admin ($Database, false); |
|
20 | -$User = new User ($Database, true); |
|
21 | -$Result = new Result(); |
|
18 | +$Database = new Database_PDO; |
|
19 | +$Admin = new Admin ($Database, false); |
|
20 | +$User = new User ($Database, true); |
|
21 | +$Result = new Result (); |
|
22 | 22 | |
23 | 23 | |
24 | 24 | // script can only be run from cli |
25 | -if(php_sapi_name()!="cli") { $Result->show_cli("This script can only be run from cli", true); } |
|
25 | +if (php_sapi_name () != "cli") { $Result->show_cli ("This script can only be run from cli", true); } |
|
26 | 26 | |
27 | 27 | // check if argv[1] provided, if not check readline support and wait for user pass |
28 | -if (isset($argv[1])) { $password = $argv[1]; } |
|
28 | +if (isset($argv[1])) { $password = $argv[1]; } |
|
29 | 29 | else { |
30 | 30 | // get available extensions |
31 | - $available_extensions = get_loaded_extensions(); |
|
31 | + $available_extensions = get_loaded_extensions (); |
|
32 | 32 | // not in array |
33 | - if (!in_array("readline", $available_extensions)) { $Result->show_cli("readline php extension is required.\nOr provide password as first argument", true); } |
|
33 | + if (!in_array ("readline", $available_extensions)) { $Result->show_cli ("readline php extension is required.\nOr provide password as first argument", true); } |
|
34 | 34 | else { |
35 | 35 | // read password |
36 | - $line = readline("Enter password: "); |
|
37 | - readline_add_history($line); |
|
36 | + $line = readline ("Enter password: "); |
|
37 | + readline_add_history ($line); |
|
38 | 38 | // save |
39 | - $password = array_pop(readline_list_history()); |
|
39 | + $password = array_pop (readline_list_history ()); |
|
40 | 40 | } |
41 | 41 | } |
42 | 42 | |
43 | 43 | // validate password |
44 | -if(strlen($password)<8) { $Result->show_cli("Password must be at least 8 characters long", true); } |
|
44 | +if (strlen ($password) < 8) { $Result->show_cli ("Password must be at least 8 characters long", true); } |
|
45 | 45 | |
46 | 46 | // hash passowrd |
47 | 47 | $password_crypted = $User->crypt_user_pass ($password); |
@@ -49,21 +49,21 @@ discard block |
||
49 | 49 | $crypt_type = $User->return_crypt_type (); |
50 | 50 | |
51 | 51 | // set update array |
52 | -$values = array("id"=>1, |
|
52 | +$values = array ("id"=>1, |
|
53 | 53 | "password"=>$password_crypted |
54 | 54 | ); |
55 | 55 | |
56 | 56 | // update password |
57 | -if(!$Admin->object_modify("users", "edit", "id", $values)) { $Result->show_cli("Failed to update Admin password", false); } |
|
58 | -else { $Result->show_cli("Admin password updated", false); } |
|
57 | +if (!$Admin->object_modify ("users", "edit", "id", $values)) { $Result->show_cli ("Failed to update Admin password", false); } |
|
58 | +else { $Result->show_cli ("Admin password updated", false); } |
|
59 | 59 | |
60 | 60 | |
61 | 61 | // debug ? |
62 | 62 | if ($debugging || $fail) { |
63 | - $Result->show_cli("---------"); |
|
64 | - $Result->show_cli("Crypt type: ".$crypt_type); |
|
65 | - $Result->show_cli("Password: ".$password_crypted); |
|
66 | - $Result->show_cli("---------"); |
|
63 | + $Result->show_cli ("---------"); |
|
64 | + $Result->show_cli ("Crypt type: ".$crypt_type); |
|
65 | + $Result->show_cli ("Password: ".$password_crypted); |
|
66 | + $Result->show_cli ("---------"); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | // fail |
@@ -72,23 +72,23 @@ discard block |
||
72 | 72 | # send mail |
73 | 73 | |
74 | 74 | # check for recipients |
75 | -foreach($Admin->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
|
76 | - $recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email); |
|
75 | +foreach ($Admin->fetch_multiple_objects ("users", "role", "Administrator") as $admin) { |
|
76 | + $recepients[] = array ("name"=>$admin->real_name, "email"=>$admin->email); |
|
77 | 77 | } |
78 | 78 | # none? |
79 | -if(!isset($recepients)) { die(); } |
|
79 | +if (!isset($recepients)) { die(); } |
|
80 | 80 | |
81 | 81 | // fetch settings |
82 | -$settings = $Admin->fetch_object("settings", "id", 1); |
|
82 | +$settings = $Admin->fetch_object ("settings", "id", 1); |
|
83 | 83 | // fetch mailer settings |
84 | -$mail_settings = $Admin->fetch_object("settingsMail", "id", 1); |
|
84 | +$mail_settings = $Admin->fetch_object ("settingsMail", "id", 1); |
|
85 | 85 | |
86 | 86 | # initialize mailer |
87 | -$phpipam_mail = new phpipam_mail($settings, $mail_settings); |
|
88 | -$phpipam_mail->initialize_mailer(); |
|
87 | +$phpipam_mail = new phpipam_mail ($settings, $mail_settings); |
|
88 | +$phpipam_mail->initialize_mailer (); |
|
89 | 89 | |
90 | 90 | // set subject |
91 | -$subject = "phpIPAM Administrator password updated"; |
|
91 | +$subject = "phpIPAM Administrator password updated"; |
|
92 | 92 | //html |
93 | 93 | $content[] = "<h3>phpIPAM Administrator password updated</h3>"; |
94 | 94 | $content[] = "<hr>"; |
@@ -99,25 +99,25 @@ discard block |
||
99 | 99 | |
100 | 100 | |
101 | 101 | # set content |
102 | -$content = $phpipam_mail->generate_message (implode("\r\n", $content)); |
|
103 | -$content_plain = implode("\r\n",$content_plain); |
|
102 | +$content = $phpipam_mail->generate_message (implode ("\r\n", $content)); |
|
103 | +$content_plain = implode ("\r\n", $content_plain); |
|
104 | 104 | |
105 | 105 | # try to send |
106 | 106 | try { |
107 | - $phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
107 | + $phpipam_mail->Php_mailer->setFrom ($mail_settings->mAdminMail, $mail_settings->mAdminName); |
|
108 | 108 | //add all admins to CC |
109 | - foreach($recepients as $admin) { |
|
110 | - $phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name'])); |
|
109 | + foreach ($recepients as $admin) { |
|
110 | + $phpipam_mail->Php_mailer->addAddress (addslashes ($admin['email']), addslashes ($admin['name'])); |
|
111 | 111 | } |
112 | 112 | $phpipam_mail->Php_mailer->Subject = $subject; |
113 | - $phpipam_mail->Php_mailer->msgHTML($content); |
|
113 | + $phpipam_mail->Php_mailer->msgHTML ($content); |
|
114 | 114 | $phpipam_mail->Php_mailer->AltBody = $content_plain; |
115 | 115 | //send |
116 | - $phpipam_mail->Php_mailer->send(); |
|
116 | + $phpipam_mail->Php_mailer->send (); |
|
117 | 117 | } catch (phpmailerException $e) { |
118 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
118 | + $Result->show_cli ("Mailer Error: ".$e->errorMessage (), true); |
|
119 | 119 | } catch (Exception $e) { |
120 | - $Result->show_cli("Mailer Error: ".$e->errorMessage(), true); |
|
120 | + $Result->show_cli ("Mailer Error: ".$e->errorMessage (), true); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | ?> |
124 | 124 | \ No newline at end of file |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | * @param bool $drop_database (default: false) |
68 | 68 | * @param bool $create_database (default: false) |
69 | 69 | * @param bool $create_grants (default: false) |
70 | - * @return void |
|
70 | + * @return boolean|null |
|
71 | 71 | */ |
72 | 72 | public function install_database ($rootuser, $rootpass, $drop_database = false, $create_database = false, $create_grants = false) { |
73 | 73 | |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | * Execute files installation |
146 | 146 | * |
147 | 147 | * @access private |
148 | - * @return void |
|
148 | + * @return false|null |
|
149 | 149 | */ |
150 | 150 | private function install_database_execute () { |
151 | 151 | # import SCHEMA file queries |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | * |
198 | 198 | * @access public |
199 | 199 | * @param bool $redirect |
200 | - * @return void |
|
200 | + * @return boolean |
|
201 | 201 | */ |
202 | 202 | public function check_db_connection ($redirect = false) { |
203 | 203 | # try to connect |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | * |
218 | 218 | * @access public |
219 | 219 | * @param mixed $table |
220 | - * @return void |
|
220 | + * @return boolean |
|
221 | 221 | */ |
222 | 222 | public function check_table ($table, $redirect = false) { |
223 | 223 | # set query |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | * @param mixed $adminpass |
285 | 285 | * @param mixed $siteTitle |
286 | 286 | * @param mixed $siteURL |
287 | - * @return void |
|
287 | + * @return boolean |
|
288 | 288 | */ |
289 | 289 | function postauth_update($adminpass, $siteTitle, $siteURL) { |
290 | 290 | # update Admin pass |
@@ -300,7 +300,7 @@ discard block |
||
300 | 300 | * |
301 | 301 | * @access public |
302 | 302 | * @param mixed $adminpass |
303 | - * @return void |
|
303 | + * @return boolean |
|
304 | 304 | */ |
305 | 305 | public function postauth_update_admin_pass ($adminpass) { |
306 | 306 | try { $this->Database->updateObject("users", array("password"=>$adminpass, "passChange"=>"No","username"=>"Admin"), "username"); } |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | * @access private |
315 | 315 | * @param mixed $siteTitle |
316 | 316 | * @param mixed $siteURL |
317 | - * @return void |
|
317 | + * @return boolean |
|
318 | 318 | */ |
319 | 319 | private function postauth_update_settings ($siteTitle, $siteURL) { |
320 | 320 | try { $this->Database->updateObject("settings", array("siteTitle"=>$siteTitle, "siteURL"=>$siteURL,"id"=>1), "id"); } |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | * Upgrade database checks and executes. |
341 | 341 | * |
342 | 342 | * @access public |
343 | - * @return void |
|
343 | + * @return null|boolean |
|
344 | 344 | */ |
345 | 345 | public function upgrade_database () { |
346 | 346 | # first check version |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | * Execute database upgrade. |
362 | 362 | * |
363 | 363 | * @access private |
364 | - * @return void |
|
364 | + * @return boolean |
|
365 | 365 | */ |
366 | 366 | private function upgrade_database_execute () { |
367 | 367 | # set queries |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | * Fetch all upgrade queries from DB files |
409 | 409 | * |
410 | 410 | * @access public |
411 | - * @return void |
|
411 | + * @return string |
|
412 | 412 | */ |
413 | 413 | public function get_upgrade_queries () { |
414 | 414 | // save all queries fro UPDATE.sql file |
@@ -41,8 +41,7 @@ discard block |
||
41 | 41 | # set debugging |
42 | 42 | $this->set_db_params (); |
43 | 43 | # Log object |
44 | - try { $this->Database->connect(); } |
|
45 | - catch ( Exception $e ) {} |
|
44 | + try { $this->Database->connect(); } catch ( Exception $e ) {} |
|
46 | 45 | } |
47 | 46 | |
48 | 47 | |
@@ -109,8 +108,7 @@ discard block |
||
109 | 108 | # set query |
110 | 109 | $query = "drop database if exists `". $this->db['name'] ."`;"; |
111 | 110 | # execute |
112 | - try { $this->Database_root->runQuery($query); } |
|
113 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
111 | + try { $this->Database_root->runQuery($query); } catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
114 | 112 | } |
115 | 113 | |
116 | 114 | /** |
@@ -123,8 +121,7 @@ discard block |
||
123 | 121 | # set query |
124 | 122 | $query = "create database `". $this->db['name'] ."`;"; |
125 | 123 | # execute |
126 | - try { $this->Database_root->runQuery($query); } |
|
127 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
124 | + try { $this->Database_root->runQuery($query); } catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
128 | 125 | } |
129 | 126 | |
130 | 127 | /** |
@@ -137,8 +134,7 @@ discard block |
||
137 | 134 | # set query |
138 | 135 | $query = 'grant ALL on `'. $this->db['name'] .'`.* to '. $this->db['user'] .'@localhost identified by "'. $this->db['pass'] .'";'; |
139 | 136 | # execute |
140 | - try { $this->Database_root->runQuery($query); } |
|
141 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
137 | + try { $this->Database_root->runQuery($query); } catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
142 | 138 | } |
143 | 139 | |
144 | 140 | /** |
@@ -158,14 +154,11 @@ discard block |
||
158 | 154 | foreach($queries as $q) { |
159 | 155 | //length check |
160 | 156 | if (strlen($q)>0) { |
161 | - try { $this->Database_root->runQuery($q.";"); } |
|
162 | - catch (Exception $e) { |
|
157 | + try { $this->Database_root->runQuery($q.";"); } catch (Exception $e) { |
|
163 | 158 | //unlock tables |
164 | - try { $this->Database_root->runQuery("UNLOCK TABLES;"); } |
|
165 | - catch (Exception $e) {} |
|
159 | + try { $this->Database_root->runQuery("UNLOCK TABLES;"); } catch (Exception $e) {} |
|
166 | 160 | //drop database |
167 | - try { $this->Database_root->runQuery("drop database if exists `". $this->db['name'] ."`;"); } |
|
168 | - catch (Exception $e) { |
|
161 | + try { $this->Database_root->runQuery("drop database if exists `". $this->db['name'] ."`;"); } catch (Exception $e) { |
|
169 | 162 | $this->Result->show("danger", 'Cannot drop database: '.$e->getMessage(), true); |
170 | 163 | } |
171 | 164 | //print error |
@@ -201,8 +194,7 @@ discard block |
||
201 | 194 | */ |
202 | 195 | public function check_db_connection ($redirect = false) { |
203 | 196 | # try to connect |
204 | - try { $res = $this->Database->connect(); } |
|
205 | - catch (Exception $e) { |
|
197 | + try { $res = $this->Database->connect(); } catch (Exception $e) { |
|
206 | 198 | $this->exception = $e->getMessage(); |
207 | 199 | # redirect ? |
208 | 200 | if($redirect == true) { $this->redirect_to_install (); } |
@@ -223,10 +215,21 @@ discard block |
||
223 | 215 | # set query |
224 | 216 | $query = "SELECT COUNT(*) AS `cnt` FROM information_schema.tables WHERE table_schema = '".$this->db['name']."' AND table_name = '$table';"; |
225 | 217 | # try to fetch count |
226 | - try { $table = $this->Database->getObjectQuery($query); } |
|
227 | - catch (Exception $e) { if($redirect === true) $this->redirect_to_install (); else return false; } |
|
218 | + try { $table = $this->Database->getObjectQuery($query); } catch (Exception $e) { if($redirect === true) { |
|
219 | + $this->redirect_to_install (); |
|
220 | + } |
|
221 | + else { |
|
222 | + return false; |
|
223 | + } |
|
224 | + } |
|
228 | 225 | # redirect if it is not existing |
229 | - if($table->cnt!=1) { if($redirect === true) $this->redirect_to_install (); else return false; } |
|
226 | + if($table->cnt!=1) { if($redirect === true) { |
|
227 | + $this->redirect_to_install (); |
|
228 | + } |
|
229 | + else { |
|
230 | + return false; |
|
231 | + } |
|
232 | + } |
|
230 | 233 | # ok |
231 | 234 | return true; |
232 | 235 | } |
@@ -303,8 +306,7 @@ discard block |
||
303 | 306 | * @return void |
304 | 307 | */ |
305 | 308 | public function postauth_update_admin_pass ($adminpass) { |
306 | - try { $this->Database->updateObject("users", array("password"=>$adminpass, "passChange"=>"No","username"=>"Admin"), "username"); } |
|
307 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
309 | + try { $this->Database->updateObject("users", array("password"=>$adminpass, "passChange"=>"No","username"=>"Admin"), "username"); } catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
308 | 310 | return true; |
309 | 311 | } |
310 | 312 | |
@@ -317,8 +319,7 @@ discard block |
||
317 | 319 | * @return void |
318 | 320 | */ |
319 | 321 | private function postauth_update_settings ($siteTitle, $siteURL) { |
320 | - try { $this->Database->updateObject("settings", array("siteTitle"=>$siteTitle, "siteURL"=>$siteURL,"id"=>1), "id"); } |
|
321 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
322 | + try { $this->Database->updateObject("settings", array("siteTitle"=>$siteTitle, "siteURL"=>$siteURL,"id"=>1), "id"); } catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
322 | 323 | return true; |
323 | 324 | } |
324 | 325 | |
@@ -378,8 +379,7 @@ discard block |
||
378 | 379 | # execute all queries |
379 | 380 | foreach($queries as $query) { |
380 | 381 | if (strlen($query)>5) { |
381 | - try { $this->Database->runQuery($query); } |
|
382 | - catch (Exception $e) { |
|
382 | + try { $this->Database->runQuery($query); } catch (Exception $e) { |
|
383 | 383 | $this->Log = new Logging ($this->Database); |
384 | 384 | # write log |
385 | 385 | $this->Log->write( "Database upgrade", $e->getMessage()."<br>query: ".$query, 2 ); |
@@ -7,461 +7,461 @@ discard block |
||
7 | 7 | class Install extends Common_functions { |
8 | 8 | |
9 | 9 | |
10 | - /** |
|
11 | - * to store DB exceptions |
|
12 | - * |
|
13 | - * @var mixed |
|
14 | - * @access public |
|
15 | - */ |
|
16 | - public $exception; |
|
17 | - |
|
18 | - /** |
|
19 | - * Database parameters |
|
20 | - * |
|
21 | - * @var mixed |
|
22 | - * @access protected |
|
23 | - */ |
|
24 | - protected $db; |
|
25 | - |
|
26 | - /** |
|
27 | - * debugging flag |
|
28 | - * |
|
29 | - * (default value: false) |
|
30 | - * |
|
31 | - * @var bool |
|
32 | - * @access public |
|
33 | - */ |
|
34 | - public $debugging = false; |
|
35 | - |
|
36 | - /** |
|
37 | - * Result |
|
38 | - * |
|
39 | - * @var mixed |
|
40 | - * @access public |
|
41 | - */ |
|
42 | - public $Result; |
|
43 | - |
|
44 | - /** |
|
45 | - * Database |
|
46 | - * |
|
47 | - * @var mixed |
|
48 | - * @access protected |
|
49 | - */ |
|
50 | - protected $Database; |
|
51 | - |
|
52 | - /** |
|
53 | - * Database_root - for initial installation |
|
54 | - * |
|
55 | - * @var mixed |
|
56 | - * @access protected |
|
57 | - */ |
|
58 | - protected $Database_root; |
|
59 | - |
|
60 | - /** |
|
61 | - * Log |
|
62 | - * |
|
63 | - * @var mixed |
|
64 | - * @access public |
|
65 | - */ |
|
66 | - public $Log; |
|
67 | - |
|
68 | - |
|
69 | - |
|
70 | - |
|
71 | - |
|
72 | - /** |
|
73 | - * __construct function. |
|
74 | - * |
|
75 | - * @access public |
|
76 | - * @param Database_PDO $Database |
|
77 | - */ |
|
78 | - public function __construct (Database_PDO $Database) { |
|
79 | - # initialize Result |
|
80 | - $this->Result = new Result (); |
|
81 | - # initialize object |
|
82 | - $this->Database = $Database; |
|
83 | - # set debugging |
|
84 | - $this->set_debugging (); |
|
85 | - # set debugging |
|
86 | - $this->set_db_params (); |
|
87 | - # Log object |
|
88 | - try { $this->Database->connect(); } |
|
89 | - catch ( Exception $e ) {} |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - |
|
94 | - |
|
95 | - |
|
96 | - |
|
97 | - |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @install methods |
|
102 | - * ------------------------------ |
|
103 | - */ |
|
104 | - |
|
105 | - /** |
|
106 | - * Install database files |
|
107 | - * |
|
108 | - * @access public |
|
109 | - * @param mixed $rootuser |
|
110 | - * @param mixed $rootpass |
|
111 | - * @param bool $drop_database (default: false) |
|
112 | - * @param bool $create_database (default: false) |
|
113 | - * @param bool $create_grants (default: false) |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - public function install_database ($rootuser, $rootpass, $drop_database = false, $create_database = false, $create_grants = false) { |
|
117 | - |
|
118 | - # open new connection |
|
119 | - $this->Database_root = new Database_PDO ($rootuser, $rootpass); |
|
120 | - |
|
121 | - # set install flag to make sure DB is not trying to be selected via DSN |
|
122 | - $this->Database_root->install = true; |
|
123 | - |
|
124 | - # drop database if requested |
|
125 | - if($drop_database===true) { $this->drop_database(); } |
|
126 | - |
|
127 | - # create database if requested |
|
128 | - if($create_database===true) { $this->create_database(); } |
|
129 | - |
|
130 | - # set permissions! |
|
131 | - if($create_grants===true) { $this->create_grants(); } |
|
132 | - |
|
133 | - # reset connection, reset install flag and connect again |
|
134 | - $this->Database_root->resetConn(); |
|
135 | - |
|
136 | - # install database |
|
137 | - if($this->install_database_execute () !== false) { |
|
138 | - # return true, if some errors occured script already died! */ |
|
139 | - sleep(1); |
|
140 | - $this->Log = new Logging ($this->Database); |
|
141 | - $this->Log->write( "Database installation", "Database installed successfully. Version ".VERSION.".".REVISION." installed", 1 ); |
|
142 | - return true; |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Drop existing database |
|
148 | - * |
|
149 | - * @access private |
|
150 | - * @return void |
|
151 | - */ |
|
152 | - private function drop_database () { |
|
153 | - # set query |
|
154 | - $query = "drop database if exists `". $this->db['name'] ."`;"; |
|
155 | - # execute |
|
156 | - try { $this->Database_root->runQuery($query); } |
|
157 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Create database |
|
162 | - * |
|
163 | - * @access private |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - private function create_database () { |
|
167 | - # set query |
|
168 | - $query = "create database `". $this->db['name'] ."`;"; |
|
169 | - # execute |
|
170 | - try { $this->Database_root->runQuery($query); } |
|
171 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Create user grants |
|
176 | - * |
|
177 | - * @access private |
|
178 | - * @return void |
|
179 | - */ |
|
180 | - private function create_grants () { |
|
181 | - # set query |
|
182 | - $query = 'grant ALL on `'. $this->db['name'] .'`.* to '. $this->db['user'] .'@localhost identified by "'. $this->db['pass'] .'";'; |
|
183 | - # execute |
|
184 | - try { $this->Database_root->runQuery($query); } |
|
185 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Execute files installation |
|
190 | - * |
|
191 | - * @access private |
|
192 | - * @return void |
|
193 | - */ |
|
194 | - private function install_database_execute () { |
|
195 | - # import SCHEMA file queries |
|
196 | - $query = file_get_contents("../../db/SCHEMA.sql"); |
|
197 | - |
|
198 | - # formulate queries |
|
199 | - $queries = array_filter(explode(";\n", $query)); |
|
200 | - |
|
201 | - # execute |
|
202 | - foreach($queries as $q) { |
|
203 | - //length check |
|
204 | - if (strlen($q)>0) { |
|
205 | - try { $this->Database_root->runQuery($q.";"); } |
|
206 | - catch (Exception $e) { |
|
207 | - //unlock tables |
|
208 | - try { $this->Database_root->runQuery("UNLOCK TABLES;"); } |
|
209 | - catch (Exception $e) {} |
|
210 | - //drop database |
|
211 | - try { $this->Database_root->runQuery("drop database if exists `". $this->db['name'] ."`;"); } |
|
212 | - catch (Exception $e) { |
|
213 | - $this->Result->show("danger", 'Cannot drop database: '.$e->getMessage(), true); |
|
214 | - } |
|
215 | - //print error |
|
216 | - $this->Result->show("danger", "Cannot install sql SCHEMA file: ".$e->getMessage()."<br>query that failed: <pre>$q</pre>", false); |
|
217 | - $this->Result->show("info", "Database dropped", false); |
|
218 | - |
|
219 | - return false; |
|
220 | - } |
|
221 | - } |
|
222 | - } |
|
223 | - } |
|
224 | - |
|
225 | - |
|
226 | - |
|
227 | - |
|
228 | - |
|
229 | - |
|
230 | - |
|
231 | - |
|
232 | - |
|
233 | - |
|
234 | - /** |
|
235 | - * @check methods |
|
236 | - * ------------------------------ |
|
237 | - */ |
|
238 | - |
|
239 | - /** |
|
240 | - * Tries to connect to database |
|
241 | - * |
|
242 | - * @access public |
|
243 | - * @param bool $redirect |
|
244 | - * @return void |
|
245 | - */ |
|
246 | - public function check_db_connection ($redirect = false) { |
|
247 | - # try to connect |
|
248 | - try { $res = $this->Database->connect(); } |
|
249 | - catch (Exception $e) { |
|
250 | - $this->exception = $e->getMessage(); |
|
251 | - # redirect ? |
|
252 | - if($redirect == true) { $this->redirect_to_install (); } |
|
253 | - else { return false; } |
|
254 | - } |
|
255 | - # ok |
|
256 | - return true; |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * Checks if table exists |
|
261 | - * |
|
262 | - * @access public |
|
263 | - * @param mixed $table |
|
264 | - * @return void |
|
265 | - */ |
|
266 | - public function check_table ($table, $redirect = false) { |
|
267 | - # set query |
|
268 | - $query = "SELECT COUNT(*) AS `cnt` FROM information_schema.tables WHERE table_schema = '".$this->db['name']."' AND table_name = '$table';"; |
|
269 | - # try to fetch count |
|
270 | - try { $table = $this->Database->getObjectQuery($query); } |
|
271 | - catch (Exception $e) { if($redirect === true) $this->redirect_to_install (); else return false; } |
|
272 | - # redirect if it is not existing |
|
273 | - if($table->cnt!=1) { if($redirect === true) $this->redirect_to_install (); else return false; } |
|
274 | - # ok |
|
275 | - return true; |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * This function redirects to install page |
|
280 | - * |
|
281 | - * @access private |
|
282 | - * @return void |
|
283 | - */ |
|
284 | - private function redirect_to_install () { |
|
285 | - # redirect to install |
|
286 | - header("Location: ".create_link("install")); |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * sets debugging if set in config.php file |
|
291 | - * |
|
292 | - * @access private |
|
293 | - * @return void |
|
294 | - */ |
|
295 | - public function set_debugging () { |
|
296 | - require( dirname(__FILE__) . '/../../config.php' ); |
|
297 | - if($debugging==true) { $this->debugging = true; } |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Sets DB parmaeters |
|
302 | - * |
|
303 | - * @access private |
|
304 | - * @return void |
|
305 | - */ |
|
306 | - private function set_db_params () { |
|
307 | - require( dirname(__FILE__) . '/../../config.php' ); |
|
308 | - $this->db = $db; |
|
309 | - } |
|
310 | - |
|
311 | - |
|
312 | - |
|
313 | - |
|
314 | - |
|
315 | - |
|
316 | - |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * @postinstallation functions |
|
321 | - * ------------------------------ |
|
322 | - */ |
|
323 | - |
|
324 | - /** |
|
325 | - * Post installation settings update. |
|
326 | - * |
|
327 | - * @access public |
|
328 | - * @param mixed $adminpass |
|
329 | - * @param mixed $siteTitle |
|
330 | - * @param mixed $siteURL |
|
331 | - * @return void |
|
332 | - */ |
|
333 | - function postauth_update($adminpass, $siteTitle, $siteURL) { |
|
334 | - # update Admin pass |
|
335 | - $this->postauth_update_admin_pass ($adminpass); |
|
336 | - # update settings |
|
337 | - $this->postauth_update_settings ($siteTitle, $siteURL); |
|
338 | - # ok |
|
339 | - return true; |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Updates admin password after installation |
|
344 | - * |
|
345 | - * @access public |
|
346 | - * @param mixed $adminpass |
|
347 | - * @return void |
|
348 | - */ |
|
349 | - public function postauth_update_admin_pass ($adminpass) { |
|
350 | - try { $this->Database->updateObject("users", array("password"=>$adminpass, "passChange"=>"No","username"=>"Admin"), "username"); } |
|
351 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
352 | - return true; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Updates settings after installation |
|
357 | - * |
|
358 | - * @access private |
|
359 | - * @param mixed $siteTitle |
|
360 | - * @param mixed $siteURL |
|
361 | - * @return void |
|
362 | - */ |
|
363 | - private function postauth_update_settings ($siteTitle, $siteURL) { |
|
364 | - try { $this->Database->updateObject("settings", array("siteTitle"=>$siteTitle, "siteURL"=>$siteURL,"id"=>1), "id"); } |
|
365 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
366 | - return true; |
|
367 | - } |
|
368 | - |
|
369 | - |
|
370 | - |
|
371 | - |
|
372 | - |
|
373 | - |
|
374 | - |
|
375 | - |
|
376 | - |
|
377 | - |
|
378 | - /** |
|
379 | - * @upgrade database |
|
380 | - * ----------------- |
|
381 | - */ |
|
382 | - |
|
383 | - /** |
|
384 | - * Upgrade database checks and executes. |
|
385 | - * |
|
386 | - * @access public |
|
387 | - * @return void |
|
388 | - */ |
|
389 | - public function upgrade_database () { |
|
390 | - # first check version |
|
391 | - $this->get_settings (); |
|
392 | - |
|
393 | - if($this->settings->version == VERSION) { $this->Result->show("danger", "Database already at latest version", true); } |
|
394 | - else { |
|
395 | - # check db connection |
|
396 | - if($this->check_db_connection(false)===false) { $this->Result->show("danger", "Cannot connect to database", true); } |
|
397 | - # execute |
|
398 | - else { |
|
399 | - return $this->upgrade_database_execute (); |
|
400 | - } |
|
401 | - } |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Execute database upgrade. |
|
406 | - * |
|
407 | - * @access private |
|
408 | - * @return void |
|
409 | - */ |
|
410 | - private function upgrade_database_execute () { |
|
411 | - # set queries |
|
412 | - $subversion_queries = $this->get_upgrade_queries (); |
|
413 | - // create default arrays |
|
414 | - $queries = array(); |
|
415 | - // succesfull queries: |
|
416 | - $queries_ok = array(); |
|
417 | - |
|
418 | - // replace CRLF |
|
419 | - $subversion_queries = str_replace("\r\n", "\n", $subversion_queries); |
|
420 | - $queries = array_filter(explode(";\n", $subversion_queries)); |
|
421 | - |
|
422 | - # execute all queries |
|
423 | - foreach($queries as $query) { |
|
424 | - if (strlen($query)>5) { |
|
425 | - try { $this->Database->runQuery($query); } |
|
426 | - catch (Exception $e) { |
|
427 | - $this->Log = new Logging ($this->Database); |
|
428 | - # write log |
|
429 | - $this->Log->write( "Database upgrade", $e->getMessage()."<br>query: ".$query, 2 ); |
|
430 | - # fail |
|
431 | - print "<h3>Upgrade failed !</h3><hr style='margin:30px;'>"; |
|
432 | - $this->Result->show("danger", $e->getMessage()."<hr>Failed query: <pre>".$query.";</pre>", false); |
|
433 | - $this->Result->show("success", "Succesfull queries: <pre>".implode(";", $queries_ok).";</pre>", false); |
|
434 | - # revert version |
|
435 | - //try { $this->Database->runQuery('update `settings` set `version` = ?', array($this->settings->version)); } |
|
436 | - //catch (Exception $e) { var_dump($e); } |
|
437 | - // false |
|
438 | - return false; |
|
439 | - } |
|
440 | - // save ok |
|
441 | - $queries_ok[] = $query; |
|
442 | - } |
|
443 | - } |
|
444 | - |
|
445 | - |
|
446 | - # all good, print it |
|
447 | - sleep(1); |
|
448 | - $this->Log = new Logging ($this->Database); |
|
449 | - $this->Log->write( "Database upgrade", "Database upgraded from version ".$this->settings->version." to version ".VERSION.".".REVISION, 1 ); |
|
450 | - return true; |
|
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * Fetch all upgrade queries from DB files |
|
455 | - * |
|
456 | - * @access public |
|
457 | - * @return void |
|
458 | - */ |
|
459 | - public function get_upgrade_queries () { |
|
460 | - // save all queries fro UPDATE.sql file |
|
461 | - $queries = str_replace("\r\n", "\n", (file_get_contents( dirname(__FILE__) . '/../../db/UPDATE.sql'))); |
|
462 | - |
|
463 | - // fetch settings if not present - for manual instructions |
|
464 | - if (!isset($this->settings->version)) { $this->get_settings (); } |
|
10 | + /** |
|
11 | + * to store DB exceptions |
|
12 | + * |
|
13 | + * @var mixed |
|
14 | + * @access public |
|
15 | + */ |
|
16 | + public $exception; |
|
17 | + |
|
18 | + /** |
|
19 | + * Database parameters |
|
20 | + * |
|
21 | + * @var mixed |
|
22 | + * @access protected |
|
23 | + */ |
|
24 | + protected $db; |
|
25 | + |
|
26 | + /** |
|
27 | + * debugging flag |
|
28 | + * |
|
29 | + * (default value: false) |
|
30 | + * |
|
31 | + * @var bool |
|
32 | + * @access public |
|
33 | + */ |
|
34 | + public $debugging = false; |
|
35 | + |
|
36 | + /** |
|
37 | + * Result |
|
38 | + * |
|
39 | + * @var mixed |
|
40 | + * @access public |
|
41 | + */ |
|
42 | + public $Result; |
|
43 | + |
|
44 | + /** |
|
45 | + * Database |
|
46 | + * |
|
47 | + * @var mixed |
|
48 | + * @access protected |
|
49 | + */ |
|
50 | + protected $Database; |
|
51 | + |
|
52 | + /** |
|
53 | + * Database_root - for initial installation |
|
54 | + * |
|
55 | + * @var mixed |
|
56 | + * @access protected |
|
57 | + */ |
|
58 | + protected $Database_root; |
|
59 | + |
|
60 | + /** |
|
61 | + * Log |
|
62 | + * |
|
63 | + * @var mixed |
|
64 | + * @access public |
|
65 | + */ |
|
66 | + public $Log; |
|
67 | + |
|
68 | + |
|
69 | + |
|
70 | + |
|
71 | + |
|
72 | + /** |
|
73 | + * __construct function. |
|
74 | + * |
|
75 | + * @access public |
|
76 | + * @param Database_PDO $Database |
|
77 | + */ |
|
78 | + public function __construct (Database_PDO $Database) { |
|
79 | + # initialize Result |
|
80 | + $this->Result = new Result (); |
|
81 | + # initialize object |
|
82 | + $this->Database = $Database; |
|
83 | + # set debugging |
|
84 | + $this->set_debugging (); |
|
85 | + # set debugging |
|
86 | + $this->set_db_params (); |
|
87 | + # Log object |
|
88 | + try { $this->Database->connect(); } |
|
89 | + catch ( Exception $e ) {} |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + |
|
94 | + |
|
95 | + |
|
96 | + |
|
97 | + |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @install methods |
|
102 | + * ------------------------------ |
|
103 | + */ |
|
104 | + |
|
105 | + /** |
|
106 | + * Install database files |
|
107 | + * |
|
108 | + * @access public |
|
109 | + * @param mixed $rootuser |
|
110 | + * @param mixed $rootpass |
|
111 | + * @param bool $drop_database (default: false) |
|
112 | + * @param bool $create_database (default: false) |
|
113 | + * @param bool $create_grants (default: false) |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + public function install_database ($rootuser, $rootpass, $drop_database = false, $create_database = false, $create_grants = false) { |
|
117 | + |
|
118 | + # open new connection |
|
119 | + $this->Database_root = new Database_PDO ($rootuser, $rootpass); |
|
120 | + |
|
121 | + # set install flag to make sure DB is not trying to be selected via DSN |
|
122 | + $this->Database_root->install = true; |
|
123 | + |
|
124 | + # drop database if requested |
|
125 | + if($drop_database===true) { $this->drop_database(); } |
|
126 | + |
|
127 | + # create database if requested |
|
128 | + if($create_database===true) { $this->create_database(); } |
|
129 | + |
|
130 | + # set permissions! |
|
131 | + if($create_grants===true) { $this->create_grants(); } |
|
132 | + |
|
133 | + # reset connection, reset install flag and connect again |
|
134 | + $this->Database_root->resetConn(); |
|
135 | + |
|
136 | + # install database |
|
137 | + if($this->install_database_execute () !== false) { |
|
138 | + # return true, if some errors occured script already died! */ |
|
139 | + sleep(1); |
|
140 | + $this->Log = new Logging ($this->Database); |
|
141 | + $this->Log->write( "Database installation", "Database installed successfully. Version ".VERSION.".".REVISION." installed", 1 ); |
|
142 | + return true; |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Drop existing database |
|
148 | + * |
|
149 | + * @access private |
|
150 | + * @return void |
|
151 | + */ |
|
152 | + private function drop_database () { |
|
153 | + # set query |
|
154 | + $query = "drop database if exists `". $this->db['name'] ."`;"; |
|
155 | + # execute |
|
156 | + try { $this->Database_root->runQuery($query); } |
|
157 | + catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Create database |
|
162 | + * |
|
163 | + * @access private |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + private function create_database () { |
|
167 | + # set query |
|
168 | + $query = "create database `". $this->db['name'] ."`;"; |
|
169 | + # execute |
|
170 | + try { $this->Database_root->runQuery($query); } |
|
171 | + catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Create user grants |
|
176 | + * |
|
177 | + * @access private |
|
178 | + * @return void |
|
179 | + */ |
|
180 | + private function create_grants () { |
|
181 | + # set query |
|
182 | + $query = 'grant ALL on `'. $this->db['name'] .'`.* to '. $this->db['user'] .'@localhost identified by "'. $this->db['pass'] .'";'; |
|
183 | + # execute |
|
184 | + try { $this->Database_root->runQuery($query); } |
|
185 | + catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Execute files installation |
|
190 | + * |
|
191 | + * @access private |
|
192 | + * @return void |
|
193 | + */ |
|
194 | + private function install_database_execute () { |
|
195 | + # import SCHEMA file queries |
|
196 | + $query = file_get_contents("../../db/SCHEMA.sql"); |
|
197 | + |
|
198 | + # formulate queries |
|
199 | + $queries = array_filter(explode(";\n", $query)); |
|
200 | + |
|
201 | + # execute |
|
202 | + foreach($queries as $q) { |
|
203 | + //length check |
|
204 | + if (strlen($q)>0) { |
|
205 | + try { $this->Database_root->runQuery($q.";"); } |
|
206 | + catch (Exception $e) { |
|
207 | + //unlock tables |
|
208 | + try { $this->Database_root->runQuery("UNLOCK TABLES;"); } |
|
209 | + catch (Exception $e) {} |
|
210 | + //drop database |
|
211 | + try { $this->Database_root->runQuery("drop database if exists `". $this->db['name'] ."`;"); } |
|
212 | + catch (Exception $e) { |
|
213 | + $this->Result->show("danger", 'Cannot drop database: '.$e->getMessage(), true); |
|
214 | + } |
|
215 | + //print error |
|
216 | + $this->Result->show("danger", "Cannot install sql SCHEMA file: ".$e->getMessage()."<br>query that failed: <pre>$q</pre>", false); |
|
217 | + $this->Result->show("info", "Database dropped", false); |
|
218 | + |
|
219 | + return false; |
|
220 | + } |
|
221 | + } |
|
222 | + } |
|
223 | + } |
|
224 | + |
|
225 | + |
|
226 | + |
|
227 | + |
|
228 | + |
|
229 | + |
|
230 | + |
|
231 | + |
|
232 | + |
|
233 | + |
|
234 | + /** |
|
235 | + * @check methods |
|
236 | + * ------------------------------ |
|
237 | + */ |
|
238 | + |
|
239 | + /** |
|
240 | + * Tries to connect to database |
|
241 | + * |
|
242 | + * @access public |
|
243 | + * @param bool $redirect |
|
244 | + * @return void |
|
245 | + */ |
|
246 | + public function check_db_connection ($redirect = false) { |
|
247 | + # try to connect |
|
248 | + try { $res = $this->Database->connect(); } |
|
249 | + catch (Exception $e) { |
|
250 | + $this->exception = $e->getMessage(); |
|
251 | + # redirect ? |
|
252 | + if($redirect == true) { $this->redirect_to_install (); } |
|
253 | + else { return false; } |
|
254 | + } |
|
255 | + # ok |
|
256 | + return true; |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * Checks if table exists |
|
261 | + * |
|
262 | + * @access public |
|
263 | + * @param mixed $table |
|
264 | + * @return void |
|
265 | + */ |
|
266 | + public function check_table ($table, $redirect = false) { |
|
267 | + # set query |
|
268 | + $query = "SELECT COUNT(*) AS `cnt` FROM information_schema.tables WHERE table_schema = '".$this->db['name']."' AND table_name = '$table';"; |
|
269 | + # try to fetch count |
|
270 | + try { $table = $this->Database->getObjectQuery($query); } |
|
271 | + catch (Exception $e) { if($redirect === true) $this->redirect_to_install (); else return false; } |
|
272 | + # redirect if it is not existing |
|
273 | + if($table->cnt!=1) { if($redirect === true) $this->redirect_to_install (); else return false; } |
|
274 | + # ok |
|
275 | + return true; |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * This function redirects to install page |
|
280 | + * |
|
281 | + * @access private |
|
282 | + * @return void |
|
283 | + */ |
|
284 | + private function redirect_to_install () { |
|
285 | + # redirect to install |
|
286 | + header("Location: ".create_link("install")); |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * sets debugging if set in config.php file |
|
291 | + * |
|
292 | + * @access private |
|
293 | + * @return void |
|
294 | + */ |
|
295 | + public function set_debugging () { |
|
296 | + require( dirname(__FILE__) . '/../../config.php' ); |
|
297 | + if($debugging==true) { $this->debugging = true; } |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Sets DB parmaeters |
|
302 | + * |
|
303 | + * @access private |
|
304 | + * @return void |
|
305 | + */ |
|
306 | + private function set_db_params () { |
|
307 | + require( dirname(__FILE__) . '/../../config.php' ); |
|
308 | + $this->db = $db; |
|
309 | + } |
|
310 | + |
|
311 | + |
|
312 | + |
|
313 | + |
|
314 | + |
|
315 | + |
|
316 | + |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * @postinstallation functions |
|
321 | + * ------------------------------ |
|
322 | + */ |
|
323 | + |
|
324 | + /** |
|
325 | + * Post installation settings update. |
|
326 | + * |
|
327 | + * @access public |
|
328 | + * @param mixed $adminpass |
|
329 | + * @param mixed $siteTitle |
|
330 | + * @param mixed $siteURL |
|
331 | + * @return void |
|
332 | + */ |
|
333 | + function postauth_update($adminpass, $siteTitle, $siteURL) { |
|
334 | + # update Admin pass |
|
335 | + $this->postauth_update_admin_pass ($adminpass); |
|
336 | + # update settings |
|
337 | + $this->postauth_update_settings ($siteTitle, $siteURL); |
|
338 | + # ok |
|
339 | + return true; |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Updates admin password after installation |
|
344 | + * |
|
345 | + * @access public |
|
346 | + * @param mixed $adminpass |
|
347 | + * @return void |
|
348 | + */ |
|
349 | + public function postauth_update_admin_pass ($adminpass) { |
|
350 | + try { $this->Database->updateObject("users", array("password"=>$adminpass, "passChange"=>"No","username"=>"Admin"), "username"); } |
|
351 | + catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
352 | + return true; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Updates settings after installation |
|
357 | + * |
|
358 | + * @access private |
|
359 | + * @param mixed $siteTitle |
|
360 | + * @param mixed $siteURL |
|
361 | + * @return void |
|
362 | + */ |
|
363 | + private function postauth_update_settings ($siteTitle, $siteURL) { |
|
364 | + try { $this->Database->updateObject("settings", array("siteTitle"=>$siteTitle, "siteURL"=>$siteURL,"id"=>1), "id"); } |
|
365 | + catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
366 | + return true; |
|
367 | + } |
|
368 | + |
|
369 | + |
|
370 | + |
|
371 | + |
|
372 | + |
|
373 | + |
|
374 | + |
|
375 | + |
|
376 | + |
|
377 | + |
|
378 | + /** |
|
379 | + * @upgrade database |
|
380 | + * ----------------- |
|
381 | + */ |
|
382 | + |
|
383 | + /** |
|
384 | + * Upgrade database checks and executes. |
|
385 | + * |
|
386 | + * @access public |
|
387 | + * @return void |
|
388 | + */ |
|
389 | + public function upgrade_database () { |
|
390 | + # first check version |
|
391 | + $this->get_settings (); |
|
392 | + |
|
393 | + if($this->settings->version == VERSION) { $this->Result->show("danger", "Database already at latest version", true); } |
|
394 | + else { |
|
395 | + # check db connection |
|
396 | + if($this->check_db_connection(false)===false) { $this->Result->show("danger", "Cannot connect to database", true); } |
|
397 | + # execute |
|
398 | + else { |
|
399 | + return $this->upgrade_database_execute (); |
|
400 | + } |
|
401 | + } |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Execute database upgrade. |
|
406 | + * |
|
407 | + * @access private |
|
408 | + * @return void |
|
409 | + */ |
|
410 | + private function upgrade_database_execute () { |
|
411 | + # set queries |
|
412 | + $subversion_queries = $this->get_upgrade_queries (); |
|
413 | + // create default arrays |
|
414 | + $queries = array(); |
|
415 | + // succesfull queries: |
|
416 | + $queries_ok = array(); |
|
417 | + |
|
418 | + // replace CRLF |
|
419 | + $subversion_queries = str_replace("\r\n", "\n", $subversion_queries); |
|
420 | + $queries = array_filter(explode(";\n", $subversion_queries)); |
|
421 | + |
|
422 | + # execute all queries |
|
423 | + foreach($queries as $query) { |
|
424 | + if (strlen($query)>5) { |
|
425 | + try { $this->Database->runQuery($query); } |
|
426 | + catch (Exception $e) { |
|
427 | + $this->Log = new Logging ($this->Database); |
|
428 | + # write log |
|
429 | + $this->Log->write( "Database upgrade", $e->getMessage()."<br>query: ".$query, 2 ); |
|
430 | + # fail |
|
431 | + print "<h3>Upgrade failed !</h3><hr style='margin:30px;'>"; |
|
432 | + $this->Result->show("danger", $e->getMessage()."<hr>Failed query: <pre>".$query.";</pre>", false); |
|
433 | + $this->Result->show("success", "Succesfull queries: <pre>".implode(";", $queries_ok).";</pre>", false); |
|
434 | + # revert version |
|
435 | + //try { $this->Database->runQuery('update `settings` set `version` = ?', array($this->settings->version)); } |
|
436 | + //catch (Exception $e) { var_dump($e); } |
|
437 | + // false |
|
438 | + return false; |
|
439 | + } |
|
440 | + // save ok |
|
441 | + $queries_ok[] = $query; |
|
442 | + } |
|
443 | + } |
|
444 | + |
|
445 | + |
|
446 | + # all good, print it |
|
447 | + sleep(1); |
|
448 | + $this->Log = new Logging ($this->Database); |
|
449 | + $this->Log->write( "Database upgrade", "Database upgraded from version ".$this->settings->version." to version ".VERSION.".".REVISION, 1 ); |
|
450 | + return true; |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * Fetch all upgrade queries from DB files |
|
455 | + * |
|
456 | + * @access public |
|
457 | + * @return void |
|
458 | + */ |
|
459 | + public function get_upgrade_queries () { |
|
460 | + // save all queries fro UPDATE.sql file |
|
461 | + $queries = str_replace("\r\n", "\n", (file_get_contents( dirname(__FILE__) . '/../../db/UPDATE.sql'))); |
|
462 | + |
|
463 | + // fetch settings if not present - for manual instructions |
|
464 | + if (!isset($this->settings->version)) { $this->get_settings (); } |
|
465 | 465 | |
466 | 466 | // explode and loop to get next version from current |
467 | 467 | $delimiter = false; |
@@ -483,7 +483,7 @@ discard block |
||
483 | 483 | $old_queries = explode("/* VERSION $delimiter */", $queries); |
484 | 484 | $old_queries = trim($old_queries[1]); |
485 | 485 | |
486 | - # return |
|
487 | - return $old_queries; |
|
488 | - } |
|
486 | + # return |
|
487 | + return $old_queries; |
|
488 | + } |
|
489 | 489 | } |
490 | 490 | \ No newline at end of file |
@@ -85,8 +85,8 @@ discard block |
||
85 | 85 | # set debugging |
86 | 86 | $this->set_db_params (); |
87 | 87 | # Log object |
88 | - try { $this->Database->connect(); } |
|
89 | - catch ( Exception $e ) {} |
|
88 | + try { $this->Database->connect (); } |
|
89 | + catch (Exception $e) {} |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | |
@@ -122,23 +122,23 @@ discard block |
||
122 | 122 | $this->Database_root->install = true; |
123 | 123 | |
124 | 124 | # drop database if requested |
125 | - if($drop_database===true) { $this->drop_database(); } |
|
125 | + if ($drop_database === true) { $this->drop_database (); } |
|
126 | 126 | |
127 | 127 | # create database if requested |
128 | - if($create_database===true) { $this->create_database(); } |
|
128 | + if ($create_database === true) { $this->create_database (); } |
|
129 | 129 | |
130 | 130 | # set permissions! |
131 | - if($create_grants===true) { $this->create_grants(); } |
|
131 | + if ($create_grants === true) { $this->create_grants (); } |
|
132 | 132 | |
133 | 133 | # reset connection, reset install flag and connect again |
134 | - $this->Database_root->resetConn(); |
|
134 | + $this->Database_root->resetConn (); |
|
135 | 135 | |
136 | 136 | # install database |
137 | - if($this->install_database_execute () !== false) { |
|
137 | + if ($this->install_database_execute () !== false) { |
|
138 | 138 | # return true, if some errors occured script already died! */ |
139 | - sleep(1); |
|
139 | + sleep (1); |
|
140 | 140 | $this->Log = new Logging ($this->Database); |
141 | - $this->Log->write( "Database installation", "Database installed successfully. Version ".VERSION.".".REVISION." installed", 1 ); |
|
141 | + $this->Log->write ("Database installation", "Database installed successfully. Version ".VERSION.".".REVISION." installed", 1); |
|
142 | 142 | return true; |
143 | 143 | } |
144 | 144 | } |
@@ -151,10 +151,10 @@ discard block |
||
151 | 151 | */ |
152 | 152 | private function drop_database () { |
153 | 153 | # set query |
154 | - $query = "drop database if exists `". $this->db['name'] ."`;"; |
|
154 | + $query = "drop database if exists `".$this->db['name']."`;"; |
|
155 | 155 | # execute |
156 | - try { $this->Database_root->runQuery($query); } |
|
157 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
156 | + try { $this->Database_root->runQuery ($query); } |
|
157 | + catch (Exception $e) { $this->Result->show ("danger", $e->getMessage (), true); } |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | /** |
@@ -165,10 +165,10 @@ discard block |
||
165 | 165 | */ |
166 | 166 | private function create_database () { |
167 | 167 | # set query |
168 | - $query = "create database `". $this->db['name'] ."`;"; |
|
168 | + $query = "create database `".$this->db['name']."`;"; |
|
169 | 169 | # execute |
170 | - try { $this->Database_root->runQuery($query); } |
|
171 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
170 | + try { $this->Database_root->runQuery ($query); } |
|
171 | + catch (Exception $e) { $this->Result->show ("danger", $e->getMessage (), true); } |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | /** |
@@ -179,10 +179,10 @@ discard block |
||
179 | 179 | */ |
180 | 180 | private function create_grants () { |
181 | 181 | # set query |
182 | - $query = 'grant ALL on `'. $this->db['name'] .'`.* to '. $this->db['user'] .'@localhost identified by "'. $this->db['pass'] .'";'; |
|
182 | + $query = 'grant ALL on `'.$this->db['name'].'`.* to '.$this->db['user'].'@localhost identified by "'.$this->db['pass'].'";'; |
|
183 | 183 | # execute |
184 | - try { $this->Database_root->runQuery($query); } |
|
185 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), true);} |
|
184 | + try { $this->Database_root->runQuery ($query); } |
|
185 | + catch (Exception $e) { $this->Result->show ("danger", $e->getMessage (), true); } |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | /** |
@@ -193,28 +193,28 @@ discard block |
||
193 | 193 | */ |
194 | 194 | private function install_database_execute () { |
195 | 195 | # import SCHEMA file queries |
196 | - $query = file_get_contents("../../db/SCHEMA.sql"); |
|
196 | + $query = file_get_contents ("../../db/SCHEMA.sql"); |
|
197 | 197 | |
198 | 198 | # formulate queries |
199 | - $queries = array_filter(explode(";\n", $query)); |
|
199 | + $queries = array_filter (explode (";\n", $query)); |
|
200 | 200 | |
201 | 201 | # execute |
202 | - foreach($queries as $q) { |
|
202 | + foreach ($queries as $q) { |
|
203 | 203 | //length check |
204 | - if (strlen($q)>0) { |
|
205 | - try { $this->Database_root->runQuery($q.";"); } |
|
204 | + if (strlen ($q) > 0) { |
|
205 | + try { $this->Database_root->runQuery ($q.";"); } |
|
206 | 206 | catch (Exception $e) { |
207 | 207 | //unlock tables |
208 | - try { $this->Database_root->runQuery("UNLOCK TABLES;"); } |
|
208 | + try { $this->Database_root->runQuery ("UNLOCK TABLES;"); } |
|
209 | 209 | catch (Exception $e) {} |
210 | 210 | //drop database |
211 | - try { $this->Database_root->runQuery("drop database if exists `". $this->db['name'] ."`;"); } |
|
211 | + try { $this->Database_root->runQuery ("drop database if exists `".$this->db['name']."`;"); } |
|
212 | 212 | catch (Exception $e) { |
213 | - $this->Result->show("danger", 'Cannot drop database: '.$e->getMessage(), true); |
|
213 | + $this->Result->show ("danger", 'Cannot drop database: '.$e->getMessage (), true); |
|
214 | 214 | } |
215 | 215 | //print error |
216 | - $this->Result->show("danger", "Cannot install sql SCHEMA file: ".$e->getMessage()."<br>query that failed: <pre>$q</pre>", false); |
|
217 | - $this->Result->show("info", "Database dropped", false); |
|
216 | + $this->Result->show ("danger", "Cannot install sql SCHEMA file: ".$e->getMessage ()."<br>query that failed: <pre>$q</pre>", false); |
|
217 | + $this->Result->show ("info", "Database dropped", false); |
|
218 | 218 | |
219 | 219 | return false; |
220 | 220 | } |
@@ -245,12 +245,12 @@ discard block |
||
245 | 245 | */ |
246 | 246 | public function check_db_connection ($redirect = false) { |
247 | 247 | # try to connect |
248 | - try { $res = $this->Database->connect(); } |
|
249 | - catch (Exception $e) { |
|
250 | - $this->exception = $e->getMessage(); |
|
248 | + try { $res = $this->Database->connect (); } |
|
249 | + catch (Exception $e) { |
|
250 | + $this->exception = $e->getMessage (); |
|
251 | 251 | # redirect ? |
252 | - if($redirect == true) { $this->redirect_to_install (); } |
|
253 | - else { return false; } |
|
252 | + if ($redirect == true) { $this->redirect_to_install (); } |
|
253 | + else { return false; } |
|
254 | 254 | } |
255 | 255 | # ok |
256 | 256 | return true; |
@@ -267,10 +267,10 @@ discard block |
||
267 | 267 | # set query |
268 | 268 | $query = "SELECT COUNT(*) AS `cnt` FROM information_schema.tables WHERE table_schema = '".$this->db['name']."' AND table_name = '$table';"; |
269 | 269 | # try to fetch count |
270 | - try { $table = $this->Database->getObjectQuery($query); } |
|
271 | - catch (Exception $e) { if($redirect === true) $this->redirect_to_install (); else return false; } |
|
270 | + try { $table = $this->Database->getObjectQuery ($query); } |
|
271 | + catch (Exception $e) { if ($redirect === true) $this->redirect_to_install (); else return false; } |
|
272 | 272 | # redirect if it is not existing |
273 | - if($table->cnt!=1) { if($redirect === true) $this->redirect_to_install (); else return false; } |
|
273 | + if ($table->cnt != 1) { if ($redirect === true) $this->redirect_to_install (); else return false; } |
|
274 | 274 | # ok |
275 | 275 | return true; |
276 | 276 | } |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | */ |
284 | 284 | private function redirect_to_install () { |
285 | 285 | # redirect to install |
286 | - header("Location: ".create_link("install")); |
|
286 | + header ("Location: ".create_link ("install")); |
|
287 | 287 | } |
288 | 288 | |
289 | 289 | /** |
@@ -293,8 +293,8 @@ discard block |
||
293 | 293 | * @return void |
294 | 294 | */ |
295 | 295 | public function set_debugging () { |
296 | - require( dirname(__FILE__) . '/../../config.php' ); |
|
297 | - if($debugging==true) { $this->debugging = true; } |
|
296 | + require(dirname (__FILE__).'/../../config.php'); |
|
297 | + if ($debugging == true) { $this->debugging = true; } |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | /** |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | * @return void |
305 | 305 | */ |
306 | 306 | private function set_db_params () { |
307 | - require( dirname(__FILE__) . '/../../config.php' ); |
|
307 | + require(dirname (__FILE__).'/../../config.php'); |
|
308 | 308 | $this->db = $db; |
309 | 309 | } |
310 | 310 | |
@@ -330,7 +330,7 @@ discard block |
||
330 | 330 | * @param mixed $siteURL |
331 | 331 | * @return void |
332 | 332 | */ |
333 | - function postauth_update($adminpass, $siteTitle, $siteURL) { |
|
333 | + function postauth_update ($adminpass, $siteTitle, $siteURL) { |
|
334 | 334 | # update Admin pass |
335 | 335 | $this->postauth_update_admin_pass ($adminpass); |
336 | 336 | # update settings |
@@ -347,8 +347,8 @@ discard block |
||
347 | 347 | * @return void |
348 | 348 | */ |
349 | 349 | public function postauth_update_admin_pass ($adminpass) { |
350 | - try { $this->Database->updateObject("users", array("password"=>$adminpass, "passChange"=>"No","username"=>"Admin"), "username"); } |
|
351 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
350 | + try { $this->Database->updateObject ("users", array ("password"=>$adminpass, "passChange"=>"No", "username"=>"Admin"), "username"); } |
|
351 | + catch (Exception $e) { $this->Result->show ("danger", $e->getMessage (), false); } |
|
352 | 352 | return true; |
353 | 353 | } |
354 | 354 | |
@@ -361,8 +361,8 @@ discard block |
||
361 | 361 | * @return void |
362 | 362 | */ |
363 | 363 | private function postauth_update_settings ($siteTitle, $siteURL) { |
364 | - try { $this->Database->updateObject("settings", array("siteTitle"=>$siteTitle, "siteURL"=>$siteURL,"id"=>1), "id"); } |
|
365 | - catch (Exception $e) { $this->Result->show("danger", $e->getMessage(), false); } |
|
364 | + try { $this->Database->updateObject ("settings", array ("siteTitle"=>$siteTitle, "siteURL"=>$siteURL, "id"=>1), "id"); } |
|
365 | + catch (Exception $e) { $this->Result->show ("danger", $e->getMessage (), false); } |
|
366 | 366 | return true; |
367 | 367 | } |
368 | 368 | |
@@ -390,10 +390,10 @@ discard block |
||
390 | 390 | # first check version |
391 | 391 | $this->get_settings (); |
392 | 392 | |
393 | - if($this->settings->version == VERSION) { $this->Result->show("danger", "Database already at latest version", true); } |
|
393 | + if ($this->settings->version == VERSION) { $this->Result->show ("danger", "Database already at latest version", true); } |
|
394 | 394 | else { |
395 | 395 | # check db connection |
396 | - if($this->check_db_connection(false)===false) { $this->Result->show("danger", "Cannot connect to database", true); } |
|
396 | + if ($this->check_db_connection (false) === false) { $this->Result->show ("danger", "Cannot connect to database", true); } |
|
397 | 397 | # execute |
398 | 398 | else { |
399 | 399 | return $this->upgrade_database_execute (); |
@@ -411,26 +411,26 @@ discard block |
||
411 | 411 | # set queries |
412 | 412 | $subversion_queries = $this->get_upgrade_queries (); |
413 | 413 | // create default arrays |
414 | - $queries = array(); |
|
414 | + $queries = array (); |
|
415 | 415 | // succesfull queries: |
416 | - $queries_ok = array(); |
|
416 | + $queries_ok = array (); |
|
417 | 417 | |
418 | 418 | // replace CRLF |
419 | - $subversion_queries = str_replace("\r\n", "\n", $subversion_queries); |
|
420 | - $queries = array_filter(explode(";\n", $subversion_queries)); |
|
419 | + $subversion_queries = str_replace ("\r\n", "\n", $subversion_queries); |
|
420 | + $queries = array_filter (explode (";\n", $subversion_queries)); |
|
421 | 421 | |
422 | 422 | # execute all queries |
423 | - foreach($queries as $query) { |
|
424 | - if (strlen($query)>5) { |
|
425 | - try { $this->Database->runQuery($query); } |
|
423 | + foreach ($queries as $query) { |
|
424 | + if (strlen ($query) > 5) { |
|
425 | + try { $this->Database->runQuery ($query); } |
|
426 | 426 | catch (Exception $e) { |
427 | 427 | $this->Log = new Logging ($this->Database); |
428 | 428 | # write log |
429 | - $this->Log->write( "Database upgrade", $e->getMessage()."<br>query: ".$query, 2 ); |
|
429 | + $this->Log->write ("Database upgrade", $e->getMessage ()."<br>query: ".$query, 2); |
|
430 | 430 | # fail |
431 | 431 | print "<h3>Upgrade failed !</h3><hr style='margin:30px;'>"; |
432 | - $this->Result->show("danger", $e->getMessage()."<hr>Failed query: <pre>".$query.";</pre>", false); |
|
433 | - $this->Result->show("success", "Succesfull queries: <pre>".implode(";", $queries_ok).";</pre>", false); |
|
432 | + $this->Result->show ("danger", $e->getMessage ()."<hr>Failed query: <pre>".$query.";</pre>", false); |
|
433 | + $this->Result->show ("success", "Succesfull queries: <pre>".implode (";", $queries_ok).";</pre>", false); |
|
434 | 434 | # revert version |
435 | 435 | //try { $this->Database->runQuery('update `settings` set `version` = ?', array($this->settings->version)); } |
436 | 436 | //catch (Exception $e) { var_dump($e); } |
@@ -444,9 +444,9 @@ discard block |
||
444 | 444 | |
445 | 445 | |
446 | 446 | # all good, print it |
447 | - sleep(1); |
|
447 | + sleep (1); |
|
448 | 448 | $this->Log = new Logging ($this->Database); |
449 | - $this->Log->write( "Database upgrade", "Database upgraded from version ".$this->settings->version." to version ".VERSION.".".REVISION, 1 ); |
|
449 | + $this->Log->write ("Database upgrade", "Database upgraded from version ".$this->settings->version." to version ".VERSION.".".REVISION, 1); |
|
450 | 450 | return true; |
451 | 451 | } |
452 | 452 | |
@@ -458,30 +458,30 @@ discard block |
||
458 | 458 | */ |
459 | 459 | public function get_upgrade_queries () { |
460 | 460 | // save all queries fro UPDATE.sql file |
461 | - $queries = str_replace("\r\n", "\n", (file_get_contents( dirname(__FILE__) . '/../../db/UPDATE.sql'))); |
|
461 | + $queries = str_replace ("\r\n", "\n", (file_get_contents (dirname (__FILE__).'/../../db/UPDATE.sql'))); |
|
462 | 462 | |
463 | 463 | // fetch settings if not present - for manual instructions |
464 | 464 | if (!isset($this->settings->version)) { $this->get_settings (); } |
465 | 465 | |
466 | 466 | // explode and loop to get next version from current |
467 | 467 | $delimiter = false; |
468 | - foreach (explode("/* VERSION ", $queries) as $k=>$q) { |
|
469 | - $q_version = str_replace(" */", "", array_shift(explode("\n", $q))); |
|
468 | + foreach (explode ("/* VERSION ", $queries) as $k=>$q) { |
|
469 | + $q_version = str_replace (" */", "", array_shift (explode ("\n", $q))); |
|
470 | 470 | |
471 | 471 | // if delimiter was found in previous loop |
472 | - if ($delimiter!==false) { |
|
472 | + if ($delimiter !== false) { |
|
473 | 473 | $delimiter = $q_version; |
474 | 474 | break; |
475 | 475 | } |
476 | 476 | // if match with current set pointer to next item - delimiter |
477 | - if ($q_version==$this->settings->version) { |
|
477 | + if ($q_version == $this->settings->version) { |
|
478 | 478 | $delimiter = true; |
479 | 479 | }; |
480 | 480 | } |
481 | 481 | |
482 | 482 | // remove older queries before this version |
483 | - $old_queries = explode("/* VERSION $delimiter */", $queries); |
|
484 | - $old_queries = trim($old_queries[1]); |
|
483 | + $old_queries = explode ("/* VERSION $delimiter */", $queries); |
|
484 | + $old_queries = trim ($old_queries[1]); |
|
485 | 485 | |
486 | 486 | # return |
487 | 487 | return $old_queries; |
@@ -80,7 +80,6 @@ discard block |
||
80 | 80 | * Sets header based on provided HTTP code |
81 | 81 | * |
82 | 82 | * @access private |
83 | - * @param mixed $code |
|
84 | 83 | * @return void |
85 | 84 | */ |
86 | 85 | private function set_header () { |
@@ -99,7 +98,7 @@ discard block |
||
99 | 98 | * |
100 | 99 | * @access public |
101 | 100 | * @param mixed $result |
102 | - * @return void |
|
101 | + * @return string |
|
103 | 102 | */ |
104 | 103 | public function formulate_result ($result) { |
105 | 104 | // make sure result is array |
@@ -210,7 +209,7 @@ discard block |
||
210 | 209 | * Outputs result |
211 | 210 | * |
212 | 211 | * @access private |
213 | - * @return void |
|
212 | + * @return string |
|
214 | 213 | */ |
215 | 214 | private function create_result () { |
216 | 215 | // reorder |
@@ -239,7 +238,7 @@ discard block |
||
239 | 238 | * Creates XML result |
240 | 239 | * |
241 | 240 | * @access private |
242 | - * @return void |
|
241 | + * @return string|false |
|
243 | 242 | */ |
244 | 243 | private function create_xml () { |
245 | 244 | // convert whole object to array |
@@ -329,7 +328,7 @@ discard block |
||
329 | 328 | * Creates JSON result |
330 | 329 | * |
331 | 330 | * @access private |
332 | - * @return void |
|
331 | + * @return string |
|
333 | 332 | */ |
334 | 333 | private function create_json () { |
335 | 334 | return json_encode((array) $this->result); |
@@ -270,7 +270,9 @@ discard block |
||
270 | 270 | if(strpos($key, " ")>0) { $key = str_replace(" ", "_", $key); } |
271 | 271 | |
272 | 272 | // if key is numeric append item |
273 | - if(is_numeric($key)) $key = "item".$key; |
|
273 | + if(is_numeric($key)) { |
|
274 | + $key = "item".$key; |
|
275 | + } |
|
274 | 276 | |
275 | 277 | // if array add child |
276 | 278 | if (is_array($value)) { |
@@ -300,8 +302,9 @@ discard block |
||
300 | 302 | * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0> |
301 | 303 | */ |
302 | 304 | public function xml_to_array ( $xmlObject, $out = array () ) { |
303 | - foreach ( (array) $xmlObject as $index => $node ) |
|
304 | - $out[$index] = ( is_object ( $node ) ) ? $this->xml_to_array ( $node ) : $node; |
|
305 | + foreach ( (array) $xmlObject as $index => $node ) { |
|
306 | + $out[$index] = ( is_object ( $node ) ) ? $this->xml_to_array ( $node ) : $node; |
|
307 | + } |
|
305 | 308 | |
306 | 309 | return $out; |
307 | 310 | } |
@@ -315,14 +318,18 @@ discard block |
||
315 | 318 | */ |
316 | 319 | private function object_to_array ($obj) { |
317 | 320 | // object to array |
318 | - if(is_object($obj)) $obj = (array) $obj; |
|
321 | + if(is_object($obj)) { |
|
322 | + $obj = (array) $obj; |
|
323 | + } |
|
319 | 324 | if(is_array($obj)) { |
320 | 325 | $new = array(); |
321 | 326 | foreach($obj as $key => $val) { |
322 | 327 | $new[$key] = $this->object_to_array($val); |
323 | 328 | } |
324 | 329 | } |
325 | - else $new = $obj; |
|
330 | + else { |
|
331 | + $new = $obj; |
|
332 | + } |
|
326 | 333 | return $new; |
327 | 334 | } |
328 | 335 |
@@ -8,369 +8,369 @@ |
||
8 | 8 | class Responses { |
9 | 9 | |
10 | 10 | |
11 | - /** |
|
12 | - * error code handler |
|
13 | - * |
|
14 | - * @var mixed |
|
15 | - * @access public |
|
16 | - */ |
|
17 | - public $errors; |
|
18 | - |
|
19 | - /** |
|
20 | - * result handler |
|
21 | - * |
|
22 | - * (default value: null) |
|
23 | - * |
|
24 | - * @var mixed |
|
25 | - * @access public |
|
26 | - */ |
|
27 | - public $result = null; |
|
28 | - |
|
29 | - /** |
|
30 | - * Sets result type |
|
31 | - * |
|
32 | - * @var mixed |
|
33 | - * @access private |
|
34 | - */ |
|
35 | - private $result_type; |
|
36 | - |
|
37 | - /** |
|
38 | - * is exception set? |
|
39 | - * |
|
40 | - * (default value: false) |
|
41 | - * |
|
42 | - * @var bool |
|
43 | - * @access public |
|
44 | - */ |
|
45 | - public $exception = false; |
|
46 | - |
|
47 | - |
|
48 | - |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * __construct function |
|
53 | - * |
|
54 | - * @access public |
|
55 | - */ |
|
56 | - public function __construct() { |
|
57 | - # set error codes |
|
58 | - $this->set_error_codes (); |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Sets error code object |
|
63 | - * |
|
64 | - * http://www.restapitutorial.com/httpstatuscodes.html |
|
65 | - * |
|
66 | - * @access private |
|
67 | - * @return void |
|
68 | - */ |
|
69 | - private function set_error_codes () { |
|
70 | - // OK |
|
71 | - $this->errors[200] = "OK"; |
|
72 | - $this->errors[201] = "Created"; |
|
73 | - $this->errors[202] = "Accepted"; |
|
74 | - $this->errors[204] = "No Content"; |
|
75 | - // Client errors |
|
76 | - $this->errors[400] = "Bad Request"; |
|
77 | - $this->errors[401] = "Unauthorized"; |
|
78 | - $this->errors[403] = "Forbidden"; |
|
79 | - $this->errors[404] = "Not Found"; |
|
80 | - $this->errors[405] = "Method Not Allowed"; |
|
81 | - $this->errors[415] = "Unsupported Media Type"; |
|
82 | - // Server errors |
|
83 | - $this->errors[500] = "Internal Server Error"; |
|
84 | - $this->errors[501] = "Not Implemented"; |
|
85 | - $this->errors[503] = "Service Unavailable"; |
|
86 | - $this->errors[505] = "HTTP Version Not Supported"; |
|
87 | - $this->errors[511] = "Network Authentication Required"; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Sets new header and throws exception |
|
92 | - * |
|
93 | - * @access public |
|
94 | - * @param int $code (default: 400) |
|
95 | - * @param mixed $exception |
|
96 | - * @return void |
|
97 | - */ |
|
98 | - public function throw_exception ($code = 400, $exception) { |
|
99 | - // set failed |
|
100 | - $this->exception = true; |
|
101 | - |
|
102 | - // set success |
|
103 | - $this->result['success'] = false; |
|
104 | - // set exit code |
|
105 | - $this->result['code'] = $code; |
|
106 | - // set message |
|
107 | - $this->result['message'] = $exception; |
|
108 | - |
|
109 | - // set header |
|
110 | - $this->set_header (); |
|
111 | - // throw exception |
|
112 | - throw new Exception($exception); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Sets header based on provided HTTP code |
|
117 | - * |
|
118 | - * @access private |
|
119 | - * @param mixed $code |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - private function set_header () { |
|
123 | - // wrong code |
|
124 | - if(!isset($this->exception)) { header("HTTP/1.1 500 Invalid result code"); } |
|
125 | - else { header("HTTP/1.1 ".$this->result['code']." ".$this->errors[$this->result['code']]); } |
|
126 | - |
|
127 | - // 401 - add location |
|
128 | - if ($this->result['code']==401) { |
|
129 | - $this->set_location_header ("/api/".$_REQUEST['app_id']."/user/"); |
|
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Formulates result to JSON or XML |
|
135 | - * |
|
136 | - * @access public |
|
137 | - * @param mixed $result |
|
138 | - * @return void |
|
139 | - */ |
|
140 | - public function formulate_result ($result) { |
|
141 | - // make sure result is array |
|
142 | - $this->result = is_null($this->result) ? (array) $result : $this->result; |
|
143 | - |
|
144 | - // get requested content type |
|
145 | - $this->get_request_content_type (); |
|
146 | - |
|
147 | - // set result contrnt type |
|
148 | - $this->set_content_type_header (); |
|
149 | - // set cache header |
|
150 | - $this->set_cache_header (); |
|
151 | - // set result header if not already set with $result['success']=false |
|
152 | - $this->exception===true ? : $this->set_success_header (); |
|
153 | - |
|
154 | - // return result |
|
155 | - return $this->create_result (); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Validates that proper content type is set in request |
|
160 | - * |
|
161 | - * @access public |
|
162 | - * @return void |
|
163 | - */ |
|
164 | - public function validate_content_type () { |
|
165 | - // not set, presume json |
|
166 | - if( !isset($_SERVER['CONTENT_TYPE']) ) {} |
|
167 | - // post |
|
168 | - elseif($_SERVER['CONTENT_TYPE']=="application/x-www-form-urlencoded") {} |
|
169 | - // set, verify |
|
170 | - elseif (!($_SERVER['CONTENT_TYPE']=="application/xml" || $_SERVER['CONTENT_TYPE']=="application/json")) { |
|
171 | - $this->throw_exception (415, "Invalid Content type ".$_SERVER['CONTENT_TYPE']); |
|
172 | - } |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Sets request content type |
|
177 | - * |
|
178 | - * @access public |
|
179 | - * @return void |
|
180 | - */ |
|
181 | - private function get_request_content_type () { |
|
182 | - $this->result_type = $_SERVER['CONTENT_TYPE']=="application/xml" ? "xml" : "json"; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Sets result content type |
|
187 | - * |
|
188 | - * @access private |
|
189 | - * @return void |
|
190 | - */ |
|
191 | - private function set_content_type_header () { |
|
192 | - // content_type |
|
193 | - $this->result_type == "xml" ? header('Content-Type: application/xml') : header('Content-Type: application/json'); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Sets Cache header. |
|
198 | - * |
|
199 | - * @access private |
|
200 | - * @return void |
|
201 | - */ |
|
202 | - private function set_cache_header ($seconds = NULL) { |
|
203 | - // none |
|
204 | - if($seconds===NULL) { |
|
205 | - header("Cache-Control: no-cache"); |
|
206 | - header("Pragma: no-cache"); |
|
207 | - } |
|
208 | - // cache |
|
209 | - else { |
|
210 | - header("Cache-Control: $seconds"); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Sets success header |
|
216 | - * |
|
217 | - * @access private |
|
218 | - * @return void |
|
219 | - */ |
|
220 | - private function set_success_header () { |
|
221 | - // check fo location |
|
222 | - if(isset($this->result['location'])) { |
|
223 | - $this->set_location_header ($this->result['location']); |
|
224 | - } |
|
225 | - |
|
226 | - // set success |
|
227 | - $this->result['success'] = true; |
|
228 | - |
|
229 | - // set header |
|
230 | - $this->set_header (); |
|
231 | - |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * Sets location header for newly created objects |
|
236 | - * |
|
237 | - * @access private |
|
238 | - * @param mixed $location |
|
239 | - * @return void |
|
240 | - */ |
|
241 | - private function set_location_header ($location) { |
|
242 | - header("Location: ".$location); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Outputs result |
|
247 | - * |
|
248 | - * @access private |
|
249 | - * @return void |
|
250 | - */ |
|
251 | - private function create_result () { |
|
252 | - // reorder |
|
253 | - $this->reorder_result (); |
|
254 | - // creates result |
|
255 | - return $this->result_type == "xml" ? $this->create_xml () : $this->create_json (); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Reorders result to proper format |
|
260 | - * |
|
261 | - * @access private |
|
262 | - * @return void |
|
263 | - */ |
|
264 | - private function reorder_result () { |
|
265 | - $tmp = $this->result; |
|
266 | - unset($this->result); |
|
267 | - // reset |
|
268 | - $this->result['code'] = $tmp['code']; |
|
269 | - $this->result['success'] = $tmp['success']; |
|
270 | - if(isset($tmp['message'])) { $this->result['message'] = $tmp['message']; } |
|
271 | - if(isset($tmp['data'])) { $this->result['data'] = $tmp['data']; } |
|
272 | - if(isset($tmp['ip'])) { $this->result['ip'] = $tmp['ip']; } |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Creates XML result |
|
277 | - * |
|
278 | - * @access private |
|
279 | - * @return void |
|
280 | - */ |
|
281 | - private function create_xml () { |
|
282 | - // convert whole object to array |
|
283 | - $this->result = $this->object_to_array($this->result); |
|
284 | - |
|
285 | - // new SimpleXMLElement object |
|
286 | - $xml = new SimpleXMLElement('<'.$_GET['controller'].'/>'); |
|
287 | - // generate xml from result |
|
288 | - $this->array_to_xml($xml, $this->result); |
|
289 | - |
|
290 | - // return XML result |
|
291 | - return $xml->asXML(); |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Transforms array to XML |
|
296 | - * |
|
297 | - * @access private |
|
298 | - * @param SimpleXMLElement $object |
|
299 | - * @param array $data |
|
300 | - * @return void |
|
301 | - */ |
|
302 | - private function array_to_xml(SimpleXMLElement $object, array $data) { |
|
303 | - // loop through values |
|
304 | - foreach ($data as $key => $value) { |
|
305 | - // if spaces exist in key replace them with underscores |
|
306 | - if(strpos($key, " ")>0) { $key = str_replace(" ", "_", $key); } |
|
307 | - |
|
308 | - // if key is numeric append item |
|
309 | - if(is_numeric($key)) $key = "item".$key; |
|
310 | - |
|
311 | - // if array add child |
|
312 | - if (is_array($value)) { |
|
313 | - $new_object = $object->addChild($key); |
|
314 | - $this->array_to_xml($new_object, $value); |
|
315 | - } |
|
316 | - // else write value |
|
317 | - else { |
|
318 | - $object->addChild($key, $value); |
|
319 | - } |
|
320 | - } |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * function xml2array |
|
325 | - * |
|
326 | - * This function is part of the PHP manual. |
|
327 | - * |
|
328 | - * The PHP manual text and comments are covered by the Creative Commons |
|
329 | - * Attribution 3.0 License, copyright (c) the PHP Documentation Group |
|
330 | - * |
|
331 | - * @author k dot antczak at livedata dot pl |
|
332 | - * @date 2011-04-22 06:08 UTC |
|
333 | - * @link http://www.php.net/manual/en/ref.simplexml.php#103617 |
|
334 | - * @license http://www.php.net/license/index.php#doc-lic |
|
335 | - * @license http://creativecommons.org/licenses/by/3.0/ |
|
336 | - * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0> |
|
337 | - */ |
|
338 | - public function xml_to_array ( $xmlObject, $out = array () ) { |
|
339 | - foreach ( (array) $xmlObject as $index => $node ) |
|
340 | - $out[$index] = ( is_object ( $node ) ) ? $this->xml_to_array ( $node ) : $node; |
|
341 | - |
|
342 | - return $out; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Transforms object to array |
|
347 | - * |
|
348 | - * @access private |
|
349 | - * @param mixed $obj |
|
350 | - * @return void |
|
351 | - */ |
|
352 | - private function object_to_array ($obj) { |
|
353 | - // object to array |
|
354 | - if(is_object($obj)) $obj = (array) $obj; |
|
355 | - if(is_array($obj)) { |
|
356 | - $new = array(); |
|
357 | - foreach($obj as $key => $val) { |
|
358 | - $new[$key] = $this->object_to_array($val); |
|
359 | - } |
|
360 | - } |
|
361 | - else $new = $obj; |
|
362 | - return $new; |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * Creates JSON result |
|
367 | - * |
|
368 | - * @access private |
|
369 | - * @return void |
|
370 | - */ |
|
371 | - private function create_json () { |
|
372 | - return json_encode((array) $this->result); |
|
373 | - } |
|
11 | + /** |
|
12 | + * error code handler |
|
13 | + * |
|
14 | + * @var mixed |
|
15 | + * @access public |
|
16 | + */ |
|
17 | + public $errors; |
|
18 | + |
|
19 | + /** |
|
20 | + * result handler |
|
21 | + * |
|
22 | + * (default value: null) |
|
23 | + * |
|
24 | + * @var mixed |
|
25 | + * @access public |
|
26 | + */ |
|
27 | + public $result = null; |
|
28 | + |
|
29 | + /** |
|
30 | + * Sets result type |
|
31 | + * |
|
32 | + * @var mixed |
|
33 | + * @access private |
|
34 | + */ |
|
35 | + private $result_type; |
|
36 | + |
|
37 | + /** |
|
38 | + * is exception set? |
|
39 | + * |
|
40 | + * (default value: false) |
|
41 | + * |
|
42 | + * @var bool |
|
43 | + * @access public |
|
44 | + */ |
|
45 | + public $exception = false; |
|
46 | + |
|
47 | + |
|
48 | + |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * __construct function |
|
53 | + * |
|
54 | + * @access public |
|
55 | + */ |
|
56 | + public function __construct() { |
|
57 | + # set error codes |
|
58 | + $this->set_error_codes (); |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Sets error code object |
|
63 | + * |
|
64 | + * http://www.restapitutorial.com/httpstatuscodes.html |
|
65 | + * |
|
66 | + * @access private |
|
67 | + * @return void |
|
68 | + */ |
|
69 | + private function set_error_codes () { |
|
70 | + // OK |
|
71 | + $this->errors[200] = "OK"; |
|
72 | + $this->errors[201] = "Created"; |
|
73 | + $this->errors[202] = "Accepted"; |
|
74 | + $this->errors[204] = "No Content"; |
|
75 | + // Client errors |
|
76 | + $this->errors[400] = "Bad Request"; |
|
77 | + $this->errors[401] = "Unauthorized"; |
|
78 | + $this->errors[403] = "Forbidden"; |
|
79 | + $this->errors[404] = "Not Found"; |
|
80 | + $this->errors[405] = "Method Not Allowed"; |
|
81 | + $this->errors[415] = "Unsupported Media Type"; |
|
82 | + // Server errors |
|
83 | + $this->errors[500] = "Internal Server Error"; |
|
84 | + $this->errors[501] = "Not Implemented"; |
|
85 | + $this->errors[503] = "Service Unavailable"; |
|
86 | + $this->errors[505] = "HTTP Version Not Supported"; |
|
87 | + $this->errors[511] = "Network Authentication Required"; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Sets new header and throws exception |
|
92 | + * |
|
93 | + * @access public |
|
94 | + * @param int $code (default: 400) |
|
95 | + * @param mixed $exception |
|
96 | + * @return void |
|
97 | + */ |
|
98 | + public function throw_exception ($code = 400, $exception) { |
|
99 | + // set failed |
|
100 | + $this->exception = true; |
|
101 | + |
|
102 | + // set success |
|
103 | + $this->result['success'] = false; |
|
104 | + // set exit code |
|
105 | + $this->result['code'] = $code; |
|
106 | + // set message |
|
107 | + $this->result['message'] = $exception; |
|
108 | + |
|
109 | + // set header |
|
110 | + $this->set_header (); |
|
111 | + // throw exception |
|
112 | + throw new Exception($exception); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Sets header based on provided HTTP code |
|
117 | + * |
|
118 | + * @access private |
|
119 | + * @param mixed $code |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + private function set_header () { |
|
123 | + // wrong code |
|
124 | + if(!isset($this->exception)) { header("HTTP/1.1 500 Invalid result code"); } |
|
125 | + else { header("HTTP/1.1 ".$this->result['code']." ".$this->errors[$this->result['code']]); } |
|
126 | + |
|
127 | + // 401 - add location |
|
128 | + if ($this->result['code']==401) { |
|
129 | + $this->set_location_header ("/api/".$_REQUEST['app_id']."/user/"); |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Formulates result to JSON or XML |
|
135 | + * |
|
136 | + * @access public |
|
137 | + * @param mixed $result |
|
138 | + * @return void |
|
139 | + */ |
|
140 | + public function formulate_result ($result) { |
|
141 | + // make sure result is array |
|
142 | + $this->result = is_null($this->result) ? (array) $result : $this->result; |
|
143 | + |
|
144 | + // get requested content type |
|
145 | + $this->get_request_content_type (); |
|
146 | + |
|
147 | + // set result contrnt type |
|
148 | + $this->set_content_type_header (); |
|
149 | + // set cache header |
|
150 | + $this->set_cache_header (); |
|
151 | + // set result header if not already set with $result['success']=false |
|
152 | + $this->exception===true ? : $this->set_success_header (); |
|
153 | + |
|
154 | + // return result |
|
155 | + return $this->create_result (); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Validates that proper content type is set in request |
|
160 | + * |
|
161 | + * @access public |
|
162 | + * @return void |
|
163 | + */ |
|
164 | + public function validate_content_type () { |
|
165 | + // not set, presume json |
|
166 | + if( !isset($_SERVER['CONTENT_TYPE']) ) {} |
|
167 | + // post |
|
168 | + elseif($_SERVER['CONTENT_TYPE']=="application/x-www-form-urlencoded") {} |
|
169 | + // set, verify |
|
170 | + elseif (!($_SERVER['CONTENT_TYPE']=="application/xml" || $_SERVER['CONTENT_TYPE']=="application/json")) { |
|
171 | + $this->throw_exception (415, "Invalid Content type ".$_SERVER['CONTENT_TYPE']); |
|
172 | + } |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Sets request content type |
|
177 | + * |
|
178 | + * @access public |
|
179 | + * @return void |
|
180 | + */ |
|
181 | + private function get_request_content_type () { |
|
182 | + $this->result_type = $_SERVER['CONTENT_TYPE']=="application/xml" ? "xml" : "json"; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Sets result content type |
|
187 | + * |
|
188 | + * @access private |
|
189 | + * @return void |
|
190 | + */ |
|
191 | + private function set_content_type_header () { |
|
192 | + // content_type |
|
193 | + $this->result_type == "xml" ? header('Content-Type: application/xml') : header('Content-Type: application/json'); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Sets Cache header. |
|
198 | + * |
|
199 | + * @access private |
|
200 | + * @return void |
|
201 | + */ |
|
202 | + private function set_cache_header ($seconds = NULL) { |
|
203 | + // none |
|
204 | + if($seconds===NULL) { |
|
205 | + header("Cache-Control: no-cache"); |
|
206 | + header("Pragma: no-cache"); |
|
207 | + } |
|
208 | + // cache |
|
209 | + else { |
|
210 | + header("Cache-Control: $seconds"); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Sets success header |
|
216 | + * |
|
217 | + * @access private |
|
218 | + * @return void |
|
219 | + */ |
|
220 | + private function set_success_header () { |
|
221 | + // check fo location |
|
222 | + if(isset($this->result['location'])) { |
|
223 | + $this->set_location_header ($this->result['location']); |
|
224 | + } |
|
225 | + |
|
226 | + // set success |
|
227 | + $this->result['success'] = true; |
|
228 | + |
|
229 | + // set header |
|
230 | + $this->set_header (); |
|
231 | + |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * Sets location header for newly created objects |
|
236 | + * |
|
237 | + * @access private |
|
238 | + * @param mixed $location |
|
239 | + * @return void |
|
240 | + */ |
|
241 | + private function set_location_header ($location) { |
|
242 | + header("Location: ".$location); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Outputs result |
|
247 | + * |
|
248 | + * @access private |
|
249 | + * @return void |
|
250 | + */ |
|
251 | + private function create_result () { |
|
252 | + // reorder |
|
253 | + $this->reorder_result (); |
|
254 | + // creates result |
|
255 | + return $this->result_type == "xml" ? $this->create_xml () : $this->create_json (); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Reorders result to proper format |
|
260 | + * |
|
261 | + * @access private |
|
262 | + * @return void |
|
263 | + */ |
|
264 | + private function reorder_result () { |
|
265 | + $tmp = $this->result; |
|
266 | + unset($this->result); |
|
267 | + // reset |
|
268 | + $this->result['code'] = $tmp['code']; |
|
269 | + $this->result['success'] = $tmp['success']; |
|
270 | + if(isset($tmp['message'])) { $this->result['message'] = $tmp['message']; } |
|
271 | + if(isset($tmp['data'])) { $this->result['data'] = $tmp['data']; } |
|
272 | + if(isset($tmp['ip'])) { $this->result['ip'] = $tmp['ip']; } |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Creates XML result |
|
277 | + * |
|
278 | + * @access private |
|
279 | + * @return void |
|
280 | + */ |
|
281 | + private function create_xml () { |
|
282 | + // convert whole object to array |
|
283 | + $this->result = $this->object_to_array($this->result); |
|
284 | + |
|
285 | + // new SimpleXMLElement object |
|
286 | + $xml = new SimpleXMLElement('<'.$_GET['controller'].'/>'); |
|
287 | + // generate xml from result |
|
288 | + $this->array_to_xml($xml, $this->result); |
|
289 | + |
|
290 | + // return XML result |
|
291 | + return $xml->asXML(); |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Transforms array to XML |
|
296 | + * |
|
297 | + * @access private |
|
298 | + * @param SimpleXMLElement $object |
|
299 | + * @param array $data |
|
300 | + * @return void |
|
301 | + */ |
|
302 | + private function array_to_xml(SimpleXMLElement $object, array $data) { |
|
303 | + // loop through values |
|
304 | + foreach ($data as $key => $value) { |
|
305 | + // if spaces exist in key replace them with underscores |
|
306 | + if(strpos($key, " ")>0) { $key = str_replace(" ", "_", $key); } |
|
307 | + |
|
308 | + // if key is numeric append item |
|
309 | + if(is_numeric($key)) $key = "item".$key; |
|
310 | + |
|
311 | + // if array add child |
|
312 | + if (is_array($value)) { |
|
313 | + $new_object = $object->addChild($key); |
|
314 | + $this->array_to_xml($new_object, $value); |
|
315 | + } |
|
316 | + // else write value |
|
317 | + else { |
|
318 | + $object->addChild($key, $value); |
|
319 | + } |
|
320 | + } |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * function xml2array |
|
325 | + * |
|
326 | + * This function is part of the PHP manual. |
|
327 | + * |
|
328 | + * The PHP manual text and comments are covered by the Creative Commons |
|
329 | + * Attribution 3.0 License, copyright (c) the PHP Documentation Group |
|
330 | + * |
|
331 | + * @author k dot antczak at livedata dot pl |
|
332 | + * @date 2011-04-22 06:08 UTC |
|
333 | + * @link http://www.php.net/manual/en/ref.simplexml.php#103617 |
|
334 | + * @license http://www.php.net/license/index.php#doc-lic |
|
335 | + * @license http://creativecommons.org/licenses/by/3.0/ |
|
336 | + * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0> |
|
337 | + */ |
|
338 | + public function xml_to_array ( $xmlObject, $out = array () ) { |
|
339 | + foreach ( (array) $xmlObject as $index => $node ) |
|
340 | + $out[$index] = ( is_object ( $node ) ) ? $this->xml_to_array ( $node ) : $node; |
|
341 | + |
|
342 | + return $out; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Transforms object to array |
|
347 | + * |
|
348 | + * @access private |
|
349 | + * @param mixed $obj |
|
350 | + * @return void |
|
351 | + */ |
|
352 | + private function object_to_array ($obj) { |
|
353 | + // object to array |
|
354 | + if(is_object($obj)) $obj = (array) $obj; |
|
355 | + if(is_array($obj)) { |
|
356 | + $new = array(); |
|
357 | + foreach($obj as $key => $val) { |
|
358 | + $new[$key] = $this->object_to_array($val); |
|
359 | + } |
|
360 | + } |
|
361 | + else $new = $obj; |
|
362 | + return $new; |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * Creates JSON result |
|
367 | + * |
|
368 | + * @access private |
|
369 | + * @return void |
|
370 | + */ |
|
371 | + private function create_json () { |
|
372 | + return json_encode((array) $this->result); |
|
373 | + } |
|
374 | 374 | |
375 | 375 | |
376 | 376 | } |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | * |
54 | 54 | * @access public |
55 | 55 | */ |
56 | - public function __construct() { |
|
56 | + public function __construct () { |
|
57 | 57 | # set error codes |
58 | 58 | $this->set_error_codes (); |
59 | 59 | } |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | // set header |
110 | 110 | $this->set_header (); |
111 | 111 | // throw exception |
112 | - throw new Exception($exception); |
|
112 | + throw new Exception ($exception); |
|
113 | 113 | } |
114 | 114 | |
115 | 115 | /** |
@@ -121,11 +121,11 @@ discard block |
||
121 | 121 | */ |
122 | 122 | private function set_header () { |
123 | 123 | // wrong code |
124 | - if(!isset($this->exception)) { header("HTTP/1.1 500 Invalid result code"); } |
|
125 | - else { header("HTTP/1.1 ".$this->result['code']." ".$this->errors[$this->result['code']]); } |
|
124 | + if (!isset($this->exception)) { header ("HTTP/1.1 500 Invalid result code"); } |
|
125 | + else { header ("HTTP/1.1 ".$this->result['code']." ".$this->errors[$this->result['code']]); } |
|
126 | 126 | |
127 | 127 | // 401 - add location |
128 | - if ($this->result['code']==401) { |
|
128 | + if ($this->result['code'] == 401) { |
|
129 | 129 | $this->set_location_header ("/api/".$_REQUEST['app_id']."/user/"); |
130 | 130 | } |
131 | 131 | } |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | */ |
140 | 140 | public function formulate_result ($result) { |
141 | 141 | // make sure result is array |
142 | - $this->result = is_null($this->result) ? (array) $result : $this->result; |
|
142 | + $this->result = is_null ($this->result) ? (array) $result : $this->result; |
|
143 | 143 | |
144 | 144 | // get requested content type |
145 | 145 | $this->get_request_content_type (); |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | // set cache header |
150 | 150 | $this->set_cache_header (); |
151 | 151 | // set result header if not already set with $result['success']=false |
152 | - $this->exception===true ? : $this->set_success_header (); |
|
152 | + $this->exception === true ? : $this->set_success_header (); |
|
153 | 153 | |
154 | 154 | // return result |
155 | 155 | return $this->create_result (); |
@@ -163,11 +163,11 @@ discard block |
||
163 | 163 | */ |
164 | 164 | public function validate_content_type () { |
165 | 165 | // not set, presume json |
166 | - if( !isset($_SERVER['CONTENT_TYPE']) ) {} |
|
166 | + if (!isset($_SERVER['CONTENT_TYPE'])) {} |
|
167 | 167 | // post |
168 | - elseif($_SERVER['CONTENT_TYPE']=="application/x-www-form-urlencoded") {} |
|
168 | + elseif ($_SERVER['CONTENT_TYPE'] == "application/x-www-form-urlencoded") {} |
|
169 | 169 | // set, verify |
170 | - elseif (!($_SERVER['CONTENT_TYPE']=="application/xml" || $_SERVER['CONTENT_TYPE']=="application/json")) { |
|
170 | + elseif (!($_SERVER['CONTENT_TYPE'] == "application/xml" || $_SERVER['CONTENT_TYPE'] == "application/json")) { |
|
171 | 171 | $this->throw_exception (415, "Invalid Content type ".$_SERVER['CONTENT_TYPE']); |
172 | 172 | } |
173 | 173 | } |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | * @return void |
180 | 180 | */ |
181 | 181 | private function get_request_content_type () { |
182 | - $this->result_type = $_SERVER['CONTENT_TYPE']=="application/xml" ? "xml" : "json"; |
|
182 | + $this->result_type = $_SERVER['CONTENT_TYPE'] == "application/xml" ? "xml" : "json"; |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | /** |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | */ |
191 | 191 | private function set_content_type_header () { |
192 | 192 | // content_type |
193 | - $this->result_type == "xml" ? header('Content-Type: application/xml') : header('Content-Type: application/json'); |
|
193 | + $this->result_type == "xml" ? header ('Content-Type: application/xml') : header ('Content-Type: application/json'); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | /** |
@@ -201,13 +201,13 @@ discard block |
||
201 | 201 | */ |
202 | 202 | private function set_cache_header ($seconds = NULL) { |
203 | 203 | // none |
204 | - if($seconds===NULL) { |
|
205 | - header("Cache-Control: no-cache"); |
|
206 | - header("Pragma: no-cache"); |
|
204 | + if ($seconds === NULL) { |
|
205 | + header ("Cache-Control: no-cache"); |
|
206 | + header ("Pragma: no-cache"); |
|
207 | 207 | } |
208 | 208 | // cache |
209 | 209 | else { |
210 | - header("Cache-Control: $seconds"); |
|
210 | + header ("Cache-Control: $seconds"); |
|
211 | 211 | } |
212 | 212 | } |
213 | 213 | |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | */ |
220 | 220 | private function set_success_header () { |
221 | 221 | // check fo location |
222 | - if(isset($this->result['location'])) { |
|
222 | + if (isset($this->result['location'])) { |
|
223 | 223 | $this->set_location_header ($this->result['location']); |
224 | 224 | } |
225 | 225 | |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | * @return void |
240 | 240 | */ |
241 | 241 | private function set_location_header ($location) { |
242 | - header("Location: ".$location); |
|
242 | + header ("Location: ".$location); |
|
243 | 243 | } |
244 | 244 | |
245 | 245 | /** |
@@ -267,9 +267,9 @@ discard block |
||
267 | 267 | // reset |
268 | 268 | $this->result['code'] = $tmp['code']; |
269 | 269 | $this->result['success'] = $tmp['success']; |
270 | - if(isset($tmp['message'])) { $this->result['message'] = $tmp['message']; } |
|
271 | - if(isset($tmp['data'])) { $this->result['data'] = $tmp['data']; } |
|
272 | - if(isset($tmp['ip'])) { $this->result['ip'] = $tmp['ip']; } |
|
270 | + if (isset($tmp['message'])) { $this->result['message'] = $tmp['message']; } |
|
271 | + if (isset($tmp['data'])) { $this->result['data'] = $tmp['data']; } |
|
272 | + if (isset($tmp['ip'])) { $this->result['ip'] = $tmp['ip']; } |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | /** |
@@ -280,15 +280,15 @@ discard block |
||
280 | 280 | */ |
281 | 281 | private function create_xml () { |
282 | 282 | // convert whole object to array |
283 | - $this->result = $this->object_to_array($this->result); |
|
283 | + $this->result = $this->object_to_array ($this->result); |
|
284 | 284 | |
285 | 285 | // new SimpleXMLElement object |
286 | - $xml = new SimpleXMLElement('<'.$_GET['controller'].'/>'); |
|
286 | + $xml = new SimpleXMLElement ('<'.$_GET['controller'].'/>'); |
|
287 | 287 | // generate xml from result |
288 | - $this->array_to_xml($xml, $this->result); |
|
288 | + $this->array_to_xml ($xml, $this->result); |
|
289 | 289 | |
290 | 290 | // return XML result |
291 | - return $xml->asXML(); |
|
291 | + return $xml->asXML (); |
|
292 | 292 | } |
293 | 293 | |
294 | 294 | /** |
@@ -299,23 +299,23 @@ discard block |
||
299 | 299 | * @param array $data |
300 | 300 | * @return void |
301 | 301 | */ |
302 | - private function array_to_xml(SimpleXMLElement $object, array $data) { |
|
302 | + private function array_to_xml (SimpleXMLElement $object, array $data) { |
|
303 | 303 | // loop through values |
304 | 304 | foreach ($data as $key => $value) { |
305 | 305 | // if spaces exist in key replace them with underscores |
306 | - if(strpos($key, " ")>0) { $key = str_replace(" ", "_", $key); } |
|
306 | + if (strpos ($key, " ") > 0) { $key = str_replace (" ", "_", $key); } |
|
307 | 307 | |
308 | 308 | // if key is numeric append item |
309 | - if(is_numeric($key)) $key = "item".$key; |
|
309 | + if (is_numeric ($key)) $key = "item".$key; |
|
310 | 310 | |
311 | 311 | // if array add child |
312 | - if (is_array($value)) { |
|
313 | - $new_object = $object->addChild($key); |
|
314 | - $this->array_to_xml($new_object, $value); |
|
312 | + if (is_array ($value)) { |
|
313 | + $new_object = $object->addChild ($key); |
|
314 | + $this->array_to_xml ($new_object, $value); |
|
315 | 315 | } |
316 | 316 | // else write value |
317 | 317 | else { |
318 | - $object->addChild($key, $value); |
|
318 | + $object->addChild ($key, $value); |
|
319 | 319 | } |
320 | 320 | } |
321 | 321 | } |
@@ -335,9 +335,9 @@ discard block |
||
335 | 335 | * @license http://creativecommons.org/licenses/by/3.0/ |
336 | 336 | * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0> |
337 | 337 | */ |
338 | - public function xml_to_array ( $xmlObject, $out = array () ) { |
|
339 | - foreach ( (array) $xmlObject as $index => $node ) |
|
340 | - $out[$index] = ( is_object ( $node ) ) ? $this->xml_to_array ( $node ) : $node; |
|
338 | + public function xml_to_array ($xmlObject, $out = array ()) { |
|
339 | + foreach ((array) $xmlObject as $index => $node) |
|
340 | + $out[$index] = (is_object ($node)) ? $this->xml_to_array ($node) : $node; |
|
341 | 341 | |
342 | 342 | return $out; |
343 | 343 | } |
@@ -351,11 +351,11 @@ discard block |
||
351 | 351 | */ |
352 | 352 | private function object_to_array ($obj) { |
353 | 353 | // object to array |
354 | - if(is_object($obj)) $obj = (array) $obj; |
|
355 | - if(is_array($obj)) { |
|
356 | - $new = array(); |
|
357 | - foreach($obj as $key => $val) { |
|
358 | - $new[$key] = $this->object_to_array($val); |
|
354 | + if (is_object ($obj)) $obj = (array) $obj; |
|
355 | + if (is_array ($obj)) { |
|
356 | + $new = array (); |
|
357 | + foreach ($obj as $key => $val) { |
|
358 | + $new[$key] = $this->object_to_array ($val); |
|
359 | 359 | } |
360 | 360 | } |
361 | 361 | else $new = $obj; |
@@ -369,7 +369,7 @@ discard block |
||
369 | 369 | * @return void |
370 | 370 | */ |
371 | 371 | private function create_json () { |
372 | - return json_encode((array) $this->result); |
|
372 | + return json_encode ((array) $this->result); |
|
373 | 373 | } |
374 | 374 | |
375 | 375 |
@@ -330,7 +330,7 @@ discard block |
||
330 | 330 | * Validates users token from database |
331 | 331 | * |
332 | 332 | * @access private |
333 | - * @return void |
|
333 | + * @return boolean |
|
334 | 334 | */ |
335 | 335 | private function validate_user_token () { |
336 | 336 | // is set |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | * Checks if token has expired |
413 | 413 | * |
414 | 414 | * @access private |
415 | - * @return void |
|
415 | + * @return boolean |
|
416 | 416 | */ |
417 | 417 | private function validate_token_expiration () { |
418 | 418 | return strtotime($this->token_expires) < time() ? true : false; |
@@ -9,153 +9,153 @@ discard block |
||
9 | 9 | class User_controller extends Common_api_functions { |
10 | 10 | |
11 | 11 | |
12 | - /** |
|
13 | - * users token |
|
14 | - * |
|
15 | - * @var mixed |
|
16 | - * @access public |
|
17 | - */ |
|
18 | - public $token; |
|
19 | - |
|
20 | - /** |
|
21 | - * time when token expires |
|
22 | - * |
|
23 | - * @var mixed |
|
24 | - * @access public |
|
25 | - */ |
|
26 | - public $token_expires; |
|
27 | - |
|
28 | - /** |
|
29 | - * for how many seconds token is valid |
|
30 | - * |
|
31 | - * @var mixed |
|
32 | - * @access private |
|
33 | - */ |
|
34 | - private $token_valid_time; |
|
35 | - |
|
36 | - /** |
|
37 | - * number of chars for token |
|
38 | - * |
|
39 | - * @var mixed |
|
40 | - * @access private |
|
41 | - */ |
|
42 | - private $token_length; |
|
43 | - |
|
44 | - /** |
|
45 | - * max number of failures before IP is blocked |
|
46 | - * |
|
47 | - * @var mixed |
|
48 | - * @access private |
|
49 | - */ |
|
50 | - private $max_failures; |
|
51 | - |
|
52 | - /** |
|
53 | - * controls if IP should be blocked for 5 minutes on invalid requests |
|
54 | - * |
|
55 | - * (default value: true) |
|
56 | - * |
|
57 | - * @var bool |
|
58 | - * @access private |
|
59 | - */ |
|
60 | - private $block_ip = true; |
|
61 | - |
|
62 | - /** |
|
63 | - * Database object |
|
64 | - * |
|
65 | - * @var mixed |
|
66 | - * @access protected |
|
67 | - */ |
|
68 | - protected $Database; |
|
69 | - |
|
70 | - /** |
|
71 | - * Master Tools object |
|
72 | - * |
|
73 | - * @var mixed |
|
74 | - * @access protected |
|
75 | - */ |
|
76 | - protected $Tools; |
|
77 | - |
|
78 | - /** |
|
79 | - * Master Admin object |
|
80 | - * |
|
81 | - * @var mixed |
|
82 | - * @access protected |
|
83 | - */ |
|
84 | - protected $Admin; |
|
85 | - |
|
86 | - /** |
|
87 | - * Master User object |
|
88 | - * |
|
89 | - * @var mixed |
|
90 | - * @access protected |
|
91 | - */ |
|
92 | - protected $User; |
|
93 | - |
|
94 | - /** |
|
95 | - * requested parameters |
|
96 | - * |
|
97 | - * @var mixed |
|
98 | - * @access public |
|
99 | - */ |
|
100 | - public $_params; |
|
101 | - |
|
102 | - |
|
103 | - |
|
104 | - /** |
|
105 | - * __construct function |
|
106 | - * |
|
107 | - * @access public |
|
108 | - * @param mixed $Database |
|
109 | - * @param mixed $Tools |
|
110 | - * @param mixed $params |
|
111 | - * @param mixed $Response |
|
112 | - */ |
|
113 | - public function __construct ($Database, $Tools=null, $params=null, $Response) { |
|
114 | - $this->Database = $Database; |
|
115 | - $this->Response = $Response; |
|
116 | - $this->_params = $params; |
|
117 | - // init required objects |
|
118 | - $this->init_object ("Admin", $Database); |
|
119 | - $this->init_object ("User", $Database); |
|
120 | - // set default for tokens |
|
121 | - $this->set_token_valid_time (); |
|
122 | - $this->set_max_failures (); |
|
123 | - $this->set_token_length (); |
|
124 | - |
|
125 | - // if HTTP_TOKEN is set change it to HTTP_PHPIPAM_TOKEN |
|
126 | - if (isset($_SERVER['HTTP_TOKEN'])&&!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $_SERVER['HTTP_PHPIPAM_TOKEN'] = $_SERVER['HTTP_TOKEN']; } |
|
127 | - } |
|
128 | - |
|
129 | - |
|
130 | - |
|
131 | - |
|
132 | - |
|
133 | - |
|
134 | - |
|
135 | - |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * returns general Controllers and supported methods |
|
140 | - * |
|
141 | - * @access public |
|
142 | - * @return void |
|
143 | - */ |
|
144 | - public function OPTIONS () { |
|
145 | - // validate |
|
146 | - $this->validate_options_request (); |
|
147 | - |
|
148 | - // methods |
|
149 | - $result = array(); |
|
150 | - $result['methods'] = array( |
|
151 | - array("href"=>"/api/".$this->_params->app_id."/user/", "methods"=>array(array("rel"=>"read", "method"=>"GET"), |
|
152 | - array("rel"=>"create", "method"=>"POST"), |
|
153 | - array("rel"=>"update", "method"=>"PATCH"), |
|
154 | - array("rel"=>"delete", "method"=>"DELETE"))), |
|
155 | - ); |
|
156 | - # result |
|
157 | - return array("code"=>200, "data"=>$result); |
|
158 | - } |
|
12 | + /** |
|
13 | + * users token |
|
14 | + * |
|
15 | + * @var mixed |
|
16 | + * @access public |
|
17 | + */ |
|
18 | + public $token; |
|
19 | + |
|
20 | + /** |
|
21 | + * time when token expires |
|
22 | + * |
|
23 | + * @var mixed |
|
24 | + * @access public |
|
25 | + */ |
|
26 | + public $token_expires; |
|
27 | + |
|
28 | + /** |
|
29 | + * for how many seconds token is valid |
|
30 | + * |
|
31 | + * @var mixed |
|
32 | + * @access private |
|
33 | + */ |
|
34 | + private $token_valid_time; |
|
35 | + |
|
36 | + /** |
|
37 | + * number of chars for token |
|
38 | + * |
|
39 | + * @var mixed |
|
40 | + * @access private |
|
41 | + */ |
|
42 | + private $token_length; |
|
43 | + |
|
44 | + /** |
|
45 | + * max number of failures before IP is blocked |
|
46 | + * |
|
47 | + * @var mixed |
|
48 | + * @access private |
|
49 | + */ |
|
50 | + private $max_failures; |
|
51 | + |
|
52 | + /** |
|
53 | + * controls if IP should be blocked for 5 minutes on invalid requests |
|
54 | + * |
|
55 | + * (default value: true) |
|
56 | + * |
|
57 | + * @var bool |
|
58 | + * @access private |
|
59 | + */ |
|
60 | + private $block_ip = true; |
|
61 | + |
|
62 | + /** |
|
63 | + * Database object |
|
64 | + * |
|
65 | + * @var mixed |
|
66 | + * @access protected |
|
67 | + */ |
|
68 | + protected $Database; |
|
69 | + |
|
70 | + /** |
|
71 | + * Master Tools object |
|
72 | + * |
|
73 | + * @var mixed |
|
74 | + * @access protected |
|
75 | + */ |
|
76 | + protected $Tools; |
|
77 | + |
|
78 | + /** |
|
79 | + * Master Admin object |
|
80 | + * |
|
81 | + * @var mixed |
|
82 | + * @access protected |
|
83 | + */ |
|
84 | + protected $Admin; |
|
85 | + |
|
86 | + /** |
|
87 | + * Master User object |
|
88 | + * |
|
89 | + * @var mixed |
|
90 | + * @access protected |
|
91 | + */ |
|
92 | + protected $User; |
|
93 | + |
|
94 | + /** |
|
95 | + * requested parameters |
|
96 | + * |
|
97 | + * @var mixed |
|
98 | + * @access public |
|
99 | + */ |
|
100 | + public $_params; |
|
101 | + |
|
102 | + |
|
103 | + |
|
104 | + /** |
|
105 | + * __construct function |
|
106 | + * |
|
107 | + * @access public |
|
108 | + * @param mixed $Database |
|
109 | + * @param mixed $Tools |
|
110 | + * @param mixed $params |
|
111 | + * @param mixed $Response |
|
112 | + */ |
|
113 | + public function __construct ($Database, $Tools=null, $params=null, $Response) { |
|
114 | + $this->Database = $Database; |
|
115 | + $this->Response = $Response; |
|
116 | + $this->_params = $params; |
|
117 | + // init required objects |
|
118 | + $this->init_object ("Admin", $Database); |
|
119 | + $this->init_object ("User", $Database); |
|
120 | + // set default for tokens |
|
121 | + $this->set_token_valid_time (); |
|
122 | + $this->set_max_failures (); |
|
123 | + $this->set_token_length (); |
|
124 | + |
|
125 | + // if HTTP_TOKEN is set change it to HTTP_PHPIPAM_TOKEN |
|
126 | + if (isset($_SERVER['HTTP_TOKEN'])&&!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $_SERVER['HTTP_PHPIPAM_TOKEN'] = $_SERVER['HTTP_TOKEN']; } |
|
127 | + } |
|
128 | + |
|
129 | + |
|
130 | + |
|
131 | + |
|
132 | + |
|
133 | + |
|
134 | + |
|
135 | + |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * returns general Controllers and supported methods |
|
140 | + * |
|
141 | + * @access public |
|
142 | + * @return void |
|
143 | + */ |
|
144 | + public function OPTIONS () { |
|
145 | + // validate |
|
146 | + $this->validate_options_request (); |
|
147 | + |
|
148 | + // methods |
|
149 | + $result = array(); |
|
150 | + $result['methods'] = array( |
|
151 | + array("href"=>"/api/".$this->_params->app_id."/user/", "methods"=>array(array("rel"=>"read", "method"=>"GET"), |
|
152 | + array("rel"=>"create", "method"=>"POST"), |
|
153 | + array("rel"=>"update", "method"=>"PATCH"), |
|
154 | + array("rel"=>"delete", "method"=>"DELETE"))), |
|
155 | + ); |
|
156 | + # result |
|
157 | + return array("code"=>200, "data"=>$result); |
|
158 | + } |
|
159 | 159 | |
160 | 160 | |
161 | 161 | |
@@ -164,99 +164,99 @@ discard block |
||
164 | 164 | |
165 | 165 | |
166 | 166 | |
167 | - /** |
|
168 | - * Authenticates user and returns token |
|
169 | - * |
|
170 | - * @access public |
|
171 | - * @return void |
|
172 | - */ |
|
173 | - public function GET () { |
|
174 | - // block IP |
|
175 | - $this->validate_block (); |
|
176 | - // validate token |
|
177 | - $this->validate_requested_token (); |
|
178 | - // ok |
|
179 | - return array("code"=>200, "data"=>array("expires"=>$this->token_expires)); |
|
180 | - } |
|
167 | + /** |
|
168 | + * Authenticates user and returns token |
|
169 | + * |
|
170 | + * @access public |
|
171 | + * @return void |
|
172 | + */ |
|
173 | + public function GET () { |
|
174 | + // block IP |
|
175 | + $this->validate_block (); |
|
176 | + // validate token |
|
177 | + $this->validate_requested_token (); |
|
178 | + // ok |
|
179 | + return array("code"=>200, "data"=>array("expires"=>$this->token_expires)); |
|
180 | + } |
|
181 | 181 | |
182 | 182 | |
183 | 183 | |
184 | 184 | |
185 | 185 | |
186 | - /** |
|
187 | - * Refreshes token and returns status |
|
188 | - * |
|
189 | - * @access public |
|
190 | - * @return void |
|
191 | - */ |
|
192 | - public function POST () { |
|
193 | - // block IP |
|
194 | - $this->validate_block (); |
|
195 | - // authenticate user and provide token |
|
196 | - return $this->authenticate (); |
|
197 | - } |
|
186 | + /** |
|
187 | + * Refreshes token and returns status |
|
188 | + * |
|
189 | + * @access public |
|
190 | + * @return void |
|
191 | + */ |
|
192 | + public function POST () { |
|
193 | + // block IP |
|
194 | + $this->validate_block (); |
|
195 | + // authenticate user and provide token |
|
196 | + return $this->authenticate (); |
|
197 | + } |
|
198 | 198 | |
199 | 199 | |
200 | 200 | |
201 | 201 | |
202 | 202 | |
203 | - /** |
|
204 | - * Extends token validity |
|
205 | - * |
|
206 | - * @access public |
|
207 | - * @return void |
|
208 | - */ |
|
209 | - public function PATCH () { |
|
210 | - // block IP |
|
211 | - $this->validate_block (); |
|
212 | - // validate token |
|
213 | - $this->validate_requested_token (); |
|
214 | - // refresh |
|
215 | - $this->refresh_token_expiration (); |
|
216 | - // ok |
|
217 | - return array("code"=>200, "data"=>array("expires"=>$this->token_expires)); |
|
218 | - } |
|
203 | + /** |
|
204 | + * Extends token validity |
|
205 | + * |
|
206 | + * @access public |
|
207 | + * @return void |
|
208 | + */ |
|
209 | + public function PATCH () { |
|
210 | + // block IP |
|
211 | + $this->validate_block (); |
|
212 | + // validate token |
|
213 | + $this->validate_requested_token (); |
|
214 | + // refresh |
|
215 | + $this->refresh_token_expiration (); |
|
216 | + // ok |
|
217 | + return array("code"=>200, "data"=>array("expires"=>$this->token_expires)); |
|
218 | + } |
|
219 | 219 | |
220 | 220 | |
221 | 221 | |
222 | 222 | |
223 | 223 | |
224 | 224 | |
225 | - /** |
|
226 | - * Deletes token |
|
227 | - * |
|
228 | - * @access public |
|
229 | - * @return void |
|
230 | - */ |
|
231 | - public function DELETE () { |
|
232 | - // block IP |
|
233 | - $this->validate_block (); |
|
234 | - // validate token |
|
235 | - $this->validate_requested_token (); |
|
236 | - // remove token |
|
237 | - $this->remove_token (); |
|
238 | - // result |
|
239 | - return array("code"=>200, "data"=>array("Token removed")); |
|
240 | - } |
|
225 | + /** |
|
226 | + * Deletes token |
|
227 | + * |
|
228 | + * @access public |
|
229 | + * @return void |
|
230 | + */ |
|
231 | + public function DELETE () { |
|
232 | + // block IP |
|
233 | + $this->validate_block (); |
|
234 | + // validate token |
|
235 | + $this->validate_requested_token (); |
|
236 | + // remove token |
|
237 | + $this->remove_token (); |
|
238 | + // result |
|
239 | + return array("code"=>200, "data"=>array("Token removed")); |
|
240 | + } |
|
241 | 241 | |
242 | 242 | |
243 | 243 | |
244 | 244 | |
245 | 245 | |
246 | - /** |
|
247 | - * Checks authentication token and refresh expiration |
|
248 | - * |
|
249 | - * @access public |
|
250 | - * @return void |
|
251 | - */ |
|
252 | - public function check_auth () { |
|
253 | - // block IP |
|
254 | - $this->validate_block (); |
|
255 | - // validate token |
|
256 | - $this->validate_requested_token (); |
|
257 | - // refresh |
|
258 | - $this->refresh_token_expiration (); |
|
259 | - } |
|
246 | + /** |
|
247 | + * Checks authentication token and refresh expiration |
|
248 | + * |
|
249 | + * @access public |
|
250 | + * @return void |
|
251 | + */ |
|
252 | + public function check_auth () { |
|
253 | + // block IP |
|
254 | + $this->validate_block (); |
|
255 | + // validate token |
|
256 | + $this->validate_requested_token (); |
|
257 | + // refresh |
|
258 | + $this->refresh_token_expiration (); |
|
259 | + } |
|
260 | 260 | |
261 | 261 | |
262 | 262 | |
@@ -265,313 +265,313 @@ discard block |
||
265 | 265 | |
266 | 266 | |
267 | 267 | |
268 | - /* @blocks -------------------- */ |
|
268 | + /* @blocks -------------------- */ |
|
269 | 269 | |
270 | 270 | |
271 | - /** |
|
272 | - * Checks if IP should be blocked form access |
|
273 | - * |
|
274 | - * @access private |
|
275 | - * @return void |
|
276 | - */ |
|
277 | - private function validate_block () { |
|
278 | - // check if block is permitted |
|
279 | - if ($this->block_ip === true) { |
|
280 | - // get count |
|
281 | - $cnt = $this->User->block_check_ip (); |
|
282 | - // failure |
|
283 | - if ($cnt > $this->max_failures) { $this->Response->throw_exception(500, "Your IP has been blocked for 5 minutes because of excesive login failures"); } |
|
284 | - } |
|
285 | - } |
|
271 | + /** |
|
272 | + * Checks if IP should be blocked form access |
|
273 | + * |
|
274 | + * @access private |
|
275 | + * @return void |
|
276 | + */ |
|
277 | + private function validate_block () { |
|
278 | + // check if block is permitted |
|
279 | + if ($this->block_ip === true) { |
|
280 | + // get count |
|
281 | + $cnt = $this->User->block_check_ip (); |
|
282 | + // failure |
|
283 | + if ($cnt > $this->max_failures) { $this->Response->throw_exception(500, "Your IP has been blocked for 5 minutes because of excesive login failures"); } |
|
284 | + } |
|
285 | + } |
|
286 | 286 | |
287 | 287 | |
288 | 288 | |
289 | 289 | |
290 | 290 | |
291 | 291 | |
292 | - /* @authentication -------------------- */ |
|
292 | + /* @authentication -------------------- */ |
|
293 | 293 | |
294 | - /** |
|
295 | - * Authenticates user and returns token and validity |
|
296 | - * |
|
297 | - * @access private |
|
298 | - * @return void |
|
299 | - */ |
|
300 | - private function authenticate () { |
|
301 | - # try to authenticate user, it it fails it will fail by itself |
|
302 | - $this->User->authenticate ($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); |
|
303 | - |
|
304 | - # if token is valid and set extend it, otherwise generate new |
|
305 | - if ($this->validate_user_token ()) { |
|
306 | - // extend |
|
307 | - $this->refresh_token_expiration (); |
|
308 | - } |
|
309 | - else { |
|
310 | - // generate new token |
|
311 | - $this->generate_token (); |
|
312 | - // save to user |
|
313 | - $this->save_user_token (); |
|
314 | - } |
|
315 | - |
|
316 | - # result |
|
317 | - return array("code"=>200, "data"=>array("token"=>$this->token, "expires"=>$this->token_expires)); |
|
318 | - } |
|
319 | - |
|
320 | - |
|
321 | - |
|
322 | - |
|
323 | - |
|
324 | - |
|
325 | - |
|
326 | - |
|
327 | - |
|
328 | - |
|
329 | - /* @tokens -------------------- */ |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * Sets default validiy for token (default 12 hours) |
|
334 | - * |
|
335 | - * @access public |
|
336 | - * @param int $token_valid_time (default: null) |
|
337 | - * @return void |
|
338 | - */ |
|
339 | - public function set_token_valid_time ($token_valid_time = null) { |
|
340 | - // validate integer |
|
341 | - if ($this->token_length!=null) { |
|
342 | - if (!is_numeric($this->token_length)) { $this->Response->throw_exception(500, "token valid time must be an integer"); } |
|
343 | - } |
|
344 | - // save |
|
345 | - $this->token_valid_time = is_null($token_valid_time) ? 21600 : $token_valid_time; |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Sets max number of failures before IP is blocked. |
|
350 | - * |
|
351 | - * @access public |
|
352 | - * @param mixed $failures (default: null) |
|
353 | - * @return void |
|
354 | - */ |
|
355 | - public function set_max_failures ($failures=null) { |
|
356 | - // validate integer |
|
357 | - if ($this->token_length!=null) { |
|
358 | - if (!is_numeric($this->token_length)) { $this->Response->throw_exception(500, "Max failures must be an integer"); } |
|
359 | - } |
|
360 | - // save |
|
361 | - $this->max_failures = $failures==null ? 10 : $failures; |
|
362 | - } |
|
363 | - |
|
364 | - /** |
|
365 | - * Block IP address. |
|
366 | - * |
|
367 | - * @access public |
|
368 | - * @param bool $block (default: true) |
|
369 | - * @return void |
|
370 | - */ |
|
371 | - public function block_ip ($block = true) { |
|
372 | - // validate integer |
|
373 | - if (!is_bool($block)) { |
|
374 | - if (!is_numeric($this->token_length)) { $this->Response->throw_exception(500, "Max failures must be an integer"); } |
|
375 | - } |
|
376 | - // save |
|
377 | - $this->block_ip = $$block; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * Sets length of token |
|
382 | - * |
|
383 | - * @access public |
|
384 | - * @param mixed $length (default: null) |
|
385 | - * @return void |
|
386 | - */ |
|
387 | - public function set_token_length ($length = null) { |
|
388 | - // validate number |
|
389 | - if ($length!=null) { |
|
390 | - if (!is_numeric($length)) { $this->Response->throw_exception(500, "token length must be an integer"); } |
|
391 | - elseif ($length>24) { $this->Response->throw_exception(500, "Maximum token length is 24 characters"); } |
|
392 | - } |
|
393 | - // save |
|
394 | - $this->token_length = is_null($length) ? 24 : $length; |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * Saves new token to database |
|
399 | - * |
|
400 | - * @access private |
|
401 | - * @return void |
|
402 | - */ |
|
403 | - private function save_user_token () { |
|
404 | - # set token values |
|
405 | - $values = array( |
|
406 | - "id"=>$this->User->user->id, |
|
407 | - "token"=>$this->token, |
|
408 | - "token_valid_until"=>$this->token_expires |
|
409 | - ); |
|
410 | - # save token to database |
|
411 | - if(!$this->Admin->object_modify ("users", "edit", "id", $values )) |
|
412 | - { $this->Response->throw_exception(500, "Failed to update token"); } |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Validates users token from database |
|
417 | - * |
|
418 | - * @access private |
|
419 | - * @return void |
|
420 | - */ |
|
421 | - private function validate_user_token () { |
|
422 | - // is set |
|
423 | - if (strlen($this->User->user->token)==0) { return false; } |
|
424 | - // date not set |
|
425 | - elseif (strlen($this->User->user->token_valid_until)==0) { return false; } |
|
426 | - // expired |
|
427 | - elseif ($this->User->user->token_valid_until < date("Y-m-d H:is:")) { return false; } |
|
428 | - // ok |
|
429 | - else { return true; } |
|
430 | - |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Validates requested token and saves it to $this->token |
|
435 | - * |
|
436 | - * @access private |
|
437 | - * @return void |
|
438 | - */ |
|
439 | - private function validate_requested_token () { |
|
440 | - return $this->_params->controller=="user" ? $this->validate_requested_token_user () : $this->validate_requested_token_general (); |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Validates token if User controller is requested - different response |
|
445 | - * |
|
446 | - * @access private |
|
447 | - * @return void |
|
448 | - */ |
|
449 | - private function validate_requested_token_user () { |
|
450 | - // check that token is present |
|
451 | - if(!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $this->Response->throw_exception(403, "Please provide token"); } |
|
452 | - // validate and remove token |
|
453 | - else { |
|
454 | - // fetch token |
|
455 | - if(($token = $this->Admin->fetch_object ("users", "token", $_SERVER['HTTP_PHPIPAM_TOKEN'])) === false) |
|
456 | - { $this->Response->throw_exception(403, "Invalid token"); } |
|
457 | - // save token |
|
458 | - $this->User->user = $token; |
|
459 | - $this->token = $token->token; |
|
460 | - $this->token_expires = $token->token_valid_until; |
|
461 | - |
|
462 | - // expired |
|
463 | - if($this->validate_token_expiration () === true) |
|
464 | - { $this->Response->throw_exception(403, "Token expired"); } |
|
465 | - } |
|
466 | - } |
|
467 | - |
|
468 | - /** |
|
469 | - * Validates token if general controller is requested - different response |
|
470 | - * |
|
471 | - * @access private |
|
472 | - * @return void |
|
473 | - */ |
|
474 | - private function validate_requested_token_general () { |
|
475 | - // check that token is present |
|
476 | - if(!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $this->Response->throw_exception(401, $this->Response->errors[401]); } |
|
477 | - // validate and remove token |
|
478 | - else { |
|
479 | - // fetch token |
|
480 | - if(($token = $this->Admin->fetch_object ("users", "token", $_SERVER['HTTP_PHPIPAM_TOKEN'])) === false) |
|
481 | - { $this->Response->throw_exception(401, $this->Response->errors[401]); } |
|
482 | - // save token |
|
483 | - $this->User->user = $token; |
|
484 | - $this->token = $token->token; |
|
485 | - $this->token_expires = $token->token_valid_until; |
|
486 | - |
|
487 | - // expired |
|
488 | - if($this->validate_token_expiration () === true) |
|
489 | - { $this->Response->throw_exception(401, $this->Response->errors[401]); } |
|
490 | - // refresh |
|
491 | - $this->refresh_token_expiration (); |
|
492 | - } |
|
493 | - } |
|
494 | - |
|
495 | - |
|
496 | - |
|
497 | - /** |
|
498 | - * Checks if token has expired |
|
499 | - * |
|
500 | - * @access private |
|
501 | - * @return void |
|
502 | - */ |
|
503 | - private function validate_token_expiration () { |
|
504 | - return strtotime($this->token_expires) < time() ? true : false; |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * Refreshes token expireation date in database |
|
509 | - * |
|
510 | - * @access private |
|
511 | - * @return void |
|
512 | - */ |
|
513 | - private function refresh_token_expiration () { |
|
514 | - # reset values |
|
515 | - $this->token = $this->User->user->token; |
|
294 | + /** |
|
295 | + * Authenticates user and returns token and validity |
|
296 | + * |
|
297 | + * @access private |
|
298 | + * @return void |
|
299 | + */ |
|
300 | + private function authenticate () { |
|
301 | + # try to authenticate user, it it fails it will fail by itself |
|
302 | + $this->User->authenticate ($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']); |
|
303 | + |
|
304 | + # if token is valid and set extend it, otherwise generate new |
|
305 | + if ($this->validate_user_token ()) { |
|
306 | + // extend |
|
307 | + $this->refresh_token_expiration (); |
|
308 | + } |
|
309 | + else { |
|
310 | + // generate new token |
|
311 | + $this->generate_token (); |
|
312 | + // save to user |
|
313 | + $this->save_user_token (); |
|
314 | + } |
|
315 | + |
|
316 | + # result |
|
317 | + return array("code"=>200, "data"=>array("token"=>$this->token, "expires"=>$this->token_expires)); |
|
318 | + } |
|
319 | + |
|
320 | + |
|
321 | + |
|
322 | + |
|
323 | + |
|
324 | + |
|
325 | + |
|
326 | + |
|
327 | + |
|
328 | + |
|
329 | + /* @tokens -------------------- */ |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * Sets default validiy for token (default 12 hours) |
|
334 | + * |
|
335 | + * @access public |
|
336 | + * @param int $token_valid_time (default: null) |
|
337 | + * @return void |
|
338 | + */ |
|
339 | + public function set_token_valid_time ($token_valid_time = null) { |
|
340 | + // validate integer |
|
341 | + if ($this->token_length!=null) { |
|
342 | + if (!is_numeric($this->token_length)) { $this->Response->throw_exception(500, "token valid time must be an integer"); } |
|
343 | + } |
|
344 | + // save |
|
345 | + $this->token_valid_time = is_null($token_valid_time) ? 21600 : $token_valid_time; |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Sets max number of failures before IP is blocked. |
|
350 | + * |
|
351 | + * @access public |
|
352 | + * @param mixed $failures (default: null) |
|
353 | + * @return void |
|
354 | + */ |
|
355 | + public function set_max_failures ($failures=null) { |
|
356 | + // validate integer |
|
357 | + if ($this->token_length!=null) { |
|
358 | + if (!is_numeric($this->token_length)) { $this->Response->throw_exception(500, "Max failures must be an integer"); } |
|
359 | + } |
|
360 | + // save |
|
361 | + $this->max_failures = $failures==null ? 10 : $failures; |
|
362 | + } |
|
363 | + |
|
364 | + /** |
|
365 | + * Block IP address. |
|
366 | + * |
|
367 | + * @access public |
|
368 | + * @param bool $block (default: true) |
|
369 | + * @return void |
|
370 | + */ |
|
371 | + public function block_ip ($block = true) { |
|
372 | + // validate integer |
|
373 | + if (!is_bool($block)) { |
|
374 | + if (!is_numeric($this->token_length)) { $this->Response->throw_exception(500, "Max failures must be an integer"); } |
|
375 | + } |
|
376 | + // save |
|
377 | + $this->block_ip = $$block; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * Sets length of token |
|
382 | + * |
|
383 | + * @access public |
|
384 | + * @param mixed $length (default: null) |
|
385 | + * @return void |
|
386 | + */ |
|
387 | + public function set_token_length ($length = null) { |
|
388 | + // validate number |
|
389 | + if ($length!=null) { |
|
390 | + if (!is_numeric($length)) { $this->Response->throw_exception(500, "token length must be an integer"); } |
|
391 | + elseif ($length>24) { $this->Response->throw_exception(500, "Maximum token length is 24 characters"); } |
|
392 | + } |
|
393 | + // save |
|
394 | + $this->token_length = is_null($length) ? 24 : $length; |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * Saves new token to database |
|
399 | + * |
|
400 | + * @access private |
|
401 | + * @return void |
|
402 | + */ |
|
403 | + private function save_user_token () { |
|
404 | + # set token values |
|
405 | + $values = array( |
|
406 | + "id"=>$this->User->user->id, |
|
407 | + "token"=>$this->token, |
|
408 | + "token_valid_until"=>$this->token_expires |
|
409 | + ); |
|
410 | + # save token to database |
|
411 | + if(!$this->Admin->object_modify ("users", "edit", "id", $values )) |
|
412 | + { $this->Response->throw_exception(500, "Failed to update token"); } |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Validates users token from database |
|
417 | + * |
|
418 | + * @access private |
|
419 | + * @return void |
|
420 | + */ |
|
421 | + private function validate_user_token () { |
|
422 | + // is set |
|
423 | + if (strlen($this->User->user->token)==0) { return false; } |
|
424 | + // date not set |
|
425 | + elseif (strlen($this->User->user->token_valid_until)==0) { return false; } |
|
426 | + // expired |
|
427 | + elseif ($this->User->user->token_valid_until < date("Y-m-d H:is:")) { return false; } |
|
428 | + // ok |
|
429 | + else { return true; } |
|
430 | + |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Validates requested token and saves it to $this->token |
|
435 | + * |
|
436 | + * @access private |
|
437 | + * @return void |
|
438 | + */ |
|
439 | + private function validate_requested_token () { |
|
440 | + return $this->_params->controller=="user" ? $this->validate_requested_token_user () : $this->validate_requested_token_general (); |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Validates token if User controller is requested - different response |
|
445 | + * |
|
446 | + * @access private |
|
447 | + * @return void |
|
448 | + */ |
|
449 | + private function validate_requested_token_user () { |
|
450 | + // check that token is present |
|
451 | + if(!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $this->Response->throw_exception(403, "Please provide token"); } |
|
452 | + // validate and remove token |
|
453 | + else { |
|
454 | + // fetch token |
|
455 | + if(($token = $this->Admin->fetch_object ("users", "token", $_SERVER['HTTP_PHPIPAM_TOKEN'])) === false) |
|
456 | + { $this->Response->throw_exception(403, "Invalid token"); } |
|
457 | + // save token |
|
458 | + $this->User->user = $token; |
|
459 | + $this->token = $token->token; |
|
460 | + $this->token_expires = $token->token_valid_until; |
|
461 | + |
|
462 | + // expired |
|
463 | + if($this->validate_token_expiration () === true) |
|
464 | + { $this->Response->throw_exception(403, "Token expired"); } |
|
465 | + } |
|
466 | + } |
|
467 | + |
|
468 | + /** |
|
469 | + * Validates token if general controller is requested - different response |
|
470 | + * |
|
471 | + * @access private |
|
472 | + * @return void |
|
473 | + */ |
|
474 | + private function validate_requested_token_general () { |
|
475 | + // check that token is present |
|
476 | + if(!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $this->Response->throw_exception(401, $this->Response->errors[401]); } |
|
477 | + // validate and remove token |
|
478 | + else { |
|
479 | + // fetch token |
|
480 | + if(($token = $this->Admin->fetch_object ("users", "token", $_SERVER['HTTP_PHPIPAM_TOKEN'])) === false) |
|
481 | + { $this->Response->throw_exception(401, $this->Response->errors[401]); } |
|
482 | + // save token |
|
483 | + $this->User->user = $token; |
|
484 | + $this->token = $token->token; |
|
485 | + $this->token_expires = $token->token_valid_until; |
|
486 | + |
|
487 | + // expired |
|
488 | + if($this->validate_token_expiration () === true) |
|
489 | + { $this->Response->throw_exception(401, $this->Response->errors[401]); } |
|
490 | + // refresh |
|
491 | + $this->refresh_token_expiration (); |
|
492 | + } |
|
493 | + } |
|
494 | + |
|
495 | + |
|
496 | + |
|
497 | + /** |
|
498 | + * Checks if token has expired |
|
499 | + * |
|
500 | + * @access private |
|
501 | + * @return void |
|
502 | + */ |
|
503 | + private function validate_token_expiration () { |
|
504 | + return strtotime($this->token_expires) < time() ? true : false; |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * Refreshes token expireation date in database |
|
509 | + * |
|
510 | + * @access private |
|
511 | + * @return void |
|
512 | + */ |
|
513 | + private function refresh_token_expiration () { |
|
514 | + # reset values |
|
515 | + $this->token = $this->User->user->token; |
|
516 | 516 | |
517 | - // convert existing expiry date string to a timestamp |
|
518 | - $expire_time = strtotime($this->token_expires); |
|
519 | - |
|
520 | - // Write Throttling from token updates |
|
521 | - // In order to keep the DB writes from token updates to a minimum, only update the expire time |
|
522 | - // if the expire time was set more than 60 seconds ago. |
|
523 | - if ( ((time()+$this->token_valid_time) - $expire_time) < 60) { |
|
524 | - return; |
|
525 | - } |
|
526 | - |
|
527 | - $this->token_expires = date("Y-m-d H:i:s", time()+$this->token_valid_time); |
|
528 | - # set token values |
|
529 | - $values = array( |
|
530 | - "id"=>$this->User->user->id, |
|
531 | - "token_valid_until"=>$this->token_expires |
|
532 | - ); |
|
533 | - # save token to database |
|
534 | - if(!$this->Admin->object_modify ("users", "edit", "id", $values )) |
|
535 | - { $this->Response->throw_exception(500, "Failed to update token expiration date"); } |
|
536 | - } |
|
537 | - |
|
538 | - /** |
|
539 | - * Removes users token |
|
540 | - * |
|
541 | - * @access private |
|
542 | - * @return void |
|
543 | - */ |
|
544 | - private function remove_token () { |
|
545 | - # set token values |
|
546 | - $values = array( |
|
547 | - "id"=>$this->User->user->id, |
|
548 | - "token"=>null, |
|
549 | - "token_valid_until"=>null |
|
550 | - ); |
|
551 | - # save token to database |
|
552 | - if(!$this->Admin->object_modify ("users", "edit", "id", $values )) |
|
553 | - { $this->Response->throw_exception(500, "Failed to remove token"); } |
|
554 | - |
|
555 | - } |
|
556 | - |
|
557 | - /** |
|
558 | - * Generates new token for user and writes it to database |
|
559 | - * |
|
560 | - * @access private |
|
561 | - * @return void |
|
562 | - */ |
|
563 | - private function generate_token () { |
|
564 | - $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$%!=.'; |
|
565 | - $chars_length = strlen($chars); |
|
566 | - // generate string |
|
567 | - $token = ''; |
|
568 | - for ($i = 0; $i < $this->token_length; $i++) { |
|
569 | - $token .= $chars[rand(0, $chars_length - 1)]; |
|
570 | - } |
|
571 | - // save token and valid time |
|
572 | - $this->token = $token; |
|
573 | - $this->token_expires = date("Y-m-d H:i:s", time()+$this->token_valid_time); |
|
574 | - } |
|
517 | + // convert existing expiry date string to a timestamp |
|
518 | + $expire_time = strtotime($this->token_expires); |
|
519 | + |
|
520 | + // Write Throttling from token updates |
|
521 | + // In order to keep the DB writes from token updates to a minimum, only update the expire time |
|
522 | + // if the expire time was set more than 60 seconds ago. |
|
523 | + if ( ((time()+$this->token_valid_time) - $expire_time) < 60) { |
|
524 | + return; |
|
525 | + } |
|
526 | + |
|
527 | + $this->token_expires = date("Y-m-d H:i:s", time()+$this->token_valid_time); |
|
528 | + # set token values |
|
529 | + $values = array( |
|
530 | + "id"=>$this->User->user->id, |
|
531 | + "token_valid_until"=>$this->token_expires |
|
532 | + ); |
|
533 | + # save token to database |
|
534 | + if(!$this->Admin->object_modify ("users", "edit", "id", $values )) |
|
535 | + { $this->Response->throw_exception(500, "Failed to update token expiration date"); } |
|
536 | + } |
|
537 | + |
|
538 | + /** |
|
539 | + * Removes users token |
|
540 | + * |
|
541 | + * @access private |
|
542 | + * @return void |
|
543 | + */ |
|
544 | + private function remove_token () { |
|
545 | + # set token values |
|
546 | + $values = array( |
|
547 | + "id"=>$this->User->user->id, |
|
548 | + "token"=>null, |
|
549 | + "token_valid_until"=>null |
|
550 | + ); |
|
551 | + # save token to database |
|
552 | + if(!$this->Admin->object_modify ("users", "edit", "id", $values )) |
|
553 | + { $this->Response->throw_exception(500, "Failed to remove token"); } |
|
554 | + |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * Generates new token for user and writes it to database |
|
559 | + * |
|
560 | + * @access private |
|
561 | + * @return void |
|
562 | + */ |
|
563 | + private function generate_token () { |
|
564 | + $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$%!=.'; |
|
565 | + $chars_length = strlen($chars); |
|
566 | + // generate string |
|
567 | + $token = ''; |
|
568 | + for ($i = 0; $i < $this->token_length; $i++) { |
|
569 | + $token .= $chars[rand(0, $chars_length - 1)]; |
|
570 | + } |
|
571 | + // save token and valid time |
|
572 | + $this->token = $token; |
|
573 | + $this->token_expires = date("Y-m-d H:i:s", time()+$this->token_valid_time); |
|
574 | + } |
|
575 | 575 | |
576 | 576 | } |
577 | 577 |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | * @param mixed $params |
111 | 111 | * @param mixed $Response |
112 | 112 | */ |
113 | - public function __construct ($Database, $Tools=null, $params=null, $Response) { |
|
113 | + public function __construct ($Database, $Tools = null, $params = null, $Response) { |
|
114 | 114 | $this->Database = $Database; |
115 | 115 | $this->Response = $Response; |
116 | 116 | $this->_params = $params; |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | $this->set_token_length (); |
124 | 124 | |
125 | 125 | // if HTTP_TOKEN is set change it to HTTP_PHPIPAM_TOKEN |
126 | - if (isset($_SERVER['HTTP_TOKEN'])&&!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $_SERVER['HTTP_PHPIPAM_TOKEN'] = $_SERVER['HTTP_TOKEN']; } |
|
126 | + if (isset($_SERVER['HTTP_TOKEN']) && !isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $_SERVER['HTTP_PHPIPAM_TOKEN'] = $_SERVER['HTTP_TOKEN']; } |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | |
@@ -146,15 +146,15 @@ discard block |
||
146 | 146 | $this->validate_options_request (); |
147 | 147 | |
148 | 148 | // methods |
149 | - $result = array(); |
|
150 | - $result['methods'] = array( |
|
151 | - array("href"=>"/api/".$this->_params->app_id."/user/", "methods"=>array(array("rel"=>"read", "method"=>"GET"), |
|
152 | - array("rel"=>"create", "method"=>"POST"), |
|
153 | - array("rel"=>"update", "method"=>"PATCH"), |
|
154 | - array("rel"=>"delete", "method"=>"DELETE"))), |
|
149 | + $result = array (); |
|
150 | + $result['methods'] = array ( |
|
151 | + array ("href"=>"/api/".$this->_params->app_id."/user/", "methods"=>array (array ("rel"=>"read", "method"=>"GET"), |
|
152 | + array ("rel"=>"create", "method"=>"POST"), |
|
153 | + array ("rel"=>"update", "method"=>"PATCH"), |
|
154 | + array ("rel"=>"delete", "method"=>"DELETE"))), |
|
155 | 155 | ); |
156 | 156 | # result |
157 | - return array("code"=>200, "data"=>$result); |
|
157 | + return array ("code"=>200, "data"=>$result); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | // validate token |
177 | 177 | $this->validate_requested_token (); |
178 | 178 | // ok |
179 | - return array("code"=>200, "data"=>array("expires"=>$this->token_expires)); |
|
179 | + return array ("code"=>200, "data"=>array ("expires"=>$this->token_expires)); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | // refresh |
215 | 215 | $this->refresh_token_expiration (); |
216 | 216 | // ok |
217 | - return array("code"=>200, "data"=>array("expires"=>$this->token_expires)); |
|
217 | + return array ("code"=>200, "data"=>array ("expires"=>$this->token_expires)); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | // remove token |
237 | 237 | $this->remove_token (); |
238 | 238 | // result |
239 | - return array("code"=>200, "data"=>array("Token removed")); |
|
239 | + return array ("code"=>200, "data"=>array ("Token removed")); |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | // get count |
281 | 281 | $cnt = $this->User->block_check_ip (); |
282 | 282 | // failure |
283 | - if ($cnt > $this->max_failures) { $this->Response->throw_exception(500, "Your IP has been blocked for 5 minutes because of excesive login failures"); } |
|
283 | + if ($cnt > $this->max_failures) { $this->Response->throw_exception (500, "Your IP has been blocked for 5 minutes because of excesive login failures"); } |
|
284 | 284 | } |
285 | 285 | } |
286 | 286 | |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | } |
315 | 315 | |
316 | 316 | # result |
317 | - return array("code"=>200, "data"=>array("token"=>$this->token, "expires"=>$this->token_expires)); |
|
317 | + return array ("code"=>200, "data"=>array ("token"=>$this->token, "expires"=>$this->token_expires)); |
|
318 | 318 | } |
319 | 319 | |
320 | 320 | |
@@ -338,11 +338,11 @@ discard block |
||
338 | 338 | */ |
339 | 339 | public function set_token_valid_time ($token_valid_time = null) { |
340 | 340 | // validate integer |
341 | - if ($this->token_length!=null) { |
|
342 | - if (!is_numeric($this->token_length)) { $this->Response->throw_exception(500, "token valid time must be an integer"); } |
|
341 | + if ($this->token_length != null) { |
|
342 | + if (!is_numeric ($this->token_length)) { $this->Response->throw_exception (500, "token valid time must be an integer"); } |
|
343 | 343 | } |
344 | 344 | // save |
345 | - $this->token_valid_time = is_null($token_valid_time) ? 21600 : $token_valid_time; |
|
345 | + $this->token_valid_time = is_null ($token_valid_time) ? 21600 : $token_valid_time; |
|
346 | 346 | } |
347 | 347 | |
348 | 348 | /** |
@@ -352,13 +352,13 @@ discard block |
||
352 | 352 | * @param mixed $failures (default: null) |
353 | 353 | * @return void |
354 | 354 | */ |
355 | - public function set_max_failures ($failures=null) { |
|
355 | + public function set_max_failures ($failures = null) { |
|
356 | 356 | // validate integer |
357 | - if ($this->token_length!=null) { |
|
358 | - if (!is_numeric($this->token_length)) { $this->Response->throw_exception(500, "Max failures must be an integer"); } |
|
357 | + if ($this->token_length != null) { |
|
358 | + if (!is_numeric ($this->token_length)) { $this->Response->throw_exception (500, "Max failures must be an integer"); } |
|
359 | 359 | } |
360 | 360 | // save |
361 | - $this->max_failures = $failures==null ? 10 : $failures; |
|
361 | + $this->max_failures = $failures == null ? 10 : $failures; |
|
362 | 362 | } |
363 | 363 | |
364 | 364 | /** |
@@ -370,8 +370,8 @@ discard block |
||
370 | 370 | */ |
371 | 371 | public function block_ip ($block = true) { |
372 | 372 | // validate integer |
373 | - if (!is_bool($block)) { |
|
374 | - if (!is_numeric($this->token_length)) { $this->Response->throw_exception(500, "Max failures must be an integer"); } |
|
373 | + if (!is_bool ($block)) { |
|
374 | + if (!is_numeric ($this->token_length)) { $this->Response->throw_exception (500, "Max failures must be an integer"); } |
|
375 | 375 | } |
376 | 376 | // save |
377 | 377 | $this->block_ip = $$block; |
@@ -386,12 +386,12 @@ discard block |
||
386 | 386 | */ |
387 | 387 | public function set_token_length ($length = null) { |
388 | 388 | // validate number |
389 | - if ($length!=null) { |
|
390 | - if (!is_numeric($length)) { $this->Response->throw_exception(500, "token length must be an integer"); } |
|
391 | - elseif ($length>24) { $this->Response->throw_exception(500, "Maximum token length is 24 characters"); } |
|
389 | + if ($length != null) { |
|
390 | + if (!is_numeric ($length)) { $this->Response->throw_exception (500, "token length must be an integer"); } |
|
391 | + elseif ($length > 24) { $this->Response->throw_exception (500, "Maximum token length is 24 characters"); } |
|
392 | 392 | } |
393 | 393 | // save |
394 | - $this->token_length = is_null($length) ? 24 : $length; |
|
394 | + $this->token_length = is_null ($length) ? 24 : $length; |
|
395 | 395 | } |
396 | 396 | |
397 | 397 | /** |
@@ -402,14 +402,14 @@ discard block |
||
402 | 402 | */ |
403 | 403 | private function save_user_token () { |
404 | 404 | # set token values |
405 | - $values = array( |
|
405 | + $values = array ( |
|
406 | 406 | "id"=>$this->User->user->id, |
407 | 407 | "token"=>$this->token, |
408 | 408 | "token_valid_until"=>$this->token_expires |
409 | 409 | ); |
410 | 410 | # save token to database |
411 | - if(!$this->Admin->object_modify ("users", "edit", "id", $values )) |
|
412 | - { $this->Response->throw_exception(500, "Failed to update token"); } |
|
411 | + if (!$this->Admin->object_modify ("users", "edit", "id", $values)) |
|
412 | + { $this->Response->throw_exception (500, "Failed to update token"); } |
|
413 | 413 | } |
414 | 414 | |
415 | 415 | /** |
@@ -420,13 +420,13 @@ discard block |
||
420 | 420 | */ |
421 | 421 | private function validate_user_token () { |
422 | 422 | // is set |
423 | - if (strlen($this->User->user->token)==0) { return false; } |
|
423 | + if (strlen ($this->User->user->token) == 0) { return false; } |
|
424 | 424 | // date not set |
425 | - elseif (strlen($this->User->user->token_valid_until)==0) { return false; } |
|
425 | + elseif (strlen ($this->User->user->token_valid_until) == 0) { return false; } |
|
426 | 426 | // expired |
427 | - elseif ($this->User->user->token_valid_until < date("Y-m-d H:is:")) { return false; } |
|
427 | + elseif ($this->User->user->token_valid_until < date ("Y-m-d H:is:")) { return false; } |
|
428 | 428 | // ok |
429 | - else { return true; } |
|
429 | + else { return true; } |
|
430 | 430 | |
431 | 431 | } |
432 | 432 | |
@@ -437,7 +437,7 @@ discard block |
||
437 | 437 | * @return void |
438 | 438 | */ |
439 | 439 | private function validate_requested_token () { |
440 | - return $this->_params->controller=="user" ? $this->validate_requested_token_user () : $this->validate_requested_token_general (); |
|
440 | + return $this->_params->controller == "user" ? $this->validate_requested_token_user () : $this->validate_requested_token_general (); |
|
441 | 441 | } |
442 | 442 | |
443 | 443 | /** |
@@ -448,20 +448,20 @@ discard block |
||
448 | 448 | */ |
449 | 449 | private function validate_requested_token_user () { |
450 | 450 | // check that token is present |
451 | - if(!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $this->Response->throw_exception(403, "Please provide token"); } |
|
451 | + if (!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $this->Response->throw_exception (403, "Please provide token"); } |
|
452 | 452 | // validate and remove token |
453 | 453 | else { |
454 | 454 | // fetch token |
455 | - if(($token = $this->Admin->fetch_object ("users", "token", $_SERVER['HTTP_PHPIPAM_TOKEN'])) === false) |
|
456 | - { $this->Response->throw_exception(403, "Invalid token"); } |
|
455 | + if (($token = $this->Admin->fetch_object ("users", "token", $_SERVER['HTTP_PHPIPAM_TOKEN'])) === false) |
|
456 | + { $this->Response->throw_exception (403, "Invalid token"); } |
|
457 | 457 | // save token |
458 | 458 | $this->User->user = $token; |
459 | 459 | $this->token = $token->token; |
460 | 460 | $this->token_expires = $token->token_valid_until; |
461 | 461 | |
462 | 462 | // expired |
463 | - if($this->validate_token_expiration () === true) |
|
464 | - { $this->Response->throw_exception(403, "Token expired"); } |
|
463 | + if ($this->validate_token_expiration () === true) |
|
464 | + { $this->Response->throw_exception (403, "Token expired"); } |
|
465 | 465 | } |
466 | 466 | } |
467 | 467 | |
@@ -473,20 +473,20 @@ discard block |
||
473 | 473 | */ |
474 | 474 | private function validate_requested_token_general () { |
475 | 475 | // check that token is present |
476 | - if(!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $this->Response->throw_exception(401, $this->Response->errors[401]); } |
|
476 | + if (!isset($_SERVER['HTTP_PHPIPAM_TOKEN'])) { $this->Response->throw_exception (401, $this->Response->errors[401]); } |
|
477 | 477 | // validate and remove token |
478 | 478 | else { |
479 | 479 | // fetch token |
480 | - if(($token = $this->Admin->fetch_object ("users", "token", $_SERVER['HTTP_PHPIPAM_TOKEN'])) === false) |
|
481 | - { $this->Response->throw_exception(401, $this->Response->errors[401]); } |
|
480 | + if (($token = $this->Admin->fetch_object ("users", "token", $_SERVER['HTTP_PHPIPAM_TOKEN'])) === false) |
|
481 | + { $this->Response->throw_exception (401, $this->Response->errors[401]); } |
|
482 | 482 | // save token |
483 | 483 | $this->User->user = $token; |
484 | 484 | $this->token = $token->token; |
485 | 485 | $this->token_expires = $token->token_valid_until; |
486 | 486 | |
487 | 487 | // expired |
488 | - if($this->validate_token_expiration () === true) |
|
489 | - { $this->Response->throw_exception(401, $this->Response->errors[401]); } |
|
488 | + if ($this->validate_token_expiration () === true) |
|
489 | + { $this->Response->throw_exception (401, $this->Response->errors[401]); } |
|
490 | 490 | // refresh |
491 | 491 | $this->refresh_token_expiration (); |
492 | 492 | } |
@@ -501,7 +501,7 @@ discard block |
||
501 | 501 | * @return void |
502 | 502 | */ |
503 | 503 | private function validate_token_expiration () { |
504 | - return strtotime($this->token_expires) < time() ? true : false; |
|
504 | + return strtotime ($this->token_expires) < time () ? true : false; |
|
505 | 505 | } |
506 | 506 | |
507 | 507 | /** |
@@ -515,24 +515,24 @@ discard block |
||
515 | 515 | $this->token = $this->User->user->token; |
516 | 516 | |
517 | 517 | // convert existing expiry date string to a timestamp |
518 | - $expire_time = strtotime($this->token_expires); |
|
518 | + $expire_time = strtotime ($this->token_expires); |
|
519 | 519 | |
520 | 520 | // Write Throttling from token updates |
521 | 521 | // In order to keep the DB writes from token updates to a minimum, only update the expire time |
522 | 522 | // if the expire time was set more than 60 seconds ago. |
523 | - if ( ((time()+$this->token_valid_time) - $expire_time) < 60) { |
|
523 | + if (((time () + $this->token_valid_time) - $expire_time) < 60) { |
|
524 | 524 | return; |
525 | 525 | } |
526 | 526 | |
527 | - $this->token_expires = date("Y-m-d H:i:s", time()+$this->token_valid_time); |
|
527 | + $this->token_expires = date ("Y-m-d H:i:s", time () + $this->token_valid_time); |
|
528 | 528 | # set token values |
529 | - $values = array( |
|
529 | + $values = array ( |
|
530 | 530 | "id"=>$this->User->user->id, |
531 | 531 | "token_valid_until"=>$this->token_expires |
532 | 532 | ); |
533 | 533 | # save token to database |
534 | - if(!$this->Admin->object_modify ("users", "edit", "id", $values )) |
|
535 | - { $this->Response->throw_exception(500, "Failed to update token expiration date"); } |
|
534 | + if (!$this->Admin->object_modify ("users", "edit", "id", $values)) |
|
535 | + { $this->Response->throw_exception (500, "Failed to update token expiration date"); } |
|
536 | 536 | } |
537 | 537 | |
538 | 538 | /** |
@@ -543,14 +543,14 @@ discard block |
||
543 | 543 | */ |
544 | 544 | private function remove_token () { |
545 | 545 | # set token values |
546 | - $values = array( |
|
546 | + $values = array ( |
|
547 | 547 | "id"=>$this->User->user->id, |
548 | 548 | "token"=>null, |
549 | 549 | "token_valid_until"=>null |
550 | 550 | ); |
551 | 551 | # save token to database |
552 | - if(!$this->Admin->object_modify ("users", "edit", "id", $values )) |
|
553 | - { $this->Response->throw_exception(500, "Failed to remove token"); } |
|
552 | + if (!$this->Admin->object_modify ("users", "edit", "id", $values)) |
|
553 | + { $this->Response->throw_exception (500, "Failed to remove token"); } |
|
554 | 554 | |
555 | 555 | } |
556 | 556 | |
@@ -561,16 +561,16 @@ discard block |
||
561 | 561 | * @return void |
562 | 562 | */ |
563 | 563 | private function generate_token () { |
564 | - $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$%!=.'; |
|
565 | - $chars_length = strlen($chars); |
|
564 | + $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$%!=.'; |
|
565 | + $chars_length = strlen ($chars); |
|
566 | 566 | // generate string |
567 | 567 | $token = ''; |
568 | 568 | for ($i = 0; $i < $this->token_length; $i++) { |
569 | - $token .= $chars[rand(0, $chars_length - 1)]; |
|
569 | + $token .= $chars[rand (0, $chars_length - 1)]; |
|
570 | 570 | } |
571 | 571 | // save token and valid time |
572 | 572 | $this->token = $token; |
573 | - $this->token_expires = date("Y-m-d H:i:s", time()+$this->token_valid_time); |
|
573 | + $this->token_expires = date ("Y-m-d H:i:s", time () + $this->token_valid_time); |
|
574 | 574 | } |
575 | 575 | |
576 | 576 | } |
@@ -37,60 +37,60 @@ discard block |
||
37 | 37 | |
38 | 38 | # set empty controller for options |
39 | 39 | if($_SERVER['REQUEST_METHOD']=="OPTIONS") { |
40 | - if( !isset($_GET['controller']) || $_GET['controller']=="") { $_GET['controller'] = "Tools"; } |
|
40 | + if( !isset($_GET['controller']) || $_GET['controller']=="") { $_GET['controller'] = "Tools"; } |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /* wrap in a try-catch block to catch exceptions */ |
44 | 44 | try { |
45 | 45 | |
46 | - /* Validate application ---------- */ |
|
47 | - |
|
48 | - // verify that API is enabled on server |
|
49 | - if($settings->api!=1) { $Response->throw_exception(503, "API server disabled");} |
|
50 | - |
|
51 | - # fetch app |
|
52 | - $app = $Tools->fetch_object ("api", "app_id", $_GET['app_id']); |
|
53 | - |
|
54 | - // verify app_id |
|
55 | - if($app === false) { $Response->throw_exception(400, "Invalid application id"); } |
|
56 | - // check that app is enabled |
|
57 | - if($app->app_permissions==="0") { $Response->throw_exception(503, "Application disabled"); } |
|
58 | - |
|
59 | - |
|
60 | - /* Check app security and prepare request parameters ---------- */ |
|
61 | - |
|
62 | - // crypt check |
|
63 | - if($app->app_security=="crypt") { |
|
64 | - // verify php extensions |
|
65 | - foreach (array("mcrypt") as $extension) { |
|
66 | - if (!in_array($extension, get_loaded_extensions())) |
|
67 | - { $Response->throw_exception(500, 'php extension '.$extension.' missing'); } |
|
68 | - } |
|
69 | - // decrypt request - to JSON |
|
70 | - $params = json_decode(trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $app->app_code, base64_decode($_GET['enc_request']), MCRYPT_MODE_ECB))); |
|
71 | - } |
|
72 | - // SSL checks |
|
73 | - elseif($app->app_security=="ssl") { |
|
74 | - // verify SSL |
|
75 | - if (!((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443)) { |
|
76 | - { $Response->throw_exception(503, 'App requires SSL connection'); } |
|
77 | - } |
|
78 | - // save request parameters |
|
79 | - $params = (object) $_GET; |
|
80 | - } |
|
81 | - // no security |
|
82 | - elseif($app->app_security=="none") { |
|
83 | - $params = (object) $_GET; |
|
84 | - } |
|
85 | - // error, invalid security |
|
86 | - else { |
|
87 | - $Response->throw_exception(503, 'Invalid app security'); |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - // append POST parameters if POST or PATCH |
|
92 | - if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH"){ |
|
93 | - // if application tupe is JSON (application/json) |
|
46 | + /* Validate application ---------- */ |
|
47 | + |
|
48 | + // verify that API is enabled on server |
|
49 | + if($settings->api!=1) { $Response->throw_exception(503, "API server disabled");} |
|
50 | + |
|
51 | + # fetch app |
|
52 | + $app = $Tools->fetch_object ("api", "app_id", $_GET['app_id']); |
|
53 | + |
|
54 | + // verify app_id |
|
55 | + if($app === false) { $Response->throw_exception(400, "Invalid application id"); } |
|
56 | + // check that app is enabled |
|
57 | + if($app->app_permissions==="0") { $Response->throw_exception(503, "Application disabled"); } |
|
58 | + |
|
59 | + |
|
60 | + /* Check app security and prepare request parameters ---------- */ |
|
61 | + |
|
62 | + // crypt check |
|
63 | + if($app->app_security=="crypt") { |
|
64 | + // verify php extensions |
|
65 | + foreach (array("mcrypt") as $extension) { |
|
66 | + if (!in_array($extension, get_loaded_extensions())) |
|
67 | + { $Response->throw_exception(500, 'php extension '.$extension.' missing'); } |
|
68 | + } |
|
69 | + // decrypt request - to JSON |
|
70 | + $params = json_decode(trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $app->app_code, base64_decode($_GET['enc_request']), MCRYPT_MODE_ECB))); |
|
71 | + } |
|
72 | + // SSL checks |
|
73 | + elseif($app->app_security=="ssl") { |
|
74 | + // verify SSL |
|
75 | + if (!((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443)) { |
|
76 | + { $Response->throw_exception(503, 'App requires SSL connection'); } |
|
77 | + } |
|
78 | + // save request parameters |
|
79 | + $params = (object) $_GET; |
|
80 | + } |
|
81 | + // no security |
|
82 | + elseif($app->app_security=="none") { |
|
83 | + $params = (object) $_GET; |
|
84 | + } |
|
85 | + // error, invalid security |
|
86 | + else { |
|
87 | + $Response->throw_exception(503, 'Invalid app security'); |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + // append POST parameters if POST or PATCH |
|
92 | + if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH"){ |
|
93 | + // if application tupe is JSON (application/json) |
|
94 | 94 | if($_SERVER['CONTENT_TYPE']=="application/json"){ |
95 | 95 | $rawPostData = file_get_contents('php://input'); |
96 | 96 | $json = json_decode($rawPostData,true); |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | $params = array_merge((array) $params, $json); |
99 | 99 | $params = (object) $params; |
100 | 100 | } |
101 | - // if application tupe is XML (application/json) |
|
101 | + // if application tupe is XML (application/json) |
|
102 | 102 | elseif($_SERVER['CONTENT_TYPE']=="application/xml"){ |
103 | 103 | $rawPostData = file_get_contents('php://input'); |
104 | 104 | $xml = $Response->xml_to_array($rawPostData); |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | $params = array_merge((array) $params, $xml); |
107 | 107 | $params = (object) $params; |
108 | 108 | } |
109 | - //if application type is default (application/x-www-form-urlencoded) |
|
109 | + //if application type is default (application/x-www-form-urlencoded) |
|
110 | 110 | elseif(sizeof(@$_POST)>0) { |
111 | 111 | $params = array_merge((array) $params, $_POST); |
112 | 112 | $params = (object) $params; |
@@ -116,74 +116,74 @@ discard block |
||
116 | 116 | |
117 | 117 | |
118 | 118 | |
119 | - /* Authentication ---------- */ |
|
119 | + /* Authentication ---------- */ |
|
120 | 120 | |
121 | - # authenticate user if required |
|
122 | - if (@$params->controller != "user" && $enable_authentication) { |
|
123 | - if($app->app_security=="ssl" || $app->app_security=="none") { |
|
124 | - // start auth class and validate connection |
|
125 | - require( dirname(__FILE__) . '/controllers/User.php'); // authentication and token handling |
|
126 | - $Authentication = new User_controller ($Database, $Tools, $params, $Response); |
|
127 | - $Authentication->check_auth (); |
|
128 | - } |
|
129 | - } |
|
121 | + # authenticate user if required |
|
122 | + if (@$params->controller != "user" && $enable_authentication) { |
|
123 | + if($app->app_security=="ssl" || $app->app_security=="none") { |
|
124 | + // start auth class and validate connection |
|
125 | + require( dirname(__FILE__) . '/controllers/User.php'); // authentication and token handling |
|
126 | + $Authentication = new User_controller ($Database, $Tools, $params, $Response); |
|
127 | + $Authentication->check_auth (); |
|
128 | + } |
|
129 | + } |
|
130 | 130 | |
131 | 131 | |
132 | - /* verify request ---------- */ |
|
132 | + /* verify request ---------- */ |
|
133 | 133 | |
134 | - // check if the request is valid by checking if it's an array and looking for the controller and action |
|
135 | - if( $params == false || isset($params->controller) == false ) { |
|
136 | - $Response->throw_exception(400, 'Request is not valid'); |
|
137 | - } |
|
138 | - // verify permissions for delete/create/edit if controller is not user (needed for auth) |
|
139 | - if (@$params->controller != "user") { |
|
140 | - if( ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH" |
|
141 | - || $_SERVER['REQUEST_METHOD']=="PUT" || $_SERVER['REQUEST_METHOD']=="DELETE" |
|
142 | - ) |
|
143 | - && $app->app_permissions<2) { |
|
144 | - $Response->throw_exception(401, 'invalid permissions'); |
|
145 | - } |
|
146 | - } |
|
147 | - // verify content type |
|
148 | - $Response->validate_content_type (); |
|
134 | + // check if the request is valid by checking if it's an array and looking for the controller and action |
|
135 | + if( $params == false || isset($params->controller) == false ) { |
|
136 | + $Response->throw_exception(400, 'Request is not valid'); |
|
137 | + } |
|
138 | + // verify permissions for delete/create/edit if controller is not user (needed for auth) |
|
139 | + if (@$params->controller != "user") { |
|
140 | + if( ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH" |
|
141 | + || $_SERVER['REQUEST_METHOD']=="PUT" || $_SERVER['REQUEST_METHOD']=="DELETE" |
|
142 | + ) |
|
143 | + && $app->app_permissions<2) { |
|
144 | + $Response->throw_exception(401, 'invalid permissions'); |
|
145 | + } |
|
146 | + } |
|
147 | + // verify content type |
|
148 | + $Response->validate_content_type (); |
|
149 | 149 | |
150 | 150 | |
151 | - /* Initialize controller ---------- */ |
|
151 | + /* Initialize controller ---------- */ |
|
152 | 152 | |
153 | - //get the controller and format it correctly |
|
154 | - $controller = ucfirst(strtolower($params->controller))."_controller"; |
|
155 | - $controller_file = ucfirst(strtolower($params->controller)); |
|
153 | + //get the controller and format it correctly |
|
154 | + $controller = ucfirst(strtolower($params->controller))."_controller"; |
|
155 | + $controller_file = ucfirst(strtolower($params->controller)); |
|
156 | 156 | |
157 | - //check if the controller exists. if not, throw an exception |
|
158 | - if( file_exists( dirname(__FILE__) . "/controllers/$controller_file.php") ) { |
|
159 | - require( dirname(__FILE__) . "/controllers/$controller_file.php"); |
|
160 | - } else { |
|
161 | - $Response->throw_exception(400, 'invalid controller'); |
|
162 | - } |
|
157 | + //check if the controller exists. if not, throw an exception |
|
158 | + if( file_exists( dirname(__FILE__) . "/controllers/$controller_file.php") ) { |
|
159 | + require( dirname(__FILE__) . "/controllers/$controller_file.php"); |
|
160 | + } else { |
|
161 | + $Response->throw_exception(400, 'invalid controller'); |
|
162 | + } |
|
163 | 163 | |
164 | - //create a new instance of the controller, and pass |
|
165 | - //it the parameters from the request and Database object |
|
166 | - $controller = new $controller($Database, $Tools, $params, $Response); |
|
164 | + //create a new instance of the controller, and pass |
|
165 | + //it the parameters from the request and Database object |
|
166 | + $controller = new $controller($Database, $Tools, $params, $Response); |
|
167 | 167 | |
168 | - //check if the action exists in the controller. if not, throw an exception. |
|
169 | - if( method_exists($controller, strtolower($_SERVER['REQUEST_METHOD'])) === false ) { |
|
170 | - $Response->throw_exception(501, $Response->errors[501]); |
|
171 | - } |
|
168 | + //check if the action exists in the controller. if not, throw an exception. |
|
169 | + if( method_exists($controller, strtolower($_SERVER['REQUEST_METHOD'])) === false ) { |
|
170 | + $Response->throw_exception(501, $Response->errors[501]); |
|
171 | + } |
|
172 | 172 | |
173 | - //execute the action |
|
174 | - $result = $controller->$_SERVER['REQUEST_METHOD'] (); |
|
173 | + //execute the action |
|
174 | + $result = $controller->$_SERVER['REQUEST_METHOD'] (); |
|
175 | 175 | |
176 | 176 | } catch ( Exception $e ) { |
177 | - //catch any exceptions and report the problem |
|
178 | - $result = $e->getMessage(); |
|
179 | - |
|
180 | - //set flag if it came from Result, just to be sure |
|
181 | - if($Response->exception!==true) { |
|
182 | - $Response->exception = true; |
|
183 | - $Response->result['success'] = false; |
|
184 | - $Response->result['code'] = 500; |
|
185 | - $Response->result['message'] = $result; |
|
186 | - } |
|
177 | + //catch any exceptions and report the problem |
|
178 | + $result = $e->getMessage(); |
|
179 | + |
|
180 | + //set flag if it came from Result, just to be sure |
|
181 | + if($Response->exception!==true) { |
|
182 | + $Response->exception = true; |
|
183 | + $Response->result['success'] = false; |
|
184 | + $Response->result['code'] = 500; |
|
185 | + $Response->result['message'] = $result; |
|
186 | + } |
|
187 | 187 | } |
188 | 188 | |
189 | 189 |
@@ -89,21 +89,23 @@ discard block |
||
89 | 89 | |
90 | 90 | |
91 | 91 | // append POST parameters if POST or PATCH |
92 | - if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH"){ |
|
92 | + if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH") { |
|
93 | 93 | // if application tupe is JSON (application/json) |
94 | - if($_SERVER['CONTENT_TYPE']=="application/json"){ |
|
94 | + if($_SERVER['CONTENT_TYPE']=="application/json") { |
|
95 | 95 | $rawPostData = file_get_contents('php://input'); |
96 | 96 | $json = json_decode($rawPostData,true); |
97 | - if(is_array($json)) |
|
98 | - $params = array_merge((array) $params, $json); |
|
97 | + if(is_array($json)) { |
|
98 | + $params = array_merge((array) $params, $json); |
|
99 | + } |
|
99 | 100 | $params = (object) $params; |
100 | 101 | } |
101 | 102 | // if application tupe is XML (application/json) |
102 | - elseif($_SERVER['CONTENT_TYPE']=="application/xml"){ |
|
103 | + elseif($_SERVER['CONTENT_TYPE']=="application/xml") { |
|
103 | 104 | $rawPostData = file_get_contents('php://input'); |
104 | 105 | $xml = $Response->xml_to_array($rawPostData); |
105 | - if(is_array($xml)) |
|
106 | - $params = array_merge((array) $params, $xml); |
|
106 | + if(is_array($xml)) { |
|
107 | + $params = array_merge((array) $params, $xml); |
|
108 | + } |
|
107 | 109 | $params = (object) $params; |
108 | 110 | } |
109 | 111 | //if application type is default (application/x-www-form-urlencoded) |
@@ -157,7 +159,8 @@ discard block |
||
157 | 159 | //check if the controller exists. if not, throw an exception |
158 | 160 | if( file_exists( dirname(__FILE__) . "/controllers/$controller_file.php") ) { |
159 | 161 | require( dirname(__FILE__) . "/controllers/$controller_file.php"); |
160 | - } else { |
|
162 | + } |
|
163 | + else { |
|
161 | 164 | $Response->throw_exception(400, 'invalid controller'); |
162 | 165 | } |
163 | 166 |
@@ -18,9 +18,9 @@ discard block |
||
18 | 18 | */ |
19 | 19 | |
20 | 20 | # include funtions |
21 | -require( dirname(__FILE__) . '/../functions/functions.php'); // functions and objects from phpipam |
|
22 | -require( dirname(__FILE__) . '/controllers/Common.php'); // common methods |
|
23 | -require( dirname(__FILE__) . '/controllers/Responses.php'); // exception, header and response handling |
|
21 | +require(dirname (__FILE__).'/../functions/functions.php'); // functions and objects from phpipam |
|
22 | +require(dirname (__FILE__).'/controllers/Common.php'); // common methods |
|
23 | +require(dirname (__FILE__).'/controllers/Responses.php'); // exception, header and response handling |
|
24 | 24 | |
25 | 25 | # settings |
26 | 26 | $enable_authentication = true; |
@@ -33,11 +33,11 @@ discard block |
||
33 | 33 | $Response = new Responses (); |
34 | 34 | |
35 | 35 | # get phpipam settings |
36 | -$settings = $Tools->fetch_object ("settings", "id", 1); |
|
36 | +$settings = $Tools->fetch_object ("settings", "id", 1); |
|
37 | 37 | |
38 | 38 | # set empty controller for options |
39 | -if($_SERVER['REQUEST_METHOD']=="OPTIONS") { |
|
40 | - if( !isset($_GET['controller']) || $_GET['controller']=="") { $_GET['controller'] = "Tools"; } |
|
39 | +if ($_SERVER['REQUEST_METHOD'] == "OPTIONS") { |
|
40 | + if (!isset($_GET['controller']) || $_GET['controller'] == "") { $_GET['controller'] = "Tools"; } |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /* wrap in a try-catch block to catch exceptions */ |
@@ -46,69 +46,69 @@ discard block |
||
46 | 46 | /* Validate application ---------- */ |
47 | 47 | |
48 | 48 | // verify that API is enabled on server |
49 | - if($settings->api!=1) { $Response->throw_exception(503, "API server disabled");} |
|
49 | + if ($settings->api != 1) { $Response->throw_exception (503, "API server disabled"); } |
|
50 | 50 | |
51 | 51 | # fetch app |
52 | 52 | $app = $Tools->fetch_object ("api", "app_id", $_GET['app_id']); |
53 | 53 | |
54 | 54 | // verify app_id |
55 | - if($app === false) { $Response->throw_exception(400, "Invalid application id"); } |
|
55 | + if ($app === false) { $Response->throw_exception (400, "Invalid application id"); } |
|
56 | 56 | // check that app is enabled |
57 | - if($app->app_permissions==="0") { $Response->throw_exception(503, "Application disabled"); } |
|
57 | + if ($app->app_permissions === "0") { $Response->throw_exception (503, "Application disabled"); } |
|
58 | 58 | |
59 | 59 | |
60 | 60 | /* Check app security and prepare request parameters ---------- */ |
61 | 61 | |
62 | 62 | // crypt check |
63 | - if($app->app_security=="crypt") { |
|
63 | + if ($app->app_security == "crypt") { |
|
64 | 64 | // verify php extensions |
65 | - foreach (array("mcrypt") as $extension) { |
|
66 | - if (!in_array($extension, get_loaded_extensions())) |
|
67 | - { $Response->throw_exception(500, 'php extension '.$extension.' missing'); } |
|
65 | + foreach (array ("mcrypt") as $extension) { |
|
66 | + if (!in_array ($extension, get_loaded_extensions ())) |
|
67 | + { $Response->throw_exception (500, 'php extension '.$extension.' missing'); } |
|
68 | 68 | } |
69 | 69 | // decrypt request - to JSON |
70 | - $params = json_decode(trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $app->app_code, base64_decode($_GET['enc_request']), MCRYPT_MODE_ECB))); |
|
70 | + $params = json_decode (trim (mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $app->app_code, base64_decode ($_GET['enc_request']), MCRYPT_MODE_ECB))); |
|
71 | 71 | } |
72 | 72 | // SSL checks |
73 | - elseif($app->app_security=="ssl") { |
|
73 | + elseif ($app->app_security == "ssl") { |
|
74 | 74 | // verify SSL |
75 | 75 | if (!((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443)) { |
76 | - { $Response->throw_exception(503, 'App requires SSL connection'); } |
|
76 | + { $Response->throw_exception (503, 'App requires SSL connection'); } |
|
77 | 77 | } |
78 | 78 | // save request parameters |
79 | 79 | $params = (object) $_GET; |
80 | 80 | } |
81 | 81 | // no security |
82 | - elseif($app->app_security=="none") { |
|
82 | + elseif ($app->app_security == "none") { |
|
83 | 83 | $params = (object) $_GET; |
84 | 84 | } |
85 | 85 | // error, invalid security |
86 | 86 | else { |
87 | - $Response->throw_exception(503, 'Invalid app security'); |
|
87 | + $Response->throw_exception (503, 'Invalid app security'); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | |
91 | 91 | // append POST parameters if POST or PATCH |
92 | - if($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH"){ |
|
92 | + if ($_SERVER['REQUEST_METHOD'] == "POST" || $_SERVER['REQUEST_METHOD'] == "PATCH") { |
|
93 | 93 | // if application tupe is JSON (application/json) |
94 | - if($_SERVER['CONTENT_TYPE']=="application/json"){ |
|
95 | - $rawPostData = file_get_contents('php://input'); |
|
96 | - $json = json_decode($rawPostData,true); |
|
97 | - if(is_array($json)) |
|
98 | - $params = array_merge((array) $params, $json); |
|
94 | + if ($_SERVER['CONTENT_TYPE'] == "application/json") { |
|
95 | + $rawPostData = file_get_contents ('php://input'); |
|
96 | + $json = json_decode ($rawPostData, true); |
|
97 | + if (is_array ($json)) |
|
98 | + $params = array_merge ((array) $params, $json); |
|
99 | 99 | $params = (object) $params; |
100 | 100 | } |
101 | 101 | // if application tupe is XML (application/json) |
102 | - elseif($_SERVER['CONTENT_TYPE']=="application/xml"){ |
|
103 | - $rawPostData = file_get_contents('php://input'); |
|
104 | - $xml = $Response->xml_to_array($rawPostData); |
|
105 | - if(is_array($xml)) |
|
106 | - $params = array_merge((array) $params, $xml); |
|
102 | + elseif ($_SERVER['CONTENT_TYPE'] == "application/xml") { |
|
103 | + $rawPostData = file_get_contents ('php://input'); |
|
104 | + $xml = $Response->xml_to_array ($rawPostData); |
|
105 | + if (is_array ($xml)) |
|
106 | + $params = array_merge ((array) $params, $xml); |
|
107 | 107 | $params = (object) $params; |
108 | 108 | } |
109 | 109 | //if application type is default (application/x-www-form-urlencoded) |
110 | - elseif(sizeof(@$_POST)>0) { |
|
111 | - $params = array_merge((array) $params, $_POST); |
|
110 | + elseif (sizeof (@$_POST) > 0) { |
|
111 | + $params = array_merge ((array) $params, $_POST); |
|
112 | 112 | $params = (object) $params; |
113 | 113 | } |
114 | 114 | } |
@@ -120,9 +120,9 @@ discard block |
||
120 | 120 | |
121 | 121 | # authenticate user if required |
122 | 122 | if (@$params->controller != "user" && $enable_authentication) { |
123 | - if($app->app_security=="ssl" || $app->app_security=="none") { |
|
123 | + if ($app->app_security == "ssl" || $app->app_security == "none") { |
|
124 | 124 | // start auth class and validate connection |
125 | - require( dirname(__FILE__) . '/controllers/User.php'); // authentication and token handling |
|
125 | + require(dirname (__FILE__).'/controllers/User.php'); // authentication and token handling |
|
126 | 126 | $Authentication = new User_controller ($Database, $Tools, $params, $Response); |
127 | 127 | $Authentication->check_auth (); |
128 | 128 | } |
@@ -132,16 +132,16 @@ discard block |
||
132 | 132 | /* verify request ---------- */ |
133 | 133 | |
134 | 134 | // check if the request is valid by checking if it's an array and looking for the controller and action |
135 | - if( $params == false || isset($params->controller) == false ) { |
|
136 | - $Response->throw_exception(400, 'Request is not valid'); |
|
135 | + if ($params == false || isset($params->controller) == false) { |
|
136 | + $Response->throw_exception (400, 'Request is not valid'); |
|
137 | 137 | } |
138 | 138 | // verify permissions for delete/create/edit if controller is not user (needed for auth) |
139 | 139 | if (@$params->controller != "user") { |
140 | - if( ($_SERVER['REQUEST_METHOD']=="POST" || $_SERVER['REQUEST_METHOD']=="PATCH" |
|
141 | - || $_SERVER['REQUEST_METHOD']=="PUT" || $_SERVER['REQUEST_METHOD']=="DELETE" |
|
140 | + if (($_SERVER['REQUEST_METHOD'] == "POST" || $_SERVER['REQUEST_METHOD'] == "PATCH" |
|
141 | + || $_SERVER['REQUEST_METHOD'] == "PUT" || $_SERVER['REQUEST_METHOD'] == "DELETE" |
|
142 | 142 | ) |
143 | - && $app->app_permissions<2) { |
|
144 | - $Response->throw_exception(401, 'invalid permissions'); |
|
143 | + && $app->app_permissions < 2) { |
|
144 | + $Response->throw_exception (401, 'invalid permissions'); |
|
145 | 145 | } |
146 | 146 | } |
147 | 147 | // verify content type |
@@ -151,37 +151,37 @@ discard block |
||
151 | 151 | /* Initialize controller ---------- */ |
152 | 152 | |
153 | 153 | //get the controller and format it correctly |
154 | - $controller = ucfirst(strtolower($params->controller))."_controller"; |
|
155 | - $controller_file = ucfirst(strtolower($params->controller)); |
|
154 | + $controller = ucfirst (strtolower ($params->controller))."_controller"; |
|
155 | + $controller_file = ucfirst (strtolower ($params->controller)); |
|
156 | 156 | |
157 | 157 | //check if the controller exists. if not, throw an exception |
158 | - if( file_exists( dirname(__FILE__) . "/controllers/$controller_file.php") ) { |
|
159 | - require( dirname(__FILE__) . "/controllers/$controller_file.php"); |
|
158 | + if (file_exists (dirname (__FILE__)."/controllers/$controller_file.php")) { |
|
159 | + require(dirname (__FILE__)."/controllers/$controller_file.php"); |
|
160 | 160 | } else { |
161 | - $Response->throw_exception(400, 'invalid controller'); |
|
161 | + $Response->throw_exception (400, 'invalid controller'); |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | //create a new instance of the controller, and pass |
165 | 165 | //it the parameters from the request and Database object |
166 | - $controller = new $controller($Database, $Tools, $params, $Response); |
|
166 | + $controller = new $controller ($Database, $Tools, $params, $Response); |
|
167 | 167 | |
168 | 168 | //check if the action exists in the controller. if not, throw an exception. |
169 | - if( method_exists($controller, strtolower($_SERVER['REQUEST_METHOD'])) === false ) { |
|
170 | - $Response->throw_exception(501, $Response->errors[501]); |
|
169 | + if (method_exists ($controller, strtolower ($_SERVER['REQUEST_METHOD'])) === false) { |
|
170 | + $Response->throw_exception (501, $Response->errors[501]); |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | //execute the action |
174 | 174 | $result = $controller->$_SERVER['REQUEST_METHOD'] (); |
175 | 175 | |
176 | -} catch ( Exception $e ) { |
|
176 | +} catch (Exception $e) { |
|
177 | 177 | //catch any exceptions and report the problem |
178 | - $result = $e->getMessage(); |
|
178 | + $result = $e->getMessage (); |
|
179 | 179 | |
180 | 180 | //set flag if it came from Result, just to be sure |
181 | - if($Response->exception!==true) { |
|
181 | + if ($Response->exception !== true) { |
|
182 | 182 | $Response->exception = true; |
183 | 183 | $Response->result['success'] = false; |
184 | - $Response->result['code'] = 500; |
|
184 | + $Response->result['code'] = 500; |
|
185 | 185 | $Response->result['message'] = $result; |
186 | 186 | } |
187 | 187 | } |