Skip to content

Commit 8c1761c

Browse files
author
mcramer
committed
Implemented: FS#1102 - Convert internationalized domains to punycode automatically
- need to have the pecl idn and intl modules installed or - have the idna_convert class in the classes/idn/ path
1 parent ecce33a commit 8c1761c

File tree

71 files changed

+553
-37
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+553
-37
lines changed

interface/lib/classes/functions.inc.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
//* This class is loaded automatically by the ispconfig framework.
3333

3434
class functions {
35-
35+
var $idn_converter = null;
36+
var $idn_converter_name = '';
3637

3738
public function mail($to, $subject, $text, $from, $filepath = '', $filetype = 'application/pdf', $filename = '', $cc = '', $bcc = '', $from_name = '') {
3839
global $app,$conf;
@@ -310,6 +311,72 @@ public function intval($string, $force_numeric = false) {
310311
return intval($string);
311312
}
312313
}
314+
315+
/** IDN converter wrapper.
316+
* all converter classes should be placed in ISPC_CLASS_PATH.'/idn/'
317+
*/
318+
public function idn_encode($domain) {
319+
if($domain == '') return '';
320+
if(preg_match('/^[0-9\.]+$/', $domain)) return $domain; // may be an ip address - anyway does not need to bee encoded
321+
322+
// get domain and user part if it is an email
323+
$user_part = false;
324+
if(strpos($domain, '@') !== false) {
325+
$user_part = substr($domain, 0, strrpos($domain, '@'));
326+
$domain = substr($domain, strrpos($domain, '@') + 1);
327+
}
328+
329+
if(function_exists('idn_to_ascii')) {
330+
$domain = idn_to_ascii($domain);
331+
} elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) {
332+
/* use idna class:
333+
* @author Matthias Sommerfeld <mso@phlylabs.de>
334+
* @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de
335+
* @version 0.8.0 2011-03-11
336+
*/
337+
338+
if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') {
339+
include_once(ISPC_CLASS_PATH.'/idn/idna_convert.class.php');
340+
$this->idn_converter = new idna_convert(array('idn_version' => 2008));
341+
$this->idn_converter_name = 'idna_convert.class';
342+
}
343+
$domain = $this->idn_converter->encode($domain);
344+
}
345+
346+
if($user_part !== false) return $user_part . '@' . $domain;
347+
else return $domain;
348+
}
349+
350+
public function idn_decode($domain) {
351+
if($domain == '') return '';
352+
if(preg_match('/^[0-9\.]+$/', $domain)) return $domain; // may be an ip address - anyway does not need to bee decoded
353+
354+
// get domain and user part if it is an email
355+
$user_part = false;
356+
if(strpos($domain, '@') !== false) {
357+
$user_part = substr($domain, 0, strrpos($domain, '@'));
358+
$domain = substr($domain, strrpos($domain, '@') + 1);
359+
}
360+
if(function_exists('idn_to_utf8')) {
361+
$domain = idn_to_utf8($domain);
362+
} elseif(file_exists(ISPC_CLASS_PATH.'/idn/idna_convert.class.php')) {
363+
/* use idna class:
364+
* @author Matthias Sommerfeld <mso@phlylabs.de>
365+
* @copyright 2004-2011 phlyLabs Berlin, http://phlylabs.de
366+
* @version 0.8.0 2011-03-11
367+
*/
368+
369+
if(!is_object($this->idn_converter) || $this->idn_converter_name != 'idna_convert.class') {
370+
include_once(ISPC_CLASS_PATH.'/idn/idna_convert.class.php');
371+
$this->idn_converter = new idna_convert(array('idn_version' => 2008));
372+
$this->idn_converter_name = 'idna_convert.class';
373+
}
374+
$domain = $this->idn_converter->decode($domain);
375+
}
376+
377+
if($user_part !== false) return $user_part . '@' . $domain;
378+
else return $domain;
379+
}
313380

314381
}
315382

interface/lib/classes/listform.inc.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ public function decode($record)
291291
if(is_array($record) && count($record) > 0 && is_array($this->listDef['item'])) {
292292
foreach($this->listDef['item'] as $field){
293293
$key = $field['field'];
294+
//* Apply filter to record value.
295+
if(isset($field['filters']) && is_array($field['filters'])) {
296+
$app->uses('tform');
297+
$record[$key] = $app->tform->filterField($key, (isset($record[$key]))?$record[$key]:'', $field['filters'], 'SHOW');
298+
}
294299
if(isset($record[$key])) {
295300
switch ($field['datatype']){
296301
case 'VARCHAR':

interface/lib/classes/remoting_lib.inc.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,10 @@ function filterField($field_name, $field_value, $filters, $filter_event) {
418418
$returnval = strtoupper($field_value);
419419
break;
420420
case 'IDNTOASCII':
421-
if(function_exists('idn_to_ascii')) {
422-
$returnval = idn_to_ascii($field_value);
423-
} else {
424-
$returnval = $field_value;
425-
}
421+
$returnval = $app->functions->idn_encode($field_value);
426422
break;
427423
case 'IDNTOUTF8':
428-
if(function_exists('idn_to_utf8')) {
429-
$returnval = idn_to_utf8($field_value);
430-
} else {
431-
$returnval = $field_value;
432-
}
424+
$returnval = $app->functions->idn_decode($field_value);
433425
break;
434426
default:
435427
$this->errorMessage .= "Unknown Filter: ".$filter['type'];

interface/lib/classes/tform.inc.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ function encode($record,$tab,$dbencode = true) {
629629
if(isset($field['filters']) && is_array($field['filters'])) {
630630
$record[$key] = $this->filterField($key, (isset($record[$key]))?$record[$key]:'', $field['filters'], 'SAVE');
631631
}
632-
633632
//* Validate record value
634633
if(isset($field['validators']) && is_array($field['validators'])) {
635634
$this->validateField($key, (isset($record[$key]))?$record[$key]:'', $field['validators']);
@@ -738,33 +737,24 @@ function filterField($field_name, $field_value, $filters, $filter_event) {
738737
if($filter['event'] == $filter_event) {
739738
switch ($filter['type']) {
740739
case 'TOLOWER':
741-
$returnval = strtolower($field_value);
740+
$returnval = strtolower($returnval);
742741
break;
743742
case 'TOUPPER':
744-
$returnval = strtoupper($field_value);
743+
$returnval = strtoupper($returnval);
745744
break;
746745
case 'IDNTOASCII':
747-
if(function_exists('idn_to_ascii')) {
748-
$returnval = idn_to_ascii($field_value);
749-
} else {
750-
$returnval = $field_value;
751-
}
746+
$returnval = $app->functions->idn_encode($returnval);
752747
break;
753748
case 'IDNTOUTF8':
754-
if(function_exists('idn_to_utf8')) {
755-
$returnval = idn_to_utf8($field_value);
756-
} else {
757-
$returnval = $field_value;
758-
}
749+
$returnval = $app->functions->idn_decode($returnval);
759750
break;
760751
default:
761752
$this->errorMessage .= "Unknown Filter: ".$filter['type'];
762753
break;
763754
}
764755
}
765756
}
766-
767-
return $returnval;
757+
return $returnval;
768758
}
769759

770760
/**

interface/web/admin/form/server_config.tform.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@
109109
'datatype' => 'VARCHAR',
110110
'formtype' => 'TEXT',
111111
'default' => 'server1.domain.tld',
112+
'filters' => array( 0 => array( 'event' => 'SAVE',
113+
'type' => 'IDNTOASCII'),
114+
1 => array( 'event' => 'SHOW',
115+
'type' => 'IDNTOUTF8'),
116+
2 => array( 'event' => 'SAVE',
117+
'type' => 'TOLOWER')
118+
),
112119
'validators' => array(0 => array('type' => 'NOTEMPTY',
113120
'errmsg' => 'hostname_error_empty'),
114121
),

interface/web/admin/form/system_config.tform.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@
212212
),
213213
'admin_mail' => array (
214214
'datatype' => 'VARCHAR',
215+
'filters' => array( 0 => array( 'event' => 'SAVE',
216+
'type' => 'IDNTOASCII'),
217+
1 => array( 'event' => 'SHOW',
218+
'type' => 'IDNTOUTF8'),
219+
2 => array( 'event' => 'SAVE',
220+
'type' => 'TOLOWER')
221+
),
215222
'formtype' => 'TEXT',
216223
'default' => '',
217224
'value' => '',
@@ -234,6 +241,13 @@
234241
),
235242
'smtp_host' => array (
236243
'datatype' => 'VARCHAR',
244+
'filters' => array( 0 => array( 'event' => 'SAVE',
245+
'type' => 'IDNTOASCII'),
246+
1 => array( 'event' => 'SHOW',
247+
'type' => 'IDNTOUTF8'),
248+
2 => array( 'event' => 'SAVE',
249+
'type' => 'TOLOWER')
250+
),
237251
'formtype' => 'TEXT',
238252
'default' => '',
239253
'value' => '',

interface/web/admin/list/server.list.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646

4747
$liste['item'][] = array( 'field' => 'server_name',
4848
'datatype' => 'VARCHAR',
49+
'filters' => array( 0 => array( 'event' => 'SHOW',
50+
'type' => 'IDNTOUTF8')
51+
),
4952
'formtype' => 'TEXT',
5053
'op' => 'like',
5154
'prefix' => '%',

interface/web/client/form/client.tform.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@
285285
'email' => array (
286286
'datatype' => 'VARCHAR',
287287
'formtype' => 'TEXT',
288+
'filters' => array( 0 => array( 'event' => 'SAVE',
289+
'type' => 'IDNTOASCII'),
290+
1 => array( 'event' => 'SHOW',
291+
'type' => 'IDNTOUTF8'),
292+
2 => array( 'event' => 'SAVE',
293+
'type' => 'TOLOWER')
294+
),
288295
'default' => '',
289296
'value' => '',
290297
'separator' => '',

interface/web/client/form/domain.tform.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@
8888
'domain' => array (
8989
'datatype' => 'VARCHAR',
9090
'formtype' => 'TEXT',
91+
'filters' => array( 0 => array( 'event' => 'SAVE',
92+
'type' => 'IDNTOASCII'),
93+
1 => array( 'event' => 'SHOW',
94+
'type' => 'IDNTOUTF8'),
95+
2 => array( 'event' => 'SAVE',
96+
'type' => 'TOLOWER')
97+
),
9198
'validators' => array ( 0 => array ( 'type' => 'NOTEMPTY',
9299
'errmsg'=> 'domain_error_empty'),
93100
1 => array ( 'type' => 'UNIQUE',

interface/web/client/form/reseller.tform.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@
286286
'email' => array (
287287
'datatype' => 'VARCHAR',
288288
'formtype' => 'TEXT',
289+
'filters' => array( 0 => array( 'event' => 'SAVE',
290+
'type' => 'IDNTOASCII'),
291+
1 => array( 'event' => 'SHOW',
292+
'type' => 'IDNTOUTF8'),
293+
2 => array( 'event' => 'SAVE',
294+
'type' => 'TOLOWER')
295+
),
289296
'default' => '',
290297
'value' => '',
291298
'separator' => '',

0 commit comments

Comments
 (0)