Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 1d835bf

Browse files
author
Chris Board
committed
Bug fixes and PHP 7.2 Ready
Implemented some bug fixes that were found durinig testing of PHP 7.2. Added changes to the encryption/decryption methods to remove mcrypt as has been removed in PHP 7.2.
1 parent 0084de7 commit 1d835bf

16 files changed

Lines changed: 220 additions & 249 deletions

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 134 additions & 179 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

APIManager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,3 @@ function decryptPostArray($postArray)
105105
}
106106

107107
}
108-
?>

CommonTasks.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ function getIP()
2020
}
2121
return $ip;
2222
}
23-
23+
2424
/**
2525
* Will generate a random password at the specified length. If length
2626
* omitted then it defaults to false
2727
* @param int $length
28+
* @param null $possible
2829
* @return string
2930
*/
3031
function generateRandomString($length = 6, $possible = null)
@@ -144,11 +145,11 @@ function getDBDateTime()
144145

145146
return "$date $time";
146147
}
147-
148+
148149
/**
149150
* Returns a long value of an epoch time stamp, this is based on the
150151
* current time
151-
* @return long
152+
* @return int
152153
*/
153154
function getEpochTimeStamp()
154155
{
@@ -194,5 +195,4 @@ function findDateInSpecificFormat($format=null)
194195
return $returnedDate;
195196
}
196197
}
197-
}
198-
?>
198+
}

ConnectionManager.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,9 @@ public function getDatabases($postArray)
243243
/**
244244
* Returns a JSON array to retrieve all of the databases that are availabe on the connection
245245
* and calls a function to also get the tables from each database
246-
* @param type $postArray The post array that contains that connection details
246+
* @param array $postArray The post array that contains that connection details
247247
* @deprecated This replaced with two separate API calls GetDatabases and GetTables
248+
* @throws Exception
248249
*/
249250
function retrieveDBsAndTables($postArray)
250251
{
@@ -427,15 +428,18 @@ function connectToDBFromPostArray($postArray)
427428
return $this->connectToDB($server, $username, $password, $database, $port, $localTunnelPort, $postArray);
428429
}
429430
}
430-
431+
431432
/**
432433
* Performs the actual MySQL Connection to the server
433-
* @param type $server The MySQL Server to connect to
434-
* @param type $username The username used for the database connection
435-
* @param type $password The password used for the database connection
436-
* @param type $database The database to use, can be null (no default database will be selected
437-
* @param type $port The port number that should used
438-
* @return type Array that details the connection state
434+
* @param string $server The MySQL Server to connect to
435+
* @param string $username The username used for the database connection
436+
* @param string $password The password used for the database connection
437+
* @param string $database The database to use, can be null (no default database will be selected
438+
* @param string $port The port number that should used
439+
* @param string $localTunnelPort
440+
* @param null $postArray
441+
* @return array that details the connection state
442+
* @throws Exception
439443
*/
440444
function connectToDB($server, $username, $password, $database, $port, $localTunnelPort = "-1", $postArray = null)
441445
{
@@ -539,11 +543,12 @@ function connectToDB($server, $username, $password, $database, $port, $localTunn
539543
}
540544
return $status;
541545
}
542-
546+
543547
/**
544548
* Gets the tables from the specified database
545-
* @param type $db The database name where the tables should be retrieved from
546-
* @return type An array detailing the error if the tables couldn't be retrieved, or an array of the tables that were retrieved
549+
* @param string $db The database name where the tables should be retrieved from
550+
* @return array An array detailing the error if the tables couldn't be retrieved, or an array of the tables that were retrieved
551+
* @throws Exception
547552
*/
548553
function getTables($db)
549554
{
@@ -571,7 +576,6 @@ function getTables($db)
571576
HelperClass::printResponseInCorrectEncodingAndCloseTunnelIfNeeded($this->postArray, $status);
572577
exit(); //MySQL Query Failed
573578
}
574-
return $tables;
575579
}
576580

577581
/**
@@ -589,5 +593,3 @@ function handleError($error, $errorCode)
589593
return $status;
590594
}
591595
}
592-
593-
?>

DBHelper.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ function closeMysqlIDBConnection($conn)
7474
}
7575
}
7676
}
77-
77+
7878
/**
79-
* Connects to a database from config array, using ConfigManager class and get the array
79+
* Connects to a database from config array, using ConfigManager class and get the array
8080
* @param array $configArray Array values from the config file created from ConfigManager.php
8181
* @return MySQLConnection A valid MySQL Connection Resouce
8282
* @deprecated since version 1.0.0.5
83+
* @throws Exception
8384
*/
8485
function connectToDBFromConfigArray($configArray)
8586
{
@@ -96,15 +97,15 @@ function connectToDBFromConfigArray($configArray)
9697

9798
return $this->connectToDB($server, $username, $password, $port, $database);
9899
}
99-
100+
100101
/**
101102
* Connects to a database using individual variables for username, password and server etc
102103
* @param String $server The server to connect to
103104
* @param String $username The username to connect to the server
104105
* @param String $password The password to connect to the server
105106
* @param int $port The port to connect to the server
106-
* @param String $database
107-
* @return MySQLConnection Valid MySQL Connection resource
107+
* @param String $database
108+
* @return resource Valid MySQL Connection resource
108109
* @throws Exception Thrown when failed to connect to database
109110
*/
110111
function connectToDB($server, $username, $password, $port, $database)
@@ -234,6 +235,4 @@ function getTheNextIDFromDBToUse($database, $table, $error)
234235
return -1;
235236
}
236237
}
237-
}
238-
239-
?>
238+
}

Encryption.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
class Encryption
1010
{
11+
private $php_version;
1112
private $cipher = null;
1213
private $iv = null;
1314
private $previousCipher = null;
1415
private $previousIV = null;
1516
public function __construct()
1617
{
18+
$this->php_version = phpversion();
1719
$configManager = new ConfigManager("mysql.conf");
1820
$this->cipher = $configManager->getConfigItemValue("encryption", "cipher", null);
1921
if ($this->cipher == null || empty($this->cipher))
@@ -34,11 +36,14 @@ function resetKeysToDefaultKeys()
3436

3537
function encrypt($data)
3638
{
37-
38-
//$iv = "ryojvlzmdalyglrj";
39-
//$key = CIPHERKEY;
40-
41-
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->cipher, $this->addpadding($data), MCRYPT_MODE_CBC, $this->iv));
39+
if ($this->version[0] === "5")
40+
{
41+
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $this->cipher, $this->addpadding($data), MCRYPT_MODE_CBC, $this->iv));
42+
}
43+
else
44+
{
45+
return base64_encode(openssl_encrypt($data, 'AES-256-CBC', $this->cipher, OPENSSL_RAW_DATA, $this->iv));
46+
}
4247
}
4348

4449
private function addpadding($string, $blocksize = 16)
@@ -67,15 +72,21 @@ function decrypt($data)
6772
{
6873
try
6974
{
70-
71-
$base64Decoded = @base64_decode($data);
72-
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->cipher, $base64Decoded, MCRYPT_MODE_CBC, $this->iv);
73-
return $this->strippadding($decrypted);
75+
if ($this->version[0] === "5")
76+
{
77+
$decoded = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->cipher, $data, MCRYPT_MODE_CBC, $this->iv);
78+
$base64Decoded = @base64_decode($data);
79+
$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $this->cipher, $base64Decoded, MCRYPT_MODE_CBC, $this->iv);
80+
return $this->strippadding($decrypted);
81+
}
82+
else
83+
{
84+
return $this->strippadding(openssl_decrypt(base64_decode($data), 'AES-256-CBC', $this->cipher, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->iv));
85+
}
7486
}
7587
catch (Exception $e)
7688
{
7789
throw new Exception("Failed to decrypt");
7890
}
7991
}
8092
}
81-
?>

HelperClass.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ function returnTunnelError($errorMessage)
3838
$array[MESSAGE] = $errorMessage;
3939
return $array;
4040
}
41-
41+
4242
/**
43-
* Prints out the output to be picked up by Android in the correct format.
43+
* Prints out the output to be picked up by Android in the correct format.
4444
* If using version 1.2.0.0 or above then the output is encrypted before it is sent
4545
* back, older versions (older versions don't post the version) revert back to
4646
* the previous behaviour and the output is not encrypted
4747
* @param type $postArray The array that was posted from android (includes the version)
4848
* @param type $response The response that should be json_encoded and possibly encrypted depending on version
49+
* @throws Exception
4950
*/
5051
public static function printResponseInCorrectEncodingAndCloseTunnelIfNeeded($postArray, $response)
5152
{
@@ -70,9 +71,6 @@ public static function printResponseInCorrectEncodingAndCloseTunnelIfNeeded($pos
7071
}
7172
else
7273
{
73-
//Temporary test for older application backward compatibility testing
74-
//TODO: REMOVE THE ENCRYPTION
75-
//print json_encode($encryption->decrypt($response));
7674
if (isset($response->data))
7775
{
7876
print json_encode($response->data);
@@ -111,5 +109,3 @@ public static function getIP()
111109
return $ip;
112110
}
113111
}
114-
115-
?>

Logger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct($logID = 0)
3939
$this->logHandle = fopen($this->configManager->getConfigItemValue("debug", "debug_log", "api.log"), "a+");
4040
if (!$this->logHandle)
4141
{
42-
throw new Exception("Unable to open debug log file: Error: " + error_get_last());
42+
throw new Exception("Unable to open debug log file: Error: " . error_get_last());
4343
}
4444
}
4545

@@ -54,7 +54,7 @@ public function getLogID()
5454

5555
/**
5656
* Write a message to the API log file. A new line will automatically be put on the end of the log message
57-
* @param $logMessage The message to be written to the file
57+
* @param string $logMessage The message to be written to the file
5858
*/
5959
public function writeToLog($logMessage)
6060
{

0 commit comments

Comments
 (0)