Ejemplo n.º 1
0
 public static function api($posts)
 {
     if (class_exists('JHttp')) {
         $posts_default = array('platform' => N2Platform::getPlatform());
         $client = new JHttp();
         $response = $client->post(self::$api, $posts + $posts_default, array('Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'));
         if ($response->code != '200') {
             N2Message::error(n2_('Unable to contact with the licensing server, please try again later!'));
             return array('status' => 'ERROR_HANDLED');
         }
         if (isset($response->headers['Content-Type'])) {
             $contentType = $response->headers['Content-Type'];
         }
         $data = $response->body;
     }
     if (!isset($data)) {
         if (function_exists('curl_init')) {
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, self::$api);
             $posts_default = array('platform' => N2Platform::getPlatform());
             curl_setopt($ch, CURLOPT_POSTFIELDS, $posts + $posts_default);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
             $data = curl_exec($ch);
             $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
             $error = curl_error($ch);
             $curlErrorNumber = curl_errno($ch);
             curl_close($ch);
             if ($curlErrorNumber) {
                 N2Message::error($curlErrorNumber . $error);
                 return array('status' => 'ERROR_HANDLED');
             }
         } else {
             $posts_default = array('platform' => N2Platform::getPlatform());
             $opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => http_build_query($posts + $posts_default)));
             $context = stream_context_create($opts);
             $data = file_get_contents(self::$api, false, $context);
             if ($data === false) {
                 N2Message::error(n2_('CURL disabled in your php.ini configuration. Please enable it!'));
                 return array('status' => 'ERROR_HANDLED');
             }
             $headers = self::parseHeaders($http_response_header);
             if ($headers['status'] != '200') {
                 N2Message::error(n2_('Unable to contact with the licensing server, please try again later!'));
                 return array('status' => 'ERROR_HANDLED');
             }
             if (isset($headers['content-type'])) {
                 $contentType = $headers['content-type'];
             }
         }
     }
     switch ($contentType) {
         case 'application/json':
             return json_decode($data, true);
     }
     return $data;
 }
 /**
  * Send data to PayPal servers and handling the PayPal method "doVoid".
  *
  * <code>
  * $url  = "https://api-3t.paypal.com/nvp";
  *
  * $options = new Registry();
  * $options->set("credentials.username", "itprism");
  * ....
  *
  * $paypal = new Prism\PayPal\Express($url, $options);
  * $paypal->doVoid();
  * </code>
  *
  * @return string
  *
  * @throws \RuntimeException
  */
 public function doVoid()
 {
     $data = array("METHOD" => "DoVoid", "USER" => $this->options->get("credentials.username"), "PWD" => $this->options->get("credentials.password"), "SIGNATURE" => $this->options->get("credentials.signature"), "VERSION" => $this->options->get("api.version"));
     $data["AUTHORIZATIONID"] = $this->options->get("payment.authorization_id");
     $response = $this->transport->post($this->url, $data);
     $body = $this->parseResponse($response);
     if (strcmp("Success", $body["ACK"]) != 0) {
         $this->error = ArrayHelper::getValue($body, "L_SHORTMESSAGE0") . ":" . ArrayHelper::getValue($body, "L_LONGMESSAGE0");
         $this->errorCode = ArrayHelper::getValue($body, "L_ERRORCODE0");
         throw new \RuntimeException($this->error, $this->errorCode);
     }
     return $body;
 }
Ejemplo n.º 3
0
 /**
  * Method to send the request which does not require authentication.
  *
  * @param   string  $path     The path of the request to make
  * @param   string  $method   The request method.
  * @param   array   $headers  The headers passed in the request.
  * @param   mixed   $data     Either an associative array or a string to be sent with the post request.
  *
  * @return  SimpleXMLElement  The XML response
  *
  * @since   13.1
  * @throws  DomainException
  */
 public function sendRequest($path, $method = 'GET', $headers = array(), $data = '')
 {
     // Send the request.
     switch ($method) {
         case 'GET':
             $response = $this->client->get($path, $headers);
             break;
         case 'POST':
             $response = $this->client->post($path, $data, $headers);
             break;
     }
     // Validate the response code.
     if ($response->code != 200) {
         $error = htmlspecialchars($response->body);
         throw new DomainException($error, $response->code);
     }
     $xml_string = simplexml_load_string($response->body);
     return $xml_string;
 }
 /**
  * Send data to PayPal servers and handling the PayPal method "CancelPreapproval".
  *
  * <code>
  * $url  = "https://svcs.sandbox.paypal.com/AdaptivePayments";
  *
  * $options = new Registry();
  * $options->set("credentials.username", "itprism");
  * ....
  *
  * $paypal = new PrismPayPalAdaptive($url, $options);
  * $paypal->doVoid();
  * </code>
  *
  * @return Response
  *
  * @throws \RuntimeException
  */
 public function doVoid()
 {
     $url = $this->url . "CancelPreapproval";
     $data = array("preapprovalKey" => $this->options->get("payment.preapproval_key"), "requestEnvelope" => $this->options->get("request.envelope"));
     // Encode data to JSON.
     $jsonData = json_encode($data);
     // Prepare headers.
     $headers = $this->getHeaders($jsonData);
     // Make a request.
     $response = $this->transport->post($url, $jsonData, $headers);
     $response = $this->parseResponse($response, "json");
     $response = new Response($response);
     if ($response->isFailure()) {
         // Failure
         $this->error = $response->getErrorMessage();
         $this->errorCode = $response->getErrorCode();
         throw new \RuntimeException($this->error, $this->errorCode);
     }
     return $response;
 }
Ejemplo n.º 5
0
 public function verify()
 {
     // if Recaptcha is not enabled, return true
     if (!$this->enabled) {
         return true;
     }
     // get the Recaptcha response from the form data
     $response = JFactory::getApplication()->input->get('g-recaptcha-response');
     if (!$response) {
         return false;
     }
     // send it to verification server for confirmation
     $http = new JHttp();
     $result = $http->post($this->verifyUrl, array('secret' => $this->privateKey, 'remoteip' => $this->ip, 'response' => $response));
     $result = json_decode($result->body);
     return $result->success === true ? true : false;
 }
Ejemplo n.º 6
0
 /**
  * Get JSON tweet data by using OAuth 2.0 authentification
  *
  * @param   int  $tweetid  The tweet ID to query against twitter API
  *
  * @return string
  */
 protected function getTweet($tweetid)
 {
     // FIXME: use AJAX instead...
     jimport('joomla.filesystem.folder');
     $config = KunenaFactory::getConfig();
     $uri = JURI::getInstance();
     $consumer_key = trim($config->twitter_consumer_key);
     $consumer_secret = trim($config->twitter_consumer_secret);
     if (JFile::exists(JPATH_CACHE . '/kunena_tweet/kunenatweetdisplay-' . $tweetid . '.json')) {
         $tweet_data = file_get_contents(JPATH_CACHE . '/kunena_tweet/kunenatweetdisplay-' . $tweetid . '.json');
         if ($tweet_data !== false) {
             return json_decode($tweet_data);
         }
     }
     if (!empty($consumer_key) && !empty($consumer_secret) && empty($this->token)) {
         $bearer_token_credentials = $consumer_key . ":" . $consumer_secret;
         $b64_bearer_token_credentials = base64_encode($bearer_token_credentials);
         $url = 'https://api.twitter.com/oauth2/token';
         $options = new JRegistry();
         $transport = new JHttpTransportStream($options);
         // Create a 'stream' transport.
         $http = new JHttp($options, $transport);
         $headers = array('Authorization' => "Basic " . $b64_bearer_token_credentials);
         $data = "grant_type=client_credentials";
         $response = $http->post($url, $data, $headers);
         if ($response->code == 200) {
             $this->token = json_decode($response->body)->access_token;
         } else {
             $tweet = new stdClass();
             $tweet->error = JText::_('COM_KUNENA_LIB_BBCODE_TWITTER_COULD_NOT_GET_TOKEN');
             return $tweet;
         }
     } elseif (empty($consumer_key) || empty($consumer_secret)) {
         $tweet = new stdClass();
         $tweet->error = JText::_('COM_KUNENA_LIB_BBCODE_TWITTER_CONSUMMER_KEY_SECRET_INVALID');
         return $tweet;
     }
     if (!empty($this->token)) {
         $url = 'https://api.twitter.com/1.1/statuses/show.json?id=' . $tweetid;
         $options = new JRegistry();
         $transport = new JHttpTransportStream($options);
         // Create a 'stream' transport.
         $http = new JHttp($options, $transport);
         $headers = array('Authorization' => "Bearer " . $this->token);
         $data = array();
         $response = $http->get($url, $headers);
         if ($response->code == 200) {
             $tweet_data = json_decode($response->body);
             if ($uri->isSSL()) {
                 $tweet_data->user->profile_image_url = $tweet_data->user->profile_image_url_https;
             }
             $tweet_data->user->profile_image_url_big = str_replace('normal', 'bigger', $tweet_data->user->profile_image_url);
             if (!empty($tweet_data->entities->urls)) {
                 foreach ($tweet_data->entities->urls as $url) {
                     if (isset($url->display_url)) {
                         $d_url = $url->display_url;
                     } else {
                         $d_url = $url->url;
                     }
                     // We need to check to verify that the URL has the protocol, just in case
                     if (strpos($url->url, 'http') !== 0) {
                         // Prepend http since there's no protocol
                         $link = 'http://' . $url->url;
                     } else {
                         $link = $url->url;
                     }
                     $tweet_data->text = str_replace($url->url, '<a href="' . $link . '" target="_blank" rel="nofollow">' . $d_url . '</a>', $tweet_data->text);
                 }
             }
             if (!empty($tweet_data->entities->user_mentions)) {
                 foreach ($tweet_data->entities->user_mentions as $mention) {
                     $tweet_data->text = str_replace('@' . $mention->screen_name, '<a href="https://twitter.com/' . $mention->screen_name . '" target="_blank" rel="nofollow">@' . $mention->screen_name . '</a>', $tweet_data->text);
                 }
             }
             if (!empty($tweet_data->entities->hashtags)) {
                 foreach ($tweet_data->entities->hashtags as $hashtag) {
                     $tweet_data->text = str_replace('#' . $hashtag->text, '<a href="https://twitter.com/hashtag/' . $hashtag->text . '?src=hash" target="_blank" rel="nofollow">#' . $hashtag->text . '</a>', $tweet_data->text);
                 }
             }
             if (!empty($tweet_data->extended_entities->media)) {
                 foreach ($tweet_data->extended_entities->media as $media) {
                     $tweet_data->text = str_replace($tweet_data->extended_entities->media[0]->url, '', $tweet_data->text);
                     if ($media->type == 'photo') {
                         if ($uri->isSSL()) {
                             $tweet_data->text .= '<img src="' . $media->media_url_https . '" alt="" />';
                         } else {
                             $tweet_data->text .= '<img src="' . $media->media_url . '" alt="" />';
                         }
                     } elseif ($media->type == 'video') {
                         if ($uri->isSSL()) {
                             $tweet_data->text .= '<a href="' . $media->url . '"><img src="' . $media->media_url_https . '" alt="" /></a>';
                         } else {
                             $tweet_data->text .= '<a href="' . $media->url . '"><img src="' . $media->media_url . '" alt="" /></a>';
                         }
                     } elseif ($media->type == 'animated_gif') {
                         if ($uri->isSSL()) {
                             $tweet_data->text .= '<a href="' . $media->url . '"><img src="' . $media->media_url_https . '" alt="" /></a>';
                         } else {
                             $tweet_data->text .= '<a href="' . $media->url . '"><img src="' . $media->media_url . '" alt="" /></a>';
                         }
                     }
                 }
             }
             if (!JFolder::exists(JPATH_CACHE . '/kunena_tweet')) {
                 JFolder::create(JPATH_CACHE . '/kunena_tweet');
             }
             $tweet_data->error = false;
             file_put_contents(JPATH_CACHE . '/kunena_tweet/kunenatweetdisplay-' . $tweetid . '.json', json_encode($tweet_data));
             return $tweet_data;
         } else {
             $tweet = new stdClass();
             $tweet->error = JText::_('COM_KUNENA_LIB_BBCODE_TWITTER_INVALID_TWEET_ID');
             return $tweet;
         }
     }
 }
Ejemplo n.º 7
0
 /**
  * Renders the notification item that is loaded for the user.
  *
  * This trigger is useful when you want to manipulate the notification item that appears
  * in the notification drop down.
  *
  * @since	1.0
  * @access	public
  * @param	string
  * @return
  */
 public function onNotificationBeforeCreate($item_dt)
 {
     //checking table is exists in database then only execute patch.
     $db = FD::db();
     $stbl = $db->getTableColumns('#__social_gcm_users');
     $reg_ids = array();
     $targetUsers = array();
     // Create a new query object.
     $query = $db->getQuery(true);
     // Select records from the user social_gcm_users table".
     $query->select($db->quoteName(array('device_id', 'sender_id', 'server_key', 'user_id')));
     $query->from($db->quoteName('#__social_gcm_users'));
     // Reset the query using our newly populated query object.
     $db->setQuery($query);
     // Load the results as a list of stdClass objects (see later for more options on retrieving data).
     $urows = $db->loadObjectList();
     $rule = $item_dt->rule;
     //Generate element from rule.
     $segments = explode('.', $rule);
     $element = array_shift($segments);
     $participants = $item_dt->participant;
     $emailOptions = $item_dt->email_options;
     $systemOptions = $item_dt->sys_options;
     $msg_data = $this->createMessageData($element, $emailOptions, $systemOptions, $participants);
     $targetUsers = $msg_data['tusers'];
     $count = rand(1, 100);
     $user = FD::user();
     $actor_name = $user->username;
     $tit = JText::_($msg_data['title']);
     $tit = str_replace('{actor}', $actor_name, $tit);
     foreach ($urows as $notfn) {
         if (in_array($notfn->user_id, $targetUsers)) {
             $reg_ids[] = $notfn->device_id;
             $server_k = $notfn->server_key;
         }
     }
     //build message for GCM
     if (!empty($reg_ids)) {
         //increment counter
         $registatoin_ids = $reg_ids;
         // Message to be sent
         $message = $tit;
         //Google cloud messaging GCM-API url
         $url = 'https://gcm-http.googleapis.com/gcm/send';
         //Setting headers for gcm service.
         $headers = array('Authorization' => 'key=' . $server_k, 'Content-Type' => 'application/json');
         //Setting fields for gcm service.
         //fields contents what data to be sent.
         $fields = array('registration_ids' => $registatoin_ids, 'data' => array("title" => $message, "message" => $msg_data['mssge'], "notId" => $count, "url" => $msg_data['ul'], "body" => $msg_data['mssge']));
         //Making call to GCM  API using POST.
         jimport('joomla.client.http');
         //Using JHttp for API call
         $http = new JHttp();
         $options = new JRegistry();
         //$transport = new JHttpTransportStream($options);
         $http = new JHttp($options);
         $gcmres = $http->post($url, json_encode($fields), $headers);
     }
 }
Ejemplo n.º 8
0
	/**
	 * Internal function to perform the actual remote API call
	 * 
	 * @param string $url Remote to the remote site
	 * @param array $query Query parameters in key=>value array format
	 * @return object
	 */
	private function executeJSONQuery($url, $query)
	{
		jimport('joomla.client.http');
		
		$http = new JHttp(array(
			'timeout'		=> 10
		));
		
		if($this->_verb == 'GET') {
			jimport('joomla.environment.uri');
			$uri = new JURI($url);
			if(!empty($query)) {
				foreach($query as $k => $v) {
					$uri->setVar($k, $v);
				}
			}
			$response = $http->get($uri->toString());
		} else {
			$response = $http->post($url, $query);
		}
		
		if($response->code != 200) {
			JLog::add('HTTP error '.$response->code, JLog::ERROR);
			throw new Exception(JText::sprintf('COM_AKEEBAEXAMPLE_APIERR_HTTPERROR',$response->code));
		}
		
		$raw = $response->body;

		$startPos = strpos($raw,'###') + 3;
		$endPos = strrpos($raw,'###');
		if( ($startPos !== false) && ($endPos !== false) ) {
			$json = substr($raw, $startPos, $endPos - $startPos);
		} else {
			$json = $raw;
		}
		$result = json_decode($json, false);
		
		if(is_null($result)) {
			JLog::add('JSON decoding error', JLog::ERROR);
			JLog::add($json, JLog::DEBUG);
			throw new Exception(JText::_('COM_AKEEBAEXAMPLE_APIERR_JSONDECODING'));
		}
		return $result;
	}
Ejemplo n.º 9
0
 /**
  * 
  */
 public function preview()
 {
     $my = JXFactory::getUser();
     $fileid = JRequest::getVar('file_id');
     $file = JTable::getInstance('File', 'StreamTable');
     $jxConfig = new JXConfig();
     $data = array();
     $data['reload'] = false;
     if ($file->load($fileid) && $my->authorise('stream.file.download', $file) && ($jxConfig->isCrocodocsEnabled() || $jxConfig->isScribdEnabled())) {
         // Grab the uuid
         $uuid = $file->getParam('uuid');
         $docid = $file->getParam('doc_id');
         if ($jxConfig->isCrocodocsEnabled() && empty($uuid) || $jxConfig->isScribdEnabled() && empty($docid)) {
             // haven't been uploaded yet, upload and grab the uuid
             $store = $file->preparePreview();
             // reload file for updated content
             $file->load($fileid);
             $uuid = $file->getParam('uuid');
             $docid = $file->getParam('doc_id');
         }
         // Check if file is ready
         $previewReady = $file->getParam('preview-ready');
         $isViewable = false;
         if (empty($previewReady)) {
             if ($jxConfig->isCrocodocsEnabled()) {
                 // Preview not ready, query status
                 $options = new JRegistry();
                 $transport = new JHttpTransportCurl($options);
                 $http = new JHttp($options, $transport);
                 $response = $http->get('https://crocodoc.com/api/v2/document/status?token=' . $jxConfig->get(JXConfig::CROCODOCS) . '&uuids=' . $file->getParam('uuid'));
                 $responseObj = json_decode($response->body);
                 //print_r($responseObj); exit;
                 if (!isset($responseObj->error)) {
                     $isViewable = !empty($responseObj[0]->viewable);
                     if ($isViewable) {
                         $previewReady = true;
                         $file->setParam('preview-ready', 1);
                         $file->store();
                     }
                 }
             } else {
                 // Query Scribd Preview status
                 $http = new JHttp();
                 $statusUrl = 'http://api.scribd.com/api?api_key=' . $jxConfig->get(JXConfig::SCRIBD_API) . '&doc_id=' . $file->getParam('doc_id') . '&method=docs.getConversionStatus&my_user_id=' . $file->user_id;
                 $response = $http->get($statusUrl);
                 $result = simplexml_load_string($response->body);
                 if (isset($result->conversion_status) && $result->conversion_status == 'DONE') {
                     $previewReady = true;
                     $file->setParam('preview-ready', 1);
                     $file->store();
                 }
             }
         }
         if ($previewReady && (!empty($uuid) || !empty($docid))) {
             if ($jxConfig->isCrocodocsEnabled()) {
                 $session_id = $file->getParam('previewSession');
                 if (time() > $file->getParam('previewExpiry')) {
                     // File uploaded, try to create session
                     $options = new JRegistry();
                     $transport = new JHttpTransportCurl($options);
                     $http = new JHttp($options, $transport);
                     $response = $http->post('https://crocodoc.com/api/v2/session/create', array('token' => '$jxConfig->get(JXConfig::CROCODOCS)', 'uuid' => $file->getParam('uuid')));
                     //$response =  $http->post( 'https://crocodoc.com/api/v2/session/create' , array('token' => 'Oe8fA1mQ59LSwtBlKy4Nkbvn', 'uuid' => $uuid ) );
                     $responseObj = json_decode($response->body);
                     $session_id = $responseObj->session;
                     // Store this session so that we don't have to fetch it again
                     $file->setParam('previewSession', $session_id);
                     $file->setParam('previewExpiry', time() + 60 * 50);
                     // ste it 50 mins from now
                     $file->store();
                 }
                 $html = '<iframe style="border:0px;width:100%;height:100%" src="https://crocodoc.com/view/' . $session_id . '"></iframe>';
             } else {
                 $html = "<div id='embedded_doc' data-doc_id=\"" . $file->getParam("doc_id") . "\" data-access_key=\"" . $file->getParam("access_key") . "\"><a href='http://www.scribd.com'>Scribd</a></div>\t\t\t\t\t\t\n\t\t\t\t\t\t\t <script type=\"text/javascript\">\n\t\t\t\t\t\t\t\t// Instantiate iPaper\n\t\t\t\t\t\t\t\t\$(document).ready(function() {\n\t\t\t\t\t\t\t\t\tscribd_doc = scribd.Document.getDoc(\$('#embedded_doc').data('doc_id'), \$('#embedded_doc').data('access_key'));\n\t\t\t\t\t\t\t\t\t// Parameters\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('height', 750);\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('width', 750);\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('auto_size', true);\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('mode', 'slideshow');\n\t\t\t\t\t\t\t\t\tscribd_doc.addParam('jsapi_version', 2);\n\n\t\t\t\t\t\t\t\t\t// Attach event listeners\n\t\t\t\t\t\t\t\t\tscribd_doc.addEventListener('docReady', onDocReady);\n\n\t\t\t\t\t\t\t\t\t// Write the instance\n\t\t\t\t\t\t\t\t\tscribd_doc.write('embedded_doc');\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t  \n\t\t\t\t\t\t\t\tvar onDocReady = function(e) {\n\t\t\t\t\t\t\t\t\tscribd_doc.api.setPage(1);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t  \n\t\t\t\t\t\t\t\t// Bookmark Helpers\n\t\t\t\t\t\t\t\tvar goToPage = function(page) {\n\t\t\t\t\t\t\t\t\tif (scribd_doc.api){\n\t\t\t\t\t\t\t\t\t\tscribd_doc.api.setPage(page);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t  \n\t\t\t\t\t\t\t\tvar goToMiddle = function() {\n\t\t\t\t\t\t\t\t\tif (scribd_doc.api){\n\t\t\t\t\t\t\t\t\t\tgoToPage( Math.floor(scribd_doc.api.getPageCount()/2) );\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\tvar goToEnd = function() {\n\t\t\t\t\t\t\t\t\tif (scribd_doc.api) {\n\t\t\t\t\t\t\t\t\t\tgoToPage(scribd_doc.api.getPageCount());\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t  </script>";
             }
             $data['html'] = $html;
         } else {
             $data['reload'] = 1;
             $data['file_id'] = $fileid;
             $data['html'] = '<p>Please wait while we prepare your document</p>';
         }
     } else {
         $data['html'] = "<p>Not allowed to preview.</p>";
     }
     echo json_encode($data);
     exit;
 }