Merge remote-tracking branch 'origin/5.22' into 5.22-master-2020-01-24-13-18-00
[civicrm-core.git] / CRM / Mailing / BAO / TrackableURL.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Mailing_BAO_TrackableURL extends CRM_Mailing_DAO_TrackableURL {
18
19 /**
fe482240 20 * Class constructor.
6a488035 21 */
00be9182 22 public function __construct() {
6a488035
TO
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 *
90c8230e
TO
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).
6a488035 36 *
a6c01b45
CW
37 * @return string
38 * The redirect/tracking url
6a488035
TO
39 */
40 public static function getTrackerURL($url, $mailing_id, $queue_id) {
41
be2fb01f 42 static $urlCache = [];
6a488035 43
5f63a33c 44 if (array_key_exists($mailing_id . $url, $urlCache)) {
45 return $urlCache[$mailing_id . $url] . "&qid=$queue_id";
6a488035
TO
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;
6a488035
TO
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;
fc8e0fd4 69
6a488035
TO
70 if (!$tracker->find(TRUE)) {
71 $tracker->save();
72 }
73 $id = $tracker->id;
6a488035 74
f8274a1c 75 $redirect = CRM_Utils_System::externUrl('extern/url', "u=$id");
5f63a33c 76 $urlCache[$mailing_id . $url] = $redirect;
6a488035
TO
77 }
78
9df3628e 79 $returnUrl = CRM_Utils_System::externUrl('extern/url', "u=$id&qid=$queue_id");
6a488035
TO
80
81 if ($hrefExists) {
43e0759f 82 $returnUrl = "href='{$returnUrl}' rel='nofollow'";
6a488035
TO
83 }
84
85 return $returnUrl;
86 }
87
467cd00c 88 /**
89 * @param $url
90 * @param $mailing_id
91 *
76e7a76c
CW
92 * @return int
93 * Url id of the given url and mail
467cd00c 94 */
95 public static function getTrackerURLId($url, $mailing_id) {
96 $tracker = new CRM_Mailing_BAO_TrackableURL();
97 $tracker->url = $url;
98 $tracker->mailing_id = $mailing_id;
99 if ($tracker->find(TRUE)) {
100 return $tracker->id;
101 }
102
103 return NULL;
104 }
105
6a488035 106}