diff --git a/app/Helpers/myhelpers_helper.php b/app/Helpers/myhelpers_helper.php index 468e0bc..758c460 100644 --- a/app/Helpers/myhelpers_helper.php +++ b/app/Helpers/myhelpers_helper.php @@ -4,7 +4,7 @@ use App\Models\UsersModel; use App\Models\TracksModel; use App\Models\CarsModel; -function get_header($title, $css=[]) +function get_header($title, $css=[], $dasboard=false) { if (!is_array($css)) $css = []; $data = [ @@ -12,12 +12,13 @@ function get_header($title, $css=[]) 'custom_css' => $css, ]; + if ($dasboard) return view('dashboard/header', $data); return view('templates/header', $data); } /** * Esta función muestra la plantilla del pie de la página. - * @param Array $css Un array con los scripts opcionales. + * @param array $js Un array con los scripts opcionales. * @return * La vista del pie o false en caso de error. */ function get_footer($js=[]) @@ -29,18 +30,18 @@ function get_footer($js=[]) /* ================================================ * make xml data saveable on database - * + * * ================================================*/ /** - @param: - $xml: SimpleXMLElement - $force: set to true to always create 'text', 'attribute', and 'children' even if empty - @return - object with attributs: - (string) name: XML tag name - (string) text: text content of the attribut name - (array) attributes: array witch keys are attribute key and values are attribute value - (array) children: array of objects made with xml2obj() on each child + @param: + $xml: SimpleXMLElement + $force: set to true to always create 'text', 'attribute', and 'children' even if empty + @return + object with attributs: + (string) name: XML tag name + (string) text: text content of the attribut name + (array) attributes: array witch keys are attribute key and values are attribute value + (array) children: array of objects made with xml2obj() on each child **/ function xmlObj($xmlstring) { @@ -50,6 +51,7 @@ function xmlObj($xmlstring) ################################### ## extract content of a json txt file an d return an object of the json string ################################### +/* function jsonTxtFileToObj($fileUrl, $objClass) { $myfile = fopen($fileUrl, "r") or die("Unable to open file!"); @@ -104,19 +106,16 @@ function getTrackCats() { return jsonTxtFileToObj(WRITEPATH . "/data/trackCategories.txt", 'TrackCategory'); } - -################################### -## -################################### +*/ function secondsToTime($seconds) { /* - $dtF = new DateTime("@0"); - $dtT = new DateTime("@$seconds"); - return $dtF->diff($dtT)->format('%a d, %h hr, %i min, %s sec'); - */ + $dtF = new DateTime("@0"); + $dtT = new DateTime("@$seconds"); + return $dtF->diff($dtT)->format('%a d, %h hr, %i min, %s sec'); + */ if ($seconds == 0) return 0; - + $date1 = new \DateTime("@0"); $date2 = new \DateTime("@$seconds"); $interval = $date1->diff($date2); @@ -140,9 +139,6 @@ function secondsToTime($seconds) return $str; } -################################### -## -################################### function formatLaptime($seconds) { $seconds = $seconds *1; @@ -150,7 +146,7 @@ function formatLaptime($seconds) //minuti $str.= sprintf('%02d', ($seconds/60)).':'; - $seconds = fmod($seconds, 60); + $seconds = fmod($seconds, 60); //secondi $str.= sprintf('%02d',$seconds).'.'; @@ -162,9 +158,6 @@ function formatLaptime($seconds) return $str; } -################################### -## -################################### function percentStr($smallvalue, $bigvalue) { if($bigvalue==0) return '-%'; @@ -173,33 +166,24 @@ function percentStr($smallvalue, $bigvalue) return $percent.'%'; } -################################### -## -################################### function weatherTag($value) { switch($value) { case 0: return ''; - break; case 1: return ''; - break; case 2: return ''; - break; case 3: return ''; - break; } } -################################### -## -################################### -//this will rewrite the current url -//and modify the given param if needed + +// This will rewrite the current url +// and modify the given param if needed function rewriteUrl($paramName, $paramValue) { $query = $_GET; @@ -208,10 +192,10 @@ function rewriteUrl($paramName, $paramValue) // rebuild url $query_result = http_build_query($query); // new link - return base_url().'?'.$query_result; + return base_url() . '?' .$query_result; } - +/* function getCar($carId) { $cars = getCars(); @@ -256,7 +240,7 @@ function getTrack($trackId) return $fakeTrack; } } - +*/ function generateConditions($conditions, $separator = ',') { $txt=''; @@ -275,37 +259,43 @@ function generateConditions($conditions, $separator = ',') return $txt; } - +/* class CarCategory { - function CarCategory($category){ + function category($category) + { $this->import($category); } - - public function import($properties){ + + public function import($properties){ foreach($properties as $key => $value){ $this->{$key} = $value; } - } + } } class TrackCategory { - function TrackCategory($category){ + function category($category){ $this->import($category); } - public function import($properties){ + public function import($properties){ foreach($properties as $key => $value){ $this->{$key} = $value; } - } + } } +*/ -function racetype($num){ +function racetype($num) +{ switch ($num){ - case 0: return 'practice';break; - case 1: return 'qualify';break; - case 2: return 'race';break; + case 0: + return 'practice'; + case 1: + return 'qualify'; + case 2: + return 'race'; } } @@ -333,12 +323,8 @@ function getBestTimesTrack($trackId) $builder->where('r.track_id', $trackId); $builder->orderBy('l.laptime'); $query = $builder->get(10); - - if (!$query || $query->getNumRows() == 0) - { - echo "No laps to show for this tracks"; - return []; - } + + if (!$query || $query->getNumRows() == 0) return []; return $query->getResult(); } @@ -352,4 +338,104 @@ function getCarBetsLaps($carId) WHERE r.car_id = '$carId' GROUP BY r.id ORDER BY l.laptime"; -} \ No newline at end of file +} + +function getOS() { + + $user_agent = $_SERVER['HTTP_USER_AGENT']; + log_message('debug', "HTTP_USER_AGENT: $user_agent"); + $os_platform = "Unknown Operative System"; + $os_array = array( + '/windows nt 10/i' => 'Windows 10', + '/windows nt 6.3/i' => 'Windows 8.1', + '/windows nt 6.2/i' => 'Windows 8', + '/windows nt 6.1/i' => 'Windows 7', + '/windows nt 6.0/i' => 'Windows Vista', + '/windows nt 5.2/i' => 'Windows Server 2003/XP x64', + '/windows nt 5.1/i' => 'Windows XP', + '/windows xp/i' => 'Windows XP', + '/windows nt 5.0/i' => 'Windows 2000', + '/windows me/i' => 'Windows ME', + '/win98/i' => 'Windows 98', + '/win95/i' => 'Windows 95', + '/win16/i' => 'Windows 3.11', + '/macintosh|mac os x/i' => 'Mac OS X', + '/mac_powerpc/i' => 'Mac OS 9', + '/linux/i' => 'Linux', + '/ubuntu/i' => 'Ubuntu', + '/iphone/i' => 'iPhone', + '/ipod/i' => 'iPod', + '/ipad/i' => 'iPad', + '/android/i' => 'Android', + '/blackberry/i' => 'BlackBerry', + '/webos/i' => 'Mobile', + '/libcurl-agent/i' => 'Unknown (Curl)' + ); + + foreach ( $os_array as $regex => $value ) + if ( preg_match($regex, $user_agent ) ) $os_platform = $value; + + log_message('debug', "Operative System: $os_platform"); + return $os_platform; +} + +/** + * Returns if the current user if log in + * @return bool The user level if logged in + */ +function ifLoggedIn(): bool +{ + //$db = \Config\Database::connect(); + $session = session(); + if ($session->get('logged_in')) return true; + return false; +} + +function findObjectById($array, $id) +{ + foreach ( $array as $element ) + { + if ( $id == $element->id ) return true; + } + + return false; +} + +function imgTag(string $img, string $width, string $title): string +{ + $img = str_replace('./', '/', $img); + $url = base_url($img); + return "$title"; +} + +function imgTagFull(string $img, string $class, string $alt) +{ + $img = str_replace('./', '/', $img); + $url = base_url($img); + return "$alt"; +} + +function clickableName(string $id, string $type, string $content): string +{ + return linkTag($id, $type, $content); +} + +function clickableImgTag(string $id, string $size, string $type, string $title): string +{ + return linkTag($id, $type, imgTag($id, $size, $title)); +} + +function linkTag(string $id, string $type , string $content): string +{ + $url = base_url("$type/$id"); + return "$content"; +} + +function linkTitleImgTag(string $id, string $type, string $name, string $img): string +{ + if ($type != 'car' && $type != 'track') return ''; + + $url = base_url("$type/$id"); + $content = $name . '
' . imgTag($img, '80px', $name); + return "$content"; +} diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php new file mode 100644 index 0000000..0a28393 --- /dev/null +++ b/app/Models/BaseModel.php @@ -0,0 +1,25 @@ +builder(); + $builder->where('id', $id); + + $query = $builder->get(1); + + if ($query) return $query->getRow(); + } +} diff --git a/app/Models/CarsModel.php b/app/Models/CarsModel.php index af6d8e7..c0fadd4 100644 --- a/app/Models/CarsModel.php +++ b/app/Models/CarsModel.php @@ -1,27 +1,29 @@ import($car); + if ($car) $this->data = $this->data($car); } - public function import($properties){ - foreach($properties as $key => $value) - { - $this->{$key} = $value; - } - } - public function getLink($text='') + public function getLink(string $text=null): string { - if($text == '') $text = $this->username; - return "id}'>$text"; + if(!$text) $text = $this->data->name; + return "data->id}'>$text"; } - public function card($text='') { - return $this->name.$this->img; + public function card() { + return $this->data->name . $this->data->img; } public function imgTag() @@ -54,4 +56,5 @@ class CarsModel $content = $this->name . '
' . $this->imgTag(); return "$content"; } + */ } diff --git a/app/Models/TracksModel.php b/app/Models/TracksModel.php index f6130b5..fa476ca 100644 --- a/app/Models/TracksModel.php +++ b/app/Models/TracksModel.php @@ -1,33 +1,40 @@ import($track); + if ($track) $this->import($track); } - public function import($properties){ - foreach($properties as $key => $value){ - $this->{$key} = $value; - } + public function import($properties) + { + foreach($properties as $key => $value) $this->{$key} = $value; } - public function getLink($text='') { + + public function getLink($text='') + { if($text == '') $text = $this->username; return ''.$text.''; } - public function card($text='') { + + public function card($text='') + { return $this->name.$this->img; } + public function imgTag() { return "".$this->name.""; } + public function imgTagFull() { return "".$this->name.""; } @@ -47,4 +54,5 @@ class TracksModel extends Model $content = $this->name . '
' . $this->imgTag(); return "$content"; } + */ } diff --git a/app/Models/UsersModel.php b/app/Models/UsersModel.php index faf44d5..197685d 100644 --- a/app/Models/UsersModel.php +++ b/app/Models/UsersModel.php @@ -1,30 +1,55 @@ db = \Config\Database::connect(); - $this->id = $id; + $this->session = \Config\Services::session(); + $this->carsModel = new CarsModel(); + $this->tracksModel = new TracksModel(); $this->raceTypes = new \stdClass; $this->raceTypes->practice = 0; $this->raceTypes->qualify = 1; $this->raceTypes->race = 2; + } - if ($this->id){ - $this->getFromDb(); - } + /* + public function initialize() + { + $this->session = session(); + $this->carsModel = new CarsModel; + $this->tracksModel = new TracksModel; + + $this->raceTypes = new \stdClass; + $this->raceTypes->practice = 0; + $this->raceTypes->qualify = 1; + $this->raceTypes->race = 2; } public function getUsers() @@ -35,18 +60,22 @@ class UsersModel extends Model if ($query && $query->getNumRows() > 0) $users = $query->getResult(); return $users; } - + */ public function getUser($username) { - $builder = $this->db->table('users'); - $builder->select('id'); + $builder = $this->builder(); + $builder->select('id, email, username, nation, img'); $builder->where('username', $username); $query = $builder->get(1); if ($query && $query->getNumRows() == 1) { - return $query->getRow()->id; - } + $data = $query->getRow(); + $this->id = $data->id; + $data->flag = str_replace(' ', '_', $data->nation) . '.png'; + return $data; + } + return false; } @@ -54,11 +83,11 @@ class UsersModel extends Model * Check if the user and/or email is already in use. * @param string $username The username to check. * @param string $email The email to check. - * @return Boolean False if the username and email are not in use + * @return boolean False if the username and email are not in use */ public function compUser($username, $email) { - $builder = $this->db->table('users'); + $builder = $this->builder(); $builder->select('id'); $builder->where('username', $username); $builder->orWhere('email', $email); @@ -72,6 +101,7 @@ class UsersModel extends Model return false; } + /* private function import($properties) { foreach($properties as $key => $value){ @@ -103,29 +133,34 @@ class UsersModel extends Model public function getLink($text='') { if ($text == '') $text=$this->username; - return "$text"; + return "$text"; } public function getSmallFlagImg() { - return "{$this->nation}"; + return "{$this->nation}"; } public function getMediumFlagImg() { - return "{$this->nation}"; + return "{$this->nation}"; } public function getImgFile() { - return "{$this->username}"; + return "{$this->username}"; } + */ public function getRaceSessions() { - $builder = $this->db->table('races'); - $builder->where('user_id', $this->id); - $builder->orderBy('id DESC'); + $builder = $this->db->table('races r'); + $builder->select('r.id, r.user_skill, r.track_id, r.car_id, r.startposition, r.endposition'); + $builder->select('r.sdversion, r.timestamp, r.type, t.name as track_name, c.name as car_name'); + $builder->join('tracks t', 't.id = r.track_id'); + $builder->join('cars c', 'c.id = r.car_id'); + $builder->where('r.user_id', $this->id); + $builder->orderBy('r.id DESC'); $query = $builder->get(); if ($query && $query->getNumRows() > 0) return $query->getResult(); @@ -246,7 +281,7 @@ class UsersModel extends Model $data = new \stdClass; if ($query && $query->getNumRows() > 0) { - $data->car = new CarsModel(getCar($query->getRow()->car_id)); + $data->car = $this->carsModel->find($query->getRow()->car_id); $data->total = $query->getRow()->count; } return $data; @@ -255,48 +290,52 @@ class UsersModel extends Model public function getMostUsedTrack() { $builder = $this->db->table('races'); - $builder->select('track_id, COUNT(*) as count'); + $builder->select('track_id, COUNT(*) AS total'); $builder->where([ 'user_id' => $this->id ]); - $builder->orderBy('count', 'desc'); + $builder->orderBy('total', 'desc'); $builder->groupBy('track_id'); $query = $builder->get(1); $data = new \stdClass; + if ($query && $query->getNumRows() > 0) { - $data->track = new TracksModel(getTrack($query->getRow()->track_id)); - $data->total = $query->getRow()->total; + $result = $query->getRow(); + log_message('debug', json_encode($result)); + $data->track = $this->tracksModel->find($result->track_id); + $data->total = $result->total; } return $data; } public function getTimeOnTracks() { - $builder = $this->db->table('laps A'); - $builder->selectSum('A.laptime'); - $builder->join('races B', 'ON A.race_id = B.id'); + $builder = $this->db->table('laps l'); + $builder->selectSum('l.laptime'); + $builder->join('races r', 'l.race_id = r.id'); $builder->where([ - 'B.user_id' => $this->id + 'r.user_id' => $this->id ]); //$builder->orderBy('count', 'desc'); $query = $builder->get(1); if ($query && $query->getNumRows() > 0) return $query->getRow()->laptime; + return 0; } public function getTimeOnRace() { - $builder = $this->db->table('laps A'); - $builder->selectSum('A.laptime'); - $builder->join('races B', 'ON A.race_id = B.id'); + $builder = $this->db->table('laps l'); + $builder->selectSum('l.laptime'); + $builder->join('races r', 'l.race_id = r.id'); $builder->where([ - 'B.user_id' => $this->id, - 'B.type' => $this->raceTypes->race + 'r.user_id' => $this->id, + 'r.type' => $this->raceTypes->race ]); //$builder->orderBy('count', 'desc'); @@ -308,12 +347,12 @@ class UsersModel extends Model public function getTimePractice() { - $builder = $this->db->table('laps A'); - $builder->selectSum('A.laptime'); - $builder->join('races B', 'ON A.race_id = B.id'); + $builder = $this->db->table('laps l'); + $builder->selectSum('l.laptime'); + $builder->join('races r', 'l.race_id = r.id'); $builder->where([ - 'B.user_id' => $this->id, - 'B.type' => $this->raceTypes->practice + 'r.user_id' => $this->id, + 'r.type' => $this->raceTypes->practice ]); //$builder->orderBy('count', 'desc'); @@ -325,12 +364,12 @@ class UsersModel extends Model public function getTimeQualify() { - $builder = $this->db->table('laps A'); - $builder->selectSum('A.laptime'); - $builder->join('races B', 'ON A.race_id = B.id'); + $builder = $this->db->table('laps l'); + $builder->selectSum('l.laptime'); + $builder->join('races r', 'l.race_id = r.id'); $builder->where([ - 'B.user_id' => $this->id, - 'B.type' => $this->raceTypes->qualify + 'r.user_id' => $this->id, + 'r.type' => $this->raceTypes->qualify ]); //$builder->orderBy('count', 'desc'); @@ -354,14 +393,13 @@ class UsersModel extends Model 'username' => $data['username'], 'email' => $data['email'], 'nation' => $data['nation'], - 'username' => $data['username'], ]; // First verify if the user or email - $sql = $this->db->table('users'); - $sql->where('username', $user['username']); - $sql->orWhere('email', $user['email']); - $query = $sql->get(1); + $builder = $this->db->table('users'); + $builder->where('username', $user['username']); + $builder->orWhere('email', $user['email']); + $query = $builder->get(1); if ($query && $query->getNumRows() == 1){ $response['msg'] = 'Username and/or email are registered'; @@ -372,24 +410,50 @@ class UsersModel extends Model $user['password'] = password_hash($data['password'], self::HASH, [self::COST]); unset($data); // Insert data - $sql->insert($user); + $this->insert($user); $error = $this->db->error(); - if ($error['code'] != 0) - { - $resp['msg'] = 'An error ocurred on insert the new user to the database'; - } + + if ($error['code'] != 0) $response['msg'] = 'An error ocurred on insert the new user to the database'; else { $id = $this->db->insertID(); - $sql->resetQuery(); + //$builder->resetQuery(); // Move the file to it's new home $filename = $user['username'] . '.' . $image->getExtension(); $image->move(FCPATH . '/img/users/', $filename); - $sql->where('id', $id)->update(['img' => $filename]); + $this->where('id', $id)->update(['img' => $filename]); $response['ok'] = true; } } return $response; } + + public function login($data) + { + $builder = $this->builder(); + $builder->select('id, password, level, username'); + $builder->where('username', $data['username']); + $query = $builder->get(1); + if ($query && $query->getNumRows() == 1) + { + $user = $query->getRow(); + if (password_verify($data['passwd'], $user->password)) + { + $sessiondata = [ + 'userid' => $user->id, + 'userlevel' => $user->level, + 'username' => $user->username, + 'logged_in' => true + ]; + + $this->session->set($sessiondata); + return true; + } + + return false; + } + + return false; + } } \ No newline at end of file