mirror of
https://github.com/ZeJMaN/LBCAlerte_ynh.git
synced 2025-07-17 00:50:49 +02:00
Initial commit
Functional, without SSO
This commit is contained in:
163
sources/lib/Message/Adapter/AdapterAbstract.php
Normal file
163
sources/lib/Message/Adapter/AdapterAbstract.php
Normal file
@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace Message\Adapter;
|
||||
|
||||
abstract class AdapterAbstract
|
||||
{
|
||||
protected $curl;
|
||||
|
||||
protected $notify_url;
|
||||
|
||||
protected $token;
|
||||
|
||||
protected $title;
|
||||
|
||||
protected $description;
|
||||
|
||||
protected $url;
|
||||
|
||||
protected $curl_options = array(
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HEADER => false,
|
||||
CURLOPT_POST => true,
|
||||
);
|
||||
|
||||
public function __construct(array $options = array())
|
||||
{
|
||||
$this->setOptions($options);
|
||||
}
|
||||
|
||||
public function setOptions(array $options)
|
||||
{
|
||||
foreach ($options AS $name => $value) {
|
||||
if (false !== strpos($name, "_")) {
|
||||
$method = "set".str_replace(" ", "", ucwords(str_replace("_", " ", $name)));
|
||||
} else {
|
||||
$method = "set".ucfirst($name);
|
||||
}
|
||||
if (method_exists($this, $method)) {
|
||||
$this->$method($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCurl(array $options = array())
|
||||
{
|
||||
if (!$this->curl) {
|
||||
$this->curl = curl_init();
|
||||
if ($this->curl_options) {
|
||||
curl_setopt_array($this->curl, $this->curl_options);
|
||||
}
|
||||
if ($this->notify_url) {
|
||||
curl_setopt($this->curl, CURLOPT_URL, $this->notify_url);
|
||||
}
|
||||
}
|
||||
if ($options) {
|
||||
curl_setopt_array($this->curl, $options);
|
||||
}
|
||||
return $this->curl;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->curl) {
|
||||
curl_close($this->curl);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @return Message_Abstract
|
||||
*/
|
||||
public function setNotifyUrl($url)
|
||||
{
|
||||
$this->notify_url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getNotifyUrl()
|
||||
{
|
||||
return $this->notify_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $token
|
||||
* @return Message_Abstract
|
||||
*/
|
||||
public function setToken($token)
|
||||
{
|
||||
$this->token = $token;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getToken()
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return Message_Abstract
|
||||
*/
|
||||
public function setTitle($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTitle()
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $description
|
||||
* @return Message_Abstract
|
||||
*/
|
||||
public function setDescription($description)
|
||||
{
|
||||
$this->description = $description;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDescription()
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @return Message_Abstract
|
||||
*/
|
||||
public function setUrl($url)
|
||||
{
|
||||
$this->url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
|
||||
abstract public function send($message);
|
||||
}
|
69
sources/lib/Message/Adapter/NotifyMyAndroid.php
Normal file
69
sources/lib/Message/Adapter/NotifyMyAndroid.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Message\Adapter;
|
||||
|
||||
class NotifyMyAndroid extends AdapterAbstract
|
||||
{
|
||||
protected $notify_url = "https://www.notifymyandroid.com/publicapi/notify";
|
||||
|
||||
/**
|
||||
* Envoi un message.
|
||||
* @param string $msg
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send($message, array $options = array())
|
||||
{
|
||||
$message = trim($message);
|
||||
$this->setOptions($options);
|
||||
if (!$this->token || !$this->title) {
|
||||
throw new \Exception("Un des paramètres obligatoires est manquant", 400);
|
||||
}
|
||||
|
||||
$params = array(
|
||||
"apikey" => $this->getToken(),
|
||||
"application" => $this->getTitle(),
|
||||
"event" => $this->getDescription(),
|
||||
"description" => $message,
|
||||
);
|
||||
if ($url = $this->getUrl()) {
|
||||
$params["url"] = $url;
|
||||
}
|
||||
|
||||
$curl = $this->getCurl(array(
|
||||
CURLOPT_POSTFIELDS => http_build_query($params)
|
||||
));
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
||||
if ($response === false) {
|
||||
throw new \Exception("cURL Error: " . curl_error($curl));
|
||||
}
|
||||
|
||||
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
if ($code == 200) {
|
||||
// find error through content
|
||||
|
||||
}
|
||||
if ($code != 200) {
|
||||
switch ($code) {
|
||||
case 400:
|
||||
$msg = "The data supplied is in the wrong format, invalid length or null.";
|
||||
break;
|
||||
case 401:
|
||||
$msg = "None of the API keys provided were valid.";
|
||||
break;
|
||||
case 402:
|
||||
$msg = "Maximum number of API calls per hour exceeded.";
|
||||
break;
|
||||
case 500:
|
||||
$msg = "Internal server error. Please contact our support if the problem persists.";
|
||||
break;
|
||||
default:
|
||||
$msg = "Unknow error.";
|
||||
}
|
||||
throw new \Exception($msg, $code);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
57
sources/lib/Message/Adapter/Pushbullet.php
Normal file
57
sources/lib/Message/Adapter/Pushbullet.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace Message\Adapter;
|
||||
|
||||
class Pushbullet extends AdapterAbstract
|
||||
{
|
||||
protected $notify_url = "https://api.pushbullet.com/v2/pushes";
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @throws \Exception
|
||||
* @return array
|
||||
*/
|
||||
public function send($message, array $options = array())
|
||||
{
|
||||
$this->setOptions($options);
|
||||
$message = trim($message);
|
||||
if (!$this->token) {
|
||||
throw new \Exception("Un des paramètres obligatoires est manquant", 400);
|
||||
}
|
||||
|
||||
$params = array(
|
||||
"type" => "note",
|
||||
"title" => $this->getTitle(),
|
||||
"body" => $message
|
||||
);
|
||||
if ($url = $this->getUrl()) {
|
||||
$params["type"] = "link";
|
||||
$params["url"] = $url;
|
||||
}
|
||||
|
||||
$params = json_encode($params);
|
||||
|
||||
$curl = $this->getCurl(array(
|
||||
CURLOPT_USERPWD => $this->getToken(),
|
||||
CURLOPT_HTTPHEADER => array(
|
||||
"Content-Type: application/json ",
|
||||
"Content-Length: ".strlen($params)
|
||||
),
|
||||
CURLOPT_POSTFIELDS => $params,
|
||||
));
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
||||
if ($response === false) {
|
||||
throw new \Exceptions("cURL Error: " . curl_error($curl));
|
||||
}
|
||||
|
||||
$json = json_decode($response, true);
|
||||
|
||||
if (400 <= $code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
|
||||
throw new \Exception($json["error"]["message"], $code);
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
}
|
67
sources/lib/Message/Adapter/Pushover.php
Normal file
67
sources/lib/Message/Adapter/Pushover.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Message\Adapter;
|
||||
|
||||
class Pushover extends AdapterAbstract
|
||||
{
|
||||
protected $notify_url = "https://api.pushover.net/1/messages.json";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $user_key;
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send($message, array $options = array())
|
||||
{
|
||||
$this->setOptions($options);
|
||||
$message = trim($message);
|
||||
if (!$this->token || !$this->user_key) {
|
||||
throw new \Exception("Un des paramètres obligatoires est manquant", 400);
|
||||
}
|
||||
$curl = $this->getCurl(array(
|
||||
CURLOPT_POSTFIELDS => array(
|
||||
"token" => $this->getToken(),
|
||||
"user" => $this->getUserkey(),
|
||||
"message" => $message,
|
||||
"title" => $this->getTitle(),
|
||||
"url" => $this->getUrl(),
|
||||
)
|
||||
));
|
||||
|
||||
$response = curl_exec($curl);
|
||||
|
||||
if ($response === false) {
|
||||
throw new \Exception("cURL Error: " . curl_error($curl));
|
||||
}
|
||||
|
||||
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
if (200 !== $code) {
|
||||
$response = json_decode($response, true);
|
||||
throw new \Exception("Errors:\n" . implode("\n", $response["errors"]));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $userkey
|
||||
* @return Pushover
|
||||
*/
|
||||
public function setUserkey($userkey)
|
||||
{
|
||||
$this->user_key = $userkey;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUserkey()
|
||||
{
|
||||
return $this->user_key;
|
||||
}
|
||||
}
|
83
sources/lib/Message/Adapter/SmsFreeMobile.php
Normal file
83
sources/lib/Message/Adapter/SmsFreeMobile.php
Normal file
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace Message\Adapter;
|
||||
|
||||
class SmsFreeMobile extends AdapterAbstract
|
||||
{
|
||||
protected $notify_url = "https://smsapi.free-mobile.fr/sendmsg";
|
||||
protected $user;
|
||||
protected $key;
|
||||
|
||||
protected $curl_options = array(
|
||||
CURLOPT_SSL_VERIFYPEER => false
|
||||
);
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send($message, array $options = array())
|
||||
{
|
||||
$this->setOptions($options);
|
||||
if (!$this->user || !$this->key) {
|
||||
throw new \Exception("Un des paramètres obligatoires est manquant", 400);
|
||||
}
|
||||
$message = trim($message);
|
||||
if ($url = $this->getUrl()) {
|
||||
$message .= ": ".$url;
|
||||
}
|
||||
$curl = $this->getCurl(array(
|
||||
CURLOPT_URL => $this->notify_url."?user=".$this->user.
|
||||
"&pass=".$this->key.
|
||||
"&msg=".urlencode($message)
|
||||
));
|
||||
curl_exec($curl);
|
||||
if (200 != $code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
|
||||
switch ($code) {
|
||||
case 400: $message = "Un des paramètres obligatoires est manquant."; break;
|
||||
case 402: $message = "Trop de SMS ont été envoyés en trop peu de temps."; break;
|
||||
case 403: $message = "Vous n'avez pas activé la notification SMS dans votre espace abonné Free Mobile ou votre identifiant/clé est incorrect."; break;
|
||||
case 500: $message = "erreur sur serveur Free Mobile."; break;
|
||||
default: $message = "erreur inconnue.";
|
||||
}
|
||||
throw new \Exception($message, $code);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $user
|
||||
* @return \Message\SMS\FreeMobile
|
||||
*/
|
||||
public function setUser($user)
|
||||
{
|
||||
$this->user = $user;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUser()
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return \Message\SMS\FreeMobile
|
||||
*/
|
||||
public function setKey($key)
|
||||
{
|
||||
$this->key = $key;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getKey()
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
}
|
142
sources/lib/Message/Adapter/SmsOvh.php
Normal file
142
sources/lib/Message/Adapter/SmsOvh.php
Normal file
@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace Message\Adapter;
|
||||
|
||||
class SmsOvh extends AdapterAbstract
|
||||
{
|
||||
protected $notify_url = "https://www.ovh.com/cgi-bin/sms/http2sms.cgi";
|
||||
protected $account;
|
||||
protected $login;
|
||||
protected $password;
|
||||
protected $from;
|
||||
protected $to;
|
||||
|
||||
protected $curl_options = array(
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HEADER => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* @param string $account
|
||||
* @return Ovh
|
||||
*/
|
||||
public function setAccount($account)
|
||||
{
|
||||
$this->account = $account;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAccount()
|
||||
{
|
||||
return $this->account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $login
|
||||
* @return Ovh
|
||||
*/
|
||||
public function setLogin($login)
|
||||
{
|
||||
$this->login = $login;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogin()
|
||||
{
|
||||
return $this->login;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
* @return Ovh
|
||||
*/
|
||||
public function setPassword($password)
|
||||
{
|
||||
$this->password = $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword()
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $from
|
||||
* @return Ovh
|
||||
*/
|
||||
public function setFrom($from)
|
||||
{
|
||||
$this->from = $from;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFrom()
|
||||
{
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $to
|
||||
* @return Ovh
|
||||
*/
|
||||
public function setTo($to)
|
||||
{
|
||||
$this->to = $to;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTo()
|
||||
{
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
public function send($message, array $options = array())
|
||||
{
|
||||
$this->setOptions($options);
|
||||
$message = trim($message);
|
||||
if ($url = $this->getUrl()) {
|
||||
$message .= ": ".$url;
|
||||
}
|
||||
$url = $this->notify_url."?".http_build_query(array(
|
||||
"account" => $this->getAccount(),
|
||||
"login" => $this->getLogin(),
|
||||
"password" => $this->getPassword(),
|
||||
"from" => $this->getFrom(),
|
||||
"to" => $this->getTo(),
|
||||
"message" => utf8_decode($message),
|
||||
"contentType" => "text/json"
|
||||
));
|
||||
$curl = $this->getCurl(array(
|
||||
CURLOPT_URL => $url
|
||||
));
|
||||
$content = curl_exec($curl);
|
||||
|
||||
if (!$content) {
|
||||
throw new \Exception("Aucun contenu récupéré.");
|
||||
}
|
||||
$data = json_decode($content, true);
|
||||
if (null === $data) {
|
||||
throw new \Exception("Données JSON incorrecte.");
|
||||
}
|
||||
if ($data["status"] < 100 || $data["status"] > 200) {
|
||||
throw new \Exception($data["message"], $data["status"]);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
33
sources/lib/Message/AdapterFactory.php
Normal file
33
sources/lib/Message/AdapterFactory.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace Message;
|
||||
|
||||
use \Message\Adapter;
|
||||
|
||||
class AdapterFactory
|
||||
{
|
||||
/**
|
||||
* @param string $service
|
||||
* @param array $options
|
||||
* @return \Adapter\AdapterAbstract
|
||||
*/
|
||||
public static function factory($service, array $options = array())
|
||||
{
|
||||
$service = strtolower($service);
|
||||
switch ($service) {
|
||||
case "smsfreemobile":
|
||||
case "freemobile":
|
||||
return new Adapter\SmsFreeMobile($options);
|
||||
case "smsovh":
|
||||
case "ovh":
|
||||
return new Adapter\SmsOvh($options);
|
||||
case "notifymyandroid":
|
||||
return new Adapter\NotifyMyAndroid($options);
|
||||
case "pushbullet":
|
||||
return new Adapter\Pushbullet($options);
|
||||
case "pushover":
|
||||
return new Adapter\Pushover($options);
|
||||
}
|
||||
throw new \Exception("No service available");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user