1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8class Tiki_Profile_InstallHandler_Webmail extends Tiki_Profile_InstallHandler
9{
10	function getData()
11	{
12		if ($this->data) {
13			return $this->data;
14		}
15
16		$defaults = [
17			'to' => '',
18			'cc' => '',
19			'bcc' => '',
20			'from' => '',             // specify a from email address to search for correct profile in cypht settings
21			'subject' => '',
22			'body' => '',
23			'fattId' => null,         // add a File Gallery file as an attachment
24			'pageaftersend' => null,  // defines wiki page to go to after webmail is sent
25			'html' => 'y',
26		];
27
28		$data = array_merge($defaults, $this->obj->getData());
29
30		return $this->data = $data;
31	}
32
33	function canInstall()
34	{
35		global $user;
36
37		$data = $this->getData();
38
39		if (! isset($data['to']) && ! isset($data['cc']) && ! isset($data['bcc']) && ! isset($data['subject']) && ! isset($data['body'])) {
40			return false;	// nothing specified?
41		}
42
43		return true;
44	}
45
46	function _install()
47	{
48		global $tikilib, $tikipath, $user;
49		$data = $this->getData();
50
51		$this->replaceReferences($data);
52
53		if (strpos($data['body'], 'wikidirect:') === 0) {
54			$pageName = substr($this->content, strlen('wikidirect:'));
55			$data['body'] = $this->obj->getProfile()->getPageContent($pageName);
56		}
57
58		if (! $data['html']) {
59			$data['body'] = strip_tags($data['body']);
60		}
61		$data['to']      = trim(str_replace(["\n","\r"], "", html_entity_decode(strip_tags($data['to']))), ' ,');
62		$data['cc']      = trim(str_replace(["\n","\r"], "", html_entity_decode(strip_tags($data['cc']))), ' ,');
63		$data['bcc']     = trim(str_replace(["\n","\r"], "", html_entity_decode(strip_tags($data['bcc']))), ' ,');
64		$data['from']    = trim(str_replace(["\n","\r"], "", html_entity_decode(strip_tags($data['from']))), ' ,');
65		$data['subject'] = trim(str_replace(["\n","\r"], "", html_entity_decode(strip_tags($data['subject']))));
66
67		$smtp_id = '';
68		if ($data['from']) {
69			$config = TikiLib::lib('tiki')->get_user_preference($user, 'cypht_user_config');
70			$config = json_decode($config);
71			if (! empty($config->smtp_servers)) {
72				foreach ($config->smtp_servers as $key => $server) {
73					if ($server->name == $data['from']) {
74						$smtp_id = $key;
75					}
76				}
77			}
78		}
79
80		$drafts = $_SESSION['cypht']['compose_drafts'] ?? [];
81		$drafts[] = [
82			'draft_to' => $data['to'],
83			'draft_cc' => $data['cc'],
84			'draft_bcc' => $data['bcc'],
85			'draft_subject' => $data['subject'],
86			'draft_body' => $data['body'],
87			'draft_fattId' => $data['fattId'],
88			'draft_smtp' => $smtp_id,
89		];
90		$draft_id = count($drafts)-1;
91		$_SESSION['cypht']['compose_drafts'] = $drafts;
92		$_SESSION['cypht']['pageaftersend'] = $data['pageaftersend'];
93
94		$webmailUrl = $tikilib->tikiUrl(
95			'tiki-webmail.php',
96			[
97				'page' => 'compose',
98				'draft_id' => $draft_id,
99				'useHTML' => $data['html'] ? 'y' : 'n'
100			]
101		);
102
103		header('Location: ' . $webmailUrl);
104		exit;	// means this profile never gets "remembered" - a good thing?
105	}
106}
107