Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* User Location & Weather Module */
- Class WL {
- /* Get user's Set Location */
- public function getUserLocation() {
- /* Query Database */
- global $db;
- $query = "SELECT location FROM users WHERE uid='{$this->uid}'";
- $rs = $db->Execute($query);
- /* Return Results */
- return ($rs->fields['location']) ? $rs->fields['location'] : false;
- }
- /* Update user's Set Location */
- public function setUserLocation($location) {
- /* Query Database for User's Locations */
- global $db;
- if (self::getLocationInfo($location)) {
- /* Update Database */
- $query = "UPDATE users SET location='$location' WHERE uid='".$_SESSION['uid']."'";
- $db->Execute($query);
- /* Return Response */
- return true;
- }
- else { return false; }
- }
- /* Get All Locations */
- public function getLocationList() {
- /* Set List Order */
- $location = self::getUserLocation();
- if ($location == "Current") { $order = "'Current', 'Home', 'Work'"; }
- elseif ($location == "Home") { $order = "'Home', 'Current', 'Work'"; }
- else { $order = "'Work', 'Current', 'Home'"; }
- /* Query Database to gather User's Locations */
- global $db;
- $query = "SELECT lat,lng,address,location FROM user_locations WHERE uid='".$_SESSION['uid']."' ORDER BY FIELD(location, $order)";
- $rs = $db->Execute($query);
- /* Return Results */
- return ($rs->recordcount()) ? $rs->getrows() : false;
- }
- /* Get Single Location Information */
- public function getLocationInfo($location) {
- /* Query Database */
- global $db;
- $query = "SELECT uid,address,lat,lng FROM user_locations WHERE location='$location' AND uid='".$_SESSION['uid']."'";
- $rs = $db->Execute($query);
- /* Return Results */
- return ($rs->recordcount()) ? self::toObject($rs->fields) : false;
- }
- /* Get Weather for Single Location */
- private function getWeather($location) {
- /* Get Location Information */
- global $config;
- $info = self::getLocationInfo($location);
- /* Build Object */
- $weather->Results = json_decode(file_get_contents($config->weatherUrl.$info->lat.",".$info->lng));
- $weather->Code = $weather->Results->data->weather[0]->weatherCode;
- $weather->Description = $weather->Results->data->weather[0]->weatherDesc[0]->value;
- /* Return Results */
- return self::reformatWeather($weather);
- }
- /* Set Session Weather Information */
- public function setWeather() {
- /* Gather Weather By Location Provided */
- $location = self::getUserLocation();
- $weather = self::getWeather($location);
- /* If Session Units are Metric else Return Fahrenheit */
- if ($_SESSION['units']) {
- $temp->Current = $weather->Results->data->current_condition[0]->temp_C;
- $temp->Min = $weather->Results->data->weather[0]->tempMinC;
- $temp->Max = $weather->Results->data->weather[0]->tempMaxC;
- }
- else {
- $temp->Current = ceil(($weather->Results->data->current_condition[0]->temp_C*9)/5 + 32);
- $temp->Min = ceil(($weather->Results->data->weather[0]->tempMinC*9)/5 + 32);
- $temp->Max = ceil(($weather->Results->data->weather[0]->tempMaxC*9)/5 + 32);
- }
- /* Set Session Variables */
- $_SESSION['weatherCode'] = $weather->Code;
- $_SESSION['weatherDescription'] = $weather->Description;
- $_SESSION['currentTemp'] = $temp->Current;
- $_SESSION['minTemp'] = $temp->Min;
- $_SESSION['maxTemp'] = $temp->Max;
- /* Response Array */
- $results = Array('weather' => Array('code' => $weather->Code, 'description' => $weather->Description, 'current' => $temp->Current, 'min' => $temp->Min, 'max' => $temp->Max));
- return $results;
- }
- /* Reformat Weather */
- private function reformatWeather($info) {
- /* Codes of descriptions that need to be reformated because of their length */
- $descriptionCodes = array("185","200","314","356","365","371","377","386","389","392","395");
- /* Codes of description that need to be reformated depending on the time of day */
- $weatherCodes = array("113","116","119","299");
- /* Check if provided code is in array, if so modify description */
- if (in_array($info->Code, $descriptionCodes)) {
- if ($info->Code == "185") { $info->Description = "Patchy freezing drizzle"; }
- elseif ($info->Code == "200") { $info->Description = "Thundery outbreaks"; }
- elseif ($info->Code == "314") { $info->Description = "Moderate freezing rain"; }
- elseif ($info->Code == "356") { $info->Description = "Moderate rain shower"; }
- elseif ($info->Code == "365") { $info->Description = "Moderate sleet showers"; }
- elseif ($info->Code == "371") { $info->Description = "Moderate snow showers"; }
- elseif ($info->Code == "377") { $info->Description = "Moderate ice pellets showers"; }
- elseif ($info->Code == "386") { $info->Description = "Light right with thunder"; }
- elseif ($info->Code == "389") { $info->Description = "Moderate rain with thunder"; }
- elseif ($info->Code == "392") { $info->Description = "Light snow with thunder"; }
- elseif ($info->Code == "395") { $info->Description = "Moderate snow with thunder"; }
- }
- /* Define weather the icon needs a _1 after */
- if (in_array($info->Code,$weatherCodes)) {
- if (self::isDay($info->lat,$info->lng) == "night") {
- if ($info->Description == "Sunny") { $info->Description = "Clear"; }
- $info->Code = $info->Code."_1";
- }
- }
- /* Return Results */
- return $info;
- }
- /* Sunset / Sunrise Function */
- private function isDay($lat,$lng,$timezone = "GMT") {
- /* Define Sunset / Sunrise by Longitude and Latitude */
- date_default_timezone_set($timezone);
- $now = time();
- $zenith = 96;
- $offset = 0;
- $sunrise = date_sunrise($now, SUNFUNCS_RET_TIMESTAMP, $lat, $lng, $zenith, $offset);
- $sunset = date_sunset($now, SUNFUNCS_RET_TIMESTAMP, $lat, $lng, $zenith, $offset);
- /* Define whether it is day or night */
- $isDay = ($sunrise < $now) && ($now < $sunset);
- if ($sunrise > $sunset) {
- $isDay = !$isDay;
- }
- $timeOfDay = $isDay ? "day" : "night";
- /* Return Time Of Day */
- return $timeOfDay;
- }
- /* Transform Array To Object */
- private function toObject($array) {
- /* Build Object */
- $object = null;
- foreach ($array as $key => $value) {
- $object->$key = $value;
- }
- /* Return Results */
- return $object;
- }
- /* Transform Array To Object */
- private function toArray($object) {
- /* Build Object */
- $array = null;
- foreach ($object as $key => $value) {
- $array[$key] = $value;
- }
- /* Return Results */
- return $array;
- }
- }
- ?>
- <?php
- class User extends WL {
- /* Public Variables */
- public $uid,
- $email,
- $firstname,
- $lastname,
- $birthday,
- $gender,
- $addtime,
- $logintime,
- $lastactivity,
- $verified,
- $status,
- $units,
- $privacy,
- $location,
- $cookie;
- /* User Model */
- private $userModel = Array('uid,email,firstname,lastname,birthday,gender,addtime,logintime,lastactivity,verified,status,units,privacy,location,cookie,tempUnit,distanceUnit');
- /* Build User Object */
- public function __construct() {
- /* Restore Session if Authenticated */
- if ($this->LoggedIn()) { $this->RestoreAccess(); }
- }
- /* Add User Function */
- public function AddUser($input) {
- /* Global Variables */
- global $config,$db,$UI;
- /* Validate User Input */
- if(!$input->firstname) { $err = "Firstname field should not be empty."; }
- elseif(!preg_match("/^(?i)[a-z]*$/", $input->firstname)){ $err = "First Name can only contain letters."; }
- elseif(strlen($input->firstname) < 3){ $err = "First Name should be at least 3 characters long"; }
- elseif(!$input->lastname) { $err = "Lastname field should not be empty."; }
- elseif(!preg_match("/^(?i)[a-z]*$/", $input->lastname)){ $err = "Last Name can only contain letters."; }
- elseif(strlen($input->lastname) < 3){ $err = "Last Name should be at least 3 characters long"; }
- elseif(!$input->password) { $err = "Password should not be empty."; }
- elseif(strlen($input->password) < 6){ $err = "Password should be at least 6 characters."; }
- elseif($input->password != $input->retype) { $err = "Retype password does not match."; }
- elseif(!$input->month) { $err = "You must enter your birthdate. (Month)"; }
- elseif(!$input->day) { $err = "You must enter your birthdate. (Day)"; }
- elseif(!$input->year) { $err = "You must enter your birthdate. (Year)"; }
- elseif(!checkdate($input->month, $input->day, $input->year)) { $err = "You must enter a valid birthdate."; }
- elseif(!$input->gender) { $err = "You must define your gender."; }
- elseif(!$input->email) { $err = "E-Mail field should not be empty."; }
- elseif(!checkEmail($input->email)) { $err = "Invalid Email format."; }
- elseif(self::Exist($input->email)) { $err = "Another account has already been created with this e-mail address."; }
- /* If No Error Add User to Database */
- if (!$err) {
- /* Format Birthday */
- $birthDay = $input->year."-".$input->month."-".$input->day;
- /* Build Query & Insert user in database */
- $query = "
- INSERT INTO users SET
- email='{$input->email}',
- gender='{$input->gender}',
- birthday='{$birthDay}',
- firstname='{$input->firstname}',
- lastname='{$input->lastname}',
- password='".md5($input->password)."',
- addtime='".time()."'
- ";
- $db->Execute($query);
- /* Assign User Id */
- $user->Id = $db->Insert_Id();
- /* Insert E-mail Verification In Database */
- $info->code = randString();
- $query = "INSERT INTO user_verification SET uid='{$user->Id}', code='{$info->code}'";
- $db->Execute($query);
- /* Prepare e-mail notification */
- $info->subject = "Please verify your account.";
- $info->resend = 0;
- /* Merge Input Object and Info Object */
- $info = (object) array_merge((array) $info, (array) $input);
- /* Assign Variables to E-Mail Template */
- UI::assign('info', $info);
- $body.= UI::fetch(''.$UI->emailTemplate.'/verification.tpl');
- /*
- User::mail($input->email,$config->siteName,$config->registrationEmail,$config->siteName.' - '.$subject,$body);
- */
- /* Success Response */
- $response->Success = "A verification email has been sent to your address. Registration will not be complete until your e-mail address has been confirmed.";
- }
- else { $response->Error = $err; }
- /* Return Response */
- return $response;
- }
- /* Authentication Function */
- public function Auth($input) {
- /* Global Variables */
- global $db;
- /* Validate User Input */
- if (!$input->email) { $err = "E-Mail Address field should not be empty."; }
- elseif (!$input->password) { $err = "Password field should not be empty."; }
- else {
- /* Query Database */
- $query = "SELECT uid,email,firstname,lastname,birthday,gender,addtime,logintime,lastactivity,verified,status,units,privacy,location,cookie FROM users WHERE email='$input->email' AND password='".md5($input->password)."'";
- $rs = $db->Execute($query);
- /* Analyze Response */
- if ($rs->recordcount()) {
- /* Set User Information */
- $this->setUserInfo($rs->fields);
- /* Check if User is Active & Verified */
- if (!$this->status) { $err = "Account Inactive. Please contact the system Administrator, for more details."; }
- elseif (!$this->verified) { $err = "You need to activate your account, to login to the site. If you have lost your activation link, please click <b><a href=\"{$config->baseUrl}/verify/resend/{$user->Id}\">here</a></b> to resend it."; }
- else {
- /* Update User Login Time */
- $query = "UPDATE users SET logintime='".time()."' WHERE uid='{$this->uid}'";
- $db->Execute($query);
- /* Set User Cookie if 'Remember Me' Checkbox is Checked */
- if ($input->persistent) { $this->setCookie(); }
- /* Unset User Email Input */
- unset($_SESSION['userEmailInput']);
- }
- }
- else { $err = "Invalid E-Mail/Password combination."; }
- }
- /* Return Response */
- if ($err) { $response->Error = $err; }
- return $response;
- }
- public function deAuth() {
- /* Global Variables */
- global $config;
- /* Unset all session variables */
- unset($_SESSION['uid']);
- unset($_SESSION['referer']);
- /* De-validate Cookie */
- setcookie($config->cookieName, '', time() - 3600);
- }
- public function restoreAccess() {
- /* Set Object Variables Back */
- foreach($_SESSION as $key => $value) {
- if (in_array($key, $this->userModel)) { $this->$key = $value; }
- }
- /* Update User Activity */
- $this->updateActivity();
- /* User Locations */
- $this->locations = $this->getLocationList();
- }
- public function setCookie() {
- /* Global Variables */
- global $config,$db;
- /* Build User Cookie */
- $cookie->New = randString();
- $cookie->Name = $config->cookieName;
- $cookie->Domain = $_SERVER['SERVER_NAME'];
- $cookie->Domain = substr($domain, 1, 0);
- setcookie($cookie->Name, $cookie->New, time()+31536000, '/', $cookie->Domain);
- /* Update User database with new cookie value */
- $query = "UPDATE users SET cookie='{$cookie->New}' WHERE uid='{$this->uid}'";
- $db->Execute($query);
- }
- /* Set User Infomation */
- private function setUserInfo($info) {
- /* Set Local & Session Variables of User */
- foreach($info as $key => $value) {
- $this->$key = $value;
- $_SESSION[$key] = $value;
- }
- /* Assign User Units to Session Variable */
- if ($_SESSION['units']) {
- $_SESSION['distanceUnit'] = "Km";
- $_SESSION['tempUnit'] = "C";
- }
- else {
- $_SESSION['distanceUnit'] = "Mi";
- $_SESSION['tempUnit'] = "F";
- }
- /* Update User Activity */
- $this->UpdateActivity();
- }
- /* Return Specify User Info */
- public function getInfo($var) {
- return (!$var) ? $this : $this->var;
- }
- /* Update user's session activity */
- public function UpdateActivity() {
- /* Global Variables */
- global $db;
- /* Update database */
- $time = time();
- $query = "UPDATE users SET lastactivity='$time' WHERE uid='".$this->uid."'";
- $db->Execute($query);
- /* Update Local Variable */
- $this->lastactivity = $time;
- $_SESSION['lastactivity'] = $time;
- }
- /* Return Whether the user is logged in or not */
- public static function LoggedIn() {
- return ($_SESSION['uid']) ? true : false;
- }
- /* User Single E-Mail Function */
- public function Mail($to,$name,$from,$subj,$body) {
- /* Global Variables */
- global $config;
- /* Prepare Body */
- $content= UI::fetch(''.$UI->emailTemplate.'/emailHeader.tpl');
- $content.= $body;
- $content.= UI::fetch(''.$UI->emailTemplate.'/emailFooter.tpl');
- /* Prepare HTML E-Mail Header */
- $headers = 'MIME-Version: 1.0' . "\r\n";
- $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
- $headers .= "To: <$to>"."\r\n";
- $headers .= "From: ".$config->siteName." <".$from.">"."\r\n";
- /* Send E-Mail */
- mail($to,$subj,$content,$headers);
- }
- /* Check if user exist in database */
- public static function Exist($email) {
- /* Global Variables */
- global $db;
- $results = null;
- /* Query Database */
- $query = "SELECT uid,firstname,lastname FROM users WHERE email='$email'";
- $rs = $db->Execute($query);
- /* Build Result Object */
- if ($rs->recordcount()) {
- foreach ($rs->fields as $key => $value) {
- $results->$key = $value;
- }
- }
- /* Return Results */
- return $results;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement