functions.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. function objDB()
  3. {
  4. $objDB = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  5. if ($objDB->connect_error) {
  6. die("Connection not established");
  7. }
  8. return $objDB;
  9. }
  10. function upload_image($image)
  11. {
  12. if (!is_dir(APPROOT . "/images")) {
  13. mkdir(APPROOT . "/images");
  14. }
  15. if ($image["error"] == 4) {
  16. die("image file not uploaded");
  17. }
  18. if ($image["type"] != "image/png") {
  19. die("Only, png image files are allowed");
  20. }
  21. $image_info = pathinfo($image["name"]);
  22. extract($image_info);
  23. $image_convention = $filename . time() . ".$extension";
  24. if (move_uploaded_file($image["tmp_name"], APPROOT . "/images/" . $imageConvention)) {
  25. return $image_convention;
  26. } else {
  27. return false;
  28. }
  29. }
  30. function cTime($timestamp)
  31. {
  32. return date("Y-m-d H:i:s", $timestamp);
  33. }
  34. function checkUserByEmail($email)
  35. {
  36. $objDB = objDB();
  37. $stmt = $objDB->prepare(
  38. "SELECT * FROM users WHERE email=?"
  39. );
  40. $stmt->bind_param("s", $email);
  41. $stmt->execute();
  42. $stmt->store_result();
  43. return $stmt->num_rows;
  44. }
  45. function checkUserByUsername($username)
  46. {
  47. $objDB = objDB();
  48. $stmt = $objDB->prepare(
  49. "SELECT * FROM users WHERE username=?"
  50. );
  51. $stmt->bind_param("s", $username);
  52. $stmt->execute();
  53. $stmt->store_result();
  54. return $stmt->num_rows;
  55. }
  56. function checkUserActivation($username)
  57. {
  58. $objDB = objDB();
  59. $stmt = $objDB->prepare(
  60. "SELECT * FROM users WHERE username=? AND is_active=1"
  61. );
  62. $stmt->bind_param("s", $username);
  63. $stmt->execute();
  64. $stmt->store_result();
  65. return $stmt->num_rows;
  66. }
  67. function setMsg($name, $value, $class = "success")
  68. {
  69. if (is_array($value)) {
  70. $_SESSION[$name] = $value;
  71. } else {
  72. $_SESSION[$name] = "<div class='alert alert-$class text-center'>$value</div>";
  73. }
  74. }
  75. function getMsg($name)
  76. {
  77. if (isset($_SESSION[$name])) {
  78. $session = $_SESSION[$name];
  79. unset($_SESSION[$name]);
  80. return $session;
  81. }
  82. }
  83. function getUserById($user_id)
  84. {
  85. $objDB = objDB();
  86. $stmt = $objDB->prepare(
  87. "SELECT * FROM users WHERE id=?"
  88. );
  89. $stmt->bind_param("i", $user_id);
  90. $stmt->execute();
  91. $result = $stmt->get_result();
  92. return $result->fetch_object();
  93. }
  94. function verifyUserAccount($code)
  95. {
  96. $objDB = objDB();
  97. $stmt = $objDB->prepare(
  98. "UPDATE users SET is_active = 1 , reset_code = '' WHERE reset_code = ?"
  99. );
  100. $stmt->bind_param("s", $code);
  101. $stmt->execute();
  102. $stmt->store_result();
  103. return $stmt->affected_rows;
  104. }
  105. function checkUserByCode($code)
  106. {
  107. $objDB = objDB();
  108. $stmt = $objDB->prepare(
  109. "SELECT * FROM users WHERE reset_code = ?"
  110. );
  111. $stmt->bind_param("s", $code);
  112. $stmt->execute();
  113. $stmt->store_result();
  114. return $stmt->num_rows;
  115. }
  116. function isUserLoggedIn()
  117. {
  118. if (isset($_SESSION["user"]) || isset($_COOKIE["user"])) {
  119. return true;
  120. } else {
  121. return false;
  122. }
  123. }
  124. function get_userinfo()
  125. {
  126. return isUserLoggedIn() ? isset($_COOKIE["user"]) ? unserialize($_COOKIE["user"]) : $_SESSION["user"] : "";
  127. }
  128. function send_mail($detail = array())
  129. {
  130. if (!empty($detail["to"]) && !empty($detail["message"]) && !empty($detail["subject"])) {
  131. $to = $detail["to"];
  132. $totitle = isset($detail["totitle"]) ? $detail["totitle"] : "";
  133. $from = SMTP_MAILADDR;
  134. $fromtitle = isset($detail["fromtitle"]) ? $detail["fromtitle"] : "";
  135. $subject = $detail["subject"];
  136. $body = $detail["message"];
  137. $mailtype = "HTML"; // HTML/TXT
  138. $smtp = new MailSMTP(SMTP_SERVER, SMTP_PORT, true, SMTP_USERNAME, SMTP_PASSWORD);
  139. $smtp->debug = false;
  140. $res = $smtp->sendmail($to, $totitle, $from, $fromtitle, $subject, $body, $mailtype);
  141. if (!$res) {
  142. return false;
  143. } else {
  144. return true;
  145. }
  146. } else {
  147. die("Your Mail Handler requires four main paramters");
  148. }
  149. }
  150. /**
  151. * redirect to functions URL
  152. */
  153. function redirect($module, $section = "", $param = [])
  154. {
  155. $url = $param ? setRouter($module, $section) . "&" . http_build_query($param) : setRouter($module, $section);
  156. // $param = $param ? http_build_query($param) : "";
  157. // $url = $section ? setRouter($module, $section) . "&" . $param : setRouter($module) . "?" . $param;
  158. header("Location: {$url}");
  159. exit;
  160. }
  161. /** make router URL
  162. * @param mixed $module
  163. * @param mixed $section
  164. * @return string
  165. */
  166. function setRouter($module, $section = "")
  167. {
  168. return empty($section) ? "{$module}.php" : "{$module}.php?s=$section";
  169. }
  170. /** make a full path http URL
  171. * @param mixed $module
  172. * @param mixed $section
  173. * @return string
  174. */
  175. function setURL($module, $section = "")
  176. {
  177. return empty($section) ? URLROOT . "/{$module}.php" : URLROOT . "/{$module}.php?s=$section";
  178. }
  179. /** Determine if a variable is an email address
  180. *
  181. * @param string $email
  182. * @return bool
  183. */
  184. function is_email($email = "")
  185. {
  186. return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $email);
  187. }