Revize 779
Přidáno uživatelem Ondřej Fibich před téměř 14 roky(ů)
freenetis/trunk/kohana/application/libraries/MY_Controller.php | ||
---|---|---|
class Controller extends Controller_Core
|
||
{
|
||
|
||
// Controller singleton
|
||
/** @staticvar Controller Controller singleton */
|
||
private static $instance;
|
||
|
||
/** @var gacl PHP GACL class */
|
||
public $gacl_class;
|
||
/** @var unknown_type */
|
||
public $arr;
|
||
/**
|
||
* @var Setting_Model Settings
|
||
*/
|
||
/** @var Setting_Model Settings */
|
||
public $settings = NULL;
|
||
/** @var array */
|
||
public $upgrade_sql = array();
|
||
/** @var integer */
|
||
public $current_svn_db_schema_version = 0;
|
||
/** @var integer */
|
||
public $popup = 0;
|
||
/** @var integer */
|
||
public $dialog = 0;
|
||
|
||
/** @var integer */
|
||
private $ICON_ERROR = 1;
|
||
/** @var integer */
|
||
private $ICON_GOOD = 2;
|
||
/** @var integer */
|
||
private $ICON_HELP = 3;
|
||
/** @var integer */
|
||
private $ICON_INFO = 4;
|
||
/** @var integer */
|
||
private $ICON_WARNING = 5;
|
||
|
||
public function __construct()
|
||
... | ... | |
|
||
// db schema version is null
|
||
if (!$this->settings->get('db_schema_version'))
|
||
// we must run install
|
||
url::redirect(url_lang::base().'installation');
|
||
|
||
{
|
||
// we must run install
|
||
url::redirect(url_lang::base().'installation');
|
||
}
|
||
// db schema is not up to date
|
||
else if ($this->current_svn_db_schema_version != $this->settings->get('db_schema_version'))
|
||
{
|
||
... | ... | |
}
|
||
|
||
// test if visitor is logged in, or he accesses public controllers like registration, redirect, installation
|
||
if (!$this->session->get('user_id', 0) && url_lang::current()!='login' &&
|
||
url_lang::current()!='forgotten_password' &&
|
||
url_lang::current() != 'registration' && url_lang::current()!='scheduler/run' && strpos(url_lang::current(), 'redirect')===false && url_lang::current()!='installation')
|
||
if (!$this->session->get('user_id', 0) &&
|
||
url_lang::current()!='login' &&
|
||
url_lang::current()!='forgotten_password' &&
|
||
url_lang::current() != 'registration' &&
|
||
url_lang::current()!='scheduler/run' &&
|
||
strpos(url_lang::current(), 'redirect')===false &&
|
||
url_lang::current()!='installation')
|
||
{
|
||
// Not logged in - redirect to login page
|
||
$this->session->set_flash('err_message', url_lang::lang('texts.Must be logged in'));
|
||
if (url_lang::current()!='installation') $this->session->set('referer',url_lang::current());
|
||
url::redirect(url_lang::base().'login');
|
||
if (url_lang::current()!='installation')
|
||
{
|
||
$this->session->set('referer',url_lang::current());
|
||
}
|
||
url::redirect(url_lang::base().'login');
|
||
die();
|
||
}
|
||
|
||
... | ... | |
* @param $content
|
||
* @return unknown_type
|
||
*/
|
||
public function warning($message, $content = NULL)
|
||
public function warning($message, $content = NULL)
|
||
{
|
||
switch ($message)
|
||
{
|
||
... | ... | |
* @param $content
|
||
* @return unknown_type
|
||
*/
|
||
private function showbox($message, $type, $content = NULL)
|
||
{
|
||
$view = new View('main');
|
||
$view->content = new View('statesbox');
|
||
switch ($type)
|
||
{
|
||
case $this->ICON_ERROR:
|
||
$view->title = url_lang::lang('texts.Error');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/error.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
case $this->ICON_GOOD:
|
||
$view->title = url_lang::lang('texts.Good');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/good.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
case $this->ICON_HELP:
|
||
$view->title = url_lang::lang('texts.Help');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/help.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
case $this->ICON_INFO:
|
||
$view->title = url_lang::lang('texts.Info');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/info.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
case $this->ICON_WARNING:
|
||
$view->title = url_lang::lang('texts.Warning');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/warning.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
}
|
||
$view->content->message = $message;
|
||
if (isset($content))
|
||
$view->content->content = $content;
|
||
$view->render(TRUE);
|
||
// must be die() - else it will be render twice !
|
||
die();
|
||
}
|
||
private function showbox($message, $type, $content = NULL)
|
||
{
|
||
$view = new View('main');
|
||
$view->content = new View('statesbox');
|
||
|
||
|
||
function get_current_svn_db_schema_info()
|
||
{
|
||
require_once("application/upgrade_sql/upgrade_sql.php");
|
||
$this->current_svn_db_schema_version = get_SVN_rev();
|
||
$this->upgrade_sql[$this->current_svn_db_schema_version] = $upgrade_sql[$this->current_svn_db_schema_version];
|
||
}
|
||
switch ($type)
|
||
{
|
||
case $this->ICON_ERROR:
|
||
$view->title = url_lang::lang('texts.Error');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/error.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
case $this->ICON_GOOD:
|
||
$view->title = url_lang::lang('texts.Good');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/good.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
case $this->ICON_HELP:
|
||
$view->title = url_lang::lang('texts.Help');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/help.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
case $this->ICON_INFO:
|
||
$view->title = url_lang::lang('texts.Info');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/info.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
case $this->ICON_WARNING:
|
||
$view->title = url_lang::lang('texts.Warning');
|
||
$view->content->icon = html::image(array('src' => 'media/images/states/warning.png', 'width' => '100', 'height' => '100', 'alt' => 'Image', 'class' => 'noborder'));
|
||
break;
|
||
}
|
||
$view->content->message = $message;
|
||
if (isset($content))
|
||
$view->content->content = $content;
|
||
$view->render(TRUE);
|
||
// must be die() - else it will be render twice !
|
||
die();
|
||
}
|
||
|
||
function upgrade_sql($from_version = 0)
|
||
{
|
||
$ok = true;
|
||
$this->db = new Database();
|
||
$config = new Config_Model();
|
||
for ($i = ($from_version+1); $i<($this->current_svn_db_schema_version); $i++)
|
||
{
|
||
/**
|
||
* Setup revision database schema.
|
||
*/
|
||
function get_current_svn_db_schema_info()
|
||
{
|
||
require_once("application/upgrade_sql/upgrade_sql.php");
|
||
$this->current_svn_db_schema_version = get_SVN_rev();
|
||
$this->upgrade_sql[$this->current_svn_db_schema_version] = $upgrade_sql[$this->current_svn_db_schema_version];
|
||
}
|
||
|
||
/**
|
||
* Upgrade database from current version to latest version.
|
||
* @todo Using database transaction, because of data integrity.
|
||
* THERE IS NO WAY TO DO SO IN MYSQL... :-(
|
||
* Code which using transactions was commented. (saved for future..)
|
||
* @param integer $from_version From which version(revision) upgrade starts
|
||
*/
|
||
function upgrade_sql($from_version = 0)
|
||
{
|
||
$ok = true;
|
||
$this->db = new Database();
|
||
$config = new Config_Model();
|
||
|
||
// for each revision
|
||
for ($i = ($from_version+1); $i<($this->current_svn_db_schema_version); $i++)
|
||
{
|
||
if (file_exists("application/upgrade_sql/upgrade_sql_".$i.".php"))
|
||
{
|
||
require("application/upgrade_sql/upgrade_sql_".$i.".php");
|
||
// database transaction
|
||
$this->db->query('START TRANSACTION;');
|
||
//try
|
||
//{
|
||
try
|
||
{
|
||
/*if (!$this->db->query('SET AUTOCOMMIT=0'))
|
||
{
|
||
throw new Kohana_Database_Exception();
|
||
}
|
||
|
||
if (!$this->db->query('BEGIN'))
|
||
{
|
||
throw new Kohana_Database_Exception();
|
||
}*/
|
||
|
||
foreach ($upgrade_sql[$i] as $query)
|
||
$this->db->query($query);
|
||
//}
|
||
// problem - it seems that Kohana uses separate connection for each query, so transaction
|
||
// processing with rollback does not work
|
||
/*catch (Kohana_Database_Exception $e)
|
||
{
|
||
if(!$this->db->query($query))
|
||
{
|
||
throw new Kohana_Database_Exception();
|
||
}
|
||
}
|
||
|
||
/*if (!$this->db->query('COMMIT'))
|
||
{
|
||
throw new Kohana_Database_Exception();
|
||
}
|
||
|
||
if (!$this->db->query('SET AUTOCOMMIT=1'))
|
||
{
|
||
throw new Kohana_Database_Exception();
|
||
}*/
|
||
}
|
||
catch (Kohana_Database_Exception $e)
|
||
{
|
||
$this->db->query('ROLLBACK;');
|
||
//$this->profiler = new Profiler();
|
||
//echo $this->profiler->render(true);
|
||
/*$this->db->query('ROLLBACK');
|
||
$this->db->query('SET AUTOCOMMIT=1');*/
|
||
|
||
$message = "SVN: $i <br />"
|
||
.url_lang::lang('texts.file').": upgrade_sql_$i.php<br /><br />
|
||
$query";
|
||
$this->error(UPGRADE, $message);
|
||
}*/
|
||
$this->db->query('COMMIT;');
|
||
}
|
||
// set up db schema
|
||
$config->set_db_schema_version($i);
|
||
}
|
||
}
|
||
// database transaction
|
||
$this->db->query('START TRANSACTION;');
|
||
try
|
||
{
|
||
foreach ($this->upgrade_sql[$this->current_svn_db_schema_version] as $query)
|
||
$this->db->query($query);
|
||
}
|
||
catch (Kohana_Database_Exception $e)
|
||
{
|
||
$this->db->query('ROLLBACK;');
|
||
//$this->profiler = new Profiler();
|
||
//echo $this->profiler->render(true);
|
||
$message = "SVN: $i <br />"
|
||
.url_lang::lang('texts.file').": upgrade_sql.php<br /><br />
|
||
$query";
|
||
$this->error(UPGRADE, $message);
|
||
}
|
||
$this->db->query('COMMIT;');
|
||
$config->set_db_schema_version($this->current_svn_db_schema_version);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* Inicialization of PHP GACL modul.
|
||
*/
|
||
public function phpgacl_init() {
|
||
require_once(APPPATH.'vendors/phpgacl/gacl.class.php');
|
||
$this->gacl_class = new gacl();
|
||
}
|
||
|
||
/**
|
||
* acl_check_2D checks the current user's rights to access an $aco_section/$aco_value
|
||
*/
|
||
... | ... | |
* an $axo_value object in ***current controller*** (!!!) by
|
||
* operation specified by $aco_section/$aco_value
|
||
*/
|
||
|
||
public function acl_check_3D($aco_section, $aco_value, $axo_value) {
|
||
if (!isset($this->gacl_class)) $this->phpgacl_init();
|
||
return $this->gacl_class->acl_check($aco_section, $aco_value, 'all', $_SESSION['username'],get_class($this),$axo_value);
|
||
}
|
||
|
||
|
||
/**
|
||
* Fuction checks access rights
|
||
* Return true if currently logged user (stored in $_SESSION['username']) may view own $axo_value object in $axo_section (and in variable $member_id is his own id of member)
|
||
* or if currently logged user may view all $axo_value object in $axo_section else return false
|
||
* Return true if currently logged user (stored in $_SESSION['username'])
|
||
* may view own $axo_value object in $axo_section
|
||
* (and in variable $member_id is his own id of member) or if currently logged user
|
||
* may view all $axo_value object in $axo_section else return false
|
||
*
|
||
* @param $axo_section group of objects to view
|
||
* @param $axo_value object to view
|
||
... | ... | |
if ($this->gacl_class->acl_check('freenetis', 'view_all', 'all', $_SESSION['username'],$axo_section,$axo_value)) $return = true;
|
||
return $return;
|
||
}
|
||
|
||
|
||
/**
|
||
* Fuction checks access rights
|
||
* Return true if currently logged user (stored in $_SESSION['username'])
|
||
* may view own $axo_value object in $axo_section
|
||
* (and in variable $member_id is his own id of member) or if currently logged user
|
||
* may edit all $axo_value object in $axo_section else return false
|
||
*
|
||
* @param $axo_section group of objects to edit
|
||
* @param $axo_value object to edit
|
||
* @param $member_id optional variable, id of other member who is being showed by logged member
|
||
* @return boolean returns true if member has enough access rights
|
||
*/
|
||
public function acl_check_edit($axo_section, $axo_value, $member_id = NULL)
|
||
{
|
||
$return = false;
|
||
... | ... | |
if ($this->gacl_class->acl_check('freenetis', 'edit_all', 'all', $_SESSION['username'],$axo_section,$axo_value)) $return = true;
|
||
return $return;
|
||
}
|
||
|
||
|
||
/**
|
||
* Fuction checks access rights
|
||
* Return true if currently logged user (stored in $_SESSION['username'])
|
||
* may view own $axo_value object in $axo_section
|
||
* (and in variable $member_id is his own id of member) or if currently logged user
|
||
* may add all $axo_value object in $axo_section else return false
|
||
*
|
||
* @param $axo_section group of objects to edit
|
||
* @param $axo_value object to add
|
||
* @param $member_id optional variable, id of other member who is being showed by logged member
|
||
* @return boolean returns true if member has enough access rights
|
||
*/
|
||
public function acl_check_new($axo_section, $axo_value, $member_id = NULL)
|
||
{
|
||
$return = false;
|
||
... | ... | |
if ($this->gacl_class->acl_check('freenetis', 'new_all', 'all', $_SESSION['username'],$axo_section,$axo_value)) $return = true;
|
||
return $return;
|
||
}
|
||
|
||
|
||
/**
|
||
* Fuction checks access rights
|
||
* Return true if currently logged user (stored in $_SESSION['username'])
|
||
* may view own $axo_value object in $axo_section
|
||
* (and in variable $member_id is his own id of member) or if currently logged user
|
||
* may delete all $axo_value object in $axo_section else return false
|
||
*
|
||
* @param $axo_section group of objects to edit
|
||
* @param $axo_value object to delete
|
||
* @param $member_id optional variable, id of other member who is being showed by logged member
|
||
* @return boolean returns true if member has enough access rights
|
||
*/
|
||
public function acl_check_delete($axo_section, $axo_value, $member_id = NULL)
|
||
{
|
||
$return = false;
|
||
... | ... | |
return $return;
|
||
}
|
||
|
||
public function acl_check_confirm($axo_section, $axo_value, $member_id = NULL)
|
||
{
|
||
if (!isset($this->gacl_class)) $this->phpgacl_init();
|
||
if($member_id==$_SESSION['member_id'])
|
||
{
|
||
return $this->gacl_class->acl_check('freenetis', 'confirm_own', 'all', $_SESSION['username'],$axo_section,$axo_value);
|
||
}
|
||
else
|
||
{
|
||
return $this->gacl_class->acl_check('freenetis', 'confirm_all', 'all', $_SESSION['username'],$axo_section,$axo_value);
|
||
}
|
||
}
|
||
/**
|
||
* Fuction checks access rights
|
||
* Return true if currently logged user (stored in $_SESSION['username'])
|
||
* may view own $axo_value object in $axo_section
|
||
* (and in variable $member_id is his own id of member) or if currently logged user
|
||
* may confirm all $axo_value object in $axo_section else return false
|
||
*
|
||
* @param $axo_section group of objects to edit
|
||
* @param $axo_value object to confirm
|
||
* @param $member_id optional variable, id of other member who is being showed by logged member
|
||
* @return boolean returns true if member has enough access rights
|
||
*/
|
||
public function acl_check_confirm($axo_section, $axo_value, $member_id = NULL)
|
||
{
|
||
if (!isset($this->gacl_class)) $this->phpgacl_init();
|
||
if($member_id==$_SESSION['member_id'])
|
||
{
|
||
return $this->gacl_class->acl_check('freenetis', 'confirm_own', 'all', $_SESSION['username'],$axo_section,$axo_value);
|
||
}
|
||
else
|
||
{
|
||
return $this->gacl_class->acl_check('freenetis', 'confirm_all', 'all', $_SESSION['username'],$axo_section,$axo_value);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Function probably assigns role to user.
|
||
* @param $user_id
|
||
* @param $username
|
||
* @param $value
|
||
* @param $group
|
||
* @param $is_edit
|
||
* @return unknown_type
|
||
*/
|
||
/**
|
||
* Function probably assigns role to user.
|
||
* @param $user_id
|
||
* @param $username
|
||
* @param $value
|
||
* @param $group
|
||
* @param $is_edit
|
||
* @return unknown_type
|
||
*/
|
||
public function insert_phpgacl($user_id,$username,$value,$group, $is_edit = false)
|
||
{
|
||
$aro_group_data = new Groups_aro_map_Model();
|
freenetis/trunk/kohana/system/libraries/ORM.php | ||
---|---|---|
|
||
/** @var array Stores column information for ORM models */
|
||
protected static $column_cache = array();
|
||
/** @var boolean Indicator of using transaction */
|
||
protected $is_in_transaction = false;
|
||
|
||
/**
|
||
* Creates and returns a new model.
|
||
... | ... | |
}
|
||
|
||
/**
|
||
* Destruct of ORM.
|
||
* Roolback if transaction is on.
|
||
*/
|
||
public function __destruct()
|
||
{
|
||
$this->transaction_roolback();
|
||
}
|
||
|
||
/**
|
||
* Prepares the model database connection, determines the table name,
|
||
* and loads column information.
|
||
*
|
||
... | ... | |
*/
|
||
public function transaction_start()
|
||
{
|
||
// Start transaction
|
||
if (!$this->db->query('BEGIN'))
|
||
if ($this->is_in_transaction === false)
|
||
{
|
||
throw new Exception('Cannot open transaction. ');
|
||
$this->db->query('SET AUTOCOMMIT=0');
|
||
$this->db->query('BEGIN');
|
||
}
|
||
$this->is_in_transaction = true;
|
||
}
|
||
|
||
/**
|
||
... | ... | |
*/
|
||
public function transaction_commit()
|
||
{
|
||
// Commit transaction
|
||
if (!$this->db->query('COMMIT'))
|
||
if ($this->is_in_transaction === true)
|
||
{
|
||
throw new Exception('Cannot commit transaction. ');
|
||
$this->db->query('COMMIT');
|
||
$this->db->query('SET AUTOCOMMIT=1');
|
||
}
|
||
$this->is_in_transaction = false;
|
||
}
|
||
|
||
/**
|
||
... | ... | |
*/
|
||
public function transaction_roolback()
|
||
{
|
||
// Commit transaction
|
||
if (!$this->db->query('ROLLBACK'))
|
||
if ($this->is_in_transaction === true)
|
||
{
|
||
throw new ErrorException('Cannot rollback transaction. ');
|
||
$this->db->query('ROLLBACK');
|
||
$this->db->query('SET AUTOCOMMIT=1');
|
||
}
|
||
$this->is_in_transaction = false;
|
||
}
|
||
|
||
} // End ORM
|
Také k dispozici: Unified diff
Vylepsena podpora transakci.
V komentarich k upgrade metode My_Controlleru byly zanechanytransakce (Odkomentovat az pojedeme na postgresu - v MySQL to nefunguje u transakci s prikazy CREATE, DROP, ALTER, ...).