Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. /**
  2. * Format Message with Attributes
  3. * @var mixed $contact
  4. * @var string message
  5. */
  6. public function formatMessageWithAttributes($contact, &$message) {
  7.  
  8. $data = array_merge(empty($contact->data) ? [] : $contact->data, [
  9. 'name' => $contact->name,
  10. 'phone' => $contact->phone
  11. ]);
  12.  
  13. preg_match_all('/{(.*?)}/', $message, $matches);
  14.  
  15. foreach($matches[1] as $match)
  16. {
  17. // Explode to get key and default value
  18. $parts = explode('|', $match);
  19.  
  20. // Get the key and default value
  21. $key = $parts[0];
  22. $defaultValue = isset($parts[1]) ? $parts[1] : '';
  23.  
  24. // Get the replacement value, with default fallback
  25. $value = array_key_exists($key, $data) ? $data[$key] : $defaultValue;
  26.  
  27. // Replace the occurrences of this with the match
  28. // DON'T put the curly braces in the same string as match, php will think it's part of the variable string injection
  29. $message = str_replace("{".$match."}", $value, $message);
  30. }
  31. }
  32.  
  33. /**
  34. * Format Message With Vanity URL
  35. * Example, Https://www.blaxtr.com or https://blaxtr.com
  36. * @var string messages
  37. */
  38. public function formatMessagesWithVanityUrlsFromWholeDomains($contact, &$message) {
  39. // Create Contact Vanity Urls
  40. preg_match_all('/(\b(https?|http?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i', $message, $vanityHttpMatches);
  41.  
  42. foreach($vanityHttpMatches as $m => $match) {
  43.  
  44. foreach($match as $ke => $value) {
  45. if(in_array($value, ['https', 'http'])) {
  46. continue;
  47. }
  48.  
  49. $_match = $value;
  50. $urlReplace = preg_quote(addslashes($value));
  51. $urlReplace = preg_replace('/\//', '\\/',$urlReplace);
  52.  
  53. try {
  54. $vanityUrl = $this->generateVanityUrl($this->campaign->id, $_match, $contact->id, $this->broadcast->id, $this->broadcast->proc_id);
  55.  
  56. $message = preg_replace('/'.$urlReplace.'/i', $vanityUrl, $message);
  57. } catch(\Exception $e) {
  58.  
  59. }
  60. }
  61. }
  62. }
  63.  
  64. public function formatMessageWithVanityUrlFromHostname($contact, &$message) {
  65. preg_match_all('/([a-z0-9][a-z0-9\-]{0,61}[a-z0-9]\.)+[a-z0-9][a-z0-9\-]*[a-z0-9]/i', $message, $vanityNoHttpMatches);
  66.  
  67. foreach($vanityNoHttpMatches as $m => $match) {
  68.  
  69. foreach($match as $k => $potentialUrl) {
  70. if(in_array($potentialUrl, ['wwr.link'])) {
  71. continue;
  72. }
  73.  
  74. if($this->urlExists('https://'.$potentialUrl)) {
  75. $vanityUrl = $this->generateVanityUrl($this->campaign->id, 'https://'.$potentialUrl, $contact->id, $this->broadcast->id, $this->broadcast->proc_id);
  76.  
  77. $urlReplace = preg_quote(addslashes($potentialUrl));
  78. $urlReplace = preg_replace('/\//', '\\/',$urlReplace);
  79.  
  80. $message = preg_replace('/'.$urlReplace.'/i', $vanityUrl, $message);
  81. }
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement