use IdnaConvert from composer

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2018-12-18 10:40:16 +01:00
parent 4c27efa4ae
commit c9ee2ae7e0
28 changed files with 231 additions and 7767 deletions

View File

@@ -203,8 +203,7 @@ class Admins extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEnt
// validation
$name = validate($name, 'name', '', '', array(), true);
// @fixme idna_convert_wrapper
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$email = $idna_convert->encode(validate($email, 'email', '', '', array(), true));
$def_language = validate($def_language, 'default language', '', '', array(), true);
$custom_notes = validate(str_replace("\r\n", "\n", $custom_notes), 'custom_notes', '/^[^\0]*$/', '', array(), true);
@@ -461,8 +460,7 @@ class Admins extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEnt
if ($this->getUserDetail('change_serversettings') == 1 || $result['adminid'] == $this->getUserDetail('adminid')) {
// parameters
$name = $this->getParam('name', true, $result['name']);
// @fixme idna_convert_wrapper
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$email = $this->getParam('email', true, $idna_convert->decode($result['email']));
$password = $this->getParam('admin_password', true, '');
$def_language = $this->getParam('def_language', true, $result['def_language']);
@@ -521,8 +519,7 @@ class Admins extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEnt
// validation
$name = validate($name, 'name', '', '', array(), true);
// @fixme idna_convert_wrapper
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$email = $idna_convert->encode(validate($email, 'email', '', '', array(), true));
$def_language = validate($def_language, 'default language', '', '', array(), true);
$custom_notes = validate(str_replace("\r\n", "\n", $custom_notes), 'custom_notes', '/^[^\0]*$/', '', array(), true);

View File

@@ -2,7 +2,6 @@
namespace Froxlor\Api\Commands;
use Froxlor\Database as Database;
use Froxlor\Settings as Settings;
/**
* This file is part of the Froxlor project.

View File

@@ -267,8 +267,7 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource
$city = validate($city, 'city', '', '', array(), true);
$phone = validate($phone, 'phone', '/^[0-9\- \+\(\)\/]*$/', '', array(), true);
$fax = validate($fax, 'fax', '/^[0-9\- \+\(\)\/]*$/', '', array(), true);
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$email = $idna_convert->encode(validate($email, 'email', '', '', array(), true));
$customernumber = validate($customernumber, 'customer number', '/^[A-Za-z0-9 \-]*$/Di', '', array(), true);
$def_language = validate($def_language, 'default language', '', '', array(), true);
@@ -868,8 +867,7 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource
// parameters
$move_to_admin = intval_ressource($this->getBoolParam('move_to_admin', true, 0));
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$email = $this->getParam('email', true, $idna_convert->decode($result['email']));
$name = $this->getParam('name', true, $result['name']);
$firstname = $this->getParam('firstname', true, $result['firstname']);
@@ -917,8 +915,7 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource
// validation
if ($this->isAdmin()) {
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$name = validate($name, 'name', '', '', array(), true);
$firstname = validate($firstname, 'first name', '', '', array(), true);
$company = validate($company, 'company', '', '', array(), true);
@@ -1093,8 +1090,7 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource
Database::needRoot(true);
$last_dbserver = 0;
// @fixme dbManager
$dbm = new \DbManager($this->logger());
$dbm = new \Froxlor\Database\DbManager($this->logger());
// For each of them
while ($row_database = $databases_stmt->fetch(\PDO::FETCH_ASSOC)) {
@@ -1402,7 +1398,6 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource
));
$id = $result['customerid'];
// @fixme use Databases-ApiCommand later
$databases_stmt = Database::prepare("
SELECT * FROM `" . TABLE_PANEL_DATABASES . "`
WHERE `customerid` = :id ORDER BY `dbserver`
@@ -1413,8 +1408,7 @@ class Customers extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resource
Database::needRoot(true);
$last_dbserver = 0;
// @fixme db manager
$dbm = new \DbManager($this->logger());
$dbm = new \Froxlor\Database\DbManager($this->logger());
while ($row_database = $databases_stmt->fetch(\PDO::FETCH_ASSOC)) {
if ($last_dbserver != $row_database['dbserver']) {

View File

@@ -76,8 +76,7 @@ class DomainZones extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resour
standard_error('dns_domain_nodns', '', true);
}
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$domain = $idna_convert->encode($result['domain']);
// select all entries

View File

@@ -82,8 +82,7 @@ class Domains extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEn
// convert possible idn domain to punycode
if (substr($domainname, 0, 4) != 'xn--') {
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$domainname = $idna_convert->encode($domainname);
}
@@ -240,8 +239,7 @@ class Domains extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEn
standard_error('domain_nopunycode', '', true);
}
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$domain = $idna_convert->encode(preg_replace(array(
'/\:(\d)+$/',
'/^https?\:\/\//'
@@ -504,8 +502,7 @@ class Domains extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEn
$issubof = '0';
}
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
if ($domain == '') {
standard_error(array(
'stringisempty',

View File

@@ -82,8 +82,7 @@ class EmailAccounts extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Reso
$id = $result['id'];
$email_full = $result['email_full'];
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$username = $idna_convert->decode($email_full);
$password = validate($email_password, 'password', '', '', array(), true);
$password = validatePassword($password, true);

View File

@@ -54,8 +54,7 @@ class EmailForwarders extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Re
$destination = $this->getParam('destination');
// validation
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$destination = $idna_convert->encode($destination);
$result = $this->apiCall('Emails.get', array(

View File

@@ -57,8 +57,7 @@ class Emails extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEnt
// validation
if (substr($domain, 0, 4) != 'xn--') {
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$domain = $idna_convert->encode(validate($domain, 'domain', '', '', array(), true));
}

View File

@@ -80,8 +80,7 @@ class Ftps extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEntit
if (Settings::Get('customer.ftpatdomain') == '1') {
$ftpusername = validate(trim($ftpusername), 'username', '/^[a-zA-Z0-9][a-zA-Z0-9\-_]+\$?$/', '', array(), true);
if (substr($ftpdomain, 0, 4) != 'xn--') {
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$ftpdomain = $idna_convert->encode(validate($ftpdomain, 'domain', '', '', array(), true));
}
}

View File

@@ -79,8 +79,7 @@ class Mysqls extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEnt
'mysql_lastaccountnumber' => ($this->isAdmin() ? $customer['mysql_lastaccountnumber'] : $this->getUserDetail('mysql_lastaccountnumber'))
);
// create database, user, set permissions, etc.pp.
// @fixme dbManager
$dbm = new \DbManager($this->logger());
$dbm = new \Froxlor\Database\DbManager($this->logger());
$username = $dbm->createDatabase($newdb_params['loginname'], $password, $newdb_params['mysql_lastaccountnumber']);
// we've checked against the password in dbm->createDatabase
@@ -459,8 +458,7 @@ class Mysqls extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\ResourceEnt
// Begin root-session
Database::needRoot(true, $result['dbserver']);
// @fixme dbManager
$dbm = new \DbManager($this->logger());
$dbm = new \Froxlor\Database\DbManager($this->logger());
$dbm->getManager()->deleteDatabase($result['databasename']);
Database::needRoot(false);
// End root-session

View File

@@ -94,8 +94,7 @@ class SubDomains extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resourc
standard_error('domain_nopunycode', '', true);
}
// @fixme idna_convert_wrapper
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$subdomain = $idna_convert->encode(preg_replace(array(
'/\:(\d)+$/',
'/^https?\:\/\//'
@@ -342,8 +341,7 @@ class SubDomains extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resourc
// convert possible idn domain to punycode
if (substr($domainname, 0, 4) != 'xn--') {
// @fixme idna_convert_wrapper
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$domainname = $idna_convert->encode($domainname);
}
@@ -581,8 +579,7 @@ class SubDomains extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resourc
Database::pexecute($stmt, $params, true, true);
$stmt = Database::prepare("DELETE FROM `" . TABLE_MAIL_VIRTUAL . "` WHERE `customerid`= :customerid AND `domainid`= :domainid");
Database::pexecute($stmt, $params, true, true);
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$this->logger()->logAction($this->isAdmin() ? ADM_ACTION : USR_ACTION, LOG_NOTICE, "[API] automatically deleted mail-table entries for '" . $idna_convert->decode($result['domain']) . "'");
}
@@ -647,8 +644,7 @@ class SubDomains extends \Froxlor\Api\ApiCommand implements \Froxlor\Api\Resourc
inserttask('1');
inserttask('4');
// @fixme idna
$idna_convert = new \idna_convert_wrapper();
$idna_convert = new \Froxlor\Idna\IdnaWrapper();
$this->logger()->logAction($this->isAdmin() ? ADM_ACTION : USR_ACTION, LOG_INFO, "[API] edited domain '" . $idna_convert->decode($result['domain']) . "'");
}
$result = $this->apiCall('SubDomains.get', array(

View File

@@ -0,0 +1,130 @@
<?php
namespace Froxlor\Database;
use \Froxlor\Settings;
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Michael Kaufmann <mkaufmann@nutime.de>
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Classes
*
* @since 0.9.31
*
*/
/**
* Class DbManager
*
* Wrapper-class for database-management like creating
* and removing databases, users and permissions
*
* @copyright (c) the authors
* @author Michael Kaufmann <mkaufmann@nutime.de>
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Classes
*/
class DbManager {
/**
* FroxlorLogger object
* @var object
*/
private $_log = null;
/**
* Manager object
* @var object
*/
private $_manager = null;
/**
* main constructor
*
* @param \FroxlorLogger $log
*/
public function __construct($log = null) {
$this->_log = $log;
$this->_setManager();
}
/**
* creates a new database and a user with the
* same name with all privileges granted on the db.
* DB-name and user-name are being generated and
* the password for the user will be set
*
* @param string $loginname
* @param string $password
* @param int $last_accnumber
*
* @return string|bool $username if successful or false of username is equal to the password
*/
public function createDatabase($loginname = null, $password = null, $last_accnumber = 0) {
Database::needRoot(true);
// check whether we shall create a random username
if (strtoupper(Settings::Get('customer.mysqlprefix')) == 'RANDOM') {
// get all usernames from db-manager
$allsqlusers = $this->getManager()->getAllSqlUsers();
// generate random username
$username = $loginname . '-' . substr(md5(uniqid(microtime(), 1)), 20, 3);
// check whether it exists on the DBMS
while (in_array($username , $allsqlusers)) {
$username = $loginname . '-' . substr(md5(uniqid(microtime(), 1)), 20, 3);
}
} else {
$username = $loginname . Settings::Get('customer.mysqlprefix') . (intval($last_accnumber) + 1);
}
// don't use a password that is the same as the username
if ($username == $password) {
return false;
}
// now create the database itself
$this->getManager()->createDatabase($username);
$this->_log->logAction(USR_ACTION, LOG_INFO, "created database '" . $username . "'");
// and give permission to the user on every access-host we have
foreach (array_map('trim', explode(',', Settings::Get('system.mysql_access_host'))) as $mysql_access_host) {
$this->getManager()->grantPrivilegesTo($username, $password, $mysql_access_host);
$this->_log->logAction(USR_ACTION, LOG_NOTICE, "grant all privileges for '" . $username . "'@'" . $mysql_access_host . "'");
}
$this->getManager()->flushPrivileges();
Database::needRoot(false);
return $username;
}
/**
* returns the manager-object
* from where we can control it
*/
public function getManager() {
return $this->_manager;
}
/**
* set manager-object by type of
* dbms: mysql only for now
*
* sets private $_manager variable
*/
private function _setManager() {
// TODO read different dbms from settings later
$this->_manager = new \Froxlor\Database\Manager\DbManagerMySQL($this->_log);
}
}

View File

@@ -0,0 +1,239 @@
<?php
namespace Froxlor\Database\Manager;
use Froxlor\Database;
/**
* This file is part of the Froxlor project.
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Michael Kaufmann <mkaufmann@nutime.de>
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Classes
*
* @since 0.9.31
*
*/
/**
* Class DbManagerMySQL
*
* Explicit class for database-management like creating
* and removing databases, users and permissions for MySQL
*
* @copyright (c) the authors
* @author Michael Kaufmann <mkaufmann@nutime.de>
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Classes
*/
class DbManagerMySQL
{
/**
* FroxlorLogger object
*
* @var object
*/
private $_log = null;
/**
* main constructor
*
* @param \FroxlorLogger $log
*/
public function __construct(&$log = null)
{
$this->_log = $log;
}
/**
* creates a database
*
* @param string $dbname
*/
public function createDatabase($dbname = null)
{
Database::query("CREATE DATABASE `" . $dbname . "`");
}
/**
* grants access privileges on a database with the same
* username and sets the password for that user the given access_host
*
* @param string $username
* @param string $password
* @param string $access_host
* @param bool $p_encrypted
* optional, whether the password is encrypted or not, default false
*/
public function grantPrivilegesTo($username = null, $password = null, $access_host = null, $p_encrypted = false)
{
// mysql8 compatibility
if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '8.0.11', '>=')) {
// create user
$stmt = Database::prepare("
CREATE USER '" . $username . "'@'" . $access_host . "' IDENTIFIED BY 'password'
");
Database::pexecute($stmt);
// grant privileges
$stmt = Database::prepare("
GRANT ALL ON `" . $username . "`.* TO :username@:host
");
Database::pexecute($stmt, array(
"username" => $username,
"host" => $access_host
));
} else {
// grant privileges
$stmt = Database::prepare("
GRANT ALL PRIVILEGES ON `" . $username . "`.* TO :username@:host IDENTIFIED BY 'password'
");
Database::pexecute($stmt, array(
"username" => $username,
"host" => $access_host
));
}
// set passoword
if (version_compare(Database::getAttribute(\PDO::ATTR_SERVER_VERSION), '5.7.6', '<')) {
if ($p_encrypted) {
$stmt = Database::prepare("SET PASSWORD FOR :username@:host = :password");
} else {
$stmt = Database::prepare("SET PASSWORD FOR :username@:host = PASSWORD(:password)");
}
} else {
$stmt = Database::prepare("ALTER USER :username@:host IDENTIFIED BY :password");
}
Database::pexecute($stmt, array(
"username" => $username,
"host" => $access_host,
"password" => $password
));
}
/**
* removes the given database from the dbms and also
* takes away any privileges from a user to that db
*
* @param string $dbname
*/
public function deleteDatabase($dbname = null)
{
if (Database::getAttribute(\PDO::ATTR_SERVER_VERSION) < '5.0.2') {
// failsafe if user has been deleted manually (requires MySQL 4.1.2+)
$stmt = Database::prepare("REVOKE ALL PRIVILEGES, GRANT OPTION FROM `" . $dbname . "`");
Database::pexecute($stmt, array(), false);
}
$host_res_stmt = Database::prepare("
SELECT `Host` FROM `mysql`.`user` WHERE `User` = :dbname");
Database::pexecute($host_res_stmt, array(
'dbname' => $dbname
));
while ($host = $host_res_stmt->fetch(\PDO::FETCH_ASSOC)) {
// as of MySQL 5.0.2 this also revokes privileges. (requires MySQL 4.1.2+)
$drop_stmt = Database::prepare("DROP USER IF EXISTS :dbname@:host");
Database::pexecute($drop_stmt, array(
'dbname' => $dbname,
'host' => $host['Host']
), false);
}
$drop_stmt = Database::prepare("DROP DATABASE IF EXISTS `" . $dbname . "`");
Database::pexecute($drop_stmt);
}
/**
* removes a user from the dbms and revokes all privileges
*
* @param string $username
* @param string $host
*/
public function deleteUser($username = null, $host = null)
{
if (Database::getAttribute(\PDO::ATTR_SERVER_VERSION) < '5.0.2') {
// Revoke privileges (only required for MySQL 4.1.2 - 5.0.1)
$stmt = Database::prepare("REVOKE ALL PRIVILEGES ON * . * FROM `" . $username . "`@`" . $host . "`");
Database::pexecute($stmt);
}
// as of MySQL 5.0.2 this also revokes privileges. (requires MySQL 4.1.2+)
$stmt = Database::prepare("DROP USER :username@:host");
Database::pexecute($stmt, array(
"username" => $username,
"host" => $host
));
}
/**
* removes permissions from a user
*
* @param string $username
* @param string $host
* (unused in mysql)
*/
public function disableUser($username = null, $host = null)
{
$stmt = Database::prepare('REVOKE ALL PRIVILEGES, GRANT OPTION FROM `' . $username . '`@`' . $host . '`');
Database::pexecute($stmt, array(), false);
}
/**
* re-grant permissions to a user
*
* @param string $username
* @param string $host
*/
public function enableUser($username = null, $host = null)
{
Database::query('GRANT ALL PRIVILEGES ON `' . $username . '`.* TO `' . $username . '`@`' . $host . '`');
Database::query('GRANT ALL PRIVILEGES ON `' . str_replace('_', '\_', $username) . '` . * TO `' . $username . '`@`' . $host . '`');
}
/**
* flushes the privileges...pretty obvious eh?
*/
public function flushPrivileges()
{
Database::query("FLUSH PRIVILEGES");
}
/**
* return an array of all usernames used in that DBMS
*
* @param bool $user_only
* if false, * will be selected from mysql.user and slightly different array will be generated
*
* @return array
*/
public function getAllSqlUsers($user_only = true)
{
if ($user_only == false) {
$result_stmt = Database::prepare('SELECT * FROM mysql.user');
} else {
$result_stmt = Database::prepare('SELECT `User` FROM mysql.user');
}
Database::pexecute($result_stmt);
$allsqlusers = array();
while ($row = $result_stmt->fetch(\PDO::FETCH_ASSOC)) {
if ($user_only == false) {
if (! isset($allsqlusers[$row['User']]) || ! is_array($allsqlusers[$row['User']])) {
$allsqlusers[$row['User']] = array(
'password' => $row['Password'],
'hosts' => array()
);
}
$allsqlusers[$row['User']]['hosts'][] = $row['Host'];
} else {
$allsqlusers[] = $row['User'];
}
}
return $allsqlusers;
}
}

View File

@@ -0,0 +1,119 @@
<?php
namespace Froxlor\Idna;
/**
* This file is part of the Froxlor project.
* Copyright (c) 2003-2009 the SysCP Team (see authors).
* Copyright (c) 2010 the Froxlor Team (see authors).
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code. You can also view the
* COPYING file online at http://files.froxlor.org/misc/COPYING.txt
*
* @copyright (c) the authors
* @author Michael Duergner <michael@duergner.com> (2003-2009)
* @author Froxlor team <team@froxlor.org> (2010-)
* @license GPLv2 http://files.froxlor.org/misc/COPYING.txt
* @package Classes
*
*/
/**
* Class for wrapping a specific idna conversion class and offering a standard interface
*
* @package Functions
*/
class IdnaWrapper
{
/**
* idna converter we use
*
* @var object
*/
private $idna_converter;
/**
* Class constructor.
* Creates a new idna converter
*/
public function __construct()
{
// Instantiate it
$this->idna_converter = new \Mso\IdnaConvert\IdnaConvert();
}
/**
* Encode a domain name, a email address or a list of one of both.
*
* @param
* string May be either a single domain name, e single email address or a list of one
* separated either by ',', ';' or ' '.
*
* @return string Returns either a single domain name, a single email address or a list of one of
* both separated by the same string as the input.
*/
public function encode($to_encode)
{
$to_encode = $this->is_utf8($to_encode) ? $to_encode : utf8_encode($to_encode);
return $this->idna_converter->encode($to_encode);
}
public function encode_uri($to_encode)
{
$to_encode = $this->is_utf8($to_encode) ? $to_encode : utf8_encode($to_encode);
return $this->idna_converter->encodeUri($to_encode);
}
/**
* Decode a domain name, a email address or a list of one of both.
*
* @param
* string May be either a single domain name, e single email address or a list of one
* separated either by ',', ';' or ' '.
*
* @return string Returns either a single domain name, a single email address or a list of one of
* both separated by the same string as the input.
*/
public function decode($to_decode)
{
return $this->idna_converter->decode($to_decode);
}
/**
* check whether a string is utf-8 encoded or not
*
* @param string $string
*
* @return boolean
*/
private function is_utf8($string = null)
{
if (function_exists("mb_detect_encoding")) {
if (mb_detect_encoding($string, 'UTF-8, ISO-8859-1') === 'UTF-8') {
return true;
}
return false;
}
$strlen = strlen($string);
for ($i = 0; $i < $strlen; $i ++) {
$ord = ord($string[$i]);
if ($ord < 0x80)
continue; // 0bbbbbbb
elseif (($ord & 0xE0) === 0xC0 && $ord > 0xC1)
$n = 1; // 110bbbbb (exkl C0-C1)
elseif (($ord & 0xF0) === 0xE0)
$n = 2; // 1110bbbb
elseif (($ord & 0xF8) === 0xF0 && $ord < 0xF5)
$n = 3; // 11110bbb (exkl F5-FF)
else
return false; // ungültiges UTF-8-Zeichen
for ($c = 0; $c < $n; $c ++) // $n Folgebytes? // 10bbbbbb
if (++ $i === $strlen || (ord($string[$i]) & 0xC0) !== 0x80)
// ungültiges UTF-8-Zeichen
return false;
}
// kein ungültiges UTF-8-Zeichen gefunden
return true;
}
}