1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Joas Schilling <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Activity; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use OCP\IL10N; |
26
|
|
|
|
27
|
|
|
class PlainTextParser { |
28
|
|
|
|
29
|
|
|
/** @var IL10N */ |
30
|
|
|
protected $l; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param IL10N $l |
34
|
|
|
*/ |
35
|
8 |
|
public function __construct(IL10N $l) { |
36
|
8 |
|
$this->l = $l; |
37
|
8 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Parse the parameters in the subject and message |
41
|
|
|
* |
42
|
|
|
* @param string $message |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
5 |
|
public function parseMessage($message) { |
46
|
5 |
|
$message = $this->parseCollections($message); |
47
|
5 |
|
$message = $this->parseParameters($message); |
48
|
5 |
|
return $message; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Parse collections |
53
|
|
|
* |
54
|
|
|
* @param string $message |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
5 |
|
protected function parseCollections($message) { |
58
|
|
|
return preg_replace_callback('/<collection>(.*?)<\/collection>/', function($match) { |
59
|
|
|
$parameterList = explode('><', $match[1]); |
60
|
|
|
$parameterListLength = sizeof($parameterList); |
61
|
|
|
|
62
|
|
|
$parameters = []; |
63
|
|
|
for ($i = 0; $i < $parameterListLength; $i++) { |
64
|
|
|
$parameter = $parameterList[$i]; |
65
|
|
|
if ($i > 0) { |
66
|
|
|
$parameter = '<' . $parameter; |
67
|
|
|
} |
68
|
|
|
if ($i + 1 < $parameterListLength) { |
69
|
|
|
$parameter = $parameter . '>'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$parameters[] = $this->parseParameters($parameter); |
73
|
|
|
} |
74
|
|
|
if ($parameterListLength === 1) { |
75
|
|
|
return array_pop($parameters); |
76
|
|
|
} else { |
77
|
|
|
$lastParameter = array_pop($parameters); |
78
|
|
|
return $this->l->t('%s and %s', [ |
79
|
|
|
implode($this->l->t(', '), $parameters), |
80
|
|
|
$lastParameter, |
81
|
|
|
]); |
82
|
|
|
} |
83
|
5 |
|
}, $message); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Parse the parameters in the subject and message |
88
|
|
|
* |
89
|
|
|
* @param string $message |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
5 |
|
protected function parseParameters($message) { |
93
|
5 |
|
$message = $this->parseUntypedParameters($message); |
94
|
5 |
|
$message = $this->parseUserParameters($message); |
95
|
5 |
|
$message = $this->parseFederatedCloudIDParameters($message); |
96
|
5 |
|
$message = $this->parseFileParameters($message); |
97
|
5 |
|
return $message; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Display the parameter value |
102
|
|
|
* |
103
|
|
|
* @param string $message |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
5 |
|
protected function parseUntypedParameters($message) { |
107
|
|
|
return preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function($match) { |
108
|
|
|
return $match[1]; |
109
|
5 |
|
}, $message); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Display the users display name |
114
|
|
|
* |
115
|
|
|
* @param string $message |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
5 |
|
protected function parseUserParameters($message) { |
119
|
|
|
return preg_replace_callback('/<user\ display\-name=\"(.*?)\">(.*?)<\/user>/', function($match) { |
120
|
|
|
// We don't want HTML to work, but quote signs are okay. |
121
|
3 |
|
return str_replace('"', '"', $match[1]); |
122
|
5 |
|
}, $message); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Display the full cloud id |
127
|
|
|
* |
128
|
|
|
* @param string $message |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
5 |
|
protected function parseFederatedCloudIDParameters($message) { |
132
|
|
|
return preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function($match) { |
133
|
|
|
return $match[1]; |
134
|
5 |
|
}, $message); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Display the path for files |
139
|
|
|
* |
140
|
|
|
* @param string $message |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
protected function parseFileParameters($message) { |
144
|
5 |
|
return preg_replace_callback('/<file\ link=\"(.*?)\"\ id=\"(.*?)\">(.*?)<\/file>/', function($match) { |
145
|
4 |
|
return $match[3]; |
146
|
5 |
|
}, $message); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|