Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 142 additions & 12 deletions src/Core/Setting.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
/**
*
* CakeManager (http://cakemanager.org)
* Copyright (c) http://cakemanager.org
*
Expand All @@ -20,40 +21,54 @@

class Setting
{

/**
*
* List of loaded data
*
* @var array
*/
protected static $_data = [];

/**
*
* Array of values currently stored in Configure.
*
* @var array
*/
protected static $_values = [];

/**
*
* Options
*
* @var array
*/
protected static $_options = [];

/**
*
* Holder for the model
*
* @var \Cake\ORM\Table
*/
protected static $_model = null;

/**
*
* Keeps the boolean if the autoload method has been loaded
*
* @var bool
*/
protected static $_autoloaded = false;

/**
*
* read
*
* Method to read the data.
*
* @param string $key Key with the name of the setting.
* @param string $key Key with the name of the setting.
* @param string $type The type to return in.
* @return mixed
*/
Expand Down Expand Up @@ -85,9 +100,26 @@ public static function read($key = null, $type = null)
if ($data->count() > 0) {
$data = $data->first()->toArray();
} else {
return null;
$data = $model->find()->select(['name', 'value'])->where(['name LIKE' => $key . '.%']);

if ($data->count() > 0) {
$data = $data->toArray();
foreach ($data as $dataSet) {
if (self::_serialized($dataSet->value)) {
$dataSet->value = unserialize($dataSet->value);
}
static::$_values = Hash::insert(static::$_values, $dataSet->name, $dataSet->value);
}

$data['value'] = static::$_values;
} else {
return null;
}
}

if (self::_serialized($data['value'])) {
$data['value'] = unserialize($data['value']);
}
self::_store($key, $data['value']);

$value = $data['value'];
Expand All @@ -100,6 +132,7 @@ public static function read($key = null, $type = null)
}

/**
*
* write
*
* Method to write data to database.
Expand All @@ -118,9 +151,9 @@ public static function read($key = null, $type = null)
* 'editable' => 0,
* ]
*
* @param string $key Key of the value. Must contain an prefix.
* @param mixed $value The value of the key.
* @param array $options Options array.
* @param string $key Key of the value. Must contain an prefix.
* @param mixed $value The value of the key.
* @param array $options Options array.
* @return void|bool
*/
public static function write($key, $value = null, $options = [])
Expand All @@ -139,6 +172,10 @@ public static function write($key, $value = null, $options = [])
$options = Hash::merge($_options, $options);

$model = self::model();

if (is_array($value) && !empty($value)) {
$value = serialize($value);
}

if (self::check($key)) {
if ($options['overrule']) {
Expand All @@ -165,6 +202,7 @@ public static function write($key, $value = null, $options = [])
}

/**
*
* check
*
* Checks if an specific key exists.
Expand Down Expand Up @@ -196,6 +234,7 @@ public static function check($key)
}

/**
*
* model
*
* Returns an instance of the Configurations-model (Table).
Expand All @@ -218,13 +257,14 @@ public static function model($model = null)
}

/**
*
* register
*
* Registers a setting and its default values.
*
* @param string $key The key.
* @param mixed $value The default value.
* @param array $data Custom data.
* @param string $key The key.
* @param mixed $value The default value.
* @param array $data Custom data.
* @return void
*/
public static function register($key, $value, $data = [])
Expand All @@ -234,6 +274,10 @@ public static function register($key, $value, $data = [])
}

self::autoLoad();

if (is_array($value)) {
$value = seralize($value);
}

$_data = [
'value' => $value,
Expand All @@ -254,10 +298,11 @@ public static function register($key, $value, $data = [])
}

/**
*
* options
*
* @param string $key Key for options.
* @param array $value Options to use.
* @param string $key Key for options.
* @param array $value Options to use.
* @return mixed
*/
public static function options($key, $value = null)
Expand All @@ -278,6 +323,7 @@ public static function options($key, $value = null)
}

/**
*
* autoLoad
*
* AutoLoad method.
Expand Down Expand Up @@ -305,6 +351,7 @@ public static function autoLoad()
}

/**
*
* clear
*
* Clears all settings out of the class. Settings
Expand All @@ -320,12 +367,13 @@ public static function clear($reload = false)
}

/**
*
* _store
*
* Stores recent data in the $_data-variable.
*
* @param string $key The key.
* @param mixed $value The value.
* @param string $key The key.
* @param mixed $value The value.
* @return void
*/
protected static function _store($key, $value)
Expand All @@ -334,6 +382,7 @@ protected static function _store($key, $value)
}

/**
*
* _tableExists
*
* @return bool
Expand All @@ -348,4 +397,85 @@ protected static function _tableExists()
}
return false;
}

/**
* _serialized
*
* @param string $value The value.
* @param mixed $result The result (null default).
* @return bool
*/
protected static function _serialized($value, $result = null)
{
if (! is_string($value)) {
return false;
}

if ('b:0;' === $value) {
$result = false;
return true;
}
$length = strlen($value);
$end = '';

if (isset($value[0])) {
switch ($value[0]) {
case 's':
if ('"' !== $value[$length - 2]) {
return false;
}
// no break
case 'b':
// no break
case 'i':
// no break
case 'd':
$end .= ';';
// no break
case 'a':
// no break
case 'O':
$end .= '}';

if (':' !== $value[1]) {
return false;
}

switch ($value[2]) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
break;

default:
return false;
}
// break appled in embedded switch
case 'N':
$end .= ';';

if ($value[$length - 1] !== $end[0]) {
return false;
}
break;

default:
return false;
}
}

if (( $result = unserialize($value) ) === false) {
$result = null;
return false;
}

return true;
}
}
34 changes: 34 additions & 0 deletions tests/TestCase/Core/SettingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ public function testRead()
$this->assertEquals(1, Setting::read('App.UniqueReadvalue'));
$this->assertEquals(1, Setting::read('App.UniqueReadvalue', 'integer'));
$this->assertEquals('1', Setting::read('App.UniqueReadvalue', 'string'));

$data = [
'name' => 'App.UniqueArray',
'value' => 'a:4:{i:0;i:1;i:2;i:3;i:3;s:3:"one";s:3:"two";s:5:"three";}'
];

$this->Settings->save($this->Settings->newEntity($data));
$read = Setting::read('App.UniqueArray');
$this->assertGreaterThan(0, count($read));
$this->assertEquals([1, 2 => 3, 'one', 'two' => 'three'], Setting::read('App.UniqueArray'));
}

/**
Expand Down Expand Up @@ -197,6 +207,30 @@ public function testWrite()
$this->assertEquals(1, $value->editable);
$this->assertEquals(20, $value->weight);
$this->assertEquals(1, $value->autoload);

Setting::write('App.WriteArray', [1, 2 => 3, 'one', 'two' => 'three'], [
'description' => 'Short description',
'type' => 'array',
'editable' => true,
'options' => [
1 => 'One',
2 => 'Two'
],
'weight' => 20,
'autoload' => true,
]);

$this->assertEquals(3, $this->Settings->find('all')->count());

$value = $this->Settings->get(3);
$this->assertEquals('App.WriteArray', $value->name);
$this->assertEquals('App.WriteArray', $value->key);
$this->assertEquals('a:4:{i:0;i:1;i:2;i:3;i:3;s:3:"one";s:3:"two";s:5:"three";}', $value->value);
$this->assertEquals('Short description', $value->description);
$this->assertEquals('array', $value->type);
$this->assertEquals(1, $value->editable);
$this->assertEquals(20, $value->weight);
$this->assertEquals(1, $value->autoload);
}

/**
Expand Down