-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptMailto.php
70 lines (60 loc) · 1.53 KB
/
CryptMailto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* @see examples.php
*/
class CryptMailto extends Crypt {
public $passPhrase;
public function __construct() {
parent::__construct();
$this->passPhrase = $this->_rndString();
}
/**
* @desc Find email addresses and convert them: <a class="mailto">[ENCRYPTEDEMAIL]</a>
* @param string $html_str
* @return string
*/
public function convertAnchors( $html_str ) {
$emails = $this->_regexEmails( $html_str );
$tags = array();
foreach( $emails as $email ) {
$tags[] = '<a class="mailto">' . $this->encrypt( $email, $this->passPhrase ) . '</a>';
}
$html_str = str_replace( $emails, $tags, $html_str );
return $html_str;
}
/**
*/
public function encryptMailto( $str ) {
return $this->encrypt( $str, $this->passPhrase );
}
public function encryptMailtoAnchor( $str ) {
$tag = '<a class="mailto">' . $this->encryptMailto( $str ) . '</a>';
return $tag;
}
/**
* @desc Extract all email addresses from an HTML string.
* @param string $html_str
* @return array
*/
private function _regexEmails( $html_str ) {
preg_match_all(
"/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i",
$html_str,
$matches
);
return $matches[ 0 ]; // array.
}
/**
* @desc Return a random string.
* @param int $length - Optional: Desired length of string to return.
* @return string
*/
private function _rndString( $length=20 ) {
$str = substr(
str_shuffle( "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ),
0,
$length
);
return $str;
}
}