00001 <?php
00002
00003 namespace Habari;
00004
00010 class FormValidators
00011 {
00012
00018 public static function have($validator)
00019 {
00020 $methods = get_class_methods(__CLASS__);
00021 return in_array($validator, $methods);
00022 }
00023
00033 public static function validate_url( $text, $control, $form, $warning = null, $schemes = array( 'http', 'https' ) )
00034 {
00035 if ( ! empty( $text ) ) {
00036 $parsed = InputFilter::parse_url( $text );
00037 if ( $parsed['is_relative'] ) {
00038
00039 $parsed = InputFilter::parse_url( 'http://' . $text );
00040 if ( $parsed['is_error'] ) {
00041
00042 $warning = empty( $warning ) ? _t( 'Relative urls are not allowed' ) : $warning;
00043 return array( $warning );
00044 }
00045 }
00046 if ( $parsed['is_pseudo'] || ! in_array( $parsed['scheme'], $schemes ) ) {
00047
00048 $warning = empty( $warning ) ? _t( 'Only %s urls are allowed', array( Format::and_list( $schemes ) ) ) : $warning;
00049 return array( $warning );
00050 }
00051 }
00052 return array();
00053 }
00054
00065 public static function validate_email( $text, $control, $form, $warning = null )
00066 {
00067 if ( ! empty( $text ) ) {
00068 if ( !preg_match( "@^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*\@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$@i", $text ) ) {
00069 $warning = empty( $warning ) ? _t( 'Value for %s must be a valid Email Address.', array($control->get_label()) ) : $warning;
00070 return array( $warning );
00071 }
00072 }
00073 return array();
00074 }
00075
00085 public static function validate_required( $value, $control, $form, $warning = null )
00086 {
00087 if ( empty( $value ) || $value == '' ) {
00088 $warning = empty( $warning ) ? _t( 'A value for the %s field is required.', array($control->get_label()) ) : $warning;
00089 return array( $warning );
00090 }
00091 return array();
00092 }
00093
00104 public static function validate_username( $value, $control, $form, $allowed_name = null, $warning = null )
00105 {
00106 if ( isset( $allowed_name ) && ( $value == $allowed_name ) ) {
00107 return array();
00108 }
00109 if ( User::get_by_name( $value ) ) {
00110 $warning = empty( $warning ) ? _t( 'That username supplied for %s is already in use.', array($control->get_label()) ) : $warning;
00111 return array( $warning );
00112 }
00113 return array();
00114 }
00115
00126 public static function validate_groupname( $value, $control, $form, $allowed_name = null, $warning = null )
00127 {
00128 if ( isset( $allowed_name ) && ( $value == $allowed_name ) ) {
00129 return array();
00130 }
00131 if ( UserGroup::exists( $value ) ) {
00132 $warning = empty( $warning ) ? _t( 'The group %s already exists.', array($value) ) : $warning;
00133 return array( $warning );
00134 }
00135 return array();
00136 }
00137
00138
00149 public static function validate_same( $value, $control, $form, $matcher, $warning = null )
00150 {
00151 if ( $value != $matcher->value ) {
00152 $warning = empty( $warning ) ? _t( 'The value of the %1$s field must match the value of %2$s.', array( $control->get_label(), $matcher->get_label() ) ) : $warning;
00153 return array( $warning );
00154 }
00155 return array();
00156 }
00157
00168 public static function validate_regex( $value, $control, $container, $regex, $warning = null )
00169 {
00170 if ( preg_match( $regex, $value ) ) {
00171 return array();
00172 }
00173 else {
00174 if ( $warning == null ) {
00175 $warning = _t( 'The value for %s does not meet submission requirements', array($control->get_label()) );
00176 }
00177 return array( $warning );
00178 }
00179 }
00180
00192 public static function validate_range( $value, $control, $container, $min, $max, $warning = null )
00193 {
00194 if ( $value < $min ) {
00195 if ( $warning == null ) {
00196 $warning = _t( 'The value entered for %s is lesser than the minimum of %d.', array( $control->get_label(), $min ) );
00197 }
00198 return array( $warning );
00199 }
00200 elseif ( $value > $max ) {
00201 if ( $warning == null ) {
00202 $warning = _t( 'The value entered for %s is greater than the maximum of %d.', array( $control->get_label(), $max ) );
00203 }
00204 return array( $warning );
00205 }
00206 else {
00207 return array();
00208 }
00209 }
00210
00220 public static function validate_wsse( $value, $control, $form, $warning = null )
00221 {
00222 if(!Utils::verify_wsse($_POST, true)) {
00223 $warning = empty( $warning ) ? _t( 'This form must be submitted directly.' ) : $warning;
00224 return array( $warning );
00225 }
00226 return array();
00227 }
00228 }
00229
00230 ?>