Permalink
Browse files

Fixes #1286 and #1184. Draft version.

  • Loading branch information...
1 parent e5c131d commit b59844260507ef9bf975b2d0dde5aaf0827f745c @resurtm resurtm committed Aug 25, 2012
@@ -51,6 +51,10 @@ class CEmailValidator extends CValidator
* meaning that if the attribute is empty, it is considered valid.
*/
public $allowEmpty=true;
+ /**
+ * @var boolean
+ */
+ public $validateIDN=false;
/**
* Validates the attribute of the object.
@@ -80,6 +84,8 @@ protected function validateAttribute($object,$attribute)
*/
public function validateValue($value)
{
+ if($this->validateIDN)
+ $value=$this->encodeIDN($value);
// make sure string length is limited to avoid DOS attacks
$valid=is_string($value) && strlen($value)<=254 && (preg_match($this->pattern,$value) || $this->allowName && preg_match($this->fullPattern,$value));
if($valid)
@@ -101,6 +107,18 @@ public function validateValue($value)
*/
public function clientValidateAttribute($object,$attribute)
{
+ if($this->validateIDN)
+ {
+ Yii::app()->getClientScript()->registerCoreScript('punycode');
+ $validateIDN='
+var info = value.match(/^(.[^@]+)@(.+)$/);
+if (info)
+ value = info[1] + "@" + punycode.toASCII(info[2]);
+';
+ }
+ else
+ $validateIDN='';
+
$message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} is not a valid email address.');
$message=strtr($message, array(
'{attribute}'=>$object->getAttributeLabel($attribute),
@@ -111,6 +129,7 @@ public function clientValidateAttribute($object,$attribute)
$condition.=" && !value.match({$this->fullPattern})";
return "
+$validateIDN
if(".($this->allowEmpty ? "$.trim(value)!='' && " : '').$condition.") {
messages.push(".CJSON::encode($message).");
}
@@ -156,4 +175,30 @@ protected function mxSort($a, $b)
return 0;
return ($a['pri']<$b['pri'])?-1:1;
}
+
+ protected function encodeIDN($value)
+ {
+ if(function_exists('idn_to_ascii'))
+ $value=idn_to_ascii($value);
+ else
+ {
+ require_once(Yii::getPathOfAlias('system.vendors.idna_convert').DIRECTORY_SEPARATOR.'idna_convert.class.php');
+ $idnaConvert=new idna_convert();
+ $value=$idnaConvert->encode($value);
+ }
+ return $value;
+ }
+
+ protected function decodeIDN($value)
+ {
+ if(function_exists('idn_to_utf8'))
+ $value=idn_to_utf8($value);
+ else
+ {
+ require_once(Yii::getPathOfAlias('system.vendors.idna_convert').DIRECTORY_SEPARATOR.'idna_convert.class.php');
+ $idnaConvert=new idna_convert();
+ $value=$idnaConvert->decode($value);
+ }
+ return $value;
+ }
}
@@ -42,6 +42,10 @@ class CUrlValidator extends CValidator
* meaning that if the attribute is empty, it is considered valid.
*/
public $allowEmpty=true;
+ /**
+ * @var boolean
+ */
+ public $validateIDN=false;
/**
* Validates the attribute of the object.
@@ -75,6 +79,9 @@ public function validateValue($value)
{
if(is_string($value) && strlen($value)<2000) // make sure the length is limited to avoid DOS attacks
{
+ if($this->validateIDN)
+ $value=$this->encodeIDN($value);
+
if($this->defaultScheme!==null && strpos($value,'://')===false)
$value=$this->defaultScheme.'://'.$value;
@@ -84,7 +91,7 @@ public function validateValue($value)
$pattern=$this->pattern;
if(preg_match($pattern,$value))
- return $value;
+ return $this->validateIDN ? $this->decodeIDN($value) : $value;
}
return false;
}
@@ -99,6 +106,18 @@ public function validateValue($value)
*/
public function clientValidateAttribute($object,$attribute)
{
+ if($this->validateIDN)
+ {
+ Yii::app()->getClientScript()->registerCoreScript('punycode');
+ $validateIDN='
+var info = value.match(/^(.+:\/\/|)(.[^/]+)/);
+if (info)
+ value = info[1] + punycode.toASCII(info[2]);
+';
+ }
+ else
+ $validateIDN='';
+
$message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} is not a valid URL.');
$message=strtr($message, array(
'{attribute}'=>$object->getAttributeLabel($attribute),
@@ -110,6 +129,7 @@ public function clientValidateAttribute($object,$attribute)
$pattern=$this->pattern;
$js="
+$validateIDN
if(!value.match($pattern)) {
messages.push(".CJSON::encode($message).");
}
@@ -135,5 +155,30 @@ public function clientValidateAttribute($object,$attribute)
return $js;
}
-}
+ protected function encodeIDN($value)
+ {
+ if(function_exists('idn_to_ascii'))
+ $value=idn_to_ascii($value);
+ else
+ {
+ require_once(Yii::getPathOfAlias('system.vendors.idna_convert').DIRECTORY_SEPARATOR.'idna_convert.class.php');
+ $idnaConvert=new idna_convert();
+ $value=$idnaConvert->encode($value);
+ }
+ return $value;
+ }
+
+ protected function decodeIDN($value)
+ {
+ if(function_exists('idn_to_utf8'))
+ $value=idn_to_utf8($value);
+ else
+ {
+ require_once(Yii::getPathOfAlias('system.vendors.idna_convert').DIRECTORY_SEPARATOR.'idna_convert.class.php');
+ $idnaConvert=new idna_convert();
+ $value=$idnaConvert->decode($value);
+ }
+ return $value;
+ }
+}
@@ -31,7 +31,7 @@
<tr>
<td><a href="http://benalman.com/projects/jquery-bbq-plugin/">jQuery BBQ Plugin 1.2.1</a></td>
<td><a href="bbq/LICENSE.txt">MIT</a></td>
- <td>CGridView and CListView widgets</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CGridView">CGridView</a> and <a href="http://www.yiiframework.com/doc/api/1.1/CListView">CListView</a> widgets</td>
</tr>
<tr>
<td><a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/">jQuery Autocomplete 1.1.0</a></td>
@@ -41,27 +41,27 @@
<tr>
<td><a href="http://digitalbush.com/projects/masked-input-plugin">jQuery Masked Input 1.3</a></td>
<td><a href="jquery/maskedinput/LICENSE.txt">MIT</a></td>
- <td>CHtml::maskedTextField()</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CMaskedTextField">CMaskedTextField</a></td>
</tr>
<tr>
<td><a href="http://www.fyneworks.com/jquery/multiple-file-upload/">jQuery Multi File Upload 1.47</a></td>
<td><a href="http://www.fyneworks.com/jquery/multiple-file-upload/#tab-License">MIT</a></td>
- <td>CMultiFileUpload</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CMultiFileUpload">CMultiFileUpload</a></td>
</tr>
<tr>
<td><a href="http://bassistance.de/jquery-plugins/jquery-plugin-treeview/">jQuery TreeView 1.4.1</a></td>
<td><a href="jquery/autocomplete/LICENSE.txt">MIT</a></td>
- <td>CTreeView</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CTreeView">CTreeView</a></td>
</tr>
<tr>
<td><a href="http://www.fyneworks.com/jquery/star-rating/">jQuery Star Rating 3.13</a></td>
<td><a href="http://www.opensource.org/licenses/mit-license.php">MIT</a></td>
- <td>CStarRating</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CStarRating">CStarRating</a></td>
</tr>
<tr>
<td><a href="http://pear.php.net/pepr/pepr-proposal-show.php?id=198">PEAR JSON</a></td>
<td><a href="json/LICENSE.txt">BSD</a></td>
- <td>CJSON</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CJSON">CJSON</a>, <a href="http://www.yiiframework.com/doc/api/1.1/CJavaScript">CJavaScript</a></td>
</tr>
<tr>
<td><a href="http://www.unicode.org/cldr/">Unicode CLDR Data 1.6</a></td>
@@ -71,32 +71,42 @@
<tr>
<td><a href="http://phplens.com/phpeverywhere/">ADOdb Date Library</a></td>
<td><a href="adodb/LICENSE.txt">BSD</a></td>
- <td>CTimestamp</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CTimestamp">CTimestamp</a></td>
</tr>
<tr>
<td><a href="http://pear.php.net/package/Text_Highlighter/">Text_Highlighter - Generic Syntax Highlighter</a> (v0.7.0 beta)</td>
<td><a href="http://www.php.net/license/3_01.txt">The PHP License</a></td>
- <td>CTextHighlighter (note: many PHP files are modified to make them workable in PHP 5 strict mode and their PEAR dependency are also removed.) </td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CTextHighlighter">CTextHighlighter</a> (note: many PHP files are modified to make them workable in PHP 5 strict mode and their PEAR dependency are also removed.) </td>
</tr>
<tr>
<td><a href="http://pear.php.net/package/File_Gettext/">PEAR Gettext</a> (v0.4.1 beta)</td>
<td><a href="gettext/LICENSE.txt">The PHP License</a></td>
- <td>CGettextMoFile</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CGettextMoFile">CGettextMoFile</a></td>
</tr>
<tr>
<td><a href="http://htmlpurifier.org/">HTML Purifier</a> (v4.4.0)</td>
<td><a href="htmlpurifier/LICENSE.txt">LGPL</a></td>
- <td>CHtmlPurifier</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CHtmlPurifier">CHtmlPurifier</a></td>
</tr>
<tr>
<td><a href="http://michelf.com/projects/php-markdown/">PHP Markdown Extra</a> (v1.2.5)</td>
<td><a href="markdown/LICENSE.txt">BSD</a></td>
- <td>CMarkdown</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CMarkdown">CMarkdown</a></td>
</tr>
<tr>
<td><a href="https://github.com/balupton/history.js/">History.js</a> (v1.7.1) - October 4 2011</td>
<td><a href="history/license.txt">BSD</a></td>
- <td>CGridView, CListView</td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CGridView">CGridView</a>, <a href="http://www.yiiframework.com/doc/api/1.1/CGridView">CListView</a></td>
+</tr>
+<tr>
+ <td><a href="http://phlymail.com/en/downloads/idna-convert.html">Net_IDNA - IDNA Converter in PHP</a> (Version 0.8.0 from 2011-03-11)</td>
+ <td><a href="idna_convert/LICENCE">LGPL</a></td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CUrlValidator">CUrlValidator</a>, <a href="http://www.yiiframework.com/doc/api/1.1/CEmailValidator">CEmailValidator</a></td>
+</tr>
+<tr>
+ <td><a href="https://github.com/bestiejs/punycode.js">Punycode.js</a> (v1.1.1) - June 27 2012</td>
+ <td><a href="punycode/LICENSE-MIT.txt">MIT</a>, <a href="punycode/LICENSE-GPL.txt">GPL</a></td>
+ <td><a href="http://www.yiiframework.com/doc/api/1.1/CUrlValidator">CUrlValidator</a>, <a href="http://www.yiiframework.com/doc/api/1.1/CEmailValidator">CEmailValidator</a></td>
</tr>
</table>
Oops, something went wrong.

0 comments on commit b598442

Please sign in to comment.