JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
CHANGED db.php API so it doesn't append WHERE for you
authorJason Woofenden <jason183@herkamire.com>
Thu, 1 Mar 2007 04:10:18 +0000 (23:10 -0500)
committerJason Woofenden <jason183@herkamire.com>
Thu, 1 Mar 2007 04:10:18 +0000 (23:10 -0500)
added messages.php
fixed db_printf() so it actually returns the generated string
run.php now clears the old template object if you return a different basename
run.php now accepts full urls in addition to basenames returned from scripts. UNTESTED
session.php now puts a 'wfpl_' prefix on table names
redirect() now uses an HTTP/1.0 header instead of 1.1

db.php
http.php
messages.php [new file with mode: 0644]
metaform/template.php
run.php
session.php

diff --git a/db.php b/db.php
index 952cc23..065ac0e 100644 (file)
--- a/db.php
+++ b/db.php
@@ -101,12 +101,12 @@ function db_send_query($sql) {
 # %"  output double quotes, surrounding the variable which is encoded to be in there.
 # %s  output encoded to be in double quotes, but don't output the quotes
 #
-# complex example: db_get_rows('mytable', 'id', 'name=%" or company like "%%%s%%"', $name, $company_partial);
+# complex example: db_get_rows('mytable', 'id', 'where name=%" or company like "%%%s%%"', $name, $company_partial);
 
 function db_printf($str) {
        $args = func_get_args();
        $args = array_slice($args, 1);
-       _db_printf($str, $args);
+       return _db_printf($str, $args);
 }
 
 # This function does the work, but takes the parameters in an array
@@ -146,7 +146,7 @@ function _db_printf($str, $args) {
 function db_send_get($table, $columns, $where, $args) {
        $sql = "SELECT $columns FROM $table";
        if($where) {
-               $sql .= ' WHERE ' . _db_printf($where, $args);
+               $sql .= ' ' . _db_printf($where, $args);
        }
 
        return db_send_query($sql);
@@ -265,17 +265,17 @@ function db_insert_ish($command, $table, $columns, $values) {
 # db_update('users', 'name', 'Bruce');
 #
 # # name user #6 Bruce
-# db_update('users', 'name', 'Bruce', 'id= %"', 6);
+# db_update('users', 'name', 'Bruce', 'where id=%i', 6);
 #
 # # update the whole bit for user #6
-# db_update('users', 'name,email,description', 'Bruce', 'bruce@example.com', 'is a cool guy', 'id= %"', 6);
+# db_update('users', 'name,email,description', 'Bruce', 'bruce@example.com', 'is a cool guy', 'where id=%i', 6);
 #
 # # update the whole bit for user #6 (passing data as an array)
 # $data = array('Bruce', 'bruce@example.com', 'is a cool guy');
-# db_update('users', 'name,email,description', $data, 'id= %"', 6);
+# db_update('users', 'name,email,description', $data, 'where id=%i', 6);
 
 # The prototype is really something like this:
-# db_update(table, columns, values..., where(optional), where_args...(optional
+# db_update(table, columns, values..., where(optional), where_args...(optional))
 function db_update($table, $columns, $values) {
        $args = func_get_args();
        $args = array_slice($args, 2);
@@ -305,7 +305,7 @@ function db_update($table, $columns, $values) {
                $where = $args[0];
                $args = array_slice($args, 1);
 
-               $sql .= ' WHERE ';
+               $sql .= ' ';
                # any left for where claus arguments?
                if($args) {
                        $sql .= _db_printf($where, $args);
@@ -322,7 +322,7 @@ function db_update($table, $columns, $values) {
 function db_delete($table, $where = '') {
        $sql = "DELETE FROM $table";
        if($where) {
-               $sql .= ' WHERE ';
+               $sql .= ' ';
                $args = func_get_args();
                $args = array_slice($args, 2);
                if($args) {
index 437ec74..b183275 100644 (file)
--- a/http.php
+++ b/http.php
@@ -48,7 +48,7 @@ function this_url() {
 }
 
 function redirect($url, $status = '302 Moved Temporarily', $message = '') {
-       header("HTTP/1.1 $status");
+       header("HTTP/1.0 $status");
        header("Location: $url");
        echo($message);
        exit();
diff --git a/messages.php b/messages.php
new file mode 100644 (file)
index 0000000..8d8196d
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+
+#  Copyright (C) 2007 Jason Woofenden
+#
+#  This file is part of wfpl.
+#
+#  wfpl is free software; you can redistribute it and/or modify it under the
+#  terms of the GNU Lesser General Public License as published by the Free
+#  Software Foundation; either version 2.1 of the License, or (at your option)
+#  any later version.
+#
+#  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
+#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+#  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
+#  more details.
+#
+#  You should have received a copy of the GNU Lesser General Public License
+#  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
+#  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+
+
+# This file is useful for putting message boxe(s) on the screen.
+#
+# Just call message("message here") whenever you have something to report.
+#
+# Once a template is loaded, call display_messages(). Your template should have
+# a <!--~message_box start~--> section with ~message_text.html~ tag in it.
+#
+# If you want a divider (any text between message boxes when there are multiple
+# boxes) provide a sub-template section named "message_divider" INSIDE
+# "message_box" at the begining of it.
+#
+# If you'd like something around the group of all message boxes, you can put
+# the whole thing in a sub-template section called "message_container"
+
+# Simple example:
+#
+#    <!--~message_box start~-->
+#        <p>~message_text.html~</p>
+#    <!--~end~-->
+
+# Full-featured example:
+#
+#    <!--~message_container start~-->
+#         <div style="border: 2px solid red; background: #800; padding: 15px">
+#         <!--~message_box start~-->
+#             <!--~message_divider start~-->
+#                 <hr />
+#             <!--~end~-->
+#             <p>~message_text.html~</p>
+#         <!--~end~-->
+#         </div>
+#    <!--~end~-->
+
+require_once('code/wfpl/template.php');
+
+function message($msg) {
+       if(!isset($GLOBALS['wfpl_messages'])) {
+               $GLOBALS['wfpl_messages'] = array();
+       }
+
+       $GLOBALS['wfpl_messages'][] = $msg;
+}
+
+# if you want the messages in a template other than the default one, pass it like so:
+#
+# display_messages(ref($my_template));
+function display_messages($template = 0) {
+       $first = true;
+       if($template === 0) {
+               $template = &$GLOBALS['wfpl_template'];
+       } else {
+               $template = &$template->ref;
+       }
+
+       if($GLOBALS['wfpl_messages']) {
+               foreach($GLOBALS['wfpl_messages'] as $msg) {
+                       if($first) {
+                               $first = false;
+                       } else {
+                               $template->sub('message_divider');
+                       }
+                       $template->set('message_text', $msg);
+                       $template->sub('message_box');
+               }
+               $template->sub('message_container');
+       }
+}
+
+?>
index 07eda6a..258581c 100644 (file)
@@ -114,7 +114,7 @@ function ~form_name~() {
                list(~php_fields~) = db_get_row('~form_name~', '~db_fields~', 'id = %"', $edit_id);
                ~tem_sets.tab~
        } else {
-               # form not submitted, you can set default values like so
+               # form not submitted, you can set default values like so:
                #tem_set('~always_field~', 'Yes');
        }<!--~upload_max start~-->
 
diff --git a/run.php b/run.php
index 79a05dc..03d849a 100644 (file)
--- a/run.php
+++ b/run.php
 
 # RewriteEngine  on
 # RewriteRule    ^$  /foo/run.php
-# RewriteRule    ^/foo/[^/]*\.html$  /foo/run.php
+# RewriteRule    ^foo/[^/]*\.html$  /foo/run.php
 
 require_once('code/wfpl/file_run.php');
+require_once('code/wfpl/http.php');
 
 function run_php($basename = false) {
        if($basename) {
@@ -71,9 +72,16 @@ function run_php($basename = false) {
        }
        if($php_file != $html_file && file_exists($php_file)) {
                require_once('code/wfpl/template.php');
-               if(file_exists($html_file)) tem_load($html_file);
+               if(file_exists($html_file)) {
+                       $GLOBALS['wfpl_template'] = new tem();
+                       tem_load($html_file);
+               }
                $other = file_run($php_file);
                if($other) {
+                       if(strpos($other, ':')) {
+                               redirect($other);
+                               exit();
+                       }
                        run_php($other);
                        return;
                }
index eac7ff2..5a4b5e1 100644 (file)
 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 # you'll need this file that calles db_connect()
-require_once('db_connect.php');
+if(!isset($GLOBALS['wfpl_db_handle'])) {
+       require_once('db_connect.php');
+}
 
 # and these database tables:
-# create table sessions (id int unique auto_increment, session_key varchar(16), length int, expires int);
-# create table session_data (id int unique auto_increment, session_id int, name varchar(100), value text);
+# create table wfpl_sessions (id int unique auto_increment, session_key varchar(16), length int, expires int);
+# create table wfpl_session_data (id int unique auto_increment, session_id int, name varchar(100), value text);
 
 # GLOSSARY
 #
@@ -55,7 +57,7 @@ function session_generate_key() {
 function session_new($length = 86400) {
        $session_key = session_generate_key();
 
-       db_insert('sessions', 'session_key,length', $session_key, $length);
+       db_insert('wfpl_sessions', 'session_key,length', $session_key, $length);
        $GLOBALS['session_id'] = db_auto_id();
        $GLOBALS['session_key'] = $session_key;
        $_REQUEST['session_key'] = $session_key; #just in case someone calls session_exists() after session_new()
@@ -66,23 +68,23 @@ function session_new($length = 86400) {
 # assumes there's a session. call session_init() if you'd like one auto-create one if not found.
 function session_touch($length = false) {
        if(!$length) {
-               $length = db_get_value('sessions', 'length', 'id = %i', $GLOBALS['session_id']);
+               $length = db_get_value('wfpl_sessions', 'length', 'where id=%i', $GLOBALS['session_id']);
        }
        $expires = time() + $length;
 
        header('Set-Cookie: session_key=' . $GLOBALS['session_key']);
 
-       db_update('sessions', 'expires', $expires, 'id = %i', $GLOBALS['session_id']);
+       db_update('wfpl_sessions', 'expires', $expires, 'where id=%i', $GLOBALS['session_id']);
 }
 
 # delete expired sessions from database
 function session_purge_old() {
        $now = time();
-       $exired_sessions = db_get_column('sessions', 'id', 'expires < %i', $now);
-       db_delete('sessions', 'expires < %i', $now);
+       $exired_sessions = db_get_column('wfpl_sessions', 'id', 'where expires < %i', $now);
+       db_delete('wfpl_sessions', 'where expires < %i', $now);
        if($expired_sessions) {
                foreach($expired_sessions as $expired_session) {
-                       db_delete('session_data', 'session_id=%i', $expired_session);
+                       db_delete('wfpl_session_data', 'where session_id=%i', $expired_session);
                }
        }
 }
@@ -102,7 +104,7 @@ function session_exists() {
        $GLOBALS['session_key'] = $session_key;
 
        session_purge_old();
-       $id = db_get_value('sessions', 'id', 'session_key = %"', $session_key);
+       $id = db_get_value('wfpl_sessions', 'id', 'where session_key=%"', $session_key);
        if($id === false) {
                return false;
        }
@@ -130,12 +132,12 @@ function init_session() {
 
 # save a variable into the session
 function session_set($name, $value) {
-       db_replace('session_data', 'name,value', $name, $value);
+       db_replace('wfpl_session_data', 'name,value', $name, $value);
 }
 
 # get a variable into the session
 function session_get($name) {
-       return db_get_value('session_data', 'value', 'name=%"', $name);
+       return db_get_value('wfpl_session_data', 'value', 'where name=%"', $name);
 }
 
 ?>