BxOAuthDb.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup OAuth2 OAuth2 server
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxOAuthDb extends BxDolModuleDb
  12. {
  13. function __construct(&$oConfig)
  14. {
  15. parent::__construct($oConfig);
  16. }
  17. function getClientsBy($aParams = array())
  18. {
  19. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  20. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  21. switch($aParams['type']) {
  22. case 'id':
  23. $aMethod['name'] = 'getRow';
  24. $aMethod['params'][1] = array(
  25. 'id' => $aParams['id']
  26. );
  27. $sWhereClause .= "AND `tc`.`id`=:id";
  28. $sLimitClause .= "LIMIT 1";
  29. break;
  30. case 'client_id':
  31. $aMethod['name'] = 'getRow';
  32. $aMethod['params'][1] = array(
  33. 'client_id' => $aParams['client_id']
  34. );
  35. $sWhereClause .= "AND `tc`.`client_id`=:client_id";
  36. $sLimitClause .= "LIMIT 1";
  37. break;
  38. case 'parent_id':
  39. $aMethod['params'][1] = array(
  40. 'parent_id' => $aParams['parent_id']
  41. );
  42. $sWhereClause .= "AND `tc`.`parent_id`=:parent_id";
  43. break;
  44. case 'user_id':
  45. $aMethod['params'][1] = array(
  46. 'user_id' => $aParams['user_id']
  47. );
  48. $sWhereClause .= "AND `tc`.`user_id`=:user_id";
  49. break;
  50. }
  51. $aMethod['params'][0] = "SELECT * " . $sSelectClause . "
  52. FROM `bx_oauth_clients` AS `tc` " . $sJoinClause . "
  53. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  54. return call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  55. }
  56. function getClientTitle($sClientId)
  57. {
  58. $sQuery = $this->prepare("SELECT `title` FROM `bx_oauth_clients` WHERE `client_id` = ?", $sClientId);
  59. return $this->getOne($sQuery);
  60. }
  61. function getClientIdByAccessToken($sToken)
  62. {
  63. $sQuery = $this->prepare("SELECT `client_id` FROM `bx_oauth_access_tokens` WHERE `access_token` = ?", $sToken);
  64. return $this->getOne($sQuery);
  65. }
  66. function addClient($aClient)
  67. {
  68. $mixedResult = $this->query("INSERT INTO `bx_oauth_clients` SET " . $this->arrayToSQL($aClient));
  69. return (int)$mixedResult > 0 ? $this->lastId() : false;
  70. }
  71. function updateClientsBy($aPrmSet, $aPrmWhere)
  72. {
  73. if(empty($aPrmSet) || !is_array($aPrmSet) || empty($aPrmWhere) || !is_array($aPrmWhere))
  74. return false;
  75. return (int)$this->query("UPDATE `bx_oauth_clients` SET " . $this->arrayToSQL($aPrmSet) . " WHERE " . $this->arrayToSQL($aPrmWhere, ' AND ')) > 0;
  76. }
  77. function deleteClients($aClients)
  78. {
  79. foreach ($aClients as $sClientId) {
  80. $sQuery = $this->prepare("DELETE FROM `bx_oauth_clients` WHERE `client_id` = ?", $sClientId);
  81. $this->query($sQuery);
  82. }
  83. }
  84. function deleteClientsBy($aParams)
  85. {
  86. if(empty($aParams) || !is_array($aParams))
  87. return false;
  88. return $this->query("DELETE FROM `bx_oauth_clients` WHERE " . $this->arrayToSQL($aParams, ' AND '));
  89. }
  90. function getSavedProfile($sClientId, $aProfiles)
  91. {
  92. $aIds = array_keys($aProfiles);
  93. return $this->getOne("SELECT `user_id` FROM `bx_oauth_refresh_tokens` WHERE `client_id` = ? AND `user_id` IN (" . $this->implode_escape($aIds) . ") LIMIT 1", array($sClientId));
  94. }
  95. function getClientByAllowedOriginUrl($sUrl)
  96. {
  97. return $this->getRow("SELECT `c`.* FROM `bx_oauth_clients` AS `c` INNER JOIN `bx_oauth_allowed_origins` AS `o` ON (`o`.`client_id` = `c`.`id`) WHERE `o`.`url` = :url", array('url' => $sUrl));
  98. }
  99. }
  100. /** @} */