2015-05-05 03:00:01 +02:00
|
|
|
<?php defined('BLUDIT') or die('Bludit CMS.');
|
|
|
|
|
|
|
|
class dbJSON
|
|
|
|
{
|
|
|
|
public $db;
|
|
|
|
public $file;
|
|
|
|
public $firstLine;
|
|
|
|
|
2015-07-03 22:44:26 +02:00
|
|
|
// $firstLine, TRUE if you want to remove the first line.
|
2015-05-05 03:00:01 +02:00
|
|
|
function __construct($file, $firstLine=true)
|
|
|
|
{
|
|
|
|
$this->file = $file;
|
|
|
|
$this->db = array();
|
|
|
|
$this->firstLine = $firstLine;
|
|
|
|
|
|
|
|
if(file_exists($file))
|
|
|
|
{
|
|
|
|
$lines = file($file);
|
|
|
|
|
2015-06-26 06:31:53 +02:00
|
|
|
if($firstLine) {
|
2015-05-05 03:00:01 +02:00
|
|
|
// Remove the first line.
|
|
|
|
unset($lines[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$implode = implode($lines);
|
|
|
|
|
|
|
|
$this->db = $this->unserialize($implode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-27 03:47:12 +02:00
|
|
|
Log::set(__METHOD__.LOG_SEP.'File '.$file.' does not exists');
|
2015-05-05 03:00:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:31:53 +02:00
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->db);
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
public function save()
|
|
|
|
{
|
|
|
|
if($this->firstLine)
|
|
|
|
$data = "<?php defined('BLUDIT') or die('Bludit CMS.'); ?>".PHP_EOL;
|
|
|
|
else
|
|
|
|
$data = '';
|
|
|
|
|
|
|
|
$data .= $this->serialize($this->db);
|
|
|
|
|
|
|
|
// LOCK_EX flag to prevent anyone else writing to the file at the same time.
|
|
|
|
file_put_contents($this->file, $data, LOCK_EX);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function serialize($data)
|
|
|
|
{
|
|
|
|
// DEBUG: La idea es siempre serializar en json, habria que ver si siempre esta cargado json_enconde y decode
|
|
|
|
if(JSON)
|
|
|
|
return json_encode($data, JSON_PRETTY_PRINT);
|
|
|
|
|
|
|
|
return serialize($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function unserialize($data)
|
|
|
|
{
|
|
|
|
// DEBUG: La idea es siempre serializar en json, habria que ver si siempre esta cargado json_enconde y decode
|
|
|
|
if(JSON)
|
|
|
|
return json_decode($data, true);
|
|
|
|
|
|
|
|
return unserialize($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEBUG, ver si sirve para la instalacion, sino borrar
|
|
|
|
public function setDb($db)
|
|
|
|
{
|
|
|
|
$this->db = $db;
|
|
|
|
|
|
|
|
return $this->save();
|
|
|
|
}
|
|
|
|
|
2015-07-03 22:44:26 +02:00
|
|
|
public function getDb()
|
|
|
|
{
|
|
|
|
return $this->db;
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:00:01 +02:00
|
|
|
// DEBUG, se puede borrar
|
|
|
|
public function show()
|
|
|
|
{
|
|
|
|
var_dump($this->db);
|
|
|
|
}
|
|
|
|
|
2015-05-31 03:06:55 +02:00
|
|
|
}
|