Merge pull request #4773 from civicrm/version-fix
[civicrm-core.git] / CRM / Mailing / BAO / TrackableURL.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Mailing_BAO_TrackableURL extends CRM_Mailing_DAO_TrackableURL {
36
37 /**
38 * Class constructor
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Given a url, mailing id and queue event id, find or construct a
46 * trackable url and redirect url.
47 *
48 * @param string $url The target url to track
49 * @param int $mailing_id The id of the mailing
50 * @param int $queue_id The queue event id (contact clicking through)
51 *
52 * @return string $redirect The redirect/tracking url
53 * @static
54 */
55 public static function getTrackerURL($url, $mailing_id, $queue_id) {
56
57 static $urlCache = array();
58
59 if (array_key_exists($url, $urlCache)) {
60 return $urlCache[$url] . "&qid=$queue_id";
61 }
62
63 // hack for basic CRM-1014 and CRM-1151 and CRM-3492 compliance:
64 // let's not replace possible image URLs and CiviMail ones
65 if (preg_match('/\.(png|jpg|jpeg|gif|css)[\'"]?$/i', $url)
66 or substr_count($url, 'civicrm/extern/')
67 or substr_count($url, 'civicrm/mailing/')
68 ) {
69 // let's not cache these, so they don't get &qid= appended to them
70 return $url;
71 }
72 else {
73
74 $hrefExists = FALSE;
75 $config = CRM_Core_Config::singleton();
76
77 $tracker = new CRM_Mailing_BAO_TrackableURL();
78 if (preg_match('/^href/i', $url)) {
79 $url = preg_replace('/^href[ ]*=[ ]*[\'"](.*?)[\'"]$/i', '$1', $url);
80 $hrefExists = TRUE;
81 }
82
83 $tracker->url = $url;
84 $tracker->mailing_id = $mailing_id;
85 if(strlen($tracker->url) > 254) {
86 return $url;
87 }
88 if (!$tracker->find(TRUE)) {
89 $tracker->save();
90 }
91 $id = $tracker->id;
92 $tracker->free();
93
94 $redirect = $config->userFrameworkResourceURL . "extern/url.php?u=$id";
95 $urlCache[$url] = $redirect;
96 }
97
98 $returnUrl = "{$urlCache[$url]}&qid={$queue_id}";
99
100 if ($hrefExists) {
101 $returnUrl = "href='{$returnUrl}'";
102 }
103
104 return $returnUrl;
105 }
106
107 /**
108 * @param $url
109 * @param $mailing_id
110 *
111 * return int Url id of the given url and mail
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
124 /**
125 * @param $msg
126 * @param int $mailing_id
127 * @param int $queue_id
128 * @param bool $onlyHrefs
129 */
130 public static function scan_and_replace(&$msg, $mailing_id, $queue_id, $onlyHrefs = FALSE) {
131 if (!$mailing_id) {
132 return;
133 }
134
135 $protos = '(https?|ftp)';
136 $letters = '\w';
137 $gunk = '/#~:.?+=&%@!\-';
138 $punc = '.:?\-';
139 $any = "{$letters}{$gunk}{$punc}";
140 if ($onlyHrefs) {
141 $pattern = "{\\b(href=([\"'])?($protos:[$any]+?(?=[$punc]*[^$any]|$))([\"'])?)}im";
142 }
143 else {
144 $pattern = "{\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))}eim";
145 }
146
147 $trackURL = CRM_Mailing_BAO_TrackableURL::getTrackerURL('\\1', $mailing_id, $queue_id);
148 $replacement = $onlyHrefs ? ("href=\"{$trackURL}\"") : ("\"{$trackURL}\"");
149
150 $msg = preg_replace($pattern, $replacement, $msg);
151 }
152 }
153