Newer
Older
Peter Rotich
committed
<?php
/*******************************************************************
Peter Rotich
committed
class.osticket.php
osTicket (sys) -> Config.
Core osTicket object: loads congfig and provides loggging facility.
Peter Rotich
committed
Use osTicket::start(configId)
Peter Rotich <peter@osticket.com>
Peter Rotich
committed
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:
**********************************************************************/
require_once(INCLUDE_DIR.'class.csrf.php'); //CSRF token class.
require_once(INCLUDE_DIR.'class.migrater.php');
require_once INCLUDE_DIR . 'class.message.php';
Peter Rotich
committed
define('LOG_WARN',LOG_WARNING);
class osTicket {
Peter Rotich
committed
var $loglevel=array(1=>'Error','Warning','Debug');
Peter Rotich
committed
var $errors;
//System
var $system;
Peter Rotich
committed
var $warning;
var $message;
var $title; //Custom title. html > head > title.
Peter Rotich
committed
var $headers;
Peter Rotich
committed
var $config;
Peter Rotich
committed
var $session;
Peter Rotich
committed
require_once(INCLUDE_DIR.'class.config.php'); //Config helper
require_once(INCLUDE_DIR.'class.company.php');
if (!defined('DISABLE_SESSION') || !DISABLE_SESSION)
$this->session = osTicketSession::start(SESSION_TTL); // start DB based session
Peter Rotich
committed
Peter Rotich
committed
$this->csrf = new CSRF('__CSRFToken__');
}
function isSystemOnline() {
return ($this->getConfig() && $this->getConfig()->isHelpDeskOnline() && !$this->isUpgradePending());
}
function isUpgradePending() {
foreach (DatabaseMigrater::getUpgradeStreams(UPGRADE_DIR.'streams/') as $stream=>$hash)
if (strcasecmp($hash,
$this->getConfig()->getSchemaSignature($stream)))
return true;
return false;
Peter Rotich
committed
}
function getSession() {
return $this->session;
}
function getConfig() {
return $this->config;
}
function getDBSignature($namespace='core') {
return $this->getConfig()->getSchemaSignature($namespace);
}
function getVersion() {
return THIS_VERSION;
}
function getCSRF(){
return $this->csrf;
}
function getCSRFToken() {
return $this->getCSRF()->getToken();
}
function getCSRFFormInput() {
return $this->getCSRF()->getFormInput();
}
function validateCSRFToken($token) {
return ($token && $this->getCSRF()->validateToken($token));
}
function checkCSRFToken($name=false) {
$name = $name ?: $this->getCSRF()->getTokenName();
if(isset($_POST[$name]) && $this->validateCSRFToken($_POST[$name]))
return true;
if(isset($_SERVER['HTTP_X_CSRFTOKEN']) && $this->validateCSRFToken($_SERVER['HTTP_X_CSRFTOKEN']))
return true;
$msg=sprintf(__('Invalid CSRF token [%1$s] on %2$s'),
($_POST[$name].''.$_SERVER['HTTP_X_CSRFTOKEN']), THISPAGE);
$this->logWarning(__('Invalid CSRF Token').' '.$name, $msg, false);
return false;
}
function getLinkToken() {
return md5($this->getCSRFToken().SECRET_SALT.session_id());
}
function validateLinkToken($token) {
return ($token && !strcasecmp($token, $this->getLinkToken()));
}
/* Replace Template Variables */
function replaceTemplateVariables($input, $vars=array()) {
$replacer->assign(array_merge($vars,
array('url' => $this->getConfig()->getBaseUrl(),
'company' => $this->company)
));
return $replacer->replaceVars($input);
}
static function getVarScope() {
return array(
'url' => __("osTicket's base url (FQDN)"),
'company' => array('class' => 'Company', 'desc' => __('Company Information')),
);
}
function addExtraHeader($header, $pjax_script=false) {
Peter Rotich
committed
$this->headers[md5($header)] = $header;
$this->pjax_extra[md5($header)] = $pjax_script;
Peter Rotich
committed
}
function getExtraHeaders() {
return $this->headers;
}
function getExtraPjax() {
return $this->pjax_extra;
}
Peter Rotich
committed
function setPageTitle($title) {
$this->title = $title;
}
function getPageTitle() {
return $this->title;
}
Peter Rotich
committed
function getErrors() {
return $this->errors;
}
function setErrors($errors) {
$this->errors = $errors;
}
function getError() {
return $this->system['err'];
Peter Rotich
committed
}
function setError($error) {
$this->system['error'] = $error;
Peter Rotich
committed
}
function clearError() {
$this->setError('');
}
function getWarning() {
return $this->system['warning'];
Peter Rotich
committed
}
function setWarning($warning) {
$this->system['warning'] = $warning;
Peter Rotich
committed
}
function clearWarning() {
$this->setWarning('');
}
function getNotice() {
return $this->system['notice'];
Peter Rotich
committed
}
function setNotice($notice) {
$this->system['notice'] = $notice;
Peter Rotich
committed
}
function clearNotice() {
$this->setNotice('');
Peter Rotich
committed
}
function alertAdmin($subject, $message, $log=false) {
Peter Rotich
committed
//Set admin's email address
if (!($to = $this->getConfig()->getAdminEmail()))
$to = ADMIN_EMAIL;
//append URL to the message
$message.="\n\n".$this->getConfig()->getBaseUrl();
Peter Rotich
committed
//Try getting the alert email.
$email=null;
if(!($email=$this->getConfig()->getAlertEmail()))
$email=$this->getConfig()->getDefaultEmail(); //will take the default email.
Peter Rotich
committed
if($email) {
$email->sendAlert($to, $subject, $message, null, array('text'=>true, 'reply-tag'=>false));
Peter Rotich
committed
} else {//no luck - try the system mail.
Mailer::sendmail($to, $subject, $message, '"'.__('osTicket Alerts').sprintf('" <%s>',$to));
Peter Rotich
committed
}
//log the alert? Watch out for loops here.
if($log)
$this->log(LOG_CRIT, $subject, $message, false); //Log the entry...and make sure no alerts are resent.
}
function logDebug($title, $message, $force=false) {
return $this->log(LOG_DEBUG, $title, $message, false, $force);
Peter Rotich
committed
}
function logInfo($title, $message, $alert=false) {
return $this->log(LOG_INFO, $title, $message, $alert);
}
function logWarning($title, $message, $alert=true) {
return $this->log(LOG_WARN, $title, $message, $alert);
}
Peter Rotich
committed
function logError($title, $error, $alert=true) {
return $this->log(LOG_ERR, $title, $error, $alert);
}
Peter Rotich
committed
function logDBError($title, $error, $alert=true) {
if($alert && !$this->getConfig()->alertONSQLError())
$alert =false;
$bt = str_replace(ROOT_DIR, _S(/* `root` is a root folder */ '(root)').'/',
$e->getTraceAsString());
$error .= nl2br("\n\n---- "._S('Backtrace')." ----\n".$bt);
// Prevent recursive loops through this code path
if (substr_count($bt, __FUNCTION__) > 1)
return;
Peter Rotich
committed
return $this->log(LOG_ERR, $title, $error, $alert);
}
function log($priority, $title, $message, $alert=false, $force=false) {
Peter Rotich
committed
//We are providing only 3 levels of logs. Windows style.
switch($priority) {
case LOG_EMERG:
case LOG_ALERT:
case LOG_CRIT:
Peter Rotich
committed
case LOG_ERR:
$level=1; //Error
break;
case LOG_WARN:
case LOG_WARNING:
$level=2; //Warning
break;
case LOG_NOTICE:
case LOG_INFO:
case LOG_DEBUG:
default:
$level=3; //Debug
}
$loglevel=array(1=>'Error','Warning','Debug');
$info = array(
'title' => &$title,
'level' => $loglevel[$level],
'level_id' => $level,
'body' => &$message,
);
Signal::send('syslog', null, $info);
Peter Rotich
committed
//Logging everything during upgrade.
if($this->getConfig()->getLogLevel()<$level && !$force)
Peter Rotich
committed
return false;
$alert = $alert && !$this->isUpgradePending();
if ($alert && $this->getConfig()->getLogLevel() >= $level)
$this->alertAdmin($title, $message);
Peter Rotich
committed
//Save log based on system log level settings.
$sql='INSERT INTO '.SYSLOG_TABLE.' SET created=NOW(), updated=NOW() '
.',title='.db_input(Format::sanitize($title, true))
.',log_type='.db_input($loglevel[$level])
.',log='.db_input(Format::sanitize($message, false))
.',ip_address='.db_input($_SERVER['REMOTE_ADDR']);
Peter Rotich
committed
db_query($sql, false);
Peter Rotich
committed
return true;
}
function purgeLogs() {
if(!($gp=$this->getConfig()->getLogGracePeriod()) || !is_numeric($gp))
Peter Rotich
committed
return false;
//System logs
$sql='DELETE FROM '.SYSLOG_TABLE.' WHERE DATE_ADD(created, INTERVAL '.$gp.' MONTH)<=NOW()';
db_query($sql);
Peter Rotich
committed
//TODO: Activity logs
return true;
}
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
/*
* Util functions
*
*/
function get_var($index, $vars, $default='', $type=null) {
if(is_array($vars)
&& array_key_exists($index, $vars)
&& (!$type || gettype($vars[$index])==$type))
return $vars[$index];
return $default;
}
function get_db_input($index, $vars, $quote=true) {
return db_input($this->get_var($index, $vars), $quote);
}
function get_path_info() {
if(isset($_SERVER['PATH_INFO']))
return $_SERVER['PATH_INFO'];
if(isset($_SERVER['ORIG_PATH_INFO']))
return $_SERVER['ORIG_PATH_INFO'];
//TODO: conruct possible path info.
return null;
}
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
/**
* Fetch the current version(s) of osTicket softwares via DNS. The
* constants of MAJOR_VERSION, THIS_VERSION, and GIT_VERSION will be
* consulted to arrive at the most relevant version code for the latest
* release.
*
* Parameters:
* $product - (string|default:'core') the product to fetch versions for
* $major - (string|optional) optional major version to compare. This is
* useful if more than one version is available. Only versions
* specifying this major version ('m') are considered as version
* candidates.
*
* Dns:
* The DNS zone will have TXT records for the product will be published
* in this format:
*
* "v=1; m=1.9; V=1.9.11; c=deadbeef"
*
* Where the string is a semicolon-separated string of key/value pairs
* with the following meanings:
*
* --+--------------------------
* v | DNS record format version
*
* For v=1, this is the meaning of the other keys
* --+-------------------------------------------
* m | (optional) major product version
* V | Full product version (usually a git tag)
* c | Git commit id of the release tag
* s | Schema signature of the version, which might help detect
* | required migration
*
* Returns:
* (string|bool|null)
* - 'v1.9.11' or 'deadbeef' if release tag or git commit id seems to
* be most appropriate based on the value of GIT_VERSION
* - null if the $major version is no longer supported
* - false if no information is available in DNS
*/
function getLatestVersion($product='core', $major=null) {
$records = dns_get_record($product.'.updates.osticket.com', DNS_TXT);
if (!$records)
return false;
$versions = array();
foreach ($records as $r) {
$txt = $r['txt'];
$info = array();
foreach (explode(';', $r['txt']) as $kv) {
list($k, $v) = explode('=', $kv);
if (!($k = trim($k)))
continue;
$info[$k] = trim($v);
}
$versions[] = $info;
}
foreach ($versions as $info) {
switch ($info['v']) {
case '1':
if ($major && $info['m'] && $info['m'] != $major)
continue;
if ($product == 'core' && GIT_VERSION == '$git')
return $info['c'];
return $info['V'];
}
}
}
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
/*
* getTrustedProxies
*
* Get defined trusted proxies
*/
static function getTrustedProxies() {
static $proxies = null;
// Parse trusted proxies from config file
if (!isset($proxies) && defined('TRUSTED_PROXIES'))
$proxies = array_filter(
array_map('trim', explode(',', TRUSTED_PROXIES)));
return $proxies ?: array();
}
/*
* getLocalNetworkAddresses
*
* Get defined local network addresses
*/
static function getLocalNetworkAddresses() {
static $ips = null;
// Parse local addreses from config file
if (!isset($ips) && defined('LOCAL_NETWORKS'))
$ips = array_filter(
array_map('trim', explode(',', LOCAL_NETWORKS)));
return $ips ?: array();
}
/* If run from the commandline, DOCUMENT_ROOT will not be set. It is
* also likely that the ROOT_PATH will not be necessary, so don't
* bother attempting to figure it out.
*
* Secondly, if the directory of main.inc.php is the same as the
* document root, the the ROOT path truly is '/'
*/
|| !strcasecmp($_SERVER['DOCUMENT_ROOT'], $dir))
return '/';
/* The main idea is to try and use full-path filename of PHP_SELF and
* SCRIPT_NAME. The SCRIPT_NAME should be the path of that script
* inside the DOCUMENT_ROOT. This is most likely useful if osTicket
* is run using something like Apache UserDir setting where the
* DOCUMENT_ROOT of Apache and the installation path of osTicket
* have nothing in comon.
*
* +---------------------------+-------------------+----------------+
* +---------------------------+-------------------+----------------+
* | /home/u1/www/osticket/... | /~u1/osticket/... | /~u1/osticket/ |
* +---------------------------+-------------------+----------------+
*
* The algorithm will remove the directory of main.inc.php from
* as seen. What's left should be the script executed inside
* the osTicket installation. That is removed from SCRIPT_NAME.
* What's left is the ROOT_PATH.
*/
$bt = debug_backtrace(false);
$frame = array_pop($bt);
$file = str_replace('\\','/', $frame['file']);
$path = substr($file, strlen(ROOT_DIR));
if($path && ($pos=strpos($_SERVER['SCRIPT_NAME'], $path))!==false)
return ($pos) ? substr($_SERVER['SCRIPT_NAME'], 0, $pos) : '/';
if (self::is_cli())
return '/';
return null;
}
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
/*
* get_client_ip
*
* Get client IP address from "Http_X-Forwarded-For" header by following a
* chain of trusted proxies.
*
* "Http_X-Forwarded-For" header value is a comma+space separated list of IP
* addresses, the left-most being the original client, and each successive
* proxy that passed the request all the way to the originating IP address.
*
*/
static function get_client_ip($header='HTTP_X_FORWARDED_FOR') {
// Request IP
$ip = $_SERVER['REMOTE_ADDR'];
// Trusted proxies.
$proxies = self::getTrustedProxies();
// Return current IP address if header is not set and
// request is not from a trusted proxy.
if (!isset($_SERVER[$header])
|| !$proxies
|| !self::is_trusted_proxy($ip, $proxies))
return $ip;
// Get chain of proxied ip addresses
$ips = array_map('trim', explode(',', $_SERVER[$header]));
// Add request IP to the chain
$ips[] = $ip;
// Walk the chain in reverse - remove invalid IPs
$ips = array_reverse($ips);
foreach ($ips as $k => $ip) {
// Make sure the IP is valid and not a trusted proxy
if ($k && !Validator::is_ip($ip))
unset($ips[$k]);
elseif ($k && !self::is_trusted_proxy($ip, $proxies))
return $ip;
}
// We trust the 400 lb hacker... return left most valid IP
return array_pop($ips);
}
/*
* Checks if the IP is that of a trusted proxy
*
*/
static function is_trusted_proxy($ip, $proxies=array()) {
$proxies = $proxies ?: self::getTrustedProxies();
// We don't have any proxies set.
if (!$proxies)
return false;
// Wildcard set - trust all proxies
else if ($proxies == '*')
return true;
return ($proxies && Validator::check_ip($ip, $proxies));
}
/**
* is_local_ip
*
* Check if a given IP is part of defined local address blocks
*
*/
static function is_local_ip($ip, $ips=array()) {
$ips = $ips
?: self::getLocalNetworkAddresses()
?: array();
foreach ($ips as $addr) {
if (Validator::check_ip($ip, $addr))
return true;
}
return false;
}
/**
* Returns TRUE if the request was made via HTTPS and false otherwise
*/
function is_https() {
// Local server flags
if (isset($_SERVER['HTTPS'])
&& strtolower($_SERVER['HTTPS']) == 'on')
return true;
// Check if SSL was terminated by a loadbalancer
return (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
&& !strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https'));
/* returns true if script is being executed via commandline */
return (!strcasecmp(substr(php_sapi_name(), 0, 3), 'cli')
|| (!isset($_SERVER['REQUEST_METHOD']) &&
!isset($_SERVER['HTTP_HOST']))
//Fallback when php-cgi binary is used via cli
Peter Rotich
committed
/**** static functions ****/
// Prep basic translation support
Internationalization::bootstrap();
Peter Rotich
committed
Peter Rotich
committed
return null;
// Bootstrap installed plugins
$ost->plugins->bootstrap();
// Mirror content updates to the search backend
$ost->searcher = new SearchInterface();
Peter Rotich
committed
return $ost;
}
}
?>