This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/chatbox/php/filestorage.class.php

85 lines
1.5 KiB
PHP
Raw Normal View History

<?php
2013-06-19 19:59:59 +00:00
class FileStorage {
public $shoutLog, $path, $handle;
2013-06-19 19:59:59 +00:00
function FileStorage($path, $shoutLog = false) {
$this->shoutLog = $shoutLog;
$folder = 'logs';
if (!is_dir($folder)) $folder = '../' . $folder;
if (!is_dir($folder)) $folder = '../' . $folder;
$this->path = $folder . '/' . $path . '.txt';
}
function open($lock = false) {
$this->handle = fopen($this->path, 'a+');
if ($lock) {
$this->lock();
return $this->load();
}
}
function close(&$array) {
if (isset($array))
$this->save($array);
$this->unlock();
fclose($this->handle);
unset($this->handle);
}
function load() {
if (($contents = $this->read($this->path)) == null)
return $this->resetArray();
return unserialize($contents);
}
function save(&$array, $unlock = true) {
$contents = serialize($array);
$this->write($contents);
if ($unlock) $this->unlock();
}
function unlock() {
if (isset($this->handle))
flock($this->handle, LOCK_UN);
}
function lock() {
if (isset($this->handle))
flock($this->handle, LOCK_EX);
}
function read() {
fseek($this->handle, 0);
//return stream_get_contents($this->handle);
return file_get_contents($this->path);
}
function write($contents) {
ftruncate($this->handle, 0);
fwrite($this->handle, $contents);
}
function resetArray() {
if ($this->shoutLog)
$default = array(
'info' => array(
'latestTimestamp' => -1
),
'posts' => array()
);
else
$default = array();
$this->save($default, false);
return $default;
}
}