/** * Tests the url_appears_valid_url function * @return void */ public function test_url_appears_valid_url() { $this->assertTrue(url_appears_valid_url('http://example')); $this->assertTrue(url_appears_valid_url('http://www.example.com')); $this->assertTrue(url_appears_valid_url('http://www.exa-mple2.com')); $this->assertTrue(url_appears_valid_url('http://www.example.com/~nobody/index.html')); $this->assertTrue(url_appears_valid_url('http://www.example.com#hmm')); $this->assertTrue(url_appears_valid_url('http://www.example.com/#hmm')); $this->assertTrue(url_appears_valid_url('http://www.example.com/žlutý koníček/lala.txt')); $this->assertTrue(url_appears_valid_url('http://www.example.com/žlutý koníček/lala.txt#hmmmm')); $this->assertTrue(url_appears_valid_url('http://www.example.com/index.php?xx=yy&zz=aa')); $this->assertTrue(url_appears_valid_url('https://*****:*****@www.example.com/žlutý koníček/lala.txt')); $this->assertTrue(url_appears_valid_url('ftp://*****:*****@www.example.com/žlutý koníček/lala.txt')); $this->assertFalse(url_appears_valid_url('http:example.com')); $this->assertFalse(url_appears_valid_url('http:/example.com')); $this->assertFalse(url_appears_valid_url('http://')); $this->assertFalse(url_appears_valid_url('http://www.exa mple.com')); $this->assertFalse(url_appears_valid_url('http://www.examplé.com')); $this->assertFalse(url_appears_valid_url('http://@www.example.com')); $this->assertFalse(url_appears_valid_url('http://user:@www.example.com')); $this->assertTrue(url_appears_valid_url('lalala://@:@/')); }
function validation($data, $files) { $errors = parent::validation($data, $files); // Validating Entered url, we are looking for obvious problems only, // teachers are responsible for testing if it actually works. // This is not a security validation!! Teachers are allowed to enter "javascript:alert(666)" for example. // NOTE: do not try to explain the difference between URL and URI, people would be only confused... if (empty($data['externalurl'])) { $errors['externalurl'] = get_string('required'); } else { $url = trim($data['externalurl']); if (empty($url)) { $errors['externalurl'] = get_string('required'); } else { if (preg_match('|^/|', $url)) { // links relative to server root are ok - no validation necessary } else { if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) { // normal URL if (!url_appears_valid_url($url)) { $errors['externalurl'] = get_string('invalidurl', 'url'); } } else { if (preg_match('|^[a-z]+:|i', $url)) { // general URI such as teamspeak, mailto, etc. - it may or may not work in all browsers, // we do not validate these at all, sorry } else { // invalid URI, we try to fix it by adding 'http://' prefix, // relative links are NOT allowed because we display the link on different pages! if (!url_appears_valid_url('http://' . $url)) { $errors['externalurl'] = get_string('invalidurl', 'url'); } } } } } } return $errors; }
public function validation($data, $files) { global $CFG; $errors = parent::validation($data, $files); $info = isset($data['content']) ? file_get_draft_area_info($data['content']) : array('filecount' => 0); $url = isset($data['externalurl']) ? trim($data['externalurl']) : ''; if (empty($data['externalurl']) && $info['filecount'] == 0 && empty($data['objectid'])) { $errors['filecheck'] = get_string('required'); } else { if (!empty($url)) { if (preg_match('|^/|', $url)) { // Links relative to server root are ok - no validation necessary. } else { if (preg_match('|^[a-z]+://|i', $url) or preg_match('|^https?:|i', $url) or preg_match('|^ftp:|i', $url)) { // Normal URL. if (!mediagallery_appears_valid_url($url)) { $errors['externalurl'] = get_string('invalidurl', 'url'); } } else { if (preg_match('|^[a-z]+:|i', $url)) { // General URI such as teamspeak, mailto, etc. - it may or may not work in all browsers. // We do not validate these at all, sorry. } else { // Invalid URI, we try to fix it by adding 'http://' prefix. // Relative links are NOT allowed because we display the link on different pages! require_once $CFG->dirroot . "/mod/url/locallib.php"; if (!url_appears_valid_url('http://' . $url)) { $errors['externalurl'] = get_string('invalidurl', 'url'); } } } } } } return $errors; }