/** * Construct * * @param $ip * @param $auth * @param $logger */ public function __construct($ip, $auth = null, Logger $logger = null) { $this->ip = $ip; $this->connector = HttpFactory::getHttp(); $this->logger = $logger; $this->auth = $auth; }
/** * @param MauticFactory $factory */ public function __construct(MauticFactory $factory) { $this->factory = $factory; // Moved to outside environment folder so that it doesn't get wiped on each config update $this->cacheFile = $this->factory->getSystemPath('cache') . '/../languageList.txt'; $this->connector = HttpFactory::getHttp(); }
/** * @param MauticFactory $factory */ public function __construct(MauticFactory $factory) { $this->factory = $factory; $options = array('transport.curl' => array(CURLOPT_SSL_VERIFYPEER => false)); // Moved to outside environment folder so that it doesn't get wiped on each config update $this->cacheFile = $this->factory->getSystemPath('cache') . '/../languageList.txt'; $this->connector = HttpFactory::getHttp($options); }
/** * Constructor. * * @param array $options Client options array. If the registry contains any headers.* elements, * these will be added to the request headers. * @param TransportInterface $transport The HTTP transport object. * * @since 1.0 * @throws \InvalidArgumentException */ public function __construct($options = array(), TransportInterface $transport = null) { $this->options = $options; if (!isset($transport)) { $transport = HttpFactory::getAvailableDriver($this->options); } // Ensure the transport is a TransportInterface instance or bail out if (!$transport instanceof TransportInterface) { throw new \InvalidArgumentException('A valid TransportInterface object was not set.'); } $this->transport = $transport; }
/** * Download remote data store * * Used by the mautic:iplookup:update_data command and form fetch button (if applicable) to update local IP data stores * * @return bool */ public function downloadRemoteDataStore() { $connector = HttpFactory::getHttp(); $package = $this->getRemoteDateStoreDownloadUrl(); try { $data = $connector->get($package); } catch (\Exception $exception) { $this->logger->error('Failed to fetch remote IP data: ' . $exception->getMessage()); } $tempTarget = $this->cacheDir . '/' . basename($package); $tempExt = strtolower(pathinfo($package, PATHINFO_EXTENSION)); $localTarget = $this->getLocalDataStoreFilepath(); $localTargetExt = strtolower(pathinfo($localTarget, PATHINFO_EXTENSION)); try { $success = false; switch (true) { case $localTargetExt === $tempExt: $success = (bool) file_put_contents($localTarget, $data->body); break; case 'gz' == $tempExt: if (function_exists('gzdecode')) { $success = (bool) file_put_contents($localTarget, gzdecode($data->body)); } elseif (function_exists('gzopen')) { if (file_put_contents($tempTarget, $data->body)) { $bufferSize = 4096; // read 4kb at a time $file = gzopen($tempTarget, 'rb'); $outFile = fopen($localTarget, 'wb'); while (!gzeof($file)) { fwrite($outFile, gzread($file, $bufferSize)); } fclose($outFile); gzclose($file); @unlink($tempTarget); $success = true; } } break; case 'zip' == $tempExt: file_put_contents($tempTarget, $data->body); $zipper = new \ZipArchive(); $zipper->open($tempTarget); $success = $zipper->extractTo($localTarget); $zipper->close(); @unlink($tempTarget); break; } } catch (\Exception $exception) { error_log($exception); $success = false; } return $success; }
/** * Retrieves an instance of the GitHub object * * @param \Joomla\Application\AbstractApplication $app Application object * @param boolean $useBot Flag to use a bot account. * @param string $botUser The bot account user name. * @param string $botPassword The bot account password. * * @return GitHub * * @since 1.0 * @throws \RuntimeException */ public static function getInstance($app, $useBot = false, $botUser = '', $botPassword = '') { $options = new Registry(); // Check if we're in the web application and a token exists if ($app instanceof \JTracker\Application) { $session = $app->getSession(); $token = $session->get('gh_oauth_access_token'); } else { $token = false; } // If a token is active in the session (web app), and we haven't been instructed to use a bot account, use that for authentication if ($token && !$useBot) { $options->set('gh.token', $token); } else { // Check if credentials are supplied if ($botUser && $botPassword) { $user = $botUser; $password = $botPassword; } else { // Check for support for multiple accounts $accounts = $app->get('github.accounts'); if ($accounts) { $user = isset($accounts[0]->username) ? $accounts[0]->username : null; $password = isset($accounts[0]->password) ? $accounts[0]->password : null; // Store the other accounts $options->set('api.accounts', $accounts); } else { // Support for a single account $user = $app->get('github.username'); $password = $app->get('github.password'); } } // Add the username and password to the options object if both are set if ($user && $password) { // Set the options from the first account $options->set('api.username', $user); $options->set('api.password', $password); } } // The cURL extension is required to properly work. $transport = HttpFactory::getAvailableDriver($options, array('curl')); // Check if we *really* got a cURL transport... if (!$transport instanceof Curl) { throw new \RuntimeException('Please enable cURL.'); } $http = new Http($options, $transport); // Instantiate the object return new Github($options, $http); }
/** * Constructor. * * @param array|\ArrayAccess $options Client options array. If the registry contains any headers.* elements, * these will be added to the request headers. * @param TransportInterface $transport The HTTP transport object. * * @since 1.0 * @throws \InvalidArgumentException */ public function __construct($options = array(), TransportInterface $transport = null) { if (!is_array($options) && !$options instanceof \ArrayAccess) { throw new \InvalidArgumentException('The options param must be an array or implement the ArrayAccess interface.'); } $this->options = $options; if (!isset($transport)) { $transport = HttpFactory::getAvailableDriver($this->options); } // Ensure the transport is a TransportInterface instance or bail out if (!$transport instanceof TransportInterface) { throw new \InvalidArgumentException('A valid TransportInterface object was not set.'); } $this->transport = $transport; }
/** * Make a basic call using cURL to get the data * * @param $url * @param array $parameters * @param string $method * @param array $settings * * @return mixed|string */ public function makeRequest($url, $parameters = array(), $method = 'GET', $settings = array()) { $method = strtoupper($method); $authType = empty($settings['auth_type']) ? $this->getAuthenticationType() : $settings['auth_type']; list($parameters, $headers) = $this->prepareRequest($url, $parameters, $method, $settings, $authType); if (empty($settings['ignore_event_dispatch'])) { $event = $this->dispatcher->dispatch(PluginEvents::PLUGIN_ON_INTEGRATION_REQUEST, new PluginIntegrationRequestEvent($this, $url, $parameters, $headers, $method, $settings, $authType)); $headers = $event->getHeaders(); $parameters = $event->getParameters(); } if (!isset($settings['query'])) { $settings['query'] = array(); } if (isset($parameters['append_to_query'])) { $settings['query'] = array_merge($settings['query'], $parameters['append_to_query']); unset($parameters['append_to_query']); } if (!$this->isConfigured()) { return array('error' => array('message' => $this->factory->getTranslator()->trans('mautic.integration.missingkeys'))); } if ($method == 'GET' && !empty($parameters)) { $parameters = array_merge($settings['query'], $parameters); $query = http_build_query($parameters); $url .= strpos($url, '?') === false ? '?' . $query : '&' . $query; } elseif (!empty($settings['query'])) { $query = http_build_query($settings['query']); $url .= strpos($url, '?') === false ? '?' . $query : '&' . $query; } // Check for custom content-type header if (!empty($settings['content_type'])) { $settings['encoding_headers_set'] = true; $headers[] = "Content-type: {$settings['content_type']}"; } if ($method !== 'GET') { if (!empty($parameters)) { if ($authType == 'oauth1a') { $parameters = http_build_query($parameters); } if (!empty($settings['encode_parameters'])) { if ($settings['encode_parameters'] == 'json') { //encode the arguments as JSON $parameters = json_encode($parameters); if (empty($settings['encoding_headers_set'])) { $headers[] = 'Content-Type: application/json'; } } } } } $options = array(CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_HEADER => 1, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FOLLOWLOCATION => 0, CURLOPT_REFERER => $this->getRefererUrl(), CURLOPT_USERAGENT => $this->getUserAgent()); if (isset($settings['curl_options'])) { $options = array_merge($options, $settings['curl_options']); } if (isset($settings['ssl_verifypeer'])) { $options[CURLOPT_SSL_VERIFYPEER] = $settings['ssl_verifypeer']; } $connector = HttpFactory::getHttp(array('transport.curl' => $options)); $parseHeaders = isset($settings['headers']) ? array_merge($headers, $settings['headers']) : $headers; // HTTP library requires that headers are in key => value pairs $headers = array(); if (is_array($parseHeaders)) { foreach ($parseHeaders as $key => $value) { if (strpos($value, ':') !== false) { list($key, $value) = explode(':', $value); $key = trim($key); $value = trim($value); } $headers[$key] = $value; } } try { switch ($method) { case 'GET': $result = $connector->get($url, $headers, 10); break; case 'POST': case 'PUT': case 'PATCH': $connectorMethod = strtolower($method); $result = $connector->{$connectorMethod}($url, $parameters, $headers, 10); break; case 'DELETE': $result = $connector->delete($url, $headers, 10); break; } } catch (\Exception $exception) { return array('error' => array('message' => $exception->getMessage(), 'code' => $exception->getCode())); } if (empty($settings['ignore_event_dispatch'])) { $event->setResponse($result); $this->dispatcher->dispatch(PluginEvents::PLUGIN_ON_INTEGRATION_RESPONSE, $event); } if (!empty($settings['return_raw'])) { return $result; } else { $response = $this->parseCallbackResponse($result->body, !empty($settings['authorize_session'])); return $response; } }
/** * @param MauticFactory $factory */ public function __construct(MauticFactory $factory) { $this->factory = $factory; $this->connector = HttpFactory::getHttp(); }
/** * Execute the controller. * * @return string The rendered view. * * @since 1.0 * @throws \Exception */ public function execute() { /* @type \JTracker\Application $app */ $app = $this->getContainer()->get('app'); $user = $app->getUser(); if ($user->id) { // The user is already logged in. $app->redirect(' '); } $error = $app->input->get('error'); if ($error) { // GitHub reported an error. throw new \Exception($error); } $code = $app->input->get('code'); if (!$code) { // No auth code supplied. throw new \Exception('Missing login code'); } // Do login /* * @todo J\oAuth scrambles our redirects - investigate.. * $options = new Registry( array( 'tokenurl' => 'https://github.com/login/oauth/access_token', 'redirect_uri' => $app->get('uri.request'), 'clientid' => $app->get('github.client_id'), 'clientsecret' => $app->get('github.client_secret') ) ); $oAuth = new oAuthClient($options); $token = $oAuth->authenticate(); $accessToken = $token['access_token']; */ $loginHelper = new GitHubLoginHelper($this->getContainer()); $accessToken = $loginHelper->requestToken($code); // Store the token into the session $app->getSession()->set('gh_oauth_access_token', $accessToken); // Get the current logged in GitHub user $options = new Registry(); $options->set('gh.token', $accessToken); // GitHub API works best with cURL $transport = HttpFactory::getAvailableDriver($options, array('curl')); $http = new Http($options, $transport); // Instantiate Github $gitHub = new Github($options, $http); $gitHubUser = $gitHub->users->getAuthenticatedUser(); $user = new GithubUser($app->getProject(), $this->getContainer()->get('db')); $user->loadGitHubData($gitHubUser)->loadByUserName($user->username); // Save the avatar $loginHelper->saveAvatar($user->username); // Set the last visit time $loginHelper->setLastVisitTime($user->id); // User login $app->setUser($user); $redirect = $app->input->getBase64('usr_redirect'); $redirect = $redirect ? base64_decode($redirect) : ''; // Set a "remember me" cookie. $app->setRememberMe(true); $app->redirect($redirect); }
/** * @param MauticFactory $factory */ public function __construct(MauticFactory $factory) { $this->factory = $factory; $options = array('transport.curl' => array(CURLOPT_SSL_VERIFYPEER => false)); $this->connector = HttpFactory::getHttp($options); }
/** * @param Http $http */ public function __construct(Http $http = null) { $this->http = null !== $http ? $http : HttpFactory::getHttp(); }
/** * Tests the getHttpTransports method. * * @return void * * @covers Joomla\Http\HttpFactory::getHttpTransports * @since 1.1.4 */ public function testGetHttpTransports() { $transports = array('Stream', 'Socket', 'Curl'); sort($transports); $this->assertEquals($transports, HttpFactory::getHttpTransports()); }
protected function getMautic_Http_ConnectorService() { return $this->services['mautic.http.connector'] = \Joomla\Http\HttpFactory::getHttp(); }
/** * Request an oAuth token from GitHub. * * @param string $code The code obtained form GitHub on the previous step. * * @return string The OAuth token * * @since 1.0 * @throws \RuntimeException * @throws \DomainException */ public function requestToken($code) { // GitHub API works best with cURL $options = new Registry(); $transport = HttpFactory::getAvailableDriver($options, array('curl')); if (false == $transport) { throw new \DomainException('No transports available (please install php-curl)'); } $http = new Http($options, $transport); $data = array('client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'code' => $code); $response = $http->post('https://github.com/login/oauth/access_token', $data, array('Accept' => 'application/json')); if (200 != $response->code) { if (JDEBUG) { var_dump($response); } throw new \DomainException('Invalid response from GitHub (2) :('); } $body = json_decode($response->body); if (isset($body->error)) { switch ($body->error) { case 'bad_verification_code': throw new \DomainException('bad verification code'); break; default: throw new \DomainException('Unknown (2) ' . $body->error); break; } } if (!isset($body->access_token)) { throw new \DomainException('Can not retrieve the access token'); } return $body->access_token; }
/** * Constructor. * * @param array $options Client options array. If the registry contains any headers.* elements, * these will be added to the request headers. * @param TransportInterface $transport The HTTP transport object. * * @since 1.0 */ public function __construct($options = array(), TransportInterface $transport = null) { $this->options = $options; $this->transport = isset($transport) ? $transport : HttpFactory::getAvailableDriver($this->options); }
/** * @param string $url * @param bool $jsondecode * * @return mixed|string */ private function getRemoteIpData($url, $jsondecode = true) { static $connector; if (empty($connector)) { $connector = HttpFactory::getHttp(); } try { $response = $connector->get($url); $data = $jsondecode ? json_decode($response->body) : $response->body; } catch (\Exception $exception) { $data = false; } return $data; }
/** * Tests the getAvailableDriver method. * * @return void * * @since 1.0 */ public function testGetAvailableDriver() { $this->assertThat(HttpFactory::getAvailableDriver(array(), array()), $this->isFalse(), 'Passing an empty array should return false due to there being no adapters to test'); $this->assertThat(HttpFactory::getAvailableDriver(array(), array('fopen')), $this->isFalse(), 'A false should be returned if a class is not present or supported'); }