The SSH host key has changed on 8 April, 2022 to this one: SHA256:573uTBSeh74kvOo0HJXi5ijdzRm8me27suzNEDlGyrQ
1
0
Fork 0
A JSON-RPC based SMS Gateway
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
smsgateway/src/SmsGateway/Sender/FileSender.php

74 lines
2.0 KiB

<?php
namespace SmsGateway\Sender;
use SmsGateway\SenderInterface;
use SmsGateway\LoggerInterface;
/**
* Description of FileSender
*
* @author Gergely Polonkai
*/
class FileSender implements SenderInterface
{
private $messageDir;
/**
* @var SmsGateway\LoggerInterface $logger
*/
private $logger;
public function __construct($messageDir)
{
if (file_exists($messageDir) && !is_dir($messageDir)) {
throw new \InvalidArgumentException('Message directory specified is not a directory!');
}
if (!file_exists($messageDir) && !is_writable(dirname($messageDir))) {
throw new \RuntimeException('Message directory cannot be created');
}
if (!file_exists($messageDir)) {
mkdir($messageDir, 0777, true);
}
if (!is_writable($messageDir)) {
throw new \RuntimeException('Message directory is not writable!');
}
$this->messageDir = $messageDir;
}
public function setLogger(LoggerInterface $logger) {
if ($logger === null) {
throw new \InvalidArgumentException('A logger must be passed to the authenticator!');
}
$this->logger = $logger;
}
public function getLogger() {
return $this->logger;
}
public function send($username, $recipient, $message, $passwordLocations)
{
$rcptDir = $this->messageDir . '/' . $recipient;
if (file_exists($rcptDir) && (!is_writable($rcptDir) || !is_dir($rcptDir))) {
throw new \RuntimeException('Message directory is not writable!');
}
if (!file_exists($rcptDir)) {
mkdir($rcptDir);
}
$messageFileName = date('YmdHis') . '-' . uniqid() . '.sms';
$fd = fopen($rcptDir . '/' . $messageFileName, 'w');
fwrite($fd, $message);
fclose($fd);
$this->logger->messageLog($username, $recipient, $message, $passwordLocations);
return true;
}
}