| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Mailing_BAO_TrackableURL extends CRM_Mailing_DAO_TrackableURL { |
| 36 | |
| 37 | /** |
| 38 | * Class constructor. |
| 39 | */ |
| 40 | public 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 |
| 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). |
| 54 | * |
| 55 | * @return string |
| 56 | * The redirect/tracking url |
| 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; |
| 88 | if (strlen($tracker->url) > 254) { |
| 89 | return $url; |
| 90 | } |
| 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 | |
| 110 | /** |
| 111 | * @param $url |
| 112 | * @param $mailing_id |
| 113 | * |
| 114 | * @return int |
| 115 | * Url id of the given url and mail |
| 116 | */ |
| 117 | public static function getTrackerURLId($url, $mailing_id) { |
| 118 | $tracker = new CRM_Mailing_BAO_TrackableURL(); |
| 119 | $tracker->url = $url; |
| 120 | $tracker->mailing_id = $mailing_id; |
| 121 | if ($tracker->find(TRUE)) { |
| 122 | return $tracker->id; |
| 123 | } |
| 124 | |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @param $msg |
| 130 | * @param int $mailing_id |
| 131 | * @param int $queue_id |
| 132 | * @param bool $onlyHrefs |
| 133 | */ |
| 134 | public static function scan_and_replace(&$msg, $mailing_id, $queue_id, $onlyHrefs = FALSE) { |
| 135 | if (!$mailing_id) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | $protos = '(https?|ftp)'; |
| 140 | $letters = '\w'; |
| 141 | $gunk = '/#~:.?+=&%@!\-'; |
| 142 | $punc = '.:?\-'; |
| 143 | $any = "{$letters}{$gunk}{$punc}"; |
| 144 | if ($onlyHrefs) { |
| 145 | $pattern = "{\\b(href=([\"'])?($protos:[$any]+?(?=[$punc]*[^$any]|$))([\"'])?)}im"; |
| 146 | } |
| 147 | else { |
| 148 | $pattern = "{\\b($protos:[$any]+?(?=[$punc]*[^$any]|$))}eim"; |
| 149 | } |
| 150 | |
| 151 | $trackURL = CRM_Mailing_BAO_TrackableURL::getTrackerURL('\\1', $mailing_id, $queue_id); |
| 152 | $replacement = $onlyHrefs ? ("href=\"{$trackURL}\"") : ("\"{$trackURL}\""); |
| 153 | |
| 154 | $msg = preg_replace($pattern, $replacement, $msg); |
| 155 | } |
| 156 | |
| 157 | } |