26 defined(
'LGV_ADB_CATCHER' ) or die ( 'Cannot Execute Directly' );
28 if ( !defined( 'LGV_DBF_CATCHER' ) ) {
29 define(
'LGV_DBF_CATCHER', 1 );
32 if ( !defined(
'LGV_ERROR_CATCHER') ) {
33 define(
'LGV_ERROR_CATCHER', 1);
36 require_once(CO_Config::db_class_dir().
'/co_main_db_record.class.php');
38 require_once(CO_Config::badger_shared_class_dir().
'/error.class.php');
68 $in_parameters = NULL,
75 $ret = $this->_pdo_object->preparedExec($in_sql, $in_parameters);
77 $ret = $this->_pdo_object->preparedQuery($in_sql, $in_parameters,
false);
79 }
catch (Exception $exception) {
83 $exception->getFile(),
84 $exception->getLine(),
85 $exception->getMessage());
86 $this->_security_db_object = NULL;
105 if ($this->access_object->god_mode()) {
108 $access_ids = $this->access_object->get_security_ids();
109 $ret =
'((read_security_id=0) OR (read_security_id IS NULL)';
111 if (isset($access_ids) && is_array($access_ids) && count($access_ids)) {
112 foreach ($access_ids as $access_id) {
113 $ret .=
' OR (read_security_id='.intval($access_id).
')';
114 $ret .=
' OR (write_security_id='.intval($access_id).
')';
121 if (($this instanceof
CO_Security_DB) && !$this->access_object->god_mode()) {
122 $ret .=
' AND (id<>'.intval(CO_Config::god_mode_id()).
')';
136 if ($this->access_object->god_mode()) {
139 $access_ids = $this->access_object->get_security_ids();
142 if (isset($access_ids) && is_array($access_ids) && count($access_ids)) {
143 $ret =
'((write_security_id=0) OR (write_security_id IS NULL)';
145 foreach ($access_ids as $access_id) {
146 $ret .=
' OR (write_security_id='.intval($access_id).
')';
152 if (($this instanceof
CO_Security_DB) && !$this->access_object->god_mode()) {
153 $ret .=
' AND (id<>'.intval(CO_Config::god_mode_id()).
')';
185 $classname = trim($in_db_result[
'access_class']);
186 $id = intval($in_db_result[
'id']);
188 if ($classname && $id) {
189 $filename = CO_Config::db_classes_class_dir().
'/'.strtolower($classname).
'.class.php';
191 if (!class_exists($classname)) {
192 if (!file_exists($filename)) {
194 $dir_array = CO_Config::db_classes_extension_class_dir();
196 if (!is_array($dir_array)) {
197 $dir_array = Array($dir_array);
200 foreach ($dir_array as $dir) {
201 $filename = $dir.
'/'.strtolower($classname).
'.class.php';
202 if (file_exists($filename)) {
203 require_once($filename);
210 require_once($filename);
225 if (class_exists($classname)) {
226 if (isset($this->_existing_record_objects[$id])) {
227 $ret = $this->_existing_record_objects[$id];
230 if (method_exists($ret,
'reload_collection')) {
231 $ret->reload_collection();
233 $ret->load_from_db($in_db_result);
236 $ret =
new $classname($this, $in_db_result);
251 $this->_existing_record_objects[$id] = $ret;
274 $this->class_description =
'Abstract Base Class for Database -Should never be instantiated.';
276 $this->access_object = $in_access_object;
278 $this->table_name = NULL;
279 $this->_pdo_object = $in_pdo_object;
280 if (isset($this->_pdo_object)) {
281 $this->_pdo_object->owner_instance = $this;
283 $this->_existing_record_objects = Array();
293 $in_parameters = NULL,
296 return $this->
_execute_query($in_sql, $in_parameters, $exec_only);
310 if ($this->access_object->god_mode()) {
311 $sql =
'SELECT * FROM '.$this->table_name.
' WHERE id>1 ORDER BY id';
334 $predicate .=
' AND ';
336 $predicate =
'true AND ';
339 $sql =
'SELECT access_class FROM '.$this->table_name.
' WHERE '.$predicate.
'id='.intval($in_id);
343 if (isset($ret) && is_array($ret) && count($ret)) {
344 $ret = strval($ret[0][
'access_class']);
362 if (intval($in_id)) {
366 $predicate .=
' AND ';
368 $predicate =
'true AND ';
371 $sql =
'SELECT id FROM '.$this->table_name.
' WHERE '.$predicate.
'id='.intval($in_id);
375 if (isset($ret) && is_array($ret) && count($ret)) {
376 $ret = intval($ret[0][
'id']) == intval($in_id);
399 $predicate .=
' AND ';
401 $predicate =
'true AND ';
404 $sql =
'SELECT * FROM '.$this->table_name.
' WHERE '.$predicate.
'id='.intval($in_id);
408 if (isset($ret) && is_array($ret) && count($ret)) {
429 if (isset($temp) && $temp && is_array($temp) && count($temp) ) {
452 $predicate .=
' AND ';
455 $sql =
'SELECT * FROM '.$this->table_name.
' WHERE '.$predicate.
'(';
458 $id_array = array_map(
'intval', $in_id_array);
459 foreach ($id_array as $id) {
461 if (0 < count($params)) {
465 array_push($params, $id);
476 if (isset($temp) && $temp && is_array($temp) && count($temp) ) {
478 foreach ($temp as $result) {
481 $weregood = $and_write ? $result->user_can_write() : $result->user_can_read();
488 usort($ret,
function($a, $b){
return ($a->id() > $b->id());});
511 $in_this_id = intval($in_this_id);
513 $predicate =
"(read_security_id=$in_this_id)";
520 $sql =
'SELECT * FROM '.$this->table_name.
' WHERE '.$predicate;
522 if (isset($temp) && $temp && is_array($temp) && count($temp) ) {
524 foreach ($temp as $result) {
525 if (isset($result) && is_array($result) && count($result)) {
528 if ($result && $result->user_can_read()) {
529 array_push($ret, $result);
533 usort($ret,
function($a, $b){
return ($a->id() > $b->id());});
551 $access_ids = $this->access_object->get_security_ids();
554 if (isset($access_ids) && is_array($access_ids) && count($access_ids)) {
559 $in_this_id = intval($in_this_id);
561 $predicate =
"(write_security_id=$in_this_id)";
568 $sql =
'SELECT * FROM '.$this->table_name.
' WHERE '.$predicate;
570 if (isset($temp) && $temp && is_array($temp) && count($temp) ) {
572 foreach ($temp as $result) {
575 if ($result && $result->user_can_write()) {
576 array_push($ret, $result);
579 usort($ret,
function($a, $b){
return ($a->id() > $b->id());});
599 $predicate .=
' AND ';
602 $in_record_id = intval($in_record_id);
604 if (0 < $in_record_id) {
606 $sql =
'SELECT read_security_id FROM '.$this->table_name.
' WHERE '.$predicate.
'id='.$in_record_id;
610 if (isset($temp) && $temp && is_array($temp) && (1 == count($temp)) ) {
611 $ret = intval($temp[0]);
613 $sql =
'UPDATE '.$this->table_name.
' SET read_security_id=-2 WHERE id=?';
639 if (isset($params_associative_array) && is_array($params_associative_array) && count($params_associative_array)) {
640 $access_ids = $this->access_object->get_security_ids();
641 $params_associative_array[
'last_access'] = date(
'Y-m-d H:i:s');
642 if (isset($access_ids) && is_array($access_ids) && count($access_ids)) {
646 $predicate .=
' AND ';
649 $id = isset($params_associative_array[
'id']) ? intval($params_associative_array[
'id']) : 0;
653 $sql =
'SELECT * FROM '.$this->table_name.
' WHERE '.$predicate.
'id='.$id;
657 if (isset($temp) && $temp && is_array($temp) && (1 == count($temp)) ) {
658 $sql =
'UPDATE '.$this->table_name.
'';
659 if (!CO_Config::use_personal_tokens()) {
660 unset($params_associative_array[
'personal_ids']);
663 unset($params_associative_array[
'id']);
665 $params = array_values($params_associative_array);
666 $keys = array_keys($params_associative_array);
667 $set_sql =
''.implode(
'=?,', $keys).
'=?';
669 $sql .=
' SET '.$set_sql.
' WHERE ('.$predicate.
'id='.$id.
')';
672 if (isset($this->_existing_record_objects[$id])) {
673 $this->_existing_record_objects[$id]->last_access = strtotime($params_associative_array[
'last_access']);
678 $sql =
'SELECT * FROM '.$this->table_name.
' WHERE id='.$id;
682 if (isset($temp) && $temp && is_array($temp) && count($temp)) {
689 $sql =
'SELECT * FROM '.$this->table_name.
' WHERE id=1';
693 if (isset($temp) && $temp && is_array($temp) && count($temp) ) {
694 $sql =
'INSERT INTO '.$this->table_name.
'';
695 if (!CO_Config::use_personal_tokens()) {
696 unset($params_associative_array[
'personal_ids']);
698 unset($params_associative_array[
'id']);
701 if (!isset($params_associative_array[
'read_security_id'])) {
702 $params_associative_array[
'read_security_id'] = 0;
706 if (!isset($params_associative_array[
'write_security_id']) && isset($access_ids[1])) {
707 $params_associative_array[
'write_security_id'] = $access_ids[1];
710 foreach ($params_associative_array as $key => $value) {
711 if (isset($temp[$key])) {
712 $temp[$key] = isset($value) ? $value : NULL;
716 $keys = array_keys($params_associative_array);
718 $keys_sql =
'('.implode(
',', $keys).
')';
719 $values_sql =
'('.implode(
',', array_map(
function($in){
return '?';}, $keys)).
') RETURNING id;';
721 $sql .=
" $keys_sql VALUES $values_sql";
722 $this->
execute_query($sql, array_values($params_associative_array),
true);
726 $ret = $this->_pdo_object->last_insert;
758 $predicate .=
' AND ';
762 $sql =
'SELECT id FROM '.$this->table_name.
' WHERE ('.$predicate.
'id='.$id.
')';
765 if (!$this->error && isset($temp) && $temp && is_array($temp) && (1 == count($temp))) {
766 $sql =
'DELETE FROM '.$this->table_name.
' WHERE ('.$predicate.
'id='.$id.
')';
768 if (!$temp || $this->error) {
774 $sql =
'SELECT id FROM '.$this->table_name.
' WHERE id='.$id;
776 if (!$this->error && isset($temp) && is_array($temp) && (0 == count($temp))) {
778 if (isset($this->_existing_record_objects[$id])) {
779 $this->_existing_record_objects[$id]->danger_will_robinson_danger_clear_id();
780 unset($this->_existing_record_objects[$id]);
if(!defined( 'LGV_DBF_CATCHER')) if(!defined( 'LGV_ERROR_CATCHER'))
_create_read_security_predicate()
execute_query( $in_sql, $in_parameters=NULL, $exec_only=false)
get_all_writeable_records( $in_this_id=NULL)
_instantiate_record( $in_db_result)
get_single_record_by_id( $in_id, $and_write=false)
get_all_readable_records( $open_only=false, $in_this_id=NULL)
get_access_class_by_id( $in_id)
$_existing_record_objects
get_multiple_records_by_id( $in_id_array, $and_write=false)
__construct( $in_pdo_object, $in_access_object)
write_record( $params_associative_array)
_create_security_predicate( $write=false)
_create_write_security_predicate()
_execute_query( $in_sql, $in_parameters=NULL, $exec_only=false)
get_single_raw_row_by_id( $in_id, $and_write=false)
can_i_see_this_record( $in_id)
lock_record( $in_record_id)
static $db_error_code_class_file_not_found
static $db_error_code_class_not_created
static $pdo_error_code_failed_delete_attempt
static $pdo_error_code_illegal_delete_attempt
static $pdo_error_code_failed_to_open_security_db
static $pdo_error_code_illegal_write_attempt
static $pdo_error_name_failed_to_open_security_db
static $pdo_error_desc_illegal_write_attempt
static $db_error_name_class_file_not_found
static $pdo_error_desc_failed_to_open_security_db
static $pdo_error_desc_illegal_delete_attempt
static $db_error_desc_class_not_created
static $pdo_error_name_illegal_delete_attempt
static $db_error_desc_class_file_not_found
static $pdo_error_desc_failed_delete_attempt
static $db_error_name_class_not_created
static $pdo_error_name_failed_delete_attempt
static $pdo_error_name_illegal_write_attempt
This class provides a general error report, with file, method and error information.