Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 491 lines (461 sloc) 14.05 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
<?php

/**
* Configuration Management API
*
* @package Cotonti
* @version 0.9.0
* @author Trustmaster
* @copyright Copyright (c) Cotonti Team 2010-2011
* @license BSD
*/

defined('COT_CODE') or die('Wrong URL');

/**
* Generic text configuration. Is displayed as textarea. Contains text.
* Is used by default.
*/
define('COT_CONFIG_TYPE_TEXT', 0);
/**
* A string, max length is 255 chars. Is displayed as a single line of input.
* The list of variants is ignored for this type.
*/
define('COT_CONFIG_TYPE_STRING', 1);
/**
* Selection from the list of possible variants. Is displayed as a dropdown.
*/
define('COT_CONFIG_TYPE_SELECT', 2);
/**
* Radio yes/no selection.
*/
define('COT_CONFIG_TYPE_RADIO', 3);
/**
* Callback function type
*/
define('COT_CONFIG_TYPE_CALLBACK', 4);
/**
* Hidden config. It is actually a text string, but it is not displayed anywhere
*/
define('COT_CONFIG_TYPE_HIDDEN', 5);
/**
* Visual separator/fieldset
*/
define('COT_CONFIG_TYPE_SEPARATOR', 6);
/**
* Integer range
*/
define('COT_CONFIG_TYPE_RANGE', 7);

/**
* Registers a set of configuration entries at once.
*
* Example:
* <code>
* $config_options = array(
* array(
* 'name' => 'disable_test',
* 'type' => COT_CONFIG_TYPE_RADIO,
* 'default' => '0'
* ),
* array(
* 'name' => 'test_selection',
* 'type' => COT_CONFIG_TYPE_SELECT,
* 'default' => '20',
* 'variants' => '5,10,15,20,25,30,35,40,50'
* ),
* array(
* 'name' => 'test_value',
* 'type' => COT_CONFIG_TYPE_STRING,
* 'default' => 'something'
* ),
* array(
* 'name' => 'not_visible',
* 'type' => COT_CONFIG_TYPE_HIDDEN,
* 'default' => 'test23'
* )
* );
*
* cot_config_add('test', $config_options, true);
* </code>
*
* @param string $name Extension name (code)
* @param array $options An associative array of configuration entries.
* Each entry of the arrray has the following keys:
* 'name' => Option name, alphanumeric and _. Must be unique for a module/plugin
* 'type' => Option type, see COT_CONFIG_TYPE_* constants
* 'default' => Default and initial value, by default is an empty string
* 'variants' => A comma separated (without spaces) list of possible values,
* only for SELECT options.
* 'order' => A string that determines position of the option in the list,
* e.g. '04'. Or will be assigned automatically if omitted
* 'text' => Textual description. It is usually omitted and stored in langfiles
* @param bool $is_module Flag indicating if it is module or plugin config
* @param string $category Structure category code. Only for per-category config options
* @param string $donor Extension name for extension-to-extension config implantations
* @return bool Operation status
*/
function cot_config_add($name, $options, $is_module = false, $category = '', $donor = '')
{
    global $db, $cfg, $db_config;
    $cnt = count($options);
    $type = $is_module ? 'module' : 'plug';
    // Check the arguments
    if (!$cnt)
    {
        return false;
    }
    // Build the SQL query
$option_set = array();
for ($i = 0; $i < $cnt; $i++)
{
$opt = $options[$i];
$option_set[] = array(
'config_owner' => $type,
'config_cat' => $name,
'config_subcat' => $category,
'config_order' => isset($opt['order']) ? $opt['order'] : str_pad($i, 2, 0, STR_PAD_LEFT),
'config_name' => $opt['name'],
'config_type' => (int) $opt['type'],
'config_value' => $opt['default'],
'config_default' => $opt['default'],
'config_variants' => $opt['variants'],
'config_text' => $opt['text'],
'config_donor' => $donor
);
}

    $ins_cnt = $db->insert($db_config, $option_set);
    return $ins_cnt == $cnt;
}

/**
* Implants given options into module configuration if they are not already there.
* Used by plugins which extend module behavior and need per-module or per-category
* options.
*
* @global array $cfg Configuration
* @param string $module_name Target module code
* @param array $options Array of implantable options, described in cot_config_add()
* @param bool $into_struct A flag indicating that config options should be implanted into
* module categories configuration rather than the module root configuration
* @param string $donor Extension name for extension-to-extension config implantations
* @return int Number of options actually implanted
* @see cot_config_add()
*/
function cot_config_implant($module_name, $options, $into_struct, $donor)
{
global $cfg;

$category = $into_struct ? '__default' : '';
$add_options = array();
foreach ($options as $opt)
{
if (!$into_struct && !isset($cfg[$module_name][$opt['name']])
|| $into_struct && !isset($cfg[$module_name]['__default'][$opt['name']]))
{
$add_options[] = $opt;
}
}

return cot_config_add($module_name, $add_options, true, $category, $donor);
}

/**
* Checks if there are already implanted config records
*
* @param string $acceptor Acceptor module name
* @param string $donor Donor extension name
* @return bool TRUE if implanted records found, FALSE if not
*/
function cot_config_implanted($acceptor, $donor)
{
global $db, $db_config;
return $db->query("SELECT COUNT(*) FROM $db_config WHERE config_owner = 'module' AND config_cat = ? AND config_donor = ?", array($acceptor, $donor))->fetchColumn() > 0;
}

/**
* Loads config structure from database into an array
*
* @param string $name Extension code
* @param bool $is_module TRUE if module, FALSE if plugin
* @param string $category Structure category code. Only for per-category config options
* @param string $donor Extension name for extension-to-extension config implantations
* @return array Config options structure
* @see cot_config_add()
*/
function cot_config_load($name, $is_module = false, $category = '', $donor = '')
{
global $db, $db_config;
$options = array();
$type = $is_module ? 'module' : 'plug';

$query = "SELECT config_name, config_type, config_value,
config_default, config_variants, config_order
FROM $db_config WHERE config_owner = ? AND config_cat = ? AND config_subcat = ? AND config_donor = ?";
$params = array($type, $name, $category, $donor);

$res = $db->query($query, $params);
while ($row = $res->fetch())
{
$options[] = array(
'name' => $row['config_name'],
'type' => $row['config_type'],
'order' => $row['config_order'],
'value' => $row['config_value'],
'default' => $row['config_default'],
'variants' => $row['config_variants']
);
}
$res->closeCursor();

return $options;
}

/**
* Updates config map properties in the database for given options
*
* @param string $name Extension code
* @param array $options Configuration entries
* @param bool $is_module TRUE if module, FALSE if plugin
* @param string $category Structure category code. Only for per-category config options
* @param string $donor Extension name for extension-to-extension config implantations
* @return int Number of entries updated
*/
function cot_config_modify($name, $options, $is_module = false, $category = '', $donor = '')
{
global $db, $db_config;
$type = $is_module ? 'module' : 'plug';
$affected = 0;

$where = "config_owner = ? AND config_cat = ? AND config_name = ? AND config_donor = ?";

if (!empty($category))
{
$where .= " AND config_subcat = ?";
}

foreach ($options as $opt)
{
$config_name = $opt['config_name'];
unset($opt['config_name']);
$opt_row = array();
foreach ($opt as $key => $val)
{
$opt_row['config_' . $key] = $val;
}
$affected += $db->update($db_config, $opt_row, $where,
empty($category) ? array($type, $name, $config_name, $donor) : array($type, $name, $config_name, $donor, $category));
}

return $affected;
}

/**
* Parses array of setup file configuration entries into array representation
*
* @param array $info_cfg Setup file config entries
* @return array Config options
*/
function cot_config_parse($info_cfg)
{
    $options = array();
if (is_array($info_cfg))
{
foreach ($info_cfg as $i => $x)
{
$line = explode(':', $x);
if (is_array($line) && !empty($line[1]) && !empty($i))
{
switch ($line[1])
{
case 'string':
$line['Type'] = COT_CONFIG_TYPE_STRING;
break;
case 'select':
$line['Type'] = COT_CONFIG_TYPE_SELECT;
break;
case 'radio':
$line['Type'] = COT_CONFIG_TYPE_RADIO;
break;
case 'callback':
$line['Type'] = COT_CONFIG_TYPE_CALLBACK;
break;
case 'hidden':
$line['Type'] = COT_CONFIG_TYPE_HIDDEN;
break;
case 'separator':
$line['Type'] = COT_CONFIG_TYPE_SEPARATOR;
break;
case 'range':
$line['Type'] = COT_CONFIG_TYPE_RANGE;
break;
default:
$line['Type'] = COT_CONFIG_TYPE_TEXT;
break;
}
$options[] = array(
'name' => $i,
'order' => $line[0],
'type' => $line['Type'],
'variants' => $line[2],
'default' => $line[3],
'text' => $line[4]
);
}
}
}
    return $options;
}

/**
* Unregisters configuration option(s).
*
* @param string $name Extension name (code)
* @param bool $is_module Flag indicating if it is module or plugin config
* @param mixed $option String name of a single configuration option.
* Or pass an array of option names to remove them at once. If empty or omitted,
* all options from selected module/plugin will be removed
* @param string $category Structure category code. Only for per-category config options
* @param string $donor Extension name for extension-to-extension config implantations
* @return int Number of options actually removed
*/
function cot_config_remove($name, $is_module = false, $option = '', $category = '', $donor = null)
{
    global $db, $db_config;

    $type = $is_module ? 'module' : 'plug';
    $where = "config_owner = '$type' AND config_cat = " . $db->quote($name);
if (!empty($category))
{
$where .= " AND config_subcat = " . $db->quote($category);
}
if (!is_null($donor))
{
$where .= " AND config_donor = " . $db->quote($donor);
}

    if (is_array($option))
    {
        $cnt = count($option);
        if ($cnt == 1)
        {
            $option = $option[0];
        }
        else
        {
            $where .= " AND config_name IN (";
            for ($i = 0; $i < $cnt; $i++)
            {
                if ($i > 0)
                    $where .= ',';
                $where .= $db->quote($option[$i]);
            }
$where .= ')';
            unset($option);
        }
    }
    if (!empty($option))
    {
        $where .= " AND config_name = " . $db->quote($option);
    }
    return $db->delete($db_config, $where);
}

/**
* Updates configuration values
*
* Example:
* <code>
* $config_values = array(
* 'disable_test' => '0',
* 'hidden_test' => 'test45',
* );
*
* cot_config_set('test', $config_values, true);
* </code>
*
* @param string $name Extension name config belongs to
* @param array $options Array of options as 'option name' => 'option value'
* @param bool $is_module Flag indicating if it is module or plugin config
* @param string $category Structure category code. Only for per-category config options
* @return int Number of entries updated
*/
function cot_config_set($name, $options, $is_module = false, $category = '')
{
    global $db, $db_config;
    $type = $is_module ? 'module' : 'plug';
    $upd_cnt = 0;

$where = 'config_owner = ? AND config_cat = ? AND config_name = ?';
if (!empty($category))
{
$where .= ' AND config_subcat = ?';
if ($category != '__default')
{
$default_options = cot_config_load($name, $is_module, '__default');
}
}

foreach ($options as $key => $val)
{
if (empty($category) || $category == '__default' || $val != $default_options[$key])
{
$params = empty($category) ? array($type, $name, $key) : array($type, $name, $key, $category);
$upd_cnt += $db->update($db_config, array('config_value' => $val), $where, $params);
}
}

    return $upd_cnt;
}

/**
* Updates existing configuration map removing obsolete options, adding new
* options and tweaking options which need to be updated.
*
* @param string $name Extension code
* @param array $options Configuration options
* @param bool $is_module TRUE for modules, FALSE for plugins
* @param string $category Structure category code. Only for per-category config options
* @param string $donor Extension name for extension-to-extension config implantations
* @return int Number of entries affected
*/
function cot_config_update($name, $options, $is_module = false, $category = '', $donor = '')
{
$affected = 0;
$old_options = cot_config_load($name, $is_module, $category, $donor);

// Find and remove options which no longer exist
$remove_opts = array();
foreach ($old_options as $old_opt)
{
$keep = false;
foreach ($options as $opt)
{
if ($opt['name'] == $old_opt['name'])
{
$keep = true;
break;
}
}
if (!$keep && $old_opt['type'] != COT_CONFIG_TYPE_HIDDEN)
{
$remove_opts[] = $old_opt['name'];
}
}
if (count($remove_opts) > 0)
{
$affected += cot_config_remove($name, $is_module, $remove_opts, $category, $donor);
}

// Find new options and options which have been modified
$new_options = array();
$upd_options = array();
foreach ($options as $opt)
{
$existed = false;
foreach ($old_options as $old_opt)
{
if ($opt['name'] == $old_opt['name'])
{
$changed = array_diff($opt, $old_opt);
foreach ($changed as $key => $val)
{
if ($key != 'value')
{
// Values for modified options are set to default
$opt['value'] = $opt['default'];
$upd_options[] = $opt;
}
}
$existed = true;
break;
}
}
if (!$existed)
{
$new_options[] = $opt;
}
}
if (count($new_options) > 0)
{
$affected += cot_config_add($name, $new_options, $is_module, $category);
}
if (count($upd_options) > 0)
{
$affected += cot_config_modify($name, $upd_options, $is_module, $category);
}

return $affected;
}
?>
Something went wrong with that request. Please try again.