Skip to content
This repository has been archived by the owner on Apr 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #139 from JonTheNiceGuy/Issue119
Browse files Browse the repository at this point in the history
Issue119
  • Loading branch information
JonTheNiceGuy committed Jan 7, 2013
2 parents c8bdc40 + 0800bc3 commit 89b82fb
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,3 +14,4 @@
/Media/images/
/Media/sketchdock-*/
/config/default.php
/config/gammu.php
14 changes: 10 additions & 4 deletions SETUP/install.php
Expand Up @@ -98,7 +98,7 @@
'gammuport' => '',
'gammudatabase' => 'gammu',
'gammudevice' => '/dev/ttyUSB0',
'gammuservice' => 'Generic',
'gammuservice' => 'SMSD',
'twitterenable' => '1',
'twitterconsumerkey' => '',
'twitterconsumersecret' => '',
Expand Down Expand Up @@ -553,7 +553,7 @@
'Connection = at',
'',
'[smsd]',
'PhoneID = phone' . $arrConfig['gammuservice'],
'PhoneID = Glue_Gammu-' . $arrConfig['gammuservice'],
'CommTimeout = 30',
'DeliveryReport = sms',
'',
Expand All @@ -568,7 +568,9 @@
'debuglevel = 3',
';";'
);
file_put_contents($_SERVER['HOME'].'/phone'.$id.'.gammu', implode("\n", $contents));
if(file_put_contents(dirname(__FILE__) . '/../config/gammu.php', implode("\n", $contents)) === false) {
echo "Unable to write the config file to the file system. Please create the following file:\r\n\r\n" . implode("\n", $contents);
}
$sql = 'INSERT INTO secureconfig (`key`, `value`) VALUES '
. "('Glue_Gammu-{$arrConfig['gammuservice']}_DBType', '{$arrConfig['gammutype']}'), "
. "('Glue_Gammu-{$arrConfig['gammuservice']}_DBHost', '{$arrConfig['gammuhost']}'), "
Expand All @@ -578,8 +580,10 @@
. "('Glue_Gammu-{$arrConfig['gammuservice']}_DBBase', '{$arrConfig['gammudatabase']}')";
mysql_query($sql, $coredb);
}
echo "Done (to: " . dirname(__FILE__).'/../config/gammu.php' . ")\r\n";
} else {
echo "Skipped";
}
echo "Done\r\n";

echo "\r\n(8/9) Configuring Twitter API access: ";
if ($arrConfig['twitterenable'] == 1
Expand Down Expand Up @@ -650,6 +654,8 @@ function force_readline_reverse($string) {
$arrConfig['forceyes'] = 1;
$return = force_readline($string);
$arrConfig['forceyes'] = -1;
} else {
$return = force_readline($string);
}
return $return;
}
Expand Down
148 changes: 143 additions & 5 deletions Tests/classes/Glue/GammuTest.php
Expand Up @@ -49,7 +49,7 @@ public function testMessageInjectionWorks()
$this->assertTrue(count($result) == 1);
$this->assertTrue($result[0]['SenderNumber'] == "+447777777777");
$this->assertTrue($result[0]['TextDecoded'] == "testing");
$this->assertTrue($result[0]['RecipientID'] == "Glue-Gammu-Provider");
$this->assertTrue($result[0]['RecipientID'] == "Glue_Gammu-SMSD");
$this->assertTrue($result[0]['Processed'] == "false");
Glue_GammuTestable::injectInboxItem($this->objGlueGammu, '+447777777777', 'testing 2');
$qry2 = $this->objDbGammu->prepare("SELECT * FROM inbox");
Expand All @@ -76,8 +76,105 @@ public function testProcessInbox()
$this->assertTrue(is_object($arrInput[1]));
$this->assertTrue($arrInput[1]->getKey('textMessage') == 'testing');
$this->assertTrue($arrInput[1]->getKey('strSender') == '+447777777777');
$this->assertTrue($arrInput[1]->getKey('strInterface') == 'Gammu-private_Glue-Gammu-Provider');
$this->assertTrue($arrInput[1]->getKey('strInterface') == 'Glue_Gammu-SMSD');
}

public function testReplyToInbox()
{
$arrOutbox = Glue_GammuTestable::checkOutboxItem($this->objGlueGammu);
$this->assertTrue(count($arrOutbox) == 0);
Glue_GammuTestable::injectInboxItem($this->objGlueGammu, '+447777777777', 'testing');
$this->objGlueGammu->read_private();
$arrInput = Object_Input::brokerAll();
$this->assertTrue(count($arrInput) == 1);
$this->assertTrue(is_object($arrInput[1]));
Object_Output::replyToInput($arrInput[1], "Test Succeeded");
$system_state = Object_User::isSystem();
Object_User::isSystem(true);
$arrGlue = Glue_GammuTestable::brokerAllGlues();
foreach ($arrGlue as $objGlue) {
$objGlue->send();
}
Object_User::isSystem($system_state);
$arrOutbox2 = Glue_GammuTestable::checkOutboxItem($this->objGlueGammu);
$this->assertTrue(count($arrOutbox2) == 1);
$this->assertTrue($arrOutbox2[0]['DestinationNumber'] == "+447777777777");
$this->assertTrue($arrOutbox2[0]['TextDecoded'] == "Test Succeeded");
$this->assertTrue($arrOutbox2[0]['SenderID'] == "Glue_Gammu-SMSD");
}


public function testSendMessage()
{
$arrOutbox = Glue_GammuTestable::checkOutboxItem($this->objGlueGammu);
$this->assertTrue(count($arrOutbox) == 0);
$system_state = Object_User::isSystem();
Object_User::isSystem(true);
$arrGlue = Glue_GammuTestable::brokerAllGlues();
foreach ($arrGlue as $objGlue) {
$this->assertTrue($objGlue->getGlue() == "Glue_Gammu-SMSD");
$strInterface = $objGlue->canSendPrivateMessage();
if ($strInterface != false) {
$output = new Object_Output();
$output->setKey('textMessage', "Testing 123");
$output->setKey('strInterface', $strInterface);
$output->setKey('strReceiver', '+447777777777');
$output->create();
$objGlue->send();
}
}
Object_User::isSystem($system_state);
$arrOutbox2 = Glue_GammuTestable::checkOutboxItem($this->objGlueGammu);
$this->assertTrue(count($arrOutbox2) == 1);
$this->assertTrue($arrOutbox2[0]['DestinationNumber'] == "+447777777777");
$this->assertTrue($arrOutbox2[0]['TextDecoded'] == "Testing 123");
$this->assertTrue($arrOutbox2[0]['SenderID'] == "Glue_Gammu-SMSD");
}

public function testSendLongerMessage()
{
$arrOutbox = Glue_GammuTestable::checkOutboxItem($this->objGlueGammu);
$this->assertTrue(count($arrOutbox) == 0);
$system_state = Object_User::isSystem();
Object_User::isSystem(true);
$arrGlue = Glue_GammuTestable::brokerAllGlues();
foreach ($arrGlue as $objGlue) {
$this->assertTrue($objGlue->getGlue() == "Glue_Gammu-SMSD");
$strInterface = $objGlue->canSendPrivateMessage();
if ($strInterface != false) {
$output = new Object_Output();
$output->setKey('textMessage', "123456789012345678901234567890"
."123456789012345678901234567890"
."123456789012345678901234567890"
."123456789012345678901234567890"
."123456789012345678901234567890"
."123456789012345678901234567890");
$output->setKey('strInterface', $strInterface);
$output->setKey('strReceiver', '+447777777777');
$output->create();
$objGlue->send();
}
}
Object_User::isSystem($system_state);

$arrOutbox2 = Glue_GammuTestable::checkOutboxItem($this->objGlueGammu);
$arrOutbox3 = Glue_GammuTestable::checkOutboxMultipart($this->objGlueGammu);
$this->assertTrue(count($arrOutbox2) == 1);
$this->assertTrue($arrOutbox2[0]['DestinationNumber'] == "+447777777777");
$this->assertTrue($arrOutbox2[0]['TextDecoded'] == "123456789012345678901234567890"
."123456789012345678901234567890"
."123456789012345678901234567890"
."123456789012345678901234567890"
."123456789012345678901234567890"
."1234567890");
$this->assertTrue($arrOutbox2[0]['SenderID'] == "Glue_Gammu-SMSD");
$this->assertTrue(substr($arrOutbox2[0]['UDH'], -4) === '2010');
$this->assertTrue(substr($arrOutbox2[0]['UDH'], 0, 10) === substr($arrOutbox3[0]['UDH'], 0, 10));
$this->assertTrue(substr($arrOutbox3[0]['UDH'], -4) === '2020');
$this->assertTrue($arrOutbox3[0]['TextDecoded'] == "12345678901234567890");
$this->assertTrue(substr($arrOutbox2[0]['UDH'], 0, 10) === substr($arrOutbox3[0]['UDH'], 0, 10));
}

}

class Glue_GammuTestable extends Glue_Gammu
Expand Down Expand Up @@ -109,10 +206,40 @@ public static function injectInboxItem(
}
$inject = $self->objDbGammu->prepare("INSERT INTO `inbox` (`UpdatedInDB`, `ReceivingDateTime`, `Text`, `SenderNumber`, `Coding`, `UDH`, `SMSCNumber`, `Class`, `TextDecoded`, `ID`, `RecipientID`, `Processed`) VALUES
(:datereceived, :dateinserted, '', :number, 'Default_No_Compression', '',
'+447771234567', -1, :message, NULL, 'Glue-Gammu-Provider', :actioned)");
'+447771234567', -1, :message, NULL, 'Glue_Gammu-SMSD', :actioned)");
$inject->execute(array('datereceived' => $datereceived, 'dateinserted' => $dateinserted, 'number' => $number, 'message' => $message, 'actioned' => $actioned));
return $self;
}

/**
* Gets a collection of data from the Outbox storage
*
* @param Glue_Gammu $self
*
* @return array
*/
public static function checkOutboxItem($self)
{
$read = $self->objDbGammu->prepare("SELECT * FROM `outbox`");
$read->execute();
$result = $read->fetchAll(PDO::FETCH_ASSOC);
return $result;
}

/**
* Gets a collection of data from the Outbox storage
*
* @param Glue_Gammu $self
*
* @return array
*/
public static function checkOutboxMultipart($self)
{
$read = $self->objDbGammu->prepare("SELECT * FROM `outbox_multipart`");
$read->execute();
$result = $read->fetchAll(PDO::FETCH_ASSOC);
return $result;
}

/**
* Return the DB object
Expand All @@ -139,7 +266,7 @@ public static function initialize($path = '')
$path = dirname(__FILE__) . '/../../../config/unittest_gammu.sqlite';
}
$arrConfigValues = array();
$arrConfigValues['GluePrefix'] = "Gammu";
$arrConfigValues['GluePrefix'] = "Glue_Gammu-SMSD";
$arrConfigValues['DBType'] = "sqlite";
$arrConfigValues['DBHost'] = $path;
if (file_exists($path)) {
Expand Down Expand Up @@ -214,7 +341,18 @@ public static function initialize($path = '')
Received INTEGER NOT NULL DEFAULT 0
);
");
$self->objDbGammu->exec("INSERT INTO `phones` (`ID`, `UpdatedInDB`, `InsertIntoDB`, `TimeOut`, `Send`, `Receive`, `IMEI`, `Client`, `Battery`, `Signal`, `Sent`, `Received`) VALUES ('Glue-Gammu-Provider', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d H:i:s") . "', 'yes', 'yes', '123456789012345', 'Dummy Client String', 0, 100, 0, 5);");
$self->objDbGammu->exec("INSERT INTO `phones` (`ID`, `UpdatedInDB`, `InsertIntoDB`, `TimeOut`, `Send`, `Receive`, `IMEI`, `Client`, `Battery`, `Signal`, `Sent`, `Received`) VALUES ('Glue_Gammu-SMSD', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d H:i:s") . "', 'yes', 'yes', '123456789012345', 'Dummy Client String', 0, 100, 0, 5);");
return $self;
}

public static function brokerAllGlues($path = '') {
if ($path == '') {
$path = dirname(__FILE__) . '/../../../config/unittest_gammu.sqlite';
}
$arrConfigValues = array();
$arrConfigValues['GluePrefix'] = "Glue_Gammu-SMSD";
$arrConfigValues['DBType'] = "sqlite";
$arrConfigValues['DBHost'] = $path;
return array(new Glue_Gammu($arrConfigValues));
}
}
82 changes: 60 additions & 22 deletions classes/Glue/Gammu.php
Expand Up @@ -125,7 +125,44 @@ public function __construct($arrConfigValues = array())
|| ($DBType != 'sqlite' && $DBPass == null)
|| ($DBType != 'sqlite' && $DBBase == null)
) {
throw new InvalidArgumentException("Insufficient detail to connect to Gammu Database");
$value = "\r\nDBType: $DBType (";
if ($DBType == null) {
$value .= "False";
} else {
$value .= "True";
}
$value .= ")\r\nDBHost: $DBHost (";
if ($DBHost == null) {
$value .= "False";
} else {
$value .= "True";
}
$value .= ")\r\nDBPort: $DBPort (";
if ($DBPort == null) {
$value .= "False";
} else {
$value .= "True";
}
$value .= ")\r\nDBUser: $DBUser (";
if ($DBUser == null) {
$value .= "False";
} else {
$value .= "True";
}
$value .= ")\r\nDBPass: $DBPass (";
if ($DBPass == null) {
$value .= "False";
} else {
$value .= "True";
}
$value .= ")\r\nDBBase: $DBBase (";
if ($DBBase == null) {
$value .= "False";
} else {
$value .= "True";
}
$value .= ")";
throw new InvalidArgumentException("Insufficient detail to connect to Gammu Database : $value");
}

$this->strInterface = $GluePrefix;
Expand Down Expand Up @@ -209,11 +246,10 @@ public function read_private()
foreach($UDH[$uniq] as $textPart) {
$text .= $textPart;
}
$strInterface = $this->strInterface . '-private_' . $message['RecipientID'];
$strSender = $message['SenderNumber'];
$textMessage = $text;
$intNativeID = $message['ID'];
Object_Input::import($strSender, $strInterface, $textMessage, $intNativeID);
Object_Input::import($strSender, $this->strInterface, $textMessage, $intNativeID);
$sqlUpdateInbox = "UPDATE inbox SET processed = 'true' WHERE UDH = ?";
$qryUpdateInbox = $this->objDbGammu->prepare($sqlUpdateInbox);
$qryUpdateInbox->execute(array('050003' . $uniq . '%'));
Expand All @@ -227,11 +263,10 @@ public function read_private()
$this->objDaemon->setKey('intUniqueCounter', $this->objDaemon->getKey('intUniqueCounter') + 1);
}

$strInterface = $this->strInterface . '-private_' . $message['RecipientID'];
$strSender = $message['SenderNumber'];
$textMessage = $message['TextDecoded'];
$intNativeID = $message['ID'];
Object_Input::import($strSender, $strInterface, $textMessage, $intNativeID);
Object_Input::import($strSender, $this->strInterface, $textMessage, $intNativeID);
$sqlUpdateInbox = "UPDATE inbox SET processed = 'true' WHERE ID = ?";
$qryUpdateInbox = $this->objDbGammu->prepare($sqlUpdateInbox);
$qryUpdateInbox->execute(array($message['ID']));
Expand Down Expand Up @@ -286,12 +321,10 @@ public function send()
. '(:SequencePosition, :UDH, :TextDecoded, :ID, :Coding);';
$qryInsertLarge = $this->objDbGammu->prepare($sqlInsertLarge);

$messages = Object_Output::brokerByColumnSearch('isActioned', 0);
$messages = Object_Output::brokerByColumnSearch('isActioned', false);
foreach ($messages as $message) {
if (preg_match('/^([^-]+)/', $message->getKey('strInterface'), $matches) == 1) {
if ($matches[1] != $this->strInterface) {
continue;
}
if ($message->getKey('strInterface') != $this->strInterface) {
continue;
}
if ($message->getKey('strReceiver') == null || $message->getKey('strReceiver') == '') {
$message->setKey('strError', 'No recipient');
Expand All @@ -303,10 +336,10 @@ public function send()
// Reset the insert data
$data = array(
'CreatorID' => 'CFM2',
'MultiPart' => null,
'MultiPart' => false,
'DestinationNumber' => $message->getKey('strReceiver'),
'UDH' => null,
'TextDecoded' => null,
'TextDecoded' => '',
'Coding' => 'Default_No_Compression',
'SenderID' => null
);
Expand All @@ -319,11 +352,12 @@ public function send()
// know which is which... which is where the SenderID comes into
// play.

if (preg_match('/^([^-]+)-([^_]+)_(.*)$/', $message->getKey('strInterface'), $matches) == 1) {
if (preg_match('/^Glue_Gammu-([^_]+)$/', $message->getKey('strInterface'), $matches) == 1) {
$data['CreatorID'] .= '_' . $matches[1];
$data['SenderID'] = $matches[3];
} elseif (preg_match('/^([^-]+)$/', $message->getKey('strInterface'), $matches) == 1) {
$data['SenderID'] = 'Glue_Gammu-' . $matches[1];
} elseif (preg_match('/^Glue_Gammu-([^_]+)_(.*)$/', $message->getKey('strInterface'), $matches) == 1) {
$data['CreatorID'] .= '_' . $matches[1];
$data['SenderID'] = 'Glue_Gammu-' . $matches[1];
}

// So, now we've got our defaults set up - let's check whether this
Expand All @@ -333,19 +367,23 @@ public function send()
$data['TextDecoded'] = $message->getKey('textMessage');
$qryInsert->execute($data);
} else {
$uniq = str_pad(dechex(rand(0, 255)), 2, "0");
$size = str_pad(dechex(count($arrMessageChunks)), 2, "0");
foreach ($arrMessageChunks as $intChunkID => $strChunk) {
$intChunkID++;
$uniq = str_pad(dechex(rand(0, 255)), 2, "0");
$size = str_pad(dechex(count($arrMessageChunks)), 2, "0");
$mNum = str_pad(dechex($intChunkID), 2, "0");
$data['UDH'] = '050003' . $uniq . $size . $mNum;
$data['TextDecoded'] = $strChunk;
if ($intChunkID == 1) {
$data['TextDecoded'] = $strChunk;
$qryInsert->execute($data);
$data['ID'] = $this->objDbGammu->lastInsertId();
$LargeData['ID'] = $this->objDbGammu->lastInsertId();
} else {
$data['SequencePosition'] = $intChunkID;
$qryInsertLarge->execute($data);
//(:SequencePosition, :UDH, :TextDecoded, :ID, :Coding)
$LargeData['UDH'] = $data['UDH'];
$LargeData['SequencePosition'] = $intChunkID;
$LargeData['TextDecoded'] = $strChunk;
$LargeData['Coding'] = $data['Coding'];
$qryInsertLarge->execute($LargeData);
}
}
}
Expand All @@ -355,7 +393,7 @@ public function send()
$this->objDaemon->setKey('lastUsedSuccessfully', date('Y-m-d H:i:s'));
$this->objDaemon->write();
} catch (Exception $e) {
error_log('Error moving data from CFM2 Outbox to Gammu Outbox: ' . $e->getMessage());
error_log('Error moving data from CFM2 Outbox to Gammu Outbox at ' . $e->getLine() . ': ' . $e->getMessage());
}
}
}
Expand Down

0 comments on commit 89b82fb

Please sign in to comment.