Add link shortening for http links in sms, using YOURLS
This commit is contained in:
parent
fb3f9425d1
commit
241d079ffb
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of RaspiSMS.
|
||||
*
|
||||
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
||||
*
|
||||
* This source file is subject to the GPL-3.0 license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace controllers\internals;
|
||||
|
||||
use Exception;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
use PHPMailer\PHPMailer\SMTP;
|
||||
|
||||
/**
|
||||
* Mailing class.
|
||||
*/
|
||||
class LinkShortener
|
||||
{
|
||||
/**
|
||||
* Shorten an URL using the configured YOURLS instance
|
||||
*/
|
||||
public static function shorten($url)
|
||||
{
|
||||
$api_url = URL_SHORTENER['HOST'] . '/yourls-api.php';
|
||||
|
||||
$data = [
|
||||
'action' => 'shorturl',
|
||||
'format' => 'json',
|
||||
'username' => URL_SHORTENER['USER'],
|
||||
'password' => URL_SHORTENER['PASS'],
|
||||
'url' => $url,
|
||||
];
|
||||
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $api_url);
|
||||
curl_setopt($ch, CURLOPT_HEADER, 0); // No header in the result
|
||||
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Enable follow location
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result
|
||||
curl_setopt($ch, CURLOPT_POST, 1); // This is a POST request
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
$response = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
try
|
||||
{
|
||||
$response = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$shortlink = $response['shorturl'] ?? false;
|
||||
return $shortlink;
|
||||
}
|
||||
}
|
|
@ -451,6 +451,7 @@ use Monolog\Logger;
|
|||
$users_settings = [];
|
||||
$users_phones = [];
|
||||
$users_phone_groups = [];
|
||||
$shortlink_cache = [];
|
||||
|
||||
$now = new \DateTime();
|
||||
$now = $now->format('Y-m-d H:i:s');
|
||||
|
@ -643,6 +644,33 @@ use Monolog\Logger;
|
|||
$text = Tool::convert_to_gsm0338($text);
|
||||
}
|
||||
|
||||
// If the text contain http links we must replace them
|
||||
if (ENABLE_URL_SHORTENER && ((int) ($users_settings[$id_user]['shorten_url'] ?? false)))
|
||||
{
|
||||
$http_links = Tool::search_http_links($text);
|
||||
if ($http_links !== false)
|
||||
{
|
||||
foreach ($http_links as $http_link)
|
||||
{
|
||||
if (!array_key_exists($http_link, $shortlink_cache))
|
||||
{
|
||||
$shortlkink = LinkShortener::shorten($http_link);
|
||||
|
||||
// If link shortening failed, keep original one
|
||||
if ($shortlkink === false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$shortlink_cache[$http_link] = $shortlkink;
|
||||
}
|
||||
|
||||
$shortlink = $shortlink_cache[$http_link];
|
||||
$text = str_replace($http_link, $shortlink, $text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Choose phone if no phone defined for message
|
||||
Phones are choosen using type, priority and remaining volume :
|
||||
|
|
|
@ -85,6 +85,22 @@ use BenMorel\GsmCharsetConverter\Converter;
|
|||
return '<a href="' . self::s($url, false, true, false) . '">' . self::s($number_format, false, true, false) . '</a>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for http link in a text
|
||||
*
|
||||
* @param string $text : Text to search a link in
|
||||
*
|
||||
* @return bool|array : False if no link in the text, or an array of all http links
|
||||
*/
|
||||
public static function search_http_links($text)
|
||||
{
|
||||
$regex = "#http(s?)://\S+#i";
|
||||
$matches = [];
|
||||
$nb_matches = preg_match_all($regex, $text, $matches);
|
||||
|
||||
return $nb_matches > 0 ? $matches[0] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cette fonction fait la correspondance entre un type d'evenement et une icone font awesome.
|
||||
*
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
'APP_SECRET' => '%APP_SECRET%',
|
||||
'ENABLE_COMMAND' => false,
|
||||
'ENABLE_ACCOUNT_DELETION' => true,
|
||||
'ENABLE_URL_SHORTENER' => %APP_URL_SHORTENER%,
|
||||
|
||||
//E-mail types
|
||||
'EMAIL_RESET_PASSWORD' => [
|
||||
|
@ -85,6 +86,7 @@
|
|||
'force_gsm_alphabet' => 0,
|
||||
'phone_limit' => 0,
|
||||
'phone_priority' => 0,
|
||||
'shorten_url' => 0,
|
||||
],
|
||||
];
|
||||
|
||||
|
|
|
@ -21,4 +21,11 @@
|
|||
'FROM' => '%APP_MAIL_FROM%',
|
||||
],
|
||||
|
||||
//YOURLS url shortener settings
|
||||
'URL_SHORTENER' => [
|
||||
'HOST' => '%APP_URL_SHORTENER_HOST%',
|
||||
'USER' => '%APP_URL_SHORTENER_USER%',
|
||||
'PASS' => '%APP_URL_SHORTENER_PASS%',
|
||||
]
|
||||
|
||||
];
|
||||
|
|
|
@ -113,6 +113,30 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<?php if (ENABLE_URL_SHORTENER) { ?>
|
||||
<div class="col-xs-12 col-md-6">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<h4 class="panel-title"><i class="fa fa-link fa-fw"></i> Support du raccourcisseur d'URL</h4>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<form action="<?php echo \descartes\Router::url('Setting', 'update', ['setting_name' => 'shorten_url', 'csrf' => $_SESSION['csrf']]); ?>" method="POST">
|
||||
<div class="form-group">
|
||||
<label>Raccourcir automatiquement les liens HTTP(S) dans les SMS : </label>
|
||||
<select name="setting_value" class="form-control">
|
||||
<option value="0">Non</option>
|
||||
<option value="1" <?php echo $_SESSION['user']['settings']['shorten_url'] ? 'selected' : ''; ?>>Oui</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="text-center">
|
||||
<button class="btn btn-success">Mettre à jour les données</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<div class="col-xs-12 col-md-6">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
|
|
Loading…
Reference in New Issue