Fossil SCM

fossil-scm / src / attach.c
Source Blame History 1859 lines
7c2577b… drh 1 /*
c19f34c… drh 2 ** Copyright (c) 2010 D. Richard Hipp
7c2577b… drh 3 **
7c2577b… drh 4 ** This program is free software; you can redistribute it and/or
c06edd2… drh 5 ** modify it under the terms of the Simplified BSD License (also
c06edd2… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
fd9b7bd… drh 7 **
7c2577b… drh 8 ** This program is distributed in the hope that it will be useful,
c06edd2… drh 9 ** but without any warranty; without even the implied warranty of
c06edd2… drh 10 ** merchantability or fitness for a particular purpose.
7c2577b… drh 11 **
7c2577b… drh 12 ** Author contact information:
7c2577b… drh 13 ** [email protected]
7c2577b… drh 14 ** http://www.hwaci.com/drh/
7c2577b… drh 15 **
7c2577b… drh 16 *******************************************************************************
7c2577b… drh 17 **
7c2577b… drh 18 ** This file contains code for dealing with attachments.
7c2577b… drh 19 */
7c2577b… drh 20 #include "config.h"
7c2577b… drh 21 #include "attach.h"
7c2577b… drh 22 #include <assert.h>
7c2577b… drh 23
7c2577b… drh 24 /*
5079ffb… stephan 25 ** Given a presumedly legal attachment target name, this guesses the
5079ffb… stephan 26 ** target type and returns one of CFTYPE_FORUM, CFTYPE_WIKI,
ca90659… stephan 27 ** CFTYPE_TICKET, or CFTYPE_EVENT. Returns 0 if it cannot distinguish
ca90659… stephan 28 ** the target type.
ca90659… stephan 29 **
ca90659… stephan 30 ** zTarget is an attachment target name: wiki page name, tech-note ID,
ca90659… stephan 31 ** ticket ID, or forumpost hash.
ca90659… stephan 32 **
ca90659… stephan 33 ** If bFull is true then it requires zTarget to be a full ID for
ca90659… stephan 34 ** tech-notes and tickets, otherwise such IDs may be prefixes. If
ca90659… stephan 35 ** bFull is false then tech-notes and tickets will perform a prefix
ca90659… stephan 36 ** match, but it is up to the caller to provide enough of a prefix to
ca90659… stephan 37 ** rule out ambiguity[^1]. When called repeatedly, this routine can
ca90659… stephan 38 ** run a bit faster and more efficiently if bFull is true, but some
ca90659… stephan 39 ** historical use cases call for prefix matches.
ca90659… stephan 40 **
ca90659… stephan 41 ** Wiki page names always require an exact match.
ca90659… stephan 42 **
ca90659… stephan 43 ** Forum posts are a special case:
ca90659… stephan 44 **
ca90659… stephan 45 ** - They ignore the bFull flag. That is, they will do prefix matches
ca90659… stephan 46 ** but will not match an ambiguous prefix.
ca90659… stephan 47 **
ca90659… stephan 48 ** - It is up to the caller to, if needed, resolve zTarget using
ca90659… stephan 49 ** forumpost_head_rid2() to resolve the RID of the earliest version
ca90659… stephan 50 ** of the post, as that is the only one which attachments should
ca90659… stephan 51 ** target.
5079ffb… stephan 52 **
ca90659… stephan 53 ** [^1]: Historically (from the perspective of 2026-06) attachment
ca90659… stephan 54 ** target lookups have used GLOB prefix matching but have taken no
ca90659… stephan 55 ** measures to ensure that the prefix is unambiguous. Ergo we do the
ca90659… stephan 56 ** same here. It is assumed that the caller passes enough of a prefix
ca90659… stephan 57 ** to be unambiguous and that's worked out fine so far.
5079ffb… stephan 58 */
ca90659… stephan 59 int attachment_target_type(const char *zTarget, int bFull){
ca90659… stephan 60 if( !zTarget || !zTarget[0] || strlen(zTarget)>64/*vs. abuse*/ ){
ca90659… stephan 61 return 0;
ca90659… stephan 62 }
ca90659… stephan 63 if( symbolic_name_to_rid(zTarget, "f")>0 ){
ca90659… stephan 64 /* Check forum posts first because they are the most likely target
ca90659… stephan 65 ** as of 2026. We should arguably use something more
ca90659… stephan 66 ** specialized/efficient than symbolic_name_to_rid(). */
5079ffb… stephan 67 return CFTYPE_FORUM;
5079ffb… stephan 68 }
ca90659… stephan 69 if( bFull ){
ca90659… stephan 70 static Stmt q = empty_Stmt_m;
ca90659… stephan 71 int rc = 0;
ca90659… stephan 72 if( !q.pStmt ){
ca90659… stephan 73 db_static_prepare(
ca90659… stephan 74 &q,
ca90659… stephan 75 "SELECT CASE "
ca90659… stephan 76 /* Ordered by presumed likelihood of attachments. */
ca90659… stephan 77 "WHEN (SELECT 1 FROM tag WHERE tagname='tkt-'||:tgt) THEN %d\n"
ca90659… stephan 78 "WHEN (SELECT 1 FROM tag WHERE tagname='wiki-'||:tgt) THEN %d\n"
ca90659… stephan 79 "WHEN (SELECT 1 FROM tag WHERE tagname='event-'||:tgt) THEN %d\n"
ca90659… stephan 80 "ELSE 0 END",
ca90659… stephan 81 CFTYPE_TICKET, CFTYPE_WIKI, CFTYPE_EVENT
ca90659… stephan 82 );
ca90659… stephan 83 }
ca90659… stephan 84 db_bind_text(&q, ":tgt", zTarget);
ca90659… stephan 85 if( SQLITE_ROW==db_step(&q) ){
ca90659… stephan 86 rc = db_column_int(&q, 0);
ca90659… stephan 87 }
ca90659… stephan 88 db_reset(&q);
ca90659… stephan 89 return rc;
ca90659… stephan 90 }else{
ca90659… stephan 91 return db_int(
ca90659… stephan 92 0,
5079ffb… stephan 93 "SELECT CASE "
ca90659… stephan 94 "WHEN (SELECT tagid FROM tag WHERE tagname GLOB 'tkt-%q*')"
ca90659… stephan 95 " THEN %d\n"
ca90659… stephan 96 "WHEN (SELECT tagid FROM tag WHERE tagname='wiki-%q')"
ca90659… stephan 97 " THEN %d\n"
ca90659… stephan 98 "WHEN (SELECT tagid FROM tag WHERE tagname GLOB 'event-%q*')"
ca90659… stephan 99 " THEN %d\n"
5079ffb… stephan 100 "ELSE 0 END",
ca90659… stephan 101 zTarget, CFTYPE_TICKET,
ca90659… stephan 102 zTarget, CFTYPE_WIKI,
ca90659… stephan 103 zTarget, CFTYPE_EVENT
5079ffb… stephan 104 );
5079ffb… stephan 105 }
ca90659… stephan 106 }
ca90659… stephan 107
ca90659… stephan 108 /*
ca90659… stephan 109 ** Given an attachment target name, returns the target's blob.rid.
ca90659… stephan 110 ** zTarget and bFull work as described for attachment_target_type().
ca90659… stephan 111 **
ca90659… stephan 112 ** For forum posts, this always returns the RID of the first version
ca90659… stephan 113 ** of the post, as attachments should always target that instance.
ca90659… stephan 114 */
ca90659… stephan 115 int attachment_target_rid(const char *zTarget, int bFull){
ca90659… stephan 116 int rid = 0;
ca90659… stephan 117 const int eType = attachment_target_type(zTarget, bFull);
ca90659… stephan 118 switch(eType){
ca90659… stephan 119 case CFTYPE_TICKET:
ca90659… stephan 120 case CFTYPE_EVENT:{
ca90659… stephan 121 const char *zTagPrefix = (eType==CFTYPE_EVENT) ? "event" : "tkt";
ca90659… stephan 122 rid = db_int(
ca90659… stephan 123 0, "SELECT b.rid FROM blob b, tag t, tagxref x\n"
ca90659… stephan 124 "WHERE tagname %s '%s-%q%s'\n"
ca90659… stephan 125 "AND x.tagtype>0\n"
ca90659… stephan 126 "AND x.tagid=t.tagid\n"
ca90659… stephan 127 "AND x.rid=b.rid\n"
ca90659… stephan 128 "ORDER BY x.mtime DESC",
ca90659… stephan 129 bFull ? "=" : "GLOB"/*safe-for-%s*/,
ca90659… stephan 130 zTagPrefix/*safe-for-%s*/,
ca90659… stephan 131 zTarget,
ca90659… stephan 132 bFull ? "" : "*"/*safe-for-%s*/
ca90659… stephan 133 );
ca90659… stephan 134 break;
ca90659… stephan 135 }
ca90659… stephan 136 case CFTYPE_FORUM:
ca90659… stephan 137 rid = db_int(
ca90659… stephan 138 0, "SELECT f.fpid FROM forumpost f, blob b\n"
ca90659… stephan 139 "WHERE f.fpid=b.rid\n"
ca90659… stephan 140 "AND b.uuid %s '%q%s'",
ca90659… stephan 141 bFull ? "=" : "GLOB"/*safe-for-%s*/,
ca90659… stephan 142 zTarget,
ca90659… stephan 143 bFull ? "" : "*"/*safe-for-%s*/
ca90659… stephan 144 );
ca90659… stephan 145 if( rid>0 ){
ca90659… stephan 146 rid = forumpost_head_rid(rid);
ca90659… stephan 147 }
ca90659… stephan 148 break;
ca90659… stephan 149 case CFTYPE_WIKI:
ca90659… stephan 150 rid = db_int(
ca90659… stephan 151 0, "SELECT b.rid FROM blob b, tag t, tagxref x\n"
ca90659… stephan 152 "WHERE tagname='wiki-%q'\n"
ca90659… stephan 153 "AND x.tagtype>0\n"
ca90659… stephan 154 "AND x.tagid=t.tagid\n"
ca90659… stephan 155 "AND x.rid=b.rid\n"
ca90659… stephan 156 "ORDER BY x.mtime DESC",
ca90659… stephan 157 zTarget
ca90659… stephan 158 );
ca90659… stephan 159 break;
ca90659… stephan 160 default:
ca90659… stephan 161 break;
ca90659… stephan 162 }
ca90659… stephan 163 return rid;
ca90659… stephan 164 }
ca90659… stephan 165
ca90659… stephan 166 /*
ca90659… stephan 167 ** For a given aritfact ID and type (from the CFTYPE_xyz enum),
ca90659… stephan 168 ** returns true if the current user could hypothetically apply and
ca90659… stephan 169 ** attachment to it, else returns 0.
ca90659… stephan 170 **
ca90659… stephan 171 ** The rid is currently only relevant when eArtifactType is
ca90659… stephan 172 ** CFTYPE_FORUM. For forum posts, it checks precisely the rid given,
ca90659… stephan 173 ** not the head RID, to keep non-admins from attaching files to
ca90659… stephan 174 ** threads which have since been taken over by another user (this
ca90659… stephan 175 ** happens when an admin edits another user's post).
ca90659… stephan 176 */
ca90659… stephan 177 int attach_user_may(int rid, int eArtifactType){
ca90659… stephan 178 if( g.perm.Admin ) return 1;
ca90659… stephan 179 if( !login_is_individual() ) return 0;
ca90659… stephan 180 switch(eArtifactType){
ca90659… stephan 181 case CFTYPE_FORUM:
ca90659… stephan 182 return g.perm.AttachForum && forumpost_is_owner(rid, 0);
ca90659… stephan 183 case CFTYPE_WIKI:
ca90659… stephan 184 return g.perm.ApndWiki && g.perm.Attach;
ca90659… stephan 185 case CFTYPE_TICKET:
ca90659… stephan 186 return g.perm.ApndTkt && g.perm.Attach;
ca90659… stephan 187 case CFTYPE_EVENT:
ca90659… stephan 188 return g.perm.Write && g.perm.ApndWiki && g.perm.Attach;
ca90659… stephan 189 default:
ca90659… stephan 190 return 0;
5079ffb… stephan 191 }
ca90659… stephan 192 }
ca90659… stephan 193
ca90659… stephan 194 /*
ca90659… stephan 195 ** Emits a single-button FORM which invokes
ca90659… stephan 196 ** /attachadd with target=$zTarget.
ca90659… stephan 197 */
ca90659… stephan 198 void attach_render_attachadd_button(const char *zTarget){
ca90659… stephan 199 /* This could be changed from POST to GET, and arguably should so
ca90659… stephan 200 ** that the target=X part becomes part of the resulting URL. */
ca90659… stephan 201 @ <form method="post" action="%R/attachadd">\
ca90659… stephan 202 @ <input type="hidden" name="target" value="%T(zTarget)">\
ca90659… stephan 203 @ <input type="submit" value="Attach...">
ca90659… stephan 204 @ </form>\
5079ffb… stephan 205 }
5079ffb… stephan 206
5079ffb… stephan 207 /*
7ab0328… drh 208 ** WEBPAGE: attachlist
7c2577b… drh 209 ** List attachments.
7ab0328… drh 210 **
8ad5e46… wyoung 211 ** tkt=HASH
7ab0328… drh 212 ** page=WIKIPAGE
8ad5e46… wyoung 213 ** technote=HASH
5079ffb… stephan 214 ** forumpost=HASH
8ad5e46… wyoung 215 **
5079ffb… stephan 216 ** At most one of technote=, tkt=, forumpost=, or page= may be supplied.
8ad5e46… wyoung 217 **
8ad5e46… wyoung 218 ** If none are given, all attachments are listed. If one is given, only
8ad5e46… wyoung 219 ** attachments for the designated technote, ticket or wiki page are shown.
7ab0328… drh 220 **
ca90659… stephan 221 ** HASH may be just a prefix of the relevant forum post, technical
ca90659… stephan 222 ** note, or ticket artifact hash, in which case all attachments of all
ca90659… stephan 223 ** technical notes or tickets with the prefix will be listed. Forum
ca90659… stephan 224 ** posts, on the other hand, require a unique hash or hash prefix.
7c2577b… drh 225 */
7c2577b… drh 226 void attachlist_page(void){
7c2577b… drh 227 const char *zPage = P("page");
7c2577b… drh 228 const char *zTkt = P("tkt");
045deb2… drh 229 const char *zTechNote = P("technote");
5079ffb… stephan 230 const char *zForumPost = P("forumpost");
ca90659… stephan 231 char *zLink = 0;
7c2577b… drh 232 Blob sql;
7c2577b… drh 233 Stmt q;
7c2577b… drh 234
7c2577b… drh 235 if( zPage && zTkt ) zTkt = 0;
7c2577b… drh 236 login_check_credentials();
112c713… drh 237 style_set_current_feature("attach");
7c2577b… drh 238 blob_zero(&sql);
49b0ff1… drh 239 blob_append_sql(&sql,
ea63a2d… drh 240 "SELECT datetime(mtime,toLocal()), src, target, filename,"
ba418ee… drh 241 " comment, user,"
5079ffb… stephan 242 " (SELECT uuid FROM blob WHERE rid=attachid), attachid"
ea63a2d… drh 243 " FROM attachment"
7c2577b… drh 244 );
5079ffb… stephan 245 if( zForumPost ){
5079ffb… stephan 246 int fnid;
5079ffb… stephan 247 if( g.perm.RdForum==0 ){ login_needed(g.anon.RdForum); return; }
5079ffb… stephan 248 style_header("Attachments To Forum post %S", zForumPost);
5079ffb… stephan 249 fnid = forumpost_head_rid2(zForumPost);
5079ffb… stephan 250 if( fnid<=0 ){
5079ffb… stephan 251 webpage_error("Invalid forum post ID: %h", zForumPost);
5079ffb… stephan 252 }
5079ffb… stephan 253 blob_append_sql(&sql, " WHERE target="
5079ffb… stephan 254 "(SELECT uuid FROM blob WHERE rid=%d)", fnid);
ca90659… stephan 255 zLink = mprintf("forum post <a href='%R/forumpost/%t'>%#h</a>",
ca90659… stephan 256 zForumPost, hash_digits(0), zForumPost);
5079ffb… stephan 257 }else if( zPage ){
653dd40… drh 258 if( g.perm.RdWiki==0 ){ login_needed(g.anon.RdWiki); return; }
5079ffb… stephan 259 style_header("Attachments To Wiki page %h", zPage);
49b0ff1… drh 260 blob_append_sql(&sql, " WHERE target=%Q", zPage);
ca90659… stephan 261 zLink = mprintf("wiki page <a href='%R/wiki?name=%t'>%h</a>",
ca90659… stephan 262 zPage, zPage);
7c2577b… drh 263 }else if( zTkt ){
653dd40… drh 264 if( g.perm.RdTkt==0 ){ login_needed(g.anon.RdTkt); return; }
275457a… jan.nijtmans 265 style_header("Attachments To Ticket %S", zTkt);
49b0ff1… drh 266 blob_append_sql(&sql, " WHERE target GLOB '%q*'", zTkt);
ca90659… stephan 267 zLink = mprintf("ticket <a href='%R/tktview?name=%t'>%#h</a>",
ca90659… stephan 268 zTkt, hash_digits(0), zTkt);
045deb2… drh 269 }else if( zTechNote ){
045deb2… drh 270 if( g.perm.RdWiki==0 ){ login_needed(g.anon.RdWiki); return; }
5079ffb… stephan 271 style_header("Attachments To Tech Note %S", zTechNote);
045deb2… drh 272 blob_append_sql(&sql, " WHERE target GLOB '%q*'",
045deb2… drh 273 zTechNote);
ca90659… stephan 274 zLink = mprintf("tech-note <a href='%R/technote?name=%t'>%#h</a>",
ca90659… stephan 275 zTechNote, hash_digits(0), zTechNote);
7c2577b… drh 276 }else{
653dd40… drh 277 if( g.perm.RdTkt==0 && g.perm.RdWiki==0 ){
653dd40… drh 278 login_needed(g.anon.RdTkt || g.anon.RdWiki);
653dd40… drh 279 return;
653dd40… drh 280 }
7c2577b… drh 281 style_header("All Attachments");
7c2577b… drh 282 }
49b0ff1… drh 283 blob_append_sql(&sql, " ORDER BY mtime DESC");
49b0ff1… drh 284 db_prepare(&q, "%s", blob_sql_text(&sql));
ca90659… stephan 285
ca90659… stephan 286 if( zLink ){
ca90659… stephan 287 @ <h2>Attachments for %s(zLink)</h2>
ca90659… stephan 288 fossil_free(zLink);
ca90659… stephan 289 zLink = 0;
ca90659… stephan 290 }
ca90659… stephan 291
ba418ee… drh 292 @ <ol>
7c2577b… drh 293 while( db_step(&q)==SQLITE_ROW ){
5079ffb… stephan 294 const char *zDate;
5079ffb… stephan 295 const char *zSrc;
5079ffb… stephan 296 const char *zTarget;
5079ffb… stephan 297 const char *zFilename;
5079ffb… stephan 298 const char *zComment;
5079ffb… stephan 299 const char *zUser;
5079ffb… stephan 300 const char *zUuid;
5079ffb… stephan 301 const char *zDispUser;
5079ffb… stephan 302 const int attachid = db_column_int(&q, 7);
5079ffb… stephan 303 int type;
7c2577b… drh 304 int i;
ca90659… stephan 305 int bDeleted;
5079ffb… stephan 306 char *zUrlTail = 0;
5079ffb… stephan 307
5079ffb… stephan 308 if( moderation_pending(attachid)
5079ffb… stephan 309 && !moderation_user_could(attachid, 1, 0) ){
5079ffb… stephan 310 /* Elide entries which are currently pending moderation unless
5079ffb… stephan 311 ** the user would be able to moderate the entry themselves. */
5079ffb… stephan 312 continue;
5079ffb… stephan 313 }
5079ffb… stephan 314
5079ffb… stephan 315 zDate = db_column_text(&q, 0);
5079ffb… stephan 316 zSrc = db_column_text(&q, 1);
5079ffb… stephan 317 zTarget = db_column_text(&q, 2);
5079ffb… stephan 318 zFilename = db_column_text(&q, 3);
5079ffb… stephan 319 zComment = db_column_text(&q, 4);
5079ffb… stephan 320 zUser = db_column_text(&q, 5);
5079ffb… stephan 321 zUuid = db_column_text(&q, 6);
5079ffb… stephan 322 zDispUser = zUser && zUser[0] ? zUser : "anonymous";
7c2577b… drh 323 for(i=0; zFilename[i]; i++){
275457a… jan.nijtmans 324 if( zFilename[i]=='/' && zFilename[i+1]!=0 ){
7c2577b… drh 325 zFilename = &zFilename[i+1];
7c2577b… drh 326 i = -1;
7c2577b… drh 327 }
7c2577b… drh 328 }
ca90659… stephan 329 bDeleted = 0==zSrc || 0==zSrc[0];
ca90659… stephan 330 type = attachment_target_type(zTarget, 1);
5079ffb… stephan 331 switch( type ){
5079ffb… stephan 332 case CFTYPE_TICKET:
5079ffb… stephan 333 zUrlTail = mprintf("tkt=%s&file=%t", zTarget, zFilename);
5079ffb… stephan 334 break;
5079ffb… stephan 335 case CFTYPE_EVENT:
5079ffb… stephan 336 zUrlTail = mprintf("technote=%s&file=%t", zTarget, zFilename);
5079ffb… stephan 337 break;
5079ffb… stephan 338 case CFTYPE_FORUM:
5079ffb… stephan 339 zUrlTail = mprintf("forumpost=%t&file=%t", zTarget, zFilename);
5079ffb… stephan 340 break;
5079ffb… stephan 341 case CFTYPE_WIKI:
5079ffb… stephan 342 zUrlTail = mprintf("page=%t&file=%t", zTarget, zFilename);
5079ffb… stephan 343 break;
ba418ee… drh 344 }
ba418ee… drh 345 @ <li><p>
ca90659… stephan 346 if( bDeleted ){
ca90659… stephan 347 @ <s>\
ca90659… stephan 348 }
ca90659… stephan 349 @ Attachment %z(href("%R/ainfo/%!S",zUuid))%S(zUuid)</a>\
99fcc43… drh 350 moderation_pending_www(attachid);
ca90659… stephan 351 @ <br>\
ca90659… stephan 352 @ <a href="%R/attachview?%s(zUrlTail)">%h(zFilename)</a>
ca90659… stephan 353 @ [<a href="%R/attachdownload/%t(zFilename)?%s(zUrlTail)">download</a>]\
ca90659… stephan 354 if( bDeleted ){
ca90659… stephan 355 @ </s>
ca90659… stephan 356 }
ca90659… stephan 357 @ <br>
2fac809… drh 358 if( zComment ) while( fossil_isspace(zComment[0]) ) zComment++;
7c2577b… drh 359 if( zComment && zComment[0] ){
ca90659… stephan 360 /* FIXME (2026-06-05): Honor the N-card (comment mimetype). %W
ca90659… stephan 361 ** (historically used here) assumes fossil-wiki and the
ca90659… stephan 362 ** fileformat.wiki doc has always claimed that it defaults to
ca90659… stephan 363 ** text/plain. /ainfo assumes it is plain text. */
ca90659… stephan 364 @ %h(zComment)<br>
7c2577b… drh 365 }
5079ffb… stephan 366 if( zForumPost==0 && zPage==0 && zTkt==0 && zTechNote==0 ){
ca90659… stephan 367 if( bDeleted ){
7c2577b… drh 368 zSrc = "Deleted from";
7c2577b… drh 369 }else {
7c2577b… drh 370 zSrc = "Added to";
7c2577b… drh 371 }
5079ffb… stephan 372 switch( type ){
5079ffb… stephan 373 case CFTYPE_TICKET:
5079ffb… stephan 374 @ %s(zSrc) ticket <a href="%R/tktview?name=%s(zTarget)">
5079ffb… stephan 375 @ %S(zTarget)</a>
5079ffb… stephan 376 break;
5079ffb… stephan 377 case CFTYPE_EVENT:
5079ffb… stephan 378 @ %s(zSrc) tech note <a href="%R/technote/%s(zTarget)">
5079ffb… stephan 379 @ %S(zTarget)</a>
5079ffb… stephan 380 break;
5079ffb… stephan 381 case CFTYPE_WIKI:
5079ffb… stephan 382 @ %s(zSrc) wiki page <a href="%R/wiki?name=%t(zTarget)">
5079ffb… stephan 383 @ %h(zTarget)</a>
5079ffb… stephan 384 break;
5079ffb… stephan 385 case CFTYPE_FORUM:
5079ffb… stephan 386 @ %s(zSrc) forum post <a href="%R/forumpost/%s(zTarget)">
5079ffb… stephan 387 @ %h(zTarget)</a>
5079ffb… stephan 388 break;
5079ffb… stephan 389 default:
5079ffb… stephan 390 @ <span class='error'>%s(zSrc) cannot determine target type
5079ffb… stephan 391 @ of %h(zTarget)</span>
5079ffb… stephan 392 break;
7c2577b… drh 393 }
7c2577b… drh 394 }else{
7c2577b… drh 395 if( zSrc==0 || zSrc[0]==0 ){
7c2577b… drh 396 @ Deleted
7c2577b… drh 397 }else {
7c2577b… drh 398 @ Added
7c2577b… drh 399 }
7c2577b… drh 400 }
1570857… drh 401 @ by %h(zDispUser) on
7c2577b… drh 402 hyperlink_to_date(zDate, ".");
7c2577b… drh 403 free(zUrlTail);
7c2577b… drh 404 }
7c2577b… drh 405 db_finalize(&q);
ba418ee… drh 406 @ </ol>
112c713… drh 407 style_finish_page();
7c2577b… drh 408 return;
7c2577b… drh 409 }
7c2577b… drh 410
7c2577b… drh 411 /*
7c2577b… drh 412 ** WEBPAGE: attachdownload
7c2577b… drh 413 ** WEBPAGE: attachimage
7c2577b… drh 414 ** WEBPAGE: attachview
7c2577b… drh 415 **
7ab0328… drh 416 ** Download or display an attachment.
8ad5e46… wyoung 417 **
7ab0328… drh 418 ** Query parameters:
7ab0328… drh 419 **
8ad5e46… wyoung 420 ** tkt=HASH
7c2577b… drh 421 ** page=WIKIPAGE
8ad5e46… wyoung 422 ** technote=HASH
5079ffb… stephan 423 ** forumpost=HASH
7c2577b… drh 424 ** file=FILENAME
7c2577b… drh 425 ** attachid=ID
7c2577b… drh 426 **
7c2577b… drh 427 */
7c2577b… drh 428 void attachview_page(void){
7c2577b… drh 429 const char *zPage = P("page");
7c2577b… drh 430 const char *zTkt = P("tkt");
045deb2… drh 431 const char *zTechNote = P("technote");
5079ffb… stephan 432 const char *zForumPost = P("forumpost");
7c2577b… drh 433 const char *zFile = P("file");
7c2577b… drh 434 const char *zTarget = 0;
7c2577b… drh 435 int attachid = atoi(PD("attachid","0"));
5079ffb… stephan 436 char *zUUID = 0;
7c2577b… drh 437
7c2577b… drh 438 if( zFile==0 ) fossil_redirect_home();
7c2577b… drh 439 login_check_credentials();
112c713… drh 440 style_set_current_feature("attach");
5079ffb… stephan 441 if( zForumPost ){
5079ffb… stephan 442 int fnid;
5079ffb… stephan 443 if( g.perm.RdForum==0 ){ login_needed(g.anon.RdForum); return; }
5079ffb… stephan 444 /* Forum attachments are always tied to the post's initial version */
5079ffb… stephan 445 fnid = forumpost_head_rid2(zForumPost);
5079ffb… stephan 446 if( fnid>0 ) zTarget = rid_to_uuid(fnid);
5079ffb… stephan 447 }else if( zPage ){
653dd40… drh 448 if( g.perm.RdWiki==0 ){ login_needed(g.anon.RdWiki); return; }
7c2577b… drh 449 zTarget = zPage;
7c2577b… drh 450 }else if( zTkt ){
653dd40… drh 451 if( g.perm.RdTkt==0 ){ login_needed(g.anon.RdTkt); return; }
7c2577b… drh 452 zTarget = zTkt;
045deb2… drh 453 }else if( zTechNote ){
045deb2… drh 454 if( g.perm.RdWiki==0 ){ login_needed(g.anon.RdWiki); return; }
045deb2… drh 455 zTarget = zTechNote;
7c2577b… drh 456 }else{
7c2577b… drh 457 fossil_redirect_home();
7c2577b… drh 458 }
7c2577b… drh 459 if( attachid>0 ){
7c2577b… drh 460 zUUID = db_text(0,
7c2577b… drh 461 "SELECT coalesce(src,'x') FROM attachment"
7c2577b… drh 462 " WHERE target=%Q AND attachid=%d",
7c2577b… drh 463 zTarget, attachid
7c2577b… drh 464 );
7c2577b… drh 465 }else{
7c2577b… drh 466 zUUID = db_text(0,
7c2577b… drh 467 "SELECT coalesce(src,'x') FROM attachment"
7c2577b… drh 468 " WHERE target=%Q AND filename=%Q"
7c2577b… drh 469 " ORDER BY mtime DESC LIMIT 1",
7c2577b… drh 470 zTarget, zFile
7c2577b… drh 471 );
7c2577b… drh 472 }
7c2577b… drh 473 if( zUUID==0 || zUUID[0]==0 ){
7c2577b… drh 474 style_header("No Such Attachment");
7c2577b… drh 475 @ No such attachment....
112c713… drh 476 style_finish_page();
7c2577b… drh 477 return;
7c2577b… drh 478 }else if( zUUID[0]=='x' ){
7c2577b… drh 479 style_header("Missing");
7c2577b… drh 480 @ Attachment has been deleted
112c713… drh 481 style_finish_page();
ba418ee… drh 482 return;
ba418ee… drh 483 }else{
045deb2… drh 484 g.perm.Read = 1;
045deb2… drh 485 cgi_replace_parameter("name",zUUID);
045deb2… drh 486 if( fossil_strcmp(g.zPath,"attachview")==0 ){
045deb2… drh 487 artifact_page();
045deb2… drh 488 }else{
045deb2… drh 489 cgi_replace_parameter("m", mimetype_from_name(zFile));
045deb2… drh 490 rawartifact_page();
045deb2… drh 491 }
ba418ee… drh 492 }
ba418ee… drh 493 }
ba418ee… drh 494
ba418ee… drh 495 /*
ba418ee… drh 496 ** Save an attachment control artifact into the repository
ba418ee… drh 497 */
ba418ee… drh 498 static void attach_put(
ba418ee… drh 499 Blob *pAttach, /* Text of the Attachment record */
ba418ee… drh 500 int attachRid, /* RID for the file that is being attached */
ba418ee… drh 501 int needMod /* True if the attachment is subject to moderation */
ba418ee… drh 502 ){
ba418ee… drh 503 int rid;
ba418ee… drh 504 if( needMod ){
ba418ee… drh 505 rid = content_put_ex(pAttach, 0, 0, 0, 1);
ba418ee… drh 506 moderation_table_create();
ba418ee… drh 507 db_multi_exec(
ba418ee… drh 508 "INSERT INTO modreq(objid,attachRid) VALUES(%d,%d);",
ba418ee… drh 509 rid, attachRid
ba418ee… drh 510 );
ba418ee… drh 511 }else{
ba418ee… drh 512 rid = content_put(pAttach);
673dc38… stephan 513 db_add_unsent(rid);
ba418ee… drh 514 db_multi_exec("INSERT OR IGNORE INTO unclustered VALUES(%d);", rid);
ba418ee… drh 515 }
1311841… jan.nijtmans 516 manifest_crosslink(rid, pAttach, MC_NONE);
ba418ee… drh 517 }
ba418ee… drh 518
ba418ee… drh 519
ba418ee… drh 520 /*
05cd9fa… rberteig 521 ** Commit a new attachment into the repository
05cd9fa… rberteig 522 */
05cd9fa… rberteig 523 void attach_commit(
05cd9fa… rberteig 524 const char *zName, /* The filename of the attachment */
8ad5e46… wyoung 525 const char *zTarget, /* The artifact hash to attach to */
05cd9fa… rberteig 526 const char *aContent, /* The content of the attachment */
05cd9fa… rberteig 527 int szContent, /* The length of the attachment */
05cd9fa… rberteig 528 int needModerator, /* Moderate the attachment? */
05cd9fa… rberteig 529 const char *zComment /* The comment for the attachment */
05cd9fa… rberteig 530 ){
05cd9fa… rberteig 531 Blob content;
05cd9fa… rberteig 532 Blob manifest;
05cd9fa… rberteig 533 Blob cksum;
05cd9fa… rberteig 534 char *zUUID;
05cd9fa… rberteig 535 char *zDate;
05cd9fa… rberteig 536 int rid;
05cd9fa… rberteig 537 int i, n;
05cd9fa… rberteig 538 int addCompress = 0;
05cd9fa… rberteig 539 Manifest *pManifest;
05cd9fa… rberteig 540
e1962ef… drh 541 db_begin_transaction();
05cd9fa… rberteig 542 blob_init(&content, aContent, szContent);
05cd9fa… rberteig 543 pManifest = manifest_parse(&content, 0, 0);
ca90659… stephan 544 addCompress = pManifest!=0;
05cd9fa… rberteig 545 manifest_destroy(pManifest);
05cd9fa… rberteig 546 blob_init(&content, aContent, szContent);
ca90659… stephan 547 if( addCompress ){
05cd9fa… rberteig 548 blob_compress(&content, &content);
05cd9fa… rberteig 549 }
05cd9fa… rberteig 550 rid = content_put_ex(&content, 0, 0, 0, needModerator);
ca90659… stephan 551 zUUID = rid_to_uuid(rid);
05cd9fa… rberteig 552 blob_zero(&manifest);
05cd9fa… rberteig 553 for(i=n=0; zName[i]; i++){
05cd9fa… rberteig 554 if( zName[i]=='/' || zName[i]=='\\' ) n = i+1;
05cd9fa… rberteig 555 }
05cd9fa… rberteig 556 zName += n;
05cd9fa… rberteig 557 if( zName[0]==0 ) zName = "unknown";
05cd9fa… rberteig 558 blob_appendf(&manifest, "A %F%s %F %s\n",
05cd9fa… rberteig 559 zName, addCompress ? ".gz" : "", zTarget, zUUID);
ca90659… stephan 560 if( zComment!=0 && zComment[0]!=0 ){
ca90659… stephan 561 while( fossil_isspace(zComment[0]) ) zComment++;
ca90659… stephan 562 n = strlen(zComment);
ca90659… stephan 563 while( n>0 && fossil_isspace(zComment[n-1]) ){ n--; }
ca90659… stephan 564 if( n>0 ){
ca90659… stephan 565 blob_appendf(&manifest, "C %#F\n", n, zComment);
ca90659… stephan 566 }
05cd9fa… rberteig 567 }
05cd9fa… rberteig 568 zDate = date_in_standard_format("now");
ca90659… stephan 569 blob_appendf(&manifest, "D %z\n", zDate);
05cd9fa… rberteig 570 blob_appendf(&manifest, "U %F\n", login_name());
05cd9fa… rberteig 571 md5sum_blob(&manifest, &cksum);
05cd9fa… rberteig 572 blob_appendf(&manifest, "Z %b\n", &cksum);
05cd9fa… rberteig 573 attach_put(&manifest, rid, needModerator);
05cd9fa… rberteig 574 assert( blob_is_reset(&manifest) );
05cd9fa… rberteig 575 db_end_transaction(0);
ca90659… stephan 576 }
ca90659… stephan 577
ca90659… stephan 578 /*
ca90659… stephan 579 ** Renders the "legacy" (static) /attachadd form. One of the first
ca90659… stephan 580 ** four arguments must be non-NULL and the other three must be NULL.
ca90659… stephan 581 ** zComment may be NULL, as may zFrom. See the call sites for more
ca90659… stephan 582 ** context.
ca90659… stephan 583 */
ca90659… stephan 584 static void attach_render_legacy_form(const char *zForumPost,
ca90659… stephan 585 const char *zTechNote,
ca90659… stephan 586 const char *zTicket,
ca90659… stephan 587 const char *zWikiPage,
ca90659… stephan 588 const char *zComment,
ca90659… stephan 589 const char *zFrom){
ca90659… stephan 590 form_begin("enctype='multipart/form-data' id='attachadd-legacy-form'",
ca90659… stephan 591 "%R/attachadd");
ca90659… stephan 592 @ <div>\
ca90659… stephan 593 @ File to Attach:
ca90659… stephan 594 @ <input type="file" name="f" size="60"><br>
ca90659… stephan 595 @ Description:<br>
ca90659… stephan 596 @ <textarea name="comment" cols="80" rows="5" wrap="virtual"\
ca90659… stephan 597 @ >%h(zComment)</textarea><br>
ca90659… stephan 598 if( zForumPost ){
ca90659… stephan 599 @ <input type="hidden" name="forumpost" value="%h(zForumPost)">\
ca90659… stephan 600 }else if( zTicket ){
ca90659… stephan 601 @ <input type="hidden" name="tkt" value="%h(zTicket)">\
ca90659… stephan 602 }else if( zTechNote ){
ca90659… stephan 603 @ <input type="hidden" name="technote" value="%h(zTechNote)">\
ca90659… stephan 604 }else if( zWikiPage ){
ca90659… stephan 605 @ <input type="hidden" name="page" value="%h(zWikiPage)">\
ca90659… stephan 606 }
ca90659… stephan 607 @ <input type="hidden" name="from" value="%h(zFrom)">\
ca90659… stephan 608 @ <input type="submit" name="ok" value="Add Attachment">\
ca90659… stephan 609 @ <input type="submit" name="cancel" value="Cancel">\
ca90659… stephan 610 @ </div>
ca90659… stephan 611 captcha_generate(0);
ca90659… stephan 612 login_insert_csrf_secret();
ca90659… stephan 613 @ </form>
5079ffb… stephan 614 }
5079ffb… stephan 615
5079ffb… stephan 616 /*
653dd40… drh 617 ** WEBPAGE: attachadd
7ab0328… drh 618 ** Add a new attachment.
653dd40… drh 619 **
8ad5e46… wyoung 620 ** tkt=HASH
653dd40… drh 621 ** page=WIKIPAGE
8ad5e46… wyoung 622 ** technote=HASH
5079ffb… stephan 623 ** forumpost=HASH
653dd40… drh 624 ** from=URL
653dd40… drh 625 **
ca90659… stephan 626 ** Adds a POSTed file attachment to the given target.
ca90659… stephan 627 **
ca90659… stephan 628 ** Or the "version 2" interface:
ca90659… stephan 629 **
ca90659… stephan 630 ** target=ATTACHMENT_TARGET
ca90659… stephan 631 **
ca90659… stephan 632 ** Behaves as documented for attachaddV2_page().
653dd40… drh 633 */
653dd40… drh 634 void attachadd_page(void){
ca90659… stephan 635 const char *zPage;
ca90659… stephan 636 const char *zForumPost;
ca90659… stephan 637 const char *zTkt;
ca90659… stephan 638 const char *zTechNote;
ca90659… stephan 639 const char *aContent;
ca90659… stephan 640 const char *zName;
ca90659… stephan 641 const char *zComment;
653dd40… drh 642 const char *zTarget;
ca90659… stephan 643 const char *zFrom; /* Origin page - redirect here after saving */
ca90659… stephan 644 char *zTo = 0; /* Optionally redirect here after saving */
5079ffb… stephan 645 char *zTargetType = 0;
5079ffb… stephan 646 char *zExtraFree = 0;
ca90659… stephan 647 int szContent;
653dd40… drh 648 int goodCaptcha = 1;
5079ffb… stephan 649 int szLimit = 0;
ca90659… stephan 650
ca90659… stephan 651 if( P("target")!=0 ){
ca90659… stephan 652 attachaddV2_page();
ca90659… stephan 653 return;
ca90659… stephan 654 }
ca90659… stephan 655 zPage = P("page");
ca90659… stephan 656 zForumPost = P("forumpost");
ca90659… stephan 657 zTkt = P("tkt");
ca90659… stephan 658 zTechNote = P("technote");
ca90659… stephan 659 zFrom = P("from");
ca90659… stephan 660 aContent = P("f");
ca90659… stephan 661 zName = PD("f:filename","unknown");
ca90659… stephan 662 zComment = PD("comment", "");
ca90659… stephan 663 szContent = atoi(PD("f:bytes","0"));
653dd40… drh 664
5079ffb… stephan 665 if( zFrom==0 ) zFrom = mprintf("%R/home");
653dd40… drh 666 if( P("cancel") ) cgi_redirect(zFrom);
5079ffb… stephan 667 if( (!!zPage + !!zTkt + !!zTechNote + !!zForumPost)!=1 ){
5079ffb… stephan 668 webpage_error("Requires exactly one one: page=X, tkt=X, forumpost=X,"
ca90659… stephan 669 " technote=X, or target=X");
045deb2… drh 670 }
653dd40… drh 671 login_check_credentials();
5079ffb… stephan 672 if( zForumPost ){
5079ffb… stephan 673 int fpid;
5079ffb… stephan 674 if( g.perm.AttachForum==0 ){
5079ffb… stephan 675 login_needed(g.anon.AttachForum);
5079ffb… stephan 676 return;
5079ffb… stephan 677 }
5079ffb… stephan 678 fpid = forumpost_head_rid2(zForumPost);
5079ffb… stephan 679 if( fpid<=0 ){
5079ffb… stephan 680 webpage_error("Invalid forum post ID: %h", zForumPost);
5079ffb… stephan 681 }else if( !g.perm.Admin && !forumpost_is_owner(fpid, 0) ){
5079ffb… stephan 682 webpage_error("Only admins can attach files to other users' "
5079ffb… stephan 683 "forum posts.");
5079ffb… stephan 684 }
5079ffb… stephan 685 zTarget = zExtraFree = rid_to_uuid(fpid);
5079ffb… stephan 686 zTargetType = mprintf("Forum post <a href=\"%R/forumpost/%S\">%h</a>",
5079ffb… stephan 687 zTarget, zForumPost);
ca90659… stephan 688 zTo = zFrom ? 0 : mprintf("%R/forumpost/%S", zTarget);
5079ffb… stephan 689 }else if( zPage ){
653dd40… drh 690 if( g.perm.ApndWiki==0 || g.perm.Attach==0 ){
653dd40… drh 691 login_needed(g.anon.ApndWiki && g.anon.Attach);
653dd40… drh 692 return;
653dd40… drh 693 }
653dd40… drh 694 if( !db_exists("SELECT 1 FROM tag WHERE tagname='wiki-%q'", zPage) ){
653dd40… drh 695 fossil_redirect_home();
653dd40… drh 696 }
653dd40… drh 697 zTarget = zPage;
ca90659… stephan 698 zTargetType = mprintf("Wiki Page <a href=\"%R/wiki?name=%t\">%h</a>",
653dd40… drh 699 zPage, zPage);
ca90659… stephan 700 zTo = zFrom ? 0 : mprintf("%R/wiki?name=%T", zTarget);
045deb2… drh 701 }else if ( zTechNote ){
045deb2… drh 702 if( g.perm.Write==0 || g.perm.ApndWiki==0 || g.perm.Attach==0 ){
045deb2… drh 703 login_needed(g.anon.Write && g.anon.ApndWiki && g.anon.Attach);
045deb2… drh 704 return;
045deb2… drh 705 }
045deb2… drh 706 if( !db_exists("SELECT 1 FROM tag WHERE tagname='event-%q'", zTechNote) ){
045deb2… drh 707 zTechNote = db_text(0, "SELECT substr(tagname,7) FROM tag"
045deb2… drh 708 " WHERE tagname GLOB 'event-%q*'", zTechNote);
045deb2… drh 709 if( zTechNote==0) fossil_redirect_home();
045deb2… drh 710 }
045deb2… drh 711 zTarget = zTechNote;
05cd9fa… rberteig 712 zTargetType = mprintf("Tech Note <a href=\"%R/technote/%s\">%S</a>",
045deb2… drh 713 zTechNote, zTechNote);
ca90659… stephan 714 zTo = zFrom ? 0 : mprintf("%R/technote/%S", zTarget);
653dd40… drh 715 }else{
5079ffb… stephan 716 assert( zTkt );
653dd40… drh 717 if( g.perm.ApndTkt==0 || g.perm.Attach==0 ){
653dd40… drh 718 login_needed(g.anon.ApndTkt && g.anon.Attach);
653dd40… drh 719 return;
653dd40… drh 720 }
653dd40… drh 721 if( !db_exists("SELECT 1 FROM tag WHERE tagname='tkt-%q'", zTkt) ){
653dd40… drh 722 zTkt = db_text(0, "SELECT substr(tagname,5) FROM tag"
653dd40… drh 723 " WHERE tagname GLOB 'tkt-%q*'", zTkt);
653dd40… drh 724 if( zTkt==0 ) fossil_redirect_home();
653dd40… drh 725 }
653dd40… drh 726 zTarget = zTkt;
ca90659… stephan 727 zTargetType = mprintf("Ticket <a href=\"%R/tktview/%S\">%S</a>",
653dd40… drh 728 zTkt, zTkt);
ca90659… stephan 729 zTo = zFrom ? 0 : mprintf("%R/tktview/%S", zTarget);
05cd9fa… rberteig 730 }
5079ffb… stephan 731 szLimit = db_get_int("attachment-size-limit", 0);
5079ffb… stephan 732 if( szContent<0 || (szLimit && szContent>szLimit) ){
5079ffb… stephan 733 /* This check must be done late so that zTargetType is set up. */
5079ffb… stephan 734 @ <p class="generalError">Attachment %h(zName) is too large.
5079ffb… stephan 735 @ <a href="%R/help/attachment-size-limit">Limit</a> is
5079ffb… stephan 736 @ %d(szLimit ? szLimit : 0x7fffffff) bytes</p>
5079ffb… stephan 737 /* Fall through and render form. */
ca90659… stephan 738 }else if( P("ok")
ca90659… stephan 739 && cgi_csrf_safe(2)
ca90659… stephan 740 && szContent>0
ca90659… stephan 741 && (goodCaptcha = captcha_is_correct(0)) ){
5079ffb… stephan 742 int needModerator = (zForumPost!=0 && forum_need_moderation()) ||
5079ffb… stephan 743 (zTkt!=0 && ticket_need_moderation(0)) ||
05cd9fa… rberteig 744 (zPage!=0 && wiki_need_moderation(0));
05cd9fa… rberteig 745 attach_commit(zName, zTarget, aContent, szContent, needModerator, zComment);
5079ffb… stephan 746 cgi_redirect(zTo ? zTo : zFrom);
05cd9fa… rberteig 747 }
5079ffb… stephan 748
112c713… drh 749 style_set_current_feature("attach");
82b8587… drh 750 style_header("Add Attachment");
82b8587… drh 751 if( !goodCaptcha ){
82b8587… drh 752 @ <p class="generalError">Error: Incorrect security code.</p>
82b8587… drh 753 }
82b8587… drh 754 @ <h2>Add Attachment To %s(zTargetType)</h2>
ca90659… stephan 755 attach_render_legacy_form(zForumPost, zTechNote, zTkt, zPage,
ca90659… stephan 756 zComment, zFrom);
ca90659… stephan 757 builtin_fossil_js_bundle_or("attach", NULL);
ca90659… stephan 758 style_finish_page();
ca90659… stephan 759 fossil_free(zTargetType);
ca90659… stephan 760 fossil_free(zExtraFree);
ca90659… stephan 761 }
ca90659… stephan 762
ca90659… stephan 763 /*
ca90659… stephan 764 ** WEBPAGE: attachadd_ajax_post hidden
ca90659… stephan 765 **
ca90659… stephan 766 ** Used by attachadd V2 to handle attachments via POST requests with:
ca90659… stephan 767 **
ca90659… stephan 768 ** target=ATTACHMENT_TARGET
ca90659… stephan 769 ** file1..fileN=FILE_OBJECTS
ca90659… stephan 770 ** dryrun=0|1
ca90659… stephan 771 **
ca90659… stephan 772 ** Each posted file in the set file1..fileN gets attached to the given
ca90659… stephan 773 ** target, permissions permitting. If dryrun>0 then the change is
ca90659… stephan 774 ** rolled back instead of committed. target=X must refer to a full
ca90659… stephan 775 ** target ID, not a prefix.
ca90659… stephan 776 **
ca90659… stephan 777 ** Responds with JSON: an empty object on success and
ca90659… stephan 778 ** {error:"message"} on error. The on-success response structure is
ca90659… stephan 779 ** subject to amendment.
ca90659… stephan 780 */
ca90659… stephan 781 void attachadd_ajax_post(void){
ca90659… stephan 782 const char *zTarget;
ca90659… stephan 783 char *zExtraFree = 0;
ca90659… stephan 784 int eTgtType = 0;
ca90659… stephan 785 int bNeedsModeration = 0;
ca90659… stephan 786 int goodCaptcha = 1;
ca90659… stephan 787 int bRollback = 0; /* Roll back if true. */
ca90659… stephan 788
ca90659… stephan 789 if( ! ajax_route_bootstrap(0, 1) ){
ca90659… stephan 790 return;
ca90659… stephan 791 }else if( !(goodCaptcha = captcha_is_correct(0)) ){
ca90659… stephan 792 goto ajax_err_403;
ca90659… stephan 793 }else if( !ajax_check_csrf(2) ){
ca90659… stephan 794 return;
ca90659… stephan 795 }
ca90659… stephan 796 db_begin_transaction();
ca90659… stephan 797 zTarget = P("target");
ca90659… stephan 798 eTgtType = attachment_target_type(zTarget, 1);
ca90659… stephan 799 CX("{");
ca90659… stephan 800 switch( eTgtType ){
ca90659… stephan 801 default:
ca90659… stephan 802 case 0:
ca90659… stephan 803 ajax_route_error(400, "Invalid attachment target.");
ca90659… stephan 804 db_rollback_transaction();
ca90659… stephan 805 return;
ca90659… stephan 806 case CFTYPE_FORUM:{
ca90659… stephan 807 int fpid;
ca90659… stephan 808 if( g.perm.AttachForum==0 ){
ca90659… stephan 809 goto ajax_err_403;
ca90659… stephan 810 }
ca90659… stephan 811 fpid = forumpost_head_rid2(zTarget);
ca90659… stephan 812 if( fpid<=0 ){
ca90659… stephan 813 goto ajax_err_404;
ca90659… stephan 814 }else if( !g.perm.Admin && !forumpost_is_owner(fpid, 0) ){
ca90659… stephan 815 ajax_route_error(403, "Only admins can attach files to "
ca90659… stephan 816 "other users' forum posts.");
ca90659… stephan 817 db_rollback_transaction();
ca90659… stephan 818 return;
ca90659… stephan 819 }
ca90659… stephan 820 zTarget = zExtraFree = rid_to_uuid(fpid);
ca90659… stephan 821 bNeedsModeration = forum_need_moderation();
ca90659… stephan 822 break;
ca90659… stephan 823 }
ca90659… stephan 824 case CFTYPE_EVENT:{
ca90659… stephan 825 if( g.perm.Write==0 || g.perm.ApndWiki==0 || g.perm.Attach==0 ){
ca90659… stephan 826 goto ajax_err_403;
ca90659… stephan 827 }
ca90659… stephan 828 if( !db_exists("SELECT 1 FROM tag WHERE tagname='event-%q'",
ca90659… stephan 829 zTarget) ){
ca90659… stephan 830 zTarget = zExtraFree =
ca90659… stephan 831 db_text(0, "SELECT substr(tagname,7) FROM tag"
ca90659… stephan 832 " WHERE tagname GLOB 'event-%q*'", zTarget);
ca90659… stephan 833 if( zTarget==0){
ca90659… stephan 834 goto ajax_err_404;
ca90659… stephan 835 }
ca90659… stephan 836 }
ca90659… stephan 837 bNeedsModeration = 0;
ca90659… stephan 838 break;
ca90659… stephan 839 }
ca90659… stephan 840 case CFTYPE_TICKET:{
ca90659… stephan 841 if( g.perm.ApndTkt==0 || g.perm.Attach==0 ){
ca90659… stephan 842 goto ajax_err_403;
ca90659… stephan 843 }
ca90659… stephan 844 if( !db_exists("SELECT 1 FROM tag WHERE tagname='tkt-%q'",
ca90659… stephan 845 zTarget) ){
ca90659… stephan 846 zTarget = db_text(0, "SELECT substr(tagname,5) FROM tag"
ca90659… stephan 847 " WHERE tagname GLOB 'tkt-%q*'", zTarget);
ca90659… stephan 848 if( zTarget==0 ){
ca90659… stephan 849 goto ajax_err_404;
ca90659… stephan 850 }
ca90659… stephan 851 }
ca90659… stephan 852 bNeedsModeration = ticket_need_moderation(0);
ca90659… stephan 853 break;
ca90659… stephan 854 }
ca90659… stephan 855 case CFTYPE_WIKI:{
ca90659… stephan 856 if( g.perm.ApndWiki==0 || g.perm.Attach==0 ){
ca90659… stephan 857 goto ajax_err_403;
ca90659… stephan 858 }
ca90659… stephan 859 if( !db_exists("SELECT 1 FROM tag WHERE tagname='wiki-%q'",
ca90659… stephan 860 zTarget) ){
ca90659… stephan 861 goto ajax_err_404;
ca90659… stephan 862 }
ca90659… stephan 863 bNeedsModeration = wiki_need_moderation(0);
ca90659… stephan 864 break;
ca90659… stephan 865 }
ca90659… stephan 866 }
ca90659… stephan 867
ca90659… stephan 868 if( attachments_ajax_from_POST(zTarget, bNeedsModeration)>=0 ){
ca90659… stephan 869 CX("}");
ca90659… stephan 870 if( atoi(PD("dryrun","0"))>0 ){
ca90659… stephan 871 bRollback = 1;
ca90659… stephan 872 }
ca90659… stephan 873 }/*else error response was set up*/
ca90659… stephan 874 fossil_free(zExtraFree);
ca90659… stephan 875 db_end_transaction(bRollback);
ca90659… stephan 876 return;
ca90659… stephan 877 ajax_err_403:
ca90659… stephan 878 if( db_transaction_nesting_depth()>0 ){
ca90659… stephan 879 db_rollback_transaction();
ca90659… stephan 880 }
ca90659… stephan 881 ajax_route_error_forbidden();
ca90659… stephan 882 return;
ca90659… stephan 883 ajax_err_404:
ca90659… stephan 884 assert( db_transaction_nesting_depth()>0 );
ca90659… stephan 885 db_rollback_transaction();
ca90659… stephan 886 ajax_route_error(404, "Target not found.");
ca90659… stephan 887 return;
ca90659… stephan 888 }
ca90659… stephan 889
ca90659… stephan 890 /*
ca90659… stephan 891 ** A helper for AJAX-style routines which accept file attachments via
ca90659… stephan 892 ** POST. zTarget must be a full attachment target. bNeedsModeration
ca90659… stephan 893 ** must be true if the attachment requires moderation.
ca90659… stephan 894 **
ca90659… stephan 895 ** It is up to the caller to have validated all security measures
ca90659… stephan 896 ** before calling this.
ca90659… stephan 897 **
ca90659… stephan 898 ** This looks for POSTed files names "file1".."fileN", stopping when
ca90659… stephan 899 ** it finds no entry. Returns the number of entries attached to the
ca90659… stephan 900 ** target or a negative value on error (in which case the current db
ca90659… stephan 901 ** transaction will be in a rollback state).
ca90659… stephan 902 **
ca90659… stephan 903 ** The only errors are currently attachment size limit violations:
ca90659… stephan 904 ** attachments must have a non-0 size and if the attachment-size-limit
ca90659… stephan 905 ** setting is >0 then each file's size must be <= that.
ca90659… stephan 906 **
ca90659… stephan 907 ** If this returns a negative value, it will have populated an error
ca90659… stephan 908 ** response using ajax_route_error(). On success it produces no
ca90659… stephan 909 ** output.
ca90659… stephan 910 **
ca90659… stephan 911 ** ACHTUNG: if zTarget is a forum post, it "really should" be the ID
ca90659… stephan 912 ** of the first version of that post, as that's where attachments are
ca90659… stephan 913 ** intended to be applied so that they can be found and removed
ca90659… stephan 914 ** consistently. Potential TODO is have this function do that if
ca90659… stephan 915 ** attachment_target_type(zTarget,1)!=0 but it would (for current
ca90659… stephan 916 ** uses) require duplicating work already done in the callers.
ca90659… stephan 917 */
ca90659… stephan 918 int attachments_ajax_from_POST(const char *zTarget, int bNeedsModeration){
ca90659… stephan 919 int i;
ca90659… stephan 920 int rc = 0;
ca90659… stephan 921 int n = 0;
ca90659… stephan 922 int szLimit; /* attachment-max-size setting */
ca90659… stephan 923 char aKeyPrefix[20]; /* Buffer for key "file%d" */
ca90659… stephan 924 char aKeySize[30]; /* Buffer for key "file%d:bytes" */
ca90659… stephan 925 char aKeyName[30]; /* Buffer for key "file%d:filename" */
ca90659… stephan 926 char aKeyDesc[30]; /* Buffer for key "file%d_desc" */
ca90659… stephan 927
ca90659… stephan 928 db_begin_transaction();
ca90659… stephan 929 szLimit = db_get_int("attachment-size-limit", 0);
ca90659… stephan 930
ca90659… stephan 931 for(i = 1; ; ++i, ++n){
ca90659… stephan 932 /* Look for P("fileN"), where N=1..n */
ca90659… stephan 933 const char *zContent;
ca90659… stephan 934 const char *zFilename;
ca90659… stephan 935 int szContent;
ca90659… stephan 936 sqlite3_snprintf(sizeof(aKeyPrefix), aKeyPrefix, "file%d", i);
ca90659… stephan 937 zContent = P(aKeyPrefix);
ca90659… stephan 938 if( !zContent ){
ca90659… stephan 939 /* End of the list. */
ca90659… stephan 940 break;
ca90659… stephan 941 }
ca90659… stephan 942 sqlite3_snprintf(sizeof(aKeySize), aKeySize, "%s:bytes",
ca90659… stephan 943 aKeyPrefix);
ca90659… stephan 944 szContent = atoi(PD(aKeySize,"-1"));
ca90659… stephan 945 if( szContent<=0 ){
ca90659… stephan 946 rc = -ajax_route_error(400,"Invalid file size: %d", szContent);
ca90659… stephan 947 break;
ca90659… stephan 948 }else if( szLimit>0 && szContent>szLimit ){
ca90659… stephan 949 rc = -ajax_route_error(413, "File size limit is %d bytes.", szLimit);
ca90659… stephan 950 break;
ca90659… stephan 951 }else{
ca90659… stephan 952 sqlite3_snprintf(sizeof(aKeyName), aKeyName, "%s:filename",
ca90659… stephan 953 aKeyPrefix);
ca90659… stephan 954 sqlite3_snprintf(sizeof(aKeyDesc), aKeyDesc, "%s_desc",
ca90659… stephan 955 aKeyPrefix);
ca90659… stephan 956 if( 0==(zFilename=P(aKeyName)) ){
ca90659… stephan 957 rc = -ajax_route_error(400, "Missing filename.");
ca90659… stephan 958 break;
ca90659… stephan 959 }
ca90659… stephan 960 attach_commit(zFilename, zTarget, zContent, szContent,
ca90659… stephan 961 bNeedsModeration, P(aKeyDesc));
ca90659… stephan 962 }
ca90659… stephan 963 }
ca90659… stephan 964 if( rc<0 ){
ca90659… stephan 965 db_rollback_transaction();
ca90659… stephan 966 return rc;
ba418ee… drh 967 }else{
ca90659… stephan 968 db_commit_transaction();
ca90659… stephan 969 return n;
ca90659… stephan 970 }
ca90659… stephan 971 }
ca90659… stephan 972
ca90659… stephan 973 /*
ca90659… stephan 974 ** Proxy for /attachadd?target=X
ca90659… stephan 975 **
ca90659… stephan 976 ** Lists attachments for, and can add them to, a target artifact.
ca90659… stephan 977 **
ca90659… stephan 978 ** target=TKT_HASH|WIKIPAGE_NAME|TECHNOTE_HASH|FORUMPOST_HASH
ca90659… stephan 979 ** from=ORIGINATING_URL
ca90659… stephan 980 **
ca90659… stephan 981 ** Works like /attachadd but uses a JS-based interactive attachment
ca90659… stephan 982 ** selector.
ca90659… stephan 983 **
ca90659… stephan 984 ** from=X tells it where to redirect to when it's done.
ca90659… stephan 985 **
ca90659… stephan 986 ** This page requires a post-2018-ish JS-capable browser.
ca90659… stephan 987 */
ca90659… stephan 988 void attachaddV2_page(void){
ca90659… stephan 989 const char *zFrom = P("from");
ca90659… stephan 990 const char *zTarget = P("target");
ca90659… stephan 991 char *zTo = 0;
ca90659… stephan 992 char *zTargetType = 0;
ca90659… stephan 993 char *zExtraFree = 0;
ca90659… stephan 994 int eTgtType = 0;
ca90659… stephan 995 int goodCaptcha = 1;
ca90659… stephan 996 char const * noJsArgs[] = {0,0,0,0}; /* Args for noscript form */
ca90659… stephan 997
ca90659… stephan 998 if( P("cancel") ) cgi_redirect(zFrom);
ca90659… stephan 999 if( 0==zTarget ){
ca90659… stephan 1000 webpage_error("Requires target=X");
ca90659… stephan 1001 }
ca90659… stephan 1002 login_check_credentials();
ca90659… stephan 1003 eTgtType = attachment_target_type(zTarget, 1);
ca90659… stephan 1004 switch( eTgtType ){
ca90659… stephan 1005 default:
ca90659… stephan 1006 case 0:
ca90659… stephan 1007 webpage_error("Cannot resolve target=%h.", zTarget);
ca90659… stephan 1008 break;
ca90659… stephan 1009 case CFTYPE_FORUM:{
ca90659… stephan 1010 int fpid;
ca90659… stephan 1011 if( g.perm.AttachForum==0 ){
ca90659… stephan 1012 login_needed(g.anon.AttachForum);
ca90659… stephan 1013 return;
ca90659… stephan 1014 }
ca90659… stephan 1015 fpid = forumpost_head_rid2(zTarget);
ca90659… stephan 1016 if( fpid<=0 ){
ca90659… stephan 1017 webpage_error("Invalid forum post ID: %h", zTarget);
ca90659… stephan 1018 }else if( !g.perm.Admin && !forumpost_is_owner(fpid, 0) ){
ca90659… stephan 1019 webpage_error("Only admins can attach files to other users' "
ca90659… stephan 1020 "forum posts.");
ca90659… stephan 1021 }
ca90659… stephan 1022 zTarget = zExtraFree = rid_to_uuid(fpid);
ca90659… stephan 1023 noJsArgs[0] = zTarget;
ca90659… stephan 1024 zTargetType = mprintf(
ca90659… stephan 1025 "Forum post <a href=\"%R/forumpost/%S\">%.16h</a>",
ca90659… stephan 1026 zTarget, zTarget
ca90659… stephan 1027 );
ca90659… stephan 1028 zTo = mprintf("%R/forumpost/%S", zTarget);
ca90659… stephan 1029 break;
ca90659… stephan 1030 }
ca90659… stephan 1031 case CFTYPE_EVENT:{
ca90659… stephan 1032 if( g.perm.Write==0 || g.perm.ApndWiki==0 || g.perm.Attach==0 ){
ca90659… stephan 1033 login_needed(g.anon.Write && g.anon.ApndWiki && g.anon.Attach);
ca90659… stephan 1034 return;
ca90659… stephan 1035 }
ca90659… stephan 1036 if( !db_exists("SELECT 1 FROM tag WHERE tagname='event-%q'",
ca90659… stephan 1037 zTarget) ){
ca90659… stephan 1038 zTarget = db_text(0, "SELECT substr(tagname,7) FROM tag"
ca90659… stephan 1039 " WHERE tagname GLOB 'event-%q*'",
ca90659… stephan 1040 zTarget);
ca90659… stephan 1041 if( zTarget==0) fossil_redirect_home();
ca90659… stephan 1042 }
ca90659… stephan 1043 zTo = zFrom ? 0 : mprintf("%R/technote?name=%T", zTarget);
ca90659… stephan 1044 zTargetType = mprintf("Tech-note <a href=\"%R/technote/%s\">%S</a>",
ca90659… stephan 1045 zTarget, zTarget);
ca90659… stephan 1046 noJsArgs[1] = zTarget;
ca90659… stephan 1047 break;
ca90659… stephan 1048 }
ca90659… stephan 1049 case CFTYPE_TICKET:{
ca90659… stephan 1050 if( g.perm.ApndTkt==0 || g.perm.Attach==0 ){
ca90659… stephan 1051 login_needed(g.anon.ApndTkt && g.anon.Attach);
ca90659… stephan 1052 return;
ca90659… stephan 1053 }
ca90659… stephan 1054 if( !db_exists("SELECT 1 FROM tag WHERE tagname='tkt-%q'",
ca90659… stephan 1055 zTarget) ){
ca90659… stephan 1056 zTarget = db_text(0, "SELECT substr(tagname,5) FROM tag"
ca90659… stephan 1057 " WHERE tagname GLOB 'tkt-%q*'", zTarget);
ca90659… stephan 1058 if( zTarget==0 ) fossil_redirect_home();
ca90659… stephan 1059 }
ca90659… stephan 1060 zTo = zFrom ? 0 : mprintf("%R/tktview/%t", zTarget);
ca90659… stephan 1061 zTargetType = mprintf("Ticket <a href=\"%R/tktview/%s\">%S</a>",
ca90659… stephan 1062 zTarget, zTarget);
ca90659… stephan 1063 noJsArgs[2] = zTarget;
ca90659… stephan 1064 break;
ca90659… stephan 1065 }
ca90659… stephan 1066 case CFTYPE_WIKI:{
ca90659… stephan 1067 if( g.perm.ApndWiki==0 || g.perm.Attach==0 ){
ca90659… stephan 1068 login_needed(g.anon.ApndWiki && g.anon.Attach);
ca90659… stephan 1069 return;
ca90659… stephan 1070 }
ca90659… stephan 1071 if( !db_exists("SELECT 1 FROM tag WHERE tagname='wiki-%q'",
ca90659… stephan 1072 zTarget) ){
ca90659… stephan 1073 fossil_redirect_home();
ca90659… stephan 1074 }
ca90659… stephan 1075 zTo = zFrom ? 0 : mprintf("%R/wiki?name=%T", zTarget);
ca90659… stephan 1076 zTargetType = mprintf(
ca90659… stephan 1077 "Wiki page <a href=\"%R/wiki?name=%h\">%h</a>",
ca90659… stephan 1078 zTarget, zTarget
ca90659… stephan 1079 );
ca90659… stephan 1080 noJsArgs[3] = zTarget;
ca90659… stephan 1081 break;
ca90659… stephan 1082 }
ca90659… stephan 1083 }
ca90659… stephan 1084
ca90659… stephan 1085 db_begin_transaction();
ca90659… stephan 1086
ca90659… stephan 1087 style_set_current_feature("attach");
ca90659… stephan 1088 style_header("Add Attachment");
ca90659… stephan 1089 if( !goodCaptcha ){
ca90659… stephan 1090 @ <p class="generalError">Error: Incorrect security code.</p>
ca90659… stephan 1091 }
ca90659… stephan 1092 @ <h2>Attachments for %s(zTargetType)</h2>
ca90659… stephan 1093 attachment_list(zTarget, NULL,
ca90659… stephan 1094 ATTACHLIST_SIZE | ATTACHLIST_HIDE_UNAPPROVED);
ca90659… stephan 1095 attach_render_legacy_form(
ca90659… stephan 1096 noJsArgs[0], noJsArgs[1], noJsArgs[2],
ca90659… stephan 1097 noJsArgs[3], 0,
ca90659… stephan 1098 zFrom ? zFrom : (zTo ? zTo : (zTo=mprintf("%R/home")))
ca90659… stephan 1099 );
ca90659… stephan 1100 @ <div id='attachadd-form-wrapper' class='hidden'>
ca90659… stephan 1101 /* fossil.attach.js populates this DIV with the attachment widget,
ca90659… stephan 1102 ** imports these hidden fields, and removes the legacy form. */
ca90659… stephan 1103 @ <input type="hidden" name="target" value="%h(zTarget)">
ca90659… stephan 1104 if( zFrom ){
ca90659… stephan 1105 @ <input type="hidden" name="from" value="%h(zFrom)">
f8a2aa0… drh 1106 }
ca90659… stephan 1107 if( zTo ){
ca90659… stephan 1108 @ <input type="hidden" name="to" value="%h(zTo)">
ca90659… stephan 1109 }
f8a2aa0… drh 1110 captcha_generate(0);
ca90659… stephan 1111 login_insert_csrf_secret();
ca90659… stephan 1112 @ </div>
ca90659… stephan 1113 builtin_fossil_js_bundle_or("attach", NULL);
ca90659… stephan 1114 db_end_transaction(0);
112c713… drh 1115 style_finish_page();
045deb2… drh 1116 fossil_free(zTargetType);
5079ffb… stephan 1117 fossil_free(zExtraFree);
ca90659… stephan 1118 fossil_free(zTo);
ba418ee… drh 1119 }
ba418ee… drh 1120
ba418ee… drh 1121 /*
ba418ee… drh 1122 ** WEBPAGE: ainfo
ba418ee… drh 1123 ** URL: /ainfo?name=ARTIFACTID
5079ffb… stephan 1124 **
5079ffb… stephan 1125 ** name=ATTACHMENT_ARTIFACT_UUID
ba418ee… drh 1126 **
ba418ee… drh 1127 ** Show the details of an attachment artifact.
ba418ee… drh 1128 */
ba418ee… drh 1129 void ainfo_page(void){
ba418ee… drh 1130 int rid; /* RID for the control artifact */
ba418ee… drh 1131 int ridSrc; /* RID for the attached file */
ba418ee… drh 1132 char *zDate; /* Date attached */
8ad5e46… wyoung 1133 const char *zUuid; /* Hash of the control artifact */
ba418ee… drh 1134 Manifest *pAttach; /* Parse of the control artifact */
045deb2… drh 1135 const char *zTarget; /* Wiki, ticket or tech note attached to */
8ad5e46… wyoung 1136 const char *zSrc; /* Hash of the attached file */
ba418ee… drh 1137 const char *zName; /* Name of the attached file */
ba418ee… drh 1138 const char *zDesc; /* Description of the attached file */
ba418ee… drh 1139 const char *zWikiName = 0; /* Wiki page name when attached to Wiki */
045deb2… drh 1140 const char *zTNUuid = 0; /* Tech Note ID when attached to tech note */
ba418ee… drh 1141 const char *zTktUuid = 0; /* Ticket ID when attached to a ticket */
5079ffb… stephan 1142 const char *zForumPost = 0; /* Forum UID when attached to forum post */
ba418ee… drh 1143 int modPending; /* True if awaiting moderation */
ba418ee… drh 1144 const char *zModAction; /* Moderation action or NULL */
ba418ee… drh 1145 int isModerator; /* TRUE if user is the moderator */
ba418ee… drh 1146 const char *zMime; /* MIME Type */
ba418ee… drh 1147 Blob attach; /* Content of the attachment */
5079ffb… stephan 1148 int fShowContent = 0; /* True to emit the content */
5079ffb… stephan 1149 int bUserIsOwner = 0; /* True if pAttach->zUser is login_name() */
5079ffb… stephan 1150 int showDelMenu = 0; /* True to enable delete option */
a5b1bef… stephan 1151 const char *zLn = P("ln");
ba418ee… drh 1152
ba418ee… drh 1153 login_check_credentials();
653dd40… drh 1154 if( !g.perm.RdTkt && !g.perm.RdWiki ){
653dd40… drh 1155 login_needed(g.anon.RdTkt || g.anon.RdWiki);
653dd40… drh 1156 return;
653dd40… drh 1157 }
ba418ee… drh 1158 rid = name_to_rid_www("name");
ba418ee… drh 1159 if( rid==0 ){ fossil_redirect_home(); }
5079ffb… stephan 1160 zUuid = rid_to_uuid(rid);
ec81aee… jan.nijtmans 1161 pAttach = manifest_get(rid, CFTYPE_ATTACHMENT, 0);
ba418ee… drh 1162 if( pAttach==0 ) fossil_redirect_home();
5079ffb… stephan 1163 bUserIsOwner =
5079ffb… stephan 1164 0==fossil_strcmp(pAttach->zUser, login_name())
5079ffb… stephan 1165 && login_is_individual();
ba418ee… drh 1166 zTarget = pAttach->zAttachTarget;
ba418ee… drh 1167 zSrc = pAttach->zAttachSrc;
49b0ff1… drh 1168 ridSrc = db_int(0,"SELECT rid FROM blob WHERE uuid='%q'", zSrc);
ba418ee… drh 1169 zName = pAttach->zAttachName;
ba418ee… drh 1170 zDesc = pAttach->zComment;
a5b1bef… stephan 1171 zMime = mimetype_from_name(zName);
a5b1bef… stephan 1172 fShowContent = zMime ? strncmp(zMime,"text/", 5)==0 : 0;
5079ffb… stephan 1173 if( db_int(0,"SELECT 1 FROM event WHERE objid=%d and type='f'", rid) ){
5079ffb… stephan 1174 if( !g.perm.RdForum ){ login_needed(g.anon.RdForum); return; }
5079ffb… stephan 1175 showDelMenu = g.perm.Admin || bUserIsOwner;
5079ffb… stephan 1176 zForumPost = zTarget;
5079ffb… stephan 1177 }else if( validate16(zTarget, strlen(zTarget))
49b0ff1… drh 1178 && db_exists("SELECT 1 FROM ticket WHERE tkt_uuid='%q'", zTarget)
ba418ee… drh 1179 ){
653dd40… drh 1180 if( !g.perm.RdTkt ){ login_needed(g.anon.RdTkt); return; }
5079ffb… stephan 1181 zTktUuid = zTarget;
5079ffb… stephan 1182 showDelMenu = g.perm.WrTkt;
ca90659… stephan 1183 }else if( db_exists("SELECT 1 FROM tag WHERE tagname='wiki-%q'",
ca90659… stephan 1184 zTarget) ){
5079ffb… stephan 1185 if( !g.perm.RdWiki ){ login_needed(g.anon.RdWiki); return; }
ba418ee… drh 1186 zWikiName = zTarget;
5079ffb… stephan 1187 showDelMenu = g.perm.WrWiki;
ca90659… stephan 1188 }else if( db_exists("SELECT 1 FROM tag WHERE tagname='event-%q'",
ca90659… stephan 1189 zTarget) ){
653dd40… drh 1190 if( !g.perm.RdWiki ){ login_needed(g.anon.RdWiki); return; }
045deb2… drh 1191 zTNUuid = zTarget;
5079ffb… stephan 1192 showDelMenu = g.perm.Write && g.perm.WrWiki;
5079ffb… stephan 1193 }
5079ffb… stephan 1194 if( showDelMenu ){
5079ffb… stephan 1195 style_submenu_element("Delete", "%R/ainfo/%s?del", zUuid);
7c2577b… drh 1196 }
ba418ee… drh 1197 zDate = db_text(0, "SELECT datetime(%.12f)", pAttach->rDate);
ba418ee… drh 1198
ba418ee… drh 1199 if( P("confirm")
5079ffb… stephan 1200 && cgi_csrf_safe(2)
5079ffb… stephan 1201 && ((zForumPost
5079ffb… stephan 1202 && ((bUserIsOwner && g.perm.AttachForum) ||
5079ffb… stephan 1203 forumpost_may_close())) ||
5079ffb… stephan 1204 (zTktUuid && g.perm.WrTkt) ||
045deb2… drh 1205 (zWikiName && g.perm.WrWiki) ||
045deb2… drh 1206 (zTNUuid && g.perm.Write && g.perm.WrWiki))
ba418ee… drh 1207 ){
5079ffb… stephan 1208 /* Delete attachment. */
7c2577b… drh 1209 int i, n, rid;
5079ffb… stephan 1210 char *zNewDate;
7c2577b… drh 1211 Blob manifest;
7c2577b… drh 1212 Blob cksum;
ba418ee… drh 1213 const char *zFile = zName;
7c2577b… drh 1214
5079ffb… stephan 1215 if( !bUserIsOwner ){
5079ffb… stephan 1216 if( zForumPost ? !forumpost_may_close() : !g.perm.Admin ){
ca90659… stephan 1217 webpage_error(
ca90659… stephan 1218 "Only admins can delete other users' attachments."
ca90659… stephan 1219 );
5079ffb… stephan 1220 }
5079ffb… stephan 1221 }
e1962ef… drh 1222 db_begin_transaction();
7c2577b… drh 1223 blob_zero(&manifest);
7c2577b… drh 1224 for(i=n=0; zFile[i]; i++){
7c2577b… drh 1225 if( zFile[i]=='/' || zFile[i]=='\\' ) n = i;
7c2577b… drh 1226 }
7c2577b… drh 1227 zFile += n;
7c2577b… drh 1228 if( zFile[0]==0 ) zFile = "unknown";
7c2577b… drh 1229 blob_appendf(&manifest, "A %F %F\n", zFile, zTarget);
5079ffb… stephan 1230 zNewDate = date_in_standard_format("now");
5079ffb… stephan 1231 blob_appendf(&manifest, "D %s\n", zNewDate);
840b762… drh 1232 blob_appendf(&manifest, "U %F\n", login_name());
7c2577b… drh 1233 md5sum_blob(&manifest, &cksum);
7c2577b… drh 1234 blob_appendf(&manifest, "Z %b\n", &cksum);
1b45161… drh 1235 rid = content_put(&manifest);
1311841… jan.nijtmans 1236 manifest_crosslink(rid, &manifest, MC_NONE);
7c2577b… drh 1237 db_end_transaction(0);
ba418ee… drh 1238 @ <p>The attachment below has been deleted.</p>
5079ffb… stephan 1239 fossil_free(zNewDate);
ba418ee… drh 1240 }
ba418ee… drh 1241
ba418ee… drh 1242 if( P("del")
ca90659… stephan 1243 && ((zForumPost && (bUserIsOwner || forumpost_may_close()))
ca90659… stephan 1244 || (zTktUuid && g.perm.WrTkt)
ca90659… stephan 1245 || (zWikiName && g.perm.WrWiki)
ca90659… stephan 1246 || (zTNUuid && g.perm.Write && g.perm.WrWiki))
ba418ee… drh 1247 ){
c62e94f… jan.nijtmans 1248 form_begin(0, "%R/ainfo/%!S", zUuid);
ba418ee… drh 1249 @ <p>Confirm you want to delete the attachment shown below.
ba418ee… drh 1250 @ <input type="submit" name="confirm" value="Confirm">
5079ffb… stephan 1251 login_insert_csrf_secret();
ba418ee… drh 1252 @ </form>
ba418ee… drh 1253 }
ba418ee… drh 1254
275457a… jan.nijtmans 1255 isModerator = g.perm.Admin ||
5079ffb… stephan 1256 (zForumPost && g.perm.ModForum) ||
5079ffb… stephan 1257 (zTktUuid && g.perm.ModTkt) ||
5079ffb… stephan 1258 (zWikiName && g.perm.ModWiki);
5079ffb… stephan 1259 zModAction = P("modaction");
5079ffb… stephan 1260 if( zModAction!=0 && cgi_csrf_safe(2) ){
ba418ee… drh 1261 if( strcmp(zModAction,"delete")==0 ){
5079ffb… stephan 1262 if( isModerator || bUserIsOwner ){
5079ffb… stephan 1263 moderation_disapprove(rid);
5079ffb… stephan 1264 }
5079ffb… stephan 1265 if( zForumPost ){
5079ffb… stephan 1266 cgi_redirectf("%R/forumpost/%!S", zForumPost);
5079ffb… stephan 1267 }else if( zTktUuid ){
c62e94f… jan.nijtmans 1268 cgi_redirectf("%R/tktview/%!S", zTktUuid);
5079ffb… stephan 1269 }else if( zWikiName ) {
ba418ee… drh 1270 cgi_redirectf("%R/wiki?name=%t", zWikiName);
ba418ee… drh 1271 }
5079ffb… stephan 1272 /* zTNUuid is intentionally unhandled. Tech note attachments
5079ffb… stephan 1273 ** don't go through moderation. */
ba418ee… drh 1274 return;
ba418ee… drh 1275 }
5079ffb… stephan 1276 if( isModerator && strcmp(zModAction,"approve")==0 ){
5e28feb… drh 1277 moderation_approve('a', rid);
ba418ee… drh 1278 }
ba418ee… drh 1279 }
112c713… drh 1280 style_set_current_feature("attach");
ba418ee… drh 1281 style_header("Attachment Details");
187424e… andygoth 1282 style_submenu_element("Raw", "%R/artifact/%s", zUuid);
a5b1bef… stephan 1283 if(fShowContent){
187424e… andygoth 1284 style_submenu_element("Line Numbers", "%R/ainfo/%s%s", zUuid,
a5b1bef… stephan 1285 ((zLn&&*zLn) ? "" : "?ln=0"));
a5b1bef… stephan 1286 }
ba418ee… drh 1287
ba418ee… drh 1288 @ <div class="section">Overview</div>
ba418ee… drh 1289 @ <p><table class="label-value">
ba418ee… drh 1290 @ <tr><th>Artifact&nbsp;ID:</th>
1fee037… drh 1291 @ <td>%z(href("%R/artifact/%!S",zUuid))%s(zUuid)</a>
ba418ee… drh 1292 if( g.perm.Setup ){
ba418ee… drh 1293 @ (%d(rid))
ba418ee… drh 1294 }
99fcc43… drh 1295 modPending = moderation_pending_www(rid);
5079ffb… stephan 1296 if( zForumPost ){
5079ffb… stephan 1297 @ <tr><th>Forum&nbsp;Post:</th>
ca90659… stephan 1298 @ <td>%z(href("%R/forumpost/%s",zForumPost))%h(zForumPost)</a>\
ca90659… stephan 1299 @ </td></tr>
5079ffb… stephan 1300 }else if( zTktUuid ){
ba418ee… drh 1301 @ <tr><th>Ticket:</th>
ba418ee… drh 1302 @ <td>%z(href("%R/tktview/%s",zTktUuid))%s(zTktUuid)</a></td></tr>
5079ffb… stephan 1303 }else if( zTNUuid ){
045deb2… drh 1304 @ <tr><th>Tech Note:</th>
045deb2… drh 1305 @ <td>%z(href("%R/technote/%s",zTNUuid))%s(zTNUuid)</a></td></tr>
5079ffb… stephan 1306 }else if( zWikiName ){
ba418ee… drh 1307 @ <tr><th>Wiki&nbsp;Page:</th>
ca90659… stephan 1308 @ <td>%z(href("%R/wiki?name=%t",zWikiName))%h(zWikiName)</a>\
ca90659… stephan 1309 @ </td></tr>
ba418ee… drh 1310 }
ba418ee… drh 1311 @ <tr><th>Date:</th><td>
ba418ee… drh 1312 hyperlink_to_date(zDate, "</td></tr>");
ba418ee… drh 1313 @ <tr><th>User:</th><td>
ba418ee… drh 1314 hyperlink_to_user(pAttach->zUser, zDate, "</td></tr>");
ba418ee… drh 1315 @ <tr><th>Artifact&nbsp;Attached:</th>
ba418ee… drh 1316 @ <td>%z(href("%R/artifact/%s",zSrc))%s(zSrc)</a>
ba418ee… drh 1317 if( g.perm.Setup ){
ba418ee… drh 1318 @ (%d(ridSrc))
ba418ee… drh 1319 }
ba418ee… drh 1320 @ <tr><th>Filename:</th><td>%h(zName)</td></tr>
ba418ee… drh 1321 if( g.perm.Setup ){
ba418ee… drh 1322 @ <tr><th>MIME-Type:</th><td>%h(zMime)</td></tr>
ba418ee… drh 1323 }
ca90659… stephan 1324 @ <tr><th valign="top">Description:</th>\
ca90659… stephan 1325 /* FIXME (2026-06-05): Honor the N-card (comment mimetype). */
ca90659… stephan 1326 @ <td valign="top">%h(zDesc)</td></tr>
ba418ee… drh 1327 @ </table>
275457a… jan.nijtmans 1328
5079ffb… stephan 1329 if( modPending && (isModerator || bUserIsOwner) ){
ba418ee… drh 1330 @ <div class="section">Moderation</div>
ba418ee… drh 1331 @ <blockquote>
dfa3579… drh 1332 form_begin(0, "%R/ainfo/%s", zUuid);
ba418ee… drh 1333 @ <label><input type="radio" name="modaction" value="delete">
5079ffb… stephan 1334 @ Delete this attachment</label><br>
5079ffb… stephan 1335 if( isModerator ){
ca90659… stephan 1336 #if 0
ca90659… stephan 1337 /* TODO/FIXME (2026-06-03): only allow approval of an attachment
ca90659… stephan 1338 ** if its target has been approved. Without this, we can end up
ca90659… stephan 1339 ** with stale attachments which refer to rejected targets. We
ca90659… stephan 1340 ** need a type-specific RID/UUID here, which requires
ca90659… stephan 1341 ** refactoring above to get it. */
ca90659… stephan 1342 const int tgtid = 0;
ca90659… stephan 1343 if( moderation_pending(tgtid) ){
ca90659… stephan 1344 @ <label><input type="radio" name="modaction" \
ca90659… stephan 1345 @ disabled value="approve">
ca90659… stephan 1346 @ <span class='modpending'>Cannot approve:
ca90659… stephan 1347 @ target is pending moderation</span>\
ca90659… stephan 1348 @ </label><br>
ca90659… stephan 1349 }else
ca90659… stephan 1350 #else
ca90659… stephan 1351 {
ca90659… stephan 1352 @ <label><input type="radio" name="modaction" value="approve">
ca90659… stephan 1353 @ Approve this attachment</label><br>
ca90659… stephan 1354 }
ca90659… stephan 1355 #endif
5079ffb… stephan 1356 }
ba418ee… drh 1357 @ <input type="submit" value="Submit">
5079ffb… stephan 1358 login_insert_csrf_secret();
ba418ee… drh 1359 @ </form>
ba418ee… drh 1360 @ </blockquote>
ba418ee… drh 1361 }
ba418ee… drh 1362
5079ffb… stephan 1363 @ <div class="section">Content:</div>
ba418ee… drh 1364 blob_zero(&attach);
5079ffb… stephan 1365 if( modPending && !moderation_user_could(rid, 1, 0) ){
5079ffb… stephan 1366 @ <p><span class="modpending">Content is awaiting moderator \
5079ffb… stephan 1367 @ approval.</span></p>
5079ffb… stephan 1368 }else{
5079ffb… stephan 1369 @ <blockquote>
5079ffb… stephan 1370 if( fShowContent ){
5079ffb… stephan 1371 const char *z;
5079ffb… stephan 1372 content_get(ridSrc, &attach);
5079ffb… stephan 1373 blob_to_utf8_no_bom(&attach, 0);
5079ffb… stephan 1374 z = blob_str(&attach);
5079ffb… stephan 1375 if( zLn ){
ca90659… stephan 1376 output_text_with_line_numbers(z, blob_size(&attach),
ca90659… stephan 1377 zName, zLn, 1);
5079ffb… stephan 1378 }else{
5079ffb… stephan 1379 @ <pre>
5079ffb… stephan 1380 @ %h(z)
5079ffb… stephan 1381 @ </pre>
5079ffb… stephan 1382 }
5079ffb… stephan 1383 }else if( strncmp(zMime, "image/", 6)==0 ){
5079ffb… stephan 1384 int sz = db_int(0, "SELECT size FROM blob WHERE rid=%d", ridSrc);
5079ffb… stephan 1385 @ <i>(file is %d(sz) bytes of image data)</i><br>
5079ffb… stephan 1386 @ <img src="%R/raw/%s(zSrc)?m=%s(zMime)"></img>
5079ffb… stephan 1387 style_submenu_element("Image", "%R/raw/%s?m=%s", zSrc, zMime);
5079ffb… stephan 1388 }else{
5079ffb… stephan 1389 int sz = db_int(0, "SELECT size FROM blob WHERE rid=%d", ridSrc);
5079ffb… stephan 1390 @ <i>(file is %d(sz) bytes of binary data)</i>
5079ffb… stephan 1391 }
5079ffb… stephan 1392 @ </blockquote>
5079ffb… stephan 1393 }
ba418ee… drh 1394 manifest_destroy(pAttach);
ba418ee… drh 1395 blob_reset(&attach);
112c713… drh 1396 style_finish_page();
ba418ee… drh 1397 }
5079ffb… stephan 1398
5079ffb… stephan 1399 #if INTERFACE
5079ffb… stephan 1400 /*
5079ffb… stephan 1401 ** Flags for use with attachment_list(). ATTACHLIST_HRULE_ABOVE
5079ffb… stephan 1402 ** must have a value of 1 for historical call compatibility.
5079ffb… stephan 1403 */
5079ffb… stephan 1404 #define ATTACHLIST_HRULE_ABOVE 0x01 /* Insert <hr> above header */
5079ffb… stephan 1405 #define ATTACHLIST_TARGET_BLANK 0x02 /* use target=_blank for links */
5079ffb… stephan 1406 #define ATTACHLIST_SIZE 0x04 /* add size */
5079ffb… stephan 1407 #define ATTACHLIST_HIDE_UNAPPROVED 0x08 /* Hide pending-moderation files */
ca90659… stephan 1408 #define ATTACHLIST_DETAILS_CLOSED 0x10 /* Wrap in a closed DETAILS element */
ca90659… stephan 1409 #define ATTACHLIST_DETAILS_OPEN 0x20 /* Wrap in an open DETAILS element */
ca90659… stephan 1410 #define ATTACHLIST_HIDE_EMPTY 0x40 /* Skip if size<1 */
5079ffb… stephan 1411 #endif
ba418ee… drh 1412
ba418ee… drh 1413 /*
ba418ee… drh 1414 ** Output HTML to show a list of attachments.
ba418ee… drh 1415 */
ba418ee… drh 1416 void attachment_list(
ba418ee… drh 1417 const char *zTarget, /* Object that things are attached to */
4f9ecd0… stephan 1418 const char *zHeader, /* Header to display with attachments */
5079ffb… stephan 1419 const int flags /* ATTACHLIST_... flags */
ba418ee… drh 1420 ){
ba418ee… drh 1421 int cnt = 0;
5079ffb… stephan 1422 char szBuf[36] = {0}; /* scratchpad for attachment size value */
ca90659… stephan 1423 const char *zLinkTgt = (ATTACHLIST_TARGET_BLANK & flags)
5079ffb… stephan 1424 ? " target=\"_blank\"" : "";
ca90659… stephan 1425 const int bUseDetail = flags &
ca90659… stephan 1426 (ATTACHLIST_DETAILS_CLOSED | ATTACHLIST_DETAILS_OPEN);
ba418ee… drh 1427 Stmt q;
ca90659… stephan 1428
ba418ee… drh 1429 db_prepare(&q,
ca90659… stephan 1430 "SELECT datetime(mtime,toLocal()), a.filename, a.user,"
ca90659… stephan 1431 " b1.uuid, a.src, a.target, a.attachid, b2.size\n"
ca90659… stephan 1432 " FROM attachment a, blob b1, blob b2\n"
ca90659… stephan 1433 " WHERE a.isLatest\n"
ca90659… stephan 1434 " AND a.src IS NOT NULL\n"
ca90659… stephan 1435 " AND a.target=%Q\n"
ca90659… stephan 1436 " AND b1.rid=a.attachid\n"
ca90659… stephan 1437 " AND b2.uuid=a.src\n"
275457a… jan.nijtmans 1438 " ORDER BY mtime DESC",
ea63a2d… drh 1439 zTarget
ba418ee… drh 1440 );
ba418ee… drh 1441 while( db_step(&q)==SQLITE_ROW ){
ba418ee… drh 1442 const char *zDate = db_column_text(&q, 0);
ba418ee… drh 1443 const char *zFile = db_column_text(&q, 1);
ba418ee… drh 1444 const char *zUser = db_column_text(&q, 2);
ba418ee… drh 1445 const char *zUuid = db_column_text(&q, 3);
ba418ee… drh 1446 const char *zSrc = db_column_text(&q, 4);
5079ffb… stephan 1447 const char *zTarget = db_column_text(&q, 5);
1570857… drh 1448 const char *zDispUser = zUser && zUser[0] ? zUser : "anonymous";
5079ffb… stephan 1449 const char *zTypeArg = 0; /* URL arg name for /attachdownload */
5079ffb… stephan 1450 const int aid = db_column_int(&q, 6);
ca90659… stephan 1451 const int sz = db_column_int(&q, 7);
5079ffb… stephan 1452 if( (flags & ATTACHLIST_HIDE_UNAPPROVED)
5079ffb… stephan 1453 && moderation_pending(aid)
5079ffb… stephan 1454 && !moderation_user_could(aid, 1, 0) ){
5079ffb… stephan 1455 continue;
5079ffb… stephan 1456 }
ca90659… stephan 1457 if( sz<1 && (flags & ATTACHLIST_HIDE_EMPTY) ){
ca90659… stephan 1458 /* Deleted or phantom items. */
ca90659… stephan 1459 continue;
ca90659… stephan 1460 }
ba418ee… drh 1461 if( cnt==0 ){
ca90659… stephan 1462 if( bUseDetail ){
ca90659… stephan 1463 @ <details class='attachlist'
ca90659… stephan 1464 if( ATTACHLIST_DETAILS_OPEN & flags ){
ca90659… stephan 1465 @ open
ca90659… stephan 1466 }
ca90659… stephan 1467 @ >
ca90659… stephan 1468 }else{
ca90659… stephan 1469 @ <section class='attachlist'>
ca90659… stephan 1470 }
5079ffb… stephan 1471 if( flags & ATTACHLIST_HRULE_ABOVE ){
4f9ecd0… stephan 1472 @ <hr>
4f9ecd0… stephan 1473 }
ca90659… stephan 1474 if( bUseDetail ){
ca90659… stephan 1475 @ <summary>%s(zHeader)</summary>
ca90659… stephan 1476 }else{
ca90659… stephan 1477 @ %s(zHeader)
ca90659… stephan 1478 }
4f9ecd0… stephan 1479 @ <ul>
ba418ee… drh 1480 }
ba418ee… drh 1481 cnt++;
ca90659… stephan 1482 switch( attachment_target_type(zTarget, 1) ){
5079ffb… stephan 1483 case CFTYPE_TICKET: zTypeArg = "tkt"; break;
5079ffb… stephan 1484 case CFTYPE_FORUM: zTypeArg = "forumpost"; break;
5079ffb… stephan 1485 case CFTYPE_EVENT: zTypeArg = "technote"; break;
5079ffb… stephan 1486 case CFTYPE_WIKI:
5079ffb… stephan 1487 default: zTypeArg = "page"; break;
5079ffb… stephan 1488 }
ba418ee… drh 1489 @ <li>
5079ffb… stephan 1490 @ <a href="%R/artifact/%!S(zSrc)"%s(zLinkTgt)>%h(zFile)</a>
5079ffb… stephan 1491 if( flags & ATTACHLIST_SIZE ){
5079ffb… stephan 1492 sqlite3_snprintf(sizeof(szBuf), szBuf, " %d bytes", sz);
5079ffb… stephan 1493 }
5079ffb… stephan 1494 @ [<a href="%R/attachdownload/%t(zFile)?%s(zTypeArg)=%t(zTarget)\
5079ffb… stephan 1495 @&file=%t(zFile)%s(zLinkTgt)">download</a>%s(szBuf)]
1570857… drh 1496 @ added by %h(zDispUser) on
ba418ee… drh 1497 hyperlink_to_date(zDate, ".");
5079ffb… stephan 1498 @ [<a href="%R/ainfo/%!S(zUuid)"%s(zLinkTgt)>details</a>]
5079ffb… stephan 1499 moderation_pending_www(aid);
ba418ee… drh 1500 @ </li>
ba418ee… drh 1501 }
ba418ee… drh 1502 if( cnt ){
ba418ee… drh 1503 @ </ul>
ca90659… stephan 1504 if( bUseDetail ){
ca90659… stephan 1505 @ </details>
ca90659… stephan 1506 }else{
ca90659… stephan 1507 @ </section>
ca90659… stephan 1508 }
05cd9fa… rberteig 1509 }
05cd9fa… rberteig 1510 db_finalize(&q);
05cd9fa… rberteig 1511 }
05cd9fa… rberteig 1512
05cd9fa… rberteig 1513 /*
05cd9fa… rberteig 1514 ** COMMAND: attachment*
05cd9fa… rberteig 1515 **
05cd9fa… rberteig 1516 ** Usage: %fossil attachment add ?PAGENAME? FILENAME ?OPTIONS?
05cd9fa… rberteig 1517 **
e58c76a… drh 1518 ** Add an attachment to an existing wiki page or tech note.
e58c76a… drh 1519 **
2512d2d… km 1520 ** Options:
e58c76a… drh 1521 ** -t|--technote DATETIME Specifies the timestamp of
e58c76a… drh 1522 ** the technote to which the attachment
e58c76a… drh 1523 ** is to be made. The attachment will be
e58c76a… drh 1524 ** to the most recently modified tech note
e58c76a… drh 1525 ** with the specified timestamp.
e58c76a… drh 1526 ** -t|--technote TECHNOTE-ID Specifies the technote to be
2512d2d… km 1527 ** updated by its technote id
e58c76a… drh 1528 **
e58c76a… drh 1529 ** One of PAGENAME, DATETIME or TECHNOTE-ID must be specified.
cd77e06… rberteig 1530 **
cd77e06… rberteig 1531 ** DATETIME may be "now" or "YYYY-MM-DDTHH:MM:SS.SSS". If in
cd77e06… rberteig 1532 ** year-month-day form, it may be truncated, the "T" may be replaced by
cd77e06… rberteig 1533 ** a space, and it may also name a timezone offset from UTC as "-HH:MM"
cd77e06… rberteig 1534 ** (westward) or "+HH:MM" (eastward). Either no timezone suffix or "Z"
cd77e06… rberteig 1535 ** means UTC.
05cd9fa… rberteig 1536 */
05cd9fa… rberteig 1537 void attachment_cmd(void){
05cd9fa… rberteig 1538 int n;
05cd9fa… rberteig 1539 db_find_and_open_repository(0, 0);
05cd9fa… rberteig 1540 if( g.argc<3 ){
05cd9fa… rberteig 1541 goto attachment_cmd_usage;
05cd9fa… rberteig 1542 }
05cd9fa… rberteig 1543 n = strlen(g.argv[2]);
05cd9fa… rberteig 1544 if( n==0 ){
05cd9fa… rberteig 1545 goto attachment_cmd_usage;
05cd9fa… rberteig 1546 }
05cd9fa… rberteig 1547
05cd9fa… rberteig 1548 if( strncmp(g.argv[2],"add",n)==0 ){
d037468… mistachkin 1549 const char *zPageName = 0; /* Name of the wiki page to attach to */
05cd9fa… rberteig 1550 const char *zFile; /* Name of the file to be attached */
05cd9fa… rberteig 1551 const char *zETime; /* The name of the technote to attach to */
05cd9fa… rberteig 1552 Manifest *pWiki = 0; /* Parsed wiki page content */
05cd9fa… rberteig 1553 char *zBody = 0; /* Wiki page content */
05cd9fa… rberteig 1554 int rid;
05cd9fa… rberteig 1555 const char *zTarget; /* Target of the attachment */
05cd9fa… rberteig 1556 Blob content; /* The content of the attachment */
05cd9fa… rberteig 1557 zETime = find_option("technote","t",1);
05cd9fa… rberteig 1558 if( !zETime ){
05cd9fa… rberteig 1559 if( g.argc!=5 ){
05cd9fa… rberteig 1560 usage("add PAGENAME FILENAME");
05cd9fa… rberteig 1561 }
05cd9fa… rberteig 1562 zPageName = g.argv[3];
05cd9fa… rberteig 1563 rid = db_int(0, "SELECT x.rid FROM tag t, tagxref x"
05cd9fa… rberteig 1564 " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
05cd9fa… rberteig 1565 " ORDER BY x.mtime DESC LIMIT 1",
05cd9fa… rberteig 1566 zPageName
bd559ff… jan.nijtmans 1567 );
05cd9fa… rberteig 1568 if( (pWiki = manifest_get(rid, CFTYPE_WIKI, 0))!=0 ){
05cd9fa… rberteig 1569 zBody = pWiki->zWiki;
05cd9fa… rberteig 1570 }
05cd9fa… rberteig 1571 if( zBody==0 ){
05cd9fa… rberteig 1572 fossil_fatal("wiki page [%s] not found",zPageName);
05cd9fa… rberteig 1573 }
05cd9fa… rberteig 1574 zTarget = zPageName;
05cd9fa… rberteig 1575 zFile = g.argv[4];
05cd9fa… rberteig 1576 }else{
05cd9fa… rberteig 1577 if( g.argc!=4 ){
05cd9fa… rberteig 1578 usage("add FILENAME --technote DATETIME|TECHNOTE-ID");
05cd9fa… rberteig 1579 }
05cd9fa… rberteig 1580 rid = wiki_technote_to_rid(zETime);
05cd9fa… rberteig 1581 if( rid<0 ){
05cd9fa… rberteig 1582 fossil_fatal("ambiguous tech note id: %s", zETime);
05cd9fa… rberteig 1583 }
05cd9fa… rberteig 1584 if( (pWiki = manifest_get(rid, CFTYPE_EVENT, 0))!=0 ){
05cd9fa… rberteig 1585 zBody = pWiki->zWiki;
05cd9fa… rberteig 1586 }
05cd9fa… rberteig 1587 if( zBody==0 ){
05cd9fa… rberteig 1588 fossil_fatal("technote [%s] not found",zETime);
05cd9fa… rberteig 1589 }
05cd9fa… rberteig 1590 zTarget = db_text(0,
275da70… danield 1591 "SELECT substr(tagname,7) FROM tag "
275da70… danield 1592 " WHERE tagid=(SELECT tagid FROM event WHERE objid='%d')",
05cd9fa… rberteig 1593 rid
05cd9fa… rberteig 1594 );
05cd9fa… rberteig 1595 zFile = g.argv[3];
05cd9fa… rberteig 1596 }
1772357… drh 1597 blob_read_from_file(&content, zFile, ExtFILE);
05cd9fa… rberteig 1598 user_select();
05cd9fa… rberteig 1599 attach_commit(
05cd9fa… rberteig 1600 zFile, /* The filename of the attachment */
8ad5e46… wyoung 1601 zTarget, /* The artifact hash to attach to */
05cd9fa… rberteig 1602 blob_buffer(&content), /* The content of the attachment */
05cd9fa… rberteig 1603 blob_size(&content), /* The length of the attachment */
05cd9fa… rberteig 1604 0, /* No need to moderate the attachment */
05cd9fa… rberteig 1605 "" /* Empty attachment comment */
05cd9fa… rberteig 1606 );
05cd9fa… rberteig 1607 if( !zETime ){
05cd9fa… rberteig 1608 fossil_print("Attached %s to wiki page %s.\n", zFile, zPageName);
05cd9fa… rberteig 1609 }else{
05cd9fa… rberteig 1610 fossil_print("Attached %s to tech note %s.\n", zFile, zETime);
05cd9fa… rberteig 1611 }
05cd9fa… rberteig 1612 }else{
05cd9fa… rberteig 1613 goto attachment_cmd_usage;
05cd9fa… rberteig 1614 }
05cd9fa… rberteig 1615 return;
05cd9fa… rberteig 1616
05cd9fa… rberteig 1617 attachment_cmd_usage:
05cd9fa… rberteig 1618 usage("add ?PAGENAME? FILENAME [-t|--technote DATETIME ]");
a057766… stephan 1619 }
a057766… stephan 1620
a057766… stephan 1621
a057766… stephan 1622 /*
a057766… stephan 1623 ** COMMAND: test-list-attachments
a057766… stephan 1624 **
a057766… stephan 1625 ** Usage: %fossil test-list-attachments ?-latest? ?TargetName(s)...?
a057766… stephan 1626 **
a057766… stephan 1627 ** List attachments for one or more attachment targets. The target
a057766… stephan 1628 ** name arguments are glob prefixes for the attachment.target
a057766… stephan 1629 ** field. If no names are provided then a prefix of [a-zA-Z] is used,
a057766… stephan 1630 ** which will match most wiki page names and some ticket hashes.
a057766… stephan 1631 **
a057766… stephan 1632 ** Options:
2512d2d… km 1633 ** -latest List only the latest version of a given attachment
a057766… stephan 1634 **
a057766… stephan 1635 */
a057766… stephan 1636 void test_list_attachments(void){
a057766… stephan 1637 Stmt q;
a057766… stephan 1638 int i;
a057766… stephan 1639 const int fLatest = find_option("latest", 0, 0) != 0;
a057766… stephan 1640
a057766… stephan 1641 db_find_and_open_repository(0, 0);
a057766… stephan 1642 verify_all_options();
a057766… stephan 1643 db_prepare(&q,
a057766… stephan 1644 "SELECT datetime(mtime,toLocal()), src, target, filename,"
a057766… stephan 1645 " comment, user "
a057766… stephan 1646 " FROM attachment"
a057766… stephan 1647 " WHERE target GLOB :tgtname ||'*'"
a057766… stephan 1648 " AND (isLatest OR %d)"
a057766… stephan 1649 " ORDER BY target, isLatest DESC, mtime DESC",
a057766… stephan 1650 !fLatest
a057766… stephan 1651 );
a057766… stephan 1652 if(g.argc<3){
a057766… stephan 1653 static char * argv[3] = {0,0,"[a-zA-Z]"};
a057766… stephan 1654 g.argc = 3;
a057766… stephan 1655 g.argv = argv;
a057766… stephan 1656 }
a057766… stephan 1657 for(i = 2; i < g.argc; ++i){
a057766… stephan 1658 const char *zPage = g.argv[i];
a057766… stephan 1659 db_bind_text(&q, ":tgtname", zPage);
a057766… stephan 1660 while(SQLITE_ROW == db_step(&q)){
ca90659… stephan 1661 const char *zTime = db_column_text(&q, 0);
ca90659… stephan 1662 const char *zSrc = db_column_text(&q, 1);
ca90659… stephan 1663 const char *zTarget = db_column_text(&q, 2);
ca90659… stephan 1664 const char *zName = db_column_text(&q, 3);
a057766… stephan 1665 printf("%-20s %s %.12s %s\n", zTarget, zTime, zSrc, zName);
a057766… stephan 1666 }
a057766… stephan 1667 db_reset(&q);
a057766… stephan 1668 }
a057766… stephan 1669 db_finalize(&q);
ca90659… stephan 1670 }
ca90659… stephan 1671
ca90659… stephan 1672 /*
ca90659… stephan 1673 ** Renders the list of attachments for artifact pManifest as JSON to
ca90659… stephan 1674 ** blob pOut. If pManifest->type is not one of (CFTYPE_TICKET,
ca90659… stephan 1675 ** CFTYPE_FORUM, CFTYPE_EVENT, CFTYPE_WIKI) then it behaves as if the
ca90659… stephan 1676 ** result set is empty.
ca90659… stephan 1677 **
ca90659… stephan 1678 ** If there are no matching attachments then its behavior depends on
ca90659… stephan 1679 ** emptyPolicy:
ca90659… stephan 1680 **
ca90659… stephan 1681 ** <0 = emit a JSON NULL
ca90659… stephan 1682 ** 0 = emit no output
ca90659… stephan 1683 ** >0 = emit an empty JSON array
ca90659… stephan 1684 **
ca90659… stephan 1685 ** If bLatestOnly is true then only the most recent entry for a given
ca90659… stephan 1686 ** attachment is emitted, else all versions are emitted in descending
ca90659… stephan 1687 ** mtime order.
ca90659… stephan 1688 **
ca90659… stephan 1689 ** Returns the number of attachments.
ca90659… stephan 1690 **
ca90659… stephan 1691 ** Output format:
ca90659… stephan 1692 **
ca90659… stephan 1693 ** [{
ca90659… stephan 1694 ** "uuid": attachment artifact hash,
ca90659… stephan 1695 ** "src": hash of the attachment blob,
ca90659… stephan 1696 ** "target": wiki page name or ticket/event ID,
ca90659… stephan 1697 ** "filename": filename of attachment,
ca90659… stephan 1698 ** "mtime": ISO-8601 timestamp UTC,
ca90659… stephan 1699 ** "isLatest": true if this is the latest version of this file
ca90659… stephan 1700 ** else false,
ca90659… stephan 1701 ** }, ...once per attachment]
ca90659… stephan 1702 **
ca90659… stephan 1703 */
ca90659… stephan 1704 int attachments_to_json(const Manifest *pManifest,
ca90659… stephan 1705 Blob *pOut, int bLatestOnly,
ca90659… stephan 1706 int emptyPolicy){
ca90659… stephan 1707 int i = 0;
ca90659… stephan 1708 Stmt q = empty_Stmt;
ca90659… stephan 1709 char *zToFree = 0;
ca90659… stephan 1710 const char *zTgt = 0;
ca90659… stephan 1711 switch(pManifest->type){
ca90659… stephan 1712 case CFTYPE_FORUM: zTgt = zToFree = rid_to_uuid(pManifest->rid);
ca90659… stephan 1713 break;
ca90659… stephan 1714 case CFTYPE_WIKI: zTgt = pManifest->zWikiTitle; break;
ca90659… stephan 1715 case CFTYPE_EVENT: zTgt = pManifest->zEventId; break;
ca90659… stephan 1716 case CFTYPE_TICKET: zTgt = pManifest->zTicketUuid; break;
ca90659… stephan 1717 default:
ca90659… stephan 1718 goto empty_result;
ca90659… stephan 1719 }
ca90659… stephan 1720 db_prepare(&q,
ca90659… stephan 1721 "SELECT datetime(mtime), a.src, a.target, a.filename, a.isLatest,\n"
ca90659… stephan 1722 " b2.size, b1.uuid, a.user, a.comment\n"
ca90659… stephan 1723 " FROM attachment a, blob b1, blob b2\n"
ca90659… stephan 1724 " WHERE a.target=%Q\n"
ca90659… stephan 1725 " AND a.src IS NOT NULL\n"
ca90659… stephan 1726 " AND b1.rid=a.attachid\n"
ca90659… stephan 1727 " AND b2.uuid=a.src\n"
ca90659… stephan 1728 " AND (a.isLatest OR %d)\n"
ca90659… stephan 1729 " ORDER BY a.target, a.isLatest DESC, a.mtime DESC\n",
ca90659… stephan 1730 zTgt, !bLatestOnly
ca90659… stephan 1731 );
ca90659… stephan 1732 while(SQLITE_ROW == db_step(&q)){
ca90659… stephan 1733 const char *zTime = db_column_text(&q, 0);
ca90659… stephan 1734 const char *zSrc = db_column_text(&q, 1);
ca90659… stephan 1735 const char *zTarget = db_column_text(&q, 2);
ca90659… stephan 1736 const char *zName = db_column_text(&q, 3);
ca90659… stephan 1737 const int isLatest = db_column_int(&q, 4);
ca90659… stephan 1738 const int sz = db_column_int(&q, 5);
ca90659… stephan 1739 const char *zUuid = db_column_text(&q, 6);
ca90659… stephan 1740 const char *zUser = db_column_text(&q, 7);
ca90659… stephan 1741 const char *zComment = db_column_text(&q, 8);
ca90659… stephan 1742 if(!i++){
ca90659… stephan 1743 blob_append_char(pOut, '[');
ca90659… stephan 1744 }else{
ca90659… stephan 1745 blob_append_char(pOut, ',');
ca90659… stephan 1746 }
ca90659… stephan 1747 blob_appendf(
ca90659… stephan 1748 pOut,
ca90659… stephan 1749 "{\"uuid\": %!j, \"src\": %!j, \"target\": %!j, "
ca90659… stephan 1750 "\"filename\": %!j, \"size\":%d, \"mtime\": %!j, "
ca90659… stephan 1751 "\"isLatest\": %s, \"user\": %!j, \"comment\": ",
ca90659… stephan 1752 zUuid, zSrc, zTarget,
ca90659… stephan 1753 zName, sz, zTime, isLatest ? "true" : "false",
ca90659… stephan 1754 zUser
ca90659… stephan 1755 );
ca90659… stephan 1756 if( zComment && zComment[0] ){
ca90659… stephan 1757 blob_appendf(pOut, "%!j", zComment);
ca90659… stephan 1758 }else{
ca90659… stephan 1759 blob_append_literal(pOut, "null");
ca90659… stephan 1760 }
ca90659… stephan 1761 blob_append_char(pOut, '}');
ca90659… stephan 1762 }
ca90659… stephan 1763 fossil_free(zToFree);
ca90659… stephan 1764 db_finalize(&q);
ca90659… stephan 1765 if(!i){
ca90659… stephan 1766 empty_result:
ca90659… stephan 1767 if( emptyPolicy>0 ){
ca90659… stephan 1768 blob_append_literal(pOut, "[]");
ca90659… stephan 1769 }else if( emptyPolicy<0 ){
ca90659… stephan 1770 blob_append_literal(pOut, "null");
ca90659… stephan 1771 }
ca90659… stephan 1772 }else{
ca90659… stephan 1773 blob_append_char(pOut, ']');
ca90659… stephan 1774 }
ca90659… stephan 1775 return i;
ca90659… stephan 1776 }
ca90659… stephan 1777
ca90659… stephan 1778 /*
ca90659… stephan 1779 ** COMMAND: test-attachment-target
ca90659… stephan 1780 **
ca90659… stephan 1781 ** Usage: %fossil test-attachment-target TARGET_ID...
ca90659… stephan 1782 */
ca90659… stephan 1783 void test_attachment_target_type_cmd(void){
ca90659… stephan 1784 int i;
ca90659… stephan 1785 verify_all_options();
ca90659… stephan 1786 db_find_and_open_repository(0, 0);
ca90659… stephan 1787 if( g.argc<3 ){
ca90659… stephan 1788 usage("test-attachment-target TARGET_ID");
ca90659… stephan 1789 return;
ca90659… stephan 1790 }
ca90659… stephan 1791 for( i = 2; i < g.argc; ++i ){
ca90659… stephan 1792 const char *zTarget = g.argv[i];
ca90659… stephan 1793 const int rid = attachment_target_rid(zTarget, 0);
ca90659… stephan 1794 const int type = attachment_target_type(zTarget, 0);
ca90659… stephan 1795 const char *zType = "<invalid>";
ca90659… stephan 1796 switch(type){
ca90659… stephan 1797 case CFTYPE_EVENT: zType = "technote"; break;
ca90659… stephan 1798 case CFTYPE_FORUM: zType = "forumpost"; break;
ca90659… stephan 1799 case CFTYPE_TICKET: zType = "ticket"; break;
ca90659… stephan 1800 case CFTYPE_WIKI: zType = "wiki"; break;
ca90659… stephan 1801 }
ca90659… stephan 1802 fossil_print("%-20s = %-9s #%d %z\n",
ca90659… stephan 1803 zTarget, zType, rid,
ca90659… stephan 1804 rid>0 ? rid_to_uuid(rid) : 0);
ca90659… stephan 1805 }
ca90659… stephan 1806 }
ca90659… stephan 1807
ca90659… stephan 1808
ca90659… stephan 1809 /*
ca90659… stephan 1810 ** COMMAND: test-attachments-to-json
ca90659… stephan 1811 **
ca90659… stephan 1812 ** Usage: %fossil test-attachments-to-json TARGET_ID...
ca90659… stephan 1813 **
ca90659… stephan 1814 ** Options:
ca90659… stephan 1815 ** --old List all versions of attachments. Default is to
ca90659… stephan 1816 ** list only the latest.
ca90659… stephan 1817 ** --full Require a full target ID, not a prefix.
ca90659… stephan 1818 **
ca90659… stephan 1819 ** Emits a JSON array of attachments for the given attachment targets.
ca90659… stephan 1820 ** The given IDs must be wiki page names, ticket hashes, tech-note
ca90659… stephan 1821 ** hashes, or forum post hashes. By default it accepts hash prefixes
ca90659… stephan 1822 ** but does no detection of ambiguity or cross-type prefix collisions
ca90659… stephan 1823 ** so may emit curious results if given short, colliding IDs.
ca90659… stephan 1824 */
ca90659… stephan 1825 void test_attachments_to_json_cmd(void){
ca90659… stephan 1826 const int emptyPolicy = 1;
ca90659… stephan 1827 const int bLatestOnly = find_option("old",0,0)==0;
ca90659… stephan 1828 const int bFullId = find_option("full",0,0)!=0;
ca90659… stephan 1829 int i;
ca90659… stephan 1830
ca90659… stephan 1831 verify_all_options();
ca90659… stephan 1832 db_find_and_open_repository(0, 0);
ca90659… stephan 1833 if( g.argc<3 ){
ca90659… stephan 1834 usage("test-attachments-to-json TARGET_ID");
ca90659… stephan 1835 return;
ca90659… stephan 1836 }
ca90659… stephan 1837 for( i = 2; i < g.argc; ++i ){
ca90659… stephan 1838 const char *zTarget = g.argv[i];
ca90659… stephan 1839 const int rid = attachment_target_rid(zTarget, bFullId);
ca90659… stephan 1840 if( 0==rid ){
ca90659… stephan 1841 fossil_print("** cannot resolve %s\n", zTarget);
ca90659… stephan 1842 }else{
ca90659… stephan 1843 Blob b = BLOB_INITIALIZER;
ca90659… stephan 1844 Manifest *pManifest = manifest_get(rid, CFTYPE_ANY, NULL);
ca90659… stephan 1845 assert( pManifest );
ca90659… stephan 1846 attachments_to_json(pManifest, &b, bLatestOnly, emptyPolicy);
ca90659… stephan 1847 fossil_print("Attachments for %s: ", zTarget);
ca90659… stephan 1848 if( b.nUsed ){
ca90659… stephan 1849 char *zPretty = db_text(0,"SELECT json_pretty(%B)", &b);
ca90659… stephan 1850 fossil_print("%s\n", zPretty);
ca90659… stephan 1851 fossil_free(zPretty);
ca90659… stephan 1852 }else{
ca90659… stephan 1853 fossil_print("none\n");
ca90659… stephan 1854 }
ca90659… stephan 1855 blob_reset(&b);
ca90659… stephan 1856 manifest_destroy(pManifest);
ca90659… stephan 1857 }
ca90659… stephan 1858 }
7c2577b… drh 1859 }

Keyboard Shortcuts

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