You are here

public function MyLiveChat::EncodeJScript in My Live Chat 7

Same name and namespace in other branches
  1. 6 mylivechat.php \mylivechat::EncodeJScript()
1 call to MyLiveChat::EncodeJScript()
MyLiveChat::getChatCode in ./mylivechat.php
Returns MyLiveChat button HTML code

File

./mylivechat.php, line 251
MyLiveChat module for Drupal

Class

MyLiveChat
@file MyLiveChat module for Drupal

Code

public function EncodeJScript($str) {
  $chars = "0123456789ABCDEF";
  $chars = str_split($chars);
  $sb = "";
  $l = strlen($str);
  $strarr = str_split($str);
  for ($i = 0; $i < $l; $i++) {
    $c = $strarr[$i];
    if ($c == '\\' || $c == '"' || $c == '\'' || $c == '>' || $c == '<' || $c == '&' || $c == '\\r' || $c == '\\n') {
      if ($sb == "") {
        if ($i > 0) {
          $sb .= substr($str, 0, $i);
        }
      }
      if ($c == '\\') {
        $sb .= "\\x5C";
      }
      elseif ($c == '"') {
        $sb .= "\\x22";
      }
      elseif ($c == '\'') {
        $sb .= "\\x27";
      }
      elseif ($c == '\\r') {
        $sb .= "\\x0D";
      }
      elseif ($c == '\\n') {
        $sb .= "\\x0A";
      }
      elseif ($c == '<') {
        $sb .= "\\x3C";
      }
      elseif ($c == '>') {
        $sb .= "\\x3E";
      }
      elseif ($c == '&') {
        $sb .= "\\x26";
      }
      else {
        $code = $c;
        $a1 = $code & 0xf;
        $a2 = ($code & 0xf0) / 0x10;
        $sb .= "\\x";
        $sb .= $chars[$a2];
        $sb .= $chars[$a1];
      }
    }
    elseif ($sb != "") {
      $sb .= $c;
    }
  }
  if ($sb != "") {
    return $sb;
  }
  return $str;
}