diff --git a/include/class.config.php b/include/class.config.php index 924f0c012cd705050803eabcc3fc60f013617549..6835a2a457e98170ef7fec090c915bed0ee49ffe 100644 --- a/include/class.config.php +++ b/include/class.config.php @@ -98,7 +98,7 @@ class Config { } function create($key, $value) { - $item = ConfigItem::create([ + $item = new ConfigItem([ $this->section_column => $this->section, 'key' => $key, 'value' => $value, @@ -215,9 +215,10 @@ class OsticketConfig extends Config { if (count($this->config) == 0) { // Fallback for osticket < 1.7@852ca89e $sql='SELECT * FROM '.$this->table.' WHERE id = 1'; + $meta = ConfigItem::getMeta(); if (($res=db_query($sql)) && db_num_rows($res)) foreach (db_fetch_array($res) as $key=>$value) - $this->config[$key] = new ConfigItem(array('value'=>$value)); + $this->config[$key] = $meta->newInstance(array('value'=>$value)); } return true; diff --git a/include/class.forms.php b/include/class.forms.php index 479d5f2ba8f2971f8b5d367afd8dc9b7662b337f..e7558c7d444d5b425909fc103356c53020db93c3 100644 --- a/include/class.forms.php +++ b/include/class.forms.php @@ -2509,14 +2509,14 @@ class FileUploadField extends FormField { static $filetypes; if (!isset($filetypes)) { - if (function_exists('apc_fetch')) { + if (function_exists('apcu_fetch')) { $key = md5(SECRET_SALT . GIT_VERSION . 'filetypes'); - $filetypes = apc_fetch($key); + $filetypes = apcu_fetch($key); } if (!$filetypes) $filetypes = YamlDataParser::load(INCLUDE_DIR . '/config/filetype.yaml'); if ($key) - apc_store($key, $filetypes, 7200); + apcu_store($key, $filetypes, 7200); } return $filetypes; } diff --git a/include/class.orm.php b/include/class.orm.php index 8764ba73f1021ac8115f27edad7dc486ce73502c..e0120814d3a72170c4412c8fa59b7b067204778b 100644 --- a/include/class.orm.php +++ b/include/class.orm.php @@ -258,7 +258,7 @@ class ModelMeta implements ArrayAccess { self::$model_cache = function_exists('apcu_fetch'); if (self::$model_cache) { $key = SECRET_SALT.GIT_VERSION."/orm/{$this['table']}"; - if ($fields = apc_fetch($key)) { + if ($fields = apcu_fetch($key)) { return $fields; } } diff --git a/include/class.ostsession.php b/include/class.ostsession.php index e6b57a916e7e31177cd43da13c47b9196fae8166..8581965eb5f538e343875a0e0ea2df94a02748c2 100644 --- a/include/class.ostsession.php +++ b/include/class.ostsession.php @@ -164,60 +164,62 @@ abstract class SessionBackend { abstract function gc($maxlife); } +class SessionData +extends VerySimpleModel { + static $meta = array( + 'table' => SESSION_TABLE, + 'pk' => array('session_id'), + ); +} + class DbSessionBackend extends SessionBackend { - - function read($id){ - $this->isnew = false; - if (!$this->data || $this->id != $id) { - $sql='SELECT session_data FROM '.SESSION_TABLE - .' WHERE session_id='.db_input($id) - .' AND session_expire>NOW()'; - if(!($res=db_query($sql))) - return false; - elseif (db_num_rows($res)) - list($this->data)=db_fetch_row($res); - else - // No session data on record -- new session - $this->isnew = true; + function read($id) { + try { + $this->data = SessionData::objects()->filter([ + 'session_id' => $id, + 'session_expire__gt' => SqlFunction::NOW(), + ])->one(); $this->id = $id; } - $this->data_hash = md5($id.$this->data); - return $this->data; + catch (DoesNotExist $e) { + $this->data = new SessionData(['session_id' => $id]); + } + catch (OrmException $e) { + return false; + } + return $this->data->session_data; } function update($id, $data){ global $thisstaff; - if (md5($id.$data) == $this->data_hash) - return; - - elseif (defined('DISABLE_SESSION') && $this->isnew) - return; + if (defined('DISABLE_SESSION') && $this->data->__new__) + return true; - $ttl = ($this && get_class($this) == 'osTicketSession') + $ttl = $this && method_exists($this, 'getTTL') ? $this->getTTL() : SESSION_TTL; - $sql='REPLACE INTO '.SESSION_TABLE.' SET session_updated=NOW() '. - ',session_id='.db_input($id). - ',session_data=0x'.bin2hex($data). - ',session_expire=(NOW() + INTERVAL '.$ttl.' SECOND)'. - ',user_id='.db_input($thisstaff?$thisstaff->getId():0). - ',user_ip='.db_input($_SERVER['REMOTE_ADDR']). - ',user_agent='.db_input($_SERVER['HTTP_USER_AGENT']); + assert($this->data->session_id == $id); + + $this->data->session_data = $data; + $this->data->session_expire = + SqlFunction::NOW()->plus(SqlInterval::SECOND($ttl)); + $this->data->user_id = $thisstaff ? $thisstaff->getId() : 0; + $this->data->user_ip = $_SERVER['REMOTE_ADDR']; + $this->data->user_agent = $_SERVER['HTTP_USER_AGENT']; - $this->data = ''; - return (db_query($sql) && db_affected_rows()); + return $this->data->save(); } function destroy($id){ - $sql='DELETE FROM '.SESSION_TABLE.' WHERE session_id='.db_input($id); - return (db_query($sql) && db_affected_rows()); + return SessionData::objects()->filter(['session_id' => $id])->delete(); } function gc($maxlife){ - $sql='DELETE FROM '.SESSION_TABLE.' WHERE session_expire<NOW()'; - db_query($sql); + SessionData::objects()->filter([ + 'session_expire__lte' => SqlFunction::NOW() + ])->delete(); } } diff --git a/include/class.translation.php b/include/class.translation.php index 711fe1d0cd66fc656623e3fc2d9066f800598948..2e864b39383fd0dc26b4244eb4f68e304eb532d8 100644 --- a/include/class.translation.php +++ b/include/class.translation.php @@ -651,16 +651,16 @@ class Translation extends gettext_reader implements Serializable { } static function resurrect($key) { - if (!function_exists('apc_fetch')) + if (!function_exists('apcu_fetch')) return false; $success = true; - if (($translation = apc_fetch($key, $success)) && $success) + if (($translation = apcu_fetch($key, $success)) && $success) return $translation; } function cache($key) { - if (function_exists('apc_add')) - apc_add($key, $this); + if (function_exists('apcu_add')) + apcu_add($key, $this); }