2012-07-25 00:54:42 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware - eTemplate serverside URL widget
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @author Nathan Gray
|
2014-01-30 13:48:16 +01:00
|
|
|
* @copyright 2002-14 by RalfBecker@outdoor-training.de
|
2012-07-25 00:54:42 +02:00
|
|
|
* @copyright 2012 Nathan Gray
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* eTemplate URL widget handles URLs, emails & phone numbers
|
|
|
|
*/
|
|
|
|
class etemplate_widget_url extends etemplate_widget
|
|
|
|
{
|
|
|
|
/**
|
2014-01-16 19:10:06 +01:00
|
|
|
* Regexes for validating email addresses incl. email in angle-brackets eg.
|
|
|
|
* + "Ralf Becker <rb@stylite.de>"
|
|
|
|
* + "Ralf Becker (Stylite AG) <rb@stylite.de>"
|
|
|
|
* + "<rb@stylite.de>" or "rb@stylite.de"
|
|
|
|
* + '"Becker, Ralf" <rb@stylite.de>'
|
|
|
|
* + "'Becker, Ralf' <rb@stylite.de>"
|
|
|
|
* but NOT:
|
|
|
|
* - "Becker, Ralf <rb@stylite.de>" (contains comma outside " or ' enclosed block)
|
|
|
|
* - "Becker < Ralf <rb@stylite.de>" (contains < ----------- " ---------------)
|
|
|
|
*
|
|
|
|
* About umlaut or IDN domains: we currently only allow German umlauts in domain part!
|
|
|
|
*
|
2014-01-17 18:26:22 +01:00
|
|
|
* Same preg is in et2_widget_url Javascript class, but no \x00 allowed and /u modifier for utf8!
|
2012-07-25 00:54:42 +02:00
|
|
|
*/
|
2014-10-30 14:20:04 +01:00
|
|
|
const EMAIL_PREG = "/^(([^\042',<][^,<]+|\042[^\042]+\042|\'[^\']+\'|)\s?<)?[^\x01-\x20()<>@,;:\042\[\]]+@([a-z0-9ÄÖÜäöüß](|[a-z0-9ÄÖÜäöüß_-]*[a-z0-9ÄÖÜäöüß])\.)+[a-z]{2,}>?$/iu";
|
2012-07-25 00:54:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate input
|
|
|
|
*
|
|
|
|
* Following attributes get checked:
|
|
|
|
* - needed: value must NOT be empty
|
|
|
|
* - maxlength: maximum length of string (longer strings get truncated to allowed size)
|
|
|
|
* - preg: perl regular expression incl. delimiters (set by default for int, float and colorpicker)
|
|
|
|
* - int and float get casted to their type
|
|
|
|
*
|
|
|
|
* @param string $cname current namespace
|
|
|
|
* @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
|
|
|
* @param array $content
|
|
|
|
* @param array &$validated=array() validated content
|
|
|
|
* @param array $expand=array values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
|
|
|
*/
|
|
|
|
public function validate($cname, array $expand, array $content, &$validated=array())
|
|
|
|
{
|
|
|
|
$form_name = self::form_name($cname, $this->id, $expand);
|
|
|
|
|
|
|
|
if (!$this->is_readonly($cname, $form_name))
|
|
|
|
{
|
|
|
|
$value = $value_in = self::get_array($content, $form_name);
|
|
|
|
|
|
|
|
if ((string)$value === '' && $this->attrs['needed'])
|
|
|
|
{
|
|
|
|
self::set_validation_error($form_name,lang('Field must not be empty !!!'),'');
|
|
|
|
return;
|
|
|
|
}
|
2013-02-04 14:22:37 +01:00
|
|
|
elseif ((string)$value != '' && !isset($this->attrs['preg']))
|
2012-07-25 00:54:42 +02:00
|
|
|
{
|
|
|
|
switch($this->type)
|
|
|
|
{
|
|
|
|
case 'url':
|
2014-01-30 13:48:16 +01:00
|
|
|
// if no protocol given eg. "www.egroupware.org" prepend "http://" for validation
|
|
|
|
if (($missing_protocol = strpos($value, '://') === false))
|
|
|
|
{
|
|
|
|
$value = 'http://'.$value;
|
|
|
|
}
|
|
|
|
$url_valid = filter_var($value, FILTER_VALIDATE_URL) ||
|
2012-07-25 00:54:42 +02:00
|
|
|
// Remove intl chars & check again, but if it passes we'll keep the original
|
2014-01-30 13:48:16 +01:00
|
|
|
filter_var(preg_replace('/[^[:print:]]/','',$value), FILTER_VALIDATE_URL);
|
|
|
|
//error_log(__METHOD__."() filter_var(value=".array2string($value).", FILTER_VALIDATE_URL)=".array2string(filter_var($value, FILTER_VALIDATE_URL))." --> url_valid=".array2string($url_valid));
|
|
|
|
// remove http:// validation prefix again
|
|
|
|
if ($missing_protocol)
|
|
|
|
{
|
|
|
|
$value = substr($value, 7);
|
|
|
|
}
|
|
|
|
if (!$url_valid)
|
2012-07-25 00:54:42 +02:00
|
|
|
{
|
2014-01-30 13:48:16 +01:00
|
|
|
self::set_validation_error($form_name,lang("'%1' has an invalid format !!!",$value),'');
|
2012-07-25 00:54:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'url-email':
|
2014-01-17 18:26:22 +01:00
|
|
|
$this->attrs['preg'] = self::EMAIL_PREG;
|
2012-07-25 00:54:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$valid =& self::get_array($validated, $form_name, true);
|
|
|
|
|
|
|
|
if ((int) $this->attrs['maxlength'] > 0 && strlen($value) > (int) $this->attrs['maxlength'])
|
|
|
|
{
|
|
|
|
$value = substr($value,0,(int) $this->attrs['maxlength']);
|
|
|
|
}
|
|
|
|
if ($this->attrs['preg'] && !preg_match($this->attrs['preg'],$value))
|
|
|
|
{
|
|
|
|
switch($this->type)
|
|
|
|
{
|
|
|
|
default:
|
2014-01-17 18:26:22 +01:00
|
|
|
//error_log("preg_match('{$this->attrs['preg']}', '$value')=".array2string(preg_match($this->attrs['preg'], $value)));
|
2014-01-30 13:48:16 +01:00
|
|
|
self::set_validation_error($form_name,lang("'%1' has an invalid format !!!",$value)/*." !preg_match('$this->attrs[preg]', '$value')"*/,'');
|
2012-07-25 00:54:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$valid = $value;
|
2014-01-30 13:48:16 +01:00
|
|
|
//error_log(__METHOD__."() $form_name: ".array2string($value_in).' --> '.array2string($value));
|
2012-07-25 00:54:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
etemplate_widget::registerWidget('etemplate_widget_url', array('url'));
|