<?php
defined( 'ABSPATH' ) or exit;   // show a blank page if try to access this file directly

require_once( EWZ_PLUGIN_DIR . "classes/ewz-layout.php");
require_once( EWZ_PLUGIN_DIR . "classes/validation/ewz-field-input.php");
require_once( EWZ_PLUGIN_DIR . "classes/validation/ewz-input.php");
require_once( EWZ_CUSTOM_DIR . "ewz-custom-data.php" );
/* Validation for the Data Management area of the Webforms page ( excluding CSV upload ) */
/* Processed in admin.php */

class Ewz_Webform_Data_Input extends Ewz_Input
{

    /**
     * @param mixed $form_data
     *
     * @return void
     */
    function __construct( $form_data ) {
        parent::__construct( $form_data );

        /* rules('type') specifies the function called to validate, defined either here or in ewz-input.php */
        /* rules('val') defines extra args to pass */
        $this->rules = array(
                             'ewzmode'          => array( 'type' => 'limited',   'req' => true,  'val' => [ 'list', 'spread', 'stored', 'download', 'images','zdownload', 'zimages' ] ),
                             'ewznonce'         => array( 'type' => 'anonce',    'req' => true,  'val' => '' ),
                             '_wp_http_referer' => array( 'type' => 'to_string', 'req' => true,  'val' => '' ),
                             'page'             => array( 'type' => 'fixed',     'req' => true,  'val' => 'entrywizlist' ),
                             'webform_id'       => array( 'type' => 'to_seq',    'req' => true,  'val' => ['len' => 10] ),
                             'fopt'             => array( 'type' => 'v_fopts',   'req' => false, 'val' => '' ),
                             'copt'             => array( 'type' => 'v_copts',   'req' => false, 'val' => '' ),
                             'uploaddays'       => array( 'type' => 'to_seq',    'req' => false, 'val' => ['len' => 4] ),
                             'action'           => array( 'type' => 'limited',   'req' => false, 'val' => array('ewz_gen_zipfile') ),
                             'archive_id'       => array( 'type' => 'to_string', 'req' => false, 'val' => ['len' => 16] ),
                             );
        $this->validate();
    }

    /**
     * @return boolean  true on success
     */
   function validate(){

        parent::validate();
        if ( !array_key_exists( 'fopt', $this->input_data ) ) {
            $this->input_data['fopt'] = array();
        }
        if ( !array_key_exists( 'copt', $this->input_data ) ) {
            $this->input_data['copt'] = array();
        }
        return true;
   }

    /**
     * Selected values for fields.
     * @param mixed $value
     * @param string $arg
     *
     * @return boolean
     */
   static function v_fopts( $value, $arg ){
         if( !is_array( $value ) ){
             throw new EWZ_Exception( 'Invalid fopt value' );
         }
         if( count( $value ) > 50 ){
             throw new EWZ_Exception( 'Invalid fopt value' );
         }
         foreach( $value as $key => $val ){
             if( !preg_match( self::REGEX_SEQ, $key )  ){
                 throw new EWZ_Exception( "Invalid key for field option: '$key' " );
             }
             if ( !( is_string( $val ) &&
                     preg_match( '/^[_a-zA-Z0-9\-\~\+\*\-]*$/', $val ) ) ){
                 throw new EWZ_Exception( "Invalid value for field option: '$val' ");
             }
         }
         return true;
     }

    /**
     * Selected values for custom variables.
     * @param mixed $value
     * @param string $arg
     *
     * @return boolean
     */
     static function v_copts( $value, $arg ){
         if( !is_array( $value ) ){
             throw new EWZ_Exception( 'Invalid custom field option value' );
         }
         foreach( $value as $key => $val ){
             if( !preg_match( '/^custom[0-9]$/', $key )  ){
                 throw new EWZ_Exception( "Invalid key for custom option: '$key' " );
             }
             if( method_exists( 'Ewz_Custom_Data', 'selection_list' ) ){
                 $allowed = Ewz_Custom_Data::selection_list( $key );
                 if( !$allowed ){
                     throw new EWZ_Exception( "Selection not implemented for custom option $key " );
                 }
                 if( !in_array( $val, $allowed ) && ( $val != '~*~' ) ){
                     throw new EWZ_Exception( "Invalid value for custom option $key: '$val' " );
                 }
             } else {
                 throw new EWZ_Exception( "Function selection_list not implemented by Ewz_Custom_Data ( see documentation )" );
             }
         }
         return true;
     }
}
