Skip to content

Commit

Permalink
Merge pull request joomla#22 from elinw/bb
Browse files Browse the repository at this point in the history
Bb bit bucket url stuff updated
  • Loading branch information
LouisLandry committed May 26, 2011
2 parents 166b1ce + 83c12e2 commit 4b060cb
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 103 deletions.
47 changes: 0 additions & 47 deletions libraries/joomla/filter/filterinput.php
Expand Up @@ -191,53 +191,6 @@ public function clean($source, $type='string')
case 'USERNAME' :
$result = (string) preg_replace('/[\x00-\x1F\x7F<>"\'%&]/', '', $source);
break;
case 'TEL' :
$source = trim($source);
if (preg_match('/^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/',$source) == 1) {
$number = (string) preg_replace('/[^\d]/', '', $source);
if (substr($number,0,1) == 1) {
$number = substr($number,1);
}
if (substr($number,0,2) == '+1') {
$number = substr($number,2);
}
$result = '1.'.$number;
} elseif
(preg_match('/^\+(?:[0-9] ?){6,14}[0-9]$/',$source) == 1) {
$countrycode = substr($source,0,strpos($source,' '));
$countrycode = (string) preg_replace('/[^\d]/', '', $countrycode);
$number = strstr($source,' ');
$number = (string) preg_replace('/[^\d]/', '', $number);
$result = $countrycode.'.'.$number;
} elseif
(preg_match('/^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/',$source) == 1){
if (strstr($source,'x')) {
$xpos = strpos($source,'x');
$source = substr($source,0,$xpos);
}
$countrycode = strstr($source,'.',true);
$countrycode = (string) preg_replace('/[^\d]/', '', $countrycode);
$number = strstr($source,'.');
$number = (string) preg_replace('/[^\d]/', '', $number);
$result = $countrycode.'.'.$number;
} else
{ $source = (string) preg_replace('/[^\d]/', '', $source);
if ($source != null) {
$length = strlen($source);
if ($length <= 12) {
$result='.'.$source;

} else {
$cclen = $length - 12;
$result = substr($source,0,$cclen).'.'.substr($source,$cclen);
}
}
else {
$result = '.';
}
}

break;

default :
// Are we dealing with an array?
Expand Down
59 changes: 58 additions & 1 deletion libraries/joomla/form/form.php
Expand Up @@ -1144,7 +1144,64 @@ protected function filterField($element, $value)
$return = '';
}
break;


case 'TEL' :
$value = trim($value);
// Does it match the NANP pattern?
if (preg_match('/^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/',$value) == 1) {
$number = (string) preg_replace('/[^\d]/', '', $value);
if (substr($number,0,1) == 1) {
$number = substr($number,1);
}
if (substr($number,0,2) == '+1') {
$number = substr($number,2);
}
$result = '1.'.$number;
}
// If not, does it match ITU-T?
elseif (preg_match('/^\+(?:[0-9] ?){6,14}[0-9]$/',$value) == 1) {
$countrycode = substr($value,0,strpos($value,' '));
$countrycode = (string) preg_replace('/[^\d]/', '', $countrycode);
$number = strstr($value,' ');
$number = (string) preg_replace('/[^\d]/', '', $number);
$result = $countrycode.'.'.$number;
}
// If not, does it match EPP?
elseif (preg_match('/^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/',$value) == 1){
if (strstr($value,'x')) {
$xpos = strpos($value,'x');
$value = substr($value,0,$xpos);
}
$result = str_replace('+','',$value);

}
// Maybe it is already ccc.nnnnnnn?
elseif (preg_match('/[0-9]{1,3}\.[0-9]{4,14}$/',$value) == 1 ){
$result = $value;
}
// If not, can we make it a string of digits?
else {
$value = (string) preg_replace('/[^\d]/', '', $value);
if ($value != null && strlen($value) <= 15) {
$length = strlen($value);
// if it is fewer than 13 digits assume it is a local number
if ($length <= 12) {
$result='.'.$value;

} else {
// If it has 13 or more digits let's make a country code.
$cclen = $length - 12;
$result = substr($value,0,$cclen).'.'.substr($value,$cclen);
}
}
// If not let's not save anything.
else {
$result = '';
}
}
$return = $result;

break;
default:
// Check for a callback filter.
if (strpos($filter, '::') !== false && is_callable(explode('::', $filter))) {
Expand Down
93 changes: 93 additions & 0 deletions libraries/joomla/form/rules/url.php
@@ -0,0 +1,93 @@
<?php
/**
* @package Joomla.Platform
* @subpackage Form
*
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

jimport('joomla.form.formrule');
jimport('joomla.utilities.string');


/**
* Form Rule class for the Joomla Framework.
*
* @package Joomla.Framework
* @subpackage Form
* @since 11.1
*/
class JFormRuleUrl extends JFormRule
{
/**
* Method to test an external url for a valid parts.
*
* @param object $element The JXMLElement object representing the <field /> tag for the
* form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as as an array
* container for the field. For example if the field has name="foo"
* and the group value is set to "bar" then the full field name
* would end up being "bar[foo]".
* @param object $input An optional JRegistry object with the entire data set to validate
* against the entire form.
* @param object $form The form object for which the field is being tested.
*
* @return boolean True if the value is valid, false otherwise.
* @since 11.1
* @throws JException on invalid rule.
* @link http://www.w3.org/Addressing/URL/url-spec.txt
* @see Jstring
*/
public function test(& $element, $value, $group = null, & $input = null, & $form = null)
{
// If the field is empty and not required, the field is valid.
$required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
if (!$required && empty($value)) {
return true;
}
$urlParts = JString::parse_url($value);
// See http://www.w3.org/Addressing/URL/url-spec.txt
// Use the full list or optionally specify a list of permitted schemes.
if ($element['schemes'] == ''){
$scheme = array('http','https','ftp','ftps','gopher','mailto','news','prospero','telnet',
'rlogin','tn3270','wais','url','mid','cid','nntp','tel','urn','ldap','file','fax','modem');
} else {
$scheme = explode(",",$element['schemes']);

}
// This rule is only for full URLs with schemes because parse_url does not parse
// accurately without a scheme.
// @see http://php.net/manual/en/function.parse-url.php
if (!array_key_exists('scheme',$urlParts)){
return false;
}
$urlScheme = (string) $urlParts['scheme'];
$urlScheme = strtolower($urlScheme);
if (in_array($urlScheme,$scheme) == false){
return false;
}
// For some schemes here must be two slashes.
if (($urlScheme == 'http' || $urlScheme == 'https' || $urlScheme == 'ftp' ||
$urlScheme == 'sftp' || $urlScheme == 'gopher' || $urlScheme == 'wais'
|| $urlScheme == 'gopher' || $urlScheme == 'prospero' || $urlScheme == 'telnet' )
&& ((substr($value,strlen($urlScheme),3)) !== '://')){
return false;
}
// The best we can do for the rest is make sure that the strings are valid UTF-8
// and the port is an integer.
if (array_key_exists('host',$urlParts) && !JString::valid((string) $urlParts['host'])){
return false;
}
if (array_key_exists('port',$urlParts) && !is_int((int) $urlParts['port']) ){
return false;
}
if (array_key_exists('path',$urlParts) && !JString::valid((string) $urlParts['path'])){
return false;
}
return true;
}
}
55 changes: 0 additions & 55 deletions tests/suite/joomla/filter/JFilterInputTest.php
Expand Up @@ -442,61 +442,6 @@ function casesGeneric()
'img height="123 /&gt;"',
'From generic cases'
),
'tel_01' => array(
'tel',
'1 (202) 223-1234',
'1.2022231234',
'NANP'
),
'tel_02' => array(
'tel',
'12022231234',
'1.2022231234',
'NANP'
),
'tel_03' => array(
'tel',
'+1-202-223-1234',
'1.2022231234',
'NANP'
),
'tel_04' => array(
'tel',
'+34 202 123 1234',
'34.2021231234',
'ITU-T'
),
'tel_05' => array(
'tel',
'+34.2021231234',
'34.2021231234',
'EPP'
),
'tel_06' => array(
'tel',
'342021231234567',
'342.021231234567',
'ITU-T'
),
'tel_07' => array(
'tel',
'202123123456',
'.202123123456',
'ITU-T'
),
'tel_08' => array(
'tel',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz!"#$%&\'()*+,-',
'.',
'Any'
),
'tel_09' => array(
'tel',
'+34.2021231234x555',
'34.2021231234',
'EPP'
),

);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/suite/joomla/form/JFormDataHelper.php
Expand Up @@ -79,6 +79,9 @@ class JFormDataHelper
<field
name="word" filter="word" />
<field
name="tel" filter="tel" />
<fields
name="params"
description="Optional Settings">
Expand Down
41 changes: 41 additions & 0 deletions tests/suite/joomla/form/JFormTest.php
Expand Up @@ -387,6 +387,47 @@ public function testFilterField()
'Line:'.__LINE__.' The default strict filter should be correctly applied.'
);

$this->assertThat(
$form->filterField($form->findField('tel'), '222.3333333333'),
$this->equalTo('222.3333333333'),
'Line:'.__LINE__.' The tel filter should be correctly applied.'
);
$this->assertThat(
$form->filterField($form->findField('tel'), '+222.3333333333'),
$this->equalTo('222.3333333333'),
'Line:'.__LINE__.' The tel filter should be correctly applied.'
);
$this->assertThat(
$form->filterField($form->findField('tel'), '+2,2,2.3,3,3,3,3,3,3,3,3,3,3,3'),
$this->equalTo('222.333333333333'),
'Line:'.__LINE__.' The tel filter should be correctly applied.'
);
$this->assertThat(
$form->filterField($form->findField('tel'), '33333333333'),
$this->equalTo('.33333333333'),
'Line:'.__LINE__.' The tel filter should be correctly applied.'
);
$this->assertThat(
$form->filterField($form->findField('tel'), '222333333333333'),
$this->equalTo('222.333333333333'),
'Line:'.__LINE__.' The tel filter should be correctly applied.'
);
$this->assertThat(
$form->filterField($form->findField('tel'), '1 (202) 555-5555'),
$this->equalTo('1.2025555555'),
'Line:'.__LINE__.' The tel filter should be correctly applied.'
);
$this->assertThat(
$form->filterField($form->findField('tel'), '+222.33333333333x444'),
$this->equalTo('222.33333333333'),
'Line:'.__LINE__.' The tel filter should be correctly applied.'
);
$this->assertThat(
$form->filterField($form->findField('tel'), 'ABCabc/?.!*x'),
$this->equalTo(''),
'Line:'.__LINE__.' The tel filter should be correctly applied.'
);

$this->assertThat(
$form->filterField($form->findField('server_utc'), 'foo'),
$this->equalTo(''),
Expand Down

0 comments on commit 4b060cb

Please sign in to comment.