<commit_before><?php

/**
 * Instagram API class
 * API Documentation: http://instagram.com/developer/
 * 
 * @author <NAME>
 * @since 30.10.2011
 * @copyright <NAME> | MetzWeb Networks
 * @version 1.0
 * 
 * @todo Extend error handling, Add new methods, Contruct with an array
 * 
 * @example Get started:
 * $ig = new Instagram('API Key', 'API Secret', 'Callback URL');
 * echo "<a href='{$ig->getLoginUrl()}'>Login</a>";
 * 
 * @example Get user token:
 * $code = $_GET['code'];
 * $userToken = $ig->getOAuthToken($code);
 * echo 'Your username is: '.$userToken->user->username;
 * 
 * @example Get user likes:
 * $likes = getUserLikes($userToken->access_token, 2);
 * echo "<pre>";
 * print_r($likes);
 * echo "<pre>";
 */ 

class Instagram {

  /**
   * The API base URL
   */
  const API_URL = 'https://api.instagram.com/v1/';

  /**
   * The API OAuth URL
   */
  const API_OAUTH_URL = 'https://api.instagram.com/oauth/authorize';

  /**
   * The OAuth token URL
   */
  const API_OAUTH_TOKEN_URL = 'https://api.instagram.com/oauth/access_token';

  /**
   * The Instagram API Key
   * 
   * @var string
   */
  private $_apikey;

  /**
   * The Instagram OAuth API secret
   * 
   * @var string
   */
  private $_apisecret;

  /**
   * The callback URL
   * 
   * @var string
   */
  private $_callbackurl;

  /**
   * Available scopes
   * 
   * @var array
   */
  private $_scopes = array('basic', 'likes', 'comments', 'relationships');


  /**
   * Default constructor
   * 
   * @param string/array $apiKey          Instagram API Key / Configuration
   * @param string $apiSecret             Instagram OAuth Secret
   * @param string $apiCallback           Website Callback URL
   * @return void
   */
  public function __construct($apiKey, $apiSecret = null, $apiCallback = null) {
    if (is_string($apiKey) && isset($apiSecret) && isset($apiCallback)) {
      $this->setApiKey($apiKey);
      $this->setApiSecret($apiSecret);
      $this->setApiCallback($apiCallback);
    } else if (is_array($apiKey)) {
      $this->setApiKey($apiKey[apiKey]);
      $this->setApiSecret($apiKey[apiSecret]);
      $this->setApiCallback($apiKey[apiCallback]);
    } else {
      throw new Exception("Error: __construct() - The parameter / array isn't valid.");
    }
  }

  /**
   * Generates the OAuth login URL
   *
   * @param array [optional] $scope       Requesting additional permissions
   * @return string                       Instagram OAuth login URL
   */
  public function getLoginUrl($scope = array('basic')) {
    if (is_array($scope) && count(array_intersect($scope, $this->_scopes)) === count($scope)) {
      return self::API_OAUTH_URL.'?client_id='.$this->getApiKey().'&redirect_uri='.$this->getApiCallback().'&scope='.implode('+', $scope).'&response_type=code';
    } else {
      throw new Exeption("Error: getLoginUrl() - The parameter isn't an array or invalid scope permissions used.");
    }
  }

  /**
   * Get the liked photos of a user
   *
   * @param string $token                 Valid Instagram token (can be recived)
   * @param string [optional] $limit      Limit of returned results
   * @return mixed
   */
  public function getUserLikes($token, $limit = 8) {
    return $this->_makeCall('users/self/media/liked?access_token='.$token.'&count='.$limit);
  }

  /**
   * Get the OAuth data of a user by the returned callback code
   *
   * @param string $code                  OAuth code variable (after a successful login)
   * @param boolean [optional] $token     If it's true, only the access token will be returned
   * @return mixed<commit_msg>Added user methods<commit_after><?php

/**
 * Instagram API class
 * API Documentation: http://instagram.com/developer/
 * 
 * @author <NAME>
 * @since 30.10.2011
 * @copyright <NAME> | MetzWeb Networks
 * @version 1.0
 * 
 * @todo Extend error handling, Add new methods, Contruct with an array
 * 
 * @example Get started:
 * $ig = new Instagram('API Key', 'API Secret', 'Callback URL');
 * echo "<a href='{$ig->getLoginUrl()}'>Login</a>";
 * 
 * @example Get user token:
 * $code = $_GET['code'];
 * $userToken = $ig->getOAuthToken($code);
 * echo 'Your username is: '.$userToken->user->username;
 * 
 * @example Get user likes:
 * $likes = getUserLikes($userToken->access_token, 2);
 * echo "<pre>";
 * print_r($likes);
 * echo "<pre>";
 */ 

class Instagram {

  /**
   * The API base URL
   */
  const API_URL = 'https://api.instagram.com/v1/';

  /**
   * The API OAuth URL
   */
  const API_OAUTH_URL = 'https://api.instagram.com/oauth/authorize';

  /**
   * The OAuth token URL
   */
  const API_OAUTH_TOKEN_URL = 'https://api.instagram.com/oauth/access_token';

  /**
   * The Instagram API Key
   * 
   * @var string
   */
  private $_apikey;

  /**
   * The Instagram OAuth API secret
   * 
   * @var string
   */
  private $_apisecret;

  /**
   * The callback URL
   * 
   * @var string
   */
  private $_callbackurl;

  /**
   * Available scopes
   * 
   * @var array
   */
  private $_scopes = array('basic', 'likes', 'comments', 'relationships');


  /**
   * Default constructor
   * 
   * @param string/array $apiKey          Instagram API Key / Configuration
   * @param string $apiSecret             Instagram OAuth Secret
   * @param string $apiCallback           Website Callback URL
   * @return void
   */
  public function __construct($apiKey, $apiSecret = null, $apiCallback = null) {
    if (is_string($apiKey) && isset($apiSecret) && isset($apiCallback)) {
      $this->setApiKey($apiKey);
      $this->setApiSecret($apiSecret);
      $this->setApiCallback($apiCallback);
    } else if (is_array($apiKey)) {
      $this->setApiKey($apiKey[apiKey]);
      $this->setApiSecret($apiKey[apiSecret]);
      $this->setApiCallback($apiKey[apiCallback]);
    } else {
      throw new Exception("Error: __construct() - The parameter / array isn't valid.");
    }
  }

  /**
   * Generates the OAuth login URL
   *
   * @param array [optional] $scope       Requesting additional permissions
   * @return string                       Instagram OAuth login URL
   */
  public function getLoginUrl($scope = array('basic')) {
    if (is_array($scope) && count(array_intersect($scope, $this->_scopes)) === count($scope)) {
      return self::API_OAUTH_URL.'?client_id='.$this->getApiKey().'&redirect_uri='.$this->getApiCallback().'&scope='.implode('+', $scope).'&response_type=code';
    } else {
      throw new Exeption("Error: getLoginUrl() - The parameter isn't an array or invalid scope permissions used.");
    }
  }

  /**
   * Search for a user
   *
   * @param string $name                  Instagram username
   * @param string $token                 Valid Instagram token
   * @return mixed
   */
  public function searchUser($name, $token) {
    return $this->_makeCall('users/search?access_token='.$token.'&q='.$name);
  }

  /**
   * Get user info by it's id
   *
   * @param string $id                    Instagram user id
   * @param string $token                 Valid Instagram token
   * @return mixed
   */
  public function getUser($id, $token) {
    return $this->_makeCall('users/'.$id.'?access_token='.$token.'&count='.$limit);
  }

  /**
   * Get user activity feed
   *
   * @param string $token                 Valid Instagram token
   * @return mixed
   */
  public function getUserFeed($token) {
    return $this->_makeCall('users/self/feed?access_token='.$token);
  }

  /**
   * Get user recent media
   *
   * @param string $id                    Instagram user id
   * @param string $token                 Valid Instagram token
   * @return mixed
   */
  public function getUserMedia($id, $token) {
    return $this->_makeCall('users/'.$id.'/media/recent?access_token='.$token);
  }

  /**
   * Get the liked photos of a user
   *
   * @param string $token                 Valid Instagram token
   * @param string [optional] $limit      Limit of returned results
   * @return mixed
   */
  public function getUserLikes($token, $limit = 8) {
    return $this->_makeCall('users/self/media/liked?access_token='.$token.'&count='.$limit);
  }

  /**
   * Get the OAuth data of a user by the returned callback code
   *
   * @param string $code                  OAuth code variable (after a successful login)
   * @param boolean [optional] $token     If it's true, only the access token will be returned
   * @return mixed
{'fraction_non_alphanumeric': 0.14919113241461954, 'fraction_numerical': 0.003115638106650689, 'mean_word_length': 2.7526978417266186, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 49, 'https://': 6, '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': '7616863', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2566, 'n_words': 674}