Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1275

Přidáno uživatelem Ondřej Fibich před asi 13 roky(ů)

Upravy:

- nove zpracovani tridy Settings spravujici nastaveni FreeNetISu:

-- necachuje promenne obsahujici retezec 'pass' kvuli bezpecnosti
-- nahrava cely obsah tabulky config do pameti

- uprava rozhrani pouzivajici Config_Model, nahrazeni knihovnou Settings
- zobrazeni registracni licence u registrace lidi cekajicich na clenstvi

Zobrazit rozdíly:

freenetis/branches/testing/application/models/config.php
*/
/**
* Config values
* Config values.
* Do not use these methods use class Settings!
*
* @package Model
* @see Settings#get
* @see Settings#set
*/
class Config_Model extends Model
{
......
}
/**
* Gets all values from config
* Gets all values from config to assoc. array
* where key is name and value is value.
*
* Do not fetch values which containst word 'pass', because of security
* of services provided by freenetis.
*
* @return Mysql_Result
* @return array
*/
public function get_all_values()
{
return $this->db->query("
// query
$data = $this->db->query("
select name, value
from config
where name not like '%pass%'
order by name
");
// create array
$arr_data = array();
foreach ($data as $row)
{
$arr_data[$row->name] = $row->value;
}
// get data
return $arr_data;
}
/**
freenetis/branches/testing/application/controllers/notifications.php
if ($this->form->validate())
{
$form_data = $this->form->as_array();
$config_model = new Config_Model();
$issaved = true;
foreach ($form_data as $name => $value)
{
$value = addslashes($value);
// check if variable exists
if ($config_model->check_exist_variable($name))
// update of variable
{
$issaved = $issaved && $config_model->update_variable($name,$value);
}
else
// insert new variable
{
$issaved = $issaved && $config_model->insert_variable($name,$value);
}
$issaved = $issaved && Settings::set($name, $value);
}
if ($issaved)
freenetis/branches/testing/application/controllers/voip.php
if ($voip->count() == 0)
Controller::error(RECORD);
$config = new Config_Model();
$config->get_value_from_name('voip_sip_server');
$sip_server = $config->get_value_from_name('voip_sip_server');
$sip_server = Settings::get('voip_sip_server', FALSE);
$voip = $voip_sip->get_record_by_user($user_id);
$voip = $voip->current();
freenetis/branches/testing/application/controllers/bank_accounts.php
if ($this->form->validate())
{
$form_data = $this->form->as_array();
$config_model = new Config_Model();
$issaved = true;
foreach ($form_data as $name => $value)
{
$value = addslashes($value);
// check if variable exists
if ($config_model->check_exist_variable($name))
// update of variable
{
$issaved = $issaved && $config_model->update_variable($name,$value);
}
else
// insert new variable
{
$issaved = $issaved && $config_model->insert_variable($name,$value);
}
$issaved = $issaved && Settings::set($name, $value);
}
if ($issaved)
freenetis/branches/testing/application/controllers/settings.php
{
$form_data = $this->form->as_array();
$config_model = new Config_Model();
$issaved = true;
$message = '';
......
}
}
// check if variable exists
if ($config_model->check_exist_variable($name))
// update of variable
$issaved = $issaved && $config_model->update_variable($name, $value);
else
// insert new variable
$issaved = $issaved && $config_model->insert_variable($name, $value);
$issaved = $issaved && Settings::set($name, $value);
}
if ($issaved)
......
$this->form->input('email_password')
->label(__('Password') . ':')
->value(Settings::get('email_username'))
->value(Settings::get('email_password'))
->help(__('For SMTP settings only.'));
$this->form->submit('Save');
......
{
$form_data = $this->form->as_array();
$config_model = new Config_Model();
$issaved = true;
foreach ($form_data as $name => $value)
{
// check if variable exists
if ($config_model->check_exist_variable($name))
// update of variable
$issaved = $issaved && $config_model->update_variable($name, $value);
else
// insert new variable
$issaved = $issaved && $config_model->insert_variable($name, $value);
$issaved = $issaved && Settings::set($name, $value);
}
if ($issaved)
// if all action were succesfull
{
status::success('E-mail variables have been successfully updated.');
}
else
// if not
{
status::error('E-mail variables havent been successfully updated.');
}
url::redirect('settings/email');
}
......
if ($this->form->validate())
{
$form_data = $this->form->as_array();
$config_model = new Config_Model();
$issaved = true;
foreach ($form_data as $name => $value)
{
$value = addslashes($value);
// check if variable exists
if ($config_model->check_exist_variable($name))
// update of variable
$issaved = $issaved && $config_model->update_variable($name, $value);
else
// insert new variable
$issaved = $issaved && $config_model->insert_variable($name, $value);
$issaved = $issaved && Settings::set($name, $value);
}
if ($issaved)
......
->options(array
(
Billing::INACTIVE => __('Inactive'),
Billing::NFX_LBILLING => "lBilling - NFX"
Billing::NFX_LBILLING => 'lBilling - NFX'
))->selected(Settings::get('voip_billing_driver'));
$this->form->input('voip_billing_partner')
......
if ($this->form->validate())
{
$form_data = $this->form->as_array();
$config_model = new Config_Model();
$issaved = true;
$driver = true;
......
foreach ($form_data as $name => $value)
{
// check if variable exists
if ($config_model->check_exist_variable($name))
// update of variable
$issaved = $issaved && $config_model->update_variable($name, $value);
else
// insert new variable
$issaved = $issaved && $config_model->insert_variable($name, $value);
$issaved = $issaved && Settings::set($name, $value);
}
if (!$driver)
......
if ($this->form->validate())
{
$form_data = $this->form->as_array();
$config_model = new Config_Model();
$issaved = true;
foreach ($form_data as $name => $value)
{
$value = empty($value) ? '' : $value;
// check if variable exists
if ($config_model->check_exist_variable($name))
// update of variable
{
$issaved = $issaved && $config_model->update_variable($name, $value);
}
else
// insert new variable
{
$issaved = $issaved && $config_model->insert_variable($name, $value);
}
$issaved = $issaved && Settings::set($name, $value);
}
if ($issaved)
......
if ($this->form->validate())
{
$form_data = $this->form->as_array();
$config_model = new Config_Model();
// action logs value
if (isset($form_data['action_logs_active']) &&
......
}
// set logs checkbox value to db
$config_model->update_variable('action_logs_active', $action_logs_active);
Settings::set('action_logs_active', $action_logs_active);
//$config_model->update_variable('ulogd_enabled', $ulogd_enabled);
Settings::set('ulogd_enabled', $ulogd_enabled);
......
}
$issaved = true;
foreach ($form_data as $name => $value)
{
if ($name == 'action_logs_active' OR $name == 'ulogd_enabled')
continue;
// check if variable exists
if ($config_model->check_exist_variable($name))
// update of variable
$issaved = $issaved && $config_model->update_variable($name, $value);
else
// insert new variable
$issaved = $issaved && $config_model->insert_variable($name, $value);
$issaved = $issaved && Settings::set($name, $value);
}
if ($issaved)
......
if ($this->form->validate())
{
$form_data = $this->form->as_array();
$config_model = new Config_Model();
$issaved = true;
foreach ($form_data as $name => $value)
{
$value = addslashes($value);
// check if variable exists
if ($config_model->check_exist_variable($name))
// update of variable
$issaved = $issaved && $config_model->update_variable($name, $value);
else
// insert new variable
$issaved = $issaved && $config_model->insert_variable($name, $value);
$issaved = $issaved && Settings::set($name, $value);
}
if ($issaved)
freenetis/branches/testing/application/libraries/Settings.php
*/
/**
* Settings of app
* Settings of whole FreeNetIS.
* Settings are casched except passwords because of security.
*/
class Settings
{
private static $db = NULL;
// variable for cache
/**
* Config model for getting values from database
*
* @var Config_Model
*/
private static $config_model = NULL;
/**
* Variable for cache
*
* @var array
*/
private static $cache = array();
// setting up default value of some variables
private static $title = 'FreeNetIS';
private static $db_schema_version = 0;
private static $currency = 'CZK';
private static $self_registration = 1;
private static $use_javascript = 1;
private static $index_page = 1;
private static $email_default_email = 'no-reply@freenetis.org';
private static $upload_directory = 'upload';
private static $upload_remove_spaces = 1;
private static $upload_create_directories = 1;
// email settings
private static $email_driver = 'native';
private static $email_port = 25;
// ulogd settings
private static $ulogd_enabled = 1;
// time of last update of ulogd
private static $ulogd_update_last = 0;
// interval of updating of ulogd (in seconds), default 1800s = 30 minutes
private static $ulogd_update_interval = 1800;
// count of the most traffic-active members to find, default 10% of members
private static $ulogd_active_count = '10%';
// type of traffic of members to find, default download traffic
private static $ulogd_active_type = 'download';
private static $allowed_subnets_enabled = 1;
// time of last update of allowed subnets
private static $allowed_subnets_update_last = 0;
// interval of updating of allowed subnets, default 60s
private static $allowed_subnets_update_interval = 60;
// default count of allowed subnets
private static $allowed_subnets_default_count = 1;
// default value for prefix of subject of notification e-mails to members
private static $email_subject_prefix = 'FreeNetIS';
private static $ip_addresses_states_interval = 60;
// count of days in which new members will not be notificated to pay, default 14
private static $initial_immunity = 14;
// count of days in which new members will not be blocked and notificated as debtor, default 35
private static $initial_debtor_immunity = 35;
/**
* Default values of settings
*
* @var array
*/
private static $default_values = array
(
// default title of system
'title' => 'FreeNetIS',
// DB schema version starts from zero
'db_schema_version' => 0,
// default currency is Czech crown
'currency' => 'CZK',
// self applicant registration is enabled by default
'self_registration' => 1,
// javascript is enabled by default
'use_javascript' => 1,
// display index.php in URL
'index_page' => 1,
// defaul email address
'email_default_email' => 'no-reply@freenetis.org',
// default upload directory is upload
'upload_directory' => 'upload',
// upload can remove spaces by default
'upload_remove_spaces' => 1,
// upload can create directorie by default
'upload_create_directories' => 1,
// default email driver
'email_driver' => 'native',
// default email port
'email_port' => 25,
// ulogd settings
'ulogd_enabled' => 1,
// time of last update of ulogd
'ulogd_update_last' => 0,
// interval of updating of ulogd (in seconds), default 1800s' => 30 minutes
'ulogd_update_interval' => 1800,
// count of the most traffic-active members to find, default 10% of members
'ulogd_active_count' => '10%',
// type of traffic of members to find, default download traffic
'ulogd_active_type' => 'download',
// allowed subnets is enabled by default
'allowed_subnets_enabled' => 1,
// time of last update of allowed subnets
'allowed_subnets_update_last' => 0,
// interval of updating of allowed subnets, default 60s
'allowed_subnets_update_interval' => 60,
// default count of allowed subnets
'allowed_subnets_default_count' => 1,
// default value for prefix of subject of notification
// e-mails to members
'email_subject_prefix' => 'FreeNetIS',
// IP adresses states interval
'ip_addresses_states_interval' => 60,
// count of days in which new members will not be notificated
// to pay, default 14
'initial_immunity' => 14,
// count of days in which new members will not be blocked
// and notificated as debtor, default 35
'initial_debtor_immunity' => 35,
);
/**
* Sets cache item if key of item does not contains word 'pass',
* because of security.
*
* @param string $key
* @param mixed $value
*/
private static function cache_value_set($key, $value)
{
if (strstr($key, 'pass') === FALSE)
{
self::$cache[$key] = $value;
}
}
/**
* Inits settings
*
* @return boolean
*/
private static function init()
{
// not connected? connect!
if (!self::$config_model)
{
try
{
// create config model
self::$config_model = new Config_Model();
// get whole config table to memory
self::$cache = self::$config_model->get_all_values();
}
catch (Kohana_Database_Exception $e)
{
return FALSE;
}
}
return TRUE;
}
/**
* Function to get value from settings by given key
......
*/
public static function get($key, $cache = TRUE)
{
// not connected? connect!
if (!self::$db)
self::$db = new Config_Model();
// init
self::init();
// if cache is enabled, return it from it
if ($cache && isset(self::$cache[$key]))
{
return self::$cache[$key];
}
// value
$value = '';
try // try if query return exception, for example config table doesn't exist
// if not cached
if (!$cache)
{
$value = self::$db->get_value_from_name($key);
// try if query return exception, for example config table doesn't exist
try
{
$value = self::$config_model->get_value_from_name($key);
}
catch (Kohana_Database_Exception $e)
{
$value = '';
}
}
catch (Kohana_Database_Exception $e)
// if we find not-null value, return it
if (!empty($value))
{
$value = '';
self::cache_value_set($key, $value);
return $value;
}
// if we find not-null value, return it
if ($value != '')
self::$cache[$key] = $value;
// else return property
else if (isset(self::$$key))
self::$cache[$key] = self::$$key;
// else return default value
else if (isset(self::$default_values[$key]))
{
self::cache_value_set($key, self::$default_values[$key]);
return self::$default_values[$key];
}
else
// in worst return value from config (from config.php)
self::$cache[$key] = Config::get($key);
return self::$cache[$key];
{
$value = Config::get($key);
self::cache_value_set($key, $value);
return $value;
}
}
/**
......
*/
public static function set($key, $value)
{
// not connected? connect!
if (!self::$db)
self::$db = new Config_Model();
// init
self::init();
try // try if query return exception, for example config table doesn't exist
// try if query return exception, for example config table doesn't exist
try
{
$exists = self::$db->check_exist_variable($key);
$exists = self::$config_model->check_exist_variable($key);
// key already exists, update it
if ($exists && self::$config_model->update_variable($key, $value))
{
self::cache_value_set($key, $value);
return TRUE;
}
// key doesn't exist, create it
else if (self::$config_model->insert_variable($key, $value))
{
self::cache_value_set($key, $value);
return TRUE;
}
}
catch (Kohana_Database_Exception $e)
{
// database error, end
return False;
return FALSE;
}
// key already exists, update it
if ($exists)
{
return self::$db->update_variable($key, $value);
}
// key doesn't exist, create it
else
{
return self::$db->insert_variable($key, $value);
}
return FALSE;
}
}
freenetis/branches/testing/application/libraries/MY_Controller.php
class Controller extends Controller_Core
{
/** @var integer */
const ICON_ERROR = 1;
const ICON_ERROR = 1;
/** @var integer */
const ICON_GOOD = 2;
const ICON_GOOD = 2;
/** @var integer */
const ICON_HELP = 3;
const ICON_HELP = 3;
/** @var integer */
const ICON_INFO = 4;
const ICON_INFO = 4;
/** @var integer */
const ICON_WARNING = 5;
const ICON_WARNING = 5;
/**
* Controller singleton
......
}
// Redirect to login
url::redirect(url_lang::base() . 'login');
url::redirect('login');
// Die
die();
......
if (url_lang::current(1) == 'setup_config')
return;
url::redirect(url_lang::base() . 'setup_config');
url::redirect('setup_config');
}
// protection before loop
......
Controller::error(DATABASE);
// db schema version is null
if (!$this->settings->get('db_schema_version'))
if (!Settings::get('db_schema_version'))
{
// we must run install
url::redirect(url_lang::base() . 'installation');
url::redirect('installation');
}
// db schema is not up to date
else if ($this->current_svn_db_schema_version !=
$this->settings->get('db_schema_version'))
Settings::get('db_schema_version'))
{
// we must run upgrade
$this->upgrade_sql($this->settings->get('db_schema_version'));
$this->upgrade_sql(Settings::get('db_schema_version'));
}
// load these variables only for logged user
......
*/
public function get_current_svn_db_schema_info()
{
require_once("application/upgrade_sql/upgrade_sql.php");
require_once('application/upgrade_sql/upgrade_sql.php');
$this->current_svn_db_schema_version = get_SVN_rev();
......
*/
public function upgrade_sql($from_version = 0)
{
$ok = true;
$this->db = Database::instance();
// database connection
$db = Database::instance();
// config model for set DB schema version
$config = new Config_Model();
$query = '';
// for each revision
for ($i = ($from_version + 1); $i < ($this->current_svn_db_schema_version); $i++)
for ($i = ($from_version + 1); $i < $this->current_svn_db_schema_version; $i++)
{
if (file_exists("application/upgrade_sql/upgrade_sql_" . $i . ".php"))
if (file_exists('application/upgrade_sql/upgrade_sql_' . $i . '.php'))
{
require("application/upgrade_sql/upgrade_sql_" . $i . ".php");
require_once('application/upgrade_sql/upgrade_sql_' . $i . '.php');
// database transaction
try
{
......
}
}
// upgrade SQL
foreach ($upgrade_sql[$i] as $query)
{
if (!$this->db->query($query))
if (!$db->query($query))
{
throw new Kohana_Database_Exception();
}
......
}
catch (Exception $e)
{
$message = "SVN: $i <br />"
. __('file') . ": upgrade_sql_$i.php<br /><br />Function: " .
$e->getMessage();
$this->error(UPGRADE, $message);
self::error(
UPGRADE, 'SVN: ' . $i . ' <br />' . __('file') .
': upgrade_sql_' . $i . '.php<br /><br />Function: ' .
$e->getMessage()
);
}
// set up db schema
$config->set_db_schema_version($i);
......
}
}
// upgrade SQL
foreach ($this->upgrade_sql[$this->current_svn_db_schema_version] as $query)
{
if (!$this->db->query($query))
if (!$db->query($query))
{
throw new Kohana_Database_Exception();
}
......
}
}
}
catch (Kohana_Database_Exception $e)
catch (Exception $e)
{
$message = "SVN: $i <br />" . __('file') .
": upgrade_sql.php<br /><br />$query";
$message = 'SVN: ' . $this->current_svn_db_schema_version . ' <br />'
. __('file') . ': upgrade_sql_' . $i . '.php<br /><br />'
. 'Function: ' . $e->getMessage();
$this->error(UPGRADE, $message);
}
catch (Exception $e)
{
$message = "SVN: $i <br />"
. __('file') . ": upgrade_sql_$i.php<br /><br />Function: " .
$e->getMessage();
}
// set up db schema
$config->set_db_schema_version($this->current_svn_db_schema_version);
......
->count_all_unread_inbox_messages_by_user_id($this->user_id);
// count registered members if enabled
if ($this->settings->get('self_registration'))
if (Settings::get('self_registration'))
{
$this->count_of_registered_members = $member->count_of_registered_members();
}
freenetis/branches/testing/application/views/registration.php
<div id="content">
<h2><?php echo $title ?></h2>
<?php echo isset($message) ? '<div class="message">' . $message . '</div>' : '' ?>
<?php echo Settings::get('registration_license') ?>
<?php echo $form ?>
<p><?php echo html::anchor(url_lang::base() . 'login', '&laquo; ' . __('back to login')) ?></p>
</div>
freenetis/branches/testing/system/core/Config.php
* @copyright (c) 2007 Kohana Team
* @license http://kohanaphp.com/license.html
*/
final class Config {
final class Config
{
// Entire configuration
private static $conf;
private static $language = 'cs_CZ';
private static $allowed_locales = array
(
'cs' => 'cs_CZ',
'en' => 'en_US',
'cs' => 'cs_CZ',
'en' => 'en_US',
);
private static $lang = 'cs';
private static $allow_config_set = True;
// Include paths
private static $include_paths = array();
/**
* Get a config item or group.
*
......
{
// Configuration autoloading
if (self::$conf === NULL)
{
{
// Invalid config file
if (file_exists('config'.EXT))
{
require('config'.EXT);
}
if (file_exists('config' . EXT))
{
require('config' . EXT);
}
if (!isset($config))
$config = array();
if (!isset($config))
$config = array();
// Load config into self
self::$conf = $config;
self::include_paths(TRUE);
self::include_paths(TRUE);
}
if (isset(self::$conf[$key]))
return self::$conf[$key];
if (isset(self::$conf[$key]))
return self::$conf[$key];
if (isset (self::$$key))
{
return self::$$key;
}
if (isset(self::$$key))
{
return self::$$key;
}
return '';
return '';
}
/**
......
// Do this to make sure that the config array is already loaded
Config::get($key);
self::$conf[$key] = $value;
self::$conf[$key] = $value;
return TRUE;
}
......
self::$include_paths = array(APPPATH);
// Normalize all paths to be absolute and have a trailing slash
foreach(array (self::get('modules')) as $path)
foreach (array(self::get('modules')) as $path)
{
if (($path = str_replace('\\', '/', realpath($path))) == '')
continue;
self::$include_paths[] = $path.'/';
self::$include_paths[] = $path . '/';
}
// Finish setting include paths by adding SYSPATH
......
$configuration = array();
// Find all the configuartion files matching the name
foreach(Kohana::find_file('config', $name, $required) as $filename)
foreach (Kohana::find_file('config', $name, $required) as $filename)
{
// Import the config
include $filename;
......
return $configuration;
}
} // End Config
}
// End Config

Také k dispozici: Unified diff