├── .gitignore ├── .vscode └── launch.json ├── Login ├── changepassword.php ├── confirmationcode.php ├── login.php └── passwordRecover.php ├── README.md ├── api ├── comment │ ├── delete.php │ ├── edit.php │ └── post.php ├── follow │ ├── add.php │ ├── delete.php │ ├── get_followed_users.php │ └── get_followers.php ├── like │ └── post.php ├── messages │ ├── DELETE.php │ ├── Send.php │ ├── get_friend_messages.php │ └── message_writing_notifier │ │ ├── add.php │ │ └── delete.php ├── post │ ├── delete.php │ ├── edit.php │ ├── post.php │ └── shared │ │ └── add.php ├── user │ ├── GET.php │ └── get_by_username.php └── user_relation │ ├── accept_request.php │ ├── cancel_request.php │ ├── decline_request.php │ ├── send_request.php │ └── unfriend_relation.php ├── chat.php ├── classes ├── Common.php ├── Config.php ├── Cookie.php ├── DB.php ├── Hash.php ├── Redirect.php ├── Session.php ├── Token.php └── Validation.php ├── composer.json ├── composer.lock ├── core ├── init.php └── rest_init.php ├── database ├── chat-database-24-2-2021.sql └── chat.sql ├── functions ├── get_extension.php ├── sanitize.php ├── sanitize_id.php ├── sanitize_text.php └── unique_key_array.php ├── index.php ├── layouts ├── chat │ ├── ChatComponent.php │ ├── discussions │ │ └── get_user_discussions.php │ ├── generate_chat_container.php │ └── get_chat_friend_by_username.php ├── general │ └── CreatePost.php ├── master_right │ └── Right.php ├── post │ ├── Post.php │ ├── generate_last_post.php │ ├── generate_post.php │ ├── generate_post_creation_image.php │ ├── generate_post_creation_video.php │ └── generate_post_data_item.php └── search │ └── Search.php ├── models ├── Comment.php ├── Follow.php ├── Like.php ├── Message.php ├── Post.php ├── User.php └── UserRelation.php ├── page_parts ├── basic │ ├── header.php │ ├── log-header.php │ ├── master-left.php │ ├── master-right.php │ └── post_creator.php ├── errors │ └── 404.php ├── profile │ ├── contact-header.php │ └── owner-profile-header.php └── settings │ └── left-panel.php ├── people.php ├── post-viewer.php ├── profile.php ├── public ├── assets │ ├── audios │ │ └── tone.mp3 │ └── images │ │ ├── favicons │ │ ├── android-chrome-192x192.png │ │ ├── android-chrome-512x512.png │ │ ├── apple-touch-icon.png │ │ ├── favicon-16x16.png │ │ ├── favicon-32x32.png │ │ └── favicon.ico │ │ ├── icons │ │ ├── add-friend.png │ │ ├── black-comment.png │ │ ├── black-live.png │ │ ├── black-more.png │ │ ├── black-refresh.png │ │ ├── black-search.png │ │ ├── calendar.png │ │ ├── change-image.png │ │ ├── close.png │ │ ├── dotted-more-white.png │ │ ├── dotted-more.png │ │ ├── down-arrow.png │ │ ├── download.png │ │ ├── explore-green.png │ │ ├── explore-w.png │ │ ├── eye.png │ │ ├── follow-black.png │ │ ├── follow-user.png │ │ ├── followed.png │ │ ├── friends.png │ │ ├── full-screen-black.png │ │ ├── full-screen.png │ │ ├── group-w.png │ │ ├── group.png │ │ ├── home-green.png │ │ ├── home-w.png │ │ ├── is-friend.png │ │ ├── left-arrow.png │ │ ├── like-black-filled.png │ │ ├── like-black.png │ │ ├── like-white-filled.png │ │ ├── like-white.png │ │ ├── live-red.png │ │ ├── live-w.png │ │ ├── lock-white.png │ │ ├── lock.png │ │ ├── log.png │ │ ├── logout.png │ │ ├── media.png │ │ ├── messages.png │ │ ├── more.png │ │ ├── multimedia.png │ │ ├── no-result.png │ │ ├── notification.png │ │ ├── off-white.png │ │ ├── off.png │ │ ├── offline.png │ │ ├── on.png │ │ ├── online.png │ │ ├── page.png │ │ ├── profile-white.png │ │ ├── profile.png │ │ ├── public-white.png │ │ ├── public.png │ │ ├── received.png │ │ ├── reply-black.png │ │ ├── reply.png │ │ ├── right-arrow.png │ │ ├── save.png │ │ ├── seen.png │ │ ├── sent.png │ │ ├── settings-w.png │ │ ├── settings.png │ │ ├── timeline.png │ │ ├── unfollow-black.png │ │ ├── unfriend-black.png │ │ ├── unfriend-white.png │ │ ├── unfriend.png │ │ ├── user.png │ │ ├── video.png │ │ ├── videos.png │ │ ├── white-close.png │ │ ├── white-comment-filled.png │ │ ├── white-comment.png │ │ ├── white-eye.png │ │ ├── white-logout.png │ │ ├── white-refresh.png │ │ └── white-search.png │ │ ├── logos │ │ ├── large.png │ │ ├── large.xcf │ │ └── logo512.png │ │ └── preview.png ├── css │ ├── chat.css │ ├── create-post-style.css │ ├── error.css │ ├── global.css │ ├── header.css │ ├── index.css │ ├── log-header.css │ ├── login.css │ ├── master-left-panel.css │ ├── master-right-contacts.css │ ├── post-viewer.css │ ├── post.css │ ├── profile.css │ ├── search.css │ └── settings.css └── javascript │ ├── chat.js │ ├── config.js │ ├── global.js │ ├── index.js │ ├── master-right.js │ ├── post-viewer.js │ ├── post.js │ ├── profile.js │ ├── search.js │ └── settings.js ├── search.php ├── security ├── check_current_user.php ├── check_user_existence.php ├── generate_new_token_post.php └── get_current_user.php ├── server ├── long-polling.php ├── message_writing_notifier.php └── update_active_presence.php ├── settings-account.php ├── settings.php └── z__important_notes.txt /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | !data/users 3 | data/users/* -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | 8 | { 9 | //"debugServer": 4711, // Uncomment for debugging the adapter 10 | "name": "Listen for XDebug", 11 | "type": "php", 12 | "request": "launch", 13 | "port": 9001, 14 | "log": true 15 | }, 16 | { 17 | //"debugServer": 4711, // Uncomment for debugging the adapter 18 | "name": "Launch", 19 | "request": "launch", 20 | "type": "php", 21 | "program": "${file}", 22 | "cwd": "${C:\\xampp\\php\\ext\\php_xdebug-3.0.2-7.4-vc15-x86_64.dll}", 23 | "externalConsole": false 24 | } 25 | ] 26 | } -------------------------------------------------------------------------------- /Login/changepassword.php: -------------------------------------------------------------------------------- 1 | fetchUser("id", Session::get("u_id")); 20 | 21 | if(isset($_POST["save"])) { 22 | if(Token::check(Common::getInput($_POST, "token_password_save"), "reset-pasword")) { 23 | $validate->check($_POST, array( 24 | "password"=>array( 25 | "name"=>"Password", 26 | "required"=>true, 27 | "min"=>6 28 | /* (later) 29 | "strength"=>array( 30 | 31 | )*/ 32 | ), 33 | "password_again"=>array( 34 | "name"=>"Repeated password", 35 | "required"=>true, 36 | "matches"=>"password" 37 | ) 38 | )); 39 | 40 | if($validate->passed()) { 41 | if(Common::getInput($_POST, "email") != $user->getPropertyValue("email")) { 42 | $validate->addError("It seems that you change the email section which is not allowed !"); 43 | } else { 44 | // Now we need to generate a salt hash, and then make a hashed password using that salt 45 | $newSalt = Hash::salt(16); 46 | $newPassword = Hash::make(Common::getInput($_POST, "password"), $newSalt); 47 | 48 | /* 49 | Here we can check if that hashed password already exists in database, and if so we generate other hash 50 | but in sake of simplicity we just update the password and salt anyway ! 51 | */ 52 | $user->setPropertyValue("password", $newPassword); 53 | $user->setPropertyValue("salt", $newSalt); 54 | 55 | $user->update(); 56 | 57 | // At the bottom in html code add a div where you check if this flash is exists, if so print its content 58 | Session::flash("Password_changed", "Your password has been changed successfully."); 59 | } 60 | } 61 | 62 | foreach($validate->errors() as $error) { 63 | echo $error . "
"; 64 | } 65 | } 66 | } 67 | 68 | ?> 69 | 70 | 71 | 72 | 73 | 74 | 75 | Password recovery 76 | 77 | 78 | 79 | 85 | 86 | 87 | 88 |
89 |
90 |
91 |

92 |
93 |

New Password

94 |

Choose a new password

95 |
" method="post" class="flex-column"> 96 |
97 | 98 | " placeholder="Email address" autocomplete="off" class="classic-input"> 99 |
100 |
101 | 102 | 103 |
104 |
105 | 106 | 107 |
108 |
109 | "> 110 | 111 |
112 |
113 |
114 |
115 | 116 | 117 | -------------------------------------------------------------------------------- /Login/confirmationcode.php: -------------------------------------------------------------------------------- 1 | check($_POST, array( 19 | "code"=>array( 20 | "name"=>"Confirmation code", 21 | "required"=>true, 22 | "max"=>16 23 | ) 24 | )); 25 | 26 | if($validate->passed()) { 27 | if(Session::get("email-confirmation") == Common::getInput($_POST, "code")) { 28 | // Here the confirmation code is good 29 | Session::delete("email-confirmation"); 30 | Session::put("password-change-allow", "allowed"); 31 | Redirect::to("changepassword.php"); 32 | } else { 33 | $validate->addError("Invalide confirmation code"); 34 | } 35 | } 36 | 37 | foreach($validate->errors() as $error) { 38 | echo $error . "
"; 39 | } 40 | } 41 | } 42 | 43 | ?> 44 | 45 | 46 | 47 | 48 | 49 | 50 | Password recovery 51 | 52 | 53 | 54 | 60 | 61 | 62 | 63 |
64 |
65 |

Email Confirmation

66 |

We sent a confirmation code into your email, copy and past it here

67 |
" method="post" class="flex-column"> 68 |
69 | 70 | 71 |
72 |
73 | "> 74 | 75 |
76 |
77 |
78 |
79 | 80 | -------------------------------------------------------------------------------- /Login/passwordRecover.php: -------------------------------------------------------------------------------- 1 | check($_POST, array( 15 | "email"=>array( 16 | "name"=>"Email", 17 | "required"=>true, 18 | "max"=>255, 19 | "min"=>6, 20 | "email"=>true 21 | ) 22 | )); 23 | 24 | if($validate->passed()) { 25 | $exists = $user->fetchUser("email", Common::getInput($_POST, "email")); 26 | if($exists) { 27 | $conf_code = substr(Hash::unique(), 16, 16); 28 | // Code to send conf code 29 | $mg = new Mailgun("YOUR_KEY"); 30 | $domain = "YOUR_DOMAIN"; 31 | # Make the call to the client. 32 | try { 33 | $result = $mg->sendMessage($domain, array( 34 | 'from' => '', 35 | 'to' => '<' . $user->getPropertyValue("email") . '>', 36 | 'subject' => "Confirmation code", 37 | 'text' => $conf_code 38 | )); 39 | 40 | /* We'll use email-confirmation sesion variable in the next page to see if the user pass from this process, 41 | If the user try directly to go to the next page, he'll not be able to access it because email-confirmation 42 | session variable will not be set here and we redirect him to login page*/ 43 | Session::put("email-confirmation", $conf_code); 44 | 45 | // We'll need this variable to fetch the user data in th next pages of password recovery 46 | Session::put("u_id", $user->getPropertyValue("id")); 47 | $user->fetchUser("email", Common::getInput($_POST, "email")); 48 | Redirect::to("confirmationcode.php"); 49 | } catch(Exception $e) { 50 | $validate->addError("There's a problem while sending the confirmation code."); 51 | $validate->addError("If send message throws an error about ssl certificat, read [IMPORTANT#5] in Z_IMPORTANT.txt file in root directory to resolve this problem!"); 52 | } 53 | 54 | } else { 55 | // Print error message 56 | echo "There's no user with this email address !"; 57 | } 58 | } else { 59 | // Here instead of printing out errors we can put them in an array and use them in proper html labels 60 | foreach($validate->errors() as $error) { 61 | echo $error . "
"; 62 | } 63 | } 64 | } 65 | } 66 | 67 | ?> 68 | 69 | 70 | 71 | 72 | 73 | 74 | Password recovery 75 | 76 | 77 | 78 | 84 | 85 | 86 | 87 |
88 |
89 |

Password recovery

90 |

Enter your email and click send button to get a confirmation code on your email box.

91 |
" method="post" class="flex-column"> 92 |
93 | 94 | 95 |
96 |
97 | "> 98 | 99 |
100 |
101 |
102 |
103 | 104 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

VOID47 Chat Application

2 | 3 | This is a real-time chat application written in pure PHP/MYSQL. 4 | 5 | -------------------------------------------------------------------------------- /api/comment/delete.php: -------------------------------------------------------------------------------- 1 | "comment id required !", 20 | "success"=>false 21 | )); 22 | } 23 | 24 | $post_id = sanitize_id($_POST["comment_id"]); 25 | 26 | $comment = new Comment(); 27 | $comment->set_property("id", $post_id); 28 | if($comment->delete()) { 29 | echo 1; 30 | } else { 31 | echo -1; 32 | } -------------------------------------------------------------------------------- /api/comment/edit.php: -------------------------------------------------------------------------------- 1 | "comment id required !", 21 | "success"=>false 22 | )); 23 | } 24 | 25 | if(!isset($_POST["new_comment"])) { 26 | echo json_encode(array( 27 | "message"=>"new comment required !", 28 | "success"=>false 29 | )); 30 | } 31 | 32 | $comment_id = sanitize_id($_POST["comment_id"]); 33 | $new_comment = sanitize_text($_POST["new_comment"]); 34 | 35 | $comment = new Comment(); 36 | $comment->fetch_comment($comment_id); 37 | 38 | $comment->set_property("comment_text", $new_comment); 39 | 40 | if($comment->update()) { 41 | echo $new_comment; 42 | } else { 43 | echo -1; 44 | } -------------------------------------------------------------------------------- /api/comment/post.php: -------------------------------------------------------------------------------- 1 | "post id required !", 23 | "success"=>false 24 | )); 25 | } 26 | 27 | if(!isset($_POST["current_user_id"])) { 28 | echo json_encode(array( 29 | "message"=>"Current user id required !", 30 | "success"=>false 31 | )); 32 | } 33 | 34 | if(!isset($_POST["comment_owner"])) { 35 | echo json_encode(array( 36 | "message"=>"comment owner required !", 37 | "success"=>false 38 | )); 39 | } 40 | 41 | if(!isset($_POST["comment_owner"]) || empty($_POST["comment_owner"])) { 42 | echo json_encode(array( 43 | "message"=>"comment should not be empty or unset !", 44 | "success"=>false 45 | )); 46 | } 47 | 48 | $comment_owner = sanitize_id($_POST["comment_owner"]); 49 | $post_id = sanitize_id($_POST["post_id"]); 50 | $comment_text = sanitize_text($_POST["comment_text"]); 51 | $current_user_id = sanitize_id($_POST["current_user_id"]); 52 | 53 | $comment = new Comment(); 54 | $comment->setData(array( 55 | "comment_owner"=>$comment_owner, 56 | "post_id"=>$post_id, 57 | "comment_text"=>$comment_text 58 | )); 59 | $comment = $comment->add(); 60 | // Right now, we don't now the id of added comment 61 | $captured_id = DB::getInstance()->query("SELECT id FROM comment WHERE comment_owner = ? AND comment_date = ?", array( 62 | "comment_owner"=>$comment->get_property("comment_owner"), 63 | "comment_date"=>$comment->get_property("comment_date") 64 | ))->results()[0]->id; 65 | 66 | $comment->set_property("id", $captured_id); 67 | 68 | 69 | $post_manager = Post_Manager::generate_comment($comment, $current_user_id); 70 | 71 | echo $post_manager; 72 | 73 | /* you can only add and return the following array, but we want to return a comment component 74 | echo json_encode(array( 75 | "message"=>"Comment added successfully !", 76 | "success"=>true 77 | ));*/ -------------------------------------------------------------------------------- /api/follow/add.php: -------------------------------------------------------------------------------- 1 | "You should provide current_user_id as post form input", 20 | "success"=>false 21 | ) 22 | ); 23 | 24 | exit(); 25 | } 26 | if(!isset($_POST["current_profile_id"])) { 27 | echo json_encode( 28 | array( 29 | "message"=>"You should provide followed_id as post form input", 30 | "success"=>false 31 | ) 32 | ); 33 | 34 | exit(); 35 | } 36 | 37 | $follower = $_POST["current_user_id"]; 38 | $followed = $_POST["current_profile_id"]; 39 | 40 | /* 41 | Here we can't allow user to follow himself because we create a UNIQUE constraint(follower_id, followed_id) in the database, 42 | If you want to allow user follow himself, remove the constraint and also remove the following if statement 43 | */ 44 | if($follower === $followed) { 45 | echo json_encode( 46 | array( 47 | "message"=>"You can't follow yourself", 48 | "success"=>false 49 | ) 50 | ); 51 | 52 | exit(); 53 | } 54 | 55 | // Check if the follower id is set, and if it is numeric by calling sanitize_id, and exists in the database using user_exists 56 | if(($follower = sanitize_id($follower)) && 57 | User::user_exists("id", $follower)) { 58 | // Same check here with the followed user 59 | if(isset($followed) && 60 | ($followed = sanitize_id($followed)) && 61 | User::user_exists("id", $followed)) { 62 | if(Follow::follow_exists($follower, $followed)) { 63 | echo json_encode( 64 | array( 65 | "message"=>"The follower user is already following the followed user", 66 | "success"=>false 67 | ) 68 | ); 69 | } else { 70 | // Now we know the follower id is valid as well as the followed id, now we can add it to our database 71 | $follow = new Follow(); 72 | $follow->set_data(array( 73 | "follower"=>$follower, 74 | "followed"=>$followed 75 | )); 76 | $follow->add(); 77 | 78 | echo json_encode( 79 | array( 80 | "message"=>"user with id: " . $follower . " followed user with id: " . $followed . " successfully !", 81 | "success"=>true 82 | ) 83 | ); 84 | } 85 | } else { 86 | echo json_encode( 87 | array( 88 | "message"=>"followed id is either not valid or not exists in our db", 89 | "success"=>false 90 | ) 91 | ); 92 | } 93 | } else { 94 | echo json_encode( 95 | array( 96 | "message"=>"follower id is either not valid or not exists in our db", 97 | "success"=>false 98 | ) 99 | ); 100 | } -------------------------------------------------------------------------------- /api/follow/delete.php: -------------------------------------------------------------------------------- 1 | "You should provide current_user_id as post form input", 20 | "success"=>false 21 | ) 22 | ); 23 | 24 | exit(); 25 | } 26 | if(!isset($_POST["current_profile_id"])) { 27 | echo json_encode( 28 | array( 29 | "message"=>"You should provide followed_id as post form input", 30 | "success"=>false 31 | ) 32 | ); 33 | 34 | exit(); 35 | } 36 | 37 | $follower = $_POST["current_user_id"]; 38 | $followed = $_POST["current_profile_id"]; 39 | 40 | /* 41 | Here we can't allow user to follow himself because we create a UNIQUE constraint(follower_id, followed_id) in the database, 42 | If you want to allow user follow himself, remove the constraint and also remove the following if statement 43 | */ 44 | if($follower === $followed) { 45 | echo json_encode( 46 | array( 47 | "message"=>"You can't unfollow yourself", 48 | "success"=>false 49 | ) 50 | ); 51 | 52 | exit(); 53 | } 54 | 55 | // Check if the follower id is set, and if it is numeric by calling sanitize_id, and exists in the database using user_exists 56 | if(($follower = sanitize_id($follower)) && 57 | User::user_exists("id", $follower)) { 58 | // Same check here with the followed user 59 | if(($followed = sanitize_id($followed)) && 60 | User::user_exists("id", $followed)) { 61 | if(Follow::follow_exists($follower, $followed)) { 62 | 63 | $follow = new Follow(); 64 | 65 | $follow->set_data(array( 66 | "follower"=>$follower, 67 | "followed"=>$followed 68 | )); 69 | 70 | $follow->fetch_follow(); 71 | 72 | $follow->delete(); 73 | 74 | echo json_encode( 75 | array( 76 | "message"=>"The follower with id: $follower unfollows the user with id: $followed successully !", 77 | "success"=>true 78 | ) 79 | ); 80 | } else { 81 | echo json_encode( 82 | array( 83 | "message"=>"The user with id: $follower cannot unfollow the user with id: $followed because he is not followed him !", 84 | "success"=>false 85 | ) 86 | ); 87 | } 88 | } else { 89 | echo json_encode( 90 | array( 91 | "message"=>"followed id is either not valid or not exists in our db", 92 | "success"=>false 93 | ) 94 | ); 95 | } 96 | } else { 97 | echo json_encode( 98 | array( 99 | "message"=>"follower id is either not valid or not exists in our db", 100 | "success"=>false 101 | ) 102 | ); 103 | } -------------------------------------------------------------------------------- /api/follow/get_followed_users.php: -------------------------------------------------------------------------------- 1 | "You should provide user_id as query string parameter along with path", 20 | "success"=>false 21 | ) 22 | ); 23 | 24 | exit(); 25 | } 26 | 27 | $user_id = $_GET["user_id"]; 28 | 29 | // Check if the follower id is set, and if it is numeric by calling sanitize_id, and exists in the database using user_exists 30 | if(($user_id = sanitize_id($_GET["user_id"])) && 31 | User::user_exists("id", $user_id)) { 32 | // If the user has followers(at least one we get them as array and encode them and return the json array) 33 | $followed_users = Follow::get_followed_users($user_id); 34 | if(count($followed_users) > 0) { 35 | $followed_users = json_encode($followed_users); 36 | echo json_encode( 37 | array( 38 | "followers"=>$followed_users, 39 | "message"=>"followed users return successfully !", 40 | "success"=>true 41 | ) 42 | ); 43 | } else { 44 | echo json_encode( 45 | array( 46 | "followers"=>null, 47 | "message"=>"This user follows no one.", 48 | "success"=>true 49 | ) 50 | ); 51 | } 52 | } else { 53 | echo json_encode( 54 | array( 55 | "message"=>"user with id: $user_id is either not valid or not exists in our db", 56 | "success"=>false 57 | ) 58 | ); 59 | } -------------------------------------------------------------------------------- /api/follow/get_followers.php: -------------------------------------------------------------------------------- 1 | "You should provide user_id as query string parameter along with path", 20 | "success"=>false 21 | ) 22 | ); 23 | 24 | exit(); 25 | } 26 | 27 | $user_id = $_GET["user_id"]; 28 | 29 | // Check if the follower id is set, and if it is numeric by calling sanitize_id, and exists in the database using user_exists 30 | if(($user_id = sanitize_id($_GET["user_id"])) && 31 | User::user_exists("id", $user_id)) { 32 | 33 | // If the user has followers(at least one we get them as array and encode them and return the json array) 34 | $followers = Follow::get_user_followers($user_id); 35 | if(count($followers) > 0) { 36 | $followers = json_encode($followers); 37 | echo json_encode( 38 | array( 39 | "followers"=>$followers, 40 | "message"=>"followers return successfully !", 41 | "success"=>true 42 | ) 43 | ); 44 | } else { 45 | echo json_encode( 46 | array( 47 | "followers"=>null, 48 | "message"=>"This user has no followers.", 49 | "success"=>true 50 | ) 51 | ); 52 | } 53 | } else { 54 | echo json_encode( 55 | array( 56 | "message"=>"user id is either not valid or not exists in our db", 57 | "success"=>false 58 | ) 59 | ); 60 | } -------------------------------------------------------------------------------- /api/like/post.php: -------------------------------------------------------------------------------- 1 | "post id required !", 20 | "success"=>false 21 | )); 22 | } 23 | 24 | if(!isset($_POST["current_user_id"])) { 25 | echo json_encode(array( 26 | "message"=>"Current user id required !", 27 | "success"=>false 28 | )); 29 | } 30 | 31 | $post_id = sanitize_id($_POST["post_id"]); 32 | $current_user_id = sanitize_id($_POST["current_user_id"]); 33 | 34 | $like = new Like(); 35 | $like->setData(array( 36 | "post_id"=>$post_id, 37 | "user_id"=>$current_user_id, 38 | )); 39 | $res = $like->add(); 40 | 41 | /* 42 | 1: added successfully 43 | 2: deleted successfully 44 | -1: there's a problem 45 | */ 46 | if($res == 1) { 47 | echo 1; 48 | } else if ($res == -1) { 49 | if($like->delete()) { 50 | echo 2; 51 | } 52 | } else { 53 | echo -1; 54 | } 55 | 56 | /* you can only add and return the following array, but we want to return a comment component 57 | echo json_encode(array( 58 | "message"=>"Comment added successfully !", 59 | "success"=>true 60 | ));*/ -------------------------------------------------------------------------------- /api/messages/DELETE.php: -------------------------------------------------------------------------------- 1 | set_property("id", $message_id); 31 | 32 | if($is_received == 'yes') { 33 | $message_manager->delete_received_message(); 34 | } else { 35 | $message_manager->delete_sended_message(); 36 | } 37 | 38 | echo json_encode(array( 39 | "success"=>true, 40 | "message"=>'message deleted successfully !' 41 | )); 42 | } else { 43 | echo json_encode(array( 44 | "success"=>false, 45 | "message"=>'message not exists' 46 | )); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /api/messages/Send.php: -------------------------------------------------------------------------------- 1 | fetchUser("id", (int)$sender); 36 | 37 | $message_model = new Message(); 38 | $message_model->set_data(array( 39 | "sender"=>$sender, 40 | "receiver"=>$receiver, 41 | "message"=>$message, 42 | "message_date"=>$message_date 43 | )); 44 | 45 | $chat_wrapper = new ChatComponent(); 46 | 47 | if($is_reply) { 48 | $message_model->set_property("is_reply", 1); 49 | $message_model->set_property("reply_to", $replied_message_id); 50 | $res = $message_model->add(); 51 | 52 | // Get original message and reply message creators 53 | $original_message_creator = Message::get_creator_by_id($replied_message_id); 54 | $original_message_creator = $original_message_creator->message_creator; 55 | $reply_message_creator = Message::get_creator_by_id($res); 56 | $reply_message_creator = $reply_message_creator->message_creator; 57 | 58 | // Then we generate a replied message components 59 | // Here we need to pass the original message id, reply_message_id, original message creator and reply creator 60 | $chat_component = $chat_wrapper->generate_sender_reply_message($replied_message_id, $res, $original_message_creator, $reply_message_creator); 61 | echo $chat_component; 62 | } else { 63 | $res = $message_model->add(); 64 | 65 | $message_obj = Message::get_message_obj("id", $res); 66 | 67 | $chat_wrapper = new ChatComponent(); 68 | $chat_component = $chat_wrapper->generate_current_user_message($sender_user, $message_obj, $message_date); 69 | echo $chat_component; 70 | } 71 | 72 | } else { 73 | echo json_encode( 74 | array( 75 | "message"=>"receiver's id is either not valid or not exists in our db", 76 | "success"=>false 77 | ) 78 | ); 79 | } 80 | } else { 81 | echo json_encode( 82 | array( 83 | "message"=>"sender's id is either not valid or not exists in our db", 84 | "success"=>false 85 | ) 86 | ); 87 | } -------------------------------------------------------------------------------- /api/messages/get_friend_messages.php: -------------------------------------------------------------------------------- 1 | fetchUser("id", $sender); 31 | $receiver_user = new User(); 32 | $receiver_user->fetchUser("id", $receiver); 33 | 34 | $chat_component = new ChatComponent(); 35 | 36 | $sender_to_receiver = Message::getMessages($sender, $receiver); 37 | $receiver_to_sender = Message::getMessages($receiver, $sender); 38 | $messages = array_merge($sender_to_receiver, $receiver_to_sender); 39 | 40 | function sortFunction($a, $b) { 41 | return strtotime($a->create_date) - strtotime($b->create_date); 42 | } 43 | 44 | usort($messages, "sortFunction"); 45 | $content = ''; 46 | 47 | foreach($messages as $message) { 48 | if($message->message_creator == $sender) { 49 | if($message->is_reply) { 50 | //Now we have a message as a reply, so first we have reply_message_id 51 | $reply_message_id = $message->id; 52 | $message_obj = Message::get_message_obj('id', $reply_message_id); 53 | 54 | // Then, we need to fetch the message that the above is a reply to 55 | $original_message_id = $message->reply_to; 56 | 57 | $original_creator = Message::get_creator_by_id($original_message_id); 58 | $original_creator = $original_creator->message_creator; 59 | $reply_creator = Message::get_creator_by_id($reply_message_id); 60 | $reply_creator = $reply_creator->message_creator; 61 | // Here we need to pass the original message id, reply_message_id, original message creator and reply creator 62 | $content .= $chat_component->generate_sender_reply_message($original_message_id, $reply_message_id, $original_creator, $reply_creator); 63 | } else { 64 | $content .= $chat_component->generate_current_user_message($sender_user, $message, $message->create_date); 65 | } 66 | } else { 67 | if($message->is_reply) { 68 | //Now we have a message as a reply, so first we have reply_message_id 69 | $reply_message_id = $message->id; 70 | $message_obj = Message::get_message_obj('id', $reply_message_id); 71 | 72 | // Then, we need to fetch the message that the above is a reply to 73 | $original_message_id = $message->reply_to; 74 | 75 | $original_creator = Message::get_creator_by_id($original_message_id); 76 | $original_creator = $original_creator->message_creator; 77 | $reply_creator = Message::get_creator_by_id($reply_message_id); 78 | $reply_creator = $reply_creator->message_creator; 79 | 80 | $content .= $chat_component->generate_received_reply_message($original_message_id, $reply_message_id, $original_creator, $reply_creator); 81 | } else { 82 | $content .= $chat_component->generate_friend_message($receiver_user, $message, $message->create_date); 83 | } 84 | } 85 | } 86 | Message::dump_channel($receiver, $sender); 87 | echo $content; 88 | 89 | } else { 90 | echo json_encode( 91 | array( 92 | "message"=>"receiver's id is either not valid or not exists in our db", 93 | "success"=>false 94 | ) 95 | ); 96 | } 97 | } else { 98 | echo json_encode( 99 | array( 100 | "message"=>"sender's id is either not valid or not exists in our db", 101 | "success"=>false 102 | ) 103 | ); 104 | } -------------------------------------------------------------------------------- /api/messages/message_writing_notifier/add.php: -------------------------------------------------------------------------------- 1 | set_property("message_sender", $sender); 31 | $message_model->set_property("message_receiver", $receiver); 32 | 33 | $message_model->add_writing_message_notifier(); 34 | } else { 35 | echo json_encode( 36 | array( 37 | "message"=>"receiver's id is either not valid or not exists in our db", 38 | "success"=>false 39 | ) 40 | ); 41 | } 42 | } else { 43 | echo json_encode( 44 | array( 45 | "message"=>"sender's id is either not valid or not exists in our db", 46 | "success"=>false 47 | ) 48 | ); 49 | } -------------------------------------------------------------------------------- /api/messages/message_writing_notifier/delete.php: -------------------------------------------------------------------------------- 1 | set_property("message_sender", $sender); 31 | $message_model->set_property("message_receiver", $receiver); 32 | 33 | $message_model->delete_writing_message_notifier(); 34 | } else { 35 | echo json_encode( 36 | array( 37 | "message"=>"receiver's id is either not valid or not exists in our db", 38 | "success"=>false 39 | ) 40 | ); 41 | } 42 | } else { 43 | echo json_encode( 44 | array( 45 | "message"=>"sender's id is either not valid or not exists in our db", 46 | "success"=>false 47 | ) 48 | ); 49 | } -------------------------------------------------------------------------------- /api/post/delete.php: -------------------------------------------------------------------------------- 1 | "post id required !", 20 | "success"=>false 21 | )); 22 | return; 23 | } 24 | if(!isset($_POST["post_owner"])) { 25 | echo json_encode(array( 26 | "message"=>"post owner id required !", 27 | "success"=>false 28 | )); 29 | return; 30 | } 31 | 32 | $post_id = sanitize_id($_POST["post_id"]); 33 | $post_owner = sanitize_id($_POST["post_owner"]); 34 | 35 | if(Post::exists($post_id)) { 36 | /* 37 | We first need to make shure that the post exists with that id, then we need to check if the user 38 | who makes the request is the owner of the post because we only allow the owner of the post to delete it. 39 | */ 40 | if($post_owner == Post::get_post_owner($post_id)->post_owner) { 41 | 42 | Like::delete_post_likes($post_id); 43 | Comment::delete_post_comments($post_id); 44 | 45 | $post = new Post(); 46 | $post->set_property('post_id', $post_id); 47 | $post->delete(); 48 | 49 | /* 50 | When the original post is deleted we want to edit all postst that are a shared post of that post and edit the column 51 | shared_post_id to empty 52 | */ 53 | $shared_posts = Post::get('post_shared_id', $post_id); 54 | foreach($shared_posts as $p) { 55 | $p->set_property('post_shared_id', null); 56 | $test = $p->update(); 57 | } 58 | 59 | echo json_encode(array( 60 | "success"=>true, 61 | "message"=>'post deleted successfully !' 62 | )); 63 | } else { 64 | echo json_encode(array( 65 | "success"=>false, 66 | "message"=>'user\'s id is invalide!' 67 | )); 68 | } 69 | 70 | } else { 71 | echo json_encode(array( 72 | "success"=>false, 73 | "message"=>'post not exists' 74 | )); 75 | } 76 | 77 | -------------------------------------------------------------------------------- /api/post/edit.php: -------------------------------------------------------------------------------- 1 | "post id required !", 21 | "success"=>false 22 | )); 23 | } 24 | 25 | if(!isset($_POST["new_post_text"])) { 26 | echo json_encode(array( 27 | "message"=>"new post text required !", 28 | "success"=>false 29 | )); 30 | } 31 | 32 | $post_id = sanitize_id($_POST["post_id"]); 33 | $new_post_text = sanitize_text($_POST["new_post_text"]); 34 | 35 | $post = new Post(); 36 | $post->fetchPost($post_id); 37 | $post->set_property('text_content', $new_post_text); 38 | 39 | if($post->update()) { 40 | echo $new_post_text; 41 | } else { 42 | echo -1; 43 | } -------------------------------------------------------------------------------- /api/post/shared/add.php: -------------------------------------------------------------------------------- 1 | "post owner id required !", 20 | "success"=>false 21 | )); 22 | } 23 | 24 | if(!isset($_POST["post_visibility"])) { 25 | echo json_encode(array( 26 | "message"=>"post_visibility required !", 27 | "success"=>false 28 | )); 29 | } 30 | 31 | if(!isset($_POST["post_place"])) { 32 | echo json_encode(array( 33 | "message"=>"post_place required !", 34 | "success"=>false 35 | )); 36 | } 37 | 38 | if(!isset($_POST["post_shared_id"])) { 39 | echo json_encode(array( 40 | "message"=>"post shared id required !", 41 | "success"=>false 42 | )); 43 | } 44 | 45 | $post_owner = sanitize_id($_POST["post_owner"]); 46 | $post_visibility = is_numeric($_POST["post_visibility"]) ? $_POST["post_visibility"] : 1; 47 | $post_place = is_numeric($_POST["post_place"]) ? $_POST["post_place"] : 1; 48 | $post_shared_id = sanitize_id($_POST["post_shared_id"]); 49 | 50 | /* 51 | Text cotent is not necessary because we make edit later; so that the user when he shares the post then he could edit it 52 | and add his own text content. We set photo and video directories to null because we don't want them in shared post; 53 | In shared post the user could only change text, post visibility, or post place 54 | We set is_shared to 1 because it's a shared post and we set the original_post to post_shared_id 55 | */ 56 | 57 | $post = new Post(); 58 | $post->setData(array( 59 | "post_owner"=> $post_owner, 60 | "post_visibility"=> $post_visibility, 61 | "post_place"=> $post_place, 62 | "text_content"=> "", 63 | "picture_media"=>null, 64 | "video_media"=>null, 65 | "is_shared"=>1, 66 | "post_shared_id"=>$post_shared_id 67 | )); 68 | 69 | $res = $post->add(); 70 | 71 | if($res) { 72 | echo 1; 73 | } else { 74 | echo -1; 75 | } -------------------------------------------------------------------------------- /api/user/GET.php: -------------------------------------------------------------------------------- 1 | fetchUser("id", $id)) { 17 | echo json_encode($user); 18 | } else { 19 | echo json_encode(array( 20 | 'problem'=>'Invalid data provided or the id is not within our database' 21 | )); 22 | } 23 | -------------------------------------------------------------------------------- /api/user/get_by_username.php: -------------------------------------------------------------------------------- 1 | fetchUser("username", $username)) { 21 | echo json_encode(array( 22 | "user"=>$user, 23 | "success"=>true 24 | )); 25 | } else { 26 | echo json_encode(array( 27 | "success"=>false 28 | )); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /api/user_relation/accept_request.php: -------------------------------------------------------------------------------- 1 | "You can't add yourself or cancel request", 28 | "success"=>false 29 | ) 30 | ); 31 | 32 | exit(); 33 | } 34 | 35 | // Check if the follower id is set, and if it is numeric by calling sanitize_id, and exists in the database using user_exists 36 | if(($current_user) && 37 | User::user_exists("id", $current_user)) { 38 | // Same check here with the followed user 39 | if($friend && 40 | User::user_exists("id", $friend)) { 41 | 42 | $user_relation = new UserRelation(); 43 | 44 | $user_relation->set_property("from", $friend); 45 | $user_relation->set_property("to", $current_user); 46 | 47 | if($user_relation->accept_request()) { 48 | echo json_encode( 49 | array( 50 | "message"=>"user with id: $current_user accepts a request sent by user with id: $friend successfully !", 51 | "success"=>true, 52 | "error"=>false 53 | ) 54 | ); 55 | } else { 56 | echo json_encode( 57 | array( 58 | "message"=>"user with id: $friend did not send a friend request to user with id: $current_user", 59 | "success"=>false, 60 | "error"=>false 61 | ) 62 | ); 63 | } 64 | 65 | } else { 66 | echo json_encode( 67 | array( 68 | "message"=>"friend's id is either not valid or not exists in our db", 69 | "error"=>true 70 | ) 71 | ); 72 | } 73 | } else { 74 | echo json_encode( 75 | array( 76 | "message"=>"your id is either not valid or not exists in our db", 77 | "error"=>true 78 | ) 79 | ); 80 | } -------------------------------------------------------------------------------- /api/user_relation/cancel_request.php: -------------------------------------------------------------------------------- 1 | "You can't add yourself or cancel request", 28 | "success"=>false 29 | ) 30 | ); 31 | 32 | exit(); 33 | } 34 | 35 | // Check if the follower id is set, and if it is numeric by calling sanitize_id, and exists in the database using user_exists 36 | if(($current_user) && 37 | User::user_exists("id", $current_user)) { 38 | // Same check here with the followed user 39 | if($friend && 40 | User::user_exists("id", $friend)) { 41 | 42 | $user_relation = new UserRelation(); 43 | 44 | $user_relation->set_property("from", $current_user); 45 | $user_relation->set_property("to", $friend); 46 | 47 | if($user_relation->cancel_request()) { 48 | echo json_encode( 49 | array( 50 | "message"=>"user with id: $current_user cancels a request that was sent to user with id: $friend successfully !", 51 | "success"=>true, 52 | "error"=>false 53 | ) 54 | ); 55 | } else { 56 | echo json_encode( 57 | array( 58 | "message"=>"user with id: $current_user is not a friend to user with id: $friend", 59 | "success"=>false, 60 | "error"=>false 61 | ) 62 | ); 63 | } 64 | 65 | } else { 66 | echo json_encode( 67 | array( 68 | "message"=>"friend's id is either not valid or not exists in our db", 69 | "error"=>true 70 | ) 71 | ); 72 | } 73 | } else { 74 | echo json_encode( 75 | array( 76 | "message"=>"your id is either not valid or not exists in our db", 77 | "error"=>true 78 | ) 79 | ); 80 | } -------------------------------------------------------------------------------- /api/user_relation/decline_request.php: -------------------------------------------------------------------------------- 1 | "You can't add yourself or cancel request", 28 | "success"=>false 29 | ) 30 | ); 31 | 32 | exit(); 33 | } 34 | 35 | // Check if the follower id is set, and if it is numeric by calling sanitize_id, and exists in the database using user_exists 36 | if(($current_user) && 37 | User::user_exists("id", $current_user)) { 38 | // Same check here with the followed user 39 | if($friend && 40 | User::user_exists("id", $friend)) { 41 | 42 | $user_relation = new UserRelation(); 43 | 44 | $user_relation->set_property("from", $friend); 45 | $user_relation->set_property("to", $current_user); 46 | 47 | if($user_relation->delete_relation()) { 48 | echo json_encode( 49 | array( 50 | "message"=>"user with id: $current_user decline a request that was sent to user with id: $friend successfully !", 51 | "success"=>true 52 | ) 53 | ); 54 | } else { 55 | echo json_encode( 56 | array( 57 | "message"=>"user with id: $friend did not send a friend request to user with id: $current_user", 58 | "success"=>false 59 | ) 60 | ); 61 | } 62 | 63 | } else { 64 | echo json_encode( 65 | array( 66 | "message"=>"friend's id is either not valid or not exists in our db", 67 | "success"=>false 68 | ) 69 | ); 70 | } 71 | } else { 72 | echo json_encode( 73 | array( 74 | "message"=>"your id is either not valid or not exists in our db", 75 | "success"=>false 76 | ) 77 | ); 78 | } -------------------------------------------------------------------------------- /api/user_relation/send_request.php: -------------------------------------------------------------------------------- 1 | "You can't add yourself", 28 | "success"=>false 29 | ) 30 | ); 31 | 32 | exit(); 33 | } 34 | 35 | // Check if the follower id is set, and if it is numeric by calling sanitize_id, and exists in the database using user_exists 36 | if(($current_user) && 37 | User::user_exists("id", $current_user)) { 38 | // Same check here with the followed user 39 | if($friend && 40 | User::user_exists("id", $friend)) { 41 | 42 | $user_relation = new UserRelation(); 43 | 44 | $user_relation->set_property("from", $current_user); 45 | $user_relation->set_property("to", $friend); 46 | 47 | if($user_relation->send_request()) { 48 | echo json_encode( 49 | array( 50 | "message"=>"user with id: $current_user sends a friend request to user with id: $friend", 51 | "success"=>true, 52 | "error"=>false 53 | ) 54 | ); 55 | } else { 56 | echo json_encode( 57 | array( 58 | "message"=>"user with id: $current_user elready send a friend request to user with id: $friend", 59 | "success"=>false, 60 | "error"=>false 61 | ) 62 | ); 63 | } 64 | 65 | } else { 66 | echo json_encode( 67 | array( 68 | "message"=>"friend's id is either not valid or not exists in our db", 69 | "error"=>true 70 | ) 71 | ); 72 | } 73 | } else { 74 | echo json_encode( 75 | array( 76 | "message"=>"your id is either not valid or not exists in our db", 77 | "error"=>true 78 | ) 79 | ); 80 | } -------------------------------------------------------------------------------- /api/user_relation/unfriend_relation.php: -------------------------------------------------------------------------------- 1 | "You can't unfriend yourself", 28 | "success"=>false 29 | ) 30 | ); 31 | 32 | exit(); 33 | } 34 | 35 | // Check if the follower id is set, and if it is numeric by calling sanitize_id, and exists in the database using user_exists 36 | if(($current_user) && 37 | User::user_exists("id", $current_user)) { 38 | // Same check here with the followed user 39 | if($friend && 40 | User::user_exists("id", $friend)) { 41 | 42 | $user_relation = new UserRelation(); 43 | 44 | $user_relation->set_property("from", $current_user); 45 | $user_relation->set_property("to", $friend); 46 | 47 | if($user_relation->unfriend()) { 48 | echo json_encode( 49 | array( 50 | "message"=>"user with id: $current_user unfriends user with id: $friend successfully", 51 | "success"=>true, 52 | "error"=>false 53 | ) 54 | ); 55 | } else { 56 | echo json_encode( 57 | array( 58 | "message"=>"user with id: $current_user is not a friend of user with id: $friend or some invalide data is provided", 59 | "success"=>false, 60 | "error"=>false 61 | ) 62 | ); 63 | } 64 | 65 | } else { 66 | echo json_encode( 67 | array( 68 | "message"=>"friend's id is either not valid or not exists in our db", 69 | "success"=>false, 70 | "error"=>true 71 | ) 72 | ); 73 | } 74 | } else { 75 | echo json_encode( 76 | array( 77 | "message"=>"your id is either not valid or not exists in our db", 78 | "success"=>false, 79 | "error"=>true 80 | ) 81 | ); 82 | } -------------------------------------------------------------------------------- /classes/Common.php: -------------------------------------------------------------------------------- 1 | getPropertyValue($key), $key_array)) { 26 | $key_array[$i] = $val->getPropertyValue($key); 27 | $temp_array[$i] = $val; 28 | } 29 | $i++; 30 | } 31 | return $temp_array; 32 | } 33 | } -------------------------------------------------------------------------------- /classes/Config.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /classes/Cookie.php: -------------------------------------------------------------------------------- 1 | query("SELECT * FROM users"); 7 | DB::getInstance()->get("user", array('username', '=', 'mouad')) 8 | 1^^^^ 2^^^^^^^^ ^ 3^^^^^ 9 | 1: table, 10 | 2, 3: where username equals (=) mouad 11 | I think i will not use this method, i don't know we'll see ! 12 | */ 13 | 14 | 15 | class DB { 16 | // This will store database isntance if it is available 17 | private static $_instance = null; 18 | private $_pdo, 19 | $_error = false, 20 | $_query, 21 | $_results, 22 | $_count = 0; 23 | 24 | private function __construct() { 25 | $this->_pdo = new \PDO("mysql:host=" . Config::get('mysql/host') . ";dbname=" . Config::get('mysql/db'), Config::get('mysql/username'), Config::get('mysql/password')); 26 | } 27 | 28 | public static function getInstance() { 29 | /* 30 | This function will first check if we've already instaiated an instance it will simply return th instance, 31 | otherwise, we're going to instatiate it by creating a DB object; 32 | Hint: In case we use getInstance function twice on a page it will simply return the instance in the second 33 | call because it will be instantiated in the first call 34 | Hint: Notice that DB class cannot be instatiated outiside the class because the constructor is private 35 | */ 36 | 37 | if(!isset(self::$_instance)) { 38 | self::$_instance = new DB(); 39 | } 40 | 41 | return self::$_instance; 42 | } 43 | 44 | /* 45 | DB::getInstance()->query("SELECT * FROM user_info WHERE username = ?", array('MOUAD')); HERE MOUAD shuld be replaced 46 | in the first ? mark, if we have two parameters in query string, we need array of two values to replace each value to 47 | its correspondent place 48 | */ 49 | public function query($sql, $params = array()) { 50 | /* Here we set error to false in order to not return error of some previous query */ 51 | $this->error = false; 52 | 53 | // Check if the query has been prepared successfully 54 | // Here we assign and at the sametime check if a prepared statement has been set 55 | if($this->_query = $this->_pdo->prepare($sql)) { 56 | //Check if parameters exists (one at least should be exist) in case we need to bind them to the prepared statement 57 | if(count($params)) { 58 | $count = 1; 59 | foreach($params as $param) { 60 | 61 | /* FROM Documentation: 62 | Parameter identifier. For a prepared statement using named placeholders, this will be a parameter name of the form :name. For a prepared statement using question mark placeholders, this will be the 1-indexed position of the parameter. 63 | 64 | We are using quesion mark in our queries 65 | 66 | if query = "SELECT * FROM user_info WHERE username = ? AND password = ?; 67 | ^ ^ 68 | count:1 count:2 69 | count1 will point to username value which will be filled by first array element, and the same thing occur to the second part 70 | */ 71 | $this->_query->bindValue($count, $param); 72 | $count++; 73 | } 74 | } 75 | 76 | // We're going to execute the query anyway regardless whether the query has params or not 77 | if($this->_query->execute()) { 78 | $this->_results = $this->_query->fetchAll(\PDO::FETCH_OBJ); 79 | $this->_count = $this->_query->rowCount(); 80 | } else { 81 | $this->_error = true; 82 | } 83 | } 84 | 85 | // This allows you to chain everything with query function 86 | return $this; 87 | } 88 | 89 | public function pdo() { 90 | return $this->_pdo; 91 | } 92 | 93 | public function error() { 94 | return $this->_error; 95 | } 96 | 97 | public function results() { 98 | return $this->_results; 99 | } 100 | 101 | public function count() { 102 | return $this->_count; 103 | } 104 | } 105 | 106 | 107 | -------------------------------------------------------------------------------- /classes/Hash.php: -------------------------------------------------------------------------------- 1 | array( 13 | 'host'=>'127.0.0.1', 14 | 'username'=>'root', 15 | 'password'=>'', 16 | 'db'=>'chat' 17 | ), 18 | "remember"=> array( 19 | 'cookie_name'=>'hash', 20 | 'cookie_expiry'=>604800 21 | ), 22 | "session"=>array( 23 | 'session_name'=>'user', 24 | "token_name" => "token", 25 | "tokens"=>array( 26 | "register"=>"register", 27 | "login"=>"login", 28 | "reset-pasword"=>"reset-pasword", 29 | "saveEdits"=>"saveEdits", 30 | "share-post"=>"share-post", 31 | "logout"=>"logout" 32 | ) 33 | ), 34 | "root"=> array( 35 | 'path'=>'http://127.0.0.1/CHAT/', 36 | 'project_name'=>"CHAT" 37 | ) 38 | ); 39 | 40 | /* 41 | 42 | Here we create a user object with no data associated to it, and in the user constructor, we check if there's already a session 43 | if so we get the data from session which is the user id, and we fetch data from database of that id and we see if that id really 44 | exists in database, if it is, WE ASSIGN TRUE TO isLoggedIn 45 | 46 | Then we check if there's a cookie set in user machine and there's no session (This case is like we switch user's computer and later tries to logged in) 47 | in this case we fetch the hash of user's machine and see if this hash exists in users_session table in database, if hash matches we fetch user_id associated with it 48 | and use it to fetch user with that id. if the count of fetching is 1 then we give username, password and true($remember=true) to login function 49 | 50 | go to login function's comment 51 | 52 | */ 53 | 54 | /* 55 | Notice that getting the root path is a common case for almost every page, so it's a good idea to put it in $root 56 | variable and only use root variable to reference it because init file also included in every page deal with config 57 | */ 58 | $root = Config::get("root/path"); 59 | $proj_name = Config::get("root/project_name"); 60 | 61 | $user = new User(); 62 | 63 | if(Cookie::exists(Config::get("remember/cookie_name")) && !Session::exists(Config::get("session/session_name"))) { 64 | $hash = Cookie::get(Config::get("remember/cookie_name")); 65 | $res = DB::getInstance()->query("SELECT * FROM users_session WHERE hash = ?", array($hash)); 66 | 67 | if($res->count()) { 68 | $user->fetchUser("id", $res->results()[0]->user_id); 69 | $user->login($user->getPropertyValue("username"),$user->getPropertyValue("password"),true); 70 | } 71 | } 72 | 73 | if($user->getPropertyValue("isLoggedIn")) { 74 | $user->update_active(); 75 | } 76 | 77 | /* 78 | IMPORTANT : 79 | 1 - sanitize function file could not be included here because the path will be relative to the caller script 80 | so if for example include it like following: include_once "functions/sanitize.php" only scripts in the root 81 | directory can use it, otherwise a fatal error will be thrown 82 | So you should include it along with autoload and init file in every page needs it 83 | 84 | 2 - Composer autoload file also follow the same rule you can't import it here 85 | */ -------------------------------------------------------------------------------- /core/rest_init.php: -------------------------------------------------------------------------------- 1 | array( 10 | 'host'=>'127.0.0.1', 11 | 'username'=>'root', 12 | 'password'=>'', 13 | 'db'=>'chat' 14 | ), 15 | "remember"=> array( 16 | 'cookie_name'=>'hash', 17 | 'cookie_expiry'=>604800 18 | ), 19 | "session"=>array( 20 | 'session_name'=>'user', 21 | "token_name" => "token", 22 | "tokens"=>array( 23 | "register"=>"register", 24 | "login"=>"login", 25 | "reset-pasword"=>"reset-pasword", 26 | "share-post"=>"share-post", 27 | "saveEdits"=>"saveEdits", 28 | "logout"=>"logout" 29 | ) 30 | ), 31 | "root"=> array( 32 | 'path'=>'http://127.0.0.1/CHAT/' 33 | ) 34 | ); -------------------------------------------------------------------------------- /functions/get_extension.php: -------------------------------------------------------------------------------- 1 | getPropertyValue($key), $key_array)) { 15 | $key_array[$i] = $val->getPropertyValue($key); 16 | $temp_array[$i] = $val; 17 | } 18 | $i++; 19 | } 20 | return $temp_array; 21 | } -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | getPropertyValue("isLoggedIn")) { 15 | Redirect::to("login/login.php"); 16 | } 17 | 18 | $welcomeMessage = ''; 19 | if(Session::exists("register_success") && $user->getPropertyValue("username") == Session::get("new_username")) { 20 | $welcomeMessage = Session::flash("new_username") . ", " . Session::flash("register_success"); 21 | } 22 | 23 | 24 | $current_user_id = $user->getPropertyValue("id"); 25 | $journal_posts = Post::fetch_journal_posts($current_user_id); 26 | // Let's randomly sort array for now 27 | shuffle($journal_posts); 28 | /*usort($journal_posts, 'post_date_latest_sort'); 29 | 30 | function post_date_latest_sort($post1, $post2) { 31 | return $post1->get_property('post_date') == $post2->get_property('post_date') ? 0 : ($post1->get_property('post_date') > $post2->get_property('post_date')) ? -1 : 1; 32 | }*/ 33 | ?> 34 | 35 | 36 | 37 | 38 | 39 | 40 | V01D47 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

THIS IS TEST

62 |
63 |
64 |
65 |
66 | 67 |
68 |
69 |
70 | 71 |
72 |
73 |

74 | 79 |
80 |
81 |

82 |
83 |
84 |
85 | 86 |
87 | 88 |
89 |

Try to add friends, or follow them to see their posts ..

90 |

click here to go to the search page

91 |
92 | generate_post($post, $user); 97 | } 98 | } 99 | ?> 100 | 101 |
102 |
103 | 104 |
105 |
106 | 107 | -------------------------------------------------------------------------------- /layouts/chat/discussions/get_user_discussions.php: -------------------------------------------------------------------------------- 1 | $discussion->message_receiver, 45 | "receiver"=>$discussion->message_creator 46 | ); 47 | 48 | if(in_array($current_disc, $temp)) { 49 | continue; 50 | } 51 | 52 | $temp[] = array( 53 | "sender"=>$discussion->message_creator, 54 | "receiver"=>$discussion->message_receiver 55 | ); 56 | $result[] = $discussion; 57 | } 58 | 59 | $content = ''; 60 | foreach($result as $discussion) { 61 | $chat_comp = new ChatComponent(); 62 | 63 | $content .= $chat_comp->generate_discussion($current_user_id, $discussion); 64 | } 65 | 66 | echo $content; 67 | -------------------------------------------------------------------------------- /layouts/chat/generate_chat_container.php: -------------------------------------------------------------------------------- 1 | "You should provide current_user_id as post form input", 21 | "success"=>false 22 | ) 23 | ); 24 | 25 | exit(); 26 | } 27 | if(!isset($_POST["receiver"])) { 28 | echo json_encode( 29 | array( 30 | "message"=>"You should provide followed_id as post form input", 31 | "success"=>false 32 | ) 33 | ); 34 | 35 | exit(); 36 | } 37 | 38 | $sender = sanitize_id($_POST["sender"]); 39 | $receiver = sanitize_id($_POST["receiver"]); 40 | 41 | if(($sender) && 42 | User::user_exists("id", $sender)) { 43 | if(sanitize_id($receiver) && 44 | User::user_exists("id", $receiver)) { 45 | $chat_container = ChatComponent::generate_chat_section($sender, $receiver); 46 | return array( 47 | $chat_container, 48 | "success"=>true 49 | ); 50 | } else { 51 | echo json_encode( 52 | array( 53 | "message"=>"sender id is either not valid or not exists in our db", 54 | "success"=>false 55 | ) 56 | ); 57 | } 58 | } else { 59 | echo json_encode( 60 | array( 61 | "message"=>"sender id is either not valid or not exists in our db", 62 | "success"=>false 63 | ) 64 | ); 65 | } -------------------------------------------------------------------------------- /layouts/chat/get_chat_friend_by_username.php: -------------------------------------------------------------------------------- 1 | getPropertyValue("id"); 19 | $username = isset($_POST["username"]) ? sanitize_text($_POST["username"]) : ""; 20 | 21 | if(!empty($username)) { 22 | $user_relation = new UserRelation(); 23 | $friends = $user_relation->get_friends($current_user_id); 24 | 25 | $content = ''; 26 | foreach($friends as $friend) { 27 | if(strpos($friend->getPropertyValue("username"), $username) !== false) { 28 | $content .= ChatComponent::generate_chat_page_friend_contact($current_user_id, $friend); 29 | } 30 | } 31 | echo $content; 32 | } else { 33 | $user_relation = new UserRelation(); 34 | $friends = $user_relation->get_friends($current_user_id); 35 | 36 | $content = ''; 37 | foreach($friends as $friend) { 38 | $content .= ChatComponent::generate_chat_page_friend_contact($current_user_id, $friend); 39 | } 40 | echo $content; 41 | } -------------------------------------------------------------------------------- /layouts/general/CreatePost.php: -------------------------------------------------------------------------------- 1 | 10 |
11 | 12 | 13 | 14 | EOS; 15 | } 16 | 17 | public static function generatePostCreationVideo() { 18 | // Path will be set in src in javascript file when the user upload a file 19 | echo << 21 |
22 | 23 | 24 |
25 | 26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 | 36 | EOS; 37 | } 38 | } 39 | ?> -------------------------------------------------------------------------------- /layouts/master_right/Right.php: -------------------------------------------------------------------------------- 1 | getPropertyValue("id"); 9 | $user_name = $user->getPropertyValue("username"); 10 | $user_picture = Config::get("root/path") . (empty($user->getPropertyValue("picture")) ? "public/assets/images/logos/logo512.png" : $user->getPropertyValue("picture")); 11 | if(strlen($user_name) > 15) { 12 | $user_name = substr($user_name, 0, 15) . " .."; 13 | } 14 | 15 | $now = strtotime(date("Y/m/d h:i:s")); 16 | $last_active_date = strtotime($user->getPropertyValue("last_active_update")); 17 | $interval = abs($last_active_date - $now); 18 | $minutes = round($interval / 60); 19 | 20 | $online_status = ($minutes < 5) ? "online.png" : "offline.png"; 21 | 22 | // Here we need to implement some code to see if the yuser is online or not 23 | 24 | echo << 26 |
27 | 28 |
29 |

$user_name

30 |
31 |
32 | 33 | 34 | 35 | 36 |
37 | 38 |
39 | 40 | EOS; 41 | } 42 | } 43 | ?> -------------------------------------------------------------------------------- /layouts/post/generate_last_post.php: -------------------------------------------------------------------------------- 1 | fetchPost($post->id); 22 | 23 | $post_component = new Post_Manager(); 24 | $post_component = $post_component->generate_post($p, $user); 25 | 26 | echo $post_component; -------------------------------------------------------------------------------- /layouts/post/generate_post.php: -------------------------------------------------------------------------------- 1 | fetchPost($post_id); 28 | 29 | $post_component = new Post_Manager(); 30 | $post_component = $post_component->generate_post($post, $user); 31 | 32 | echo $post_component; 33 | -------------------------------------------------------------------------------- /layouts/post/generate_post_creation_image.php: -------------------------------------------------------------------------------- 1 | generatePostCreationImage(); 12 | 13 | ?> -------------------------------------------------------------------------------- /layouts/post/generate_post_creation_video.php: -------------------------------------------------------------------------------- 1 | generatePostCreationVideo(); 12 | 13 | ?> -------------------------------------------------------------------------------- /layouts/post/generate_post_data_item.php: -------------------------------------------------------------------------------- 1 | generatePostCreationImage(); 13 | 14 | ?> -------------------------------------------------------------------------------- /layouts/search/Search.php: -------------------------------------------------------------------------------- 1 | picture)) ? Config::get("root/path") . $user->picture : Config::get("root/path") . "public/assets/images/logos/logo512.png"; 12 | $fullname = $user->firstname . " " . $user->lastname; 13 | $username = $user->username; 14 | $id = $user->id; 15 | 16 | $follower_id = $current_user_id; 17 | $followed_id = $user->id; 18 | 19 | $follow = new Follow(); 20 | $follow->set_data(array( 21 | "follower"=>$follower_id, 22 | "followed"=>$followed_id 23 | )); 24 | 25 | if($follow->fetch_follow()) { 26 | $follow_btn = <<