# Daily Blog # Main Page global prefix hardprefix # The URL of the Blog: set prefix "/school" set hardprefix "/aaron/school" source "/web/swartzfam/aaron/school/definitions.tcl" # Procedure to display a page using the template proc sa_display_page {title content {extra ""} {status 200}} { global hardprefix prefix ns_return $status text/html [ns_adp_parse -file "[ns_info pageroot]$hardprefix/main-template.adp" $title $extra $content] } proc sa_check_password {} { global password if {[regexp {password=([^;]+)} [ns_set get [ns_conn headers] "Cookie"] {} pass] && \ $pass == $password} { return 1 } { return 0 } } proc sa_require_admin {} { if {![sa_check_password]} { sa_returnredirect "/login/" return -code return } } proc sa_edit_button {post_date} { global hardprefix prefix if {[sa_check_password]} { return "
" } {return ""} } proc sa_email_button {post_date} { global hardprefix prefix if {[sa_check_password]} { return "" } {return ""} } proc sa_nav {post_date} { append out "\[" global db hardprefix prefix set prev_date [database_to_tcl_string_or_null $db "SELECT post_date FROM sa_entries WHERE post_date < '$post_date' ORDER BY post_date DESC LIMIT 1"] if {$prev_date != ""} { regsub -all -- {-} $prev_date {/} prev_date append out "" set later "" } {set later ""} regsub -all -- {-} $post_date {/} post_date append out "<$later * " set next_date [database_to_tcl_string_or_null $db "SELECT post_date FROM sa_entries WHERE post_date > '$post_date' ORDER BY post_date ASC LIMIT 1"] if {$next_date != ""} { regsub -all -- {-} $next_date {/} next_date append out "" set later "" } {set later ""} append out ">$later\]" return $out } proc sa_returnredirect {url} { global hardprefix servername prefix ns_returnredirect "$servername$prefix$url" } proc sa_display_entry {entry {full_url 0}} { global hardprefix prefix servername if {$full_url} {set starter $servername} append starter $prefix regsub -all -nocase {This entry hasn't been officially posted yet -- we're still working on it, so don't take anything too seriously. Thanks!"} } } proc sa_get_special {} { set content "" set rd [open /web/swartzfam/aaron/school/unschool.htmf RDONLY] while {![eof $rd]} { append content [read $rd] } return $content } set url [string range [ns_conn url] [string length $hardprefix] end] global db set db [ns_db gethandle] catch {ns_db dml $db "INSERT INTO sa_log(url, referer, visit_time, ip) VALUES('[DoubleApos $url]','[DoubleApos [ns_set iget [ns_conn headers] "Referer"]]','now','[DoubleApos [ns_set iget [ns_conn headers] "X-Peer-Addr"]]')"} # Smash case after we've logged it: set out_type "" set test_url [string tolower $url] regexp {(/?(.*/)?)index\.([^/]*)$} $test_url {} test_url {} out_type switch $test_url { "/login/" { if {[sa_check_password]} {set status "You are currently logged in.
"} {set status ""} sa_display_page "Login" \ "$statusTo gain administrator access, please enter the secret password below:
" return } "/login2/" { set pass [ns_queryget password] if {$pass != $password} { sa_display_page "Bad Password" "Sorry, that's the wrong password.
" return } if {[ns_queryexists save]} {set expire_val ";expires=Fri, 01-Jan-2010 01:00:00 GMT"} else {set expire_val ""} ns_set put [ns_conn outputheaders] "Set-Cookie" "password=$password$expire_val;path=$prefix" sa_returnredirect "/" return } "/edit/" { sa_require_admin set post_date [ns_queryget post_date] if {$post_date == ""} { sa_display_page "Value Required" "You need to supply something to edit!
" return } if {[regexp {^[-0-9]+$} $post_date]} { set table "sa_entries" set column "post_date" set title_sql "" } { set table "sa_stories" set column "url" set title_sql ", title" } set selection [ns_db 0or1row $db "SELECT body, status$title_sql FROM $table WHERE $column = '$post_date'"] if {$selection != ""} { set_variables_after_query set post_url $post_date if {$table == "sa_entries"} {regsub -all -- {-} $post_url {/} post_url} set view "Take a look at your work.
" if {$status == "done"} {set status " checked"} {set status ""} } { set body "" set view "" set status "" } if {$table == "sa_stories"} { if {![info exists title]} {set title ""} set title_html "Title:Would you like to receive updates to this site by email? We promise not to give out your email address to anyone else. (Don't worry, we hate spam too.) Enter your email address below to subscribe:
} return } "/subscribe2/" { set email [ns_queryget email] if {![philg_email_valid_p $email]} { sa_display_page "Invalid Email" {Sorry, that email is invalid. Examples of valid email addresses are:
Please go back and try again.
} return } # Make sure they're not already subscribed: if {[database_to_tcl_string $db "SELECT count(email) FROM sa_subscribers WHERE email = '[DoubleApos $email]' AND remove_date IS NULL"]} { sa_display_page "Already Subscribed" "That email address is already subscribed! You can also unsubscribe.
" return } set code [as_random_string 5] # Stick them in the database: set try 0 while {[catch {ns_db dml $db "INSERT INTO sa_subscribers(email,add_date,code,sent_p) VALUES ('[DoubleApos $email]','now','$code','f')"} errMsg] && $try < 5} { incr try # Could be a duplicate secret code -- regenerate set code [as_random_string 5] } sa_display_page "Subscribed" "You've been subscribed to receive updates by email. You've been sent an email with more information and instructions on how to unsubscribe.
" ns_conn close ns_sendmail $email $webmaster "Subscribed to $sitename" \ "You've been subscribed to receive updates on $sitename: $servername$prefix/ If you didn't mean to subscribe, or would like to unsubscribe, you can unsubscribe at any time by visiting: $servername$prefix/unsubscribe2/?code=$code Enjoy! $webmaster_name" return } "/unsubscribe/" { sa_display_page "Unsubscribe" {Annoyed about the emails? Enter your email address below to unsubscribe:
} return } "/unsubscribe2/" { if {[ns_queryexists code]} { set code [ns_queryget code] # Wants to unsubscribe ns_db dml $db "UPDATE sa_subscribers SET remove_date = 'now' WHERE remove_date IS NULL AND code = '[DoubleApos $code]'" sa_display_page "Unsubscribed" "You have been successfully unsubscribed. Sorry you didn't like it!
" return } elseif {[ns_queryexists email]} { set email [ns_queryget email] set code [database_to_tcl_string_or_null $db "SELECT code FROM sa_subscribers WHERE email = '[DoubleApos $email]' AND remove_date IS NULL"] if {$code == ""} { sa_display_page "Not Subscribed" "You couldn't be unsubscribed because you weren't subscribed. Oh well, nothing to worry about then.
" return } ns_sendmail $email $webmaster "Unsubscribing" "To unsubscribe visit this URL: $servername$prefix/unsubscribe2/?code=$code Thanks!" sa_display_page "Check Your Email" "We've sent you an email with instructions on how to unsubscribe. Just go ahead and follow them.
" return } else { sa_returnredirect "/unsubscribe/" } } "/sendcp/" { sa_require_admin set selection [ns_db select $db "SELECT DISTINCT responder,target FROM sa_cp"] while {[ns_db getrow $db $selection]} { set_variables_after_query append result "[ta_httppost $responder "url=[ns_urlencode $target]"]\n" } ns_return 200 text/plain "$responder: did it: $result" return } "/sendemail/" { sa_require_admin set total [database_to_tcl_string $db "SELECT count(*) FROM sa_subscribers WHERE remove_date IS NULL"] set unsent [database_to_tcl_string $db "SELECT count(*) FROM sa_subscribers WHERE remove_date IS NULL AND sent_p = 'f'"] set post_date [ns_queryget post_date] regsub -all -- {-} $post_date / addy set body [database_to_tcl_string $db "SELECT body FROM sa_entries WHERE post_date = '$post_date'"] if {$post_date == "2001-04-05"} { set body [sa_get_special] } set email_text "Here's today's update from $sitename: [ad_html_to_text "Current email state: $unsent unsent of $total
" return } "/sendemail2/" { set body [ns_queryget body] set subject [ns_queryget subject] if {[set reset [ns_queryexists reset]]} { set reset [ns_queryget reset] } sa_require_admin sa_returnredirect "/sendemail3/" ns_conn close if {$reset} { # Reset the sent_p flag: ns_db dml $db "UPDATE sa_subscribers SET sent_p = 'f'" } set email_list [database_to_tcl_list_list $db "SELECT email, code FROM sa_subscribers WHERE remove_date IS NULL"] foreach person_list $email_list { catch { ns_sendmail [lindex $person_list 0] $webmaster "$subject ($sitename)" $body ns_db dml $db "UPDATE sa_subscribers SET sent_p = 't' WHERE email = '[DoubleApos [lindex $person_list 0]]' AND remove_date IS NULL" } } return } "/sendemail3/" { sa_require_admin set total [database_to_tcl_string $db "SELECT count(*) FROM sa_subscribers WHERE remove_date IS NULL"] set sent [database_to_tcl_string $db "SELECT count(*) FROM sa_subscribers WHERE remove_date IS NULL AND sent_p = 't'"] set selection [ns_db select $db "SELECT email, sent_p FROM sa_subscribers WHERE remove_date IS NULL"] set email_list "" while {[ns_db getrow $db $selection]} { set_variables_after_query append email_list "Don't Forget: Send out the notifications too!
Status: $sent sent of $total
$email_list" return } "/viewall/" { set selection [ns_db select $db "SELECT post_date FROM sa_entries ORDER BY post_date"] append page_content "The link report:
\n$referer ($n)$servername$prefix/$id/" set later "" } {set later ""} foreach href_url $href_list { append page_content "\n$href_url" } append page_content "$later " ns_return 200 text/html $page_content } html - default {sa_display_page $title "[sa_display_message $status][sa_display_entry $body][sa_edit_button $id]"} } return } elseif {![regexp {^/[0-9]} $url]} { sa_display_page "File Not Found" "
Sorry, the page you requested does not exist. You may want to email the webmaster to complain.
[sa_edit_button $id]" "" 404 return } } } set year "" set month "" set day "" set match "" set type "" regexp {/([0-9]+)(/([0-9]+))?(/([0-9]+))?} $test_url match year {} month {} day if {"$match/" != $test_url} { sa_returnredirect "$match/" } if {$day == ""} { if {$month == ""} { if {$year == ""} { # Show Today's Blog: set selection [ns_db 0or1row $db "SELECT body, status, post_date FROM sa_entries ORDER BY post_date DESC LIMIT 1"] if {$selection == ""} { sa_display_page "Today's Blog" "Sorry, there are no entries quite yet. You'll have to wait until something is written!
" } { set_variables_after_query if {$post_date == "2001-04-05"} { set body [sa_get_special] } switch $out_type { "xhtml" { ns_return 200 text/html \ "Sorry, there was no entry on that date. Try that month, that year, or the latest entry.
[sa_edit_button "$year-$month-$day"]" } else { set_variables_after_query if {$post_date == "2001-04-05"} { set body [sa_get_special] } switch $out_type { xhtml {ns_return 200 text/html \ "$servername$prefix$test_url" set later "" } {set later ""} foreach href_url $href_list { append page_content "\n$href_url" } append page_content "$later " ns_return 200 text/html $page_content } html - default {sa_display_page "Day Blog" "[sa_display_message $status][ns_adp_parse -file "[ns_info pageroot]$hardprefix/day-template.adp" $post_date [sa_display_entry $body]][sa_edit_button $post_date]"} } } }