acf
This commit is contained in:
19
plugins/system/nrframework/helpers/urls/bitly.php
Normal file
19
plugins/system/nrframework/helpers/urls/bitly.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @author Tassos Marinos <info@tassos.gr>
|
||||
* @link https://www.tassos.gr
|
||||
* @copyright Copyright © 2024 Tassos All Rights Reserved
|
||||
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class nrURLShortBitly extends NRURLShortener
|
||||
{
|
||||
|
||||
function baseURL()
|
||||
{
|
||||
return 'http://api.bit.ly/v3/shorten?login='.$this->service->login.'&apiKey='.$this->service->api.'&format=txt&uri='.urlencode($this->url);
|
||||
}
|
||||
}
|
||||
58
plugins/system/nrframework/helpers/urls/google.php
Normal file
58
plugins/system/nrframework/helpers/urls/google.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @author Tassos Marinos <info@tassos.gr>
|
||||
* @link https://www.tassos.gr
|
||||
* @copyright Copyright © 2024 Tassos All Rights Reserved
|
||||
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use \Joomla\CMS\Http\HttpFactory;
|
||||
|
||||
class nrURLShortGoogle extends NRURLShortener
|
||||
{
|
||||
|
||||
function get()
|
||||
{
|
||||
|
||||
if (!$this->validateCredentials())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$baseURL = "https://www.googleapis.com/urlshortener/v1/url?key=".$this->service->api;
|
||||
|
||||
$data = '{ "longUrl": "'.$this->url.'" }';
|
||||
$headers['Content-Type'] = 'application/json';
|
||||
|
||||
try
|
||||
{
|
||||
$response = HttpFactory::getHttp()->post($baseURL, $data, $headers, 5);
|
||||
|
||||
if ($response === null || $response->code !== 200)
|
||||
{
|
||||
$result = json_decode($response->body);
|
||||
$this->throwError($result->error->message);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (RuntimeException $e)
|
||||
{
|
||||
$this->throwError($e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = json_decode($response->body);
|
||||
|
||||
if (!isset($data->id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $data->id;
|
||||
}
|
||||
}
|
||||
140
plugins/system/nrframework/helpers/urls/shortener.php
Normal file
140
plugins/system/nrframework/helpers/urls/shortener.php
Normal file
@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @author Tassos Marinos <info@tassos.gr>
|
||||
* @link https://www.tassos.gr
|
||||
* @copyright Copyright © 2024 Tassos All Rights Reserved
|
||||
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\Http\HttpFactory;
|
||||
|
||||
/**
|
||||
* Framework URL Shortening Class
|
||||
* Should be extended. The get() method is required.
|
||||
*/
|
||||
class NRURLShortener {
|
||||
|
||||
/**
|
||||
* The Shortener Service
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* The URL to be shortened
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
/**
|
||||
* Sets if the service needs a valid login name
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $needsLogin = true;
|
||||
|
||||
/**
|
||||
* Sets if the service needs a valid API Key
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $needsKey = true;
|
||||
|
||||
/**
|
||||
* Constructor of class
|
||||
*
|
||||
* @param object $service The Shortener service information
|
||||
* @param string $url The URL to be shortened
|
||||
*/
|
||||
public function __construct($service, $url) {
|
||||
$this->service = $service;
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception
|
||||
*
|
||||
* @param string $msg
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function throwError($msg)
|
||||
{
|
||||
throw new Exception(Text::sprintf('NR_URL_SHORTENING_FAILED', $this->url, $this->service->name, $msg));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if credentials are set
|
||||
*
|
||||
* @return boolean Returns true if credentials are set
|
||||
*/
|
||||
protected function validateCredentials()
|
||||
{
|
||||
|
||||
if ($this->needsKey && !isset($this->service->api))
|
||||
{
|
||||
$this->throwError("API Key not set");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->needsLogin && !isset($this->service->login))
|
||||
{
|
||||
$this->throwError("Login not set");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shortens the URL
|
||||
*
|
||||
* @return string On success returns the shortened URL
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
|
||||
if (!$this->validateCredentials())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$baseURL = $this->baseURL();
|
||||
|
||||
if (!$baseURL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$response = HttpFactory::getHttp()->get($baseURL, null, 5);
|
||||
|
||||
if ($response === null || $response->code !== 200)
|
||||
{
|
||||
$this->throwError($response->body);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (RuntimeException $e)
|
||||
{
|
||||
$this->throwError($e->getMessage());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return trim($response->body);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
23
plugins/system/nrframework/helpers/urls/tinyurl.php
Normal file
23
plugins/system/nrframework/helpers/urls/tinyurl.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @author Tassos Marinos <info@tassos.gr>
|
||||
* @link https://www.tassos.gr
|
||||
* @copyright Copyright © 2024 Tassos All Rights Reserved
|
||||
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class nrURLShortTinyURL extends NRURLShortener
|
||||
{
|
||||
|
||||
protected $needsKey = false;
|
||||
protected $needsLogin = false;
|
||||
|
||||
function baseURL()
|
||||
{
|
||||
return "http://tinyurl.com/api-create.php?url=".urlencode($this->url);
|
||||
}
|
||||
|
||||
}
|
||||
158
plugins/system/nrframework/helpers/urls/urls.php
Normal file
158
plugins/system/nrframework/helpers/urls/urls.php
Normal file
@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @author Tassos Marinos <info@tassos.gr>
|
||||
* @link https://www.tassos.gr
|
||||
* @copyright Copyright © 2024 Tassos All Rights Reserved
|
||||
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use \NRFramework\Cache;
|
||||
|
||||
class NRURLs {
|
||||
|
||||
private $url;
|
||||
private $shortener;
|
||||
private $cache;
|
||||
|
||||
function __construct($url = null)
|
||||
{
|
||||
if (isset($url)) {
|
||||
$this->set($url);
|
||||
}
|
||||
|
||||
$this->setCache(true);
|
||||
}
|
||||
|
||||
public function set($url)
|
||||
{
|
||||
$url = trim(filter_var($url, FILTER_SANITIZE_URL));
|
||||
return ($this->url = $url);
|
||||
}
|
||||
|
||||
public function setCache($state)
|
||||
{
|
||||
$this->cache = (bool) $state;
|
||||
}
|
||||
|
||||
public function setShortener($service)
|
||||
{
|
||||
$this->shortener = $service;
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
public function validate($url_ = null)
|
||||
{
|
||||
$url = isset($url_) ? $url_ : $this->url;
|
||||
|
||||
if (!$url)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove all illegal characters from the URL
|
||||
$url = filter_var($url, FILTER_SANITIZE_URL);
|
||||
|
||||
// Validate URL
|
||||
if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getShort()
|
||||
{
|
||||
if (!$this->validate() || !isset($this->shortener))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$hash = MD5($this->shortener->name . $this->url);
|
||||
$cache = Cache::read($hash, true);
|
||||
|
||||
if ($cache)
|
||||
{
|
||||
return $cache;
|
||||
}
|
||||
|
||||
// Load Shorten Service Class
|
||||
$file = __DIR__ . "/" . strtolower($this->shortener->name) . '.php';
|
||||
$class = 'nrURLShort' . $this->shortener->name;
|
||||
$method = "get";
|
||||
|
||||
require_once(__DIR__ . "/shortener.php");
|
||||
|
||||
if (!class_exists($class) && file_exists($file)) {
|
||||
require_once($file);
|
||||
}
|
||||
|
||||
if (!class_exists($class) || !method_exists($class, $method))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$class_ = new $class($this->shortener, $this->url);
|
||||
$data = $class_->$method();
|
||||
|
||||
// Return the original URL if we don't have a valid short URL
|
||||
if (!$this->validate($data))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Cache::set($hash, $data);
|
||||
|
||||
// Store to cache
|
||||
if ($this->cache)
|
||||
{
|
||||
Cache::write($hash, $data);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends extra parameters to the end of the URL
|
||||
*
|
||||
* @param String $url Pass URL
|
||||
* @param String $params String of parameters (param=1¶m=2)
|
||||
*
|
||||
* @return string Returns new url
|
||||
*/
|
||||
public function appendParams($params)
|
||||
{
|
||||
|
||||
if (!$params)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
$url = $this->url;
|
||||
|
||||
$query = parse_url($url, PHP_URL_QUERY);
|
||||
$params = trim($params, "?");
|
||||
$params = trim($params, "&");
|
||||
|
||||
if ($query)
|
||||
{
|
||||
$url .= '&' . $params;
|
||||
} else
|
||||
{
|
||||
$url .= '?' . $params;
|
||||
}
|
||||
|
||||
$this->set($url);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user