#!/usr/bin/perl
# This script is Copyright 1995, Steven Dick <ssd@mae.engr.ucf.edu>
# Copy this freely, use as you like, but please send me e-mail.

# Original documentation can be found at:
#   http://www-mae.engr.ucf.edu/~ssd/counter.html

# Newest change: this will accept GET and POST format data.

# Note:  If you set the value of a key to a negative value,
# no image will be generated for that key.

# This version of the script will generate an error document instead of a gif
# if the given key is not exactly right.

# Note: there are obsolete features still supported in this script for
# backwards compatability.
# Much of the configuring below (other than the file paths) is to make
# those obsolete features work.  These features are documented in:
#   http://www-mae.engr.ucf.edu/~ssd/counter/perl-old.html

# Initial setup: You will need to modify paths in various places here.
# Not everything is at the top of this script.  Scan through the things
# at the top; also search for:
#   If you don't have your cgi-bins set up the same
#   way I have mine set up, you may have to add more shifts down below
#   in the key interpretation section.  (search for SHIFT)  Rule of thumb
#   is that you need one shift per slash in the base url after the hostname.
#
#   Search for 'mail', and modify as appropriate for your site.
#   If you need help debuging, you can comment it out in one place below.
#
#   This script sends reply errors back refering to my counter page in
#   two or three places.  You can leave that as is if you like.

# Style note:  I've left all my debugging code in, but commented out.
#   Should I delete some of it?  :)

# help lusers debug by sending mail
$badkeymail = 0;
$debuguser="real-ssd";

# counter database is a dbm file--some versions of dbm require it to
# be pre-created by touching the data and index files (.pag and .dir).
# Check your local man pages...
$database="/home/system/www/counters";

# CERN httpd works best with SCRIPT_NAME
# NCSA httpd works best with PATH_INFO
# This may be sensitive to the way I configured it.
# This method will be obsoleted by std GET or POST methods.
# Uncomment the 'mail' debug stuff somewhere below to be sure.
$PathVar = 'SCRIPT_NAME';
# $PathVar = 'PATH_INFO';

# for security...
$ENV{PATH}="/bin:/usr/bin:/usr/local/bin";

# The primary font...
$line00[0]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[0]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[0]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[0]="80 30 34 36 36 63 132 146 119 51 38 38 32 20 55";
$line04[0]="80 32 37 38 120 255 255 255 255 242 80 35 27 15 56";
$line05[0]="70 39 42 70 255 255 202 148 228 255 227 25 22 17 56";
$line06[0]="74 44 44 162 255 228 32 28 55 255 255 94 20 18 57";
$line07[0]="64 38 39 200 255 169 22 21 21 226 255 138 20 18 56";
$line08[0]="51 27 27 241 255 138 20 20 20 196 255 181 20 16 56";
$line09[0]="42 21 21 255 255 138 20 20 20 196 255 196 18 13 56";
$line10[0]="51 18 20 255 255 138 20 20 20 196 255 195 13 10 57";
$line11[0]="51 11 15 240 255 137 18 16 16 195 255 177 6 4 56";
$line12[0]="55 3 6 193 255 162 7 7 5 223 255 128 1 1 56";
$line13[0]="57 0 1 143 255 223 1 1 33 255 255 80 0 0 55";
$line14[0]="57 0 0 32 255 255 191 128 223 255 223 0 0 0 54";
$line15[0]="54 0 0 0 96 255 255 255 255 239 48 0 0 0 53";
$line16[0]="32 0 0 0 0 32 112 128 96 16 0 0 0 0 52";
$line17[0]="34 0 0 0 0 0 0 0 0 0 0 0 0 0 50";
$line18[0]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[0]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";

$line00[1]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[1]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[1]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[1]="80 30 34 36 36 36 36 63 255 255 38 38 32 20 55";
$line04[1]="80 32 37 38 39 40 53 188 255 255 40 35 27 15 56";
$line05[1]="70 39 42 43 123 202 255 255 255 255 31 25 22 17 56";
$line06[1]="74 44 44 44 149 255 255 255 255 255 22 21 20 18 57";
$line07[1]="64 38 39 37 31 26 22 138 255 255 20 20 20 18 56";
$line08[1]="51 27 27 25 22 21 20 138 255 255 20 20 20 16 56";
$line09[1]="42 21 21 21 20 20 20 137 254 254 20 20 18 13 56";
$line10[1]="51 18 20 20 20 20 20 134 245 247 19 17 13 10 57";
$line11[1]="51 11 15 18 18 18 18 124 230 231 11 7 6 4 56";
$line12[1]="55 3 6 7 8 7 7 114 219 219 2 1 1 1 56";
$line13[1]="57 0 1 1 1 1 1 110 218 218 0 0 0 0 55";
$line14[1]="57 0 0 0 0 0 0 109 218 218 0 0 0 0 54";
$line15[1]="54 0 0 0 0 0 0 82 163 163 0 0 0 0 53";
$line16[1]="32 0 0 0 0 0 0 0 0 0 0 0 0 0 52";
$line17[1]="34 0 0 0 0 0 0 0 0 0 0 0 0 0 50";
$line18[1]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[1]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";

$line00[2]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[2]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[2]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[2]="80 30 34 36 63 159 200 200 146 64 38 38 32 20 55";
$line04[2]="80 32 37 79 241 255 255 255 255 242 80 35 27 15 56";
$line05[2]="70 39 42 215 255 229 110 108 214 255 227 25 22 17 56";
$line06[2]="74 44 70 255 255 120 32 28 98 255 255 65 20 18 57";
$line07[2]="64 38 80 200 199 69 22 21 80 255 255 79 20 18 56";
$line08[2]="51 27 27 25 22 21 20 20 167 255 255 35 20 16 56";
$line09[2]="42 21 21 21 20 20 20 122 253 254 167 20 18 13 56";
$line10[2]="51 18 20 20 20 34 160 245 247 206 34 17 13 10 57";
$line11[2]="51 11 15 18 45 194 234 235 166 28 11 7 6 4 56";
$line12[2]="55 3 6 21 213 226 226 89 5 4 2 1 1 1 56";
$line13[2]="57 0 1 166 220 220 42 1 1 0 0 0 0 0 55";
$line14[2]="57 0 27 218 218 218 218 218 218 218 218 55 0 0 54";
$line15[2]="54 0 55 218 218 218 218 218 218 218 218 55 0 0 53";
$line16[2]="32 0 0 0 0 0 0 0 0 0 0 0 0 0 52";
$line17[2]="34 0 0 0 0 0 0 0 0 0 0 0 0 0 50";
$line18[2]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[2]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";

$line00[3]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[3]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[3]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[3]="80 30 34 36 36 36 36 36 37 37 38 38 32 20 55";
$line04[3]="80 32 37 38 93 188 255 255 215 109 40 35 27 15 56";
$line05[3]="70 39 42 96 255 255 255 255 255 255 101 25 22 17 56";
$line06[3]="74 44 44 189 255 187 46 42 226 255 211 21 20 18 57";
$line07[3]="64 38 39 255 255 83 22 21 138 255 255 20 20 18 56";
$line08[3]="51 27 27 83 80 36 20 20 167 255 211 20 20 16 56";
$line09[3]="42 21 21 21 20 20 138 166 254 239 79 20 18 13 56";
$line10[3]="51 18 20 20 20 20 254 252 250 220 62 17 13 10 57";
$line11[3]="51 11 15 18 18 18 77 103 215 243 214 7 6 4 56";
$line12[3]="55 3 22 68 69 22 7 7 79 240 240 61 1 1 56";
$line13[3]="57 0 63 244 241 60 1 1 59 232 235 59 0 0 55";
$line14[3]="57 0 0 216 227 169 14 14 183 225 215 0 0 0 54";
$line15[3]="54 0 0 97 220 219 219 219 219 220 84 0 0 0 53";
$line16[3]="32 0 0 0 68 163 218 218 163 68 0 0 0 0 52";
$line17[3]="34 0 0 0 0 0 0 0 0 0 0 0 0 0 50";
$line18[3]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[3]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";

$line00[4]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[4]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[4]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[4]="80 30 34 36 36 36 36 36 37 37 38 38 32 20 55";
$line04[4]="80 32 37 38 39 40 40 121 201 202 121 35 27 15 56";
$line05[4]="70 39 42 43 44 44 56 242 255 255 143 25 22 17 56";
$line06[4]="74 44 44 44 43 39 171 255 255 255 139 21 20 18 57";
$line07[4]="64 38 39 37 31 69 255 167 255 255 138 20 20 18 56";
$line08[4]="51 27 27 25 22 196 239 49 254 254 137 20 20 16 56";
$line09[4]="42 21 21 21 107 250 135 20 250 250 135 20 18 13 56";
$line10[4]="51 18 20 34 215 215 34 20 242 242 131 17 13 10 57";
$line11[4]="51 11 15 127 235 100 18 16 237 237 124 7 6 4 56";
$line12[4]="55 3 6 176 231 174 174 173 231 231 202 131 1 1 56";
$line13[4]="57 0 1 173 229 226 225 224 224 225 225 170 0 0 55";
$line14[4]="57 0 0 43 56 55 55 55 218 219 137 42 0 0 54";
$line15[4]="54 0 0 0 0 0 0 0 218 218 109 0 0 0 53";
$line16[4]="32 0 0 0 0 0 0 0 218 218 109 0 0 0 52";
$line17[4]="34 0 0 0 0 0 0 0 0 0 0 0 0 0 50";
$line18[4]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[4]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";

$line00[5]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[5]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[5]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[5]="80 30 34 36 36 36 36 36 37 37 38 38 32 20 55";
$line04[5]="80 32 37 38 147 148 148 148 148 149 121 35 27 15 56";
$line05[5]="70 39 42 56 255 255 255 255 255 255 199 25 22 17 56";
$line06[5]="74 44 44 97 255 228 144 142 141 139 110 21 20 18 57";
$line07[5]="64 38 39 105 254 168 22 21 21 20 20 20 20 18 56";
$line08[5]="51 27 27 137 248 135 49 77 49 20 20 20 20 16 56";
$line09[5]="42 21 21 145 243 215 243 243 243 174 34 20 18 13 56";
$line10[5]="51 18 20 186 241 241 199 199 240 240 158 17 13 10 57";
$line11[5]="51 11 15 100 127 86 18 16 165 233 234 21 6 4 56";
$line12[5]="55 3 6 7 8 7 7 7 59 222 223 57 1 1 56";
$line13[5]="57 0 29 111 111 28 1 1 55 217 217 41 0 0 55";
$line14[5]="57 0 14 217 215 108 0 0 146 213 201 0 0 0 54";
$line15[5]="54 0 0 119 210 209 169 182 208 208 92 0 0 0 53";
$line16[5]="32 0 0 13 116 207 207 207 194 104 0 0 0 0 52";
$line17[5]="34 0 0 0 0 13 52 52 0 0 0 0 0 0 50";
$line18[5]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[5]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";

$line00[6]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[6]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[6]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[6]="80 30 34 36 36 36 36 36 37 37 38 38 32 20 55";
$line04[6]="80 32 37 38 39 148 228 255 228 149 53 35 27 15 56";
$line05[6]="70 39 42 43 189 255 255 255 255 255 157 25 22 17 56";
$line06[6]="74 44 44 97 255 255 116 28 141 255 255 36 20 18 57";
$line07[6]="64 38 39 173 255 198 22 21 36 79 79 35 20 18 56";
$line08[6]="51 27 27 197 255 138 79 138 123 49 20 20 20 16 56";
$line09[6]="42 21 21 252 252 237 251 251 251 237 107 20 18 13 56";
$line10[6]="51 18 20 245 245 245 174 132 230 244 230 31 13 10 57";
$line11[6]="51 11 15 241 241 184 18 16 72 240 240 95 6 4 56";
$line12[6]="55 3 6 206 234 121 7 7 5 233 235 118 1 1 56";
$line13[6]="57 0 1 156 226 127 1 1 15 226 227 86 0 0 55";
$line14[6]="57 0 0 83 220 206 55 0 137 220 221 28 0 0 54";
$line15[6]="54 0 0 0 177 218 218 218 218 219 123 0 0 0 53";
$line16[6]="32 0 0 0 14 136 191 218 191 96 0 0 0 0 52";
$line17[6]="34 0 0 0 0 0 0 0 0 0 0 0 0 0 50";
$line18[6]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[6]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";

$line00[7]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[7]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[7]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[7]="80 30 34 36 36 36 36 36 37 37 38 38 32 20 55";
$line04[7]="80 32 37 147 147 148 148 148 148 149 148 118 27 15 56";
$line05[7]="70 39 42 255 255 255 255 255 255 255 255 197 22 17 56";
$line06[7]="74 44 44 150 149 147 144 142 154 255 255 138 20 18 57";
$line07[7]="64 38 39 37 31 26 22 21 182 255 181 20 20 18 56";
$line08[7]="51 27 27 25 22 21 20 107 251 238 35 20 20 16 56";
$line09[7]="42 21 21 21 20 20 34 224 239 104 20 20 18 13 56";
$line10[7]="51 18 20 20 20 20 126 230 205 20 19 17 13 10 57";
$line11[7]="51 11 15 18 18 18 217 230 110 14 11 7 6 4 56";
$line12[7]="55 3 6 7 8 76 228 228 19 4 2 1 1 1 56";
$line13[7]="57 0 1 1 1 139 221 180 1 0 0 0 0 0 55";
$line14[7]="57 0 0 0 0 191 218 109 0 0 0 0 0 0 54";
$line15[7]="54 0 0 0 0 218 218 82 0 0 0 0 0 0 53";
$line16[7]="32 0 0 0 55 218 218 55 0 0 0 0 0 0 52";
$line17[7]="34 0 0 0 0 0 0 0 0 0 0 0 0 0 50";
$line18[7]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[7]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";

$line00[8]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[8]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[8]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[8]="80 30 34 36 77 186 255 255 214 105 38 38 32 20 55";
$line04[8]="80 32 37 79 241 255 255 255 255 255 134 35 27 15 56";
$line05[8]="70 39 42 175 255 215 70 41 174 255 227 25 22 17 56";
$line06[8]="74 44 44 202 255 147 32 28 83 255 255 21 20 18 57";
$line07[8]="64 38 39 145 253 225 51 21 166 252 179 20 20 18 56";
$line08[8]="51 27 27 39 189 245 245 244 244 215 62 20 20 16 56";
$line09[8]="42 21 21 75 222 235 234 234 234 234 154 20 18 13 56";
$line10[8]="51 18 20 218 229 190 33 20 150 229 230 57 13 10 57";
$line11[8]="51 11 68 225 223 69 18 16 29 224 225 117 6 4 56";
$line12[8]="55 3 59 219 218 73 7 7 18 219 219 97 1 1 56";
$line13[8]="57 0 1 204 218 177 15 15 136 218 218 27 0 0 55";
$line14[8]="57 0 0 82 218 218 218 218 218 218 122 0 0 0 54";
$line15[8]="54 0 0 0 69 163 218 218 177 82 0 0 0 0 53";
$line16[8]="32 0 0 0 0 0 0 0 0 0 0 0 0 0 52";
$line17[8]="34 0 0 0 0 0 0 0 0 0 0 0 0 0 50";
$line18[8]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[8]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";

$line00[9]="93 101 104 115 88 75 58 38 27 26 24 24 28 41 46";
$line01[9]="102 25 30 32 33 33 33 33 34 34 34 33 28 15 55";
$line02[9]="98 30 33 35 35 35 35 35 35 35 35 35 33 20 55";
$line03[9]="80 30 34 36 36 36 36 36 37 37 38 38 32 20 55";
$line04[9]="80 32 37 38 66 161 201 201 135 55 40 35 27 15 56";
$line05[9]="70 39 42 83 240 253 253 254 254 214 45 25 22 17 56";
$line06[9]="74 44 44 195 246 220 99 97 238 254 139 21 20 18 57";
$line07[9]="64 38 64 242 242 94 22 21 106 253 226 20 20 18 56";
$line08[9]="51 27 81 242 242 76 20 20 76 250 253 35 20 16 56";
$line09[9]="42 21 63 242 242 104 20 20 104 245 249 78 18 13 56";
$line10[9]="51 18 20 214 242 214 89 89 224 238 239 73 13 10 57";
$line11[9]="51 11 15 88 239 238 235 232 231 232 232 64 6 4 56";
$line12[9]="55 3 6 7 49 144 171 142 87 224 226 29 1 1 56";
$line13[9]="57 0 1 1 1 1 1 1 83 219 207 0 0 0 55";
$line14[9]="57 0 0 192 219 136 0 14 177 218 137 0 0 0 54";
$line15[9]="54 0 0 109 218 218 177 191 218 204 27 0 0 0 53";
$line16[9]="32 0 0 0 123 218 218 218 191 41 0 0 0 0 52";
$line17[9]="34 0 0 0 0 14 56 42 0 0 0 0 0 0 50";
$line18[9]="38 0 0 0 0 0 0 0 0 0 0 0 0 0 37";
$line19[9]="39 57 66 65 62 61 60 60 58 55 54 50 44 34 21";


sub digits {
local ($num) = @_;
#  @digits=  unpack("C",$num);
@digits = split('',$num);

#  print "num=$num, digits=[",join(",",@digits),"]\n";

$num = $num + 0;
print OUT "P2\n",15*(1+$#digits), " 20\n255\n";

foreach $digit ( @digits ) { print OUT $line00[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line01[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line02[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line03[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line04[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line05[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line06[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line07[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line08[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line09[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line10[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line11[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line12[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line13[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line14[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line15[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line16[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line17[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line18[$digit],"\n"; }
foreach $digit ( @digits ) { print OUT $line19[$digit],"\n"; }
}

# flock(fd, command);
# $LOCK_SH = 1; $LOCK_EX = 2; $LOCK_NB = 4; $LOCK_UN = 8;



undef $key;
undef $numsize;

@days = ( "Sun", "Mon" , "Tue", "Wed", "Thu", "Fri", "Sat" );
@months = ( "Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul","Aug","Sep",
   "Oct","Nov","Dec");

sub badkey{
  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time+3600);
  $etime = sprintf("%s, %02d %s %d %02d:%02d:%02d GMT", $days[$wday],$mday,$months[$mon], $year+1900, $hour, $min, $sec);
  print "HTTP/1.0 404 Bad handle -- please read http://www-mae.engr.ucf.edu/~ssd/counter.html
Expires: $etime
MIME-Version: 1.0
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Error</TITLE>
</HEAD>
<BODY>
<H1>Error 404</H1>
</BODY>
</HTML>
";

### I'm not interested in any errors anymore, and there aren't many anyway,
### so this section is just totally commented out
#  if ($badkeymail || $ENV{HTTP_REFERER} =~ m,ucf.edu.*counter, || 
#	 ( $ENV{HTTP_REFERER} =~ /file:|ucf.edu/ && $_[2] =~ /^.0+$/)
#    {
#    open(MAIL,"|/usr/bin/mailx -s counter-debug $debuguser");
#    print MAIL "Bad key: ",@_,"\n";
#    foreach (keys %ENV)
#    {
#      print MAIL $_,'=',$ENV{$_},"\n";
#    }
#    close(MAIL);
#  }
  exit(1);
}

## uncomment this section for debugging
## change the 0 to a 1 to actually activate it
#if (0){
#  open(MAIL,"|/usr/bin/mailx -s counter-info $debuguser");
#  foreach (keys %ENV) {
#    print MAIL $_,'=',$ENV{$_},"\n";
#  }
#}

if (defined($ENV{CONTENT_LENGTH})) {  # POST
  read(STDIN,$_,$ENV{CONTENT_LENGTH});
  @options=split(/&/);
#  Blah.  Ignore spaces and escapes anyway.
#  s/\+/ /g;
#  s/%(..)/pack("c",hex($1))/ge;
  $key = shift @options;	# key is first instead of last
#  print MAIL "POST: key=$key options= ",join('|',@options),"\n";
} elsif ($ENV{QUERY_STRING}){	# GET
  $_ = $ENV{QUERY_STRING};
  @options=split(/&/); 
  $key = shift @options;
#  print MAIL "GET: key=$key options= ",join('|',@options),"\n";
} elsif (defined($ENV{$PathVar})){ # old style
  @options = split('/',$ENV{$PathVar});
# SHIFT
  shift @options;
  shift @options;
  $key = pop(@options);
#  print MAIL "OLD: key=$key options= ",join('|',@options),"\n";
#  print MAIL "olddata: '$ENV{$PathVar}'\n";
}

if ($key || @options){
#  print MAIL "options = ",join('|',@options),"\n";
  &badkey("worthless key '$key'") if (!$key || $key =~ /width/i || $key =~ / / || $key=~/^index(.html)?$/i || $key=~/^welcome(.html)$/i);
  if ($key =~ /^x+$/ && defined($ENV{REFERER_URL})){
      $key = $ENV{REFERER_URL};
  } elsif ($key =~ /^\d+$/){
    $value= $key;
    undef $key;
  } elsif ($key =~ /^(rand|random)$/i){
      undef $key;
      srand(time + $$);
      $value = int(rand(100000));
  } else {
#    print MAIL "standard key '$key'\n";
  }
  foreach (@options){
    &badkey("http in url") if (/http:/);
    if (/^width=([0-9]+)$/i) {
      $numsize = $1;
    } elsif (/^noinc$/i) {
      $noinc++;
    } else {
      &badkey("unknown option '$_'");
    }
  }
}

if (defined($key)){
 dbmopen(%keys,$database,666);
 if (defined($ENV{REFERER_URL})){
    $k = $ENV{REFERER_URL};
    $k =~ s/[# ].*$//;  # truncate at internal tag or space
    if ($k =~ /^file:/i){
      &badkey("Counters only work with real pages. ");
    }
    if ($key ne $k ){
      $keys{$k}++;
    }
 }
 if ($noinc) {
   $value = $keys{$key};
 } else {
   $value = $keys{$key}++;
   $keys{$key} = $value if ($value<0);
 }
 dbmclose(%keys);
 exit(0) if ($value<0);
}

#print MAIL "\nkey=$key value=$value\n";
     

$| = 1;
#print "HTTP/1.0 200 Document follows\n";
#print "Content-Type: image/x-portable-graymap\n\n";
print "Content-Type: image/gif\n\n";

open(OUT,"|/opt/pbmplus/bin/ppmtogif 2>/dev/null");

if (defined($numsize) && length($value) < $numsize){
  $value = '0'x($numsize-length($value)) . $value;
}
&digits($value);
close(OUT);
