Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.47 KB | None | 0 0
  1. <?php
  2. /* User Location & Weather Module */
  3. Class WL {
  4. /* Get user's Set Location */
  5. public function getUserLocation() {
  6. /* Query Database */
  7. global $db;
  8. $query = "SELECT location FROM users WHERE uid='{$this->uid}'";
  9. $rs = $db->Execute($query);
  10.  
  11. /* Return Results */
  12. return ($rs->fields['location']) ? $rs->fields['location'] : false;
  13. }
  14.  
  15. /* Update user's Set Location */
  16. public function setUserLocation($location) {
  17. /* Query Database for User's Locations */
  18. global $db;
  19. if (self::getLocationInfo($location)) {
  20. /* Update Database */
  21. $query = "UPDATE users SET location='$location' WHERE uid='".$_SESSION['uid']."'";
  22. $db->Execute($query);
  23.  
  24. /* Return Response */
  25. return true;
  26. }
  27. else { return false; }
  28. }
  29.  
  30. /* Get All Locations */
  31. public function getLocationList() {
  32. /* Set List Order */
  33. $location = self::getUserLocation();
  34. if ($location == "Current") { $order = "'Current', 'Home', 'Work'"; }
  35. elseif ($location == "Home") { $order = "'Home', 'Current', 'Work'"; }
  36. else { $order = "'Work', 'Current', 'Home'"; }
  37.  
  38. /* Query Database to gather User's Locations */
  39. global $db;
  40. $query = "SELECT lat,lng,address,location FROM user_locations WHERE uid='".$_SESSION['uid']."' ORDER BY FIELD(location, $order)";
  41. $rs = $db->Execute($query);
  42.  
  43. /* Return Results */
  44. return ($rs->recordcount()) ? $rs->getrows() : false;
  45. }
  46.  
  47. /* Get Single Location Information */
  48. public function getLocationInfo($location) {
  49. /* Query Database */
  50. global $db;
  51. $query = "SELECT uid,address,lat,lng FROM user_locations WHERE location='$location' AND uid='".$_SESSION['uid']."'";
  52. $rs = $db->Execute($query);
  53.  
  54. /* Return Results */
  55. return ($rs->recordcount()) ? self::toObject($rs->fields) : false;
  56. }
  57.  
  58. /* Get Weather for Single Location */
  59. private function getWeather($location) {
  60. /* Get Location Information */
  61. global $config;
  62. $info = self::getLocationInfo($location);
  63.  
  64. /* Build Object */
  65. $weather->Results = json_decode(file_get_contents($config->weatherUrl.$info->lat.",".$info->lng));
  66. $weather->Code = $weather->Results->data->weather[0]->weatherCode;
  67. $weather->Description = $weather->Results->data->weather[0]->weatherDesc[0]->value;
  68.  
  69. /* Return Results */
  70. return self::reformatWeather($weather);
  71. }
  72.  
  73. /* Set Session Weather Information */
  74. public function setWeather() {
  75. /* Gather Weather By Location Provided */
  76. $location = self::getUserLocation();
  77. $weather = self::getWeather($location);
  78.  
  79. /* If Session Units are Metric else Return Fahrenheit */
  80. if ($_SESSION['units']) {
  81. $temp->Current = $weather->Results->data->current_condition[0]->temp_C;
  82. $temp->Min = $weather->Results->data->weather[0]->tempMinC;
  83. $temp->Max = $weather->Results->data->weather[0]->tempMaxC;
  84. }
  85. else {
  86. $temp->Current = ceil(($weather->Results->data->current_condition[0]->temp_C*9)/5 + 32);
  87. $temp->Min = ceil(($weather->Results->data->weather[0]->tempMinC*9)/5 + 32);
  88. $temp->Max = ceil(($weather->Results->data->weather[0]->tempMaxC*9)/5 + 32);
  89. }
  90.  
  91. /* Set Session Variables */
  92. $_SESSION['weatherCode'] = $weather->Code;
  93. $_SESSION['weatherDescription'] = $weather->Description;
  94. $_SESSION['currentTemp'] = $temp->Current;
  95. $_SESSION['minTemp'] = $temp->Min;
  96. $_SESSION['maxTemp'] = $temp->Max;
  97.  
  98. /* Response Array */
  99. $results = Array('weather' => Array('code' => $weather->Code, 'description' => $weather->Description, 'current' => $temp->Current, 'min' => $temp->Min, 'max' => $temp->Max));
  100. return $results;
  101. }
  102.  
  103. /* Reformat Weather */
  104. private function reformatWeather($info) {
  105. /* Codes of descriptions that need to be reformated because of their length */
  106. $descriptionCodes = array("185","200","314","356","365","371","377","386","389","392","395");
  107.  
  108. /* Codes of description that need to be reformated depending on the time of day */
  109. $weatherCodes = array("113","116","119","299");
  110.  
  111.  
  112. /* Check if provided code is in array, if so modify description */
  113. if (in_array($info->Code, $descriptionCodes)) {
  114. if ($info->Code == "185") { $info->Description = "Patchy freezing drizzle"; }
  115. elseif ($info->Code == "200") { $info->Description = "Thundery outbreaks"; }
  116. elseif ($info->Code == "314") { $info->Description = "Moderate freezing rain"; }
  117. elseif ($info->Code == "356") { $info->Description = "Moderate rain shower"; }
  118. elseif ($info->Code == "365") { $info->Description = "Moderate sleet showers"; }
  119. elseif ($info->Code == "371") { $info->Description = "Moderate snow showers"; }
  120. elseif ($info->Code == "377") { $info->Description = "Moderate ice pellets showers"; }
  121. elseif ($info->Code == "386") { $info->Description = "Light right with thunder"; }
  122. elseif ($info->Code == "389") { $info->Description = "Moderate rain with thunder"; }
  123. elseif ($info->Code == "392") { $info->Description = "Light snow with thunder"; }
  124. elseif ($info->Code == "395") { $info->Description = "Moderate snow with thunder"; }
  125. }
  126.  
  127.  
  128. /* Define weather the icon needs a _1 after */
  129. if (in_array($info->Code,$weatherCodes)) {
  130. if (self::isDay($info->lat,$info->lng) == "night") {
  131. if ($info->Description == "Sunny") { $info->Description = "Clear"; }
  132. $info->Code = $info->Code."_1";
  133. }
  134. }
  135.  
  136. /* Return Results */
  137. return $info;
  138. }
  139.  
  140. /* Sunset / Sunrise Function */
  141. private function isDay($lat,$lng,$timezone = "GMT") {
  142. /* Define Sunset / Sunrise by Longitude and Latitude */
  143. date_default_timezone_set($timezone);
  144. $now = time();
  145. $zenith = 96;
  146. $offset = 0;
  147. $sunrise = date_sunrise($now, SUNFUNCS_RET_TIMESTAMP, $lat, $lng, $zenith, $offset);
  148. $sunset = date_sunset($now, SUNFUNCS_RET_TIMESTAMP, $lat, $lng, $zenith, $offset);
  149.  
  150. /* Define whether it is day or night */
  151. $isDay = ($sunrise < $now) && ($now < $sunset);
  152. if ($sunrise > $sunset) {
  153. $isDay = !$isDay;
  154. }
  155. $timeOfDay = $isDay ? "day" : "night";
  156.  
  157. /* Return Time Of Day */
  158. return $timeOfDay;
  159.  
  160. }
  161.  
  162. /* Transform Array To Object */
  163. private function toObject($array) {
  164. /* Build Object */
  165. $object = null;
  166. foreach ($array as $key => $value) {
  167. $object->$key = $value;
  168. }
  169.  
  170. /* Return Results */
  171. return $object;
  172. }
  173.  
  174. /* Transform Array To Object */
  175. private function toArray($object) {
  176. /* Build Object */
  177. $array = null;
  178. foreach ($object as $key => $value) {
  179. $array[$key] = $value;
  180. }
  181.  
  182. /* Return Results */
  183. return $array;
  184. }
  185. }
  186. ?>
  187.  
  188. <?php
  189. class User extends WL {
  190.  
  191. /* Public Variables */
  192. public $uid,
  193. $email,
  194. $firstname,
  195. $lastname,
  196. $birthday,
  197. $gender,
  198. $addtime,
  199. $logintime,
  200. $lastactivity,
  201. $verified,
  202. $status,
  203. $units,
  204. $privacy,
  205. $location,
  206. $cookie;
  207.  
  208. /* User Model */
  209. private $userModel = Array('uid,email,firstname,lastname,birthday,gender,addtime,logintime,lastactivity,verified,status,units,privacy,location,cookie,tempUnit,distanceUnit');
  210.  
  211. /* Build User Object */
  212. public function __construct() {
  213. /* Restore Session if Authenticated */
  214. if ($this->LoggedIn()) { $this->RestoreAccess(); }
  215. }
  216.  
  217. /* Add User Function */
  218. public function AddUser($input) {
  219. /* Global Variables */
  220. global $config,$db,$UI;
  221.  
  222. /* Validate User Input */
  223. if(!$input->firstname) { $err = "Firstname field should not be empty."; }
  224. elseif(!preg_match("/^(?i)[a-z]*$/", $input->firstname)){ $err = "First Name can only contain letters."; }
  225. elseif(strlen($input->firstname) < 3){ $err = "First Name should be at least 3 characters long"; }
  226. elseif(!$input->lastname) { $err = "Lastname field should not be empty."; }
  227. elseif(!preg_match("/^(?i)[a-z]*$/", $input->lastname)){ $err = "Last Name can only contain letters."; }
  228. elseif(strlen($input->lastname) < 3){ $err = "Last Name should be at least 3 characters long"; }
  229. elseif(!$input->password) { $err = "Password should not be empty."; }
  230. elseif(strlen($input->password) < 6){ $err = "Password should be at least 6 characters."; }
  231. elseif($input->password != $input->retype) { $err = "Retype password does not match."; }
  232. elseif(!$input->month) { $err = "You must enter your birthdate. (Month)"; }
  233. elseif(!$input->day) { $err = "You must enter your birthdate. (Day)"; }
  234. elseif(!$input->year) { $err = "You must enter your birthdate. (Year)"; }
  235. elseif(!checkdate($input->month, $input->day, $input->year)) { $err = "You must enter a valid birthdate."; }
  236. elseif(!$input->gender) { $err = "You must define your gender."; }
  237. elseif(!$input->email) { $err = "E-Mail field should not be empty."; }
  238. elseif(!checkEmail($input->email)) { $err = "Invalid Email format."; }
  239. elseif(self::Exist($input->email)) { $err = "Another account has already been created with this e-mail address."; }
  240.  
  241. /* If No Error Add User to Database */
  242. if (!$err) {
  243. /* Format Birthday */
  244. $birthDay = $input->year."-".$input->month."-".$input->day;
  245.  
  246. /* Build Query & Insert user in database */
  247. $query = "
  248. INSERT INTO users SET
  249. email='{$input->email}',
  250. gender='{$input->gender}',
  251. birthday='{$birthDay}',
  252. firstname='{$input->firstname}',
  253. lastname='{$input->lastname}',
  254. password='".md5($input->password)."',
  255. addtime='".time()."'
  256. ";
  257. $db->Execute($query);
  258.  
  259. /* Assign User Id */
  260. $user->Id = $db->Insert_Id();
  261.  
  262. /* Insert E-mail Verification In Database */
  263. $info->code = randString();
  264. $query = "INSERT INTO user_verification SET uid='{$user->Id}', code='{$info->code}'";
  265. $db->Execute($query);
  266.  
  267. /* Prepare e-mail notification */
  268. $info->subject = "Please verify your account.";
  269. $info->resend = 0;
  270.  
  271. /* Merge Input Object and Info Object */
  272. $info = (object) array_merge((array) $info, (array) $input);
  273.  
  274. /* Assign Variables to E-Mail Template */
  275. UI::assign('info', $info);
  276. $body.= UI::fetch(''.$UI->emailTemplate.'/verification.tpl');
  277.  
  278. /*
  279. User::mail($input->email,$config->siteName,$config->registrationEmail,$config->siteName.' - '.$subject,$body);
  280. */
  281.  
  282. /* Success Response */
  283. $response->Success = "A verification email has been sent to your address. Registration will not be complete until your e-mail address has been confirmed.";
  284. }
  285. else { $response->Error = $err; }
  286.  
  287. /* Return Response */
  288. return $response;
  289. }
  290.  
  291. /* Authentication Function */
  292. public function Auth($input) {
  293. /* Global Variables */
  294. global $db;
  295.  
  296. /* Validate User Input */
  297. if (!$input->email) { $err = "E-Mail Address field should not be empty."; }
  298. elseif (!$input->password) { $err = "Password field should not be empty."; }
  299. else {
  300. /* Query Database */
  301. $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)."'";
  302. $rs = $db->Execute($query);
  303.  
  304. /* Analyze Response */
  305. if ($rs->recordcount()) {
  306. /* Set User Information */
  307. $this->setUserInfo($rs->fields);
  308.  
  309. /* Check if User is Active & Verified */
  310. if (!$this->status) { $err = "Account Inactive. Please contact the system Administrator, for more details."; }
  311. 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."; }
  312. else {
  313. /* Update User Login Time */
  314. $query = "UPDATE users SET logintime='".time()."' WHERE uid='{$this->uid}'";
  315. $db->Execute($query);
  316.  
  317. /* Set User Cookie if 'Remember Me' Checkbox is Checked */
  318. if ($input->persistent) { $this->setCookie(); }
  319.  
  320. /* Unset User Email Input */
  321. unset($_SESSION['userEmailInput']);
  322. }
  323. }
  324. else { $err = "Invalid E-Mail/Password combination."; }
  325. }
  326.  
  327. /* Return Response */
  328. if ($err) { $response->Error = $err; }
  329. return $response;
  330. }
  331.  
  332. public function deAuth() {
  333. /* Global Variables */
  334. global $config;
  335.  
  336. /* Unset all session variables */
  337. unset($_SESSION['uid']);
  338. unset($_SESSION['referer']);
  339.  
  340. /* De-validate Cookie */
  341. setcookie($config->cookieName, '', time() - 3600);
  342. }
  343.  
  344. public function restoreAccess() {
  345. /* Set Object Variables Back */
  346. foreach($_SESSION as $key => $value) {
  347. if (in_array($key, $this->userModel)) { $this->$key = $value; }
  348. }
  349.  
  350. /* Update User Activity */
  351. $this->updateActivity();
  352.  
  353. /* User Locations */
  354. $this->locations = $this->getLocationList();
  355. }
  356.  
  357. public function setCookie() {
  358. /* Global Variables */
  359. global $config,$db;
  360.  
  361. /* Build User Cookie */
  362. $cookie->New = randString();
  363. $cookie->Name = $config->cookieName;
  364. $cookie->Domain = $_SERVER['SERVER_NAME'];
  365. $cookie->Domain = substr($domain, 1, 0);
  366.  
  367. setcookie($cookie->Name, $cookie->New, time()+31536000, '/', $cookie->Domain);
  368.  
  369. /* Update User database with new cookie value */
  370. $query = "UPDATE users SET cookie='{$cookie->New}' WHERE uid='{$this->uid}'";
  371. $db->Execute($query);
  372. }
  373.  
  374. /* Set User Infomation */
  375. private function setUserInfo($info) {
  376. /* Set Local & Session Variables of User */
  377. foreach($info as $key => $value) {
  378. $this->$key = $value;
  379. $_SESSION[$key] = $value;
  380. }
  381.  
  382. /* Assign User Units to Session Variable */
  383. if ($_SESSION['units']) {
  384. $_SESSION['distanceUnit'] = "Km";
  385. $_SESSION['tempUnit'] = "C";
  386. }
  387. else {
  388. $_SESSION['distanceUnit'] = "Mi";
  389. $_SESSION['tempUnit'] = "F";
  390. }
  391.  
  392. /* Update User Activity */
  393. $this->UpdateActivity();
  394. }
  395.  
  396. /* Return Specify User Info */
  397. public function getInfo($var) {
  398. return (!$var) ? $this : $this->var;
  399. }
  400.  
  401. /* Update user's session activity */
  402. public function UpdateActivity() {
  403. /* Global Variables */
  404. global $db;
  405.  
  406. /* Update database */
  407. $time = time();
  408. $query = "UPDATE users SET lastactivity='$time' WHERE uid='".$this->uid."'";
  409. $db->Execute($query);
  410.  
  411. /* Update Local Variable */
  412. $this->lastactivity = $time;
  413. $_SESSION['lastactivity'] = $time;
  414. }
  415.  
  416. /* Return Whether the user is logged in or not */
  417. public static function LoggedIn() {
  418. return ($_SESSION['uid']) ? true : false;
  419. }
  420.  
  421. /* User Single E-Mail Function */
  422. public function Mail($to,$name,$from,$subj,$body) {
  423. /* Global Variables */
  424. global $config;
  425.  
  426. /* Prepare Body */
  427. $content= UI::fetch(''.$UI->emailTemplate.'/emailHeader.tpl');
  428. $content.= $body;
  429. $content.= UI::fetch(''.$UI->emailTemplate.'/emailFooter.tpl');
  430.  
  431. /* Prepare HTML E-Mail Header */
  432. $headers = 'MIME-Version: 1.0' . "\r\n";
  433. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  434. $headers .= "To: <$to>"."\r\n";
  435. $headers .= "From: ".$config->siteName." <".$from.">"."\r\n";
  436.  
  437. /* Send E-Mail */
  438. mail($to,$subj,$content,$headers);
  439. }
  440.  
  441. /* Check if user exist in database */
  442. public static function Exist($email) {
  443. /* Global Variables */
  444. global $db;
  445. $results = null;
  446.  
  447. /* Query Database */
  448. $query = "SELECT uid,firstname,lastname FROM users WHERE email='$email'";
  449. $rs = $db->Execute($query);
  450.  
  451. /* Build Result Object */
  452. if ($rs->recordcount()) {
  453. foreach ($rs->fields as $key => $value) {
  454. $results->$key = $value;
  455. }
  456. }
  457.  
  458. /* Return Results */
  459. return $results;
  460. }
  461. }
  462. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement