PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Service/ReCaptcha/MailHide.php

https://github.com/grjones/qframe
PHP | 351 lines | 149 code | 45 blank | 157 comment | 14 complexity | 62725db726b316a279857904bb28d15e MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage ReCaptcha
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** @see Zend_Service_ReCaptcha */
  22. require_once 'Zend/Service/ReCaptcha.php';
  23. /**
  24. * Zend_Service_ReCaptcha_MailHide
  25. *
  26. * @category Zend
  27. * @package Zend_Service
  28. * @subpackage ReCaptcha
  29. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @version $Id: MailHide.php 20108 2010-01-06 22:05:31Z matthew $
  32. */
  33. class Zend_Service_ReCaptcha_MailHide extends Zend_Service_ReCaptcha
  34. {
  35. /**#@+
  36. * Encryption constants
  37. */
  38. const ENCRYPTION_MODE = MCRYPT_MODE_CBC;
  39. const ENCRYPTION_CIPHER = MCRYPT_RIJNDAEL_128;
  40. const ENCRYPTION_BLOCK_SIZE = 16;
  41. const ENCRYPTION_IV = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
  42. /**#@-*/
  43. /**
  44. * Url to the mailhide server
  45. *
  46. * @var string
  47. */
  48. const MAILHIDE_SERVER = 'http://mailhide.recaptcha.net/d';
  49. /**
  50. * The email address to protect
  51. *
  52. * @var string
  53. */
  54. protected $_email = null;
  55. /**
  56. * @var Zend_Validate_Interface
  57. */
  58. protected $_emailValidator;
  59. /**
  60. * Binary representation of the private key
  61. *
  62. * @var string
  63. */
  64. protected $_privateKeyPacked = null;
  65. /**
  66. * The local part of the email
  67. *
  68. * @var string
  69. */
  70. protected $_emailLocalPart = null;
  71. /**
  72. * The domain part of the email
  73. *
  74. * @var string
  75. */
  76. protected $_emailDomainPart = null;
  77. /**
  78. * Local constructor
  79. *
  80. * @param string $publicKey
  81. * @param string $privateKey
  82. * @param string $email
  83. * @param array|Zend_Config $options
  84. */
  85. public function __construct($publicKey = null, $privateKey = null, $email = null, $options = null)
  86. {
  87. /* Require the mcrypt extension to be loaded */
  88. $this->_requireMcrypt();
  89. /* If options is a Zend_Config object we want to convert it to an array so we can merge it with the default options */
  90. if ($options instanceof Zend_Config) {
  91. $options = $options->toArray();
  92. }
  93. /* Merge if needed */
  94. if (is_array($options)) {
  95. $options = array_merge($this->getDefaultOptions(), $options);
  96. } else {
  97. $options = $this->getDefaultOptions();
  98. }
  99. parent::__construct($publicKey, $privateKey, null, $options);
  100. if ($email !== null) {
  101. $this->setEmail($email);
  102. }
  103. }
  104. /**
  105. * Get emailValidator
  106. *
  107. * @return Zend_Validate_Interface
  108. */
  109. public function getEmailValidator()
  110. {
  111. if (null === $this->_emailValidator) {
  112. require_once 'Zend/Validate/EmailAddress.php';
  113. $this->setEmailValidator(new Zend_Validate_EmailAddress());
  114. }
  115. return $this->_emailValidator;
  116. }
  117. /**
  118. * Set email validator
  119. *
  120. * @param Zend_Validate_Interface $validator
  121. * @return Zend_Service_ReCaptcha_MailHide
  122. */
  123. public function setEmailValidator(Zend_Validate_Interface $validator)
  124. {
  125. $this->_emailValidator = $validator;
  126. return $this;
  127. }
  128. /**
  129. * See if the mcrypt extension is available
  130. *
  131. * @throws Zend_Service_ReCaptcha_MailHide_Exception
  132. */
  133. protected function _requireMcrypt()
  134. {
  135. if (!extension_loaded('mcrypt')) {
  136. /** @see Zend_Service_ReCaptcha_MailHide_Exception */
  137. require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
  138. throw new Zend_Service_ReCaptcha_MailHide_Exception('Use of the Zend_Service_ReCaptcha_MailHide component requires the mcrypt extension to be enabled in PHP');
  139. }
  140. }
  141. /**
  142. * Serialize as string
  143. *
  144. * When the instance is used as a string it will display the email address. Since we can't
  145. * throw exceptions within this method we will trigger a user warning instead.
  146. *
  147. * @return string
  148. */
  149. public function __toString()
  150. {
  151. try {
  152. $return = $this->getHtml();
  153. } catch (Exception $e) {
  154. $return = '';
  155. trigger_error($e->getMessage(), E_USER_WARNING);
  156. }
  157. return $return;
  158. }
  159. /**
  160. * Get the default set of parameters
  161. *
  162. * @return array
  163. */
  164. public function getDefaultOptions()
  165. {
  166. return array(
  167. 'encoding' => 'UTF-8',
  168. 'linkTitle' => 'Reveal this e-mail address',
  169. 'linkHiddenText' => '...',
  170. 'popupWidth' => 500,
  171. 'popupHeight' => 300,
  172. );
  173. }
  174. /**
  175. * Override the setPrivateKey method
  176. *
  177. * Override the parent method to store a binary representation of the private key as well.
  178. *
  179. * @param string $privateKey
  180. * @return Zend_Service_ReCaptcha_MailHide
  181. */
  182. public function setPrivateKey($privateKey)
  183. {
  184. parent::setPrivateKey($privateKey);
  185. /* Pack the private key into a binary string */
  186. $this->_privateKeyPacked = pack('H*', $this->_privateKey);
  187. return $this;
  188. }
  189. /**
  190. * Set the email property
  191. *
  192. * This method will set the email property along with the local and domain parts
  193. *
  194. * @param string $email
  195. * @return Zend_Service_ReCaptcha_MailHide
  196. */
  197. public function setEmail($email)
  198. {
  199. $this->_email = $email;
  200. $validator = $this->getEmailValidator();
  201. if (!$validator->isValid($email)) {
  202. require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
  203. throw new Zend_Service_ReCaptcha_MailHide_Exception('Invalid email address provided');
  204. }
  205. $emailParts = explode('@', $email, 2);
  206. /* Decide on how much of the local part we want to reveal */
  207. if (strlen($emailParts[0]) <= 4) {
  208. $emailParts[0] = substr($emailParts[0], 0, 1);
  209. } else if (strlen($emailParts[0]) <= 6) {
  210. $emailParts[0] = substr($emailParts[0], 0, 3);
  211. } else {
  212. $emailParts[0] = substr($emailParts[0], 0, 4);
  213. }
  214. $this->_emailLocalPart = $emailParts[0];
  215. $this->_emailDomainPart = $emailParts[1];
  216. return $this;
  217. }
  218. /**
  219. * Get the email property
  220. *
  221. * @return string
  222. */
  223. public function getEmail()
  224. {
  225. return $this->_email;
  226. }
  227. /**
  228. * Get the local part of the email address
  229. *
  230. * @return string
  231. */
  232. public function getEmailLocalPart()
  233. {
  234. return $this->_emailLocalPart;
  235. }
  236. /**
  237. * Get the domain part of the email address
  238. *
  239. * @return string
  240. */
  241. public function getEmailDomainPart()
  242. {
  243. return $this->_emailDomainPart;
  244. }
  245. /**
  246. * Get the HTML code needed for the mail hide
  247. *
  248. * @param string $email
  249. * @return string
  250. * @throws Zend_Service_ReCaptcha_MailHide_Exception
  251. */
  252. public function getHtml($email = null)
  253. {
  254. if ($email !== null) {
  255. $this->setEmail($email);
  256. } elseif (null === ($email = $this->getEmail())) {
  257. /** @see Zend_Service_ReCaptcha_MailHide_Exception */
  258. require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
  259. throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing email address');
  260. }
  261. if ($this->_publicKey === null) {
  262. /** @see Zend_Service_ReCaptcha_MailHide_Exception */
  263. require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
  264. throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing public key');
  265. }
  266. if ($this->_privateKey === null) {
  267. /** @see Zend_Service_ReCaptcha_MailHide_Exception */
  268. require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
  269. throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing private key');
  270. }
  271. /* Generate the url */
  272. $url = $this->_getUrl();
  273. $enc = $this->getOption('encoding');
  274. /* Genrate the HTML used to represent the email address */
  275. $html = htmlentities($this->getEmailLocalPart(), ENT_COMPAT, $enc)
  276. . '<a href="'
  277. . htmlentities($url, ENT_COMPAT, $enc)
  278. . '" onclick="window.open(\''
  279. . htmlentities($url, ENT_COMPAT, $enc)
  280. . '\', \'\', \'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='
  281. . $this->_options['popupWidth']
  282. . ',height='
  283. . $this->_options['popupHeight']
  284. . '\'); return false;" title="'
  285. . $this->_options['linkTitle']
  286. . '">' . $this->_options['linkHiddenText'] . '</a>@'
  287. . htmlentities($this->getEmailDomainPart(), ENT_COMPAT, $enc);
  288. return $html;
  289. }
  290. /**
  291. * Get the url used on the "hidden" part of the email address
  292. *
  293. * @return string
  294. */
  295. protected function _getUrl()
  296. {
  297. /* Figure out how much we need to pad the email */
  298. $numPad = self::ENCRYPTION_BLOCK_SIZE - (strlen($this->_email) % self::ENCRYPTION_BLOCK_SIZE);
  299. /* Pad the email */
  300. $emailPadded = str_pad($this->_email, strlen($this->_email) + $numPad, chr($numPad));
  301. /* Encrypt the email */
  302. $emailEncrypted = mcrypt_encrypt(self::ENCRYPTION_CIPHER, $this->_privateKeyPacked, $emailPadded, self::ENCRYPTION_MODE, self::ENCRYPTION_IV);
  303. /* Return the url */
  304. return self::MAILHIDE_SERVER . '?k=' . $this->_publicKey . '&c=' . strtr(base64_encode($emailEncrypted), '+/', '-_');
  305. }
  306. }