PageRenderTime 34ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/api/1/service.php

https://github.com/dreamhackcrew/API
PHP | 147 lines | 96 code | 28 blank | 23 comment | 28 complexity | e6b8388827abb098c6ebcaa0cc4cdc04 MD5 | raw file
  1. <?php
  2. function filter_methods($method) {
  3. // Only return methods that have a pre _
  4. if ( substr($method,0,1)=='_' && substr($method,1,1)!='_' )
  5. return substr($method,1);
  6. else
  7. return null;
  8. }
  9. class service {
  10. function __construct($request) {
  11. if ( is_callable(array($this,'construct')) )
  12. $this->construct();
  13. // Always add a _ to the requested method
  14. $command = '_'.$request[0];
  15. array_shift($request);
  16. // Check if the method exists
  17. if ( !is_callable(array($this,$command)) ) {
  18. header('HTTP/1.0 404 Not found');
  19. response(array(
  20. 'error'=>'Method not found!',
  21. 'available'=>array_filter(array_map('filter_methods',get_class_methods($this))) // Get all methods that starts with a _
  22. ));
  23. }
  24. // Call the method, and return the result
  25. response(call_user_func_array(array($this,$command),$request));
  26. }
  27. function requireFlag() {/*{{{*/
  28. $args = func_get_args();
  29. foreach($args as $key => $line) {
  30. if ( !is_string($line) )
  31. trigger_error('requireFlag arguments must be strings',E_USER_ERROR);
  32. // Add the flag to the list if it is a string.
  33. $flags[] = $line;
  34. }
  35. // Check that the user is signed in, if not.. send the user to the sign in
  36. if ( !isset($_SESSION['id']) ) {
  37. header('HTTP/1.0 401 Unauthorized');
  38. if ( isset($_SERVER['HTTP_X_URL_SCHEME']) && $_SERVER['HTTP_X_URL_SCHEME'] == "https")
  39. header('WWW-Authenticate: Basic realm="Sign in with our Crew Corner account"');
  40. response(array(
  41. 'error' => 'Access denied to a restricted service, please authorize first',
  42. 'desc' => 'You are trying to access a restricted service without authorization.',
  43. 'no' => E_USER_ERROR,
  44. ));
  45. }
  46. // If we dont have an access string, make one
  47. if ( !isset($_SESSION['access']) )
  48. $_SESSION['access'] = $this->makeAccessStr($_SESSION['id']);
  49. if ( !isset($_SESSION['access_flags']) )
  50. $_SESSION['access_flags'] = $this->getAccessFlags($_SESSION['access']);
  51. $flagsMissing = array();
  52. if ( is_array($_SESSION['access_flags']) )
  53. foreach($flags as $key => $line) {
  54. if ( !in_array($line,$_SESSION['access_flags']) )
  55. $flagsMissing[] = $line;
  56. }
  57. // We dont have access, throw error message
  58. if ( $flagsMissing )
  59. response(array(
  60. 'error'=>'Access denied, insufficient permissions!',
  61. 'access_flags_needed' => $flagsMissing
  62. ));
  63. }/*}}}*/
  64. function makeAccessStr($uid) {/*{{{*/
  65. // Start with the G-1 flag (signed in user) and the user id flag U{user id}
  66. $str = "G-1,|U$uid,";
  67. $level = db()->fetchOne("SELECT level FROM users WHERE uid=$uid");
  68. // The admin flag
  69. if ($level == 'admin')
  70. $str .= '|G-2,';
  71. // The developer flag, developers also gets the admin flag
  72. elseif ($level == 'developer')
  73. $str .= '|G-2,|G-4,';
  74. // Find all groups that the user is a member of
  75. if ($groups = db()->fetchAll("SELECT gid,name,lft,rgt FROM membership JOIN groups USING(gid) WHERE uid=$uid")) {
  76. $terms = array();
  77. foreach ($groups as $line) {
  78. // Get all child groups
  79. $terms[] = "lft BETWEEN {$line['lft']} AND {$line['rgt']}";
  80. // Find all parents
  81. $terms[] = "lft < {$line['lft']} AND rgt > {$line['rgt']}";
  82. }
  83. if ( $terms ) {
  84. // Get all the groups and childgroups that the user is a member of
  85. if ($groups = db()->fetchAllOne("SELECT gid FROM groups WHERE (".implode(' OR ',$terms).") AND NOT name LIKE '-%'"))
  86. // And add them to the access string
  87. $str .= '|G'.implode($groups,',|G').',';
  88. }
  89. }
  90. // G-3 - member of a group in an active event
  91. if ( $events = db()->fetchAll("SELECT *, events.id, max(groups.gid) `gid` FROM events,groups,membership WHERE (groups.event=events.id OR groups.gid<0) AND membership.gid=groups.gid AND uid=$uid GROUP BY events.id")) {
  92. foreach ( $events as $event ) {
  93. // The E{event number} flag
  94. if ( $event['gid'] > 0 )
  95. $str .= '|E'.$event['id'].',';
  96. // Remember if we find any active events
  97. if ( $event['active'] == 'Y' )
  98. $active = true;
  99. }
  100. // Add the G-3 flag
  101. if ( isset($active) && $active )
  102. $str .= '|G-3,';
  103. }
  104. // Special flag groups, if the user is member of a group named something that starts with a - for example (-TA) we add the group flags like F-TA{group number}
  105. // This is used to identify team- and group-leaders for a group
  106. if ($flags = db()->fetchAll("SELECT * FROM membership JOIN groups USING (gid) WHERE name LIKE '-%' AND uid=$uid")) {
  107. foreach ($flags as $flag)
  108. $str .= '|F'.$flag['name'].''.$flag['parent'].',';
  109. }
  110. return $str;
  111. }/*}}}*/
  112. function getAccessFlags( $accessString ) {
  113. // If we dont get an access string, dont bother to run the search
  114. if ( !$accessString )
  115. return array();
  116. return db()->fetchAllOne("SELECT flag FROM api_access WHERE auth_string REGEXP '%s'",$accessString);
  117. }
  118. }
  119. ?>