<commit_before><?php

class Podio {
  public static $oauth, $debug, $logger, $session_manager, $last_response, $auth_type;
  protected static $url, $client_id, $client_secret, $secret, $ch, $headers;

  const GET = 'GET';
  const POST = 'POST';
  const PUT = 'PUT';
  const DELETE = 'DELETE';

  public static function setup($client_id, $client_secret, $options = array('session_manager' => null, 'curl_options' => array())) {
    // Setup client info
    self::$client_id = $client_id;
    self::$client_secret = $client_secret;

    // Setup curl
    self::$url = empty($options['api_url']) ? 'https://api.podio.com:443' : $options['api_url'];
    self::$debug = self::$debug ? self::$debug : false;
    self::$ch = curl_init();
    self::$headers = array(
      'Accept' => 'application/json',
    );
    curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt(self::$ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt(self::$ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt(self::$ch, CURLOPT_USERAGENT, 'Podio PHP Client/3.0');
    curl_setopt(self::$ch, CURLOPT_HEADER, true);
    curl_setopt(self::$ch, CURLINFO_HEADER_OUT, true);

    if ($options && !empty($options['curl_options'])) {
      curl_setopt_array(self::$ch, $options['curl_options']);
    }

    self::$session_manager = null;
    if ($options && !empty($options['session_manager']) && class_exists($options['session_manager'])) {
      self::$session_manager = new $options['session_manager'];
      self::$oauth = self::$session_manager->get();
    }

    // Register shutdown function for debugging and session management
    register_shutdown_function('Podio::shutdown');
  }

  public static function authenticate_with_app($app_id, $app_token) {
    return self::authenticate('app', array('app_id' => $app_id, 'app_token' => $app_token));
  }

  public static function authenticate_with_password($username, $password) {
    return self::authenticate('username', array('username' => $username, 'password' => $password));
  }

  public static function authenticate_with_authorization_code($authorization_code, $redirect_uri) {
    return self::authenticate('authorization_code', array('code' => $authorization_code, 'redirect_uri' => $redirect_uri));
  }

  public static function refresh_access_token() {
    return self::authenticate('refresh_token', array('refresh_token' => self::$oauth->refresh_token));
  }

  public static function authenticate($grant_type, $attributes) {
    $data = array();
    $auth_type = array('type' => $grant_type);

    switch ($grant_type) {
      case 'password':
        $data['grant_type'] = 'password';
        $data['username'] = $attributes['username'];
        $data['password'] = $<PASSWORD>['password'];

        $auth_type['identifier'] = $attributes['username'];
        break;
      case 'refresh_token':
        $data['grant_type'] = 'refresh_token';
        $data['refresh_token'] = $attributes['refresh_token'];
        break;
      case 'authorization_code':
        $data['grant_type'] = 'authorization_code';
        $data['code'] = $attributes['code'];
        $data['redirect_uri'] = $attributes['redirect_uri'];
        break;
      case 'app':
        $data['grant_type'] = 'app';
        $data['app_id'] = $attributes['app_id'];
        $data['app_token'] = $attributes['app_token'];

        $auth_type['identifier'] = $attributes['app_id'];
      default:
        break;
    }

    $request_data = array_merge($data, array('client_id' => self::$client_id, 'client_secret' => self::$client_secret));
    if ($response = self::request(self::POST, '/oauth/token', $request_data, array('oauth_request' => true))) {
      $body = $response->json_body();
      self::$oauth = new PodioOAuth($body['access_token'], $body['refresh_token'], $body['expires_in'], $body['ref']);
      self::$auth_type = $auth_type;

      if (self::$session_manager) {
        self::$session_manager->set(self::$oauth, self::$auth_type);
      }

      return true;
    }
    return false;
  }

  public static function clear_authentication() {
    self::$oauth = new PodioOAuth();

    if (self::$session_manager) {
      self::$session_manager->set(self::$oauth, self::$auth_type);
    }
  }

  public static function authorize_url($redirect_uri) {
    $parsed_url = parse_url(self::$url);
    $host = str_replace('api.', '', $parsed_url['host']);
    return 'https://'.$host.'/oauth/authorize?response_type=code&client_id='.self::$client_id.'&redirect_uri='.rawurlencode($redirect_uri);
  }

  public static function is_authenticated() {
    return self::$oauth && self::$oauth->access_token;<commit_msg>Don't reset auth_type when refreshing tokens<commit_after><?php

class Podio {
  public static $oauth, $debug, $logger, $session_manager, $last_response, $auth_type;
  protected static $url, $client_id, $client_secret, $secret, $ch, $headers;

  const GET = 'GET';
  const POST = 'POST';
  const PUT = 'PUT';
  const DELETE = 'DELETE';

  public static function setup($client_id, $client_secret, $options = array('session_manager' => null, 'curl_options' => array())) {
    // Setup client info
    self::$client_id = $client_id;
    self::$client_secret = $client_secret;

    // Setup curl
    self::$url = empty($options['api_url']) ? 'https://api.podio.com:443' : $options['api_url'];
    self::$debug = self::$debug ? self::$debug : false;
    self::$ch = curl_init();
    self::$headers = array(
      'Accept' => 'application/json',
    );
    curl_setopt(self::$ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt(self::$ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt(self::$ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt(self::$ch, CURLOPT_USERAGENT, 'Podio PHP Client/3.0');
    curl_setopt(self::$ch, CURLOPT_HEADER, true);
    curl_setopt(self::$ch, CURLINFO_HEADER_OUT, true);

    if ($options && !empty($options['curl_options'])) {
      curl_setopt_array(self::$ch, $options['curl_options']);
    }

    self::$session_manager = null;
    if ($options && !empty($options['session_manager']) && class_exists($options['session_manager'])) {
      self::$session_manager = new $options['session_manager'];
      self::$oauth = self::$session_manager->get();
    }

    // Register shutdown function for debugging and session management
    register_shutdown_function('Podio::shutdown');
  }

  public static function authenticate_with_app($app_id, $app_token) {
    return self::authenticate('app', array('app_id' => $app_id, 'app_token' => $app_token));
  }

  public static function authenticate_with_password($username, $password) {
    return self::authenticate('username', array('username' => $username, 'password' => $password));
  }

  public static function authenticate_with_authorization_code($authorization_code, $redirect_uri) {
    return self::authenticate('authorization_code', array('code' => $authorization_code, 'redirect_uri' => $redirect_uri));
  }

  public static function refresh_access_token() {
    return self::authenticate('refresh_token', array('refresh_token' => self::$oauth->refresh_token));
  }

  public static function authenticate($grant_type, $attributes) {
    $data = array();
    $auth_type = array('type' => $grant_type);

    switch ($grant_type) {
      case 'password':
        $data['grant_type'] = 'password';
        $data['username'] = $attributes['username'];
        $data['password'] = $attributes['password'];

        $auth_type['identifier'] = $attributes['username'];
        break;
      case 'refresh_token':
        $data['grant_type'] = 'refresh_token';
        $data['refresh_token'] = $attributes['refresh_token'];
        break;
      case 'authorization_code':
        $data['grant_type'] = 'authorization_code';
        $data['code'] = $attributes['code'];
        $data['redirect_uri'] = $attributes['redirect_uri'];
        break;
      case 'app':
        $data['grant_type'] = 'app';
        $data['app_id'] = $attributes['app_id'];
        $data['app_token'] = $attributes['app_token'];

        $auth_type['identifier'] = $attributes['app_id'];
      default:
        break;
    }

    $request_data = array_merge($data, array('client_id' => self::$client_id, 'client_secret' => self::$client_secret));
    if ($response = self::request(self::POST, '/oauth/token', $request_data, array('oauth_request' => true))) {
      $body = $response->json_body();
      self::$oauth = new PodioOAuth($body['access_token'], $body['refresh_token'], $body['expires_in'], $body['ref']);

      // Don't touch auth_type if we are refreshing automatically as it'll be reset to null
      if ($grant_type !== 'refresh_token') {
        self::$auth_type = $auth_type;
      }

      if (self::$session_manager) {
        self::$session_manager->set(self::$oauth, self::$auth_type);
      }

      return true;
    }
    return false;
  }

  public static function clear_authentication() {
    self::$oauth = new PodioOAuth();

    if (self::$session_manager) {
      self::$session_manager->set(self::$oauth, self::$auth_type);
    }
  }

  public static function authorize_url($redirect_uri) {
    $parsed_url = parse_url(self::$url);
    $host = str_replace('api.', '', $parsed_url['host']);
    return 'https://'.$host.'/oauth/authorize?response_type=code&client_id='.self::$client_id.'&redirect_uri='.rawurlencode($redirect_uri);
  }

  public static function is_authenticated() {
    return self::$oauth && self::$oauth->access_token;
{'fraction_non_alphanumeric': 0.17250873663030816, 'fraction_numerical': 0.001058985491898761, 'mean_word_length': 4.35374149659864, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 44, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, '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': '5060248', 'n_tokens_mistral': 3003, 'n_tokens_neox': 2963, 'n_words': 543}