private function authenticate()
 {
     //Try gaining access to the Facebook PHP SDK
     try {
         $accessToken = SDK::instance()->helper->getAccessToken();
     } catch (Facebook\Exceptions\FacebookResponseException $e) {
         throw new Exception("Graph returned an error: " . $e->getMessage());
     } catch (Facebok\Exceptions\FacebookSDKException $e) {
         throw new Exception("Facebook SDK returned an error: " . $e->getMessage());
     }
     //Assuming it went well, let's process our login state
     if (!is_null($this->getToken()) || isset($accessToken)) {
         //This if statements means that it doesn't matter if the session token is set or not,
         //as long as we have the access token either by request or by session, we can use the session
         if (is_null($this->getToken())) {
             $this->setToken((string) $accessToken);
             header("Location: " . Engine::getRemoteAbsolutePath((new Analyse())->getURL()));
         }
         //Get basic user profile information such as user id, name and email to test whether the session works
         try {
             $this->importFromJson($this->getBasicUserProfile()->getGraphUser());
         } catch (Facebook\Exceptions\FacebookResponseException $e) {
             if (strpos($e->getMessage(), "The user has not authorized application") > -1) {
                 Engine::clearSession();
                 header("Location: " . Engine::getRemoteAbsolutePath((new Home())->getURL()));
             } else {
                 throw $e;
             }
             exit;
         }
         return true;
     } else {
         return false;
     }
 }
 public function isMatch($URL)
 {
     //Check if we're forcing a new URL
     $forcingNewAnalysis = $URL == $this->getNewAnalysisURL();
     //Are they matching the URL?
     $isMatchingURL = $URL == $this->getURL() || $forcingNewAnalysis;
     //Are they logged in?
     $isLoggedIn = $this->user->isLoggedIn();
     if ($isMatchingURL && $isLoggedIn) {
         //Only start considering if it's a match here as we need to query the database after this.
         $dbh = Engine::getDatabase();
         //If this user exists in the database, they have used our application
         //before and an analysis would have been created on authentication
         $this->userExists = $dbh->query("SELECT * FROM Users WHERE User_ID=" . $this->user->id)->fetch(PDO::FETCH_ASSOC) != null;
         if (!$forcingNewAnalysis && $this->userExists) {
             ob_clean();
             header("Location: " . Engine::getRemoteAbsolutePath((new Account())->getURL()));
             exit;
         }
         if (!$this->userExists) {
             $dbh->exec("INSERT INTO Users (User_ID, Name, Email) VALUES ('" . User::instance()->id . "', '" . User::instance()->name . "', '" . User::instance()->email . "')");
         }
         //Otherwise, we are a new user and we don't need to force a new analysis
         return true;
     } else {
         if ($isMatchingURL && !$isLoggedIn) {
             //Go back home as we're not authenticated.
             require 'login.php';
         } else {
             //Wasn't a match at all.
             return false;
         }
     }
 }
 public function run($template)
 {
     $authenticatePageURL = Engine::getRemoteAbsolutePath((new Analyse())->getURL());
     $FacebookAuthenticationURL = User::instance()->getFacebookAuthURL($authenticatePageURL);
     $title = "<b>Login</b> with <b>Facebook</b>";
     if (User::instance()->isLoggedIn()) {
         $FacebookAuthenticationURL = $authenticatePageURL;
         $title = "<b>Continue</b> as " . User::instance()->name . "<b></b>";
     }
     $this->loginButton = "<a class=\"fblogin\" style='margin: 0.5em auto;' href=\"{$FacebookAuthenticationURL}\"><i class=\"fa fa-facebook\"></i><span>{$title}</span></a>";
 }
 public function test_getRemoteAbsolutePath()
 {
     //These are some unit test variables that we need to use
     //We need to know if the function is able to give us the
     //right URL.
     $_SERVER['SERVER_PORT'] = 80;
     $_SERVER['HTTP_HOST'] = "localhost";
     $_SERVER['REMOTE_ADDRESS'] = "/";
     $_SERVER["PHP_SELF"] = "/index.php";
     $_SERVER['DOCUMENT_ROOT'] = "C:\nmpp\\htdocs";
     $this->assertEquals("http://localhost/", Engine::getRemoteAbsolutePath("/"));
 }
 public function run($template)
 {
     if (isset($_POST['action']) && $_POST['action'] == 'delete') {
         if (self::delete($this->URLMatch[1])) {
             header('Location: ' . Engine::getRemoteAbsolutePath((new Account())->getURL()));
         }
     } else {
         if (isset($_POST['action']) && ($_POST['action'] == 'make-public' || $_POST['action'] == 'make-private')) {
             $test = self::toggleVisibility($this->URLMatch[1]);
             var_dump($test);
             if ($test) {
                 header('Location: ' . Engine::getRemoteAbsolutePath((new Account())->getURL()));
             }
         }
     }
 }
<?php

if (!User::instance()->isLoggedIn()) {
    //Redirect back to the login page
    ob_clean();
    header("Location: " . Engine::getRemoteAbsolutePath((new Home())->getURL()));
    exit;
}
<main class="centerstage">
	<header title>
		<span style='display:inline-block;margin:5% 0;'>
			<h1 style='margin:0;font-weight:400;letter-spacing:-5px'>
				<img id='logo' <?php 
echo "src='" . Engine::getRemoteAbsolutePath($template->getLocalDir() . '/public/images/white-logo-transparent.png') . "'";
?>
 height="128"/>
				FacebookAnalyser
			</h1>
			<div align='right'>
			</div>
		</span>
		<div class='loader' style='margin-top:0;'></div>	
		<div class='loaderDescription'>
			<noscript>Please wait...</noscript>
		</div>
	</header>
	<script id='remove'>
		var urlRedirect = "";
		var factor = 1;
		var speedUpSpeed = 50;
		var updateSpeed = 250;
		var animationSpeed = 500 + speedUpSpeed;
		function scroller(){
			factor = $('.loaderDescription p').length * speedUpSpeed;
			if($('.loaderDescription p').length > 1){
				$('.loaderDescription p:first').css({opacity: 0, transition: 'opacity 0.25s'});
				$('.loaderDescription p:first').slideUp(animationSpeed - factor, function(){
					$('.loaderDescription p:first').remove();
					setTimeout(scroller, updateSpeed - factor);
require __DIR__ . '/top_account_header.php';
?>
<main class="settings">
	<div class="container">
		<div class='result information'>
			<div class='text'>
				<form method='POST' id='delete-account'>
					<br/>
					<h3 align='center'>Attention!</h3>
					<p>Deletion of your user account will remove all data present from our website including personal information (e.g. names, emails) and also your analyses you have created. Any analyses that have been shared will be invalidated. <b>This process is unrecoverable and data <i>will</i> be lost</b></p>
					<br/>
					<div id='controls'>
						<p>Are you sure you want to proceed?</p>
						<br/>
						<table style='width: 100%;'>
							<tr>
								<td align='left'><a <?php 
echo "href='" . Engine::getRemoteAbsolutePath((new Account())->getURL()) . "'";
?>
>No, take me back</a></td>
								<td align='right'><input type='submit' name='confirm' value='Yes, delete my account'></td>
							</tr>
						</table>
						<input type='hidden' name='action' value='confirm'/>
					</div>
				</form>
			</div>
		</div>
	</div>
</main>
 public function run($template)
 {
     $homeURL = Engine::getRemoteAbsolutePath((new Home())->getURL());
     $this->homeButton = "<a href=\"{$homeURL}\">Take me home, please.</a>";
 }
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<link rel="icon" type="image/icon" <?php 
echo "href='" . Engine::getRemoteAbsolutePath($template->getLocalDir() . "/public/favicon.ico") . "'";
?>
>
		<?php 
if (get_class($this) == "Result") {
    ?>
			<meta property="og:url" <?php 
    echo "content=\"" . $this->Data['share-url'] . "\"";
    ?>
 />
			<meta property="og:type" content="article" />
			<meta property="og:title" <?php 
    echo "content=\"" . $this->Data['share-title'] . "\"";
    ?>
 />
			<meta property="og:description" <?php 
    echo "content=\"" . $this->Data['share-description'] . "\"";
    ?>
 />
			<meta property="og:image" <?php 
    echo "content=\"" . $this->Data['share-image-url'] . "\"";
    ?>
 />
			<meta property="og:image:width" content="475"/>
			<meta property="og:image:height" content="256"/>
			<meta property="fb:app_id" <?php 
    public function getAllResultHistory($dbh)
    {
        //Insert a new analysis button
        ?>
		<div class='result'>
			<a <?php 
        echo "href='" . Engine::getRemoteAbsolutePath((new Analyse())->getNewAnalysisURL()) . "'";
        ?>
 title='Make a new analysis'>
				<div>
					<div>
						<img <?php 
        echo "src='" . Engine::getRemoteAbsolutePath($this->template->getLocalDir() . "/public/images/add1.png") . "'";
        ?>
>
					</div>
				</div>
				<span>Create Analysis</span>
			</a>
		</div>
		<?php 
        //Get all the results and order them by the date (Most recent come first)
        $query = $dbh->query("SELECT Result_ID, UNIX_TIMESTAMP( DATE ) \"Date\", Data, Visible FROM Results WHERE Result_ID IN (SELECT Result_ID FROM Result_History WHERE User_ID='" . User::instance()->id . "') ORDER BY Date DESC");
        //For all of the analyses, we give a link and the time created.
        foreach ($query->fetchALL(PDO::FETCH_CLASS, 'Result') as $obj) {
            $timeSinceCreation = $obj->getTimeDifferenceApproximate(time() - $obj->Date);
            $timeSinceCreation = stripos($timeSinceCreation, "second") > -1 || !$timeSinceCreation ? " just now" : $timeSinceCreation . " ago";
            $resultLink = Engine::getRemoteAbsolutePath($obj->getURL());
            $resultActionDelete = null;
            $resultActionShare = null;
            ?>
			<script>
				function updateActions(sender){
					//if(this === window) return;
					//alert($(sender).html());
					var url = $(sender).parentsUntil('div.result span.actions').attr('action');
					$.ajax({
						type: 'POST',
						url: url,
						data: {'action':$(sender).attr('data-action')},
						complete: function(){url = this.url;},
						success: function(data){
							if($('.results', $(data)) == null){
								alert(window.location.href + " \n" + url)
								alert("We were unable to fulfil your request.");
								return;
							}
							updateResults();
						}
					});
				}
				function updateResults(){
					$.ajax({
						type: 'GET',
						url: window.location.href,
						success: function(data){
							if($('.results', $(data)) == null){return};
							if($('.results', $(data)) !== $('.results')){
								$('.result').remove();
								$('.results').append($('.result', $(data)));
							}
						}
					});
				}
				setInterval(updateResults, 30000);
			</script>
			<div class="result" <?php 
            echo "id='" . $obj->Result_ID . "'";
            ?>
>
				<span class="actions">
					<form method='post' <?php 
            echo "action='" . $resultLink . "'";
            ?>
>
						<a href='javascript:void(0);' <?php 
            echo "data-action='make-" . ($obj->Visible ? "private" : "public") . "'";
            ?>
 <?php 
            echo "data-action-done='We have now made the result " . ($obj->Visible ? "private" : "public") . ".'";
            ?>
 onclick='updateActions(this);' title='Change the result visibility'>
							<i <?php 
            echo "class=\"fa fa-" . ($obj->Visible ? "lock" : "unlock-alt") . "\"";
            ?>
 aria-hidden="true"></i>
						</a>
						<a href='javascript:void(0);' data-action='delete' data-action-done='We have deleted this result.' onclick='updateActions(this);' title='Delete this result'>
							<i class="fa fa-times" aria-hidden="true"></i>
						</a>
					</form>
				</span>
				<a <?php 
            echo "href='" . $resultLink . "'";
            ?>
 title='View this result'>
					<div>
						<div>
							<img <?php 
            echo "src='" . Engine::getRemoteAbsolutePath($this->template->getLocalDir() . "/public/images/graph.png") . "'";
            ?>
>
						</div>
						<span>Created<br><?php 
            echo $timeSinceCreation;
            ?>
</span>
					</div>
				</a>
			</div>
			<?php 
        }
    }
<?php

require __DIR__ . '/top_account_header.php';
?>
<main class="settings">
	<div class="container">
		<div class='result information'>
			<?php 
if ($this->isCorrupt()) {
    $link = Engine::getRemoteAbsolutePath($this->isPublicUser ? (new Home())->getURL() : (new Account())->getURL());
    ?>
					<h2 align='center'>Analysis incomplete</h2><div align='center' style='padding: 10px'>
					<div class='text'>
						<p>We weren't able to display this result as this analysis was either; unable to catch enough data to provide a result or the result conforms to an old format that our website is unable to read.</p>
						<?php 
    echo "<a href='{$link}'>Take me back " . ($this->isPublicViewer() ? "home" : "to my account") . ", please</a>";
    ?>
						<!--<div style='background: rgb(240,240,240);border-radius: 5px;display:inline-block; padding: 1vh 1vw; margin:auto'>This data doesn't meet the requirements of our analyis.</div>-->
					</div>
			<?php 
} else {
    $timeSinceCreation = $this->getTimeDifferenceApproximate(time() - $this->Date);
    $timeSinceCreation = stripos($timeSinceCreation, "second") > -1 || !$timeSinceCreation ? " just now" : $timeSinceCreation . " ago";
    echo ($this->Visible && !$this->isPublicViewer() ? $this->getShareButton() : "") . "<h2 align='center'>" . $this->getPronoun() . " Result</h2>\n\t\t\t\t\t<h6 align='center'>Created " . $timeSinceCreation . "</h6>\n\t\t\t\t\t<div class='text'>" . $this->getTopThreeFriends() . $this->getTopThreeCatergory() . $this->getHoroscope() . " \n\t\t\t\t\t\t<div>\n\t\t\t\t\t\t\t<h3 align='center'> Facts </h3>\n\t\t\t\t\t\t\t<br/>\n\t\t\t\t\t\t\t<div>" . $this->getAveragePost() . $this->getInteractionTable() . $this->getHoroscopeFact() . "\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t</div>\n\t\t\t\t\t</div>\n\t\t\t\t";
}
?>
		</div>
	</div>
</main>
<nav class='topbar'>
	<div class='container'>
		<ul>
			<li>
				<?php 
echo "<a href='" . Engine::getRemoteAbsolutePath((new Home())->getURL()) . "'><img id='logo' src='" . Engine::getRemoteAbsolutePath($template->getLocalDir() . '/public/images/white-logo-transparent.png') . "' height='32' style='padding: 5px;'/><b>FacebookAnalyser</b></a>";
?>
			</li>
		</ul>
		<?php 
if (User::instance()->isLoggedIn()) {
    ?>
			<ul align='right'>
				<li>
					<?php 
    echo "<a class='fblogin' style='float:right' href='" . Engine::getRemoteAbsolutePath((new Account())->getURL()) . "'>Account</a>";
    ?>
				</li>
				<li>
					<?php 
    echo "<a class='fblogin' style='float:right' href='" . User::instance()->getFacebookDeAuthURL(Engine::getRemoteAbsolutePath((new Home())->getURL())) . "'>Logout</a>";
    ?>
				</li>
			</ul>
		<?php 
}
?>
	</div>
</nav>
 public function run()
 {
     //Tell the user we're going to use the API to get the user's data
     $this->echoImplicit("Preparing...");
     //Start working
     $this->startAsync();
     $resultID = $this->data[self::OUT]['analysis-id'];
     $resultURL = Engine::getRemoteAbsolutePath((new Result())->getURL() . $resultID);
     //add the result to the database
     Result::create($resultID, $this->data[self::OUT]);
     //Tell the user that we're finished
     $this->echoImplicit("All finished!");
     //Stop processing the page and redirect the user
     $this->echoImplicitFinishAndRedirect($resultURL);
 }