Fossil SCM

fossil-scm / src / forum.c
Source Blame History 3539 lines
9a2e5f4… drh 1 /*
9a2e5f4… drh 2 ** Copyright (c) 2018 D. Richard Hipp
9a2e5f4… drh 3 **
9a2e5f4… drh 4 ** This program is free software; you can redistribute it and/or
9a2e5f4… drh 5 ** modify it under the terms of the Simplified BSD License (also
9a2e5f4… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
9a2e5f4… drh 7 **
9a2e5f4… drh 8 ** This program is distributed in the hope that it will be useful,
9a2e5f4… drh 9 ** but without any warranty; without even the implied warranty of
9a2e5f4… drh 10 ** merchantability or fitness for a particular purpose.
9a2e5f4… drh 11 **
9a2e5f4… drh 12 ** Author contact information:
9a2e5f4… drh 13 ** [email protected]
9a2e5f4… drh 14 ** http://www.hwaci.com/drh/
9a2e5f4… drh 15 **
9a2e5f4… drh 16 *******************************************************************************
9a2e5f4… drh 17 **
9a2e5f4… drh 18 ** This file contains code used to generate the user forum.
9a2e5f4… drh 19 */
9a2e5f4… drh 20 #include "config.h"
9a2e5f4… drh 21 #include <assert.h>
9a2e5f4… drh 22 #include "forum.h"
9a2e5f4… drh 23
5f3cb27… drh 24 /*
5f3cb27… drh 25 ** Default to using Markdown markup
5f3cb27… drh 26 */
5f3cb27… drh 27 #define DEFAULT_FORUM_MIMETYPE "text/x-markdown"
5f3cb27… drh 28
99fcc43… drh 29 #if INTERFACE
99fcc43… drh 30 /*
5182a1b… drh 31 ** Each instance of the following object represents a single message -
99fcc43… drh 32 ** either the initial post, an edit to a post, a reply, or an edit to
99fcc43… drh 33 ** a reply.
99fcc43… drh 34 */
5182a1b… drh 35 struct ForumPost {
5182a1b… drh 36 int fpid; /* rid for this post */
0cec52e… drh 37 int sid; /* Serial ID number */
5182a1b… drh 38 int rev; /* Revision number */
99fcc43… drh 39 char *zUuid; /* Artifact hash */
9543ddb… drh 40 char *zDisplayName; /* Name of user who wrote this post */
9543ddb… drh 41 double rDate; /* Date for this post */
5182a1b… drh 42 ForumPost *pIrt; /* This post replies to pIrt */
5182a1b… drh 43 ForumPost *pEditHead; /* Original, unedited post */
5182a1b… drh 44 ForumPost *pEditTail; /* Most recent edit for this post */
5182a1b… drh 45 ForumPost *pEditNext; /* This post is edited by pEditNext */
5182a1b… drh 46 ForumPost *pEditPrev; /* This post is an edit of pEditPrev */
5182a1b… drh 47 ForumPost *pNext; /* Next in chronological order */
5182a1b… drh 48 ForumPost *pPrev; /* Previous in chronological order */
5182a1b… drh 49 ForumPost *pDisplay; /* Next in display order */
5182a1b… drh 50 int nEdit; /* Number of edits to this post */
5182a1b… drh 51 int nIndent; /* Number of levels of indentation for this post */
2d7e2cb… stephan 52 int iClosed; /* True if forum_rid_is_tagged("closed") */
99fcc43… drh 53 };
99fcc43… drh 54
99fcc43… drh 55 /*
99fcc43… drh 56 ** A single instance of the following tracks all entries for a thread.
99fcc43… drh 57 */
99fcc43… drh 58 struct ForumThread {
5182a1b… drh 59 ForumPost *pFirst; /* First post in chronological order */
5182a1b… drh 60 ForumPost *pLast; /* Last post in chronological order */
5182a1b… drh 61 ForumPost *pDisplay; /* Entries in display order */
5182a1b… drh 62 ForumPost *pTail; /* Last on the display list */
0cec52e… drh 63 int mxIndent; /* Maximum indentation level */
923aa75… drh 64 int nArtifact; /* Number of forum artifacts in this thread */
923aa75… drh 65 };
4c4c28d… drh 66
4c4c28d… drh 67 /*
4c4c28d… drh 68 ** A single entry from the forum-statuses setting.
4c4c28d… drh 69 */
4c4c28d… drh 70 struct ForumStatus {
4c4c28d… drh 71 char *zLabel; /* Label for the UI */
4c4c28d… drh 72 char *zValue; /* status=X tag value */
4c4c28d… drh 73 };
4c4c28d… drh 74
4c4c28d… drh 75 /*
4c4c28d… drh 76 ** A list of ForumStatus objects.
4c4c28d… drh 77 */
4c4c28d… drh 78 struct ForumStatusList {
4c4c28d… drh 79 struct ForumStatus *aStatus; /* List of statuses */
4c4c28d… drh 80 unsigned int n; /* Number of entries */
4c4c28d… drh 81 };
4c4c28d… drh 82
4c4c28d… drh 83 /*
4c4c28d… drh 84 ** Information passed into the status_match() SQL function
4c4c28d… drh 85 ** via the sqlite3_user_data() mechanism, and used by status_match()
4c4c28d… drh 86 ** to determine whether or not a particular forum thread should
4c4c28d… drh 87 ** be displayed.
4c4c28d… drh 88 */
4c4c28d… drh 89 struct ForumStatusMatch {
4c4c28d… drh 90 const ForumStatusList *pFses; /* Parsed forum-statuses setting */
4c4c28d… drh 91 int eStatusTag; /* tagid for the "status" property */
4c4c28d… drh 92 unsigned int iMatch; /* Match this status value */
4c4c28d… drh 93 };
99fcc43… drh 94 #endif /* INTERFACE */
4c4c28d… drh 95
4c4c28d… drh 96
4c4c28d… drh 97 /*
4c4c28d… drh 98 ** Returns a high-level representation of the forum-statuses setting.
4c4c28d… drh 99 ** This is a singleton, cached across calls.
4c4c28d… drh 100 */
ca90659… stephan 101 const ForumStatusList * forum_statuses(void){
4c4c28d… drh 102 static ForumStatusList fses = {0,0};
4c4c28d… drh 103 static int once = 0;
4c4c28d… drh 104 while( !once ){
4c4c28d… drh 105 ++once;
4c4c28d… drh 106 /* Read `forum-statuses` setting and transform it into the
4c4c28d… drh 107 ** fses object.
4c4c28d… drh 108 **
4c4c28d… drh 109 ** Maybe: if it's empty, synthesize a length-1 list from
4c4c28d… drh 110 ** {value:"default",label:"Default",...}. It's expected that
4c4c28d… drh 111 ** usage may be slightly simplified if we always have a non-empty
4c4c28d… drh 112 ** list. A length-1 list is, for purposes of the UI, identical to
4c4c28d… drh 113 ** an empty one - status selection/filtering makes no sense if
4c4c28d… drh 114 ** there's only one choice. */
4c4c28d… drh 115 db_multi_exec(
4c4c28d… drh 116 "CREATE TEMP TABLE IF NOT EXISTS forumstatus("
c56de3f… stephan 117 " ord INTEGER PRIMARY KEY, label, value"
4c4c28d… drh 118 ");"
4c4c28d… drh 119 "DELETE FROM forumstatus;"
c56de3f… stephan 120 "INSERT INTO forumstatus(label,value)"
4c4c28d… drh 121 " WITH setting(v) AS ("
4c4c28d… drh 122 " SELECT value v FROM config WHERE name='forum-statuses'"
4c4c28d… drh 123 " ),"
4c4c28d… drh 124 " room(r) AS ("
4c4c28d… drh 125 " SELECT e.value FROM setting s, jsonb_each(s.v) e"
4c4c28d… drh 126 " WHERE json_valid(s.v, 0x02)"
4c4c28d… drh 127 " )"
c56de3f… stephan 128 " SELECT r->>'label', r->>'value'"
4c4c28d… drh 129 " FROM room;"
4c4c28d… drh 130 );
4c4c28d… drh 131 fses.n = (unsigned)db_int(0, "SELECT count(*) FROM forumstatus");
4c4c28d… drh 132 if( fses.n ){
4c4c28d… drh 133 int i = 0;
4c4c28d… drh 134 Stmt q;
c56de3f… stephan 135 db_prepare(&q,"SELECT label, value FROM forumstatus"
c56de3f… stephan 136 " ORDER BY ord");
4c4c28d… drh 137 fses.aStatus = fossil_malloc(sizeof(fses.aStatus[0]) * fses.n);
4c4c28d… drh 138 while( SQLITE_ROW==db_step(&q) ){
4c4c28d… drh 139 ForumStatus * fs = &fses.aStatus[i++];
4c4c28d… drh 140 fs->zLabel = fossil_strdup(db_column_text(&q, 0));
4c4c28d… drh 141 fs->zValue = fossil_strdup(db_column_text(&q, 1));
4c4c28d… drh 142 }
4c4c28d… drh 143 db_finalize(&q);
4c4c28d… drh 144 }
4c4c28d… drh 145 }
4c4c28d… drh 146 return &fses;
4c4c28d… drh 147 }
4c4c28d… drh 148
4c4c28d… drh 149 /*
4c4c28d… drh 150 ** Search for a ForumStatus object by its tag value. If a match is
4c4c28d… drh 151 ** found, the corresponding object is returned. If no match is found
4c4c28d… drh 152 ** then (A) if bFirst is false then 0 is returned, else (B) the first
4c4c28d… drh 153 ** entry in the list is returned, noting that the list may be empty,
4c4c28d… drh 154 ** in which case 0 is returned.
4c4c28d… drh 155 */
ca90659… stephan 156 static const ForumStatus * forum_status_by_value(
ca90659… stephan 157 const char *z, int bFirst
ca90659… stephan 158 ){
4c4c28d… drh 159 const ForumStatusList * const fses = forum_statuses();
4c4c28d… drh 160 const ForumStatus * fs0 = 0;
4c4c28d… drh 161 unsigned int i;
4c4c28d… drh 162 if( !fses->n ) return 0;
4c4c28d… drh 163 for( i = 0; i < fses->n; ++i ){
4c4c28d… drh 164 const ForumStatus * fs = &fses->aStatus[i];
4c4c28d… drh 165 if( 0==fossil_strcmp(z, fs->zValue) ){
4c4c28d… drh 166 return fs;
4c4c28d… drh 167 }else if( !fs0 ){
4c4c28d… drh 168 fs0 = fs;
4c4c28d… drh 169 }
4c4c28d… drh 170 }
4c4c28d… drh 171 return bFirst ? fs0 : 0;
4c4c28d… drh 172 }
4c4c28d… drh 173
4c4c28d… drh 174 /*
4c4c28d… drh 175 ** COMMAND: test-forum-statuses
4c4c28d… drh 176 */
4c4c28d… drh 177 void test_forum_statuses_cmd(void){
4c4c28d… drh 178 const ForumStatusList * fses;
4c4c28d… drh 179 unsigned i;
4c4c28d… drh 180 db_find_and_open_repository(0,0);
4c4c28d… drh 181 fses = forum_statuses();
4c4c28d… drh 182 for(i = 0; i < fses->n; ++i ){
4c4c28d… drh 183 const ForumStatus * fs = &fses->aStatus[i];
c56de3f… stephan 184 fossil_print("Status: %!j %!j\n", fs->zValue, fs->zLabel);
4c4c28d… drh 185 assert( fs==forum_status_by_value(fs->zValue, 0) );
4c4c28d… drh 186 }
4c4c28d… drh 187 fossil_print("Total statuses: %u\n", i);
4c4c28d… drh 188 }
5182a1b… drh 189
e5653a4… drh 190 /*
5182a1b… drh 191 ** Return true if the forum post with the given rid has been
e5653a4… drh 192 ** subsequently edited.
e5653a4… drh 193 */
e5653a4… drh 194 int forum_rid_has_been_edited(int rid){
e5653a4… drh 195 static Stmt q;
e5653a4… drh 196 int res;
e5653a4… drh 197 db_static_prepare(&q,
e5653a4… drh 198 "SELECT 1 FROM forumpost A, forumpost B"
e5653a4… drh 199 " WHERE A.fpid=$rid AND B.froot=A.froot AND B.fprev=$rid"
e5653a4… drh 200 );
e5653a4… drh 201 db_bind_int(&q, "$rid", rid);
e5653a4… drh 202 res = db_step(&q)==SQLITE_ROW;
e5653a4… drh 203 db_reset(&q);
e5653a4… drh 204 return res;
673dc38… stephan 205 }
673dc38… stephan 206
673dc38… stephan 207 /*
5079ffb… stephan 208 ** Given a valid forumpost.fpid value, this function returns the
5079ffb… stephan 209 ** initial forumpost.fpid in the chain of edits for that forum post,
5079ffb… stephan 210 ** or rid if no prior versions are found.
673dc38… stephan 211 */
5079ffb… stephan 212 int forumpost_head_rid(int rid){
5079ffb… stephan 213 static Stmt q = empty_Stmt_m;
673dc38… stephan 214 int rcRid = rid;
5079ffb… stephan 215 if( !q.pStmt ){
5079ffb… stephan 216 db_static_prepare(&q,
5079ffb… stephan 217 "SELECT fprev FROM forumpost"
5079ffb… stephan 218 " WHERE fpid=:rid AND fprev IS NOT NULL"
5079ffb… stephan 219 );
5079ffb… stephan 220 }
673dc38… stephan 221 db_bind_int(&q, ":rid", rid);
673dc38… stephan 222 while( SQLITE_ROW==db_step(&q) ){
673dc38… stephan 223 rcRid = db_column_int(&q, 0);
673dc38… stephan 224 db_reset(&q);
673dc38… stephan 225 db_bind_int(&q, ":rid", rcRid);
673dc38… stephan 226 }
5079ffb… stephan 227 db_reset(&q);
673dc38… stephan 228 return rcRid;
5079ffb… stephan 229 }
5079ffb… stephan 230
5079ffb… stephan 231 /*
5079ffb… stephan 232 ** Works like forumpost_head_rid() but expects zUuid to be an
5079ffb… stephan 233 ** unambiguous forum post name. It may be a hash prefix, so long as
ca90659… stephan 234 ** it's unambiguous. Returns the rid of the head post, -1 if the name
ca90659… stephan 235 ** is ambiguous, and 0 if the name cannot be resolved as a forum post.
5079ffb… stephan 236 */
5079ffb… stephan 237 int forumpost_head_rid2(const char *zUuid){
5079ffb… stephan 238 const int fpid = symbolic_name_to_rid(zUuid, "f");
5079ffb… stephan 239 return fpid>0
5079ffb… stephan 240 ? forumpost_head_rid(fpid)
ca90659… stephan 241 : fpid;
5079ffb… stephan 242 }
5079ffb… stephan 243
5079ffb… stephan 244 /*
5079ffb… stephan 245 ** Given a forum post RID and user name, returns true if zUserName
5079ffb… stephan 246 ** matches the event.(euser,user) field for a formpost entry with the
5079ffb… stephan 247 ** matching RID. Returns false if no match is found. If zUserName is
5079ffb… stephan 248 ** 0 then login_name() is used.
5079ffb… stephan 249 */
5079ffb… stephan 250 int forumpost_is_owner(int rid, const char *zUserName){
4c4c28d… drh 251 static Stmt q;
4c4c28d… drh 252 int rc;
4c4c28d… drh 253 if( !q.pStmt ){
4c4c28d… drh 254 db_static_prepare(
4c4c28d… drh 255 &q, "SELECT 1 FROM event"
4c4c28d… drh 256 " WHERE type='f' AND objid=$rid"
4c4c28d… drh 257 " AND coalesce(euser,user)=$user"
4c4c28d… drh 258 );
4c4c28d… drh 259 }
4c4c28d… drh 260 db_bind_int(&q, "$rid", rid);
4c4c28d… drh 261 db_bind_text(&q, "$user", zUserName ? zUserName : login_name());
4c4c28d… drh 262 rc = SQLITE_ROW==db_step(&q);
4c4c28d… drh 263 db_reset(&q);
4c4c28d… drh 264 return rc;
673dc38… stephan 265 }
673dc38… stephan 266
673dc38… stephan 267 /*
673dc38… stephan 268 ** Returns true if p, or any parent of p, has a non-zero iClosed
673dc38… stephan 269 ** value. Returns 0 if !p. For an edited chain of post, the tag is
673dc38… stephan 270 ** checked on the pEditHead entry, to simplify subsequent unlocking of
673dc38… stephan 271 ** the post.
673dc38… stephan 272 **
673dc38… stephan 273 ** If bCheckIrt is true then p's thread in-response-to parents are
673dc38… stephan 274 ** checked (recursively) for closure, else only p is checked.
673dc38… stephan 275 */
923aa75… drh 276 static int forumpost_is_closed(
923aa75… drh 277 ForumThread *pThread, /* Thread that the post is a member of */
923aa75… drh 278 ForumPost *p, /* the forum post */
923aa75… drh 279 int bCheckIrt /* True to check In-Reply-To posts */
923aa75… drh 280 ){
923aa75… drh 281 int mx = pThread->nArtifact+1;
923aa75… drh 282 while( p && (mx--)>0 ){
673dc38… stephan 283 if( p->pEditHead ) p = p->pEditHead;
673dc38… stephan 284 if( p->iClosed || !bCheckIrt ) return p->iClosed;
673dc38… stephan 285 p = p->pIrt;
673dc38… stephan 286 }
673dc38… stephan 287 return 0;
673dc38… stephan 288 }
673dc38… stephan 289
673dc38… stephan 290 /*
673dc38… stephan 291 ** Given a forum post RID, this function returns true if that post has
2d7e2cb… stephan 292 ** (or inherits) an active tag named zTagName. If bCheckIrt is true
2d7e2cb… stephan 293 ** then the post to which the given post responds is also checked
673dc38… stephan 294 ** (recursively), else they are not. When checking in-response-to
673dc38… stephan 295 ** posts, the first one which is closed ends the search.
673dc38… stephan 296 **
2d7e2cb… stephan 297 ** This function checks _exactly_ the given rid, whereas forum post
2d7e2cb… stephan 298 ** closure/re-opening is always applied to the head of an edit chain
2d7e2cb… stephan 299 ** so that we get consistent implied locking behavior for later
2d7e2cb… stephan 300 ** versions and responses to arbitrary versions in the chain. Even so,
2d7e2cb… stephan 301 ** the "closed" tag is applied as a propagating tag so will apply to
2d7e2cb… stephan 302 ** all edits in a given chain.
673dc38… stephan 303 **
673dc38… stephan 304 ** The return value is one of:
673dc38… stephan 305 **
2d7e2cb… stephan 306 ** - 0 if no matching tag is found.
673dc38… stephan 307 **
673dc38… stephan 308 ** - The tagxref.rowid of the tagxref entry for the closure if rid is
673dc38… stephan 309 ** the forum post to which the closure applies.
673dc38… stephan 310 **
4c4c28d… drh 311 ** - (-tagxref.rowid) if the given rid inherits the tag from an IRT
4c4c28d… drh 312 ** forum post.
673dc38… stephan 313 */
2d7e2cb… stephan 314 static int forum_rid_is_tagged(int rid, const char *zTagName, int bCheckIrt){
673dc38… stephan 315 static Stmt qIrt = empty_Stmt_m;
673dc38… stephan 316 int rc = 0, i = 0;
673dc38… stephan 317 /* TODO: this can probably be turned into a CTE by someone with
673dc38… stephan 318 ** superior SQL-fu. */
673dc38… stephan 319 for( ; rid; i++ ){
2d7e2cb… stephan 320 rc = rid_has_active_tag_name(rid, zTagName);
673dc38… stephan 321 if( rc || !bCheckIrt ) break;
673dc38… stephan 322 else if( !qIrt.pStmt ) {
673dc38… stephan 323 db_static_prepare(&qIrt,
673dc38… stephan 324 "SELECT firt FROM forumpost "
673dc38… stephan 325 "WHERE fpid=$fpid ORDER BY fmtime DESC"
673dc38… stephan 326 );
673dc38… stephan 327 }
673dc38… stephan 328 db_bind_int(&qIrt, "$fpid", rid);
673dc38… stephan 329 rid = SQLITE_ROW==db_step(&qIrt) ? db_column_int(&qIrt, 0) : 0;
673dc38… stephan 330 db_reset(&qIrt);
673dc38… stephan 331 }
673dc38… stephan 332 return i ? -rc : rc;
673dc38… stephan 333 }
673dc38… stephan 334
5079ffb… stephan 335 /* True if moderation of forum posts performs the same operation
5079ffb… stephan 336 ** on its attachments. */
5079ffb… stephan 337 #define FORUMPOST_MOD_ATTACHMENTS 1
5079ffb… stephan 338 #if FORUMPOST_MOD_ATTACHMENTS
5079ffb… stephan 339 /*
5079ffb… stephan 340 ** Internal helper for moderation_forumpost_...().
5079ffb… stephan 341 */
5079ffb… stephan 342 static void forumpost_prep_pending_attachids(Stmt *q, int fpid){
5079ffb… stephan 343 db_prepare(
5079ffb… stephan 344 q,
5079ffb… stephan 345 "SELECT attachid FROM attachment "
5079ffb… stephan 346 "WHERE target=("
5079ffb… stephan 347 " SELECT uuid FROM blob WHERE rid=%d"
5079ffb… stephan 348 ") and attachid in ("
5079ffb… stephan 349 " SELECT objid FROM modreq"
5079ffb… stephan 350 ")",
5079ffb… stephan 351 forumpost_head_rid(fpid)
5079ffb… stephan 352 );
5079ffb… stephan 353 }
5079ffb… stephan 354 #endif
5079ffb… stephan 355
5079ffb… stephan 356 /*
5079ffb… stephan 357 ** Approve the given forum post RID and any pending-approval
5079ffb… stephan 358 ** attachments associated with its initial version.
5079ffb… stephan 359 */
5079ffb… stephan 360 static void moderation_forumpost_approve(int fpid){
5079ffb… stephan 361 #if !FORUMPOST_MOD_ATTACHMENTS
5079ffb… stephan 362 moderation_approve('f', fpid);
5079ffb… stephan 363 #else
5079ffb… stephan 364 /* Also approve any pending attachments */
5079ffb… stephan 365 Stmt q;
5079ffb… stephan 366 moderation_approve('f', fpid);
5079ffb… stephan 367 forumpost_prep_pending_attachids(&q, fpid);
5079ffb… stephan 368 while( SQLITE_ROW==db_step(&q) ){
5079ffb… stephan 369 moderation_approve('a', db_column_int(&q, 0));
5079ffb… stephan 370 }
5079ffb… stephan 371 db_finalize(&q);
5079ffb… stephan 372 #endif
5079ffb… stephan 373 }
5079ffb… stephan 374
5079ffb… stephan 375 /*
5079ffb… stephan 376 ** Disapprove the given forum post and any pending-moderation
5079ffb… stephan 377 ** attachments on its initial version.
5079ffb… stephan 378 */
5079ffb… stephan 379 static void moderation_forumpost_disapprove(int fpid){
5079ffb… stephan 380 #if !FORUMPOST_MOD_ATTACHMENTS
5079ffb… stephan 381 moderation_disapprove(fpid);
5079ffb… stephan 382 #else
5079ffb… stephan 383 /* Also disapprove any pending attachments */
5079ffb… stephan 384 Stmt q;
5079ffb… stephan 385 moderation_disapprove(fpid);
5079ffb… stephan 386 forumpost_prep_pending_attachids(&q, fpid);
5079ffb… stephan 387 while( SQLITE_ROW==db_step(&q) ){
5079ffb… stephan 388 moderation_disapprove(db_column_int(&q, 0));
5079ffb… stephan 389 }
5079ffb… stephan 390 db_finalize(&q);
5079ffb… stephan 391 #endif
5079ffb… stephan 392 }
5079ffb… stephan 393 #undef FORUMPOST_MOD_ATTACHMENTS
5079ffb… stephan 394
673dc38… stephan 395 /*
4d58d6e… stephan 396 ** Applies or cancels a tag named zTagName on the given forum RID via
4d58d6e… stephan 397 ** addition of a new control artifact into the repository. In order to
4d58d6e… stephan 398 ** provide consistent behavior, it always acts on the first version of
4d58d6e… stephan 399 ** the given forum post, walking the forumpost.fprev values to find
4d58d6e… stephan 400 ** the head of the chain.
4d58d6e… stephan 401 **
4d58d6e… stephan 402 ** If addTag is true then a propagating tag is added, except as noted
4c4c28d… drh 403 ** below, with the given optional zValue string as the tag's
4d58d6e… stephan 404 ** value. If addTag is false then any matching active tag on frid is
4c4c28d… drh 405 ** cancelled, except as noted below. zValue is ignored if it is NULL
5079ffb… stephan 406 ** or starts with a NUL byte, or if addTag is false.
4d58d6e… stephan 407 **
4d58d6e… stephan 408 ** This function only adds a tag if forum_rid_is_tagged() indicates
4d58d6e… stephan 409 ** that frid's head is not tagged. If a parent post is already tagged,
5079ffb… stephan 410 ** no tag is added. Similarly, it will only remove a tag from a post
5079ffb… stephan 411 ** which has its own tag, and will not remove an inherited one from a
5079ffb… stephan 412 ** parent post.
5079ffb… stephan 413 **
4c4c28d… drh 414 ** If addTag is true and frid is already tagged, this is a
ca90659… stephan 415 ** no-op. Likewise, if addTag is false and frid is not tagged (not
ca90659… stephan 416 ** accounting for a tag inherited via an in-response-to post), this is
ca90659… stephan 417 ** a no-op.
ca90659… stephan 418 **
ca90659… stephan 419 ** Returns a positive value (a new tag.tagid value) if it actually
ca90659… stephan 420 ** creates a new tag, else 0. On error it returns a negative alue
ca90659… stephan 421 ** and g.zErrMsg "should" contain details.
4d58d6e… stephan 422 **
4d58d6e… stephan 423 ** If it returns true then state from previously-loaded posts may be
4d58d6e… stephan 424 ** invalidated if they refer to the amended post or a response to it.
4d58d6e… stephan 425 ** e.g. if zTagName is "closed" then ForumPost::iClosed values may be
4d58d6e… stephan 426 ** stale.
673dc38… stephan 427 **
673dc38… stephan 428 ** Sidebars:
673dc38… stephan 429 **
673dc38… stephan 430 ** - Unless the caller has a transaction open, via
673dc38… stephan 431 ** db_begin_transaction(), there is a very tiny race condition
673dc38… stephan 432 ** window during which the caller's idea of whether or not the forum
4d58d6e… stephan 433 ** post is tagged may differ from the current repository state.
673dc38… stephan 434 **
673dc38… stephan 435 ** - This routine assumes that frid really does refer to a forum post.
673dc38… stephan 436 **
673dc38… stephan 437 ** - This routine assumes that frid is not private or pending
673dc38… stephan 438 ** moderation.
673dc38… stephan 439 **
4d58d6e… stephan 440 ** - The applied tag is propagating so so that "closed" tags can
673dc38… stephan 441 ** account for how edits of posts are handled. This differs from
673dc38… stephan 442 ** closure of a branch, where a non-propagating tag is used.
673dc38… stephan 443 */
ca90659… stephan 444 static int forumpost_tag(int frid, int addTag,
ca90659… stephan 445 const char *zTagName,
4c4c28d… drh 446 const char *zValue){
673dc38… stephan 447 Blob artifact = BLOB_INITIALIZER; /* Output artifact */
673dc38… stephan 448 Blob cksum = BLOB_INITIALIZER; /* Z-card */
4d58d6e… stephan 449 int iTagged; /* true if frid is already tagged */
673dc38… stephan 450 int trid; /* RID of new control artifact */
673dc38… stephan 451 char *zUuid; /* UUID of head version of post */
673dc38… stephan 452
673dc38… stephan 453 db_begin_transaction();
673dc38… stephan 454 frid = forumpost_head_rid(frid);
4c4c28d… drh 455 iTagged = forum_rid_is_tagged(frid, zTagName, 0);
4c4c28d… drh 456 if( !addTag && !iTagged ){
4c4c28d… drh 457 /* Nothing to do. We never tag an IRT-inherited post via this
4c4c28d… drh 458 ** function. */
673dc38… stephan 459 db_end_transaction(0);
673dc38… stephan 460 return 0;
673dc38… stephan 461 }
4c4c28d… drh 462 if( !addTag || (zValue && !zValue[0]) ){
4c4c28d… drh 463 zValue = 0;
4c4c28d… drh 464 }
4c4c28d… drh 465 if( addTag && iTagged ){
4c4c28d… drh 466 char *zOld = 0;
4c4c28d… drh 467 int cmp;
ca90659… stephan 468 rid_has_tag2(frid, zTagName, &zOld);
4c4c28d… drh 469 cmp = fossil_strcmp(zOld, zValue);
4c4c28d… drh 470 fossil_free(zOld);
4c4c28d… drh 471 if( 0==cmp ){
4c4c28d… drh 472 /* Same value - leave it as is. */
4c4c28d… drh 473 db_end_transaction(0);
4c4c28d… drh 474 return 0;
4c4c28d… drh 475 }
673dc38… stephan 476 }
673dc38… stephan 477 zUuid = rid_to_uuid(frid);
673dc38… stephan 478 blob_appendf(&artifact, "D %z\n", date_in_standard_format( "now" ));
4d58d6e… stephan 479 blob_appendf(&artifact, "T %c%s %s%s%F\n",
4d58d6e… stephan 480 addTag ? '*' : '-', zTagName,
4c4c28d… drh 481 zUuid, zValue ? " " : "", zValue ? zValue : "");
673dc38… stephan 482 blob_appendf(&artifact, "U %F\n", login_name());
673dc38… stephan 483 md5sum_blob(&artifact, &cksum);
673dc38… stephan 484 blob_appendf(&artifact, "Z %b\n", &cksum);
673dc38… stephan 485 blob_reset(&cksum);
673dc38… stephan 486 trid = content_put_ex(&artifact, 0, 0, 0, 0);
673dc38… stephan 487 if( trid==0 ){
ca90659… stephan 488 return -1;
673dc38… stephan 489 }
4c4c28d… drh 490 if( manifest_crosslink(trid, &artifact, MC_NONE)==0 ){
ca90659… stephan 491 return -2;
673dc38… stephan 492 }
673dc38… stephan 493 assert( blob_is_reset(&artifact) );
673dc38… stephan 494 db_add_unsent(trid);
4d58d6e… stephan 495 admin_log("Tag forum post %S with %c%s",
4d58d6e… stephan 496 zUuid, addTag ? '*' : '-', zTagName);
673dc38… stephan 497 fossil_free(zUuid);
673dc38… stephan 498 db_end_transaction(0);
ca90659… stephan 499 return trid;
ca90659… stephan 500 }
ca90659… stephan 501
ca90659… stephan 502 /*
ca90659… stephan 503 ** COMMAND: test-forumpost-tag
ca90659… stephan 504 **
ca90659… stephan 505 ** Usage: %fossil test-forumpost-tag ?-cancel? THREADID TAGNAME TAGVAL
ca90659… stephan 506 **
ca90659… stephan 507 ** A tester for forumpost_tag(). It always rolls back changes.
ca90659… stephan 508 */
ca90659… stephan 509 void test_forumpost_tag_command(void){
ca90659… stephan 510 int fpid;
ca90659… stephan 511 int rc;
ca90659… stephan 512 const char *zPost;
ca90659… stephan 513 const char *zTag;
ca90659… stephan 514 const char *zVal;
ca90659… stephan 515 const int bAdd = find_option("cancel","",0)==0;
ca90659… stephan 516
ca90659… stephan 517 db_find_and_open_repository(0,0);
ca90659… stephan 518 verify_all_options();
ca90659… stephan 519 if( g.argc<5 ){
ca90659… stephan 520 usage("forum-post-id tag-name value");
ca90659… stephan 521 }
ca90659… stephan 522 zPost = g.argv[2];
ca90659… stephan 523 zTag = g.argv[3];
ca90659… stephan 524 zVal = g.argv[4];
ca90659… stephan 525
ca90659… stephan 526 db_begin_transaction();
ca90659… stephan 527 fpid = forumpost_head_rid2(zPost);
ca90659… stephan 528 if( fpid<=0 ){
ca90659… stephan 529 fossil_fatal("Cannot resolve post ID %s", zPost);
ca90659… stephan 530 }
ca90659… stephan 531 fossil_print("%s => %d => %z\n", zTag, fpid,
ca90659… stephan 532 rid_to_uuid(fpid));
ca90659… stephan 533 rc = forumpost_tag(fpid, bAdd, zTag, zVal);
ca90659… stephan 534 fossil_print("tag fpid=%d taxgxref.tagid=%d\n", fpid, rc);
ca90659… stephan 535 db_end_transaction(1);
673dc38… stephan 536 }
673dc38… stephan 537
673dc38… stephan 538 /*
673dc38… stephan 539 ** Returns true if the forum-close-policy setting is true, else false,
673dc38… stephan 540 ** caching the result for subsequent calls.
673dc38… stephan 541 */
673dc38… stephan 542 static int forumpost_close_policy(void){
673dc38… stephan 543 static int closePolicy = -99;
673dc38… stephan 544
673dc38… stephan 545 if( closePolicy==-99 ){
673dc38… stephan 546 closePolicy = db_get_boolean("forum-close-policy",0)>0;
673dc38… stephan 547 }
673dc38… stephan 548 return closePolicy;
673dc38… stephan 549 }
673dc38… stephan 550
673dc38… stephan 551 /*
673dc38… stephan 552 ** Returns 1 if the current user is an admin, -1 if the current user
673dc38… stephan 553 ** is a forum moderator and the forum-close-policy setting is true,
673dc38… stephan 554 ** else returns 0. The value is cached for subsequent calls.
5079ffb… stephan 555 **
5079ffb… stephan 556 ** This policy also determines whether non-admin forum moderators
5079ffb… stephan 557 ** may delete forum attachments.
673dc38… stephan 558 */
5079ffb… stephan 559 int forumpost_may_close(void){
673dc38… stephan 560 static int permClose = -99;
673dc38… stephan 561 if( permClose!=-99 ){
673dc38… stephan 562 return permClose;
673dc38… stephan 563 }else if( g.perm.Admin ){
673dc38… stephan 564 return permClose = 1;
673dc38… stephan 565 }else if( g.perm.ModForum ){
673dc38… stephan 566 return permClose = forumpost_close_policy()>0 ? -1 : 0;
673dc38… stephan 567 }else{
673dc38… stephan 568 return permClose = 0;
b2f0071… stephan 569 }
673dc38… stephan 570 }
673dc38… stephan 571
673dc38… stephan 572 /*
673dc38… stephan 573 ** Emits a warning that the current forum post is CLOSED and can only
673dc38… stephan 574 ** be edited or responded to by an administrator. */
673dc38… stephan 575 static void forumpost_error_closed(void){
673dc38… stephan 576 @ <div class='error'>This (sub)thread is CLOSED and can only be
673dc38… stephan 577 @ edited or replied to by an admin user.</div>
9543ddb… drh 578 }
9543ddb… drh 579
9543ddb… drh 580 /*
99fcc43… drh 581 ** Delete a complete ForumThread and all its entries.
99fcc43… drh 582 */
99fcc43… drh 583 static void forumthread_delete(ForumThread *pThread){
5182a1b… drh 584 ForumPost *pPost, *pNext;
5182a1b… drh 585 for(pPost=pThread->pFirst; pPost; pPost = pNext){
5182a1b… drh 586 pNext = pPost->pNext;
5182a1b… drh 587 fossil_free(pPost->zUuid);
9543ddb… drh 588 fossil_free(pPost->zDisplayName);
5182a1b… drh 589 fossil_free(pPost);
99fcc43… drh 590 }
99fcc43… drh 591 fossil_free(pThread);
99fcc43… drh 592 }
99fcc43… drh 593
99fcc43… drh 594 /*
5182a1b… drh 595 ** Search a ForumPost list forwards looking for the post with fpid
99fcc43… drh 596 */
5182a1b… drh 597 static ForumPost *forumpost_forward(ForumPost *p, int fpid){
99fcc43… drh 598 while( p && p->fpid!=fpid ) p = p->pNext;
99fcc43… drh 599 return p;
99fcc43… drh 600 }
99fcc43… drh 601
99fcc43… drh 602 /*
5182a1b… drh 603 ** Search backwards for a ForumPost
99fcc43… drh 604 */
5182a1b… drh 605 static ForumPost *forumpost_backward(ForumPost *p, int fpid){
99fcc43… drh 606 while( p && p->fpid!=fpid ) p = p->pPrev;
99fcc43… drh 607 return p;
99fcc43… drh 608 }
99fcc43… drh 609
99fcc43… drh 610 /*
5182a1b… drh 611 ** Add a post to the display list
99fcc43… drh 612 */
5182a1b… drh 613 static void forumpost_add_to_display(ForumThread *pThread, ForumPost *p){
99fcc43… drh 614 if( pThread->pDisplay==0 ){
99fcc43… drh 615 pThread->pDisplay = p;
99fcc43… drh 616 }else{
99fcc43… drh 617 pThread->pTail->pDisplay = p;
99fcc43… drh 618 }
99fcc43… drh 619 pThread->pTail = p;
99fcc43… drh 620 }
99fcc43… drh 621
99fcc43… drh 622 /*
99fcc43… drh 623 ** Extend the display list for pThread by adding all entries that
5182a1b… drh 624 ** reference fpid. The first such post will be no earlier then
5182a1b… drh 625 ** post "p".
99fcc43… drh 626 */
99fcc43… drh 627 static void forumthread_display_order(
97697b7… drh 628 ForumThread *pThread, /* The complete thread */
5182a1b… drh 629 ForumPost *pBase /* Add replies to this post */
99fcc43… drh 630 ){
5182a1b… drh 631 ForumPost *p;
5182a1b… drh 632 ForumPost *pPrev = 0;
5182a1b… drh 633 ForumPost *pBaseIrt;
97697b7… drh 634 for(p=pBase->pNext; p; p=p->pNext){
5182a1b… drh 635 if( !p->pEditPrev && p->pIrt ){
5182a1b… drh 636 pBaseIrt = p->pIrt->pEditHead ? p->pIrt->pEditHead : p->pIrt;
5182a1b… drh 637 if( pBaseIrt==pBase ){
5182a1b… drh 638 if( pPrev ){
5182a1b… drh 639 pPrev->nIndent = pBase->nIndent + 1;
5182a1b… drh 640 forumpost_add_to_display(pThread, pPrev);
5182a1b… drh 641 forumthread_display_order(pThread, pPrev);
5182a1b… drh 642 }
5182a1b… drh 643 pPrev = p;
5182a1b… drh 644 }
99fcc43… drh 645 }
97697b7… drh 646 }
97697b7… drh 647 if( pPrev ){
0cec52e… drh 648 pPrev->nIndent = pBase->nIndent + 1;
0cec52e… drh 649 if( pPrev->nIndent>pThread->mxIndent ) pThread->mxIndent = pPrev->nIndent;
5182a1b… drh 650 forumpost_add_to_display(pThread, pPrev);
97697b7… drh 651 forumthread_display_order(pThread, pPrev);
99fcc43… drh 652 }
99fcc43… drh 653 }
99fcc43… drh 654
99fcc43… drh 655 /*
99fcc43… drh 656 ** Construct a ForumThread object given the root record id.
99fcc43… drh 657 */
99fcc43… drh 658 static ForumThread *forumthread_create(int froot, int computeHierarchy){
99fcc43… drh 659 ForumThread *pThread;
5182a1b… drh 660 ForumPost *pPost;
5182a1b… drh 661 ForumPost *p;
99fcc43… drh 662 Stmt q;
0cec52e… drh 663 int sid = 1;
5182a1b… drh 664 int firt, fprev;
99fcc43… drh 665 pThread = fossil_malloc( sizeof(*pThread) );
99fcc43… drh 666 memset(pThread, 0, sizeof(*pThread));
99fcc43… drh 667 db_prepare(&q,
9543ddb… drh 668 "SELECT fpid, firt, fprev, (SELECT uuid FROM blob WHERE rid=fpid), fmtime"
99fcc43… drh 669 " FROM forumpost"
99fcc43… drh 670 " WHERE froot=%d ORDER BY fmtime",
99fcc43… drh 671 froot
99fcc43… drh 672 );
99fcc43… drh 673 while( db_step(&q)==SQLITE_ROW ){
5182a1b… drh 674 pPost = fossil_malloc( sizeof(*pPost) );
5182a1b… drh 675 memset(pPost, 0, sizeof(*pPost));
5182a1b… drh 676 pPost->fpid = db_column_int(&q, 0);
5182a1b… drh 677 firt = db_column_int(&q, 1);
5182a1b… drh 678 fprev = db_column_int(&q, 2);
5182a1b… drh 679 pPost->zUuid = fossil_strdup(db_column_text(&q,3));
9543ddb… drh 680 pPost->rDate = db_column_double(&q,4);
5182a1b… drh 681 if( !fprev ) pPost->sid = sid++;
5182a1b… drh 682 pPost->pPrev = pThread->pLast;
5182a1b… drh 683 pPost->pNext = 0;
99fcc43… drh 684 if( pThread->pLast==0 ){
5182a1b… drh 685 pThread->pFirst = pPost;
5182a1b… drh 686 }else{
5182a1b… drh 687 pThread->pLast->pNext = pPost;
5182a1b… drh 688 }
5182a1b… drh 689 pThread->pLast = pPost;
923aa75… drh 690 pThread->nArtifact++;
5182a1b… drh 691
5182a1b… drh 692 /* Find the in-reply-to post. Default to the topic post if the replied-to
5182a1b… drh 693 ** post cannot be found. */
5182a1b… drh 694 if( firt ){
5182a1b… drh 695 pPost->pIrt = pThread->pFirst;
5182a1b… drh 696 for(p=pThread->pFirst; p; p=p->pNext){
5182a1b… drh 697 if( p->fpid==firt ){
5182a1b… drh 698 pPost->pIrt = p;
5182a1b… drh 699 break;
5182a1b… drh 700 }
5182a1b… drh 701 }
5182a1b… drh 702 }
5182a1b… drh 703
5182a1b… drh 704 /* Maintain the linked list of post edits. */
5182a1b… drh 705 if( fprev ){
5182a1b… drh 706 p = forumpost_backward(pPost->pPrev, fprev);
5182a1b… drh 707 p->pEditNext = pPost;
5182a1b… drh 708 pPost->sid = p->sid;
5182a1b… drh 709 pPost->rev = p->rev+1;
5182a1b… drh 710 pPost->nEdit = p->nEdit+1;
5182a1b… drh 711 pPost->pEditPrev = p;
5182a1b… drh 712 pPost->pEditHead = p->pEditHead ? p->pEditHead : p;
5182a1b… drh 713 for(; p; p=p->pEditPrev ){
5182a1b… drh 714 p->nEdit = pPost->nEdit;
5182a1b… drh 715 p->pEditTail = pPost;
5182a1b… drh 716 }
5182a1b… drh 717 }
2d7e2cb… stephan 718 pPost->iClosed = forum_rid_is_tagged(pPost->pEditHead
673dc38… stephan 719 ? pPost->pEditHead->fpid
2d7e2cb… stephan 720 : pPost->fpid, "closed", 1);
5182a1b… drh 721 }
5182a1b… drh 722 db_finalize(&q);
99fcc43… drh 723
99fcc43… drh 724 if( computeHierarchy ){
99fcc43… drh 725 /* Compute the hierarchical display order */
5182a1b… drh 726 pPost = pThread->pFirst;
5182a1b… drh 727 pPost->nIndent = 1;
0cec52e… drh 728 pThread->mxIndent = 1;
5182a1b… drh 729 forumpost_add_to_display(pThread, pPost);
5182a1b… drh 730 forumthread_display_order(pThread, pPost);
99fcc43… drh 731 }
99fcc43… drh 732
99fcc43… drh 733 /* Return the result */
99fcc43… drh 734 return pThread;
99fcc43… drh 735 }
99fcc43… drh 736
99fcc43… drh 737 /*
98270ad… drh 738 ** List all forum threads to standard output.
98270ad… drh 739 */
98270ad… drh 740 static void forum_thread_list(void){
98270ad… drh 741 Stmt q;
98270ad… drh 742 db_prepare(&q,
98270ad… drh 743 " SELECT"
98270ad… drh 744 " datetime(max(fmtime)),"
98270ad… drh 745 " sum(fprev IS NULL),"
98270ad… drh 746 " froot"
98270ad… drh 747 " FROM forumpost"
98270ad… drh 748 " GROUP BY froot"
98270ad… drh 749 " ORDER BY 1;"
98270ad… drh 750 );
98270ad… drh 751 fossil_print(" id cnt most recent post\n");
98270ad… drh 752 fossil_print("------ ---- -------------------\n");
98270ad… drh 753 while( db_step(&q)==SQLITE_ROW ){
98270ad… drh 754 fossil_print("%6d %4d %s\n",
98270ad… drh 755 db_column_int(&q, 2),
98270ad… drh 756 db_column_int(&q, 1),
98270ad… drh 757 db_column_text(&q, 0)
98270ad… drh 758 );
98270ad… drh 759 }
98270ad… drh 760 db_finalize(&q);
98270ad… drh 761 }
98270ad… drh 762
98270ad… drh 763 /*
99fcc43… drh 764 ** COMMAND: test-forumthread
99fcc43… drh 765 **
98270ad… drh 766 ** Usage: %fossil test-forumthread [THREADID]
98270ad… drh 767 **
98270ad… drh 768 ** Display a summary of all messages on a thread THREADID. If the
98270ad… drh 769 ** THREADID argument is omitted, then show a list of all threads.
99fcc43… drh 770 **
98270ad… drh 771 ** This command is intended for testing an analysis only.
99fcc43… drh 772 */
99fcc43… drh 773 void forumthread_cmd(void){
99fcc43… drh 774 int fpid;
99fcc43… drh 775 int froot;
99fcc43… drh 776 const char *zName;
99fcc43… drh 777 ForumThread *pThread;
5182a1b… drh 778 ForumPost *p;
99fcc43… drh 779
99fcc43… drh 780 db_find_and_open_repository(0,0);
99fcc43… drh 781 verify_all_options();
98270ad… drh 782 if( g.argc==2 ){
98270ad… drh 783 forum_thread_list();
98270ad… drh 784 return;
98270ad… drh 785 }
99fcc43… drh 786 if( g.argc!=3 ) usage("THREADID");
99fcc43… drh 787 zName = g.argv[2];
99fcc43… drh 788 fpid = symbolic_name_to_rid(zName, "f");
99fcc43… drh 789 if( fpid<=0 ){
97697b7… drh 790 fpid = db_int(0, "SELECT rid FROM blob WHERE rid=%d", atoi(zName));
97697b7… drh 791 }
97697b7… drh 792 if( fpid<=0 ){
93a5d65… drh 793 fossil_fatal("unknown or ambiguous forum id: \"%s\"", zName);
99fcc43… drh 794 }
99fcc43… drh 795 froot = db_int(0, "SELECT froot FROM forumpost WHERE fpid=%d", fpid);
99fcc43… drh 796 if( froot==0 ){
99fcc43… drh 797 fossil_fatal("Not a forum post: \"%s\"", zName);
99fcc43… drh 798 }
99fcc43… drh 799 fossil_print("fpid = %d\n", fpid);
99fcc43… drh 800 fossil_print("froot = %d\n", froot);
99fcc43… drh 801 pThread = forumthread_create(froot, 1);
923aa75… drh 802 fossil_print("count = %d\n", pThread->nArtifact);
99fcc43… drh 803 fossil_print("Chronological:\n");
dbdf50d… drh 804 fossil_print(
dbdf50d… drh 805 /* 0 1 2 3 4 5 6 7 */
dbdf50d… drh 806 /* 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123 */
673dc38… stephan 807 " sid rev closed fpid pIrt pEditPrev pEditTail hash\n");
99fcc43… drh 808 for(p=pThread->pFirst; p; p=p->pNext){
673dc38… stephan 809 fossil_print("%4d %4d %7d %9d %9d %9d %9d %8.8s\n",
673dc38… stephan 810 p->sid, p->rev,
673dc38… stephan 811 p->iClosed,
5182a1b… drh 812 p->fpid, p->pIrt ? p->pIrt->fpid : 0,
5182a1b… drh 813 p->pEditPrev ? p->pEditPrev->fpid : 0,
5182a1b… drh 814 p->pEditTail ? p->pEditTail->fpid : 0, p->zUuid);
99fcc43… drh 815 }
99fcc43… drh 816 fossil_print("\nDisplay\n");
99fcc43… drh 817 for(p=pThread->pDisplay; p; p=p->pDisplay){
99fcc43… drh 818 fossil_print("%*s", (p->nIndent-1)*3, "");
5182a1b… drh 819 if( p->pEditTail ){
673dc38… stephan 820 fossil_print("%d->%d", p->fpid, p->pEditTail->fpid);
99fcc43… drh 821 }else{
673dc38… stephan 822 fossil_print("%d", p->fpid);
99fcc43… drh 823 }
673dc38… stephan 824 if( p->iClosed ){
673dc38… stephan 825 fossil_print(" [closed%s]", p->iClosed<0 ? " via parent" : "");
673dc38… stephan 826 }
673dc38… stephan 827 fossil_print("\n");
99fcc43… drh 828 }
99fcc43… drh 829 forumthread_delete(pThread);
673dc38… stephan 830 }
673dc38… stephan 831
673dc38… stephan 832 /*
0d2ceb8… drh 833 ** Fill the TEMP table zTab with the RID of every artifact associated
0d2ceb8… drh 834 ** with the forum thread whose root post is froot. That is the forum
0d2ceb8… drh 835 ** posts themselves, plus any control artifacts that tag those posts,
0d2ceb8… drh 836 ** such as to close or reopen a post or to change the status of the
0d2ceb8… drh 837 ** thread. The table is created if it does not already exist, with
0d2ceb8… drh 838 ** this schema:
0d2ceb8… drh 839 **
0d2ceb8… drh 840 ** CREATE TEMP TABLE zTab(rid INTEGER PRIMARY KEY,
0d2ceb8… drh 841 ** isCtrl BOOLEAN, zDesc TEXT);
0d2ceb8… drh 842 **
0d2ceb8… drh 843 ** isCtrl is true for control artifacts. If bDesc is true, then zDesc
0d2ceb8… drh 844 ** is set to an HTML description of the tags that each control artifact
0d2ceb8… drh 845 ** applies to posts in the thread. Otherwise zDesc is left NULL. zDesc
0d2ceb8… drh 846 ** is always NULL for the forum posts themselves.
0d2ceb8… drh 847 **
0d2ceb8… drh 848 ** If bClear is true, any prior content of zTab is deleted first.
0d2ceb8… drh 849 ** Otherwise, new rows are added to whatever zTab already contains,
0d2ceb8… drh 850 ** and RIDs that are already present are left unchanged.
0d2ceb8… drh 851 **
0d2ceb8… drh 852 ** Candidate control artifacts come from tagxref.srcid, which only
0d2ceb8… drh 853 ** remembers the most recent tag of each name on each post, and from
0d2ceb8… drh 854 ** the event.comment that manifest_crosslink() generates for each
0d2ceb8… drh 855 ** control artifact, which also finds tags that have since been
0d2ceb8… drh 856 ** superseded. That comment includes a hash prefix of at least 12
0d2ceb8… drh 857 ** characters for each tagged artifact. Each candidate is then
0d2ceb8… drh 858 ** parsed to verify that it really does tag a post in this thread.
0d2ceb8… drh 859 **
0d2ceb8… drh 860 ** zTab is inserted into the SQL as-is, so it must be a trusted
0d2ceb8… drh 861 ** identifier, not user input.
0d2ceb8… drh 862 */
0d2ceb8… drh 863 void forum_thread_artifacts(
0d2ceb8… drh 864 const char *zTab, /* Name of the TEMP table to fill */
0d2ceb8… drh 865 int froot, /* RID of the root post of the thread */
0d2ceb8… drh 866 int bClear, /* True to delete prior content of zTab first */
0d2ceb8… drh 867 int bDesc /* True to fill in zDesc for control artifacts */
0d2ceb8… drh 868 ){
0d2ceb8… drh 869 Stmt q;
0d2ceb8… drh 870 db_multi_exec(
0d2ceb8… drh 871 "CREATE TEMP TABLE IF NOT EXISTS \"%w\"("
0d2ceb8… drh 872 " rid INTEGER PRIMARY KEY, isCtrl BOOLEAN, zDesc TEXT);",
0d2ceb8… drh 873 zTab
0d2ceb8… drh 874 );
0d2ceb8… drh 875 if( bClear ){
0d2ceb8… drh 876 db_multi_exec("DELETE FROM \"%w\";", zTab);
0d2ceb8… drh 877 }
0d2ceb8… drh 878 db_multi_exec(
0d2ceb8… drh 879 "INSERT OR IGNORE INTO \"%w\"(rid,isCtrl)"
0d2ceb8… drh 880 " SELECT fpid, 0 FROM forumpost WHERE froot=%d;",
0d2ceb8… drh 881 zTab, froot
0d2ceb8… drh 882 );
0d2ceb8… drh 883
0d2ceb8… drh 884 /* Find candidate control artifacts. Each one is verified before
0d2ceb8… drh 885 ** being added, and candidates already in zTab are skipped, so that
0d2ceb8… drh 886 ** when bClear is false, rows that were already in zTab are not
0d2ceb8… drh 887 ** disturbed. */
0d2ceb8… drh 888 db_prepare(&q,
0d2ceb8… drh 889 "SELECT srcid FROM tagxref"
0d2ceb8… drh 890 " WHERE rid IN (SELECT fpid FROM forumpost WHERE froot=%d)"
0d2ceb8… drh 891 " AND srcid>0 AND srcid!=rid"
0d2ceb8… drh 892 " UNION "
0d2ceb8… drh 893 "SELECT event.objid FROM event, forumpost, blob"
0d2ceb8… drh 894 " WHERE forumpost.froot=%d"
0d2ceb8… drh 895 " AND blob.rid=forumpost.fpid"
0d2ceb8… drh 896 " AND event.type='g'"
0d2ceb8… drh 897 " AND instr(event.comment, '['||substr(blob.uuid,1,12))>0"
0d2ceb8… drh 898 " AND event.mtime >= forumpost.fmtime-0.01"
0d2ceb8… drh 899 " AND event.mtime <= (SELECT max(mtime) FROM tagxref"
0d2ceb8… drh 900 " WHERE rid=%d)",
0d2ceb8… drh 901 froot, froot, froot
0d2ceb8… drh 902 );
0d2ceb8… drh 903 while( db_step(&q)==SQLITE_ROW ){
0d2ceb8… drh 904 int rid = db_column_int(&q, 0);
0d2ceb8… drh 905 Manifest *pCtrl;
0d2ceb8… drh 906 Blob desc = BLOB_INITIALIZER;
0d2ceb8… drh 907 int nMatch = 0;
0d2ceb8… drh 908 int i;
0d2ceb8… drh 909 if( db_exists("SELECT 1 FROM \"%w\" WHERE rid=%d", zTab, rid) ){
0d2ceb8… drh 910 continue;
0d2ceb8… drh 911 }
0d2ceb8… drh 912 pCtrl = manifest_get(rid, CFTYPE_CONTROL, 0);
0d2ceb8… drh 913 for(i=0; pCtrl && i<pCtrl->nTag; i++){
0d2ceb8… drh 914 const char *zTag = pCtrl->aTag[i].zName;
0d2ceb8… drh 915 const char *zVal = pCtrl->aTag[i].zValue;
0d2ceb8… drh 916 const char *zTarget = pCtrl->aTag[i].zUuid;
0d2ceb8… drh 917 int isCancel = zTag[0]=='-';
0d2ceb8… drh 918 if( zTarget==0
0d2ceb8… drh 919 || !db_exists("SELECT 1 FROM forumpost, blob"
0d2ceb8… drh 920 " WHERE forumpost.froot=%d AND blob.rid=forumpost.fpid"
0d2ceb8… drh 921 " AND blob.uuid=%Q", froot, zTarget)
0d2ceb8… drh 922 ){
0d2ceb8… drh 923 continue;
0d2ceb8… drh 924 }
0d2ceb8… drh 925 nMatch++;
0d2ceb8… drh 926 if( !bDesc ) break;
0d2ceb8… drh 927 if( blob_size(&desc) ) blob_append(&desc, "; ", 2);
0d2ceb8… drh 928 if( fossil_strcmp(&zTag[1], "closed")==0 ){
0d2ceb8… drh 929 blob_append(&desc, isCancel ? "reopen" : "close", -1);
0d2ceb8… drh 930 }else if( fossil_strcmp(&zTag[1], "status")==0 ){
0d2ceb8… drh 931 if( isCancel || zVal==0 || zVal[0]==0 ){
0d2ceb8… drh 932 blob_append(&desc, "clear status of", -1);
0d2ceb8… drh 933 }else{
0d2ceb8… drh 934 blob_appendf(&desc, "set status \"%h\" on", zVal);
0d2ceb8… drh 935 }
0d2ceb8… drh 936 }else if( zVal && zVal[0] ){
0d2ceb8… drh 937 blob_appendf(&desc, "tag %h=\"%h\" on", zTag, zVal);
0d2ceb8… drh 938 }else{
0d2ceb8… drh 939 blob_appendf(&desc, "tag %h on", zTag);
0d2ceb8… drh 940 }
0d2ceb8… drh 941 blob_appendf(&desc, " post %z%S</a>",
0d2ceb8… drh 942 href("%R/forumpost/%!S", zTarget), zTarget);
0d2ceb8… drh 943 }
0d2ceb8… drh 944 manifest_destroy(pCtrl);
0d2ceb8… drh 945 if( nMatch ){
0d2ceb8… drh 946 db_multi_exec(
0d2ceb8… drh 947 "INSERT INTO \"%w\"(rid,isCtrl,zDesc) VALUES(%d,1,%Q)",
0d2ceb8… drh 948 zTab, rid, bDesc ? blob_str(&desc) : 0
0d2ceb8… drh 949 );
0d2ceb8… drh 950 }
0d2ceb8… drh 951 blob_reset(&desc);
0d2ceb8… drh 952 }
0d2ceb8… drh 953 db_finalize(&q);
0d2ceb8… drh 954 }
0d2ceb8… drh 955
0d2ceb8… drh 956 /*
83928d8… drh 957 ** WEBPAGE: forumthreadhashlist
83928d8… drh 958 **
83928d8… drh 959 ** Usage: /forumthreadhashlist/HASH-OF-ROOT
83928d8… drh 960 **
83928d8… drh 961 ** This page (accessibly only to admins) shows a list of all artifacts
0d2ceb8… drh 962 ** associated with a single forum thread, including control artifacts
0d2ceb8… drh 963 ** that close or reopen posts or change the status of the thread.
0d2ceb8… drh 964 ** An admin might copy/paste the list of hashes into the /shun page in
0d2ceb8… drh 965 ** order to shun an entire thread.
83928d8… drh 966 */
83928d8… drh 967 void forumthreadhashlist(void){
83928d8… drh 968 int fpid;
83928d8… drh 969 int froot;
83928d8… drh 970 const char *zName = P("name");
83928d8… drh 971 char *fuuid;
042a750… drh 972 Stmt q;
83928d8… drh 973
83928d8… drh 974 login_check_credentials();
83928d8… drh 975 if( !g.perm.Admin ){
83928d8… drh 976 return;
83928d8… drh 977 }
83928d8… drh 978 if( zName==0 ){
83928d8… drh 979 webpage_error("Missing \"name=\" query parameter");
83928d8… drh 980 }
83928d8… drh 981 fpid = symbolic_name_to_rid(zName, "f");
83928d8… drh 982 if( fpid<=0 ){
83928d8… drh 983 if( fpid==0 ){
83928d8… drh 984 webpage_notfound_error("Unknown forum id: \"%s\"", zName);
83928d8… drh 985 }else{
83928d8… drh 986 ambiguous_page();
83928d8… drh 987 }
83928d8… drh 988 return;
83928d8… drh 989 }
83928d8… drh 990 froot = db_int(0, "SELECT froot FROM forumpost WHERE fpid=%d", fpid);
83928d8… drh 991 if( froot==0 ){
83928d8… drh 992 webpage_notfound_error("Not a forum post: \"%s\"", zName);
83928d8… drh 993 }
83928d8… drh 994 fuuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", froot);
83928d8… drh 995 style_set_current_feature("forum");
83928d8… drh 996 style_header("Artifacts Of Forum Thread");
0d2ceb8… drh 997
0d2ceb8… drh 998 forum_thread_artifacts("fthread", froot, 1, 1);
0d2ceb8… drh 999 describe_artifacts("IN (SELECT rid FROM fthread)");
0d2ceb8… drh 1000
83928d8… drh 1001 @ <h2>
83928d8… drh 1002 @ Artifacts associated with the forum thread
83928d8… drh 1003 @ <a href="%R/forumthread/%S(fuuid)">%S(fuuid)</a>:</h2>
0d2ceb8… drh 1004 @ <table border="1" cellpadding="4" cellspacing="0">
0d2ceb8… drh 1005 @ <tr><th>Date<th>Hash<th>User<th>Description
0d2ceb8… drh 1006 db_prepare(&q,
0d2ceb8… drh 1007 "SELECT description.uuid,"
0d2ceb8… drh 1008 " datetime(coalesce(event.mtime,description.ctime)),"
0d2ceb8… drh 1009 " coalesce(event.euser,event.user),"
0d2ceb8… drh 1010 " description.summary, fthread.zDesc"
0d2ceb8… drh 1011 " FROM fthread JOIN description USING(rid)"
0d2ceb8… drh 1012 " LEFT JOIN event ON event.objid=fthread.rid"
0d2ceb8… drh 1013 " ORDER BY coalesce(event.mtime,description.ctime), fthread.rid"
0d2ceb8… drh 1014 );
0d2ceb8… drh 1015 while( db_step(&q)==SQLITE_ROW ){
0d2ceb8… drh 1016 const char *zUuid = db_column_text(&q, 0);
0d2ceb8… drh 1017 const char *zCtrlDesc = db_column_text(&q, 4);
0d2ceb8… drh 1018 @ <tr><td>%h(db_column_text(&q,1))\
0d2ceb8… drh 1019 @ <td>%z(href("%R/artifact/%!S",zUuid))%S(zUuid)</a>\
0d2ceb8… drh 1020 @ <td>%h(db_column_text(&q,2))\
0d2ceb8… drh 1021 if( zCtrlDesc ){
0d2ceb8… drh 1022 @ <td>forum-tag: %s(zCtrlDesc)</tr>
0d2ceb8… drh 1023 }else{
0d2ceb8… drh 1024 @ <td>%h(db_column_text(&q,3))</tr>
0d2ceb8… drh 1025 }
0d2ceb8… drh 1026 }
0d2ceb8… drh 1027 @ </table>
0d2ceb8… drh 1028 @ <h2>Artifact hashes, suitable for pasting into
0d2ceb8… drh 1029 @ %z(href("%R/shun"))/shun</a>:</h2>
83928d8… drh 1030 @ <pre>
0d2ceb8… drh 1031 db_reset(&q);
0d2ceb8… drh 1032 while( db_step(&q)==SQLITE_ROW ){
0d2ceb8… drh 1033 @ %h(db_column_text(&q,0))
83928d8… drh 1034 }
0d2ceb8… drh 1035 db_finalize(&q);
83928d8… drh 1036 @ </pre>
042a750… drh 1037 @ <hr>
042a750… drh 1038 @ <h2>Related FORUMPOST Table Content</h2>
042a750… drh 1039 @ <table border="1" cellpadding="4" cellspacing="0">
042a750… drh 1040 @ <tr><th>fpid<th>froot<th>fprev<th>firt<th>fmtime
042a750… drh 1041 db_prepare(&q, "SELECT fpid, froot, fprev, firt, datetime(fmtime)"
042a750… drh 1042 " FROM forumpost"
042a750… drh 1043 " WHERE froot=%d"
042a750… drh 1044 " ORDER BY fmtime", froot);
042a750… drh 1045 while( db_step(&q)==SQLITE_ROW ){
042a750… drh 1046 @ <tr><td>%d(db_column_int(&q,0))\
042a750… drh 1047 @ <td>%d(db_column_int(&q,1))\
042a750… drh 1048 @ <td>%d(db_column_int(&q,2))\
042a750… drh 1049 @ <td>%d(db_column_int(&q,3))\
042a750… drh 1050 @ <td>%h(db_column_text(&q,4))</tr>
042a750… drh 1051 }
042a750… drh 1052 @ </table>
042a750… drh 1053 db_finalize(&q);
83928d8… drh 1054 style_finish_page();
4c4c28d… drh 1055 }
4c4c28d… drh 1056
4c4c28d… drh 1057 /*
4c4c28d… drh 1058 ** Returns true if the current user is authorized to set forum post
4c4c28d… drh 1059 ** fpid's status.
4c4c28d… drh 1060 */
4c4c28d… drh 1061 static int forum_may_set_status(int fpid){
449bb3b… stephan 1062 if( moderation_pending(fpid) ) return 0;
4c4c28d… drh 1063 return g.perm.Admin
4c4c28d… drh 1064 || g.perm.ModForum
4c4c28d… drh 1065 || (login_is_individual()
4c4c28d… drh 1066 && forumpost_is_owner(fpid, 0));
4c4c28d… drh 1067 }
4c4c28d… drh 1068
4c4c28d… drh 1069 /*
4c4c28d… drh 1070 ** If the current user is authorized to set fp's status then this
4c4c28d… drh 1071 ** renders a mini-form for setting the status then redirecting back to
4c4c28d… drh 1072 ** the post. Else it may emit a status label or no output.
4c4c28d… drh 1073 */
4c4c28d… drh 1074 static void forum_render_status_selection( const ForumPost *fp ){
4c4c28d… drh 1075 const ForumStatusList * const fss = forum_statuses();
4c4c28d… drh 1076 if( fss->n>1 ){
4c4c28d… drh 1077 const ForumPost * pHead = fp->pEditHead ? fp->pEditHead : fp;
4c4c28d… drh 1078 int i;
4c4c28d… drh 1079 char * zCurrent = 0;
4c4c28d… drh 1080 const ForumStatus * sCurrent = 0;
4c4c28d… drh 1081 rid_has_tag2(pHead->fpid, "status", &zCurrent);
4c4c28d… drh 1082 for( i = 0; i < fss->n; ++i ){
4c4c28d… drh 1083 const ForumStatus * const fs = &fss->aStatus[i];
4c4c28d… drh 1084 if( 0==fossil_strcmp(zCurrent, fs->zValue) ){
4c4c28d… drh 1085 sCurrent = fs;
4c4c28d… drh 1086 break;
4c4c28d… drh 1087 }
4c4c28d… drh 1088 }
4c4c28d… drh 1089 if( !sCurrent ) sCurrent = &fss->aStatus[0];
4c4c28d… drh 1090 assert( sCurrent );
ca90659… stephan 1091 @ <fieldset class='forum-status-selection'>\
ca90659… stephan 1092 @ <legend>Status \
ca90659… stephan 1093 @ <span class='help-buttonlet initially-hidden'>\
ca90659… stephan 1094 @ Moderators and the post's owner may change \
ca90659… stephan 1095 @ the status of this thread unless it is still. \
ca90659… stephan 1096 @ pending moderation. See \
ca90659… stephan 1097 @ <a href='%R/help/forum-statuses' target='_new'>\
ca90659… stephan 1098 @ /help/forum-statuses</a></span>\
ca90659… stephan 1099 @ </legend>\
4c4c28d… drh 1100 if( forum_may_set_status(fp->fpid) ){
4c4c28d… drh 1101 @ <form method="post" action='%R/forumpost_status'>
4c4c28d… drh 1102 login_insert_csrf_secret();
4c4c28d… drh 1103 @ <input type='hidden' name='fpid' value='%s(fp->zUuid)' />
4c4c28d… drh 1104 @ <select name='status' data-fpid='%s(fp->zUuid)'\
4c4c28d… drh 1105 @ data-initial-value='%h(zCurrent ? zCurrent : "")'>
4c4c28d… drh 1106 for( i = 0; i < fss->n; ++i ){
4c4c28d… drh 1107 const ForumStatus * const fs = &fss->aStatus[i];
4c4c28d… drh 1108 @ <option value='%h(fs->zValue)'\
4c4c28d… drh 1109 @ %s(sCurrent==fs ? " selected" : "")>\
4c4c28d… drh 1110 @ %h(fs->zLabel)</option>
4c4c28d… drh 1111 }
4c4c28d… drh 1112 @ </select>
4c4c28d… drh 1113 @ <input type='button' class='submit action-status' disabled
4c4c28d… drh 1114 @ value='Change' />
4c4c28d… drh 1115 /* ^^^ This must be <input>, not <button>, or else tapping it
4c4c28d… drh 1116 ** will unconditionally submit. */
4c4c28d… drh 1117 @ </form>
4c4c28d… drh 1118 /* Form is activated in fossil.page.forumpost.js */
4c4c28d… drh 1119 }else{
4c4c28d… drh 1120 @ <button disabled>Status: %h(sCurrent->zLabel)</button>
4c4c28d… drh 1121 }
ca90659… stephan 1122 @ </fieldset>
4c4c28d… drh 1123 fossil_free(zCurrent);
4c4c28d… drh 1124 }
99fcc43… drh 1125 }
99fcc43… drh 1126
99fcc43… drh 1127 /*
99fcc43… drh 1128 ** Render a forum post for display
99fcc43… drh 1129 */
99fcc43… drh 1130 void forum_render(
99fcc43… drh 1131 const char *zTitle, /* The title. Might be NULL for no title */
99fcc43… drh 1132 const char *zMimetype, /* Mimetype of the message */
99fcc43… drh 1133 const char *zContent, /* Content of the message */
46dd696… drh 1134 const char *zClass, /* Put in a <div> if not NULL */
46dd696… drh 1135 int bScroll /* Large message content scrolls if true */
99fcc43… drh 1136 ){
99fcc43… drh 1137 if( zClass ){
99fcc43… drh 1138 @ <div class='%s(zClass)'>
99fcc43… drh 1139 }
99fcc43… drh 1140 if( zTitle ){
99fcc43… drh 1141 if( zTitle[0] ){
99fcc43… drh 1142 @ <h1>%h(zTitle)</h1>
99fcc43… drh 1143 }else{
99fcc43… drh 1144 @ <h1><i>Deleted</i></h1>
99fcc43… drh 1145 }
99fcc43… drh 1146 }
99fcc43… drh 1147 if( zContent && zContent[0] ){
99fcc43… drh 1148 Blob x;
7caaa28… stephan 1149 const int isFossilWiki = zMimetype==0
7caaa28… stephan 1150 || fossil_strcmp(zMimetype, "text/x-fossil-wiki")==0;
46dd696… drh 1151 if( bScroll ){
46dd696… drh 1152 @ <div class='forumPostBody'>
46dd696… drh 1153 }else{
46dd696… drh 1154 @ <div class='forumPostFullBody'>
46dd696… drh 1155 }
99fcc43… drh 1156 blob_init(&x, 0, 0);
99fcc43… drh 1157 blob_append(&x, zContent, -1);
89b6dda… drh 1158 safe_html_context(DOCSRC_FORUM);
7caaa28… stephan 1159 if( isFossilWiki ){
7caaa28… stephan 1160 /* Markdown and plain-text rendering add a wrapper DIV resp. PRE
7caaa28… stephan 1161 ** element around the post, and some CSS relies on its existence
7caaa28… stephan 1162 ** in order to handle expansion/collapse of the post. Fossil
7caaa28… stephan 1163 ** Wiki rendering does not do so, so we must wrap those manually
7caaa28… stephan 1164 ** here. */
7caaa28… stephan 1165 @ <div class='fossilWiki'>
7caaa28… stephan 1166 }
89b6dda… drh 1167 wiki_render_by_mimetype(&x, zMimetype);
7caaa28… stephan 1168 if( isFossilWiki ){
7caaa28… stephan 1169 @ </div>
7caaa28… stephan 1170 }
99fcc43… drh 1171 blob_reset(&x);
421955c… drh 1172 @ </div>
99fcc43… drh 1173 }else{
99fcc43… drh 1174 @ <i>Deleted</i>
99fcc43… drh 1175 }
99fcc43… drh 1176 if( zClass ){
99fcc43… drh 1177 @ </div>
99fcc43… drh 1178 }
99fcc43… drh 1179 }
99fcc43… drh 1180
99fcc43… drh 1181 /*
2e71dc2… drh 1182 ** Compute a display name from a login name.
2e71dc2… drh 1183 **
2e71dc2… drh 1184 ** If the input login is found in the USER table, then check the USER.INFO
2e71dc2… drh 1185 ** field to see if it has display-name followed by an email address.
2e71dc2… drh 1186 ** If it does, that becomes the new display name. If not, let the display
2e71dc2… drh 1187 ** name just be the login.
2e71dc2… drh 1188 **
d5455f5… drh 1189 ** Space to hold the returned name is obtained from fossil_strdup() or
d5455f5… drh 1190 ** mprintf() and should be freed by the caller.
46d7ccd… drh 1191 **
46d7ccd… drh 1192 ** HTML markup within the reply has been property escaped. Hyperlinks
46d7ccd… drh 1193 ** may have been added. The result is safe for use with %s.
2e71dc2… drh 1194 */
9543ddb… drh 1195 static char *display_name_from_login(const char *zLogin){
2e71dc2… drh 1196 static Stmt q;
2e71dc2… drh 1197 char *zResult;
2e71dc2… drh 1198 db_static_prepare(&q,
d5455f5… drh 1199 "SELECT display_name(info) FROM user WHERE login=$login"
2e71dc2… drh 1200 );
2e71dc2… drh 1201 db_bind_text(&q, "$login", zLogin);
d5455f5… drh 1202 if( db_step(&q)==SQLITE_ROW && db_column_type(&q,0)==SQLITE_TEXT ){
a5d6567… drh 1203 const char *zDisplay = db_column_text(&q,0);
a5d6567… drh 1204 if( fossil_strcmp(zDisplay,zLogin)==0 ){
46d7ccd… drh 1205 zResult = mprintf("%z%h</a>",
46d7ccd… drh 1206 href("%R/timeline?ss=v&y=f&vfx&u=%t",zLogin),zLogin);
a5d6567… drh 1207 }else{
46d7ccd… drh 1208 zResult = mprintf("%s (%z%h</a>)", zDisplay,
46d7ccd… drh 1209 href("%R/timeline?ss=v&y=f&vfx&u=%t",zLogin),zLogin);
a5d6567… drh 1210 }
d5455f5… drh 1211 }else{
46d7ccd… drh 1212 zResult = mprintf("%z%h</a>",
46d7ccd… drh 1213 href("%R/timeline?ss=v&y=f&vfx&u=%t",zLogin),zLogin);
d5455f5… drh 1214 }
2e71dc2… drh 1215 db_reset(&q);
2e71dc2… drh 1216 return zResult;
2e71dc2… drh 1217 }
2e71dc2… drh 1218
cd9cb4d… drh 1219 /*
9543ddb… drh 1220 ** Compute and return the display name for a ForumPost. If
9543ddb… drh 1221 ** pManifest is not NULL, then it is a Manifest object for the post.
9543ddb… drh 1222 ** if pManifest is NULL, this routine has to fetch and parse the
9543ddb… drh 1223 ** Manifest object for itself.
9543ddb… drh 1224 **
9543ddb… drh 1225 ** Memory to hold the display name is attached to p->zDisplayName
9543ddb… drh 1226 ** and will be freed together with the ForumPost object p when it
9543ddb… drh 1227 ** is freed.
46d7ccd… drh 1228 **
46d7ccd… drh 1229 ** The returned text has had all HTML markup escaped and is safe for
46d7ccd… drh 1230 ** use within %s.
9543ddb… drh 1231 */
9543ddb… drh 1232 static char *forum_post_display_name(ForumPost *p, Manifest *pManifest){
9543ddb… drh 1233 Manifest *pToFree = 0;
9543ddb… drh 1234 if( p->zDisplayName ) return p->zDisplayName;
9543ddb… drh 1235 if( pManifest==0 ){
9543ddb… drh 1236 pManifest = pToFree = manifest_get(p->fpid, CFTYPE_FORUM, 0);
9543ddb… drh 1237 if( pManifest==0 ) return "(unknown)";
9543ddb… drh 1238 }
9543ddb… drh 1239 p->zDisplayName = display_name_from_login(pManifest->zUser);
9543ddb… drh 1240 if( pToFree ) manifest_destroy(pToFree);
9543ddb… drh 1241 if( p->zDisplayName==0 ) return "(unknown)";
9543ddb… drh 1242 return p->zDisplayName;
9543ddb… drh 1243 }
9543ddb… drh 1244
5079ffb… stephan 1245 /*
5079ffb… stephan 1246 ** Renders the attachment list for the given forum post.
5079ffb… stephan 1247 ** Emits no output if there are no attachments.
5079ffb… stephan 1248 */
5079ffb… stephan 1249 static void forum_render_attachment_list(const char *zUuid){
ca90659… stephan 1250 #if 1
ca90659… stephan 1251 attachment_list(zUuid, "&#x1f4ce; Attachments", 0
ca90659… stephan 1252 | ATTACHLIST_SIZE
ca90659… stephan 1253 | ATTACHLIST_HIDE_UNAPPROVED
ca90659… stephan 1254 | ATTACHLIST_DETAILS_CLOSED
ca90659… stephan 1255 | ATTACHLIST_HIDE_EMPTY);
ca90659… stephan 1256 #else
ca90659… stephan 1257 char * zLbl = mprintf("<a href='%R/attachlist?forumpost=%!S'>"
ca90659… stephan 1258 "Attachments</a>:", zUuid);
ca90659… stephan 1259 attachment_list(zUuid, zLbl,
ca90659… stephan 1260 ATTACHLIST_HRULE_ABOVE
ca90659… stephan 1261 | ATTACHLIST_SIZE
ca90659… stephan 1262 | ATTACHLIST_HIDE_UNAPPROVED
ca90659… stephan 1263 | ATTACHLIST_HIDE_EMPTY);
ca90659… stephan 1264 fossil_free(zLbl);
ca90659… stephan 1265 #endif
5079ffb… stephan 1266 }
5079ffb… stephan 1267
5079ffb… stephan 1268 /*
5079ffb… stephan 1269 ** Renders the attachment list for p or (if not NULL) pEditHead.
5079ffb… stephan 1270 */
5079ffb… stephan 1271 static void forum_render_attachment_list2(ForumPost *p){
5079ffb… stephan 1272 if( p->pEditHead ) p = p->pEditHead;
5079ffb… stephan 1273 forum_render_attachment_list(p->zUuid);
5079ffb… stephan 1274 }
5079ffb… stephan 1275
4c4c28d… drh 1276 /* Flags for use with forum_display_post() */
4c4c28d… drh 1277 #define FDISPLAY_RAW 0x01 /* omit the border */
4c4c28d… drh 1278 #define FDISPLAY_UNFORMATTED 0x02 /* leave the post unformatted */
4c4c28d… drh 1279 #define FDISPLAY_HISTORY 0x04 /* Showing edit history */
4c4c28d… drh 1280 #define FDISPLAY_SELECTED 0x08 /* This is the selected post */
4c4c28d… drh 1281
9543ddb… drh 1282 /*
5182a1b… drh 1283 ** Display a single post in a forum thread.
dbdf50d… drh 1284 */
5182a1b… drh 1285 static void forum_display_post(
923aa75… drh 1286 ForumThread *pThread, /* The thread that this post is a member of */
5182a1b… drh 1287 ForumPost *p, /* Forum post to display */
5182a1b… drh 1288 int iIndentScale, /* Indent scale factor */
4c4c28d… drh 1289 int flags, /* From the FDISPLAY_... enum */
5182a1b… drh 1290 char *zQuery /* Common query string */
5182a1b… drh 1291 ){
9543ddb… drh 1292 char *zPosterName; /* Name of user who originally made this post */
9543ddb… drh 1293 char *zEditorName; /* Name of user who provided the current edit */
5182a1b… drh 1294 char *zDate; /* The time/date string */
68b2737… drh 1295 char *zDateZulu; /* The date/time string in Zulul time */
5182a1b… drh 1296 char *zHist; /* History query string */
9543ddb… drh 1297 Manifest *pManifest; /* Manifest comprising the current post */
5182a1b… drh 1298 int bPrivate; /* True for posts awaiting moderation */
5182a1b… drh 1299 int bSameUser; /* True if author is also the reader */
5182a1b… drh 1300 int iIndent; /* Indent level */
673dc38… stephan 1301 int iClosed; /* True if (sub)thread is closed */
4c4c28d… drh 1302 const int bRaw = flags & FDISPLAY_RAW;
4c4c28d… drh 1303 const int bUnf = flags & FDISPLAY_UNFORMATTED;
4c4c28d… drh 1304 const int bHist = flags & FDISPLAY_HISTORY;
4c4c28d… drh 1305 const int bSelect = flags & FDISPLAY_SELECTED;
5182a1b… drh 1306 const char *zMimetype;/* Formatting MIME type */
5182a1b… drh 1307
9543ddb… drh 1308 /* Get the manifest for the post. Abort if not found (e.g. shunned). */
9543ddb… drh 1309 pManifest = manifest_get(p->fpid, CFTYPE_FORUM, 0);
9543ddb… drh 1310 if( !pManifest ) return;
923aa75… drh 1311 iClosed = forumpost_is_closed(pThread, p, 1);
ca90659… stephan 1312 bPrivate = content_is_private(p->fpid);
ca90659… stephan 1313 bSameUser = login_is_individual()
ca90659… stephan 1314 && fossil_strcmp(pManifest->zUser, g.zLogin)==0;
5182a1b… drh 1315 /* When not in raw mode, create the border around the post. */
5182a1b… drh 1316 if( !bRaw ){
923aa75… drh 1317 /* Open the <div> enclosing the post. Set the class string to mark the post
5182a1b… drh 1318 ** as selected and/or obsolete. */
5182a1b… drh 1319 iIndent = (p->pEditHead ? p->pEditHead->nIndent : p->nIndent)-1;
ca90659… stephan 1320 @ <div id='forum%d(p->fpid)' class='forumpost forumTime\
5182a1b… drh 1321 @ %s(bSelect ? " forumSel" : "")\
673dc38… stephan 1322 @ %s(iClosed ? " forumClosed" : "")\
7221e49… drh 1323 @ %s(p->pEditTail ? " forumObs" : "")' \
5182a1b… drh 1324 if( iIndent && iIndentScale ){
ca90659… stephan 1325 @ style='margin-left:%d(iIndent*iIndentScale)ex;' \
5182a1b… drh 1326 }
ca90659… stephan 1327 /* These data-X fields are used by the JS editor. */
ca90659… stephan 1328 if( p->pIrt ){
ca90659… stephan 1329 @ data-firt="%s(p->pIrt->zUuid)" \
ca90659… stephan 1330 }
ca90659… stephan 1331 if( p->pEditHead ){
ca90659… stephan 1332 @ data-fedithead="%s(p->pEditHead->zUuid)" \
ca90659… stephan 1333 }
ca90659… stephan 1334 @ data-fpid="%s(p->zUuid)">\
5182a1b… drh 1335
5182a1b… drh 1336 /* If this is the first post (or an edit thereof), emit the thread title. */
9543ddb… drh 1337 if( pManifest->zThreadTitle ){
9543ddb… drh 1338 @ <h1>%h(pManifest->zThreadTitle)</h1>
9543ddb… drh 1339 }
9543ddb… drh 1340
9543ddb… drh 1341 /* Begin emitting the header line. The forum of the title
9543ddb… drh 1342 ** varies depending on whether:
91ff34d… drh 1343 ** * The post is unedited
91ff34d… drh 1344 ** * The post was last edited by the original author
91ff34d… drh 1345 ** * The post was last edited by a different person
91ff34d… drh 1346 */
68b2737… drh 1347 if( !p->pEditHead ){
c67f5bf… george 1348 zPosterName = forum_post_display_name(p, pManifest);
c67f5bf… george 1349 zEditorName = zPosterName;
c67f5bf… george 1350 }
c67f5bf… george 1351 zDate = db_text(0, "SELECT datetime(%.17g,toLocal())", p->rDate);
68b2737… drh 1352 zDateZulu = db_text(0,
68b2737… drh 1353 "SELECT strftime('%%Y-%%m-%%dT%%H:%%M:%%SZ',%.17g)",
68b2737… drh 1354 p->rDate);
c67f5bf… george 1355 if( p->pEditPrev ){
c67f5bf… george 1356 zPosterName = forum_post_display_name(p->pEditHead, 0);
c67f5bf… george 1357 zEditorName = forum_post_display_name(p, pManifest);
168eb71… drh 1358 zHist = bHist ? "" : zQuery[0]==0 ? "?hist" : "&hist";
c67f5bf… george 1359 @ <h3 class='forumPostHdr'>(%d(p->sid)\
673dc38… stephan 1360 @ .%0*d(fossil_num_digits(p->nEdit))(p->rev))
c67f5bf… george 1361 if( fossil_strcmp(zPosterName, zEditorName)==0 ){
68b2737… drh 1362 @ By %s(zPosterName) on \
68b2737… drh 1363 style_copy_button(1, 0, 0, 0, zDateZulu, "%h", zDate);
68b2737… drh 1364 @ &#32;edited from \
114b17c… george 1365 @ %z(href("%R/forumpost/%S%s%s",p->pEditPrev->zUuid,zQuery,zHist))\
c67f5bf… george 1366 @ %d(p->sid).%0*d(fossil_num_digits(p->nEdit))(p->pEditPrev->rev)</a>
c67f5bf… george 1367 }else{
46d7ccd… drh 1368 @ Originally by %s(zPosterName) \
68b2737… drh 1369 @ with edits by %s(zEditorName) on \
68b2737… drh 1370 style_copy_button(1, 0, 0, 0, zDateZulu, "%h", zDate);
68b2737… drh 1371 @ &#32;from \
168eb71… drh 1372 @ %z(href("%R/forumpost/%S%s%s",p->pEditPrev->zUuid,zQuery,zHist))\
c67f5bf… george 1373 @ %d(p->sid).%0*d(fossil_num_digits(p->nEdit))(p->pEditPrev->rev)</a>
c67f5bf… george 1374 }
c67f5bf… george 1375 }else{
c67f5bf… george 1376 zPosterName = forum_post_display_name(p, pManifest);
673dc38… stephan 1377 @ <h3 class='forumPostHdr'>(%d(p->sid))
e399337… drh 1378 @ By %s(zPosterName) on \
68b2737… drh 1379 style_copy_button(1, 0, 0, 0, zDateZulu, "%h", zDate);
68b2737… drh 1380 cgi_append_content(" ", 1);
c67f5bf… george 1381 }
c67f5bf… george 1382 fossil_free(zDate);
68b2737… drh 1383 fossil_free(zDateZulu);
9543ddb… drh 1384
5182a1b… drh 1385
5182a1b… drh 1386 /* If debugging is enabled, link to the artifact page. */
dbdf50d… drh 1387 if( g.perm.Debug ){
dbdf50d… drh 1388 @ <span class="debug">\
dbdf50d… drh 1389 @ <a href="%R/artifact/%h(p->zUuid)">(artifact-%d(p->fpid))</a></span>
dbdf50d… drh 1390 }
5182a1b… drh 1391
5182a1b… drh 1392 /* If this is a reply, refer back to the parent post. */
5182a1b… drh 1393 if( p->pIrt ){
168eb71… drh 1394 @ in reply to %z(href("%R/forumpost/%S%s",p->pIrt->zUuid,zQuery))\
5182a1b… drh 1395 @ %d(p->pIrt->sid)\
5182a1b… drh 1396 if( p->pIrt->nEdit ){
9543ddb… drh 1397 @ .%0*d(fossil_num_digits(p->pIrt->nEdit))(p->pIrt->rev)\
5182a1b… drh 1398 }
5182a1b… drh 1399 @ </a>
5182a1b… drh 1400 }
5182a1b… drh 1401
5182a1b… drh 1402 /* If this post was later edited, refer forward to the next edit. */
5182a1b… drh 1403 if( p->pEditNext ){
168eb71… drh 1404 @ updated by %z(href("%R/forumpost/%S%s",p->pEditNext->zUuid,zQuery))\
5182a1b… drh 1405 @ %d(p->pEditNext->sid)\
9543ddb… drh 1406 @ .%0*d(fossil_num_digits(p->nEdit))(p->pEditNext->rev)</a>
5182a1b… drh 1407 }
5182a1b… drh 1408
5182a1b… drh 1409 /* Provide a link to select the individual post. */
5182a1b… drh 1410 if( !bSelect ){
168eb71… drh 1411 @ %z(href("%R/forumpost/%!S%s",p->zUuid,zQuery))[link]</a>
5182a1b… drh 1412 }
5182a1b… drh 1413
5182a1b… drh 1414 /* Provide a link to the raw source code. */
5182a1b… drh 1415 if( !bUnf ){
62f0ce8… stephan 1416 @ %z(href("%R/forumpost/%!S?raw",p->zUuid))[source]</a>
5182a1b… drh 1417 }
dbdf50d… drh 1418 @ </h3>
ca90659… stephan 1419
ca90659… stephan 1420 if( bPrivate && (bSameUser || g.perm.Admin || g.perm.ModForum) ){
ca90659… stephan 1421 moderation_pending_www(p->fpid);
ca90659… stephan 1422 }
4c4c28d… drh 1423 }/*!bRaw*/
4c4c28d… drh 1424
ca90659… stephan 1425 /* Check if this post is approved, also if it's by the current user.
ca90659… stephan 1426 Render the post if the user is able to see it. */
5182a1b… drh 1427 if( bPrivate && !g.perm.ModForum && !bSameUser ){
5182a1b… drh 1428 @ <p><span class="modpending">Awaiting Moderator Approval</span></p>
5182a1b… drh 1429 }else{
5182a1b… drh 1430 if( bRaw || bUnf || p->pEditTail ){
5182a1b… drh 1431 zMimetype = "text/plain";
5182a1b… drh 1432 }else{
9543ddb… drh 1433 zMimetype = pManifest->zMimetype;
5182a1b… drh 1434 }
9543ddb… drh 1435 forum_render(0, zMimetype, pManifest->zWiki, 0, !bRaw);
5182a1b… drh 1436 }
5182a1b… drh 1437
5182a1b… drh 1438 /* When not in raw mode, finish creating the border around the post. */
5182a1b… drh 1439 if( !bRaw ){
ca90659… stephan 1440 int bBrBeforeAttach = 0; /* Layout kludge for Attach button */
ca90659… stephan 1441 forum_render_attachment_list2(p);
5182a1b… drh 1442 /* If the user is able to write to the forum and if this post has not been
5182a1b… drh 1443 ** edited, create a form with various interaction buttons. */
5182a1b… drh 1444 if( g.perm.WrForum && !p->pEditTail ){
673dc38… stephan 1445 @ <div class="forumpost-single-controls">\
673dc38… stephan 1446 @ <form action="%R/forumedit" method="POST">
dbdf50d… drh 1447 @ <input type="hidden" name="fpid" value="%s(p->zUuid)">
5182a1b… drh 1448 if( !bPrivate ){
673dc38… stephan 1449 /* Reply and Edit are only available if the post has been
673dc38… stephan 1450 ** approved. Closed threads can only be edited or replied to
673dc38… stephan 1451 ** if forumpost_may_close() is true but a user may delete
673dc38… stephan 1452 ** their own posts even if they are closed. */
673dc38… stephan 1453 if( forumpost_may_close() || !iClosed ){
673dc38… stephan 1454 @ <input type="submit" name="reply" value="Reply">
673dc38… stephan 1455 if( g.perm.Admin || (bSameUser && !iClosed) ){
673dc38… stephan 1456 @ <input type="submit" name="edit" value="Edit">
673dc38… stephan 1457 }
673dc38… stephan 1458 if( g.perm.Admin || bSameUser ){
673dc38… stephan 1459 @ <input type="submit" name="nullout" value="Delete">
673dc38… stephan 1460 }
dbdf50d… drh 1461 }
dbdf50d… drh 1462 }else if( g.perm.ModForum ){
5182a1b… drh 1463 /* Allow moderators to approve or reject pending posts. Also allow
5182a1b… drh 1464 ** forum supervisors to mark non-special users as trusted and therefore
5182a1b… drh 1465 ** able to post unmoderated. */
68414a4… stephan 1466 @ <input type="submit" name="approve" value="Approve" \
68414a4… stephan 1467 @ class="action-approve">\
68414a4… stephan 1468 @ <input type="submit" name="reject" value="Reject" \
68414a4… stephan 1469 @ class="action-reject">\
9543ddb… drh 1470 if( g.perm.AdminForum && !login_is_special(pManifest->zUser) ){
5182a1b… drh 1471 @ <br><label><input type="checkbox" name="trust">
9543ddb… drh 1472 @ Trust user "%h(pManifest->zUser)" so that future posts by \
9543ddb… drh 1473 @ "%h(pManifest->zUser)" do not require moderation.
5182a1b… drh 1474 @ </label>
9543ddb… drh 1475 @ <input type="hidden" name="trustuser" value="%h(pManifest->zUser)">
ca90659… stephan 1476 bBrBeforeAttach = 1 /* slightly unmangle the layout */;
5182a1b… drh 1477 }
5182a1b… drh 1478 }else if( bSameUser ){
5182a1b… drh 1479 /* Allow users to delete (reject) their own pending posts. */
dbdf50d… drh 1480 @ <input type="submit" name="reject" value="Delete">
dbdf50d… drh 1481 }
920ace1… drh 1482 login_insert_csrf_secret();
920ace1… drh 1483 @ </form>
5079ffb… stephan 1484
5079ffb… stephan 1485 if( bSelect ){
5079ffb… stephan 1486 const ForumPost *pHead = p->pEditHead ? p->pEditHead : p;
f1c281d… stephan 1487 if( !bPrivate && forumpost_may_close() && iClosed>=0 ){
5079ffb… stephan 1488 @ <form method="post" \
5079ffb… stephan 1489 @ action='%R/forumpost_%s(iClosed > 0 ? "reopen" : "close")'>
5079ffb… stephan 1490 login_insert_csrf_secret();
4c4c28d… drh 1491 @ <input type="hidden" name="fpid" value="%s(p->zUuid)" />
5079ffb… stephan 1492 if( moderation_pending(p->fpid)==0 ){
5079ffb… stephan 1493 @ <input type="button" value='%s(iClosed ? "Re-open" : "Close")' \
4c4c28d… drh 1494 @ class='submit hidden \
4c4c28d… drh 1495 @ %s(iClosed ? "action-reopen" : "action-close")'/>
4c4c28d… drh 1496 /* ^^^ activated by fossil.page.forumpost.js */
5079ffb… stephan 1497 }
5079ffb… stephan 1498 @ </form>
5079ffb… stephan 1499 }
ca90659… stephan 1500 if( attach_user_may(p/*not pHead*/->fpid, CFTYPE_FORUM) ){
5079ffb… stephan 1501 /* When an admin edits someone else's post, the admin
5079ffb… stephan 1502 ** effectively takes over ownership of it (and we currently
5079ffb… stephan 1503 ** have no way of passing it back). Because of this, we
5079ffb… stephan 1504 ** check the ownership of `p` instead of `pHead`. */
ca90659… stephan 1505 if( bBrBeforeAttach ){
ca90659… stephan 1506 @ <br>
ca90659… stephan 1507 }
ca90659… stephan 1508 attach_render_attachadd_button(pHead->zUuid);
5079ffb… stephan 1509 }
673dc38… stephan 1510 }
673dc38… stephan 1511 @ </div>
673dc38… stephan 1512 }
4c4c28d… drh 1513 if( !p->pIrt && (flags & FDISPLAY_SELECTED)){
4c4c28d… drh 1514 forum_render_status_selection(p);
4c4c28d… drh 1515 }
dbdf50d… drh 1516 @ </div>
4c4c28d… drh 1517 }/*!bRaw*/
5182a1b… drh 1518
5182a1b… drh 1519 /* Clean up. */
9543ddb… drh 1520 manifest_destroy(pManifest);
5182a1b… drh 1521 }
5182a1b… drh 1522
5182a1b… drh 1523 /*
5182a1b… drh 1524 ** Possible display modes for forum_display_thread().
5182a1b… drh 1525 */
5182a1b… drh 1526 enum {
5182a1b… drh 1527 FD_RAW, /* Like FD_SINGLE, but additionally omit the border, force
5182a1b… drh 1528 ** unformatted mode, and inhibit history mode */
5182a1b… drh 1529 FD_SINGLE, /* Render a single post and (optionally) its edit history */
5182a1b… drh 1530 FD_CHRONO, /* Render all posts in chronological order */
5182a1b… drh 1531 FD_HIER, /* Render all posts in an indented hierarchy */
5182a1b… drh 1532 };
5182a1b… drh 1533
5182a1b… drh 1534 /*
5182a1b… drh 1535 ** Display a forum thread. If mode is FD_RAW or FD_SINGLE, display only a
5182a1b… drh 1536 ** single post from the thread and (optionally) its edit history.
5182a1b… drh 1537 */
5182a1b… drh 1538 static void forum_display_thread(
5182a1b… drh 1539 int froot, /* Forum thread root post ID */
5182a1b… drh 1540 int fpid, /* Selected forum post ID, or 0 if none selected */
5182a1b… drh 1541 int mode, /* Forum display mode, one of the FD_* enumerations */
168eb71… drh 1542 int autoMode, /* mode was selected automatically */
5182a1b… drh 1543 int bUnf, /* True if rendering unformatted */
5182a1b… drh 1544 int bHist /* True if showing edit history, ignored for FD_RAW */
5182a1b… drh 1545 ){
5182a1b… drh 1546 ForumThread *pThread; /* Thread structure */
5182a1b… drh 1547 ForumPost *pSelect; /* Currently selected post, or NULL if none */
5182a1b… drh 1548 ForumPost *p; /* Post iterator pointer */
168eb71… drh 1549 char zQuery[30]; /* Common query string */
5182a1b… drh 1550 int iIndentScale = 4; /* Indent scale factor, measured in "ex" units */
5182a1b… drh 1551 int sid; /* Comparison serial ID */
168eb71… drh 1552 int i;
5182a1b… drh 1553
5182a1b… drh 1554 /* In raw mode, force unformatted display and disable history. */
5182a1b… drh 1555 if( mode == FD_RAW ){
5182a1b… drh 1556 bUnf = 1;
5182a1b… drh 1557 bHist = 0;
dbdf50d… drh 1558 }
dbdf50d… drh 1559
5182a1b… drh 1560 /* Thread together the posts and (optionally) compute the hierarchy. */
5182a1b… drh 1561 pThread = forumthread_create(froot, mode==FD_HIER);
5182a1b… drh 1562
5182a1b… drh 1563 /* Compute the appropriate indent scaling. */
5182a1b… drh 1564 if( mode==FD_HIER ){
5182a1b… drh 1565 iIndentScale = 4;
5182a1b… drh 1566 while( iIndentScale>1 && iIndentScale*pThread->mxIndent>25 ){
5182a1b… drh 1567 iIndentScale--;
5182a1b… drh 1568 }
5182a1b… drh 1569 }else{
5182a1b… drh 1570 iIndentScale = 0;
5182a1b… drh 1571 }
5182a1b… drh 1572
5182a1b… drh 1573 /* Find the selected post, or (depending on parameters) its latest edit. */
5182a1b… drh 1574 pSelect = fpid ? forumpost_forward(pThread->pFirst, fpid) : 0;
5182a1b… drh 1575 if( !bHist && mode!=FD_RAW && pSelect && pSelect->pEditTail ){
5182a1b… drh 1576 pSelect = pSelect->pEditTail;
5182a1b… drh 1577 }
5182a1b… drh 1578
5182a1b… drh 1579 /* When displaying only a single post, abort if no post was selected or the
5182a1b… drh 1580 ** selected forum post does not exist in the thread. Otherwise proceed to
5182a1b… drh 1581 ** display the entire thread without marking any posts as selected. */
5182a1b… drh 1582 if( !pSelect && (mode==FD_RAW || mode==FD_SINGLE) ){
5182a1b… drh 1583 return;
5182a1b… drh 1584 }
5182a1b… drh 1585
5182a1b… drh 1586 /* Create the common query string to append to nearly all post links. */
168eb71… drh 1587 i = 0;
168eb71… drh 1588 if( !autoMode ){
168eb71… drh 1589 char m = 'a';
168eb71… drh 1590 switch( mode ){
168eb71… drh 1591 case FD_RAW: m = 'r'; break;
168eb71… drh 1592 case FD_CHRONO: m = 'c'; break;
168eb71… drh 1593 case FD_HIER: m = 'h'; break;
168eb71… drh 1594 case FD_SINGLE: m = 's'; break;
168eb71… drh 1595 }
168eb71… drh 1596 zQuery[i++] = '?';
168eb71… drh 1597 zQuery[i++] = 't';
168eb71… drh 1598 zQuery[i++] = '=';
168eb71… drh 1599 zQuery[i++] = m;
168eb71… drh 1600 }
168eb71… drh 1601 if( bUnf ){
168eb71… drh 1602 zQuery[i] = i==0 ? '?' : '&'; i++;
168eb71… drh 1603 zQuery[i++] = 'u';
168eb71… drh 1604 zQuery[i++] = 'n';
168eb71… drh 1605 zQuery[i++] = 'f';
168eb71… drh 1606 }
168eb71… drh 1607 if( bHist ){
168eb71… drh 1608 zQuery[i] = i==0 ? '?' : '&'; i++;
168eb71… drh 1609 zQuery[i++] = 'h';
168eb71… drh 1610 zQuery[i++] = 'i';
168eb71… drh 1611 zQuery[i++] = 's';
168eb71… drh 1612 zQuery[i++] = 't';
168eb71… drh 1613 }
53db40e… drh 1614 assert( i<(int)sizeof(zQuery) );
168eb71… drh 1615 zQuery[i] = 0;
114b17c… george 1616 assert( zQuery[0]==0 || zQuery[0]=='?' );
5182a1b… drh 1617
5182a1b… drh 1618 /* Identify which post to display first. If history is shown, start with the
5182a1b… drh 1619 ** original, unedited post. Otherwise advance to the post's latest edit. */
5182a1b… drh 1620 if( mode==FD_RAW || mode==FD_SINGLE ){
5182a1b… drh 1621 p = pSelect;
5182a1b… drh 1622 if( bHist && p->pEditHead ) p = p->pEditHead;
5182a1b… drh 1623 }else{
5182a1b… drh 1624 p = mode==FD_CHRONO ? pThread->pFirst : pThread->pDisplay;
5182a1b… drh 1625 if( !bHist && p->pEditTail ) p = p->pEditTail;
5182a1b… drh 1626 }
5182a1b… drh 1627
5182a1b… drh 1628 /* Display the appropriate subset of posts in sequence. */
5182a1b… drh 1629 while( p ){
5182a1b… drh 1630 /* Display the post. */
4c4c28d… drh 1631 forum_display_post(
4c4c28d… drh 1632 pThread, p, iIndentScale,
4c4c28d… drh 1633 (mode==FD_RAW ? FDISPLAY_RAW : 0) |
4c4c28d… drh 1634 (bUnf ? FDISPLAY_UNFORMATTED : 0) |
4c4c28d… drh 1635 (bHist ? FDISPLAY_HISTORY : 0) |
4c4c28d… drh 1636 (p==pSelect ? FDISPLAY_SELECTED : 0),
4c4c28d… drh 1637 zQuery
4c4c28d… drh 1638 );
5182a1b… drh 1639
5182a1b… drh 1640 /* Advance to the next post in the thread. */
5182a1b… drh 1641 if( mode==FD_CHRONO ){
5182a1b… drh 1642 /* Chronological mode: display posts (optionally including edits) in their
5182a1b… drh 1643 ** original commit order. */
5182a1b… drh 1644 if( bHist ){
5182a1b… drh 1645 p = p->pNext;
5182a1b… drh 1646 }else{
5182a1b… drh 1647 sid = p->sid;
5182a1b… drh 1648 if( p->pEditHead ) p = p->pEditHead;
5182a1b… drh 1649 do p = p->pNext; while( p && p->sid<=sid );
5182a1b… drh 1650 if( p && p->pEditTail ) p = p->pEditTail;
5182a1b… drh 1651 }
5182a1b… drh 1652 }else if( bHist && p->pEditNext ){
5182a1b… drh 1653 /* Hierarchical and single mode: display each post's edits in sequence. */
5182a1b… drh 1654 p = p->pEditNext;
5182a1b… drh 1655 }else if( mode==FD_HIER ){
5182a1b… drh 1656 /* Hierarchical mode: after displaying with each post (optionally
5182a1b… drh 1657 ** including edits), go to the next post in computed display order. */
5182a1b… drh 1658 p = p->pEditHead ? p->pEditHead->pDisplay : p->pDisplay;
5182a1b… drh 1659 if( !bHist && p && p->pEditTail ) p = p->pEditTail;
5182a1b… drh 1660 }else{
5182a1b… drh 1661 /* Single and raw mode: terminate after displaying the selected post and
5182a1b… drh 1662 ** (optionally) its edits. */
5182a1b… drh 1663 break;
5182a1b… drh 1664 }
5182a1b… drh 1665 }
5182a1b… drh 1666
5182a1b… drh 1667 /* Undocumented "threadtable" query parameter causes thread table to be
5182a1b… drh 1668 ** displayed for debugging purposes. */
dbdf50d… drh 1669 if( PB("threadtable") ){
dbdf50d… drh 1670 @ <hr>
dbdf50d… drh 1671 @ <table border="1" cellpadding="3" cellspacing="0">
5182a1b… drh 1672 @ <tr><th>sid<th>rev<th>fpid<th>pIrt<th>pEditHead<th>pEditTail\
5182a1b… drh 1673 @ <th>pEditNext<th>pEditPrev<th>pDisplay<th>hash
dbdf50d… drh 1674 for(p=pThread->pFirst; p; p=p->pNext){
5182a1b… drh 1675 @ <tr><td>%d(p->sid)<td>%d(p->rev)<td>%d(p->fpid)\
5182a1b… drh 1676 @ <td>%d(p->pIrt ? p->pIrt->fpid : 0)\
5182a1b… drh 1677 @ <td>%d(p->pEditHead ? p->pEditHead->fpid : 0)\
5182a1b… drh 1678 @ <td>%d(p->pEditTail ? p->pEditTail->fpid : 0)\
5182a1b… drh 1679 @ <td>%d(p->pEditNext ? p->pEditNext->fpid : 0)\
5182a1b… drh 1680 @ <td>%d(p->pEditPrev ? p->pEditPrev->fpid : 0)\
5182a1b… drh 1681 @ <td>%d(p->pDisplay ? p->pDisplay->fpid : 0)\
dbdf50d… drh 1682 @ <td>%S(p->zUuid)</tr>
dbdf50d… drh 1683 }
dbdf50d… drh 1684 @ </table>
dbdf50d… drh 1685 }
dbdf50d… drh 1686
5182a1b… drh 1687 /* Clean up. */
5182a1b… drh 1688 forumthread_delete(pThread);
db86517… stephan 1689 }
db86517… stephan 1690
db86517… stephan 1691 /*
db86517… stephan 1692 ** Emit Forum Javascript which applies (or optionally can apply)
db86517… stephan 1693 ** to all forum-related pages. It does not include page-specific
db86517… stephan 1694 ** code (e.g. "forum.js").
db86517… stephan 1695 */
db86517… stephan 1696 static void forum_emit_js(void){
751dbe2… stephan 1697 builtin_fossil_js_bundle_or("copybutton", "pikchr", "confirmer",
ca90659… stephan 1698 "attach", "tabs", "storage",
ca90659… stephan 1699 "popupwidget", NULL);
9b2b6f5… stephan 1700 builtin_request_js("fossil.page.forumpost.js");
99fcc43… drh 1701 }
99fcc43… drh 1702
99fcc43… drh 1703 /*
99fcc43… drh 1704 ** WEBPAGE: forumpost
99fcc43… drh 1705 **
99fcc43… drh 1706 ** Show a single forum posting. The posting is shown in context with
5182a1b… drh 1707 ** its entire thread. The selected posting is enclosed within
99fcc43… drh 1708 ** <div class='forumSel'>...</div>. Javascript is used to move the
99fcc43… drh 1709 ** selected posting into view after the page loads.
99fcc43… drh 1710 **
99fcc43… drh 1711 ** Query parameters:
99fcc43… drh 1712 **
d04ad10… drh 1713 ** name=NAME Required. The NAME value is a hash prefix or timestamp
d04ad10… drh 1714 ** for the post to be displayed.
5182a1b… drh 1715 ** t=a Automatic display mode, i.e. hierarchical for
5182a1b… drh 1716 ** desktop and chronological for mobile. This is the
5182a1b… drh 1717 ** default if the "t" query parameter is omitted.
5182a1b… drh 1718 ** t=c Show posts in the order they were written.
2f78b2c… danield 1719 ** t=h Show posts using hierarchical indenting.
5182a1b… drh 1720 ** t=s Show only the post specified by "name=X".
d04ad10… drh 1721 ** t=r Alias for "t=c" with "unf" and "hist".
d04ad10… drh 1722 ** t=y Alias for "t=s" with "unf" and "hist".
d04ad10… drh 1723 ** raw Alias for "t=s" with "unf". Additionally, omit
d04ad10… drh 1724 ** the border around the post, and ignore "t" and "hist".
5182a1b… drh 1725 ** unf Show the original, unformatted source text.
5182a1b… drh 1726 ** hist Show edit history in addition to current posts.
99fcc43… drh 1727 */
99fcc43… drh 1728 void forumpost_page(void){
99fcc43… drh 1729 forumthread_page();
99fcc43… drh 1730 }
99fcc43… drh 1731
99fcc43… drh 1732 /*
99fcc43… drh 1733 ** WEBPAGE: forumthread
99fcc43… drh 1734 **
99fcc43… drh 1735 ** Show all forum messages associated with a particular message thread.
99fcc43… drh 1736 ** The result is basically the same as /forumpost except that none of
99fcc43… drh 1737 ** the postings in the thread are selected.
99fcc43… drh 1738 **
99fcc43… drh 1739 ** Query parameters:
99fcc43… drh 1740 **
d04ad10… drh 1741 ** name=NAME Required. The NAME value is a hash prefix or timestamp
d04ad10… drh 1742 ** for one of the posts in the thread.
5182a1b… drh 1743 ** t=a Automatic display mode, i.e. hierarchical for
5182a1b… drh 1744 ** desktop and chronological for mobile. This is the
5182a1b… drh 1745 ** default if the "t" query parameter is omitted.
5182a1b… drh 1746 ** t=c Show posts in the order they were written.
5182a1b… drh 1747 ** t=h Show posts using hierarchical indenting.
5182a1b… drh 1748 ** unf Show the original, unformatted source text.
5182a1b… drh 1749 ** hist Show edit history in addition to current posts.
99fcc43… drh 1750 */
99fcc43… drh 1751 void forumthread_page(void){
99fcc43… drh 1752 int fpid;
99fcc43… drh 1753 int froot;
5182a1b… drh 1754 char *zThreadTitle;
99fcc43… drh 1755 const char *zName = P("name");
c720327… drh 1756 const char *zMode = PD("t","a");
cc46524… drh 1757 int bRaw = PB("raw");
5182a1b… drh 1758 int bUnf = PB("unf");
5182a1b… drh 1759 int bHist = PB("hist");
a872067… drh 1760 int mode = 0;
168eb71… drh 1761 int autoMode = 0;
99fcc43… drh 1762 login_check_credentials();
99fcc43… drh 1763 if( !g.perm.RdForum ){
99fcc43… drh 1764 login_needed(g.anon.RdForum);
99fcc43… drh 1765 return;
99fcc43… drh 1766 }
99fcc43… drh 1767 if( zName==0 ){
99fcc43… drh 1768 webpage_error("Missing \"name=\" query parameter");
61998ec… drh 1769 return;
99fcc43… drh 1770 }
57f1e87… drh 1771 cgi_check_for_malice();
99fcc43… drh 1772 fpid = symbolic_name_to_rid(zName, "f");
99fcc43… drh 1773 if( fpid<=0 ){
93a5d65… drh 1774 if( fpid==0 ){
93a5d65… drh 1775 webpage_notfound_error("Unknown forum id: \"%s\"", zName);
93a5d65… drh 1776 }else{
93a5d65… drh 1777 ambiguous_page();
93a5d65… drh 1778 }
93a5d65… drh 1779 return;
99fcc43… drh 1780 }
99fcc43… drh 1781 froot = db_int(0, "SELECT froot FROM forumpost WHERE fpid=%d", fpid);
99fcc43… drh 1782 if( froot==0 ){
93a5d65… drh 1783 webpage_notfound_error("Not a forum post: \"%s\"", zName);
61998ec… drh 1784 return;
99fcc43… drh 1785 }
5182a1b… drh 1786
5182a1b… drh 1787 /* Decode the mode parameters. */
5182a1b… drh 1788 if( bRaw ){
5182a1b… drh 1789 mode = FD_RAW;
5182a1b… drh 1790 bUnf = 1;
5182a1b… drh 1791 bHist = 0;
5182a1b… drh 1792 cgi_replace_query_parameter("unf", "on");
5182a1b… drh 1793 cgi_delete_query_parameter("hist");
5182a1b… drh 1794 cgi_delete_query_parameter("raw");
5182a1b… drh 1795 }else{
5182a1b… drh 1796 switch( *zMode ){
168eb71… drh 1797 case 'a': mode = cgi_from_mobile() ? FD_CHRONO : FD_HIER;
168eb71… drh 1798 autoMode=1; break;
5182a1b… drh 1799 case 'c': mode = FD_CHRONO; break;
5182a1b… drh 1800 case 'h': mode = FD_HIER; break;
5182a1b… drh 1801 case 's': mode = FD_SINGLE; break;
5182a1b… drh 1802 case 'r': mode = FD_CHRONO; break;
5182a1b… drh 1803 case 'y': mode = FD_SINGLE; break;
5182a1b… drh 1804 default: webpage_error("Invalid thread mode: \"%s\"", zMode);
5182a1b… drh 1805 }
5182a1b… drh 1806 if( *zMode=='r' || *zMode=='y') {
5182a1b… drh 1807 bUnf = 1;
5182a1b… drh 1808 bHist = 1;
5182a1b… drh 1809 cgi_replace_query_parameter("t", mode==FD_CHRONO ? "c" : "s");
5182a1b… drh 1810 cgi_replace_query_parameter("unf", "on");
5182a1b… drh 1811 cgi_replace_query_parameter("hist", "on");
5182a1b… drh 1812 }
5182a1b… drh 1813 }
5182a1b… drh 1814
5182a1b… drh 1815 /* Define the page header. */
5182a1b… drh 1816 zThreadTitle = db_text("",
5182a1b… drh 1817 "SELECT"
5182a1b… drh 1818 " substr(event.comment,instr(event.comment,':')+2)"
5182a1b… drh 1819 " FROM forumpost, event"
5182a1b… drh 1820 " WHERE event.objid=forumpost.fpid"
5182a1b… drh 1821 " AND forumpost.fpid=%d;",
5182a1b… drh 1822 fpid
5182a1b… drh 1823 );
112c713… drh 1824 style_set_current_feature("forum");
5182a1b… drh 1825 style_header("%s%s", zThreadTitle, *zThreadTitle ? "" : "Forum");
5182a1b… drh 1826 fossil_free(zThreadTitle);
5182a1b… drh 1827 if( mode!=FD_CHRONO ){
5182a1b… drh 1828 style_submenu_element("Chronological", "%R/%s/%s?t=c%s%s", g.zPath, zName,
5182a1b… drh 1829 bUnf ? "&unf" : "", bHist ? "&hist" : "");
5182a1b… drh 1830 }
5182a1b… drh 1831 if( mode!=FD_HIER ){
5182a1b… drh 1832 style_submenu_element("Hierarchical", "%R/%s/%s?t=h%s%s", g.zPath, zName,
5182a1b… drh 1833 bUnf ? "&unf" : "", bHist ? "&hist" : "");
5182a1b… drh 1834 }
5182a1b… drh 1835 style_submenu_checkbox("unf", "Unformatted", 0, 0);
5182a1b… drh 1836 style_submenu_checkbox("hist", "History", 0, 0);
0d2ceb8… drh 1837 style_submenu_element("Timeline", "%R/timeline?thread=%t", zName);
83928d8… drh 1838 if( g.perm.Admin ){
83928d8… drh 1839 style_submenu_element("Artifacts", "%R/forumthreadhashlist/%t", zName);
83928d8… drh 1840 }
5182a1b… drh 1841
5182a1b… drh 1842 /* Display the thread. */
ffd32a9… george 1843 if( fossil_strcmp(g.zPath,"forumthread")==0 ) fpid = 0;
168eb71… drh 1844 forum_display_thread(froot, fpid, mode, autoMode, bUnf, bHist);
5182a1b… drh 1845
5182a1b… drh 1846 /* Emit Forum Javascript. */
5182a1b… drh 1847 builtin_request_js("forum.js");
db86517… stephan 1848 forum_emit_js();
5182a1b… drh 1849
5182a1b… drh 1850 /* Emit the page style. */
112c713… drh 1851 style_finish_page();
99fcc43… drh 1852 }
99fcc43… drh 1853
99fcc43… drh 1854 /*
99fcc43… drh 1855 ** Return true if a forum post should be moderated.
99fcc43… drh 1856 */
5079ffb… stephan 1857 int forum_need_moderation(void){
99fcc43… drh 1858 if( P("domod") ) return 1;
99fcc43… drh 1859 if( g.perm.WrTForum ) return 0;
99fcc43… drh 1860 if( g.perm.ModForum ) return 0;
99fcc43… drh 1861 return 1;
d2c81b9… drh 1862 }
d2c81b9… drh 1863
3f6aa94… stephan 1864 /* Flags for use with forum_post() */
3f6aa94… stephan 1865 #define FPOST_NO_ALERT 1 /* do not send any alerts */
ca90659… stephan 1866 #define FPOST_DRYRUN 2 /* do not save the artifact */
3f6aa94… stephan 1867
d2c81b9… drh 1868 /*
3f6aa94… stephan 1869 ** Return a flags value for use with the final argument to
3f6aa94… stephan 1870 ** forum_post(), extracted from the CGI environment.
d2c81b9… drh 1871 */
3f6aa94… stephan 1872 static int forum_post_flags(void){
3f6aa94… stephan 1873 int iPostFlags = 0;
3f6aa94… stephan 1874 if( g.perm.Debug && P("fpsilent")!=0 ){
3f6aa94… stephan 1875 iPostFlags |= FPOST_NO_ALERT;
ca90659… stephan 1876 }
ca90659… stephan 1877 if( P("dryrun")!=0 ){
ca90659… stephan 1878 iPostFlags |= FPOST_DRYRUN;
3f6aa94… stephan 1879 }
3f6aa94… stephan 1880 return iPostFlags;
99fcc43… drh 1881 }
99fcc43… drh 1882
99fcc43… drh 1883 /*
99fcc43… drh 1884 ** Add a new Forum Post artifact to the repository.
99fcc43… drh 1885 **
99fcc43… drh 1886 ** Return true if a redirect occurs.
99fcc43… drh 1887 */
99fcc43… drh 1888 static int forum_post(
99fcc43… drh 1889 const char *zTitle, /* Title. NULL for replies */
99fcc43… drh 1890 int iInReplyTo, /* Post replying to. 0 for new threads */
99fcc43… drh 1891 int iEdit, /* Post being edited, or zero for a new post */
99fcc43… drh 1892 const char *zUser, /* Username. NULL means use login name */
99fcc43… drh 1893 const char *zMimetype, /* Mimetype of content. */
3f6aa94… stephan 1894 const char *zContent, /* Content */
3f6aa94… stephan 1895 int iFlags /* FPOST_xyz flag values */
99fcc43… drh 1896 ){
99fcc43… drh 1897 char *zDate;
99fcc43… drh 1898 char *zI;
99fcc43… drh 1899 char *zG;
99fcc43… drh 1900 int iBasis;
99fcc43… drh 1901 Blob x, cksum, formatCheck, errMsg;
99fcc43… drh 1902 Manifest *pPost;
d2c81b9… drh 1903 int nContent = zContent ? (int)strlen(zContent) : 0;
99fcc43… drh 1904
99fcc43… drh 1905 schema_forum();
673dc38… stephan 1906 if( !g.perm.Admin && (iEdit || iInReplyTo)
2d7e2cb… stephan 1907 && forum_rid_is_tagged(iEdit ? iEdit : iInReplyTo, "closed", 1) ){
673dc38… stephan 1908 forumpost_error_closed();
673dc38… stephan 1909 return 0;
673dc38… stephan 1910 }
ca90659… stephan 1911 if( iEdit==0 && fossil_all_whitespace(zContent) ){
d2c81b9… drh 1912 return 0;
d2c81b9… drh 1913 }
99fcc43… drh 1914 if( iInReplyTo==0 && iEdit>0 ){
99fcc43… drh 1915 iBasis = iEdit;
99fcc43… drh 1916 iInReplyTo = db_int(0, "SELECT firt FROM forumpost WHERE fpid=%d", iEdit);
99fcc43… drh 1917 }else{
99fcc43… drh 1918 iBasis = iInReplyTo;
99fcc43… drh 1919 }
99fcc43… drh 1920 webpage_assert( (zTitle==0)+(iInReplyTo==0)==1 );
99fcc43… drh 1921 blob_init(&x, 0, 0);
99fcc43… drh 1922 zDate = date_in_standard_format("now");
99fcc43… drh 1923 blob_appendf(&x, "D %s\n", zDate);
99fcc43… drh 1924 fossil_free(zDate);
5182a1b… drh 1925 zG = db_text(0,
99fcc43… drh 1926 "SELECT uuid FROM blob, forumpost"
99fcc43… drh 1927 " WHERE blob.rid==forumpost.froot"
99fcc43… drh 1928 " AND forumpost.fpid=%d", iBasis);
99fcc43… drh 1929 if( zG ){
99fcc43… drh 1930 blob_appendf(&x, "G %s\n", zG);
99fcc43… drh 1931 fossil_free(zG);
99fcc43… drh 1932 }
99fcc43… drh 1933 if( zTitle ){
99fcc43… drh 1934 blob_appendf(&x, "H %F\n", zTitle);
99fcc43… drh 1935 }
ca90659… stephan 1936 zI = rid_to_uuid(iInReplyTo);
99fcc43… drh 1937 if( zI ){
99fcc43… drh 1938 blob_appendf(&x, "I %s\n", zI);
99fcc43… drh 1939 fossil_free(zI);
99fcc43… drh 1940 }
ca90659… stephan 1941 if( zMimetype!=0
ca90659… stephan 1942 && zMimetype[0]!=0
ca90659… stephan 1943 && fossil_strcmp(zMimetype,"text/x-fossil-wiki")!=0 ){
ca90659… stephan 1944 blob_appendf(&x, "N %F\n", zMimetype);
99fcc43… drh 1945 }
99fcc43… drh 1946 if( iEdit>0 ){
99fcc43… drh 1947 char *zP = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", iEdit);
99fcc43… drh 1948 if( zP==0 ) webpage_error("missing edit artifact %d", iEdit);
99fcc43… drh 1949 blob_appendf(&x, "P %s\n", zP);
99fcc43… drh 1950 fossil_free(zP);
99fcc43… drh 1951 }
99fcc43… drh 1952 if( zUser==0 ){
99fcc43… drh 1953 if( login_is_nobody() ){
99fcc43… drh 1954 zUser = "anonymous";
99fcc43… drh 1955 }else{
99fcc43… drh 1956 zUser = login_name();
99fcc43… drh 1957 }
99fcc43… drh 1958 }
99fcc43… drh 1959 blob_appendf(&x, "U %F\n", zUser);
d2c81b9… drh 1960 blob_appendf(&x, "W %d\n%s\n", nContent, zContent);
99fcc43… drh 1961 md5sum_blob(&x, &cksum);
99fcc43… drh 1962 blob_appendf(&x, "Z %b\n", &cksum);
99fcc43… drh 1963 blob_reset(&cksum);
99fcc43… drh 1964
99fcc43… drh 1965 /* Verify that the artifact we are creating is well-formed */
99fcc43… drh 1966 blob_init(&formatCheck, 0, 0);
99fcc43… drh 1967 blob_init(&errMsg, 0, 0);
99fcc43… drh 1968 blob_copy(&formatCheck, &x);
99fcc43… drh 1969 pPost = manifest_parse(&formatCheck, 0, &errMsg);
99fcc43… drh 1970 if( pPost==0 ){
99fcc43… drh 1971 webpage_error("malformed forum post artifact - %s", blob_str(&errMsg));
99fcc43… drh 1972 }
99fcc43… drh 1973 webpage_assert( pPost->type==CFTYPE_FORUM );
99fcc43… drh 1974 manifest_destroy(pPost);
99fcc43… drh 1975
ca90659… stephan 1976 if( (iFlags & FPOST_DRYRUN)!=0 ){
99fcc43… drh 1977 @ <div class='debug'>
99fcc43… drh 1978 @ This is the artifact that would have been generated:
99fcc43… drh 1979 @ <pre>%h(blob_str(&x))</pre>
99fcc43… drh 1980 @ </div>
99fcc43… drh 1981 blob_reset(&x);
99fcc43… drh 1982 return 0;
99fcc43… drh 1983 }else{
3f6aa94… stephan 1984 int nrid;
3f6aa94… stephan 1985 db_begin_transaction();
3f6aa94… stephan 1986 nrid = wiki_put(&x, iEdit>0 ? iEdit : 0, forum_need_moderation());
5b7888c… stephan 1987 blob_reset(&x);
3f6aa94… stephan 1988 if( (iFlags & FPOST_NO_ALERT)!=0 ){
3f6aa94… stephan 1989 alert_unqueue('f', nrid);
3f6aa94… stephan 1990 }
3f6aa94… stephan 1991 db_commit_transaction();
99fcc43… drh 1992 cgi_redirectf("%R/forumpost/%S", rid_to_uuid(nrid));
99fcc43… drh 1993 return 1;
99fcc43… drh 1994 }
99fcc43… drh 1995 }
99fcc43… drh 1996
99fcc43… drh 1997 /*
99fcc43… drh 1998 ** Paint the form elements for entering a Forum post
99fcc43… drh 1999 */
5182a1b… drh 2000 static void forum_post_widget(
99fcc43… drh 2001 const char *zTitle,
99fcc43… drh 2002 const char *zMimetype,
99fcc43… drh 2003 const char *zContent
99fcc43… drh 2004 ){
99fcc43… drh 2005 if( zTitle ){
8361306… drh 2006 int mxTitle = db_get_int("forum-max-title", 125);
81fb5e7… ashepilko 2007 @ Title: <input type="input" name="title" value="%h(zTitle)" size="50"
8361306… drh 2008 if( mxTitle>0 ){
8361306… drh 2009 @ maxlength="%d(mxTitle)"><br>
8361306… drh 2010 }else{
8361306… drh 2011 @ ><br>
8361306… drh 2012 }
99fcc43… drh 2013 }
5f328d9… drh 2014 @ %z(href("%R/markup_help"))Markup style</a>:
fcf17b2… drh 2015 mimetype_option_menu(zMimetype, "mimetype");
268bb73… stephan 2016 @ <div class="forum-editor-widget">
268bb73… stephan 2017 @ <textarea aria-label="Content:" name="content" class="wikiedit" \
268bb73… stephan 2018 @ cols="80" rows="25" wrap="virtual">%h(zContent)</textarea></div>
268bb73… stephan 2019 }
268bb73… stephan 2020
268bb73… stephan 2021 /*
4c4c28d… drh 2022 ** If PD("fpid") refers to a forum post, its rid is returned, else
4c4c28d… drh 2023 ** this function emits an error does not does return.
4c4c28d… drh 2024 */
4c4c28d… drh 2025 static int forum_validate_fpid_param(void){
4c4c28d… drh 2026 const char *zFpid = PD("fpid","");
4c4c28d… drh 2027 int fpid = symbolic_name_to_rid(zFpid, "f");
4c4c28d… drh 2028 if( fpid<=0 ){
4c4c28d… drh 2029 webpage_error("Missing or invalid fpid parameter.");
4c4c28d… drh 2030 }
4c4c28d… drh 2031 return fpid;
4c4c28d… drh 2032 }
4c4c28d… drh 2033
4c4c28d… drh 2034 /*
4c4c28d… drh 2035 ** Internal helper for /forumpost_XYZ internal pages which tag/untag
4c4c28d… drh 2036 ** posts.
4c4c28d… drh 2037 */
4c4c28d… drh 2038 static void forumpost_action_helper(const char *zTag, const char *zVal,
4c4c28d… drh 2039 int addTag, int validFpid){
4c4c28d… drh 2040 if( !cgi_csrf_safe(2) ){
4c4c28d… drh 2041 webpage_error("CSRF validation failed");
4c4c28d… drh 2042 }else{
4c4c28d… drh 2043 const int fpid = validFpid>0 ? validFpid : forum_validate_fpid_param();
ca90659… stephan 2044 if( fpid>0 ){
ca90659… stephan 2045 if( forumpost_tag(fpid, addTag, zTag, zVal) < 0 ){
ca90659… stephan 2046 webpage_error("Tagging artifact failed: %s", g.zErrMsg);
ca90659… stephan 2047 }else{
ca90659… stephan 2048 cgi_redirectf("%R/forumpost/%S",P("fpid"));
ca90659… stephan 2049 }
ca90659… stephan 2050 }
4c4c28d… drh 2051 }
4c4c28d… drh 2052 }
4c4c28d… drh 2053
4c4c28d… drh 2054 /*
673dc38… stephan 2055 ** WEBPAGE: forumpost_close hidden
673dc38… stephan 2056 ** WEBPAGE: forumpost_reopen hidden
673dc38… stephan 2057 **
673dc38… stephan 2058 ** fpid=X Hash of the post to be edited. REQUIRED
673dc38… stephan 2059 **
673dc38… stephan 2060 ** Closes or re-opens the given forum post, within the bounds of the
4d58d6e… stephan 2061 ** API for forumpost_tag(). After (perhaps) modifying the "closed"
673dc38… stephan 2062 ** status of the given thread, it redirects to that post's thread
673dc38… stephan 2063 ** view. Requires admin privileges.
673dc38… stephan 2064 */
673dc38… stephan 2065 void forum_page_close(void){
673dc38… stephan 2066 login_check_credentials();
673dc38… stephan 2067 if( forumpost_may_close()==0 ){
673dc38… stephan 2068 login_needed(g.anon.Admin);
4c4c28d… drh 2069 }else{
4c4c28d… drh 2070 const int bIsAdd = sqlite3_strglob("*_close*", g.zPath)==0;
4c4c28d… drh 2071 forumpost_action_helper("closed", 0, bIsAdd, 0);
4c4c28d… drh 2072 }
4c4c28d… drh 2073 }
4c4c28d… drh 2074
4c4c28d… drh 2075 /*
4c4c28d… drh 2076 ** WEBPAGE: forumpost_status hidden
4c4c28d… drh 2077 **
4c4c28d… drh 2078 ** fpid=X Hash of the post to be edited. REQUIRED
4c4c28d… drh 2079 ** status=Y New status value. REQUIRED
4c4c28d… drh 2080 **
4c4c28d… drh 2081 ** Updates the current status=Y tag on the first version of
4c4c28d… drh 2082 ** the forum post X. Requires forum_may_set_status() permissions.
4c4c28d… drh 2083 */
4c4c28d… drh 2084 void forum_page_status(void){
4c4c28d… drh 2085 int fpid;
4c4c28d… drh 2086 login_check_credentials();
4c4c28d… drh 2087 fpid = forum_validate_fpid_param();
4c4c28d… drh 2088 if(forum_may_set_status(fpid)){
4c4c28d… drh 2089 const char *zStatus = PD("status",0);
4c4c28d… drh 2090 if( !zStatus || !zStatus[0] ){
4c4c28d… drh 2091 webpage_error("Missing required status.");
4c4c28d… drh 2092 }
4c4c28d… drh 2093 forumpost_action_helper("status", zStatus, 1, fpid);
4c4c28d… drh 2094 }else{
4c4c28d… drh 2095 webpage_error("You lack permissions to change this post's status.");
4c4c28d… drh 2096 }
99fcc43… drh 2097 }
99fcc43… drh 2098
99fcc43… drh 2099 /*
99fcc43… drh 2100 ** WEBPAGE: forumnew
99fcc43… drh 2101 ** WEBPAGE: forumedit
99fcc43… drh 2102 **
99fcc43… drh 2103 ** Start a new thread on the forum or reply to an existing thread.
99fcc43… drh 2104 ** But first prompt to see if the user would like to log in.
99fcc43… drh 2105 */
99fcc43… drh 2106 void forum_page_init(void){
99fcc43… drh 2107 int isEdit;
99fcc43… drh 2108 char *zGoto;
673dc38… stephan 2109
99fcc43… drh 2110 login_check_credentials();
99fcc43… drh 2111 if( !g.perm.WrForum ){
99fcc43… drh 2112 login_needed(g.anon.WrForum);
99fcc43… drh 2113 return;
99fcc43… drh 2114 }
99fcc43… drh 2115 if( sqlite3_strglob("*edit*", g.zPath)==0 ){
c717d28… drh 2116 zGoto = mprintf("forume2?fpid=%S",PD("fpid",""));
99fcc43… drh 2117 isEdit = 1;
99fcc43… drh 2118 }else{
c717d28… drh 2119 zGoto = mprintf("forume1");
99fcc43… drh 2120 isEdit = 0;
99fcc43… drh 2121 }
99fcc43… drh 2122 if( login_is_individual() ){
99fcc43… drh 2123 if( isEdit ){
99fcc43… drh 2124 forumedit_page();
99fcc43… drh 2125 }else{
99fcc43… drh 2126 forumnew_page();
99fcc43… drh 2127 }
99fcc43… drh 2128 return;
99fcc43… drh 2129 }
112c713… drh 2130 style_set_current_feature("forum");
99fcc43… drh 2131 style_header("%h As Anonymous?", isEdit ? "Reply" : "Post");
99fcc43… drh 2132 @ <p>You are not logged in.
99fcc43… drh 2133 @ <p><table border="0" cellpadding="10">
99fcc43… drh 2134 @ <tr><td>
99fcc43… drh 2135 @ <form action="%s(zGoto)" method="POST">
99fcc43… drh 2136 @ <input type="submit" value="Remain Anonymous">
99fcc43… drh 2137 @ </form>
99fcc43… drh 2138 @ <td>Post to the forum anonymously
99fcc43… drh 2139 if( login_self_register_available(0) ){
99fcc43… drh 2140 @ <tr><td>
99fcc43… drh 2141 @ <form action="%R/register" method="POST">
99fcc43… drh 2142 @ <input type="hidden" name="g" value="%s(zGoto)">
99fcc43… drh 2143 @ <input type="submit" value="Create An Account">
99fcc43… drh 2144 @ </form>
99fcc43… drh 2145 @ <td>Create a new account and post using that new account
99fcc43… drh 2146 }
99fcc43… drh 2147 @ <tr><td>
99fcc43… drh 2148 @ <form action="%R/login" method="POST">
99fcc43… drh 2149 @ <input type="hidden" name="g" value="%s(zGoto)">
99fcc43… drh 2150 @ <input type="hidden" name="noanon" value="1">
99fcc43… drh 2151 @ <input type="submit" value="Login">
99fcc43… drh 2152 @ </form>
99fcc43… drh 2153 @ <td>Log into an existing account
99fcc43… drh 2154 @ </table>
db86517… stephan 2155 forum_emit_js();
112c713… drh 2156 style_finish_page();
99fcc43… drh 2157 fossil_free(zGoto);
99fcc43… drh 2158 }
99fcc43… drh 2159
99fcc43… drh 2160 /*
99fcc43… drh 2161 ** Write the "From: USER" line on the webpage.
99fcc43… drh 2162 */
99fcc43… drh 2163 static void forum_from_line(void){
99fcc43… drh 2164 if( login_is_nobody() ){
99fcc43… drh 2165 @ From: anonymous<br>
99fcc43… drh 2166 }else{
99fcc43… drh 2167 @ From: %h(login_name())<br>
99fcc43… drh 2168 }
99fcc43… drh 2169 }
99fcc43… drh 2170
3f6aa94… stephan 2171 static void forum_render_debug_options(void){
99fcc43… drh 2172 if( g.perm.Debug ){
0475b4f… wyoung 2173 /* Give extra control over the post to users with the special
0475b4f… wyoung 2174 * Debug capability, which includes Admin and Setup users */
99fcc43… drh 2175 @ <div class="debug">
99fcc43… drh 2176 @ <label><input type="checkbox" name="dryrun" %s(PCK("dryrun"))> \
99fcc43… drh 2177 @ Dry run</label>
99fcc43… drh 2178 @ <br><label><input type="checkbox" name="domod" %s(PCK("domod"))> \
99fcc43… drh 2179 @ Require moderator approval</label>
99fcc43… drh 2180 @ <br><label><input type="checkbox" name="showqp" %s(PCK("showqp"))> \
99fcc43… drh 2181 @ Show query parameters</label>
3f6aa94… stephan 2182 @ <br><label><input type="checkbox" name="fpsilent" %s(PCK("fpsilent"))> \
1b71969… wyoung 2183 @ Do not send notification emails</label>
99fcc43… drh 2184 @ </div>
99fcc43… drh 2185 }
3f6aa94… stephan 2186 }
3f6aa94… stephan 2187
3f6aa94… stephan 2188 /*
5079ffb… stephan 2189 ** If the user has AttachForum permissions, emit a notice that
8ada7fa… stephan 2190 ** attachments may be added after saving. If p is not NULL, also emit
8ada7fa… stephan 2191 ** its list of attachments. We only emit this for No-JS environments,
8ada7fa… stephan 2192 ** as in JS environments the interactive forum editor includes
8ada7fa… stephan 2193 ** attachment support.
5079ffb… stephan 2194 */
5079ffb… stephan 2195 static void forum_render_attachment_notice(void){
5079ffb… stephan 2196 if( g.perm.AttachForum ){
8ada7fa… stephan 2197 @ <noscript><div>You will be able to attach files to this post
8ada7fa… stephan 2198 @ after saving it.</div></noscript>
5079ffb… stephan 2199 }
5079ffb… stephan 2200 }
5079ffb… stephan 2201
5079ffb… stephan 2202 /*
ca90659… stephan 2203 ** WEBPAGE: forume1 hidden
3f6aa94… stephan 2204 **
3f6aa94… stephan 2205 ** Start a new forum thread.
3f6aa94… stephan 2206 */
3f6aa94… stephan 2207 void forumnew_page(void){
3f6aa94… stephan 2208 const char *zTitle = PDT("title","");
3f6aa94… stephan 2209 const char *zMimetype = PD("mimetype",DEFAULT_FORUM_MIMETYPE);
3f6aa94… stephan 2210 const char *zContent = PDT("content","");
3f6aa94… stephan 2211
3f6aa94… stephan 2212 login_check_credentials();
3f6aa94… stephan 2213 if( !g.perm.WrForum ){
3f6aa94… stephan 2214 login_needed(g.anon.WrForum);
3f6aa94… stephan 2215 return;
3f6aa94… stephan 2216 }
920ace1… drh 2217 if( P("submit") && cgi_csrf_safe(2) ){
3f6aa94… stephan 2218 if( forum_post(zTitle, 0, 0, 0, zMimetype, zContent,
3f6aa94… stephan 2219 forum_post_flags()) ) return;
3f6aa94… stephan 2220 }
ca90659… stephan 2221 if( P("preview") && !fossil_all_whitespace(zContent) ){
3f6aa94… stephan 2222 @ <h1>Preview:</h1>
3f6aa94… stephan 2223 forum_render(zTitle, zMimetype, zContent, "forumEdit", 1);
3f6aa94… stephan 2224 }
3f6aa94… stephan 2225 style_set_current_feature("forum");
3f6aa94… stephan 2226 style_header("New Forum Thread");
ca90659… stephan 2227
ca90659… stephan 2228 @ <form action="%R/forume1" method="POST" \
ca90659… stephan 2229 @ class="remove-if-replaced">
3f6aa94… stephan 2230 @ <h1>New Thread:</h1>
3f6aa94… stephan 2231 forum_from_line();
3f6aa94… stephan 2232 forum_post_widget(zTitle, zMimetype, zContent);
3f6aa94… stephan 2233 @ <input type="submit" name="preview" value="Preview">
ca90659… stephan 2234 if( P("preview") && !fossil_all_whitespace(zContent) ){
3f6aa94… stephan 2235 @ <input type="submit" name="submit" value="Submit">
3f6aa94… stephan 2236 }else{
3f6aa94… stephan 2237 @ <input type="submit" name="submit" value="Submit" disabled>
3f6aa94… stephan 2238 }
3f6aa94… stephan 2239 forum_render_debug_options();
920ace1… drh 2240 login_insert_csrf_secret();
99fcc43… drh 2241 @ </form>
ca90659… stephan 2242 /* When JS is disabled the block above will work. When it's
ca90659… stephan 2243 enabled, the above will be removed and JS will render the editor
ca90659… stephan 2244 form in the next element. */
ca90659… stephan 2245 @ <div hidden id='forumnew-placeholder'>
ca90659… stephan 2246 @ <input type='hidden' name='title' value='%h(zTitle)'>
ca90659… stephan 2247 login_insert_csrf_secret();
ca90659… stephan 2248 @ </div>
5079ffb… stephan 2249 forum_render_attachment_notice();
db86517… stephan 2250 forum_emit_js();
112c713… drh 2251 style_finish_page();
99fcc43… drh 2252 }
99fcc43… drh 2253
99fcc43… drh 2254 /*
ca90659… stephan 2255 ** WEBPAGE: forume2 hidden
99fcc43… drh 2256 **
99fcc43… drh 2257 ** Edit an existing forum message.
99fcc43… drh 2258 ** Query parameters:
99fcc43… drh 2259 **
91ff34d… drh 2260 ** fpid=X Hash of the post to be edited. REQUIRED
99fcc43… drh 2261 */
99fcc43… drh 2262 void forumedit_page(void){
99fcc43… drh 2263 int fpid;
c6dfb55… drh 2264 int froot;
af39da6… andybradford 2265 Manifest *pPost = 0;
c6dfb55… drh 2266 Manifest *pRootPost = 0;
99fcc43… drh 2267 const char *zMimetype = 0;
99fcc43… drh 2268 const char *zContent = 0;
99fcc43… drh 2269 const char *zTitle = 0;
c6dfb55… drh 2270 char *zDate = 0;
62f0ce8… stephan 2271 const char *zFpid = PD("fpid","");
99fcc43… drh 2272 int isCsrfSafe;
99fcc43… drh 2273 int isDelete = 0;
673dc38… stephan 2274 int iClosed = 0;
19333c8… stephan 2275 int bSameUser; /* True if author is also the reader */
673dc38… stephan 2276 int bPreview; /* True in preview mode. */
19333c8… stephan 2277 int bPrivate; /* True if post is private (not yet moderated) */
c2b38f0… stephan 2278 int bReply; /* True if replying to a post */
99fcc43… drh 2279
99fcc43… drh 2280 login_check_credentials();
99fcc43… drh 2281 if( !g.perm.WrForum ){
99fcc43… drh 2282 login_needed(g.anon.WrForum);
99fcc43… drh 2283 return;
99fcc43… drh 2284 }
62f0ce8… stephan 2285 fpid = symbolic_name_to_rid(zFpid, "f");
99fcc43… drh 2286 if( fpid<=0 || (pPost = manifest_get(fpid, CFTYPE_FORUM, 0))==0 ){
99fcc43… drh 2287 webpage_error("Missing or invalid fpid query parameter");
99fcc43… drh 2288 }
c6dfb55… drh 2289 froot = db_int(0, "SELECT froot FROM forumpost WHERE fpid=%d", fpid);
4c4c28d… drh 2290 if( (froot==0 || (pRootPost = manifest_get(froot, CFTYPE_FORUM, 0))==0)
4c4c28d… drh 2291 && P("reject")==0
4c4c28d… drh 2292 ){
c6dfb55… drh 2293 webpage_error("fpid does not appear to be a forum post: \"%d\"", fpid);
c6dfb55… drh 2294 }
99fcc43… drh 2295 if( P("cancel") ){
673dc38… stephan 2296 cgi_redirectf("%R/forumpost/%S",zFpid);
99fcc43… drh 2297 return;
99fcc43… drh 2298 }
673dc38… stephan 2299 bPreview = P("preview")!=0;
c2b38f0… stephan 2300 bReply = P("reply")!=0;
2d7e2cb… stephan 2301 iClosed = forum_rid_is_tagged(fpid, "closed", 1);
920ace1… drh 2302 isCsrfSafe = cgi_csrf_safe(2);
19333c8… stephan 2303 bPrivate = content_is_private(fpid);
19333c8… stephan 2304 bSameUser = login_is_individual()
19333c8… stephan 2305 && fossil_strcmp(pPost->zUser, g.zLogin)==0;
19333c8… stephan 2306 if( isCsrfSafe && (g.perm.ModForum || (bPrivate && bSameUser)) ){
19333c8… stephan 2307 if( g.perm.ModForum && P("approve") ){
d33d446… drh 2308 const char *zUserToTrust;
5079ffb… stephan 2309 moderation_forumpost_approve(fpid);
d33d446… drh 2310 if( g.perm.AdminForum
d33d446… drh 2311 && PB("trust")
d33d446… drh 2312 && (zUserToTrust = P("trustuser"))!=0
d33d446… drh 2313 ){
f741baa… drh 2314 db_unprotect(PROTECT_USER);
d33d446… drh 2315 db_multi_exec("UPDATE user SET cap=cap||'4' "
d33d446… drh 2316 "WHERE login=%Q AND cap NOT GLOB '*4*'",
d33d446… drh 2317 zUserToTrust);
f741baa… drh 2318 db_protect_pop();
d33d446… drh 2319 }
99fcc43… drh 2320 cgi_redirectf("%R/forumpost/%S",P("fpid"));
99fcc43… drh 2321 return;
99fcc43… drh 2322 }
99fcc43… drh 2323 if( P("reject") ){
5182a1b… drh 2324 char *zParent =
99fcc43… drh 2325 db_text(0,
99fcc43… drh 2326 "SELECT uuid FROM forumpost, blob"
99fcc43… drh 2327 " WHERE forumpost.fpid=%d AND blob.rid=forumpost.firt",
99fcc43… drh 2328 fpid
99fcc43… drh 2329 );
5079ffb… stephan 2330 moderation_forumpost_disapprove(fpid);
99fcc43… drh 2331 if( zParent ){
99fcc43… drh 2332 cgi_redirectf("%R/forumpost/%S",zParent);
99fcc43… drh 2333 }else{
99fcc43… drh 2334 cgi_redirectf("%R/forum");
99fcc43… drh 2335 }
99fcc43… drh 2336 return;
99fcc43… drh 2337 }
99fcc43… drh 2338 }
112c713… drh 2339 style_set_current_feature("forum");
99fcc43… drh 2340 isDelete = P("nullout")!=0;
3684c58… drh 2341 if( P("submit")
3684c58… drh 2342 && isCsrfSafe
3684c58… drh 2343 && (zContent = PDT("content",""))!=0
ca90659… stephan 2344 && (isDelete || !fossil_all_whitespace(zContent))
3684c58… drh 2345 ){
99fcc43… drh 2346 int done = 1;
5f3cb27… drh 2347 const char *zMimetype = PD("mimetype",DEFAULT_FORUM_MIMETYPE);
c2b38f0… stephan 2348 if( bReply ){
3f6aa94… stephan 2349 done = forum_post(0, fpid, 0, 0, zMimetype, zContent,
3f6aa94… stephan 2350 forum_post_flags());
99fcc43… drh 2351 }else if( P("edit") || isDelete ){
3f6aa94… stephan 2352 done = forum_post(P("title"), 0, fpid, 0, zMimetype, zContent,
3f6aa94… stephan 2353 forum_post_flags());
99fcc43… drh 2354 }else{
99fcc43… drh 2355 webpage_error("Missing 'reply' query parameter");
99fcc43… drh 2356 }
99fcc43… drh 2357 if( done ) return;
99fcc43… drh 2358 }
99fcc43… drh 2359 if( isDelete ){
99fcc43… drh 2360 zMimetype = "text/x-fossil-wiki";
99fcc43… drh 2361 zContent = "";
99fcc43… drh 2362 if( pPost->zThreadTitle ) zTitle = "";
99fcc43… drh 2363 style_header("Delete %s", zTitle ? "Post" : "Reply");
99fcc43… drh 2364 @ <h1>Original Post:</h1>
99fcc43… drh 2365 forum_render(pPost->zThreadTitle, pPost->zMimetype, pPost->zWiki,
46dd696… drh 2366 "forumEdit", 1);
99fcc43… drh 2367 @ <h1>Change Into:</h1>
46dd696… drh 2368 forum_render(zTitle, zMimetype, zContent,"forumEdit", 1);
99fcc43… drh 2369 @ <form action="%R/forume2" method="POST">
920ace1… drh 2370 login_insert_csrf_secret();
99fcc43… drh 2371 @ <input type="hidden" name="fpid" value="%h(P("fpid"))">
99fcc43… drh 2372 @ <input type="hidden" name="nullout" value="1">
99fcc43… drh 2373 @ <input type="hidden" name="mimetype" value="%h(zMimetype)">
99fcc43… drh 2374 @ <input type="hidden" name="content" value="%h(zContent)">
99fcc43… drh 2375 if( zTitle ){
7dd07b2… drh 2376 @ <input aria-label="Title" type="hidden" name="title" value="%h(zTitle)">
99fcc43… drh 2377 }
99fcc43… drh 2378 }else if( P("edit") ){
99fcc43… drh 2379 /* Provide an edit to the fpid post */
99fcc43… drh 2380 zMimetype = P("mimetype");
99fcc43… drh 2381 zContent = PT("content");
99fcc43… drh 2382 zTitle = P("title");
99fcc43… drh 2383 if( zContent==0 ) zContent = fossil_strdup(pPost->zWiki);
99fcc43… drh 2384 if( zMimetype==0 ) zMimetype = fossil_strdup(pPost->zMimetype);
99fcc43… drh 2385 if( zTitle==0 && pPost->zThreadTitle!=0 ){
99fcc43… drh 2386 zTitle = fossil_strdup(pPost->zThreadTitle);
99fcc43… drh 2387 }
99fcc43… drh 2388 style_header("Edit %s", zTitle ? "Post" : "Reply");
fb85d7b… drh 2389 @ <h2>Original Post:</h2>
99fcc43… drh 2390 forum_render(pPost->zThreadTitle, pPost->zMimetype, pPost->zWiki,
46dd696… drh 2391 "forumEdit", 1);
673dc38… stephan 2392 if( bPreview ){
fb85d7b… drh 2393 @ <h2>Preview of Edited Post:</h2>
46dd696… drh 2394 forum_render(zTitle, zMimetype, zContent,"forumEdit", 1);
99fcc43… drh 2395 }
fb85d7b… drh 2396 @ <h2>Revised Message:</h2>
99fcc43… drh 2397 @ <form action="%R/forume2" method="POST">
920ace1… drh 2398 login_insert_csrf_secret();
99fcc43… drh 2399 @ <input type="hidden" name="fpid" value="%h(P("fpid"))">
99fcc43… drh 2400 @ <input type="hidden" name="edit" value="1">
99fcc43… drh 2401 forum_from_line();
5182a1b… drh 2402 forum_post_widget(zTitle, zMimetype, zContent);
99fcc43… drh 2403 }else{
99fcc43… drh 2404 /* Reply */
2e71dc2… drh 2405 char *zDisplayName;
5f3cb27… drh 2406 zMimetype = PD("mimetype",DEFAULT_FORUM_MIMETYPE);
99fcc43… drh 2407 zContent = PDT("content","");
99fcc43… drh 2408 style_header("Reply");
7b92fc6… stephan 2409 @ <h2>Replying to
62f0ce8… stephan 2410 @ <a href="%R/forumpost/%!S(zFpid)" target="_blank">%S(zFpid)</a>
0b6b3a0… stephan 2411 if( pRootPost->zThreadTitle ){
7b92fc6… stephan 2412 @ in thread
7b92fc6… stephan 2413 @ <span class="forumPostReplyTitle">%h(pRootPost->zThreadTitle)</span>
0b6b3a0… stephan 2414 }
7b92fc6… stephan 2415 @ </h2>
c67f5bf… george 2416 zDate = db_text(0, "SELECT datetime(%.17g,toLocal())", pPost->rDate);
2e71dc2… drh 2417 zDisplayName = display_name_from_login(pPost->zUser);
46d7ccd… drh 2418 @ <h3 class='forumPostHdr'>By %s(zDisplayName) on %h(zDate)</h3>
2e71dc2… drh 2419 fossil_free(zDisplayName);
c6dfb55… drh 2420 fossil_free(zDate);
46dd696… drh 2421 forum_render(0, pPost->zMimetype, pPost->zWiki, "forumEdit", 1);
ca90659… stephan 2422 if( bPreview && !fossil_all_whitespace(zContent) ){
fb85d7b… drh 2423 @ <h2>Preview:</h2>
46dd696… drh 2424 forum_render(0, zMimetype,zContent, "forumEdit", 1);
99fcc43… drh 2425 }
fb85d7b… drh 2426 @ <h2>Enter Reply:</h2>
99fcc43… drh 2427 @ <form action="%R/forume2" method="POST">
99fcc43… drh 2428 @ <input type="hidden" name="fpid" value="%h(P("fpid"))">
99fcc43… drh 2429 @ <input type="hidden" name="reply" value="1">
99fcc43… drh 2430 forum_from_line();
5182a1b… drh 2431 forum_post_widget(0, zMimetype, zContent);
99fcc43… drh 2432 }
99fcc43… drh 2433 if( !isDelete ){
99fcc43… drh 2434 @ <input type="submit" name="preview" value="Preview">
99fcc43… drh 2435 }
99fcc43… drh 2436 @ <input type="submit" name="cancel" value="Cancel">
ca90659… stephan 2437 if( isDelete || (bPreview && !fossil_all_whitespace(zContent)) ){
673dc38… stephan 2438 if( !iClosed || g.perm.Admin ) {
673dc38… stephan 2439 @ <input type="submit" name="submit" value="Submit">
673dc38… stephan 2440 }
3f6aa94… stephan 2441 }
3f6aa94… stephan 2442 forum_render_debug_options();
920ace1… drh 2443 login_insert_csrf_secret();
99fcc43… drh 2444 @ </form>
5079ffb… stephan 2445 if( !bReply ){
5079ffb… stephan 2446 forum_render_attachment_list(rid_to_uuid(fpid));
5079ffb… stephan 2447 }
ca90659… stephan 2448 if( !isDelete ){
ca90659… stephan 2449 forum_render_attachment_notice();
ca90659… stephan 2450 }
b2f0071… stephan 2451 forum_emit_js();
112c713… drh 2452 style_finish_page();
673dc38… stephan 2453 }
f646537… drh 2454
f646537… drh 2455 /*
f646537… drh 2456 ** SETTING: forum-close-policy boolean default=off
f646537… drh 2457 ** If true, forum moderators may close/re-open forum posts, and reply
f646537… drh 2458 ** to closed posts. If false, only administrators may do so. Note that
f646537… drh 2459 ** this only affects the forum web UI, not post-closing tags which
f646537… drh 2460 ** arrive via the command-line or from synchronization with a remote.
5079ffb… stephan 2461 ** This policy also determines whether moderators may delete forum
5079ffb… stephan 2462 ** attachments.
ca90659… stephan 2463 **
f646537… drh 2464 ** SETTING: forum-title width=20 default=Forum
f646537… drh 2465 ** This is the name or "title" of the Forum for this repository. The
f646537… drh 2466 ** default is just "Forum". But in some setups, admins might want to
f646537… drh 2467 ** change it to "Developer Forum" or "User Forum" or whatever other name
f646537… drh 2468 ** seems more appropriate for the particular usage.
5079ffb… stephan 2469 **
8361306… drh 2470 ** SETTING: forum-max-title width=10 default=125
8361306… drh 2471 ** The maximum number of characters allowed in the title of a new
8361306… drh 2472 ** forum thread, as enforced by the web browser on the post editor.
8361306… drh 2473 ** A value of zero or less means there is no limit.
8361306… drh 2474 **
5079ffb… stephan 2475 ** SETTING: attachment-size-limit width=16
ca90659… stephan 2476 ** The maximum number of bytes for an attachment to a wiki page,
ca90659… stephan 2477 ** ticket, tech note, or forum post. The default (or 0) is unlimited
ca90659… stephan 2478 ** but a limit may be imposed by the web server or a proxy.
4c4c28d… drh 2479 **
4c4c28d… drh 2480 ** SETTING: forum-statuses width=40 block-text
4c4c28d… drh 2481 ** This JSON5-formatted value defines an array of objects describing
4c4c28d… drh 2482 ** the available statuses of forum posts. Each entry of the array must
c56de3f… stephan 2483 ** be an object in the form {label:"X",value:"Y"}.
4c4c28d… drh 2484 ** The label is used in the UI and value becomes the value of the
4c4c28d… drh 2485 ** "status" tag on forum posts. Any forum post which has a status
4c4c28d… drh 2486 ** value which does not appear in this list is treated as if it had
4c4c28d… drh 2487 ** the first value from this list. If this setting is empty, is
4c4c28d… drh 2488 ** ill-formed JSON, or has only a single entry then the forum will
4c4c28d… drh 2489 ** lack the capability of setting and filtering by status.
f646537… drh 2490 */
673dc38… stephan 2491
673dc38… stephan 2492 /*
673dc38… stephan 2493 ** WEBPAGE: setup_forum
673dc38… stephan 2494 **
673dc38… stephan 2495 ** Forum configuration and metrics.
673dc38… stephan 2496 */
673dc38… stephan 2497 void forum_setup(void){
673dc38… stephan 2498 /* boolean config settings specific to the forum. */
f646537… drh 2499 static const char *azForumSettings[] = {
f646537… drh 2500 "forum-close-policy",
f646537… drh 2501 "forum-title",
8361306… drh 2502 "forum-max-title",
bcf094c… drh 2503 "forum-statuses",
673dc38… stephan 2504 };
673dc38… stephan 2505
673dc38… stephan 2506 login_check_credentials();
673dc38… stephan 2507 if( !g.perm.Setup ){
673dc38… stephan 2508 login_needed(g.anon.Setup);
673dc38… stephan 2509 return;
673dc38… stephan 2510 }
673dc38… stephan 2511 style_set_current_feature("forum");
673dc38… stephan 2512 style_header("Forum Setup");
619d514… stephan 2513
673dc38… stephan 2514 @ <h2>Metrics</h2>
673dc38… stephan 2515 {
673dc38… stephan 2516 int nPosts = db_int(0, "SELECT COUNT(*) FROM event WHERE type='f'");
673dc38… stephan 2517 @ <p><a href='%R/forum'>Forum posts</a>:
673dc38… stephan 2518 @ <a href='%R/timeline?y=f'>%d(nPosts)</a></p>
619d514… stephan 2519 }
673dc38… stephan 2520
673dc38… stephan 2521 @ <h2>Supervisors</h2>
673dc38… stephan 2522 {
673dc38… stephan 2523 Stmt q = empty_Stmt;
673dc38… stephan 2524 db_prepare(&q, "SELECT uid, login, cap FROM user "
673dc38… stephan 2525 "WHERE cap GLOB '*[as6]*' ORDER BY login");
673dc38… stephan 2526 @ <table class='bordered'>
673dc38… stephan 2527 @ <thead><tr><th>User</th><th>Capabilities</th></tr></thead>
673dc38… stephan 2528 @ <tbody>
673dc38… stephan 2529 while( SQLITE_ROW==db_step(&q) ){
673dc38… stephan 2530 const int iUid = db_column_int(&q, 0);
673dc38… stephan 2531 const char *zUser = db_column_text(&q, 1);
673dc38… stephan 2532 const char *zCap = db_column_text(&q, 2);
673dc38… stephan 2533 @ <tr>
673dc38… stephan 2534 @ <td><a href='%R/setup_uedit?id=%d(iUid)'>%h(zUser)</a></td>
673dc38… stephan 2535 @ <td>(%h(zCap))</td>
673dc38… stephan 2536 @ </tr>
673dc38… stephan 2537 }
673dc38… stephan 2538 db_finalize(&q);
673dc38… stephan 2539 @</tbody></table>
673dc38… stephan 2540 }
673dc38… stephan 2541
673dc38… stephan 2542 @ <h2>Moderators</h2>
25e4b54… drh 2543 if( db_int(0, "SELECT count(*) FROM user "
25e4b54… drh 2544 " WHERE cap GLOB '*5*' AND cap NOT GLOB '*[as6]*'")==0 ){
25e4b54… drh 2545 @ <p>No non-supervisor moderators
25e4b54… drh 2546 }else{
673dc38… stephan 2547 Stmt q = empty_Stmt;
673dc38… stephan 2548 db_prepare(&q, "SELECT uid, login, cap FROM user "
25e4b54… drh 2549 "WHERE cap GLOB '*5*' AND cap NOT GLOB '*[as6]*'"
25e4b54… drh 2550 " ORDER BY login");
673dc38… stephan 2551 @ <table class='bordered'>
673dc38… stephan 2552 @ <thead><tr><th>User</th><th>Capabilities</th></tr></thead>
673dc38… stephan 2553 @ <tbody>
673dc38… stephan 2554 while( SQLITE_ROW==db_step(&q) ){
673dc38… stephan 2555 const int iUid = db_column_int(&q, 0);
673dc38… stephan 2556 const char *zUser = db_column_text(&q, 1);
673dc38… stephan 2557 const char *zCap = db_column_text(&q, 2);
673dc38… stephan 2558 @ <tr>
673dc38… stephan 2559 @ <td><a href='%R/setup_uedit?id=%d(iUid)'>%h(zUser)</a></td>
673dc38… stephan 2560 @ <td>(%h(zCap))</td>
673dc38… stephan 2561 @ </tr>
673dc38… stephan 2562 }
673dc38… stephan 2563 db_finalize(&q);
673dc38… stephan 2564 @ </tbody></table>
673dc38… stephan 2565 }
673dc38… stephan 2566
673dc38… stephan 2567 @ <h2>Settings</h2>
920ace1… drh 2568 if( P("submit") && cgi_csrf_safe(2) ){
673dc38… stephan 2569 int i = 0;
673dc38… stephan 2570 db_begin_transaction();
f646537… drh 2571 for(i=0; i<ArraySize(azForumSettings); i++){
f646537… drh 2572 char zQP[4];
f646537… drh 2573 const char *z;
f646537… drh 2574 const Setting *pSetting = setting_find(azForumSettings[i]);
f646537… drh 2575 if( pSetting==0 ) continue;
f646537… drh 2576 zQP[0] = 'a'+i;
f646537… drh 2577 zQP[1] = zQP[0];
f646537… drh 2578 zQP[2] = 0;
f646537… drh 2579 z = P(zQP);
f646537… drh 2580 if( z==0 || z[0]==0 ) continue;
f646537… drh 2581 db_set(pSetting->name/*works-like:"x"*/, z, 0);
673dc38… stephan 2582 }
673dc38… stephan 2583 db_end_transaction(0);
673dc38… stephan 2584 @ <p><em>Settings saved.</em></p>
673dc38… stephan 2585 }
673dc38… stephan 2586 {
673dc38… stephan 2587 int i = 0;
673dc38… stephan 2588 @ <form action="%R/setup_forum" method="post">
673dc38… stephan 2589 login_insert_csrf_secret();
673dc38… stephan 2590 @ <table class='forum-settings-list'><tbody>
f646537… drh 2591 for(i=0; i<ArraySize(azForumSettings); i++){
f646537… drh 2592 char zQP[4];
f646537… drh 2593 const Setting *pSetting = setting_find(azForumSettings[i]);
f646537… drh 2594 if( pSetting==0 ) continue;
f646537… drh 2595 zQP[0] = 'a'+i;
f646537… drh 2596 zQP[1] = zQP[0];
f646537… drh 2597 zQP[2] = 0;
f646537… drh 2598 if( pSetting->width==0 ){
f646537… drh 2599 /* Boolean setting */
25e4b54… drh 2600 @ <tr><td align="right">
c64f28d… drh 2601 @ <a href='%R/help/%h(pSetting->name)'>%h(pSetting->name)</a>:
25e4b54… drh 2602 @ </td><td>
f646537… drh 2603 onoff_attribute("", zQP, pSetting->name/*works-like:"x"*/, 0, 0);
c64f28d… drh 2604 @ </td></tr>
bcf094c… drh 2605 }else if( pSetting->forceTextArea ){
bcf094c… drh 2606 /* Multi-line text value setting */
bcf094c… drh 2607 @ <tr><td align="right" valign="top">
bcf094c… drh 2608 @ <a href='%R/help/%h(pSetting->name)'>%h(pSetting->name)</a>:
bcf094c… drh 2609 @ </td><td>
bcf094c… drh 2610 textarea_attribute("", 6, 60, pSetting->name, zQP/*works-like:""*/,
bcf094c… drh 2611 pSetting->def, 0);
bcf094c… drh 2612 if( fossil_strcmp(pSetting->name,"forum-statuses")==0
bcf094c… drh 2613 && db_int(0, "SELECT value<>'' AND NOT json_valid(value,0x02)"
bcf094c… drh 2614 " FROM config WHERE name='forum-statuses'") ){
bcf094c… drh 2615 @ <br><span class='error'>Warning: this value is not \
bcf094c… drh 2616 @ well-formed JSON5 and will be ignored.</span>
bcf094c… drh 2617 }
bcf094c… drh 2618 @ </td></tr>
f646537… drh 2619 }else{
f646537… drh 2620 /* Text value setting */
25e4b54… drh 2621 @ <tr><td align="right">
c64f28d… drh 2622 @ <a href='%R/help/%h(pSetting->name)'>%h(pSetting->name)</a>:
25e4b54… drh 2623 @ </td><td>
f646537… drh 2624 entry_attribute("", 25, pSetting->name, zQP/*works-like:""*/,
f646537… drh 2625 pSetting->def, 0);
f646537… drh 2626 @ </td></tr>
5079ffb… stephan 2627 }
673dc38… stephan 2628 }
673dc38… stephan 2629 @ </tbody></table>
673dc38… stephan 2630 @ <input type='submit' name='submit' value='Apply changes'>
673dc38… stephan 2631 @ </form>
673dc38… stephan 2632 }
673dc38… stephan 2633
673dc38… stephan 2634 style_finish_page();
4c4c28d… drh 2635 }
4c4c28d… drh 2636
4c4c28d… drh 2637 /*
4c4c28d… drh 2638 ** If the forum-statuses setting is active and has 2 or more entries,
4c4c28d… drh 2639 ** this adds a submenu for selecting the status filter, else it emits
4c4c28d… drh 2640 ** nothing.
40c6d75… drh 2641 **
40c6d75… drh 2642 ** If bCookie is true, the status selection is remembered in the
40c6d75… drh 2643 ** forumStatus cookie.
4c4c28d… drh 2644 */
40c6d75… drh 2645 void forum_status_submenu(int bCookie){
4c4c28d… drh 2646 const ForumStatusList * const fss = forum_statuses();
4c4c28d… drh 2647 static int i = 0;
4c4c28d… drh 2648 static const char **az;
4c4c28d… drh 2649 if( i==0 && fss->n>1 ){
4c4c28d… drh 2650 unsigned j;
4c4c28d… drh 2651 az = fossil_malloc(sizeof(az[0]) * ((1 + fss->n) * 2));
4c4c28d… drh 2652 az[i++] = "*";
4c4c28d… drh 2653 az[i++] = "Any status";
4c4c28d… drh 2654 for( j = 0; j < fss->n; ++j ){
4c4c28d… drh 2655 const ForumStatus * fs = &fss->aStatus[j];
4c4c28d… drh 2656 /* Potential TODO: skip any entries for which there are no
4c4c28d… drh 2657 ** forum posts with a status=${fs->zValue} tag. */
4c4c28d… drh 2658 az[i++] = fs->zValue;
4c4c28d… drh 2659 az[i++] = fs->zLabel;
4c4c28d… drh 2660 }
4c4c28d… drh 2661 //assert( i==(1+fss->n)*2 );
4c4c28d… drh 2662 }
4c4c28d… drh 2663 if( i ){
40c6d75… drh 2664 if( bCookie ) cookie_link_parameter("status","forumStatus","*");
4c4c28d… drh 2665 style_submenu_multichoice("status", i/2, az, 0);
4c4c28d… drh 2666 }
4c4c28d… drh 2667 }
4c4c28d… drh 2668
4c4c28d… drh 2669 /*
4c4c28d… drh 2670 ** Transient SQL Function: status_match(FROOT)
4c4c28d… drh 2671 **
4c4c28d… drh 2672 ** Return true if the forum thread identified by FROOT should be included
4c4c28d… drh 2673 ** in a list of threads. Used to implement the status=NAME query parameter
4c4c28d… drh 2674 ** on /forum.
4c4c28d… drh 2675 **
4c4c28d… drh 2676 ** The result of this routine depends on the content of the
4c4c28d… drh 2677 ** ForumStatusMatch *pMData object that is available via sqlite3_user_data().
4c4c28d… drh 2678 **
4c4c28d… drh 2679 ** * If pMData==NULL, always return true. This means that no
4c4c28d… drh 2680 ** filtering of threads is being done. This is the common case.
4c4c28d… drh 2681 **
4c4c28d… drh 2682 ** * If FROOT contains a status property value that matches
4c4c28d… drh 2683 ** pMData->iMatch, return true.
4c4c28d… drh 2684 **
4c4c28d… drh 2685 ** * if pMData->iMatch==0 (meaning we want to match the default
4c4c28d… drh 2686 ** status value) and if the FROOT thread contains a status that
4c4c28d… drh 2687 ** is not on the list of statuses or if FROOT has no statue
4c4c28d… drh 2688 ** property at all, then return true. In other words, a forum
4c4c28d… drh 2689 ** thread with no status property or an unknown status property
4c4c28d… drh 2690 ** is treated as if it had the default status.
4c4c28d… drh 2691 **
4c4c28d… drh 2692 ** * Otherwise, return false.
4c4c28d… drh 2693 */
4c4c28d… drh 2694 static void forum_status_match(
4c4c28d… drh 2695 sqlite3_context *context,
4c4c28d… drh 2696 int argc,
4c4c28d… drh 2697 sqlite3_value **argv
4c4c28d… drh 2698 ){
4c4c28d… drh 2699 static Stmt q;
4c4c28d… drh 2700 ForumStatusMatch *pMData = sqlite3_user_data(context);
4c4c28d… drh 2701 int i;
4c4c28d… drh 2702
4c4c28d… drh 2703 if( pMData==0 ){
4c4c28d… drh 2704 sqlite3_result_int(context, 1);
4c4c28d… drh 2705 return;
4c4c28d… drh 2706 }
4c4c28d… drh 2707 db_static_prepare(&q,
4c4c28d… drh 2708 "SELECT value FROM tagxref\n"
4c4c28d… drh 2709 " WHERE tagid=%d\n"
4c4c28d… drh 2710 " AND tagtype>=1\n"
4c4c28d… drh 2711 " AND rid=:rid\n"
4c4c28d… drh 2712 " ORDER BY mtime DESC LIMIT 1",
4c4c28d… drh 2713 pMData->eStatusTag
4c4c28d… drh 2714 );
4c4c28d… drh 2715 db_bind_int(&q, ":rid", sqlite3_value_int(argv[0]));
4c4c28d… drh 2716 if( db_step(&q)==SQLITE_ROW ){
4c4c28d… drh 2717 const char *zValue = (const char*)db_column_text(&q,0);
4c4c28d… drh 2718 const ForumStatusList *pFses = pMData->pFses;
4c4c28d… drh 2719 if( zValue==0 ){
4c4c28d… drh 2720 i = 0;
4c4c28d… drh 2721 }else{
4c4c28d… drh 2722 for(i=0; i<pFses->n; i++){
4c4c28d… drh 2723 if( fossil_strcmp(pFses->aStatus[i].zValue,zValue)==0 ) break;
4c4c28d… drh 2724 }
4c4c28d… drh 2725 }
4c4c28d… drh 2726 if( i>=pMData->pFses->n ) i = 0;
4c4c28d… drh 2727 }else{
4c4c28d… drh 2728 i = 0;
4c4c28d… drh 2729 }
4c4c28d… drh 2730 db_reset(&q);
4c4c28d… drh 2731 sqlite3_result_int(context, i==pMData->iMatch);
4c4c28d… drh 2732 }
4c4c28d… drh 2733
4c4c28d… drh 2734 /*
556f21d… drh 2735 ** Register the status_match() SQL function so that it matches forum
556f21d… drh 2736 ** threads having status zStatus. Used to implement the status=NAME
556f21d… drh 2737 ** query parameter on /timeline.
556f21d… drh 2738 **
556f21d… drh 2739 ** Return true if the status_match() function was registered and
556f21d… drh 2740 ** filtering should be applied. Return false (and register nothing)
556f21d… drh 2741 ** if zStatus is NULL, empty, or "*", or if the forum-statuses setting
556f21d… drh 2742 ** has fewer than two entries, or if there is no forum.
556f21d… drh 2743 */
556f21d… drh 2744 int forum_status_match_setup(const char *zStatus){
556f21d… drh 2745 static ForumStatusMatch sFSM;
556f21d… drh 2746 const ForumStatusList *pFstat;
556f21d… drh 2747 if( zStatus==0 || zStatus[0]==0 || fossil_strcmp(zStatus,"*")==0 ){
556f21d… drh 2748 return 0;
556f21d… drh 2749 }
556f21d… drh 2750 if( !db_table_exists("repository","forumpost") ) return 0;
556f21d… drh 2751 pFstat = forum_statuses();
556f21d… drh 2752 if( pFstat->n<2 ) return 0;
556f21d… drh 2753 sFSM.pFses = pFstat;
556f21d… drh 2754 sFSM.eStatusTag = db_int(0, "SELECT tagid FROM tag WHERE tagname='status'");
556f21d… drh 2755 for(sFSM.iMatch=0; sFSM.iMatch<pFstat->n; sFSM.iMatch++){
556f21d… drh 2756 if( 0==fossil_strcmp(zStatus, pFstat->aStatus[sFSM.iMatch].zValue) ){
556f21d… drh 2757 break;
556f21d… drh 2758 }
556f21d… drh 2759 }
556f21d… drh 2760 sqlite3_create_function(g.db,"status_match",1,SQLITE_UTF8,(void*)&sFSM,
556f21d… drh 2761 forum_status_match, 0, 0);
556f21d… drh 2762 return 1;
556f21d… drh 2763 }
556f21d… drh 2764
556f21d… drh 2765 /*
4b82e96… drh 2766 ** WEBPAGE: forummain
99fcc43… drh 2767 ** WEBPAGE: forum
99fcc43… drh 2768 **
99fcc43… drh 2769 ** The main page for the forum feature. Show a list of recent forum
99fcc43… drh 2770 ** threads. Also show a search box at the top if search is enabled,
99fcc43… drh 2771 ** and a button for creating a new thread, if enabled.
aff2090… drh 2772 **
aff2090… drh 2773 ** Query parameters:
aff2090… drh 2774 **
aff2090… drh 2775 ** n=N The number of threads to show on each page
aff2090… drh 2776 ** x=X Skip the first X threads
b086261… stephan 2777 ** s=Y Search for term Y.
99fcc43… drh 2778 */
99fcc43… drh 2779 void forum_main_page(void){
99fcc43… drh 2780 Stmt q;
2ee93fc… stephan 2781 int iLimit = 0, iOfst, iCnt;
d4281bb… drh 2782 int srchFlags;
b086261… stephan 2783 const int isSearch = P("s")!=0;
4c4c28d… drh 2784 const char *zStatusFilter;
4c4c28d… drh 2785 char const *zLimit = 0; /* Value of the n= query parameter */
4c4c28d… drh 2786 int eStatusTag = 0; /* tagid for the "status" property */
4c4c28d… drh 2787 int bHasStatus = 0; /* True if forum-statuses setting exists */
4c4c28d… drh 2788 int bFilter = 0; /* True if status=NAME query parameter */
0f934bb… stephan 2789 int bHasForum = 0; /* True if forumpost table exists */
0f934bb… stephan 2790 const ForumStatusList *pFstat = forum_statuses();
4c4c28d… drh 2791 ForumStatusMatch sFSM; /* Aux data to status_match() SQL function */
2ee93fc… stephan 2792
99fcc43… drh 2793 login_check_credentials();
2e9e369… drh 2794 srchFlags = search_restrict(SRCH_FORUM);
99fcc43… drh 2795 if( !g.perm.RdForum ){
99fcc43… drh 2796 login_needed(g.anon.RdForum);
99fcc43… drh 2797 return;
99fcc43… drh 2798 }
57f1e87… drh 2799 cgi_check_for_malice();
0f934bb… stephan 2800 bHasForum = db_table_exists("repository","forumpost");
0f934bb… stephan 2801 if( bHasForum ){
0f934bb… stephan 2802 eStatusTag = db_int(0, "SELECT tagid FROM tag WHERE tagname='status'");
0f934bb… stephan 2803 bHasStatus = pFstat->n>1;
4c4c28d… drh 2804 }
112c713… drh 2805 style_set_current_feature("forum");
5079ffb… stephan 2806 style_header("%s%s", db_get("forum-title","Forum"),
f646537… drh 2807 isSearch ? " Search Results" : "");
99fcc43… drh 2808 if( g.perm.WrForum ){
e11d391… wyoung 2809 style_submenu_element("New Thread","%R/forumnew");
c0e384c… wyoung 2810 }else{
c0e384c… wyoung 2811 style_submenu_element("New Thread","%R/login");
99fcc43… drh 2812 }
99fcc43… drh 2813 if( g.perm.ModForum && moderation_needed() ){
99fcc43… drh 2814 style_submenu_element("Moderation Requests", "%R/modreq");
99fcc43… drh 2815 }
d4281bb… drh 2816 if( (srchFlags & SRCH_FORUM)!=0 ){
d4281bb… drh 2817 if( search_screen(SRCH_FORUM, 0) ){
d4281bb… drh 2818 style_submenu_element("Recent Threads","%R/forum");
112c713… drh 2819 style_finish_page();
d4281bb… drh 2820 return;
d4281bb… drh 2821 }
d4281bb… drh 2822 }
2a4b604… stephan 2823 cookie_read_parameter("n","forum-n");
2ee93fc… stephan 2824 zLimit = P("n");
2ee93fc… stephan 2825 if( zLimit!=0 ){
2ee93fc… stephan 2826 iLimit = atoi(zLimit);
2ee93fc… stephan 2827 if( iLimit>=0 && P("udc")!=0 ){
2ee93fc… stephan 2828 cookie_write_parameter("n","forum-n",0);
2ee93fc… stephan 2829 }
2ee93fc… stephan 2830 }
2ee93fc… stephan 2831 if( iLimit<=0 ){
2ee93fc… stephan 2832 cgi_replace_query_parameter("n", fossil_strdup("25"))
2ee93fc… stephan 2833 /*for the sake of Max, below*/;
2ee93fc… stephan 2834 iLimit = 25;
2ee93fc… stephan 2835 }
2ee93fc… stephan 2836 style_submenu_entry("n","Max:",4,0);
40c6d75… drh 2837 forum_status_submenu(1);
4c4c28d… drh 2838 zStatusFilter = P("status") /*must be after forum_status_submenu()!*/;
aff2090… drh 2839 iOfst = atoi(PD("x","0"));
dbd96b2… drh 2840 iCnt = 0;
4c4c28d… drh 2841 if( zStatusFilter ){
4c4c28d… drh 2842 if( zStatusFilter[0]==0 || 0==fossil_strcmp("*",zStatusFilter) ){
4c4c28d… drh 2843 zStatusFilter = 0;
4c4c28d… drh 2844 }else{
4c4c28d… drh 2845 bFilter = bHasStatus;
4c4c28d… drh 2846 }
d6dcee6… drh 2847 }
d6dcee6… drh 2848 if( bFilter ){
d6dcee6… drh 2849 style_submenu_element("Timeline",
d6dcee6… drh 2850 "%R/timeline?ss=v&y=f&status=%t", zStatusFilter);
d6dcee6… drh 2851 }else{
d6dcee6… drh 2852 style_submenu_element("Timeline", "%R/timeline?ss=v&y=f");
0f934bb… stephan 2853 }
0f934bb… stephan 2854 if( bHasForum ){
4c4c28d… drh 2855 Stmt qStat = empty_Stmt; /* Query to get status information */
4c4c28d… drh 2856 if( bHasStatus ){
4c4c28d… drh 2857 /* The qStat query runs once for each output row generate by the
4c4c28d… drh 2858 ** q query. It determines the value and label of the status for
4c4c28d… drh 2859 ** the row with froot=:rowid
4c4c28d… drh 2860 */
4c4c28d… drh 2861 db_prepare(&qStat,
4c4c28d… drh 2862 "SELECT tagxref.value, forumstatus.label\n"
4c4c28d… drh 2863 " FROM forumstatus, tagxref\n"
4c4c28d… drh 2864 " WHERE tagid=%d AND tagtype>=1\n"
4c4c28d… drh 2865 " AND forumstatus.value=tagxref.value\n"
4c4c28d… drh 2866 " AND rid=:rid\n"
4c4c28d… drh 2867 " ORDER BY mtime DESC",
4c4c28d… drh 2868 eStatusTag
4c4c28d… drh 2869 );
4c4c28d… drh 2870 }
4c4c28d… drh 2871
4c4c28d… drh 2872 /* Create the status_match() SQL function that will determine
4c4c28d… drh 2873 ** whether or not each thread in the "q" query below is eligible
4c4c28d… drh 2874 ** for display
4c4c28d… drh 2875 */
4c4c28d… drh 2876 if( bFilter ){
4c4c28d… drh 2877 sFSM.pFses = pFstat;
4c4c28d… drh 2878 sFSM.eStatusTag = eStatusTag;
4c4c28d… drh 2879 for(sFSM.iMatch=0; sFSM.iMatch<pFstat->n; sFSM.iMatch++){
4c4c28d… drh 2880 if( 0==fossil_strcmp(zStatusFilter,
4c4c28d… drh 2881 pFstat->aStatus[sFSM.iMatch].zValue) ){
4c4c28d… drh 2882 break;
4c4c28d… drh 2883 }
4c4c28d… drh 2884 }
4c4c28d… drh 2885 sqlite3_create_function(g.db,"status_match",1,SQLITE_UTF8,(void*)&sFSM,
4c4c28d… drh 2886 forum_status_match, 0, 0);
4c4c28d… drh 2887 }else{
4c4c28d… drh 2888 sqlite3_create_function(g.db,"status_match",1,SQLITE_UTF8,0,
4c4c28d… drh 2889 forum_status_match, 0, 0);
4c4c28d… drh 2890 }
4c4c28d… drh 2891
ef7f85e… drh 2892 db_prepare(&q,
4c4c28d… drh 2893 "WITH thread(root,endtime,lastrid) AS (\n"
4c4c28d… drh 2894 " SELECT\n"
4c4c28d… drh 2895 " froot,\n"
4c4c28d… drh 2896 " max(fmtime),\n"
4c4c28d… drh 2897 " fpid\n"
4c4c28d… drh 2898 " FROM forumpost\n"
4c4c28d… drh 2899 " WHERE %s/*ModForum*/\n"
4c4c28d… drh 2900 " GROUP BY froot\n"
4c4c28d… drh 2901 " HAVING status_match(froot)\n"
4c4c28d… drh 2902 " ORDER BY 2 DESC\n"
4c4c28d… drh 2903 " LIMIT %d OFFSET %d\n"
4c4c28d… drh 2904 ")\n"
4c4c28d… drh 2905 "SELECT\n"
4c4c28d… drh 2906 " julianday('now') - thread.endtime,\n" /* 0 */
4c4c28d… drh 2907 " thread.endtime - "
4c4c28d… drh 2908 "(SELECT fmtime FROM forumpost WHERE fpid=root),\n" /* 1 */
4c4c28d… drh 2909 " (SELECT sum(fprev IS NULL) FROM forumpost"
4c4c28d… drh 2910 " WHERE froot=root),\n" /* 2 */
4c4c28d… drh 2911 " blob.uuid,\n" /* 3 */
4c4c28d… drh 2912 " substr(event.comment,instr(event.comment,':')+1),\n" /* 4 */
4c4c28d… drh 2913 " thread.lastrid,\n" /* 5 */
4c4c28d… drh 2914 " thread.root\n" /* 6 */
4c4c28d… drh 2915 " FROM thread, blob, event\n"
4c4c28d… drh 2916 " WHERE blob.rid=thread.lastrid\n"
4c4c28d… drh 2917 " AND event.objid=thread.lastrid\n"
ef7f85e… drh 2918 " ORDER BY 1;",
ef7f85e… drh 2919 g.perm.ModForum ? "true" : "fpid NOT IN private" /*safe-for-%s*/,
aff2090… drh 2920 iLimit+1, iOfst
d73c5fd… drh 2921 );
d73c5fd… drh 2922 while( db_step(&q)==SQLITE_ROW ){
4c4c28d… drh 2923 char *zAge;
4c4c28d… drh 2924 int nMsg;
4c4c28d… drh 2925 const char *zUuid;
4c4c28d… drh 2926 const char *zTitle;
4c4c28d… drh 2927 const char *zStatus;
4c4c28d… drh 2928 const char *zStatusLbl;
4c4c28d… drh 2929 const int bShowStatus = bHasStatus && !zStatusFilter;
4c4c28d… drh 2930 const int nCols = bShowStatus ? 4 : 3;
4c4c28d… drh 2931
4c4c28d… drh 2932 if( qStat.pStmt ){
4c4c28d… drh 2933 /* Determine the status value for this row */
4c4c28d… drh 2934 db_reset(&qStat);
4c4c28d… drh 2935 db_bind_int(&qStat, ":rid", db_column_int(&q,6));
4c4c28d… drh 2936 if( db_step(&qStat)==SQLITE_ROW ){
4c4c28d… drh 2937 zStatus = db_column_text(&qStat, 0);
4c4c28d… drh 2938 zStatusLbl = db_column_text(&qStat, 1);
4c4c28d… drh 2939 }else{
4c4c28d… drh 2940 zStatus = pFstat->aStatus[0].zValue;
4c4c28d… drh 2941 zStatusLbl = pFstat->aStatus[0].zLabel;
4c4c28d… drh 2942 }
4c4c28d… drh 2943 }else{
4c4c28d… drh 2944 zStatus = zStatusLbl = NULL;
4c4c28d… drh 2945 }
4c4c28d… drh 2946 zAge = human_readable_age(db_column_double(&q,0));
4c4c28d… drh 2947 nMsg = db_column_int(&q, 2);
4c4c28d… drh 2948 zUuid = db_column_text(&q, 3);
4c4c28d… drh 2949 zTitle = db_column_text(&q, 4);
dbd96b2… drh 2950 if( iCnt==0 ){
4c4c28d… drh 2951 char *zTail = bFilter ? mprintf(" with status=%Q", zStatusFilter): 0;
dbd96b2… drh 2952 if( iOfst>0 ){
4c4c28d… drh 2953 @ <h1>Threads at least %s(zAge) old%h(zTail ? zTail : "")</h1>
aff2090… drh 2954 }else{
4c4c28d… drh 2955 @ <h1>Most recent threads%h(zTail ? zTail : "")</h1>
dbd96b2… drh 2956 }
4c4c28d… drh 2957 fossil_free(zTail);
7778249… wyoung 2958 @ <div class='forumPosts fileage'><table width="100%%">
dbd96b2… drh 2959 if( iOfst>0 ){
dbd96b2… drh 2960 if( iOfst>iLimit ){
4c4c28d… drh 2961 @ <tr><td colspan="%d(nCols)">\
4c4c28d… drh 2962 @ <a href='%R/forum?x=%d(iOfst-iLimit)&n=%d(iLimit) \
4c4c28d… drh 2963 if( bFilter ){
4c4c28d… drh 2964 @ &status=%T(zStatusFilter)\
4c4c28d… drh 2965 }
4c4c28d… drh 2966 @ '>&uarr; Newer...</a></td></tr>
dbd96b2… drh 2967 }else{
4c4c28d… drh 2968 @ <tr><td colspan="%d(nCols)">\
4c4c28d… drh 2969 @ <a href='%R/forum?n=%d(iLimit)\
4c4c28d… drh 2970 if( bFilter ){
4c4c28d… drh 2971 @ &status=%T(zStatusFilter) \
4c4c28d… drh 2972 }
4c4c28d… drh 2973 @ '>&uarr; Newer...</a></td></tr>
dbd96b2… drh 2974 }
aff2090… drh 2975 }
aff2090… drh 2976 }
aff2090… drh 2977 iCnt++;
aff2090… drh 2978 if( iCnt>iLimit ){
4c4c28d… drh 2979 @ <tr><td colspan="%d(nCols)">\
4c4c28d… drh 2980 @ <a href='%R/forum?x=%d(iOfst+iLimit)&n=%d(iLimit) \
4c4c28d… drh 2981 if( bFilter ){
4c4c28d… drh 2982 @ &status=%T(zStatusFilter)\
4c4c28d… drh 2983 }
4c4c28d… drh 2984 @ '>&darr; Older...</a></td></tr>
dbd96b2… drh 2985 fossil_free(zAge);
aff2090… drh 2986 break;
aff2090… drh 2987 }
4c4c28d… drh 2988 @ <tr \
4c4c28d… drh 2989 if( bHasStatus ){
4c4c28d… drh 2990 @ data-status="%h(zStatus)"\
4c4c28d… drh 2991 }
4c4c28d… drh 2992 @ ><td>%h(zAge) ago</td>
4c4c28d… drh 2993 @ <td class='subject'>%z(href("%R/forumpost/%S",zUuid))%h(zTitle)</a>\
4c4c28d… drh 2994 @ </td><td>\
ef7f85e… drh 2995 if( g.perm.ModForum && moderation_pending(db_column_int(&q,5)) ){
ef7f85e… drh 2996 @ <span class="modpending">\
ef7f85e… drh 2997 @ Awaiting Moderator Approval</span><br>
ef7f85e… drh 2998 }
a50dfe6… drh 2999 if( nMsg<2 ){
4c4c28d… drh 3000 @ no replies\
a50dfe6… drh 3001 }else{
09103fd… wyoung 3002 char *zDuration = human_readable_age(db_column_double(&q,1));
4c4c28d… drh 3003 @ %d(nMsg) posts spanning %h(zDuration)\
09103fd… wyoung 3004 fossil_free(zDuration);
09103fd… wyoung 3005 }
4c4c28d… drh 3006 @ </td>\
4c4c28d… drh 3007 if( bShowStatus ){
4c4c28d… drh 3008 @ <td class='status'>%h(zStatusLbl)</td>\
4c4c28d… drh 3009 }
4c4c28d… drh 3010 if( qStat.pStmt ){
4c4c28d… drh 3011 db_reset(&qStat);
4c4c28d… drh 3012 }
4c4c28d… drh 3013 @</tr>
d73c5fd… drh 3014 fossil_free(zAge);
d73c5fd… drh 3015 }
d73c5fd… drh 3016 db_finalize(&q);
4c4c28d… drh 3017 if( qStat.pStmt ) db_finalize(&qStat);
4c4c28d… drh 3018 sqlite3_create_function(g.db,"status_match",1,SQLITE_UTF8,0,0,0,0);
dbd96b2… drh 3019 }
dbd96b2… drh 3020 if( iCnt>0 ){
dbd96b2… drh 3021 @ </table></div>
dbd96b2… drh 3022 }else{
dbd96b2… drh 3023 @ <h1>No forum posts found</h1>
dbd96b2… drh 3024 }
4c4c28d… drh 3025 if( bHasStatus ){
4c4c28d… drh 3026 /* We need a JS-side kludge to avoid passing on the x=N
4c4c28d… drh 3027 ** URL arg when the status selection list is activated. */
4c4c28d… drh 3028 forum_emit_js();
4c4c28d… drh 3029 }
112c713… drh 3030 style_finish_page();
ca90659… stephan 3031 }
ca90659… stephan 3032
ca90659… stephan 3033 /*
ca90659… stephan 3034 ** The AJAX counterpart of forum_post().
ca90659… stephan 3035 **
ca90659… stephan 3036 ** Returns the new artifact's RID on success, 0 if no changes were
ca90659… stephan 3037 ** necessary (e.g. an empty new post or dry-run mode), and a negative
ca90659… stephan 3038 ** value on error. If it returns a negative value then it will have
ca90659… stephan 3039 ** populated the ajax response state with an error object.
ca90659… stephan 3040 **
ca90659… stephan 3041 ** zTitle must be NULL if iInReplyTo>0 and must be non-empty if
ca90659… stephan 3042 ** iInReplyTo==0.
ca90659… stephan 3043 **
ca90659… stephan 3044 ** The caller must have started a transaction and must roll it back if
ca90659… stephan 3045 ** this call returns <=0, noting that only the negative-value case is
ca90659… stephan 3046 ** an error.
ca90659… stephan 3047 **
ca90659… stephan 3048 ** This function does some work to try to ensure that duplicate
ca90659… stephan 3049 ** entries are not save (this can happen as a side effect of the forum
ca90659… stephan 3050 ** post editor added in 2026-06). If the given post will not have been
ca90659… stephan 3051 ** materially edited by these changes, they are not applied and the
ca90659… stephan 3052 ** rid of the existing entry is used.
ca90659… stephan 3053 **
ca90659… stephan 3054 ** Maintenance reminders:
ca90659… stephan 3055 **
ca90659… stephan 3056 ** - iInReplyTo==0 && iEdit==0: new thread
ca90659… stephan 3057 ** - iInReplyTo==0 && iEdit>0 : edit top post or response
ca90659… stephan 3058 ** - iInReplyTo>0 && iEdit==0: new response
ca90659… stephan 3059 ** - iInReplyTo>0 && iEdit>0 : edit response
ca90659… stephan 3060 */
ca90659… stephan 3061 static int forum_post_ajax(
ca90659… stephan 3062 const char *zTitle, /* Title. NULL for replies */
ca90659… stephan 3063 int iInReplyTo, /* Post replying to. 0 for new threads */
ca90659… stephan 3064 int iEdit, /* Post being edited, or zero for a new post */
ca90659… stephan 3065 const char *zUser, /* Username. NULL means use login name */
ca90659… stephan 3066 const char *zMimetype, /* Mimetype of content. */
ca90659… stephan 3067 const char *zContent, /* Content */
ca90659… stephan 3068 int iFlags /* FPOST_xyz flag values */
ca90659… stephan 3069 ){
ca90659… stephan 3070 char *zI;
ca90659… stephan 3071 char *zG;
ca90659… stephan 3072 char *zP = 0;
ca90659… stephan 3073 int iBasis;
ca90659… stephan 3074 Blob x = BLOB_INITIALIZER,
ca90659… stephan 3075 cksum = BLOB_INITIALIZER,
ca90659… stephan 3076 formatCheck = BLOB_INITIALIZER,
ca90659… stephan 3077 errMsg = BLOB_INITIALIZER;
ca90659… stephan 3078 Manifest *pPost = 0;
ca90659… stephan 3079 int nContent = zContent ? (int)strlen(zContent) : 0;
ca90659… stephan 3080 int rc = 0;
ca90659… stephan 3081
ca90659… stephan 3082 assert( db_transaction_nesting_depth()>0 );
ca90659… stephan 3083 schema_forum();
ca90659… stephan 3084 if( iEdit==0 && fossil_all_whitespace(zContent) ){
ca90659… stephan 3085 return 0;
ca90659… stephan 3086 }
ca90659… stephan 3087 if( !g.perm.Admin && (iEdit || iInReplyTo)
ca90659… stephan 3088 && forum_rid_is_tagged(iEdit ? iEdit : iInReplyTo, "closed", 1) ){
ca90659… stephan 3089 return -ajax_route_error(400, "Thread is closed.");
ca90659… stephan 3090 }
ca90659… stephan 3091 if( 0==iInReplyTo && fossil_all_whitespace(zTitle) ){
ca90659… stephan 3092 return -ajax_route_error(400, "Empty title is not permitted.");
ca90659… stephan 3093 }
ca90659… stephan 3094
ca90659… stephan 3095 if( zUser==0 ){
ca90659… stephan 3096 if( login_is_nobody() ){
ca90659… stephan 3097 zUser = "anonymous";
ca90659… stephan 3098 }else{
ca90659… stephan 3099 zUser = login_name();
ca90659… stephan 3100 }
ca90659… stephan 3101 }
ca90659… stephan 3102 if( iEdit>0
ca90659… stephan 3103 && !g.perm.Admin
ca90659… stephan 3104 && !forumpost_is_owner(iEdit, zUser) ){
ca90659… stephan 3105 return -ajax_route_error(
ca90659… stephan 3106 403, "Only admins may edit other peoples' posts."
ca90659… stephan 3107 );
ca90659… stephan 3108 }
ca90659… stephan 3109 if( iInReplyTo==0 && iEdit>0 ){
ca90659… stephan 3110 iBasis = iEdit;
ca90659… stephan 3111 iInReplyTo = db_int(0, "SELECT firt FROM forumpost WHERE fpid=%d",
ca90659… stephan 3112 iEdit);
ca90659… stephan 3113 }else{
ca90659… stephan 3114 iBasis = iInReplyTo;
ca90659… stephan 3115 /* TODO (2026-06-008) If (iInReplyTo>0 && iEdit>0), validate that
ca90659… stephan 3116 ** iInReplyTo is connected to iEdit properly, else we risk
ca90659… stephan 3117 ** reparenting the new edit and having unrepredictable downstream
ca90659… stephan 3118 ** side effects. */
ca90659… stephan 3119 }
ca90659… stephan 3120
ca90659… stephan 3121 if( 0!=zMimetype && 0==zMimetype[0] ){
ca90659… stephan 3122 zMimetype = 0;
ca90659… stephan 3123 }
ca90659… stephan 3124
ca90659… stephan 3125 if( 0!=zTitle && 0==zTitle[0] ) zTitle = 0;
ca90659… stephan 3126 webpage_assert( (zTitle==0)+(iInReplyTo==0)==1 );
ca90659… stephan 3127
ca90659… stephan 3128 if( iEdit>0 ){
ca90659… stephan 3129 int cmp;
ca90659… stephan 3130 pPost = manifest_get(iEdit, CFTYPE_FORUM, 0);
ca90659… stephan 3131 if( pPost==0 ){
ca90659… stephan 3132 rc = -ajax_route_error(404, "Missing edit artifact %d", iEdit);
ca90659… stephan 3133 goto post_ajax_end;
ca90659… stephan 3134 }
ca90659… stephan 3135 /*
ca90659… stephan 3136 ** If the old content matches the new then do not save a new copy.
ca90659… stephan 3137 ** It's easy to get re-posts of unedited content via the forum
ca90659… stephan 3138 ** editor, especially since the one added in 2026-06, where a
ca90659… stephan 3139 ** post's status and attachments may be amended from the editor
ca90659… stephan 3140 ** without modifying any of the post's content. In the legacy
ca90659… stephan 3141 ** editor such "out-of-band" changes weren't possible and users
ca90659… stephan 3142 ** have never made a practice of re-posting unedited content.
ca90659… stephan 3143 **
ca90659… stephan 3144 ** We compare the following fields to the original: user, mimetype,
ca90659… stephan 3145 ** content, and (for root posts only) the title.
ca90659… stephan 3146 */
ca90659… stephan 3147 cmp = (0==pPost->zInReplyTo)
ca90659… stephan 3148 ? fossil_strcmp(pPost->zThreadTitle, zTitle)
ca90659… stephan 3149 : 0;
ca90659… stephan 3150 if( 0==cmp ){
ca90659… stephan 3151 cmp=fossil_strcmp(pPost->zWiki, zContent);
ca90659… stephan 3152 if( 0==cmp ){
ca90659… stephan 3153 cmp = fossil_strcmp(pPost->zUser, zUser);
ca90659… stephan 3154 }
ca90659… stephan 3155 if( 0==cmp
ca90659… stephan 3156 && 0!=(cmp=fossil_strcmp(pPost->zMimetype, zMimetype)) ){
ca90659… stephan 3157 /* Extra mimetype checks for a common condition seen elsewhere */
ca90659… stephan 3158 if( (0==zMimetype
ca90659… stephan 3159 && 0==fossil_strcmp(pPost->zMimetype, "text/x-fossil-wiki"))
ca90659… stephan 3160 || (0==pPost->zMimetype
ca90659… stephan 3161 && 0==fossil_strcmp(zMimetype, "text/x-fossil-wiki")) ){
ca90659… stephan 3162 cmp = 0;
ca90659… stephan 3163 }
ca90659… stephan 3164 }
ca90659… stephan 3165 if( 0==cmp ){
ca90659… stephan 3166 rc = iEdit;
ca90659… stephan 3167 goto post_ajax_end;
ca90659… stephan 3168 }
ca90659… stephan 3169 }
ca90659… stephan 3170 zP = rid_to_uuid(iEdit);
ca90659… stephan 3171 }
ca90659… stephan 3172
ca90659… stephan 3173 /* Write the new artifact */
ca90659… stephan 3174 blob_init(&x, 0, 0);
ca90659… stephan 3175 blob_appendf(&x, "D %z\n", date_in_standard_format("now"));
ca90659… stephan 3176 zG = db_text(
ca90659… stephan 3177 0,
ca90659… stephan 3178 "SELECT uuid FROM blob, forumpost"
ca90659… stephan 3179 " WHERE blob.rid==forumpost.froot"
ca90659… stephan 3180 " AND forumpost.fpid=%d",
ca90659… stephan 3181 iBasis
ca90659… stephan 3182 );
ca90659… stephan 3183 if( zG ){
ca90659… stephan 3184 blob_appendf(&x, "G %z\n", zG);
ca90659… stephan 3185 }
ca90659… stephan 3186 if( zTitle ){
ca90659… stephan 3187 blob_appendf(&x, "H %F\n", zTitle);
ca90659… stephan 3188 }
ca90659… stephan 3189 if( iInReplyTo>0 ){
ca90659… stephan 3190 zI = rid_to_uuid(iInReplyTo);
ca90659… stephan 3191 if( 0==zI ){
ca90659… stephan 3192 rc = -ajax_route_error(404, "Missing in-reply-to artifact %d",
ca90659… stephan 3193 iInReplyTo);
ca90659… stephan 3194 goto post_ajax_end;
ca90659… stephan 3195 }
ca90659… stephan 3196 blob_appendf(&x, "I %z\n", zI);
ca90659… stephan 3197 }
ca90659… stephan 3198 if( zMimetype!=0
ca90659… stephan 3199 && fossil_strcmp(zMimetype,"text/x-fossil-wiki")!=0 ){
ca90659… stephan 3200 blob_appendf(&x, "N %F\n", zMimetype);
ca90659… stephan 3201 }
ca90659… stephan 3202 if( zP ){
ca90659… stephan 3203 blob_appendf(&x, "P %s\n", zP);
ca90659… stephan 3204 }
ca90659… stephan 3205
ca90659… stephan 3206 blob_appendf(&x, "U %F\n", zUser);
ca90659… stephan 3207 blob_appendf(&x, "W %d\n%s\n", nContent, zContent);
ca90659… stephan 3208 md5sum_blob(&x, &cksum);
ca90659… stephan 3209 blob_appendf(&x, "Z %b\n", &cksum);
ca90659… stephan 3210 blob_reset(&cksum);
ca90659… stephan 3211
ca90659… stephan 3212 /* Verify that the artifact we are creating is well-formed */
ca90659… stephan 3213 blob_init(&formatCheck, 0, 0);
ca90659… stephan 3214 blob_init(&errMsg, 0, 0);
ca90659… stephan 3215 blob_copy(&formatCheck, &x);
ca90659… stephan 3216 pPost = manifest_parse(&formatCheck, 0, &errMsg);
ca90659… stephan 3217 if( pPost==0 ){
ca90659… stephan 3218 ajax_route_error(500, "Malformed forum post artifact: %b", &errMsg);
ca90659… stephan 3219 rc = -500;
ca90659… stephan 3220 goto post_ajax_end;
ca90659… stephan 3221 }
ca90659… stephan 3222 webpage_assert( pPost->type==CFTYPE_FORUM );
ca90659… stephan 3223
ca90659… stephan 3224 if( (iFlags & FPOST_DRYRUN)!=0 ){
ca90659… stephan 3225 rc = 0;
ca90659… stephan 3226 }else{
ca90659… stephan 3227 int nrid;
ca90659… stephan 3228 db_begin_transaction();
ca90659… stephan 3229 nrid = wiki_put(&x, iEdit>0 ? iEdit : 0, forum_need_moderation());
ca90659… stephan 3230 blob_reset(&x);
ca90659… stephan 3231 if( (iFlags & FPOST_NO_ALERT)!=0 ){
ca90659… stephan 3232 alert_unqueue('f', nrid);
ca90659… stephan 3233 }
ca90659… stephan 3234 rc = nrid;
ca90659… stephan 3235 db_end_transaction(0);
ca90659… stephan 3236 }
ca90659… stephan 3237 post_ajax_end:
ca90659… stephan 3238 manifest_destroy(pPost);
ca90659… stephan 3239 fossil_free(zP);
ca90659… stephan 3240 blob_reset(&x);
ca90659… stephan 3241 blob_reset(&cksum);
ca90659… stephan 3242 blob_reset(&formatCheck);
ca90659… stephan 3243 return rc;
ca90659… stephan 3244 }
ca90659… stephan 3245 /*
ca90659… stephan 3246 ** WEBPAGE: forumajax_save hidden
ca90659… stephan 3247 **
ca90659… stephan 3248 ** WIP
ca90659… stephan 3249 **
ca90659… stephan 3250 ** Response JSON:
ca90659… stephan 3251 **
ca90659… stephan 3252 ** { uuid: hash, ...tbd }
ca90659… stephan 3253 */
ca90659… stephan 3254 void forum_ajax_save_page(void){
ca90659… stephan 3255 const char *zFpid;
ca90659… stephan 3256 const char *zTitle;
ca90659… stephan 3257 const char *zIrt;
ca90659… stephan 3258 const char *zMimetype;
ca90659… stephan 3259 const char *zContent;
ca90659… stephan 3260 const char *zStatus;
ca90659… stephan 3261 const int bHasAttachment = P("file1")!=0;
ca90659… stephan 3262 Manifest *pPost = 0;
ca90659… stephan 3263 char *zNewUuid = 0;
ca90659… stephan 3264 int firt = 0; /* In-reply-to rid or 0 */
ca90659… stephan 3265 int fpid = 0; /* Post rid being edited or 0 */
ca90659… stephan 3266 int rc = 0; /* Result code. */
ca90659… stephan 3267 int nrid = 0; /* New artifact rid. */
ca90659… stephan 3268 int iPostFlags; /* forum_post_flags() (after perms check) */
ca90659… stephan 3269 int bRollback; /* True = roll back. */
ca90659… stephan 3270 int nAttach = 0; /* Number of attachments added */
ca90659… stephan 3271 int bStatusSet = 0; /* True if status tag set. */
ca90659… stephan 3272
ca90659… stephan 3273 if( !ajax_route_bootstrap(0, 1) ){
ca90659… stephan 3274 return;
ca90659… stephan 3275 }else if( !g.perm.WrForum
ca90659… stephan 3276 || (bHasAttachment && !g.perm.AttachForum) ){
ca90659… stephan 3277 ajax_route_error_forbidden();
ca90659… stephan 3278 return;
ca90659… stephan 3279 }else if( !ajax_check_csrf(2) ){
ca90659… stephan 3280 ajax_route_error_csrf();
ca90659… stephan 3281 return;
ca90659… stephan 3282 }
ca90659… stephan 3283
ca90659… stephan 3284 iPostFlags = forum_post_flags(/*must come after permissions init*/);
ca90659… stephan 3285 bRollback = (FPOST_DRYRUN & iPostFlags);
ca90659… stephan 3286 zFpid = P("fpid");
ca90659… stephan 3287 zIrt = P("firt");
ca90659… stephan 3288 zMimetype = P("mimetype");
ca90659… stephan 3289 zContent = P("content");
ca90659… stephan 3290 zStatus = P("status");
ca90659… stephan 3291 db_begin_transaction();
ca90659… stephan 3292 if( zFpid && zFpid[0] ){
ca90659… stephan 3293 fpid = symbolic_name_to_rid(zFpid, "f");
ca90659… stephan 3294 if( fpid<0 ){
ca90659… stephan 3295 rc = -ajax_route_error(400, "Ambiguous forum ID.");
ca90659… stephan 3296 goto ajax_save_end;
ca90659… stephan 3297 }else if( 0==fpid
ca90659… stephan 3298 || 0==(pPost = manifest_get(fpid, CFTYPE_FORUM, 0)) ){
ca90659… stephan 3299 rc = -ajax_route_error(404, "Cannot resolve forum post ID.");
ca90659… stephan 3300 goto ajax_save_end;
ca90659… stephan 3301 }
ca90659… stephan 3302 }
ca90659… stephan 3303 /*
ca90659… stephan 3304 ** Problem: if we derive firt from fpid/pPost then there's a race
ca90659… stephan 3305 ** condition where the IRT post is edited between the time that this
ca90659… stephan 3306 ** edit was initiated and when it is posted: the new edit's IRT will
ca90659… stephan 3307 ** point to the edit which was made in the meantime, not the one the
ca90659… stephan 3308 ** user intended to respond to. However, if we accept firt from the
ca90659… stephan 3309 ** enviornment, we "really should" validate that it's actually in
ca90659… stephan 3310 ** the current chain, to prohibit that malicious posts could move
ca90659… stephan 3311 ** posts around.
ca90659… stephan 3312 **
ca90659… stephan 3313 ** forum_post_ajax() will, if fpid>0 && !firt, select fpid's current
ca90659… stephan 3314 ** firt.
ca90659… stephan 3315 */
ca90659… stephan 3316 if( zIrt && zIrt[0] ){
ca90659… stephan 3317 firt = symbolic_name_to_rid(zIrt, "f");
ca90659… stephan 3318 if( firt<0 ){
ca90659… stephan 3319 rc = -ajax_route_error(400, "Ambiguous in-reply-do ID.");
ca90659… stephan 3320 goto ajax_save_end;
ca90659… stephan 3321 }else if( 0==firt ){
ca90659… stephan 3322 rc = -ajax_route_error(404, "Cannot resolve in-reply-do ID.");
ca90659… stephan 3323 goto ajax_save_end;
ca90659… stephan 3324 }
ca90659… stephan 3325 }
ca90659… stephan 3326
ca90659… stephan 3327 if( 0 ){
ca90659… stephan 3328 rc = -ajax_route_error(400, "Save is TODO. "
ca90659… stephan 3329 "iPostFlags=%d debug=%d",
ca90659… stephan 3330 iPostFlags, g.perm.Debug);
ca90659… stephan 3331 goto ajax_save_end;
ca90659… stephan 3332 }
ca90659… stephan 3333
ca90659… stephan 3334 zTitle = firt ? 0 : P("title");
ca90659… stephan 3335 nrid = forum_post_ajax(zTitle, firt, fpid, 0, zMimetype,
ca90659… stephan 3336 zContent, iPostFlags);
ca90659… stephan 3337 if( nrid<0 ){
ca90659… stephan 3338 rc = nrid;
ca90659… stephan 3339 goto ajax_save_end;
ca90659… stephan 3340 }else if( nrid==0 ){
ca90659… stephan 3341 if( 0==(FPOST_DRYRUN & iPostFlags) ){
ca90659… stephan 3342 bRollback = 1;
ca90659… stephan 3343 CX("{\"message\": \"No saving needed.\"}\n");
ca90659… stephan 3344 }else{
ca90659… stephan 3345 CX("{\"message\": \"Rolled back for dry-run.\","
ca90659… stephan 3346 "\"iPostFlags\":%d}\n", iPostFlags);
ca90659… stephan 3347 }
ca90659… stephan 3348 goto ajax_save_end;
ca90659… stephan 3349 }else{
ca90659… stephan 3350 const int bNeedsModeration = forum_need_moderation();
ca90659… stephan 3351 const int fpHead = forumpost_head_rid(nrid);
ca90659… stephan 3352 assert( nrid>0 );
ca90659… stephan 3353 assert( fpHead>0 );
ca90659… stephan 3354 zNewUuid = rid_to_uuid(nrid);
ca90659… stephan 3355 if( 0!=P("file1") ){
ca90659… stephan 3356 /* Attachments */
ca90659… stephan 3357 if( !g.perm.Admin && !g.perm.AttachForum ){
ca90659… stephan 3358 rc = -ajax_route_error(403, "No permission no attach files.");
ca90659… stephan 3359 goto ajax_save_end;
ca90659… stephan 3360 }else{
ca90659… stephan 3361 char *zRoot = (nrid==fpHead) ? 0 : rid_to_uuid(fpHead);
ca90659… stephan 3362 nAttach = attachments_ajax_from_POST(zRoot ? zRoot : zNewUuid,
ca90659… stephan 3363 bNeedsModeration);
ca90659… stephan 3364 fossil_free(zRoot);
ca90659… stephan 3365 if( nAttach<0 ){
ca90659… stephan 3366 rc = nAttach;
ca90659… stephan 3367 goto ajax_save_end;
ca90659… stephan 3368 }
ca90659… stephan 3369 if( nAttach>0
ca90659… stephan 3370 && (iPostFlags & FPOST_NO_ALERT)!=0
ca90659… stephan 3371 && db_table_exists("repository","pending_alert") ){
ca90659… stephan 3372 /* Unqueue any alerts for these attachments. Recall that
ca90659… stephan 3373 ** they're attached to the first version of the post, which
ca90659… stephan 3374 ** means we actually risk cancelling _other_ pending
ca90659… stephan 3375 ** notifications for attachments on this same post. C'est la
ca90659… stephan 3376 ** vie.*/
ca90659… stephan 3377 db_multi_exec(
ca90659… stephan 3378 "WITH x(id) AS (\n"
ca90659… stephan 3379 " SELECT 'f%d'\n"
ca90659… stephan 3380 " UNION ALL\n"
ca90659… stephan 3381 " SELECT 'f'||a.attachid FROM blob b, attachment a\n"
ca90659… stephan 3382 " WHERE b.rid=%d\n"
ca90659… stephan 3383 " AND b.uuid=a.target\n"
ca90659… stephan 3384 ") DELETE FROM pending_alert WHERE eventid IN x",
ca90659… stephan 3385 fpHead, fpHead
ca90659… stephan 3386 );
ca90659… stephan 3387 }
ca90659… stephan 3388 }
ca90659… stephan 3389 }
ca90659… stephan 3390 if( 0==bNeedsModeration
ca90659… stephan 3391 /* ^^^ Do not allow a status tag on a pending-moderation post
ca90659… stephan 3392 ** because it will introduce a reference to an artifact which
ca90659… stephan 3393 ** will become a phantom if it is rejected by a moderator. */
ca90659… stephan 3394 && zStatus!=0 && zStatus[0]!=0
ca90659… stephan 3395 && forum_may_set_status(nrid)
ca90659… stephan 3396 && (bStatusSet=forumpost_tag(nrid, 1, "status", zStatus))<0 ){
ca90659… stephan 3397 rc = -ajax_route_error(500, "Tagging failed: %s", g.zErrMsg);
ca90659… stephan 3398 goto ajax_save_end;
ca90659… stephan 3399 }
ca90659… stephan 3400 }
ca90659… stephan 3401
ca90659… stephan 3402 assert( 0==rc );
ca90659… stephan 3403 assert( zNewUuid );
ca90659… stephan 3404 CX("{\"uuid\": %!j, \"attachedCount\": %d, "
ca90659… stephan 3405 "\"statusModified\": %d, "
ca90659… stephan 3406 "\"dryrun\": %s, \"iPostFlags\":%d}\n",
ca90659… stephan 3407 zNewUuid, nAttach,
ca90659… stephan 3408 bStatusSet, bRollback ? "true" : "false", iPostFlags);
ca90659… stephan 3409
ca90659… stephan 3410 ajax_save_end:
ca90659… stephan 3411 manifest_destroy(pPost);
ca90659… stephan 3412 fossil_free(zNewUuid);
ca90659… stephan 3413 db_end_transaction(rc || bRollback);
8bdb8bc… drh 3414 }
8bdb8bc… drh 3415
8bdb8bc… drh 3416 /*
8bdb8bc… drh 3417 ** COMMAND: forum
8bdb8bc… drh 3418 **
8bdb8bc… drh 3419 ** Usage: %fossil forum SUBCOMMAND ...
8bdb8bc… drh 3420 **
8bdb8bc… drh 3421 ** Command-line access to the forum. Subcommands:
8bdb8bc… drh 3422 **
8bdb8bc… drh 3423 ** > fossil forum new --title TITLE --content FILE ?OPTIONS?
8bdb8bc… drh 3424 **
8e94450… drh 3425 ** Create a new forum thread. The text of the initial post is
8e94450… drh 3426 ** read from FILE, or from standard input if FILE is "-".
8bdb8bc… drh 3427 **
8e94450… drh 3428 ** Options:
8e94450… drh 3429 ** --content FILE Read the post content from FILE. Required.
8e94450… drh 3430 ** --date-override DATE Set the date and time of the post using
8e94450… drh 3431 ** the format 'YYYY-MM-DD HH:MM:SS'.
8e94450… drh 3432 ** --dry-run Do not commit. Just print the artifact.
8e94450… drh 3433 ** --markup FORMAT The markup format of the content. FORMAT
8bdb8bc… drh 3434 ** is one of "markdown" (the default), "wiki",
8bdb8bc… drh 3435 ** or "plaintext".
8e94450… drh 3436 ** --moderate Hold the new post pending moderation
8e94450… drh 3437 ** --title TITLE Title of the new thread. Required.
8e94450… drh 3438 ** --user-override USER Post as USER instead of the default user
8bdb8bc… drh 3439 */
8bdb8bc… drh 3440 void forum_cmd(void){
8bdb8bc… drh 3441 const char *zCmd;
8bdb8bc… drh 3442 size_t n;
8bdb8bc… drh 3443 if( g.argc<3 ){
8bdb8bc… drh 3444 usage("new ...");
8bdb8bc… drh 3445 }
8bdb8bc… drh 3446 zCmd = g.argv[2];
8bdb8bc… drh 3447 n = strlen(zCmd);
8bdb8bc… drh 3448 if( strncmp(zCmd, "new", n)==0 ){
8bdb8bc… drh 3449 const char *zTitle = find_option("title", 0, 1);
8bdb8bc… drh 3450 const char *zMarkup = find_option("markup", 0, 1);
8bdb8bc… drh 3451 const char *zFile = find_option("content", 0, 1);
8e94450… drh 3452 const char *zDateOvrd = find_option("date-override", 0, 1);
8bdb8bc… drh 3453 const int bModerate = find_option("moderate", 0, 0)!=0;
8bdb8bc… drh 3454 const int bDryRun = find_option("dry-run", 0, 0)!=0;
8bdb8bc… drh 3455 const char *zMimetype;
8e94450… drh 3456 const char *zUserOvrd = find_option("user-override",0,1);
8bdb8bc… drh 3457 char *zDate;
8bdb8bc… drh 3458 char *zUuid;
8bdb8bc… drh 3459 Blob content, x, cksum, formatCheck, errMsg;
8bdb8bc… drh 3460 Manifest *pPost;
8bdb8bc… drh 3461 int nrid;
8bdb8bc… drh 3462
8bdb8bc… drh 3463 db_find_and_open_repository(0, 0);
8bdb8bc… drh 3464 verify_all_options();
8bdb8bc… drh 3465 if( g.argc!=3 ){
8e94450… drh 3466 usage("new --title TITLE --content FILE [OPTIONS]");
8bdb8bc… drh 3467 }
8bdb8bc… drh 3468 if( zTitle==0 || zTitle[0]==0 ){
8bdb8bc… drh 3469 fossil_fatal("the --title option is required");
8bdb8bc… drh 3470 }
8bdb8bc… drh 3471 if( zFile==0 ){
8bdb8bc… drh 3472 fossil_fatal("the --content option is required");
8bdb8bc… drh 3473 }
8bdb8bc… drh 3474 if( zMarkup==0 || fossil_strcmp(zMarkup,"markdown")==0 ){
8bdb8bc… drh 3475 zMimetype = "text/x-markdown";
8bdb8bc… drh 3476 }else if( fossil_strcmp(zMarkup,"wiki")==0 ){
8bdb8bc… drh 3477 zMimetype = 0;
8bdb8bc… drh 3478 }else if( fossil_strcmp(zMarkup,"plaintext")==0 ){
8bdb8bc… drh 3479 zMimetype = "text/plain";
8bdb8bc… drh 3480 }else{
8bdb8bc… drh 3481 fossil_fatal("unknown --markup \"%s\". Should be one of: "
8bdb8bc… drh 3482 "markdown, wiki, plaintext", zMarkup);
8bdb8bc… drh 3483 }
8bdb8bc… drh 3484 if( fossil_strcmp(zFile,"-")==0 ){
8bdb8bc… drh 3485 blob_read_from_channel(&content, stdin, -1);
8bdb8bc… drh 3486 }else{
8bdb8bc… drh 3487 blob_read_from_file(&content, zFile, ExtFILE);
8bdb8bc… drh 3488 }
8bdb8bc… drh 3489 if( fossil_all_whitespace(blob_str(&content)) ){
8bdb8bc… drh 3490 fossil_fatal("forum post content is empty");
8bdb8bc… drh 3491 }
8bdb8bc… drh 3492 user_select();
8e94450… drh 3493 if( zUserOvrd==0 ) zUserOvrd = login_name();
8bdb8bc… drh 3494
8bdb8bc… drh 3495 schema_forum();
8bdb8bc… drh 3496 blob_init(&x, 0, 0);
8e94450… drh 3497 zDate = date_in_standard_format(zDateOvrd ? zDateOvrd : "now");
8bdb8bc… drh 3498 blob_appendf(&x, "D %s\n", zDate);
8bdb8bc… drh 3499 fossil_free(zDate);
8bdb8bc… drh 3500 blob_appendf(&x, "H %F\n", zTitle);
8bdb8bc… drh 3501 if( zMimetype ){
8bdb8bc… drh 3502 blob_appendf(&x, "N %F\n", zMimetype);
8bdb8bc… drh 3503 }
8e94450… drh 3504 blob_appendf(&x, "U %F\n", zUserOvrd);
8bdb8bc… drh 3505 blob_appendf(&x, "W %d\n%s\n", blob_size(&content), blob_str(&content));
8bdb8bc… drh 3506 blob_reset(&content);
8bdb8bc… drh 3507 md5sum_blob(&x, &cksum);
8bdb8bc… drh 3508 blob_appendf(&x, "Z %b\n", &cksum);
8bdb8bc… drh 3509 blob_reset(&cksum);
8bdb8bc… drh 3510
8bdb8bc… drh 3511 /* Verify that the artifact we are creating is well-formed */
8bdb8bc… drh 3512 blob_init(&formatCheck, 0, 0);
8bdb8bc… drh 3513 blob_init(&errMsg, 0, 0);
8bdb8bc… drh 3514 blob_copy(&formatCheck, &x);
8bdb8bc… drh 3515 pPost = manifest_parse(&formatCheck, 0, &errMsg);
8bdb8bc… drh 3516 if( pPost==0 ){
8bdb8bc… drh 3517 fossil_fatal("malformed forum post artifact - %s", blob_str(&errMsg));
8bdb8bc… drh 3518 }
8bdb8bc… drh 3519 if( pPost->type!=CFTYPE_FORUM ){
8bdb8bc… drh 3520 fossil_fatal("artifact is not a forum post");
8bdb8bc… drh 3521 }
8bdb8bc… drh 3522 manifest_destroy(pPost);
8bdb8bc… drh 3523
8bdb8bc… drh 3524 if( bDryRun ){
8bdb8bc… drh 3525 fossil_print("%s", blob_str(&x));
8bdb8bc… drh 3526 }else{
8bdb8bc… drh 3527 db_begin_transaction();
8bdb8bc… drh 3528 nrid = wiki_put(&x, 0, bModerate);
8bdb8bc… drh 3529 db_end_transaction(0);
8bdb8bc… drh 3530 zUuid = rid_to_uuid(nrid);
8bdb8bc… drh 3531 fossil_print("New forum thread %S%s\n", zUuid,
8bdb8bc… drh 3532 bModerate ? " (pending moderation)" : "");
8bdb8bc… drh 3533 fossil_free(zUuid);
8bdb8bc… drh 3534 }
8bdb8bc… drh 3535 blob_reset(&x);
8bdb8bc… drh 3536 }else{
8bdb8bc… drh 3537 usage("new ...");
8bdb8bc… drh 3538 }
9a2e5f4… drh 3539 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button