[go: up one dir, main page]

Menu

[3822d7]: / core / lib / Conf.php  Maximize  Restore  History

Download this file

169 lines (146 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
namespace ICT\Core;
/* * ***************************************************************
* Copyright © 2014 ICT Innovations Pakistan All Rights Reserved *
* Developed By: Nasir Iqbal *
* Website : http://www.ictinnovations.com/ *
* Mail : nasir@ictinnovations.com *
* *************************************************************** */
class Conf extends Data
{
/* constant to represent all nodes simultaneously */
const NODE_ALL = 0;
const ALL = 0;
const SYSTEM = 1;
const USER = 2;
const CAMPAIGN = 3;
const CONTACT = 4; // not in use, only for concept
const PERMISSION_GLOBAL_WRITE = 1;
const PERMISSION_GLOBAL_READ = 2;
const PERMISSION_NODE_WRITE = 4;
const PERMISSION_NODE_READ = 8;
const PERMISSION_ADMIN_WRITE = 16;
const PERMISSION_ADMIN_READ = 32;
const PERMISSION_USER_WRITE = 64;
const PERMISSION_USER_READ = 128;
// system = 170 = GLOBAL_READ | NODE_READ | ADMIN_READ | USER_READ
// admin = 186 = system | ADMIN_WRITE
// user = 250 = admin | USER_WRITE
// node = 254 = user | NODE_WRITE
/**
* @var Conf
*/
public static $oConf = array();
/**
* @staticvar boolean $initialized
* @return Conf
*/
public static function get_instance()
{
static $initialized = FALSE;
if (!$initialized) {
self::$oConf = new self;
$initialized = TRUE;
}
return self::$oConf;
}
public static function set($name, $value, $permanent = FALSE, $reference = array(), $permission = Conf::PERMISSION_NODE_WRITE)
{
$oConf = self::get_instance();
$oConf->__set($name, $value);
if ($permanent) {
list($type, $conf_name) = explode(':', $name, 2);
static::database_conf_set($type, $conf_name, $value, $reference, $permission);
}
}
public static function &get($name, $default = NULL)
{
$oConf = self::get_instance();
$value = &$oConf->__get($name);
if (NULL === $value) {
return $default;
}
return $value;
}
public static function load($config_id)
{
Corelog::log("Demo, loading configuration for $config_id");
}
protected static function merge_array($configuration = array())
{
$newConf = new Data($configuration);
$oConf = self::get_instance();
$oConf->merge($newConf);
return true;
}
protected static function database_conf_load($filter = array(), $type = null)
{
$configuration = array();
if (!empty($type)) {
$filter[] = "c.type='$type'";
}
$filter_string = implode(' AND ', $filter);
// in following query ORDER BY class make sure that top level values should be overwritten specific configuration
$query = "SELECT c.type, c.name, cd.data FROM configuration c LEFT JOIN configuration_data cd
ON c.configuration_id = cd.configuration_id
WHERE $filter_string
ORDER BY cd.class ASC, cd.node_id ASC, cd.created_by ASC";
$result = DB::query('configuration', $query);
if (!$result) {
throw new CoreException('500', 'Unable to get configuration, query failed');
}
while ($config = mysqli_fetch_assoc($result)) {
$type = $config['type'];
$name = $config['name'];
$data = $config['data'];
$configuration[$type][$name] = $data; // setting a global array
}
Corelog::log("configuration loaded", Corelog::DEBUG, $configuration);
return $configuration;
}
public static function database_conf_save($aConf = null, $reference = array(), $permission = Conf::PERMISSION_NODE_WRITE)
{
Corelog::log("configuration save request", Corelog::COMMON, array($aConf, $reference));
foreach ($aConf as $type => $conf) {
foreach ($conf as $name => $data) {
static::database_conf_set($type, $name, $data, $reference, $permission);
}
}
}
public static function database_conf_set($type, $name, $data, $reference = array(), $permission = Conf::PERMISSION_NODE_WRITE)
{
$query = "SELECT configuration_id FROM configuration
WHERE (permission_flag & $permission)=$permission AND type='$type' AND name='$name'";
$result = DB::query('configuration', $query);
if (mysqli_num_rows($result)) {
$configuration_id = static::sql_result($result, 0, 0);
} else {
Corelog::log("Unable to save configuration. type:$type, name:$name", Corelog::ERROR);
return;
}
$update_query = '';
$search_query = '';
if (!empty($reference)) {
$reference_pair = array();
foreach ($reference as $key => $value) {
$reference_pair[] = "$key=$value";
}
$update_query = ', ' . implode(', ', $reference_pair);
$search_query = ' AND ' . implode(' AND ', $reference_pair);
}
if ($data == '[default]') {
DB::query('configuration_data', "DELETE FROM configuration_data WHERE configuration_id=$configuration_id $search_query");
} else {
$query = "INSERT INTO configuration_data SET configuration_id='$configuration_id', data='$data',
date_created=UNIX_TIMESTAMP() $update_query
ON DUPLICATE KEY UPDATE data='$data', last_updated=UNIX_TIMESTAMP()";
Corelog::log($query, Corelog::INFO);
DB::query('configuration', $query);
}
}
public static function sql_result($result, $number, $field=0) {
mysqli_data_seek($result, $number);
$row = mysqli_fetch_array($result);
return $row[$field];
}
}