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