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

107 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2015-05-05 03:00:01 +02:00
<?php defined('BLUDIT') or die('Bludit CMS.');
class dbJSON
{
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.
// $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;
if(file_exists($file))
{
2015-07-14 04:16:28 +02:00
// Read JSON file.
2015-05-05 03:00:01 +02:00
$lines = file($file);
2015-07-14 04:16:28 +02:00
// Remove the first line, the first line is for security reasons.
2015-06-26 06:31:53 +02:00
if($firstLine) {
2015-05-05 03:00:01 +02:00
unset($lines[0]);
}
2015-07-14 04:16:28 +02:00
// Regenerate the JSON file.
2015-05-05 03:00:01 +02:00
$implode = implode($lines);
2015-07-14 04:16:28 +02:00
// Unserialize, JSON to Array.
2015-07-20 05:14:12 +02:00
$array = $this->unserialize($implode);
if(empty($array)) {
Log::set(__METHOD__.LOG_SEP.'Invalid JSON file: '.$file.', cannot be decoded. Check the file content.');
}
else {
$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;
2016-02-07 00:44:43 +01:00
2015-08-31 03:18:06 +02:00
return true;
}
2015-07-14 04:16:28 +02:00
// Returns the amount of database items.
2015-06-26 06:31:53 +02:00
public function count()
{
return count($this->db);
}
2016-01-21 02:46:13 +01: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'];
}
2015-07-14 04:16:28 +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 = '';
2015-07-14 04:16:28 +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.
2015-09-02 02:42:21 +02:00
return file_put_contents($this->file, $data, LOCK_EX);
2015-05-05 03:00:01 +02:00
}
2016-01-21 02:46:13 +01:00
// Returns a JSON encoded string on success or FALSE on failure.
2015-05-05 03:00:01 +02:00
private function serialize($data)
{
2016-01-21 02:46:13 +01:00
return json_encode($data, JSON_PRETTY_PRINT);
2015-05-05 03:00:01 +02:00
}
2016-01-21 02:46:13 +01:00
// Returns the value encoded in json in appropriate PHP type.
2015-05-05 03:00:01 +02:00
private function unserialize($data)
{
2016-01-21 02:46:13 +01:00
// NULL is returned if the json cannot be decoded.
$decode = json_decode($data, true);
// If NULL returns false.
if(empty($decode)) {
return false;
2015-07-14 04:16:28 +02:00
}
2015-05-05 03:00:01 +02:00
2016-01-21 02:46:13 +01:00
return $decode;
2015-05-05 03:00:01 +02:00
}
2016-01-21 02:46:13 +01:00
}