Newer
Older
<?php
/*********************************************************************
class.config.php
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:
**********************************************************************/
class Config {
Peter Rotich
committed
var $config = array();
var $section = null; # Default namespace ('core')
var $table = CONFIG_TABLE; # Table name (with prefix)
var $section_column = 'namespace'; # namespace column name
var $session = null; # Session-backed configuration
# Defaults for this configuration. If settings don't exist in the
# database yet, the ->getInfo() method will not include the (default)
# values in the returned array. $defaults allows developers to define
# new settings and the corresponding default values.
var $defaults = array(); # List of default values
$this->section = $section;
if ($this->section === null)
if (!isset($_SESSION['cfg:'.$this->section]))
$_SESSION['cfg:'.$this->section] = array();
$this->session = &$_SESSION['cfg:'.$this->section];
$sql='SELECT id, `key`, value, `updated` FROM '.$this->table
.' WHERE `'.$this->section_column.'` = '.db_input($this->section);
if(($res=db_query($sql)) && db_num_rows($res))
while ($row = db_fetch_array($res))
$this->config[$row['key']] = $row;
function getNamespace() {
return $this->section;
}
foreach ($this->config as $key=>$setting)
$info[$key] = $setting['value'];
return $info;
}
function get($key, $default=null) {
if (isset($this->session[$key]))
elseif (isset($this->config[$key]))
return $this->config[$key]['value'];
elseif (isset($this->defaults[$key]))
return $this->defaults[$key];
function exists($key) {
return $this->get($key, null) ? true : false;
}
function set($key, $value) {
return ($this->update($key, $value)) ? $value : null;
function persist($key, $value) {
$this->session[$key] = $value;
return true;
}
function lastModified($key) {
if (isset($this->config[$key]))
return $this->config[$key]['updated'];
else
return false;
}
function create($key, $value) {
.' SET `'.$this->section_column.'`='.db_input($this->section)
.', `key`='.db_input($key)
.', value='.db_input($value);
if (!db_query($sql) || !($id=db_insert_id()))
$this->config[$key] = array('key'=>$key, 'value'=>$value, 'id'=>$id);
return true;
}
function update($key, $value) {
if (!$key)
return false;
elseif (!isset($this->config[$key]))
return $this->create($key, $value);
$setting = &$this->config[$key];
if ($setting['value'] == $value)
return true;
if (!db_query('UPDATE '.$this->table.' SET updated=NOW(), value='
.db_input($value).' WHERE id='.db_input($setting['id'])))
$setting['value'] = $value;
return true;
}
function updateAll($updates) {
if (!$this->update($key, $value))
$sql='DELETE FROM '.$this->table
.' WHERE `'.$this->section_column.'` = '.db_input($this->section);
db_query($sql);
unset($this->session);
}
}
class OsticketConfig extends Config {
var $table = CONFIG_TABLE;
var $section = 'core';
var $defaultDept; //Default Department
var $defaultSLA; //Default SLA
var $defaultEmail; //Default Email
var $alertEmail; //Alert Email
var $defaultSMTPEmail; //Default SMTP Email
var $defaults = array(
'allow_pw_reset' => true,
'enable_html_thread' => true,
'name_format' => 'full', # First Last
'auto_claim_tickets'=> true,
'message_autoresponder_collabs' => true,
'add_email_collabs' => true,
'clients_only' => false,
'client_registration' => 'closed',
'accept_unregistered_email' => true,
'default_help_topic' => 0,
'help_topic_sort_mode' => 'a',
'client_verify_email' => 1,
function OsticketConfig($section=null) {
parent::Config($section);
if (count($this->config) == 0) {
// Fallback for osticket < 1.7@852ca89e
$sql='SELECT * FROM '.$this->table.' WHERE id = 1';
if (($res=db_query($sql)) && db_num_rows($res))
foreach (db_fetch_array($res) as $key=>$value)
$this->config[$key] = array('value'=>$value);
}
return true;
}
function lastModified($key=false) {
return max(array_map(array('parent', 'lastModified'),
array_keys($this->config)));
}
return !$this->isOnline();
function isHelpDeskOnline() {
return $this->isOnline();
function isOnline() {
function isKnowledgebaseEnabled() {
return ($this->get('enable_kb') && FAQ::countPublishedFAQs());
return THIS_VERSION;
function getSchemaSignature($section=null) {
if ((!$section || $section == $this->section)
&& ($v=$this->get('schema_signature')))
return $v;
// 1.7 after namespaced configuration, other namespace
if ($section) {
$sql='SELECT value FROM '.$this->table
.' WHERE `key` = "schema_signature" and namespace='.db_input($section);
if (($res=db_query($sql, false)) && db_num_rows($res))
return db_result($res);
}
$sql='SELECT `schema_signature` FROM '.$this->table
if (($res=db_query($sql, false)) && db_num_rows($res))
return db_result($res);
Peter Rotich
committed
function getDBTZoffset() {
$sql='SELECT (TIME_TO_SEC(TIMEDIFF(NOW(), UTC_TIMESTAMP()))/3600) as db_tz_offset';
if(($res=db_query($sql)) && db_num_rows($res))
$this->persist('db_tz_offset', db_result($res));
function getDefaultTimezone() {
return $this->get('default_timezone');
}
function getTimezone() {
global $thisstaff, $thisclient;
if ($thisstaff)
$zone = $thisstaff->getTimezone();
#elseif ($thisclient)
# $zone = $thisclient->getTimezone();
else
$zone = $this->get('default_timezone');
if (!$zone)
$zone = ini_get('date.timezone');
return $zone;
}
function getDefaultLocale() {
return $this->get('default_locale');
/* Date & Time Formats */
function getTimeFormat() {
if ($this->get('date_formats') == 'custom')
return $this->get('time_format');
return '';
}
function isForce24HourTime() {
return $this->get('date_formats') == '24';
}
/**
* getDateFormat
*
* Retrieve the current date format for the system, as a string, and in
* the intl (icu) format.
*
* Parameters:
* $propogate - (boolean:default=false), if set and the configuration
* indicates default date and time formats (ie. not custom), then
* the intl date formatter will be queried to find the pattern used
* internally for the current locale settings.
*/
function getDateFormat($propogate=false) {
if ($this->get('date_formats') == 'custom')
return $this->get('date_format');
if ($propogate) {
if (class_exists('IntlDateFormatter')) {
$formatter = new IntlDateFormatter(
Internationalization::getCurrentLocale(),
IntlDateFormatter::SHORT,
IntlDateFormatter::NONE,
$this->getTimezone(),
IntlDateFormatter::GREGORIAN
);
return $formatter->getPattern();
}
else {
// Use a standard
return 'y-M-d';
}
}
return '';
if ($this->get('date_formats') == 'custom')
return $this->get('datetime_format');
return '';
if ($this->get('date_formats') == 'custom')
return $this->get('daydatetime_format');
return '';
function getConfigInfo() {
function getBaseUrl() { //Same as above with no trailing slash.
return rtrim($this->getUrl(),'/');
}
function getPageSize() {
return $this->get('overdue_grace_period');
return $this->get('passwd_reset_period');
function isHtmlThreadEnabled() {
return $this->get('enable_html_thread');
}
function getClientTimeout() {
return $this->getClientSessionTimeout();
}
return $this->get('client_session_timeout')*60;
return $this->get('client_login_timeout')*60;
}
function getStaffTimeout() {
return $this->getStaffSessionTimeout();
}
function getStaffSessionTimeout() {
return $this->get('staff_session_timeout')*60;
return $this->get('staff_login_timeout')*60;
function getDefaultNameFormat() {
return $this->get('name_format');
}
}
function getDefaultDept() {
if(!$this->defaultDept && $this->getDefaultDeptId())
$this->defaultDept=Dept::lookup($this->getDefaultDeptId());
return $this->defaultDept;
}
function getDefaultEmail() {
if(!$this->defaultEmail && $this->getDefaultEmailId())
$this->defaultEmail = Email::lookup($this->getDefaultEmailId());
return $this->defaultEmail;
}
function getDefaultEmailAddress() {
return ($email=$this->getDefaultEmail()) ? $email->getAddress() : null;
function getDefaultTicketStatusId() {
return $this->get('default_ticket_status_id', 1);
}
}
function getDefaultSLA() {
if(!$this->defaultSLA && $this->getDefaultSLAId())
$this->defaultSLA = SLA::lookup($this->getDefaultSLAId());
return $this->defaultSLA;
}
function getAlertEmailId() {
if(!$this->alertEmail)
if(!($this->alertEmail = Email::lookup($this->getAlertEmailId())))
$this->alertEmail = $this->getDefaultEmail();
return $this->alertEmail;
}
function getDefaultSMTPEmail() {
if(!$this->defaultSMTPEmail && $this->get('default_smtp_id'))
$this->defaultSMTPEmail = Email::lookup($this->get('default_smtp_id'));
return $this->defaultSMTPEmail;
}
function getDefaultPriorityId() {
return $this->get('default_priority_id');
function getDefaultPriority() {
if (!isset($this->defaultPriority))
$this->defaultPriority = Priority::lookup($this->getDefaultPriorityId());
return $this->defaultPriority;
}
function getDefaultTopicId() {
return $this->get('default_help_topic');
}
function getDefaultTopic() {
return Topic::lookup($this->getDefaultTopicId());
}
function getTopicSortMode() {
return $this->get('help_topic_sort_mode');
}
function setTopicSortMode($mode) {
$modes = static::allTopicSortModes();
if (!isset($modes[$mode]))
throw new InvalidArgumentException(sprintf(
'%s: Unsupported help topic sort mode', $mode));
$this->update('help_topic_sort_mode', $mode);
}
static function allTopicSortModes() {
return array(
'a' => __('Alphabetically'),
'm' => __('Manually'),
return $this->get('default_template_id');
if(!$this->defaultTemplate && $this->getDefaultTemplateId())
$this->defaultTemplate = EmailTemplateGroup::lookup($this->getDefaultTemplateId());
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
function getLandingPageId() {
return $this->get('landing_page_id');
}
function getLandingPage() {
if(!$this->landing_page && $this->getLandingPageId())
$this->landing_page = Page::lookup($this->getLandingPageId());
return $this->landing_page;
}
function getOfflinePageId() {
return $this->get('offline_page_id');
}
function getOfflinePage() {
if(!$this->offline_page && $this->getOfflinePageId())
$this->offline_page = Page::lookup($this->getOfflinePageId());
return $this->offline_page;
}
function getThankYouPageId() {
return $this->get('thank-you_page_id');
}
function getThankYouPage() {
if(!$this->thankyou_page && $this->getThankYouPageId())
$this->thankyou_page = Page::lookup($this->getThankYouPageId());
return $this->thankyou_page;
}
function getDefaultPages() {
/* Array of ids...as opposed to objects */
return array(
$this->getLandingPageId(),
$this->getOfflinePageId(),
$this->getThankYouPageId(),
);
}
return ($this->get('staff_ip_binding'));
/**
* Configuration: allow_pw_reset
*
* TRUE if the <a>Forgot my password</a> link and system should be
* enabled, and FALSE otherwise.
*/
function allowPasswordReset() {
return $this->get('allow_pw_reset');
}
/**
* Configuration: pw_reset_window
*
* Number of minutes for which the password reset token is valid.
*
* Returns: Number of seconds the password reset token is valid. The
* number of minutes from the database is automatically converted
* to seconds here.
*/
function getPwResetWindow() {
// pw_reset_window is stored in minutes. Return value in seconds
return $this->get('pw_reset_window') * 60;
}
function isClientLoginRequired() {
return $this->get('clients_only');
}
function isClientRegistrationEnabled() {
return in_array($this->getClientRegistrationMode(),
array('public', 'auto'));
}
function getClientRegistrationMode() {
return $this->get('client_registration');
}
function isClientEmailVerificationRequired() {
return $this->get('client_verify_email');
}
function isCaptchaEnabled() {
return (extension_loaded('gd') && function_exists('gd_info') && $this->get('enable_captcha'));
function isAutoCronEnabled() {
return ($this->get('enable_auto_cron'));
function isEmailPollingEnabled() {
return ($this->get('enable_mail_polling'));
return ($this->get('use_email_priority'));
function acceptUnregisteredEmail() {
return $this->get('accept_unregistered_email');
}
function addCollabsViaEmail() {
return ($this->get('add_email_collabs'));
}
return ($this->get('strip_quoted_reply'));
}
function saveEmailHeaders() {
return true; //No longer an option...hint: big plans for headers coming!!
}
function getDefaultSequence() {
if ($this->get('sequence_id'))
$sequence = Sequence::lookup($this->get('sequence_id'));
if (!$sequence)
$sequence = new RandomSequence();
return $sequence;
}
function getDefaultNumberFormat() {
return $this->get('number_format');
}
function getNewTicketNumber() {
$s = $this->getDefaultSequence();
return $s->next($this->getDefaultNumberFormat(),
array('Ticket', 'isTicketNumberUnique'));
}
/* autoresponders & Alerts */
function autoRespONNewTicket() {
return ($this->get('ticket_autoresponder'));
return ($this->get('message_autoresponder'));
function notifyCollabsONNewMessage() {
return ($this->get('message_autoresponder_collabs'));
}
return ($this->get('ticket_notice_active'));
return ($this->get('message_alert_active'));
}
function alertLastRespondentONNewMessage() {
return ($this->get('message_alert_laststaff'));
return ($this->get('message_alert_assigned'));
return ($this->get('message_alert_dept_manager'));
function alertAcctManagerONNewMessage() {
return ($this->get('message_alert_acct_manager'));
}
return ($this->get('note_alert_active'));
}
function alertLastRespondentONNewNote() {
return ($this->get('note_alert_laststaff'));
return ($this->get('note_alert_assigned'));
}
function alertDeptManagerONNewNote() {
return ($this->get('note_alert_dept_manager'));
return ($this->get('ticket_alert_active'));
return ($this->get('ticket_alert_admin'));
return ($this->get('ticket_alert_dept_manager'));
}
function alertDeptMembersONNewTicket() {
return ($this->get('ticket_alert_dept_members'));
function alertAcctManagerONNewTicket() {
return ($this->get('ticket_alert_acct_manager'));
}
return ($this->get('transfer_alert_active'));
}
function alertAssignedONTransfer() {
return ($this->get('transfer_alert_assigned'));
return ($this->get('transfer_alert_dept_manager'));
return ($this->get('transfer_alert_dept_members'));
return ($this->get('assigned_alert_active'));
return ($this->get('assigned_alert_staff'));
}
function alertTeamLeadONAssignment() {
return ($this->get('assigned_alert_team_lead'));
}
function alertTeamMembersONAssignment() {
return ($this->get('assigned_alert_team_members'));
}
function alertONOverdueTicket() {
return ($this->get('overdue_alert_active'));
}
function alertAssignedONOverdueTicket() {
return ($this->get('overdue_alert_assigned'));
}
function alertDeptManagerONOverdueTicket() {
return ($this->get('overdue_alert_dept_manager'));
}
function alertDeptMembersONOverdueTicket() {
return ($this->get('overdue_alert_dept_members'));
function autoClaimTickets() {
return $this->get('auto_claim_tickets');
return ($this->get('show_assigned_tickets'));
return ($this->get('show_answered_tickets'));
return ($this->get('overlimit_notice_active'));
/* Error alerts sent to admin email when enabled */
function alertONSQLError() {
return ($this->get('send_login_errors'));
function getAllowedFileTypes() {
return trim($this->get('allowed_filetypes'));
return ($this->get('email_attachments'));
return ($this->get('allow_attachments'));
function getPrimaryLanguage() {
return $this->get('system_language');
}
function getSecondaryLanguages() {
$langs = $this->get('secondary_langs');
$langs = (is_string($langs)) ? explode(',', $langs) : array();
return array_filter($langs);
/* Needed by upgrader on 1.6 and older releases upgrade - not not remove */
function getDefaultStorageBackendChar() {
return $this->get('default_storage_bk');
}
function getVar($name) {
return $this->get($name);
}
function updateSettings($vars, &$errors) {
case 'system':
return $this->updateSystemSettings($vars, $errors);
return $this->updateTicketsSettings($vars, $errors);
return $this->updateEmailsSettings($vars, $errors);
case 'pages':
return $this->updatePagesSettings($vars, $errors);
break;
case 'access':
return $this->updateAccessSettings($vars, $errors);
break;
case 'autoresp':
return $this->updateAutoresponderSettings($vars, $errors);
return $this->updateAlertsSettings($vars, $errors);
return $this->updateKBSettings($vars, $errors);
$errors['err']=__('Unknown setting option. Get technical support.');
function updateSystemSettings($vars, &$errors) {
$f['helpdesk_url']=array('type'=>'string', 'required'=>1, 'error'=>__('Helpdesk URL is required'));
$f['helpdesk_title']=array('type'=>'string', 'required'=>1, 'error'=>__('Helpdesk title is required'));
$f['default_dept_id']=array('type'=>'int', 'required'=>1, 'error'=>__('Default Department is required'));
$f['time_format']=array('type'=>'string', 'required'=>1, 'error'=>__('Time format is required'));
$f['date_format']=array('type'=>'string', 'required'=>1, 'error'=>__('Date format is required'));
$f['datetime_format']=array('type'=>'string', 'required'=>1, 'error'=>__('Datetime format is required'));
$f['daydatetime_format']=array('type'=>'string', 'required'=>1, 'error'=>__('Day, Datetime format is required'));
$f['default_timezone']=array('type'=>'string', 'required'=>1, 'error'=>__('Default Timezone is required'));
$f['system_language']=array('type'=>'string', 'required'=>1, 'error'=>__('A primary system language is required'));
if(!Validator::process($f, $vars, $errors) || $errors)
// Manage secondard languages
$vars['secondary_langs'][] = $vars['add_secondary_language'];
foreach ($vars['secondary_langs'] as $i=>$lang) {
if (!$lang || !Internationalization::isLanguageInstalled($lang))
unset($vars['secondary_langs'][$i]);
}
$secondary_langs = implode(',', $vars['secondary_langs']);
return $this->updateAll(array(
'isonline'=>$vars['isonline'],
'helpdesk_title'=>$vars['helpdesk_title'],
'helpdesk_url'=>$vars['helpdesk_url'],
'default_dept_id'=>$vars['default_dept_id'],
'max_page_size'=>$vars['max_page_size'],
'log_level'=>$vars['log_level'],
'log_graceperiod'=>$vars['log_graceperiod'],
'name_format'=>$vars['name_format'],
'time_format'=>$vars['time_format'],
'date_format'=>$vars['date_format'],
'datetime_format'=>$vars['datetime_format'],
'daydatetime_format'=>$vars['daydatetime_format'],
'date_formats'=>$vars['date_formats'],
'default_timezone'=>$vars['default_timezone'],
'default_locale'=>$vars['default_locale'],
'system_language'=>$vars['system_language'],
'secondary_langs'=>$secondary_langs,
));
}
function updateAccessSettings($vars, &$errors) {
$f=array();
$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['pw_reset_window']=array('type'=>'int', 'required'=>1, 'min'=>1,
'error'=>__('Valid password reset window required'));
if(!Validator::process($f, $vars, $errors) || $errors)
return false;
return $this->updateAll(array(
'passwd_reset_period'=>$vars['passwd_reset_period'],
'staff_max_logins'=>$vars['staff_max_logins'],
'staff_login_timeout'=>$vars['staff_login_timeout'],
'staff_session_timeout'=>$vars['staff_session_timeout'],
'staff_ip_binding'=>isset($vars['staff_ip_binding'])?1:0,
'client_max_logins'=>$vars['client_max_logins'],
'client_login_timeout'=>$vars['client_login_timeout'],
'client_session_timeout'=>$vars['client_session_timeout'],
'allow_pw_reset'=>isset($vars['allow_pw_reset'])?1:0,
'pw_reset_window'=>$vars['pw_reset_window'],
'clients_only'=>isset($vars['clients_only'])?1:0,
'client_registration'=>$vars['client_registration'],
'client_verify_email'=>isset($vars['client_verify_email'])?1:0,
function updateTicketsSettings($vars, &$errors) {
$f['default_sla_id']=array('type'=>'int', 'required'=>1, 'error'=>__('Selection required'));
$f['default_ticket_status_id'] = array('type'=>'int', 'required'=>1, 'error'=>__('Selection required'));
$f['default_priority_id']=array('type'=>'int', 'required'=>1, 'error'=>__('Selection required'));
$f['max_open_tickets']=array('type'=>'int', 'required'=>1, 'error'=>__('Enter valid numeric value'));
$f['autolock_minutes']=array('type'=>'int', 'required'=>1, 'error'=>__('Enter lock time in minutes'));
if($vars['enable_captcha']) {
if (!extension_loaded('gd'))
$errors['enable_captcha']=__('The GD extension is required');
$errors['enable_captcha']=__('PNG support is required for Image Captcha');
if ($vars['default_help_topic']
&& ($T = Topic::lookup($vars['default_help_topic']))
&& !$T->isActive()) {
$errors['default_help_topic'] = __('Default help topic must be set to active');
if (!preg_match('`(?!<\\\)#`', $vars['number_format']))
$errors['number_format'] = 'Ticket number format requires at least one hash character (#)';
if(!Validator::process($f, $vars, $errors) || $errors)
if (isset($vars['default_storage_bk']))
$this->update('default_storage_bk', $vars['default_storage_bk']);