public function __construct($directoryLayer)
 {
     if (session_status() === PHP_SESSION_NONE) {
         session_start();
     }
     $this->pathToRoot = GenerateRootPath::getRoot($directoryLayer);
     $userkey = intval($_SESSION["userkey"]);
     $db = UniversalConnect::doConnect();
     $query = "SELECT name, networth FROM users WHERE userkey={$userkey} LIMIT 1";
     $result = $db->query($query) or die;
     if ($row = $result->fetch_assoc()) {
         $this->name = $row["name"];
         $this->marketValue = $row["networth"];
     }
     $query = "SELECT amount FROM wallet WHERE userkey={$userkey} AND currencyid=1 LIMIT 1";
     $result = $db->query($query) or die;
     if ($row = $result->fetch_assoc()) {
         $this->netPosition = $row["amount"] - 10000000;
     }
     $query = "SELECT transid FROM transactions WHERE userkey={$userkey} AND (transtype=0 OR transtype=1)";
     $result = $db->query($query) or die;
     $this->transactionCount = $result->num_rows;
     $this->basecurr = new BaseCurrency();
     $this->return = "";
     $db->close();
 }
 public function __construct($HTMLtitle, $directoryLayer, $appendScript = "")
 {
     $this->title = $HTMLtitle;
     $this->extraScript = $appendScript;
     $this->pathToRoot = GenerateRootPath::getRoot($directoryLayer);
     $this->return = "";
 }
 public function __construct($directoryLayer, $pageID)
 {
     $this->pathToRoot = GenerateRootPath::getRoot($directoryLayer);
     $this->return = "";
     $this->pageID = $pageID;
     /* $pageID reference table:
      * 0 = home
      * 10 = history
      * 20 = leaderboards
      * 30 = admin console
      * 40 = change password
      */
 }
    public function __construct()
    {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
        $SessAuthWorker = new SessionAuthenticate();
        if (!$SessAuthWorker->authenticate()) {
            header("Location: " . GenerateRootPath::getRoot(3));
            exit;
        }
        DatabasePurger::purge();
        $headerFactory = new HeaderFactory();
        echo $headerFactory->startFactory(new HeaderProduct("Leaderboards - Forex Trading Simulator", 3));
        ?>
            <body class="blue lighten-5">

                <?php 
        $navbarFactory = new NavbarFactory();
        echo $navbarFactory->startFactory(new NavbarProduct(3, 20));
        ?>
                <div class="container">
                    <div class="row">
                        <div class="col s4">
                            <?php 
        $profileCardFactory = new ProfileCardFactory();
        echo $profileCardFactory->startFactory(new ProfileCardProduct(3));
        ?>
                        </div>
                        <div class="col s8">
                            <div class="row">
                                <div class="col s12">
                                    <?php 
        $leaderboardWorker = new LeaderboardFactory();
        echo $leaderboardWorker->startFactory(new LeaderboardProduct());
        ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php 
    }
    public function __construct()
    {
        $this->baseCurrency = new BaseCurrency();
        DatabasePurger::purge();
        $SessAuthWorker = new SessionAuthenticate();
        if (!$SessAuthWorker->authenticate()) {
            header("Location: " . GenerateRootPath::getRoot(3));
            exit;
        }
        $headerFactory = new HeaderFactory();
        echo $headerFactory->startFactory(new HeaderProduct("History - Forex Trading Simulator", 3));
        ?>
            <body class="blue lighten-5">
                <script>
                    function changeHeight()
                    {
                        setTimeout(function ()
                        {
                            if($("#news ul").height() >= $("#news").height())
                            {
                                $("#news").addClass("active")
                            }
                            else
                            {
                                $("#news").removeClass("active");
                            }
                        }, 100)
                    }
                    window.onload = function ()
                    {
                        $(document).ready(function ()
                        {
                            // News card
                            Materialize.showStaggeredList('#news ul.collapsible');
                            changeHeight();
                            $(".collapsible-header").click(function ()
                            {
                                changeHeight()
                            });

                            // Mobile Sidenav
                            $('.button-collapse').sideNav({
                                menuWidth: 240, // Default is 240
                                edge: 'right', // Choose the horizontal origin
                                closeOnClick: true // Closes side-nav on <a> clicks, useful for Angular/Meteor
                            });
                        })
                    }
                </script>
                <?php 
        $navbarFactory = new NavbarFactory();
        echo $navbarFactory->startFactory(new NavbarProduct(3, 10));
        ?>
                <div class="container">
                    <div class="row">
                        <div class="col s12 m5 l4">
                            <?php 
        $profileCardFactory = new ProfileCardFactory();
        echo $profileCardFactory->startFactory(new ProfileCardProduct(3));
        $newsFactory = new NewsBoardFactory();
        echo $newsFactory->startFactory(new NewsBoardProduct(0));
        ?>
                        </div>
                        <div class="col s12 m7 l8">
                            <div class="card center">
                                <div class="card-content">
                                    <div class="card-title">
                                        <p><?php 
        echo $this->baseCurrency->getShortName();
        ?>
-JPY Historical Bid Rates</p>
                                    </div>
                                    <?php 
        //USD-JPY above is sloppy coding to be improved on when we need multiple currencies
        $currencyChartFactory = new CurrencyChartFactory();
        echo $currencyChartFactory->startFactory(new CurrencyChartProduct(2, 50));
        ?>
                                </div>
                            </div>
                            <ul class="collapsible" data-collapsible="expandable">
                                <li>
                                    <div class="collapsible-header"><i class="material-icons">trending_up</i>Full Historical Market Rates</div>
                                    <div class="collapsible-body">
                                        <?php 
        $currencyHistoryBoardFactory = new CurrencyHistoryBoardFactory();
        echo $currencyHistoryBoardFactory->startFactory(new CurrencyHistoryBoardProduct());
        ?>
                                    </div>
                                </li>
                                <li>
                                    <div class="collapsible-header"><i class="material-icons">shopping_basket</i>Past Transactions</div>
                                    <div class="collapsible-body">
                                        <?php 
        $transactionHistoryBoardFactory = new TransactionHistoryBoardFactory();
        echo $transactionHistoryBoardFactory->startFactory(new TransactionHistoryBoardProduct());
        ?>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </body>
            <?php 
    }
    public function __construct()
    {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
        $SessAuthWorker = new SessionAuthenticate();
        if (!$SessAuthWorker->authenticate()) {
            header("Location: " . GenerateRootPath::getRoot(3));
            exit;
        }
        if (isset($_POST["currpass"]) && isset($_POST["newpass"]) && isset($_POST["conpass"])) {
            if ($_POST["newpass"] === $_POST["conpass"]) {
                $db = UniversalConnect::doConnect();
                $query = "SELECT userid FROM users WHERE userkey=" . $_SESSION["userkey"] . " LIMIT 1";
                $result = $db->query($query);
                $row = $result->fetch_assoc();
                $userid = $row["userid"];
                if (PasswordAuthenticate::authenticate($userid, $_POST["currpass"])) {
                    $query = "UPDATE users SET password=\"" . password_hash($db->real_escape_string(trim($_POST["newpass"])), PASSWORD_DEFAULT) . "\" WHERE userkey=" . $_SESSION["userkey"];
                    $db->query($query);
                }
            }
        }
        DatabasePurger::purge();
        $javascript = <<<JAVASCRIPT
<script>
            var passwordsMatch = false;
            function checkPass()
            {
                if(document.getElementById("newpass").value == "" || document.getElementById("conpass").value == "" || document.getElementById("currpass").value == "" || document.getElementById("newpass").value == null || document.getElementById("conpass").value == null || document.getElementById("currpass").value == null)
                {
                    document.getElementById("checkpassresult").innerHTML = "<p style=\\"color:red\\">Please fill in all password fields.</p>";
                    passwordsMatch = false;
                }
                else if(document.getElementById("newpass").value == document.getElementById("conpass").value)
                {
                    document.getElementById("checkpassresult").innerHTML = "<p style=\\"color:green\\">Passwords match!</p>";
                    passwordsMatch = true;
                    console.log("in");
                }
                else
                {
                    document.getElementById("checkpassresult").innerHTML = "<p style=\\"color:red\\">Passwords do not match</p>";
                    passwordsMatch = false;
                    console.log("out");
                }
            }
            function submitValidation()
            {
                checkPass();
                return passwordsMatch;
            }
</script>
JAVASCRIPT;
        $headerFactory = new HeaderFactory();
        echo $headerFactory->startFactory(new HeaderProduct("Change Password - Forex Trading Simulator", 3, $javascript));
        echo "<body class=\"blue lighten-5\">";
        $navbarFactory = new NavbarFactory();
        echo $navbarFactory->startFactory(new NavbarProduct(3, 40));
        ?>
            <div class="container">
                <div class="card">
                    <div class="row">
                        <div class="card-title col s12 center">
                            Change Password
                        </div>
                    </div>
                    <div class="row">
                        <form id="passChangeForm" name="passChangeForm" method="post" action="./" onsubmit="return submitValidation();">
                            <div class="row">
                                <div class="input-field col s8 push-s2">
                                    <input type="password" name="currpass" id="currpass" />
                                    <label for="currpass">Current Password</label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="input-field col s8 push-s2">
                                    <input type="password" name="newpass" id="newpass" onkeyup="checkpass()" onchange="checkpass()" />
                                    <label for="newpass" >New Password</label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="input-field col s8 push-s2">
                                    <input type="password" name="conpass" id="conpass" onkeyup="checkpass()" onchange="checkpass()" />
                                    <label for="conpass">Confirm Password</label>
                                </div>
                            </div>
                            <div id="checkpassresult"></div>
                            <div class="row">
                                <div class="center">
                                    <button class="btn waves-effect waves-light blue accent-4" type="submit" name="action">Change Password
                                        <i class="material-icons right">send</i>
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <?php 
    }
    public function __construct()
    {
        //Checks if user is logged in or has posted passwords. Redirects as appropriate.
        $SessAuthWorker = new SessionAuthenticate();
        if ($SessAuthWorker->authenticate()) {
            header("Location: " . GenerateRootPath::getRoot(1) . "/dashboard/");
            exit;
        }
        if (isset($_POST["username"]) && isset($_POST["password"])) {
            $PassAuthWorker = new PasswordAuthenticate();
            if ($PassAuthWorker->authenticate($_POST["username"], $_POST["password"])) {
                $TimeAuthWorker = new TimeAuthenticate();
                $PrivAuthWorker = new PrivilegeAuthenticate();
                if (session_status() === PHP_SESSION_NONE) {
                    session_start();
                }
                $db = UniversalConnect::doConnect();
                $query = "SELECT userkey, usertype FROM users WHERE userid=\"" . $db->real_escape_string(trim($_POST["username"])) . "\" LIMIT 1";
                $result = $db->query($query);
                if ($result->num_rows < 1) {
                    die("An unexpected error has occurred. The problem should go away by itself after some time.");
                }
                $row = $result->fetch_assoc();
                $_SESSION["userkey"] = $row["userkey"];
                $_SESSION["usertype"] = $row["usertype"];
                if (!$PrivAuthWorker->authenticate($_SESSION["usertype"]) && !$TimeAuthWorker->authenticate()) {
                    $this->authenticationStatus = 2;
                } else {
                    header("Location: " . GenerateRootPath::getRoot(1) . "/dashboard/");
                    exit;
                }
            } else {
                $this->authenticationStatus = 0;
            }
        }
        //generates header from <!DOCTYPE html> all the way to </head>
        //Title of the page is set in constructor i.e. new HeaderProduct("Title of page here");
        $headerFactory = new HeaderFactory();
        echo $headerFactory->startFactory(new HeaderProduct("Login - Forex Trading Simulator ", 1));
        echo <<<HTML
    <body class="blue lighten-5">
        <div class="container">
            <div id="login-card" class="pageCenter card
HTML;
        if ($this->authenticationStatus === 0) {
            echo " failed";
        }
        echo <<<HTML
">
                <div class="center">
                    <h3 class="title">Forex Trading Simulator</h3>
                    <h5 class="title top-margin">Exchange rates, made easier</h5>
                </div>
                <form id="loginform" name="loginform" method="post">
                    <div class="row">
                        <div class="input-field col s12 m10 l10 push-m1 push-l1">
                            <i class="material-icons prefix">account_circle</i>
HTML;
        echo "<input type=\"text\" required=\"\" name=\"username\" id=\"username\"";
        if ($this->authenticationStatus === 2 || $this->authenticationStatus === 0) {
            echo " value=\"" . htmlentities($_POST["username"], ENT_QUOTES, "UTF-8") . "\"";
        }
        echo "/>";
        echo <<<HTML
                            <label for="username">Username</label>
                        </div>
                    </div>
                    <div class="row">
                        <div class="input-field col s12 m10 l10 push-m1 push-l1">
                            <i class="material-icons prefix">vpn_key</i>
                            <input type="password" name="password" id="password" />
                            <label for="password">Password</label>
                        </div>
                    </div>
                    <div class="row input-field center" id="Submit">
                        <button class="btn waves-effect waves-light blue accent-4" type="submit" name="action">Login
                        </button>
                    </div>
                </form>
HTML;
        if ($this->authenticationStatus === 2) {
            $db = new UniversalConnect();
            $result = $db->query("SELECT starttime FROM startendtime LIMIT 1");
            $row = $result->fetch_assoc();
            $startTime = $row["starttime"];
            echo "<script>alert('The game has not started yet. It starts in " . FormatTimePassed::format($startTime) . ".');window.onload = function(){document.getElementById(\"password\").focus();};</script>";
            $db->close();
        }
        echo <<<HTML
            </div>
        </div>
    </body>
</html>
HTML;
    }
    public function __construct()
    {
        $SessAuthWorker = new SessionAuthenticate();
        DatabasePurger::purge();
        if (!$SessAuthWorker->authenticate()) {
            header("Location: " . GenerateRootPath::getRoot(2));
            exit;
        }
        if (isset($_POST["currid"]) && !GameEndedChecker::GameEnded()) {
            $currid = intval($_POST["currid"]);
            if ($currid > 1) {
                if (isset($_POST["sellamt" . $currid]) && isset($_POST["sellBase" . $currid])) {
                    $exceptionThrown = false;
                    try {
                        $this->secCurr = new Currency($currid);
                    } catch (Exception $e) {
                        $exceptionThrown = true;
                    }
                    if (!$exceptionThrown) {
                        $sellamt = round(floatval($_POST["sellamt" . $currid] * 1000000), 2);
                        $this->secCurr->sell($sellamt);
                    }
                } else {
                    if (isset($_POST["buyamt" . $currid]) && isset($_POST["buyBase" . $currid])) {
                        $exceptionThrown = false;
                        try {
                            $this->secCurr = new Currency($currid);
                        } catch (Exception $e) {
                            $exceptionThrown = true;
                        }
                        if (!$exceptionThrown) {
                            $buyamt = round(floatval($_POST["buyamt" . $currid] * 1000000), 2);
                            $this->secCurr->buy($buyamt);
                        }
                    }
                }
            }
        }
        $this->baseCurrency = new BaseCurrency();
        $headerFactory = new HeaderFactory();
        echo $headerFactory->startFactory(new HeaderProduct("Dashboard - Forex Trading Simulator", 2));
        ?>

            <body class="blue lighten-5">
                <script>
                    function changeHeight()
                    {
                        setTimeout(function ()
                        {
                            if($("#news ul").height() >= $("#news").height())
                            {
                                $("#news").addClass("active")
                            }
                            else
                            {
                                $("#news").removeClass("active");
                            }
                        }, 100)
                    }
                    window.onload = function ()
                    {
                        $(document).ready(function ()
                        {
                            // News card
                            Materialize.showStaggeredList('#news ul.collapsible');
                            changeHeight();
                            $(".collapsible-header").click(function ()
                            {
                                changeHeight()
                            });

                            // Mobile Sidenav
                            $('.button-collapse').sideNav({
                                menuWidth: 240, // Default is 240
                                edge: 'right', // Choose the horizontal origin
                                closeOnClick: true // Closes side-nav on <a> clicks, useful for Angular/Meteor
                            });
                        })
                    }
                </script>
                <?php 
        $navbarFactory = new NavbarFactory();
        echo $navbarFactory->startFactory(new NavbarProduct(2, 0));
        ?>
                <div class="container">
                    <div class="row">
                        <div class="col s12 m5 l4">
                            <?php 
        $profileCardFactory = new ProfileCardFactory();
        echo $profileCardFactory->startFactory(new ProfileCardProduct(2));
        $newsFactory = new NewsBoardFactory();
        echo $newsFactory->startFactory(new NewsBoardProduct());
        ?>
                        </div>
                        <div class="col s12 m7 l8">
                            <div class="card center">
                                <div class="card-content">
                                    <div class="card-title">
                                        <p><?php 
        echo $this->baseCurrency->getShortName();
        ?>
-JPY Bid Rates</p>
                                    </div>
                                    <?php 
        //USD-JPY above is sloppy coding to be improved on when we need multiple currencies
        $currencyChartFactory = new CurrencyChartFactory();
        echo $currencyChartFactory->startFactory(new CurrencyChartProduct(2));
        ?>
                                </div>
                            </div>
                            <?php 
        $currencyBoardFactory = new CurrencyBoardFactory();
        echo $currencyBoardFactory->startFactory(new CurrencyBoardProduct());
        ?>
                        </div>
                    </div>
                </div>
            </body>
            <?php 
    }