Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Mailing_BAO_TrackableURL extends CRM_Mailing_DAO_TrackableURL { | |
36 | ||
37 | /** | |
100fef9d | 38 | * Class constructor |
6a488035 TO |
39 | */ |
40 | function __construct() { | |
41 | parent::__construct(); | |
42 | } | |
43 | ||
44 | /** | |
45 | * Given a url, mailing id and queue event id, find or construct a | |
46 | * trackable url and redirect url. | |
47 | * | |
48 | * @param string $url The target url to track | |
49 | * @param int $mailing_id The id of the mailing | |
50 | * @param int $queue_id The queue event id (contact clicking through) | |
51 | * | |
52 | * @return string $redirect The redirect/tracking url | |
53 | * @static | |
54 | */ | |
55 | public static function getTrackerURL($url, $mailing_id, $queue_id) { | |
56 | ||
57 | static $urlCache = array(); | |
58 | ||
59 | if (array_key_exists($url, $urlCache)) { | |
60 | return $urlCache[$url] . "&qid=$queue_id"; | |
61 | } | |
62 | ||
63 | // hack for basic CRM-1014 and CRM-1151 and CRM-3492 compliance: | |
64 | // let's not replace possible image URLs and CiviMail ones | |
65 | if (preg_match('/\.(png|jpg|jpeg|gif|css)[\'"]?$/i', $url) | |
66 | or substr_count($url, 'civicrm/extern/') | |
67 | or substr_count($url, 'civicrm/mailing/') | |
68 | ) { | |
69 | // let's not cache these, so they don't get &qid= appended to them | |
70 | return $url; | |
71 | } | |
72 | else { | |
73 | ||
74 | $hrefExists = FALSE; | |
75 | $config = CRM_Core_Config::singleton(); | |
76 | ||
77 | $tracker = new CRM_Mailing_BAO_TrackableURL(); | |
78 | if (preg_match('/^href/i', $url)) { | |
79 | $url = preg_replace('/^href[ ]*=[ ]*[\'"](.*?)[\'"]$/i', '$1', $url); | |
80 | $hrefExists = TRUE; | |
81 | } | |
82 | ||
83 | $tracker->url = $url; | |
84 | $tracker->mailing_id = $mailing_id; | |
d78d31a1 EM |
85 | if(strlen($tracker->url) > 254) { |
86 | return $url; | |
87 | } | |
6a488035 TO |
88 | if (!$tracker->find(TRUE)) { |
89 | $tracker->save(); | |
90 | } | |
91 | $id = $tracker->id; | |
92 | $tracker->free(); | |
93 | ||
94 | $redirect = $config->userFrameworkResourceURL . "extern/url.php?u=$id"; | |
95 | $urlCache[$url] = $redirect; | |
96 | } | |
97 | ||
98 | $returnUrl = "{$urlCache[$url]}&qid={$queue_id}"; | |
99 | ||
100 | if ($hrefExists) { | |
101 | $returnUrl = "href='{$returnUrl}'"; | |
102 | } | |
103 | ||
104 | return $returnUrl; | |
105 | } | |
106 | ||
e0ef6999 EM |
107 | /** |
108 | * @param $msg | |
100fef9d CW |
109 | * @param int $mailing_id |
110 | * @param int $queue_id | |
e0ef6999 EM |
111 | * @param bool $onlyHrefs |
112 | */ | |
6a488035 TO |
113 | public static function scan_and_replace(&$msg, $mailing_id, $queue_id, $onlyHrefs = FALSE) { |
114 | if (!$mailing_id) { | |
115 | return; | |
116 | } | |
117 | ||
118 | $protos = '(https?|ftp)'; | |
119 | $letters = '\w'; | |
120 | $gunk = '/#~:.?+=&%@!\-'; | |
121 | $punc = '.:?\-'; | |
122 | $any = "{$letters}{$gunk}{$punc}"; | |
123 | if ($onlyHrefs) { | |
124 | $pattern = "{\\b(href=([\"'])?($protos:[$any]+?(?=[$punc]*[^$any]|$))([\"'])?)}im"; | |
125 | } | |
126 | else { | |
127 | $pattern = "{\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))}eim"; | |
128 | } | |
129 | ||
130 | $trackURL = CRM_Mailing_BAO_TrackableURL::getTrackerURL('\\1', $mailing_id, $queue_id); | |
131 | $replacement = $onlyHrefs ? ("href=\"{$trackURL}\"") : ("\"{$trackURL}\""); | |
132 | ||
133 | $msg = preg_replace($pattern, $replacement, $msg); | |
134 | } | |
135 | } | |
136 |