PERL 20
Img_counter.cgi Guest on 24th September 2020 06:07:41 PM
  1. #!/usr/bin/perl
  2.  
  3. # This is a graphical counter that is called from the <img> tag.
  4. # Syantax is:
  5. # <img src="pathToScript/img_counter.pl?CounterDataFile">
  6. # So, for example, if your CounterDataFile was count.txt in the /file dir :
  7. # <img src="cgi-bin/img_counter.pl?file/count.txt
  8. #
  9. # The script should be chmod 755, and your data file chmod 666 or 777
  10.  
  11.  
  12.    $minLen = 1;           # minimum number of digits in bigmap
  13.    $isHigh = 1;           # if 1, digits are 16 pixels high, to
  14.                           # allow room for border
  15.    $isInverse = 1;        # 1 = digits are white on black
  16.                           # 0 = black on white
  17.    $lockWait = 5;         # number of seconds to wait for lock
  18.    $counterFile = $ENV{QUERY_STRING};   # where the logging file is.
  19.                                         # This allows many counters
  20.                                         # to run off of this script.
  21.  
  22.    &initialize;
  23.  
  24.    &incrementCount;
  25.  
  26.    &generateBitmap;
  27.    &writeBitmap;
  28.  
  29.    exit(0);
  30.  
  31.  
  32. sub writeBitmap {
  33.    print ("Content-type: image/x-xbitmap\n\n");
  34.    if ($isHigh) {
  35.       printf ("#define count_width %d\n#define count_height 16\n",
  36.               $len*8);
  37.    }
  38.    else {
  39.       printf ("#define count_width %d\n#define count_height 10\n",
  40.               $len*8);
  41.    }
  42.    printf STDOUT "static char count_bits[] = {\n";
  43.    for($i = 0; $i < ($#bytes + 1); $i++) {
  44.       print("0x$bytes[$i]");
  45.       if ($i != $#bytes) {
  46.          print(",");
  47.          if (($i+1) % 7 == 0) {
  48.             print("\n");
  49.          }
  50.       }
  51.    }
  52.    print("};\n");
  53. }
  54.  
  55. # generateBitmap() - $count contains number to display
  56. #                    $minLen contains minimum number of digits to display
  57. #                    $isHigh is one for 16 bit high numbers (else 10)
  58. #                    $isInverse is one for reverse (white on black);
  59. sub generateBitmap {
  60.    $count = $totalReads;
  61.    @bytes = ();
  62.    $len = length($count) > $minLen ? length($count) : $minLen;
  63.    $formattedCount = sprintf("%0${len}d",$count);
  64.    if ($isHigh) {
  65.       for ($i = 0; $i < $len*3; $i++ ) {
  66.          if ($isInverse) {
  67.             push(@bytes,"ff");       # add three blank rows to each digit
  68.          }
  69.          else {
  70.             push(@bytes,"00");
  71.          }
  72.       }
  73.    }
  74.    for ($y=0; $y < 10; $y++) {
  75.        for ($x=0; $x < $len; $x++) {
  76.            $digit = substr($formattedCount,$x,1);
  77.            if ($isInverse) {             # $inv = 1 for inverted text
  78.                $byte = substr(@invdigits[$digit],$y*3,2);
  79.            }
  80.            else {
  81.                $byte = substr(@digits[$digit],$y*3,2);
  82.            }
  83.            push(@bytes,$byte);
  84.        }
  85.    }
  86.    if ($isHigh) {
  87.       for ($i = 0; $i < $len*3; $i++ ) {
  88.          if ($isInverse) {
  89.             push(@bytes,"ff");       # add three blank rows to each digit
  90.          }
  91.          else {
  92.             push(@bytes,"00");
  93.          }
  94.       }
  95.    }
  96. }
  97.  
  98. sub initialize {
  99.   # $hostname = $ENV{REMOTE_HOST};
  100.   # $host     = $ENV{REMOTE_ADDR};
  101.   # $port     = $ENV{SERVER_PORT};
  102.    # bitmap for each digit
  103.    #  Each digit is 8 pixels wide, 10 high
  104.    #  @invdigits are white on black, @digits black on white
  105.    @invdigits = ("c3 99 99 99 99 99 99 99 99 c3",  # 0
  106.                  "cf c7 cf cf cf cf cf cf cf c7",  # 1
  107.                  "c3 99 9f 9f cf e7 f3 f9 f9 81",  # 2
  108.                  "c3 99 9f 9f c7 9f 9f 9f 99 c3",  # 3
  109.                  "cf cf c7 c7 cb cb cd 81 cf 87",  # 4
  110.                  "81 f9 f9 f9 c1 9f 9f 9f 99 c3",  # 5
  111.                  "c7 f3 f9 f9 c1 99 99 99 99 c3",  # 6
  112.                  "81 99 9f 9f cf cf e7 e7 f3 f3",  # 7
  113.                  "c3 99 99 99 c3 99 99 99 99 c3",  # 8
  114.                  "c3 99 99 99 99 83 9f 9f cf e3"); # 9
  115.    
  116.       @digits = ("3c 66 66 66 66 66 66 66 66 3c",  # 0
  117.                  "30 38 30 30 30 30 30 30 30 30",  # 1
  118.                  "3c 66 60 60 30 18 0c 06 06 7e",  # 2
  119.                  "3c 66 60 60 38 60 60 60 66 3c",  # 3
  120.                  "30 30 38 38 34 34 32 7e 30 78",  # 4
  121.                  "7e 06 06 06 3e 60 60 60 66 3c",  # 5
  122.                  "38 0c 06 06 3e 66 66 66 66 3c",  # 6
  123.                  "7e 66 60 60 30 30 18 18 0c 0c",  # 7
  124.                  "3c 66 66 66 3c 66 66 66 66 3c",  # 8
  125.                  "3c 66 66 66 66 7c 60 60 30 1c"); # 9
  126. }
  127. sub incrementCount {
  128.    &incrementTotalReads;
  129. }
  130.  
  131.  
  132. sub incrementTotalReads {
  133.    if (-e $counterFile) {
  134.      open(COUNT,"$counterFile") || die("Can't open $counterFile: $!\n");
  135.    }
  136.    $totalReads = <COUNT>;
  137.    #chop $totalReads;
  138.    close(COUNT);
  139.  
  140.      $totalReads++;
  141.      open(COUNT,">$counterFile") || die "$0: can\'t open $counterFile: $!\n";
  142.  
  143.      print (COUNT "$totalReads\n");
  144.  
  145.  
  146. open(LOG,">>log.txt");
  147. $logtime=gmtime(time);
  148. print (LOG "$totalReads***\n");
  149. print (LOG "$logtime***\n\n");
  150. close (LOG);
  151.  
  152.  
  153.  
  154.      close(COUNT);
  155.  
  156.  
  157. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.