|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Validates sms (for text messaging). |
| 5 | + * |
| 6 | + * The relevant specification for this protocol is RFC 5724. |
| 7 | + * This class normalizes SMS numbers so that they only include |
| 8 | + * digits, optionally with a leading plus for international numbers. |
| 9 | + * |
| 10 | + * According to RFC 5724, SMS URIs support the 'body' parameter |
| 11 | + * using the format: sms:number?body=message |
| 12 | + * However, the format: sms:number&body=message is commonly used on |
| 13 | + * the web, so it is also supported here. |
| 14 | + */ |
| 15 | + |
| 16 | +class HTMLPurifier_URIScheme_sms extends HTMLPurifier_URIScheme |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @type bool |
| 20 | + */ |
| 21 | + public $browsable = false; |
| 22 | + |
| 23 | + /** |
| 24 | + * @type bool |
| 25 | + */ |
| 26 | + public $may_omit_host = true; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param HTMLPurifier_URI $uri |
| 30 | + * @param HTMLPurifier_Config $config |
| 31 | + * @param HTMLPurifier_Context $context |
| 32 | + * @return bool |
| 33 | + */ |
| 34 | + public function doValidate(&$uri, $config, $context) |
| 35 | + { |
| 36 | + $uri->userinfo = null; |
| 37 | + $uri->host = null; |
| 38 | + $uri->port = null; |
| 39 | + |
| 40 | + // Handle SMS URIs with &body= syntax (non-standard but common) |
| 41 | + if (strpos($uri->path, '&body=') !== false) { |
| 42 | + $parts = explode('&body=', $uri->path, 2); |
| 43 | + $phone_number = $parts[0]; |
| 44 | + $body_content = isset($parts[1]) ? $parts[1] : ''; |
| 45 | + |
| 46 | + // Clean the phone number part |
| 47 | + $phone_number = preg_replace( |
| 48 | + '/(?!^\+)[^\d]/', |
| 49 | + '', |
| 50 | + rawurldecode($phone_number) |
| 51 | + ); |
| 52 | + |
| 53 | + // Sanitize the body content |
| 54 | + $body_content = $this->sanitizeBody($body_content); |
| 55 | + |
| 56 | + // Reconstruct the path |
| 57 | + if (!empty($body_content)) { |
| 58 | + $uri->path = $phone_number . '&body=' . $body_content; |
| 59 | + } else { |
| 60 | + $uri->path = $phone_number; |
| 61 | + } |
| 62 | + } else { |
| 63 | + // Clean the phone number (no body parameter) |
| 64 | + $uri->path = preg_replace( |
| 65 | + '/(?!^\+)[^\d]/', |
| 66 | + '', |
| 67 | + rawurldecode($uri->path) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + // Handle standard ?body= syntax in query parameters |
| 72 | + if (!is_null($uri->query) && strpos($uri->query, 'body=') === 0) { |
| 73 | + $body_content = substr($uri->query, 5); // Remove 'body=' |
| 74 | + $body_content = $this->sanitizeBody($body_content); |
| 75 | + |
| 76 | + if (!empty($body_content)) { |
| 77 | + $uri->query = 'body=' . $body_content; |
| 78 | + } else { |
| 79 | + $uri->query = null; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Sanitizes SMS body content |
| 88 | + * @param string $body |
| 89 | + * @return string |
| 90 | + */ |
| 91 | + private function sanitizeBody($body) |
| 92 | + { |
| 93 | + // Remove potentially dangerous characters |
| 94 | + $sanitized = preg_replace('/[<>"\']/', '', $body); |
| 95 | + // Remove any remaining script-like content |
| 96 | + $sanitized = preg_replace('/script|alert|javascript/i', '', $sanitized); |
| 97 | + return $sanitized; |
| 98 | + } |
| 99 | +} |
0 commit comments