XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
XCube_ActionForm.class.php
1<?php
39
40if ( ! defined( 'XCUBE_CORE_PATH' ) ) {
41 define( 'XCUBE_CORE_PATH', __DIR__ );
42}
43
44require_once XCUBE_CORE_PATH . '/XCube_Root.class.php';
45
46require_once XCUBE_CORE_PATH . '/XCube_Property.class.php';
47require_once XCUBE_CORE_PATH . '/XCube_Validator.class.php';
48require_once XCUBE_CORE_PATH . '/XCube_FormFile.class.php';
49
50
59 public $mContext;
60
69 public $mUser;
70
75 public $mFormProperties = [];
76
81 public $mFieldProperties = [];
82
89 public $mErrorFlag = false;
90
95 public $mErrorMessages = [];
96
103 public $_mToken;
104
109 public function __construct() {
110 $root =& XCube_Root::getSingleton();
111 $this->mContext =& $root->getContext();
112 $this->mUser =& $this->mContext->getUser();
113 }
114
119 public function prepare() {
120 }
121
130 public function getTokenName() {
131 return null;
132 }
133
143 public function getToken() {
144 if ( null === $this->_mToken ) {
145 mt_srand( (int) (microtime( true ) * 100000) );
146 $root =& XCube_Root::getSingleton();
147 $salt = $root->getSiteConfig( 'Cube', 'Salt' );
148 $this->_mToken = md5( $salt . uniqid( random_int(0, mt_getrandmax()), true ) );
149
150 $_SESSION['XCUBE_TOKEN'][ $this->getTokenName() ] = $this->_mToken;
151 }
152
153 return $this->_mToken;
154 }
155
161 public function getTokenErrorMessage() {
162 return _TOKEN_ERROR; //< FIXME
163 }
164
183 public function set() {
184 if ( isset( $this->mFormProperties[ func_get_arg( 0 ) ] ) ) {
185 if ( func_num_args() === 2 ) {
186 $value = func_get_arg( 1 );
187 $this->mFormProperties[ func_get_arg( 0 ) ]->setValue( $value );
188 } elseif ( func_num_args() === 3 ) {
189 $index = func_get_arg( 1 );
190 $value = func_get_arg( 2 );
191 $this->mFormProperties[ func_get_arg( 0 ) ]->setValue( $index, $value );
192 }
193 }
194 }
195
199 public function setVar() {
200 if ( isset( $this->mFormProperties[ func_get_arg( 0 ) ] ) ) {
201 if ( func_num_args() === 2 ) {
202 $this->mFormProperties[ func_get_arg( 0 ) ]->setValue( func_get_arg( 1 ) );
203 } elseif ( func_num_args() === 3 ) {
204 $this->mFormProperties[ func_get_arg( 0 ) ]->setValue( func_get_arg( 1 ), func_get_arg( 2 ) );
205 }
206 }
207 }
208
222 public function get( $key, $index = null ) {
223 return isset( $this->mFormProperties[ $key ] ) ? $this->mFormProperties[ $key ]->getValue( $index ) : null;
224 }
225
233 public function getVar( $key, $index = null ) {
234 return $this->get( $key, $index );
235 }
236
245 public function &getFormProperties() {
247 }
248
267 public function fetch() {
268 foreach ( array_keys( $this->mFormProperties ) as $name ) {
269 if ( $this->mFormProperties[ $name ]->hasFetchControl() ) {
270 $this->mFormProperties[ $name ]->fetch( $this );
271 } else {
272 $value = $this->mContext->mRequest->getRequest( $name );
273 $this->mFormProperties[ $name ]->set( $value );
274 }
275 $methodName = 'fetch' . ucfirst( $name );
276 if ( method_exists( $this, $methodName ) ) {
277 // call_user_func(array($this,$methodName));
278 $this->$methodName();
279 }
280 }
281 }
282
293 public function _validateToken() {
294 //
295 // check one-time & transaction token
296 //
297 if ( null !== $this->getTokenName() ) {
298 $key = str_replace( '.', '_', $this->getTokenName() );
299 $token = $_REQUEST[ $key ] ?? null;
300
301 $flag = true;
302
303 if ( ! isset( $_SESSION['XCUBE_TOKEN'][ $this->getTokenName() ] ) ) {
304 $flag = false;
305 } elseif ( $_SESSION['XCUBE_TOKEN'][ $this->getTokenName() ] != $token ) {
306 unset( $_SESSION['XCUBE_TOKEN'][ $this->getTokenName() ] );
307 $flag = false;
308 }
309
310 if ( ! $flag ) {
311 $message = $this->getTokenErrorMessage();
312 if ( null === $message ) {
313 $this->mErrorFlag = true;
314 } else {
315 $this->addErrorMessage( $message );
316 }
317 }
318
319 //
320 // clear token
321 //
322 unset( $_SESSION['XCUBE_TOKEN'][ $this->getTokenName() ] );
323 }
324 }
325
326
344 public function validate() {
345 $this->_validateToken();
346
347 foreach ( array_keys( $this->mFormProperties ) as $name ) {
348 if ( isset( $this->mFieldProperties[ $name ] ) ) {
349 if ( $this->mFormProperties[ $name ]->isArray() ) {
350 foreach ( array_keys( $this->mFormProperties[ $name ]->mProperties ) as $_name ) {
351 $this->mFieldProperties[ $name ]->validate( $this->mFormProperties[ $name ]->mProperties[ $_name ] );
352 }
353 } else {
354 $this->mFieldProperties[ $name ]->validate( $this->mFormProperties[ $name ] );
355 }
356 }
357 }
358
359 //
360 // If this class has original validation methods, call them.
361 //
362 foreach ( array_keys( $this->mFormProperties ) as $name ) {
363 $methodName = 'validate' . ucfirst( $name );
364 if ( method_exists( $this, $methodName ) ) {
365 // call_user_func(array($this,$methodName));
366 $this->$methodName();
367 }
368 }
369 }
370
376 public function hasError() {
377 return ( count( $this->mErrorMessages ) > 0 || $this->mErrorFlag );
378 }
379
386 public function addErrorMessage( $message ) {
387 $this->mErrorMessages[] = $message;
388 }
389
395 public function getErrorMessages() {
397 }
398
413 public function load( &$obj ) {
414 }
415
430 public function update( &$obj ) {
431 }
432}
433
443 public $mForm;
444
449 public $mDepends;
450
472
478
488 public function __construct( &$form ) {
489 $this->mForm =& $form;
490 }
491
500 public function setDependsByArray( $dependsArr ) {
501 foreach ( $dependsArr as $dependName ) {
502 $instance =& XCube_DependClassFactory::factoryClass( $dependName );
503 if ( null !== $instance ) {
504 $this->mDepends[ $dependName ] =& $instance;
505 }
506
507 unset( $instance );
508 }
509 }
510
527 public function addMessage( $name, $message ) {
528 if ( func_num_args() >= 2 ) {
529 $args = func_get_args();
530 $this->mMessages[ $args[0] ]['message'] = $args[1];
531 for ( $i = 0; isset( $args[ $i + 2 ] ); $i ++ ) {
532 $this->mMessages[ $args[0] ]['args'][ $i ] = $args[ $i + 2 ];
533 }
534 }
535 }
536
555 public function renderMessage(string $name ) {
556 if ( ! isset( $this->mMessages[ $name ] ) ) {
557 return null;
558 }
559
560 $message = $this->mMessages[ $name ]['message'];
561
562 if ( isset( $this->mMessages[ $name ]['args'] ) ) {
563 // Use a unity method.
564 $message = XCube_Utils::formatString( $message, $this->mMessages[ $name ]['args'] );
565 }
566
567 return $message;
568 }
569
583 public function addVar( $name, $value ) {
584 $this->mVariables[ $name ] = $value;
585 }
586
598 public function validate( &$form ) {
599 if ( is_array( $this->mDepends ) && count( $this->mDepends ) > 0 ) {
600 foreach ( $this->mDepends as $name => $depend ) {
601 if ( ! $depend->isValid( $form, $this->mVariables ) ) {
602 // Error
603 // NOTICE: This is temporary until we decide how to handle the errors.
604 $this->mForm->mErrorFlag = true;
605
606 // TEST!!
607 $this->mForm->addErrorMessage( $this->renderMessage( $name ) );
608 } else {
609 // OK check
611 return null;
612 }
613 }
614 }
615 }
616}
617
637 public static function &factoryClass(string $dependName ) {
638 static $_cache;
639
640 if ( ! is_array( $_cache ) ) {
641 $_cache = [];
642 }
643
644 if ( ! isset( $_cache[ $dependName ] ) ) {
645 // or switch?
646 $class_name = 'XCube_' . ucfirst( $dependName ) . 'Validator';
647 if ( XC_CLASS_EXISTS( $class_name ) ) {
648 $_cache[ $dependName ] = new $class_name();
649 } else {
650 // FIXME:: use delegate?
651 die( "This is an error message of Alpha or Beta series. {$dependName} Validator is not found." );
652 }
653 }
654
655 return $_cache[ $dependName ];
656 }
657}
validate()
Validates fetched values.
$mUser
[READ ONLY] XCube_Principal
getToken()
Gets the token value of this actionform's token.
update(&$obj)
[Abstract] Updates an object with properties values.
& getFormProperties()
Gets form properties of this member property.
$mFieldProperties
XCube_FieldProperty[].
getTokenName()
Gets the token name of this actionform's token.
addErrorMessage( $message)
Adds a message to the form's error message buffer.
hasError()
Gets a value indicating whether this action form keeps error messages or error flag.
_validateToken()
Validates the token.
$mFormProperties
XCube_FormProperty[].
prepare()
[Abstract] Set up form properties and field properties.
getVar( $key, $index=null)
getTokenErrorMessage()
Gets a message about the failure of the token validation.
getErrorMessages()
Gets error messages.
fetch()
Fetches values through the request object.
$mContext
[READ ONLY] XCube_HttpContext
load(&$obj)
[Abstract] Initializes properties' values from an object.
static & factoryClass(string $dependName)
[Abstract] Used for validating member property values of XCube_ActionForm.
$mVariables
Hash-Map Array - std::map<string, mixed>
setDependsByArray( $dependsArr)
Initializes the list of validators for this field property with the list of dependency rule names.
validate(&$form)
Validates the form property with the validators that the field's property contains.
addVar( $name, $value)
Adds a virtual variable used by validators.
$mForm
XCube_ActionForm - Parent form contains this field property.
addMessage( $name, $message)
Adds an error message that will be used if the validation of the "$name" rule fails.
renderMessage(string $name)
Gets the error message rendered by XCube_Utils::formaString().
__construct(&$form)
Constructor.
$mDepends
XCube_Validator[] - std::map<string, XCube_Validator*>
static formatString()
[Static] Formats string with special care for international.