Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | */ |
33 | class CRM_Mailing_BAO_TrackableURL extends CRM_Mailing_DAO_TrackableURL { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Class constructor. |
6a488035 | 37 | */ |
00be9182 | 38 | public function __construct() { |
6a488035 TO |
39 | parent::__construct(); |
40 | } | |
41 | ||
42 | /** | |
43 | * Given a url, mailing id and queue event id, find or construct a | |
44 | * trackable url and redirect url. | |
45 | * | |
90c8230e TO |
46 | * @param string $url |
47 | * The target url to track. | |
48 | * @param int $mailing_id | |
49 | * The id of the mailing. | |
50 | * @param int $queue_id | |
51 | * The queue event id (contact clicking through). | |
6a488035 | 52 | * |
a6c01b45 CW |
53 | * @return string |
54 | * The redirect/tracking url | |
6a488035 TO |
55 | */ |
56 | public static function getTrackerURL($url, $mailing_id, $queue_id) { | |
57 | ||
58 | static $urlCache = array(); | |
59 | ||
5f63a33c | 60 | if (array_key_exists($mailing_id . $url, $urlCache)) { |
61 | return $urlCache[$mailing_id . $url] . "&qid=$queue_id"; | |
6a488035 TO |
62 | } |
63 | ||
64 | // hack for basic CRM-1014 and CRM-1151 and CRM-3492 compliance: | |
65 | // let's not replace possible image URLs and CiviMail ones | |
66 | if (preg_match('/\.(png|jpg|jpeg|gif|css)[\'"]?$/i', $url) | |
67 | or substr_count($url, 'civicrm/extern/') | |
68 | or substr_count($url, 'civicrm/mailing/') | |
69 | ) { | |
70 | // let's not cache these, so they don't get &qid= appended to them | |
71 | return $url; | |
72 | } | |
73 | else { | |
74 | ||
75 | $hrefExists = FALSE; | |
76 | $config = CRM_Core_Config::singleton(); | |
77 | ||
78 | $tracker = new CRM_Mailing_BAO_TrackableURL(); | |
79 | if (preg_match('/^href/i', $url)) { | |
80 | $url = preg_replace('/^href[ ]*=[ ]*[\'"](.*?)[\'"]$/i', '$1', $url); | |
81 | $hrefExists = TRUE; | |
82 | } | |
83 | ||
84 | $tracker->url = $url; | |
85 | $tracker->mailing_id = $mailing_id; | |
fc8e0fd4 | 86 | |
6a488035 TO |
87 | if (!$tracker->find(TRUE)) { |
88 | $tracker->save(); | |
89 | } | |
90 | $id = $tracker->id; | |
91 | $tracker->free(); | |
92 | ||
93 | $redirect = $config->userFrameworkResourceURL . "extern/url.php?u=$id"; | |
5f63a33c | 94 | $urlCache[$mailing_id . $url] = $redirect; |
6a488035 TO |
95 | } |
96 | ||
5f63a33c | 97 | $returnUrl = "{$urlCache[$mailing_id . $url]}&qid={$queue_id}"; |
6a488035 TO |
98 | |
99 | if ($hrefExists) { | |
43e0759f | 100 | $returnUrl = "href='{$returnUrl}' rel='nofollow'"; |
6a488035 TO |
101 | } |
102 | ||
103 | return $returnUrl; | |
104 | } | |
105 | ||
467cd00c | 106 | /** |
107 | * @param $url | |
108 | * @param $mailing_id | |
109 | * | |
76e7a76c CW |
110 | * @return int |
111 | * Url id of the given url and mail | |
467cd00c | 112 | */ |
113 | public static function getTrackerURLId($url, $mailing_id) { | |
114 | $tracker = new CRM_Mailing_BAO_TrackableURL(); | |
115 | $tracker->url = $url; | |
116 | $tracker->mailing_id = $mailing_id; | |
117 | if ($tracker->find(TRUE)) { | |
118 | return $tracker->id; | |
119 | } | |
120 | ||
121 | return NULL; | |
122 | } | |
123 | ||
6a488035 | 124 | } |