PageRenderTime 28ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/apilogin.php

https://github.com/drobbins/s3db
PHP | 134 lines | 105 code | 19 blank | 10 comment | 16 complexity | 6a1e97217d2410112c4a19dbf99750fd MD5 | raw file
  1. <?php
  2. ini_set('display_errors',0);
  3. if($_REQUEST['su3d']){
  4. ini_set('display_errors',1);
  5. }
  6. if(file_exists('config.inc.php'))
  7. {
  8. include('config.inc.php');
  9. }
  10. else
  11. {
  12. Header('Location: index.php');
  13. exit;
  14. }
  15. include_once (S3DB_SERVER_ROOT.'/s3id.php');
  16. include_once(S3DB_SERVER_ROOT.'/dbstruct.php');
  17. include_once(S3DB_SERVER_ROOT.'/s3dbcore/uid_resolve.php');
  18. include_once(S3DB_SERVER_ROOT.'/s3dbcore/authentication.php');
  19. include_once(S3DB_SERVER_ROOT.'/s3dbcore/s3encription.php');
  20. include_once(S3DB_SERVER_ROOT.'/s3dbcore/display.php');
  21. include_once(S3DB_SERVER_ROOT.'/s3dbcore/callback.php');
  22. include_once (S3DB_SERVER_ROOT.'/s3dbcore/element_info.php');
  23. include_once (S3DB_SERVER_ROOT.'/s3dbcore/validation_engine.php');
  24. include_once (S3DB_SERVER_ROOT.'/s3dbcore/insert_entries.php');
  25. include_once (S3DB_SERVER_ROOT.'/s3dbcore/file2folder.php');
  26. include_once (S3DB_SERVER_ROOT.'/s3dbcore/update_entries.php');
  27. include_once (S3DB_SERVER_ROOT.'/s3dbcore/delete_entries.php');
  28. include_once (S3DB_SERVER_ROOT.'/s3dbcore/datamatrix.php');
  29. include_once (S3DB_SERVER_ROOT.'/s3dbcore/create.php');
  30. include_once (S3DB_SERVER_ROOT.'/s3dbcore/permission.php');
  31. include_once (S3DB_SERVER_ROOT.'/s3dbcore/list.php');
  32. include_once (S3DB_SERVER_ROOT.'/s3dbcore/S3QLRestWrapper.php');
  33. include_once (S3DB_SERVER_ROOT.'/s3dbcore/SQL.php');
  34. include_once (S3DB_SERVER_ROOT.'/s3dbcore/S3QLaction.php');
  35. include_once (S3DB_SERVER_ROOT.'/s3dbcore/htmlgen.php');
  36. include_once (S3DB_SERVER_ROOT.'/s3dbcore/acceptFile.php');
  37. include_once (S3DB_SERVER_ROOT.'/s3dbcore/URIaction.php');
  38. include_once(S3DB_SERVER_ROOT.'/s3dbcore/common_functions.inc.php');
  39. include_once(S3DB_SERVER_ROOT.'/s3dbcore/api.php');
  40. $format = $_REQUEST['format'];
  41. if($format=='') $format='html';
  42. if(!$_REQUEST['key']){
  43. $username = ($_REQUEST['username']!='')?$_REQUEST['username']:$_REQUEST['user_id'];
  44. $password= $_REQUEST['password'];
  45. $authority= $_REQUEST['authority'];
  46. list($valid,$user_info,$key,$expires)=login($username, $password, $authority);
  47. if(!$valid)
  48. {
  49. if(!$authority){
  50. $msg = "Authentication failed. If you wish to authenticate with a specific authority other than local S3DB Deployment, please use the argument 'authority' to specify it.";
  51. }
  52. else {
  53. $msg = $user_info;
  54. }
  55. echo formatReturn($GLOBALS['error_codes']['no_permission_message'],$msg, $format, '');
  56. exit;
  57. }
  58. #is user was authenticated, create the token and return it
  59. if($valid){
  60. #if there is no token, create it now
  61. if(!$key) $key=random_string(15);
  62. if(!$expires) $expires = date('Y-m-d H:i:s', time()+(1 * 24 * 60 * 60));
  63. $db = CreateObject('s3dbapi.db');
  64. $db->Halt_On_Error = 'no';
  65. $db->Host = $GLOBALS['s3db_info']['server']['db']['db_host'];
  66. $db->Type = $GLOBALS['s3db_info']['server']['db']['db_type'];
  67. $db->Database = $GLOBALS['s3db_info']['server']['db']['db_name'];
  68. $db->User = $GLOBALS['s3db_info']['server']['db']['db_user'];
  69. $db->Password = $GLOBALS['s3db_info']['server']['db']['db_pass'];
  70. $db->connect();
  71. #create a log indicating the user has logged in
  72. $user_lid = $user_info['account_lid'];
  73. $user_id = $user_info['account_id'];
  74. create_log($user_lid, $db);
  75. #Create the key
  76. $inputs = array('key_id'=>$key, 'expires'=>$expires, 'notes'=>'Key generated automatically via API', 'account_id'=>$user_id);
  77. $added = add_entry ('access_keys', $inputs, $db);
  78. $data[0] = $inputs;$letter ='E';
  79. $pack= compact('data', 'user_id','db', 'letter','t','format');
  80. if($added){
  81. echo completeDisplay($pack);
  82. exit;
  83. }
  84. else {
  85. echo formatReturn('2', 'Your authentication was valid but a key could not be created.', $format,'');
  86. exit;
  87. }
  88. }
  89. }
  90. else {
  91. #if a key has been provided, validate the key
  92. $key=$_REQUEST['key'];
  93. include_once('core.header.php');
  94. #$user_proj = $GLOBALS['users_project'];
  95. if(is_file($GLOBALS['uploads'].'/userManage.s3db'))
  96. $user_proj = unserialize(file_get_contents($GLOBALS['uploads'].'/userManage.s3db'));
  97. if($user_id!='1'){
  98. $msg="A project to manage users has not been created. This project can only be created by the generic Admin users. Please add your Admin key to apilogin.php to create it automatically.";
  99. echo formatReturn('5',$msg, $format, '');
  100. exit;
  101. }
  102. #if it does not exist, create it and save it in config.inc.php;
  103. $user_proj=create_user_proj(compact('user_id','db','user_proj','timer'));
  104. #now, if query is not empyt, read it, parse it, interpret it.
  105. if($_REQUEST['query']){
  106. $query = $_REQUEST['query'];
  107. $q=compact('query','format','key','user_proj','user_id','db');
  108. $s3ql=parse_xml_query($q);
  109. ##now interpret the query
  110. $q['s3ql']=$s3ql;
  111. $return=actBasedOnQuery($q);
  112. }
  113. #if user is trying to authenticate, one of the options will be query that user item on the users project for alternative authentication
  114. }
  115. ?>