Commit | Line | Data |
---|---|---|
bdf67e28 SL |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
6 | | This work is published under the GNU AGPLv3 license with some | | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
9 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | namespace Civi\FlexMailer\ClickTracker; | |
12 | ||
13 | class HtmlClickTracker implements ClickTrackerInterface { | |
14 | ||
15 | public function filterContent($msg, $mailing_id, $queue_id) { | |
16 | return self::replaceHrefUrls($msg, | |
17 | function ($url) use ($mailing_id, $queue_id) { | |
18 | if (strpos($url, '{') !== FALSE) { | |
19 | return $url; | |
20 | } | |
21 | $data = \CRM_Mailing_BAO_TrackableURL::getTrackerURL( | |
22 | $url, $mailing_id, $queue_id); | |
23 | $data = htmlentities($data, ENT_NOQUOTES); | |
24 | return $data; | |
25 | } | |
26 | ); | |
27 | } | |
28 | ||
29 | /** | |
30 | * Find any HREF-style URLs and replace them. | |
31 | * | |
32 | * @param string $html | |
33 | * @param callable $replace | |
34 | * Function(string $oldHtmlUrl) => string $newHtmlUrl. | |
35 | * @return mixed | |
36 | * String, HTML. | |
37 | */ | |
38 | public static function replaceHrefUrls($html, $replace) { | |
39 | $useNoFollow = TRUE; | |
40 | $callback = function ($matches) use ($replace, $useNoFollow) { | |
41 | $replacement = $replace($matches[2]); | |
42 | ||
43 | // See: https://github.com/civicrm/civicrm-core/pull/12561 | |
44 | // If we track click-throughs on a link, then don't encourage search-engines to traverse them. | |
45 | // At a policy level, I'm not sure I completely agree, but this keeps things consistent. | |
46 | // You can tell if we're tracking a link because $replace() yields a diff URL. | |
47 | $noFollow = ''; | |
48 | if ($useNoFollow && $replacement !== $matches[2]) { | |
49 | $noFollow = " rel='nofollow'"; | |
50 | } | |
51 | ||
52 | return $matches[1] . $replacement . $matches[3] . $noFollow; | |
53 | }; | |
54 | ||
55 | // Find anything like href="..." or href='...' inside a tag. | |
56 | $tmp = preg_replace_callback( | |
57 | ';(\<[^>]*href *= *")([^">]+)(");', $callback, $html); | |
58 | return preg_replace_callback( | |
59 | ';(\<[^>]*href *= *\')([^\'>]+)(\');', $callback, $tmp); | |
60 | } | |
61 | ||
62 | // /** | |
63 | // * Find URL expressions; replace them with tracked URLs. | |
64 | // * | |
65 | // * @param string $msg | |
66 | // * @param int $mailing_id | |
67 | // * @param int|string $queue_id | |
68 | // * @param bool $html | |
69 | // * @return string | |
70 | // * Updated $msg | |
71 | // */ | |
72 | // public static function scanAndReplace_old($msg, $mailing_id, $queue_id, $html = FALSE) { | |
73 | // | |
74 | // $protos = '(https?|ftp)'; | |
75 | // $letters = '\w'; | |
76 | // $gunk = '/#~:.?+=&%@!\-'; | |
77 | // $punc = '.:?\-'; | |
78 | // $any = "{$letters}{$gunk}{$punc}"; | |
79 | // if ($html) { | |
80 | // $pattern = "{\\b(href=([\"'])?($protos:[$any]+?(?=[$punc]*[^$any]|$))([\"'])?)}im"; | |
81 | // } | |
82 | // else { | |
83 | // $pattern = "{\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))}eim"; | |
84 | // } | |
85 | // | |
86 | // $trackURL = \CRM_Mailing_BAO_TrackableURL::getTrackerURL('\\1', $mailing_id, $queue_id); | |
87 | // $replacement = $html ? ("href=\"{$trackURL}\"") : ("\"{$trackURL}\""); | |
88 | // | |
89 | // $msg = preg_replace($pattern, $replacement, $msg); | |
90 | // if ($html) { | |
91 | // $msg = htmlentities($msg, ENT_NOQUOTES); | |
92 | // } | |
93 | // return $msg; | |
94 | // } | |
95 | ||
96 | } |