upgrade
This commit is contained in:
14
main/auth/shibboleth/lib/model.class.php
Normal file
14
main/auth/shibboleth/lib/model.class.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
* Description of model
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
class Model
|
||||
{
|
||||
|
||||
}
|
||||
64
main/auth/shibboleth/lib/scaffolder/scaffolder.class.php
Normal file
64
main/auth/shibboleth/lib/scaffolder/scaffolder.class.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
* Scaffolder. Genereate code templates from the database layout.
|
||||
* See /template/ for the code being generated
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
class Scaffolder
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @staticvar boolean $result
|
||||
* @return Scaffolder
|
||||
*/
|
||||
public static function instance()
|
||||
{
|
||||
static $result = false;
|
||||
if (empty($result))
|
||||
{
|
||||
$result = new self();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function scaffold($table_name, $class_name = '', $prefix = '_')
|
||||
{
|
||||
$db_name = Database :: get_main_database();
|
||||
$sql = "SELECT * FROM `$db_name`.`$table_name` LIMIT 1";
|
||||
|
||||
$fields = array();
|
||||
$unique_fields = array();
|
||||
$rs = Database::query($sql, null, __FILE__);
|
||||
while ($field = mysql_fetch_field($rs))
|
||||
{
|
||||
$fields[] = $field;
|
||||
if ($field->primary_key)
|
||||
{
|
||||
/**
|
||||
* Could move that to an array to support multiple keys
|
||||
*/
|
||||
$id_name = $field->name;
|
||||
}
|
||||
if ($field->unique_key | $field->primary_key)
|
||||
{
|
||||
$keys[] = $field->name;
|
||||
}
|
||||
}
|
||||
$name = $table_name;
|
||||
$class_name = ucfirst($table_name);
|
||||
|
||||
|
||||
|
||||
ob_start();
|
||||
include __DIR__.'/template/model.php';
|
||||
$result = ob_get_clean();
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
146
main/auth/shibboleth/lib/scaffolder/template/default.php
Normal file
146
main/auth/shibboleth/lib/scaffolder/template/default.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
|
||||
echo '<?php';
|
||||
?>
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
* This file is autogenerated. Do not modifiy it.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* Model for table <?php echo $table_name ?>
|
||||
*
|
||||
* @copyright (c) 2012 University of Geneva
|
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>
|
||||
*/
|
||||
class <?php echo $prefix . $class_name ?>
|
||||
|
||||
{
|
||||
|
||||
/**
|
||||
* Store for <?php echo $class_name ?> objects. Interact with the database.
|
||||
*
|
||||
* @return <?php echo $class_name ?>Store
|
||||
*/
|
||||
public static function store()
|
||||
{
|
||||
static $result = false;
|
||||
if (empty($result))
|
||||
{
|
||||
$result = new <?php echo $class_name ?>Store();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>
|
||||
*/
|
||||
public static function create($data = null)
|
||||
{
|
||||
return self::store()->create_object($data);
|
||||
}
|
||||
|
||||
<?php foreach($fields as $field){?>
|
||||
public $<?php echo $field->name; ?> = <?php echo $field->def ? $field->def : 'null'; ?>;
|
||||
<?php }?>
|
||||
|
||||
/**
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
return self::store()->save($this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Store for <?php echo $class_name ?> objects. Interact with the database.
|
||||
*
|
||||
* @copyright (c) 2012 University of Geneva
|
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>
|
||||
*/
|
||||
class <?php echo $prefix . $class_name ?>Store extends Store
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>Store
|
||||
*/
|
||||
public static function instance()
|
||||
{
|
||||
static $result = false;
|
||||
if (empty($result))
|
||||
{
|
||||
$result = new self();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('<?php echo $table_name;?>', '<?php echo $class_name;?>', '<?php echo $id_name;?>');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>
|
||||
*/
|
||||
public function get($w)
|
||||
{
|
||||
$args = func_get_args();
|
||||
$f = array('parent', 'get');
|
||||
return call_user_func_array($f, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>
|
||||
*/
|
||||
public function create_object($data)
|
||||
{
|
||||
return parent::create_object($data);
|
||||
}
|
||||
<?php foreach($keys as $key){?>
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>
|
||||
*/
|
||||
public function get_by_<?php echo $key ?>($value)
|
||||
{
|
||||
return $this->get(array('<?php echo $key; ?>' => $value));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function <?php echo $key ?>_exists($value)
|
||||
{
|
||||
return $this->exist(array('<?php echo $key; ?>' => $value));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete_by_<?php echo $key ?>($value)
|
||||
{
|
||||
return $this->delete(array('<?php echo $key; ?>' => $value));
|
||||
}
|
||||
<?php }?>
|
||||
}
|
||||
146
main/auth/shibboleth/lib/scaffolder/template/model.php
Normal file
146
main/auth/shibboleth/lib/scaffolder/template/model.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
|
||||
echo '<?php';
|
||||
?>
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
* This file is autogenerated. Do not modifiy it.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* Model for table <?php echo $table_name ?>
|
||||
*
|
||||
* @copyright (c) 2012 University of Geneva
|
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>
|
||||
*/
|
||||
class <?php echo $prefix . $class_name ?>
|
||||
|
||||
{
|
||||
|
||||
/**
|
||||
* Store for <?php echo $class_name ?> objects. Interact with the database.
|
||||
*
|
||||
* @return <?php echo $class_name ?>Store
|
||||
*/
|
||||
public static function store()
|
||||
{
|
||||
static $result = false;
|
||||
if (empty($result))
|
||||
{
|
||||
$result = new <?php echo $class_name ?>Store();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>
|
||||
*/
|
||||
public static function create($data = null)
|
||||
{
|
||||
return self::store()->create_object($data);
|
||||
}
|
||||
|
||||
<?php foreach($fields as $field){?>
|
||||
public $<?php echo $field->name; ?> = <?php echo $field->def ? $field->def : 'null'; ?>;
|
||||
<?php }?>
|
||||
|
||||
/**
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
return self::store()->save($this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Store for <?php echo $class_name ?> objects. Interact with the database.
|
||||
*
|
||||
* @copyright (c) 2012 University of Geneva
|
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>
|
||||
*/
|
||||
class <?php echo $prefix . $class_name ?>Store extends Store
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>Store
|
||||
*/
|
||||
public static function instance()
|
||||
{
|
||||
static $result = false;
|
||||
if (empty($result))
|
||||
{
|
||||
$result = new self();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('<?php echo $table_name;?>', '<?php echo $class_name;?>', '<?php echo $id_name;?>');
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>
|
||||
*/
|
||||
public function get($w)
|
||||
{
|
||||
$args = func_get_args();
|
||||
$f = array('parent', 'get');
|
||||
return call_user_func_array($f, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>
|
||||
*/
|
||||
public function create_object($data)
|
||||
{
|
||||
return parent::create_object($data);
|
||||
}
|
||||
<?php foreach($keys as $key){?>
|
||||
|
||||
/**
|
||||
*
|
||||
* @return <?php echo $class_name ?>
|
||||
*/
|
||||
public function get_by_<?php echo $key ?>($value)
|
||||
{
|
||||
return $this->get(array('<?php echo $key; ?>' => $value));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function <?php echo $key ?>_exists($value)
|
||||
{
|
||||
return $this->exist(array('<?php echo $key; ?>' => $value));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete_by_<?php echo $key ?>($value)
|
||||
{
|
||||
return $this->delete(array('<?php echo $key; ?>' => $value));
|
||||
}
|
||||
<?php }?>
|
||||
}
|
||||
39
main/auth/shibboleth/lib/scaffolder/template/public.php
Normal file
39
main/auth/shibboleth/lib/scaffolder/template/public.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
|
||||
echo '<?php';
|
||||
?>
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
*
|
||||
* Model for table <?php echo $table_name ?>
|
||||
*
|
||||
* @copyright (c) 2012 University of Geneva
|
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>
|
||||
*/
|
||||
class <?php echo $class_name ?>
|
||||
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Store for <?php echo $class_name ?> objects. Interact with the database.
|
||||
*
|
||||
* @copyright (c) 2012 University of Geneva
|
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>
|
||||
*/
|
||||
class <?php echo $class_name ?>Store extends Store
|
||||
{
|
||||
|
||||
}
|
||||
61
main/auth/shibboleth/lib/shibboleth_config.class.php
Normal file
61
main/auth/shibboleth/lib/shibboleth_config.class.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
/**
|
||||
* Shibboleth configuration. All configuration for the Shibboleth authentication
|
||||
* plugin: field names mapping, etc.
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
class ShibbolethConfig
|
||||
{
|
||||
public $unique_id = '';
|
||||
public $firstname = '';
|
||||
public $lastname = '';
|
||||
public $email = '';
|
||||
public $language = '';
|
||||
public $gender = '';
|
||||
public $address = '';
|
||||
public $staff_category = '';
|
||||
public $home_organization_type = '';
|
||||
public $home_organization = '';
|
||||
public $affiliation = '';
|
||||
public $persistent_id = '';
|
||||
|
||||
public $default_status = Shibboleth::UNKNOWN_STATUS;
|
||||
|
||||
/**
|
||||
* Mapping of affiliation => right
|
||||
* @var array
|
||||
*/
|
||||
public $affiliation_status = array();
|
||||
|
||||
/**
|
||||
* Mapping of affiliation => bool. Display the request status form.
|
||||
* @var array
|
||||
*/
|
||||
public $affiliation_status_request = array();
|
||||
|
||||
/**
|
||||
* List of fields to update when the user already exists field_name => boolean.
|
||||
* @var array
|
||||
*/
|
||||
public $update_fields = array();
|
||||
|
||||
/*
|
||||
* True if email is mandatory. False otherwise.
|
||||
*/
|
||||
public $is_email_mandatory = true;
|
||||
|
||||
/**
|
||||
* The email of the shibboleth administrator.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $admnistrator_email = '';
|
||||
|
||||
|
||||
|
||||
}
|
||||
100
main/auth/shibboleth/lib/shibboleth_session.class.php
Normal file
100
main/auth/shibboleth/lib/shibboleth_session.class.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
use ChamiloSession as Session;
|
||||
use Database;
|
||||
use Event;
|
||||
|
||||
/**
|
||||
* A Chamilo user session. Used as there is no session object so far provided by the core API.
|
||||
* Should be moved to the core library.Prefixed by Shibboleth to avoid name clashes.
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
class ShibbolethSession
|
||||
{
|
||||
/**
|
||||
* @return ShibbolethSession
|
||||
*/
|
||||
public static function instance()
|
||||
{
|
||||
static $result = false;
|
||||
if (empty($result)) {
|
||||
$result = new self();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function is_logged_in()
|
||||
{
|
||||
return isset($_SESSION['_user']['user_id']);
|
||||
}
|
||||
|
||||
function user()
|
||||
{
|
||||
return $_SESSION['_user'];
|
||||
}
|
||||
|
||||
function logout()
|
||||
{
|
||||
$_SESSION['_user'] = array();
|
||||
online_logout(null, false);
|
||||
global $logoutInfo;
|
||||
Event::courseLogout($logoutInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Shibboleth session for the user ID
|
||||
*
|
||||
* @param string $uid The user ID
|
||||
* @return array $_user The user infos array created when the user logs in
|
||||
*/
|
||||
function login($uid)
|
||||
{
|
||||
/* This must be set for local.inc.php to register correctly the global variables in session
|
||||
* This is BAD. Logic should be migrated into a function and stop relying on global variables.
|
||||
*/
|
||||
global $_uid, $is_allowedCreateCourse, $is_platformAdmin, $_real_cid, $is_courseAdmin;
|
||||
global $is_courseMember, $is_courseTutor, $is_session_general_coach, $is_allowed_in_course, $is_sessionAdmin, $_gid;
|
||||
$_uid = $uid;
|
||||
|
||||
//is_allowedCreateCourse
|
||||
$user = User::store()->get_by_user_id($uid);
|
||||
if (empty($user)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->logout();
|
||||
|
||||
Session::instance();
|
||||
Session::write('_uid', $_uid);
|
||||
|
||||
global $_user;
|
||||
$_user = (array) $user;
|
||||
|
||||
$_SESSION['_user'] = $_user;
|
||||
$_SESSION['_user']['user_id'] = $_uid;
|
||||
$_SESSION['noredirection'] = true;
|
||||
|
||||
//must be called before 'init_local.inc.php'
|
||||
Event::eventLogin($_uid);
|
||||
|
||||
//used in 'init_local.inc.php' this is BAD but and should be changed
|
||||
$loginFailed = false;
|
||||
$uidReset = true;
|
||||
|
||||
$gidReset = true;
|
||||
$cidReset = false; //FALSE !!
|
||||
|
||||
$mainDbName = Database :: get_main_database();
|
||||
$includePath = api_get_path(SYS_INC_PATH);
|
||||
|
||||
$no_redirection = true;
|
||||
require("$includePath/local.inc.php");
|
||||
|
||||
return $_user;
|
||||
}
|
||||
|
||||
}
|
||||
357
main/auth/shibboleth/lib/store.class.php
Normal file
357
main/auth/shibboleth/lib/store.class.php
Normal file
@@ -0,0 +1,357 @@
|
||||
<?php
|
||||
|
||||
namespace Shibboleth;
|
||||
|
||||
use \Database;
|
||||
|
||||
/**
|
||||
* A database store. Used interact with the database - save objects, run queries.
|
||||
*
|
||||
* One store = one table.
|
||||
*
|
||||
* @license see /license.txt
|
||||
* @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
|
||||
*/
|
||||
class Store
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* @return Store
|
||||
*/
|
||||
public static function create($table_name, $class_name = '', $id_name = 'id', $db_name = '')
|
||||
{
|
||||
return new self($table_name, $class_name, $id_name, $db_name);
|
||||
}
|
||||
|
||||
protected $db_name = '';
|
||||
protected $table_name = '';
|
||||
protected $id_name = '';
|
||||
protected $class_name = '';
|
||||
|
||||
function __construct($table_name, $class_name = '', $id_name = 'id', $db_name = '')
|
||||
{
|
||||
$this->db_name = $db_name ? $db_name : Database::get_main_database();
|
||||
$this->table_name = $table_name;
|
||||
$this->class_name = $class_name;
|
||||
$this->id_name = $id_name;
|
||||
}
|
||||
|
||||
function get_db_name($object = '')
|
||||
{
|
||||
if ($this->db_name)
|
||||
{
|
||||
return $this->db_name;
|
||||
}
|
||||
if ($object)
|
||||
{
|
||||
$result = isset($object->{db_name}) ? $object->{db_name} : '';
|
||||
$result = $result ? $result : Database :: get_main_database();
|
||||
return $result;
|
||||
}
|
||||
|
||||
return Database::get_main_database();
|
||||
}
|
||||
|
||||
function get($w)
|
||||
{
|
||||
$args = func_get_args();
|
||||
$f = array($this, 'get_where');
|
||||
$db_name = $this->get_db_name();
|
||||
$where = call_user_func_array($f, $args);
|
||||
$sql = "SELECT *
|
||||
FROM `{$db_name}`.`{$this->table_name}`
|
||||
WHERE $where";
|
||||
|
||||
$items = $this->query($sql);
|
||||
return (count($items) == 1) ? reset($items) : null;
|
||||
}
|
||||
|
||||
function select($w)
|
||||
{
|
||||
$args = func_get_args();
|
||||
$f = array($this, 'get_where');
|
||||
$db_name = $this->get_db_name();
|
||||
$where = call_user_func_array($f, $args);
|
||||
$sql = "SELECT *
|
||||
FROM `{$db_name}`.`{$this->table_name}`
|
||||
WHERE $where";
|
||||
|
||||
$result = $this->query($sql);
|
||||
return $result;
|
||||
}
|
||||
|
||||
function exist($w)
|
||||
{
|
||||
$args = func_get_args();
|
||||
$f = array($this, 'get');
|
||||
$object = call_user_func_array($f, $args);
|
||||
return !empty($object);
|
||||
}
|
||||
|
||||
function is_new($object)
|
||||
{
|
||||
$id_name = $this->id_name;
|
||||
$id = isset($object->{$id_name}) ? $object->{$id_name} : false;
|
||||
return empty($id);
|
||||
}
|
||||
|
||||
function save($object)
|
||||
{
|
||||
if (empty($object))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
$object = is_array($object) ? $this->create_object($object) : $object;
|
||||
$this->before_save($object);
|
||||
if ($this->is_new($object))
|
||||
{
|
||||
$result = $this->insert($object);
|
||||
}
|
||||
else
|
||||
{
|
||||
$result = $this->update($object);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
function delete($object)
|
||||
{
|
||||
$args = func_get_args();
|
||||
$f = array($this, 'get_where');
|
||||
$db_name = $this->get_db_name();
|
||||
$where = call_user_func_array($f, $args);
|
||||
$sql = "DELETE
|
||||
FROM `{$db_name
|
||||
}
|
||||
|
||||
`.`{$this->table_name
|
||||
}
|
||||
|
||||
`
|
||||
WHERE $where";
|
||||
|
||||
$result = $this->query($sql);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array|object $data
|
||||
* @return object
|
||||
*/
|
||||
public function create_object($data = array())
|
||||
{
|
||||
$data = $data ? $data : array();
|
||||
$data = (object) $data;
|
||||
$class = $this->class_name;
|
||||
if (empty($class))
|
||||
{
|
||||
return clone $data;
|
||||
}
|
||||
$result = new $class();
|
||||
|
||||
foreach ($result as $key => $value)
|
||||
{
|
||||
$result->{$key} = property_exists($data, $key) ? $data->{$key} : null;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function fields($object)
|
||||
{
|
||||
static $result = array();
|
||||
if (!empty($result))
|
||||
{
|
||||
return $result;
|
||||
}
|
||||
|
||||
$db_name = $this->get_db_name($object);
|
||||
$sql = "SELECT *
|
||||
FROM `{$db_name}`.`{$this->table_name}`
|
||||
LIMIT 1";
|
||||
$rs = Database::query($sql, null, __FILE__);
|
||||
while ($field = mysql_fetch_field($rs))
|
||||
{
|
||||
$result[] = $field;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function before_save($object)
|
||||
{
|
||||
//hook
|
||||
}
|
||||
|
||||
protected function update($object)
|
||||
{
|
||||
$id = isset($object->{$this->id_name}) ? $object->{$this->id_name} : false;
|
||||
if (empty($id))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
$items = array();
|
||||
$fields = $this->fields($object);
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
$name = $field->name;
|
||||
if ($name != $this->id_name)
|
||||
{
|
||||
if (property_exists($object, $name))
|
||||
{
|
||||
$value = $object->{$name};
|
||||
$value = $this->format_value($value);
|
||||
$items[] = "$name=$value";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$db_name = $this->get_db_name($object);
|
||||
$sql = "UPDATE `{$db_name}`.`{$this->table_name}` SET ";
|
||||
$sql .= join(', ', $items);
|
||||
$sql .= " WHERE {$this->id_name}=$id";
|
||||
|
||||
$result = $this->execute($sql);
|
||||
if ($result)
|
||||
{
|
||||
$object->{db_name} = $db_name;
|
||||
}
|
||||
return (bool) $result;
|
||||
}
|
||||
|
||||
protected function insert($object)
|
||||
{
|
||||
$id = isset($object->{$this->id_name}) ? $object->{$this->id_name} : false;
|
||||
if (empty($object))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
$values = array();
|
||||
$keys = array();
|
||||
$fields = $this->fields($object);
|
||||
foreach ($fields as $field)
|
||||
{
|
||||
$name = $field->name;
|
||||
if ($name != $this->id_name)
|
||||
{
|
||||
if (property_exists($object, $name))
|
||||
{
|
||||
$value = $object->{$name};
|
||||
$value = is_null($value) ? 'DEFAULT' : $this->format_value($value);
|
||||
$values[] = $value;
|
||||
$keys[] = $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$db_name = $this->get_db_name($object);
|
||||
$sql = "INSERT INTO `{$db_name}`.`{$this->table_name}` ";
|
||||
$sql .= ' (' . join(', ', $keys) . ') ';
|
||||
$sql .= 'VALUES';
|
||||
$sql .= ' (' . join(', ', $values) . ') ';
|
||||
|
||||
$result = $this->execute($sql);
|
||||
if ($result)
|
||||
{
|
||||
$id = mysql_insert_id();
|
||||
$object->{$this->id_name} = $id;
|
||||
$object->{db_name} = $db_name;
|
||||
return $id;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function get_where($_)
|
||||
{
|
||||
$args = func_get_args();
|
||||
if (count($args) == 1)
|
||||
{
|
||||
$arg = reset($args);
|
||||
if (is_numeric($arg))
|
||||
{
|
||||
$id = (int) $arg;
|
||||
if (empty($id))
|
||||
{
|
||||
return '';
|
||||
}
|
||||
$args = array($this->pk_name, $arg);
|
||||
}
|
||||
else if (is_string($arg))
|
||||
{
|
||||
return $arg;
|
||||
}
|
||||
else if (is_array($arg))
|
||||
{
|
||||
$args = $arg;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $arg;
|
||||
}
|
||||
}
|
||||
$items = array();
|
||||
foreach ($args as $key => $val)
|
||||
{
|
||||
$items[] = $key . ' = ' . $this->format_value($val);
|
||||
}
|
||||
return implode(' AND ', $items);
|
||||
}
|
||||
|
||||
protected function format_value($value)
|
||||
{
|
||||
if (is_null($value))
|
||||
{
|
||||
return 'NULL';
|
||||
}
|
||||
if (is_bool($var))
|
||||
{
|
||||
return $value ? '1' : '0';
|
||||
}
|
||||
else if (is_numeric($value))
|
||||
{
|
||||
return empty($value) ? '0' : $value;
|
||||
}
|
||||
else if (is_string($value))
|
||||
{
|
||||
$value = mysql_escape_string($value);
|
||||
return "'$value'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $sql
|
||||
* @return array
|
||||
*/
|
||||
protected function query($sql)
|
||||
{
|
||||
$resource = Database::query($sql, null, __FILE__);
|
||||
if ($resource == false)
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
$result = array();
|
||||
while ($data = mysql_fetch_assoc($resource))
|
||||
{
|
||||
$result[] = $this->create_object($data);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
*/
|
||||
protected function execute($sql)
|
||||
{
|
||||
return Database::query($sql, null, __FILE__);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user