Show sourcecode

The following files exists in this folder. Click to view.

Foogler_blog/src

CHTMLPage.php
FDestroySession.php

CHTMLPage.php

553 lines ASCII Windows (CRLF)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php
//--------------------------------
//CHTMLPage.php
//Author: Susanne Lindberg
//
//Class for printing HTML for Foogler blog
//--------------------------------

class CHTMLPage
{
protected 
$iMenu;
protected 
$iStylesheet;

//---------------------------------
//Constructor
public function __construct($aSheet WS_STYLESHEET
{
  
$this->iStylesheet $aSheet//Gets chosen stylesheet file
  
$this->iMenu unserialize(WS_MENU); //Gets menu from config file ----------
}

//---------------------------------
//Destructor
public function __destruct()
{

}

//---------------------------------
//Prints head with chosen title
public function printHTMLHeader($aTitle WS_TITLE)
{
  echo <<<END
<!doctype html>
<html lang=sv>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" type="text/css" href="stylesheet.css" />    
    <link rel="shortcut icon" href="images/FooglerIcon.png" type="image/x-icon" />
    <title>
{$aTitle}</title>
  </head>
END;
}

//---------------------------------
//Prints header with chosen headline
public function printPageHeader()
{
  
$menuHTML "";
  foreach(
$this->iMenu as $key => $value)
  {
    
$menuHTML .="\n     <li><a href='{$value}'>{$key}</a></li>";
  }
  
  
$htmlLoggedInName $this->getLoggedInName(); //Gets name of logged in user
  
$htmlLoginMenu $this->getLoginLogoutMenu(); //Gets menu for logging in / out etc.
  
  
echo <<<END
  
  <body>
    <div class="header">
      <a href="."><img class="logo" src="images/FooglerLogo.png" alt="Foogler Blog" /></a>
      
{$htmlLoggedInName}
      
      <img class="line" src="images/verticalLine.jpg" alt="" />
      
      <ul class="menuList">
        
{$htmlLoginMenu}
        
{$menuHTML}
      </ul>  
    </div><!-- header -->
END;
}

//---------------------------------
//Prints body with chosen text
public function printPageBody($aBody)
{
  
$htmlErrorMessage $this->getErrorMessage(); //Gets error message from session variable
  
  
echo <<<END
  
    <div class="postContainer">
      
{$aBody}
    </div><!-- postContainer -->
END;
}

//---------------------------------
//Prints left column with list of tags
public function printTagList()
{
  
//-----------------------
  //Handles DB stuff
  
$mysqli = new mysqli(DB_HOSTDB_USERDB_PASSWORDDB_DATABASE); //New DB object
  
$tableTag   DB_PREFIX 'Tag';
  
$tablePostTag   DB_PREFIX 'PostTag';
  
  if (
mysqli_connect_error()) {
     echo 
"Connect failed: ".mysqli_connect_error()."<br>";
     exit();
  }
  
$mysqli->set_charset("utf8");
  
  
$query = <<<END
  --
  -- Gets name of and counts times used of all tags
  --
  SELECT 
    tagName, 
    COUNT(idTag) AS 'noOfUsed' 
  FROM 
{$tableTag}
  JOIN 
{$tablePostTag} ON (PostTag_idTag = idTag) 
  GROUP BY tagName
  ORDER BY tagName ASC;
END;
  
//Performs query
  
$res $mysqli->query($query) or die("Could not query database"); 

  
//HTML
  
echo <<<END
  
  <ul class="tagList">
  <li style="margin-left:-20px;">Tags:</li>
END;
  
  
//For each returned row, prints list item 
  
while($row $res->fetch_object()) {
    echo 
"\n    <li><a href='?p=index&amp;tag={$row->tagName}'>{$row->tagName} ($row->noOfUsed)</a></li>";
  }
  
$res->close();
  
$mysqli->close(); //Closes DB connection

  
echo <<<END
  </ul>
END;
}

//---------------------------------
//Prints right column
public function printRightColumn()
{
  
//-----------------------
  //DB stuff
  
$mysqli = new mysqli(DB_HOSTDB_USERDB_PASSWORDDB_DATABASE); //New DB object
  
$tablePost   DB_PREFIX 'Post';
  
$tableAuthor   DB_PREFIX 'Author';
  
  if (
mysqli_connect_error()) {
     echo 
"Connect failed: ".mysqli_connect_error()."<br>";
     exit();
  }
  
$mysqli->set_charset("utf8");
  
  
//------------------------
  //SQL queries
  
$query = <<<END
  --
  -- Gets titles of ten latests posts
  --
  SELECT idPost, postTitle FROM 
{$tablePost}
  ORDER BY postDate DESC
  LIMIT 10;
  
  --
  -- Gets timestamp for all months that have posts
  --
  SELECT UNIX_TIMESTAMP(postDate) AS 'postDate' 
  FROM 
{$tablePost}
  GROUP BY MONTH(postDate)
  ORDER BY postDate DESC;
  
  --
  -- Gets screenname and number of posts of all authors
  --
  SELECT screenname, COUNT(screenname) AS 'numPosts'
  FROM 
{$tableAuthor}
  JOIN 
{$tablePost} ON (idAuthor = Post_idAuthor)
  GROUP BY screenname
  ORDER BY screenname ASC;
  
  --
  -- Gets post stats (10, 30, 365 days) 
  --
  SELECT DISTINCT (
  SELECT COUNT(idPost) FROM 
{$tablePost} WHERE postDate BETWEEN DATE_SUB(NOW(), INTERVAL 10 DAY) AND NOW()
  ) AS 'tenDays',
  (
  SELECT COUNT(idPost) FROM 
{$tablePost} WHERE postDate BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()
  ) AS 'thirtyDays',
  (
  SELECT COUNT(idPost) FROM 
{$tablePost} WHERE postDate BETWEEN DATE_SUB(NOW(), INTERVAL 1 YEAR) AND NOW()
  ) AS 'year'
  FROM 
{$tablePost};
END;
  
  
$res $mysqli->multi_query($query) or die("Could not query database"); //Performs query
  
  //----------------------------
  //Gets result from first query
  
$res $mysqli->store_result() or die("Failed to retrieve result from query.");
  
  echo <<<END
    <div class="rightColumn">
      <ul class="list">
        <li style="margin:40px 0 5px 0;">10 latest posts</li>
END;
  
//For each returned row, adds list item
  
while($row $res->fetch_object()) {
    echo 
"\n    <li><a href='?p=post&amp;id={$row->idPost}'>{$row->postTitle}</a></li>";
  }
  
$res->close();
  
  
//------------------------------
  //Gets result from second query
  
($mysqli->next_result() && ($res $mysqli->store_result()) ) 
  or die(
"Failed to retrive result from query.");
  
  echo <<<END
      </ul>
      
      <ul class="list">
        <li style="margin:40px 0 5px 0;">Archive</li>
END;
  
//For eac returned row, adds list item
  
while($row $res->fetch_object()) {
    echo 
"\n    <li><a href='?p=index&amp;date=" date('Y-m'$row->postDate) . "'>" date('F Y'$row->postDate) . "</a></li>";
  }
  
$res->close();
  
  
//----------------------------
  //Gets result from third query
  
($mysqli->next_result() && ($res $mysqli->store_result()) ) 
  or die(
"Failed to retrive result from query.");
  
  echo <<<END
      </ul>
      
      <ul class="list">
        <li style="margin:40px 0 5px 0;">Posts by authors</li>
END;
  
  
//For each returned row, adds list item
  
while($row $res->fetch_object()) {
    echo <<<END
    
    <li>
      <a href='?p=index&amp;author=
{$row->screenname}'>{$row->screenname} ({$row->numPosts})</a> &middot; 
      <a href="?p=author&amp;id=
{$row->screenname}">info</a>
    </li>
END;
  }
  
$res->close();
  
  
//Checks if user is logged in
  
if(isset($_SESSION['accountUser'])) {
    
    
//-----------------------------
    //Gets result from fourth query
    
($mysqli->next_result() && ($res $mysqli->store_result()) ) 
    or die(
"Failed to retrive result from query.");
    
$row $res->fetch_object();
    
    echo <<<END
    <ul class="list">
      <li style="margin:40px 0 5px 0;">Statistics</li>
      <li>Last 10 days: <b>
{$row->tenDays}</b></li>
      <li>Last 30 days: <b>
{$row->thirtyDays}</b></li>
      <li>Last year: <b>
{$row->year}</b></li>
    </ul>
END;
  
    
$res->close();
  }
  
  
$mysqli->close(); //Closes DB connection

  //Prints HTML for other stuff + end tags
  
echo <<<END
      </ul>
      
      <ul class="list">
        <li style="margin:40px 0 5px 0;">
          <a href="?p=rss">
            <img src="images/RSS-icon.png" title="RSS Feed" />
            RSS Feed
          </a>
        </li>
        <li style="margin:40px 0 5px 0;">
          <a href="?p=validate">
            Validate blog
          </a>
        </li>
      </ul>
    </div><!-- rightColumn -->
  </body>
</html>
END;
}

//---------------------------------
//Returns chosen post with correct HTML
public function getFormattedPost($postId) {
  
  
//--------------------
  //DB stuff
  
$mysqli = new mysqli(DB_HOSTDB_USERDB_PASSWORDDB_DATABASE); //New DB object
  
$tablePost   DB_PREFIX 'Post';
  
$tableAuthor   DB_PREFIX 'Author';
  
$tableComment   DB_PREFIX 'Comment';
  
$tableTag   DB_PREFIX 'Tag';
  
$tablePostTag   DB_PREFIX 'PostTag';
  
  if (
mysqli_connect_error()) {
     echo 
"Connect failed: ".mysqli_connect_error()."<br>";
     exit();
  }
  
$mysqli->set_charset("utf8");
  
  
//-----------------------
  //SQL queries
  
$query = <<<END
  --
  -- Selects the chosen post
  --
  SELECT * FROM 
{$tablePost}
  WHERE idPost = 
{$postId};
  
  --
  -- Selects username of post author
  --
  SELECT screenname, idAuthor FROM 
{$tableAuthor}
  WHERE idAuthor = (
    SELECT Post_idAuthor FROM 
{$tablePost}
    WHERE idPost = 
{$postId}
  );
  
  --
  -- Counts number of comments for chosen post
  --
  SELECT COUNT(*) AS 'noOfComments' FROM 
{$tableComment}
  WHERE Comment_idPost = 
{$postId};
  
  --
  -- Selects tag for chosen post
  --
  SELECT PostTag_idPost, idTag, tagName FROM 
{$tableTag}
  JOIN 
{$tablePostTag} ON (idTag = PostTag_idTag) 
  WHERE PostTag_idPost = 
{$postId};
END;
  
//Performs queries
  
$mysqli->multi_query($query) or die("Could not query database"); 
  
  
//----------------------------
  //Gets result from first query
  
$res $mysqli->store_result() or die("Failed to retrieve result from query.");
  
$row $res->fetch_object();
  
  
//Assigns variable values  
  
$title $row->postTitle;
  
$authorId $row->Post_idAuthor;
  
$text $row->postText;
  
$date $row->postDate;
  
  
$text nl2br($text); //Changes /n to <br />
  
  
$res->close();
  
  
//------------------------------
  //Gets result from second query
  
($mysqli->next_result() && ($res $mysqli->store_result()) ) 
  or die(
"Failed to retrive result from query.");
  
$row $res->fetch_object();
  
  
//Assigns variable values
  
$author $row->screenname;
  
$authorId $row->idAuthor;
  
$res->close();
  
  
//----------------------------
  //Gets result from third query
  
($mysqli->next_result() && ($res $mysqli->store_result())) 
  or die(
"Failed to retrive result from query.");
  
$row $res->fetch_object(); //Gets first returned row
  
  //Assigns variable values
  
$noOfComments $row->noOfComments;
  
$res->close();
  
  
//-----------------------------
  //Gets result from fourth query
  
($mysqli->next_result() && ($res $mysqli->store_result())) 
  or die(
"Failed to retrive result from query.");
  
  
//For each returned row adds tag to string
  
$tagString "";
  while(
$row $res->fetch_object()) {
    
$tagString .= "<a href='?p=index&amp;tag={$row->tagName}'>{$row->tagName}</a>, ";
  }
  
$tagString substr($tagString0, -2); //Removes last comma on the end
  
  
$res->close();
  
$mysqli->close(); //Close DB connection
  
  
$editHTML $this->getEditLinks($authorId$postId); //If user is logged in and is the author of the post, adds edit links
  
  //Post HTML
  
$html = <<<END
  
  <!-- START POST -->
  <h2><a href="?p=post&amp;id=
{$postId}">{$title}</a></h2><span class="author"><a href="?p=index&amp;author={$author}">{$author}</a></span>
  <span class="post">
    
{$text}
  </span><!-- post -->
  <hr style="margin-top:15px;" />
  <span class="comment">
  <a href="?p=post&amp;id=
{$postId}#comment">Comments ({$noOfComments})</a> &middot; 
  <a href="?p=post&amp;id=
{$postId}#comment">Write comment</a>
  </span>
  <span class="date">Written 
{$date}</span>
  <span class="tags">Tags: 
{$tagString}</span>
  
{$editHTML}  
  <hr style="margin-bottom:120px;" />
  <!-- END POST -->
END;

  return 
$html;
}

//---------------------------------
//Returns chosen comment with HTML
public function getFormattedComment($commId$postId) {
  
//----------------------
  //DB stuff
  
$mysqli = new mysqli(DB_HOSTDB_USERDB_PASSWORDDB_DATABASE); //New DB object
  
$tableComment DB_PREFIX 'Comment';
  
  if (
mysqli_connect_error()) {
     echo 
"Connect failed: ".mysqli_connect_error()."<br>";
     exit();
  }
  
$mysqli->set_charset("utf8");
  
  
//----------------------
  //SQL query
  
$query = <<<END
  --
  -- Selects chosen comment
  --
  SELECT * FROM 
{$tableComment}
  WHERE idComment = 
{$commId}
END;

  
$res $mysqli->query($query) or die("Could not query database"); //Performs query
  
$row $res->fetch_object(); //Gets first row
  
  
$emailName substr($row->commEmail0, (strpos($row->commEmail'@'))); //Gets first part of e-mail
  
$deleteHTML $this->getDeleteCommentLink($row->idComment$postId); //If user is logged in, get delete link
  
  //HTML
  
$html = <<<END
  <!-- START COMMENT -->
  <h3>
{$row->commTitle}  </h3>
  <span class="post">
    
{$row->commText}
  </span><!-- post -->
  <hr />
  <span class="tags">Written by: 
{$row->commSignature} ({$emailName})</span>
  <span class="date">
{$row->commDate}</span>
  
{$deleteHTML}
  <hr style="margin-bottom:50px;" />
  <!-- END COMMENT -->
END;

  return 
$html;
}

//---------------------------------
// Prints the login-menu, changes links if user is logged in or not
public function getLoginLogoutMenu() {
  
$htmlMenu "";

  
// If user is logged in
  
if(isset($_SESSION['accountUser'])) {
    
$htmlMenu = <<<EOD
    <li><a href="?p=logout">Log out</a></li>
    <li><a href="?p=new">New post</a></li>
    <li><a href="?p=stats">Statistics</a></li>
EOD;
  } else {
    
// If user is not logged in, show link to login-page
    
$htmlMenu = <<<EOD
    <li><a href='?p=login'>Login</a></li>
EOD;
  }

      return 
$htmlMenu;    


//---------------------------------
//Returns link for editing and deleting post
public function getEditLinks($authorId$postId) {
  
//If user is logged in and the user is the author of the post
  
if(isset($_SESSION['accountUser']) && $_SESSION['accountId'] == $authorId) {
    return 
'<span class="edit"><a href="?p=edit&amp;id=' $postId '">Edit</a> &middot; <a href="?p=delete&amp;id=' $postId '">Delete</a></span>';
  } else {
    return 
"";  
  }
}

//---------------------------------
//Returns link to delete comment
public function getDeleteCommentLink($commentId$postId) {
  
//Checks if user is logged in
  
if(isset($_SESSION['accountUser'])) {
    return 
"<span class='edit'><a href='?p=cdeletep&amp;id={$commentId}&amp;post={$postId}'>Delete</a></span>";
  } else {
    return 
"";  
  }  
}

//---------------------------------
//Returns string with username if logged in
public function getLoggedInName() {
  
// If user is logged in
  
if(isset($_SESSION['accountUser'])) {
    return 
"<p class='loginName'>Logged in as {$_SESSION['accountUser']}</p>";
  } else {
    return 
"";
  }  
}

//---------------------------------
// Create a errormessage if its set in the SESSION
public function getErrorMessage() {

$html "";

//Checks if error message is set
if(isset($_SESSION['errorMessage'])) {
    
$html = <<<EOD
  <div class='errorMessage'>
  
{$_SESSION['errorMessage']}
  </div>
EOD;

    unset(
$_SESSION['errorMessage']); //Empties error message
}

return 
$html;   
}
}