From 241d079ffbabf254d2dcab27e247c3b771b2e5c4 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Sun, 17 Sep 2023 16:16:35 +0200 Subject: [PATCH] Add link shortening for http links in sms, using YOURLS --- controllers/internals/LinkShortener.php | 62 +++++++++++++++++++++++++ controllers/internals/Scheduled.php | 30 +++++++++++- controllers/internals/Tool.php | 16 +++++++ env.php.dist | 2 + env.prod.php.dist | 7 +++ templates/setting/show.php | 24 ++++++++++ 6 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 controllers/internals/LinkShortener.php diff --git a/controllers/internals/LinkShortener.php b/controllers/internals/LinkShortener.php new file mode 100644 index 0000000..296d116 --- /dev/null +++ b/controllers/internals/LinkShortener.php @@ -0,0 +1,62 @@ + + * + * 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; + } +} diff --git a/controllers/internals/Scheduled.php b/controllers/internals/Scheduled.php index 1b61a90..df688d5 100644 --- a/controllers/internals/Scheduled.php +++ b/controllers/internals/Scheduled.php @@ -450,7 +450,8 @@ use Monolog\Logger; $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'); @@ -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 : diff --git a/controllers/internals/Tool.php b/controllers/internals/Tool.php index a09fab6..7f5473c 100644 --- a/controllers/internals/Tool.php +++ b/controllers/internals/Tool.php @@ -85,6 +85,22 @@ use BenMorel\GsmCharsetConverter\Converter; return '' . self::s($number_format, false, true, false) . ''; } + /** + * 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. * diff --git a/env.php.dist b/env.php.dist index 2c13aab..afd6dd5 100644 --- a/env.php.dist +++ b/env.php.dist @@ -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, ], ]; diff --git a/env.prod.php.dist b/env.prod.php.dist index 8ff5fd5..628b500 100644 --- a/env.prod.php.dist +++ b/env.prod.php.dist @@ -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%', + ] + ]; diff --git a/templates/setting/show.php b/templates/setting/show.php index bc64c83..5b9a0d8 100644 --- a/templates/setting/show.php +++ b/templates/setting/show.php @@ -112,6 +112,30 @@ + + +
+
+
+

Support du raccourcisseur d'URL

+
+
+
+
+ + +
+
+ +
+
+
+
+
+