PageRenderTime 66ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/Includes/configs/functions.php

https://bitbucket.org/leow/openautoclassifieds-14-stable
PHP | 1123 lines | 901 code | 138 blank | 84 comment | 132 complexity | e38194c5d2a366e9a07be17249658b9f MD5 | raw file
Possible License(s): GPL-2.0
  1. <?PHP
  2. //================================================================================
  3. // PLEASE DO NOT REMOVE THIS HEADER!!!
  4. //
  5. // COPYRIGHT NOTICE
  6. // This script is licensed under the GPL
  7. //
  8. // Copyright 2007-2008 Alias 454 Studios and Brandon Keep (c) All rights reserved.
  9. // Created 11/18/2007
  10. // Brandon Keep, http://www.openautoclassifieds.com
  11. // http://www.alias454studios.com/scripts/
  12. //
  13. // Last Modified 12-21-2008 by
  14. // Brandon Keep, http://alias454studios.com
  15. //================================================================================
  16. // This software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. // OTHER DEALINGS IN THE SOFTWARE.
  23. //================================================================================
  24. include_once 'env.inc.php';
  25. //Check if magic qoutes is on then stripslashes if needed
  26. function codeClean($var)
  27. {
  28. if (is_array($var)) {
  29. foreach($var as $key=>$val) {
  30. $output[$key] = codeClean($val);
  31. }
  32. } else {
  33. $var = strip_tags($var);
  34. $output = mysql_real_escape_string((get_magic_quotes_gpc())? stripslashes($var): $var);
  35. }
  36. return $output;
  37. }
  38. function viewOnPage($var)
  39. {
  40. $var = htmlentities(trim($var));
  41. if (get_magic_quotes_gpc())
  42. $var = stripslashes($var);
  43. return $var;
  44. }
  45. //Mail functions
  46. function sendEmail($ToEmail,$Subject,$Body,$From,$FromEmail)
  47. {
  48. $ver = phpversion();
  49. $Body = preg_replace("!<br \/>!","\n",$Body);
  50. $headers.="From: $From <$FromEmail>\n";
  51. $headers.="Reply-To: <$FromEmail>\n";
  52. $headers.="X-Sender: <$FromEmail>\n";
  53. $headers.="X-Mailer: PHP-$ver \n";
  54. $headers.="X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
  55. $headers.="Return-Path: <$FromEmail> \n";
  56. mail($ToEmail,$Subject,wordwrap($Body),$headers);
  57. }
  58. //check contact us form for submission errors
  59. function checkSubmitForm($from_email,$from_name,$subject,$msg,$captcha,$security_code)
  60. {
  61. if (!validateEmail($from_email)){
  62. return 1;
  63. } elseif (empty($from_name)) {
  64. return 2;
  65. } elseif (empty($subject)) {
  66. return 3;
  67. } elseif (empty($msg)) {
  68. return 4;
  69. } elseif (!empty($security_code) && $security_code !== "$captcha") {
  70. return 5;
  71. } elseif (empty($captcha)) {
  72. return 6;
  73. } else {
  74. return 99;
  75. }
  76. }
  77. // function to check the referer for security reasons.
  78. function checkReferer($referers)
  79. {
  80. $referer = getenv("HTTP_REFERER");
  81. list($remove,$stuff) = split('//',$referer,2);
  82. list($home,$stuff) = split('/',$stuff,2);
  83. for ($x = 0; $x < count($referers); $x++) {
  84. if (preg_match("!$referers[$x]!","$home")) {
  85. //print "$home <-home $referer <-referer";
  86. return true;
  87. }
  88. }
  89. //if you get this far you have not met the criteria and will be redirected
  90. //if someone comes from a place other then in our referers list
  91. //set them in the right spot on our domain
  92. if (empty($referer)) {
  93. header("Location: index.php");
  94. $time = date('Y-m-d h:i');
  95. error_log(" $time Empty Referer. '".getenv("REMOTE_ADDR")."' \r\n", 3, "error.txt");
  96. return false;
  97. } else {
  98. header("Location: index.php");
  99. $time = date('Y-m-d h:i');
  100. error_log(" $time Illegal Referer. '".getenv("HTTP_REFERER")."' \r\n", 3, "error.txt");
  101. return false;
  102. }
  103. }
  104. //Login functions
  105. function verifyLogin($user,$pass)
  106. {
  107. //Encrypt password for database verification
  108. $salt = 's+(_a*';
  109. $pass = md5($pass.$salt);
  110. $sql = "SELECT pass FROM users WHERE pass = '" . $pass . "' AND user = '" . $user ."'";
  111. $res = mysql_query($sql);
  112. $num = mysql_num_rows($res);
  113. if ($num > 0)
  114. return true;
  115. return false;
  116. }
  117. //Page auth function
  118. function checkPrivs()
  119. {
  120. if (!empty($_SESSION["admin"])){
  121. $status = 'admin';
  122. return $status;
  123. } elseif (!empty($_SESSION["seller"])){
  124. $status = 'seller';
  125. return $status;
  126. } else {
  127. $status = 'user';
  128. return $status;
  129. }
  130. return false;
  131. }
  132. function checkIfAdmin($user,$pass)
  133. {
  134. $sql = "SELECT pass FROM users WHERE pass = '" . $pass . "' AND user = '" . $user ."' AND user_level = 9 ";
  135. $res = mysql_query($sql);
  136. $num = mysql_num_rows($res);
  137. if ($num > 0)
  138. return true;
  139. return false;
  140. }
  141. function checkIfSeller($user,$pass)
  142. {
  143. $sql = "SELECT pass FROM users WHERE pass = '" . $pass . "' AND user = '" . $user ."' AND user_level = 2 ";
  144. $res = mysql_query($sql);
  145. $num = mysql_num_rows($res);
  146. if ($num > 0)
  147. return true;
  148. return false;
  149. }
  150. function verifyCookie($user,$pass)
  151. {
  152. $sql = "SELECT pass FROM users WHERE pass = '" . $pass . "' AND user = '" . $user ."'";
  153. $res = mysql_query($sql);
  154. $num = mysql_num_rows($res);
  155. if ($num > 0)
  156. return true;
  157. return false;
  158. }
  159. function logoff()
  160. {
  161. //mysql_query("DELETE FROM onlineusers WHERE user = '" . $_SESSION["username"] . "'");
  162. // kill session variables
  163. unset($_SESSION["user"]);
  164. unset($_SESSION["pass"]);
  165. unset($_SESSION["logged_in"]);
  166. unset($_SESSION["admin"]);
  167. session_destroy();
  168. setcookie("user", NULL, time()-3600);
  169. setcookie("pass", NULL, time()-3600);
  170. setcookie("sessid", NULL, time()-3600);
  171. // redirect them to anywhere you like.
  172. header("Location: index.php");
  173. }
  174. //Update account functions
  175. function getUserRecords($user)
  176. {
  177. $sql = "SELECT * FROM users WHERE user = '" . $user . "'";
  178. $res = mysql_query($sql);
  179. $c=0;
  180. while ($a_row = mysql_fetch_array($res)) {
  181. $records[$c]["id"] = $a_row["id"];
  182. $records[$c]["email"] = $a_row["email"];
  183. $records[$c]["user"] = $a_row["user"];
  184. $records[$c]["first_name"] = $a_row["first_name"];
  185. $records[$c]["last_name"] = $a_row["last_name"];
  186. $records[$c]["phone"] = $a_row["phone"];
  187. $records[$c]["alt_phone"] = $a_row["alt_phone"];
  188. $records[$c]["fax"] = $a_row["fax"];
  189. $records[$c]["image"] = $a_row["image"];
  190. $records[$c]["address"] = $a_row["address"];
  191. $records[$c]["city"] = $a_row["city"];
  192. $records[$c]["state"] = $a_row["state"];
  193. $records[$c]["zip"] = $a_row["zip"];
  194. $records[$c]["reg_date"] = $a_row["reg_date"];
  195. $records[$c]["image"] = $a_row["image"];
  196. $c++;
  197. }
  198. return $records;
  199. }
  200. function updateUser($user, $email, $first_name, $last_name, $phone, $alt_phone, $fax, $address, $city, $state, $zip)
  201. {
  202. if (!validateEmail($email)) {
  203. return 1;
  204. } elseif (!validatePhone($phone)) {
  205. return 2;
  206. } elseif (!validateName($first_name)) {
  207. return 3;
  208. } elseif (!validateName($last_name)) {
  209. return 4;
  210. } else {
  211. // Get remote IP
  212. $ipaddress = getenv('REMOTE_ADDR');
  213. $sql = "UPDATE users SET ipaddress = INET_ATON('" . $ipaddress . "'), email = '" . $email . "', first_name = '" . $first_name . "', last_name = '" . $last_name . "', phone = '" . $phone . "', alt_phone = '" . $alt_phone . "', fax = '" . $fax . "', address = '". $address . "', city = '". $city . "', state = '". $state . "', zip = '". $zip . "' WHERE user = '" . $user . "'";
  214. $res = mysql_query($sql);
  215. return 99;
  216. }
  217. }
  218. //Reset password functions
  219. function updatePass($user,$pass)
  220. {
  221. //Encrypt password for database
  222. $salt = 's+(_a*';
  223. $new_password = md5($pass.$salt);
  224. //if user logged in change their session password
  225. if (isset($_SESSION["pass"])) {
  226. $_SESSION["pass"] = "$new_password";
  227. }
  228. //if remember me function already set
  229. //change cookie for remember me
  230. if (isset($_COOKIE["pass"])) {
  231. setcookie("pass", "$new_password", time() + (60*60*24*30));
  232. }
  233. //perform query and update user info in the database
  234. $sql = "UPDATE users SET pass = '" . $new_password . "' WHERE user = '" . $user . "'";
  235. $res = mysql_query($sql);
  236. }
  237. function generatePassword($len)
  238. {
  239. $password = "";
  240. $char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  241. $count=0;
  242. while ($count <= $len) {
  243. $random = rand(1,strlen($char));
  244. $password.=substr($char,$random -1,1);
  245. $count++;
  246. }
  247. //echo $password;
  248. return $password;
  249. }
  250. //Registration functions
  251. function checkIfUser($user)
  252. {
  253. $sql = "SELECT user FROM users WHERE user = '" . $user ."' ";
  254. $res = mysql_query($sql);
  255. $num = mysql_num_rows($res);
  256. if ($num > 0)
  257. return true;
  258. return false;
  259. }
  260. function checkIfEmail($email)
  261. {
  262. $sql = "SELECT COUNT(*) as NUMBER FROM users WHERE email = '" . $email ."' ";
  263. $res = mysql_query($sql);
  264. $num = mysql_result($res,0,"NUMBER");
  265. if ($num > 0)
  266. return true;
  267. return false;
  268. }
  269. function validatePhone($phone)
  270. {
  271. if (preg_match('!^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$!', $phone))
  272. return true;
  273. return false;
  274. }
  275. function validateName($name)
  276. {
  277. if (preg_match('!^([a-zA-Z]{3,60})$!', $name))
  278. return true;
  279. return false;
  280. }
  281. function validateUsername($user)
  282. {
  283. if (preg_match('!^\w+$!', $user))
  284. return true;
  285. return false;
  286. }
  287. function validateEmail($email)
  288. {
  289. if (preg_match("!^[a-zA-Z0-9]+([_\\.-][a-zA-Z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,4}$!", $email))
  290. return true;
  291. return false;
  292. }
  293. function registerUser($user, $pass, $email, $first_name, $last_name, $phone, $alt_phone, $fax, $address, $city, $state, $zip, $agree)
  294. {
  295. global $admin_name;
  296. global $admin_email;
  297. global $site_url;
  298. if (checkIfUser($user)) {
  299. return 1;
  300. } elseif (!validateEmail($email)) {
  301. return 2;
  302. } elseif (!validateUsername($user)) {
  303. return 3;
  304. } elseif (checkIfEmail($email)) {
  305. return 4;
  306. } elseif (empty($agree)) {
  307. return 5;
  308. } elseif (!validatePhone($phone)) {
  309. return 6;
  310. } elseif (!validateName($first_name)) {
  311. return 7;
  312. } elseif (!validateName($last_name)) {
  313. return 8;
  314. } else {
  315. //if blank password one is generated then the details are emailed
  316. if (empty($pass)) {
  317. $pass = generatePassword(6);
  318. //build email to be sent from lang file
  319. $body = preg_replace("!%USERNAME%!","$user",ACCT_SIGNUP_BODY);
  320. $body = preg_replace("!%PASSWORD%!","$pass", $body);
  321. $body = preg_replace("!%URL%!","$site_url/login.php", $body);
  322. $subject = preg_replace("!%URL%!","$site_url",ACCT_SIGNUP_SUBJECT);
  323. $subject = preg_replace("!%USERNAME%!","$user", $subject);
  324. sendEmail($email,$subject,$body,$admin_name,$admin_email);
  325. }
  326. // Get remote IP
  327. $ipaddress = getenv('REMOTE_ADDR');
  328. $reg_date = date("Y-m-d H:i:s");
  329. //Encrypt password for database
  330. $salt = 's+(_a*';
  331. $pass = md5($pass.$salt);
  332. $sql = "INSERT INTO users (ipaddress,user,pass,email,first_name,last_name,phone,alt_phone,fax,address,city,state,zip,reg_date) VALUES (INET_ATON('" . $ipaddress . "'), '" . $user . "','" . $pass . "', '" . $email . "', '" . $first_name . "', '" . $last_name . "', '" . $phone . "', '" . $alt_phone . "', '" . $fax . "', '" . $address . "', '" . $city . "', '" . $state . "', '" . $zip . "', '" . $reg_date . "')";
  333. $res = mysql_query($sql);
  334. return 99;
  335. }
  336. }
  337. function lastActive($user)
  338. {
  339. $last_active = date("Y-m-d H:i:s");
  340. $sql = "UPDATE users SET last_active = '" . $last_active . "' WHERE user = '" . $user . "' ";
  341. $res = mysql_query($sql);
  342. }
  343. //Admin Functions
  344. function updateEnv($url, $admin_name, $admin_email, $site_mode, $listings_per_page, $users_per_page, $listings_stored_path, $users_stored_path, $site_name, $description, $keywords, $site_lang, $site_template)
  345. {
  346. $sql = "UPDATE env_settings SET site_url = '" . $url . "', admin_name = '" . $admin_name . "', admin_email = '" . $admin_email . "', site_mode = '" . $site_mode . "', listings_per_page = " . $listings_per_page . ", users_per_page = " . $users_per_page . ", listings_stored_path = '" . $listings_stored_path . "', users_stored_path = '" . $users_stored_path . "', site_name = '" . $site_name . "', description = '" . $description . "', keywords = '" . $keywords . "', site_lang = '" . $site_lang . "', site_template = '" . $site_template . "' ";
  347. $res = mysql_query($sql);
  348. return 99;
  349. }
  350. function deleteUser($id)
  351. {
  352. $sql = "DELETE FROM users WHERE id = " . $id . "";
  353. $res = mysql_query($sql);
  354. return 99;
  355. }
  356. function updateUserDetails($user, $email, $first_name, $last_name, $phone, $alt_phone, $fax, $address, $city, $state, $zip, $user_level, $admin_notes, $id)
  357. {
  358. $sql = "UPDATE users SET user = '" . $user . "', email = '" . $email . "', first_name = '" . $first_name . "', last_name = '" . $last_name . "', phone = '" . $phone . "', alt_phone = '" . $alt_phone . "', fax = '" . $fax . "', address = '" . $address . "', city = '" . $city . "', state = '" . $state . "', zip = '" . $zip . "', user_level = '" . $user_level . "', admin_notes = '" . $admin_notes . "' WHERE id = " . $id . "";
  359. $res = mysql_query($sql);
  360. return 99;
  361. }
  362. //function to convert from INET_ATON http://www.ipligence.com/en/faq/
  363. //select INET_NTOA('3515134258');
  364. function ipConvert($ip)
  365. {
  366. $b = array(0,0,0,0);
  367. $c = 16777216.0;
  368. $ip += 0.0;
  369. for ($i = 0; $i < 4; $i++) {
  370. $k = (int)($ip / $c);
  371. $ip -= $c * $k;
  372. $b[$i]= $k;
  373. $c /=256.0;
  374. }
  375. $d = join('.', $b);
  376. return $d;
  377. }
  378. function getUserDetails($id)
  379. {
  380. if (!empty($id) && $id == "all") {
  381. $sql = "SELECT * FROM users";
  382. } else {
  383. $sql = "SELECT * FROM users WHERE id = " . $id . "";
  384. }
  385. $res = mysql_query($sql);
  386. $c=0;
  387. while ($a_row = mysql_fetch_array($res)) {
  388. $records[$c]["id"] = $a_row["id"];
  389. $records[$c]["ipaddress"] = ipConvert($a_row["ipaddress"]);
  390. $records[$c]["user"] = $a_row["user"];
  391. $records[$c]["email"] = $a_row["email"];
  392. $records[$c]["first_name"] = $a_row["first_name"];
  393. $records[$c]["last_name"] = $a_row["last_name"];
  394. $records[$c]["phone"] = $a_row["phone"];
  395. $records[$c]["alt_phone"] = $a_row["alt_phone"];
  396. $records[$c]["fax"] = $a_row["fax"];
  397. $records[$c]["address"] = $a_row["address"];
  398. $records[$c]["city"] = $a_row["city"];
  399. $records[$c]["state"] = $a_row["state"];
  400. $records[$c]["zip"] = $a_row["zip"];
  401. $records[$c]["reg_date"] = $a_row["reg_date"];
  402. $records[$c]["last_active"] = $a_row["last_active"];
  403. $records[$c]["user_level"] = $a_row["user_level"];
  404. $records[$c]["notes"] = $a_row["notes"];
  405. $records[$c]["image"] = $a_row["image"];
  406. $records[$c]["admin_notes"] = $a_row["admin_notes"];
  407. $c++;
  408. }
  409. if (!empty($id) && $id == "all") {
  410. //smarty paginate class used for users list in admin and also vehicle listings
  411. $paginate = new SmartyPaginate();
  412. $paginate->setTotal(count($records));
  413. return array_slice($records, $paginate->getCurrentIndex(),
  414. $paginate->getLimit());
  415. } elseif (isset($records)) {
  416. return $records;
  417. }
  418. }
  419. function checkImageSize($tmpfile, $max)
  420. {
  421. //check the tmpimage file size and see if it is to big returns true if to large
  422. $size = filesize($tmpfile);
  423. if ($size > $max)
  424. return true;
  425. return false;
  426. }
  427. function checkAllowedExt($file)
  428. {
  429. //check file for allowed extensions returns true if wrong type
  430. $temp = strtolower($file);
  431. $ext_split = split("\.",$temp);
  432. $ext = $ext_split[1];
  433. $allowed = array('gif', 'jpg', 'jpeg', 'png');
  434. if (!in_array($ext, $allowed))
  435. return true;
  436. return false;
  437. }
  438. function deleteUserImage($user)
  439. {
  440. //look up old image path then remove the file before preceding with the new image upload
  441. $sql = "SELECT image FROM users WHERE user = '" . $user . "'";
  442. $res = mysql_query($sql);
  443. $del = mysql_result($res,0,"image");
  444. if ($del != "") {
  445. $ext_split = split("\.",$del);
  446. $ext = $ext_split[1];
  447. $base = $ext_split[0];
  448. unlink("$del");
  449. unlink("$base" . "_thumb" . "." . "$ext");
  450. $sql = "UPDATE users SET image = '' WHERE user = '" . $user . "'";
  451. $res = mysql_query($sql);
  452. return true;
  453. }
  454. return false;
  455. }
  456. function openImage($file)
  457. {
  458. // Get extension and return it
  459. $temp = strtolower($file);
  460. $ext_split = split("\.",$temp);
  461. $ext = $ext_split[1];
  462. switch($ext) {
  463. case 'jpg':
  464. case 'jpeg':
  465. $im = @imagecreatefromjpeg($file);
  466. break;
  467. case 'gif':
  468. $im = @imagecreatefromgif($file);
  469. break;
  470. case 'png':
  471. $im = @imagecreatefrompng($file);
  472. break;
  473. default:
  474. $im = false;
  475. break;
  476. }
  477. return $im;
  478. }
  479. function createThumb($file, $ext, $width)
  480. {
  481. $im = openImage($file);
  482. $old_x = imageSX($im);
  483. $old_y = imageSY($im);
  484. $new_w = (int)($width);
  485. if (($new_w <= 0) or ($new_w>$old_x)) {
  486. $new_w=$old_x;
  487. }
  488. $new_h = ($old_x*($new_w/$old_x));
  489. if ($old_x > $old_y) {
  490. $thumb_w = $new_w;
  491. $thumb_h = $old_y*($new_h/$old_x);
  492. }
  493. if ($old_x < $old_y) {
  494. $thumb_w = $old_x*($new_w/$old_y);
  495. $thumb_h = $new_h;
  496. }
  497. if ($old_x == $old_y) {
  498. $thumb_w = $new_w;
  499. $thumb_h = $new_h;
  500. }
  501. $thumb = ImageCreateTrueColor($thumb_w,$thumb_h);
  502. if ($ext == 'png' || 'PNG') {
  503. imagealphablending($thumb, false);
  504. $colorTransparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
  505. imagefill($thumb, 0, 0, $colorTransparent);
  506. imagesavealpha($thumb, true);
  507. } elseif ($ext == 'gif' || 'GIF') {
  508. $trnprt_indx = imagecolortransparent($im);
  509. if ($trnprt_indx >= 0) {
  510. //its transparent
  511. $trnprt_color = imagecolorsforindex($im, $trnprt_indx);
  512. $trnprt_indx = imagecolorallocate($thumb, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  513. imagefill($thumb, 0, 0, $trnprt_indx);
  514. imagecolortransparent($thumb, $trnprt_indx);
  515. }
  516. }
  517. imagecopyresampled($thumb,$im,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
  518. //choose which image program to use
  519. if ($ext == 'jpeg' || 'jpg' || 'JPEG' || 'JPG') {
  520. imagejpeg($thumb,$file,75);
  521. } elseif ($ext == 'png' || 'PNG') {
  522. imagepng($thumb,$file,75);
  523. } elseif ($ext == 'gif' || 'GIF') {
  524. imagegif($thumb,$file,75);
  525. }
  526. imagedestroy($thumb);
  527. }
  528. function moveUploadImage($path, $file, $tmpfile, $max, $user)
  529. {
  530. //upload your image and give it a random name so no conflicts occour
  531. $rand = mt_rand(1,3000);
  532. $save_path = $path . $user . $rand . $file;
  533. //move the temp file to the proper place
  534. if (move_uploaded_file($tmpfile, $save_path)) {
  535. $ext_split = split("\.",$save_path);
  536. $ext = $ext_split[1];
  537. $base = $ext_split[0];
  538. copy($save_path, "$base" . "_thumb" . "." . "$ext");
  539. createThumb("$base" . "_thumb" . "." . "$ext", $ext, 150);
  540. createThumb("$base" . "." . "$ext", $ext, 350);
  541. //chmod("$base" . "_thumb" . "." . "$ext", 0644);
  542. //chmod("$base" . "." . "$ext", 0644);
  543. return $save_path;
  544. }
  545. return false;
  546. }
  547. //upload the images for the members page
  548. function uploadUserImage($path, $file, $tmpfile, $max, $user)
  549. {
  550. if (empty($file))
  551. return false;
  552. if (checkImageSize($tmpfile, $max))
  553. return 1;
  554. if (checkAllowedExt($file))
  555. return 2;
  556. //look up old image path then remove the file before preceding with the new image upload
  557. $sql = "SELECT image FROM users WHERE user = '" . $user . "'";
  558. $res = mysql_query($sql);
  559. $del = mysql_result($res,0,"image");
  560. if (!empty($del)) {
  561. $ext_split = split("\.",$del);
  562. $ext = $ext_split[1];
  563. $base = $ext_split[0];
  564. unlink("$del");
  565. unlink("$base" . "_thumb" . "." . "$ext");
  566. }
  567. $save_path = moveUploadImage($path, $file, $tmpfile, $max, $user);
  568. if (isset($save_path)) {
  569. $sql = "UPDATE users SET image = '" . $save_path . "' WHERE user = '" . $user . "'";
  570. $res = mysql_query($sql);
  571. return 99;
  572. }
  573. return false;
  574. }
  575. //upload the images for the listings pages
  576. function uploadListImage($path, $file, $tmpfile, $max, $listingid, $user, $owner)
  577. {
  578. if (empty($file))
  579. return false;
  580. if (checkImageSize($tmpfile, $max))
  581. return 1;
  582. if (checkAllowedExt($file))
  583. return 2;
  584. //see if listing already has main image set if not set image as main
  585. $sql = "SELECT COUNT(*) as NUMBER FROM listimages WHERE listingid = " . $listingid ." AND mainimage = 1";
  586. $res = mysql_query($sql);
  587. $num = mysql_result($res,0,"NUMBER");
  588. $save_path = moveUploadImage($path, $file, $tmpfile, $max, $user);
  589. $ext_split = split("\.",$save_path);
  590. $ext = $ext_split[1];
  591. $base = $ext_split[0];
  592. $save_thumb_path = "$base" . "_thumb" . "." . "$ext";
  593. if ($num > 0) {
  594. if (isset($save_path)) {
  595. $sql = "INSERT INTO listimages (imagepath,imagethumbpath,listingid,owner) VALUES ('" . $save_path . "','" . $save_thumb_path . "', " . $listingid . ", '" . $owner . "')";
  596. $res = mysql_query($sql);
  597. return 99;
  598. }
  599. } else {
  600. if (isset($save_path)) {
  601. $sql = "INSERT INTO listimages (imagepath,imagethumbpath,mainimage,listingid,owner) VALUES ('" . $save_path . "','" . $save_thumb_path . "',1, " . $listingid . ", '" . $owner . "')";
  602. $res = mysql_query($sql);
  603. return 99;
  604. }
  605. }
  606. return false;
  607. }
  608. function markImageMain($listingid, $imageid, $owner)
  609. {
  610. if (isset($listingid) && isset($imageid) && !empty($owner)) {
  611. //check if already main and unset
  612. $sql = "SELECT * FROM listimages WHERE listingid = " . $listingid . " AND owner = '" . $owner . "' AND mainimage = 1";
  613. $res = mysql_query($sql);
  614. $num = mysql_num_rows($res);
  615. if (!empty($num)) {
  616. $setimageid = mysql_result($res,0,"id");
  617. $sql = "UPDATE listimages SET mainimage = 0 WHERE id = " . $setimageid . "";
  618. $res = mysql_query($sql);
  619. } else {
  620. //return false;
  621. // Possibly, this happens when mainimage is deleted
  622. // Correct approach would be to mark this image as main!
  623. $sql = "UPDATE listimages SET mainimage = 1 WHERE id = " . $imageid . "";
  624. $res = mysql_query($sql);
  625. if (!$res)
  626. return false;
  627. return 99;
  628. }
  629. $sql = "UPDATE listimages SET mainimage = 1 WHERE id = " . $imageid . "";
  630. $res = mysql_query($sql);
  631. return 99;
  632. }
  633. }
  634. function deleteFullListing($listingid)
  635. {
  636. $sql = "DELETE FROM listings WHERE id = " . $listingid . "";
  637. $res = mysql_query($sql);
  638. $sql = "SELECT * FROM listimages WHERE listingid = " . $listingid . "";
  639. $res = mysql_query($sql);
  640. $c=0;
  641. while ($a_row = mysql_fetch_array($res)) {
  642. if (!empty($a_row["imagepath"]))
  643. unlink($a_row["imagepath"]);
  644. if (!empty($a_row["imagethumbpath"]))
  645. unlink($a_row["imagethumbpath"]);
  646. $c++;
  647. }
  648. $sql = "DELETE FROM listimages WHERE listingid = " . $listingid . "";
  649. $res = mysql_query($sql);
  650. return 99;
  651. }
  652. function deleteListingsImage($id, $imageid, $owner)
  653. {
  654. if (isset($id) && isset($imageid) && !empty($owner)) {
  655. //look up image path then remove the files before preceding
  656. $sql = "SELECT imagepath,imagethumbpath FROM listimages WHERE id = " . $imageid . " AND owner = '" . $owner . "' LIMIT 1";
  657. $res = mysql_query($sql);
  658. $num = mysql_num_rows($res);
  659. if (!empty($num)) {
  660. $imagepath = mysql_result($res,0,"imagepath");
  661. $imagethumbpath = mysql_result($res,0,"imagethumbpath");
  662. } else {
  663. return false;
  664. }
  665. }
  666. if (!empty($imagepath)) {
  667. unlink("$imagepath");
  668. unlink("$imagethumbpath");
  669. $sql = "DELETE FROM listimages WHERE id = " . $imageid . " AND owner = '" . $owner . "'";
  670. $res = mysql_query($sql);
  671. return 99;
  672. }
  673. return false;
  674. }
  675. function getListingTitle ($listingid)
  676. {
  677. $sql = "SELECT ad_title FROM listings WHERE id = " . $listingid . " LIMIT 1";
  678. $res = mysql_query($sql);
  679. $title = mysql_result($res,0,"ad_title");
  680. return $title;
  681. }
  682. function getRandomImage()
  683. {
  684. $limit = 6;
  685. $sql = "SELECT * FROM listimages, listings WHERE listings.id = listimages.listingid AND listings.sold !=1 AND listimages.mainimage = 1 ORDER BY RAND() LIMIT $limit";
  686. $res = mysql_query($sql);
  687. $c=0;
  688. while ($a_row = mysql_fetch_array($res)) {
  689. $records[$c]["ad_title"] = getListingTitle($a_row["listingid"]);
  690. $records[$c]["listingid"] = $a_row["listingid"];
  691. $records[$c]["imagepath"] = $a_row["imagepath"];
  692. $records[$c]["imagethumbpath"] = $a_row["imagethumbpath"];
  693. $c++;
  694. }
  695. return $records;
  696. }
  697. function getListImages($listingid)
  698. {
  699. //look up image path for listing
  700. $sql = "SELECT * FROM listimages WHERE listingid = " . $listingid . "";
  701. $res = mysql_query($sql);
  702. $c=0;
  703. while ($a_row = mysql_fetch_array($res)) {
  704. $records[$c]["id"] = $a_row["id"];
  705. $records[$c]["imagepath"] = $a_row["imagepath"];
  706. $records[$c]["imagethumbpath"] = $a_row["imagethumbpath"];
  707. $records[$c]["mainimage"] = $a_row["mainimage"];
  708. $records[$c]["listingid"] = $a_row["listingid"];
  709. $c++;
  710. }
  711. return $records;
  712. }
  713. function getListing($id='all', $addOnSQL="", $sellerid="", $sort="")
  714. {
  715. if (empty($sort))
  716. $sort = "ORDER BY sold,id DESC";
  717. if (!empty($id) && $id == "all") {
  718. $sql = "SELECT * FROM listings $sort";
  719. } elseif (!empty($id) && !empty($sellerid)) {
  720. $sql = "SELECT * FROM listings WHERE id = " . $id . " AND sellerid = '" . $sellerid . "'";
  721. } elseif (!empty($addOnSQL)){
  722. $sql = "SELECT * FROM listings WHERE $addOnSQL $sort";
  723. } else {
  724. $sql = "SELECT * FROM listings WHERE id = " . $id . " LIMIT 1";
  725. }
  726. //print "$sql";
  727. $res = mysql_query($sql);
  728. $c=0;
  729. while ($a_row = mysql_fetch_array($res)) {
  730. $records[$c]["id"] = $a_row["id"];
  731. $records[$c]["sold"] = $a_row["sold"];
  732. $records[$c]["featured"] = $a_row["featured"];
  733. $records[$c]["ad_title"] = $a_row["ad_title"];
  734. $records[$c]["make"] = $a_row["make"];
  735. $records[$c]["model"] = $a_row["model"];
  736. $records[$c]["vehicle_type"] = $a_row["vehicle_type"];
  737. $records[$c]["doors"] = $a_row["doors"];
  738. $records[$c]["color"] = $a_row["color"];
  739. $records[$c]["mileage"] = $a_row["mileage"];
  740. $records[$c]["year"] = $a_row["year"];
  741. $records[$c]["listing_condition"] = $a_row["listing_condition"];
  742. $records[$c]["engine"] = $a_row["engine"];
  743. $records[$c]["trans"] = $a_row["trans"];
  744. $records[$c]["drive_train"] = $a_row["drive_train"];
  745. $records[$c]["mpg"] = $a_row["mpg"];
  746. $records[$c]["fuel_type"] = $a_row["fuel_type"];
  747. $records[$c]["price"] = $a_row["price"];
  748. $records[$c]["adddesc"] = $a_row["adddesc"];
  749. $records[$c]["features"] = unserialize($a_row["features"]);
  750. $records[$c]["vin"] = $a_row["vin"];
  751. $records[$c]["stock"] = $a_row["stock"];
  752. $records[$c]["state"] = $a_row["state"];
  753. $records[$c]["zip"] = $a_row["zip"];
  754. $records[$c]["sellerid"] = $a_row["sellerid"];
  755. $records[$c]["seller"] = $a_row["seller"];
  756. $records[$c]["added_on"] = $a_row["added_on"];
  757. $records[$c]["last_updated"] = $a_row["last_updated"];
  758. $records[$c]["images"] = getListImages($a_row["id"]);
  759. $c++;
  760. }
  761. if (!empty($id) && $id == "all" || !empty($addOnSQL)) {
  762. //smarty paginate class used for users list in admin and also vehicle listings
  763. $paginate = new SmartyPaginate();
  764. $paginate->setTotal(count($records));
  765. if (!empty($records))
  766. return array_slice($records, $paginate->getCurrentIndex(), $paginate->getLimit());
  767. } elseif (isset($records)) {
  768. return $records;
  769. }
  770. }
  771. //add classifieds options
  772. function addOption($option, $table)
  773. {
  774. //check if empty if empty return no good
  775. if (empty($option))
  776. return false;
  777. //check if option already exists if it does return an error
  778. $sql = "SELECT COUNT(*) as NUMBER FROM $table WHERE $table = '" . $option ."' ";
  779. $res = mysql_query($sql);
  780. $num = mysql_result($res,0,"NUMBER");
  781. if ($num > 0) {
  782. return 1;
  783. //if the option is not empty and does not exist then add it
  784. } else {
  785. $sql = "INSERT INTO $table ($table) VALUES ('" . $option . "')";
  786. $res = mysql_query($sql);
  787. return 99;
  788. }
  789. }
  790. //get search list data for search page
  791. function getSearchList($table)
  792. {
  793. $sql = "SELECT DISTINCT $table FROM listings ORDER by $table";
  794. $res = mysql_query($sql);
  795. $c=0;
  796. while ($a_row = mysql_fetch_array($res)) {
  797. $searchlist[$c]["$table"] = $a_row["$table"];
  798. $c++;
  799. }
  800. return $searchlist;
  801. }
  802. //Get option information lists
  803. function getStatesList()
  804. {
  805. $sql = "SELECT * FROM states ORDER by state_name";
  806. $res = mysql_query($sql);
  807. $c=0;
  808. while ($a_row = mysql_fetch_array($res)) {
  809. $stateslist[$c]["state_prefix"] = $a_row["state_prefix"];
  810. $stateslist[$c]["state_name"] = $a_row["state_name"];
  811. $c++;
  812. }
  813. return $stateslist;
  814. }
  815. function getManufacturersList()
  816. {
  817. $sql = "SELECT * FROM manufacturer ORDER by manufacturer";
  818. $res = mysql_query($sql);
  819. $c=0;
  820. while ($a_row = mysql_fetch_array($res)) {
  821. $manufacturerslist[$c]["id"] = $a_row["id"];
  822. $manufacturerslist[$c]["manufacturer"] = $a_row["manufacturer"];
  823. $c++;
  824. }
  825. return $manufacturerslist;
  826. }
  827. function getFeaturesList()
  828. {
  829. $sql = "SELECT * FROM features ORDER by features";
  830. $res = mysql_query($sql);
  831. $c=0;
  832. while ($a_row = mysql_fetch_array($res)) {
  833. $featurelist[$c]["id"] = $a_row["id"];
  834. $featurelist[$c]["features"] = $a_row["features"];
  835. $featurelist[$c]["lists_default"] = $a_row["lists_default"];
  836. $c++;
  837. }
  838. return $featurelist;
  839. }
  840. function getDriveTrainsList()
  841. {
  842. $sql = "SELECT * FROM drive_train ORDER by drive_train";
  843. $res = mysql_query($sql);
  844. $c=0;
  845. while ($a_row = mysql_fetch_array($res)) {
  846. $drivetrainlist[$c]["id"] = $a_row["id"];
  847. $drivetrainlist[$c]["drive_train"] = $a_row["drive_train"];
  848. $c++;
  849. }
  850. return $drivetrainlist;
  851. }
  852. function getTypesList()
  853. {
  854. $sql = "SELECT * FROM vehicle_type ORDER by vehicle_type";
  855. $res = mysql_query($sql);
  856. $c=0;
  857. while ($a_row = mysql_fetch_array($res)) {
  858. $typeslist[$c]["id"] = $a_row["id"];
  859. $typeslist[$c]["vehicle_type"] = $a_row["vehicle_type"];
  860. $c++;
  861. }
  862. return $typeslist;
  863. }
  864. function getSingleOption($table, $id)
  865. {
  866. $sql = "SELECT * FROM $table WHERE id = " . $id . "";
  867. $res = mysql_query($sql);
  868. $row = mysql_fetch_row($res);
  869. if (isset($row))
  870. return $row;
  871. return false;
  872. }
  873. function updateSingleOption($table, $data, $id, $lists_default)
  874. {
  875. //check if var for default list is set
  876. if (isset($lists_default)) {
  877. $sql = "UPDATE $table SET lists_default = '" . $lists_default . "', $table = '" . $data . "' WHERE id = " . $id . "";
  878. $res = mysql_query($sql);
  879. return 99;
  880. //otherwise update without it
  881. } else {
  882. $sql = "UPDATE $table SET $table = '" . $data . "' WHERE id = " . $id . "";
  883. $res = mysql_query($sql);
  884. return 99;
  885. }
  886. }
  887. function deleteOption($table, $id)
  888. {
  889. $sql = "DELETE FROM $table WHERE id = " . $id . "";
  890. $res = mysql_query($sql);
  891. return 99;
  892. }
  893. function getSellers()
  894. {
  895. $sql = "SELECT * FROM users WHERE user_level = 2";
  896. $res = mysql_query($sql);
  897. $c=0;
  898. while ($a_row = mysql_fetch_array($res)) {
  899. $records[$c]["id"] = $a_row["id"];
  900. $records[$c]["user"] = $a_row["user"];
  901. $c++;
  902. }
  903. return $records;
  904. }
  905. function updateListing($id, $ad_title, $make, $model, $vehicle_type, $doors, $color, $mileage, $year, $listing_condition, $engine, $trans, $drive_train, $mpg, $fuel_type, $price, $adddesc, $features, $vin, $stock, $state, $zip, $sold, $sellerid, $seller)
  906. {
  907. if (empty($model))
  908. return false;
  909. if (strpos($adddesc, "&") !== false) {
  910. return false;
  911. } elseif (strlen(strip_tags($adddesc)) < strlen($adddesc)) {
  912. return false;
  913. }
  914. if (!empty($model)) {
  915. $sql = "UPDATE listings SET ad_title = '" . $ad_title . "', make = '" . $make . "', model = '" . $model . "', vehicle_type = '" . $vehicle_type . "', doors = '" . $doors . "', color = '" . $color . "', mileage = '" . $mileage . "', year = '" . $year . "', listing_condition = '" . $listing_condition . "', engine = '" . $engine . "', trans = '" . $trans . "', drive_train = '" . $drive_train . "', mpg = '" . $mpg . "', fuel_type = '" . $fuel_type . "', price = '" . $price . "', adddesc = '" . $adddesc . "', features = '" . $features . "', vin = '" . $vin . "', stock = '" . $stock . "', state = '" . $state . "', zip = '" . $zip . "', sold = '" . $sold . "', sellerid = '" . $sellerid . "', seller = '" . $seller . "' WHERE id = " . $id . "";
  916. $res = mysql_query($sql);// or die(mysql_error());
  917. return 99;
  918. }
  919. return false;
  920. }
  921. function addListing($ad_title, $make, $model, $vehicle_type, $doors, $color, $mileage, $year, $listing_condition, $engine, $trans, $drive_train, $mpg, $fuel_type, $price, $adddesc, $features, $vin, $stock, $state, $zip, $sellerid, $seller)
  922. {
  923. if (empty($model))
  924. return false;
  925. if (strpos($adddesc, "&") !== false) {
  926. return false;
  927. } elseif (strlen(strip_tags($adddesc)) < strlen($adddesc)) {
  928. return false;
  929. }
  930. $added_on = date('Y-m-d h:i');
  931. if (!empty($model)) {
  932. $sql = "INSERT INTO listings (ad_title, make, model, vehicle_type, doors, color, mileage, year, listing_condition, engine, trans, drive_train, mpg, fuel_type, price, adddesc, features, vin, stock, state, zip, sellerid, seller, added_on) VALUES ('" . $ad_title . "', '" . $make . "', '" . $model . "', '" . $vehicle_type . "', '" . $doors . "', '" . $color . "', '" . $mileage . "', '" . $year . "', '" . $listing_condition . "', '" . $engine . "', '" . $trans . "', '" . $drive_train . "', '" . $mpg . "', '" . $fuel_type . "', '" . $price . "', '" . $adddesc . "', '" . $features . "', '" . $vin . "', '" . $stock . "', '" . $state . "', '" . $zip . "', '" . $sellerid . "', '" . $seller . "', '" . $added_on . "')";
  933. $res = mysql_query($sql);// or die(mysql_error());
  934. return 99;
  935. }
  936. return false;
  937. }
  938. function getAllSellers()
  939. {
  940. $sql = "SELECT user,id,first_name,last_name FROM users WHERE user_level = 2";
  941. $res = mysql_query($sql);
  942. $c=0;
  943. while ($a_row = mysql_fetch_array($res)) {
  944. $records[$c]["id"] = $a_row["id"];
  945. $records[$c]["user"] = $a_row["user"];
  946. $records[$c]["first_name"] = $a_row["first_name"];
  947. $records[$c]["last_name"] = $a_row["last_name"];
  948. $c++;
  949. }
  950. return $records;
  951. }
  952. function getSellerId($user)
  953. {
  954. $sql = "SELECT id FROM users WHERE user = '" . $user . "' AND user_level = 2 LIMIT 1";
  955. $res = mysql_query($sql);
  956. if (!empty($res)) {
  957. $id = mysql_result($res,0,"id");
  958. return $id;
  959. } else {
  960. return false;
  961. }
  962. }
  963. function getSellerEmail($seller)
  964. {
  965. $sql = "SELECT email FROM users WHERE id = " . $seller . " AND user_level = 2 LIMIT 1";
  966. $res = mysql_query($sql);
  967. if (!empty($res)) {
  968. $email = mysql_result($res,0,"email");
  969. return $email;
  970. } else {
  971. return false;
  972. }
  973. }
  974. function getUserId($user)
  975. {
  976. $sql = "SELECT id FROM users WHERE user = '" . $user . "' LIMIT 1";
  977. $res = mysql_query($sql);
  978. if (!empty($res)) {
  979. $id = mysql_result($res,0,"id");
  980. return $id;
  981. } else {
  982. return false;
  983. }
  984. }
  985. ?>