-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
executable file
·373 lines (258 loc) · 16.3 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
include 'lib/bones.php';
#require_once 'functions/app_functions.php';
// we locked down our _users database, so we could secure our user data, meaning that any time we deal with the _users database,
// we need to provide the administrator login. For this, we'll add PHP constants for the user and the password at the top of the
// index.php file, so that we can reference it any time we need to perform an administrator function.
// For this, we'll add PHP constants for the user and the password at the top of the index.php file, so that we can
// reference it any time we need to perform an administrator function. pag 127
define('ADMIN_USER', 'yozaira');
define('ADMIN_PASSWORD', '123');
// - Our two get routes are now clean, little functions, including our route and a function that will act as our callback function
// - Once the function is executed, we are using echo to display the simple text
// - When a route is matched and a callback is executed from Bones, the instance of Bones is returned as the variable $app, which
// can be used anywhere in the callback function
// For the root route, we used our new function set to pass a variable with the key of 'message' and its contents being
// 'Welcome Back!'
get('/', function($app) {
// the two parameters in set() method - $index and $value - are passed to $var array (see set() in bones.php)
// in render method we looped through public $var = array (in bone.php).
$app->set('message', 'Welcome Back!');
$app->render('home'); // We are then going to tell Bones to render the home view, allowing us to see the message.
});
// This is the home page for administration area of the site
get('admin/', function($app) {
// the two parameters in set() method - $index and $value - are passed to $var array (see set() in bones.php)
// in render method we looped through public $var = array (in bone.php).
$app->set('message', 'Welcome to admin area!');
$app->render('admin/home'); // We are then going to tell Bones to render the home view, allowing us to see the message.
});
get('/signup', function($app) {
$app->render('admin/signup');
});
// Define a post method in index.php, so the form can be submitted.
// Set all of the values for CouchDB user documents:
// collect the simple fields: full_name, email, and roles. The fields full_name and email will come directly from the form
// submission, and roles we will set to an empty array because this user has no special permissions
post('/signup', function($app) {
$author = new Author();
$author->full_name = $app->form('full_name');
$author->email = $app->form('email');
// roles we will set to an empty array because this user has no special permissions.
// $author->roles = array();
// call signup method
$author->signup($app->form('username'), $app->form('password'));
// pag 143
$app->set('success', 'Thanks for Signing Up ' . $author->full_name . '!');
// Finally, let's close the user signup function and render the home page.
$app->render('home');
});
get('/login', function($app) {
$app->render('admin/login');
});
// Define a post method in index.php, so the form can be submitted.
post('/login', function($app) {
$author = new Author();
$author->name = $app->form('username'); // bones.php
$author->login($app->form('password')); // bones.php
$app->set('success', 'You are now logged in!');
$app->render('admin/home');
});
get('/logout', function($app) {
Author::logout();
$app->redirect('/');
// This function is included in bones.php. It will allow us to redirect a user to a route by using make_route.
});
/* This did not work
// Open index.php, and create a function called get_user_profile that takes $app as a parameter, and
// place it above /user/:username route
// Copy the code from /user/:username/:skip into this function. But, this time, instead of just
// passing $app->request('skip'), let's check if it exists. If it exists, let's pass it to the get_posts_by_user
// function. If it doesn't exist, we'll just pass it 0.
function get_author_profile($app) {
$app->set('author', Author::get_by_username($app->request('username')));
$app->set('isCurrentAuthor', ($app->request('username') == Author::currentAuthor() ? true : false));
$app->set('posts', Post::get_posts_by_author($app->request('username'), ($app->request('skip') ? $app->request('skip') : 0)));
$app->set('post_count', Post::get_post_count_by_author($app->request('username')));
$app->render('admin/_posts', false);
}
// Finally, let's clean up both of our profile functions so that both of them just
// call the get_user_profile function.
get('/admin/:username', function($app) {
get_author_profile($app);
$app->render('admin/profile');
});
get('/admin/:username/:skip', function($app) {
get_author_profile($app);
$app->render('admin/_posts', false);
});
*/
// We are going to create a route so that people can see a profile by going to a unique URL. This will be the first time
// that we'll really utilize our routing system's ability to handle route variables.
get('/admin/:username', function($app) {
// use the route variable :username to tell us the username that we want to find;
// we'll pass this to the findByAuthorname function we created in the Author class.
$app->set('author', Author::findByAuthorname($app->request('username')));
// add a variable called 'isCurrentAuthor' that will determine if the profile that you are viewing is equal to the
// currently logged-in user. pag 171
// if the username passed from the route is equal to that of the currently logged-in user, then return true,
// otherwise return false
$app->set('isCurrentAuthor', ($app->request('username') == Author::currentAuthor() ? true : false));
// # add the code to pass the returned posts from our get_posts_by_user function to a variable for our view to access. pag 225
$app->set('posts', Post::get_posts_by_author($app->request('username')));
// # Add code to pass the value from the get_post_count_by_user function to a variable that our view can access. page 205
$app->set('post_count', Post::get_post_count_by_author($app->request('username')));
// Lastly, render the user/profile.php view
$app->render('admin/profile');
// Open views/user/profile.php, and add the corresponding code right below the Create a new post text area
//so that we can display a list of posts on the user profile page. pag 199
});
// Now that we have updated our function to include skip and limit, let's create a new route in index.php that's similar to
// the user/:username route but takes in a route variable of skip to drive the pagination. In this route, we're just going
// to return _posts partially, instead of the whole layout:
get('/author/:username/:skip', function($app) {
$app->set('author', Author::get_by_username($app->request('username')));
$app->set('isCurrentAuthor', ($app->request('username') == Author::currentAuthor() ? true : false));
#$app->set('posts', Post::get_posts_by_author($app->request('username'), $app->request('skip')));
$app->set('posts', Post::get_posts_by_author($app->request('username'), ($app->request('skip') ? $app->request('skip') : 0)));
$app->set('post_count', Post::get_post_count_by_author($app->request('username')));
$app->render('admin/_posts', false);
});
// Display add post form
get('/add-post', function($app) {
$app->render('admin/add-post');
});
// Create a post:
// The action executed on add-post.php form has to point to the same route used in this post(). Both routes -- make-route
// on add-post.php and post() func on index.php -- has to be the same, so the add-post form knows what is going
// to execute on submition. page 181
// This post route accepts the value of the passed value content and uses the create function on our Post class
// to actually create the post. Once the post is created, we'll redirect the user back to the admin-hom or to their profile.
post('/create-post', function($app) {
// check that the user is authenticated whe creating a post.
if (Author::isAuthenticated()) {
$post = new Post();
$post->content = trim($app->form('content') ); // content referes to the name value of the textarea
$post->title = $app->form('title');
// create the post by calling the public create function.
$post->create();
// added by me :)
$app->set('success', 'Your post '. ucwords($post->title). ' was created succesfully !');
$app->render('admin-home');
// We can also redirect the user back to his/her own profile
# $app->redirect('/admin/' . Author::currentAuthor());
}
else {
// If it turns out that the user is not authenticated, our application will forward them to the user login
// page with an error message.
$app->set('error', 'You must be logged in to do that.');
$app->render('admin/login');
}
});
// CouchDB has a really interesting way of listing and handling documents. In order to get into that discussion,
// we'll need to define how to use Design Documents for views and validation. page 185
// With this route, we can trigger the deletion of posts from our profile page. page 209
// Then update views/user/profile.php page, and add the route, so that when users click on the delete link, they hit our route,
// and the necessary variables are passed.
// UPDATE: (page 221)
// Let's change the route to use a delete method by changing get to delete.
get('/post/delete/:id/:rev', function($app) {
# delete('/post/delete/:id/:rev', function($app) { // it does not work...
$post = new Post();
$post->_id = $app->request('id');
$post->_rev = $app->request('rev');
$post->delete();
// UPDATE (page 221): Then, remove the success variable and the redirection code, because we'll no longer need them
$app->set('success', 'Your post has been deleted');
$app->redirect('/admin/' . Author::currentAuthor() ); // is not redirecting!!!
});
// Now that we have the backend support to delete the posts, let's add a route in our index.php file that
// accepts _id and _rev.
// DISPLAY SINGLE POST
get('/post/:id/:title', function($app) {
$bones = new Bones();
# $app->set('single_post', Post::findByPostId($app->request('id') ) ); // DONT WORK
echo ' <!DOCTYPE html><html lang="en">
<head>
<title>slslsls</title>
<link href="/verge3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="/verge3/css/master.css" rel="stylesheet" type="text/css" />
<link href="/verge3/css/bootstrap-responsive.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<div class="row"> <div class="span8">';
foreach ($bones->couch->get('_design/myblog/_view/posts_by_id')->body->rows as $post_by_id) {
// echo $post_by_id->key.'<br>'; // debug
if ($post_by_id->key == $app->request('id') && $post_by_id->value->title == $app->request('title') ) {
echo '<h2>'.$post_by_id->value->title.'</h2>';
echo '<p>'.$post_by_id->value->author.'</p>';
echo '<p>'.$post_by_id->value->date_created.'</p>';
echo '<p>'.$post_by_id->value->content.'</p>';
//$app->set('author', Author::findByAuthorname($app->request($post_by_id->value->author)));
$app->set('currentPostId', 'Current ID: '.$app->request('id') ).'<br>';
$app->set('currentPostid1','Current Postid: ' .$post_by_id->key ).'<br>';
$app->set('currentTitle1', 'Current Title: ' .$post_by_id->value->title ).'<br>';
}
}
echo $currentPostId;
echo '<div><div>';
$app->render('/posts-content/'.$post_by_id->key);
// $app->render('/comments');
// $app->render('/posts-content/post-test');
});
post('/create-comment', function($app) {
// check that the user is authenticated whe creating a post.
$comment = new PostComment();
$comment->content = $app->form('content'); // content referes to the name value of the textarea
$comment->username = $app->form('username');
$comment->email = $app->form('email');
$comment->postId = $currentPostId;
// create the post by calling the public create function.
$comment->create_comment();
// added by me :)
$app->set('success', 'Thank you for your comment !');
// $app->render('/posts-content/'.$post_by_id->key);
$app->render('/comments');
});
/*
// This can be used to display more content and different sections for the site
get('post/:id', function($app) {
// the two parameters in set() method - $index and $value - are passed to $var array (see set() in bones.php)
// in render method we looped through public $var = array (in bone.php).
$app->set('message', 'Welcome to admin area!');
$app->render('posts-content/all_posts');
// We are then going to tell Bones to render the home view, allowing us to see the message.
});
*/
// This view can be used to display more content and different sections for the site
get('posts-content/', function($app) {
// the two parameters in set() method - $index and $value - are passed to $var array (see set() in bones.php)
// in render method we looped through public $var = array (in bone.php).
$app->set('message', 'Welcome to admin area!');
$app->render('posts-content/single_post');
// We are then going to tell Bones to render the home view, allowing us to see the message.
});
// This function was created outside the Bones class and it can be called outside anywhere.
// Has to be executed at the bottom of our index.php file after all of our routes. It serves as a "clean up"
// function that will be executed if no routes match up. If no routes match, resolve will display a 404 error to the visitor
// and terminate the current script.
resolve();
/* Test
echo '<pre>'; print_r( $_SERVER['PHP_SELF']); echo '<pre>';
$path = '';
// $path = '/ ';
// $path = '/user/';
$url = explode("/", $_SERVER['PHP_SELF']);
echo '<pre>'; print_r($url); echo '<pre>';
echo 'index 1: '. $url[1].'<br/>';
echo 'index 2: '.$url[2].'<br/>';
if ($url[1] == "index.php") {
echo $path;
}
else {
echo 'Path: /' . $url[1] . $path.'<br/>';
}
$route_segments = explode('/', trim($this->route, '/'));
echo '<pre>'; print_r(route_segments); echo '<pre>';
*/