Skip to content

Commit

Permalink
Update smartlink to improve the Redirect behaviour corresponding to t…
Browse files Browse the repository at this point in the history
…he channels the app is installed on. The whole smartlink redirect behaviour can be controlled by GET parameters
  • Loading branch information
Sebastian Buckpesch committed Feb 2, 2018
1 parent 10541d6 commit 6614c74
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 286 deletions.
17 changes: 9 additions & 8 deletions docs/smartlink.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ Note
You can add all of the listed parameters to the SmartLink Url to modify
the Redirect behaviour.

| Parameter | Description | Example |
|:----------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------|
| device | To simulate a different device type (mobile, tablet or desktop), just add a device-GET Parameter to your URL. The SmartLink will automatically use this device type then and respond with it. Allowed values are `mobile`, `tablet` and `desktop`. | Simulate the mobile view of an app: https://www.my-web-app.com/?i_id=1234&device=mobile |
| website | If your app is being embedded in a website via iframe, you should add a GET-Parameter called website containing the URL the app is embedded in to your smartlink.php Url. Your users will then be redirected the Website Url and not directly to your app. This will keep traffic up on your website. :-) | Redirect the user to a certain website the iframe with your app is embedded in e.g. https://www.app-arena.com/fotowettbewerb.html The SmartLink is: |
| fb_page_id | Submit this parameter to redirect to this Facebook fanpage Tab your app is embedded in. You can use this to have the same app installed on several fanpages and to control the redirects
| ref_app_env | Set this parameter to fb to force the redirection to the facebook fanpage the app is installed on. | https://www.my-app.com/?i_id=1234&ref_app_env=fb |
| lang | The language parameter controls the used language of the app | Show your app in french: https://www.my-web-app.com/?i_id=1234&lang=fr_FR
| debug | Add the debug parameter to disable redirects and show Debug info on the smartlink.php page | Show the Debug-Page for the SmartLink: https://www.my-web-app.com/smartlink.php?debug=1
| Parameter | Description | Example |
|:-----------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------|
| device | To simulate a different device type (mobile, tablet or desktop), just add a device-GET Parameter to your URL. The SmartLink will automatically use this device type then and respond with it. Allowed values are `mobile`, `tablet` and `desktop`. | Simulate the mobile view of an app: https://www.my-web-app.com/?i_id=1234&device=mobile |
| channelId | Add this parameter to prioritize the channel with the submitted ID as redirection target. | |
| website | If your app is being embedded in a website via iframe, you should add a GET-Parameter called website containing the URL the app is embedded in to your smartlink.php Url. Your users will then be redirected the Website Url and not directly to your app. This will keep traffic up on your website. :-) | Redirect the user to a certain website the iframe with your app is embedded in e.g. https://www.app-arena.com/fotowettbewerb.html The SmartLink is: |
| fb_page_id | Submit this parameter to redirect to this Facebook fanpage Tab your app is embedded in. You can use this to have the same app installed on several fanpages and to control the redirects | |
| target | Set this parameter to prioritize `facebook`, `website` or `domain` as Smartlink redirection target. | |
| lang | The language parameter controls the used language of the app | Show your app in french: https://www.my-web-app.com/?i_id=1234&lang=fr_FR |
| debug | Add the debug parameter to disable redirects and show Debug info on the smartlink.php page | Show the Debug-Page for the SmartLink: https://www.my-web-app.com/smartlink.php?debug=1 |

## Embed an App via iframe ($_GET[‘website’])

Expand Down
72 changes: 47 additions & 25 deletions src/AppArena/Models/Entities/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function getInfos() {

if ( isset( $meta['_embedded']['data'] ) && is_array( $meta['_embedded']['data'] ) ) {
$values = array_map( function ( $item ) {
if (isset($item['value'])) {
if ( isset( $item['value'] ) ) {
return $item['value'];
}
}, $meta['_embedded']['data'] );
Expand Down Expand Up @@ -448,44 +448,47 @@ public function getLang() {
if ( ! $this->lang ) {
// Try to recover language from Request
$lang = false;
if ( isset( $_GET['lang'] ) ) {
$this->setLang($_GET['lang']);
return $this->lang;
}
if ( isset( $_GET['lang'] ) ) {
$this->setLang( $_GET['lang'] );

return $this->lang;
}

// Try to get lang from Cookie
if ( isset( $_COOKIE[ 'aa_' . $this->id . '_lang' ] ) ) {
$this->lang = $_COOKIE[ 'aa_' . $this->id . '_lang' ];

return $this->lang;
}

// Get the default language from of the entity
if ($languages = $this->getLanguages()) {
foreach ( $languages['activated'] as $language ) {
if ($language['default']) {
$this->lang = $language['lang'];
return $this->lang;
}
}
}
if ( $languages = $this->getLanguages() ) {
foreach ( $languages['activated'] as $language ) {
if ( $language['default'] ) {
$this->lang = $language['lang'];

return $this->lang;
}
}
}
}

return $this->lang;
}

/**
* @param string $lang
*/
public function setLang( $lang ) {
// Validate language code
$languages = $this->validLanguages;
if ( ! isset( $languages[ $lang ] ) ) {
throw new \InvalidArgumentException( $lang . ' is not a valid language code' );
}
setcookie('aa_' . $this->id . '_lang', $lang, time() + 172600, '/');
/**
* @param string $lang
*/
public function setLang( $lang ) {
// Validate language code
$languages = $this->validLanguages;
if ( ! isset( $languages[ $lang ] ) ) {
throw new \InvalidArgumentException( $lang . ' is not a valid language code' );
}
setcookie( 'aa_' . $this->id . '_lang', $lang, time() + 172600, '/' );

$this->lang = $lang;
}
$this->lang = $lang;
}

/**
* @return mixed
Expand All @@ -502,4 +505,23 @@ public function setName( $name ) {
}


/**
* Returns the base url of the entity
*/
protected function getBaseUrl() {
// Initialize the base_url
$base_url = $this->getInfo( 'base_url' );
if ( isset( $_SERVER['SERVER_NAME'] ) ) {
$base_url = 'http';
if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) {
$base_url .= 's';
}
$base_url .= '://';
$base_url .= $_SERVER['SERVER_NAME'];
if ( substr( $base_url, - 1 ) !== '/' ) {
$base_url .= '/';
}
}
return $base_url;
}
}
132 changes: 120 additions & 12 deletions src/AppArena/Models/Entities/App.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace AppArena\Models\Entities;

/**
Expand All @@ -14,11 +15,11 @@ class App extends AbstractEntity {
/**
* Initialize app related information and try to get the App ID from different environments
*
* @param int $id ID of the entity
* @param int $id ID of the entity
* @param int $versionId Version ID, which has been submitted during App-Manager initialization
*/
public function __construct( $id = null, $versionId ) {
$this->type = 'app';
$this->type = 'app';
$this->versionId = $versionId;

// If no App ID available, then try to recover it
Expand All @@ -33,7 +34,7 @@ public function __construct( $id = null, $versionId ) {
* @return integer
*/
public function getTemplateId() {
return $this->getInfo('templateId');
return $this->getInfo( 'templateId' );
}

/**
Expand Down Expand Up @@ -97,16 +98,16 @@ public function recoverAppId() {

// Set ID to the object and the users session and cookie
if ( $id ) {
$_SESSION['current_appId'] = (int)$id;
$this->id = (int)$id;
$_SESSION['current_appId'] = (int) $id;
$this->id = (int) $id;
}

return $this->id;
}


/**
* Returns a list of all channels the app is published on
* Returns a list of all channels the app is published on in prioritized order (highest channel first)
* @return array|bool
*/
public function getChannels() {
Expand All @@ -115,19 +116,126 @@ public function getChannels() {
return $this->channels;
}

// 1. Initialize the default channel (Base Url with direct access)
$channels = [
[
'channelId' => 0,
'priority' => 100,
'type' => 'domain',
'name' => 'Default domain',
'url' => $this->getBaseUrl()
]
];

// 2. Add a channel which might be added (e.g. added via GET param)
if ( isset( $_GET['fb_page_id'] ) && $this->getInfo( 'fb_app_id' ) ) {
// Add channel with high priority as it is explicitly defined in GET parameter
$channels[] = [
'channelId' => 0,
'priority' => 9999,
'type' => 'facebook',
'name' => 'Facebook Page added via GET parameter fb_page_id',
'url' => 'https://www.facebook.com/' . $_GET['fb_page_id'] . '/app/' . $this->getInfo( 'fb_app_id' ),
];
}

if ( isset( $_GET['website'] ) ) {
// Add channel with high priority as it is explicitly defined in GET parameter
$channels[] = [
'channelId' => 0,
'priority' => 9999,
'type' => 'website',
'name' => 'Website added via GET parameter website',
'url' => $_GET['website'],
];
}
// App infos is a merged array of basic app information and additional app meta data
$channels = $this->api->get( 'apps/' . $this->id . '/channels' );
$installedChannels = $this->api->get( 'apps/' . $this->id . '/channels' );

if ( isset( $installedChannels['_embedded']['data'] ) && is_array( $installedChannels['_embedded']['data'] ) ) {
$installedChannels = $installedChannels['_embedded']['data'];

if ( isset( $channels['_embedded']['data'] ) && is_array( $channels['_embedded']['data'] ) ) {
$this->channels = $channels['_embedded']['data'];
// Prepare data
$installedChannels = array_map(function($channel){
if ($channel['type'] === 'facebook') {
// If a target GET parameter is defined and set to 'facebook', then Facebook channels will get higher prio
$priority = $channel['priority'] ?? 0;
if (isset($_GET['target']) && $_GET['target'] === 'facebook'){
$priority = 8888; // Not as high as directly called channels
}

return [
'channelId' => $channel['channelId'],
'priority' => $priority,
'type' => 'facebook',
'name' => $channel['name'] ?? 'Channel ID ' . $channel['channelId'],
'url' => 'https://www.facebook.com/' . $channel['value'] . '/app/' . $this->getInfo( 'fb_app_id' ),
];
}
if ($channel['type'] === 'website') {
// If a target GET parameter is defined and set to 'facebook', then Facebook channels will get higher prio
$priority = $channel['priority'] ?? 0;
if (isset($_GET['target']) && $_GET['target'] === 'website'){
$priority = 8888; // Not as high as directly called channels
}

return [
'channelId' => $channel['channelId'],
'priority' => $priority,
'type' => 'website',
'name' => $channel['name'] ?? 'Channel ID ' . $channel['channelId'],
'url' => $channel['value'],
];
}
if ($channel['type'] === 'domain') {
// If a target GET parameter is defined and set to 'facebook', then Facebook channels will get higher prio
$priority = $channel['priority'] ?? 0;
if (isset($_GET['target']) && $_GET['target'] === 'domain'){
$priority = 8888; // Not as high as directly called channels
}

return [
'channelId' => $channel['channelId'],
'priority' => $priority,
'type' => 'domain',
'name' => $channel['name'] ?? 'Channel ID ' . $channel['channelId'],
'url' => $channel['value'],
];
}


}, $installedChannels);

// Merge default channels and channels the customer has installed the app on
$channels = array_merge_recursive( $channels, $installedChannels );
$this->channels = $channels;
} else {
return false;
$this->channels = $defaultChannels;
}

if ( ! $this->channels ) {
return false;
}

// Order all channel by priority
$priority = [];
foreach ( $channels as $key => $row ) {
// If the current channel is exoplicitly prioritized via GET param, then give it a high priority
$channelId = $row['channelId'] ?? 0;
if ( isset( $_GET['channelId'] ) && $_GET['channelId'] == $channelId ) {
$row['priority'] = 9999;
$channels[$key]['priority'] = 9999;
}

if ( ! isset( $row['priority'] ) ) {
$row['priority'] = 0;
}

$priority[ $key ] = $row['priority'];
}
array_multisort( $priority, SORT_DESC, $channels );
$this->channels = $channels;

return $this->channels;
}

Expand Down Expand Up @@ -155,11 +263,11 @@ private function getIdFromFBRequest() {
$request_url = "https://manager.app-arena.com/api/v1/env/fb/pages/" . $fb_page_id .
"/instances.json?projectId=" . $this->versionId . "&active=true";
// If the facebook App ID is submitted, then it will be added to the request
if (isset($_GET['fb_app_id']) && strlen($_GET['fb_app_id']) > 10) {
if ( isset( $_GET['fb_app_id'] ) && strlen( $_GET['fb_app_id'] ) > 10 ) {
$request_url .= '&fb_app_id=' . $_GET['fb_app_id'];
}

$instances = json_decode( file_get_contents( $request_url ), true );
$instances = json_decode( file_get_contents( $request_url ), true );
foreach ( $instances['data'] as $instance ) {
if ( $instance['activate'] == 1 ) {
$appId = $instance['i_id'];
Expand Down
4 changes: 2 additions & 2 deletions src/AppArena/Models/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct( AbstractEntity $entity ) {
*
* @return AbstractEnvironment
*/
public function getPrimaryEnvironment() {
/*public function getPrimaryEnvironment() {
if ($this->website->getUrl()) {
return $this->website;
Expand All @@ -64,7 +64,7 @@ public function getPrimaryEnvironment() {
}
return $this->domain;
}
}*/


/**
Expand Down
Loading

0 comments on commit 6614c74

Please sign in to comment.