-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add link shortening for http links in sms, using YOURLS
- Loading branch information
osaajani
committed
Sep 17, 2023
1 parent
fb3f942
commit 241d079
Showing
6 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters