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
8//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
10	header("location: index.php");
11	exit;
12}
13
14/**
15 *
16 */
17class UserMailinLib extends TikiLib
18{
19	/**
20	 * locate_struct will find matching structure routing rules, given the email subject and body
21	 *
22	 * @param mixed $user Resolved Tiki user from email address
23	 * @param mixed $subject Email subject
24	 * @param mixed $body Email body
25	 * @return mixed array - 'data', 'cant'
26	 *
27	 */
28	function locate_struct($user, $subject, &$body)
29	{
30		$result = [];
31		$routes = $this->list_user_mailin_struct($user);
32		foreach ($routes['data'] as $r) {
33			if ($r['username'] == $user) {
34				if ($r['is_active'] === 'y') {
35					if ($this->matchPattern($r['subj_pattern'], $subject, $r['body_pattern'], $body)) {
36						// Found a matching routing pattern for the user
37						$result[] = $r;
38					}
39				}
40			}
41		}
42
43		$retval = [];
44		$retval["data"] = $result;
45		$retval["cant"] = count($result);
46		return $retval;
47	}
48
49	/**
50	 * matchPattern
51	 * Either subj_pattern or body_pattern or both must be specified to find a match.
52	 * If both are specified, both must match. If only one is specified, it must match. The empty pattern is ignored.
53	 *
54	 * @param mixed $subj_pattern Pattern to match in subject
55	 * @param mixed $subject The email subject text
56	 * @param mixed $body_pattern Pattern to match in the body
57	 * @param mixed $body The email body
58	 * @return mixed boolean
59	 *
60	 */
61	private function matchPattern($subj_pattern, $subject, $body_pattern, &$body)
62	{
63		$rc1 = null;
64		$rc2 = null;
65		if (! empty($subj_pattern)) {
66			if (stripos($subject, $subj_pattern, 0) !== false) {
67				$rc1 = true;
68			} else {
69				$rc1 = false;
70			}
71		}
72		if (! empty($body_pattern)) {
73			if (stripos($body, $body_pattern, 0) !== false) {
74				$rc2 = true;
75			} else {
76				$rc2 = false;
77			}
78		}
79		if ($rc1 == null && $rc2 == null) {
80			return false;
81		}
82		$rc1 = $rc1 == null ? $rc2 : $rc1;
83		$rc2 = $rc2 == null ? $rc1 : $rc2;
84		return $rc1 && $rc2;
85	}
86
87	/**
88	 * @param $offset
89	 * @param $maxRecords
90	 * @param $sort_mode
91	 * @param $find
92	 * @return array
93	 */
94	function list_user_mailin_struct($user, $maxRecords = -1, $offset = 0)
95	{
96		$bindvars = [$user];
97		$query = "select u.email, mailin.*, p.pageName, s2.page_ref_id as page_struct_refid, s2.parent_id as page_struct_parentid, s.page_ref_id, s.parent_id , p2.pageName as structName
98from `tiki_user_mailin_struct` mailin
99        left outer join `tiki_pages` p on p.`page_id` = mailin.`page_id`
100        left outer join `tiki_structures` s on s.`structure_id` = mailin.`structure_id` and s.`parent_id` = 0
101        left outer join `tiki_pages` p2 on p2.`page_id` = s.`page_id`
102        left outer join `tiki_structures` s2 on s2.`structure_id` = mailin.`structure_id` and s2.`page_id` = mailin.`page_id`
103        left outer join `users_users` u on u.login = mailin.username
104where mailin.`username` = ?
105order by p2.pageName, p.pageName";
106
107		$result = $this->query($query, $bindvars, $maxRecords, $offset);
108
109		$retval = [];
110		$retval["data"] = $result->result;
111		$retval["cant"] = $result->numrows;
112		return $retval;
113	}
114
115
116	function list_all_user_mailin_struct($onlyActive = true, $maxRecords = -1, $offset = 0)
117	{
118		$sqlOnlyActive = '';
119		if ($onlyActive) {
120			$sqlOnlyActive = " and mailin.is_active = 'y' ";
121		}
122		$query = "select u.email, mailin.*, p.pageName, s2.page_ref_id as page_struct_refid, s2.parent_id as page_struct_parentid, s.page_ref_id, s.parent_id , p2.pageName as structName
123from `tiki_user_mailin_struct` mailin
124        left outer join `tiki_pages` p on p.`page_id` = mailin.`page_id`
125        left outer join `tiki_structures` s on s.`structure_id` = mailin.`structure_id` and s.`parent_id` = 0
126        left outer join `tiki_pages` p2 on p2.`page_id` = s.`page_id`
127        left outer join `tiki_structures` s2 on s2.`structure_id` = mailin.`structure_id` and s2.`page_id` = mailin.`page_id`
128        left outer join `users_users` u on u.login = mailin.username
129where 1 = 1
130" . $sqlOnlyActive . "
131order by mailin.username, p2.pageName, p.pageName
132";
133		$bindvars = [];
134		$result = $this->query($query, $bindvars, $maxRecords, $offset);
135
136		$retval = [];
137		$retval["data"] = $result->result;
138		$retval["cant"] = $result->numrows;
139		return $retval;
140	}
141
142	function add_user_mailin_struct($username, $subj_pattern, $body_pattern, $structure_id, $page_id, $is_active)
143	{
144		$bindvars = [$username, $subj_pattern, $body_pattern, (int)$structure_id, (int)$page_id, $is_active];
145		$query = "insert into `tiki_user_mailin_struct`(`username`,`subj_pattern`,`body_pattern`,`structure_id`,`page_id`,`is_active`) values(?,?,?,?,?,?)";
146		$result = $this->query($query, $bindvars);
147	}
148
149	function update_user_mailin_struct($mailin_struct_id, $username, $subj_pattern, $body_pattern, $structure_id, $page_id, $is_active)
150	{
151		if ($mailin_struct_id) {
152			$bindvars = [$username, $subj_pattern, $body_pattern, (int)$structure_id, (int)$page_id, $is_active, (int)$mailin_struct_id];
153			$query = "update `tiki_user_mailin_struct` set `username`=?, `subj_pattern`=?, `body_pattern`=?, `structure_id`=?, `page_id`=?, `is_active`=? where `mailin_struct_id`=?";
154			$result = $this->query($query, $bindvars);
155			return true;
156		}
157		return false;
158	}
159
160	function delete_user_mailin_struct($mailin_struct_id)
161	{
162		if ($mailin_struct_id) {
163			$bindvars = [(int)$mailin_struct_id];
164			$query = "delete from `tiki_user_mailin_struct` where `mailin_struct_id`=?";
165			$result = $this->query($query, $bindvars, -1, -1, false);
166			return true;
167		}
168		return false;
169	}
170}
171