1
0

BxGitHubDb.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 GitHub GitHub
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxGitHubDb extends BxDolModuleDb
  12. {
  13. protected $_oConfig;
  14. public function __construct(&$oConfig)
  15. {
  16. parent::__construct($oConfig);
  17. $this->_oConfig = $oConfig;
  18. }
  19. public function getApps($aParams = [])
  20. {
  21. $CNF = &$this->_oConfig->CNF;
  22. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  23. $sSelectClause = "*";
  24. $sWhereClause = $sOrderByClause = "";
  25. switch($aParams['sample']) {
  26. case 'id':
  27. $aMethod['name'] = 'getRow';
  28. $aMethod['params'][1] = [
  29. 'id' => $aParams['id']
  30. ];
  31. $sWhereClause .= " AND `id`=:id";
  32. break;
  33. case 'profile_id':
  34. $aMethod['params'][1] = [
  35. 'profile_id' => $aParams['profile_id']
  36. ];
  37. $sWhereClause .= " AND `profile_id`=:profile_id";
  38. break;
  39. }
  40. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_APPS'] . "` WHERE 1 " . $sWhereClause;
  41. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  42. }
  43. public function getAuthorizations($aParams = [])
  44. {
  45. $CNF = &$this->_oConfig->CNF;
  46. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  47. $sSelectClause = "*";
  48. $sWhereClause = $sOrderByClause = "";
  49. switch($aParams['sample']) {
  50. case 'id':
  51. $aMethod['name'] = 'getRow';
  52. $aMethod['params'][1] = [
  53. 'id' => $aParams['id']
  54. ];
  55. $sWhereClause .= " AND `id`=:id";
  56. break;
  57. case 'profile_app_ids':
  58. $aMethod['name'] = 'getRow';
  59. $aMethod['params'][1] = [
  60. 'profile_id' => $aParams['profile_id'],
  61. 'app_id' => $aParams['app_id']
  62. ];
  63. $sWhereClause .= " AND `profile_id`=:profile_id AND `app_id`=:app_id";
  64. break;
  65. }
  66. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_AUTHORIZATIONS'] . "` WHERE 1 " . $sWhereClause;
  67. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  68. }
  69. public function getAuthorization($iProfileId, $iAppId)
  70. {
  71. $aAuthorization = $this->getAuthorizations([
  72. 'sample' => 'profile_app_ids',
  73. 'profile_id' => $iProfileId,
  74. 'app_id' => $iAppId
  75. ]);
  76. return ($aAuthorization['access_token'] ?? false) ? $aAuthorization : false;
  77. }
  78. public function isAuthorization($iProfileId, $iAppId)
  79. {
  80. return $this->getAuthorization($iProfileId, $iAppId) !== false;
  81. }
  82. public function insertAuthorization($iProfileId, $iAppId, $aAccessToken)
  83. {
  84. $CNF = &$this->_oConfig->CNF;
  85. $iNow = time();
  86. $aItems = array_merge(['profile_id', 'app_id', 'added', 'changed'], array_keys($aAccessToken));
  87. return $this->query("INSERT INTO `" . $CNF['TABLE_AUTHORIZATIONS'] . "`(`" . implode('`, `', $aItems) . "`) VALUES(:" . implode(', :', $aItems) . ") ON DUPLICATE KEY UPDATE `changed`=:changed, " . $this->arrayToSQL($aAccessToken), array_merge([
  88. 'profile_id' => $iProfileId,
  89. 'app_id' => $iAppId,
  90. 'added' => $iNow,
  91. 'changed' => $iNow
  92. ], $aAccessToken));
  93. }
  94. public function getSettings($aParams = [])
  95. {
  96. $CNF = &$this->_oConfig->CNF;
  97. $aMethod = ['name' => 'getAll', 'params' => [0 => 'query']];
  98. $sSelectClause = "*";
  99. $sWhereClause = $sOrderByClause = "";
  100. switch($aParams['sample']) {
  101. case 'id':
  102. $aMethod['name'] = 'getRow';
  103. $aMethod['params'][1] = [
  104. 'id' => $aParams['id']
  105. ];
  106. $sWhereClause .= " AND `id`=:id";
  107. break;
  108. case 'profile_id':
  109. $aMethod['name'] = 'getRow';
  110. $aMethod['params'][1] = [
  111. 'profile_id' => $aParams['profile_id']
  112. ];
  113. $sWhereClause .= " AND `profile_id`=:profile_id";
  114. break;
  115. }
  116. $aMethod['params'][0] = "SELECT " . $sSelectClause . " FROM `" . $CNF['TABLE_SETTINGS'] . "` WHERE 1 " . $sWhereClause;
  117. return call_user_func_array([$this, $aMethod['name']], $aMethod['params']);
  118. }
  119. }
  120. /** @} */