CVSTrac Legacy Code

Check-in [8a6641cd16]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:add some CSRF (http://en.wikipedia.org/wiki/Cross-site_request_forgery) prevention. It's a little over-engineered in places, but the patch itself is non-intrusive enough that this isn't a huge problem.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 8a6641cd16b0203fc08366a9439c16b80c0bc81b
User & Date: cpb 2008-10-21 02:05:17
Context
2008-10-23
16:24
we need to armour the cookie value. Otherwise, it can contain values which aren't legal in a CGI parameter name. check-in: 89bfff7fd9 user: cpb tags: trunk
2008-10-21
02:05
add some CSRF (http://en.wikipedia.org/wiki/Cross-site_request_forgery) prevention. It's a little over-engineered in places, but the patch itself is non-intrusive enough that this isn't a huge problem. check-in: 8a6641cd16 user: cpb tags: trunk
2008-10-11
01:02
(#764) _setup_ permissions should also imply wiki permissions. Simplify the Windows condition slightly. check-in: e1b44eb962 user: cpb tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to attach.c.

125
126
127
128
129
130
131


132
133
134
135
136
137
138
...
199
200
201
202
203
204
205

206
207
208
209
210
211
212
...
351
352
353
354
355
356
357

358
359
360
361
362
363
364
...
371
372
373
374
375
376
377

378
379
380
381
382
383
384
               (size+1023)/1024);
    }else{
      sqlite3 *pDb;
      sqlite3_stmt *pStmt;
      const char *zTail;
      int rc;



      for(z=zName; *z; z++){
        if( (*z=='/' || *z=='\\') && z[1]!=0 ){ zName = &z[1]; }
      }

      /*
      ** In order to insert a blob, we need to drop down to raw SQLite 3
      ** calls.
................................................................................
  @ Description:
  @ (<small>See <a href="#format_hints">formatting hints</a></small>)<br>
  cgi_wikitext("d",20,0);
  @ <br>
  @ <input type="submit" name="ok" value="Add Attachment">
  @ <input type="submit" name="can" value="Cancel">
  @ <input type="hidden" name="tn" value="%h(zPage)">

  @ </form>
  @ <hr>
  @ <a name="format_hints"></a>
  @ <h3>Formatting Hints:</h3>
  append_formatting_hints();
  common_footer();
}
................................................................................
    zDocView = mprintf("wiki?p=%t",az[0]);
  }
  if( P("can") ){
    cgi_redirect(zDocView);
    return;
  }
  if( P("ok") ){

    db_execute("DELETE FROM attachment WHERE atn=%d", atn);
    cgi_redirect(zDocView);
    return;
  }
  common_add_action_item(zDocView, "Cancel");
  common_add_help_item("CvstracAttachment");
  pTm = gmtime(&t);
................................................................................
    @ <br>
    output_formatted(az[6], NULL);
    @ <br>
  }
  @ </blockquote>
  @
  @ <form method="POST" action="attach_del">

  @ <input type="hidden" name="atn" value="%d(atn)">
  @ &nbsp;&nbsp;&nbsp;&nbsp;
  @ <input type="submit" name="ok" value="Yes, Delete">
  @ &nbsp;&nbsp;&nbsp;&nbsp;
  @ <input type="submit" name="can" value="No, Cancel">
  @ </form>
  common_footer();







>
>







 







>







 







>







 







>







125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
...
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
               (size+1023)/1024);
    }else{
      sqlite3 *pDb;
      sqlite3_stmt *pStmt;
      const char *zTail;
      int rc;

      login_check_csrf_token();

      for(z=zName; *z; z++){
        if( (*z=='/' || *z=='\\') && z[1]!=0 ){ zName = &z[1]; }
      }

      /*
      ** In order to insert a blob, we need to drop down to raw SQLite 3
      ** calls.
................................................................................
  @ Description:
  @ (<small>See <a href="#format_hints">formatting hints</a></small>)<br>
  cgi_wikitext("d",20,0);
  @ <br>
  @ <input type="submit" name="ok" value="Add Attachment">
  @ <input type="submit" name="can" value="Cancel">
  @ <input type="hidden" name="tn" value="%h(zPage)">
  login_output_csrf_token();
  @ </form>
  @ <hr>
  @ <a name="format_hints"></a>
  @ <h3>Formatting Hints:</h3>
  append_formatting_hints();
  common_footer();
}
................................................................................
    zDocView = mprintf("wiki?p=%t",az[0]);
  }
  if( P("can") ){
    cgi_redirect(zDocView);
    return;
  }
  if( P("ok") ){
    login_check_csrf_token();
    db_execute("DELETE FROM attachment WHERE atn=%d", atn);
    cgi_redirect(zDocView);
    return;
  }
  common_add_action_item(zDocView, "Cancel");
  common_add_help_item("CvstracAttachment");
  pTm = gmtime(&t);
................................................................................
    @ <br>
    output_formatted(az[6], NULL);
    @ <br>
  }
  @ </blockquote>
  @
  @ <form method="POST" action="attach_del">
  @ login_output_csrf_token();
  @ <input type="hidden" name="atn" value="%d(atn)">
  @ &nbsp;&nbsp;&nbsp;&nbsp;
  @ <input type="submit" name="ok" value="Yes, Delete">
  @ &nbsp;&nbsp;&nbsp;&nbsp;
  @ <input type="submit" name="can" value="No, Cancel">
  @ </form>
  common_footer();

Changes to login.c.

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
...
365
366
367
368
369
370
371














































































































# include <windows.h>
typedef BOOL (CALLBACK FNLOGONUSERA)(LPTSTR, LPTSTR, LPTSTR, DWORD, DWORD, PHANDLE);
#endif

/*
** Return the name of the login cookie
*/
static char *login_cookie_name(void){
  return mprintf("%s_login", g.zName);
}

/*
** WEBPAGE: /login
** WEBPAGE: /logout
**
................................................................................
** a redirect to the "login" page.
*/
void login_needed(void){
  const char *zUrl = getenv("REQUEST_URI");
  if( zUrl==0 ) zUrl = "index";
  cgi_redirect(mprintf("login?nxp=%T", zUrl));
}





















































































































|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
...
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# include <windows.h>
typedef BOOL (CALLBACK FNLOGONUSERA)(LPTSTR, LPTSTR, LPTSTR, DWORD, DWORD, PHANDLE);
#endif

/*
** Return the name of the login cookie
*/
static const char *login_cookie_name(void){
  return mprintf("%s_login", g.zName);
}

/*
** WEBPAGE: /login
** WEBPAGE: /logout
**
................................................................................
** a redirect to the "login" page.
*/
void login_needed(void){
  const char *zUrl = getenv("REQUEST_URI");
  if( zUrl==0 ) zUrl = "index";
  cgi_redirect(mprintf("login?nxp=%T", zUrl));
}

/*
** Figuring out the best cookie value to use for checking for CSRF's is
** actually rather involved. Logged in users are easy since they have
** login cookies. Anonymous users, however, are only sure to have cookies
** if captcha is enabled... and even then, _only_ if they tried to
** do something triggering a captcha. But the captcha cookie is basically
** just a timestamp, and _it changes_ everytime it gets checked. So
** we'd be a bit dumb to use it as a field name.
** Note that the resulting token _field name_ ends up being the value
** of the cookie, not the cookie name. For logged in users, this means
** we're effectively doing a "double submit" cookie test using the field
** name.
*/
static const char* login_csrf_token_name(void){
  const char *z;
  if( !g.isAnon ) return P(login_cookie_name());

  /*
  ** Well, that's about all we have for anon users. IP address is the
  ** only other usable option.
  */
  return encode64(((z = getenv("REMOTE_ADDR")) ? z : "0.0.0.0"),0);
}

static const char* login_csrf_secret(){
  /*
  ** A hashed password would be a perfect user-specific secret, quite
  ** frankly, if the attacker can figure out the password the user is
  ** pretty much hosed. But we can't assume that we know anything
  ** about the password (if we're using the Windows password stuff).
  ** As it turns out, about the _only_ bit an attacker might not
  ** be able to find at the browser level is the capabilities.
  ** Of course, CVSTrac capabilities are pretty standard. We really
  ** need to add some extra "salt" to the picture. "cvsroot"
  ** in theory is fairly private to the server. By mixing, I
  ** mean we MD5 hash them both together to get a nicely opaque token.
  ** This isn't a perfect "secret", but combined with the results of
  ** login_csrf_token_name() we should be raising the CSRF bar far higher
  ** than most things out there.
  */
  const char *z1 = db_short_query("SELECT capabilities FROM user WHERE id='%q'",
                        g.zUser);
  const char *z2 = db_config("cvsroot","/");
  MD5Context ctx;
  unsigned char digest[16];

  MD5Init(&ctx);
  MD5Update(&ctx,z1,strlen(z1));
  MD5Update(&ctx,z2,strlen(z2));
  MD5Final(digest,&ctx);
  return encode64(digest, sizeof(digest));
}

/*
** Output a hidden form element which can be checked to prevent Cross-Site
** Request Forgeries.
*/
void login_output_csrf_token(void){
  /* To build a CSRF token, there's a few approaches. The easiest is to
  ** just embed the cookie into the form as a value. Later we can check
  ** this value against the cookie and scream if it's not missing. Of
  ** course, this assumes that an attacker doesn't have access to the
  ** cookie itself to fake a CSRF token.
  ** A better approach somehow integrates some server state which an
  ** attacker won't easily know (i.e. by sniffing, or examining the local
  ** system outside of the form). But we want to have enough flexibility that,
  ** for example, the user could be doing things in multiple browser
  ** windows, in parallel, without hitting the CSRF code. So what shouldn't
  ** an attacker easily know that's relatively static?
  */
  const char *zName = login_csrf_token_name();
  const char *zValue = login_csrf_secret();

  @ <input type="hidden" name="%h(zName)" value="%h(zValue)" />
}

/*
** Check for a CSRF token. If it's missing or invalid, STOP THINGS HERE.
** Generally speaking, this test should only be needed for potentially
** destructive actions like deletions or various kinds of admin
** escalations, but it sure won't hurt to use it elsewhere.
*/
void login_check_csrf_token(void){
  const char *zName = login_csrf_token_name();
  if( zName && zName[0] ){
    const char *zToken = P(zName);
    if( zToken && zToken[0] ){
      const char *z = login_csrf_secret();
      if( !strcmp(zToken, z) ){
        /* We're good. The CRSF token exists under the right parameter (the
        ** "cookie" itself is the name), and the value matches the
        ** "secret" of the logged in user.
        */
        return;
      }
    }
  }

  /* Uh oh! Someone is playing games. */
  cgi_reset_content();
  common_standard_menu(0,0);
  common_header("Access denied");
  @ <p>This request appears to be a Cross-Site Request Forgery attempt.</p>
  common_footer();
  cgi_append_header("Pragma: no-cache\r\n");
  cgi_set_status(403,"Forbidden");
  cgi_reply();
  exit(0);
}

Changes to setup.c.

140
141
142
143
144
145
146

147
148
149
150
151
152
153
...
154
155
156
157
158
159
160

161
162
163
164
165
166
167
...
186
187
188
189
190
191
192

193
194
195
196
197
198
199
...
203
204
205
206
207
208
209

210
211
212
213
214
215
216
...
219
220
221
222
223
224
225

226
227
228
229
230
231
232
...
245
246
247
248
249
250
251

252
253
254
255
256
257
258
...
311
312
313
314
315
316
317

318
319
320
321
322
323
324
325
326
327
328
329
330

331
332
333
334
335
336
337
338

339
340
341
342
343
344
345
346
347
348
349

350
351
352
353
354
355
356
...
376
377
378
379
380
381
382

383
384
385
386
387
388
389
390
391
392
393
394

395
396
397
398
399
400
401
...
425
426
427
428
429
430
431

432
433
434
435
436
437
438
...
439
440
441
442
443
444
445

446
447
448
449
450
451
452
...
454
455
456
457
458
459
460

461
462
463
464
465
466
467
...
469
470
471
472
473
474
475

476
477
478
479
480
481
482
...
484
485
486
487
488
489
490

491
492
493
494
495
496
497
498
499
500
501
502

503
504
505
506
507
508
509
...
557
558
559
560
561
562
563


564
565
566
567
568
569
570
...
604
605
606
607
608
609
610

611
612
613
614
615
616
617
...
639
640
641
642
643
644
645

646
647
648
649
650
651
652
...
656
657
658
659
660
661
662

663
664
665
666
667
668
669
...
674
675
676
677
678
679
680

681
682
683
684
685
686
687
...
699
700
701
702
703
704
705

706
707
708
709
710
711
712
...
913
914
915
916
917
918
919

920
921
922
923
924
925
926
...
938
939
940
941
942
943
944

945
946
947
948
949
950
951
...
971
972
973
974
975
976
977



978
979
980
981
982
983
984
....
1056
1057
1058
1059
1060
1061
1062

1063
1064
1065
1066
1067
1068
1069
....
1092
1093
1094
1095
1096
1097
1098

1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110

1111
1112
1113
1114
1115
1116
1117
....
1195
1196
1197
1198
1199
1200
1201

1202
1203
1204
1205
1206
1207
1208
....
1216
1217
1218
1219
1220
1221
1222

1223
1224
1225
1226
1227
1228
1229
....
1236
1237
1238
1239
1240
1241
1242

1243
1244
1245
1246
1247
1248
1249
....
1252
1253
1254
1255
1256
1257
1258

1259
1260
1261
1262
1263
1264
1265
....
1317
1318
1319
1320
1321
1322
1323

1324
1325
1326
1327
1328
1329
1330
....
1364
1365
1366
1367
1368
1369
1370

1371
1372
1373
1374
1375
1376
1377
....
1421
1422
1423
1424
1425
1426
1427

1428
1429
1430
1431
1432
1433
1434
....
1471
1472
1473
1474
1475
1476
1477

1478
1479
1480
1481
1482
1483
1484
....
1549
1550
1551
1552
1553
1554
1555

1556
1557
1558
1559
1560
1561
1562
....
1563
1564
1565
1566
1567
1568
1569

1570
1571
1572
1573
1574
1575
1576
....
1594
1595
1596
1597
1598
1599
1600

1601
1602
1603
1604
1605
1606

1607
1608
1609
1610
1611
1612

1613
1614
1615
1616
1617
1618

1619
1620
1621
1622
1623
1624

1625
1626
1627
1628
1629
1630

1631
1632
1633
1634
1635
1636
1637
....
1640
1641
1642
1643
1644
1645
1646

1647
1648
1649
1650
1651
1652
1653
....
1690
1691
1692
1693
1694
1695
1696

1697
1698
1699
1700
1701
1702
1703
....
1710
1711
1712
1713
1714
1715
1716

1717
1718
1719
1720
1721
1722
1723
....
1726
1727
1728
1729
1730
1731
1732

1733
1734
1735
1736
1737
1738
1739
....
1741
1742
1743
1744
1745
1746
1747

1748
1749
1750
1751
1752
1753
1754
....
1793
1794
1795
1796
1797
1798
1799


1800
1801
1802
1803
1804
1805
1806
1807

1808
1809
1810
1811
1812
1813
1814
1815
1816
1817


1818
1819
1820
1821
1822
1823
1824
....
1837
1838
1839
1840
1841
1842
1843

1844
1845
1846
1847
1848
1849
1850
....
1988
1989
1990
1991
1992
1993
1994


1995
1996
1997
1998
1999
2000
2001
2002

2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015

2016
2017
2018
2019
2020
2021
2022
....
2032
2033
2034
2035
2036
2037
2038

2039
2040
2041
2042
2043
2044
2045
....
2210
2211
2212
2213
2214
2215
2216

2217
2218
2219
2220

2221
2222
2223
2224
2225
2226
2227
....
2240
2241
2242
2243
2244
2245
2246

2247
2248
2249
2250
2251
2252
2253
....
2264
2265
2266
2267
2268
2269
2270

2271
2272
2273
2274
2275
2276
2277
....
2286
2287
2288
2289
2290
2291
2292

2293
2294
2295
2296
2297
2298
2299
  /*
  ** The "r" query parameter is the name of the CVS repository root
  ** directory.  Change it if it has changed.
  */
  zOldRoot = db_config("cvsroot","");
  zRoot = P("r");
  if( zRoot && strcmp(zOldRoot,zRoot)!=0 ){

    db_execute("REPLACE INTO config(name,value) VALUES('cvsroot','%q');",
      zRoot);
    zOldRoot = zRoot;
    db_config(0,0);
  }

  /*
................................................................................
  ** The "m" query parameter is the name of the module within the
  ** CVS repository that this CVSTrac instance is suppose to track.
  ** Change it if it has changed.
  */
  zOldModule = db_config("module","");
  zModule = P("m");
  if( zModule && strcmp(zOldModule,zModule)!=0 ){

    db_execute("REPLACE INTO config(name,value) VALUES('module','%q');",
      zModule);
    zOldModule = zModule;
    db_config(0,0);
  }

  /*
................................................................................
    @ </p>
    @
    @ <p>In either case, you may want to make a <a href="setup_backup">
    @ backup copy</a> of the database so that you can recover if something
    @ goes wrong.</p>
    @
    @ <form action="%s(g.zPath)" method="POST">

    @ <p>
    @ <input type="submit" name="rrh2" value="Reconstruct">
    @ Reconstruct the check-in database from scratch.
    @ </p>
    @ <p>
    @ <input type="submit" name="rrh3" value="Rescan">
    @ Attempt to reuse existing check-in numbers.
................................................................................
    @ Do no do anything.
    @ </p>
    @ </form>
    common_footer();
    return;
  }
  if( P("rrh2") ){

    db_execute(
      "BEGIN;"
      "DELETE FROM chng WHERE not milestone;"
      "DELETE FROM filechng;"
      "DELETE FROM file;"
      "UPDATE config SET value=0 WHERE name='historysize';"
    );
................................................................................

    db_execute("COMMIT; VACUUM;");

    db_config(0,0);
    history_update(0);
  }
  if( P("rrh3") ){

    db_execute(
      "BEGIN;"
      "DELETE FROM filechng WHERE rowid NOT IN ("
         "SELECT min(rowid) FROM filechng "
         "GROUP BY filename, vers||'x'"
      ");"
      "DELETE FROM chng WHERE milestone=0 AND cn NOT IN ("
................................................................................
    history_update(1);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminRepository");
  common_header("Configure Repository");
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>Enter the full pathname of the root directory of the
  @ %s(g.scm.zName) repository in the space provided below.
  if( g.scm.canFilterModules ){
    @ If you want to restrict this 
    @ server to see only a subset of the files contained in the
    @ %s(g.scm.zName) repository
    @ (for example, if you want to see only one module in a 
................................................................................
  ** The "wpw" query parameter is "yes" if the CVSROOT/passwd file is
  ** writable and "no" if not.  
  ** Change it if it has changed.
  */
  zOldWPswd = db_config("write_cvs_passwd","yes");
  zWPswd = P("wpw");
  if( zWPswd && strcmp(zOldWPswd,zWPswd)!=0 ){

    db_execute(
      "REPLACE INTO config(name,value) VALUES('write_cvs_passwd','%q');",
      zWPswd
    );
    zOldWPswd = zWPswd;
    db_config(0,0);
  }

  /*
  ** Import users out of the CVSROOT/passwd file if the user pressed
  ** the Import Users button.  Only setup can do this.
  */
  if( P("import_users") && g.scm.pxUserRead ){

    g.scm.pxUserRead();
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminUserDatabase");
  common_header("Configure User Database Linkage");
  if( g.scm.pxUserWrite ){
    @ <form action="%s(g.zPath)" method="POST">

    @ <p>CVSTrac can update the CVSROOT/passwd file with the usernames and
    @ passwords of all CVSTrac users.  Enable or disable this feature
    @ below.</p>
    @ <p>Write User Changes to CVSROOT/passwd?
    cgi_optionmenu(0, "wpw", zOldWPswd, "Yes", "yes", "No", "no", NULL);
    @ <input type="submit" value="Submit">
    @ </p>
    @ </form>
  }
  if( g.scm.pxUserRead ){
    @ <form action="%s(g.zPath)" method="POST">

    @ <p>Use the following button to automatically create a CVSTrac user ID
    @ for every user currently named in CVSROOT/passwd.  The new users will
    @ be given the same access permissions as user "anonymous" plus check-out
    @ permission and check-in permission if CVS allows the user to write.</p>
    @ <p><input type="submit" name="import_users" value="Import CVS Users"></p>
    @ </form>
  }
................................................................................
  ** The "log" query parameter specifies a log file into which a record
  ** of all HTTP hits is written.  Write this value if this has changed.
  ** Only setup can make this change.
  */
  zOldLog = db_config("logfile","");
  zLog = P("log");
  if( zLog && strcmp(zOldLog,zLog)!=0 ){

    db_execute(
      "REPLACE INTO config(name,value) VALUES('logfile','%q');",
      zLog
    );
    zOldLog = zLog;
    db_config(0,0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminLog");
  common_header("Configure Log File");
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>Enter the name of file into which is written a log of all accesses
  @ to this server.  Leave the entry blank to disable logging:
  @ </p>
  @ <p>Log File: <input type="text" name="log" size="40" value="%h(zOldLog)">
  @ <input type="submit" value="Submit"></p>
  @ </form>
  common_footer();
................................................................................
  ** The "asgnto" query parameter specifies a userid who is assigned to
  ** all new tickets.  Record this value in the configuration table if
  ** it has changed.
  */
  zOldAsgnto = db_config("assignto","");
  zAsgnto = P("asgnto");
  if( zAsgnto && strcmp(zOldAsgnto,zAsgnto)!=0 ){

    db_execute(
      "REPLACE INTO config(name,value) VALUES('assignto','%q');", zAsgnto
    );
    zOldAsgnto = zAsgnto;
    db_config(0,0);
  }

................................................................................
  /*
  ** The "istate" query parameter specifies the initial state for new
  ** tickets.  Record any changes to this value.
  */
  zOldState = db_config("initial_state","");
  zState = P("istate");
  if( zState && strcmp(zOldState,zState)!=0 ){

    db_execute(
      "REPLACE INTO config(name,value) VALUES('initial_state','%q');",
      zState
    );
    zOldState = zState;
    db_config(0,0);
  }
................................................................................
  /*
  ** The "type" query parameter specifies the initial type for new
  ** tickets.  Record any changes to this value.
  */
  zOldType = db_config("dflt_tkt_type","code");
  zType = P("type");
  if( zType && strcmp(zOldType,zType)!=0 ){

    db_execute(
      "REPLACE INTO config(name,value) VALUES('dflt_tkt_type','%q');",
      zType
    );
    zOldType = zType;
    db_config(0,0);
  }
................................................................................
  /*
  ** The "pri" query parameter specifies the initial priority for new
  ** tickets.  Record any changes to this value.
  */
  zOldPri = db_config("dflt_priority","1");
  zPri = P("pri");
  if( zPri && strcmp(zOldPri,zPri)!=0 ){

    db_execute(
      "REPLACE INTO config(name,value) VALUES('dflt_priority','%q');",
      zPri
    );
    zOldPri = zPri;
    db_config(0,0);
  }
................................................................................
  /*
  ** The "sev" query parameter specifies the initial severity for new
  ** tickets.  Record any changes to this value.
  */
  zOldSev = db_config("dflt_severity","1");
  zSev = P("sev");
  if( zSev && strcmp(zOldSev,zSev)!=0 ){

    db_execute(
      "REPLACE INTO config(name,value) VALUES('dflt_severity','%q');",
      zSev
    );
    zOldSev = zSev;
    db_config(0,0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminNewTicket");
  common_header("Configure New Ticket Defaults");
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>
  @ Select a user to whom new tickets will be assigned by default:</p><p>
  @ Assigned To:
  azResult = db_query("SELECT id FROM user UNION SELECT '' ORDER BY id");
  cgi_v_optionmenu(0, "asgnto", zOldAsgnto, (const char**)azResult);
  @ </p>
  @
................................................................................
  login_check_credentials();
  if( !g.okAdmin ){
    login_needed();
    return;
  }

  if( P("update") ){


    cols = atoi(PD("cols",db_config("wiki_textarea_cols",WIKI_TEXTAREA_COLS)));
    if (cols < 20) cols = 20;

    rows = atoi(PD("rows",db_config("wiki_textarea_rows",WIKI_TEXTAREA_ROWS)));
    if (rows < 5) rows = 5;

    db_execute(
................................................................................
  nCookieLife = atoi(db_config("browse_url_cookie_life", "90"));

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminInterface");
  common_header("Configure User Interface");

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">

  @ <h1>Ticket and Check-in/Milestone Links</h1>
  @ <p>Ticket and check-in/milestone link information enables link tooltips
  @ in most browsers. For example,
  @ <a href="tktview?tn=1" title="First ticket">#1</a> and
  @ <a href="chngview?cn=1" title="Check-in [1]: First check-in
  @   (By anonymous)">[1]</a>. While this provides information to the
  @ user without having to follow a link, it is additional database
................................................................................
  @ </p>
  @ <p>
  cgi_submit("update", 0, 0, 0, 0, "Set");
  @ </p>
  @ </form>

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">

  @ <h1>Repository</h1>
  @ <p>When browsing the repository there are two ways to list files and
  @ directories. The <em>Short</em> view is a compact listing combining
  @ all files and directories into just four columns. The <em>Long</em> view
  @ shows the most recent repository information for each file.</p>
  @ <p><label for="bu0"><input type="radio" name="bu" id="bu0"
  @    %s(strcmp("dirview",zBrowseUrl)==0?" checked":"") value="dirview">
................................................................................
  @ Short</label>
  @ <p>
  cgi_submit("update", 0, 0, 0, 0, "Set");
  @ </p>
  @ </form>

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">

  @ <h1>Cookies</h1>
  @ <p>
  @ Enter number of days browse mode cookie should be kept by users browser.
  @ This cookie keeps track of user's preferred browse mode across user's
  @ multiple visits.<br>
  @ This applies to all users.<br>
  @ Set it to 0 to disable browse mode cookie.
................................................................................
  @ </p>
  @ <p>
  cgi_submit("update", 0, 0, 0, 0, "Set");
  @ </p>
  @ </form>

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">

  @ <h1>Wikitext Input</h1>
  @ <p>Configure the wikitext input/editing dialog by setting the parameters
  @ below. The input area wrap method configures the way input text will be
  @ submitted to the program.  If input wrapping is set to <em>physical</em>
  @ then hard returns will be inserted into the text wherever a line of
  @ text wraps in the box. <em>Virtual</em> wrapping does not insert hard
  @ returns. This option should be set to <em>virtual</em> when for
................................................................................
  @ </p>
  @ <p>
  cgi_submit("update", 0, 0, 0, 0, "Set");
  @ </p>
  @ </form>

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">

  @ <h1>Safe Ticket Editting</h1>
  @ <p>Enabling this option will cause CVSTrac to generate an error if
  @ a ticket change conflicts with another change which may have happened
  @ while an update was being prepared.
  @ </p>
  @ <p>
  @ <label for="st">
................................................................................
  /*
  ** The "s" query parameter is a long text string that specifies
  ** the names of all subsystems.  If any subsystem names have been
  ** added or removed, then make appropriate changes to the subsyst
  ** table in the database.
  */
  if( P("x") ){

    db_execute("BEGIN");
    string_to_enum(zEnum, P("x"));
    db_execute("COMMIT");
  }

  /* Genenerate the page.
  */
................................................................................
  @ The token on the left is the value as it is stored in the database.
  @ The text that follows is a human-readable description for the meaning
  @ of the token.  A color name for use in reports may optionally appear
  @ in parentheses after the description.
  @ </p>
  @
  @ <form action="%s(g.zPath)" method="POST">

  @ <p><input type="hidden" name="e" value="%s(zEnum)">
  @ <textarea cols=60 rows=%d(nRow) name="x">%h(zText)</textarea></p>
  @ <p><input type="submit" value="Submit"></p>
  @ </form>
  common_footer();
}

................................................................................
  idx = atoi(PD("idx","0"));
  zName = P("n");
  zText = P("x");
  if( idx>=1 && idx<=5 && zName && zText ){
    char zEnum[20];
    char *zName2 = trim_string(zName);
    char *zDesc2 = trim_string(PD("d",""));



    bprintf(zEnum,sizeof(zEnum),"extra%d", idx);
    db_execute("BEGIN");

    /* Always delete... A missing description is meaningful for /tktnew */
    db_execute("DELETE FROM config WHERE name='%s_desc'", zEnum);

    if( zName2[0] ){
................................................................................
    zEnumName[6] = 0;
    zAllowed = enum_to_string(zEnumName);
    bprintf(zEnumName,sizeof(zEnumName),"extra%d_desc",i+1);
    zDesc = db_config(zEnumName,"");
    @ <hr>
    @ <h3>Database column "extra%d(i+1)":</h3>
    @ <form action="%s(g.zPath)" method="POST">

    @ <input type="hidden" name="idx" value="%d(i+1)">
    @ Display Name:
    @ <input type="text" name="n" value="%h(zOld)"><br>
    @ Allowed Values: (<i>Name Desc Color</i> - omit for free text)<br>
    @ <textarea cols=60 rows=15 name="x">%h(zAllowed)</textarea><br>
    @ Description: (HTML - Leave blank to omit from new-ticket page)<br>
    @ <textarea cols=60 rows=5 name="d">%h(zDesc)</textarea><br>
................................................................................
  ** The "notify" query parameter is the name of a program or script that
  ** is run whenever a ticket is created or modified.  Modify the notify
  ** value if it has changed.  Only setup can do this.
  */
  zOldNotify = db_config("notify","");
  zNotify = P("notify");
  if( zNotify && strcmp(zOldNotify,zNotify)!=0 ){

    db_execute(
      "REPLACE INTO config(name,value) VALUES('notify','%q');",
      zNotify
    );
    zOldNotify = zNotify;
    db_config(0,0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminNotification");
  common_header("Configure Ticket Change Notification");
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>Enter a shell command to run whenever a ticket is
  @ created or modified.  The following substitutions are made
  @ on the string before it is passed to /bin/sh:</p>
  @
  @ <table border=1 cellspacing=0 cellpadding=5 align="right" width="45%%">
  @ <tr><td bgcolor="#e0c0c0">
  @ <big><b>Important Security Note</b></big>
................................................................................
  ** The "diff" query parameter is the name of a program or script that
  ** is run to show the diff between revisions.  Modify the filediff
  ** value if it has changed.  Only setup can do this.
  */
  zOldDiff = db_config("filediff","");
  zDiff = P("diff");
  if( zDiff && strcmp(zOldDiff,zDiff)!=0 ){

    if( zDiff[0] ){
      db_execute(
        "REPLACE INTO config(name,value) VALUES('filediff','%q');",
        zDiff
      );
    }else{
      db_execute("DELETE FROM config WHERE name='filediff'");
................................................................................
    ** The "list" query parameter is the name of a program or script that
    ** is run to dump the first version of a file. Modify the filelist
    ** value if it has changed.  Only setup can do this.
    */
    zOldList = db_config("filelist","");
    zList = P("list");
    if( zList && strcmp(zOldList,zList)!=0 ){

      if( zList[0] ){
        db_execute(
          "REPLACE INTO config(name,value) VALUES('filelist','%q');",
          zList
        );
      }else{
        db_execute("DELETE FROM config WHERE name='filelist'");
................................................................................
  /*
  ** The "filter" query parameter is the name of a program or script that any
  ** files get filtered through for HTML markup.
  */
  zOldFilter = db_config("filefilter","");
  zFilter = P("filter");
  if( zFilter && strcmp(zOldFilter,zFilter)!=0 ){

    if( zFilter[0] ){
      db_execute(
        "REPLACE INTO config(name,value) VALUES('filefilter','%q');",
        zFilter
      );
    }else{
      db_execute("DELETE FROM config WHERE name='filefilter'");
................................................................................
    db_config(0,0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminFilter");
  common_header("Configure Source Code Diff Program");
  @ <form action="%s(g.zPath)" method="POST">

  @ <h2>File Diff</h2>
  @ <p>Enter a shell command to run in order to compute the difference between
  @ two versions of the same file.  The output can be either plain text
  @ or HTML.  If HTML, then the first non-whitespace character of output
  @ should be a "<".  Otherwise the output will be assumed to be plain text.</p>
  @
  @ <table border=1 cellspacing=0 cellpadding=5 align="right" width="33%%">
................................................................................
  /*
  ** File List is only applicable to SCM's which have a concept of a version
  ** of a file. SCM's like GIT where the _commits_ themselves are the version
  ** objects don't need this.
  */
  if( strcmp(g.scm.zSCM,"git") ){
    @ <form action="%s(g.zPath)" method="POST">

    @ <h2>File List</h2>
    @ <p>Enter below a shell command to run in order to list the content
    @ of a single version of a file <i>as a diff</i> (i.e. for the first
    @ revision of a file).  The output can be either plain text
    @ or HTML.  If HTML, then the first non-whitespace character of output
    @ should be a "<".  Otherwise the output will be assumed to be plain
    @ text.</p>
................................................................................
    }
    @ </pre></blockquote>
    @ </form>
    @ <hr>
  }

  @ <form action="%s(g.zPath)" method="POST">

  @ <h2>File Filter</h2>
  @ <p>Enter below a shell command to run in order to filter the contents
  @ of a single version of a file.  The filter should expect the file contents
  @ on standard input. The output can be either plain text
  @ or HTML.  If HTML, then the first non-whitespace character of output
  @ should be a "<".  Otherwise the output will be assumed to be plain text.</p>
  @
................................................................................

  /*
  ** If both "header" and "footer" query parameters are present, then
  ** change the header and footer to the values of those parameters.
  ** Only the setup user can do this.
  */
  if( P("ok") && P("header") && P("footer") ){

    db_execute(
      "REPLACE INTO config VALUES('header','%q');"
      "REPLACE INTO config VALUES('footer','%q');",
       trim_string(P("header")),
       trim_string(P("footer"))
    );
    db_config(0,0);
................................................................................
  @ <tr><td><b>%%B</b></td><td>CVSTrac base URL</td></tr>
  @ <tr><td><b>%%D</b></td><td>Current document URL (without base URL)</td></tr>
  @ <tr><td><b>%%%%</b></td><td>The literal character "<b>%%</b>"</td></tr>
  @ </table>
  @ </blockquote>
  @
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>
  zHeader = db_config("header","");
  zFooter = db_config("footer","");

  /* user wants to restore the defaults */
  if( P("def") ){
    zHeader = HEADER;
................................................................................
  if( !g.okSetup ){
    cgi_redirect("setup");
    return;
  }

  if( P("sz") ){
    int sz = atoi(P("sz"))*1024;

    db_execute("REPLACE INTO config VALUES('max_attach_size',%d)", sz);
    db_config(0, 0);
    cgi_redirect("setup");
  }
 
  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminAttachment");
................................................................................
  common_header("Set Maximum Attachment Size");
  @ <p>
  @ Enter the maximum attachment size below.  If you enter a size of
  @ zero, attachments are disallowed.
  @ </p>
  @
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>
  @ Maximum attachment size in kilobytes: 
  @ <input type="text" name="sz" value="%d(attachment_max()/1024)" size=5>
  @ <input type="submit" value="Set">
  @ </p>
  @ </form>
  common_footer();
................................................................................
  if( !g.okSetup ){
    cgi_redirect("setup");
    return;
  }
  

  if( P("sz") && atoi(P("sz"))!=mxHit ){

    mxHit = atoi(P("sz"));
    db_execute("REPLACE INTO config VALUES('throttle',%d)", mxHit);
    db_config(0, 0);
  }

  if( P("nf") && atoi(P("nf"))!=nf ){

    nf = atoi(P("nf"));
    db_execute("REPLACE INTO config VALUES('nofollow_link',%d)", nf);
    db_config(0, 0);
  }
 
  if( P("cp") && atoi(P("cp"))!=cp ){

    cp = atoi(P("cp"));
    db_execute("REPLACE INTO config VALUES('enable_captcha',%d)", cp);
    db_config(0, 0);
  }
 
  if( P("lnk") && atoi(P("lnk"))!=lnk ){

    lnk = atoi(P("lnk"));
    db_execute("REPLACE INTO config VALUES('max_links_per_edit',%d)", lnk);
    db_config(0, 0);
  }

  if( P("mscore") && atoi(P("mscore"))!=mscore ){

    mscore = atoi(P("mscore"));
    db_execute("REPLACE INTO config VALUES('keywords_max_score',%d)", mscore);
    db_config(0, 0);
  }

  if( P("keys") && strcmp(zKeys,PD("keys","")) ){

    zKeys = P("keys");
    db_execute("REPLACE INTO config VALUES('keywords','%q')", zKeys);
    db_config(0, 0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminAbuse");
................................................................................
  @ <p>
  @ Enter the limit on the number of anonymous accesses from the same
  @ IP address that can occur within one hour.  Enter zero to disable
  @ the limiter.
  @ </p>
  @
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>
  @ Maximum hits per hour: 
  @ <input type="text" name="sz" value="%d(mxHit)" size=5>
  @ <input type="submit" value="Set">
  @ </p>
  @ </form>
  @
................................................................................
  @
  @ <p>A summary of the <a href="info_throttle">Access Log</a> is available
  @ separately.</p>

  @ <hr>
  @ <h2>Captcha</h2>
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>
  @ By turning on this option, anonymous users will be required to pass a
  @ simple <a href="http://en.wikipedia.org/wiki/Captcha">captcha</a>
  @ test before being allowed to change content (tickets, wiki, etc). Passing
  @ the test will set a cookie on the browser. Too many failures to pass
  @ the test will trigger the throttler and lock the users IP address out.
  @ Note that the rate limiter has to be enabled (non-zero) for this option
................................................................................
  @ </p>
  @ <input type="submit" value="Set">
  @ </form>
  @ <hr>

  @ <h2>External Links</h2>
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>
  @ By turning on this option, all links to external sites are tagged as
  @ "nofollow". This provides a hint to search engines to ignore such links
  @ and reduces the value of wiki spam. However, this may be of limited use
  @ since wiki spammers aren't always smart enough to notice that they're
  @ wasting their time.
  @ </p>
................................................................................
  @    %s(nf?" checked":"") value="1">
  @ Don't allow search engines to follow external links.</label>
  @ </p>
  @ <input type="submit" value="Set">
  @ </form>
  @ 
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>
  @ Wiki spam generally works by inserting large numbers of links in a
  @ single page edit. A simple way to prevent this is to simply impose a
  @ maximum number of new external links in a single wiki edit.
  @ A value of zero will disable this option.
  @ </p>
  @ <p>
................................................................................
  @ <input type="text" name="lnk" value="%d(lnk)" size=5>
  @ </p>
  @ <input type="submit" value="Set">
  @ </form>
  @ <hr>
  @ <h2>Keyword Filtering</h2>
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>
  @ Enter a space-separated list of keywords. All wiki edits will be
  @ checked against this list and, if the maximum score is exceeded, 
  @ the change will be denied. The scoring algorithm uses the standard
  @ CVSTrac text <strong>search()</strong> function (where each matched
  @ keyword scores from 6 to 10 points). Repeating a keyword in the
  @ list will cause it to score higher.
................................................................................
  common_add_help_item("CvstracAdminMarkup");
  common_header("Custom Wiki Markup");

  if( P("can") ){
    cgi_redirect("setup_markup");
    return;
  }else if( P("ok") ){


    /* delete it */
    db_execute("DELETE FROM markup WHERE markup='%q';", zMarkup);
    cgi_redirect("setup_markup");
    return;
  }else if( delete && zMarkup[0] ){
    @ <p>Are you sure you want to delete markup <b>%h(zMarkup)</b>?</p>
    @
    @ <form method="POST" action="setup_markupedit">

    @ <input type="hidden" name="m" value="%h(zMarkup)">
    @ <input type="submit" name="ok" value="Yes, Delete">
    @ <input type="submit" name="can" value="No, Cancel">
    @ </form>
    common_footer();
    return;
  }

  if( P("u") ){
    if( zMarkup[0] && zType[0] && zFormat[0] ) {


      /* update database and bounce back to listing page. If the
      ** description is empty, we'll survive (and wing it).
      */
      db_execute("REPLACE INTO markup(markup,type,formatter,description) "
                 "VALUES('%q',%d,'%q','%q');",
                 zMarkup, atoi(zType), zFormat, zDescription);
    }
................................................................................
      zType = az[0];
      zFormat = az[1];
      zDescription = az[2];
    }
  }

  @ <form action="%s(g.zPath)" method="POST">

  @ Markup Name: <input type="text" name="m" value="%h(zMarkup)" size=12>
  cgi_optionmenu(0,"t",zType, "Markup","0", "Block","2",
    "Program Markup","1", "Program Block","3",
    "Trusted Program Markup","4", "Trusted Program Block","5",
    NULL);
  @ <br>Formatter:<br>
  @ <textarea name="f" rows="4" cols="60">%h(zFormat)</textarea><br>
................................................................................
                         "Delete");
  common_header("External Tools");

  if( P("can") ){
    cgi_redirect("setup_tools");
    return;
  }else if( P("ok") ){


    /* delete it */
    db_execute("DELETE FROM tool WHERE name='%q';", zTool);
    cgi_redirect("setup_tools");
    return;
  }else if( delete && zTool[0] ){
    @ <p>Are you sure you want to delete tool <b>%h(zTool)</b>?</p>
    @
    @ <form method="POST" action="setup_toolsedit">

    @ <input type="hidden" name="t" value="%h(zTool)">
    @ <input type="submit" name="ok" value="Yes, Delete">
    @ <input type="submit" name="can" value="No, Cancel">
    @ </form>
    common_footer();
    return;
  }

  if( P("u") ){
    if( zTool[0] && zPerms[0] && zObject[0] && zCommand[0] ) {
      /* update database and bounce back to listing page. If the
      ** description is empty, we'll survive (and wing it).
      */

      db_execute("REPLACE INTO tool(name,perms,object,command,description) "
                 "VALUES('%q','%q','%q','%q','%q');",
                 zTool, zPerms, zObject, zCommand, zDescription);
    }

    cgi_redirect("setup_tools");
  }
................................................................................
      zObject = az[1];
      zCommand = az[2];
      zDescription = az[3];
    }
  }

  @ <form action="%s(g.zPath)" method="POST">

  @ Tool Name: <input type="text" name="t" value="%h(zTool)" size=12>
  cgi_optionmenu(0,"o",zObject,
                 "File","file",
                 "Wiki","wiki",
                 "Ticket","tkt",
                 "Check-in","chng",
                 "Milestone","ms",
................................................................................
  login_check_credentials();
  if( !g.okSetup ){
    cgi_redirect("setup");
    return;
  }

  if( P("bkup") ){

    db_execute("BEGIN");
    zMsg = file_copy(zDbName, zBuName);
    db_execute("COMMIT");
  }else if( P("rstr") ){

    db_execute("BEGIN");
    zMsg = file_copy(zBuName, zDbName);
    db_execute("COMMIT");
  }
 
  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminBackup");
................................................................................
  @ It is always safe to do a backup.  The worst that can happen is that
  @ you can overwrite a prior backup.  But a restore can destroy your
  @ database if the backup copy you are restoring from is incorrect.
  @ Use caution when doing a restore.
  @ </p>
  @
  @ <form action="%s(g.zPath)" method="POST">

  @ <p><input type="submit" name="bkup" value="Backup"></p>
  @ <p><input type="submit" name="rstr" value="Restore"></p>
  @ </form>
  common_footer();
}

/*
................................................................................
  login_check_credentials();
  if( !g.okSetup ){
    cgi_redirect("setup");
    return;
  }
  
  if( P("cl") || P("ttl") || P("rdl") ){

    if( P("cl") ){
      int nCookieLife = atoi(P("cl"));
      db_execute("REPLACE INTO config VALUES('timeline_cookie_life',%d)", nCookieLife);
    }
    if( P("ttl") ){
      int nTTL = atoi(P("ttl"));
      db_execute("REPLACE INTO config VALUES('rss_ttl',%d)", nTTL);
................................................................................
  nCookieLife = atoi(db_config("timeline_cookie_life", "90"));
  nTTL = atoi(db_config("rss_ttl", "60"));
  nRDL = atoi(db_config("rss_detail_level", "5"));
  
  common_add_nav_item("setup", "Main Setup Menu");
  common_header("Timeline & RSS Setup");
  @ <form action="%s(g.zPath)" method="POST">

  @ <p>
  @ Enter number of days timeline cookie should be kept by users browser.
  @ This cookie keeps timeline settings persistent across users multiple visits.<br>
  @ This applies to all users.<br>
  @ Set it to 0 to disable timeline cookie.
  @ </p>
  @ <p>







>







 







>







 







>







 







>







 







>







 







>







 







>













>








>











>







 







>












>







 







>







 







>







 







>







 







>







 







>












>







 







>
>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>
>
>







 







>







 







>












>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>







 







>






>






>






>






>






>







 







>







 







>







 







>







 







>







 







>







 







>
>








>










>
>







 







>







 







>
>








>













>







 







>







 







>




>







 







>







 







>







 







>







140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
...
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
...
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
...
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
...
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
...
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
...
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
...
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
...
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
...
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
...
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
...
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
...
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
...
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
...
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
...
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
...
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
...
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
...
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
....
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
....
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
....
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
....
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
....
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
....
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
....
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
....
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
....
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
....
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
....
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
....
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
....
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
....
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
....
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
....
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
....
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
....
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
....
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
....
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
....
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
....
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
....
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
....
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
....
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
....
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
  /*
  ** The "r" query parameter is the name of the CVS repository root
  ** directory.  Change it if it has changed.
  */
  zOldRoot = db_config("cvsroot","");
  zRoot = P("r");
  if( zRoot && strcmp(zOldRoot,zRoot)!=0 ){
    login_check_csrf_token();
    db_execute("REPLACE INTO config(name,value) VALUES('cvsroot','%q');",
      zRoot);
    zOldRoot = zRoot;
    db_config(0,0);
  }

  /*
................................................................................
  ** The "m" query parameter is the name of the module within the
  ** CVS repository that this CVSTrac instance is suppose to track.
  ** Change it if it has changed.
  */
  zOldModule = db_config("module","");
  zModule = P("m");
  if( zModule && strcmp(zOldModule,zModule)!=0 ){
    login_check_csrf_token();
    db_execute("REPLACE INTO config(name,value) VALUES('module','%q');",
      zModule);
    zOldModule = zModule;
    db_config(0,0);
  }

  /*
................................................................................
    @ </p>
    @
    @ <p>In either case, you may want to make a <a href="setup_backup">
    @ backup copy</a> of the database so that you can recover if something
    @ goes wrong.</p>
    @
    @ <form action="%s(g.zPath)" method="POST">
    login_output_csrf_token();
    @ <p>
    @ <input type="submit" name="rrh2" value="Reconstruct">
    @ Reconstruct the check-in database from scratch.
    @ </p>
    @ <p>
    @ <input type="submit" name="rrh3" value="Rescan">
    @ Attempt to reuse existing check-in numbers.
................................................................................
    @ Do no do anything.
    @ </p>
    @ </form>
    common_footer();
    return;
  }
  if( P("rrh2") ){
    login_check_csrf_token();
    db_execute(
      "BEGIN;"
      "DELETE FROM chng WHERE not milestone;"
      "DELETE FROM filechng;"
      "DELETE FROM file;"
      "UPDATE config SET value=0 WHERE name='historysize';"
    );
................................................................................

    db_execute("COMMIT; VACUUM;");

    db_config(0,0);
    history_update(0);
  }
  if( P("rrh3") ){
    login_check_csrf_token();
    db_execute(
      "BEGIN;"
      "DELETE FROM filechng WHERE rowid NOT IN ("
         "SELECT min(rowid) FROM filechng "
         "GROUP BY filename, vers||'x'"
      ");"
      "DELETE FROM chng WHERE milestone=0 AND cn NOT IN ("
................................................................................
    history_update(1);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminRepository");
  common_header("Configure Repository");
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>Enter the full pathname of the root directory of the
  @ %s(g.scm.zName) repository in the space provided below.
  if( g.scm.canFilterModules ){
    @ If you want to restrict this 
    @ server to see only a subset of the files contained in the
    @ %s(g.scm.zName) repository
    @ (for example, if you want to see only one module in a 
................................................................................
  ** The "wpw" query parameter is "yes" if the CVSROOT/passwd file is
  ** writable and "no" if not.  
  ** Change it if it has changed.
  */
  zOldWPswd = db_config("write_cvs_passwd","yes");
  zWPswd = P("wpw");
  if( zWPswd && strcmp(zOldWPswd,zWPswd)!=0 ){
    login_check_csrf_token();
    db_execute(
      "REPLACE INTO config(name,value) VALUES('write_cvs_passwd','%q');",
      zWPswd
    );
    zOldWPswd = zWPswd;
    db_config(0,0);
  }

  /*
  ** Import users out of the CVSROOT/passwd file if the user pressed
  ** the Import Users button.  Only setup can do this.
  */
  if( P("import_users") && g.scm.pxUserRead ){
    login_check_csrf_token();
    g.scm.pxUserRead();
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminUserDatabase");
  common_header("Configure User Database Linkage");
  if( g.scm.pxUserWrite ){
    @ <form action="%s(g.zPath)" method="POST">
    login_output_csrf_token();
    @ <p>CVSTrac can update the CVSROOT/passwd file with the usernames and
    @ passwords of all CVSTrac users.  Enable or disable this feature
    @ below.</p>
    @ <p>Write User Changes to CVSROOT/passwd?
    cgi_optionmenu(0, "wpw", zOldWPswd, "Yes", "yes", "No", "no", NULL);
    @ <input type="submit" value="Submit">
    @ </p>
    @ </form>
  }
  if( g.scm.pxUserRead ){
    @ <form action="%s(g.zPath)" method="POST">
    login_output_csrf_token();
    @ <p>Use the following button to automatically create a CVSTrac user ID
    @ for every user currently named in CVSROOT/passwd.  The new users will
    @ be given the same access permissions as user "anonymous" plus check-out
    @ permission and check-in permission if CVS allows the user to write.</p>
    @ <p><input type="submit" name="import_users" value="Import CVS Users"></p>
    @ </form>
  }
................................................................................
  ** The "log" query parameter specifies a log file into which a record
  ** of all HTTP hits is written.  Write this value if this has changed.
  ** Only setup can make this change.
  */
  zOldLog = db_config("logfile","");
  zLog = P("log");
  if( zLog && strcmp(zOldLog,zLog)!=0 ){
    login_check_csrf_token();
    db_execute(
      "REPLACE INTO config(name,value) VALUES('logfile','%q');",
      zLog
    );
    zOldLog = zLog;
    db_config(0,0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminLog");
  common_header("Configure Log File");
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>Enter the name of file into which is written a log of all accesses
  @ to this server.  Leave the entry blank to disable logging:
  @ </p>
  @ <p>Log File: <input type="text" name="log" size="40" value="%h(zOldLog)">
  @ <input type="submit" value="Submit"></p>
  @ </form>
  common_footer();
................................................................................
  ** The "asgnto" query parameter specifies a userid who is assigned to
  ** all new tickets.  Record this value in the configuration table if
  ** it has changed.
  */
  zOldAsgnto = db_config("assignto","");
  zAsgnto = P("asgnto");
  if( zAsgnto && strcmp(zOldAsgnto,zAsgnto)!=0 ){
    login_check_csrf_token();
    db_execute(
      "REPLACE INTO config(name,value) VALUES('assignto','%q');", zAsgnto
    );
    zOldAsgnto = zAsgnto;
    db_config(0,0);
  }

................................................................................
  /*
  ** The "istate" query parameter specifies the initial state for new
  ** tickets.  Record any changes to this value.
  */
  zOldState = db_config("initial_state","");
  zState = P("istate");
  if( zState && strcmp(zOldState,zState)!=0 ){
    login_check_csrf_token();
    db_execute(
      "REPLACE INTO config(name,value) VALUES('initial_state','%q');",
      zState
    );
    zOldState = zState;
    db_config(0,0);
  }
................................................................................
  /*
  ** The "type" query parameter specifies the initial type for new
  ** tickets.  Record any changes to this value.
  */
  zOldType = db_config("dflt_tkt_type","code");
  zType = P("type");
  if( zType && strcmp(zOldType,zType)!=0 ){
    login_check_csrf_token();
    db_execute(
      "REPLACE INTO config(name,value) VALUES('dflt_tkt_type','%q');",
      zType
    );
    zOldType = zType;
    db_config(0,0);
  }
................................................................................
  /*
  ** The "pri" query parameter specifies the initial priority for new
  ** tickets.  Record any changes to this value.
  */
  zOldPri = db_config("dflt_priority","1");
  zPri = P("pri");
  if( zPri && strcmp(zOldPri,zPri)!=0 ){
    login_check_csrf_token();
    db_execute(
      "REPLACE INTO config(name,value) VALUES('dflt_priority','%q');",
      zPri
    );
    zOldPri = zPri;
    db_config(0,0);
  }
................................................................................
  /*
  ** The "sev" query parameter specifies the initial severity for new
  ** tickets.  Record any changes to this value.
  */
  zOldSev = db_config("dflt_severity","1");
  zSev = P("sev");
  if( zSev && strcmp(zOldSev,zSev)!=0 ){
    login_check_csrf_token();
    db_execute(
      "REPLACE INTO config(name,value) VALUES('dflt_severity','%q');",
      zSev
    );
    zOldSev = zSev;
    db_config(0,0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminNewTicket");
  common_header("Configure New Ticket Defaults");
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>
  @ Select a user to whom new tickets will be assigned by default:</p><p>
  @ Assigned To:
  azResult = db_query("SELECT id FROM user UNION SELECT '' ORDER BY id");
  cgi_v_optionmenu(0, "asgnto", zOldAsgnto, (const char**)azResult);
  @ </p>
  @
................................................................................
  login_check_credentials();
  if( !g.okAdmin ){
    login_needed();
    return;
  }

  if( P("update") ){
    login_check_csrf_token();

    cols = atoi(PD("cols",db_config("wiki_textarea_cols",WIKI_TEXTAREA_COLS)));
    if (cols < 20) cols = 20;

    rows = atoi(PD("rows",db_config("wiki_textarea_rows",WIKI_TEXTAREA_ROWS)));
    if (rows < 5) rows = 5;

    db_execute(
................................................................................
  nCookieLife = atoi(db_config("browse_url_cookie_life", "90"));

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminInterface");
  common_header("Configure User Interface");

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <h1>Ticket and Check-in/Milestone Links</h1>
  @ <p>Ticket and check-in/milestone link information enables link tooltips
  @ in most browsers. For example,
  @ <a href="tktview?tn=1" title="First ticket">#1</a> and
  @ <a href="chngview?cn=1" title="Check-in [1]: First check-in
  @   (By anonymous)">[1]</a>. While this provides information to the
  @ user without having to follow a link, it is additional database
................................................................................
  @ </p>
  @ <p>
  cgi_submit("update", 0, 0, 0, 0, "Set");
  @ </p>
  @ </form>

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <h1>Repository</h1>
  @ <p>When browsing the repository there are two ways to list files and
  @ directories. The <em>Short</em> view is a compact listing combining
  @ all files and directories into just four columns. The <em>Long</em> view
  @ shows the most recent repository information for each file.</p>
  @ <p><label for="bu0"><input type="radio" name="bu" id="bu0"
  @    %s(strcmp("dirview",zBrowseUrl)==0?" checked":"") value="dirview">
................................................................................
  @ Short</label>
  @ <p>
  cgi_submit("update", 0, 0, 0, 0, "Set");
  @ </p>
  @ </form>

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <h1>Cookies</h1>
  @ <p>
  @ Enter number of days browse mode cookie should be kept by users browser.
  @ This cookie keeps track of user's preferred browse mode across user's
  @ multiple visits.<br>
  @ This applies to all users.<br>
  @ Set it to 0 to disable browse mode cookie.
................................................................................
  @ </p>
  @ <p>
  cgi_submit("update", 0, 0, 0, 0, "Set");
  @ </p>
  @ </form>

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <h1>Wikitext Input</h1>
  @ <p>Configure the wikitext input/editing dialog by setting the parameters
  @ below. The input area wrap method configures the way input text will be
  @ submitted to the program.  If input wrapping is set to <em>physical</em>
  @ then hard returns will be inserted into the text wherever a line of
  @ text wraps in the box. <em>Virtual</em> wrapping does not insert hard
  @ returns. This option should be set to <em>virtual</em> when for
................................................................................
  @ </p>
  @ <p>
  cgi_submit("update", 0, 0, 0, 0, "Set");
  @ </p>
  @ </form>

  @ <form class="setup-section" action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <h1>Safe Ticket Editting</h1>
  @ <p>Enabling this option will cause CVSTrac to generate an error if
  @ a ticket change conflicts with another change which may have happened
  @ while an update was being prepared.
  @ </p>
  @ <p>
  @ <label for="st">
................................................................................
  /*
  ** The "s" query parameter is a long text string that specifies
  ** the names of all subsystems.  If any subsystem names have been
  ** added or removed, then make appropriate changes to the subsyst
  ** table in the database.
  */
  if( P("x") ){
    login_check_csrf_token();
    db_execute("BEGIN");
    string_to_enum(zEnum, P("x"));
    db_execute("COMMIT");
  }

  /* Genenerate the page.
  */
................................................................................
  @ The token on the left is the value as it is stored in the database.
  @ The text that follows is a human-readable description for the meaning
  @ of the token.  A color name for use in reports may optionally appear
  @ in parentheses after the description.
  @ </p>
  @
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p><input type="hidden" name="e" value="%s(zEnum)">
  @ <textarea cols=60 rows=%d(nRow) name="x">%h(zText)</textarea></p>
  @ <p><input type="submit" value="Submit"></p>
  @ </form>
  common_footer();
}

................................................................................
  idx = atoi(PD("idx","0"));
  zName = P("n");
  zText = P("x");
  if( idx>=1 && idx<=5 && zName && zText ){
    char zEnum[20];
    char *zName2 = trim_string(zName);
    char *zDesc2 = trim_string(PD("d",""));

    login_check_csrf_token();

    bprintf(zEnum,sizeof(zEnum),"extra%d", idx);
    db_execute("BEGIN");

    /* Always delete... A missing description is meaningful for /tktnew */
    db_execute("DELETE FROM config WHERE name='%s_desc'", zEnum);

    if( zName2[0] ){
................................................................................
    zEnumName[6] = 0;
    zAllowed = enum_to_string(zEnumName);
    bprintf(zEnumName,sizeof(zEnumName),"extra%d_desc",i+1);
    zDesc = db_config(zEnumName,"");
    @ <hr>
    @ <h3>Database column "extra%d(i+1)":</h3>
    @ <form action="%s(g.zPath)" method="POST">
    login_output_csrf_token();
    @ <input type="hidden" name="idx" value="%d(i+1)">
    @ Display Name:
    @ <input type="text" name="n" value="%h(zOld)"><br>
    @ Allowed Values: (<i>Name Desc Color</i> - omit for free text)<br>
    @ <textarea cols=60 rows=15 name="x">%h(zAllowed)</textarea><br>
    @ Description: (HTML - Leave blank to omit from new-ticket page)<br>
    @ <textarea cols=60 rows=5 name="d">%h(zDesc)</textarea><br>
................................................................................
  ** The "notify" query parameter is the name of a program or script that
  ** is run whenever a ticket is created or modified.  Modify the notify
  ** value if it has changed.  Only setup can do this.
  */
  zOldNotify = db_config("notify","");
  zNotify = P("notify");
  if( zNotify && strcmp(zOldNotify,zNotify)!=0 ){
    login_check_csrf_token();
    db_execute(
      "REPLACE INTO config(name,value) VALUES('notify','%q');",
      zNotify
    );
    zOldNotify = zNotify;
    db_config(0,0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminNotification");
  common_header("Configure Ticket Change Notification");
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>Enter a shell command to run whenever a ticket is
  @ created or modified.  The following substitutions are made
  @ on the string before it is passed to /bin/sh:</p>
  @
  @ <table border=1 cellspacing=0 cellpadding=5 align="right" width="45%%">
  @ <tr><td bgcolor="#e0c0c0">
  @ <big><b>Important Security Note</b></big>
................................................................................
  ** The "diff" query parameter is the name of a program or script that
  ** is run to show the diff between revisions.  Modify the filediff
  ** value if it has changed.  Only setup can do this.
  */
  zOldDiff = db_config("filediff","");
  zDiff = P("diff");
  if( zDiff && strcmp(zOldDiff,zDiff)!=0 ){
    login_check_csrf_token();
    if( zDiff[0] ){
      db_execute(
        "REPLACE INTO config(name,value) VALUES('filediff','%q');",
        zDiff
      );
    }else{
      db_execute("DELETE FROM config WHERE name='filediff'");
................................................................................
    ** The "list" query parameter is the name of a program or script that
    ** is run to dump the first version of a file. Modify the filelist
    ** value if it has changed.  Only setup can do this.
    */
    zOldList = db_config("filelist","");
    zList = P("list");
    if( zList && strcmp(zOldList,zList)!=0 ){
      login_check_csrf_token();
      if( zList[0] ){
        db_execute(
          "REPLACE INTO config(name,value) VALUES('filelist','%q');",
          zList
        );
      }else{
        db_execute("DELETE FROM config WHERE name='filelist'");
................................................................................
  /*
  ** The "filter" query parameter is the name of a program or script that any
  ** files get filtered through for HTML markup.
  */
  zOldFilter = db_config("filefilter","");
  zFilter = P("filter");
  if( zFilter && strcmp(zOldFilter,zFilter)!=0 ){
    login_check_csrf_token();
    if( zFilter[0] ){
      db_execute(
        "REPLACE INTO config(name,value) VALUES('filefilter','%q');",
        zFilter
      );
    }else{
      db_execute("DELETE FROM config WHERE name='filefilter'");
................................................................................
    db_config(0,0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminFilter");
  common_header("Configure Source Code Diff Program");
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <h2>File Diff</h2>
  @ <p>Enter a shell command to run in order to compute the difference between
  @ two versions of the same file.  The output can be either plain text
  @ or HTML.  If HTML, then the first non-whitespace character of output
  @ should be a "<".  Otherwise the output will be assumed to be plain text.</p>
  @
  @ <table border=1 cellspacing=0 cellpadding=5 align="right" width="33%%">
................................................................................
  /*
  ** File List is only applicable to SCM's which have a concept of a version
  ** of a file. SCM's like GIT where the _commits_ themselves are the version
  ** objects don't need this.
  */
  if( strcmp(g.scm.zSCM,"git") ){
    @ <form action="%s(g.zPath)" method="POST">
    login_output_csrf_token();
    @ <h2>File List</h2>
    @ <p>Enter below a shell command to run in order to list the content
    @ of a single version of a file <i>as a diff</i> (i.e. for the first
    @ revision of a file).  The output can be either plain text
    @ or HTML.  If HTML, then the first non-whitespace character of output
    @ should be a "<".  Otherwise the output will be assumed to be plain
    @ text.</p>
................................................................................
    }
    @ </pre></blockquote>
    @ </form>
    @ <hr>
  }

  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <h2>File Filter</h2>
  @ <p>Enter below a shell command to run in order to filter the contents
  @ of a single version of a file.  The filter should expect the file contents
  @ on standard input. The output can be either plain text
  @ or HTML.  If HTML, then the first non-whitespace character of output
  @ should be a "<".  Otherwise the output will be assumed to be plain text.</p>
  @
................................................................................

  /*
  ** If both "header" and "footer" query parameters are present, then
  ** change the header and footer to the values of those parameters.
  ** Only the setup user can do this.
  */
  if( P("ok") && P("header") && P("footer") ){
    login_check_csrf_token();
    db_execute(
      "REPLACE INTO config VALUES('header','%q');"
      "REPLACE INTO config VALUES('footer','%q');",
       trim_string(P("header")),
       trim_string(P("footer"))
    );
    db_config(0,0);
................................................................................
  @ <tr><td><b>%%B</b></td><td>CVSTrac base URL</td></tr>
  @ <tr><td><b>%%D</b></td><td>Current document URL (without base URL)</td></tr>
  @ <tr><td><b>%%%%</b></td><td>The literal character "<b>%%</b>"</td></tr>
  @ </table>
  @ </blockquote>
  @
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>
  zHeader = db_config("header","");
  zFooter = db_config("footer","");

  /* user wants to restore the defaults */
  if( P("def") ){
    zHeader = HEADER;
................................................................................
  if( !g.okSetup ){
    cgi_redirect("setup");
    return;
  }

  if( P("sz") ){
    int sz = atoi(P("sz"))*1024;
    login_check_csrf_token();
    db_execute("REPLACE INTO config VALUES('max_attach_size',%d)", sz);
    db_config(0, 0);
    cgi_redirect("setup");
  }
 
  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminAttachment");
................................................................................
  common_header("Set Maximum Attachment Size");
  @ <p>
  @ Enter the maximum attachment size below.  If you enter a size of
  @ zero, attachments are disallowed.
  @ </p>
  @
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>
  @ Maximum attachment size in kilobytes: 
  @ <input type="text" name="sz" value="%d(attachment_max()/1024)" size=5>
  @ <input type="submit" value="Set">
  @ </p>
  @ </form>
  common_footer();
................................................................................
  if( !g.okSetup ){
    cgi_redirect("setup");
    return;
  }
  

  if( P("sz") && atoi(P("sz"))!=mxHit ){
    login_check_csrf_token();
    mxHit = atoi(P("sz"));
    db_execute("REPLACE INTO config VALUES('throttle',%d)", mxHit);
    db_config(0, 0);
  }

  if( P("nf") && atoi(P("nf"))!=nf ){
    login_check_csrf_token();
    nf = atoi(P("nf"));
    db_execute("REPLACE INTO config VALUES('nofollow_link',%d)", nf);
    db_config(0, 0);
  }
 
  if( P("cp") && atoi(P("cp"))!=cp ){
    login_check_csrf_token();
    cp = atoi(P("cp"));
    db_execute("REPLACE INTO config VALUES('enable_captcha',%d)", cp);
    db_config(0, 0);
  }
 
  if( P("lnk") && atoi(P("lnk"))!=lnk ){
    login_check_csrf_token();
    lnk = atoi(P("lnk"));
    db_execute("REPLACE INTO config VALUES('max_links_per_edit',%d)", lnk);
    db_config(0, 0);
  }

  if( P("mscore") && atoi(P("mscore"))!=mscore ){
    login_check_csrf_token();
    mscore = atoi(P("mscore"));
    db_execute("REPLACE INTO config VALUES('keywords_max_score',%d)", mscore);
    db_config(0, 0);
  }

  if( P("keys") && strcmp(zKeys,PD("keys","")) ){
    login_check_csrf_token();
    zKeys = P("keys");
    db_execute("REPLACE INTO config VALUES('keywords','%q')", zKeys);
    db_config(0, 0);
  }

  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminAbuse");
................................................................................
  @ <p>
  @ Enter the limit on the number of anonymous accesses from the same
  @ IP address that can occur within one hour.  Enter zero to disable
  @ the limiter.
  @ </p>
  @
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>
  @ Maximum hits per hour: 
  @ <input type="text" name="sz" value="%d(mxHit)" size=5>
  @ <input type="submit" value="Set">
  @ </p>
  @ </form>
  @
................................................................................
  @
  @ <p>A summary of the <a href="info_throttle">Access Log</a> is available
  @ separately.</p>

  @ <hr>
  @ <h2>Captcha</h2>
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>
  @ By turning on this option, anonymous users will be required to pass a
  @ simple <a href="http://en.wikipedia.org/wiki/Captcha">captcha</a>
  @ test before being allowed to change content (tickets, wiki, etc). Passing
  @ the test will set a cookie on the browser. Too many failures to pass
  @ the test will trigger the throttler and lock the users IP address out.
  @ Note that the rate limiter has to be enabled (non-zero) for this option
................................................................................
  @ </p>
  @ <input type="submit" value="Set">
  @ </form>
  @ <hr>

  @ <h2>External Links</h2>
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>
  @ By turning on this option, all links to external sites are tagged as
  @ "nofollow". This provides a hint to search engines to ignore such links
  @ and reduces the value of wiki spam. However, this may be of limited use
  @ since wiki spammers aren't always smart enough to notice that they're
  @ wasting their time.
  @ </p>
................................................................................
  @    %s(nf?" checked":"") value="1">
  @ Don't allow search engines to follow external links.</label>
  @ </p>
  @ <input type="submit" value="Set">
  @ </form>
  @ 
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>
  @ Wiki spam generally works by inserting large numbers of links in a
  @ single page edit. A simple way to prevent this is to simply impose a
  @ maximum number of new external links in a single wiki edit.
  @ A value of zero will disable this option.
  @ </p>
  @ <p>
................................................................................
  @ <input type="text" name="lnk" value="%d(lnk)" size=5>
  @ </p>
  @ <input type="submit" value="Set">
  @ </form>
  @ <hr>
  @ <h2>Keyword Filtering</h2>
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>
  @ Enter a space-separated list of keywords. All wiki edits will be
  @ checked against this list and, if the maximum score is exceeded, 
  @ the change will be denied. The scoring algorithm uses the standard
  @ CVSTrac text <strong>search()</strong> function (where each matched
  @ keyword scores from 6 to 10 points). Repeating a keyword in the
  @ list will cause it to score higher.
................................................................................
  common_add_help_item("CvstracAdminMarkup");
  common_header("Custom Wiki Markup");

  if( P("can") ){
    cgi_redirect("setup_markup");
    return;
  }else if( P("ok") ){
    login_check_csrf_token();

    /* delete it */
    db_execute("DELETE FROM markup WHERE markup='%q';", zMarkup);
    cgi_redirect("setup_markup");
    return;
  }else if( delete && zMarkup[0] ){
    @ <p>Are you sure you want to delete markup <b>%h(zMarkup)</b>?</p>
    @
    @ <form method="POST" action="setup_markupedit">
    login_output_csrf_token();
    @ <input type="hidden" name="m" value="%h(zMarkup)">
    @ <input type="submit" name="ok" value="Yes, Delete">
    @ <input type="submit" name="can" value="No, Cancel">
    @ </form>
    common_footer();
    return;
  }

  if( P("u") ){
    if( zMarkup[0] && zType[0] && zFormat[0] ) {
      login_check_csrf_token();

      /* update database and bounce back to listing page. If the
      ** description is empty, we'll survive (and wing it).
      */
      db_execute("REPLACE INTO markup(markup,type,formatter,description) "
                 "VALUES('%q',%d,'%q','%q');",
                 zMarkup, atoi(zType), zFormat, zDescription);
    }
................................................................................
      zType = az[0];
      zFormat = az[1];
      zDescription = az[2];
    }
  }

  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ Markup Name: <input type="text" name="m" value="%h(zMarkup)" size=12>
  cgi_optionmenu(0,"t",zType, "Markup","0", "Block","2",
    "Program Markup","1", "Program Block","3",
    "Trusted Program Markup","4", "Trusted Program Block","5",
    NULL);
  @ <br>Formatter:<br>
  @ <textarea name="f" rows="4" cols="60">%h(zFormat)</textarea><br>
................................................................................
                         "Delete");
  common_header("External Tools");

  if( P("can") ){
    cgi_redirect("setup_tools");
    return;
  }else if( P("ok") ){
    login_check_csrf_token();

    /* delete it */
    db_execute("DELETE FROM tool WHERE name='%q';", zTool);
    cgi_redirect("setup_tools");
    return;
  }else if( delete && zTool[0] ){
    @ <p>Are you sure you want to delete tool <b>%h(zTool)</b>?</p>
    @
    @ <form method="POST" action="setup_toolsedit">
    login_output_csrf_token();
    @ <input type="hidden" name="t" value="%h(zTool)">
    @ <input type="submit" name="ok" value="Yes, Delete">
    @ <input type="submit" name="can" value="No, Cancel">
    @ </form>
    common_footer();
    return;
  }

  if( P("u") ){
    if( zTool[0] && zPerms[0] && zObject[0] && zCommand[0] ) {
      /* update database and bounce back to listing page. If the
      ** description is empty, we'll survive (and wing it).
      */
      login_check_csrf_token();
      db_execute("REPLACE INTO tool(name,perms,object,command,description) "
                 "VALUES('%q','%q','%q','%q','%q');",
                 zTool, zPerms, zObject, zCommand, zDescription);
    }

    cgi_redirect("setup_tools");
  }
................................................................................
      zObject = az[1];
      zCommand = az[2];
      zDescription = az[3];
    }
  }

  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ Tool Name: <input type="text" name="t" value="%h(zTool)" size=12>
  cgi_optionmenu(0,"o",zObject,
                 "File","file",
                 "Wiki","wiki",
                 "Ticket","tkt",
                 "Check-in","chng",
                 "Milestone","ms",
................................................................................
  login_check_credentials();
  if( !g.okSetup ){
    cgi_redirect("setup");
    return;
  }

  if( P("bkup") ){
    login_check_csrf_token();
    db_execute("BEGIN");
    zMsg = file_copy(zDbName, zBuName);
    db_execute("COMMIT");
  }else if( P("rstr") ){
    login_check_csrf_token();
    db_execute("BEGIN");
    zMsg = file_copy(zBuName, zDbName);
    db_execute("COMMIT");
  }
 
  common_add_nav_item("setup", "Main Setup Menu");
  common_add_help_item("CvstracAdminBackup");
................................................................................
  @ It is always safe to do a backup.  The worst that can happen is that
  @ you can overwrite a prior backup.  But a restore can destroy your
  @ database if the backup copy you are restoring from is incorrect.
  @ Use caution when doing a restore.
  @ </p>
  @
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p><input type="submit" name="bkup" value="Backup"></p>
  @ <p><input type="submit" name="rstr" value="Restore"></p>
  @ </form>
  common_footer();
}

/*
................................................................................
  login_check_credentials();
  if( !g.okSetup ){
    cgi_redirect("setup");
    return;
  }
  
  if( P("cl") || P("ttl") || P("rdl") ){
    login_check_csrf_token();
    if( P("cl") ){
      int nCookieLife = atoi(P("cl"));
      db_execute("REPLACE INTO config VALUES('timeline_cookie_life',%d)", nCookieLife);
    }
    if( P("ttl") ){
      int nTTL = atoi(P("ttl"));
      db_execute("REPLACE INTO config VALUES('rss_ttl',%d)", nTTL);
................................................................................
  nCookieLife = atoi(db_config("timeline_cookie_life", "90"));
  nTTL = atoi(db_config("rss_ttl", "60"));
  nRDL = atoi(db_config("rss_detail_level", "5"));
  
  common_add_nav_item("setup", "Main Setup Menu");
  common_header("Timeline & RSS Setup");
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <p>
  @ Enter number of days timeline cookie should be kept by users browser.
  @ This cookie keeps timeline settings persistent across users multiple visits.<br>
  @ This applies to all users.<br>
  @ Set it to 0 to disable timeline cookie.
  @ </p>
  @ <p>

Changes to throttle.c.

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
** times.
*/
#define WIKI_EDIT_LOCKOUT 2

/*
** Return the name of the captcha cookie
*/
static char *captcha_cookie_name(void){
  return mprintf("%s_captcha", g.zName);
}

static void captcha_set_cookie(void){
  /* The captcha cookie is just an expiry time value. Nothing fancy,
  ** we don't need to keep much information. We do want to update it
  ** to a later expiry on successful connection...







|







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
** times.
*/
#define WIKI_EDIT_LOCKOUT 2

/*
** Return the name of the captcha cookie
*/
const char *captcha_cookie_name(void){
  return mprintf("%s_captcha", g.zName);
}

static void captcha_set_cookie(void){
  /* The captcha cookie is just an expiry time value. Nothing fancy,
  ** we don't need to keep much information. We do want to update it
  ** to a later expiry on successful connection...

Changes to ticket.c.

293
294
295
296
297
298
299


300
301
302
303
304
305
306
...
340
341
342
343
344
345
346

347
348
349
350
351
352
353
...
593
594
595
596
597
598
599

600
601
602
603
604
605
606
...
615
616
617
618
619
620
621


622
623
624
625
626
627
628
....
1097
1098
1099
1100
1101
1102
1103

1104
1105
1106
1107
1108
1109
1110
1111
1112


1113
1114
1115
1116
1117
1118
1119
....
1234
1235
1236
1237
1238
1239
1240


1241
1242
1243
1244
1245
1246
1247
....
1311
1312
1313
1314
1315
1316
1317

1318
1319
1320
1321
1322
1323
1324
....
1534
1535
1536
1537
1538
1539
1540


1541
1542
1543
1544
1545
1546
1547
....
1589
1590
1591
1592
1593
1594
1595

1596
1597
1598
1599
1600
1601
1602
  }
  if( zErrMsg==0 && zTitle[0] && zType[0] && zDesc[0] && P("submit")
      && (zContact[0] || !g.isAnon) ){
    int tn;
    time_t now;
    const char *zState;



    db_execute("BEGIN");
    az = db_query("SELECT max(tn)+1 FROM ticket");
    tn = atoi(az[0]);
    if( tn<=0 ) tn = 1;
    time(&now);
    zState = db_config("initial_state", "new");
    db_execute(
................................................................................
  common_header("Create A New Ticket");
  if( zErrMsg ){
    @ <blockquote class="error">
    @ %h(zErrMsg)
    @ </blockquote>
  }
  @ <form action="%T(g.zPath)" method="POST">

  @ <table cellpadding="5">
  @
  @ <tr>
  @ <td colspan=2>
  @ Enter a one-line summary of the problem:<br>
  @ <input type="text" name="t" size=70
  @     maxlength=%d(maxSummaryLength) value="%h(zTitle)">
................................................................................
    common_standard_menu(0,0);
    common_add_help_item("CvstracTicket");
    common_add_action_item(mprintf("tkthistory?tn=%d",tn), "Cancel");
    common_header("Undo Change To Ticket?");
    @ <p>If you really want to remove the last edit to ticket #%d(tn)
    @ then click on the "OK" link below.  Otherwise, click on "Cancel".</p>
    @ <form method="POST" action="tktundo">

    @ <input type="hidden" name="tn" value="%d(tn)">
    @ <input type="hidden" name="u" value="%t(zUser)">
    @ <input type="hidden" name="t" value="%d(tm)">
    @ <table cellpadding="30">
    @ <tr><td>
    @ <input type="submit" name="w" value="OK">
    @ </td><td>
................................................................................
  /* Make sure the change we are requested to undo is the vary last
  ** change.
  */
  z = db_short_query("SELECT max(chngtime) FROM tktchng WHERE tn=%d", tn);
  if( z==0 || tm!=atoi(z) ){
    goto undo_finished;
  }



  /* If we get this far, it means the user has confirmed that they
  ** want to undo the last change to the ticket.
  */
  db_execute("BEGIN");
  az = db_query("SELECT fieldid, oldval FROM tktchng "
                "WHERE tn=%d AND user='%q' AND chngtime=%d",
................................................................................
    @ &nbsp;<strong>%h(zTitle)</strong> from
    @ the database.  This is an irreversible operation.  All records
    @ related to this ticket will be removed and cannot be recovered.</p>
    @
    if( te ){
      @ <input type="hidden" name="le" value="%d(te)">
    }

    @ <input type="hidden" name="tn" value="%h(PD("tn",""))">
    @ <input type="submit" name="del2" value="Delete The Ticket">
    @ <input type="submit" name="can" value="Cancel">
    @ </form>
    common_footer();
    return;
  }

  if( P("del2") && ok_to_delete_ticket(tn) ){


    if( le && te > le ){
      common_add_action_item(mprintf("tktview?tn=%d",tn), "Cancel");
      common_add_action_item(mprintf("tktedit?tn=%d,%d&del1=1", tn, rn),
                             "Delete");
      common_header("Ticket Changed!");
      @ <p>Ticket #%d(tn) has been changed by someone else while you
      @ attempted to delete it!</p>
................................................................................
  /* Update the record in the TICKET table.  Also update the XREF table.
  */
  if( cnt==nField && P("submit")!=0 && (!le || le >= te) ){
    time_t now;
    char **az;
    int first_change;
    int last_change;


    
    time(&now);
    db_execute("BEGIN");
    az = db_query(
        "SELECT MAX(ROWID)+1 FROM tktchng"
    );
    first_change = atoi(az[0]);
................................................................................
    common_add_action_item( mprintf("tktedit?tn=%d,%d&del1=1", tn, rn),
                            "Delete");
  }
  common_add_help_item("CvstracTicket");
  common_header("Edit Ticket #%d", tn);

  @ <form action="tktedit" method="POST">

  @ 
  @ <input type="hidden" name="tn" value="%d(tn),%d(rn)">
  if( le || te ){
    /* Don't stomp on someone elses edit if they hit submit first. */
    @ <input type="hidden" name="le" value="%d(le ? le : te)">
  }
  @ <span class="nowrap">Ticket Number: %d(tn)</span><br>
................................................................................
    /* If le==0, we won't check for other changes while editting */
    le = atoi(PD("le","0"));

    if( le && le<te ) doSubmit = 0;
  }

  if( doSubmit ){


    zErrMsg = is_edit_allowed(0,zText);
    if( zText[0] && 0==zErrMsg ){
      time_t now;
      struct tm *pTm;
      char zDate[200];
      const char *zOrig;
      char *zNew;
................................................................................
  if( zErrMsg ){
    @ <blockquote class="error">
    @ %h(zErrMsg)
    @ </blockquote>
  }

  @ <form action="tktappend" method="POST">

  if( le || te ){
    /* Don't stomp on someone elses edit if they hit submit first. */
    @ <input type="hidden" name="le" value="%d(le ? le : te)">
  }
  @ <input type="hidden" name="tn" value="%h(zTn)">
  @ Append to #%d(tn):
  cgi_href(zTktTitle, 0, 0, 0, 0, 0, "tktview?tn=%d", tn);







>
>







 







>







 







>







 







>
>







 







>









>
>







 







>
>







 







>







 







>
>







 







>







293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
...
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
...
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
...
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
....
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
....
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
....
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
....
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
....
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
  }
  if( zErrMsg==0 && zTitle[0] && zType[0] && zDesc[0] && P("submit")
      && (zContact[0] || !g.isAnon) ){
    int tn;
    time_t now;
    const char *zState;

    login_check_csrf_token();

    db_execute("BEGIN");
    az = db_query("SELECT max(tn)+1 FROM ticket");
    tn = atoi(az[0]);
    if( tn<=0 ) tn = 1;
    time(&now);
    zState = db_config("initial_state", "new");
    db_execute(
................................................................................
  common_header("Create A New Ticket");
  if( zErrMsg ){
    @ <blockquote class="error">
    @ %h(zErrMsg)
    @ </blockquote>
  }
  @ <form action="%T(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <table cellpadding="5">
  @
  @ <tr>
  @ <td colspan=2>
  @ Enter a one-line summary of the problem:<br>
  @ <input type="text" name="t" size=70
  @     maxlength=%d(maxSummaryLength) value="%h(zTitle)">
................................................................................
    common_standard_menu(0,0);
    common_add_help_item("CvstracTicket");
    common_add_action_item(mprintf("tkthistory?tn=%d",tn), "Cancel");
    common_header("Undo Change To Ticket?");
    @ <p>If you really want to remove the last edit to ticket #%d(tn)
    @ then click on the "OK" link below.  Otherwise, click on "Cancel".</p>
    @ <form method="POST" action="tktundo">
    login_output_csrf_token();
    @ <input type="hidden" name="tn" value="%d(tn)">
    @ <input type="hidden" name="u" value="%t(zUser)">
    @ <input type="hidden" name="t" value="%d(tm)">
    @ <table cellpadding="30">
    @ <tr><td>
    @ <input type="submit" name="w" value="OK">
    @ </td><td>
................................................................................
  /* Make sure the change we are requested to undo is the vary last
  ** change.
  */
  z = db_short_query("SELECT max(chngtime) FROM tktchng WHERE tn=%d", tn);
  if( z==0 || tm!=atoi(z) ){
    goto undo_finished;
  }

  login_check_csrf_token();

  /* If we get this far, it means the user has confirmed that they
  ** want to undo the last change to the ticket.
  */
  db_execute("BEGIN");
  az = db_query("SELECT fieldid, oldval FROM tktchng "
                "WHERE tn=%d AND user='%q' AND chngtime=%d",
................................................................................
    @ &nbsp;<strong>%h(zTitle)</strong> from
    @ the database.  This is an irreversible operation.  All records
    @ related to this ticket will be removed and cannot be recovered.</p>
    @
    if( te ){
      @ <input type="hidden" name="le" value="%d(te)">
    }
    login_output_csrf_token();
    @ <input type="hidden" name="tn" value="%h(PD("tn",""))">
    @ <input type="submit" name="del2" value="Delete The Ticket">
    @ <input type="submit" name="can" value="Cancel">
    @ </form>
    common_footer();
    return;
  }

  if( P("del2") && ok_to_delete_ticket(tn) ){
    login_check_csrf_token();

    if( le && te > le ){
      common_add_action_item(mprintf("tktview?tn=%d",tn), "Cancel");
      common_add_action_item(mprintf("tktedit?tn=%d,%d&del1=1", tn, rn),
                             "Delete");
      common_header("Ticket Changed!");
      @ <p>Ticket #%d(tn) has been changed by someone else while you
      @ attempted to delete it!</p>
................................................................................
  /* Update the record in the TICKET table.  Also update the XREF table.
  */
  if( cnt==nField && P("submit")!=0 && (!le || le >= te) ){
    time_t now;
    char **az;
    int first_change;
    int last_change;

    login_check_csrf_token();
    
    time(&now);
    db_execute("BEGIN");
    az = db_query(
        "SELECT MAX(ROWID)+1 FROM tktchng"
    );
    first_change = atoi(az[0]);
................................................................................
    common_add_action_item( mprintf("tktedit?tn=%d,%d&del1=1", tn, rn),
                            "Delete");
  }
  common_add_help_item("CvstracTicket");
  common_header("Edit Ticket #%d", tn);

  @ <form action="tktedit" method="POST">
  login_output_csrf_token();
  @ 
  @ <input type="hidden" name="tn" value="%d(tn),%d(rn)">
  if( le || te ){
    /* Don't stomp on someone elses edit if they hit submit first. */
    @ <input type="hidden" name="le" value="%d(le ? le : te)">
  }
  @ <span class="nowrap">Ticket Number: %d(tn)</span><br>
................................................................................
    /* If le==0, we won't check for other changes while editting */
    le = atoi(PD("le","0"));

    if( le && le<te ) doSubmit = 0;
  }

  if( doSubmit ){
    login_check_csrf_token();

    zErrMsg = is_edit_allowed(0,zText);
    if( zText[0] && 0==zErrMsg ){
      time_t now;
      struct tm *pTm;
      char zDate[200];
      const char *zOrig;
      char *zNew;
................................................................................
  if( zErrMsg ){
    @ <blockquote class="error">
    @ %h(zErrMsg)
    @ </blockquote>
  }

  @ <form action="tktappend" method="POST">
  login_output_csrf_token();
  if( le || te ){
    /* Don't stomp on someone elses edit if they hit submit first. */
    @ <input type="hidden" name="le" value="%d(le ? le : te)">
  }
  @ <input type="hidden" name="tn" value="%h(zTn)">
  @ Append to #%d(tn):
  cgi_href(zTktTitle, 0, 0, 0, 0, 0, "tktview?tn=%d", tn);

Changes to timeline.c.

1221
1222
1223
1224
1225
1226
1227


1228
1229
1230
1231
1232
1233
1234
....
1246
1247
1248
1249
1250
1251
1252

1253
1254
1255
1256
1257
1258
1259
....
1285
1286
1287
1288
1289
1290
1291

1292
1293
1294
1295
1296
1297
1298
1299
1300
1301

1302
1303
1304
1305
1306
1307
1308
....
1400
1401
1402
1403
1404
1405
1406

1407
1408
1409
1410
1411
1412
1413
1414
1415
1416

1417
1418
1419
1420
1421
1422
1423
1424


1425
1426
1427
1428
1429
1430
1431
....
1479
1480
1481
1482
1483
1484
1485

1486
1487
1488
1489
1490
1491
1492
  zCancel = mprintf("chngview?cn=%d", cn);
  if( P("can") ){
    cgi_redirect(zCancel);
    return;
  }
  zMsg = P("m");
  if( zMsg ){


    /* Update message in CHNG table and update XREF table in case some 
    ** tickets were removed or added in new message. 
    ** Preserve manually added associations by pre-deleteing only 
    ** those XREF rows that are present in old message.
    */
    db_execute("BEGIN");
    zOldMsg = db_short_query("SELECT message FROM chng WHERE cn=%d", cn);
................................................................................
    milestone_edit();
    return;
  }
  common_add_action_item(zCancel, "Cancel");
  common_add_help_item("CvstracCheckin");
  common_header("Edit Check-in [%d]", cn);
  @ <form action="chngedit" method="POST">

  @ <input type="hidden" name="cn" value="%d(cn)">
  @ Edit the change message and press "OK":<br>
  cgi_wikitext("m",40,zMsg);
  @ <blockquote>
  @ <input type="submit" name="ok" value="OK">
  @ <input type="submit" name="can" value="Cancel">
  @ </blockquote>
................................................................................
  if( P("can") ){
    cgi_redirect(zCancel);
    return;
  }
  if( zResult && P("ok") ){
    time_t now;
    time(&now);

    db_execute("INSERT INTO inspect(cn,inspecttime,inspector,result,comment) "
       "VALUES(%d,%d,'%q','%q','%q')",
       cn, now, g.zUser, zResult, zComment);
    cgi_redirect(zCancel);
    return;
  }
  common_add_help_item("CvstracInspection");
  common_header("Inspection Report");

  @ <form action="inspect" method="POST">

  @ <input type="hidden" name="cn" value="%d(cn)">
  /*
  ** FIXME: admin should be able to pre-define accepted inspection types
  ** which we could provide in a dropdown
  */
  cgi_text("r",0,0,0,0,40,1024,1,zResult?zResult:"","Inspection results");
  @ <br>Comment:
................................................................................
  zBr = PD("br","");
  mtype = atoi(zMType);

  if( cn>0 && P("del1") ){
    common_add_action_item(mprintf("msedit?cn=%d",cn), "Cancel");
    common_header("Are You Sure?");
    @ <form action="msedit" method="POST">

    @ <p>You are about to delete all traces of milestone [%d(cn)] from
    @ the database.  This is an irreversible operation.</p>
    @
    @ <input type="hidden" name="cn" value="%d(cn)">
    @ <input type="submit" name="del2" value="Delete The Milestone">
    @ <input type="submit" name="can" value="Cancel">
    @ </form>
    common_footer();
    return;
  }else if( cn>0 && P("del2") ){

    db_execute("BEGIN");
    db_execute("DELETE FROM chng WHERE cn=%d AND milestone", cn);
    db_execute("DELETE FROM xref WHERE cn=%d", cn);
    db_execute("COMMIT");
    cgi_redirect("index");
  }

  if( tm>0 && zTime[0] && zMsg[0] && mtype>0 ){


    if( cn>0 ){
      /*
      ** Preserve manually added associations by pre-deleteing only 
      ** those XREF rows that are present in old message.
      */
      db_execute("BEGIN");
      zOldMsg = db_short_query("SELECT message FROM chng WHERE cn=%d", cn);
................................................................................
    common_header("Edit Milestone");
  }else{
    common_standard_menu("msnew", 0);
    common_add_action_item("index", "Cancel");
    common_header("Create New Milestone");
  }
  @ <form action="msedit" method="POST">

  if( cn>0 ){
    @ <input type="hidden" name="cn" value="%d(cn)">
  }
  pTm = localtime(&tm);
  strftime(zDate, sizeof(zDate), "%Y-%b-%d %H:%M:%S", pTm);
  @ <table>
  @ <tr><td align="right">Date &amp; Time:</td>







>
>







 







>







 







>










>







 







>










>








>
>







 







>







1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
....
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
....
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
....
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
....
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
  zCancel = mprintf("chngview?cn=%d", cn);
  if( P("can") ){
    cgi_redirect(zCancel);
    return;
  }
  zMsg = P("m");
  if( zMsg ){
    login_check_csrf_token();

    /* Update message in CHNG table and update XREF table in case some 
    ** tickets were removed or added in new message. 
    ** Preserve manually added associations by pre-deleteing only 
    ** those XREF rows that are present in old message.
    */
    db_execute("BEGIN");
    zOldMsg = db_short_query("SELECT message FROM chng WHERE cn=%d", cn);
................................................................................
    milestone_edit();
    return;
  }
  common_add_action_item(zCancel, "Cancel");
  common_add_help_item("CvstracCheckin");
  common_header("Edit Check-in [%d]", cn);
  @ <form action="chngedit" method="POST">
  login_output_csrf_token();
  @ <input type="hidden" name="cn" value="%d(cn)">
  @ Edit the change message and press "OK":<br>
  cgi_wikitext("m",40,zMsg);
  @ <blockquote>
  @ <input type="submit" name="ok" value="OK">
  @ <input type="submit" name="can" value="Cancel">
  @ </blockquote>
................................................................................
  if( P("can") ){
    cgi_redirect(zCancel);
    return;
  }
  if( zResult && P("ok") ){
    time_t now;
    time(&now);
    login_check_csrf_token();
    db_execute("INSERT INTO inspect(cn,inspecttime,inspector,result,comment) "
       "VALUES(%d,%d,'%q','%q','%q')",
       cn, now, g.zUser, zResult, zComment);
    cgi_redirect(zCancel);
    return;
  }
  common_add_help_item("CvstracInspection");
  common_header("Inspection Report");

  @ <form action="inspect" method="POST">
  login_output_csrf_token();
  @ <input type="hidden" name="cn" value="%d(cn)">
  /*
  ** FIXME: admin should be able to pre-define accepted inspection types
  ** which we could provide in a dropdown
  */
  cgi_text("r",0,0,0,0,40,1024,1,zResult?zResult:"","Inspection results");
  @ <br>Comment:
................................................................................
  zBr = PD("br","");
  mtype = atoi(zMType);

  if( cn>0 && P("del1") ){
    common_add_action_item(mprintf("msedit?cn=%d",cn), "Cancel");
    common_header("Are You Sure?");
    @ <form action="msedit" method="POST">
    login_output_csrf_token();
    @ <p>You are about to delete all traces of milestone [%d(cn)] from
    @ the database.  This is an irreversible operation.</p>
    @
    @ <input type="hidden" name="cn" value="%d(cn)">
    @ <input type="submit" name="del2" value="Delete The Milestone">
    @ <input type="submit" name="can" value="Cancel">
    @ </form>
    common_footer();
    return;
  }else if( cn>0 && P("del2") ){
    login_check_csrf_token();
    db_execute("BEGIN");
    db_execute("DELETE FROM chng WHERE cn=%d AND milestone", cn);
    db_execute("DELETE FROM xref WHERE cn=%d", cn);
    db_execute("COMMIT");
    cgi_redirect("index");
  }

  if( tm>0 && zTime[0] && zMsg[0] && mtype>0 ){
    login_check_csrf_token();

    if( cn>0 ){
      /*
      ** Preserve manually added associations by pre-deleteing only 
      ** those XREF rows that are present in old message.
      */
      db_execute("BEGIN");
      zOldMsg = db_short_query("SELECT message FROM chng WHERE cn=%d", cn);
................................................................................
    common_header("Edit Milestone");
  }else{
    common_standard_menu("msnew", 0);
    common_add_action_item("index", "Cancel");
    common_header("Create New Milestone");
  }
  @ <form action="msedit" method="POST">
  login_output_csrf_token();
  if( cn>0 ){
    @ <input type="hidden" name="cn" value="%d(cn)">
  }
  pTm = localtime(&tm);
  strftime(zDate, sizeof(zDate), "%Y-%b-%d %H:%M:%S", pTm);
  @ <table>
  @ <tr><td align="right">Date &amp; Time:</td>

Changes to user.c.

165
166
167
168
169
170
171

172
173
174
175
176
177
178
...
229
230
231
232
233
234
235


236
237
238
239
240
241
242
...
318
319
320
321
322
323
324

325
326
327
328
329
330
331
    if( P("delete") ){
      common_add_action_item("userlist", "Cancel");
      common_header("Are You Sure?");
      @ <form action="useredit" method="POST">
      @ <p>You are about to delete the user <strong>%h(zId)</strong> from
      @ the database.  This is an irreversible operation.</p>
      @

      @ <input type="hidden" name="id" value="%h(zId)">
      @ <input type="hidden" name="nm" value="">
      @ <input type="hidden" name="em" value="">
      @ <input type="hidden" name="pw" value="">
      @ <input type="submit" name="delete2" value="Delete The User">
      @ <input type="submit" name="can" value="Cancel">
      @ </form>
................................................................................
    if( an ){ zCap[i++] = 'n'; }
    if( ao ){ zCap[i++] = 'o'; }
    if( ap ){ zCap[i++] = 'p'; }
    if( aq ){ zCap[i++] = 'q'; }
    if( ar ){ zCap[i++] = 'r'; }
    if( as ){ zCap[i++] = 's'; }
    if( aw ){ zCap[i++] = 'w'; }



    zCap[i] = 0;
    db_execute("DELETE FROM user WHERE id='%q'", zId);
    if( !P("delete2") ){
      const char *zPw = P("pw");
      char zBuf[3];
      if( zOldPw==0 ){
................................................................................
  common_add_action_item(mprintf("useredit?delete=1&id=%t",zId), "Delete");
  if( zId ){
    common_header("Edit User %s", zId);
  }else{
    common_header("Add New User");
  }
  @ <form action="%s(g.zPath)" method="POST">

  @ <table align="left" style="margin: 10px;">
  @ <tr>
  @   <td align="right" class="nowrap">User ID:</td>
  if( zId ){
    @   <td>%h(zId) <input type="hidden" name="id" value="%h(zId)"></td>
  }else{
    @   <td><input type="text" name="id" size=10></td>







>







 







>
>







 







>







165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
...
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
    if( P("delete") ){
      common_add_action_item("userlist", "Cancel");
      common_header("Are You Sure?");
      @ <form action="useredit" method="POST">
      @ <p>You are about to delete the user <strong>%h(zId)</strong> from
      @ the database.  This is an irreversible operation.</p>
      @
      login_output_csrf_token();
      @ <input type="hidden" name="id" value="%h(zId)">
      @ <input type="hidden" name="nm" value="">
      @ <input type="hidden" name="em" value="">
      @ <input type="hidden" name="pw" value="">
      @ <input type="submit" name="delete2" value="Delete The User">
      @ <input type="submit" name="can" value="Cancel">
      @ </form>
................................................................................
    if( an ){ zCap[i++] = 'n'; }
    if( ao ){ zCap[i++] = 'o'; }
    if( ap ){ zCap[i++] = 'p'; }
    if( aq ){ zCap[i++] = 'q'; }
    if( ar ){ zCap[i++] = 'r'; }
    if( as ){ zCap[i++] = 's'; }
    if( aw ){ zCap[i++] = 'w'; }

    login_check_csrf_token();

    zCap[i] = 0;
    db_execute("DELETE FROM user WHERE id='%q'", zId);
    if( !P("delete2") ){
      const char *zPw = P("pw");
      char zBuf[3];
      if( zOldPw==0 ){
................................................................................
  common_add_action_item(mprintf("useredit?delete=1&id=%t",zId), "Delete");
  if( zId ){
    common_header("Edit User %s", zId);
  }else{
    common_header("Add New User");
  }
  @ <form action="%s(g.zPath)" method="POST">
  login_output_csrf_token();
  @ <table align="left" style="margin: 10px;">
  @ <tr>
  @   <td align="right" class="nowrap">User ID:</td>
  if( zId ){
    @   <td>%h(zId) <input type="hidden" name="id" value="%h(zId)"></td>
  }else{
    @   <td><input type="text" name="id" size=10></td>

Changes to view.c.

335
336
337
338
339
340
341

342
343
344
345
346
347
348
349
350
351
352

353
354
355
356
357
358
359
...
377
378
379
380
381
382
383

384
385
386
387
388
389
390
...
451
452
453
454
455
456
457

458
459
460
461
462
463
464
  zTitle = P("t");
  zOwner = PD("w",g.zUser);
  z = P("s");
  zSQL = z ? trim_string(z) : 0;
  zClrKey = trim_string(PD("k",""));
  zDesc = PD("d","");
  if( rn>0 && P("del2") ){

    db_execute("DELETE FROM reportfmt WHERE rn=%d", rn);
    cgi_redirect("reportlist");
    return;
  }else if( rn>0 && P("del1") ){
    zTitle = db_short_query("SELECT title FROM reportfmt "
                            "WHERE rn=%d", rn);
    if( zTitle==0 ) cgi_redirect("reportlist");

    common_add_action_item(mprintf("rptview?rn=%d",rn), "Cancel");
    common_header("Are You Sure?");
    @ <form action="rptedit" method="POST">

    @ <p>You are about to delete all traces of the report
    @ <strong>%h(zTitle)</strong> from
    @ the database.  This is an irreversible operation.  All records
    @ related to this report will be removed and cannot be recovered.</p>
    @
    @ <input type="hidden" name="rn" value="%d(rn)">
    @ <input type="submit" name="del2" value="Delete The Report">
................................................................................
      /* check query syntax by actually trying the query */
      db_restrict_access(1);
      zErr = db_query_check("%s", zSQL);
      if( zErr ) zErr = mprintf("%s",zErr);
      db_restrict_access(0);
    }
    if( zErr==0 ){

      if( rn>0 ){
        db_execute("UPDATE reportfmt SET title='%q', sqlcode='%q',"
                   " owner='%q', cols='%q', description='%q' WHERE rn=%d",
           zTitle, zSQL, zOwner, zClrKey, zDesc, rn);
      }else{
        db_execute("INSERT INTO "
                   "reportfmt(title,sqlcode,owner,cols,description) "
................................................................................
  }
  common_add_help_item("CvstracReport");
  common_header(rn>0 ? "Edit Report Format":"Create New Report Format");
  if( zErr ){
    @ <blockquote class="error">%h(zErr)</blockquote>
  }
  @ <form action="rptedit" method="POST">

  @ <input type="hidden" name="rn" value="%d(rn)">
  @ <p>Report Title:<br>
  @ <input type="text" name="t" value="%h(zTitle)" size="60"></p>
  @ <p>Enter a complete SQL query statement against the "TICKET" table:<br>
  cgi_textarea("s","sql",20,70,zSQL);
  @ </p>
  if( g.okAdmin ){







>











>







 







>







 







>







335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
...
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
  zTitle = P("t");
  zOwner = PD("w",g.zUser);
  z = P("s");
  zSQL = z ? trim_string(z) : 0;
  zClrKey = trim_string(PD("k",""));
  zDesc = PD("d","");
  if( rn>0 && P("del2") ){
    login_check_csrf_token();
    db_execute("DELETE FROM reportfmt WHERE rn=%d", rn);
    cgi_redirect("reportlist");
    return;
  }else if( rn>0 && P("del1") ){
    zTitle = db_short_query("SELECT title FROM reportfmt "
                            "WHERE rn=%d", rn);
    if( zTitle==0 ) cgi_redirect("reportlist");

    common_add_action_item(mprintf("rptview?rn=%d",rn), "Cancel");
    common_header("Are You Sure?");
    @ <form action="rptedit" method="POST">
    login_output_csrf_token();
    @ <p>You are about to delete all traces of the report
    @ <strong>%h(zTitle)</strong> from
    @ the database.  This is an irreversible operation.  All records
    @ related to this report will be removed and cannot be recovered.</p>
    @
    @ <input type="hidden" name="rn" value="%d(rn)">
    @ <input type="submit" name="del2" value="Delete The Report">
................................................................................
      /* check query syntax by actually trying the query */
      db_restrict_access(1);
      zErr = db_query_check("%s", zSQL);
      if( zErr ) zErr = mprintf("%s",zErr);
      db_restrict_access(0);
    }
    if( zErr==0 ){
      login_check_csrf_token();
      if( rn>0 ){
        db_execute("UPDATE reportfmt SET title='%q', sqlcode='%q',"
                   " owner='%q', cols='%q', description='%q' WHERE rn=%d",
           zTitle, zSQL, zOwner, zClrKey, zDesc, rn);
      }else{
        db_execute("INSERT INTO "
                   "reportfmt(title,sqlcode,owner,cols,description) "
................................................................................
  }
  common_add_help_item("CvstracReport");
  common_header(rn>0 ? "Edit Report Format":"Create New Report Format");
  if( zErr ){
    @ <blockquote class="error">%h(zErr)</blockquote>
  }
  @ <form action="rptedit" method="POST">
  login_output_csrf_token();
  @ <input type="hidden" name="rn" value="%d(rn)">
  @ <p>Report Title:<br>
  @ <input type="text" name="t" value="%h(zTitle)" size="60"></p>
  @ <p>Enter a complete SQL query statement against the "TICKET" table:<br>
  cgi_textarea("s","sql",20,70,zSQL);
  @ </p>
  if( g.okAdmin ){

Changes to wiki.c.

548
549
550
551
552
553
554

555
556
557
558
559
560
561
562
563

564
565
566
567
568
569
570
...
582
583
584
585
586
587
588

589
590
591
592
593
594
595
...
764
765
766
767
768
769
770

771
772
773
774
775
776
777
...
853
854
855
856
857
858
859

860
861
862
863
864
865
866
    "FROM wiki WHERE name='%q' LIMIT 1", pg
  );
  isLocked = az[0] ? atoi(az[1]) : 0;
  if( !g.okAdmin && !isHome && (!g.okWiki || isLocked) ){
    cgi_redirect(mprintf("wiki?p=%t", pg));
  }
  if( g.okAdmin && az[0] && P("lock")!=0 ){

    isLocked = !isLocked;
    db_execute("UPDATE wiki SET locked=%d WHERE name='%q'", isLocked, pg);
    if( text && strcmp(remove_blank_lines(text),remove_blank_lines(az[4]))==0 ){
      cgi_redirect(mprintf("wiki?p=%t",pg));
      return;
    }
  }
  if( P("submit")!=0 && text!=0 ){
    time_t now = time(0);

    char *zOld = db_short_query("SELECT text FROM wiki "
                                "WHERE name='%q' AND invtime>=%d LIMIT 1",
                                pg, now);
    zErrMsg = is_edit_allowed(zOld,text);
    if( 0==zErrMsg ){
      const char *zIp = getenv("REMOTE_ADDR");
      if( zIp==0 ){ zIp = ""; }
................................................................................
  text = remove_blank_lines(text);
  common_add_help_item("CvstracWiki");
  common_add_action_item( mprintf("wiki?p=%t",pg), "Cancel");
  common_header("Edit Wiki %h", pg);

  @ <p><big><b>Edit: "%h(wiki_expand_name(pg))"</b></big></p>
  @ <form action="wikiedit" method="POST">

  @ <input type="hidden" name="p" value="%h(pg)">

  if( zErrMsg ){
    @ <blockquote class="error">
    @ %h(zErrMsg)
    @ </blockquote>
  }
................................................................................
     "Cancel"     
  );
  common_add_help_item("CvstracWiki");
  common_header("Verify Delete");
  @ <p><big><b>Delete Wiki Page "%h(wiki_expand_name(pg))"?</b></big></p>
  @ <p>All delete actions are irreversible. Make your choice carefully!</p>
  @ <form action="wikidodel" method="POST">

  @ <input type="hidden" name="p" value="%h(pg)">
  if( P("t") ){
    @ <input type="hidden" name="t" value="%d(tm)">
  }
  @ <input type="hidden" name="t2" value="%d(tm)">
  @ <table border=0 cellpadding=5>
  @
................................................................................
    if( t==0 ){
      cgi_redirect(mprintf("wiki?p=%t",pg));
    }else{
      cgi_redirect(mprintf("wiki?p=%t&t=%t",pg,t));
    }
    return;
  }

  db_add_functions();
  if( isHome || g.okSetup ){
    /* The Setup user can delete anything. A user can always delete their
    ** home page.
    */
    zRestrict = "";
  }else if( g.okDelete ){







>









>







 







>







 







>







 







>







548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
...
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
...
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
...
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
    "FROM wiki WHERE name='%q' LIMIT 1", pg
  );
  isLocked = az[0] ? atoi(az[1]) : 0;
  if( !g.okAdmin && !isHome && (!g.okWiki || isLocked) ){
    cgi_redirect(mprintf("wiki?p=%t", pg));
  }
  if( g.okAdmin && az[0] && P("lock")!=0 ){
    login_check_csrf_token();
    isLocked = !isLocked;
    db_execute("UPDATE wiki SET locked=%d WHERE name='%q'", isLocked, pg);
    if( text && strcmp(remove_blank_lines(text),remove_blank_lines(az[4]))==0 ){
      cgi_redirect(mprintf("wiki?p=%t",pg));
      return;
    }
  }
  if( P("submit")!=0 && text!=0 ){
    time_t now = time(0);
    login_check_csrf_token();
    char *zOld = db_short_query("SELECT text FROM wiki "
                                "WHERE name='%q' AND invtime>=%d LIMIT 1",
                                pg, now);
    zErrMsg = is_edit_allowed(zOld,text);
    if( 0==zErrMsg ){
      const char *zIp = getenv("REMOTE_ADDR");
      if( zIp==0 ){ zIp = ""; }
................................................................................
  text = remove_blank_lines(text);
  common_add_help_item("CvstracWiki");
  common_add_action_item( mprintf("wiki?p=%t",pg), "Cancel");
  common_header("Edit Wiki %h", pg);

  @ <p><big><b>Edit: "%h(wiki_expand_name(pg))"</b></big></p>
  @ <form action="wikiedit" method="POST">
  login_output_csrf_token();
  @ <input type="hidden" name="p" value="%h(pg)">

  if( zErrMsg ){
    @ <blockquote class="error">
    @ %h(zErrMsg)
    @ </blockquote>
  }
................................................................................
     "Cancel"     
  );
  common_add_help_item("CvstracWiki");
  common_header("Verify Delete");
  @ <p><big><b>Delete Wiki Page "%h(wiki_expand_name(pg))"?</b></big></p>
  @ <p>All delete actions are irreversible. Make your choice carefully!</p>
  @ <form action="wikidodel" method="POST">
  login_output_csrf_token();
  @ <input type="hidden" name="p" value="%h(pg)">
  if( P("t") ){
    @ <input type="hidden" name="t" value="%d(tm)">
  }
  @ <input type="hidden" name="t2" value="%d(tm)">
  @ <table border=0 cellpadding=5>
  @
................................................................................
    if( t==0 ){
      cgi_redirect(mprintf("wiki?p=%t",pg));
    }else{
      cgi_redirect(mprintf("wiki?p=%t&t=%t",pg,t));
    }
    return;
  }
  login_check_csrf_token();
  db_add_functions();
  if( isHome || g.okSetup ){
    /* The Setup user can delete anything. A user can always delete their
    ** home page.
    */
    zRestrict = "";
  }else if( g.okDelete ){