Skip to content

Commit

Permalink
multiple fixes
Browse files Browse the repository at this point in the history
- update to ttrss v21.03-ddfa39015
- php8
- fix hotkey action
  • Loading branch information
joshp23 committed Mar 11, 2021
1 parent 1915c0f commit 095d6d7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 46 deletions.
60 changes: 36 additions & 24 deletions yourls/init.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
<?php
class Yourls extends Plugin {
private $host;
private $curl_yourls;

function init($host) {
$this->host = $host;
$this->curl_yourls = $curl_yourls;
$this->curl_yourls = curl_init() ;
curl_setopt($this->curl_yourls, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl_yourls, CURLOPT_FOLLOWLOCATION, true);
$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
$host->add_hook($host::HOOK_PREFS_TAB, $this);
$host->add_hook($host::HOOK_HOTKEY_MAP, $this);
$host->add_hook($host::HOOK_HOTKEY_INFO, $this);
}

function about() {
return array("1.1.1",
"Shorten article Link using Yourls",
"Beun and acaranta");
return array(
"2.0.0",
"Shorten article Link using Yourls",
"Beun and acaranta, and joshu@unfettered.net");
}

function hook_hotkey_map($hotkeys) {
$hotkeys['s y'] = 'send_to_yourls';
$hotkeys['s y'] = 'send_to_yourls';

return $hotkeys;
}
Expand Down Expand Up @@ -68,24 +64,40 @@ function getInfo() {
WHERE id = ? AND ref_id = id AND owner_uid = ?");
$sth->execute([$id, $_SESSION['uid']]);
if ($row = $sth->fetch()) {

$title = truncate_string(strip_tags($row['title']), 100, '...');
$article_link = $row['link'];
$yourls_url = $this->host->get($this, "Yourls_URL");
$yourls_url = $yourls_url . "/yourls-api.php";
$yourls_api = $this->host->get($this, "Yourls_API");
$postfields = array(
'signature' => $yourls_api,
'action' => 'shorturl',
'format' => 'simple',
'url' => $article_link,
'title' => $title,
);

$curl_yourls = curl_init();

curl_setopt($curl_yourls, CURLOPT_URL, $yourls_url);
curl_setopt($curl_yourls, CURLOPT_HEADER, 0);
curl_setopt($curl_yourls, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_yourls, CURLOPT_TIMEOUT, 30);
curl_setopt($curl_yourls, CURLOPT_POST, 1);
curl_setopt($curl_yourls, CURLOPT_POSTFIELDS,$postfields);

if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
curl_setopt($curl_yourls, CURLOPT_FOLLOWLOCATION, true);
}
$short_url = curl_exec($curl_yourls) ;
$status = curl_getinfo($curl_yourls, CURLINFO_HTTP_CODE);
curl_close($curl_yourls);

print json_encode(array("status" => $status, "title" => $title, "shorturl" => $short_url));
} else {
print json_encode(array( "status" => "Database fail" ));
}

$yourls_url = $this->host->get($this, "Yourls_URL");
$yourls_api = $this->host->get($this, "Yourls_API");

curl_setopt($this->curl_yourls, CURLOPT_URL, "$yourls_url/yourls-api.php?signature=$yourls_api&action=shorturl&format=simple&url=".urlencode($article_link)."&title=".urlencode($title)) ;
curl_setopt($this->curl_yourls, CURLOPT_RETURNTRANSFER, true);
if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
curl_setopt($this->curl_yourls, CURLOPT_FOLLOWLOCATION, true);
}
$short_url = curl_exec($this->curl_yourls) ;

curl_setopt($this->curl_yourls, CURLOPT_URL, "$yourls_url/yourls-api.php?signature=$yourls_api&action=shorturl&format=simple&url=".urlencode($article_link)."&title=".urlencode($title)) ;
$short_url = curl_exec($this->curl_yourls) ;

print json_encode(array("title" => $title, "link" => $article_link, "id" => $id, "yourlsurl" => $yourls_url, "yourlsapi" => $yourls_api, "shorturl" => $short_url));
}

function hook_prefs_tab($args) {
Expand Down
69 changes: 47 additions & 22 deletions yourls/yourls.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
function shareArticleToYourls(id) {
try {
var query = "?op=pluginhandler&plugin=yourls&method=getInfo&id=" + encodeURIComponent(id);
var d = new Date();
var ts = d.getTime();

new Ajax.Request("backend.php", {
parameters: query,
onSuccess: function(transport) {
var ti = JSON.parse(transport.responseText);
var share_url_query = "?signature=" + ti.yourlsapi + "&action=shorturl&format=simple&url=" + encodeURIComponent(ti.link) + "&title=" + encodeURIComponent(ti.title);
dialog = new dijit.Dialog({
id: "YourlsShortLinkDlg"+ts,
title: __("Youlrs Shortened URL"),
style: "width: 200px",
content: '<p align=center>' + ti.shorturl + '<br/><a target="_blank" href="https://twitter.com/share?_=' + ts + '&text=' + encodeURIComponent(ti.title) + '&url=' + encodeURIComponent(ti.shorturl) + '"><img src="/plugins.local/yourls/tweetshare.png"/></a><br/><a target="_blank" href="https://www.facebook.com/sharer.php?u=' + encodeURIComponent(ti.shorturl) + '"><img src="/plugins.local/yourls/fbshare.png" border=0/></a></p>',
});
dialog.show();
} });

} catch (e) {
exception_error("yourlsArticle", e);
}
try {
Notify.progress("Saving to YOURLS …", true);
xhr.json("backend.php",
{
'op': 'pluginhandler',
'plugin': 'yourls',
'method': 'getInfo',
'id': encodeURIComponent(id)
},
(reply) => {
if (reply.status) {
if (reply.status=="200") {
var d = new Date();
var ts = d.getTime();

dialog = new dijit.Dialog({
id: "YourlsShortLinkDlg"+ts,
title: __("Youlrs Shortened URL"),
style: "width: 200px",
content: '<p align=center>' + reply.shorturl + '<br/><a target="_blank" href="https://twitter.com/share?_=' + ts + '&text=' + encodeURIComponent(reply.title) + '&url=' + encodeURIComponent(reply.shorturl) + '"><img src="/plugins.local/yourls/tweetshare.png"/></a><br/><a target="_blank" href="https://www.facebook.com/sharer.php?u=' + encodeURIComponent(reply.shorturl) + '"><img src="/plugins.local/yourls/fbshare.png" border=0/></a></p>',
});

dialog.show();
Notify.info("Saved to YOURLS");
} else {
Notify.error("<strong>Error: "+reply.status+" encountered while saving to YOURLS!</strong>", true);
}
} else {
Notify.error("The YOURLS plugin needs to be configured. See the README for help", true);
}
});
} catch (e) {
Notify.error("yourlsArticle", e);
}
}

require(['dojo/_base/kernel', 'dojo/ready'], function (dojo, ready) {
ready(function () {
PluginHost.register(PluginHost.HOOK_INIT_COMPLETE, () => {
App.hotkey_actions["send_to_yourls"] = function() {
if (Article.getActive()) {
shareArticleToYourls(Article.getActive());
return;
}
};
});
});
});

0 comments on commit 095d6d7

Please sign in to comment.