REBOL [
    Title: "Phone Log"
    Date: 2-Jul-2001
    Version: 1.3.0
    File: %phone-log.r
    Author: "Sean C. Johnson"
    Usage: {
    ^-This script is a cgi program. Place this script in the cgi-bin
    ^-directory of your webserver, change the path where the logs are saved,
    ^-call script from a browser (i.e. http://localhost/cgi-bin/phone-log.r).
    ^-Enjoy!
    }
    Purpose: {A cgi shared phone log in REBOL. Inspired by Andrew Grossman's guestbook.
}
    History: {
    ^-1.3.0 Added debug func, revised logic to update log, more comments
    ^-1.1.0 Finally works with Xitami & Apache servers!
    }
    Email: sean@scj.net
    Category: [cgi markup web 3]
    Requires: %webpage.r
]

;--- Change the following path to be where you want to save the logs
;--- on your server. Remember it is relative to where the script is called!
;--- So, if your script is in Apache's cgi-bin this could be "%../htdocs/phone-logs/"

save-log-path: %"../webpages/reb/phone-logs/"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;--- You should not need to change anything below
;--- unless you like messing things up to learn :)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;--- Setup some basic junk
message-form: log-today: data: message: none

how-to-use: [
    print rejoin [system/script/header/title ": "]
    print system/script/header/purpose
    print system/script/header/usage
    print {This script quits in 15 seconds.}
    wait 15
    quit
]

if system/options/cgi/request-method = none [do how-to-use]

do system/script/header/requires

;--- Crude cgi debug functions
verbose: off  ;--- turns on/off debug messages

cgi-debug: func [/local m][
    if not verbose [return ""]
        m: system/options/cgi/request-method
        either m = "POST" [
            data: retrieve-user-data
            rejoin [m ":<br>" mold data]
           ][
            rejoin [m ":<br>" system/options/cgi/query-string]
        ]
]

;--- Create phone log directories if they don't exist
;--- Logs are saved in dir "phone-logs", then by month (i.e. 3 means March),
;--- then by date (HTML).
log-path: rejoin [save-log-path now/month "/"]
log-today: to-file rejoin [log-path now/date ".html"]
    if not exists? to-file log-path [make-dir/deep to-file log-path]

;--- HTML form for new messages
;--- May want to customize some field options
;--- Changes to form method require changes elsewhere in this script!

message-form: {
 <form method="post" action="phone-log.r">
    <table border="1" width="530" bordercolor="#FF0000" bgcolor="#33CCCC">
      <tr>
        <td width="520" colspan="2" valign="middle"><input name="name" size=25 value="Name"><input name="phone1" size=24 value="727-"><select size="1" name="phone1_type">
            <option selected>Home</option>
            <option>Work</option>
            <option>Cell</option>
            <option>Pager</option>
            <option>Fax</option>
          </select><select size="1" name="for">
            <option selected>Tim</option>
            <option>Sean</option>
            <option>Heather</option>
          </select><font color="#FF0000">&nbsp;</font><br>
          <input type="text" name="company" size="25" value="Company"><input name="phone2" size=24 value="727-"><select size="1" name="phone2_type">
            <option>Home</option>
            <option selected>Work</option>
            <option>Cell</option>
            <option>Pager</option>
            <option>Fax</option>
          </select><font color="#FF0000"><input type="checkbox" name="urgent" value="1"><b><font size="4">Urgent</font></b></font></td>
      </tr>
      <tr>
        <td width="520" colspan="2">
    <textarea name="message" rows=3 cols=60>Message:&nbsp;</textarea></td>
      </tr>
      <tr>
        <td width="520" valign="center" colspan="2">
    <input type="submit" name="save" value="Save New Message To Log">
 <input type="radio" value="1" checked name="inout">Inbound Call
  &nbsp;&nbsp;&nbsp;
 <input type="radio" name="inout" value="2">Outbound Call<br>
        </td>
      </tr></form>
      <tr>
        <td width="242" valign="center">
          <p align="center" valign="center"><a href="http://reb.eaglecrest.net/phone-logs/">View Old Phone Logs</a></p>
        </td>
      </tr>
    </table>
<hr>
}

;--- Print out the form first
;--- To understand the structure of this next statement see %webpage.r
print [
      content-type
      begin-body
      header 6 rejoin ["<font color='#C0C0C0'>version: " system/script/header/version " (" read dns:// ")</font>"]
      cgi-debug
      begin-center
      <a href="#top"> header 1 rejoin [system/script/header/title ": " now/date] </a>
      message-form
      end-center
]

;--- Message formatting
message: [{^/}
        either not error? try [data/urgent][if data/urgent = "1" [urgent-banner]][""]
        {<table border="0" width="100%">
          <tr>
            <td width="50%" valign="top" bgcolor="} color {">}
            {<h4>} data/name {<br>} data/company {</h4><blockquote>}
            data/phone1_type {: } data/phone1{<br>}
            data/phone2_type {: } data/phone2 {</blockquote></td>
            <td width="50%" valign="top"><h5>} data/for {</h5><p>} data/message {</p></td>
          </tr>
          <tr>
            <td width="50%"><h6>}  now/date { | } now/time {</h6></td>}
            {<td align="right">} either data/inout = "1" ["Inbound"]["Outbound"]
            {&nbsp;&nbsp;&nbsp;<a href="#top"><img border="0" src="http://reb.eaglecrest.net/images/up.gif" alt="Top of Page" width="20" height="22"></a></td>}
          {</tr>
        </table>}
         ;either not error? try [data/urgent][if data/urgent = "1" [urgent-banner]][""]
        {<hr>}
]

urgent-banner: {
   <table border="2" width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100%" bgcolor="#FF0000" bordercolor="#000000" align="center" valign="center"><b>URGENT&nbsp;&nbsp;&nbsp;&nbsp;
      URGENT&nbsp;&nbsp;&nbsp;&nbsp; URGENT&nbsp;&nbsp;&nbsp;&nbsp;
      URGENT&nbsp;&nbsp;&nbsp;&nbsp; URGENT</b></td>
  </tr>
 </table>
}

;--- Get form data and update log if new message is posted.
;--- When script is first called, method is "get"; therefore,
;--- we skip data retrieval and log update just showing log-today.

if system/options/cgi/request-method = "POST"[

        ;--- if cgi-debug is on it does the data retrieval,
        ;--- this keeps from getting empty string and error! :)
        if not verbose [data: retrieve-user-data]

        ;--- Set colors for distinction between inbound/outbound calls
        color: either data/inout = "1"
               ["#FFFFGG"]["#CCCCFF"]    ;--- You can change these colors

        ;--- Write new message to log
        write/append log-today reform message
]

;--- Print updated phone log to client
print [
        either exists? log-today
            [read log-today]
            [{<center><b>The Phone Log is empty</b></center><hr>}]
        end-body
]