-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.php
More file actions
439 lines (426 loc) · 21.8 KB
/
posts.php
File metadata and controls
439 lines (426 loc) · 21.8 KB
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
<?php
function displayPosts( $db, $db2, $sql, $userID, $max_posts, $param_types, $param1 = "", $param2 = "", $param3 = "" )
{
$total_count_sql = substr( $sql, strpos( $sql, " FROM " ) );
$total_count_sql = substr( $total_count_sql, 0, strpos( $total_count_sql, " ORDER BY " ) );
$total_count_sql = "SELECT COUNT(*)" . $total_count_sql;
//print( "SQL: $sql<br>\n$total_count_sql<br>" );
if( $param3 != "" )
$total_count = get_db_value( $db, $total_count_sql, $param_types, $param1, $param2, $param3 );
elseif( $param2 != "" )
$total_count = get_db_value( $db, $total_count_sql, $param_types, $param1, $param2 );
elseif( $param1 != "" )
$total_count = get_db_value( $db, $total_count_sql, $param_types, $param1 );
else
$total_count = get_db_value( $db, $total_count_sql );
//print( "Total: $total_count<br>\n" );
$output = "";
$stmt = $db->stmt_init();
//print "$param_types $param1 $param2<br>" ;
print( "<div id=\"post-container\" class=\"post-container\">\n" );
// If not logged in, only display public posts
if( $userID == "" )
$sql = str_replace( "ORDER BY", " WHERE posts.public = 1 ORDER BY", $sql );
if( $stmt->prepare( $sql ) )
{
if( $param3 != "" )
$stmt->bind_param( $param_types, $param1, $param2, $param3 );
elseif( $param2 != "" )
$stmt->bind_param( $param_types, $param1, $param2 );
elseif( $param1 != "" )
$stmt->bind_param( $param_types, $param1 );
$stmt->execute();
print $db->error;
print $stmt->error;
$stmt->store_result();
$stmt->bind_result( $post_id, $content, $created, $author_visible_name, $author_real_name, $author_username, $author_public, $author_id, $parent_post_id, $editable, $broadcast_id );
$post_index = 0;
while( $stmt->fetch() && $post_index < $max_posts )
{
print( "<div class=\"post\">\n" );
printAuthorInfo( $db2, $userID, $author_id, $author_username, $author_visible_name, $author_real_name, $author_public, $post_id, "full" );
print( "<div class=\"post-content\"" );
if( $userID != "" && $userID != 0 )
print( " onmouseover=\"javascript:document.getElementById('post-navigation-$post_id').style.visibility='visible';\" onmouseleave=\"javascript:document.getElementById('post-navigation-$post_id').style.visibility='hidden';\"" );
print( ">" );
print( "<div class=\"timestamp\"><a href=\"post.php?i=$post_id#main-post\">" . getAge( $created ) . "</a></div>\n" );
if( $editable == 1 )
{
$timeout = intval( get_db_value( $db2, "SELECT timeout FROM post_locks WHERE post = ?", "s", $post_id ) );
if( $timeout < time() )
{
$compressed_content = compressContent( $content );
print( "<div class=\"edit-icon\"><a href=\"#\" onclick=\"javascript:setComposeForEdit('$post_id','compose-post','$compressed_content','');document.getElementById('set-post-editable').checked=true;updatePreview('compose-post','post-preview');return false;\"><img src=\"assets/images/pencil.png\" width=\"16\" height=\"16\" alt=\"Edit\" title=\"Click here to edit this post.\" /></a></div>\n" );
}
else
{
$name_of_locked_user = intval( get_db_value( $db2, "SELECT visible_name FROM users JOIN post_locks ON (post_locks.user = users.id AND post_locks.post = ?)", "s", $post_id ) );
$minutes_until_unlocked = intval( ($timeout - time()) / 60 );
$msg = "$minutes_until_unlocked minutes";
if( $minutes_until_unlocked == 0 )
$msg = "less than a minute";
elseif( $minutes_until_unlocked == 1 )
$msg = "1 minute";
print( "<div class=\"edit-icon\"><img src=\"assets/images/pencil-disabled.png\" width=\"16\" height=\"16\" alt=\"Edit\" title=\"$name_of_locked_user is editing this post. It will become unlocked in $msg.\" /></div>\n" );
}
}
// Get world info
$world_name = "";
$p_stmt = $db2->stmt_init();
$p_stmt = $db2->prepare( "SELECT worlds.id, worlds.display_name FROM worlds, world_posts WHERE world_posts.world = worlds.id AND world_posts.post = ?" );
$p_stmt->bind_param( "s", $post_id );
$p_stmt->execute();
$p_stmt->bind_result( $world_id, $world_name );
$p_stmt->fetch();
$p_stmt->close();
if( $world_name != "" )
print( "<div class=\"in-world\">In the world of <a href=\"world.php?i=$world_id\" class=\"world-name\">$world_name</a>:</div>\n" );
if( $broadcast_id != "" )
{
$u_stmt = $db2->prepare( "SELECT broadcasts.created, users.id, users.real_name, users.visible_name, users.profile_public FROM broadcasts JOIN users ON (broadcasts.user = users.id) WHERE broadcasts.id = ?" );
$u_stmt->bind_param( "s", $broadcast_id );
$u_stmt->execute();
$u_stmt->bind_result( $b_time, $b_user_id, $b_real_name, $b_visible_name, $b_public );
$u_stmt->fetch();
$u_stmt->close();
print( "<div class=\"broadcast\">Broadcast " . getAge( $b_time) . " ago by " . getAuthorLink( $b_user_id, $b_visible_name, $b_real_name, $b_public ) . "</div>\n" );
}
// Get reply-to info
if( $parent_post_id != "" && $userID != "" && $userID != 0 )
{
$p_stmt = $db2->stmt_init();
$p_stmt->prepare( "SELECT posts.content, users.visible_name, users.profile_public, users.id FROM posts, users WHERE posts.author = users.id AND posts.id = ?" );
$p_stmt->bind_param( "s", $parent_post_id );
$p_stmt->execute();
$p_stmt->bind_result( $parent_post_content, $parent_author_visible_name, $parent_author_profile_public, $parent_author_id );
if( $p_stmt->fetch() )
{
print( "<div class=\"in-reply-to\">In reply to " );
if( $parent_author_profile_public == 1 )
print( "<a href=\"profile.php?i=$parent_author_id\">$parent_author_visible_name</a>" );
else
print( "$parent_author_visible_name" );
print( "'s post <em><a href=\"post.php?i=$parent_post_id#main-post\">" . getPostSnippet( $parent_post_content ) . "</a></em></div>\n" );
}
$p_stmt->close();
}
// Display the actual post
print( formatPost( $content ) );
// Get comments
$comments_stmt = $db2->stmt_init();
$comments_sql = "SELECT comments.id, comments.content, comments.created, users.username, users.visible_name, users.real_name, users.profile_public, users.id " .
"FROM comments " .
"JOIN users ON (comments.author = users.id) " .
"WHERE comments.post = ? " .
"ORDER BY comments.created ASC LIMIT 10";
if( $comments_stmt->prepare( $comments_sql ) )
{
$comments_stmt->bind_param( "s", $post_id );
$comments_stmt->execute();
$comments_stmt->store_result();
$comments_stmt->bind_result( $comment_id, $comment_content, $comment_created, $commenter_username, $commenter_visible_name, $commenter_real_name, $commenter_public, $commenter_id );
if( $comments_stmt->num_rows > 0 )
print( "<div class=\"comments\">\n" );
while( $comments_stmt->fetch() )
{
print( "<div class=\"comment\"" );
if( $commenter_id == $userID )
print( "onmouseover=\"javascript:document.getElementById('comment-edit-link-$comment_id').style.display='block';\" onmouseleave=\"javascript:document.getElementById('comment-edit-link-$comment_id').style.display='none';\"" );
print( ">\n" );
printAuthorInfo( $db2, $userID, $commenter_id, $commenter_username, $commenter_visible_name, $commenter_real_name, $commenter_public, $comment_id, "comment" );
$compressed_comment = compressContent( $comment_content );
print( "<div class=\"comment-content\"><div class=\"timestamp\">" );
if( $commenter_id == $userID )
print( "<a onclick=\"javascript:setComposeForEdit('$post_id','compose-post','$compressed_content');updatePreview('compose-post','post-preview');return false\" href=\"#\">" );
print( getAge( $comment_created ) );
if( $commenter_id == $userID )
print( "</a><br /><div id=\"comment-edit-link-$comment_id\" style=\"float: right; display: none;\"><a onclick=\"javascript:setComposeForEdit('$post_id','compose-comment-$post_id','$compressed_comment','$comment_id');updatePreview('compose-comment-$post_id','comment-preview-$post_id');return false;\" href=\"#\">Edit</a></div>" );
print( "</div>" . formatPost( $comment_content ) . "</div>" );
print( "</div>\n" ); // end .comment
}
if( $comments_stmt->num_rows > 0 )
print( "</div>\n" ); // end .comments
}
$snippet = getPostSnippet( $content );
if( $userID != "" && $userID != 0 )
{
print( "<div id=\"post-navigation-$post_id\" class=\"post-navigation\" style=\"visibility: hidden\"><a href=\"post.php?i=$post_id#main-post\">View conversation</a> " );
if( $author_id == $userID )
{
$compressed_content = compressContent( $content );
$post_is_public = get_db_value( $db, "SELECT public FROM posts WHERE id = ?", "s", $post_id );
print( "<a href=\"#\" onclick=\"javascript:setComposeForEdit('$post_id','compose-post','$compressed_content','','$editable','$post_is_public');updatePreview('compose-post','post-preview');return false;\">Edit</a> <a onclick=\"javascript:displayDelete('$post_id');return false;\" href=\"#\">Delete</a> " );
}
else
{
$tracking = get_db_value( $db, "SELECT COUNT(*) FROM tracking WHERE user = ? AND post = ?", "ss", $userID, $post_id );
if( $tracking >= 1 )
print( "Tracking " );
else
print( "<a title=\"Get pinged when comments are added to this post.\" href=\"track.php?i=$post_id&redirect=" . getRedirectURL() . "\">Track</a> " );
print( "<a title=\"Share this post with people who have you in their teams.\" href=\"broadcast.php?i=$post_id&redirect=" . getRedirectURL() . "\">Broadcast</a> " );
}
print( "<a onclick=\"javascript:setReplyTo('$post_id', '$author_visible_name', '$snippet');\" href=\"#top\">Reply with post</a> <a onclick=\"javascript:toggleComposePane('compose-tools-$post_id','compose-pane-$post_id','compose-comment-$post_id');return false;\" href=\"#\">Reply with comment</a> </div> <!-- .post-navigation -->\n" );
displayComposePane( "comment", $db, $userID, $post_id );
}
print( "</div>\n" ); // end .post-content
print( "</div>\n" ); // end .post
$post_index++;
}
}
// If there are more results even than this,
if( $post_index < $total_count )
{
$tab = "";
if( isset( $_GET["tab"] ) )
$tab = $_GET["tab"];
print( "<div id=\"load-more-posts\">\n" );
print( "<button onclick=\"javascript:loadMorePosts('$tab','$userID',$post_index);return false;\">Load more results</button>\n" );
print( "</div>\n" );
}
print( "</div>\n" ); // end .post-container
print $db->error;
print $stmt->error;
}
function processListItem( $list_item, $list_type, $in_list, $num_stars )
{
$list_item = substr( $list_item, $num_stars + 1 );
$list_item = "<li> " . $list_item;
$diff = $num_stars - $in_list;
while( $diff > 0 )
{
$list_item = "<" . $list_type . ">" . $list_item;
$diff--;
}
while( $diff < 0 )
{
$list_item = "</" . $list_type . ">" . $list_item;
$diff++;
}
$list_item = preg_replace( "/<br \/>$/", "</li>", $list_item );
return $list_item;
}
function addVideoEmbed( $matches )
{
$videoURL = $matches[1] . "://" . $matches[2] . "." . $matches[3];
$uuid = getGUID();
$output = "<div id=\"$uuid\">Loading the player...</div>" .
"<script type=\"text/javascript\">" .
"jwplayer(\"$uuid\").setup({" .
"file: \"$videoURL\"," .
"image: \"assets/images/video-background.jpg\"," .
"width: 500," .
"height: 280" .
"});" .
"</script>";
return $output;
}
function formatPost( $text )
{
// Replace HTML with BBcode and remove the rest.
$text = preg_replace( "/<strong>|<b>/i", "[b]", $text );
$text = preg_replace( "/<\/strong>|<\/b>/i", "[/b]", $text );
$text = preg_replace( "/<em>|<i>/i", "[i]", $text );
$text = preg_replace( "/<\/em>|<\/i>/i", "[/i]", $text );
$text = preg_replace( "/<u>/i", "[u]", $text );
$text = preg_replace( "/<\/u>/i", "[/u]", $text );
$text = preg_replace( "/<a target=[\"'][\S]+?[\"'] href=[\"']([\S]+?)[\"']>([\S\s]+?)<\/a>/i", "[url=$1]$2[/url]", $text );
$text = preg_replace( "/<a href=[\"']([\S]+?)[\"'] target=[\"'][\S]+?[\"']>([\S\s]+?)<\/a>/i", "[url=$1]$2[/url]", $text );
$text = preg_replace( "/<a href=[\"']([\S]+?)[\"']>([\S\s]+?)<\/a>/i", "[url=$1]$2[/url]", $text );
$text = preg_replace( "/<img src=[\"']([\S]+?)[\"']>/i", "$1", $text );
$text = strip_tags( $text );
// Add breaks
$text = preg_replace( "/\n/", "<br />\n", $text );
// Process ordered and unordered lists
$lines = explode( "\n", $text );
$in_ul = 0;
$in_ol = 0;
$in_code = 0;
for( $i = 0; $i < count($lines); $i++ )
{
while( stripos( $lines[$i], "[CODE]" ) !== false &&
stripos( $lines[$i], "[/CODE]" ) !== false )
{
$lines[$i] = str_ireplace( "[CODE]", "<span class=\"style-code-snippet\">", $lines[$i] );
$lines[$i] = str_ireplace( "[/CODE]", "</span>", $lines[$i] );
}
if( stripos( $lines[$i], "[CODE]" ) !== false )
{
$in_code = 1;
$lines[$i] = str_ireplace( "[CODE]", "<div class=\"style-code-block\">Code:", $lines[$i] );
$lines[$i] = str_ireplace( "<br />", "", $lines[$i] );
$lines[$i] = str_ireplace( "<br></br>", "", $lines[$i] );
}
elseif( stripos( $lines[$i], "[/CODE]" ) !== false )
{
$in_code = 0;
$lines[$i] = str_ireplace( "[/CODE]", "</div>", $lines[$i] );
$lines[$i] = str_ireplace( "<br />", "", $lines[$i] );
}
elseif( $in_code >= 1 )
{
$lines[$i] = str_replace( "<br />", "", $lines[$i] );
$lines[$i] = str_replace( "<br></br>", "", $lines[$i] );
$lines[$i] = "<span class=\"code-line-number\">" . str_pad( $in_code, 3, "0", STR_PAD_LEFT ) . "</span> " . $lines[$i];
$in_code++;
}
if( preg_match( "/^[\*]+ /", $lines[$i] ) > 0 )
{
$num_stars = strpos( $lines[$i], " " );
$lines[$i] = processListItem( $lines[$i], "ul", $in_ul, $num_stars );
$in_ul = $num_stars;
}
elseif( $in_ul > 0 )
{
while( $in_ul > 0 )
{
$lines[$i] = "</ul>" . $lines[$i];
$in_ul--;
}
}
if( preg_match( "/^[\#]+ /", $lines[$i] ) > 0 )
{
$num_stars = strpos( $lines[$i], " " );
$lines[$i] = processListItem( $lines[$i], "ol", $in_ol, $num_stars );
$in_ol = $num_stars;
}
elseif( preg_match( "/^[0-9] /", $lines[$i] ) > 0 )
{
$num_stars = strpos( $lines[$i], " " );
$lines[$i] = processListItem( $lines[$i], "ol", $in_ol, $num_stars );
$in_ol = $num_stars;
}
elseif( $in_ol > 0 )
{
while( $in_ol > 0 )
{
$lines[$i] = "</ol>" . $lines[$i];
$in_ol--;
}
}
}
$text = implode( "\n", $lines );
if( $in_ul == 1 )
$text .= "</ul>";
if( $in_ol == 1 )
$text .= "</ol>";
// Process forum-style formatting
$text = preg_replace( "/\[B\]([\S\s]+?)\[\/B\]/i", "<strong>$1</strong>", $text );
$text = preg_replace( "/\[I\]([\S\s]+?)\[\/I\]/i", "<em>$1</em>", $text );
$text = preg_replace( "/\[U\]([\S\s]+?)\[\/U\]/i", "<span style=\"text-decoration: underline\">$1</span>", $text );
$text = preg_replace( "/\[COLOR=([\S]+?)\]([\S\s]+?)\[\/COLOR\]/i", "<span style=\"color: $1\">$2</span>", $text );
$text = preg_replace( "/\[SIZE=([\S]+?)\]([\S\s]+?)\[\/SIZE\]/i", "<span style=\"font-size: $1\">$2</span>", $text );
$text = preg_replace( "/\[FONT=([\S]+?)\]([\S\s]+?)\[\/FONT\]/i", "<span style=\"font-family: $1\">$2</span>", $text );
$text = preg_replace( "/\[ALIGN=(LEFT|CENTER|RIGHT)\]([\S\s]+?)\[\/ALIGN\]/i", "<div style=\"text-align: $1\">$2</div>", $text );
$text = preg_replace( "/\[INDENT\]([\S\s]+?)\[\/INDENT\]/i", "<div style=\"padding-left: 25px;\">$1</div>", $text );
$text = preg_replace( "/\[EMAIL\]([\S\s]+?)\[\/EMAIL\]/i", "<a href=\"mailto:$1\">$1</a>", $text );
$text = preg_replace( "/\[URL\]([\S\s]+?)\[\/URL\]/i", "<a href=\"$1\">$1</a>", $text );
$text = preg_replace( "/\[URL=([\S]+?)\]([\S\s]+?)\[\/URL\]/i", "<a href=\"$1\">$2</a>", $text );
$text = preg_replace( "/\[IMG\]([\S\s]+?)\[\/IMG\]/i", "<img src=\"$1\" style=\"max-width: 500px\" />", $text );
// Process other stuff
$text = preg_replace( "/@\"([\S\s]+?)\"/", "<span class=\"reply-name\">@$1</span>", $text );
$text = preg_replace( "/<br \/>\n<br \/>\n<ul>/", "<br />\n<ul>", $text );
$text = preg_replace( "/<br \/>\n<br \/>\n<ol>/", "<br />\n<ol>", $text );
$text = preg_replace( "/'''([\S\s]+?)'''/", "<strong>$1</strong>", $text );
$text = preg_replace( "/''([\S\s]+?)''/", "<em>$1</em>", $text );
$text = preg_replace( "/(\s|^)__([\S\s]+?)__(\s|$)/", "$1<strong>$2</strong>$3", $text );
$text = preg_replace( "/\*\*([\S\s]+?)\*\*/", "<strong>$1</strong>", $text );
//$text = preg_replace( "/(\s|^)_([\S\s]+?)_(\s|\.|$)/", "$1<em>$2</em>$3", $text );
$text = preg_replace( "/(\s|^)_([\S\s]+?)_(\s|\n|\.|\,|\:|$)/", "$1<em>$2</em>$3", $text );
$text = preg_replace( "/\*([\S\s]+?)\*/", "<em>$1</em>", $text );
$text = preg_replace( "/\[(http|https):\/\/([\S]+) ([\S\s]+?)\]/i", "<a href=\"$1://$2\">$3</a>", $text );
$text = preg_replace( "/(http|https):\/\/www\.youtube\.com\/watch\?v=([\S]+)/i", "<iframe type=\"text/html\" width=\"500\" height=\"320\" src=\"http://www.youtube.com/embed/$2\" frameborder=\"0\"></iframe>", $text );
$text = preg_replace( "/(http|https):\/\/youtu\.be\/([\S]+)/i", "<iframe type=\"text/html\" width=\"500\" height=\"320\" src=\"http://www.youtube.com/embed/$2\" frameborder=\"0\"></iframe>", $text );
$text = preg_replace( "/(http|https):\/\/([\S]+)\.(jpg|jpeg|gif|png)\|([0-9]+)/", "<img src=\"$1://$2.$3\" style=\"width: $4px; max-width: 500px\" />", $text );
$text = preg_replace_callback( "/(http|https):\/\/([\S]+)\.(mp4)/", "addVideoEmbed", $text );
$text = preg_replace( "/(http|https):\/\/([\S]+)\.(jpg|jpeg|gif|png)([^\"])/", "<img src=\"$1://$2.$3\" style=\"max-width: 500px\" />$4", $text );
$text = preg_replace( "/(http|https):\/\/([A-Za-z0-9\.\%$&\?\#\/\-_=]+)(\s|\n|$)/im", "<a href=\"$1://$2\">$2</a>$3", $text );
$text = preg_replace( "/(\s|^)#([A-Za-z0-9\-]+)/", "$1<a href=\"hashtag.php?tag=$2\">#$2</a>", $text );
return $text;
}
function getPostSnippet( $post_content )
{ // Returns a sane first line of the beginning of a post.
$snippet = trim( strip_tags( $post_content ) );
$max_length = 50;
if( strlen($snippet) > $max_length )
{
$snippet = substr( $snippet, 0, 50 );
$snippet .= "...";
}
$snippet = addslashes( $snippet );
return $snippet;
}
function editPost( $db, $userID, $post_id, $content, $world, $editable )
{
// Editing a post.
// Add history.
$original_content = get_db_value( $db,
"SELECT content FROM posts WHERE id = ?", "s",
$post_id );
$sql = "INSERT INTO post_history " .
"(id, post, author, edited, original_content) " .
"VALUES (UUID(), ?, ?, ?, ?)";
$result = update_db( $db, $sql, "ssis", $post_id, $userID, time(), $original_content );
// Update post.
$sql = "UPDATE posts SET content = ?, editable = ? WHERE id = ?";
$stmt = $db->stmt_init();
$stmt->prepare( $sql );
$stmt->bind_param( "sis", $content, $editable, $post_id );
$stmt->execute();
$stmt->close();
// If world is different,
$current_world_name_basic = processWorldNameForBasic( get_db_value( $db, "SELECT worlds.basic_name FROM worlds JOIN world_posts ON (world_posts.world = worlds.id AND world_posts.post = ?)", "s", $post_id ) );
$posted_world_name_basic = processWorldNameForBasic( $world );
// Update world.
if( $current_world_name_basic != $posted_world_name_basic )
{
$result = update_db( $db, "DELETE FROM world_posts WHERE post = ?", "s", $post_id );
$new_world_id = get_db_value( $db, "SELECT id FROM worlds WHERE basic_name = ?", "s", $posted_world_name_basic );
if( $new_world_id == "" )
{
$posted_world_name_display = processWorldNameForDisplay( $world );
update_db( $db, "INSERT INTO worlds (id, basic_name, display_name, class) VALUES (UUID(), ?, ?, UUID())", "ss", $posted_world_name_basic, $posted_world_name_display );
$new_world_id = get_db_value( $db, "SELECT id FROM worlds WHERE basic_name = ? AND display_name = ?", "ss", $posted_world_name_basic, $posted_world_name_display );
}
$result = update_db( $db, "INSERT INTO world_posts (id, world, post) VALUES (UUID(), ?, ?)", "ss", $new_world_id, $post_id );
$_POST["redirect"] = "world.php?i=$new_world_id";
}
}
function insertPost( $db, $userID, $post_content, $parent, $public, $editable, $world_name = "" )
{
update_db( $db, "INSERT INTO posts (id, author, created, content, " .
"parent, public, editable) " .
"VALUES (UUID(), ?, ?, ?, ?, ?, ?)",
"sissii", $userID, time(), $post_content, $parent, $public, $editable );
$new_post_id = get_db_value( $db, "SELECT id FROM posts WHERE author = ? ORDER BY created DESC LIMIT 1", "s", $userID );
if( $world_name != "" )
{
$full_world_name = processWorldNameForDisplay( $world_name );
$basic_world_name = processWorldNameForBasic( $world_name );
$world_id = get_db_value( $db, "SELECT id FROM worlds WHERE basic_name = ?", "s", $basic_world_name );
if( $world_id == "" )
{ // It doesn't exist, so create it
update_db( $db, "INSERT INTO worlds (id, basic_name, display_name, class) VALUES (UUID(), ?, ?, UUID())", "ss", $basic_world_name, $full_world_name );
$world_id = get_db_value( $db, "SELECT id FROM worlds WHERE basic_name = ?", "s", $basic_world_name );
}
update_db( $db, "INSERT INTO world_posts (id, world, post) VALUES (UUID(), ?, ?)", "ss", $world_id, $new_post_id );
}
// Process @ replies
preg_match_all( "/[ ^]@\"[\S\s]+?\"/", $post_content, $matches );
foreach( $matches[0] as $match )
{
// Remove @" and " from match.
$pos = strpos( $match, '@' );
$match = substr( $match, $pos + 2 );
$match = substr( $match, 0, strlen($match) - 1 );
// If not already tracking,
$is_tracking = get_db_value( $db, "SELECT pings.id FROM pings JOIN users ON (users.id = pings.user AND users.visible_name = ?) WHERE content_id = ?", "ss", $match, $new_post_id );
if( $is_tracking == "" )
{
// Add ping.
$match_user_id = get_db_value( $db, "SELECT id FROM users WHERE users.visible_name = ?", "s", $match );
update_db( $db, "INSERT INTO pings (id, user, created, content_type, content_id, is_read) VALUES (UUID(), ?, ?, 'm', ?, 0)", "sis", $match_user_id, time(), $new_post_id );
}
}
}
?>