| 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 | 22 |  | 	public function __construct(IL10N $l) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 22 |  | 		$this->l = $l; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 22 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * Parse the parameters in the subject and message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 4 |  | 	public function parseMessage($message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 4 |  | 		$message = $this->parseCollections($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 4 |  | 		$message = $this->parseParameters($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 4 |  | 		return $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * Parse collections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	protected function parseCollections($message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 4 |  | 		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 | 4 |  | 		}, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * Parse the parameters in the subject and message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 4 |  | 	protected function parseParameters($message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 4 |  | 		$message = $this->parseUntypedParameters($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  | 		$message = $this->parseUserParameters($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  | 		$message = $this->parseFederatedCloudIDParameters($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  | 		$message = $this->parseFileParameters($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 4 |  | 		return $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 * Display the parameter value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	protected function parseUntypedParameters($message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 4 |  | 		return preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function($match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			return $match[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 4 |  | 		}, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * Display the users display name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	protected function parseUserParameters($message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 4 |  | 		return preg_replace_callback('/<user\ display\-name=\"(.*?)\">(.*?)<\/user>/', function($match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			// We don't want HTML to work, but quote signs are okay. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 3 |  | 			return str_replace('"', '"', $match[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 4 |  | 		}, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * Display the full cloud id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 131 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  | 	protected function parseFederatedCloudIDParameters($message) { | 
            
                                                                        
                            
            
                                    
            
            
                | 133 | 4 |  | 		return preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function($match) { | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  | 			return $match[1]; | 
            
                                                                        
                            
            
                                    
            
            
                | 135 | 4 |  | 		}, $message); | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 * Display the path for files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	protected function parseFileParameters($message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 4 |  | 		return preg_replace_callback('/<file\ link=\"(.*?)\"\ id=\"(.*?)\">(.*?)<\/file>/', function($match) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 4 |  | 			return $match[3]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 4 |  | 		}, $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |