Skip to content

Commit 64ffbc6

Browse files
author
Sebastian Buckpesch
committed
fix: resilience against url shortener added
1 parent 0c0713f commit 64ffbc6

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"minimum-stability": "dev",
2525
"require": {
2626
"ext-curl": "*",
27+
"ext-json": "*",
2728
"ext-gd": "*",
2829
"leafo/scssphp": "^v0.8.4",
2930
"mobiledetect/mobiledetectlib": "^2.8.34",

src/AppArena/Models/SmartLink.php

+19-22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use AppArena\Models\Environment\AbstractEnvironment;
88
use AppArena\Models\Environment\Facebook;
99
use AppArena\Models\Environment\Website;
10+
use Exception;
11+
use Mustache_Engine;
12+
use Mustache_Loader_FilesystemLoader;
1013

1114
/**
1215
* SmartLink class which handles user redirects for the app
@@ -57,7 +60,7 @@ class SmartLink {
5760
* @param AbstractEnvironment $environment Environment the app is currently running in
5861
* @param Cache $cache Cache adapter for managing the link shortener
5962
*
60-
* @throws \Exception When no app ID is passed
63+
* @throws Exception When no app ID is passed
6164
*/
6265
public function __construct( AbstractEntity $entity, Environment $environment, Cache $cache ) {
6366
// Initialize the base url
@@ -67,7 +70,7 @@ public function __construct( AbstractEntity $entity, Environment $environment, C
6770
$this->environment = $environment;
6871
$this->entity = $entity;
6972
if ( ! $this->entity ) {
70-
throw( new \Exception( 'No app id available' ) );
73+
throw( new Exception( 'No app id available' ) );
7174
}
7275
$this->cookie_key = AppManager::COOKIE_KEY . $this->getEntity()->getId();
7376

@@ -268,14 +271,12 @@ public function getParams( $includeExpired = false ) {
268271
return $params;
269272
}
270273

271-
/**
272-
* Sets values to the SmartCookie
273-
*
274-
* @param array $values Array of key value pairs which should be added to the Smart-Cookie cookie
275-
* @param int $expiration Number of seconds until the cookie will expire
276-
*
277-
* @return array Returns the whole updated cookie as array
278-
*/
274+
/**
275+
* Sets values to the SmartCookie
276+
*
277+
* @param array $values Array of key value pairs which should be added to the Smart-Cookie cookie
278+
* @param int $expiration Number of seconds until the cookie will expire
279+
*/
279280
private function setCookieValues( $values, $expiration = 7200 ) {
280281
$cookie = [];
281282
if ( isset( $_COOKIE[ $this->cookie_key ] ) ) {
@@ -294,9 +295,6 @@ private function setCookieValues( $values, $expiration = 7200 ) {
294295
$cookie_encoded = json_encode( $cookie );
295296

296297
setcookie( $this->cookie_key, $cookie_encoded, time() + $expiration, '/', $this->cookie_domain );
297-
298-
return false;
299-
300298
}
301299

302300
/**
@@ -307,12 +305,12 @@ private function setCookieValues( $values, $expiration = 7200 ) {
307305
public function renderSharePage( $debug = false ) {
308306
if ( ! $this->mustache ) {
309307
if ( ! defined( 'SMART_LIB_PATH' ) ) {
310-
define( 'SMART_LIB_PATH', realpath( dirname( __FILE__ ) ) . '/..' );
308+
define( 'SMART_LIB_PATH', realpath(__DIR__) . '/..' );
311309
}
312310
// Initialize mustache
313-
$loader = new \Mustache_Loader_FilesystemLoader( SMART_LIB_PATH . '/views' );
314-
$partials = new \Mustache_Loader_FilesystemLoader( SMART_LIB_PATH . '/views/partials' );
315-
$this->mustache = new \Mustache_Engine( [
311+
$loader = new Mustache_Loader_FilesystemLoader( SMART_LIB_PATH . '/views' );
312+
$partials = new Mustache_Loader_FilesystemLoader( SMART_LIB_PATH . '/views/partials' );
313+
$this->mustache = new Mustache_Engine( [
316314
'loader' => $loader,
317315
'partials_loader' => $partials,
318316
] );
@@ -323,7 +321,7 @@ public function renderSharePage( $debug = false ) {
323321
$meta = $this->getMeta();
324322
if ( isset( $meta['image'] ) ) {
325323
if ( extension_loaded( 'gd' ) && function_exists( 'gd_info' ) && $meta['image'] ) {
326-
list( $width, $height ) = getimagesize( $meta['image'] );
324+
[ $width, $height ] = getimagesize( $meta['image'] );
327325
$this->meta['image_height'] = $height;
328326
$this->meta['image_width'] = $width;
329327
}
@@ -738,13 +736,12 @@ private function shortenLink( $url ) {
738736

739737
// Do something with the result. Here, we echo the long URL
740738
$data = json_decode( $data );
741-
742-
$this->url_short_array[ $url ] = $data->shorturl;
739+
$this->url_short_array[ $url ] = isset($data->shorturl)? $data->shorturl : $url;
743740

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

747-
return $data->shorturl;
744+
return isset($data->shorturl)? $data->shorturl : $url;
748745
}
749746

750747
/**
@@ -838,7 +835,7 @@ private function parse_signed_request( $signed_request ) {
838835
}
839836

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

844841
return $data;

0 commit comments

Comments
 (0)