Merge pull request #15825 from seamuslee001/dev_core_183_logging
[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 $config = CRM_Core_Config::singleton();
61
62 $tracker = new CRM_Mailing_BAO_TrackableURL();
63 if (preg_match('/^href/i', $url)) {
64 $url = preg_replace('/^href[ ]*=[ ]*[\'"](.*?)[\'"]$/i', '$1', $url);
65 $hrefExists = TRUE;
66 }
67
68 $tracker->url = $url;
69 $tracker->mailing_id = $mailing_id;
70
71 if (!$tracker->find(TRUE)) {
72 $tracker->save();
73 }
74 $id = $tracker->id;
75
76 $redirect = $config->userFrameworkResourceURL . "extern/url.php?u=$id";
77 $urlCache[$mailing_id . $url] = $redirect;
78 }
79
80 $returnUrl = "{$urlCache[$mailing_id . $url]}&qid={$queue_id}";
81
82 if ($hrefExists) {
83 $returnUrl = "href='{$returnUrl}' rel='nofollow'";
84 }
85
86 return $returnUrl;
87 }
88
89 /**
90 * @param $url
91 * @param $mailing_id
92 *
93 * @return int
94 * Url id of the given url and mail
95 */
96 public static function getTrackerURLId($url, $mailing_id) {
97 $tracker = new CRM_Mailing_BAO_TrackableURL();
98 $tracker->url = $url;
99 $tracker->mailing_id = $mailing_id;
100 if ($tracker->find(TRUE)) {
101 return $tracker->id;
102 }
103
104 return NULL;
105 }
106
107 }