MailHide.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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-2008 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @version $Id$
  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. * Binary representation of the private key
  57. *
  58. * @var string
  59. */
  60. protected $_privateKeyPacked = null;
  61. /**
  62. * The local part of the email
  63. *
  64. * @var string
  65. */
  66. protected $_emailLocalPart = null;
  67. /**
  68. * The domain part of the email
  69. *
  70. * @var string
  71. */
  72. protected $_emailDomainPart = null;
  73. /**
  74. * Local constructor
  75. *
  76. * @param string $publicKey
  77. * @param string $privateKey
  78. * @param string $email
  79. * @param array|Zend_Config $options
  80. */
  81. public function __construct($publicKey = null, $privateKey = null, $email = null, $options = null)
  82. {
  83. /* Require the mcrypt extension to be loaded */
  84. $this->_requireMcrypt();
  85. /* If options is a Zend_Config object we want to convert it to an array so we can merge it with the default options */
  86. if ($options instanceof Zend_Config) {
  87. $options = $options->toArray();
  88. }
  89. /* Merge if needed */
  90. if (is_array($options)) {
  91. $options = array_merge($this->getDefaultOptions(), $options);
  92. } else {
  93. $options = $this->getDefaultOptions();
  94. }
  95. parent::__construct($publicKey, $privateKey, null, $options);
  96. if ($email !== null) {
  97. $this->setEmail($email);
  98. }
  99. }
  100. /**
  101. * See if the mcrypt extension is available
  102. *
  103. * @throws Zend_Service_ReCaptcha_MailHide_Exception
  104. */
  105. protected function _requireMcrypt()
  106. {
  107. if (!extension_loaded('mcrypt')) {
  108. /** @see Zend_Service_ReCaptcha_MailHide_Exception */
  109. require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
  110. throw new Zend_Service_ReCaptcha_MailHide_Exception('Use of the Zend_Service_ReCaptcha_MailHide component requires the mcrypt extension to be enabled in PHP');
  111. }
  112. }
  113. /**
  114. * Serialize as string
  115. *
  116. * When the instance is used as a string it will display the email address. Since we can't
  117. * throw exceptions within this method we will trigger a user warning instead.
  118. *
  119. * @return string
  120. */
  121. public function __toString()
  122. {
  123. try {
  124. $return = $this->getHtml();
  125. } catch (Exception $e) {
  126. $return = '';
  127. trigger_error($e->getMessage(), E_USER_WARNING);
  128. }
  129. return $return;
  130. }
  131. /**
  132. * Get the default set of parameters
  133. *
  134. * @return array
  135. */
  136. public function getDefaultOptions()
  137. {
  138. return array(
  139. 'linkTitle' => 'Reveal this e-mail address',
  140. 'linkHiddenText' => '...',
  141. 'popupWidth' => 500,
  142. 'popupHeight' => 300,
  143. );
  144. }
  145. /**
  146. * Override the setPrivateKey method
  147. *
  148. * Override the parent method to store a binary representation of the private key as well.
  149. *
  150. * @param string $privateKey
  151. * @return Zend_Service_ReCaptcha_MailHide
  152. */
  153. public function setPrivateKey($privateKey)
  154. {
  155. parent::setPrivateKey($privateKey);
  156. /* Pack the private key into a binary string */
  157. $this->_privateKeyPacked = pack('H*', $this->_privateKey);
  158. return $this;
  159. }
  160. /**
  161. * Set the email property
  162. *
  163. * This method will set the email property along with the local and domain parts
  164. *
  165. * @param string $email
  166. * @return Zend_Service_ReCaptcha_MailHide
  167. */
  168. public function setEmail($email)
  169. {
  170. $this->_email = $email;
  171. $emailParts = explode('@', $email, 2);
  172. /* Decide on how much of the local part we want to reveal */
  173. if (strlen($emailParts[0]) <= 4) {
  174. $emailParts[0] = substr($emailParts[0], 0, 1);
  175. } else if (strlen($emailParts[0]) <= 6) {
  176. $emailParts[0] = substr($emailParts[0], 0, 3);
  177. } else {
  178. $emailParts[0] = substr($emailParts[0], 0, 4);
  179. }
  180. $this->_emailLocalPart = $emailParts[0];
  181. $this->_emailDomainPart = $emailParts[1];
  182. return $this;
  183. }
  184. /**
  185. * Get the email property
  186. *
  187. * @return string
  188. */
  189. public function getEmail()
  190. {
  191. return $this->_email;
  192. }
  193. /**
  194. * Get the local part of the email address
  195. *
  196. * @return string
  197. */
  198. public function getEmailLocalPart()
  199. {
  200. return $this->_emailLocalPart;
  201. }
  202. /**
  203. * Get the domain part of the email address
  204. *
  205. * @return string
  206. */
  207. public function getEmailDomainPart()
  208. {
  209. return $this->_emailDomainPart;
  210. }
  211. /**
  212. * Get the HTML code needed for the mail hide
  213. *
  214. * @param string $email
  215. * @return string
  216. * @throws Zend_Service_ReCaptcha_MailHide_Exception
  217. */
  218. public function getHtml($email = null)
  219. {
  220. if ($email !== null) {
  221. $this->setEmail($email);
  222. } else if ($this->_email === null) {
  223. /** @see Zend_Service_ReCaptcha_MailHide_Exception */
  224. require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
  225. throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing email address');
  226. }
  227. if ($this->_publicKey === null) {
  228. /** @see Zend_Service_ReCaptcha_MailHide_Exception */
  229. require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
  230. throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing public key');
  231. }
  232. if ($this->_privateKey === null) {
  233. /** @see Zend_Service_ReCaptcha_MailHide_Exception */
  234. require_once 'Zend/Service/ReCaptcha/MailHide/Exception.php';
  235. throw new Zend_Service_ReCaptcha_MailHide_Exception('Missing private key');
  236. }
  237. /* Generate the url */
  238. $url = $this->_getUrl();
  239. /* Genrate the HTML used to represent the email address */
  240. $html = htmlentities($this->_emailLocalPart) . '<a href="' . htmlentities($url) . '" onclick="window.open(\'' . htmlentities($url) . '\', \'\', \'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=' . $this->_options['popupWidth'] . ',height=' . $this->_options['popupHeight'] . '\'); return false;" title="' . $this->_options['linkTitle'] . '">' . $this->_options['linkHiddenText'] . '</a>@' . htmlentities($this->_emailDomainPart);
  241. return $html;
  242. }
  243. /**
  244. * Get the url used on the "hidden" part of the email address
  245. *
  246. * @return string
  247. */
  248. protected function _getUrl()
  249. {
  250. /* Figure out how much we need to pad the email */
  251. $numPad = self::ENCRYPTION_BLOCK_SIZE - (strlen($this->_email) % self::ENCRYPTION_BLOCK_SIZE);
  252. /* Pad the email */
  253. $emailPadded = str_pad($this->_email, strlen($this->_email) + $numPad, chr($numPad));
  254. /* Encrypt the email */
  255. $emailEncrypted = mcrypt_encrypt(self::ENCRYPTION_CIPHER, $this->_privateKeyPacked, $emailPadded, self::ENCRYPTION_MODE, self::ENCRYPTION_IV);
  256. /* Return the url */
  257. return self::MAILHIDE_SERVER . '?k=' . $this->_publicKey . '&c=' . strtr(base64_encode($emailEncrypted), '+/', '-_');
  258. }
  259. }