Skip to content

Commit

Permalink
Add link shortening for http links in sms, using YOURLS
Browse files Browse the repository at this point in the history
  • Loading branch information
osaajani committed Sep 17, 2023
1 parent fb3f942 commit 241d079
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 1 deletion.
62 changes: 62 additions & 0 deletions controllers/internals/LinkShortener.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
30 changes: 29 additions & 1 deletion controllers/internals/Scheduled.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ public function get_smss_to_send()
$users_smsstops = [];
$users_settings = [];
$users_phones = [];
$users_phone_groups = [];
$users_phone_groups = [];
$shortlink_cache = [];

$now = new \DateTime();
$now = $now->format('Y-m-d H:i:s');
Expand Down Expand Up @@ -643,6 +644,33 @@ public function get_smss_to_send()
$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 :
Expand Down
16 changes: 16 additions & 0 deletions controllers/internals/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ public static function phone_link($number)
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.
*
Expand Down
2 changes: 2 additions & 0 deletions env.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -85,6 +86,7 @@
'force_gsm_alphabet' => 0,
'phone_limit' => 0,
'phone_priority' => 0,
'shorten_url' => 0,
],
];

7 changes: 7 additions & 0 deletions env.prod.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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%',
]

];
24 changes: 24 additions & 0 deletions templates/setting/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@
</div>
</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">
Expand Down

0 comments on commit 241d079

Please sign in to comment.