osticket /upload/include/class.config.php

git https://bitbucket.org/fstech/osticket.git
Language PHP Code Lines 455
MD5 Hash c90e408f87ce464629034bb914575d12 Comment Lines 23
Estimated Cost $11,764 Blank Lines 129
  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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
<?php
/*********************************************************************
    class.config.php

    osTicket config info manager. 

    Peter Rotich <peter@osticket.com>
    Copyright (c)  2006-2010 osTicket
    http://www.osticket.com

    Released under the GNU General Public License WITHOUT ANY WARRANTY.
    See LICENSE.TXT for details.

    vim: expandtab sw=4 ts=4 sts=4:
    $Id: $
**********************************************************************/

require_once(INCLUDE_DIR.'class.email.php');

class Config {
    
    var $id=0;
    var $mysqltzoffset=0;
    var $config=array();

    var $defaultDept;   //Default Department    
    var $defaultEmail;  //Default Email 
    var $alertEmail;  //Alert Email
    var $defaultSMTPEmail; //Default  SMTP Email

    function Config($id) { 
        $this->load($id);
    }

    function load($id) {

        if($id && is_numeric($id)):
         $this->id=$id;
         $this->config=array();
         $res=db_query('SELECT * FROM '.CONFIG_TABLE.' WHERE id='.$id);
         if($res && db_num_rows($res))
            $this->config=db_fetch_array($res); 
        endif;
        return $this->config?true:false;
    }

    //Initialize some default values.
    function init(){
        list($mysqltz)=db_fetch_row(db_query('SELECT @@session.time_zone '));
        $this->setMysqlTZ($mysqltz);
    }
    
    function reload() {
        if($this->load($this->id))
            $this->init();
    }


    function isHelpDeskOffline() {
        return $this->config['isonline']?false:true;
    }

    function getAPIPassphrase(){
        return $this->config['api_passphrase'];
    }

    function getVersion(){
        return $this->config['ostversion'];
    }

    function setMysqlTZ($tz){
        //TODO: Combine the 2 replace regex 
        $this->mysqltzoffset=($tz=='SYSTEM')?preg_replace('/([+-]\d{2})(\d{2})/','\1',date('O')):preg_replace('/([+-]\d{2})(:)(\d{2})/','\1',$tz);
    }
    
    function getMysqlTZoffset() {
        return $this->mysqltzoffset;
    }

    /* Date & Time Formats */
    function observeDaylightSaving() {
        return $this->config['enable_daylight_saving']?true:false;
    }
    function getTimeFormat(){
        return $this->config['time_format'];
    }
    function getDateFormat(){
        return $this->config['date_format'];
    }

    function getDateTimeFormat(){
        return $this->config['datetime_format'];
    }

    function getDayDateTimeFormat(){
        return $this->config['daydatetime_format'];
    }

    function getId() {
        return $this->config['id'];
    }
   
    function getTitle() {
        return $this->config['helpdesk_title'];
    }
    
    function getUrl() {
        return $this->config['helpdesk_url'];        
    }
    
    function getBaseUrl(){ //Same as above with no trailing slash.
        return rtrim($this->getUrl(),'/');
    }

    function getConfig() {
        return $this->config;
    }

    function getTZOffset(){
        return $this->config['timezone_offset'];
    }

    function getPageSize() {
        return $this->config['max_page_size'];
    }

    function getGracePeriod() {
        return $this->config['overdue_grace_period'];
    }

        
    function getClientTimeout() {
        return $this->getClientSessionTimeout();
    }
 
    function getClientSessionTimeout() {
        return $this->config['client_session_timeout']*60;
    }

    function getClientLoginTimeout() {
        return $this->config['client_login_timeout']*60;
    }

    function getClientMaxLogins() {
        return $this->config['client_max_logins'];
    }

    function getStaffTimeout() {
        return $this->getStaffSessionTimeout();
    }
        
    function getStaffSessionTimeout() {                
        return $this->config['staff_session_timeout']*60;
    }

    function getStaffLoginTimeout() {
        return $this->config['staff_login_timeout']*60;
    }

    function getStaffMaxLogins() {
        return $this->config['staff_max_logins'];
    }

    function getLockTime() {
        return $this->config['autolock_minutes'];
    }

    function getDefaultDeptId(){
        return $this->config['default_dept_id'];
    }

    function getDefaultDept(){

        if(!$this->defaultDept && $this->getDefaultDeptId())
            $this->defaultDept= new Dept($this->getDefaultDeptId());
        return $this->defaultDept;
    }   

    function getDefaultEmailId(){
        return $this->config['default_email_id'];
    }

    function getDefaultEmail(){

        if(!$this->defaultEmail && $this->getDefaultEmailId())
            $this->defaultEmail= new Email($this->getDefaultEmailId());
        return $this->defaultEmail;
    }

    function getDefaultEmailAddress() {
        $email=$this->getDefaultEmail();
        return $email?$email->getAddress():null;
    }

    function getAlertEmailId() {
        return $this->config['alert_email_id'];
    }

    function getAlertEmail(){

        if(!$this->alertEmail && $this->config['alert_email_id'])
            $this->alertEmail= new Email($this->config['alert_email_id']);
        return $this->alertEmail;
    }

    function getDefaultSMTPEmail(){

        if(!$this->defaultSMTPEmail && $this->config['default_smtp_id'])
            $this->defaultSMTPEmail= new Email($this->config['default_smtp_id']);
        return $this->defaultSMTPEmail;
    }

    function allowSMTPSpoofing() {
        return $this->config['spoof_default_smtp'];
    }

    function getDefaultPriorityId(){
        return $this->config['default_priority_id'];
    }

    function getDefaultTemplateId() {
        return $this->config['default_template_id'];
    }

    function getMaxOpenTickets() {
         return $this->config['max_open_tickets'];
    }

    function getMaxFileSize(){
        return $this->config['max_file_size'];
    }

    function getLogLevel(){
        return $this->config['log_level'];
    }

    function getLogGracePeriod(){
        return $this->config['log_graceperiod'];
    }

    function logTicketActivity(){
        return $this->config['log_ticket_activity'];
    }

    function clickableURLS() {
        return $this->config['clickable_urls']?true:false;
    }
        
    function canFetchMail() {
        return $this->config['enable_mail_fetch']?true:false;
    }

    function enableStaffIPBinding(){
        return $this->config['staff_ip_binding']?true:false;
    }

    function enableCaptcha() {
        
        //Checking it in real time!
        if(!extension_loaded('gd') || !function_exists('gd_info'))
            return false;

        return $this->config['enable_captcha']?true:false;
    }

    function enableAutoCron() {
        return $this->config['enable_auto_cron']?true:false;
    }
        
    function enableEmailPiping() {
        return $this->config['enable_email_piping']?true:false;
    }

    function allowPriorityChange() {
        return $this->config['allow_priority_change']?true:false;
    }

        
    function useEmailPriority() {
        return $this->config['use_email_priority']?true:false;
    }

    function getAdminEmail(){
         return $this->config['admin_email'];
    }

    function getReplySeparator() {
        return $this->config['reply_separator'];
    }
  
    function stripQuotedReply() {
        return $this->config['strip_quoted_reply']?true:false;
    }

    function saveEmailHeaders() {
        return true; //No longer an option...hint: big plans for headers coming!!
    }
    
    function useRandomIds() {
        return $this->config['random_ticket_ids']?true:false;
    }

    /* autoresponders  & Alerts */
    function autoRespONNewTicket() {
        return $this->config['ticket_autoresponder']?true:false;
    }
    
    function autoRespONNewMessage() {
        return $this->config['message_autoresponder']?true:false;
    }

    function notifyONNewStaffTicket(){
        return $this->config['ticket_notice_active']?true:false;
    }

    function alertONNewMessage() {
        return $this->config['message_alert_active']?true:false;
    }

    function alertLastRespondentONNewMessage() {
        return $this->config['message_alert_laststaff']?true:false;
    }
   
    function alertAssignedONNewMessage() {
        return $this->config['message_alert_assigned']?true:false;
    }
    
    function alertDeptManagerONNewMessage() {
        return $this->config['message_alert_dept_manager']?true:false;
    }

    function alertONNewNote() {
        return $this->config['note_alert_active']?true:false;
    }

    function alertLastRespondentONNewNote() {
        return $this->config['note_alert_laststaff']?true:false;
    }

    function alertAssignedONNewNote() {
        return $this->config['note_alert_assigned']?true:false;
    }

    function alertDeptManagerONNewNote() {
        return $this->config['note_alert_dept_manager']?true:false;
    }

    function alertONNewTicket() {
        return $this->config['ticket_alert_active']?true:false;
    }

    function alertAdminONNewTicket() {
        return $this->config['ticket_alert_admin']?true:false;
    }
     
    function alertDeptManagerONNewTicket() {
        return $this->config['ticket_alert_dept_manager']?true:false;
    }

    function alertDeptMembersONNewTicket() {
        return $this->config['ticket_alert_dept_members']?true:false;
    }

    function alertONOverdueTicket() {
        return $this->config['overdue_alert_active']?true:false;
    }

    function alertAssignedONOverdueTicket() {
        return $this->config['overdue_alert_assigned']?true:false;
    }

    function alertDeptManagerONOverdueTicket() {
        return $this->config['overdue_alert_dept_manager']?true:false;
    }

    function alertDeptMembersONOverdueTicket() {
        return $this->config['overdue_alert_dept_members']?true:false;
    }

    function autoCloseTicketOnReply() {
        return $this->config['auto_close_ticket_on_reply']?true:false;
    }

    function autoAssignReopenedTickets() {
        return $this->config['auto_assign_reopened_tickets']?true:false;
    }

    function showAssignedTickets() {
        return $this->config['show_assigned_tickets']?true:false;
    }

    function showAnsweredTickets() {
        return $this->config['show_answered_tickets']?true:false;
    }
        
    function hideStaffName() {
        return $this->config['hide_staff_name']?true:false;
    }

    function sendOverLimitNotice() {
        return $this->config['overlimit_notice_active']?true:false;
    }
        
    /* Error alerts sent to admin email when enabled */
    function alertONSQLError() {
        return $this->config['send_sql_errors']?true:false;                    
    }
    function alertONLoginError() {
        return $this->config['send_login_errors']?true:false;
    }

    function alertONMailParseError() {
        return $this->config['send_mailparse_errors']?true:false;
    }

    

    /* Attachments */

    function emailAttachments() {
        return $this->config['email_attachments']?true:false;
    }

    function allowAttachments() {
        return $this->config['allow_attachments']?true:false;
    }

    function allowOnlineAttachments() {
        return ($this->allowAttachments() && $this->config['allow_online_attachments'])?true:false;
    }

    function allowAttachmentsOnlogin() {
        return ($this->allowOnlineAttachments() && $this->config['allow_online_attachments_onlogin'])?true:false;
    }
    
    function allowEmailAttachments() {
        return ($this->allowAttachments() && $this->config['allow_email_attachments'])?true:false;
    }

    function getUploadDir() {
        return $this->config['upload_dir'];
    }
    
    //simply checking if destination dir is usable..nothing to do with permission to upload!
    function canUploadFiles() {   
        $dir=$this->config['upload_dir'];
        return ($dir && is_writable($dir))?TRUE:FALSE;
    }

    function canUploadFileType($filename) {       
        $ext = strtolower(preg_replace("/.*\.(.{3,4})$/", "$1", $filename));
        $allowed=$this->config['allowed_filetypes']?array_map('trim',explode(',',strtolower($this->config['allowed_filetypes']))):null;
        return ($ext && is_array($allowed) && (in_array(".$ext",$allowed) || in_array(".*",$allowed)))?TRUE:FALSE;
    }

    function updatePref($var,&$errors) {
      
        if(!$var || $errors)
            return false;
        
        $f=array();
        $f['helpdesk_url']=array('type'=>'string',   'required'=>1, 'error'=>'Helpdesk URl required'); //TODO: Add url validation
        $f['helpdesk_title']=array('type'=>'string',   'required'=>1, 'error'=>'Helpdesk title required');
        $f['default_dept_id']=array('type'=>'int',   'required'=>1, 'error'=>'Default Dept. required');
        $f['default_email_id']=array('type'=>'int',   'required'=>1, 'error'=>'Default email required');
        $f['default_template_id']=array('type'=>'int',   'required'=>1, 'error'=>'You must select template.');
        $f['staff_session_timeout']=array('type'=>'int',   'required'=>1, 'error'=>'Enter idle time in minutes');
        $f['client_session_timeout']=array('type'=>'int',   'required'=>1, 'error'=>'Enter idle time in minutes');
        $f['time_format']=array('type'=>'string',   'required'=>1, 'error'=>'Time format required'); //TODO: Add date format validation  
        $f['date_format']=array('type'=>'string',   'required'=>1, 'error'=>'Date format required');
        $f['datetime_format']=array('type'=>'string',   'required'=>1, 'error'=>'Datetime format required');
        $f['daydatetime_format']=array('type'=>'string',   'required'=>1, 'error'=>'Day, Datetime format required');
        $f['admin_email']=array('type'=>'email',   'required'=>1, 'error'=>'Valid email required');
        $f['autolock_minutes']=array('type'=>'int',   'required'=>1, 'error'=>'Enter lock time in minutes');
        //TODO: check option fields for validity.

        //do the validation.
        $val = new Validator();        
        $val->setFields($f);
        if(!$val->validate($var)){
            $errors=array_merge($errors,$val->errors());                                        
        }
                        
        if($var['ticket_alert_active'] 
                && (!isset($var['ticket_alert_admin']) 
                    && !isset($var['ticket_alert_dept_manager'])
                    && !isset($var['ticket_alert_dept_members']))){        
            $errors['ticket_alert_active']='No target recipient(s) selected';
        }       
        if($var['message_alert_active']
                && (!isset($var['message_alert_laststaff'])
                    && !isset($var['message_alert_assigned'])
                    && !isset($var['message_alert_dept_manager']))){
            $errors['message_alert_active']='No target recipient(s) selected';
        }

        if($var['note_alert_active']
                && (!isset($var['note_alert_laststaff'])
                    && !isset($var['note_alert_assigned'])
                    && !isset($var['note_alert_dept_manager']))){
            $errors['note_alert_active']='No target recipient(s) selected';
        }

        if($var['strip_quoted_reply'] && !$var['reply_separator'])
            $errors['reply_separator']='Reply separator required (?)';

        if($var['enable_captcha']){
            if (!extension_loaded('gd'))
                $errors['enable_captcha']='The GD extension required';
            elseif(!function_exists('imagepng'))
                $errors['enable_captcha']='PNG support required for Image Captcha';
        }

        if(!$errors['admin_email'] && Email::getIdByEmail($var['admin_email'])) //Make sure admin email is not also a system email.
            $errors['admin_email']='Email already setup as system email';




        if($errors) return false; //No go! 

        //We are good to go...blanket update!
        $sql= 'UPDATE '.CONFIG_TABLE.' SET isonline='.db_input($var['isonline']).
            ',timezone_offset='.db_input($var['timezone_offset']).
            ',enable_daylight_saving='.db_input(isset($var['enable_daylight_saving'])?1:0).
            ',staff_ip_binding='.db_input(isset($var['staff_ip_binding'])?1:0).
            ',staff_max_logins='.db_input($var['staff_max_logins']).
            ',staff_login_timeout='.db_input($var['staff_login_timeout']).
            ',staff_session_timeout='.db_input($var['staff_session_timeout']).
            ',client_max_logins='.db_input($var['client_max_logins']).
            ',client_login_timeout='.db_input($var['client_login_timeout']).
            ',client_session_timeout='.db_input($var['client_session_timeout']).
            ',max_page_size='.db_input($var['max_page_size']).
            ',log_level='.db_input($var['log_level']).
            ',log_graceperiod='.db_input($var['log_graceperiod']).
            ',max_open_tickets='.db_input($var['max_open_tickets']).
            ',autolock_minutes='.db_input($var['autolock_minutes']).
            ',overdue_grace_period='.db_input($var['overdue_grace_period']).
            ',alert_email_id='.db_input($var['alert_email_id']).
            ',default_email_id='.db_input($var['default_email_id']).
            ',default_dept_id='.db_input($var['default_dept_id']).
            ',default_priority_id='.db_input($var['default_priority_id']).
            ',default_template_id='.db_input($var['default_template_id']).
            ',default_smtp_id='.db_input($var['default_smtp_id']).
            ',spoof_default_smtp='.db_input(($var['default_smtp'] && isset($var['spoof_default_smtp']))?1:0).
            ',clickable_urls='.db_input(isset($var['clickable_urls'])?1:0).
            ',allow_priority_change='.db_input(isset($var['allow_priority_change'])?1:0).
            ',use_email_priority='.db_input(isset($var['use_email_priority'])?1:0).
            ',enable_captcha='.db_input(isset($var['enable_captcha'])?1:0).
            ',enable_auto_cron='.db_input(isset($var['enable_auto_cron'])?1:0).
            ',enable_mail_fetch='.db_input(isset($var['enable_mail_fetch'])?1:0).
            ',enable_email_piping='.db_input(isset($var['enable_email_piping'])?1:0).
            ',send_sql_errors='.db_input(isset($var['send_sql_errors'])?1:0).
            ',send_login_errors='.db_input(isset($var['send_login_errors'])?1:0).
            ',save_email_headers='.db_input(isset($var['save_email_headers'])?1:0).
            ',strip_quoted_reply='.db_input(isset($var['strip_quoted_reply'])?1:0).
            ',log_ticket_activity='.db_input(isset($var['log_ticket_activity'])?1:0).
            ',ticket_autoresponder='.db_input($var['ticket_autoresponder']).
            ',message_autoresponder='.db_input($var['message_autoresponder']).
            ',ticket_notice_active='.db_input($var['ticket_notice_active']).
            ',ticket_alert_active='.db_input($var['ticket_alert_active']).
            ',ticket_alert_admin='.db_input(isset($var['ticket_alert_admin'])?1:0).
            ',ticket_alert_dept_manager='.db_input(isset($var['ticket_alert_dept_manager'])?1:0).
            ',ticket_alert_dept_members='.db_input(isset($var['ticket_alert_dept_members'])?1:0).
            ',message_alert_active='.db_input($var['message_alert_active']).
            ',message_alert_laststaff='.db_input(isset($var['message_alert_laststaff'])?1:0).
            ',message_alert_assigned='.db_input(isset($var['message_alert_assigned'])?1:0).
            ',message_alert_dept_manager='.db_input(isset($var['message_alert_dept_manager'])?1:0).
            ',note_alert_active='.db_input($var['note_alert_active']).
            ',note_alert_laststaff='.db_input(isset($var['note_alert_laststaff'])?1:0).
            ',note_alert_assigned='.db_input(isset($var['note_alert_assigned'])?1:0).
            ',note_alert_dept_manager='.db_input(isset($var['note_alert_dept_manager'])?1:0).
            ',overdue_alert_active='.db_input($var['overdue_alert_active']).
            ',overdue_alert_assigned='.db_input(isset($var['overdue_alert_assigned'])?1:0).
            ',overdue_alert_dept_manager='.db_input(isset($var['overdue_alert_dept_manager'])?1:0).
            ',overdue_alert_dept_members='.db_input(isset($var['overdue_alert_dept_members'])?1:0).
            ',auto_assign_reopened_tickets='.db_input(isset($var['auto_assign_reopened_tickets'])?1:0).
            ',auto_close_ticket_on_reply='.db_input(isset($var['auto_close_ticket_on_reply'])?1:0).
            ',show_assigned_tickets='.db_input(isset($var['show_assigned_tickets'])?1:0).
            ',show_answered_tickets='.db_input(isset($var['show_answered_tickets'])?1:0).
            ',hide_staff_name='.db_input(isset($var['hide_staff_name'])?1:0).
            ',overlimit_notice_active='.db_input($var['overlimit_notice_active']).
            ',random_ticket_ids='.db_input($var['random_ticket_ids']).
            ',time_format='.db_input($var['time_format']).
            ',date_format='.db_input($var['date_format']).
            ',datetime_format='.db_input($var['datetime_format']).
            ',daydatetime_format='.db_input($var['daydatetime_format']).
            ',reply_separator='.db_input(trim($var['reply_separator'])).
            ',admin_email='.db_input($var['admin_email']).
            ',helpdesk_title='.db_input($var['helpdesk_title']).
            ',helpdesk_url='.db_input($var['helpdesk_url']).
            ' WHERE id='.$this->getId();
        //echo $sql;
        if(db_query($sql)) {
            if(db_affected_rows()) {//Something actually changed!!!!
                $this->reload();//Reload the new info.
                require_once(INCLUDE_DIR.'class.cron.php');
                Sys::purgeLogs(); //Cleanup the logs --- too bad if it was a mistaken config.
                Cron::TicketMonitor(); //Age & cleanup
            }
            return true;
        }
        return false;
    }
    
}
?>