public static function create($formIds, $submissions, $filename) { $db = JFactory::getDbo(); $user = JFactory::getUser(); $app = JFactory::getApplication(); $config = JFactory::getConfig(); $version = new RSFormProVersion(); $xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n"; $xml .= '<RSinstall type="rsformbackup">' . "\n"; $xml .= '<name>RSform backup</name>' . "\n"; $xml .= '<creationDate>' . date('Y-m-d') . '</creationDate>' . "\n"; $xml .= '<author>' . $user->username . '</author>' . "\n"; $xml .= '<copyright>(C) ' . date('Y') . ' ' . JURI::root() . '</copyright>' . "\n"; $xml .= '<authorEmail>' . $config->get('mailfrom') . '</authorEmail>' . "\n"; $xml .= '<authorUrl>' . JURI::root() . '</authorUrl>' . "\n"; $xml .= '<version>' . (string) $version . '</version>' . "\n"; $xml .= '<revision>' . $version->revision . '</revision>' . "\n"; $xml .= '<description>RSForm! Pro Backup</description>' . "\n"; $xml .= '<tasks>' . "\n"; // We need to see all available languages here, because the conditions are attached to languages $lang = JFactory::getLanguage(); $languages = $lang->getKnownLanguages(JPATH_SITE); //LOAD FORMS $db->setQuery("SELECT * FROM #__rsform_forms WHERE FormId IN ('" . implode("','", $formIds) . "') ORDER BY FormId"); $form_rows = $db->loadObjectList(); foreach ($form_rows as $form_row) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_forms', $form_row, 'FormId') . "\n"; $xml .= "\t" . '<task type="eval"><![CDATA[$GLOBALS[\'q_FormId\'] = $db->insertid();]]></task>' . "\n"; $db->setQuery("SELECT * FROM #__rsform_translations WHERE `form_id`='" . $form_row->FormId . "' AND `reference`='forms'"); $translations = $db->loadObjectList(); foreach ($translations as $translation) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_translations', $translation, 'id') . "\n"; } $xml .= "\t" . '<task type="eval"><![CDATA[$GLOBALS[\'ComponentIds\'] = array();]]></task>' . "\n"; //LOAD COMPONENTS $db->setQuery("SELECT * FROM #__rsform_components WHERE FormId = '" . $form_row->FormId . "'"); $component_rows = $db->loadObjectList(); foreach ($component_rows as $component_row) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_components', $component_row, 'ComponentId', 'FormId') . "\n"; $xml .= "\t" . '<task type="eval"><![CDATA[$GLOBALS[\'q_ComponentId\'] = $db->insertid();]]></task>' . "\n"; //LOAD PROPERTIES $db->setQuery("SELECT * FROM #__rsform_properties WHERE ComponentId = '" . $component_row->ComponentId . "'"); $property_rows = $db->loadObjectList(); $ComponentName = ''; foreach ($property_rows as $property_row) { if ($property_row->PropertyName == 'NAME') { $ComponentName = $property_row->PropertyValue; } $xml .= RSFormProBackup::createXMLEntry('#__rsform_properties', $property_row, 'PropertyId', 'ComponentId') . "\n"; } if ($ComponentName) { $xml .= "\t" . '<task type="eval"><![CDATA[$GLOBALS[\'ComponentIds\'][\'' . $ComponentName . '\'] = $GLOBALS[\'q_ComponentId\'];]]></task>' . "\n"; } //LOAD TRANSLATIONS $db->setQuery("SELECT * FROM #__rsform_translations WHERE `form_id`='" . $form_row->FormId . "' AND `reference_id` LIKE '" . $component_row->ComponentId . ".%'"); $translations = $db->loadObjectList(); foreach ($translations as $translation) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_translations', $translation, 'id') . "\n"; } } if ($submissions) { //LOAD SUBMISSIONS $db->setQuery("SELECT * FROM #__rsform_submissions WHERE FormId = '" . $form_row->FormId . "'"); $submission_rows = $db->loadObjectList(); foreach ($submission_rows as $submission_row) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_submissions', $submission_row, 'SubmissionId', 'FormId') . "\n"; $xml .= "\t" . '<task type="eval"><![CDATA[$GLOBALS[\'q_SubmissionId\'] = $db->insertid();]]></task>' . "\n"; //LOAD SUBMISSION_VALUES $db->setQuery("SELECT * FROM #__rsform_submission_values WHERE SubmissionId = '" . $submission_row->SubmissionId . "'"); $submission_value_rows = $db->loadObjectList(); foreach ($submission_value_rows as $submission_value_row) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_submission_values', $submission_value_row, 'SubmissionValueId', array('SubmissionId', 'FormId')) . "\n"; } } } //LOAD CONDITIONS foreach ($languages as $tag => $properties) { $conditions = RSFormProHelper::getConditions($form_row->FormId, $tag); if ($conditions) { foreach ($conditions as $condition) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_conditions', $condition, array('id')) . "\n"; $xml .= "\t" . '<task type="eval"><![CDATA[$GLOBALS[\'q_ConditionId\'] = $db->insertid();]]></task>' . "\n"; if ($condition->details) { foreach ($condition->details as $detail) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_condition_details', $detail, array('id')) . "\n"; } } } } } //LOAD POSTS $db->setQuery("SELECT * FROM #__rsform_posts WHERE `form_id`='" . $form_row->FormId . "'"); if ($post = $db->loadObject()) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_posts', $post, null, null, true) . "\n"; } //LOAD CALCULATIONS if ($calculations = RSFormProHelper::getCalculations($form_row->FormId)) { foreach ($calculations as $calculation) { $xml .= RSFormProBackup::createXMLEntry('#__rsform_calculations', $calculation, null, null, true) . "\n"; } } //Trigger Event - onFormBackup $app->triggerEvent('rsfp_bk_onFormBackup', array(array('formId' => $form_row->FormId, 'xml' => &$xml))); } $xml .= '</tasks>' . "\n"; $xml .= '</RSinstall>'; jimport('joomla.filesystem.file'); return JFile::write($filename, $xml); }
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; }
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; }