bludit/bl-kernel/abstract/dbjson.class.php

113 lines
2.3 KiB
PHP
Raw Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
2018-07-25 23:42:00 +02:00
class dbJSON {
2015-05-05 03:00:01 +02:00
public $db;
2015-08-31 03:18:06 +02:00
public $dbBackup;
2015-05-05 03:00:01 +02:00
public $file;
public $firstLine;
2015-07-14 04:16:28 +02:00
// $file, the JSON file.
2018-07-25 23:42:00 +02:00
// $firstLine, TRUE if you want to remove the first line, FALSE otherwise
2015-05-05 03:00:01 +02:00
function __construct($file, $firstLine=true)
{
$this->file = $file;
$this->db = array();
2015-08-31 03:18:06 +02:00
$this->dbBackup = array();
2015-05-05 03:00:01 +02:00
$this->firstLine = $firstLine;
2018-07-25 23:42:00 +02:00
if (file_exists($file)) {
// Read JSON file
2015-05-05 03:00:01 +02:00
$lines = file($file);
2018-07-25 23:42:00 +02:00
// Remove the first line, the first line is for security reasons
if ($firstLine) {
2015-05-05 03:00:01 +02:00
unset($lines[0]);
}
2018-07-25 23:42:00 +02:00
// Regenerate the JSON file
2015-05-05 03:00:01 +02:00
$implode = implode($lines);
2018-07-25 23:42:00 +02:00
// Unserialize, JSON to Array
2015-07-20 05:14:12 +02:00
$array = $this->unserialize($implode);
2018-07-25 23:42:00 +02:00
if (empty($array)) {
$this->db = array();
$this->dbBackup = array();
} else {
2015-07-20 05:14:12 +02:00
$this->db = $array;
2015-08-31 03:18:06 +02:00
$this->dbBackup = $array;
2015-07-20 05:14:12 +02:00
}
2015-05-05 03:00:01 +02:00
}
}
public function restoreDB()
2015-08-31 03:18:06 +02:00
{
$this->db = $this->dbBackup;
return true;
}
2018-07-25 23:42:00 +02:00
// Returns the amount of rows in the database
2015-06-26 06:31:53 +02:00
public function count()
{
return count($this->db);
}
2018-07-25 23:42:00 +02:00
// Returns the value from the field
public function getField($field)
{
if (isset($this->db[$field])) {
return $this->db[$field];
}
return $this->dbFields[$field]['value'];
}
2018-07-25 23:42:00 +02:00
// Save the JSON file
2015-05-05 03:00:01 +02:00
public function save()
{
2015-09-02 02:42:21 +02:00
$data = '';
2018-07-25 23:42:00 +02:00
if ($this->firstLine) {
2015-05-05 03:00:01 +02:00
$data = "<?php defined('BLUDIT') or die('Bludit CMS.'); ?>".PHP_EOL;
2015-07-14 04:16:28 +02:00
}
2015-05-05 03:00:01 +02:00
2015-09-02 02:42:21 +02:00
// Serialize database
2015-05-05 03:00:01 +02:00
$data .= $this->serialize($this->db);
2015-09-02 02:42:21 +02:00
// Backup the new database.
$this->dbBackup = $this->db;
2015-05-05 03:00:01 +02:00
// LOCK_EX flag to prevent anyone else writing to the file at the same time.
2018-07-25 23:42:00 +02:00
if (file_put_contents($this->file, $data, LOCK_EX)) {
return true;
2018-07-25 23:42:00 +02:00
} else {
Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.', LOG_TYPE_ERROR);
return false;
}
2015-05-05 03:00:01 +02:00
}
2018-07-25 23:42:00 +02:00
// Returns a JSON encoded string on success or FALSE on failure
2015-05-05 03:00:01 +02:00
private function serialize($data)
{
2018-07-25 23:42:00 +02:00
if (DEBUG_MODE) {
return json_encode($data, JSON_PRETTY_PRINT);
}
return json_encode($data);
2015-05-05 03:00:01 +02:00
}
2018-07-25 23:42:00 +02:00
// Returns the value encoded in json in appropriate PHP type
2015-05-05 03:00:01 +02:00
private function unserialize($data)
{
2018-07-25 23:42:00 +02:00
// NULL is returned if the json cannot be decoded
2016-01-21 02:46:13 +01:00
$decode = json_decode($data, true);
2018-07-25 23:42:00 +02:00
if (empty($decode)) {
2016-01-21 02:46:13 +01:00
return false;
2015-07-14 04:16:28 +02:00
}
2016-01-21 02:46:13 +01:00
return $decode;
2015-05-05 03:00:01 +02:00
}
2018-07-25 23:42:00 +02:00
public function getDB()
{
return $this->db;
}
2016-01-21 02:46:13 +01:00
}