<commit_before><?php
/**
 * TabT API
 *
 * A programming interface to access information managed
 * by TabT, the table tennis information manager.
 *
 * @author <NAME> <<EMAIL>>
 * @version 0.7.22
 *
 * Copyright (C) 2007-2018 <NAME> (<EMAIL>)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.

 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function _GetPermissions($Credentials) {
  // Establish a dummy connection to make sure "mysql_real_escape_string" works as expected
  $db = new DB_Session();
  $db->query('SELECT COUNT(*) FROM auth_user');
  $Account    = isset($Credentials->Account) ? mysql_real_escape_string($Credentials->Account) : '';
  $Password   = isset($Credentials->Password) ? mysql_real_escape_string($Credentials->Password) : '';
  $OnBehalfOf = isset($Credentials->OnBehalfOf) && is_numeric($Credentials->OnBehalfOf)? intval($Credentials->OnBehalfOf) : 0;

  if ($Account != '') {
    $q = <<<EOQ
SELECT
  a.perms,
  s.sid,
  a.player_id,
  pc.club_id,
  cc_reg.id as region_category,
  cc_reg.main_level as region_level
FROM
  auth_user as a
  LEFT JOIN active_sessions s ON s.sid=a.user_id
  LEFT JOIN playerclub pc ON a.player_id=pc.player_id
  LEFT JOIN clubs as c ON c.id=pc.club_id
  LEFT JOIN clubcategories as cc_reg ON CONCAT(',', cc_reg.group, ',') LIKE CONCAT('%%,', c.category, ',%%') AND NOT ISNULL(cc_reg.main_level)
WHERE
  a.username='{$Account}' AND a.password=<PASSWORD>('{<PASSWORD>}') AND ISNULL(a.conf_id)
ORDER BY pc.season DESC;
EOQ;
    list($permissions, $session_id, $player_id, $club_id, $region, $region_level) = $db->select_one_array(utf8_decode($q));
  }

  // If valid account, try to retrieve the preferred language of
  // the connected user
  if (isset($permissions) && is_string($permissions)) {
    $session_access = new DB_CT_Sql();
    $session_access->ac_start();
    if (preg_match("#\\\$GLOBALS\['lang'\] *= *'([a-zA-Z]+)';#",
                   $session_access->ac_get_value($session_id, 'HurriUser'), $m)) {
      if ($GLOBALS['lang'] != $m[1]) {
        $GLOBALS['lang'] = $m[1];
        include($GLOBALS['site_info']['path'].'public/localization.php');
        dict_compute();
      }
    }
    unset($session_access);

    // Create dummy "Perm" object for TabT functions that are using it
    if (is_string($permissions) && $region && is_numeric($region)) {
      $GLOBALS['perm'] = new HurriPerm();
      $GLOBALS['auth'] = new Auth();
      $levels   = select_list("SELECT main_level FROM clubcategories cc WHERE (SELECT CONCAT(',', `group`, ',') FROM clubcategories WHERE id={$region}) LIKE CONCAT('%,', cc.id, ',%')", 'main_level');
      $levels[] = $region_level;

      $GLOBALS['auth']->auth = array('perm' => $permissions, 'pid' => $player_id, 'club_id' => $club_id, 'region' => $region, 'region_levels' => $levels);

      if ($OnBehalfOf > 0) {
        $season = $db->select_one("SELECT max(id) FROM seasoninfo");
        list($requested_player_id, $requested_club_category, $requested_permissions) = $db->select_one_array("SELECT pi.id, c.category, IFNULL(a.perms, 'user') FROM playerinfo as pi LEFT JOIN playerclub pc ON pc.season={$season} AND pc.player_id=pi.id LEFT JOIN clubs as c ON c.id=pc.club_id LEFT JOIN auth_user a ON a.player_id=pi.id WHERE pi.vttl_index={$OnBehalfOf}");

        if ($requested_player_id > 0) {
          $permissions_array = explode(',', $permissions);
          if (count(array_intersect($permissions_array, array('region', 'admin'))) == 0) {
            $GLOBALS['permission_error'] = array(
              'code'    => '53',
              'message' => "You don't have enough permission to act on behalf of another user."
            );
            unset($permissions);
          } elseif (count(array_intersect($permissions_array, array('region'))) == 1 && count(array_intersect($requested_permissions, array('admin'))) > 0) {
            $GLOBALS['permission_error'] = array(
              'code'    => '54',
              'message' => "You don't have enough permission to act on behalf of a admin user."
            );
            unset($permissions);
          } elseif (count(array_intersect($permissions_array, array('region'))) == 1 && count(array_intersect($permissions_array, array('admin'))) == 0 && !in_array($requested_club_category, get_club_category_array($region))) {
            $GLOBALS['permission_error'] = array(
              'code'    => '55',
              'message' => "You don't have enough permission to act on behalf of a user from another region."
            );<commit_msg>Join unique index in $Global auth<commit_after><?php
/**
 * TabT API
 *
 * A programming interface to access information managed
 * by TabT, the table tennis information manager.
 *
 * @author <NAME> <<EMAIL>>
 * @version 0.7.22
 *
 * Copyright (C) 2007-2018 <NAME> (<EMAIL>)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.

 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function _GetPermissions($Credentials) {
  // Establish a dummy connection to make sure "mysql_real_escape_string" works as expected
  $db = new DB_Session();
  $db->query('SELECT COUNT(*) FROM auth_user');
  $Account    = isset($Credentials->Account) ? mysql_real_escape_string($Credentials->Account) : '';
  $Password   = isset($Credentials->Password) ? mysql_real_escape_string($Credentials->Password) : '';
  $OnBehalfOf = isset($Credentials->OnBehalfOf) && is_numeric($Credentials->OnBehalfOf)? intval($Credentials->OnBehalfOf) : 0;

  if ($Account != '') {
    $q = <<<EOQ
SELECT
  a.perms,
  s.sid,
  a.player_id,
  pc.club_id,
  cc_reg.id as region_category,
  cc_reg.main_level as region_level,
  pi.vttl_index as unique_index
FROM
  auth_user as a
  LEFT JOIN active_sessions s ON s.sid=a.user_id
  LEFT JOIN playerclub pc ON a.player_id=pc.player_id
  LEFT JOIN clubs as c ON c.id=pc.club_id
  LEFT JOIN playerinfo as pi ON a.player_id=pi.id
  LEFT JOIN clubcategories as cc_reg ON CONCAT(',', cc_reg.group, ',') LIKE CONCAT('%%,', c.category, ',%%') AND NOT ISNULL(cc_reg.main_level)
WHERE
  a.username='{$Account}' AND a.password=<PASSWORD>('{<PASSWORD>}') AND ISNULL(a.conf_id)
ORDER BY pc.season DESC;
EOQ;
    list($permissions, $session_id, $player_id, $club_id, $region, $region_level, $unique_index) = $db->select_one_array(utf8_decode($q));
  }

  // If valid account, try to retrieve the preferred language of
  // the connected user
  if (isset($permissions) && is_string($permissions)) {
    $session_access = new DB_CT_Sql();
    $session_access->ac_start();
    if (preg_match("#\\\$GLOBALS\['lang'\] *= *'([a-zA-Z]+)';#",
                   $session_access->ac_get_value($session_id, 'HurriUser'), $m)) {
      if ($GLOBALS['lang'] != $m[1]) {
        $GLOBALS['lang'] = $m[1];
        include($GLOBALS['site_info']['path'].'public/localization.php');
        dict_compute();
      }
    }
    unset($session_access);

    // Create dummy "Perm" object for TabT functions that are using it
    if (is_string($permissions) && $region && is_numeric($region)) {
      $GLOBALS['perm'] = new HurriPerm();
      $GLOBALS['auth'] = new Auth();
      $levels   = select_list("SELECT main_level FROM clubcategories cc WHERE (SELECT CONCAT(',', `group`, ',') FROM clubcategories WHERE id={$region}) LIKE CONCAT('%,', cc.id, ',%')", 'main_level');
      $levels[] = $region_level;

      $GLOBALS['auth']->auth = array('perm' => $permissions, 'pid' => $player_id, 'club_id' => $club_id, 'region' => $region, 'region_levels' => $levels, 'unique_index' => $unique_index);

      if ($OnBehalfOf > 0) {
        $season = $db->select_one("SELECT max(id) FROM seasoninfo");
        list($requested_player_id, $requested_club_category, $requested_permissions) = $db->select_one_array("SELECT pi.id, c.category, IFNULL(a.perms, 'user') FROM playerinfo as pi LEFT JOIN playerclub pc ON pc.season={$season} AND pc.player_id=pi.id LEFT JOIN clubs as c ON c.id=pc.club_id LEFT JOIN auth_user a ON a.player_id=pi.id WHERE pi.vttl_index={$OnBehalfOf}");

        if ($requested_player_id > 0) {
          $permissions_array = explode(',', $permissions);
          if (count(array_intersect($permissions_array, array('region', 'admin'))) == 0) {
            $GLOBALS['permission_error'] = array(
              'code'    => '53',
              'message' => "You don't have enough permission to act on behalf of another user."
            );
            unset($permissions);
          } elseif (count(array_intersect($permissions_array, array('region'))) == 1 && count(array_intersect($requested_permissions, array('admin'))) > 0) {
            $GLOBALS['permission_error'] = array(
              'code'    => '54',
              'message' => "You don't have enough permission to act on behalf of a admin user."
            );
            unset($permissions);
          } elseif (count(array_intersect($permissions_array, array('region'))) == 1 && count(array_intersect($permissions_array, array('admin'))) == 0 && !in_array($requested_club_category, get_club_category_array($region))) {
            $GLOBALS['permission_error'] = array(
              'code'    => '55',
              'message' => "You don't have enough permission to act on behalf of a user from another region."
            );
{'fraction_non_alphanumeric': 0.1470730527334301, 'fraction_numerical': 0.005805515239477504, 'mean_word_length': 3.6621560667568787, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 76, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3471550', 'n_tokens_mistral': 3301, 'n_tokens_neox': 3214, 'n_words': 904}