コード例 #1
0
 public static function validateForm($formId, $validationType = 'form', $SubmissionId = 0)
 {
     $mainframe = JFactory::getApplication();
     $db = JFactory::getDbo();
     $invalid = array();
     $formId = (int) $formId;
     $post = JRequest::get('post', JREQUEST_ALLOWRAW);
     $query = $db->getQuery(true);
     $query->select($db->qn('c.ComponentId'))->select($db->qn('c.ComponentTypeId'))->from($db->qn('#__rsform_components', 'c'))->where($db->qn('FormId') . '=' . $db->q($formId))->where($db->qn('Published') . '=' . $db->q(1))->order($db->qn('Order') . ' ' . $db->escape('asc'));
     // if $type is directory, we need to validate the fields that are editable in the directory
     if ($validationType == 'directory') {
         $subquery = $db->getQuery(true);
         $subquery->select($db->qn('componentId'))->from($db->qn('#__rsform_directory_fields'))->where($db->qn('formId') . '=' . $db->q($formId))->where($db->qn('editable') . '=' . $db->q(1));
         $query->where($db->qn('ComponentId') . ' IN (' . (string) $subquery . ')');
     }
     $db->setQuery($query);
     if ($components = $db->loadObjectList('ComponentId')) {
         $componentIds = array_keys($components);
         // load properties
         $all_data = RSFormProHelper::getComponentProperties($componentIds);
         if (empty($all_data)) {
             return $invalid;
         }
         // load conditions
         if ($conditions = RSFormProHelper::getConditions($formId)) {
             foreach ($conditions as $condition) {
                 if ($condition->details) {
                     $condition_vars = array();
                     foreach ($condition->details as $detail) {
                         $isChecked = RSFormProHelper::verifyChecked($detail->ComponentName, $detail->value, $post);
                         $condition_vars[] = $detail->operator == 'is' ? $isChecked : !$isChecked;
                     }
                     // this check is performed like this
                     // 'all' must be true (ie. no 0s in the array); 'any' can be true (ie. one value of 1 in the array will do)
                     $result = $condition->condition == 'all' ? !in_array(0, $condition_vars) : in_array(1, $condition_vars);
                     // if the item is hidden, no need to validate it
                     if ($condition->action == 'show' && !$result || $condition->action == 'hide' && $result) {
                         foreach ($components as $i => $component) {
                             if ($component->ComponentId == $condition->component_id) {
                                 // ... just remove it from the components array
                                 unset($components[$i]);
                                 break;
                             }
                         }
                     }
                 }
             }
         }
         // load validation rules
         require_once JPATH_SITE . '/components/com_rsform/helpers/validation.php';
         require_once JPATH_SITE . '/components/com_rsform/helpers/datevalidation.php';
         $validations = array_flip(get_class_methods('RSFormProValidations'));
         $dateValidations = array_flip(get_class_methods('RSFormProDateValidations'));
         // validate through components
         foreach ($components as $component) {
             $data = $all_data[$component->ComponentId];
             $required = !empty($data['REQUIRED']) && $data['REQUIRED'] == 'YES';
             $validationRule = !empty($data['VALIDATIONRULE']) ? $data['VALIDATIONRULE'] : '';
             $typeId = $component->ComponentTypeId;
             // birthDay field
             if ($typeId == 211) {
                 // flag to check if we need to run the validation functions
                 $runValidations = false;
                 if ($required) {
                     // we need all of the fields to be selected
                     if ($data['SHOWDAY'] == 'YES' && empty($post['form'][$data['NAME']]['d']) || $data['SHOWMONTH'] == 'YES' && empty($post['form'][$data['NAME']]['m']) || $data['SHOWYEAR'] == 'YES' && empty($post['form'][$data['NAME']]['y'])) {
                         $invalid[] = $data['componentId'];
                         continue;
                     }
                     $runValidations = true;
                 } else {
                     // the field is not required, but if a selection is made it needs to be valid
                     $selections = array();
                     if ($data['SHOWDAY'] == 'YES') {
                         $selections[] = !empty($post['form'][$data['NAME']]['d']) ? $post['form'][$data['NAME']]['d'] : '';
                     }
                     if ($data['SHOWMONTH'] == 'YES') {
                         $selections[] = !empty($post['form'][$data['NAME']]['m']) ? $post['form'][$data['NAME']]['m'] : '';
                     }
                     if ($data['SHOWYEAR'] == 'YES') {
                         $selections[] = !empty($post['form'][$data['NAME']]['y']) ? $post['form'][$data['NAME']]['y'] : '';
                     }
                     $foundEmpty = false;
                     $foundValue = false;
                     foreach ($selections as $selection) {
                         if ($selection == '') {
                             $foundEmpty = true;
                         } else {
                             $foundValue = true;
                         }
                     }
                     // at least 1 value has been selected but we've found empty values as well, make sure the selection is valid first!
                     if ($foundEmpty && $foundValue) {
                         $invalid[] = $data['componentId'];
                         continue;
                     } elseif ($foundValue && !$foundEmpty) {
                         $runValidations = true;
                     }
                 }
                 // we have all the info we need, validations only work when all fields are selected
                 if ($runValidations && $data['SHOWDAY'] == 'YES' && $data['SHOWMONTH'] == 'YES' && $data['SHOWYEAR'] == 'YES') {
                     $validationRule = !empty($data['VALIDATIONRULE_DATE']) ? $data['VALIDATIONRULE_DATE'] : '';
                     $day = $post['form'][$data['NAME']]['d'];
                     $month = $post['form'][$data['NAME']]['m'];
                     $year = $post['form'][$data['NAME']]['y'];
                     // start checking validation rules
                     if (isset($dateValidations[$validationRule]) && !call_user_func(array('RSFormProDateValidations', $validationRule), $day, $month, $year, $data)) {
                         $invalid[] = $data['componentId'];
                         continue;
                     }
                 }
                 // no need to process further
                 continue;
             }
             // CAPTCHA
             if ($typeId == 8) {
                 $session = JFactory::getSession();
                 $captchaCode = $session->get('com_rsform.captcha.' . $component->ComponentId);
                 if ($data['IMAGETYPE'] == 'INVISIBLE') {
                     $words = RSFormProHelper::getInvisibleCaptchaWords();
                     if (!empty($post[$captchaCode])) {
                         $invalid[] = $data['componentId'];
                     }
                     foreach ($words as $word) {
                         if (!empty($post[$word])) {
                             $invalid[] = $data['componentId'];
                         }
                     }
                 } else {
                     if (empty($post['form'][$data['NAME']]) || empty($captchaCode) || $post['form'][$data['NAME']] != $captchaCode) {
                         $invalid[] = $data['componentId'];
                     }
                 }
                 // no sense continuing
                 continue;
             }
             // Upload field
             if ($typeId == 9) {
                 $originalUpload = false;
                 if ($validationType == 'directory' && $SubmissionId) {
                     $db->setQuery("SELECT FieldValue FROM #__rsform_submission_values WHERE FieldName='" . $db->escape($data['NAME']) . "' AND SubmissionId='" . (int) $SubmissionId . "' LIMIT 1");
                     $originalUpload = $db->loadResult();
                 }
                 $files = JRequest::getVar('form', null, 'files');
                 // File has been *sent* to the server
                 if (isset($files['tmp_name'][$data['NAME']]) && $files['error'][$data['NAME']] != 4) {
                     // File has been uploaded correctly to the server
                     if ($files['error'][$data['NAME']] == 0) {
                         // Let's check if the extension is allowed
                         $ext = strtolower(end(explode('.', $files['name'][$data['NAME']])));
                         $acceptedExts = !empty($data['ACCEPTEDFILES']) ? self::explode($data['ACCEPTEDFILES']) : false;
                         // Let's check only if accepted extensions are set
                         if ($acceptedExts) {
                             $accepted = false;
                             foreach ($acceptedExts as $acceptedExt) {
                                 $acceptedExt = trim(strtolower($acceptedExt));
                                 if (strlen($acceptedExt) && $acceptedExt == $ext) {
                                     $accepted = true;
                                     break;
                                 }
                             }
                             if (!$accepted) {
                                 $invalid[] = $data['componentId'];
                             }
                         }
                         // Let's check if it's the correct size
                         if ($files['size'][$data['NAME']] > 0 && $data['FILESIZE'] > 0 && $files['size'][$data['NAME']] > $data['FILESIZE'] * 1024) {
                             $invalid[] = $data['componentId'];
                         }
                     } else {
                         $invalid[] = $data['componentId'];
                     }
                 } elseif ($required && !$originalUpload) {
                     $invalid[] = $data['componentId'];
                 }
                 // files have been handled, no need to continue
                 continue;
             }
             // flag to check if we need to run the validation functions
             $runValidations = false;
             if ($required) {
                 // field is required, but is missing
                 if (!isset($post['form'][$data['NAME']])) {
                     $invalid[] = $data['componentId'];
                     continue;
                 }
                 // must have a value if it's required
                 if (is_array($post['form'][$data['NAME']])) {
                     // it's an empty array
                     $valid = implode('', $post['form'][$data['NAME']]);
                     if (empty($valid)) {
                         $invalid[] = $data['componentId'];
                         continue;
                     }
                 } else {
                     // it's a string with no length
                     if (!strlen(trim($post['form'][$data['NAME']]))) {
                         $invalid[] = $data['componentId'];
                         continue;
                     }
                     $runValidations = true;
                 }
             } else {
                 // not required, perform checks only when something is selected
                 // we have a value, make sure it's the correct one
                 if (isset($post['form'][$data['NAME']]) && !is_array($post['form'][$data['NAME']]) && strlen(trim($post['form'][$data['NAME']]))) {
                     $runValidations = true;
                 }
             }
             if ($runValidations && isset($validations[$validationRule]) && !call_user_func(array('RSFormProValidations', $validationRule), $post['form'][$data['NAME']], isset($data['VALIDATIONEXTRA']) ? $data['VALIDATIONEXTRA'] : '', $data)) {
                 $invalid[] = $data['componentId'];
                 continue;
             }
         }
     }
     return $invalid;
 }
コード例 #2
0
ファイル: rsform.php プロジェクト: jtresca/nysurveyor
 function validateForm($formId)
 {
     require_once JPATH_SITE . DS . 'components' . DS . 'com_rsform' . DS . 'helpers' . DS . 'validation.php';
     $mainframe =& JFactory::getApplication();
     $db = JFactory::getDBO();
     $invalid = array();
     $formId = (int) $formId;
     $post = JRequest::get('post', JREQUEST_ALLOWRAW);
     $db->setQuery("SELECT ComponentId, ComponentTypeId FROM #__rsform_components WHERE FormId='" . $formId . "' AND Published=1 ORDER BY `Order`");
     if ($components = $db->loadObjectList()) {
         $componentIds = array();
         foreach ($components as $component) {
             $componentIds[] = $component->ComponentId;
         }
         $all_data = RSFormProHelper::getComponentProperties($componentIds);
         if (empty($all_data)) {
             return $invalid;
         }
         if ($conditions = RSFormProHelper::getConditions($formId)) {
             foreach ($conditions as $condition) {
                 if ($condition->details) {
                     $condition_vars = array();
                     foreach ($condition->details as $detail) {
                         $isChecked = RSFormProHelper::verifyChecked($detail->ComponentName, $detail->value, $post);
                         $condition_vars[] = $detail->operator == 'is' ? $isChecked : !$isChecked;
                     }
                     // this check is performed like this
                     // 'all' must be true (ie. no 0s in the array); 'any' can be true (ie. one value of 1 in the array will do)
                     $result = $condition->condition == 'all' ? !in_array(0, $condition_vars) : in_array(1, $condition_vars);
                     // if the item is hidden, no need to validate it
                     if ($condition->action == 'show' && !$result || $condition->action == 'hide' && $result) {
                         foreach ($components as $i => $component) {
                             if ($component->ComponentId == $condition->component_id) {
                                 // ... just remove it from the components array
                                 unset($components[$i]);
                                 break;
                             }
                         }
                     }
                 }
             }
         }
         foreach ($components as $component) {
             $data = $all_data[$component->ComponentId];
             $required = isset($data['REQUIRED']) ? $data['REQUIRED'] : 'NO';
             $validationRule = isset($data['VALIDATIONRULE']) ? $data['VALIDATIONRULE'] : '';
             $typeId = $component->ComponentTypeId;
             // CAPTCHA
             if ($typeId == 8) {
                 $session =& JFactory::getSession();
                 $captchaCode = $session->get('com_rsform.captcha.' . $component->ComponentId);
                 if ($data['IMAGETYPE'] == 'INVISIBLE') {
                     $words = RSFormProHelper::getInvisibleCaptchaWords();
                     if (!empty($post[$captchaCode])) {
                         $invalid[] = $data['componentId'];
                     }
                     foreach ($words as $word) {
                         if (!empty($post[$word])) {
                             $invalid[] = $data['componentId'];
                         }
                     }
                 } else {
                     if (empty($post['form'][$data['NAME']]) || empty($captchaCode) || $post['form'][$data['NAME']] != $captchaCode) {
                         $invalid[] = $data['componentId'];
                     }
                 }
             }
             // Trigger Event - rsfp_bk_validate_onSubmitValidateRecaptcha
             if ($typeId == 24) {
                 $mainframe->triggerEvent('rsfp_bk_validate_onSubmitValidateRecaptcha', array(array('data' => &$data, 'invalid' => &$invalid)));
             }
             if ($typeId == 9) {
                 $files = JRequest::getVar('form', null, 'files');
                 // File has been *sent* to the server
                 if (isset($files['tmp_name'][$data['NAME']]) && $files['error'][$data['NAME']] != 4) {
                     // File has been uploaded correctly to the server
                     if ($files['error'][$data['NAME']] == 0) {
                         // Let's check if the extension is allowed
                         $ext = strtolower(end(explode('.', $files['name'][$data['NAME']])));
                         $acceptedExts = !empty($data['ACCEPTEDFILES']) ? RSFormProHelper::explode($data['ACCEPTEDFILES']) : false;
                         // Let's check only if accepted extensions are set
                         if ($acceptedExts) {
                             $accepted = false;
                             foreach ($acceptedExts as $acceptedExt) {
                                 $acceptedExt = trim(strtolower($acceptedExt));
                                 if (strlen($acceptedExt) && $acceptedExt == $ext) {
                                     $accepted = true;
                                     break;
                                 }
                             }
                             if (!$accepted) {
                                 $invalid[] = $data['componentId'];
                             }
                         }
                         // Let's check if it's the correct size
                         if ($files['size'][$data['NAME']] > 0 && $data['FILESIZE'] > 0 && $files['size'][$data['NAME']] > $data['FILESIZE'] * 1024) {
                             $invalid[] = $data['componentId'];
                         }
                     } else {
                         $invalid[] = $data['componentId'];
                     }
                 } elseif ($required == 'YES') {
                     $invalid[] = $data['componentId'];
                 }
                 continue;
             }
             if ($required == 'YES') {
                 if (!isset($post['form'][$data['NAME']])) {
                     $invalid[] = $data['componentId'];
                     continue;
                 }
                 if (!is_array($post['form'][$data['NAME']]) && strlen(trim($post['form'][$data['NAME']])) == 0) {
                     $invalid[] = $data['componentId'];
                     continue;
                 }
                 if (!is_array($post['form'][$data['NAME']]) && strlen(trim($post['form'][$data['NAME']])) > 0 && is_callable(array('RSFormProValidations', $validationRule)) && call_user_func(array('RSFormProValidations', $validationRule), $post['form'][$data['NAME']], isset($data['VALIDATIONEXTRA']) ? $data['VALIDATIONEXTRA'] : '', $data) == false) {
                     $invalid[] = $data['componentId'];
                     continue;
                 }
                 if (is_array($post['form'][$data['NAME']])) {
                     $valid = implode('', $post['form'][$data['NAME']]);
                     if (empty($valid)) {
                         $invalid[] = $data['componentId'];
                         continue;
                     }
                 }
             } else {
                 if (isset($post['form'][$data['NAME']]) && !is_array($post['form'][$data['NAME']]) && strlen(trim($post['form'][$data['NAME']])) > 0 && is_callable(array('RSFormProValidations', $validationRule)) && call_user_func(array('RSFormProValidations', $validationRule), $post['form'][$data['NAME']], isset($data['VALIDATIONEXTRA']) ? $data['VALIDATIONEXTRA'] : '', $data) == false) {
                     $invalid[] = $data['componentId'];
                     continue;
                 }
             }
         }
     }
     return $invalid;
 }