├── .gitignore
├── .htaccess
├── app
├── .htaccess
├── bootstrap.php
├── config
│ └── config.php
├── controllers
│ ├── Pages.php
│ ├── Posts.php
│ └── Users.php
├── helpers
│ ├── datetime_helper.php
│ ├── session_helper.php
│ └── url_helper.php
├── libraries
│ ├── Controller.php
│ ├── Core.php
│ └── Database.php
├── models
│ ├── Post.php
│ └── User.php
└── views
│ ├── inc
│ ├── footer.php
│ ├── header.php
│ └── navbar.php
│ ├── pages
│ ├── about.php
│ └── index.php
│ ├── posts
│ ├── add.php
│ ├── edit.php
│ ├── index.php
│ └── show.php
│ └── users
│ ├── add.php
│ ├── changepassword.php
│ ├── index.php
│ ├── login.php
│ └── register.php
├── examples
├── mvc-apache2.conf
├── mvc-create-database.sql
└── readme.txt
├── public
├── .htaccess
├── css
│ ├── bootstrap.min.css
│ ├── font-awesome.min.css
│ ├── font-awesome
│ │ ├── HELP-US-OUT.txt
│ │ ├── css
│ │ │ ├── font-awesome.css
│ │ │ └── font-awesome.min.css
│ │ ├── fonts
│ │ │ ├── FontAwesome.otf
│ │ │ ├── fontawesome-webfont.eot
│ │ │ ├── fontawesome-webfont.svg
│ │ │ ├── fontawesome-webfont.ttf
│ │ │ ├── fontawesome-webfont.woff
│ │ │ └── fontawesome-webfont.woff2
│ │ ├── less
│ │ │ ├── animated.less
│ │ │ ├── bordered-pulled.less
│ │ │ ├── core.less
│ │ │ ├── fixed-width.less
│ │ │ ├── font-awesome.less
│ │ │ ├── icons.less
│ │ │ ├── larger.less
│ │ │ ├── list.less
│ │ │ ├── mixins.less
│ │ │ ├── path.less
│ │ │ ├── rotated-flipped.less
│ │ │ ├── screen-reader.less
│ │ │ ├── stacked.less
│ │ │ └── variables.less
│ │ └── scss
│ │ │ ├── _animated.scss
│ │ │ ├── _bordered-pulled.scss
│ │ │ ├── _core.scss
│ │ │ ├── _fixed-width.scss
│ │ │ ├── _icons.scss
│ │ │ ├── _larger.scss
│ │ │ ├── _list.scss
│ │ │ ├── _mixins.scss
│ │ │ ├── _path.scss
│ │ │ ├── _rotated-flipped.scss
│ │ │ ├── _screen-reader.scss
│ │ │ ├── _stacked.scss
│ │ │ ├── _variables.scss
│ │ │ └── font-awesome.scss
│ └── style.css
├── index.php
└── js
│ ├── bootstrap.min.js
│ ├── jquery-3.2.1.min.js
│ ├── main.js
│ └── popper.min.js
└── readme.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
2 |
--------------------------------------------------------------------------------
/.htaccess:
--------------------------------------------------------------------------------
1 |
2 | Options -Multiviews
3 | RewriteEngine On
4 | RewriteRule ^$ public/ [L]
5 | RewriteRule (.*)$ public/$1 [L]
6 |
--------------------------------------------------------------------------------
/app/.htaccess:
--------------------------------------------------------------------------------
1 | Options -Indexes
--------------------------------------------------------------------------------
/app/bootstrap.php:
--------------------------------------------------------------------------------
1 | 'PHP MVC Framework',
14 | 'description' => 'Simple social network built using PHP/MVC.'
15 | ];
16 | $this->view('pages/index', $data);
17 | }
18 |
19 | public function about()
20 | {
21 | $data = [
22 | 'title' => 'About Us',
23 | 'description' => 'App to share posts with other users'
24 | ];
25 | $this->view('pages/about',$data);
26 | }
27 | }
--------------------------------------------------------------------------------
/app/controllers/Posts.php:
--------------------------------------------------------------------------------
1 | postModel = $this->model('Post');
12 | $this->userModel = $this->model('User');
13 | }
14 |
15 | public function index()
16 | {
17 | $posts = $this->postModel->getPosts();
18 | $data = [
19 | 'posts' => $posts
20 | ];
21 | $this->view('posts/index', $data);
22 | }
23 |
24 |
25 | public function add()
26 | {
27 | if($_SERVER['REQUEST_METHOD']=='POST'){
28 | // Sanitize POST Array
29 | $_POST = filter_input_array(INPUT_POST,FILTER_SANITIZE_STRING);
30 |
31 | $data = [
32 | 'title' => trim($_POST['title']),
33 | 'body' => trim($_POST['body']),
34 | 'user_id' => $_SESSION['user_id'],
35 | 'title_err' => '',
36 | 'body_err' => ''
37 | ];
38 |
39 | // Validate
40 | if( empty($data['title']) ){
41 | $data['title_err'] = 'Please enter the title';
42 | }
43 | if( empty($data['body']) ){
44 | $data['body_err'] = 'Please enter the body';
45 | }
46 |
47 | // Make sure no errors
48 | if ( empty($data['title_err']) && empty($data['body_err']) ){
49 | // Validated
50 | if( $this->postModel->addPost($data) ){
51 | flash('post_message', 'Post Added');
52 | redirect('posts');
53 | } else{
54 | die('Something went wrong');
55 | }
56 | } else {
57 | // Load the view
58 | $this->view('posts/add', $data);
59 | }
60 |
61 | } else{
62 | $data = [
63 | 'title' => '',
64 | 'body' => ''
65 | ];
66 | $this->view('posts/add', $data);
67 | }
68 |
69 | }
70 |
71 |
72 |
73 | public function edit($id)
74 | {
75 | if($_SERVER['REQUEST_METHOD']=='POST'){
76 | // Sanitize POST Array
77 | $_POST = filter_input_array(INPUT_POST,FILTER_SANITIZE_STRING);
78 |
79 | $data = [
80 | 'id' => $id,
81 | 'title' => trim($_POST['title']),
82 | 'body' => trim($_POST['body']),
83 | 'user_id' => $_SESSION['user_id'],
84 | 'title_err' => '',
85 | 'body_err' => ''
86 | ];
87 |
88 | // Validate
89 | if( empty($data['title']) ){
90 | $data['title_err'] = 'Please enter the title';
91 | }
92 | if( empty($data['body']) ){
93 | $data['body_err'] = 'Please enter the body';
94 | }
95 |
96 | // Make sure no errors
97 | if ( empty($data['title_err']) && empty($data['body_err']) ){
98 | // Validated
99 | if( $this->postModel->updatePost($data) ){
100 | flash('post_message', 'Post Updated');
101 | redirect('posts');
102 | } else{
103 | die('Something went wrong');
104 | }
105 | } else {
106 | // Load the view
107 | $this->view('posts/edit', $data);
108 | }
109 |
110 | } else{
111 | // Get existing post from model
112 | $post = $this->postModel->getPostById($id);
113 |
114 | //Check for owner
115 | if( $post->user_id != $_SESSION['user_id'] ){
116 | redirect('posts');
117 | }
118 | $data = [
119 | 'id' => $post->id,
120 | 'title' => $post->title,
121 | 'body' => $post->body,
122 | 'title_err' => '',
123 | 'body_err' => ''
124 | ];
125 | $this->view('posts/edit', $data);
126 | }
127 |
128 | }
129 |
130 | public function show($id)
131 | {
132 | $post = $this->postModel->getPostById($id);
133 | $user = $this->userModel->getUserById($post->user_id);
134 | $data = [
135 | 'post' => $post,
136 | 'user' => $user
137 | ];
138 | $this->view('posts/show', $data);
139 | }
140 |
141 |
142 | public function delete($id)
143 | {
144 | if($_SERVER['REQUEST_METHOD']=='POST') {
145 | // Get existing post from model
146 | $post = $this->postModel->getPostById($id);
147 |
148 | //Check for owner
149 | if( $post->user_id != $_SESSION['user_id'] ){
150 | redirect('posts');
151 | }
152 | if( $this->postModel->deletePost($id) ){
153 | flash('post_message', 'Post removed');
154 | redirect('posts');
155 | } else {
156 | die('Something went wrong');
157 | }
158 |
159 | } else {
160 | redirect('posts');
161 | }
162 | } //end function
163 |
164 |
165 | }
--------------------------------------------------------------------------------
/app/controllers/Users.php:
--------------------------------------------------------------------------------
1 | userModel = $this->model('User');
9 | $this->postModel = $this->model('Post');
10 | }
11 |
12 | public function index()
13 | {
14 | if(!isLoggedIn() ){
15 | redirect('users/login');
16 | }
17 | $users = $this->userModel->getUsers();
18 | $data = [
19 | 'users' => $users
20 | ];
21 | return $this->view('users/index', $data);
22 | }
23 |
24 | public function register()
25 | {
26 | //Check for POST
27 | if ($_SERVER['REQUEST_METHOD']=='POST') {
28 | // Sanitize POST Data
29 | $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
30 |
31 | // Process form
32 | $data = [
33 | 'name' => trim($_POST['name']),
34 | 'email' => trim($_POST['email']),
35 | 'password' => trim($_POST['password']),
36 | 'confirm_password' => trim($_POST['confirm_password']),
37 | 'name_err' => '',
38 | 'email_err' => '',
39 | 'password_err' => '',
40 | 'confirm_password_err' => ''
41 | ];
42 |
43 | // Validate email
44 | if ( empty($data['email']) ) {
45 | $data['email_err'] = 'Please inform your email';
46 | } else {
47 | // Check email
48 | if ( $this->userModel->getUserByEmail($data['email']) ) {
49 | $data['email_err'] = 'Email is already in use. Choose another one!';
50 | }
51 | }
52 |
53 | // Validate Name
54 | if ( empty($data['name']) ) {
55 | $data['name_err'] = 'Please inform your name';
56 | }
57 |
58 | // Validate Password
59 | if ( empty($data['password']) ) {
60 | $data['password_err'] = 'Please inform your password';
61 | } elseif ( strlen($data['password']) < 6 ) {
62 | $data['password_err'] = 'Password must be at least 6 characters';
63 | }
64 |
65 | // Validate Confirm Password
66 | if ( empty($data['confirm_password']) ) {
67 | $data['confirm_password_err'] = 'Please inform your password';
68 | } else if ( $data['password'] != $data['confirm_password'] ) {
69 | $data['confirm_password_err'] = 'Password does not match!';
70 | }
71 |
72 | //Make sure errors are empty
73 | if ( empty($data['name_err']) && empty($data['email_err']) && empty($data['password_err']) && empty($data['confirm_password_err']) ) {
74 | // Hash Password
75 | $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
76 |
77 | if ( $this->userModel->register($data) ) {
78 | flash('register_success','You are now registered! You !');
79 | $this->login();
80 | //redirect('posts/login');
81 | } else {
82 | die ('Something wrong');
83 | }
84 | } else{
85 | // Load view with errors
86 | $this->view('users/register',$data);
87 | }
88 | } else {
89 | // Init data
90 | $data = [
91 | 'name' => '',
92 | 'email' => '',
93 | 'password' => '',
94 | 'confirm_password' => '',
95 | 'name_err' => '',
96 | 'email_err' => '',
97 | 'password_err' => '',
98 | 'confirm_password_err' => ''
99 | ];
100 |
101 | // Load view
102 | $this->view('users/register', $data);
103 | }
104 | }
105 |
106 | public function login()
107 | {
108 | //Check for POST
109 | if ($_SERVER['REQUEST_METHOD']=='POST') {
110 | // Process form
111 | // Sanitize POST Data
112 | $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
113 |
114 | // Process form
115 | $data = [
116 | 'email' => trim($_POST['email']),
117 | 'password' => trim($_POST['password']),
118 | 'email_err' => '',
119 | 'password_err' => '',
120 | ];
121 |
122 | // Validate email
123 | if ( empty($data['email']) ) {
124 | $data['email_err'] = 'Please inform your email';
125 | } else if (! $this->userModel->getUserByEmail($data['email']) ) {
126 | // User not found
127 | $data['email_err'] = 'No user found!';
128 | }
129 |
130 | // Validate password
131 | if ( empty($data['password']) ) {
132 | $data['password_err'] = 'Please inform your password';
133 | }
134 |
135 | //Make sure are empty
136 | if ( empty($data['email_err']) && empty($data['password_err']) ) {
137 | // Validated
138 | // Check and set logged in user
139 | $userAuthenticated = $this->userModel->login($data['email'], $data['password']);
140 | if ( $userAuthenticated) {
141 | // Create session
142 | $this->createUserSession($userAuthenticated);
143 | } else {
144 | $data = [
145 | 'email' => trim($_POST['email']),
146 | 'password' => '',
147 | 'email_err' => 'Email or Password are incorrect',
148 | 'password_err' => 'Email or Password are incorrect',
149 | ];
150 | $this->view('users/login', $data);
151 | }
152 | } else {
153 | // Load view with errors
154 | $this->view('users/login',$data);
155 | }
156 | } else {
157 | // Init data
158 | $data = [
159 | 'email' => '',
160 | 'password' => '',
161 | 'email_err' => '',
162 | 'password_err' => '',
163 | ];
164 | // Load view
165 | $this->view('users/login', $data);
166 | }
167 | }
168 |
169 | public function logout()
170 | {
171 | unset($_SESSION['user_id']);
172 | unset($_SESSION['user_mail']);
173 | unset($_SESSION['user_name']);
174 | session_destroy();
175 | redirect('users/login');
176 | }
177 |
178 | public function createUserSession($user)
179 | {
180 | $_SESSION['user_id'] = $user->id;
181 | $_SESSION['user_email'] = $user->email;
182 | $_SESSION['user_name'] = $user->name;
183 | redirect('posts');
184 | }
185 |
186 | public function isLoggedIn()
187 | {
188 | if ( isset($_SESSION['user_id']) && isset($_SESSION['user_name']) && isset($_SESSION['user_email'])) {
189 | return true;
190 | } else {
191 | return false;
192 | }
193 | }
194 |
195 | public function changePassword()
196 | {
197 | if(!isLoggedIn() ){
198 | redirect('users/login');
199 | }
200 |
201 | //Check for POST
202 | if ($_SERVER['REQUEST_METHOD']=='POST') {
203 | // Sanitize POST Data
204 | $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
205 |
206 | // Process form
207 | $data = [
208 | 'email' => $_SESSION['user_email'],
209 | 'password_old' => trim($_POST['password_old']),
210 | 'password' => trim($_POST['password']),
211 | 'confirm_password' => trim($_POST['confirm_password']),
212 | 'password_old_err' => '',
213 | 'password_err' => '',
214 | 'confirm_password_err' => ''
215 | ];
216 |
217 | // Validate Password Old
218 | if ( empty($data['password_old']) ) {
219 | $data['password_old_err'] = 'Please inform your old password';
220 | } elseif ( strlen($data['password_old']) < 6 ) {
221 | $data['password_old_err'] = 'Password old must be at least 6 characters';
222 | } else if (! $this->userModel->checkPassword($data['email'], $data['password_old']) ) {
223 | $data['password_old_err'] = 'Your old password is wrong!';
224 | }
225 |
226 | // Validate Password
227 | if ( empty($data['password']) ) {
228 | $data['password_err'] = 'Please inform your password';
229 | } elseif ( strlen($data['password']) < 6 ) {
230 | $data['password_err'] = 'Password must be at least 6 characters';
231 | }
232 |
233 | // Validate Confirm Password
234 | if ( empty($data['confirm_password']) ) {
235 | $data['confirm_password_err'] = 'Please confirm your password';
236 | } else if ( $data['password'] != $data['confirm_password'] ) {
237 | $data['confirm_password_err'] = 'Password does not match!';
238 | }
239 |
240 | //Make sure errors are empty
241 | if ( empty($data['password_old_err']) && empty($data['password_err']) && empty($data['confirm_password_err']) ) {
242 | // Hash Password
243 | $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
244 |
245 | if ( $this->userModel->updatePassword($data) ) {
246 | flash('register_success','Password updated!');
247 | redirect('posts');
248 | } else {
249 | die ('Something wrong');
250 | }
251 | } else{
252 | // Load view with errors
253 | $this->view('users/changepassword',$data);
254 | }
255 | } else {
256 | // Init data
257 | $data = [
258 | 'email' => $_SESSION['user_email'],
259 | 'password_old' => '',
260 | 'password' => '',
261 | 'confirm_password' => '',
262 | 'password_old_err' => '',
263 | 'password_err' => '',
264 | 'confirm_password_err' => ''
265 | ];
266 |
267 | // Load view
268 | $this->view('users/changepassword', $data);
269 | }
270 | }
271 |
272 | public function add()
273 | {
274 | //Check for POST
275 | if ($_SERVER['REQUEST_METHOD']=='POST') {
276 | // Sanitize POST Data
277 | $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
278 |
279 | // Process form
280 | $data = [
281 | 'name' => trim($_POST['name']),
282 | 'email' => trim($_POST['email']),
283 | 'password' => trim($_POST['password']),
284 | 'confirm_password' => trim($_POST['confirm_password']),
285 | 'name_err' => '',
286 | 'email_err' => '',
287 | 'password_err' => '',
288 | 'confirm_password_err' => ''
289 | ];
290 |
291 | // Validate email
292 | if ( empty($data['email']) ) {
293 | $data['email_err'] = 'Please inform the email user';
294 | } else {
295 | // Check email
296 | if ( $this->userModel->getUserByEmail($data['email']) ) {
297 | $data['email_err'] = 'This email already exists in the database.';
298 | }
299 | }
300 |
301 | // Validate Name
302 | if ( empty($data['name']) ) {
303 | $data['name_err'] = 'Please inform the name of user';
304 | }
305 |
306 | // Validate Password
307 | if ( empty($data['password']) ) {
308 | $data['password_err'] = 'Please inform the password';
309 | } elseif ( strlen($data['password']) < 6 ) {
310 | $data['password_err'] = 'Password must be at least 6 characters';
311 | }
312 |
313 | // Validate Confirm Password
314 | if ( empty($data['confirm_password']) ) {
315 | $data['confirm_password_err'] = 'Please inform the password';
316 | } else if ( $data['password'] != $data['confirm_password'] ) {
317 | $data['confirm_password_err'] = 'Password does not match!';
318 | }
319 |
320 | //Make sure errors are empty
321 | if ( empty($data['name_err']) && empty($data['email_err']) && empty($data['password_err']) && empty($data['confirm_password_err']) ) {
322 | // Hash Password
323 | $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
324 |
325 | if ( $this->userModel->addUser($data) ) {
326 | flash('user_message','User created with success!');
327 | redirect('users/index');
328 | } else {
329 | die ('Something wrong');
330 | }
331 | } else{
332 | // Load view with errors
333 | $this->view('users/add',$data);
334 | }
335 | } else {
336 | // Init data
337 | $data = [
338 | 'name' => '',
339 | 'email' => '',
340 | 'password' => '',
341 | 'confirm_password' => '',
342 | 'name_err' => '',
343 | 'email_err' => '',
344 | 'password_err' => '',
345 | 'confirm_password_err' => ''
346 | ];
347 |
348 | // Load view
349 | $this->view('users/add', $data);
350 | }
351 | }
352 |
353 | public function delete($id)
354 | {
355 | if($_SERVER['REQUEST_METHOD']=='POST') {
356 | // Get existing post from model
357 | $user = $this->userModel->getUserById($id);
358 |
359 | //Check if the user is logged
360 | if( $user->id == $_SESSION['user_id'] ){
361 | flash('user_message', 'You cannot delete your own user!');
362 | redirect('users');
363 | }
364 |
365 | //Check if the user has posts
366 | $row = $this->postModel->getPostByUserId($id);
367 | if ($row->total > 0 ) {
368 | flash('user_message', 'You cannot delete a user with published posts!');
369 | redirect('users');
370 | }
371 |
372 | if( $this->userModel->deleteUser($id) ){
373 | flash('user_message', 'The user was removed with success!');
374 | redirect('users');
375 | } else {
376 | flash('user_message', 'An erro ocurred when delete user');
377 | redirect('users');
378 | }
379 |
380 | } else {
381 | redirect('users');
382 | }
383 | } //end function
384 |
385 | }
--------------------------------------------------------------------------------
/app/helpers/datetime_helper.php:
--------------------------------------------------------------------------------
1 |
7 | function flash($name = '', $message = '', $class = 'alert alert-success alert-dismissible fade show')
8 | {
9 | if (! empty($name) ) {
10 | if (! empty($message) && empty($_SESSION['name']) ) {
11 | if ( !empty($_SESSION[$name]) ) {
12 | unset( $_SESSION[$name] );
13 | }
14 | if (! empty($_SESSION[$name. '_class']) ) {
15 | unset( $_SESSION[$name. '_class'] );
16 | }
17 | $_SESSION[$name] = $message;
18 | $_SESSION[$name. '_class'] = $class;
19 | } elseif ( empty($mesage) && !empty($_SESSION[$name]) ) {
20 | $class = !empty($_SESSION[$name . '_class']) ? $_SESSION[$name . '_class'] : '';
21 | echo '
' . $_SESSION[$name] . '
22 |
24 |
';
25 |
26 | unset($_SESSION[$name]);
27 | unset($_SESSION[$name.'_class']);
28 | }
29 | }
30 | }
31 |
32 |
33 | function isLoggedIn(){
34 | if ( isset($_SESSION['user_id']) && isset($_SESSION['user_name']) && isset($_SESSION['user_email'])) {
35 | return true;
36 | } else {
37 | return false;
38 | }
39 | }
--------------------------------------------------------------------------------
/app/helpers/url_helper.php:
--------------------------------------------------------------------------------
1 | getUrl();
17 |
18 | // Look in controllers for first value
19 | if (file_exists('../app/controllers/' . ucwords($url[0]) . '.php')) {
20 | // If exists, set as controller
21 | $this->currentController = ucwords( $url[0] );
22 | // Unset 0 url
23 | unset($url[0]);
24 | }
25 |
26 | // Require the controller
27 | require_once '../app/controllers/' . $this->currentController . '.php';
28 |
29 | // Instantiate controller class
30 | $this->currentController = new $this->currentController;
31 |
32 | // Check for second part of url
33 | if (isset($url[1])) {
34 | if(method_exists($this->currentController, $url[1]))
35 | {
36 | $this->currentMethod = $url[1];
37 | unset($url[1]);
38 | }
39 | }
40 |
41 | // Get params
42 | $this->params = $url ? array_values($url) : [];
43 |
44 | //Call a callback with array of params
45 | call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
46 | }
47 |
48 | public function getUrl()
49 | {
50 | if (isset($_GET['url'])) {
51 | $url = rtrim($_GET['url'],'/');
52 | $url = filter_var($url,FILTER_SANITIZE_URL);
53 | $url = explode('/',$url);
54 | return $url;
55 | }
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/app/libraries/Database.php:
--------------------------------------------------------------------------------
1 | host . ';charset=' . $this->charset . ';dbname=' . $this->dbname;
25 | $options = array(
26 | PDO::ATTR_PERSISTENT => true,
27 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
28 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
29 | PDO::ATTR_CASE => PDO::CASE_LOWER
30 | );
31 |
32 | //Create PDO Instance
33 | try{
34 | $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
35 | } catch(PDOException $e){
36 | $this->error = $e->getMessage();
37 | echo $this->error;
38 | }
39 | }
40 |
41 | // Prepare statement query
42 | public function query($sql)
43 | {
44 | $this->stmt = $this->dbh->prepare($sql);
45 | }
46 |
47 | // Bind values
48 | public function bind($param, $value, $type = null)
49 | {
50 | if(is_null($type)){
51 | switch(true){
52 | case is_int($value):
53 | $type = PDO::PARAM_INT;
54 | break;
55 | case is_bool($value):
56 | $type = PDO::PARAM_BOOL;
57 | break;
58 | case is_null($value):
59 | $type = PDO::PARAM_NULL;
60 | break;
61 | default:
62 | $type = PDO::PARAM_STR;
63 | break;
64 | }
65 | }
66 | $this->stmt->bindValue($param, $value, $type);
67 | }
68 |
69 | // Execute the prepared statement
70 | public function execute()
71 | {
72 | return $this->stmt->execute();
73 | }
74 |
75 | // Get result set as array of objects
76 | public function resultSet(){
77 | $this->execute();
78 | return $this->stmt->fetchAll();
79 | }
80 |
81 | // Get single record as object
82 | public function single(){
83 | $this->execute();
84 | return $this->stmt->fetch();
85 | }
86 |
87 | public function rowCount(){
88 | return $this->stmt->rowCount();
89 | }
90 |
91 | }
--------------------------------------------------------------------------------
/app/models/Post.php:
--------------------------------------------------------------------------------
1 | db = new Database();
11 | }
12 |
13 | public function getPosts()
14 | {
15 | $this->db->query('select p.id as post_id, p.user_id, u.name, u.email, p.title, p.body, p.created_at
16 | from posts p
17 | left join users u on u.id = p.user_id
18 | order by p.created_at desc');
19 | return $this->db->resultSet();
20 |
21 | }
22 |
23 | public function getPostById($id)
24 | {
25 | $this->db->query('select * from posts where id = :id');
26 | $this->db->bind(':id',$id);
27 | return $this->db->single();
28 | }
29 |
30 | public function getPostByUserId($user_id)
31 | {
32 | $this->db->query('select count(*) as total from posts where user_id = :user_id');
33 | $this->db->bind(':user_id',$user_id);
34 | return $this->db->single();
35 | }
36 |
37 | public function addPost($data)
38 | {
39 | $this->db->query('INSERT INTO posts (user_id, title, body) values (:user_id, :title, :body)');
40 | // Bind values
41 | $this->db->bind(':user_id', $data['user_id']);
42 | $this->db->bind(':title', $data['title']);
43 | $this->db->bind(':body', $data['body']);
44 |
45 | // Execute
46 | if( $this->db->execute() ){
47 | return true;
48 | } else {
49 | return false;
50 | }
51 | }
52 |
53 | public function updatePost($data)
54 | {
55 | $this->db->query('UPDATE posts SET title = :title, body = :body where id = :id');
56 | // Bind values
57 | $this->db->bind(':id', $data['id']);
58 | $this->db->bind(':title', $data['title']);
59 | $this->db->bind(':body', $data['body']);
60 |
61 | // Execute
62 | if( $this->db->execute() ){
63 | return true;
64 | } else {
65 | return false;
66 | }
67 | }
68 |
69 | public function deletePost($id)
70 | {
71 | $this->db->query('DELETE FROM posts where id = :id');
72 | // Bind values
73 | $this->db->bind(':id', $id);
74 |
75 | // Execute
76 | if( $this->db->execute() ){
77 | return true;
78 | } else {
79 | return false;
80 | }
81 | }
82 | }
--------------------------------------------------------------------------------
/app/models/User.php:
--------------------------------------------------------------------------------
1 | db = new Database();
10 | }
11 |
12 | public function register($data)
13 | {
14 | $this->db->query('INSERT INTO users (name, email, password) values (:name, :email, :password)');
15 | // Bind values
16 | $this->db->bind(':name', $data['name']);
17 | $this->db->bind(':email', $data['email']);
18 | $this->db->bind(':password', $data['password']);
19 | // Execute
20 | if ( $this->db->execute() ) {
21 | return true;
22 | } else {
23 | return false;
24 | }
25 | }
26 |
27 | public function addUser($data)
28 | {
29 | $this->db->query('INSERT INTO users (name, email, password) values (:name, :email, :password)');
30 | // Bind values
31 | $this->db->bind(':name', $data['name']);
32 | $this->db->bind(':email', $data['email']);
33 | $this->db->bind(':password', $data['password']);
34 | // Execute
35 | if ( $this->db->execute() ) {
36 | return true;
37 | } else {
38 | return false;
39 | }
40 | }
41 |
42 | public function deleteUser($id)
43 | {
44 | $this->db->query('DELETE FROM users where id = :id');
45 | // Bind values
46 | $this->db->bind(':id', $id);
47 |
48 | // Execute
49 | if( $this->db->execute() ){
50 | return true;
51 | } else {
52 | return false;
53 | }
54 | }
55 |
56 | public function login($email,$password)
57 | {
58 | $this->db->query('SELECT * from users where email = :email');
59 | $this->db->bind(':email', $email);
60 | $row = $this->db->single();
61 |
62 | $hashed_password = $row->password;
63 | if ( password_verify($password,$hashed_password) ) {
64 | return $row;
65 | } else {
66 | return false;
67 | }
68 | }
69 |
70 | public function checkPassword($email,$password)
71 | {
72 | $this->db->query('SELECT * from users where email = :email');
73 | $this->db->bind(':email', $email);
74 | $row = $this->db->single();
75 |
76 | $hashed_password = $row->password;
77 | if ( password_verify($password,$hashed_password) ) {
78 | return $row;
79 | } else {
80 | return false;
81 | }
82 | }
83 |
84 | public function getUserByEmail($email)
85 | {
86 | $this->db->query('SELECT * FROM users WHERE email = :email');
87 | // Bind values
88 | $this->db->bind(':email', $email);
89 | $this->db->single();
90 |
91 | // Check row
92 | if ( $this->db->rowCount() > 0 ) {
93 | return true;
94 | } else {
95 | return false;
96 | }
97 | }
98 |
99 | public function getUserById($id)
100 | {
101 | $this->db->query('SELECT * FROM users WHERE id = :id');
102 | // Bind values
103 | $this->db->bind(':id', $id);
104 | return $this->db->single();
105 | }
106 |
107 | public function updatePassword($data)
108 | {
109 | $this->db->query('UPDATE users SET password = :password where email = :email');
110 | // Bind values
111 | $this->db->bind(':password', $data['password']);
112 | $this->db->bind(':email', $data['email']);
113 | // Execute
114 | if( $this->db->execute() ){
115 | return true;
116 | } else {
117 | return false;
118 | }
119 | }
120 |
121 | public function getUsers()
122 | {
123 | $this->db->query('SELECT * FROM users');
124 | return $this->db->resultSet();
125 |
126 | } }
--------------------------------------------------------------------------------
/app/views/inc/footer.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |