histogram: Make histograms crash less
[ninja.git] / application / helpers / config.php
blobb10ce0bc833ff43b3acf19fdb410028f97c3daa8
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Help class for handling config items transparently,
4 * i.e independent of storage location (file or database)
5 */
6 class config_Core
8 /**
9 * Dummy value to compare to when ... no config was found!
11 const CONFIG_NOT_FOUND = null;
13 /**
14 * Fetch config item from db or config file
15 * If $page is set it will fetch for a page-specific
16 * setting for current user
18 public static function get($config_str=false, $page='', $save=false)
20 $config_str = trim($config_str);
21 if (empty($config_str) || !is_string($config_str)) {
22 return false;
25 $setting = self::CONFIG_NOT_FOUND;
27 # check for database value
28 $cfg = Ninja_setting_Model::fetch_page_setting($config_str, $page);
29 if ($cfg!==false) {
30 $setting = $cfg->setting;
33 if ($setting === self::CONFIG_NOT_FOUND) {
34 # if nothing was found - try the config file
35 $setting = Kohana::config($config_str, false, false);
36 if (is_array($setting) && empty($setting)) {
37 $setting = false;
38 } elseif ($save) {
39 # save to database and session as user setting
40 Ninja_setting_Model::save_page_setting($config_str, $page, $setting);
44 return $setting;
47 /**
48 * Fetch specific key from config file
49 * Default is cgi.cfg
51 public static function get_cgi_cfg_key($key=false, $file='cgi.cfg')
53 $key = trim($key);
54 if (empty($key) || empty($file) || !Auth::instance()->logged_in())
55 return false;
57 $session = Session::instance();
58 $val = $session->get($key, null);
59 if ($val === null) {
60 $val = arr::search(System_Model::parse_config_file($file), $key, null);
61 if (!is_null($val)) {
62 # store value in session
63 $session->set($key, $val);
66 return $val;
69 /**
70 * On a OP5 Monitor system, return the system version
72 * @return string
74 public static function get_version_info()
76 static $version = NULL;
77 if ($version === NULL) {
78 $file = Kohana::config('config.version_info');
79 if (@is_readable($file)) {
80 $handle = fopen($file, 'r');
81 $contents = fread($handle, filesize($file));
82 fclose($handle);
83 $version = trim(str_replace('VERSION=','',$contents));
86 return $version;