Skip to content

Commit

Permalink
fix: resilience against url shortener added
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Buckpesch committed Mar 25, 2020
1 parent 0c0713f commit 64ffbc6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"minimum-stability": "dev",
"require": {
"ext-curl": "*",
"ext-json": "*",
"ext-gd": "*",
"leafo/scssphp": "^v0.8.4",
"mobiledetect/mobiledetectlib": "^2.8.34",
Expand Down
41 changes: 19 additions & 22 deletions src/AppArena/Models/SmartLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use AppArena\Models\Environment\AbstractEnvironment;
use AppArena\Models\Environment\Facebook;
use AppArena\Models\Environment\Website;
use Exception;
use Mustache_Engine;
use Mustache_Loader_FilesystemLoader;

/**
* SmartLink class which handles user redirects for the app
Expand Down Expand Up @@ -57,7 +60,7 @@ class SmartLink {
* @param AbstractEnvironment $environment Environment the app is currently running in
* @param Cache $cache Cache adapter for managing the link shortener
*
* @throws \Exception When no app ID is passed
* @throws Exception When no app ID is passed
*/
public function __construct( AbstractEntity $entity, Environment $environment, Cache $cache ) {
// Initialize the base url
Expand All @@ -67,7 +70,7 @@ public function __construct( AbstractEntity $entity, Environment $environment, C
$this->environment = $environment;
$this->entity = $entity;
if ( ! $this->entity ) {
throw( new \Exception( 'No app id available' ) );
throw( new Exception( 'No app id available' ) );
}
$this->cookie_key = AppManager::COOKIE_KEY . $this->getEntity()->getId();

Expand Down Expand Up @@ -268,14 +271,12 @@ public function getParams( $includeExpired = false ) {
return $params;
}

/**
* Sets values to the SmartCookie
*
* @param array $values Array of key value pairs which should be added to the Smart-Cookie cookie
* @param int $expiration Number of seconds until the cookie will expire
*
* @return array Returns the whole updated cookie as array
*/
/**
* Sets values to the SmartCookie
*
* @param array $values Array of key value pairs which should be added to the Smart-Cookie cookie
* @param int $expiration Number of seconds until the cookie will expire
*/
private function setCookieValues( $values, $expiration = 7200 ) {
$cookie = [];
if ( isset( $_COOKIE[ $this->cookie_key ] ) ) {
Expand All @@ -294,9 +295,6 @@ private function setCookieValues( $values, $expiration = 7200 ) {
$cookie_encoded = json_encode( $cookie );

setcookie( $this->cookie_key, $cookie_encoded, time() + $expiration, '/', $this->cookie_domain );

return false;

}

/**
Expand All @@ -307,12 +305,12 @@ private function setCookieValues( $values, $expiration = 7200 ) {
public function renderSharePage( $debug = false ) {
if ( ! $this->mustache ) {
if ( ! defined( 'SMART_LIB_PATH' ) ) {
define( 'SMART_LIB_PATH', realpath( dirname( __FILE__ ) ) . '/..' );
define( 'SMART_LIB_PATH', realpath(__DIR__) . '/..' );
}
// Initialize mustache
$loader = new \Mustache_Loader_FilesystemLoader( SMART_LIB_PATH . '/views' );
$partials = new \Mustache_Loader_FilesystemLoader( SMART_LIB_PATH . '/views/partials' );
$this->mustache = new \Mustache_Engine( [
$loader = new Mustache_Loader_FilesystemLoader( SMART_LIB_PATH . '/views' );
$partials = new Mustache_Loader_FilesystemLoader( SMART_LIB_PATH . '/views/partials' );
$this->mustache = new Mustache_Engine( [
'loader' => $loader,
'partials_loader' => $partials,
] );
Expand All @@ -323,7 +321,7 @@ public function renderSharePage( $debug = false ) {
$meta = $this->getMeta();
if ( isset( $meta['image'] ) ) {
if ( extension_loaded( 'gd' ) && function_exists( 'gd_info' ) && $meta['image'] ) {
list( $width, $height ) = getimagesize( $meta['image'] );
[ $width, $height ] = getimagesize( $meta['image'] );
$this->meta['image_height'] = $height;
$this->meta['image_width'] = $width;
}
Expand Down Expand Up @@ -738,13 +736,12 @@ private function shortenLink( $url ) {

// Do something with the result. Here, we echo the long URL
$data = json_decode( $data );

$this->url_short_array[ $url ] = $data->shorturl;
$this->url_short_array[ $url ] = isset($data->shorturl)? $data->shorturl : $url;

$value->set( $this->url_short_array );
$cache->save( $value );

return $data->shorturl;
return isset($data->shorturl)? $data->shorturl : $url;
}

/**
Expand Down Expand Up @@ -838,7 +835,7 @@ private function parse_signed_request( $signed_request ) {
}

//$signed_request = $_REQUEST['signed_request'];
list( $encoded_sig, $payload ) = explode( '.', $signed_request, 2 );
[ $encoded_sig, $payload ] = explode( '.', $signed_request, 2 );
$data = json_decode( base64_decode( strtr( $payload, '-_', '+/' ) ), true );

return $data;
Expand Down

0 comments on commit 64ffbc6

Please sign in to comment.