]> git.wh0rd.org Git - tt-rss.git/blob - plugins/share/init.php
84bc78eb4ed6508c139885eb5b5bb2fea6865078
[tt-rss.git] / plugins / share / init.php
1 <?php
2 class Share extends Plugin {
3         private $host;
4
5         function about() {
6                 return array(1.0,
7                         "Share article by unique URL",
8                         "fox");
9         }
10
11         /* @var PluginHost $host */
12         function init($host) {
13                 $this->host = $host;
14
15                 $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
16                 $host->add_hook($host::HOOK_PREFS_TAB_SECTION, $this);
17         }
18
19         function get_js() {
20                 return file_get_contents(dirname(__FILE__) . "/share.js");
21         }
22
23         function get_prefs_js() {
24                 return file_get_contents(dirname(__FILE__) . "/share_prefs.js");
25         }
26
27
28         function unshare() {
29                 $id = $_REQUEST['id'];
30
31                 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET uuid = '' WHERE int_id = ?
32                         AND owner_uid = ?");
33                 $sth->execute([$id, $_SESSION['uid']]);
34
35                 print "OK";
36         }
37
38         function hook_prefs_tab_section($id) {
39                 if ($id == "prefFeedsPublishedGenerated") {
40
41                         print "<p>" . __("You can disable all articles shared by unique URLs here.") . "</p>";
42
43                         print "<button class=\"danger\" dojoType=\"dijit.form.Button\" onclick=\"return clearArticleAccessKeys()\">".
44                                 __('Unshare all articles')."</button> ";
45
46                         print "</p>";
47
48                 }
49         }
50
51         // Silent
52         function clearArticleKeys() {
53                 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET uuid = '' WHERE
54                         owner_uid = ?");
55                 $sth->execute([$_SESSION['uid']]);
56
57                 return;
58         }
59
60
61         function newkey() {
62                 $id = $_REQUEST['id'];
63                 $uuid = uniqid_short();
64
65                 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET uuid = ? WHERE int_id = ?
66                         AND owner_uid = ?");
67                 $sth->execute([$uuid, $id, $_SESSION['uid']]);
68
69                 print json_encode(array("link" => $uuid));
70         }
71
72         function hook_article_button($line) {
73                 $img = $line['uuid'] ? "share.png" : "notshared.png";
74
75                 return "<img id='SHARE-IMG-".$line['int_id']."' src=\"plugins/share/$img\"
76                         class='tagsPic' style=\"cursor : pointer\"
77                         onclick=\"shareArticle(".$line['int_id'].")\"
78                         title='".__('Share by URL')."'>";
79         }
80
81         function shareArticle() {
82                 $param = $_REQUEST['param'];
83
84                 $sth = $this->pdo->prepare("SELECT uuid FROM ttrss_user_entries WHERE int_id = ?
85                         AND owner_uid = ?");
86                 $sth->execute([$param, $_SESSION['uid']]);
87
88                 if ($row = $sth->fetch()) {
89
90                         $uuid = $row['uuid'];
91
92                         if (!$uuid) {
93                                 $uuid = uniqid_short();
94
95                                 $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET uuid = ? WHERE int_id = ?
96                                         AND owner_uid = ?");
97                                 $sth->execute([$uuid, $param, $_SESSION['uid']]);
98                         }
99
100                         print __("You can share this article by the following unique URL:") . "<br/>";
101
102                         $url_path = get_self_url_prefix();
103                         $url_path .= "/public.php?op=share&key=$uuid";
104
105                         print "<div class=\"tagCloudContainer\">";
106                         print "<a id='gen_article_url' href='$url_path' target='_blank' rel='noopener noreferrer'>$url_path</a>";
107                         print "</div>";
108
109                         /* if (!label_find_id(__('Shared'), $_SESSION["uid"]))
110                                 label_create(__('Shared'), $_SESSION["uid"]);
111
112                         label_add_article($ref_id, __('Shared'), $_SESSION['uid']); */
113
114
115                 } else {
116                         print "Article not found.";
117                 }
118
119                 print "<div align='center'>";
120
121                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('shareArticleDlg').unshare()\">".
122                         __('Unshare article')."</button>";
123
124                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('shareArticleDlg').newurl()\">".
125                         __('Generate new URL')."</button>";
126
127                 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('shareArticleDlg').hide()\">".
128                         __('Close this window')."</button>";
129
130                 print "</div>";
131         }
132
133         function api_version() {
134                 return 2;
135         }
136
137 }