Passed
Push — master ( 7ab0bd...e4c9ff )
by Joe
02:32
created

Fantastico::getIpTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Fantastico
4
 * Fantastico Class for interfacing with there API.  Their API is pretty simple, so this
5
 * might not be needed, it just simplifies things a little more.
6
 *
7
 * @package MyAdmin
8
 * @author detain
9
 * @copyright 2017
10
 * @version $Id$
11
 * @access public
12
 */
13
14
namespace Detain\Fantastico;
15
16
class Fantastico {
0 ignored issues
show
Coding Style introduced by
The property $api_username is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $api_password is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
17
18
	/**
19
	 * All fantastico license types
20
	 */
21
	const ALL_TYPES = 0;
22
23
	/**
24
	 * Normal/Server license types
25
	 */
26
	const NORMAL_TYPES = 1;
27
28
	/**
29
	 * VPS license types
30
	 */
31
	const VPS_TYPES = 2;
32
33
	/**
34
	 * the WSDL API file
35
	 */
36
	public $wsdl = 'https://netenberg.com/api/netenberg.wsdl';
37
38
	/**
39
	 * the username to use for API access
40
	 */
41
	private $api_username;
42
43
	/**
44
	 * the password to use for api access
45
	 */
46
	private $api_password;
47
48
	/**
49
	 * this will hold the soap client, which hopefully we can reuse for multiple queries if need be
50
	 */
51
	private $soapClient;
52
53
	/**
54
	 * this will hold responses from the API for future caching use
55
	 */
56
	private $cache;
57
58
59
60
	/**
61
	 * this holds an array of possible license types you can use and there descriptions
62
	 * note the ALL_TYPES is only for listing, you cant buy one thats all_types
63
	 */
64
	public $types = array(
65
		self::ALL_TYPES => 'All IPs',
66
		self::NORMAL_TYPES => 'Normal Licenses',
67
		self::VPS_TYPES => 'VPS Licenses');
68
69
	/**
70
	 * Starts an instance of the fantastico license API.
71
	 *
72
	 * @param string $username username to connect to fantastico api with
73
	 * @param string $password password to connect to fantastico api with
74
	 */
75
	public function __construct($username, $password) {
76
		$this->cache = array();
77
		$this->soapClient = null;
78
		$this->api_username = $username;
79
		$this->api_password = $password;
80
	}
81
82
	/**
83
	 * Fantastico::connect()
84
	 * function called automatically to ensure that we're connected to the API
85
	 *
86
	 * @return void
87
	 */
88
	public function connect() {
89
		if (is_null($this->soapClient)) {
90
			ini_set('soap.wsdl_cache_enabled', '0');
91
			ini_set('max_execution_time', 1000);
92
			ini_set('default_socket_timeout', 1000);
93
			try {
94
				$this->soapClient = new \SoapClient($this->wsdl, array(
95
					'soap_version' => SOAP_1_1,
96
					'connection_timeout' => 1000,
97
					'trace' => 1,
98
					'exception' => 1));
99
			} catch (\Exception $e) {
100
				require_once (INCLUDE_ROOT.'/../vendor/detain/nusoap/lib/nusoap.php');
101
				$this->soapClient = new \nusoap_client($this->wsdl);
102
			}
103
		}
104
	}
105
106
	/**
107
	 * Fantastico::getIpTypes()
108
	 * returns an array of the possible license types in the format of
109
	 *   ID => Description
110
	 * where ID is the Type ID you need to pass to various functions asking for a license type
111
	 *
112
	 * @return array returns an array of possible license types and descriptions of them
113
	 */
114
	public function getIpTypes() {
115
		return $this->types;
116
	}
117
118
	/**
119
	 * Fantastico::isType()
120
	 * a check to make sure the passed type is valid
121
	 *
122
	 * @param mixed the license type your trying to validate
123
	 * @param integer $type
124
	 * @return bool whether or not its a valid fantastico license type
125
	 */
126
	public function isType($type) {
127
		return array_key_exists($type, $this->types);
128
	}
129
130
	/**
131
	 * Fantastico::getHash()
132
	 * returns the login hash
133
	 *
134
	 * @return string the login hash
135
	 */
136
	private function getHash() {
137
		return md5($this->api_username.$this->api_password);
138
	}
139
140
	/**
141
	 * Fantastico::getIpList()
142
	 * returns a list of all license types.
143
	 *
144
	 * Sample Return Output
145
	 * Array
146
	 * (
147
	 *     [0] => 130.253.175.32
148
	 *     [1] => 140.99.16.206
149
	 *     [2] => 150.101.195.140
150
	 * )
151
	 *
152
	 * @param integer $type one of the possible fantastico license types, defaults to {@link self::ALL_TYPES}
153
	 * @return false|array returns false on error or an array of license details
154
	 */
155
	public function getIpList($type = self::ALL_TYPES) {
156
		if (isset($this->cache['getIpList_'.$type])) {
157
			return $this->cache['getIpList_'.$type];
158
		}
159
		if (!$this->isType($type)) {
160
			return false;
161
		}
162
		$this->connect();
163
		$this->cache['getIpList_'.$type] = json_decode($this->soapClient->getIpList($this->getHash(), $type), true);
164
		myadmin_log('fantastico', 'debug', json_encode($this->cache['getIpList_'.$type]), __LINE__, __FILE__);
165
		return $this->cache['getIpList_'.$type];
166
	}
167
168
	/**
169
	 * Fantastico::getIpListDetailed()
170
	 * returns a list of all license types and details about each one
171
	 *
172
	 * Sample Return Output
173
	 *
174
	 *
175
	 * 	Array
176
	 * 	(
177
	 * 	    [0] => {
178
	 * 	    	Array
179
	 * 			(
180
	 * 			    [ipAddress] => 130.253.175.32
181
	 * 			    [addedOn] => 2010-03-01 00:00:00
182
	 * 			    [isVPS] => ( Yes || No )
183
	 * 			    [status] => ( Active || Inactive )
184
	 * 			)
185
	 * 		}
186
	 * 	    [1] => {
187
	 * 	    	Array
188
	 * 			(
189
	 * 			    [ipAddress] => 131.253.175.32
190
	 * 			    [addedOn] => 2011-03-01 00:00:00
191
	 * 			    [isVPS] => ( Yes || No )
192
	 * 			    [status] => ( Active || Inactive )
193
	 * 			)
194
	 * 	    }
195
	 * 	    [2] => {
196
	 * 	    	Array
197
	 * 			(
198
	 * 			    [ipAddress] => 132.253.175.32
199
	 * 			    [addedOn] => 2012-03-01 00:00:00
200
	 * 			    [isVPS] => ( Yes || No )
201
	 * 			    [status] => ( Active || Inactive )
202
	 * 			)
203
	 * 	    }
204
	 * 	)
205
	 *
206
	 * @param integer $type one of the possible fantastico license types, defaults to {@link self::ALL_TYPES}
207
	 * @return false|array returns false on error or an array of license details
208
	 */
209
	public function getIpListDetailed($type = self::ALL_TYPES) {
210
		if (!$this->isType($type)) {
211
			return false;
212
		}
213
		if (isset($this->cache['getIpListDetailed_'.$type])) {
214
			return $this->cache['getIpListDetailed_'.$type];
215
		}
216
		$this->connect();
217
		//try {
218
		$response = json_decode($this->soapClient->__soapCall('getIpListDetailed', array($this->getHash(), $type)), true);
219
		myadmin_log('fantastico', 'debug', json_encode($response), __LINE__, __FILE__);
220
		//echo '<pre>';echo print_r($response, true);echo '</pre>';
221
		//$this->cache['getIpListDetailed_' . $type] = $this->cache['getIpListDetailed_' . $type]->Licenses;
222
		$this->cache['getIpListDetailed_'.$type] = array();
223
		$this->cache['getIpList_'.$type] = array();
224
		foreach ($response['Licenses'] as $idx => $data) {
225
			$tdata = array(
226
				'ipAddress' => $data[0],
227
				'addedOn' => $data[1],
228
				'isVPS' => $data[2],
229
				'status' => $data[3]);
230
			$this->cache['getIpListDetailed_'.$type][] = $tdata;
231
			$this->cache['getIpList_'.$type][] = $tdata['ipAddress'];
232
			$this->cache['getIpDetails_'.$tdata['ipAddress']] = $tdata;
233
		}
234
		//} catch (SoapFault $fault) {
235
		//var_dump($fault);
236
		//var_dump($this->soapClient->__getLastRequest());
237
		//var_dump($this->soapClient->__getLastResponse());
238
		//}
239
240
		//foreach ($this->cache['getIpListDetailed_' . $type] as $idx => $data)
241
		//{
242
		//$this->cache['getIpList_' . $type][] = $data['ipAddress'];
243
		//$this->cache['getIpDetails_' . $data['ipAddress']] = $data;
244
		//}
245
		//echo '<pre>';print_r($this->cache);echo '</pre>';
246
		return $this->cache['getIpListDetailed_'.$type];
247
	}
248
249
	/**
250
	 * Fantastico::valid_ip()
251
	 * validates the IP address
252
	 *
253
	 * @param string $ip IP Address to validate
254
	 * @return bool whether or not the ip was validated
255
	 */
256
	public function valid_ip($ip) {
257
		return ip2long($ip) !== false;
258
	}
259
260
	/**
261
	 * Fantastico::getIpDetails()
262
	 * get details about a license
263
	 *
264
	 * Output Success
265
	 * Array
266
	 * (
267
	 *     [ipAddress] => 130.253.175.32
268
	 *     [addedOn] => 2010-03-01 00:00:00
269
	 *     [isVPS] => ( Yes || No )
270
	 *     [status] => ( Active || Inactive )
271
	 * )
272
	 *
273
	 *
274
	 * Output Error
275
	 *
276
	 * Array
277
	 * (
278
	 *     [faultcode] => 1801
279
	 *     [fault ]=> "The IP Address that you have specified does not exist."
280
	 * )
281
	 *
282
	 * @param string $ip ip address to get details for
283
	 * @return mixed returns false on invalid IP, or an array of the details.
284
	 */
285
	public function getIpDetails($ip) {
286
		if (!$this->valid_ip($ip)) {
287
			return array('faultcode' => 1, 'fault ' => 'Invalid IP Address '.$ip);
288
		}
289
		if (isset($this->cache['getIpDetails_'.$ip])) {
290
			return $this->cache['getIpDetails_'.$ip];
291
		}
292
		$this->connect();
293
		$this->cache['getIpDetails_'.$ip] = json_decode($this->soapClient->getIpDetails($this->getHash(), $ip), true);
294
		myadmin_log('fantastico', 'debug', json_encode($this->cache['getIpDetails_'.$ip]), __LINE__, __FILE__);
295
		return $this->cache['getIpDetails_'.$ip];
296
	}
297
298
	/**
299
	 * Fantastico::editIp()
300
	 * changes the IP address of a license
301
	 *
302
	 *
303
	 * Output Success
304
	 *
305
	 * Array
306
	 * (
307
	 *     ["ip"]=>"130.253.175.32"
308
	 *     ["new_ip"]=>"130.253.175.33"
309
	 *
310
	 * )
311
	 *
312
	 *
313
	 * Output Error
314
	 *
315
	 * Array
316
	 * (
317
	 *     [faultcode] => 1704
318
	 *     [fault ]=> "The new IP Address that you have specified is not a valid cPanel IP Address."
319
	 * )
320
	 *
321
	 * Array
322
	 * (
323
	 *     [faultcode] => 1705
324
	 *     [fault ]=> "The new IP Address that you have specified is not a valid cPanel IP Address."
325
	 * )
326
	 *
327
	 * Array
328
	 * (
329
	 *     [faultcode] => 1703
330
	 *     [fault ]=> "The IP Address that you have specified is not a valid VPS IP Address."
331
	 * )
332
	 *
333
	 * Array
334
	 * (
335
	 *     [faultcode] => 1804
336
	 *     [fault ]=> "The IP Address that you have specified already exists."
337
	 * )
338
	 *
339
	 * Array
340
	 * (
341
	 *     [faultcode] => 1801
342
	 *     [fault ]=> "The IP Address that you have specified does not exist."
343
	 * )
344
	 *
345
	 *
346
	 *
347
	 * Array
348
	 * (
349
	 *     [faultcode] => 1401
350
	 *     [fault ]=> "You are trying to access the API from a server whose IP Address is not authorized."
351
	 * )
352
	 *
353
	 * Array
354
	 * (
355
	 *     [faultcode] => 1302
356
	 *     [fault ]=> "You have specified an invalid hash."
357
	 * )
358
	 *
359
	 *
360
	 * @param mixed $ip old ip address currently licensed
361
	 * @param mixed $newip new ip address to change i tot
362
	 * @return array returns an array of ip and newip or a fault and faultcode
363
	 */
364
	public function editIp($ip, $newip) {
365
		if (!$this->valid_ip($ip)) {
366
			$response = ['faultcode' => 1, 'fault' => 'Invalid IP Address '.$ip];
367
		} elseif (!$this->valid_ip($newip)) {
368
			$response = ['faultcode' => 2, 'fault' => 'Invalid IP Address '.$newip];
369 View Code Duplication
		} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
370
			$this->connect();
371
			$response = json_decode($this->soapClient->editIp($this->getHash(), $ip, $newip), true);
372
			myadmin_log('fantastico', 'debug', json_encode($response), __LINE__, __FILE__);
373
			if (isset($response['fault '])) {
374
				$response['fault'] = $response['fault '];
375
				unset($response['fault ']);
376
			}
377
		}
378
		$this->cache = [];
379
		return $response;
380
	}
381
382
	/**
383
	 * Fantastico::addIp()
384
	 * adds a new license into the system
385
	 *
386
	 * Output Success
387
	 *
388
	 * Array
389
	 * (
390
	 *     ["ip"]=>"130.253.175.32"
391
	 *     ["id"]=>"112461"	 *
392
	 * )
393
	 *
394
	 *
395
	 * Output Error
396
	 *
397
	 * Array
398
	 * (
399
	 *     [faultcode] => 1704
400
	 *     [fault ]=> "The new IP Address that you have specified is not a valid cPanel IP Address."
401
	 * )
402
	 *
403
	 * Array
404
	 * (
405
	 *     [faultcode] => 1705
406
	 *     [fault ]=> "The new IP Address that you have specified is not a valid cPanel IP Address."
407
	 * )
408
	 *
409
	 * Array
410
	 * (
411
	 *     [faultcode] => 1703
412
	 *     [fault ]=> "The IP Address that you have specified is not a valid VPS IP Address."
413
	 * )
414
	 *
415
	 * Array
416
	 * (
417
	 *     [faultcode] => 1804
418
	 *     [fault ]=> "The IP Address that you have specified already exists."
419
	 * )
420
	 *
421
	 * Array
422
	 * (
423
	 *     [faultcode] => 1603
424
	 *     [fault ]=> "You are not allowed to add any more IP Addresses because you have reached your IP Address quota."
425
	 * )
426
	 *
427
	 * Array
428
	 * (
429
	 *     [faultcode] => 1401
430
	 *     [fault ]=> "You are trying to access the API from a server whose IP Address is not authorized."
431
	 * )
432
	 *
433
	 * Array
434
	 * (
435
	 *     [faultcode] => 1302
436
	 *     [fault ]=> "You have specified an invalid hash."
437
	 * )
438
	 *
439
	 *
440
	 * @param string $ip ip address
441
	 * @param integer $type license type
442
	 * @return array response array containing a faultcode and fault, or ip and id on success
443
	 */
444
	public function addIp($ip, $type) {
445
		if (!$this->valid_ip($ip)) {
446
			$response = ['faultcode' => 1, 'fault' => 'Invalid IP Address '.$ip];
447 View Code Duplication
		} else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
448
			$this->connect();
449
			$response = json_decode($this->soapClient->addIp($this->getHash(), $ip, $type), true);
450
			myadmin_log('fantastico', 'debug', json_encode($response), __LINE__, __FILE__);
451
			if (isset($response['fault '])) {
452
				$response['fault'] = $response['fault '];
453
				unset($response['fault ']);
454
			}
455
		}
456
		$this->cache = [];
457
		return $response;
458
	}
459
460
	private function soapIpFunction($function, $ip) {
461
		if (!$this->valid_ip($ip)) {
462
			return array('faultcode' => 1, 'fault ' => 'Invalid IP Address '.$ip);
463
		}
464
		$this->connect();
465
		$response = json_decode($this->soapClient->$function($this->getHash(), $ip), true);
466
		myadmin_log('fantastico', 'debug', json_encode($response), __LINE__, __FILE__);
467
		$this->cache = array();
468
		return $response;
469
	}
470
471
	/**
472
	 * deactivateIp()
473
	 * Deactivates a Fantastico IP License
474
	 * Output Success
475
	 * Array
476
	 * (
477
	 *     [ipAddress] => 130.253.175.32
478
	 *     [addedOn] => 2010-03-01 00:00:00
479
	 *     [isVPS] => ( Yes || No )
480
	 *     [status] => ( Active || Inactive )
481
	 * )
482
	 *
483
	 * Output Error
484
	 * Array
485
	 * (
486
	 *     [faultcode] => 1801
487
	 *     [fault ]=> "The IP Address that you have specified does not exist."
488
	 * )
489
	 *
490
	 * @param mixed $ip
491
	 * @return void
492
	 */
493
	public function deactivateIp($ip) {
494
		return $this->soapIpFunction('deactivateIp', $ip);
495
	}
496
497
	/**
498
	 * reactivateIp()
499
	 * Deactivates a Fantastico IP License
500
	 * Output Success
501
	 * Array
502
	 * (
503
	 *     [ipAddress] => 130.253.175.32
504
	 *     [addedOn] => 2010-03-01 00:00:00
505
	 *     [isVPS] => ( Yes || No )
506
	 *     [status] => ( Active || Inactive )
507
	 * )
508
	 *
509
	 * Output Error
510
	 * Array
511
	 * (
512
	 *     [faultcode] => 1801
513
	 *     [fault ]=> "The IP Address that you have specified does not exist."
514
	 * )
515
	 *
516
	 * @param mixed $ip
517
	 * @return void
518
	 */
519
	public function reactivateIp($ip) {
520
		return $this->soapIpFunction('reactivateIp', $ip);
521
	}
522
523
	/**
524
	 * deleteIp()
525
	 * Deletes a Fantastico IP License
526
	 *
527
	 * Output Success
528
	 *
529
	 * Array
530
	 * (
531
	 *     [ip] => 130.253.175.32
532
	 *     [deleted] => "Yes"
533
	 * )
534
	 *
535
	 * Output Error
536
	 *
537
	 * Array
538
	 * (
539
	 *     [faultcode] => 1801
540
	 *     [fault ]=> "The IP Address that you have specified does not exist."
541
	 * )
542
	 *
543
	 * @param mixed $ip
544
	 * @return void
545
	 */
546
	public function deleteIp($ip) {
547
		return $this->soapIpFunction('deleteIp', $ip);
548
	}
549
550
}
551