#1 handle redirects
This commit is contained in:
parent
6b37eaf919
commit
b0da900d3f
1 changed files with 56 additions and 42 deletions
98
shaargem.php
98
shaargem.php
|
@ -9,6 +9,7 @@ use Shaarli\Config\ConfigManager;
|
||||||
use Shaarli\Plugin\PluginManager;
|
use Shaarli\Plugin\PluginManager;
|
||||||
use Shaarli\Render\TemplatePage;
|
use Shaarli\Render\TemplatePage;
|
||||||
|
|
||||||
|
static $redirect_counter = 0;
|
||||||
/**
|
/**
|
||||||
* In the footer hook, there is a working example of a translation extension for Shaarli.
|
* In the footer hook, there is a working example of a translation extension for Shaarli.
|
||||||
*
|
*
|
||||||
|
@ -19,6 +20,7 @@ use Shaarli\Render\TemplatePage;
|
||||||
*/
|
*/
|
||||||
const EXT_TRANSLATION_DOMAIN = 'shaargem';
|
const EXT_TRANSLATION_DOMAIN = 'shaargem';
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is not necessary, but it's easier if you don't want Poedit to mix up your translations.
|
* This is not necessary, but it's easier if you don't want Poedit to mix up your translations.
|
||||||
*/
|
*/
|
||||||
|
@ -60,48 +62,7 @@ function shaargem_init($conf)
|
||||||
*/
|
*/
|
||||||
function hook_shaargem_render_editlink($data)
|
function hook_shaargem_render_editlink($data)
|
||||||
{
|
{
|
||||||
$ret = preg_match(",gemini://([\d\w\.\+\-@\:%._\~#=]+)/?,", $data['link']['url'] ,$domain);
|
$data['link']['title'] = fetch_iri_title($data['link']['url']);
|
||||||
|
|
||||||
error_log("ret:".$ret);
|
|
||||||
if ($ret)
|
|
||||||
{
|
|
||||||
$stream_context = stream_context_create([ 'ssl' => [
|
|
||||||
'verify_peer' => false,
|
|
||||||
'verify_peer_name' => false,
|
|
||||||
'allow_self_signed' => true,
|
|
||||||
'verify_depth' => 0 ]]);
|
|
||||||
|
|
||||||
$fp = stream_socket_client("tls://".$domain[1].":1965", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $stream_context);
|
|
||||||
|
|
||||||
if (!$fp) {
|
|
||||||
error_log("$errstr ($errno)");
|
|
||||||
} else {
|
|
||||||
fwrite($fp, $data['link']['url']."\r\n");
|
|
||||||
$header = fgets($fp);
|
|
||||||
$ret = preg_match(",^(\d\d) (.*)\r\n$,", $header, $headers);
|
|
||||||
$status = $headers[1];
|
|
||||||
$meta = $headers[2];
|
|
||||||
error_log("status:".$status.", meta:".$meta);
|
|
||||||
if (strstr($header, "20 text/gemini"))
|
|
||||||
{
|
|
||||||
$nblines = 1;
|
|
||||||
while($line = fgets($fp)) {
|
|
||||||
$ret = preg_match(",# (.*),",$line,$title);
|
|
||||||
if ($title[0] or $nblines > 100 ) {
|
|
||||||
error_log($nblines);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
$nblines++;
|
|
||||||
}
|
|
||||||
$data['link']['title'] = $title[1];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
error_log("[shaargem] invalid response:".$header);
|
|
||||||
}
|
|
||||||
fclose($fp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,3 +122,56 @@ function shaargem_dummy_translation()
|
||||||
t('Provides Gemini protocol support.');
|
t('Provides Gemini protocol support.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fetch_iri_title($iri)
|
||||||
|
{
|
||||||
|
$ret = preg_match(",gemini://([\d\w\.\+\-@\:%._\~#=]+)/?,", $iri ,$domain);
|
||||||
|
static $redirect_counter = 0;
|
||||||
|
error_log("ret:".$ret);
|
||||||
|
if ($ret)
|
||||||
|
{
|
||||||
|
$stream_context = stream_context_create([ 'ssl' => [
|
||||||
|
'verify_peer' => false,
|
||||||
|
'verify_peer_name' => false,
|
||||||
|
'allow_self_signed' => true,
|
||||||
|
'verify_depth' => 0 ]]);
|
||||||
|
|
||||||
|
$fp = stream_socket_client("tls://".$domain[1].":1965", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $stream_context);
|
||||||
|
|
||||||
|
if (!$fp) {
|
||||||
|
error_log("$errstr ($errno)");
|
||||||
|
} else {
|
||||||
|
fwrite($fp, $iri."\r\n");
|
||||||
|
$header = fgets($fp);
|
||||||
|
error_log("headers:".$headers);
|
||||||
|
$ret = preg_match(",^(\d\d) (.*)\r\n$,", $header, $headers);
|
||||||
|
$status = $headers[1];
|
||||||
|
$meta = $headers[2];
|
||||||
|
error_log("status:".$status.", meta:".$meta);
|
||||||
|
$max_redirects = 10;
|
||||||
|
if ($status[0] == 3 and $redirect_counter < $max_redirects)
|
||||||
|
{
|
||||||
|
$redirect_counter++;
|
||||||
|
error_log("[shaargem] found status 30. redirecting to ".$meta." (".$redirect_counter."/".$max_redirects.")");
|
||||||
|
$title[1] = fetch_iri_title($meta);
|
||||||
|
}
|
||||||
|
elseif ($status[0] == 2)
|
||||||
|
{
|
||||||
|
$nblines = 1;
|
||||||
|
while($line = fgets($fp)) {
|
||||||
|
$ret = preg_match(",# (.*),",$line,$title);
|
||||||
|
if ($title[0] or $nblines > 100 ) {
|
||||||
|
error_log($nblines);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$nblines++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
error_log("[shaargem] invalid response:".$header);
|
||||||
|
}
|
||||||
|
fclose($fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $title[1];
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue