26 defined(
'LGV_DBF_CATCHER' ) or die ( 'Cannot Execute Directly' );
28 if ( !defined('LGV_SDBN_CATCHER') ) {
29 define(
'LGV_SDBN_CATCHER', 1);
32 require_once(CO_Config::db_class_dir().
'/co_security_node.class.php');
52 protected static function _random_str($length, $keyspace =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
55 $max = mb_strlen($keyspace,
'8bit') - 1;
56 for ($i = 0; $i < $length; ++$i) {
57 $pieces []= $keyspace[random_int(0, $max)];
59 return implode(
'', $pieces);
70 $temp_api_key .=
' - '.strval(microtime(
true));
73 if (isset(CO_Config::$api_key_includes_ip_address) && CO_Config::$api_key_includes_ip_address) {
74 $temp_api_key .=
' - '.strtolower(strval($_SERVER[
'REMOTE_ADDR']));
77 $this->_api_key = strval($temp_api_key);
92 $default_setup = parent::_default_setup();
96 $default_setup[
'personal_ids'] = (CO_Config::use_personal_tokens() && (NULL !=
$this->_personal_ids)) ? $this->_personal_ids :
'';
97 $default_setup[
'ids'] = (NULL !=
$this->_ids) ? $this->_ids :
'';
99 return $default_setup;
111 $ret = parent::_build_parameter_array();
113 if (NULL == $this->_personal_ids) {
114 $this->_personal_ids = [];
119 $personal_ids_as_string_array = Array();
120 if (CO_Config::use_personal_tokens()) {
121 $personal_ids_as_int = array_map(
'intval', $this->_personal_ids);
122 sort($personal_ids_as_int);
124 foreach ($this->_personal_ids as $id) {
125 array_push($personal_ids_as_string_array, strval($id));
128 $personal_id_list_string = trim(implode(
',', $personal_ids_as_string_array));
129 $ret[
'personal_ids'] = $personal_id_list_string ? $personal_id_list_string : NULL;
132 $ids_as_int = array_map(
'intval', $this->_ids);
135 $ids_as_string_array = Array();
136 foreach ($this->_ids as $id) {
137 if ($id != $this->
id() && (isset($personal_ids_as_int) && !in_array($id, $personal_ids_as_int))) {
138 array_push($ids_as_string_array, strval($id));
139 } elseif ($id != $this->
id()) {
140 array_push($ids_as_string_array, strval($id));
144 $id_list_string = trim(implode(
',', $ids_as_string_array));
146 $ret[
'ids'] = $id_list_string ? $id_list_string : NULL;
148 if ($this->_override_access_class) {
149 $ret[
'access_class'] =
'CO_Security_ID';
150 $ret[
'object_name'] = NULL;
152 $this->context = NULL;
153 $this->_override_access_class =
false;
165 $in_db_result = NULL,
168 $in_personal_ids = NULL
170 $this->login_id = $in_login_id;
171 $this->_override_access_class =
false;
172 parent::__construct($in_db_object, $in_db_result, $in_ids);
173 $this->class_description =
'This is a security class for individual logins.';
176 if (isset($in_ids) && is_array($in_ids) && count($in_ids)) {
177 $in_db_result[
'ids'] = implode(
',', $in_ids);
180 if (CO_Config::use_personal_tokens() && isset($in_personal_ids) && is_array($in_personal_ids) && count($in_personal_ids)) {
181 $in_db_result[
'personal_ids'] = implode(
',', $in_ids);
184 if (!isset($this->context)) {
185 $this->context = Array();
188 if (!isset($this->context[
'lang'])) {
192 if (isset($in_db_result[
'api_key'])) {
193 $this->_api_key = $in_db_result[
'api_key'];
196 if (intval($this->
id()) == intval(CO_Config::god_mode_id())) {
198 $this->instance_description =
'GOD MODE: '.(isset($this->name) && $this->name ?
"$this->name (".$this->login_id.
")" :
"Unnamed Login Node (".$this->login_id.
")");
200 $this->instance_description = isset($this->name) && $this->name ?
"$this->name (".$this->login_id.
")" :
"Unnamed Login Node (".$this->login_id.
")";
206 if (isset($access_object) && ($access_object->god_mode() || ($access_object->get_login_id() == $this->_id))) {
207 $this->_ids = Array($this->
id());
209 if (isset($in_db_result[
'ids']) && $in_db_result[
'ids']) {
210 $temp = $in_db_result[
'ids'];
211 if (isset ($temp) && $temp) {
212 $tempAr = explode(
',', $temp);
213 if (is_array($tempAr) && count($tempAr)) {
214 $tempAr = array_unique(array_map(
'intval', $tempAr));
215 $tempAr = array_merge($this->_ids, $tempAr);
218 if (isset($tempAr) && is_array($tempAr) && count($tempAr)) {
219 $access_ids = $access_object->get_security_ids();
220 if ($access_object->god_mode() || (isset($access_ids) && is_array($access_ids) && count($access_ids))) {
221 foreach($tempAr as $id) {
222 if (($access_object->god_mode() || (in_array($id, $access_ids))) && !in_array($id, $this->_ids)) {
227 $this->_ids = $tempAr;
234 $this->_personal_ids = NULL;
236 if (CO_Config::use_personal_tokens() && isset($in_db_result[
'personal_ids']) && $in_db_result[
'personal_ids']) {
237 $temp = $in_db_result[
'personal_ids'];
238 if (isset ($temp) && $temp) {
239 $tempAr = explode(
',', $temp);
240 if (is_array($tempAr) && count($tempAr)) {
241 $tempAr = array_unique(array_map(
'intval', $tempAr));
243 if (isset($tempAr) && is_array($tempAr) && count($tempAr)) {
244 $this->_personal_ids = $tempAr;
259 $ret = parent::load_from_db($in_db_result);
260 $this->_personal_ids = NULL;
261 $this->_ids = Array($this->
id());
264 if (!isset($this->context)) {
265 $this->context = Array();
268 if (!isset($this->context[
'lang'])) {
272 $this->class_description =
'This is a security class for individual logins.';
274 if (isset($in_db_result[
'login_id'])) {
275 $this->login_id = $in_db_result[
'login_id'];
276 $this->instance_description = isset($this->name) && $this->name ?
"$this->name (".$this->login_id.
")" :
"Unnamed Login Node (".$this->login_id.
")";
279 if (isset($in_db_result[
'api_key'])) {
280 $this->_api_key = $in_db_result[
'api_key'];
283 if (isset($in_db_result[
'ids']) && $in_db_result[
'ids']) {
284 if ($this->_db_object) {
285 if (isset($in_db_result[
'ids']) && $in_db_result[
'ids']) {
286 $temp = $in_db_result[
'ids'];
287 if (isset ($temp) && $temp) {
288 $tempAr = explode(
',', $temp);
289 if (is_array($tempAr) && count($tempAr)) {
290 $tempAr = array_map(
'intval', $tempAr);
292 $tempAr = array_unique(array_merge($this->_ids, $tempAr));
293 if (isset($tempAr) && is_array($tempAr) && count($tempAr)) {
294 $this->_ids = $tempAr;
302 if (CO_Config::use_personal_tokens() && isset($in_db_result[
'personal_ids']) || isset($in_db_result[
'personal_ids'])) {
303 $temp = $in_db_result[
'personal_ids'];
304 if (isset ($temp) && $temp) {
305 $tempAr = explode(
',', $temp);
306 if (is_array($tempAr) && count($tempAr)) {
307 $tempAr = array_unique(array_map(
'intval', $tempAr));
309 if (isset($tempAr) && is_array($tempAr) && count($tempAr)) {
310 $this->_personal_ids = $tempAr;
331 $in_ids_array = array_map(
'intval', $in_ids_array);
334 if ($this->
get_access_object()->god_mode() || (isset($id_pool) && is_array($id_pool) && count($id_pool))) {
337 foreach($this->_ids as $id) {
338 if (!$this->
get_access_object()->god_mode() && (isset($id) && (0 < $id) && !in_array($id, $id_pool))) {
352 if ($this->
get_access_object()->god_mode() || (isset($in_ids_array) && is_array($in_ids_array) && count($in_ids_array))) {
353 $temp_ids = array_map(
'intval', $in_ids_array);
355 foreach($temp_ids as $in_id) {
356 if (($in_id != $this->
id()) && ($this->
get_access_object()->god_mode() || in_array($in_id, $id_pool))) {
361 $this->_ids = $new_ids;
364 $this->_ids = Array();
399 $in_id = intval($in_id);
403 if ($this->
get_access_object()->god_mode() || (isset($id_pool) && is_array($id_pool) && count($id_pool))) {
405 if (($this->
get_access_object()->god_mode() || (in_array($in_id, $id_pool)) || ($this->_added_new_id == $in_id)) && ($in_id != $this->
id())) {
406 if (!isset($this->_ids) || !is_array($this->_ids) || !count($this->_ids)) {
407 $this->_ids = Array(intval($in_id));
409 $this->_ids[] = $in_id;
410 $this->_ids = array_unique($this->_ids);
415 if ($in_id != $this->
id()) {
455 if ($this->
get_access_object()->god_mode() || (isset($id_pool) && is_array($id_pool) && count($id_pool) && in_array($in_id, $id_pool))) {
456 if (isset($this->_ids) && is_array($this->_ids) && count($this->_ids) && $this->
user_can_edit_ids()) {
457 $new_array = Array();
459 foreach($this->_ids as $id) {
461 array_push($new_array, $id);
467 $ret = $this->
set_ids($new_array);
505 foreach ($this->_ids as $id) {
506 if (in_array($id, $my_ids)) {
507 array_push($ret, $id);
540 $personal_ids_temp = array_unique(array_map(
'intval', $in_personal_ids));
541 $this->_personal_ids = [];
544 if (CO_Config::use_personal_tokens() && isset($access_object) && $access_object->god_mode()) {
545 if (0 < count($personal_ids_temp)) {
550 foreach($personal_ids_temp as $id) {
552 if (($key = array_search($id, $my_ids)) !==
false) {
553 unset($my_ids[$key]);
555 if (!$this->
get_access_object()->is_this_a_login_id($id) || in_array($id, $this->_personal_ids)) {
556 array_push($personal_ids, $id);
559 $this->_ids = $my_ids;
561 $this->_personal_ids = $personal_ids;
577 if (!CO_Config::use_personal_tokens()) {
586 if (isset($this->_personal_ids) && is_array($this->_personal_ids) && count($this->_personal_ids)) {
587 foreach ($this->_personal_ids as $id) {
588 if (in_array($id, $my_ids)) {
589 array_push($ret, $id);
604 $ret = $this->context[
'hashed_password'];
606 if ($in_password_to_crypt) {
607 if (strlen($in_password_to_crypt) >= CO_Config::$min_pw_len) {
608 $ret = password_hash($in_password_to_crypt, PASSWORD_DEFAULT);
622 $in_hashed_password = NULL,
623 $in_raw_password = NULL,
624 $in_dont_create_new_api_key =
false
627 if (isset($this->login_id) && $this->login_id && ($this->login_id == $in_login_id)) {
629 if ($this->
id() == CO_Config::god_mode_id()) {
630 if ((
"" != $in_hashed_password) && ($in_hashed_password == $api_key)) {
633 if ($in_raw_password && !$in_dont_create_new_api_key && isset(CO_Config::$block_logins_for_valid_api_key) && CO_Config::$block_logins_for_valid_api_key && $api_key) {
636 $ret = ($in_raw_password == CO_Config::god_mode_password());
641 if ($in_raw_password && !$in_dont_create_new_api_key && isset(CO_Config::$block_logins_for_valid_api_key) && CO_Config::$block_logins_for_valid_api_key && $api_key) {
643 } elseif (isset($this->context[
'hashed_password']) && $this->context[
'hashed_password']) {
645 if ($in_hashed_password) {
655 if ($ret && !$in_dont_create_new_api_key) {
657 $this->
_set_up_api_key($this->
id() == CO_Config::god_mode_id() ? 40 : 32);
668 return intval(CO_Config::god_mode_id()) == intval($this->
id());
676 return $this->
id() == CO_Config::god_mode_id();
692 return $this->context[
'lang'];
704 $this->context[
'lang'] = strtolower(trim(strval($in_lang_id)));
720 if ($access_instance && method_exists($access_instance,
'get_user_from_login')) {
721 $ret = $access_instance->get_user_from_login($this->
id());
755 if ($ret && !$this->error) {
774 $timeout = floatval($this->
i_am_a_god() ? CO_Config::$god_session_timeout_in_seconds : CO_Config::$session_timeout_in_seconds);
776 if ( 0 < $timeout ) {
777 if (isset($this->_api_key) && $this->_api_key) {
778 list($api_key, $api_time) = explode(
' - ', trim($this->_api_key));
779 $ret = $timeout - ceil((microtime(
true) - floatval($api_time)) * 10000) / 10000;
796 if (isset($this->_api_key) && $this->_api_key) {
797 $api_expl = explode(
' - ', trim($this->_api_key));
801 $timeout = floatval($this->
i_am_a_god() ? CO_Config::$god_session_timeout_in_seconds : CO_Config::$session_timeout_in_seconds);
805 if (isset($api_expl[1]) && ((0 > $timeout) || ((microtime(
true) - floatval($api_expl[1])) <= $timeout))) {
806 if (isset(CO_Config::$api_key_includes_ip_address) && CO_Config::$api_key_includes_ip_address) {
807 $my_ip = strtolower(strval($_SERVER[
'REMOTE_ADDR']));
808 if (isset($api_expl[2])) {
809 if ($api_expl[2] == $my_ip) {
826 } elseif ($api_expl[0]) {
844 if (isset($this->_api_key) && $this->_api_key) {
845 list($api_key, $api_time) = explode(
' - ', trim($this->_api_key));
846 $ret = ceil(microtime(
true) - floatval($api_time));
859 $this->_api_key = NULL;
872 $ret = $this->
get_access_object()->add_personal_token_from_current_login($this->_id, $in_id);
892 $ret = $this->
get_access_object()->remove_personal_token_from_this_login($this->_id, $in_id);
929 $my_write_item = intval($this->write_security_id);
934 if (isset($ids) && is_array($ids) && count($ids)) {
935 $ret = in_array($my_write_item, $ids);
950 if ($this->
id() != CO_Config::god_mode_id()) {
955 $user_object->set_login(NULL);
958 $this->read_security_id = 0;
959 $this->write_security_id = -1;
960 $this->api_key = NULL;
961 $this->context = NULL;
963 $this->login_id = NULL;
964 $this->_ids = Array();
965 $this->_override_access_class =
true;
if(!defined( 'LGV_SDBN_CATCHER'))
$_id
This is the within-table unique ID of this record.
set_lang( $in_lang_id=NULL)
$_api_key
This is an API key for REST.
$_personal_ids
These are personal IDs (special IDs, unique to the login).
is_login_valid( $in_login_id, $in_hashed_password=NULL, $in_raw_password=NULL, $in_dont_create_new_api_key=false)
_set_up_api_key( $key_length)
get_crypted_password( $in_password_to_crypt=NULL)
$_ids
These are security tokens, available to this ID.
add_personal_token_from_current_login( $in_id)
remove_personal_token_from_this_login( $in_id)
load_from_db($in_db_result)
set_personal_ids( $in_personal_ids=[])
is_api_key_valid( $in_api_key)
$_override_access_class
This is a special "one-shot" semaphore telling the save to override the access class.
get_logins_that_have_any_of_my_ids()
set_password_from_cleartext( $in_cleartext_password)
get_api_key_age_in_seconds()
__construct( $in_db_object=NULL, $in_db_result=NULL, $in_login_id=NULL, $in_ids=NULL, $in_personal_ids=NULL)
static _random_str($length, $keyspace='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
static $login_error_code_attempt_to_delete_god
static $login_error_code_api_key_mismatch
static $db_error_code_user_not_authorized
static $login_error_code_api_key_invalid
static $db_error_name_user_not_authorized
static $db_error_desc_user_not_authorized
static $login_error_name_attempt_to_delete_god
static $login_error_desc_attempt_to_delete_god
static $login_error_desc_api_key_mismatch
static $login_error_name_api_key_invalid
static $login_error_name_api_key_mismatch
static $login_error_desc_api_key_invalid
This class provides a general error report, with file, method and error information.