Skip to content

Commit

Permalink
Fallback requests, Fixes
Browse files Browse the repository at this point in the history
Re-implement the hook to trigger fallback responses if Flex TV doesn't get back to the mothership in time.

Fix issues where sending a null string to compareTitles would throw an error.
Fix a warning in the log if open_basedir warnings are thrown trying to find php logs to read.

Add a param to continue a session for fallbacks...
  • Loading branch information
VaMaster committed May 28, 2018
1 parent c556479 commit 11639d9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
50 changes: 35 additions & 15 deletions api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function analyzeRequest()
{

$json = file_get_contents('php://input');
$sessionId = json_decode($json, true)['originalRequest']['data']['conversation']['conversationId'] ?? false;
$sessionId = json_decode($json, true)['originalRequest']['data']['conversation']['conversationId'] ?? $_GET['sessionId'] ?? false;

if (!session_started()) {
if ($sessionId) session_id($sessionId);
Expand Down Expand Up @@ -77,7 +77,7 @@ function analyzeRequest()
}
initialize();
} else {
write_log("User is not valid!","ERROR");
write_log("APIToken $token is not valid!","ERROR");
if (isset($_GET['testclient'])) {
write_log("API Link Test failed.", "ERROR");
http_response_code(401);
Expand Down Expand Up @@ -177,12 +177,7 @@ function initialize()
echo($valid ? "valid" : "invalid");
bye();
}
if (isset($_GET['msg'])) {
if ($_GET['msg'] === 'FAIL') {
write_log("Received response failure from server, firing fallback command.","INFO");
sendFallback();
}
}


if (isset($_GET['castLogs'])) {
write_log("Trying to grab cast logs.","INFO");
Expand Down Expand Up @@ -246,7 +241,6 @@ function initialize()
write_log("No, really, we have an amazonrequest: ".json_encode($request),"ALERT");
writeSession('lastRequest',$request['result']['resolvedQuery']);
}

if ($request) {
if (isset($request['result']['resolvedQuery']) || isset($request['type'])) {
write_log("Request JSON: " . $json);
Expand Down Expand Up @@ -276,6 +270,12 @@ function initialize()
write_log(json_encode($error->getMessage()), "ERROR");
}
}

if (isset($_GET['fireFallback'])) {
sendFallback();
echo "SUCCESS";
bye();
}
}

function setSessionData($rescan = true)
Expand Down Expand Up @@ -2302,14 +2302,23 @@ function sendCommandCast($cmd, $value = false)
return $return;
}

function sendFallback()
{
function sendFallback() {
write_log("Function fired!!", "WARN");
if (isset($_SESSION['fallback'])) {
$fb = $_SESSION['fallback'];
if (isset($fb['media'])) sendMedia($fb['media']);
writeSession('fallback', null, true);
write_log("Session vars: ".getSessionData());
$fallBackMedia = $_SESSION['fallBackMedia'] ?? false;
$fallBackAction = $_SESSION['fallBackAction'] ?? false;
if ($fallBackAction && is_array($fallBackMedia)) {
write_log("We have an action of $fallBackAction and media: ".json_encode($fallBackMedia));
if ($fallBackAction == 'play') {
write_log("Sending media.");
sendMedia($fallBackMedia);
} else {
downloadMedia($fallBackMedia,scanFetchers($fallBackMedia['type']));
}
} else {
write_log("Missing ". ($fallBackMedia ? " action." : " media."));
}
writeSessionArray(['fallBackMedia'=>false,'fallBackAction'=>false],true);
}

function sendMedia($media)
Expand Down Expand Up @@ -2708,6 +2717,8 @@ function mapApiRequest($request)
#TODO Add a parser here to determine if we can prompt for more info, or if we just play something
$playResult = false;
$result = false;
$fallBackMedia = false;
$fallBackAction = false;
$params = checkDeviceChange($params);
write_log("Intent is $intent","INFO");
switch ($intent) {
Expand Down Expand Up @@ -2831,15 +2842,19 @@ function mapApiRequest($request)
// Ask which one to download
write_log("Multiple search results found, need moar info.", "INFO");
$result['fetch'] = "MULTI";
$fallBackAction = 'fetch';
$fallBackMedia = $dataArray[0];
}
}
}

}
if ($action == 'play' || $action == 'playMedia') {
$fallBackAction = 'play';
$playItem = false;
if (count($result['media']) == 1 || $noPrompts) $playItem = $result['media'][0];
if (count($result['media']) >= 2 && !$noPrompts) {
$fallBackMedia = fetchPlayItem($media[0]);
$_SESSION['mediaArray'] = $result['media'];
$oontext = "playMedia-followup";
$data = [
Expand Down Expand Up @@ -2887,6 +2902,11 @@ function mapApiRequest($request)
'sessionId' => $_SESSION['sessionId'] ?? [],
'intent' => $intent
];

if ($fallBackMedia && $fallBackAction) {
$data['fallBackMedia'] = $fallBackMedia;
$data['fallBackAction'] = $fallBackAction;
}
$clearSet = $result['wait'] ?? true;
$clearSet = $clearSet ? false : true;
writeSessionArray($data, $clearSet);
Expand Down
1 change: 1 addition & 0 deletions log.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_once dirname(__FILE__) . '/php/util.php';
require_once dirname(__FILE__) . '/PHPTail.php';

error_reporting(E_ERROR);
if (!isset($_GET['apiToken']) || isWebApp()) {
die("Unauthorize access detected.");
} else {
Expand Down
3 changes: 2 additions & 1 deletion php/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ function cmp($a, $b) {
return $b['ratingCount'] > $a['ratingCount'] ? 1 : -1;
}

function compareTitles(string $search, string $check, $sendWeight = false, $exact=false) {
function compareTitles($search, $check, $sendWeight = false, $exact=false) {
if (!is_string($search) || !is_string($check)) return false;
$search = cleanCommandString($search);
$check = cleanCommandString($check);
// Check for a 100% match.
Expand Down

0 comments on commit 11639d9

Please sign in to comment.