<commit_before><?php
/*
 * Message Broker ("MB") toolbox class library for functionality related to CURL requests.
 */

namespace DoSomething\MB_Toolbox;

use DoSomething\MBStatTracker\StatHat;

/**
 * MB_Toolbox_cURL:
 */
class MB_Toolbox_cURL
{

  const DRUPAL_API = '/api/v1';
  const TIMEOUT = 0;

  /**
   * Singleton instance of MB_Configuration application settings and service objects
   *
   * @var object
   */
  protected $mbConfig;

  /**
   * Setting from external service to track activity - StatHat.
   *
   * @var object
   */
  private $statHat;

  /**
   * Authentication details from Drupal site
   *
   * @var object
   */
  private $auth;

  /**
   * Constructor
   *
   * @param array $settings
   *   Connection and configuration settings common to the application
   *
   * @return object
   */
  public function __construct() {

    $this->auth = NULL;

    // Check for cURL on server
    if (!is_callable('curl_init')) {
      throw new Exception("Error - PHP cURL extension is not enabled on the server. cURL is required by many of the methods in the mb-toolbox library.");
    }

    $this->mbConfig = MB_Configuration::getInstance();
    $this->statHat = $this->mbConfig->getProperty('statHat');
  }

  /**
   * cURL GETs
   *
   * @param string $curlUrl
   *  The URL to GET from. Include domain and path.
   * @param boolean $isAuth
   *  Optional flag to keep track of the current authencation state.
   *
   * @return boolean $results
   *  Response from cURL GET request.
   *
   * @return object $result
   *   The results returned from the cURL call.
   *   - [0]: Results in json format
   *   - [1]: Status code
   */
  public function curlGET($curlUrl, $isAuth = FALSE) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $curlUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // Only add token and cookie values to header when values are available and
    // the curlPOSTauth() method is making the POST request.
    if (isset($this->auth->token) && $isAuth) {
      curl_setopt($ch, CURLOPT_HTTPHEADER,
        array(
          'Content-type: application/json',
          'Accept: application/json',
          'X-CSRF-Token: ' . $this->auth->token,
          'Cookie: ' . $this->auth->session_name . '=' . $this->auth->sessid
        )
      );
    }
    else {
      curl_setopt($ch, CURLOPT_HTTPHEADER,
        array(
          'Content-type: application/json',
          'Accept: application/json'
        )
      );
    }

    $jsonResult = curl_exec($ch);
    $results[0] = json_decode($jsonResult);
    $results[1] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $results;
  }

  /**
   * cURL GET with authentication
   *
   * @param string $curlUrl
   *  The URL to GET to. Include domain and path.
   *
   * @return object $result
   *   The results returned from the cURL call.
   */
  public function curlGETauth($curlUrl) {

    // Remove authentication until POST to /api/v1/auth/login is resolved
    if (!isset($this->auth)) {
      $this->authenticate();
    }

    $results = $this->curlGET($curlUrl, TRUE);

    return $results;
  }

  /**
   * cURL GET Image (curlGETImage).
   *
   * @param string $imageUrl
   *  The URL to GET image from. Include domain and path.
   *
   * @return boolean $results
   *  Response from cURL GET request.
   *
   * @return object $result
   *   The results returned from the cURL call.
   *   - [0]: Results in json format
   *   - [1]: Status code
   */
  static public function curlGETImage($imageUrl) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $imageUrl);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

    $results[0] = curl_exec($ch);
    $results[1] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $results;
  }

  /**
   * cURL POSTs
   *
   * @param string $curlUrl
   *  The URL to POST to. Include domain and path.
   * @param array $post
   *  The values to POST.
   * @param boolean $isAuth
   *  Optional flag to denote if the method is being called from curlPOSTauth().
   *
   * @return object $result
   *   The results returned from the cURL call as an array:
   *   - [0]: Results in json format
   *   - [1]: Status code
   */
  public function curlPOST($curlUrl, $post, $isAuth = FALSE) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $curlUrl);
    curl_setopt($ch, CURLOPT_POST, count($post));
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch,CURLOPT_TIMEOUT, 20);

    // Only add token and cookie values to header when values are available and
    // the curlPOSTauth() method is making the POST request.
    $northstarConfig = $this->mbConfig->getProperty('northstar_config');
    if (isset($this->auth->token) && $isAuth) {
      curl_setopt($ch, CURLOPT_HTTPHEADER,
        array(
          'Content-type: application/json',
          'Accept: application/json',
          'X-CSRF-Token: ' . $this->auth->token,
          'Cookie: ' . $this->auth->session_name . '=' . $this->auth->sessid
        )
      );
    }
    elseif (strpos($curlUrl, 'api.dosomething') !== FALSE && isset($northstarConfig['id']) && isset($northstarConfig['key'])) {
      curl_setopt($ch, CURLOPT_HTTPHEADER,
        array(
          'Content-type: application/json',
          'Accept: application/json',
          'X-DS-Application-Id: ' . $northstarConfig['id'],
          'X-DS-REST-API-Key: ' . $northstarConfig['key'],
        )
      );
    }
    elseif (strpos($curlUrl, 'api.dosomething') !== FALSE) {<commit_msg>Turn off warnings for missing northstar_config value<commit_after><?php
/*
 * Message Broker ("MB") toolbox class library for functionality related to CURL requests.
 */

namespace DoSomething\MB_Toolbox;

use DoSomething\MBStatTracker\StatHat;

/**
 * MB_Toolbox_cURL:
 */
class MB_Toolbox_cURL
{

  const DRUPAL_API = '/api/v1';
  const TIMEOUT = 0;

  /**
   * Singleton instance of MB_Configuration application settings and service objects
   *
   * @var object
   */
  protected $mbConfig;

  /**
   * Setting from external service to track activity - StatHat.
   *
   * @var object
   */
  private $statHat;

  /**
   * Authentication details from Drupal site
   *
   * @var object
   */
  private $auth;

  /**
   * Constructor
   *
   * @param array $settings
   *   Connection and configuration settings common to the application
   *
   * @return object
   */
  public function __construct() {

    $this->auth = NULL;

    // Check for cURL on server
    if (!is_callable('curl_init')) {
      throw new Exception("Error - PHP cURL extension is not enabled on the server. cURL is required by many of the methods in the mb-toolbox library.");
    }

    $this->mbConfig = MB_Configuration::getInstance();
    $this->statHat = $this->mbConfig->getProperty('statHat');
  }

  /**
   * cURL GETs
   *
   * @param string $curlUrl
   *  The URL to GET from. Include domain and path.
   * @param boolean $isAuth
   *  Optional flag to keep track of the current authencation state.
   *
   * @return boolean $results
   *  Response from cURL GET request.
   *
   * @return object $result
   *   The results returned from the cURL call.
   *   - [0]: Results in json format
   *   - [1]: Status code
   */
  public function curlGET($curlUrl, $isAuth = FALSE) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $curlUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // Only add token and cookie values to header when values are available and
    // the curlPOSTauth() method is making the POST request.
    if (isset($this->auth->token) && $isAuth) {
      curl_setopt($ch, CURLOPT_HTTPHEADER,
        array(
          'Content-type: application/json',
          'Accept: application/json',
          'X-CSRF-Token: ' . $this->auth->token,
          'Cookie: ' . $this->auth->session_name . '=' . $this->auth->sessid
        )
      );
    }
    else {
      curl_setopt($ch, CURLOPT_HTTPHEADER,
        array(
          'Content-type: application/json',
          'Accept: application/json'
        )
      );
    }

    $jsonResult = curl_exec($ch);
    $results[0] = json_decode($jsonResult);
    $results[1] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $results;
  }

  /**
   * cURL GET with authentication
   *
   * @param string $curlUrl
   *  The URL to GET to. Include domain and path.
   *
   * @return object $result
   *   The results returned from the cURL call.
   */
  public function curlGETauth($curlUrl) {

    // Remove authentication until POST to /api/v1/auth/login is resolved
    if (!isset($this->auth)) {
      $this->authenticate();
    }

    $results = $this->curlGET($curlUrl, TRUE);

    return $results;
  }

  /**
   * cURL GET Image (curlGETImage).
   *
   * @param string $imageUrl
   *  The URL to GET image from. Include domain and path.
   *
   * @return boolean $results
   *  Response from cURL GET request.
   *
   * @return object $result
   *   The results returned from the cURL call.
   *   - [0]: Results in json format
   *   - [1]: Status code
   */
  static public function curlGETImage($imageUrl) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $imageUrl);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT);
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

    $results[0] = curl_exec($ch);
    $results[1] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $results;
  }

  /**
   * cURL POSTs
   *
   * @param string $curlUrl
   *  The URL to POST to. Include domain and path.
   * @param array $post
   *  The values to POST.
   * @param boolean $isAuth
   *  Optional flag to denote if the method is being called from curlPOSTauth().
   *
   * @return object $result
   *   The results returned from the cURL call as an array:
   *   - [0]: Results in json format
   *   - [1]: Status code
   */
  public function curlPOST($curlUrl, $post, $isAuth = FALSE) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $curlUrl);
    curl_setopt($ch, CURLOPT_POST, count($post));
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch,CURLOPT_TIMEOUT, 20);

    // Only add token and cookie values to header when values are available and
    // the curlPOSTauth() method is making the POST request.
    $northstarConfig = $this->mbConfig->getProperty('northstar_config', FALSE);
    if (isset($this->auth->token) && $isAuth) {
      curl_setopt($ch, CURLOPT_HTTPHEADER,
        array(
          'Content-type: application/json',
          'Accept: application/json',
          'X-CSRF-Token: ' . $this->auth->token,
          'Cookie: ' . $this->auth->session_name . '=' . $this->auth->sessid
        )
      );
    }
    elseif (strpos($curlUrl, 'api.dosomething') !== FALSE && isset($northstarConfig['id']) && isset($northstarConfig['key'])) {
      curl_setopt($ch, CURLOPT_HTTPHEADER,
        array(
          'Content-type: application/json',
          'Accept: application/json',
          'X-DS-Application-Id: ' . $northstarConfig['id'],
          'X-DS-REST-API-Key: ' . $northstarConfig['key'],
        )
      );
    }
    elseif (strpos($curlUrl, 'api.dosomething') !== FALSE) {
{'fraction_non_alphanumeric': 0.12977033327577275, 'fraction_numerical': 0.003453634950785702, 'mean_word_length': 2.6841603053435112, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, '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': '321027', 'n_tokens_mistral': 3854, 'n_tokens_neox': 3689, 'n_words': 983}