~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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
// ajaxdata.php - Return data for AJAX calls
//
// 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>

require ('core.php');
$permission = PERM_NOT_REQUIRED; // not required
require (APPLICATION_LIBPATH . 'functions.inc.php');
require (APPLICATION_LIBPATH . 'triggers.inc.php');


// This page requires authentication
if ($_REQUEST['action'] == 'contexthelp' AND $_REQUEST['auth'] == 'portal')
{
    // Special exception for contexthelp, use the portal authentication for
    // portal help tips
    $accesslevel = 'any';
    require (APPLICATION_LIBPATH . 'portalauth.inc.php');
}
else
{
    require (APPLICATION_LIBPATH . 'auth.inc.php');
}
$action = cleanvar($_REQUEST['action']);
$selected = cleanvar($_REQUEST['selected']);

switch ($action)
{
    case 'auto_save':
        $userid = $_SESSION['userid'];
        $incidentid = clean_int($_REQUEST['incidentid']);
        $type = cleanvar($_REQUEST['type']);
        $draftid = clean_int($_REQUEST['draftid']);
        $meta = cleanvar($_REQUEST['meta']);
        $content = cleanvar($_REQUEST['content']);

        if ($userid == $_SESSION['userid'])
        {
            if ($draftid == -1)
            {
                $sql = "INSERT INTO `{$dbDrafts}` (userid,incidentid,type,meta,content,lastupdate) VALUES ('{$userid}','{$incidentid}','{$type}','{$meta}','{$content}','{$now}')";
            }
            else
            {
                $sql = "UPDATE `{$dbDrafts}` SET content = '{$content}', meta = '{$meta}', lastupdate = '{$now}' WHERE id = {$draftid}";
            }
            $result = mysql_query($sql);
            if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
            echo mysql_insert_id();
        }
        break;
    case 'servicelevel_timed':
        $sltag = cleanvar($_REQUEST['servicelevel']);
        if (servicelevel_timed($sltag))
        {
            echo "TRUE";
        }
        else
        {
            echo "FALSE";
        }
        break;
    case 'contexthelp':
        $context = cleanvar($_REQUEST['context']);
        $helpfile = APPLICATION_HELPPATH . "{$_SESSION['lang']}". DIRECTORY_SEPARATOR . "{$context}.txt";
        // Default back to english if language helpfile isn't found
        if (!file_exists($helpfile)) $helpfile = APPLICATION_HELPPATH . "en-GB/{$context}.txt";
        if (file_exists($helpfile))
        {
            $fp = fopen(clean_fspath($helpfile), 'r', TRUE);
            $helptext = fread($fp, 1024);
            fclose($fp);
            echo nl2br($helptext);
        }
        else
        {
            echo "Error: Missing helpfile '{$_SESSION['lang']}/{$context}.txt'";
        }
        break;
    case 'dismiss_notice':
        require (APPLICATION_LIBPATH . 'auth.inc.php');
        // We don't use clean_int here as it may be a int or 'all' if its a string its not used directly
        $noticeid = clean_dbstring($_REQUEST['noticeid']);
        $userid = clean_int($_REQUEST['userid']);
        if (is_numeric($noticeid))
        {
            $sql = "DELETE FROM `{$GLOBALS['dbNotices']}` WHERE id='{$noticeid}' AND userid='{$sit[2]}'";
            mysql_query($sql);
            if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
            else echo "deleted {$noticeid}";
        }
        elseif ($noticeid == 'all')
        {
            $sql = "DELETE FROM `{$GLOBALS['dbNotices']}` WHERE userid={$userid} LIMIT 20"; // only delete 20 max as we only show 20 max
            mysql_query($sql);
            if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
            else echo "deleted {$noticeid}";
        }
        break;
    case 'dashboard_display':
        require (APPLICATION_LIBPATH . 'auth.inc.php');
        $dashboard = clean_dbstring($_REQUEST['dashboard']);
        $dashletid = 'win'.cleanvar($_REQUEST['did']);
        if (is_dashlet_installed($dashboard))
        {
            include (APPLICATION_PLUGINPATH . "dashboard_{$dashboard}.php");
            $dashfn = "dashboard_{$dashboard}_display";
            echo $dashfn($dashletid);
        }
        break;
    case 'dashboard_save':
    case 'dashboard_edit':
        require (APPLICATION_LIBPATH . 'auth.inc.php');

        $dashboard = clean_dbstring($_REQUEST['dashboard']);
        $dashletid = 'win'.cleanvar($_REQUEST['did']);
        if (is_dashlet_installed($dashboard))
        {
            include (APPLICATION_PLUGINPATH . "dashboard_{$dashboard}.php");
            $dashfn = "dashboard_{$dashboard}_edit";
            echo $dashfn($dashletid);
        }
        break;
    case 'autocomplete_sitecontact':
        $s = clean_dbstring($_REQUEST['s']);
        $htmllist = clean_dbstring($_REQUEST['htmllist']);
        $sql = "SELECT DISTINCT forenames, surname FROM `{$dbContacts}` ";
        $sql .= "WHERE active='true' AND (forenames LIKE '{$s}%' OR surname LIKE '{$s}%')";
        $result = mysql_query($sql);
        if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
        if ($htmllist == 'true') $str = "<ul>";
        if (mysql_num_rows($result) > 0)
        {
            while ($obj = mysql_fetch_object($result))
            {
                if ($htmllist == 'true') $str .= "<li>{$obj->forenames} {$obj->surname}</li>";
                else $str .= "[\"".$obj->forenames." ".$obj->surname."\"],";
            }
        }
        $sql = "SELECT DISTINCT name FROM `{$dbSites}` ";
        $sql .= "WHERE active='true' AND name LIKE '{$s}%'";
        $result = mysql_query($sql);
        if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
        if (mysql_num_rows($result) > 0)
        {
            while ($obj = mysql_fetch_object($result))
            {
                if ($htmllist == 'true') $str .= "<li>{$obj->name}</li>";
                else $str .= "[\"".$obj->name."\"],";
            }
        }
        if ($htmllist == 'true') $str .= "</ul>";
        else $str .= "[".mb_substr($str, 0, -1)."]";
        echo $str;
        break;
    case 'tags':
        $sql = "SELECT DISTINCT t.name FROM `{$dbSetTags}` AS st, `{$dbTags}` AS t WHERE st.tagid = t.tagid GROUP BY t.name";
        $result = mysql_query($sql);
        if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
        if (mysql_num_rows($result) > 0)
        {
            while ($obj = mysql_fetch_object($result))
            {
                $str .= "[".$obj->name."],";
            }
        }
        echo "[".mb_substr($str,0,-1)."]";
        break;
    case 'contact' :
        $s = clean_dbstring($_REQUEST['s']);
        $htmllist = clean_dbstring($_REQUEST['htmllist']);
        $sql = "SELECT DISTINCT forenames, surname FROM `{$dbContacts}` ";
        $sql .= "WHERE active='true' AND (forenames LIKE '{$s}%' OR surname LIKE '{$s}%')";
        $result = mysql_query($sql);
        if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
        if ($htmllist == 'true') $str = "<ul>";
        if (mysql_num_rows($result) > 0)
        {
            while ($obj = mysql_fetch_object($result))
            {
                if ($htmllist == 'true') $str .= "<li>{$obj->forenames} {$obj->surname}</li>";
                else $str .= "[\"".$obj->forenames." ".$obj->surname."\"],";
            }
        }
        if ($htmllist == 'true') $str .= "</ul>";
        else $str .= "[".mb_substr($str, 0, -1)."]";
        echo $str;
        break;
    case 'sites':
        $s = clean_dbstring($_REQUEST['s']);
        $htmllist = clean_dbstring($_REQUEST['htmllist']);
        $sql = "SELECT DISTINCT name FROM `{$dbSites}` ";
        $sql .= "WHERE active='true' AND name LIKE '{$s}%'";
        $result = mysql_query($sql);
        if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
        if ($htmllist == 'true') $str = "<ul>";
        if (mysql_num_rows($result) > 0)
        {
            while ($obj = mysql_fetch_object($result))
            {
                if ($htmllist == 'true') $str .= "<li>{$obj->name}</li>";
                else $str .= "[\"".$obj->name."\"],";
            }
        }
        if ($htmllist == 'true') $str .= "</ul>";
        else $str .= "[".mb_substr($str, 0, -1)."]";
        echo $str; 
        break;
    case 'slas':
        $sql = "SELECT DISTINCT tag FROM `{$dbServiceLevels}`";
        $result = mysql_query($sql);
        if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
        while ($obj = mysql_fetch_object($result))
        {
            $strIsSelected = '';
            if ($obj->tag == $selected)
            {
                $strIsSelected = "selected='selected'";
            }
            echo "<option value='{$obj->tag}' {$strIsSelected}>{$obj->tag}</option>";
        }
        break;
    case 'products':
        $sql = "SELECT id, name FROM `{$dbProducts}` ORDER BY name ASC";
        $result = mysql_query($sql);
        if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
        while ($obj = mysql_fetch_object($result))
        {
            $strIsSelected = '';
            if ($obj->id == $selected)
            {
                $strIsSelected = "selected='selected'";
            }
            echo "<option value='{$obj->id}' {$strIsSelected}>{$obj->name}</option>";
        }
        break;
    case 'skills':
        $sql = "SELECT id, name FROM `{$dbSoftware}` ORDER BY name ASC";
        $result = mysql_query($sql);
        if (mysql_error()) trigger_error(mysql_error(), E_USER_WARNING);
        while ($obj = mysql_fetch_object($result))
        {
            $strIsSelected = '';
            if ($obj->id == $selected)
            {
                $strIsSelected = "selected='selected'";
            }
            echo "<option value='{$obj->id}' {$strIsSelected}>{$obj->name}</option>";
        }
        break;
    case 'storedashboard':
        $id = $_SESSION['userid'];
        $val = clean_dbstring($_REQUEST['val']);

        if ($id == $_SESSION['userid'])
        {
            //check you're changing your own
            $sql = "UPDATE `{$dbUsers}` SET dashboard = '{$val}' WHERE id = {$id}";
            $contactresult = mysql_query($sql);
            if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
        }
        break;
    case 'checkldap':
        $ldap_host = cleanvar($_REQUEST['ldap_host']);
        $ldap_port = clean_int($_REQUEST['ldap_port']);
        $ldap_protocol = cleanvar($_REQUEST['ldap_protocol']);
        $ldap_security = cleanvar($_REQUEST['ldap_security']);
        $ldap_type = clean_fixed_list($_REQUEST['ldap_type'], array('', 'SSL'));
        $ldap_user = clean_ldapstring($_REQUEST['ldap_bind_user']);
        $ldap_password = clean_ldapstring($_REQUEST['ldap_bind_pass']);
        $ldap_user_base = clean_ldapstring($_REQUEST['ldap_user_base']);
        $ldap_admin_group = clean_ldapstring($_REQUEST['ldap_admin_group']);
        $ldap_manager_group = clean_ldapstring($_REQUEST['ldap_manager_group']);
        $ldap_user_group = clean_ldapstring($_REQUEST['ldap_user_group']);
        $ldap_customer_group = clean_ldapstring($_REQUEST['ldap_customer_group']);

        $r = ldapOpen($ldap_host, $ldap_port, $ldap_protocol, $ldap_security, $ldap_user, $ldap_password);
        if ($r == -1) echo LDAP_PASSWORD_INCORRECT; // Failed
        else
        {
            // Check user base
            if (!ldapCheckObjectExists($ldap_user_base, "*")) echo LDAP_BASE_INCORRECT;
            else
            {
            	// Check Admin group
                if (!ldapCheckGroupExists($ldap_admin_group, $ldap_type)) echo LDAP_ADMIN_GROUP_INCORRECT;
                else
                {
                    // Check manager group
                    if (!ldapCheckGroupExists($ldap_manager_group, $ldap_type)) echo LDAP_MANAGER_GROUP_INCORRECT;
                    else
                    {
                        // Check user group
                        if (!ldapCheckGroupExists($ldap_user_group, $ldap_type)) echo LDAP_USER_GROUP_INCORRECT;
                        else
                        {
                            // Check customer group
                            if (!ldapCheckGroupExists($ldap_customer_group, $ldap_type)) echo LDAP_CUSTOMER_GROUP_INCORRECT;
                            else
                            {
                                // ALL OK
                                echo LDAP_CORRECT;
                            }
                        }
                    }
                }
            }
        }

        break;
    case 'triggerpairmatch':
        $triggertype = cleanvar($_REQUEST['triggertype']);
        $action = cleanvar($_REQUEST['triggeraction']);
        debug_log("Returning a template for {$triggertype} and {$action}", TRUE);
        if ($action == 'ACTION_EMAIL')
        {
            echo $email_pair[$triggertype];
        }
        elseif ($action == 'ACTION_NOTICE')
        {
            echo $notice_pair[$triggertype];
        }
        break;
    case 'checkhtml':
        $triggertype = cleanvar($_REQUEST['triggertype']);
        if (is_numeric($trigger_type)) $trigger_type = $trigger_type[0];
        if (is_array($trigger_types[$triggertype]['params']))
        {
            echo "<p align='left'>{$strNotifyWhen} ";
            echo "<select name='conditions'><option value='all'>{$strAllConditionsMet}</option>";
            echo "<option value='any'>{$strAnyConditionMet}</option></select></p>";
            echo "<table>";
            $i = 0;
            foreach ($trigger_types[$triggertype]['params'] as $param)
            {
                // if we return a number here, the variable is multiply-defined;
                // as the replacements are the same, we use the first one
                if (is_array($ttvararray['{'.$param.'}']) AND
                    is_numeric(key($ttvararray['{'.$param.'}'])))
                {
                    //echo "\$ttvararray[\{{$param}\}] = ".$ttvararray['{'.$param.'}'];
                    $ttvararray['{'.$param.'}'] = $ttvararray['{'.$param.'}'][0];
                }

                if (isset($ttvararray['{'.$param.'}']['checkreplace']))
                {
                    echo '<tr>';
                    echo "<td><input type='hidden' name='param[{$i}]' value='{$param}' /></td>";
                    echo '<td align="right">'.$ttvararray['{'.$param.'}']['description']. '</td>';
                    echo '<td>'.check_match_drop_down('join['.$i.']'). '</td>';
                    echo '<td>'.$ttvararray['{'.$param.'}']['checkreplace']('value['.$i.']')."</td>";
                    // put a hidden input so we can see unchecked boxes
                    echo "<td><input type='hidden' name='enabled[{$i}]' value='off' />";
                    echo "<label><input type='checkbox' name='enabled[{$i}]' /> {$strEnableCondition}</label></td></tr>";
                    $i++;
                }
            }
            echo '</table>';
        }
        // if ($html == " ") $html = "No variables available for this action.";
        echo " ";
        break;
    case 'set_user_status':
        $userstatus = cleanvar($_REQUEST['userstatus']);
        $result = set_user_status($_SESSION['userid'], $userstatus);
        if ($result === FALSE)
        {
            echo 'FALSE';
        }
        else
        {
            echo $result;
        }
        break;
    case 'delete_temp_assign':
        if (user_permission($sit[2], PERM_UPDATE_DELETE))
        {
            $incidentid = clean_int($_REQUEST['incidentid']);
            $originalowner = clean_int($_REQUEST['originalowner']);
            $sql = "UPDATE `{$dbTempAssigns}` SET assigned='yes' ";
            $sql .= "WHERE incidentid='{$incidentid}' AND originalowner='{$originalowner}' LIMIT 1";
            mysql_query($sql);
            if (mysql_error())
            {
                echo "FAILED";
                trigger_error(mysql_error(), E_USER_ERROR);
            }
            else
            {
                echo "OK";
            }
        }
        else
        {
            echo "NOPERMISSION";
        }
        break;
    case 'ldap_browse_groups':
        $base = cleanvar($_REQUEST['base']);
        $field = cleanvar($_REQUEST['field']);
        $ldap_type = cleanvar($_REQUEST['ldap_type']);
        $ldap_host = cleanvar($_REQUEST['ldap_host']);
        $ldap_port = clean_int($_REQUEST['ldap_port']);
        $ldap_protocol = clean_int($_REQUEST['ldap_protocol']);
        $ldap_security = cleanvar($_REQUEST['ldap_security']);
        $ldap_bind_user = cleanvar($_REQUEST['ldap_bind_user']);
        $ldap_bind_pass = cleanvar($_REQUEST['ldap_bind_pass']);
        echo json_encode(ldapGroupBrowse($base, $ldap_host, $ldap_port, $ldap_type, $ldap_protocol, $ldap_security, $ldap_bind_user, $ldap_bind_pass));
        break;
    case 'display_billingmatrix':
        $billingtype = cleanvar($_REQUEST['billingtype']);
        $selected = cleanvar($_REQUEST['selected']);
        
        $bill = new $billingtype();
        $s = $bill->billing_matrix_selector('billing_matrix', $selected);
        
        if (empty($s)) 
        {
            $s = $GLOBALS['strNotApplicableAbbrev'];
        }
        else
        {
            $s = $s . "<span class='required'>{$GLOBALS['strRequired']}</span>"; 
        }
        
        echo $s;
        break;
    default :
        plugin_do('ajaxdata_action', array('action' => $action));
        break;
}


?>