worked on CRM-12357, show bulk mailing activities if they are created.
[civicrm-core.git] / CRM / Mailing / BAO / TrackableURL.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35class 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
86 if (!$tracker->find(TRUE)) {
87 $tracker->save();
88 }
89 $id = $tracker->id;
90 $tracker->free();
91
92 $redirect = $config->userFrameworkResourceURL . "extern/url.php?u=$id";
93 $urlCache[$url] = $redirect;
94 }
95
96 $returnUrl = "{$urlCache[$url]}&qid={$queue_id}";
97
98 if ($hrefExists) {
99 $returnUrl = "href='{$returnUrl}'";
100 }
101
102 return $returnUrl;
103 }
104
105 public static function scan_and_replace(&$msg, $mailing_id, $queue_id, $onlyHrefs = FALSE) {
106 if (!$mailing_id) {
107 return;
108 }
109
110 $protos = '(https?|ftp)';
111 $letters = '\w';
112 $gunk = '/#~:.?+=&%@!\-';
113 $punc = '.:?\-';
114 $any = "{$letters}{$gunk}{$punc}";
115 if ($onlyHrefs) {
116 $pattern = "{\\b(href=([\"'])?($protos:[$any]+?(?=[$punc]*[^$any]|$))([\"'])?)}im";
117 }
118 else {
119 $pattern = "{\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))}eim";
120 }
121
122 $trackURL = CRM_Mailing_BAO_TrackableURL::getTrackerURL('\\1', $mailing_id, $queue_id);
123 $replacement = $onlyHrefs ? ("href=\"{$trackURL}\"") : ("\"{$trackURL}\"");
124
125 $msg = preg_replace($pattern, $replacement, $msg);
126 }
127}
128