summaryrefslogtreecommitdiff
path: root/init.php
blob: 6b0dc1c4db3797d080fb107244a3fb7bd7e11c8a (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
<?php
class Mail extends Plugin {

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

	function about() {
		return array(null,
			"Share article via email",
			"fox");
	}

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

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

	function get_js() {
		return file_get_contents(__DIR__ . "/mail.js");
	}

	function hook_headline_toolbar_select_menu_item2($feed_id, $is_cat) {
		return "<option value='Plugins.Mail.send()'>".__('Forward by email')."</option>";
	}

	function save() : void {
		$addresslist = $_POST["addresslist"];

		$this->host->set($this, "addresslist", $addresslist);

		echo __("Mail addresses saved.");
	}

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

		$addresslist = $this->host->get($this, "addresslist");

		?>

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

			<form dojoType="dijit.form.Form">
				<?= \Controls\pluginhandler_tags($this, "save") ?>

				<script type="dojo/method" event="onSubmit" args="evt">
					evt.preventDefault();
					if (this.validate()) {
						Notify.progress('Saving data...', true);
						xhr.post("backend.php", this.getValues(), (reply) => {
							Notify.info(reply);
						})
					}
				</script>

				<header><?= __("You can set predefined email addressed here (comma-separated list):") ?></header>

				<textarea dojoType="dijit.form.SimpleTextarea" style='font-size : 12px; width : 50%' rows="3"
					name='addresslist'><?= $addresslist ?></textarea>

				<hr/>

				<?= \Controls\submit_tag(__("Save")) ?>

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

	function hook_article_button($line) {
		return "<i class='material-icons' style=\"cursor : pointer\"
					onclick=\"Plugins.Mail.send(".$line["id"].")\"
					title='".__('Forward by email')."'>mail</i>";
	}

	function emailArticle() : void {

		$ids = explode(",", clean($_REQUEST['ids']));
		$ids_qmarks = arr_qmarks($ids);


		$sth = $this->pdo->prepare("SELECT email, full_name FROM ttrss_users WHERE
			id = ?");
		$sth->execute([$_SESSION['uid']]);

		if ($row = $sth->fetch()) {
			$user_email = htmlspecialchars($row['email']);
			$user_name = htmlspecialchars($row['full_name']);
		} else {
			$user_name = "";
			$user_email = "";
		}

		if (!$user_name)
			$user_name = $_SESSION['name'];

		$tpl = new Templator();

		$tpl->readTemplateFromFile("email_article_template.txt");

		$tpl->setVariable('USER_NAME', $_SESSION["name"], true);
		$tpl->setVariable('USER_EMAIL', $user_email, true);
		$tpl->setVariable('TTRSS_HOST', $_SERVER["HTTP_HOST"], true);

		$sth = $this->pdo->prepare("SELECT DISTINCT link, content, title, note
			FROM ttrss_user_entries, ttrss_entries WHERE id = ref_id AND
			id IN ($ids_qmarks) AND owner_uid = ?");
		$sth->execute(array_merge($ids, [$_SESSION['uid']]));

		if (count($ids) > 1) {
			$subject = __("[Forwarded]") . " " . __("Multiple articles");
		} else {
			$subject = "";
		}

		while ($line = $sth->fetch()) {

			if (!$subject)
				$subject = __("[Forwarded]") . " " . htmlspecialchars($line["title"]);

			$tpl->setVariable('ARTICLE_TITLE', strip_tags($line["title"]));
			$tnote = strip_tags($line["note"]);
			if( $tnote != ''){
				$tpl->setVariable('ARTICLE_NOTE', $tnote, true);
				$tpl->addBlock('note');
			}
			$tpl->setVariable('ARTICLE_URL', strip_tags($line["link"]));

			$tpl->addBlock('article');
		}

		$tpl->addBlock('email');

		$content = "";
		$tpl->generateOutputToString($content);

		$addresslist = explode(",", $this->host->get($this, "addresslist"));

		?>

		<form dojoType='dijit.form.Form'>

			<?= \Controls\pluginhandler_tags($this, "sendemail") ?>

			<?= \Controls\hidden_tag("from_email", $user_email) ?>
			<?= \Controls\hidden_tag("from_name", $user_name) ?>

			<script type='dojo/method' event='onSubmit' args='evt'>
				evt.preventDefault();
				if (this.validate()) {
					xhr.json("backend.php", this.getValues(), (reply) => {
						if (reply && reply.error)
							Notify.error(reply.error);
						else
							this.hide();
					});
				}
			</script>

			<section>
				<fieldset class='narrow'>
					<label><?= __('To:') ?></label>
					<?= \Controls\select_tag("destination", "", $addresslist,
											["style" => "width: 380px", "required" => 1, "dojoType" => "dijit.form.ComboBox"]) ?>
				</fieldset>
			</section>

			<section>
				<fieldset class='narrow'>
					<label><?= __('Subject:') ?></label>
					<input dojoType='dijit.form.ValidationTextBox' required='true'
						style='width : 380px' name='subject' value="<?= htmlspecialchars($subject) ?>" id='subject'>
				</fieldset>
			</section>

			<textarea dojoType='dijit.form.SimpleTextarea'
				style='height : 200px; font-size : 12px; width : 98%' rows="20"
				name='content'><?= $content ?></textarea>

			<footer>
				<?= \Controls\submit_tag(__('Send email')) ?>
				<?= \Controls\cancel_dialog_tag(__('Cancel')) ?>
			</footer>

		</form>
		<?php
	}

	function sendEmail() : void {
		$reply = array();

		/*$mail->AddReplyTo(strip_tags($_REQUEST['from_email']),
			strip_tags($_REQUEST['from_name']));
		//$mail->AddAddress($_REQUEST['destination']);
		$addresses = explode(';', $_REQUEST['destination']);
		foreach($addresses as $nextaddr)
			$mail->AddAddress($nextaddr);

		$mail->IsHTML(false);
		$mail->Subject = $_REQUEST['subject'];
		$mail->Body = $_REQUEST['content'];

		$rc = $mail->Send(); */

		$to = $_REQUEST["destination"];
		$subject = strip_tags($_REQUEST["subject"]);
		$message = strip_tags($_REQUEST["content"]);
		$from = strip_tags($_REQUEST["from_email"]);

		$mailer = new Mailer();

		$rc = $mailer->mail(["to_address" => $to,
			"headers" => ["Reply-To: $from"],
			"subject" => $subject,
			"message" => $message]);

		if (!$rc) {
			$reply['error'] =  $mailer->error();
		} else {
			//save_email_address($destination);
			$reply['message'] = "UPDATE_COUNTERS";
		}

		print json_encode($reply);
	}

	function api_version() {
		return 2;
	}

}