BAOBAB
co_cobra.class.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************************************************************************/
26 defined( 'LGV_ACCESS_CATCHER' ) or die ( 'Cannot Execute Directly' ); // Makes sure that this file is in the correct context.
27 
28 define('__COBRA_VERSION__', '1.1.2.3000');
29 
30 require_once(CO_Config::chameleon_main_class_dir().'/co_chameleon.class.php');
31 
32 if ( !defined('LGV_LANG_CATCHER') ) {
33  define('LGV_LANG_CATCHER', 1);
34 }
35 
36 require_once(CO_Config::cobra_lang_class_dir().'/common.inc.php');
37 
38 /***************************************************************************************************************************/
46 class CO_Cobra {
47  private $_chameleon_instance = NULL;
48 
49  var $version;
50 
51  /***********************/
59  static function make_cobra($in_chameleon_instance) {
60  $ret = NULL;
61 
62  // We must have a valid CHAMELEON instance that is logged in. The login user must be a COBRA Manager user (the standard logins cannot use COBRA).
63  if (isset($in_chameleon_instance)
64  && ($in_chameleon_instance instanceof CO_Chameleon)
65  && $in_chameleon_instance->security_db_available()
66  && ($in_chameleon_instance->god_mode() || ($in_chameleon_instance->get_login_item() instanceof CO_Login_Manager))) {
67  $ret = new CO_Cobra($in_chameleon_instance);
68  } elseif (isset($in_chameleon_instance) && ($in_chameleon_instance instanceof CO_Chameleon)) {
72  } else {
76  }
77 
78  return $ret;
79  }
80 
81  /***********************************************************************************************************************/
82  /***********************/
88  private function __construct( $in_chameleon_instance = NULL
89  ) {
90  $this->_chameleon_instance = $in_chameleon_instance;
91  $this->version = __COBRA_VERSION__;
92  }
93 
94  /***********************/
102  private function _make_security_token() {
103  $ret = 0;
104 
105  $manager = $this->_chameleon_instance->get_login_item();
106  if ($this->_chameleon_instance->god_mode() || ($manager instanceof CO_Login_Manager)) {
107  $new_token = $this->_chameleon_instance->make_new_blank_record('CO_Security_ID');
108  if (isset($new_token) && ($new_token instanceof CO_Security_ID)) {
109  $new_id = $new_token->id();
110  if (!$this->_chameleon_instance->error) {
111  $ret = $new_id;
112  } else { // We were unable to create the token.
113  $this->error = $this->_chameleon_instance->error;
114  }
115  } else { // Token object did not get instantiated.
119  }
120  } else { // Should never happen, but what the hell...
124  }
125 
126  return $ret;
127  }
128 
129  /***********************/
135  private function _create_this_many_personal_ids($in_count
136  ) {
137  $ret = [];
138 
139  for ($index = 0; $index < $in_count; $index++) {
140  $ret[] = $this->_make_security_token();
141  }
142 
143  return $ret;
144  }
145 
146  /***********************/
153  protected function _convert_login( $in_login_id,
154  $in_is_login_manager = false
155  ) {
156  $ret = NULL;
157  $working_login = $this->_chameleon_instance->get_login_item_by_login_string($in_login_id); // This is the login we are changing.
158  $manager = $this->_chameleon_instance->get_login_item(); // This is our current login.
159  // Make sure we are a login manager, first, and that we have write permission on the user.
160  if ($working_login && $working_login->user_can_write() && ($manager instanceof CO_Login_Manager || $manager->is_god())) {
161  // Now, make sure that we're actually making a change.
162  if (($in_is_login_manager && !$working_login->is_manager()) || (!$in_is_login_manager && $working_login->is_manager())) {
163  $className = $in_is_login_manager ? 'CO_Login_Manager' : 'CO_Cobra_Login';
164  $user_object = $this->get_user_from_login($working_login->id()); // Get this, so we can modify it.
165 
166  // Create a new blank instance.
167  $new_login_object = $this->_chameleon_instance->make_new_blank_record($className);
168 
169  if (isset($new_login_object) && ($new_login_object instanceof CO_Cobra_Login)) {
170  $original_login_id = $working_login->id();
171  $original_ids = $working_login->ids();
172  if (!isset($original_ids)) {
173  $original_ids = [];
174  }
175 
176  array_push($original_ids, $original_login_id); // We will add this to the new login's IDs.
177  $original_ids = array_unique($original_ids);
178 
179  asort($original_ids);
180 
181  $new_login_object->login_id = $working_login->login_id;
182  $new_login_object->name = $working_login->name;
183  $new_login_object->context = $working_login->context;
184  $new_login_object->set_ids($original_ids);
185  // OK. At this point, we have a new login that is of the type that we want. It is different from the previous one, and has all the relevant data.
186  // Now, we will delete the old one, and then replace it with the new one.
187  if ($working_login->delete_from_db()) {
188  if ($new_login_object->update_db()) {
189  $new_id = $new_login_object->id();
190  if (method_exists($manager, 'add_new_login_id')) { // We only add the ID, if we are a manager object. God doesn't need this.
191  $manager->add_new_login_id($new_id);
192  }
193  $new_login_object->read_security_id = $new_id;
194  $new_login_object->write_security_id = $new_id;
195  if ($new_login_object->update_db()) {
196  $user_object->write_security_id = $new_id;
197  $user_object->set_login($new_id);
198  if ($user_object->update_db()) {
199  $ret = $new_login_object; // If everything went well, to this point, we update the return.
200  } else {
201  $this->error = $user_object->error;
202  }
203  } else {
204  $this->error = $new_login_object->error;
205  }
206  } else {
207  $this->error = $new_login_object->error;
208  }
209  } else {
210  $this->error = $working_login->error;
211  }
212  }
213  }
214  }
215 
216  return $ret;
217  }
218 
219  /***********************/
226  protected function _create_new_login( $in_login_id,
227  $in_cleartext_password,
228  $in_create_this_many_personal_ids = 0,
229  $in_security_token_ids = NULL,
230  $in_is_login_manager = false
231  ) {
232  $ret = NULL;
233 
234  if (isset($in_login_id) && !$this->_chameleon_instance->check_login_exists_by_login_string($in_login_id)) {
235  $manager = $this->_chameleon_instance->get_login_item();
236  if ($manager instanceof CO_Login_Manager || $manager->is_god()) { // Make sure we are a login manager, first.
237  $use_these_ids = Array();
238  // Next, see if they provided IDs. If so, we remove any that we don't own.
239  if (isset($in_security_token_ids) && is_array($in_security_token_ids) && count($in_security_token_ids)) {
240  $my_ids = $this->get_security_ids();
241 
242  foreach ($in_security_token_ids as $id) {
243  if (in_array($id, $my_ids)) {
244  array_push($use_these_ids, $id);
245  }
246  }
247  // At this point, only IDs that we have available are in the array.
248  }
249 
250  $className = $in_is_login_manager ? 'CO_Login_Manager' : 'CO_Cobra_Login';
251 
252  $new_login_object = $this->_chameleon_instance->make_new_blank_record($className);
253 
254  if (isset($new_login_object) && ($new_login_object instanceof CO_Cobra_Login)) {
255  $new_login_object->login_id = $in_login_id;
256  if (strlen($in_cleartext_password) >= CO_Config::$min_pw_len) {
257  $new_login_object->context['hashed_password'] = password_hash($in_cleartext_password, PASSWORD_DEFAULT);
258 
259  if (!$new_login_object->update_db()) {
260  $this->error = $new_login_object->error;
261  $new_login_object->delete_from_db();
262  $new_login_object = NULL;
263  } else {
264  $new_id = $new_login_object->id();
265  if (method_exists($manager, 'add_new_login_id')) {
266  $manager->add_new_login_id($new_id);
267  }
268  if ($new_login_object->set_read_security_id($new_id)) {
269  if ($new_login_object->set_write_security_id($new_id)) {
270  $new_ids = [];
271  foreach ($use_these_ids as $id) {
272  if (in_array($id, $my_ids) && ($id != $new_id)) {
273  array_push($new_ids, $id);
274  }
275  }
276 
277  if ($new_login_object->set_ids($new_ids)) {
278  if (CO_Config::use_personal_tokens() && (0 < $in_create_this_many_personal_ids)) {
279  $new_personal_ids = $this->_create_this_many_personal_ids($in_create_this_many_personal_ids);
280  $new_login_object->set_personal_ids($new_personal_ids);
281  if (!$new_login_object->error) {
282  $ret = $new_login_object;
283  } else {
284  $this->error = $new_login_object->error;
285  $new_login_object->delete_from_db();
286  $new_login_object = NULL;
287  }
288  }
289 
290  $ret = $new_login_object;
291  } else {
292  $this->error = $new_login_object->error;
293  $new_login_object->delete_from_db();
294  $new_login_object = NULL;
295  }
296  } else {
297  $this->error = $new_login_object->error;
298  $new_login_object->delete_from_db();
299  $new_login_object = NULL;
300  }
301  } else {
302  $this->error = $new_login_object->error;
303  $new_login_object->delete_from_db();
304  $new_login_object = NULL;
305  }
306  }
307  } else {
308  $new_login_object->delete_from_db();
309  $new_login_object = NULL;
313  }
314  } else {
315  if (isset($this->_chameleon_instance->error)) {
316  $this->error = $this->_chameleon_instance->error;
317  } else {
321  }
322  }
323 
324  } else {
328  }
329  } elseif (isset($in_login_id)) {
333  } else {
337  }
338 
339  return $ret;
340  }
341 
342  /***********************/
346  public function get_chameleon_instance() {
348  }
349 
350  /***********************/
354  public function get_security_ids() {
355  return $this->_chameleon_instance->get_security_ids();
356  }
357 
358  /***********************/
368  public function set_personal_ids( $in_login_id,
369  $in_personal_ids = []
370  ) {
371  $target = $this->_chameleon_instance->get_login_item($in_login_id);
372  if (CO_Config::use_personal_tokens() && ($target instanceof CO_Security_Login) && $this->_chameleon_instance->god_mode()) {
373  return $target->set_personal_ids($in_personal_ids);
374  }
375 
376  return [];
377  }
378 
379  /***********************/
385  public function make_standalone_user() {
386  $user = NULL;
387 
388  if ($this->_chameleon_instance->god_mode() || ($this->_chameleon_instance->get_login_item() instanceof CO_Login_Manager)) { // We have to be a manager to create a user.
389  $user = $this->_chameleon_instance->make_new_blank_record('CO_User_Collection');
390 
391  if ($user) {
392  if (!isset($user->error)) {
393  $user->set_read_security_id(1); // Users default to 1 (only logged-in users can see).
394  $user->set_write_security_id($this->_chameleon_instance->get_login_item()->id()); // Make sure that only we can modify this record.
395  if (isset($user->error)) {
396  $this->error = $user->error;
397  $user->delete_from_db();
398  $user = NULL;
399  }
400  } else {
401  $this->error = $user->error;
402  $user->delete_from_db();
403  $user = NULL;
404  }
405  } else {
409  }
410  } elseif (!($this->_chameleon_instance->get_login_item() instanceof CO_Login_Manager)) {
414  }
415 
416  return $user;
417  }
418 
419  /***********************/
427  public function get_login_instance( $in_login_id
428  ) {
429  $ret = NULL;
430 
431  if (isset($in_login_id) && $in_login_id) {
432  $ret = $this->_chameleon_instance->get_login_item_by_login_string($in_login_id);
433 
434  $this->error = $this->_chameleon_instance->error;
435  }
436 
437  return $ret;
438  }
439 
440  /***********************/
448  public function get_user_from_login( $in_login_id = NULL,
449  $in_make_user_if_necessary = false
450  ) {
451  $user = $this->_chameleon_instance->get_user_from_login($in_login_id); // First, see if it's already a thing.
452  if (!$user && $in_make_user_if_necessary && ($this->_chameleon_instance->god_mode() || ($this->_chameleon_instance->get_login_item() instanceof CO_Login_Manager))) { // If not, we will create a new one, based on the given login. We must be a manager.
453  if (isset($in_login_id) && (0 < intval($in_login_id))) { // See if they seek a different login.
454  $login_id = intval($in_login_id);
455  }
456  // Assuming all is well, we need to create a new user. We have to be a login manager to do this.
457  if (isset($in_login_id) && (0 < intval($in_login_id))) {
458  $login_item = $this->_chameleon_instance->get_login_item($in_login_id);
459 
460  if (isset($login_item) && ($login_item instanceof CO_Security_Login)) {
461  if (!$this->_chameleon_instance->check_user_exists($in_login_id)) {
462  $user = $this->make_standalone_user();
463 
464  if ($user) {
465  $user->set_login($in_login_id); // We set the user's login instance to the login instance we're using as the basis.
466  if (isset($user->error)) {
467  $this->error = $user->error;
468  $user->delete_from_db();
469  $user = NULL;
470  } else {
471  $user->set_write_security_id($in_login_id); // Make sure the user can modify their own record.
472  if (isset($user->error)) {
473  $this->error = $user->error;
474  $user->delete_from_db();
475  $user = NULL;
476  }
477  }
478  } else {
482  }
483  } else {
487  }
488  } else {
492  }
493  } else {
497  }
498  } elseif (!($this->_chameleon_instance->get_login_item() instanceof CO_Login_Manager)) {
502  }
503 
504  return $user;
505  }
506 
507  /***********************/
514  public function create_new_standard_login( $in_login_id,
515  $in_cleartext_password,
516  $in_create_this_many_personal_ids = 0
517  ) {
518  return $this->_create_new_login($in_login_id, $in_cleartext_password, $in_create_this_many_personal_ids);
519  }
520 
521  /***********************/
528  public function create_new_manager_login( $in_login_id,
529  $in_cleartext_password,
530  $in_create_this_many_personal_ids = 0
531  ) {
532  return $this->_create_new_login($in_login_id, $in_cleartext_password, $in_create_this_many_personal_ids, NULL, true);
533  }
534 
535  /***********************/
543  public function convert_login( $in_login_id,
544  $in_is_login_manager = false
545  ) {
546  return $this->_convert_login($in_login_id, $in_is_login_manager);
547  }
548 
549  /***********************/
557  public function delete_login( $in_login_id,
558  $also_delete_user = false
559  ) {
560  $ret = false;
561 
562  $cobra_login_instance = $this->get_login_instance($in_login_id);
563 
564  if ($cobra_login_instance) {
565  $cobra_user_instance = NULL;
566 
567  if ($also_delete_user) {
568  $cobra_user_instance = $this->get_user_from_login($cobra_login_instance->id());
569  }
570 
571  $ret = $cobra_login_instance->delete_from_db();
572 
573  if ($ret && $cobra_user_instance) {
574  $ret = $cobra_user_instance->delete_from_db();
575  }
576  }
577 
578  return $ret;
579  }
580 
581  /***********************/
585  public function get_all_logins( $and_write = false,
586  $in_login_id = NULL,
587  $in_login_integer_id = NULL
588  ) {
589  if (!$this->_chameleon_instance->god_mode()) { // Definitely won't look at this unless we are God.
590  $in_login_id = NULL;
591  $in_login_integer_id = 0;
592  } else {
593  $in_login_id = trim(strval($in_login_id));
594 
595  if (!$in_login_id) { // String login ID trumps integer.
596  $in_login_integer_id = intval($in_login_integer_id);
597  } else {
598  $item = $this->_chameleon_instance->get_login_item_by_login_string($in_login_id);
599  $in_login_id = NULL;
600  $in_login_integer_id = $item->id();
601  }
602  }
603 
604  // If both $in_login_id and $in_login_integer_id are unspecified, then we'll find every login we can see.
605  // If they are specified (which means we're God), then we filter for only the
606  $id_list = Array();
607  $results = $this->_chameleon_instance->get_all_login_objects($and_write);
608  if (isset($results) && is_array($results) && count($results)) {
609  foreach ($results as $result) {
610  if (!$in_login_integer_id || ($in_login_integer_id == $result->id())) {
611  $id_list[] = $result->id();
612  foreach ($result->ids() as $id) {
613  if (($id != CO_Config::god_mode_id()) || (($id == CO_Config::god_mode_id()) && ($in_login_integer_id == CO_Config::god_mode_id()))) {
614  $id_list[] = $id;
615  }
616  }
617  }
618  }
619  }
620 
621  $id_list = array_unique($id_list);
622  $ret = Array();
623  foreach ($id_list as $id) {
624  $object = $this->_chameleon_instance->get_single_security_record_by_id($id);
625  if ($object instanceof CO_Security_Login) {
626  $ret[] = $object;
627  }
628  }
629  if (1 < count($ret)) {
630  // Sort the results by ID.
631  usort($ret, function ($a, $b) {
632  if ($a->id() == $b->id()) {
633  return 0;
634  }
635 
636  if ($a->id() < $b->id()) {
637  return -1;
638  }
639 
640  return 1;
641  });
642  }
643 
644  return $ret;
645  }
646 
647  /***********************/
655  public function who_can_see( $in_test_target
656  ) {
657  $ret = false;
658 
659  if (isset($in_test_target) && ($in_test_target instanceof A_CO_DB_Table_Base)) {
660  $id = intval($in_test_target->read_security_id);
661 
662  if (0 < $id) {
663  $ret = $this->_chameleon_instance->get_all_login_objects_with_access($id);
664  if (!isset($ret) || !is_array($ret) || !count($ret)) {
665  $ret = false;
666  }
667  } elseif (0 == $id) {
668  $ret = true;
669  }
670  }
671 
672  return $ret;
673  }
674 
675  /***********************/
683  public function who_can_modify( $in_test_target,
684  $non_managers_only = false
689  ) {
690  $ret = false;
691 
692  if (isset($in_test_target) && ($in_test_target instanceof A_CO_DB_Table_Base)) {
693  $id = intval($in_test_target->write_security_id);
694 
695  if (0 < $id) {
696  $ret = $this->_chameleon_instance->get_all_login_objects_with_access($id, true);
697  // Check to see if any non-manager objects can modify the login (should never be).
698  if (($in_test_target instanceof CO_Security_Login) && isset($ret) && is_array($ret) && $non_managers_only) {
699  $ret_temp = Array();
700  foreach ($ret as $login) {
701  if (!($login instanceof CO_Login_Manager)) {
702  $ret_temp[] = $login;
703  }
704  }
705 
706  if (count($ret_temp)) {
707  $ret = $ret_temp;
708  } else {
709  $ret = false;
710  }
711  } elseif (!isset($ret) || !is_array($ret) || !count($ret)) {
712  $ret = false;
713  }
714  } elseif (0 == $id) {
715  $ret = true;
716  }
717  }
718 
719  return $ret;
720  }
721 
722  /***********************/
730  public function make_security_token() {
731  $ret = 0;
732 
733  $manager = $this->_chameleon_instance->get_login_item();
734  if ($this->_chameleon_instance->god_mode() || ($manager instanceof CO_Login_Manager)) {
735  $new_token = $this->_chameleon_instance->make_new_blank_record('CO_Security_ID');
736  if (isset($new_token) && ($new_token instanceof CO_Security_ID)) {
737  $new_id = $new_token->id();
738  if ($this->_chameleon_instance->god_mode() || $manager->add_new_login_id($new_id)) { // We need to do the "special excemption" add of the ID to the manager.
739  $ret = $new_id;
740  } else { // We were unable to set the new token ID to the manager.
741  $new_token->delete_from_db();
745  }
746  } else { // Token object did not get instantiated.
750  }
751  } else { // Should never happen, but what the hell...
755  }
756 
757  return $ret;
758  }
759 };
if(!defined( 'LGV_LANG_CATCHER'))
const __COBRA_VERSION__
_create_this_many_personal_ids($in_count)
get_chameleon_instance()
_convert_login( $in_login_id, $in_is_login_manager=false)
who_can_modify( $in_test_target, $non_managers_only=false)
_make_security_token()
who_can_see( $in_test_target)
get_login_instance( $in_login_id)
$version
The version indicator.
delete_login( $in_login_id, $also_delete_user=false)
set_personal_ids( $in_login_id, $in_personal_ids=[])
make_standalone_user()
create_new_manager_login( $in_login_id, $in_cleartext_password, $in_create_this_many_personal_ids=0)
convert_login( $in_login_id, $in_is_login_manager=false)
create_new_standard_login( $in_login_id, $in_cleartext_password, $in_create_this_many_personal_ids=0)
static make_cobra($in_chameleon_instance)
__construct( $in_chameleon_instance=NULL)
get_user_from_login( $in_login_id=NULL, $in_make_user_if_necessary=false)
_create_new_login( $in_login_id, $in_cleartext_password, $in_create_this_many_personal_ids=0, $in_security_token_ids=NULL, $in_is_login_manager=false)
$_chameleon_instance
This is the CHAMELEON instance that is associated with this COBRA instance.
get_all_logins( $and_write=false, $in_login_id=NULL, $in_login_integer_id=NULL)
static $cobra_error_code_user_already_exists
Definition: common.inc.php:50
static $cobra_error_code_invalid_chameleon
Definition: common.inc.php:49
static $cobra_error_code_token_id_not_set
Definition: common.inc.php:54
static $cobra_error_code_login_unavailable
Definition: common.inc.php:51
static $cobra_error_code_login_error
Definition: common.inc.php:52
static $cobra_error_code_instance_failed_to_initialize
Definition: common.inc.php:48
static $cobra_error_code_password_too_short
Definition: common.inc.php:55
static $cobra_error_code_token_instance_failed_to_initialize
Definition: common.inc.php:53
static $cobra_error_code_user_not_authorized
Definition: common.inc.php:47
static $cobra_error_desc_user_not_authorized
Definition: en.php:36
static $cobra_error_name_token_instance_failed_to_initialize
Definition: en.php:45
static $cobra_error_name_login_error
Definition: en.php:43
static $cobra_error_name_user_not_authorized
Definition: en.php:34
static $cobra_error_desc_invalid_chameleon
Definition: en.php:33
static $cobra_error_name_token_id_not_set
Definition: en.php:47
static $cobra_error_name_user_already_exists
Definition: en.php:39
static $cobra_error_name_password_too_short
Definition: en.php:49
static $cobra_error_desc_instance_failed_to_initialize
Definition: en.php:38
static $cobra_error_name_login_unavailable
Definition: en.php:41
static $cobra_error_desc_login_unavailable
Definition: en.php:42
static $cobra_error_desc_password_too_short
Definition: en.php:50
static $cobra_error_name_instance_failed_to_initialize
Definition: en.php:37
static $cobra_error_desc_user_already_exists
Definition: en.php:40
static $cobra_error_desc_login_error
Definition: en.php:44
static $cobra_error_desc_user_not_authorized_instance
Definition: en.php:35
static $cobra_error_name_invalid_chameleon
Definition: en.php:32
static $cobra_error_desc_token_id_not_set
Definition: en.php:48
static $cobra_error_desc_token_instance_failed_to_initialize
Definition: en.php:46
This class provides a general error report, with file, method and error information.
Definition: error.class.php:32