| 1: | <?php
|
| 2: |
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: |
|
| 11: | class HTMLPurifier_PercentEncoder
|
| 12: | {
|
| 13: |
|
| 14: | |
| 15: | |
| 16: | |
| 17: |
|
| 18: | protected $preserve = array();
|
| 19: |
|
| 20: | |
| 21: | |
| 22: | |
| 23: |
|
| 24: | public function __construct($preserve = false)
|
| 25: | {
|
| 26: |
|
| 27: | for ($i = 48; $i <= 57; $i++) {
|
| 28: | $this->preserve[$i] = true;
|
| 29: | }
|
| 30: | for ($i = 65; $i <= 90; $i++) {
|
| 31: | $this->preserve[$i] = true;
|
| 32: | }
|
| 33: | for ($i = 97; $i <= 122; $i++) {
|
| 34: | $this->preserve[$i] = true;
|
| 35: | }
|
| 36: | $this->preserve[45] = true;
|
| 37: | $this->preserve[46] = true;
|
| 38: | $this->preserve[95] = true;
|
| 39: | $this->preserve[126]= true;
|
| 40: |
|
| 41: |
|
| 42: | if ($preserve !== false) {
|
| 43: | for ($i = 0, $c = strlen($preserve); $i < $c; $i++) {
|
| 44: | $this->preserve[ord($preserve[$i])] = true;
|
| 45: | }
|
| 46: | }
|
| 47: | }
|
| 48: |
|
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: |
|
| 59: | public function encode($string)
|
| 60: | {
|
| 61: | $ret = '';
|
| 62: | for ($i = 0, $c = strlen($string); $i < $c; $i++) {
|
| 63: | if ($string[$i] !== '%' && !isset($this->preserve[$int = ord($string[$i])])) {
|
| 64: | $ret .= '%' . sprintf('%02X', $int);
|
| 65: | } else {
|
| 66: | $ret .= $string[$i];
|
| 67: | }
|
| 68: | }
|
| 69: | return $ret;
|
| 70: | }
|
| 71: |
|
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: |
|
| 80: | public function normalize($string)
|
| 81: | {
|
| 82: | if ($string == '') {
|
| 83: | return '';
|
| 84: | }
|
| 85: | $parts = explode('%', $string);
|
| 86: | $ret = array_shift($parts);
|
| 87: | foreach ($parts as $part) {
|
| 88: | $length = strlen($part);
|
| 89: | if ($length < 2) {
|
| 90: | $ret .= '%25' . $part;
|
| 91: | continue;
|
| 92: | }
|
| 93: | $encoding = substr($part, 0, 2);
|
| 94: | $text = substr($part, 2);
|
| 95: | if (!ctype_xdigit($encoding)) {
|
| 96: | $ret .= '%25' . $part;
|
| 97: | continue;
|
| 98: | }
|
| 99: | $int = hexdec($encoding);
|
| 100: | if (isset($this->preserve[$int])) {
|
| 101: | $ret .= chr($int) . $text;
|
| 102: | continue;
|
| 103: | }
|
| 104: | $encoding = strtoupper($encoding);
|
| 105: | $ret .= '%' . $encoding . $text;
|
| 106: | }
|
| 107: | return $ret;
|
| 108: | }
|
| 109: | }
|
| 110: |
|
| 111: |
|
| 112: | |