Plugin Directory

Changeset 3487266


Ignore:
Timestamp:
03/20/2026 02:49:26 PM (2 weeks ago)
Author:
devsoftbaltic
Message:

fixed https://github.com/surveyjs/surveyjs-wordpress/issues/89

Location:
surveyjs/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • surveyjs/trunk/ajax_handlers/save_result.php

    r3446910 r3487266  
    1313        if($_SERVER['REQUEST_METHOD'] === 'POST') {
    1414            if(!check_ajax_referer( 'surveyjs-save-result' )) exit;
    15             $SurveyId = intval(sanitize_key($_POST['SurveyId']));
    16             $Json =  sanitize_text_field($_POST['Json']);
     15            $SurveyId = absint( wp_unslash( $_POST['SurveyId'] ?? 0 ) );
     16            $raw_json = wp_unslash( $_POST['Json'] ?? '' );
     17            $Json = $this->sanitize_result_json( $raw_json );
     18            if ( null === $Json ) {
     19                wp_send_json_error(
     20                    array(
     21                        'message' => 'Invalid survey result payload',
     22                    ),
     23                    400
     24                );
     25                return;
     26            }
    1727            $TableName = 'sjs_results';
    1828           
     
    3444        }
    3545    }
     46
     47    private function sanitize_result_json( $raw_json ) {
     48        if ( ! is_string( $raw_json ) || '' === trim( $raw_json ) ) {
     49            return null;
     50        }
     51
     52        $decoded = json_decode( $raw_json, true );
     53        if ( JSON_ERROR_NONE !== json_last_error() || ! is_array( $decoded ) ) {
     54            return null;
     55        }
     56
     57        $sanitized = $this->sanitize_json_value( $decoded );
     58        return wp_json_encode( $sanitized );
     59    }
     60
     61    private function sanitize_json_value( $value ) {
     62        if ( is_array( $value ) ) {
     63            $sanitized = array();
     64            foreach ( $value as $key => $item ) {
     65                $sanitized[ $key ] = $this->sanitize_json_value( $item );
     66            }
     67            return $sanitized;
     68        }
     69
     70        if ( is_string( $value ) ) {
     71            return sanitize_text_field( wp_kses( $value, array() ) );
     72        }
     73
     74        return $value;
     75    }
    3676}
    3777
  • surveyjs/trunk/ajax_handlers/save_survey.php

    r3446910 r3487266  
    1616            $table_name = $wpdb->prefix . 'sjs_my_surveys';
    1717
    18             $id = sanitize_key($_POST['Id']);
    19             $json = current_user_can( 'unfiltered_html' ) ? $_POST['Json'] : wp_kses_post( $_POST['Json'] );
    20             $theme = current_user_can( 'unfiltered_html' ) ? $_POST['Theme'] : wp_kses_post( $_POST['Theme'] );
     18            $id = absint( wp_unslash( $_POST['Id'] ?? 0 ) );
     19            $raw_json = wp_unslash( $_POST['Json'] ?? '' );
     20            $raw_theme = wp_unslash( $_POST['Theme'] ?? '' );
     21            $json = current_user_can( 'unfiltered_html' ) ? $raw_json : wp_kses_post( $raw_json );
     22            $theme = current_user_can( 'unfiltered_html' ) ? $raw_theme : wp_kses_post( $raw_theme );
    2123
    2224            // create 'theme' column if not exists
     
    3537                    ),
    3638                    array(
    37                         'id' => intval($id)
     39                        'id' => $id
    3840                    )
    3941                );
  • surveyjs/trunk/readme.txt

    r3446910 r3487266  
    44Requires at least: 6.4
    55Tested up to: 6.9
    6 Stable tag: 2.5.3
     6Stable tag: 2.5.4
    77Requires PHP: 8.2
    88
     
    8080
    8181
    82 = v2.5.3 =
     82= v2.5.4 =
    8383
    8484== Support ==
  • surveyjs/trunk/surveyjs.php

    r3446910 r3487266  
    44Plugin URI: https://wordpress.org/plugins/surveyjs
    55Description: Easy to use, drag & drop Survey Builder with myriad options.
    6 Version: 2.5.3
     6Version: 2.5.4
    77Author: Devsoft Baltic OÜ
    88Author URI: http://devsoftbaltic.com/
  • surveyjs/trunk/views/results.php

    r3446910 r3487266  
    99    public static function render() {
    1010        global $wpdb;
    11         $surveyId = sanitize_key($_GET['id']);
     11        $surveyId = absint( wp_unslash( $_GET['id'] ?? 0 ) );
    1212        $table_name = $wpdb->prefix . 'sjs_results';
    1313        $query = $wpdb->prepare("SELECT id, json FROM " . esc_sql( $table_name ) . " WHERE surveyId=%d", intval($surveyId));
     
    1818        $row = $wpdb->get_row($query);
    1919        $surveyJson = isset($row->json) ? $row->json : '{}';
     20        $decodedSurveyJson = self::decode_survey_json( $surveyJson );
    2021       
    21         $surveyName = sanitize_text_field($_GET['name']);
     22        $surveyName = sanitize_text_field( wp_unslash( $_GET['name'] ?? '' ) );
    2223
    2324        $deleteResultUri = add_query_arg(array('action' => 'SurveyJS_DeleteResult'), admin_url('admin-ajax.php'));
     
    4748            <script>
    4849                var $ = jQuery;
    49                 var surveyJson = '<?php echo htmlspecialchars_decode($surveyJson); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>';
    50                 var survey = new Survey.Model(JSON.parse(surveyJson));
     50                var surveyJson = <?php echo wp_json_encode( $decodedSurveyJson ); ?>;
     51                var survey = new Survey.Model(surveyJson);
    5152
    5253                var columns = survey.getAllQuestions().map(function(q) {
     
    5758                        survey.data = row;
    5859                        var displayValue = q.displayValue;
    59                         return (
    60                             (typeof displayValue === "string"
     60                        var safeValue = (
     61                            typeof displayValue === "string"
    6162                            ? displayValue
    62                             : JSON.stringify(displayValue)) || ""
    63                         );
     63                            : JSON.stringify(displayValue)
     64                        ) || "";
     65                        return $.fn.dataTable.render.text().display(safeValue);
    6466                        }
    6567                    };
     
    108110                ?>
    109111
    110                 function decodeHtml(str) {
    111                     var textarea = document.createElement("textarea");
    112                     textarea.innerHTML = str;
    113                     return textarea.innerText;
    114                 }
    115 
    116112                var data = results.map(function(result) {
    117                     var replacedResult  = decodeHtml(result.json.replace(/\\\"/g, "\"").replace(/\\\\/g, "\\").replace(/\\'/g, "'"));                   
    118                     var dataItem = JSON.parse(replacedResult || "{}");
     113                    var dataItem = {};
     114                    try {
     115                        dataItem = JSON.parse(result.json || "{}");
     116                    } catch (e) {
     117                        dataItem = {};
     118                    }
    119119                    dataItem.resultId = result.id;
    120120                    return dataItem;
     
    151151       
    152152    }
     153
     154    private static function decode_survey_json( $raw_json ) {
     155        if ( ! is_string( $raw_json ) || '' === trim( $raw_json ) ) {
     156            return array();
     157        }
     158
     159        $candidates = array(
     160            $raw_json,
     161            wp_specialchars_decode( $raw_json, ENT_QUOTES ),
     162            stripslashes( $raw_json ),
     163            wp_specialchars_decode( stripslashes( $raw_json ), ENT_QUOTES ),
     164        );
     165
     166        foreach ( $candidates as $candidate ) {
     167            $decoded = json_decode( $candidate, true );
     168            if ( JSON_ERROR_NONE !== json_last_error() ) {
     169                continue;
     170            }
     171
     172            // Some legacy records store survey JSON as a JSON string (double encoded).
     173            if ( is_string( $decoded ) ) {
     174                $decoded_twice = json_decode( $decoded, true );
     175                if ( JSON_ERROR_NONE === json_last_error() && is_array( $decoded_twice ) ) {
     176                    return $decoded_twice;
     177                }
     178                continue;
     179            }
     180
     181            if ( is_array( $decoded ) ) {
     182                return $decoded;
     183            }
     184        }
     185
     186        return array();
     187    }
    153188}
    154189
Note: See TracChangeset for help on using the changeset viewer.