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