Merge pull request #17524 from mattwire/memberbaocreate
[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
cbed6fe5
TO
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}";
6a488035
TO
82
83 if ($hrefExists) {
43e0759f 84 $returnUrl = "href='{$returnUrl}' rel='nofollow'";
6a488035
TO
85 }
86
87 return $returnUrl;
88 }
89
467cd00c 90 /**
91 * @param $url
92 * @param $mailing_id
93 *
76e7a76c
CW
94 * @return int
95 * Url id of the given url and mail
467cd00c 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
6a488035 108}