Beispiel #1
0
 public function escapeJavascript($string)
 {
     if (is_object($string) == true) {
         if (method_exists($string, '__toString') == false) {
             throw EscapeException::fromBadObject($string);
         }
         $string = (string) $string;
     }
     if (is_array($string) == true) {
         throw EscapeException::fromBadArray();
     }
     return $this->zendEscape->escapeJs($string);
 }
Beispiel #2
0
 /**
  * Escapes strings based on context
  * @param string $string The string to escape
  * @param int $context The context to escape in
  * @return string The escaped string
  * @throws \InvalidArgumentException If the context is invalid
  */
 public function escape($string, $context = self::HTML_BODY)
 {
     $type = gettype($string);
     if (in_array($type, array('boolean', 'integer', 'double', 'NULL'), true)) {
         return $string;
     }
     if (in_array($type, array('object', 'resource', 'unknown type'), true)) {
         throw new \InvalidArgumentException("Unable to escape variable of type {$type}.");
     }
     if ($context === self::HTML_STRING) {
         return parent::escapeHtml($string);
     }
     if ($context === self::HTML_ATTR) {
         return parent::escapeHtmlAttr($string);
     }
     if ($context === self::CSS) {
         return parent::escapeCss($string);
     }
     if ($context === self::JS_STRING) {
         return parent::escapeJs($string);
     }
     if ($context === self::URL_PARAM) {
         return parent::escapeUrl($string);
     }
     throw new \InvalidArgumentException('Invalid context.');
 }
    public function Index03Action()
    {
        /** JS SCRIPT */
        $input = <<<INPUT
' onmouseover='alert(/ZF2!/);
INPUT;
        $escaper = new Escape('utf-8');
        $output = $escaper->escapeJs($input);
        echo '<span title=' . $output . '>Zend</span>';
        return $this->response;
    }
Beispiel #4
0
 /**
  * Escapes strings to make them safe for use
  * within HTML templates. Used by the auto-escaping
  * functionality in setVar() and available to
  * use within your views.
  *
  * Uses ZendFramework's Escaper to handle the actual escaping,
  * based on context. Valid contexts are:
  *      - html
  *      - htmlAttr
  *      - js
  *      - css
  *      - url
  *
  * References:
  *  - https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
  *  - http://framework.zend.com/manual/current/en/modules/zend.escaper.introduction.html
  *
  * @param $data
  * @param $context
  * @param escaper   // An instance of ZF's Escaper to avoid repeated class instantiation.
  *
  * @return string
  */
 function esc($data, $context = 'html', $escaper = null)
 {
     if (is_array($data)) {
         foreach ($data as $key => &$value) {
             $value = esc($value, $context);
         }
     }
     $context = strtolower($context);
     if (!is_object($escaper)) {
         $escaper = new Escaper(config_item('charset'));
     }
     // Valid context?
     if (!in_array($context, ['html', 'htmlattr', 'js', 'css', 'url'])) {
         throw new \InvalidArgumentException('Invalid Context type: ' . $context);
     }
     if (!is_string($data)) {
         return $data;
     }
     switch ($context) {
         case 'html':
             $data = $escaper->escapeHtml($data);
             break;
         case 'htmlattr':
             $data = $escaper->escapeHtmlAttr($data);
             break;
         case 'js':
             $data = $escaper->escapeJs($data);
             break;
         case 'css':
             $data = $escaper->escapeCss($data);
             break;
         case 'url':
             $data = $escaper->escapeUrl($data);
             break;
         default:
             break;
     }
     return $data;
 }
Beispiel #5
0
 /**
  * {@inheritdoc}
  */
 public function escapeJs($string)
 {
     return $this->escaper->escapeJs($string);
 }
 /**
  * @param mixed $input
  * @return mixed
  */
 public static function escapeJs($input)
 {
     self::init();
     return self::$escaper->escapeJs($input);
 }