Merge pull request #19422 from mattwire/deprecatedcaseactivity
[civicrm-core.git] / CRM / Mailing / BAO / TrackableURL.php
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
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Mailing_BAO_TrackableURL extends CRM_Mailing_DAO_TrackableURL {
18
19 /**
20 * Class constructor.
21 */
22 public function __construct() {
23 parent::__construct();
24 }
25
26 /**
27 * Given a url, mailing id and queue event id, find or construct a
28 * trackable url and redirect url.
29 *
30 * @param string $url
31 * The target url to track.
32 * @param int $mailing_id
33 * The id of the mailing.
34 * @param int $queue_id
35 * The queue event id (contact clicking through).
36 *
37 * @return string
38 * The redirect/tracking url
39 */
40 public static function getTrackerURL($url, $mailing_id, $queue_id) {
41 if (strpos($url, '{') !== FALSE) {
42 return self::getTrackerURLForUrlWithTokens($url, $mailing_id, $queue_id);
43 }
44 else {
45 return self::getBasicTrackerURL($url, $mailing_id, $queue_id);
46 }
47 }
48
49 private static function getBasicTrackerURL($url, $mailing_id, $queue_id) {
50 static $urlCache = [];
51
52 if (array_key_exists($mailing_id . $url, $urlCache)) {
53 return $urlCache[$mailing_id . $url] . "&qid=$queue_id";
54 }
55
56 // hack for basic CRM-1014 and CRM-1151 and CRM-3492 compliance:
57 // let's not replace possible image URLs and CiviMail ones
58 if (preg_match('/\.(png|jpg|jpeg|gif|css)[\'"]?$/i', $url)
59 or substr_count($url, 'civicrm/extern/')
60 or substr_count($url, 'civicrm/mailing/')
61 ) {
62 // let's not cache these, so they don't get &qid= appended to them
63 return $url;
64 }
65 else {
66
67 $hrefExists = FALSE;
68
69 $tracker = new CRM_Mailing_BAO_TrackableURL();
70 if (preg_match('/^href/i', $url)) {
71 $url = preg_replace('/^href[ ]*=[ ]*[\'"](.*?)[\'"]$/i', '$1', $url);
72 $hrefExists = TRUE;
73 }
74
75 $tracker->url = $url;
76 $tracker->mailing_id = $mailing_id;
77
78 if (!$tracker->find(TRUE)) {
79 $tracker->save();
80 }
81 $id = $tracker->id;
82
83 $redirect = CRM_Utils_System::externUrl('extern/url', "u=$id");
84 $urlCache[$mailing_id . $url] = $redirect;
85 }
86
87 // This looks silly - calling the hook twice. This smells like an accident. Restoring old cache-based lookup.
88 // $returnUrl = CRM_Utils_System::externUrl('extern/url', "u=$id&qid=$queue_id");
89 $returnUrl = "{$urlCache[$mailing_id . $url]}&qid={$queue_id}";
90
91 if ($hrefExists) {
92 $returnUrl = "href='{$returnUrl}' rel='nofollow'";
93 }
94
95 return $returnUrl;
96 }
97
98 /**
99 * Create a trackable URL for a URL with tokens.
100 *
101 * @param string $url
102 * @param int $mailing_id
103 * @param int|string $queue_id
104 *
105 * @return string
106 */
107 private static function getTrackerURLForUrlWithTokens($url, $mailing_id, $queue_id) {
108
109 // Parse the URL.
110 // (not using parse_url because it's messy to reassemble)
111 if (!preg_match('/^([^?#]+)([?][^#]*)?(#.*)?$/', $url, $parsed)) {
112 // Failed to parse it, give up and don't track it.
113 return $url;
114 }
115
116 // If we have a token in the URL + path section, we can't tokenise.
117 if (strpos($parsed[1], '{') !== FALSE) {
118 return $url;
119 }
120
121 $trackable_url = $parsed[1];
122
123 // Process the query parameters, if there are any.
124 $tokenised_params = [];
125 $static_params = [];
126 if (!empty($parsed[2])) {
127 $query_key_value_pairs = explode('&', substr($parsed[2], 1));
128
129 // Separate the tokenised from the static parts.
130 foreach ($query_key_value_pairs as $_) {
131 if (strpos($_, '{') === FALSE) {
132 $static_params[] = $_;
133 }
134 else {
135 $tokenised_params[] = $_;
136 }
137 }
138 // Add the static params to the trackable part.
139 if ($static_params) {
140 $trackable_url .= '?' . implode('&', $static_params);
141 }
142 }
143
144 // Get trackable URL.
145 $data = self::getBasicTrackerURL($trackable_url, $mailing_id, $queue_id);
146
147 // Append the tokenised bits and the fragment.
148 if ($tokenised_params) {
149 // We know the URL will already have the '?'
150 $data .= '&' . implode('&', $tokenised_params);
151 }
152 if (!empty($parsed[3])) {
153 $data .= $parsed[3];
154 }
155 return $data;
156 }
157
158 /**
159 * @param $url
160 * @param $mailing_id
161 *
162 * @return int
163 * Url id of the given url and mail
164 */
165 public static function getTrackerURLId($url, $mailing_id) {
166 $tracker = new CRM_Mailing_BAO_TrackableURL();
167 $tracker->url = $url;
168 $tracker->mailing_id = $mailing_id;
169 if ($tracker->find(TRUE)) {
170 return $tracker->id;
171 }
172
173 return NULL;
174 }
175
176 }