3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Mailing_BAO_TrackableURL
extends CRM_Mailing_DAO_TrackableURL
{
40 public function __construct() {
41 parent
::__construct();
45 * Given a url, mailing id and queue event id, find or construct a
46 * trackable url and redirect url.
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)
52 * @return string $redirect The redirect/tracking url
55 public static function getTrackerURL($url, $mailing_id, $queue_id) {
57 static $urlCache = array();
59 if (array_key_exists($url, $urlCache)) {
60 return $urlCache[$url] . "&qid=$queue_id";
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/')
69 // let's not cache these, so they don't get &qid= appended to them
75 $config = CRM_Core_Config
::singleton();
77 $tracker = new CRM_Mailing_BAO_TrackableURL();
78 if (preg_match('/^href/i', $url)) {
79 $url = preg_replace('/^href[ ]*=[ ]*[\'"](.*?)[\'"]$/i', '$1', $url);
84 $tracker->mailing_id
= $mailing_id;
85 if(strlen($tracker->url
) > 254) {
88 if (!$tracker->find(TRUE)) {
94 $redirect = $config->userFrameworkResourceURL
. "extern/url.php?u=$id";
95 $urlCache[$url] = $redirect;
98 $returnUrl = "{$urlCache[$url]}&qid={$queue_id}";
101 $returnUrl = "href='{$returnUrl}'";
111 * return int Url id of the given url and mail
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)) {
126 * @param int $mailing_id
127 * @param int $queue_id
128 * @param bool $onlyHrefs
130 public static function scan_and_replace(&$msg, $mailing_id, $queue_id, $onlyHrefs = FALSE) {
135 $protos = '(https?|ftp)';
137 $gunk = '/#~:.?+=&%@!\-';
139 $any = "{$letters}{$gunk}{$punc}";
141 $pattern = "{\\b(href=([\"'])?($protos:[$any]+?(?=[$punc]*[^$any]|$))([\"'])?)}im";
144 $pattern = "{\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))}eim";
147 $trackURL = CRM_Mailing_BAO_TrackableURL
::getTrackerURL('\\1', $mailing_id, $queue_id);
148 $replacement = $onlyHrefs ?
("href=\"{$trackURL}\"") : ("\"{$trackURL}\"");
150 $msg = preg_replace($pattern, $replacement, $msg);