Hex Artifact Content
Not logged in

Artifact 9050864c16dda3b1234deae0d7f669aabbac7896:

File src/browse.c part of check-in [d57de28756] - The "h" capability is now used to enable hyperlinks to non-wiki pages. When "h" is missing, many pages give a hyperlink to the login page and automatically fill in "anonymous" as the user name. The login page jumps back to the target page after a successful login. by drh on 2008-05-05 23:15:29.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 38 20 44 2e 20 52 69 63 68  (c) 2008 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74   contains code t
0380: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
0390: 66 69 6c 65 20 62 72 6f 77 73 65 72 20 77 65 62  file browser web
03a0: 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23   interface..*/.#
03b0: 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 67 2e  include "config.
03c0: 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 62 72 6f  h".#include "bro
03d0: 77 73 65 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20  wse.h".#include 
03e0: 3c 61 73 73 65 72 74 2e 68 3e 0a 0a 2f 2a 0a 2a  <assert.h>../*.*
03f0: 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d  * This is the im
0400: 70 6c 65 6d 65 6e 74 69 6f 6e 20 6f 66 20 74 68  plemention of th
0410: 65 20 22 70 61 74 68 65 6c 65 6d 65 6e 74 28 58  e "pathelement(X
0420: 2c 4e 29 22 20 53 51 4c 20 66 75 6e 63 74 69 6f  ,N)" SQL functio
0430: 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 58 20 69 73  n..**.** If X is
0440: 20 61 20 75 6e 69 78 2d 6c 69 6b 65 20 70 61 74   a unix-like pat
0450: 68 6e 61 6d 65 20 28 77 69 74 68 20 22 2f 22 20  hname (with "/" 
0460: 73 65 70 61 72 61 74 6f 72 73 29 20 61 6e 64 20  separators) and 
0470: 4e 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65 67  N is an.** integ
0480: 65 72 2c 20 74 68 65 6e 20 73 6b 69 70 20 74 68  er, then skip th
0490: 65 20 69 6e 69 74 69 61 6c 20 4e 20 63 68 61 72  e initial N char
04a0: 61 63 74 65 72 73 20 6f 66 20 58 20 61 6e 64 20  acters of X and 
04b0: 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 61  return the.** na
04c0: 6d 65 20 6f 66 20 74 68 65 20 70 61 74 68 20 63  me of the path c
04d0: 6f 6d 70 6f 6e 65 6e 74 20 74 68 61 74 20 62 65  omponent that be
04e0: 67 69 6e 73 20 6f 6e 20 74 68 65 20 4e 2b 31 74  gins on the N+1t
04f0: 68 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 28  h character.** (
0500: 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 29  numbered from 0)
0510: 2e 20 20 49 66 20 74 68 65 20 70 61 74 68 20 63  .  If the path c
0520: 6f 6d 70 6f 6e 65 6e 74 20 69 73 20 61 20 64 69  omponent is a di
0530: 72 65 63 74 6f 72 79 20 28 69 66 0a 2a 2a 20 69  rectory (if.** i
0540: 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79  t is followed by
0550: 20 6f 74 68 65 72 20 70 61 74 68 20 63 6f 6d 70   other path comp
0560: 6f 6e 65 6e 74 73 29 20 74 68 65 6e 20 70 72 65  onents) then pre
0570: 70 65 6e 64 20 22 2f 22 2e 0a 2a 2a 0a 2a 2a 20  pend "/"..**.** 
0580: 45 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  Examples:.**.** 
0590: 20 20 20 20 20 70 61 74 68 65 6c 65 6d 65 6e 74       pathelement
05a0: 28 27 61 62 63 2f 70 71 72 2f 78 79 7a 27 2c 20  ('abc/pqr/xyz', 
05b0: 34 29 20 20 2d 3e 20 20 27 2f 70 71 72 27 0a 2a  4)  ->  '/pqr'.*
05c0: 2a 20 20 20 20 20 20 70 61 74 68 65 6c 65 6d 65  *      patheleme
05d0: 6e 74 28 27 61 62 63 2f 70 71 72 27 2c 20 34 29  nt('abc/pqr', 4)
05e0: 20 20 20 20 20 20 2d 3e 20 20 27 70 71 72 27 0a        ->  'pqr'.
05f0: 2a 2a 20 20 20 20 20 20 70 61 74 68 65 6c 65 6d  **      pathelem
0600: 65 6e 74 28 27 61 62 63 2f 70 71 72 2f 78 79 7a  ent('abc/pqr/xyz
0610: 27 2c 20 30 29 20 20 2d 3e 20 20 27 2f 61 62 63  ', 0)  ->  '/abc
0620: 27 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  '.*/.static void
0630: 20 70 61 74 68 65 6c 65 6d 65 6e 74 46 75 6e 63   pathelementFunc
0640: 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
0650: 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
0660: 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
0670: 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
0680: 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  .){.  const unsi
0690: 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20  gned char *z;.  
06a0: 69 6e 74 20 6c 65 6e 2c 20 6e 2c 20 69 3b 0a 20  int len, n, i;. 
06b0: 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 0a 20 20   char *zOut;..  
06c0: 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20  assert( argc==2 
06d0: 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  );.  z = sqlite3
06e0: 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
06f0: 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30  [0]);.  if( z==0
0700: 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6c 65 6e   ) return;.  len
0710: 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
0720: 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b  _bytes(argv[0]);
0730: 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76  .  n = sqlite3_v
0740: 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d  alue_int(argv[1]
0750: 29 3b 0a 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 20  );.  if( len<=n 
0760: 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
0770: 6e 3e 30 20 26 26 20 7a 5b 6e 2d 31 5d 21 3d 27  n>0 && z[n-1]!='
0780: 2f 27 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  /' ) return;.  f
0790: 6f 72 28 69 3d 6e 3b 20 69 3c 6c 65 6e 20 26 26  or(i=n; i<len &&
07a0: 20 7a 5b 69 5d 21 3d 27 2f 27 3b 20 69 2b 2b 29   z[i]!='/'; i++)
07b0: 7b 7d 0a 20 20 69 66 28 20 69 3d 3d 6c 65 6e 20  {}.  if( i==len 
07c0: 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
07d0: 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
07e0: 78 74 2c 20 28 63 68 61 72 2a 29 26 7a 5b 6e 5d  xt, (char*)&z[n]
07f0: 2c 20 6c 65 6e 2d 6e 2c 20 53 51 4c 49 54 45 5f  , len-n, SQLITE_
0800: 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65  TRANSIENT);.  }e
0810: 6c 73 65 7b 0a 20 20 20 20 7a 4f 75 74 20 3d 20  lse{.    zOut = 
0820: 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
0830: 22 2f 25 2e 2a 73 22 2c 20 69 2d 6e 2c 20 26 7a  "/%.*s", i-n, &z
0840: 5b 6e 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  [n]);.    sqlite
0850: 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
0860: 6e 74 65 78 74 2c 20 7a 4f 75 74 2c 20 69 2d 6e  ntext, zOut, i-n
0870: 2b 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65  +1, sqlite3_free
0880: 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
0890: 47 69 76 65 6e 20 61 20 70 61 74 68 6e 61 6d 65  Given a pathname
08a0: 20 77 68 69 63 68 20 69 73 20 61 20 72 65 6c 61   which is a rela
08b0: 74 69 76 65 20 70 61 74 68 20 66 72 6f 6d 20 74  tive path from t
08c0: 68 65 20 72 6f 6f 74 20 6f 66 0a 2a 2a 20 74 68  he root of.** th
08d0: 65 20 72 65 70 6f 73 69 74 6f 72 79 20 74 6f 20  e repository to 
08e0: 61 20 66 69 6c 65 20 6f 72 20 64 69 72 65 63 74  a file or direct
08f0: 6f 72 79 2c 20 63 6f 6d 70 75 74 65 20 61 20 73  ory, compute a s
0900: 74 72 69 6e 67 20 77 68 69 63 68 0a 2a 2a 20 69  tring which.** i
0910: 73 20 61 6e 20 48 54 4d 4c 20 72 65 6e 64 65 72  s an HTML render
0920: 69 6e 67 20 6f 66 20 74 68 61 74 20 70 61 74 68  ing of that path
0930: 20 77 69 74 68 20 68 79 70 65 72 6c 69 6e 6b 73   with hyperlinks
0940: 20 6f 6e 20 65 61 63 68 0a 2a 2a 20 64 69 72 65   on each.** dire
0950: 63 74 6f 72 79 20 63 6f 6d 70 6f 6e 65 6e 74 20  ctory component 
0960: 6f 66 20 74 68 65 20 70 61 74 68 20 77 68 65 72  of the path wher
0970: 65 20 74 68 65 20 68 79 70 65 72 6c 69 6e 6b 20  e the hyperlink 
0980: 72 65 64 69 72 65 63 74 73 0a 2a 2a 20 74 6f 20  redirects.** to 
0990: 74 68 65 20 22 64 69 72 22 20 70 61 67 65 20 66  the "dir" page f
09a0: 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  or the directory
09b0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73  ..**.** There is
09c0: 20 6e 6f 20 68 79 70 65 72 6c 69 6e 6b 20 6f 6e   no hyperlink on
09d0: 20 74 68 65 20 66 69 6c 65 20 65 6c 65 6d 65 6e   the file elemen
09e0: 74 20 6f 66 20 74 68 65 20 70 61 74 68 2e 0a 2a  t of the path..*
09f0: 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d 70 75 74 65  *.** The compute
0a00: 64 20 73 74 72 69 6e 67 20 69 73 20 61 70 70 65  d string is appe
0a10: 6e 64 65 64 20 74 6f 20 74 68 65 20 70 4f 75 74  nded to the pOut
0a20: 20 62 6c 6f 62 2e 20 20 70 4f 75 74 20 73 68 6f   blob.  pOut sho
0a30: 75 6c 64 0a 2a 2a 20 68 61 76 65 20 61 6c 72 65  uld.** have alre
0a40: 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  ady been initial
0a50: 69 7a 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 68 79  ized..*/.void hy
0a60: 70 65 72 6c 69 6e 6b 65 64 5f 70 61 74 68 28 63  perlinked_path(c
0a70: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
0a80: 2c 20 42 6c 6f 62 20 2a 70 4f 75 74 29 7b 0a 20  , Blob *pOut){. 
0a90: 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61   int i, j;.  cha
0aa0: 72 20 2a 7a 53 65 70 20 3d 20 22 22 3b 0a 0a 20  r *zSep = "";.. 
0ab0: 20 66 6f 72 28 69 3d 30 3b 20 7a 50 61 74 68 5b   for(i=0; zPath[
0ac0: 69 5d 3b 20 69 3d 6a 29 7b 0a 20 20 20 20 66 6f  i]; i=j){.    fo
0ad0: 72 28 6a 3d 69 3b 20 7a 50 61 74 68 5b 6a 5d 20  r(j=i; zPath[j] 
0ae0: 26 26 20 7a 50 61 74 68 5b 6a 5d 21 3d 27 2f 27  && zPath[j]!='/'
0af0: 3b 20 6a 2b 2b 29 7b 7d 0a 20 20 20 20 69 66 28  ; j++){}.    if(
0b00: 20 7a 50 61 74 68 5b 6a 5d 20 26 26 20 67 2e 6f   zPath[j] && g.o
0b10: 6b 48 69 73 74 6f 72 79 20 29 7b 0a 20 20 20 20  kHistory ){.    
0b20: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70    blob_appendf(p
0b30: 4f 75 74 2c 20 22 25 73 3c 61 20 68 72 65 66 3d  Out, "%s<a href=
0b40: 5c 22 25 73 2f 64 69 72 3f 6e 61 6d 65 3d 25 23  \"%s/dir?name=%#
0b50: 54 5c 22 3e 25 23 68 3c 2f 61 3e 22 2c 20 0a 20  T\">%#h</a>", . 
0b60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
0b70: 20 20 7a 53 65 70 2c 20 67 2e 7a 42 61 73 65 55    zSep, g.zBaseU
0b80: 52 4c 2c 20 6a 2c 20 7a 50 61 74 68 2c 20 6a 2d  RL, j, zPath, j-
0b90: 69 2c 20 26 7a 50 61 74 68 5b 69 5d 29 3b 0a 20  i, &zPath[i]);. 
0ba0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
0bb0: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 4f 75  blob_appendf(pOu
0bc0: 74 2c 20 22 25 73 25 23 68 22 2c 20 7a 53 65 70  t, "%s%#h", zSep
0bd0: 2c 20 6a 2d 69 2c 20 26 7a 50 61 74 68 5b 69 5d  , j-i, &zPath[i]
0be0: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 65  );.    }.    zSe
0bf0: 70 20 3d 20 22 2f 22 3b 0a 20 20 20 20 77 68 69  p = "/";.    whi
0c00: 6c 65 28 20 7a 50 61 74 68 5b 6a 5d 3d 3d 27 2f  le( zPath[j]=='/
0c10: 27 20 29 7b 20 6a 2b 2b 3b 20 7d 0a 20 20 7d 0a  ' ){ j++; }.  }.
0c20: 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47  }.../*.** WEBPAG
0c30: 45 3a 20 64 69 72 0a 2a 2a 0a 2a 2a 20 51 75 65  E: dir.**.** Que
0c40: 72 79 20 70 61 72 61 6d 65 74 65 72 73 3a 0a 2a  ry parameters:.*
0c50: 2a 0a 2a 2a 20 20 20 20 6e 61 6d 65 3d 50 41 54  *.**    name=PAT
0c60: 48 20 20 20 20 20 20 20 20 44 69 72 65 63 74 6f  H        Directo
0c70: 72 79 20 74 6f 20 64 69 73 70 6c 61 79 2e 20 20  ry to display.  
0c80: 52 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 76 6f 69  Required..*/.voi
0c90: 64 20 70 61 67 65 5f 64 69 72 28 76 6f 69 64 29  d page_dir(void)
0ca0: 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  {.  const char *
0cb0: 7a 44 20 3d 20 50 28 22 6e 61 6d 65 22 29 3b 0a  zD = P("name");.
0cc0: 20 20 69 6e 74 20 6d 78 4c 65 6e 3b 0a 20 20 69    int mxLen;.  i
0cd0: 6e 74 20 6e 43 6f 6c 2c 20 6e 52 6f 77 3b 0a 20  nt nCol, nRow;. 
0ce0: 20 69 6e 74 20 63 6e 74 2c 20 69 3b 0a 20 20 63   int cnt, i;.  c
0cf0: 68 61 72 20 2a 7a 50 72 65 66 69 78 3b 0a 20 20  har *zPrefix;.  
0d00: 53 74 6d 74 20 71 3b 0a 0a 20 20 6c 6f 67 69 6e  Stmt q;..  login
0d10: 5f 63 68 65 63 6b 5f 63 72 65 64 65 6e 74 69 61  _check_credentia
0d20: 6c 73 28 29 3b 0a 20 20 69 66 28 20 21 67 2e 6f  ls();.  if( !g.o
0d30: 6b 48 69 73 74 6f 72 79 20 29 7b 20 6c 6f 67 69  kHistory ){ logi
0d40: 6e 5f 6e 65 65 64 65 64 28 29 3b 20 72 65 74 75  n_needed(); retu
0d50: 72 6e 3b 20 7d 0a 20 20 73 74 79 6c 65 5f 68 65  rn; }.  style_he
0d60: 61 64 65 72 28 22 46 69 6c 65 20 4c 69 73 74 22  ader("File List"
0d70: 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  );.  sqlite3_cre
0d80: 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 67 2e 64  ate_function(g.d
0d90: 62 2c 20 22 70 61 74 68 65 6c 65 6d 65 6e 74 22  b, "pathelement"
0da0: 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , 2, SQLITE_UTF8
0db0: 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
0dc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
0dd0: 61 74 68 65 6c 65 6d 65 6e 74 46 75 6e 63 2c 20  athelementFunc, 
0de0: 30 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  0, 0);..  /* If 
0df0: 74 68 65 20 6e 61 6d 65 3d 20 70 61 72 61 6d 65  the name= parame
0e00: 74 65 72 20 69 73 20 61 6e 20 65 6d 70 74 79 20  ter is an empty 
0e10: 73 74 72 69 6e 67 2c 20 6d 61 6b 65 20 69 74 20  string, make it 
0e20: 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 2a  a NULL pointer *
0e30: 2f 0a 20 20 69 66 28 20 7a 44 20 26 26 20 73 74  /.  if( zD && st
0e40: 72 6c 65 6e 28 7a 44 29 3d 3d 30 20 29 7b 20 7a  rlen(zD)==0 ){ z
0e50: 44 20 3d 20 30 3b 20 7d 0a 0a 20 20 2f 2a 20 43  D = 0; }..  /* C
0e60: 6f 6d 70 75 74 65 20 74 68 65 20 74 69 74 6c 65  ompute the title
0e70: 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 20   of the page */ 
0e80: 20 0a 20 20 69 66 28 20 7a 44 20 29 7b 0a 20 20   .  if( zD ){.  
0e90: 20 20 42 6c 6f 62 20 74 69 74 6c 65 3b 0a 0a 20    Blob title;.. 
0ea0: 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 74 69     blob_zero(&ti
0eb0: 74 6c 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61  tle);.    blob_a
0ec0: 70 70 65 6e 64 66 28 26 74 69 74 6c 65 2c 20 22  ppendf(&title, "
0ed0: 46 69 6c 65 73 20 69 6e 20 64 69 72 65 63 74 6f  Files in directo
0ee0: 72 79 20 22 29 3b 0a 20 20 20 20 68 79 70 65 72  ry ");.    hyper
0ef0: 6c 69 6e 6b 65 64 5f 70 61 74 68 28 7a 44 2c 20  linked_path(zD, 
0f00: 26 74 69 74 6c 65 29 3b 0a 20 20 20 20 40 20 3c  &title);.    @ <
0f10: 68 32 3e 25 73 28 62 6c 6f 62 5f 73 74 72 28 26  h2>%s(blob_str(&
0f20: 74 69 74 6c 65 29 29 3c 2f 68 32 3e 0a 20 20 20  title))</h2>.   
0f30: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 74 69 74   blob_reset(&tit
0f40: 6c 65 29 3b 0a 20 20 20 20 7a 50 72 65 66 69 78  le);.    zPrefix
0f50: 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 68 2f 22   = mprintf("%h/"
0f60: 2c 20 7a 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  , zD);.  }else{.
0f70: 20 20 20 20 40 20 3c 68 32 3e 46 69 6c 65 73 20      @ <h2>Files 
0f80: 69 6e 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c  in the top-level
0f90: 20 64 69 72 65 63 74 6f 72 79 3c 2f 68 32 3e 0a   directory</h2>.
0fa0: 20 20 20 20 7a 50 72 65 66 69 78 20 3d 20 22 22      zPrefix = ""
0fb0: 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70  ;.  }..  /* Comp
0fc0: 75 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72  ute the temporar
0fd0: 79 20 74 61 62 6c 65 20 22 6c 6f 63 61 6c 66 69  y table "localfi
0fe0: 6c 65 73 22 20 63 6f 6e 74 61 69 6e 69 6e 67 20  les" containing 
0ff0: 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f  the names.  ** o
1000: 66 20 61 6c 6c 20 66 69 6c 65 73 20 61 6e 64 20  f all files and 
1010: 73 75 62 64 69 72 65 63 74 6f 72 69 65 73 20 69  subdirectories i
1020: 6e 20 74 68 65 20 7a 44 5b 5d 20 64 69 72 65 63  n the zD[] direc
1030: 74 6f 72 79 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a  tory.  .  **.  *
1040: 2a 20 53 75 62 64 69 72 65 63 74 6f 72 79 20 6e  * Subdirectory n
1050: 61 6d 65 73 20 62 65 67 69 6e 20 77 69 74 68 20  ames begin with 
1060: 22 2f 22 2e 20 20 54 68 69 73 20 63 61 75 73 65  "/".  This cause
1070: 73 20 74 68 65 6d 20 74 6f 20 73 6f 72 74 0a 20  s them to sort. 
1080: 20 2a 2a 20 66 69 72 73 74 20 61 6e 64 20 69 74   ** first and it
1090: 20 61 6c 73 6f 20 67 69 76 65 73 20 75 73 20 61   also gives us a
10a0: 6e 20 65 61 73 79 20 77 61 79 20 74 6f 20 64 69  n easy way to di
10b0: 73 74 69 6e 67 75 69 73 68 20 66 69 6c 65 73 0a  stinguish files.
10c0: 20 20 2a 2a 20 66 72 6f 6d 20 64 69 72 65 63 74    ** from direct
10d0: 6f 72 69 65 73 20 69 6e 20 74 68 65 20 6c 6f 6f  ories in the loo
10e0: 70 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a  p that follows..
10f0: 20 20 2a 2f 0a 20 20 69 66 28 20 7a 44 20 29 7b    */.  if( zD ){
1100: 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  .    db_multi_ex
1110: 65 63 28 0a 20 20 20 20 20 20 20 22 43 52 45 41  ec(.       "CREA
1120: 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6c 6f  TE TEMP TABLE lo
1130: 63 61 6c 66 69 6c 65 73 28 78 20 55 4e 49 51 55  calfiles(x UNIQU
1140: 45 20 4e 4f 54 20 4e 55 4c 4c 29 3b 22 0a 20 20  E NOT NULL);".  
1150: 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20       "INSERT OR 
1160: 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6c 6f 63 61  IGNORE INTO loca
1170: 6c 66 69 6c 65 73 20 22 0a 20 20 20 20 20 20 20  lfiles ".       
1180: 22 20 20 53 45 4c 45 43 54 20 70 61 74 68 65 6c  "  SELECT pathel
1190: 65 6d 65 6e 74 28 6e 61 6d 65 2c 25 64 29 20 46  ement(name,%d) F
11a0: 52 4f 4d 20 66 69 6c 65 6e 61 6d 65 22 0a 20 20  ROM filename".  
11b0: 20 20 20 20 20 22 20 20 20 57 48 45 52 45 20 2b       "   WHERE +
11c0: 6e 61 6d 65 20 47 4c 4f 42 20 27 25 71 2f 2a 27  name GLOB '%q/*'
11d0: 22 2c 0a 20 20 20 20 20 20 20 73 74 72 6c 65 6e  ",.       strlen
11e0: 28 7a 44 29 2b 31 2c 20 7a 44 0a 20 20 20 20 29  (zD)+1, zD.    )
11f0: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64  ;.  }else{.    d
1200: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
1210: 20 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d       "CREATE TEM
1220: 50 20 54 41 42 4c 45 20 6c 6f 63 61 6c 66 69 6c  P TABLE localfil
1230: 65 73 28 78 20 55 4e 49 51 55 45 20 4e 4f 54 20  es(x UNIQUE NOT 
1240: 4e 55 4c 4c 29 3b 22 0a 20 20 20 20 20 20 20 22  NULL);".       "
1250: 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45  INSERT OR IGNORE
1260: 20 49 4e 54 4f 20 6c 6f 63 61 6c 66 69 6c 65 73   INTO localfiles
1270: 20 22 0a 20 20 20 20 20 20 20 22 20 20 53 45 4c   ".       "  SEL
1280: 45 43 54 20 70 61 74 68 65 6c 65 6d 65 6e 74 28  ECT pathelement(
1290: 6e 61 6d 65 2c 30 29 20 46 52 4f 4d 20 66 69 6c  name,0) FROM fil
12a0: 65 6e 61 6d 65 22 0a 20 20 20 20 29 3b 0a 20 20  ename".    );.  
12b0: 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  }..  /* Generate
12c0: 20 61 20 6d 75 6c 74 69 2d 63 6f 6c 75 6d 6e 20   a multi-column 
12d0: 74 61 62 6c 65 20 6c 69 73 74 69 6e 67 20 74 68  table listing th
12e0: 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 7a 44  e contents of zD
12f0: 5b 5d 0a 20 20 2a 2a 20 64 69 72 65 63 74 6f 72  [].  ** director
1300: 79 2e 0a 20 20 2a 2f 0a 20 20 6d 78 4c 65 6e 20  y..  */.  mxLen 
1310: 3d 20 64 62 5f 69 6e 74 28 31 32 2c 20 22 53 45  = db_int(12, "SE
1320: 4c 45 43 54 20 6d 61 78 28 6c 65 6e 67 74 68 28  LECT max(length(
1330: 78 29 29 20 46 52 4f 4d 20 6c 6f 63 61 6c 66 69  x)) FROM localfi
1340: 6c 65 73 22 29 3b 0a 20 20 63 6e 74 20 3d 20 64  les");.  cnt = d
1350: 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54  b_int(0, "SELECT
1360: 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 6c   count(*) FROM l
1370: 6f 63 61 6c 66 69 6c 65 73 22 29 3b 0a 20 20 6e  ocalfiles");.  n
1380: 43 6f 6c 20 3d 20 34 3b 0a 20 20 6e 52 6f 77 20  Col = 4;.  nRow 
1390: 3d 20 28 63 6e 74 2b 6e 43 6f 6c 2d 31 29 2f 6e  = (cnt+nCol-1)/n
13a0: 43 6f 6c 3b 0a 20 20 64 62 5f 70 72 65 70 61 72  Col;.  db_prepar
13b0: 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 78 20  e(&q, "SELECT x 
13c0: 46 52 4f 4d 20 6c 6f 63 61 6c 66 69 6c 65 73 20  FROM localfiles 
13d0: 4f 52 44 45 52 20 42 59 20 78 22 29 3b 0a 20 20  ORDER BY x");.  
13e0: 40 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d  @ <table border=
13f0: 22 30 22 20 77 69 64 74 68 3d 22 31 30 30 25 25  "0" width="100%%
1400: 22 3e 3c 74 72 3e 3c 74 64 20 76 61 6c 69 67 6e  "><tr><td valign
1410: 3d 22 74 6f 70 22 20 77 69 64 74 68 3d 22 32 35  ="top" width="25
1420: 25 25 22 3e 0a 20 20 69 20 3d 20 30 3b 0a 20 20  %%">.  i = 0;.  
1430: 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26  while( db_step(&
1440: 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  q)==SQLITE_ROW )
1450: 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
1460: 20 2a 7a 46 4e 61 6d 65 3b 0a 20 20 20 20 69 66   *zFName;.    if
1470: 28 20 69 3d 3d 6e 52 6f 77 20 29 7b 0a 20 20 20  ( i==nRow ){.   
1480: 20 20 20 40 20 3c 2f 74 64 3e 3c 74 64 20 76 61     @ </td><td va
1490: 6c 69 67 6e 3d 22 74 6f 70 22 20 77 69 64 74 68  lign="top" width
14a0: 3d 22 32 35 25 25 22 3e 0a 20 20 20 20 20 20 69  ="25%%">.      i
14b0: 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
14c0: 69 2b 2b 3b 0a 20 20 20 20 7a 46 4e 61 6d 65 20  i++;.    zFName 
14d0: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  = db_column_text
14e0: 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  (&q, 0);.    if(
14f0: 20 7a 46 4e 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20   zFName[0]=='/' 
1500: 29 7b 0a 20 20 20 20 20 20 7a 46 4e 61 6d 65 2b  ){.      zFName+
1510: 2b 3b 0a 20 20 20 20 20 20 40 20 3c 6c 69 3e 3c  +;.      @ <li><
1520: 61 20 68 72 65 66 3d 22 25 73 28 67 2e 7a 42 61  a href="%s(g.zBa
1530: 73 65 55 52 4c 29 2f 64 69 72 3f 6e 61 6d 65 3d  seURL)/dir?name=
1540: 25 54 28 7a 50 72 65 66 69 78 29 25 54 28 7a 46  %T(zPrefix)%T(zF
1550: 4e 61 6d 65 29 22 3e 0a 20 20 20 20 20 20 40 20  Name)">.      @ 
1560: 20 20 20 20 25 68 28 7a 46 4e 61 6d 65 29 2f 3c      %h(zFName)/<
1570: 2f 61 3e 3c 2f 6c 69 3e 0a 20 20 20 20 7d 65 6c  /a></li>.    }el
1580: 73 65 7b 0a 20 20 20 20 20 20 40 20 3c 6c 69 3e  se{.      @ <li>
1590: 3c 61 20 68 72 65 66 3d 22 25 73 28 67 2e 7a 42  <a href="%s(g.zB
15a0: 61 73 65 55 52 4c 29 2f 66 69 6e 66 6f 3f 6e 61  aseURL)/finfo?na
15b0: 6d 65 3d 25 54 28 7a 50 72 65 66 69 78 29 25 54  me=%T(zPrefix)%T
15c0: 28 7a 46 4e 61 6d 65 29 22 3e 0a 20 20 20 20 20  (zFName)">.     
15d0: 20 40 20 20 20 20 20 25 68 28 7a 46 4e 61 6d 65   @     %h(zFName
15e0: 29 3c 2f 61 3e 3c 2f 6c 69 3e 0a 20 20 20 20 7d  )</a></li>.    }
15f0: 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61 6c 69  .  }.  db_finali
1600: 7a 65 28 26 71 29 3b 0a 20 20 40 20 3c 2f 74 64  ze(&q);.  @ </td
1610: 3e 3c 2f 74 72 3e 3c 2f 74 61 62 6c 65 3e 0a 20  ></tr></table>. 
1620: 20 73 74 79 6c 65 5f 66 6f 6f 74 65 72 28 29 3b   style_footer();
1630: 0a 7d 0a                                         .}.