-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.DOMDocument.php
318 lines (180 loc) · 7.42 KB
/
index.DOMDocument.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
<?php
if( substr_count( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ], 'gzip' ) ) { ob_start( "ob_gzhandler" ); } else { ob_start(); }
session_start();
require_once( "./User.class.php" );
require_once( "./Token.class.php" );
require_once( "./Article.class.php" );
require_once( "./Comment.class.php" );
require_once( "./markdown.php" );
{ // page building variables
$url = ''; // set to your sites URL, may or may not be usefull
$name = 'Ibrahim';
$proffesion = 'web developer';
$adminEmail = ""; // all "contact site admin" links will point to this
$HTMLEmailheaders = 'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'From: ' . $adminEmail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$output = '';
$pageTitle = ''; // set the pages title here
}
$doc = new DOMDocument();
$doc -> validateOnParse = true;
$doc -> loadHTML( file_get_contents( 'template.xhtml' ) );
$mainColumn = $doc -> getElementById( "mainColumn" );
$commentsElement = $doc -> getElementById( "commentsDiv" );
$commentElementTemplate = $doc -> getElementById( "commentDiv" );
$commentFormSample = $doc -> getElementById( 'commentFormSample' );
{ // page building functions
function generateDialog( $message = "hello, world!" ) {
GLOBAL $doc;
$dialog = $doc -> createElement( 'div' );
$p = $doc -> createElement( 'p' );
$text = $doc -> createTextNode( $message );
$text = $p -> appendChild( $text );
$p = $dialog -> appendChild( $p );
// $dialog = $mainColumn -> appendChild( $dialog );
$dialog -> setAttribute( "class", "dialog" );
return $dialog;
}
function generateComment( $commentID ) {
GLOBAL $commentElementTemplate, $doc;
$comment = new Comment( $commentID );
$commentElement = $commentElementTemplate -> cloneNode( true );
$commentElement -> removeAttribute( "id" );
$ps = $commentElement -> getElementsByTagName( 'p' );
$p = $ps -> item( 0 );
$text = $doc -> createTextNode( 'on ' . substr( $comment -> getDateCreated(), 0, 10 ) . ' at ' . substr( $comment -> getDateCreated(), 11, 5 ) . ', ' . $comment -> getAuthor() . ' said :' );
$text = $p -> appendChild( $text );
$fragment = $doc -> createDocumentFragment();
$fragment -> appendXML( Markdown( $comment -> getBody() ) );
$commentElement -> appendChild( $fragment );
return $commentElement;
}
function generatePost( $articleID ) {
GLOBAL $doc, $commentsElement, $commentElementTemplate;
$article = new Article( $articleID );
$articleElement = $doc -> createElement( 'div' );
$articleHeader = $doc -> createElement( 'h1' );
$text = $doc -> createTextNode( $article -> getTitle() );
$text = $articleHeader -> appendChild( $text );
$articleHeader = $articleElement -> appendChild( $articleHeader );
$fragment = $doc -> createDocumentFragment();
$fragment -> appendXML( Markdown( $article -> getBody() ) );
$articleBody = $articleElement -> appendChild( $fragment );
if( count( $article -> getComments() ) > 0 ) {
foreach( $article -> getComments() as $commentID ) { // Display each comment
$commentsElement -> appendChild( generateComment( $commentID ) );
}
$commentsElement -> removeChild( $commentElementTemplate ); // Remove the comment template
$articleElement -> appendChild( $commentsElement ); // Attach the comments
}
$articleElement -> setAttribute( "class", "article" );
return $articleElement;
}
}
$section = "articles";
if( isset( $_REQUEST[ "section" ] ) ) {
$section = $_REQUEST[ "section" ];
}
switch( $section ) {
case "articles" : {
$action = "list";
if( isset( $_REQUEST[ "action" ] ) ) {
$action = $_REQUEST[ "action" ];
}
switch( $action ) {
case "list" : {
$mainColumn -> removeChild( $commentsElement );
$mainColumn -> removeChild( $commentFormSample );
$articles = getArticles( 0, "published" );
if( count( $articles ) > 0 ) {
$limit = 5;
if( count( $articles ) <= 5 ) {
$limit = count( $articles );
}
for( $i = 0; $i < $limit; $i++ ) {
$article = new Article( $articles[ $i ] );
if( articleExists( 0, $articles[ $i ] ) ) {
$articleElement = $doc -> createElement( 'div' );
$href = $doc -> createElement( 'a' );
$href = $articleElement -> appendChild( $href );
$href -> setAttribute( 'href', '?section=articles&action=view&target=' . $articles[ $i ] );
$articleHeader = $doc -> createElement( 'h1' );
$text = $doc -> createTextNode( $article -> getTitle() );
$text = $articleHeader -> appendChild( $text );
$articleHeader = $href -> appendChild( $articleHeader );
$articleElement = $mainColumn -> appendChild( $articleElement );
$articleElement -> setAttribute( "class", "article" );
}
else {
$mainColumn -> appendChild( generateDialog( 'Sorry, the article referenced no longer exists' ) );
}
}
}
else {
/*
$dialog = $doc -> createElement( 'div' );
$p = $doc -> createElement( 'p' );
$text = $doc -> createTextNode( 'no posts yet :(' );
$text = $p -> appendChild( $text );
$p = $dialog -> appendChild( $p );
$dialog = $mainColumn -> appendChild( $dialog );
$dialog -> setAttribute( "class", "dialog" );
*/
$mainColumn -> appendChild( generateDialog( 'no posts yet :(' ) );
}
}
break;
case "view" : {
$mainColumn -> removeChild( $commentsElement );
$mainColumn -> removeChild( $commentFormSample );
if( isset( $_REQUEST[ "target" ] ) ) {
$article = new Article( $_REQUEST[ "target" ] );
$mainColumn -> appendChild( generatePost( $_REQUEST[ "target" ] ) );
$commentForm = $commentFormSample -> cloneNode( true ); // get and attach the comment form
$mainColumn -> appendChild( $commentForm );
}
else {
$mainColumn -> appendChild( generateDialog( 'you have to specify an article to view' ) );
}
}
break;
}
}
break;
case "comments" : {
$action = "add";
if( isset( $_REQUEST[ "action" ] ) ) {
$action = $_REQUEST[ "action" ];
}
switch( $action ) {
case "add" :
case "new" :
default : {
if( isset( $_POST[ "comment" ] ) ) {
if( isset( $_POST[ "name" ] ) && isset( $_POST[ "email" ] ) && isset( $_POST[ "target" ] ) && ( filter_var( $_POST[ "email" ], FILTER_VALIDATE_EMAIL ) ) ) {
$comment = new Comment( "00000", $_POST[ "comment" ], $_POST[ "target" ], $_POST[ "name" ], $_POST[ "email" ] );
if( $comment -> saveToDB() ) {
$mainColumn -> appendChild( generateDialog( 'your comment has been saved and is awaiting moderation, thank you for your feedback' ) );
}
else {
$mainColumn -> appendChild( generateDialog( 'There was a problem saving your comment, please click back and try again' ) );
}
}
else {
$mainColumn -> appendChild( generateDialog( 'you must provide a valid email address and a name' ) );
}
}
else {
$mainColumn -> appendChild( generateDialog( 'you comment cannot be empty' ) );
}
}
break;
}
}
break;
}
header( 'Content-type: text/html' );
echo $doc -> saveHTML();
?>