/** * Sanitizes a string to a (valid) URL. * @param string $url * @return string */ function sanitize_url(&$url) { // if there’s no protocol given if (!validate_protocol($url)) { // if it’s an email adress if (strpos($url, '@')) { $url = 'mailto://' . $url; } else { $url = 'http://' . $url; } } return htmlspecialchars($url); }
/** * Sanitizes a string to a (valid) URL. * @param string $url * @return string */ function sanitize_url(&$url) { if (preg_match('#^[[:space:]]*$#', $url)) { return ''; } // if there’s no protocol given if (!validate_protocol($url)) { // if it’s an email adress if (strpos($url, '@')) { $url = 'mailto://' . $url; } else { $url = 'http://' . $url; } } return htmlspecialchars($url); }
<?php /** * Tests for function validate_protocol(). * * @author Paul Vorbach <*****@*****.**> * @license http://opensource.org/licenses/mit-license.php MIT License * @package org.genitis.yuki.mod.user-input.test */ ?> <!DOCTYPE html> <html> <head> <title>Tests for function validate_protocol()</title> </head> <body> <h1>Tests for function <code>validate_protocol()</code></h1> <p>Checks for each URL, whether it has got a protocol given.</p> <?php $urls = array('http://www.genitis.org/', 'ssh://user@domain.com/', 'mailto:user@domain.com', 'http://', 'ssh://', 'mailto:', 'http:/www.genitis.org', 'mailto', 'www.genitis.org', '*****@*****.**'); foreach ($urls as $url) { echo $url . ', ' . (validate_protocol($url) ? 'true' : 'false') . "<br/>\n"; } ?> </body> </html>
function validate_all($parameters) { $result = validate_domain_ip($parameters['AIP']) && validate_protocol($parameters['protocol']) && \validate_port($parameters['port']) && validate_domain_ip($parameters['SIP']) && validate_protocol($parameters['access_proto']) && validate_port($parameters['access_port']) && validate_key($parameters['RIJK']); return $result; }