<commit_before><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Repository\Vcs;

use Composer\IO\IOInterface;
use Composer\Util\ProcessExecutor;

/**
 * A driver implementation for driver with authorization interaction.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class VcsDriver
{
    protected $url;
    protected $io;
    protected $process;
    private $firstCall;
    private $contentUrl;
    private $content;

    /**
     * Constructor.
     *
     * @param string      $url The URL
     * @param IOInterface $io  The IO instance
     * @param ProcessExecutor $process  Process instance, injectable for mocking
     */
    public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
    {
        $this->url = $url;
        $this->io = $io;
        $this->process = $process ?: new ProcessExecutor;
        $this->firstCall = true;
    }

    /**
     * Get the https or http protocol depending on SSL support.
     *
     * Call this only if you know that the server supports both.
     *
     * @return string The correct type of protocol
     */
    protected function getScheme()
    {
        if (extension_loaded('openssl')) {
            return 'https';
        }
        return 'http';
    }

    /**
     * Get the remote content.
     *
     * @param string $url The URL of content
     *
     * @return mixed The result
     */
    protected function getContents($url)
    {
        $this->contentUrl = $url;
        $auth = $this->io->getAuthorization($this->url);
        $params = array();

        // add authorization to curl options
        if ($this->io->hasAuthorization($this->url)) {
            $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
            $params['http'] = array('header' => "Authorization: Basic $authStr\r\n");
        } else if (null !== $this->io->getLastUsername()) {
            $authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword());
            $params['http'] = array('header' => "Authorization: Basic $authStr\r\n");
        }

        $ctx = stream_context_create($params);
        stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet')));

        $content = @file_get_contents($url, false, $ctx);

        // content get after authorization
        if (false === $content) {
            $content = $this->content;
            $this->content = null;
            $this->contentUrl = null;
        }

        return $content;
    }

    /**
     * Get notification action.
     *
     * @param integer $notificationCode The notification code
     * @param integer $severity         The severity level
     * @param string  $message          The message<commit_msg>Fix bug on authorization of file download for the private repositories<commit_after><?php

/*
 * This file is part of Composer.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Composer\Repository\Vcs;

use Composer\IO\IOInterface;
use Composer\Util\ProcessExecutor;

/**
 * A driver implementation for driver with authorization interaction.
 *
 * @author <NAME> <<EMAIL>>
 */
abstract class VcsDriver
{
    protected $url;
    protected $io;
    protected $process;
    private $firstCall;
    private $contentUrl;
    private $content;

    /**
     * Constructor.
     *
     * @param string      $url The URL
     * @param IOInterface $io  The IO instance
     * @param ProcessExecutor $process  Process instance, injectable for mocking
     */
    public function __construct($url, IOInterface $io, ProcessExecutor $process = null)
    {
        $this->url = $url;
        $this->io = $io;
        $this->process = $process ?: new ProcessExecutor;
        $this->firstCall = true;
    }

    /**
     * Get the https or http protocol depending on SSL support.
     *
     * Call this only if you know that the server supports both.
     *
     * @return string The correct type of protocol
     */
    protected function getScheme()
    {
        if (extension_loaded('openssl')) {
            return 'https';
        }
        return 'http';
    }

    /**
     * Get the remote content.
     *
     * @param string $url The URL of content
     *
     * @return mixed The result
     */
    protected function getContents($url)
    {
        $this->contentUrl = $url;
        $auth = $this->io->getAuthorization($this->url);
        $params = array();

        // add authorization to curl options
        if ($this->io->hasAuthorization($this->url)) {
            $authStr = base64_encode($auth['username'] . ':' . $auth['password']);
            $params['http'] = array('header' => "Authorization: Basic $authStr\r\n");
        } else if (null !== $this->io->getLastUsername()) {
            $authStr = base64_encode($this->io->getLastUsername() . ':' . $this->io->getLastPassword());
            $params['http'] = array('header' => "Authorization: Basic $authStr\r\n");
            $this->io->setAuthorization($this->url, $this->io->getLastUsername(), $this->io->getLastPassword());
        }

        $ctx = stream_context_create($params);
        stream_context_set_params($ctx, array("notification" => array($this, 'callbackGet')));

        $content = @file_get_contents($url, false, $ctx);

        // content get after authorization
        if (false === $content) {
            $content = $this->content;
            $this->content = null;
            $this->contentUrl = null;
        }

        return $content;
    }

    /**
     * Get notification action.
     *
     * @param integer $notificationCode The notification code
     * @param integer $severity         The severity level
     * @param string  $message          The message
{'fraction_non_alphanumeric': 0.13237243207422134, 'fraction_numerical': 0.0013253810470510272, 'mean_word_length': 2.3445983379501385, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1447059', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1663, 'n_words': 480}