diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..485dee64bcfb48793379b200a1afd14e85a8aaf4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000000000000000000000000000000000000..f97815c26fd0f4799935b9159c06cba58e5cb0d6 --- /dev/null +++ b/.htaccess @@ -0,0 +1,4 @@ +Options -MultiViews +RewriteEngine On +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^ public/index.php [QSA,L] \ No newline at end of file diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000000000000000000000000000000000000..24fcb561951799b6f84459acf5e4fac06abc5066 --- /dev/null +++ b/public/index.php @@ -0,0 +1,14 @@ +<?php + + $env = getenv('DAGOJEK_ENV'); + if (!$env || $env == "development") { + $env = "development"; + error_reporting(E_ALL); + ini_set('display_errors', 1); + } + + require __DIR__.'/../src/app.php'; + + $App = DagoJek::Instance(); + $App->prepareRouting(); + $App->Start(); \ No newline at end of file diff --git a/public/scripts.js b/public/scripts.js new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/public/style.css b/public/style.css new file mode 100644 index 0000000000000000000000000000000000000000..828a05da95303169f9da97ccfc9d2ca25274a37a --- /dev/null +++ b/public/style.css @@ -0,0 +1,51 @@ +.container{ + width: 100%; + max-width: 1200px; + margin:auto; +} + +.row:before, +.row:after { + content:""; + display: table ; + clear:both; +} + +[class*='col-'] { + float: left; + min-height: 1px; + width: 16.66%; +} + +.col-1{ width: 16.66%; } +.col-2{ width: 33.33%; } +.col-3{ width: 50%; } +.col-4{ width: 66.66%; } +.col-5{ width: 83.33%; } +.col-6{ width: 100%; } + +.tab { + outline: 1px solid #004D40; + padding-top:10px; + padding-bottom:10px; + font-weight: 900; +} + +.tab.active { + background-color: #00695C; + color : #FAFAFA; +} + +.text-right { + text-align: right; +} + +.text-center { + text-align: center; +} + +.img-circle { + max-width: 170px; + max-height: 170px; + border-radius: 50%; +} \ No newline at end of file diff --git a/src/app.php b/src/app.php new file mode 100644 index 0000000000000000000000000000000000000000..0bdcc30d18bb88c5e01a038f8c1cc6da37eb56c8 --- /dev/null +++ b/src/app.php @@ -0,0 +1,63 @@ +<?php + + +/** + * DagoJek is a singleton class that representing DagoJek application + */ +class DagoJek { + + private $routingTable = array(); + + private function __construct() { + // empty constructor + } + + public static function Instance() { + static $instance = null; + if ($instance === null) { + $instance = new DagoJek(); + } + + $instance->includeAllController(); + + return $instance; + } + + private function includeAllController() { + foreach (scandir(dirname(__FILE__)."/controller") as $filename) { + $path = dirname(__FILE__)."/controller" . '/' . $filename; + if (is_file($path)) { + require_once $path; + } + } + } + + public function prepareRouting() { + require __DIR__.'/route.php'; + } + + private function getCurrentUri() { + $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/'; + $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath)); + if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?')); + $uri = '/' . trim($uri, '/'); + return $uri; + } + + public function addRoute($route, $function) { + $data = array( + $route => $function, + ); + $this->routingTable += $data; + } + + public function Start() { + $base_url = $this->getCurrentUri(); + if (array_key_exists ($base_url, $this->routingTable)) { + $this->routingTable[$base_url](); + } else { + die ("404 Page not Found"); + } + } + +} diff --git a/src/controller/Controller.php b/src/controller/Controller.php new file mode 100644 index 0000000000000000000000000000000000000000..d417e614a59a86650e41636f57064e2f47d9bd09 --- /dev/null +++ b/src/controller/Controller.php @@ -0,0 +1,53 @@ +<?php + +// ----------------------- Setting Up Global Connection ----------------------------------- + +class DB { + private $_db; + static $_instance; + + private function __construct() { + + $dbhost = 'localhost'; + $dbuser = 'root'; + $dbpass = 'superadmin'; + $dbname = 'db_dagojek'; + + $this->_db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); + $this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + } + + private function __clone(){} + + public static function getInstance() { + if (!(self::$_instance instanceof self)) { + self::$_instance = new self(); + } + return self::$_instance->_db; + } + +} + + +// ------------------------------ Helper Function ---------------------------------------- + + +function simpleCrypt( $string, $action = 'e' ) { + + $secret_key = 'dagojek_key'; + $secret_iv = 'dagojek_iv'; + + $output = false; + $encrypt_method = "AES-256-CBC"; + $key = hash( 'sha256', $secret_key ); + $iv = substr( hash( 'sha256', $secret_iv ), 0, 16 ); + + if( $action == 'e' ) { + $output = base64_encode( openssl_encrypt( $string, $encrypt_method, $key, 0, $iv ) ); + } + else if( $action == 'd' ){ + $output = openssl_decrypt( base64_decode( $string ), $encrypt_method, $key, 0, $iv ); + } + + return $output; +} \ No newline at end of file diff --git a/src/controller/MainController.php b/src/controller/MainController.php new file mode 100644 index 0000000000000000000000000000000000000000..c0fa8167c4d80d68f33d6ea3e1411852efec3e28 --- /dev/null +++ b/src/controller/MainController.php @@ -0,0 +1,13 @@ +<?php + +class MainController { + + public static function LoginHandler() { + echo "This is login handler"; + } + + public static function DefaultHandler() { + echo "This is default handler"; + } + +} \ No newline at end of file diff --git a/src/controller/ProfilController.php b/src/controller/ProfilController.php new file mode 100644 index 0000000000000000000000000000000000000000..d5a5b9c9d8a24ee4bd3d48051eefe242118bfd1d --- /dev/null +++ b/src/controller/ProfilController.php @@ -0,0 +1,36 @@ +<?php + +require_once __DIR__.'/../model/User.php'; +require_once __DIR__.'/../model/Driver.php'; + +class ProfilController { + + public static function ProfilHandler() { + + // Getting user id from url + if (!isset($_GET['u']) || $_GET['u'] == "") { + echo "Invalid parameter!"; + return; + } + + // Decrypt user id + $uid = simpleCrypt($_GET['u'], 'd'); + + // Getting driver profile + $dbconn = DB::getInstance(); + $user = Driver::Create($uid, $dbconn); + + if (!$user) { + echo "User not found!"; + return; + } + + require __DIR__.'/../view/profil.php'; + + } + + public static function EditHandler() { + echo "TBD"; + } + +} \ No newline at end of file diff --git a/src/model/Driver.php b/src/model/Driver.php new file mode 100644 index 0000000000000000000000000000000000000000..af1cf7b4e507aa92e42562db2e1f5f1ef4842cca --- /dev/null +++ b/src/model/Driver.php @@ -0,0 +1,27 @@ +<?php + +class Driver extends User { + public $rating; + public $sumOrder; + + public static function Create($id, PDO $dbconn) { + try { + $stmt = $dbconn->prepare(" + SELECT id, name, username, email, phone, rating, is_driver AS isDriver, sum_order AS sumOrder + FROM user NATURAL JOIN driver + WHERE id =:id" + ); + $stmt->execute(array('id'=>$id)); + + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $result = $stmt->fetchObject('Driver'); + + return $result; + + } catch (PDOException $e) { + echo "Error : ".$e->getMessage(); + return false; + } + } + +} \ No newline at end of file diff --git a/src/model/User.php b/src/model/User.php new file mode 100644 index 0000000000000000000000000000000000000000..780a775e9669f9d0409807a19e80353c97516fbd --- /dev/null +++ b/src/model/User.php @@ -0,0 +1,30 @@ +<?php + +class User { + + public $id; + public $name; + public $username; + public $email; + public $phone; + public $isDriver; + + public static function Create($id, PDO $conn) { + try { + $stmt = $conn->prepare("SELECT * FROM user WHERE id=$id"); + $stmt->execute(); + + $stmt->setFetchMode(PDO::FETCH_ASSOC); + $user = $stmt->fetchObject(); + $result = new User($user->id, $user->name, $user->username, $user->email, $user->phone); + $result->isDriver = $user->is_driver; + + return $result; + } catch (PDOException $e) { + echo "Error : ".$e->getMessage(); + return false; + } + } + + +} \ No newline at end of file diff --git a/src/route.php b/src/route.php new file mode 100644 index 0000000000000000000000000000000000000000..76edc5e0004fd73322974d8b1de994f3f7081497 --- /dev/null +++ b/src/route.php @@ -0,0 +1,15 @@ +<?php + +// ---------------------------- LIST OF ALL BASIC ROUTES ---------------------------- + +$AppInstance = Dagojek::Instance(); + +$AppInstance->addRoute("/", 'MainController::LoginHandler'); +$AppInstance->addRoute("/login", 'MainController::LoginHandler'); +$AppInstance->addRoute("/register", 'MainController::DefaultHandler'); +$AppInstance->addRoute("/main/profil", 'ProfilController::ProfilHandler'); +$AppInstance->addRoute("/main/profil/edit", 'ProfilController::EditHandler'); +$AppInstance->addRoute("/main/history", 'MainController::DefaultHandler'); +$AppInstance->addRoute("/main/order/", 'MainController::DefaultHandler'); +$AppInstance->addRoute("/main/order/select", 'MainController::DefaultHandler'); +$AppInstance->addRoute("/main/order/finish", 'MainController::DefaultHandler'); diff --git a/src/view/profil.php b/src/view/profil.php new file mode 100644 index 0000000000000000000000000000000000000000..fa2297d74c8c95b77399a8f65102e4242aab8b19 --- /dev/null +++ b/src/view/profil.php @@ -0,0 +1,52 @@ +<html> +<head> + <title>DAGO-JEK | Profil</title> + <link rel="stylesheet" type="text/css" href="/style.css"> +</head> +<body> + <div class="container"> + <div class="row"> + <div class="col-3">Logo</div> + <div class="col-3 text-right"> + <p> + Hi, <?=$user->username?><br> + Logout + </p> + </div> + </div> + <div class="row"> + <div class="col-2 tab text-center">ORDER</div> + <div class="col-2 tab text-center">HISTORY</div> + <div class="col-2 tab text-center active">MY PROFILE</div> + </div> + <div class="row"> + <div class="col-5"><h1>MY PROFILE</h1></div> + <div class="col-1 text-right">edit</div> + </div> + <div class="text-center"> + <img class="img-circle" src="<?=$user->photo?>"/><br> + <h2>@<?=$user->username?></h2> + <p><?=$user->name?></p> + <?php if ($user->isDriver) : ?> + <p>Driver | <?=$driver_rating?> (<?=$driver_order?> vote<?=($driver_order>1)?'s':''?>)</p> + <?php else : ?> + <p>Non Driver</p> + <?php endif; ?> + <p><?=$user->email?></p> + <p><?=$user->phone?></p> + </div> + <div class="row"> + <div class="col-5"><h2>PREFERED LOCATIONS</h2></div> + <div class="col-1 text-right">edit</div> + </div> + <div class="row"> + <ul> + <li>Lokasi 1</li> + <li>Lokasi 2</li> + <li>Lokasi 3</li> + <li>Lokasi 4</li> + </ul> + </div> + </div> +</body> +</html> \ No newline at end of file