| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * ownCloud | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author Thomas Müller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright 2014 Thomas Müller [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * You should have received a copy of the GNU Affero General Public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * License along with this library.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace OCA\Mail\Service\HtmlPurify; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use HTMLPurifier_Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use HTMLPurifier_URI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use HTMLPurifier_URIFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use HTMLPurifier_URIParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use OCP\IRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use OCP\IURLGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class TransformURLScheme extends HTMLPurifier_URIFilter { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	public $name = 'TransformURLScheme'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	public $post = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	/** @var IURLGenerator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	private $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** @var IRequest */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	private $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * @var \Closure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	private $mapCidToAttachmentId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  | 	public function __construct($messageParameters, \Closure $mapCidToAttachmentId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		IURLGenerator $urlGenerator, IRequest $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  | 		$this->messageParameters = $messageParameters; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  | 		$this->mapCidToAttachmentId = $mapCidToAttachmentId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  | 		$this->urlGenerator = $urlGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  | 		$this->request = $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * Transformator which will rewrite all HTTPS and HTTP urls to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param \HTMLPurifier_URI $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @param HTMLPurifier_Config $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @param \HTMLPurifier_Context $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  | 	public function filter(&$uri, $config, $context) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		/** @var \HTMLPurifier_Context $context */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		/** @var \HTMLPurifier_Config $config */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		// Only HTTPS and HTTP urls should get rewritten | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  | 		if ($uri->scheme === 'https' || $uri->scheme === 'http') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  | 			$uri = $this->filterHttp($uri, $context); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  | 		if ($uri->scheme === 'cid') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			$attachmentId = $this->mapCidToAttachmentId->__invoke($uri->path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			if (is_null($attachmentId)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 				return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			$this->messageParameters['attachmentId'] = $attachmentId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			$imgUrl = $this->urlGenerator->linkToRouteAbsolute('mail.messages.downloadAttachment', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 				$this->messageParameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			$parser = new HTMLPurifier_URIParser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$uri = $parser->parse($imgUrl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * @param $uri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * @param $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * @return HTMLPurifier_URI | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  | 	private function filterHttp(&$uri, $context) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  | 		$originalURL = urlencode($uri->scheme . '://' . $uri->host . $uri->path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  | 		if ($uri->query !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  | 			$originalURL = $originalURL . urlencode('?' . $uri->query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		// Get the HTML attribute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  | 		$element = $context->get('CurrentAttr'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		// If element is of type "href" it is most likely a link that should get redirected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		// otherwise it's an element that we send through our proxy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  | 		if ($element === 'href') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  | 			$uri = new \HTMLPurifier_URI( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  | 				$this->getServerProtocol(), null, $this->getServerHost(), null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  | 				$this->urlGenerator->linkToRoute('mail.proxy.redirect'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  | 				'src=' . $originalURL, null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  | 			return $uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  | 			$uri = new \HTMLPurifier_URI( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  | 				$this->getServerProtocol(), null, $this->getServerHost(), null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  | 				$this->urlGenerator->linkToRoute('mail.proxy.proxy'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  | 				'src=' . $originalURL . '&requesttoken=' . \OC::$server->getSession()->get('requesttoken'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  | 				null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  | 			return $uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @todo remove version-hack once core 8.1+ is supported | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 114 | 1 | View Code Duplication | 	private function getServerProtocol() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  | 		$ocVersion = \OC::$server->getConfig()->getSystemValue('version', '0.0.0'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  | 		if (version_compare($ocVersion, '8.2.0', '<')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			return Util::getServerProtocol(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  | 		return $this->request->getServerProtocol(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * @todo remove version-hack once core 8.1+ is supported | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | 	 */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 126 | 1 | View Code Duplication | 	private function getServerHost() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 127 | 1 |  | 		$ocVersion = \OC::$server->getConfig()->getSystemValue('version', '0.0.0'); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 | 1 |  | 		if (version_compare($ocVersion, '8.2.0', '<')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  | 			return Util::getServerHost(); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 131 | 1 |  | 		return $this->request->getServerHost(); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 134 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: