kUtRL/inc/lib.kutrl.srv.php

244 lines
6.0 KiB
PHP
Raw Normal View History

2021-08-25 23:06:22 +00:00
<?php
/**
* @brief kUtRL, a plugin for Dotclear 2
2021-11-06 15:43:02 +00:00
*
* @package Dotclear
* @subpackage Plugin
2021-11-06 15:43:02 +00:00
*
* @author Jean-Christian Denis and contributors
2021-11-06 15:43:02 +00:00
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-08-25 23:06:22 +00:00
# Generic class for shorten link service
# A service class must extends this one
class kutrlService
{
2021-08-27 22:06:46 +00:00
public $error;
public $settings;
public $log;
2021-11-06 15:43:02 +00:00
protected $config = [];
2021-08-27 22:06:46 +00:00
2022-11-20 16:15:36 +00:00
public function __construct()
2021-08-27 22:06:46 +00:00
{
2022-11-20 16:15:36 +00:00
$this->settings = dcCore::app()->blog->settings->kUtRL;
$this->log = new kutrlLog();
2021-10-26 21:03:38 +00:00
$this->error = new dcError();
2022-11-20 16:15:36 +00:00
//$this->error->setHTMLFormat('%s', "%s\n");
2021-08-27 22:06:46 +00:00
$this->init();
// Force setting
2021-11-06 15:43:02 +00:00
$allow_external_url = $this->settings->kutrl_allow_external_url;
2021-08-27 22:06:46 +00:00
$this->config['$allow_external_url'] = null === $allow_external_url ?
true : $allow_external_url;
$this->config = array_merge(
[
2021-11-06 15:43:02 +00:00
'id' => 'undefined',
'name' => 'undefined',
'home' => '',
2021-08-27 22:06:46 +00:00
'allow_external_url' => true,
2021-10-26 21:03:38 +00:00
'allow_custom_hash' => false,
'allow_protocols' => ['http://'],
2021-08-27 22:06:46 +00:00
2021-11-06 15:43:02 +00:00
'url_test' => 'http://dotclear.jcdenis.com/go/kUtRL',
'url_api' => '',
'url_base' => '',
2022-11-20 16:15:36 +00:00
'url_min_len' => 0,
2021-08-27 22:06:46 +00:00
],
$this->config
);
}
# Magic get for config values
public function __get($k)
2021-08-27 22:49:26 +00:00
{
return $this->get($k);
}
# get config value
public function get($k)
2021-08-27 22:06:46 +00:00
{
2021-11-06 15:43:02 +00:00
return $this->config[$k] ?? null;
2021-08-27 22:06:46 +00:00
}
# Additionnal actions on child start
protected function init()
{
//
}
# Save settings from admin page
public function saveSettings()
{
return null;
}
# Settings form for admin page
public function settingsForm()
{
2021-11-06 15:43:02 +00:00
echo
2021-08-27 22:06:46 +00:00
'<p class="form-note">' .
__('There is nothing to configure for this service.') .
'</p>';
}
# Test if service is well configured
public function testService()
{
return null;
}
# Test if an url is valid
public function isValidUrl($url)
{
2021-11-06 15:43:02 +00:00
return (bool) filter_var($url, FILTER_VALIDATE_URL);
2021-08-27 22:06:46 +00:00
}
# Test if an url contents know prefix
public function isServiceUrl($url)
{
return strpos($url, $this->url_base) === 0;
}
# Test if an url is long enoutgh
public function isLongerUrl($url)
{
2021-11-06 15:43:02 +00:00
return (strlen($url) >= (int) $this->url_min_len);
2021-08-27 22:06:46 +00:00
}
# Test if an url protocol (eg: http://) is allowed
public function isProtocolUrl($url)
{
2021-11-06 15:43:02 +00:00
foreach ($this->allow_protocols as $protocol) {
2021-08-27 22:06:46 +00:00
if (empty($protocol)) {
continue;
}
2021-11-06 15:43:02 +00:00
if (strpos($url, $protocol) === 0) {
2021-08-27 22:06:46 +00:00
return true;
}
}
2021-10-26 21:03:38 +00:00
2021-08-27 22:06:46 +00:00
return false;
}
# Test if an url is from current blog
public function isBlogUrl($url)
{
2022-11-20 16:15:36 +00:00
$base = dcCore::app()->blog->url;
2021-11-06 15:43:02 +00:00
$url = substr($url, 0, strlen($base));
2021-08-27 22:06:46 +00:00
return $url == $base;
}
# Test if an url is know
public function isKnowUrl($url)
{
return $this->log->select($url, null, $this->id, 'kutrl');
}
# Test if an custom short url is know
public function isKnowHash($hash)
{
return $this->log->select(null, $hash, $this->id, 'kutrl');
}
# Create hash from url
public function hash($url, $hash = null)
{
2022-11-20 16:15:36 +00:00
$url = trim(dcCore::app()->con->escape($url));
2021-08-27 22:06:46 +00:00
if ('undefined' === $this->id) {
return false;
}
if ($hash && !$this->allow_custom_hash) {
return false;
}
if ($this->isServiceUrl($url)) {
return false;
}
if (!$this->isLongerUrl($url)) {
return false;
}
if (!$this->allow_external_url && $this->isBlogUrl($url)) {
return false;
}
if ($hash && false !== ($rs = $this->isKnowHash($hash))) {
return false;
}
if (false === ($rs = $this->isKnowUrl($url))) {
if (false === ($rs = $this->createHash($url, $hash))) {
return false;
}
$this->log->insert($rs->url, $rs->hash, $rs->type, 'kutrl');
2022-11-20 16:15:36 +00:00
dcCore::app()->blog->triggerBlog();
2021-08-27 22:06:46 +00:00
# --BEHAVIOR-- kutrlAfterCreateShortUrl
2022-11-20 16:15:36 +00:00
dcCore::app()->callBehavior('kutrlAfterCreateShortUrl', $rs);
2021-08-27 22:06:46 +00:00
}
2021-11-06 15:43:02 +00:00
2021-08-27 22:06:46 +00:00
return $rs;
}
2021-11-06 15:43:02 +00:00
# Create a hash for a given url (and its custom hash)
2021-08-27 22:06:46 +00:00
public function createHash($url, $hash = null)
{
return false;
}
# Remove an url from list of know urls
public function remove($url)
{
if (!($rs = $this->isKnowUrl($url))) {
return false;
}
$this->deleteUrl($url);
$this->log->delete($rs->id);
2021-10-26 21:03:38 +00:00
2021-08-27 22:06:46 +00:00
return true;
}
# Delete url on service (second argument really delete urls)
public function deleteUrl($url, $delete = false)
{
return null;
}
# Retrieve long url from hash
public function getUrl($hash)
{
return false;
}
# Post request
public static function post($server, $data, $verbose = true, $get = false, $headers = [])
{
2021-11-06 15:43:02 +00:00
$url = (string) $server;
2021-08-27 22:06:46 +00:00
$client = netHttp::initClient($url, $url);
$client->setUserAgent('kUtRL - http://kutrl.fr');
$client->setPersistReferers(false);
if (is_array($headers) && !empty($headers)) {
2021-11-06 15:43:02 +00:00
foreach ($headers as $header) {
2021-08-27 22:06:46 +00:00
$client->setMoreHeader($header);
}
}
if ($get) {
$client->get($url, $data);
} else {
$client->post($url, $data);
}
if (!$verbose && $client->getStatus() != 200) {
return false;
}
if ($verbose) {
return $client->getContent();
}
2021-10-26 21:03:38 +00:00
2021-08-27 22:06:46 +00:00
return true;
}
2021-11-06 15:43:02 +00:00
}