<commit_before><?php
/**
 *                    Jojo CMS
 *                ================
 *
 * Copyright 2007-2008 <NAME> <<EMAIL>>
 * Copyright 2007-2008 <NAME> <<EMAIL>>
 * Copyright 2007 <NAME> <<EMAIL>>
 *
 * See the enclosed file license.txt for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  <NAME> <<EMAIL>>
 * @author  <NAME> <<EMAIL>>
 * @author  <NAME> <<EMAIL>>
 * @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License
 * @link    http://www.jojocms.org JojoCMS
 * @package jojo_contact
 */

class Jojo_Plugin_Jojo_contact extends Jojo_Plugin
{

    function sendEnquiry($formID=false)
    {
        global $smarty;
        /* Check for form injection attempts */
        Jojo::noFormInjection();

        $fields = array();
        $errors = array();
        $from_email = '';
        $from_name = '';
        $sender_email =  Jojo::either(_CONTACTADDRESS, _FROMADDRESS, _WEBMASTERADDRESS);
        $to_email = '';
        $formSubject = '';
        $formID;

        /* Get form from current page id or formid*/
        if ($formID) {
            $formfields = Jojo::selectQuery("SELECT * FROM {form} f LEFT JOIN {formfield} ff ON ( ff.ff_form_id = f.form_id) WHERE f.form_id = ? ORDER BY ff_order", array($formID));
        } else {
            $pageID = $this->page['pageid'];
            $formfields = Jojo::selectQuery("SELECT * FROM {form} f LEFT JOIN {formfield} ff ON ( ff.ff_form_id = f.form_id) WHERE f.form_page_id = ? ORDER BY ff_order", array($pageID));
        }

        /* Find the form that belongs to the current page id and get all the formfields that belong to that form */
        $form = $formfields[0];
        $formID = $form['form_id'];
        $formSend = isset($form['form_send']) ? $form['form_send'] : 1;
        $formName = $form['form_name'];
        $formSubject = $form['form_subject'];
        $formAnalytics = $form['form_tracking_code_analytics'];
        $formTrackingcode = $form['form_tracking_code'];
        $formSuccessMessage = !empty($form['form_success_message']) ? $form['form_success_message'] : (Jojo::getOption('contact_success_message', '') ? Jojo::getOption('contact_success_message') : 'Your message was sent successfully.');

        $smarty->assign('formTrackingcode', $formTrackingcode);

        $fields = Jojo::applyFilter("formfields_first", array(), $formID);
        $f = count($fields);
        foreach ($formfields as $ff) {
            foreach ($ff as $k=>$v) {
                $key = str_replace('ff_', '', $k);
                $fields[$f][$key] = $v;
            }
            $fields[$f]['fieldsetid']       = isset($ff['ff_fieldset']) && $ff['ff_fieldset'] ? Jojo::cleanURL($ff['ff_fieldset']) : '';
            $fields[$f]['field']       = isset($ff['ff_fieldname']) && $ff['ff_fieldname'] ? Jojo::cleanURL($ff['ff_fieldname']) : Jojo::cleanURL($ff['ff_display']);
            $fields[$f]['options']     = explode("\r\n", $ff['ff_options']);
            $f++;
        }
        $fields = Jojo::applyFilter("formfields_last", $fields, $formID);

        if ($form['form_captcha']){
            $captchacode = Jojo::getFormData('CAPTCHA','');
            if (!PhpCaptcha::Validate($captchacode)) {
                $errors[] = 'Incorrect Spam Prevention Code entered';
            }
        }


        if ($form['form_choice'] && $form['form_choice_list']) {
            $sendto       =  $_POST['form_sendto'];
            $formchoices = explode("\r\n", $form['form_choice_list']);
            foreach ($formchoices as $to) {
                $to = explode(',', $to);
                $to_email =  trim(array_pop($to));
                $to_name  =  str_replace('#', '', trim(implode(',', $to)));
                if (Jojo::cleanURL($to_name)==$sendto) {
                    break;
                }
            }

        } else {
            $to_email       =  empty($form['form_to']) ? Jojo::either(_CONTACTADDRESS, _FROMADDRESS, _WEBMASTERADDRESS) : $form['form_to'];
            $to_name       =  Jojo::either(_FROMNAME, _WEBMASTERNAME);
        }

        foreach ($fields as &$field) {
            /* set field value from POST */
            if (isset($_POST['form_' . $field['field']]) && is_array($_POST['form_' . $field['field']])) {
                /* convert array to string */
                $field['value'] = implode("\r\n", $_POST['form_' . $field['field']]);
                /* create an assoc array for resetting the value of checkboxes when server-side checking fails */
                $field['valuearr'] = array();
                foreach($_POST['form_' . $field['field']] as $f) {
                    $field['valuearr'][$f] = $f;
                }
            } elseif (isset($_POST[$field['field']]) && is_array($_POST[$field['field']])) {
                /* convert array to string */
                $field['value'] = implode("\r\n", $_POST[$field['field']]);
                /* create an assoc array for resetting the value of checkboxes when server-side checking fails */
                $field['valuearr'] = array();
                foreach($_POST[$field['field']] as $f) {
                    $field['valuearr'][$f] = $f;
                }
            } elseif (Util::getFormData('form_' . $field['field'], '')) {
                $field['value'] = ($field['type']!='heading') ? Util::getFormData('form_' . $field['field'], '') : $field['value'];
            } else {
                $field['value'] = ($field['type']!='heading') ? Util::getFormData($field['field'], '') : $field['value'];
            }
            /* set the fromemail value if appropriate */

             if($field['is_email']){
                $from_email = $field['value'];
             }
             if($field['is_name']){
                $from_name .= $field['value'];
                $from_name .= ' ';
             }

            /* check value is set on required fields */
            if ($field['required'] && empty($field['value'])) {
                $errors[] = $field['display'] . ' is a required field';
            }
            if (!empty($field['value'])) {
                switch ($field['validation']) {
                    case 'email':
                        if (!Jojo::checkEmailFormat($field['value'])) {$errors[] = $field['display'] . ' is not a valid email format';}
                        break;
                    case 'url':
                        $field['value'] = addHttp($field['value']);
                        if (!Jojo::checkUrlFormat($field['value'])) {$errors[] = $field['display'] . ' is not a valid URL format';}
                        break;
                    case 'text':
                        //do we need to check anything?
                        break;
                    case 'integer':
                        if (!is_numeric($field['value'])) {$errors[] = $field['display'] . ' is not an integer value';}
                        break;
                    case 'numeric':
                        if (!is_numeric($field['value'])) {$errors[] = $field['display'] . ' is not an integer value';}
                        break;
                }
            }

            /* if field is confirmation field then need to check both fields match */
            if($field['type'] == 'emailwithconfirmation') {
                $confirmation = $_POST['form_' . $field['field'] . '_confirmation'];
                if($field['value'] != $confirmation) {
                    $errors[] = $field['display'] . ' and confirmation email fields must match';
                }
            }
            /* if field is file upload field then need to check for a file and transfer it to the folder */
            if ($field['type'] == 'upload' || $field['type'] == 'privateupload') {
                $field['filelink'] = '';
                if (!isset($_FILES["FILE_".$field['field']])) continue;
                $file = $_FILES["FILE_".$field['field']];
                $uploadresponse = self::upload($file, $field, $form);
                if ($uploadresponse['errors']) $errors[] = $uploadresponse['errors'];
                $field['filelink'] = $uploadresponse['filepath'];
            }
       }

        if(!count($errors)){
            /* run further validation hook */
            $validationReturn = Jojo::runHook('contact_form_validation_success', array($errors, $fields));
            $errors = $validationReturn[0];
        }

        unset($field);


        $from_name = empty($from_name) ? Jojo::getOption('sitetitle') : $from_name;
        $from_email = empty($from_email) ? Jojo::either(_CONTACTADDRESS, _FROMADDRESS, _WEBMASTERADDRESS) : $from_email;
        $subject  = $formSubject ? $formSubject : 'Message from the ' . $formName . ' form';
        $subject = mb_convert_encoding($subject, 'HTML-ENTITIES', 'UTF-8');

        $smarty->assign('subject', $subject);

        $message  = '';
        foreach ($fields as $f) {
            if (isset($f['displayonly'])) {
                continue;
            } elseif ($f['type'] == 'note') {
                continue;<commit_msg>fixed the handling of the return of the validation hook<commit_after><?php
/**
 *                    Jojo CMS
 *                ================
 *
 * Copyright 2007-2008 <NAME> <<EMAIL>>
 * Copyright 2007-2008 <NAME> <<EMAIL>>
 * Copyright 2007 <NAME> <<EMAIL>>
 *
 * See the enclosed file license.txt for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  <NAME> <<EMAIL>>
 * @author  <NAME> <<EMAIL>>
 * @author  <NAME>z <<EMAIL>>
 * @license http://www.fsf.org/copyleft/lgpl.html GNU Lesser General Public License
 * @link    http://www.jojocms.org JojoCMS
 * @package jojo_contact
 */

class Jojo_Plugin_Jojo_contact extends Jojo_Plugin
{

    function sendEnquiry($formID=false)
    {
        global $smarty;
        /* Check for form injection attempts */
        Jojo::noFormInjection();

        $fields = array();
        $errors = array();
        $from_email = '';
        $from_name = '';
        $sender_email =  Jojo::either(_CONTACTADDRESS, _FROMADDRESS, _WEBMASTERADDRESS);
        $to_email = '';
        $formSubject = '';
        $formID;

        /* Get form from current page id or formid*/
        if ($formID) {
            $formfields = Jojo::selectQuery("SELECT * FROM {form} f LEFT JOIN {formfield} ff ON ( ff.ff_form_id = f.form_id) WHERE f.form_id = ? ORDER BY ff_order", array($formID));
        } else {
            $pageID = $this->page['pageid'];
            $formfields = Jojo::selectQuery("SELECT * FROM {form} f LEFT JOIN {formfield} ff ON ( ff.ff_form_id = f.form_id) WHERE f.form_page_id = ? ORDER BY ff_order", array($pageID));
        }

        /* Find the form that belongs to the current page id and get all the formfields that belong to that form */
        $form = $formfields[0];
        $formID = $form['form_id'];
        $formSend = isset($form['form_send']) ? $form['form_send'] : 1;
        $formName = $form['form_name'];
        $formSubject = $form['form_subject'];
        $formAnalytics = $form['form_tracking_code_analytics'];
        $formTrackingcode = $form['form_tracking_code'];
        $formSuccessMessage = !empty($form['form_success_message']) ? $form['form_success_message'] : (Jojo::getOption('contact_success_message', '') ? Jojo::getOption('contact_success_message') : 'Your message was sent successfully.');

        $smarty->assign('formTrackingcode', $formTrackingcode);

        $fields = Jojo::applyFilter("formfields_first", array(), $formID);
        $f = count($fields);
        foreach ($formfields as $ff) {
            foreach ($ff as $k=>$v) {
                $key = str_replace('ff_', '', $k);
                $fields[$f][$key] = $v;
            }
            $fields[$f]['fieldsetid']       = isset($ff['ff_fieldset']) && $ff['ff_fieldset'] ? Jojo::cleanURL($ff['ff_fieldset']) : '';
            $fields[$f]['field']       = isset($ff['ff_fieldname']) && $ff['ff_fieldname'] ? Jojo::cleanURL($ff['ff_fieldname']) : Jojo::cleanURL($ff['ff_display']);
            $fields[$f]['options']     = explode("\r\n", $ff['ff_options']);
            $f++;
        }
        $fields = Jojo::applyFilter("formfields_last", $fields, $formID);

        if ($form['form_captcha']){
            $captchacode = Jojo::getFormData('CAPTCHA','');
            if (!PhpCaptcha::Validate($captchacode)) {
                $errors[] = 'Incorrect Spam Prevention Code entered';
            }
        }


        if ($form['form_choice'] && $form['form_choice_list']) {
            $sendto       =  $_POST['form_sendto'];
            $formchoices = explode("\r\n", $form['form_choice_list']);
            foreach ($formchoices as $to) {
                $to = explode(',', $to);
                $to_email =  trim(array_pop($to));
                $to_name  =  str_replace('#', '', trim(implode(',', $to)));
                if (Jojo::cleanURL($to_name)==$sendto) {
                    break;
                }
            }

        } else {
            $to_email       =  empty($form['form_to']) ? Jojo::either(_CONTACTADDRESS, _FROMADDRESS, _WEBMASTERADDRESS) : $form['form_to'];
            $to_name       =  Jojo::either(_FROMNAME, _WEBMASTERNAME);
        }

        foreach ($fields as &$field) {
            /* set field value from POST */
            if (isset($_POST['form_' . $field['field']]) && is_array($_POST['form_' . $field['field']])) {
                /* convert array to string */
                $field['value'] = implode("\r\n", $_POST['form_' . $field['field']]);
                /* create an assoc array for resetting the value of checkboxes when server-side checking fails */
                $field['valuearr'] = array();
                foreach($_POST['form_' . $field['field']] as $f) {
                    $field['valuearr'][$f] = $f;
                }
            } elseif (isset($_POST[$field['field']]) && is_array($_POST[$field['field']])) {
                /* convert array to string */
                $field['value'] = implode("\r\n", $_POST[$field['field']]);
                /* create an assoc array for resetting the value of checkboxes when server-side checking fails */
                $field['valuearr'] = array();
                foreach($_POST[$field['field']] as $f) {
                    $field['valuearr'][$f] = $f;
                }
            } elseif (Util::getFormData('form_' . $field['field'], '')) {
                $field['value'] = ($field['type']!='heading') ? Util::getFormData('form_' . $field['field'], '') : $field['value'];
            } else {
                $field['value'] = ($field['type']!='heading') ? Util::getFormData($field['field'], '') : $field['value'];
            }
            /* set the fromemail value if appropriate */

             if($field['is_email']){
                $from_email = $field['value'];
             }
             if($field['is_name']){
                $from_name .= $field['value'];
                $from_name .= ' ';
             }

            /* check value is set on required fields */
            if ($field['required'] && empty($field['value'])) {
                $errors[] = $field['display'] . ' is a required field';
            }
            if (!empty($field['value'])) {
                switch ($field['validation']) {
                    case 'email':
                        if (!Jojo::checkEmailFormat($field['value'])) {$errors[] = $field['display'] . ' is not a valid email format';}
                        break;
                    case 'url':
                        $field['value'] = addHttp($field['value']);
                        if (!Jojo::checkUrlFormat($field['value'])) {$errors[] = $field['display'] . ' is not a valid URL format';}
                        break;
                    case 'text':
                        //do we need to check anything?
                        break;
                    case 'integer':
                        if (!is_numeric($field['value'])) {$errors[] = $field['display'] . ' is not an integer value';}
                        break;
                    case 'numeric':
                        if (!is_numeric($field['value'])) {$errors[] = $field['display'] . ' is not an integer value';}
                        break;
                }
            }

            /* if field is confirmation field then need to check both fields match */
            if($field['type'] == 'emailwithconfirmation') {
                $confirmation = $_POST['form_' . $field['field'] . '_confirmation'];
                if($field['value'] != $confirmation) {
                    $errors[] = $field['display'] . ' and confirmation email fields must match';
                }
            }
            /* if field is file upload field then need to check for a file and transfer it to the folder */
            if ($field['type'] == 'upload' || $field['type'] == 'privateupload') {
                $field['filelink'] = '';
                if (!isset($_FILES["FILE_".$field['field']])) continue;
                $file = $_FILES["FILE_".$field['field']];
                $uploadresponse = self::upload($file, $field, $form);
                if ($uploadresponse['errors']) $errors[] = $uploadresponse['errors'];
                $field['filelink'] = $uploadresponse['filepath'];
            }
       }

        if(!count($errors)){
            /* run further validation hook */
            $validationReturn = Jojo::runHook('contact_form_validation_success', array($errors, $fields));
            $errors = $validationReturn;
        }

        unset($field);


        $from_name = empty($from_name) ? Jojo::getOption('sitetitle') : $from_name;
        $from_email = empty($from_email) ? Jojo::either(_CONTACTADDRESS, _FROMADDRESS, _WEBMASTERADDRESS) : $from_email;
        $subject  = $formSubject ? $formSubject : 'Message from the ' . $formName . ' form';
        $subject = mb_convert_encoding($subject, 'HTML-ENTITIES', 'UTF-8');

        $smarty->assign('subject', $subject);

        $message  = '';
        foreach ($fields as $f) {
            if (isset($f['displayonly'])) {
                continue;
            } elseif ($f['type'] == 'note') {
                continue;
{'fraction_non_alphanumeric': 0.17046450482033304, 'fraction_numerical': 0.002574496056091148, 'mean_word_length': 2.022682119205298, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2748921', 'n_tokens_mistral': 5307, 'n_tokens_neox': 5057, 'n_words': 1124}