summaryrefslogtreecommitdiff
path: root/plugins/bookmarklets/init.php
blob: 72aeb2c3863d33759351e5d743d2a64bb16d089e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
class Bookmarklets extends Plugin {

	/** @var PluginHost $host */
	private $host;

	function about() {
		return array(null,
			"Easy feed subscription and web page sharing using bookmarklets",
			"fox",
			false,
			"https://git.tt-rss.org/fox/tt-rss/wiki/ShareAnything");
  }

	function init($host) {
		$this->host = $host;

		$host->add_hook($host::HOOK_PREFS_TAB, $this);
	}

	function is_public_method($method) {
		return in_array($method, ["subscribe", "sharepopup"]);
	}

	function subscribe() : void {
		if (Config::get(Config::SINGLE_USER_MODE)) {
			UserHelper::login_sequence();
		}

		if (!empty($_SESSION["uid"])) {

			$feed_url = clean($_REQUEST["feed_url"] ?? "");
			$csrf_token = clean($_POST["csrf_token"] ?? "");

			header('Content-Type: text/html; charset=utf-8');
			?>
			<!DOCTYPE html>
			<html>
			<head>
				<title><?= __("Subscribe to feed...") ?></title>
				<?= javascript_tag("lib/dojo/dojo.js") ?>
				<?= javascript_tag("js/utility.js") ?>
				<?= javascript_tag("js/common.js") ?>
				<?= javascript_tag("lib/dojo/tt-rss-layer.js") ?>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
				<link rel="shortcut icon" type="image/png" href="images/favicon.png">
				<link rel="icon" type="image/png" sizes="72x72" href="images/favicon-72px.png">
				<style type="text/css">
					@media (prefers-color-scheme: dark) {
						body {
							background : #303030;
						}
					}

					body.css_loading * {
						display : none;
					}
				</style>
			</head>
			<body class='flat ttrss_utility css_loading'>
			<script type="text/javascript">
				const UtilityApp = {
					init: function() {
                        require(['dojo/parser', "dojo/ready", 'dijit/form/Button','dijit/form/CheckBox', 'dijit/form/Form',
                            'dijit/form/Select','dijit/form/TextBox','dijit/form/ValidationTextBox'], function(parser, ready){
                            ready(function() {
                                parser.parse();
                            });
                        });
					}
				};
			</script>
			<div class="container">
			<h1><?= __("Subscribe to feed...") ?></h1>
			<div class='content'>
			<?php

			if (!$feed_url || !validate_csrf($csrf_token)) {
				?>
				<form method="post" action='public.php'>
					<?= \Controls\public_method_tags($this, "subscribe") ?>
					<?= \Controls\hidden_tag("csrf_token", $_SESSION["csrf_token"]) ?>

					<fieldset>
						<label>Feed or site URL:</label>
						<input style="width: 300px" dojoType="dijit.form.ValidationTextBox" required="1" name="feed_url" value="<?= htmlspecialchars($feed_url) ?>">
					</fieldset>

					<button class="alt-primary" dojoType="dijit.form.Button" type="submit">
						<?= __("Subscribe") ?>
					</button>

					<a href="index.php"><?= __("Return to Tiny Tiny RSS") ?></a>
				</form>
				<?php
			} else {

				$rc = Feeds::_subscribe($feed_url);
				$feed_urls = false;

				switch ($rc['code']) {
					case 0:
						print_warning(T_sprintf("Already subscribed to <b>%s</b>.", $feed_url));
						break;
					case 1:
						print_notice(T_sprintf("Subscribed to <b>%s</b>.", $feed_url));
						break;
					case 2:
						print_error(T_sprintf("Could not subscribe to <b>%s</b>.", $feed_url));
						break;
					case 3:
						print_error(T_sprintf("No feeds found in <b>%s</b>.", $feed_url));
						break;
					case 4:
						$feed_urls = $rc["feeds"];
						break;
					case 5:
						print_error(T_sprintf("Could not subscribe to <b>%s</b>.<br>Can't download the Feed URL.", $feed_url));
						break;
				}

				if ($feed_urls) {
					?>
					<form method='post' action='public.php'>
						<?= \Controls\public_method_tags($this, "subscribe") ?>
						<?= \Controls\hidden_tag("csrf_token", $_SESSION["csrf_token"]) ?>

						<fieldset>
							<label style='display : inline'><?= __("Multiple feed URLs found:") ?></label>
							<select name='feed_url' dojoType='dijit.form.Select'>
							<?php foreach ($feed_urls as $url => $name) { ?>
								<option value="<?= htmlspecialchars($url) ?>"><?= htmlspecialchars($name) ?></option>
							<?php } ?>
							</select>
						</fieldset>

						<button class='alt-primary' dojoType='dijit.form.Button' type='submit'><?= __("Subscribe to selected feed") ?></button>
						<a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a>
					</form>
					<?php
				}

				if ($rc['code'] <= 2) {
					$feed_id = Feeds::_find_by_url($feed_url, $_SESSION["uid"]);
				} else {
					$feed_id = 0;
				}

				if ($feed_id) {
					?>
					<form method='GET' action="<?= htmlspecialchars(Config::get_self_url() . "/prefs.php") ?>">
						<input type='hidden' name='tab' value='feeds'>
						<input type='hidden' name='method' value='editfeed'>
						<input type='hidden' name='methodparam' value="<?= $feed_id ?>">
						<button dojoType='dijit.form.Button' class='alt-info' type='submit'><?= __("Edit subscription options") ?></button>
						<a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a>
					</form>
					<?php
				} else if (!$feed_urls) {
					?>
					<a href='index.php'><?= __("Return to Tiny Tiny RSS") ?></a>
					<?php
				}
			}
			?>
			</div>
		</div>
		</body>
		</html>
			<?php
		} else {
			Handler_Public::_render_login_form($this->host->get_public_method_url($this, "subscribe"));
		}
	}

	function sharepopup() : void {
		if (Config::get(Config::SINGLE_USER_MODE)) {
			UserHelper::login_sequence();
		}

		header('Content-Type: text/html; charset=utf-8');
		?>
		<!DOCTYPE html>
		<html>
		<head>
			<title><?= __("Share with Tiny Tiny RSS") ?></title>
			<?= javascript_tag("lib/dojo/dojo.js") ?>
			<?= javascript_tag("js/utility.js") ?>
			<?= javascript_tag("js/common.js") ?>
			<?= javascript_tag("lib/dojo/tt-rss-layer.js") ?>
			<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
			<link rel="shortcut icon" type="image/png" href="images/favicon.png">
			<link rel="icon" type="image/png" sizes="72x72" href="images/favicon-72px.png">
			<style type="text/css">
				@media (prefers-color-scheme: dark) {
					body {
						background : #303030;
					}
				}

				body.css_loading * {
					display : none;
				}
			</style>
		</head>
		<body class='flat ttrss_utility share_popup css_loading'>
			<script type="text/javascript">
				const UtilityApp = {
					init: function() {
						require(['dojo/parser', "dojo/ready", 'dijit/form/Button','dijit/form/CheckBox', 'dijit/form/Form',
							'dijit/form/Select','dijit/form/TextBox','dijit/form/ValidationTextBox'], function(parser, ready){
							ready(function() {
								parser.parse();

								/* new Ajax.Autocompleter('labels_value', 'labels_choices',
									"backend.php?op=rpc&method=completeLabels",
									{ tokens: ',', paramName: "search" }); */
								});
						});
					}
				};
			</script>
		<div class="content">

		<?php
			if ($_SESSION["uid"]) {

				$action = clean($_REQUEST["action"] ?? "");

				if ($action == 'share') {

					$title = strip_tags(clean($_REQUEST["title"]));
					$url = strip_tags(clean($_REQUEST["url"]));
					$content = strip_tags(clean($_REQUEST["content"]));
					$labels = strip_tags(clean($_REQUEST["labels"]));

					Article::_create_published_article($title, $url, $content, $labels,
						$_SESSION["uid"]);

					?>
					<script type="text/javascript">
						window.close();
					</script>
					<?php

				} else {
					$title = htmlspecialchars(clean($_REQUEST["title"]));
					$url = htmlspecialchars(clean($_REQUEST["url"]));

					?>
					<form method='post' action='public.php'>

						<?= \Controls\public_method_tags($this, "sharepopup") ?>
						<?= \Controls\hidden_tag("csrf_token", $_SESSION["csrf_token"]) ?>
						<?= \Controls\hidden_tag("action", "share") ?>

						<fieldset>
							<label><?= __("Title:") ?></label>
							<input style='width : 270px' dojoType='dijit.form.TextBox' name='title' value="<?= $title ?>">
						</fieldset>

						<fieldset>
							<label><?= __("URL:") ?></label>
							<input style='width : 270px' name='url' dojoType='dijit.form.TextBox' value="<?= $url ?>">
						</fieldset>

						<fieldset>
							<label><?= __("Content:") ?></label>
							<input style='width : 270px' name='content' dojoType='dijit.form.TextBox' value="">
						</fieldset>

						<fieldset>
							<label><?= __("Labels:") ?></label>
							<input style='width : 270px' name='labels' dojoType='dijit.form.TextBox' id="labels_value"
								placeholder='Alpha, Beta, Gamma' value="">
							<!-- <div class="autocomplete" id="labels_choices"
								style="display : block"></div> -->
						</fieldset>

						<hr/>

						<fieldset>
							<?= \Controls\submit_tag(__("Share")) ?>
							<?= \Controls\button_tag(__("Cancel"), "", ["onclick" => "window.close()"]) ?>
							<span class="text-muted small"><?= __("Shared article will appear in the Published feed.") ?></span>
						</fieldset>

					</form>
					<?php

				}

			} else {
				$return_to = $this->host->get_public_method_url($this, "sharepopup");
			?>

			<?= format_error("Not logged in") ?>

			<form action="public.php?return=<?= urlencode($return_to) ?>" method="post">

				<input type="hidden" name="op" value="login">

				<fieldset>
					<label><?= __("Login:") ?></label>
					<input name="login" id="login" dojoType="dijit.form.TextBox" type="text"
							onchange="fetchProfiles()" onfocus="fetchProfiles()" onblur="fetchProfiles()"
							required="1" value="<?= $_SESSION["fake_login"] ?>" />
				</fieldset>

				<fieldset>
					<label><?= __("Password:") ?></label>

					<input type="password" name="password" required="1"
							dojoType="dijit.form.TextBox"
							class="input input-text"
							value="<?= $_SESSION["fake_password"] ?>"/>
				</fieldset>

				<hr/>

				<fieldset>
					<label> </label>

					<button dojoType="dijit.form.Button" type="submit" class="alt-primary"><?= __('Log in') ?></button>
				</fieldset>

			</form>
			<?php
			}
		?>
		</div>
		</body>
		</html>
		<?php
	}

	function hook_prefs_tab($args) {
		if ($args != "prefFeeds")
			return;

			$bm_subscribe_url = $this->host->get_public_method_url($this, "subscribe");
			$bm_share_url = $this->host->get_public_method_url($this, "sharepopup");

			$confirm_str = str_replace("'", "\'", __('Subscribe to %s in Tiny Tiny RSS?'));

			$bm_subscribe_url = htmlspecialchars("javascript:{if(confirm('$confirm_str'.replace('%s',window.location.href)))window.location.href='$bm_subscribe_url&feed_url='+encodeURIComponent(window.location.href)}");
			$bm_share_url = htmlspecialchars("javascript:(function(){var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='$bm_share_url',l=d.location,e=encodeURIComponent,g=f+'&title='+((e(s))?e(s):e(document.title))+'&url='+e(l.href);function a(){if(!w.open(g,'t','toolbar=0,resizable=0,scrollbars=1,status=1,width=500,height=250')){l.href=g;}}a();})()");
		?>

		<div dojoType="dijit.layout.AccordionPane"
			title="<i class='material-icons'>bookmark</i> <?= __('Bookmarklets') ?>">

			<h3><?= __("Drag the link below to your browser toolbar, open the feed you're interested in in your browser and click on the link to subscribe to it.") ?></h3>

			<label class='dijitButton'>
				<a href="<?= $bm_subscribe_url ?>"><?= __('Subscribe in Tiny Tiny RSS') ?></a>
			</label>

			<h3><?= __("Use this bookmarklet to publish arbitrary pages using Tiny Tiny RSS") ?></h3>

			<label class='dijitButton'>
				<a href="<?= $bm_share_url ?>"><?= __('Share with Tiny Tiny RSS') ?></a>
			</label>

			<?= \Controls\button_tag(\Controls\icon("help") . " " . __("More info..."), "",
									["class" => 'alt-info', "onclick" => "window.open('https://tt-rss.org/wiki/ShareAnything')"]) ?>

		</div>

		<?php
	}

	function api_version() {
		return 2;
	}

}