~sit-developers/sit/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
// feedback.php - Display a form for customers to provide feedback
//
// SiT (Support Incident Tracker) - Support call tracking system
// Copyright (C) 2010-2014 The Support Incident Tracker Project
// Copyright (C) 2000-2009 Salford Software Ltd. and Contributors
//
// This software may be used and distributed according to the terms
// of the GNU General Public License, incorporated herein by reference.
//

// Author: Ivan Lucas <ivanlucas[at]users.sourceforge.net>, June 2004


require ('core.php');
require (APPLICATION_LIBPATH . 'functions.inc.php');

// External variables
$hashcode = $_REQUEST['ax'];
$oucode = $_REQUEST['ou'];
$mode = clean_fixed_list($_REQUEST['mode'], array('', 'bare'));
$ouemail = cleanvar($_POST['unsubscribe']);

// lets ignore the opt out if both variables have data
if ((!empty($oucode)) AND (!empty($hashcode)))
{
    $oucode = '';
}

if (!empty($hashcode))
{
    $decodehash = str_rot13(@gzuncompress(base64_decode(urldecode($hashcode))));
    $hashvars = explode('&&', $decodehash);
    $formid = clean_int($hashvars['0']);
    $contactid = clean_int($hashvars['1']);
    $incidentid = clean_int(urldecode($hashvars['2']));
    $contactemail = clean_dbstring(urldecode($hashvars['3']));

}
elseif (!empty($oucode))
{
    $decodehash = str_rot13(@gzuncompress(base64_decode(urldecode($oucode))));
    $hashvars = explode('&&', $decodehash);
    $contactid = clean_int($hashvars['0']);
    $contactemail = clean_dbstring(urldecode($hashvars['1']));
}
else
{
    $hashcode = '';
    $oucode = '';
}

if (!empty($oucode))
{

    if (!empty($ouemail))
    {
        if ($ouemail == (contact_email($contactid)))
        {
            $sql = "UPDATE `{$dbContactConfig}` SET value = 'no' ";
            $sql .= "WHERE contactid = '{$contactid}' ";
            $sql .= "AND config = 'feedback_enable' ";
            $result = mysql_query($sql);
            if (mysql_error())
            {
                trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING);
            }
            else
            {
                include (APPLICATION_INCPATH . 'htmlheader.inc.php');
                echo "<h3><div id='pagecontent'><span class=\"success\">{$strThankYou}<span></h3>";
                echo "<h4 align='center'>{$strReceiveFeedbackAgain}</h4><br /><br />";
                include (APPLICATION_INCPATH . 'htmlfooter.inc.php');
            }
        }
        else
        {
            html_redirect("{$_SERVER['PHP_SELF']}?ou={$oucode}", FALSE, $strNoValidEmailEntered);
        }


    }
    else
    {
        include (APPLICATION_INCPATH . 'htmlheader.inc.php');
        echo "<h3><div id='pagecontent'><span class=\"success\">{$strConfirmOptOut}<span></h3><br />";
        echo "<br />";
        echo "<div align='center'><form action='$PHP_SELF?ou={$oucode}' method='post'>\n";
        echo "{$strEnterSubscribedEmail} <br /> ";
        echo "<input type='text' name='unsubscribe' maxlength='20' />";
        echo "<input type='submit' value='{$strGo}' />";
        echo "</form></div><br /><br />";
        include (APPLICATION_INCPATH . 'htmlfooter.inc.php');
    }

}
else
{
    unset($errorfields);

    switch ($_REQUEST['action'])
    {
        case 'save':
            // Have a look to see if this respondant has already responded to this form
            $sql = "SELECT id AS respondentid FROM `{$dbFeedbackRespondents}` ";
            $sql .= "WHERE contactid='$contactid' AND formid='{$formid}' AND incidentid='{$incidentid}' AND completed = 'no'";
            $result = mysql_query($sql);
            if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
            if (mysql_num_rows($result) < 1)
            {
                echo "<p>{$strFeedbackFormAlreadyCompleted}</p>";
            }
            else
            {
                list($respondentid) = mysql_fetch_row($result);
            }
            // Store this respondent and references

            // Loop through the questions in this form and store the results
            $sql = "SELECT * FROM `{$dbFeedbackQuestions}` WHERE formid='{$formid}'";
            $result = mysql_query($sql);
            if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
            while ($question = mysql_fetch_object($result))
            {
                $qid = $question->id;

                $options = nl2br(trim($question->options));
                $options = str_replace('<br>', '{@}', $options);
                $options = str_replace('<br />', '{@}', $options);
                $options = str_replace('<br/>', '{@}', $options);
                $option_list = explode('{@}', $options);

                $fieldname = "Q{$question->id}";

                // Check required fields are filled
                if (is_array($_POST[$fieldname]))
                {
                    // make sure there aren't any commas in the list and convert array to comma separated list
                    $_POST[$fieldname] = str_replace(',', '_', $_POST[$fieldname]);
                    $_POST[$fieldname] = implode(",", $_POST[$fieldname]);
                }
                if ($question->required == 'true' AND (count($_POST[$fieldname]) < 1 OR
                        isset($_POST[$fieldname]) == FALSE))
                        {
                            $errorfields[] = "{$question->id}";
                        }

                // Store text responses in the appropriate field
                if ($question->type == 'text')
                {
                    if (mb_strlen($_POST[$fieldname]) < 255 AND $option_list[1] < 2)
                    {
                        // If we've got just one row and less than 255 characters store it in the result field
                        $qresult = clean_dbstring($_POST[$fieldname]);
                        $qresulttext = '';
                    }
                    else
                    {
                        // If we've got more than one row or more than 255 chars store it in the resulttext field (which is a blob)
                        $qresult = '';
                        $qresulttext = clean_dbstring($_POST[$fieldname]);
                    }
                }
                else
                {
                    // Store all other types of results in the result field.
                    $qresult = clean_dbstring($_POST[$fieldname]);
                    $qresulttext = clean_dbstring($_POST[$fieldname]);
                }

                $debugtext .= "_POST[$fieldname]=" . cleanvar($_POST[$fieldname]) . "\n";

                // Put the SQL to be executed into an array to execute later
                $rsql[] = "INSERT INTO `{$dbFeedbackResults}` (respondentid, questionid, result, resulttext) VALUES ('{$respondentid}', '{$qid}','{$qresult}', '{$qresulttext}')";
                // Store the field in an array
                $fieldarray[$question->id] = cleanvar($_POST[$fieldname]);
            }

            if (count($errorfields) >= 1)
            {
                $error = implode(",",$errorfields);
                $fielddata = base64_encode(serialize($fieldarray));
                $errortext = urlencode($fielddata.','.$error);
                echo "<?";
                echo "xml version=\"1.0\" encoding=\"\"?";
                echo ">";

                $url = "feedback.php?ax={$hashcode}&error={$errortext}&mode={$mode}";
                html_redirect($url, FALSE, $strErrorRequiredQuestionsNotCompleted);
                exit;
            }

            if (empty($_REQUEST['rr']))
            {
                $rsql[] = "UPDATE `{$dbFeedbackRespondents}` SET completed='yes' WHERE formid='{$formid}' AND contactid='{$contactid}' AND incidentid='{$incidentid}'";
            }

            // Loop through array and execute the array to insert the form data
            foreach ($rsql AS $sql)
            {
                mysql_query($sql);
                if (mysql_error()) trigger_error(mysql_error(), E_USER_ERROR);
                $sqltext .= $sql."\n";
            }

            $title = $strThankYou;
            include (APPLICATION_INCPATH . 'htmlheader.inc.php');
            echo "<h3><div id='pagecontent'><span class=\"success\">{$strThankYou}<span></h3>";
            echo "<h4>{$strThankYouCompleteForm}</h4>";
            include (APPLICATION_INCPATH . 'htmlfooter.inc.php');
            break;

        default:
            if ($_REQUEST['mode'] != 'bare')
            {
                include (APPLICATION_INCPATH . 'htmlheader.inc.php');
            }
            else
            {
                echo "<html>\n<head>\n<title>{$strFeedbackForm}</title>\n</head>\n<body>\n<div id='pagecontent'>\n\n";
            }
            $errorfields = explode(",", urldecode($_REQUEST['error']));
            $fielddata = unserialize(base64_decode($errorfields[0]));

            // check if contact is the right person
            $csql = "SELECT id FROM `{$dbContacts}` ";
            $csql .= "WHERE id='{$contactid}' AND email='{$contactemail}'";

            $cresult = mysql_query($csql);
            if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);

            $contactexists = mysql_num_rows($cresult);

            if ($contactexists > 0)
            {
                // Have a look to see if this person has a form waiting to be filled
                $rsql = "SELECT id FROM `{$dbFeedbackRespondents}` ";
                $rsql .= "WHERE contactid='{$contactid}' AND incidentid='{$incidentid}' AND formid='{$formid}' AND completed = 'no'";


                $rresult = mysql_query($rsql);
                if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);

                $waitingforms = mysql_num_rows($rresult);
                $waitingform = mysql_fetch_object($rresult);

                if ($waitingforms < 1)
                {
                    echo "<h3><span class='failure'>{$strError}</span></h3>";
                    echo "<h4>{$strNoFeedBackFormToCompleteHere}</h4>";
                    debug_log("\n\n<!-- FORM NOT WAITING f: {$formid} r:{$respondent} rr:{$responseref} dh:{$decodehash}  hc:{$hashcode} ce:{$contactemail} -->\n\n", TRUE);
                }
                else
                {
                    $sql = "SELECT * FROM `{$dbFeedbackForms}` WHERE id='{$formid}'";
                    $result = mysql_query($sql);
                    if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
                    if (mysql_num_rows($result) < 1)
                    {
                        echo "<h2>{$strError}</h2>";
                        echo "<p>{$strNoFeedBackFormToCompleteHere}</p>";
                        debug_log("\n\n<!-- FORM DOES NOT EXIST f: {$formid} r:{$respondent} rr:{$responseref} dh:{$decodehash}  hc:{$hashcode} ce:{$contactemail} -->\n\n", TRUE);
                    }
                    else
                    {
                        $reqd = 0;
                        while ($form = mysql_fetch_object($result))
                        {
                            echo "<form action='feedback.php' method='post'>\n";
                            echo "<h2>{$form->name}</h2>\n";
                            echo "<p>{$strRelatingToIncident} <strong>#{$incidentid}</strong> &mdash; <strong>".incident_title($incidentid)."</strong><br />";
                            echo sprintf($strOpenedbyXonY, contact_realname(incident_contact($incidentid)), ldate($CONFIG['dateformat_date'],db_read_column('opened', $dbIncidents, $incidentid)));
                            echo ' &nbsp; ';
                            echo sprintf($strClosedOnX, ldate($CONFIG['dateformat_date'],db_read_column('closed', $dbIncidents, $incidentid))).".</p>";

                            if (!empty($_REQUEST['error']))
                            {
                                echo "<p style='color: red'>{$strErrorRequiredQuestionsNotCompleted}</p>";
                            }
                            echo "<div align='center'>" . nl2br($form->introduction) . "</div>";

                            $qsql  = "SELECT * FROM `{$dbFeedbackQuestions}` ";
                            $qsql .= "WHERE formid='{$form->id}' ";
                            $qsql .= "ORDER BY taborder ASC";
                            $qresult = mysql_query($qsql);
                            if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);

                            echo "<table class='maintable vertical'>";

                            $shade = 'shade1';
                            while ($question = mysql_fetch_object($qresult))
                            {
                                if (mb_strlen(trim($question->sectiontext)) > 3)
                                {
                                    echo "<tr class='shade'><td colspan='2'><table><hr /><td>{$question->sectiontext}\n</td></table></td></tr>";
                                }
                                echo "<tr class='{$shade}'>";
                                echo "<td><h4>Q{$question->taborder}: {$question->question}";
                                if ($question->required == 'true')
                                {
                                    echo "<sup style='color: red; font-size: 120%;'>*</sup>";
                                    $reqd++;
                                }
                                echo "</h4>";

                                if (!empty($question->questiontext))
                                {
                                    echo "<p>{$question->questiontext}</p>";
                                }
                                if (!empty($fielddata[$question->id]))
                                {
                                    $answer = $fielddata[$question->id];
                                }
                                else
                                {
                                    $answer = '';
                                }
                                echo "</td><td>";
                                echo feedback_html_question($question->type, "Q{$question->id}", $question->required, $question->options, $answer);
                                if (in_array($question->id, $errorfields))
                                {
                                    echo "<p style='color: red'>".sprintf($strQuestionXNeedsAnsweringBeforeContinuing, $question->taborder)."</p>";
                                }
                                echo "</td><br />";
                                if ($shade == 'shade1')
                                {
                                    $shade = 'shade2';
                                }
                                else
                                {
                                    $shade = 'shade1';
                                }
                            }
                            echo "</table>\n";
                            echo "<p align='center'>" . nl2br($form->thanks) . "</p>" ;

                            echo "<br /><input type='hidden' name='action' value='save' />\n";
                            echo "<input type='hidden' name='ax' value='".strip_tags($_REQUEST['ax'])."' />\n";
                            echo "<div class='formbuttons'><input type='submit' value='Submit' /></div>\n";
                            echo "</form>\n";
                            if ($reqd >= 1)
                            {
                                echo "<p align='center'><sup style='color: red; font-size: 120%;'>*</sup> {$strQuestionRequired}</p>";
                            }
                        }
                    }
                }
            }
            else
            {
                echo "<h3><span class='failure'>{$strError}</span></h3>";
                echo "<h4>{$strNoFeedBackFormToCompleteHere}</h4>";
                debug_log("\n\n<!-- CONTACT DOES NOT EXIST f: {$formid} r:{$respondent} rr:{$responseref} dh:{$decodehash}  hc:{$hashcode} ce:{$contactemail} -->\n\n", TRUE);
            }
            if ($_REQUEST['mode'] != 'bare')
            {
                include (APPLICATION_INCPATH . 'htmlfooter.inc.php');
            }
            else
            {
                echo "\n</div>\n</body>\n</html>\n";
            }
            break;
    }
}
?>