Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SmsSenderInterface #172

Merged
merged 8 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 89 additions & 89 deletions actions-sms.php

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions actions-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ function userbikes($userId)

function revert($userId, $bikeNum)
{
global $db;
global $db, $smsSender;

$standId = 0;
$result = $db->query("SELECT currentUser FROM bikes WHERE bikeNum=$bikeNum AND currentUser IS NOT NULL");
Expand Down Expand Up @@ -427,7 +427,7 @@ function revert($userId, $bikeNum)
$result = $db->query("INSERT INTO history SET userId=0,bikeNum=$bikeNum,action='RENT',parameter=$code");
$result = $db->query("INSERT INTO history SET userId=0,bikeNum=$bikeNum,action='RETURN',parameter=$standId");
response('<h3>' . _('Bicycle') . ' ' . $bikeNum . ' ' . _('reverted to') . ' <span class="label label-primary">' . $stand . '</span> ' . _('with code') . ' <span class="label label-primary">' . $code . '</span>.</h3>');
sendSMS($revertusernumber, _('Bike') . ' ' . $bikeNum . ' ' . _('has been returned. You can now rent a new bicycle.'));
$smsSender->send($revertusernumber, _('Bike') . ' ' . $bikeNum . ' ' . _('has been returned. You can now rent a new bicycle.'));
} else {
response(_('No last stand or code for bicycle') . ' ' . $bikeNum . ' ' . _('found. Revert not successful!'), ERROR);
}
Expand Down Expand Up @@ -539,7 +539,7 @@ function checkprivileges($userid)

function smscode($number)
{
global $db, $gatewayId, $gatewayKey, $gatewaySenderNumber, $connectors;
global $db, $gatewayId, $gatewayKey, $gatewaySenderNumber, $connectors, $smsSender;
srand();

$number = normalizephonenumber($number);
Expand Down Expand Up @@ -570,7 +570,7 @@ function smscode($number)
if (DEBUG === true) {
response($number, 0, array('checkcode' => $checkcode, 'smscode' => $smscode, 'existing' => $userexists));
} else {
sendSMS($number, $text);
$smsSender->send($number, $text);
if (issmssystemenabled() == true) {
response($number, 0, array('checkcode' => $checkcode, 'existing' => $userexists));
} else {
Expand Down
2 changes: 1 addition & 1 deletion admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require('actions-web.php');

/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down
2 changes: 1 addition & 1 deletion agree.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require('actions-web.php');

/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down
2 changes: 1 addition & 1 deletion command.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require('actions-web.php');

/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down
74 changes: 22 additions & 52 deletions common.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use BikeShare\Mail\PHPMailerMailSender;
use BikeShare\Db\DbInterface;
use BikeShare\Db\MysqliDb;
use BikeShare\Sms\SmsSender;
use BikeShare\Sms\SmsSenderInterface;
use BikeShare\SmsConnector\DebugConnector;
use BikeShare\SmsConnector\SmsConnectorFactory;

require_once 'vendor/autoload.php';
Expand All @@ -21,10 +24,16 @@
DEBUG
);

/**
* @var DbInterface $db
*/
$db = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname);
$db->connect();

/**
* @var MailSenderInterface $mailer
*/
if (DEBUG===TRUE) {
if (DEBUG === TRUE) {
$mailer = new DebugMailSender();
} else {
$mailer = new PHPMailerMailSender(
Expand All @@ -35,6 +44,14 @@
);
}

/**
* @var SmsSenderInterface $smsSender
*/
$smsSender = new SmsSender(
DEBUG === TRUE ? new DebugConnector() : $sms,
$db
);


function error($message)
{
Expand All @@ -43,53 +60,6 @@ function error($message)
exit($message);
}

function sendSMS($number,$text)
{

global $sms;

$message = $text;
if (strlen($message) > 160) {
$message = chunk_split($message, 160, '|');
$message = explode('|', $message);
foreach ($message as $text) {
$text = trim($text);
if ($text) {
logSendsms($number, $text);
if (DEBUG === true) {
echo $number, ' -&gt ', $text, '<br />';
} else {
$sms->send($number, $text);
}
}
}
} else {
logSendsms($number, $text);
if (DEBUG === true) {
echo $number, ' -&gt ', $text, '<br />';
} else {
$sms->send($number, $text);
}
}
}

function logSendsms($number, $text)
{
global $dbserver, $dbuser, $dbpassword, $dbname;
/**
* @var DbInterface
*/
$localdb = new MysqliDb($dbserver, $dbuser, $dbpassword, $dbname);
$localdb->connect();

#TODO does it needed???
$localdb->setAutocommit(true);
$number = $localdb->escape($number);
$text = $localdb->escape($text);

$result = $localdb->query("INSERT INTO sent SET number='$number',text='$text'");

}

function generatecodes($numcodes,$codelength,$wastage=25)
{
Expand Down Expand Up @@ -288,12 +258,12 @@ function checkstandname($stand)
**/
function notifyAdmins($message, $notificationtype = 0)
{
global $db, $systemname, $watches, $mailer;
global $db, $systemname, $watches, $mailer, $smsSender;

$result = $db->query('SELECT number,mail FROM users where privileges & 2 != 0');
while ($row = $result->fetch_assoc()) {
if ($notificationtype == 0) {
sendSMS($row['number'], $message);
$smsSender->send($row['number'], $message);
$mailer->send($watches['email'], $systemname . ' ' . _('notification'), $message);
} else {
$mailer->send($row['mail'], $systemname . ' ' . _('notification'), $message);
Expand Down Expand Up @@ -373,7 +343,7 @@ function checktopofstack($standid)

function checklongrental()
{
global $db, $watches, $notifyuser;
global $db, $smsSender, $watches, $notifyuser;

$abusers = '';
$found = 0;
Expand All @@ -392,7 +362,7 @@ function checklongrental()
$abusers .= ' b' . $bikenum . ' ' . _('by') . ' ' . $username . ',';
$found = 1;
if ($notifyuser) {
sendSMS($userphone, _('Please, return your bike ') . $bikenum . _(' immediately to the closest stand! Ignoring this warning can get you banned from the system.'));
$smsSender->send($userphone, _('Please, return your bike ') . $bikenum . _(' immediately to the closest stand! Ignoring this warning can get you banned from the system.'));
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions config.php.example
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ $connectors["sms"]=""; // API connector used for SMS operations (connectors/ dir
$connectors["config"]["disabled"]="{}"; //json string for configuration of sms service
$countrycode=""; // international dialing code (country code prefix), no plus sign

$cities = ['Bratislava']; //avalible in cities
$citiesGPS = [
'Bratislava' => ['48.148154', '17.117232']
];

/*** geoJSON files - uncomment line below to use, any number of geoJSON files can be included ***/
// $geojson[]="http://example.com/poi.json"; // example geojson file with points of interests to be displayed on the map

Expand Down
2 changes: 1 addition & 1 deletion cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require("actions-web.php");

/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down
1 change: 1 addition & 0 deletions docker-data/mysql/create-database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ CREATE TABLE `users` (
`mail` varchar(30) NOT NULL,
`number` varchar(30) NOT NULL,
`privileges` int(11) NOT NULL DEFAULT '0',
`city` varchar(45) NOT NULL DEFAULT 'Bratisalva',
PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require "actions-web.php";

/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down
2 changes: 1 addition & 1 deletion install/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}

/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down
10 changes: 5 additions & 5 deletions install/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function return_bytes($val) {
$newconfig=implode($configfile);
file_put_contents($configfilename,$newconfig);
/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($_POST["dbserver"],$_POST["dbuser"],$_POST["dbpassword"],$_POST["dbname"]);
$db->connect();
Expand Down Expand Up @@ -239,7 +239,7 @@ function return_bytes($val) {
<?php endif; ?>
<?php if ($step==3):
/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand All @@ -263,7 +263,7 @@ function return_bytes($val) {
<?php endif; ?>
<?php if ($step==4):
/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down Expand Up @@ -329,7 +329,7 @@ function return_bytes($val) {
<?php endif; ?>
<?php if ($step==5):
/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down Expand Up @@ -401,7 +401,7 @@ function return_bytes($val) {
<?php endif; ?>
<?php if ($step==6):
/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down
4 changes: 2 additions & 2 deletions receive.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require("config.php");

/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand All @@ -25,7 +25,7 @@
if(!validateNumber($sms->getNumber()))
{
####
#sendSMS($sms->getNumber(),_('Your number is not registered.'));
#$smsSender->send($sms->getNumber(),_('Your number is not registered.'));
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
require('actions-qrcode.php');

/**
* @var DbInterface
* @var DbInterface $db
*/
$db=new MysqliDb($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
Expand Down
49 changes: 49 additions & 0 deletions src/Sms/SmsSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace BikeShare\Sms;

use BikeShare\Db\DbInterface;
use BikeShare\SmsConnector\SmsConnectorInterface;

class SmsSender implements SmsSenderInterface
{
/**
* @var SmsConnectorInterface
*/
private $smsConnector;
/**
* @var DbInterface
*/
private $db;

public function __construct(
SmsConnectorInterface $smsConnector,
DbInterface $db
) {
$this->smsConnector = $smsConnector;
$this->db = $db;
}

public function send($number, $message)
{
if (strlen($message) > 160) {
$messageParts = str_split($message, 160);
foreach ($messageParts as $text) {
$text = trim($text);
if ($text) {
$this->log($number, $text);
$this->smsConnector->send($number, $text);
}
}
} else {
$this->log($number, $message);
$this->smsConnector->send($number, $message);
}
}

private function log($number, $message)
{
$this->db->query("INSERT INTO sent SET number='$number', text='$message'");
$this->db->commit();
}
}
8 changes: 8 additions & 0 deletions src/Sms/SmsSenderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace BikeShare\Sms;

interface SmsSenderInterface
{
public function send($number, $message);
}
19 changes: 19 additions & 0 deletions src/SmsConnector/DebugConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace BikeShare\SmsConnector;

class DebugConnector extends AbstractConnector
{
public function checkConfig(array $config)
{
}

public function respond()
{
}

public function send($number, $text)
{
echo $number . ' -&gt ' . $text . PHP_EOL;
}
}
Loading