Merge pull request #19280 from eileenmcnaughton/params
[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
42 static $urlCache = [];
43
44 if (array_key_exists($mailing_id . $url, $urlCache)) {
45 return $urlCache[$mailing_id . $url] . "&qid=$queue_id";
46 }
47
48 // hack for basic CRM-1014 and CRM-1151 and CRM-3492 compliance:
49 // let's not replace possible image URLs and CiviMail ones
50 if (preg_match('/\.(png|jpg|jpeg|gif|css)[\'"]?$/i', $url)
51 or substr_count($url, 'civicrm/extern/')
52 or substr_count($url, 'civicrm/mailing/')
53 ) {
54 // let's not cache these, so they don't get &qid= appended to them
55 return $url;
56 }
57 else {
58
59 $hrefExists = FALSE;
60
61 $tracker = new CRM_Mailing_BAO_TrackableURL();
62 if (preg_match('/^href/i', $url)) {
63 $url = preg_replace('/^href[ ]*=[ ]*[\'"](.*?)[\'"]$/i', '$1', $url);
64 $hrefExists = TRUE;
65 }
66
67 $tracker->url = $url;
68 $tracker->mailing_id = $mailing_id;
69
70 if (!$tracker->find(TRUE)) {
71 $tracker->save();
72 }
73 $id = $tracker->id;
74
75 $redirect = CRM_Utils_System::externUrl('extern/url', "u=$id");
76 $urlCache[$mailing_id . $url] = $redirect;
77 }
78
79 // This looks silly - calling the hook twice. This smells like an accident. Restoring old cache-based lookup.
80 // $returnUrl = CRM_Utils_System::externUrl('extern/url', "u=$id&qid=$queue_id");
81 $returnUrl = "{$urlCache[$mailing_id . $url]}&qid={$queue_id}";
82
83 if ($hrefExists) {
84 $returnUrl = "href='{$returnUrl}' rel='nofollow'";
85 }
86
87 return $returnUrl;
88 }
89
90 /**
91 * @param $url
92 * @param $mailing_id
93 *
94 * @return int
95 * Url id of the given url and mail
96 */
97 public static function getTrackerURLId($url, $mailing_id) {
98 $tracker = new CRM_Mailing_BAO_TrackableURL();
99 $tracker->url = $url;
100 $tracker->mailing_id = $mailing_id;
101 if ($tracker->find(TRUE)) {
102 return $tracker->id;
103 }
104
105 return NULL;
106 }
107
108 }