url = $url; $tracker->mailing_id = $mailing_id; if (!$tracker->find(TRUE)) { $tracker->save(); } $id = $tracker->id; $redirect = CRM_Utils_System::externUrl('extern/url', "u=$id"); $urlCache[$mailing_id . $url] = $redirect; } // This looks silly - calling the hook twice. This smells like an accident. Restoring old cache-based lookup. // $returnUrl = CRM_Utils_System::externUrl('extern/url', "u=$id&qid=$queue_id"); $returnUrl = "{$urlCache[$mailing_id . $url]}&qid={$queue_id}"; if ($hrefExists) { $returnUrl = "href='{$returnUrl}' rel='nofollow'"; } return $returnUrl; } /** * Create a trackable URL for a URL with tokens. * * @param string $url * @param int $mailing_id * @param int|string $queue_id * * @return string */ private static function getTrackerURLForUrlWithTokens($url, $mailing_id, $queue_id) { // Parse the URL. // (not using parse_url because it's messy to reassemble) if (!preg_match('/^([^?#]+)([?][^#]*)?(#.*)?$/', $url, $parsed)) { // Failed to parse it, give up and don't track it. return $url; } // If we have a token in the URL + path section, we can't tokenise. if (strpos($parsed[1], '{') !== FALSE) { return $url; } $trackable_url = $parsed[1]; // Process the query parameters, if there are any. $tokenised_params = []; $static_params = []; if (!empty($parsed[2])) { $query_key_value_pairs = explode('&', substr($parsed[2], 1)); // Separate the tokenised from the static parts. foreach ($query_key_value_pairs as $_) { if (strpos($_, '{') === FALSE) { $static_params[] = $_; } else { $tokenised_params[] = $_; } } // Add the static params to the trackable part. if ($static_params) { $trackable_url .= '?' . implode('&', $static_params); } } // Get trackable URL. $data = self::getBasicTrackerURL($trackable_url, $mailing_id, $queue_id); // Append the tokenised bits and the fragment. if ($tokenised_params) { // We know the URL will already have the '?' $data .= '&' . implode('&', $tokenised_params); } if (!empty($parsed[3])) { $data .= $parsed[3]; } return $data; } /** * @param $url * @param $mailing_id * * @return int * Url id of the given url and mail */ public static function getTrackerURLId($url, $mailing_id) { $tracker = new CRM_Mailing_BAO_TrackableURL(); $tracker->url = $url; $tracker->mailing_id = $mailing_id; if ($tracker->find(TRUE)) { return $tracker->id; } return NULL; } }