From 6b038bb1556e1b9790a49834817d0bdef530bfde Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Thu, 12 Feb 2009 22:13:20 -0500 Subject: [PATCH 01/16] metaform: default listing page now contains all non-huge failds --- metaform.php | 37 +++++++++++++++++++++++++++++++++---- metaform/template.html | 8 ++++++-- metaform/template.php | 6 +++--- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/metaform.php b/metaform.php index f89e720..06bb7dd 100644 --- a/metaform.php +++ b/metaform.php @@ -222,6 +222,7 @@ function make_html($whole_file = true) { if($input != 'hidden') { $tem->show('row'); } + if($input == 'image' && !$uploads_output_already) { $tem->show('uploads'); $tem->set('enctype_attr', '" enctype="multipart/form-data'); @@ -231,7 +232,8 @@ function make_html($whole_file = true) { $tem->set('html_field_name', $name); $tem->show('replace_textarea'); } - if($GLOBALS['opt_display']) { + + if($GLOBALS['opt_display'] == 'Yes') { switch($input) { case 'checkbox': case 'leftcheck': @@ -251,6 +253,16 @@ function make_html($whole_file = true) { } $tem->show('display_row'); } + + if($GLOBALS['opt_listing'] == 'Yes') { + if($GLOBALS['opt_display'] != 'Yes') { + $tem->show('opt_display_a_else'); + } + if(show_in_listing($type, $input, $format, $sql)) { + $tem->show('listing_head_col'); + $tem->show('listing_row_col'); + } + } } if($GLOBALS['opt_db'] == 'Yes') { @@ -261,9 +273,6 @@ function make_html($whole_file = true) { } if($GLOBALS['opt_listing'] == 'Yes') { - if($GLOBALS['opt_display'] != 'Yes') { - $tem->show('opt_display_a_else'); - } $tem->show('opt_listing_1'); } @@ -300,6 +309,19 @@ function view_html() { echo make_html(); } +function show_in_listing($type, $input, $format, $sql) { + switch($input) { + case 'submit': + case 'hidden': + case 'password': + case 'textarea': + case 'html': + return false; + default: + return true; + } +} + function make_php() { $tem = new tem(); @@ -341,6 +363,13 @@ function make_php() { } $tem->show('tem_sets'); } + + if($GLOBALS['opt_listing'] == 'Yes') { + if(show_in_listing($type, $input, $format, $sql)) { + $tem->show('listing_fields_1'); + $tem->show('listing_fields_2'); + } + } } $tem->set('always_field', $always_field); diff --git a/metaform/template.html b/metaform/template.html index de12f03..dbb1d8d 100644 --- a/metaform/template.html +++ b/metaform/template.html @@ -84,8 +84,12 @@

[Add a new ~singular~]

- - +
~~~always_field~.html~~[delete this ~singular~]
+ + + + +
~caption~
~~~name~.html~~[delete this ~singular~]
diff --git a/metaform/template.php b/metaform/template.php index be61e2f..b9c1097 100644 --- a/metaform/template.php +++ b/metaform/template.php @@ -62,7 +62,7 @@ function ~form_name~_tem_sets(~php_fields~) { # You may pass a "where clause" for the db query. function ~form_name~_display_listing($where = 'order by ~always_field~ limit 100') { - $rows = db_get_rows('~form_name~', 'id,~always_field~', $where); + $rows = db_get_rows('~form_name~', 'id,~name~', $where); if($rows == false || count($rows) == 0) { tem_show('empty_listing'); tem_show('listings'); @@ -70,12 +70,12 @@ function ~form_name~_display_listing($where = 'order by ~always_field~ limit 100 } foreach($rows as $row) { - list($id, $~always_field~) = $row; + list($id, $~name~) = $row; tem_set('id', $id); if($~always_field~ == '') { $~always_field~ = '--'; } - tem_set('~always_field~', $~always_field~); + ~form_name~_tem_sets(~php_fields~); tem_show('listing_row'); } tem_show('populated_listing'); -- 1.7.10.4 From 48bc8c7ca2448555a225b5996effcc9d41bdcfed Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Thu, 12 Feb 2009 23:00:33 -0500 Subject: [PATCH 02/16] metaform: added pretty-printing for checkboxes and dates to listing --- metaform.php | 5 +++++ metaform/template.html | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/metaform.php b/metaform.php index 06bb7dd..ecde564 100644 --- a/metaform.php +++ b/metaform.php @@ -259,6 +259,11 @@ function make_html($whole_file = true) { $tem->show('opt_display_a_else'); } if(show_in_listing($type, $input, $format, $sql)) { + if($format == 'bool' || $format == 'yesno') { + $tem->set('listing_enc', 'yesno'); + } elseif($input == 'date') { + $tem->set('listing_enc', 'mmddyyyy'); + } $tem->show('listing_head_col'); $tem->show('listing_row_col'); } diff --git a/metaform/template.html b/metaform/template.html index dbb1d8d..8f0890c 100644 --- a/metaform/template.html +++ b/metaform/template.html @@ -87,7 +87,7 @@ - + -- 1.7.10.4 From 2deeeff570acf50071180f79040b9f685aea7603 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Fri, 13 Feb 2009 15:07:53 -0500 Subject: [PATCH 03/16] metaform: fixed typo (display of date format hint was wrong) --- metaform/template.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metaform/template.html b/metaform/template.html index 8f0890c..2206620 100644 --- a/metaform/template.html +++ b/metaform/template.html @@ -74,7 +74,7 @@
~caption~
~~~name~.html~~~~~name~.~listing_enc~~~ [delete this ~singular~]
- +
~caption.html~: ~caption.html~: ~caption.html~: dd/mm/yyyy~caption.html~: ~caption.html~: ~caption.html~:
~caption.html~: ~caption.html~: ~caption.html~: ~caption.html~? ~caption.html~?
~caption.html~: ~caption.html~: ~caption.html~: mm/dd/yyyy~caption.html~: ~caption.html~: ~caption.html~:
~caption.html~: ~caption.html~: ~caption.html~: ~caption.html~? ~caption.html~?
-- 1.7.10.4 From 88edfcf26c7b4cdbd083dede0834de5cf6f16999 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Wed, 11 Mar 2009 21:25:21 -0400 Subject: [PATCH 04/16] added http_cache_forever() --- http.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/http.php b/http.php index b82e3d1..1abd50b 100644 --- a/http.php +++ b/http.php @@ -92,4 +92,11 @@ function redirect($url, $status = '302 Moved Temporarily', $message = '') { exit(); } +# output http headers to allow caching of this page forever +function http_cache_forever($cache_control = 'public') { + header('Last-Modified: '.gmdate('D, d M Y H:i:s', 5025) . ' GMT'); # long time ago + header("Cache-Control: $cache_control"); + header('Expires: ' . gmdate('D, d M Y H:i:s',time()+31536000) . ' GMT'); # rfc 2616 says not to go more than a year in the future +} + ?> -- 1.7.10.4 From bf91aed8316e74c8d80c1c4b5e4645eeb6ba9dcd Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Thu, 19 Mar 2009 04:22:08 -0400 Subject: [PATCH 05/16] CONFLICT WARNING: added: logged_in() and logged_in_as_admin() --- session.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/session.php b/session.php index b36af68..9dc3f4e 100644 --- a/session.php +++ b/session.php @@ -125,8 +125,13 @@ function session_exists() { return true; } -# return username if a session exists and is authenticated +# depricated function session_exists_and_authed() { + return logged_in(); +} + +# return username if a session exists and is authenticated +function logged_in() { if(!session_exists()) { return false; } @@ -135,6 +140,20 @@ function session_exists_and_authed() { } + +# return username if a session exists and is authenticated +function logged_in_as_admin() { + if(!session_exists()) { + return false; + } + + if(session_get('auth_admin')) { + return true; + } + return false; +} + + # find existing session, or make one function init_session() { if(!session_exists()) { -- 1.7.10.4 From 7596b1ca035ff09e584e915a6e5572ffb46d7625 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Fri, 10 Apr 2009 01:12:54 -0400 Subject: [PATCH 06/16] API CHANGE for cms integration. Instead of calling cms_get() which should return a hash, we create the template from template.html and pass that to cms_display() --- run.php | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/run.php b/run.php index b5b91a1..09b51b5 100644 --- a/run.php +++ b/run.php @@ -84,12 +84,24 @@ function run_php($dest = false) { $html_exists = file_exists($html_file); $php_exists = file_exists($php_file); + if(file_exists('template.html')) { + $GLOBALS['wfpl_main_template'] = new tem(); + $GLOBALS['wfpl_main_template']->load("template.html"); + $GLOBALS['wfpl_main_template']->set('basename', $basename); + + # This helps put in a stylesheet link if you have pages with custom css + if(file_exists("$basename.css")) { + $GLOBALS['wfpl_main_template']->set('css_link', "$basename.css"); + $GLOBALS['wfpl_main_template']->sub('css_links'); + } + } + # cms_get can return one of: # 1) false to indicate that there's no cms content for this basename # 2) a string to indicate a soft/full redirect just as foo_main() # 3) a hash of key/value pairs to be tem_set(key,value) on the template - if(function_exists('cms_get')) { - $cms_content = cms_get($basename); + if(function_exists('cms_display')) { + $cms_content = cms_display($basename, $GLOBALS['wfpl_main_template']); if(is_string($cms_content)) { run_php($cms_content); return; @@ -134,24 +146,13 @@ function run_php($dest = false) { # Check for $GLOBALS['wfpl_template'] because it might have been set (or unset) by the php script. if($GLOBALS['wfpl_template']) { - if(file_exists('template.html')) { - $tem = new tem(); - $tem->load("template.html"); - $tem->set('basename', $basename); - if($cms_content) foreach($cms_content as $name => $val) { - $tem->append($name, $val); - } + if(isset($GLOBALS['wfpl_main_template'])) { $sections = tem_top_subs(); if($sections) foreach($sections as $name => $val) { - $tem->append($name, $val); - } - - if(file_exists("$basename.css")) { - $tem->set('css_link', "$basename.css"); - $tem->sub('css_links'); + $GLOBALS['wfpl_main_template']->append($name, $val); } - $GLOBALS['wfpl_template'] = $tem; + $GLOBALS['wfpl_template'] = $GLOBALS['wfpl_main_template']; } if(function_exists('display_messages')) { -- 1.7.10.4 From c63498c465bec640bbd8a521216aa37ba178b821 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Mon, 4 May 2009 03:13:06 -0400 Subject: [PATCH 07/16] allowed file name and table name and pretty-print plural to differ --- metaform.php | 41 ++++++++++++++------ metaform/main.html | 9 ++++- metaform/preview.html | 4 +- metaform/template.email.txt | 2 +- metaform/template.html | 20 +++++----- metaform/template.php | 90 +++++++++++++++++++++---------------------- metaform/template.sql | 4 +- 7 files changed, 96 insertions(+), 74 deletions(-) diff --git a/metaform.php b/metaform.php index ecde564..a556f1a 100644 --- a/metaform.php +++ b/metaform.php @@ -66,8 +66,18 @@ function list_available_types() { function metaform() { - if(isset($_REQUEST['form_name'])) { - $GLOBALS['form_name'] = format_varname($_REQUEST['form_name']); + if(isset($_REQUEST['singular'])) { + $GLOBALS['file_name'] = format_varname($_REQUEST['file_name']); + $GLOBALS['table_name'] = format_varname($_REQUEST['table_name']); + $GLOBALS['plural'] = format_varname($_REQUEST['plural']); + # backwards compatibility: + if(isset($_REQUEST['form_name'])) { + $GLOBALS['file_name'] = $GLOBALS['table_name'] = $GLOBALS['plural'] = format_varname($_REQUEST['form_name']); + } + tem_set('file_name', $GLOBALS['file_name']); + tem_set('table_name', $GLOBALS['table_name']); + tem_set('plural', $GLOBALS['plural']); + $GLOBALS['singular'] = format_varname($_REQUEST['singular']); tem_set('singular', $GLOBALS['singular']); $GLOBALS['opt_email'] = format_yesno($_REQUEST['opt_email']); @@ -80,8 +90,6 @@ function metaform() { tem_set('opt_display', $GLOBALS['opt_display']); $GLOBALS['opt_http_pass'] = format_yesno($_REQUEST['opt_http_pass']); tem_set('opt_http_pass', $GLOBALS['opt_http_pass']); - } else { - $GLOBALS['form_name'] = 'some_form'; } if(isset($_REQUEST['fields'])) { @@ -105,7 +113,6 @@ function metaform() { exit(); } elseif(isset($_REQUEST['edit'])) { tem_set('fields', $_REQUEST['fields']); - tem_set('form_name', $GLOBALS['form_name']); # fall through } else { die("Sorry... couldn't tell which button you pressed"); @@ -164,7 +171,7 @@ function view_headers() { function make_sql() { $tem = new tem(); $tem->load('code/wfpl/metaform/template.sql'); - $tem->set('form_name', $GLOBALS['form_name']); + $tem->set('table_name', $GLOBALS['table_name']); $fields = get_fields(); foreach($fields as $field) { list($name, $type, $input, $format, $sql) = $field; @@ -210,8 +217,10 @@ function make_html($whole_file = true) { $has_html_editors = false; $tem = new tem(); $tem->load('code/wfpl/metaform/template.html'); - $tem->set('form_name', $GLOBALS['form_name']); + $tem->set('file_name', $GLOBALS['file_name']); + $tem->set('table_name', $GLOBALS['table_name']); $tem->set('singular', $GLOBALS['singular']); + $tem->set('plural', $GLOBALS['plural']); $fields = get_fields(); $tem->set('always_field', find_always_field($fields)); foreach($fields as $field) { @@ -331,8 +340,10 @@ function show_in_listing($type, $input, $format, $sql) { function make_php() { $tem = new tem(); $tem->load('code/wfpl/metaform/template.php'); - $tem->set('form_name', $GLOBALS['form_name']); + $tem->set('file_name', $GLOBALS['file_name']); + $tem->set('table_name', $GLOBALS['table_name']); $tem->set('singular', $GLOBALS['singular']); + $tem->set('plural', $GLOBALS['plural']); $fields = get_fields(); $db_fields = ''; $php_fields = ''; @@ -429,7 +440,10 @@ function view_php() { function make_email() { $tem = new tem(); $tem->load('code/wfpl/metaform/template.email.txt'); - $tem->set('form_name', $GLOBALS['form_name']); + $tem->set('file_name', $GLOBALS['file_name']); + $tem->set('table_name', $GLOBALS['table_name']); + $tem->set('singular', $GLOBALS['singular']); + $tem->set('plural', $GLOBALS['plural']); $fields = get_fields(); foreach($fields as $field) { list($name, $type, $input, $format, $sql) = $field; @@ -449,7 +463,7 @@ function make_email() { function make_htaccess() { $tem = new tem(); - $tem->set('form', $GLOBALS['form_name']); + $tem->set('form', $GLOBALS['file_name']); return $tem->run('code/wfpl/metaform/htaccess'); } @@ -461,7 +475,10 @@ function view_email() { function preview() { tem_load('code/wfpl/metaform/preview.html'); - tem_set('form_name', $GLOBALS['form_name']); + tem_set('file_name', $GLOBALS['file_name']); + tem_set('table_name', $GLOBALS['table_name']); + tem_set('singular', $GLOBALS['singular']); + tem_set('plural', $GLOBALS['plural']); tem_set('fields', $_REQUEST['fields']); $preview_tem = new tem(); $preview_tem->load_str(make_html(false)); @@ -485,7 +502,7 @@ function preview() { } function download_tar() { - $name = $GLOBALS['form_name']; + $name = $GLOBALS['file_name']; $data = array( ".htaccess" => make_htaccess(), "run.php ->" => 'code/wfpl/run.php', diff --git a/metaform/main.html b/metaform/main.html index ed6b0c4..650269d 100644 --- a/metaform/main.html +++ b/metaform/main.html @@ -11,9 +11,14 @@

The form name should not contain any capitals or symbols (besides underscore)

-

Page/table name:

+

File name: .html

+ +

DB table:

+ +

Printable singular:

+ +

Printable plural:

-

Same as above but singular: (Used in messages and headers.)

Features

diff --git a/metaform/preview.html b/metaform/preview.html index 8bd5656..98c0109 100644 --- a/metaform/preview.html +++ b/metaform/preview.html @@ -11,10 +11,10 @@

PREVIEW

~preview~ -

~hiddens~

+

~hiddens~

-

+

diff --git a/metaform/template.email.txt b/metaform/template.email.txt index c61be5f..bc13503 100644 --- a/metaform/template.email.txt +++ b/metaform/template.email.txt @@ -1,4 +1,4 @@ -~form_name~ form submitted with the following: +The following new ~singular~ was just received: ~caption~: ~~~name~~~ ~caption~: ~~~name~.yesno~~ diff --git a/metaform/template.html b/metaform/template.html index 2206620..3dd3abb 100644 --- a/metaform/template.html +++ b/metaform/template.html @@ -2,7 +2,7 @@ - ~form_name.cap~ + ~plural.cap~ + diff --git a/metaform/template.php b/metaform/template.php index 1822ff3..9742f4e 100644 --- a/metaform/template.php +++ b/metaform/template.php @@ -226,7 +226,8 @@ function _~file_name~_main() { tem_show('edit_msg'); } - tem_show('form'); + tem_show('form'); + tem_show('extra_headers'); } ?> -- 1.7.10.4 From 7f42492ea33a8d43ee12089b97bd8c0693b293e0 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Tue, 2 Jun 2009 07:19:12 -0400 Subject: [PATCH 13/16] added enc_s() --- encode.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/encode.php b/encode.php index 9aa09a6..cd1e0b4 100644 --- a/encode.php +++ b/encode.php @@ -370,3 +370,12 @@ function enc_thumb_height($str) { list($a, $b, $c, $src, $width, $height) = explode(' ', $str); return $height; } + +# example template: Length: ~length.html~ day~length.s~ +function enc_s($str) { + if($str == '1') { + return ''; + } + + return 's'; +} -- 1.7.10.4 From 608ecf965408645758cdca1e5f01ff5ac3eff166 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Tue, 2 Jun 2009 11:15:39 -0400 Subject: [PATCH 14/16] cleaned up README a little --- README | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README b/README index f501698..61ebc9a 100644 --- a/README +++ b/README @@ -1,11 +1,11 @@ -WFPL (Well Factored PHP Library) +wfpl (Well Factored PHP Library) wfpl is a collection of php functions that make it easy to create websites, including things that are tricky to do from scratch like editing databases. It includes: -* A templating system (so all your data/markup can be in the .html file) +* A templating system (so all your data/markup can be in html files) * An easy and secure database API @@ -13,10 +13,10 @@ It includes: data. * metaform (early development) a page to auto-generate code for forms (html - template, php code, SQL code and even an e-mail template.) + template, PHP code, SQL code and even an e-mail template.) * Easy soft redirects (meaning that in your php code, you can easily specify that you'd like a different php file to handle this request.) -wfpl is released under the Gnu GPL v3 or greater. +wfpl is released under the GNU GPL v3 or greater. -- 1.7.10.4 From 28ea182162da1c22d16f93bc2cb5619c14e222a7 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Wed, 3 Jun 2009 01:00:35 -0400 Subject: [PATCH 15/16] added new_readable_password() --- misc.php | 5 +---- session.php | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/misc.php b/misc.php index 1737a86..857f6db 100644 --- a/misc.php +++ b/misc.php @@ -67,15 +67,12 @@ function exec_pipe($command, $stdin) { } - - - - function unix_newlines($str) { $str = str_replace("\r\n", "\n", $str); return str_replace("\r", "\n", $str); } + # return current year (all 4 digits) function this_year() { return strftime('%Y'); diff --git a/session.php b/session.php index 9dc3f4e..26523c2 100644 --- a/session.php +++ b/session.php @@ -26,7 +26,7 @@ # GLOSSARY # # session_key 16 digit string identifying the session -# session_id integer id of the record in the "sessions" table of the database +# session_id integer id of the record in the "wfpl_sessions" table of the database # UNTIL_CLOSE a constant passed as session length to indicate "until browser window closes" @@ -130,6 +130,22 @@ function session_exists_and_authed() { return logged_in(); } + +# generate a random password using only letters and numbers that look +# particularly unique +function new_readable_password($length = 8) { + $character_set = "ABCDEFHJKLMNPQRTUVWXY34789"; + $code = ""; + + # PHP 4.2.0 and up seed the random number generator for you. + # Lets hope that it seeds with something harder to guess than the clock. + while($length--) { + $code .= $character_set{mt_rand(0, 25)}; # inclusive + } + + return $code; +} + # return username if a session exists and is authenticated function logged_in() { if(!session_exists()) { -- 1.7.10.4 From 85176c05d844a9b3c693e3436d358cc1bf30b1eb Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Sun, 14 Jun 2009 12:38:06 -0400 Subject: [PATCH 16/16] metaform: fixed bug with listing but no display page (some links got an extra _edit in them) --- metaform.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/metaform.php b/metaform.php index ebd1f99..19e2168 100644 --- a/metaform.php +++ b/metaform.php @@ -268,9 +268,6 @@ function make_html($whole_file = true) { } if($GLOBALS['opt_listing'] == 'Yes') { - if($GLOBALS['opt_display'] != 'Yes') { - $tem->show('opt_display_a_else'); - } if(show_in_listing($type, $input, $format, $sql)) { if($format == 'bool' || $format == 'yesno') { $tem->set('listing_enc', 'yesno'); @@ -284,6 +281,10 @@ function make_html($whole_file = true) { $tem->set('listing_enc', 'html'); $tem->show('listing_value_enc'); } + + if($GLOBALS['opt_display'] != 'Yes') { + $tem->show('opt_display_a_else'); + } $tem->show('listing_head_col'); $tem->show('listing_row_col'); } -- 1.7.10.4