PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/user/user_registration/user_registration.php

https://gitlab.com/mehedi-xion/Project-mehedi
PHP | 430 lines | 315 code | 89 blank | 26 comment | 58 complexity | 5a69da6618009ec01c6e3c82a4a2c59f MD5 | raw file
  1. <?php
  2. namespace ProjectMehedi\user\user_registration;
  3. use PDO;
  4. require_once '../../src/DatabaseConnection/DatabaseConnection.php';
  5. // require(dirname(__FILE__).'../../../../src/DatabaseConnection/DatabaseConnection.php');
  6. class user_registration extends \DatabaseConnection{
  7. public $id;
  8. public $username;
  9. public $firstname;
  10. public $lastname;
  11. public $fullname;
  12. public $password;
  13. public $confrm_password;
  14. public $email;
  15. public $confirm_email;
  16. public $image;
  17. public $is_admin;
  18. public $is_active;
  19. public $is_delete;
  20. public $created;
  21. public $modified;
  22. public $deleted;
  23. public $data;
  24. public $error;
  25. public function prepare($data = ""){
  26. if(!empty($data['id'])){
  27. $this->id = $data['id'];
  28. }
  29. if(!empty($data['username'])){
  30. $this->username = $data['username'];
  31. }
  32. if(!empty($data['firstname'])){
  33. $this->firstname = $data['firstname'];
  34. }
  35. if(!empty($data['lastname'])){
  36. $this->lastname = $data['lastname'];
  37. }
  38. if(!empty($data['lastname']) || !empty($data['firstname'])){
  39. $this->fullname = ucfirst($data['firstname'])." ".ucfirst($data['lastname']);
  40. }
  41. if(!empty($data['password'])){
  42. $this->password = $data['password'];
  43. }
  44. if(!empty($data['confrm_password'])){
  45. $this->confrm_password = $data['confrm_password'];
  46. }
  47. if(!empty($data['email'])){
  48. $this->email = $data['email'];
  49. }
  50. if(!empty($data['confirm_email'])){
  51. $this->confirm_email = $data['confirm_email'];
  52. }
  53. if(!empty($data['image'])){
  54. $this->image = $data['image'];
  55. }
  56. if(!empty($data['is_admin'])){
  57. $this->is_admin = $data['is_admin'];
  58. }
  59. return $this;
  60. }// prepare \\
  61. public function validate(){
  62. //username validation goes here
  63. if(!empty($this->username) && isset($this->username)){
  64. if(strlen($this->username) >= 5 && strlen($this->username) <=12){
  65. if(preg_match("/^[a-zA-Z0-9_]+$/",$this->username)){
  66. $unique_username_query = "SELECT * FROM `users` WHERE `username` = '".$this->username."'";
  67. $stmt = $this->conn -> prepare($unique_username_query);
  68. $stmt -> execute();
  69. $username_row = $stmt -> fetch(PDO::FETCH_ASSOC);
  70. if(!empty($username_row)){
  71. if($this->id != $username_row['unique_id']){
  72. $_SESSION['Uname_exists'] = "This username already exits";
  73. $this->error = TRUE;
  74. // header('location:add_user.php');
  75. }
  76. }else{ // unique username
  77. $_SESSION['username'] = $this-> username;
  78. }
  79. }else{ // preg_match only text allowed.
  80. $_SESSION['Uname_onlyText'] = "Only text and number allowed here";
  81. $this->error = TRUE;
  82. // header('location:add_user.php');
  83. }
  84. }else{ // strlen 5 to 12
  85. $_SESSION['Uname_charLength'] = "Username have to 6 to 12 charecter";
  86. $this->error = TRUE;
  87. // header('location:add_user.php');
  88. }
  89. }else{ // !empty($this->username)
  90. $_SESSION['Uname_required'] = "Username must be required";
  91. $this->error = TRUE;
  92. // header('location:add_user.php');
  93. }
  94. // Firstname validation goes here
  95. if(!empty($this->firstname) && isset($this->firstname)){
  96. if(strlen($this->firstname) <= 15){
  97. if(preg_match("/^[a-zA-Z ]*$/",$this->firstname)){
  98. $_SESSION['firstname'] = $this->firstname;
  99. }else{
  100. $_SESSION['firstname_onlyText'] = "Only text is allowed here";
  101. $this->error = TRUE;
  102. // header('location:add_user.php');
  103. }
  104. }else{ // strlen
  105. $_SESSION['firstname_charLimit'] = "Name must be within 15 charecter";
  106. $this->error = TRUE;
  107. // header('location:add_user.php');
  108. }
  109. }else{ // empty firstname
  110. $_SESSION['firstname_required'] = "You can add name later.";
  111. }
  112. // Lastname & Lastname validation goes here
  113. if(!empty($this->lastname) && isset($this->lastname)){
  114. if(strlen($this->lastname) <= 15){
  115. if(preg_match("/^[a-zA-Z ]*$/",$this->lastname)){
  116. $_SESSION['lastname'] = $this->lastname;
  117. }else{
  118. $_SESSION['lastname_onlyText'] = "Only text is allowed here";
  119. $this->error = TRUE;
  120. // header('location:add_user.php');
  121. }
  122. }else{ // strlen
  123. $_SESSION['lastname_charLimit'] = "Name must be within 15 charecter";
  124. $this->error = TRUE;
  125. // header('location:add_user.php');
  126. }
  127. }else{ // empty firstname
  128. $_SESSION['lastname_required'] = "You can add name later.";
  129. }
  130. // Password validation goes here
  131. if(!empty($this->password) && isset($this->password)){
  132. if(strlen($this->password) >=5 && strlen($this->password) <=12){
  133. if(preg_match("#[a-zA-Z ]#",$this->password)){
  134. if(preg_match("#[0-9]#",$this->password)){
  135. // $_SESSION['password'] = $this->password;
  136. }else{
  137. $_SESSION['password_digiteRequired'] = "Password must be have number";
  138. $this->error = TRUE;
  139. // header('location:add_user.php');
  140. }
  141. }else{
  142. $_SESSION['password_charRequired'] = "Password must have charecter";
  143. $this->error = TRUE;
  144. // header('location:add_user.php');
  145. }
  146. }else{ // password 5 to 12 char
  147. $_SESSION['password_charLength'] = "Password must be 5 to 12 charecter";
  148. $this->error = TRUE;
  149. // header('location:add_user.php');
  150. }
  151. }else{ // empty password and isset
  152. $_SESSION['password_required'] = "Password must be required";
  153. $this->error = TRUE;
  154. // header('location:add_user.php');
  155. }
  156. // Confirm Password validataion goes here
  157. if(!empty($this->confrm_password) && isset($this->confrm_password)){
  158. if($this->confrm_password == $this->password){
  159. // $_SESSION['confrm_password'] = $this->confrm_password;
  160. }else{
  161. $_SESSION['confirmPassword_mismatch'] = "Password have to be matched";
  162. $this->error = TRUE;
  163. // header('location:add_user.php');
  164. }
  165. }else{
  166. $_SESSION['confrmPassword_required'] = "Please retype password";
  167. $this->error = TRUE;
  168. // header('location:add_user.php');
  169. }
  170. // Email validation goes here
  171. if(!empty($this->email) && isset($this->email)){
  172. if (filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
  173. $unique_email_query = "SELECT * FROM `users` WHERE `email`= '".$this->email."'";
  174. $stmt = $this->conn->prepare($unique_email_query);
  175. $stmt -> execute();
  176. $email_row = $stmt->fetch(PDO::FETCH_ASSOC);
  177. if(!empty($email_row)){
  178. if($this->id != $email_row['unique_id']){
  179. $_SESSION['email_exists'] = "Email already exits";
  180. }
  181. }else{ // email_row emty!!
  182. $_SESSION['email'] = $this->email;
  183. }
  184. }else{ // format check of email
  185. $_SESSION['email_formateInvalid'] = "Invalid email formate";
  186. $this->error = TRUE;
  187. // header('location:add_user.php');
  188. }
  189. }else{ // empty email
  190. $_SESSION['emai_required'] = "Email must be required";
  191. $this->error = TRUE;
  192. // header('location:add_user.php');
  193. }
  194. }// validate
  195. public function add_image(){
  196. if(!empty($_FILES['image']['name']) && isset($_FILES['image'])){
  197. $this->image = uniqid().$_FILES['image']['name'];
  198. $image_type = $_FILES['image']['type'];
  199. $image_temp_location = $_FILES['image']['tmp_name'];
  200. $image_size = $_FILES['image']['size'];
  201. $require_extension = array('jpg', 'jpeg', 'gif', 'bmp', 'png');
  202. $image_extension = strtolower(end(explode('.', $this->image)));
  203. if(in_array($image_extension, $require_extension) == false){
  204. $_SESSION['ErrorImageExtension'] = 'Please give only image extension (.jpg, .png)';
  205. $error = TRUE;
  206. $this->error = TRUE;
  207. }
  208. if($image_size > 2000000){
  209. $_SESSION['ErrorImageSize'] = " Size must be below 2 MB";
  210. $error = TRUE;
  211. $this->error = TRUE;
  212. }
  213. if(empty($error) && $this->error == FALSE){
  214. move_uploaded_file($image_temp_location, "../assets/images/user/".$this->image."");
  215. // $_POST['image'] = $this->image;
  216. }
  217. }else{
  218. $_SESSION['NeedImage'] = "You can add image latter";
  219. }
  220. }// add_image \\
  221. public function addUser(){
  222. if($this->error == FALSE){
  223. try {
  224. $query = "INSERT INTO users (`id`, `unique_id`, `full_name`, `username`, `email`, `password`, `image`, `is_active`, `is_admin`, `is_delete`, `created`) VALUES (:id, :unique_id, :full_name, :username, :email, :password, :image, :is_active, :is_admin, :is_delete, :created)";
  225. $stmt = $this->conn -> prepare($query);
  226. if($stmt -> execute(array(
  227. ':id' => null,
  228. ':unique_id' => uniqid(),
  229. ':full_name' => (isset($this-> fullname) ? $this-> fullname : ''),
  230. ':username' => $this-> username,
  231. ':email' => $this-> email,
  232. ':password' => $this-> password,
  233. ':image' => $this-> image, //(isset($this-> image) ? $this-> image : ''),
  234. ':is_active' => 1,
  235. ':is_admin' => $this-> is_admin,
  236. ':is_delete' => 0,
  237. ':created' => date("Y-m-d h:i:s"),
  238. ))){
  239. $_SESSION['addSuccess'] = '<div class="alert alert-success alert-styled-left"><button data-dismiss="alert" class="close" type="button"><span>×</span><span class="sr-only">Close</span></button>User Added Successfully. Thank You.</div>';
  240. header('location:add_user.php');
  241. unset($_SESSION['username']);
  242. unset($_SESSION['firstname']);
  243. unset($_SESSION['lastname']);
  244. unset($_SESSION['email']);
  245. unset($_SESSION['confirm_email']);
  246. }
  247. } catch (Exception $e) {
  248. echo 'Error: ' . $e->getMessage();
  249. }
  250. }else{
  251. header('location:add_user.php');
  252. }
  253. }// addUser \\
  254. public function all_user(){
  255. $query = "SELECT * FROM `users` WHERE `is_delete` = 0";
  256. $stmt = $this->conn->prepare($query);
  257. $stmt -> execute();
  258. while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  259. $this->data[] = $row;
  260. }
  261. return $this->data;
  262. } // all_user \\
  263. public function single_user(){
  264. $query = "SELECT * FROM `users` WHERE `unique_id` = '".$_SESSION['logged']['unique_id']."'";
  265. $stmt = $this->conn->prepare($query);
  266. $stmt -> execute();
  267. $row = $stmt -> fetch(PDO::FETCH_ASSOC);
  268. return $row;
  269. }// Single_user\\
  270. public function update_user(){
  271. if($this->error == FALSE){
  272. try {
  273. $query = "UPDATE users SET full_name = :full_name, username = :username, email = :email, password = :password, image = :image, updated = :updated WHERE users.unique_id = :id";
  274. $stmt = $this->conn -> prepare($query);
  275. if($stmt -> execute(array(
  276. ':full_name' => (isset($this-> fullname) ? $this-> fullname : ''),
  277. ':username' => $this-> username,
  278. ':email' => $this-> email,
  279. ':password' => $this-> password,
  280. ':image' => $this-> image,
  281. ':updated' => date("Y-m-d h:i:s"),
  282. ':id' => $this-> id,
  283. ))){
  284. header("location:edit_user.php?id=$this->id");
  285. unset($_SESSION['username']);
  286. unset($_SESSION['firstname']);
  287. unset($_SESSION['lastname']);
  288. unset($_SESSION['email']);
  289. unset($_SESSION['confirm_email']);
  290. unset($_SESSION['pass']);
  291. unset($_SESSION['img']);
  292. }
  293. $_SESSION['updateSuccess'] = '<div class="alert alert-success alert-styled-left"><button data-dismiss="alert" class="close" type="button"><span>×</span><span class="sr-only">Close</span></button>User Updated Successfully. Thank You.</div>';
  294. } catch (Exception $e) {
  295. echo 'Error: ' . $e->getMessage();
  296. }
  297. }else{
  298. header("location:edit_user.php?id=$this->id");
  299. }
  300. }// update_user \\
  301. public function disable_user(){
  302. try {
  303. $query = "UPDATE users SET is_delete = :is_delete, deleted = :deleted WHERE users.unique_id = :id";
  304. $stmt = $this->conn -> prepare($query);
  305. $stmt -> execute(array(
  306. ':is_delete' => 1,
  307. ':deleted' => date("Y-m-d h:i:s"),
  308. ':id' => $this-> id,
  309. ));
  310. $_SESSION['userDisabled'] = '<div class="alert alert-success alert-styled-left"><button data-dismiss="alert" class="close" type="button"><span>×</span><span class="sr-only">Close</span></button>User Blocked Successfully. Thank You.</div>';
  311. header("location:index.php");
  312. } catch (Exception $e) {
  313. echo 'Error: ' . $e->getMessage();
  314. header("location:index.php");
  315. }
  316. }// disabled_user \\
  317. public function restore_user(){
  318. try {
  319. $query = "UPDATE users SET is_delete = :is_delete WHERE users.unique_id = :id";
  320. $stmt = $this->conn -> prepare($query);
  321. $stmt -> execute(array(
  322. ':is_delete' => 0,
  323. ':id' => $this-> id,
  324. ));
  325. $_SESSION['userRestored'] = '<div class="alert alert-success alert-styled-left"><button data-dismiss="alert" class="close" type="button"><span>×</span><span class="sr-only">Close</span></button>User Restored Successfully. Thank You.</div>';
  326. header("location:index.php");
  327. } catch (Exception $e) {
  328. echo 'Error: ' . $e->getMessage();
  329. header("location:index.php");
  330. }
  331. }// disabled_user \\
  332. public function disable_user_list(){
  333. $query = "SELECT * FROM `users` WHERE `is_delete` = 1";
  334. $stmt = $this->conn->prepare($query);
  335. $stmt -> execute();
  336. while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  337. $this->data[] = $row;
  338. }
  339. return $this->data;
  340. } // disable_user_list \\
  341. }// Class \\