JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed conflict
authorJason Woofenden <jason183@herkamire.com>
Tue, 5 Jun 2007 08:40:20 +0000 (04:40 -0400)
committerJason Woofenden <jason183@herkamire.com>
Tue, 5 Jun 2007 08:40:20 +0000 (04:40 -0400)
format.php
metaform.php
metaform/template.html
metaform/template.php
run.php
upload.php

index a658dbb..cf59504 100644 (file)
@@ -32,8 +32,8 @@ function format_zip($str) {
 
 function format_filename($str) {
        $str = strtolower($str);
-       $str = ereg_replace('[^a-z0-9_.-]', '_', $str);
-       return ereg_replace('^[0-9.-]*', '', $str);
+       $str = ereg_replace('[^a-z0-9_.]', '_', $str);
+       return ereg_replace('^[.]*', '', $str);
 }
 
 function format_varname($str) {
index 8215394..7bfa9f7 100644 (file)
@@ -261,8 +261,8 @@ function make_php() {
                                }
                        } else {
                                $tem->sub('formats');
-                               $tem->sub('tem_sets');
                        }
+                       $tem->sub('tem_sets');
                        if(!$always_field and $input != 'checkbox' and $input != 'radio') {
                                $always_field = $name;
                        }
index d5a2a5a..a7c0e88 100644 (file)
@@ -4,6 +4,7 @@
 <head>
   <title>~form_name~ entry</title>
   <style type="text/css"><!--
+td.field { vertical-align: bottom; }
 td.caption { text-align: right; vertical-align: top; font-weight: bold; }
 td.errorcaption { text-align: right; vertical-align: top; font-weight: bold; color: red; }
 div.error { border: 2px solid red; padding: 13px; margin: 20px; background: #ffdddd; }
@@ -31,8 +32,7 @@ div.error { border: 2px solid red; padding: 13px; margin: 20px; background: #ffd
   <form action="~form_name~.html~enctype_attr~" method="post"><!--~~editing start~~--><input type="hidden" name="~form_name~_edit_id" value="~~~form_name~_edit_id.attr~~" /><!--~~end~~--><!--~uploads start~--><input type="hidden" name="MAX_FILE_SIZE" value="~~upload_max_filesize~~" /><!--~end~-->
     <table cellspacing="0" cellpadding="4" border="0" summary=""><!--~row start~-->
 
-      <tr><!--~image start~--><td class="caption">~caption.html~: </td><td><input type="file" name="~name~" /></td><!--~end~--><!--~textbox start~--><td class="caption">~caption.html~: </td><td><input type="text" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~password start~--><td class="caption">~caption.html~: </td><td><input type="password" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~textarea start~--><td class="caption">~caption.html~: </td><td><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~html start~--><td class="caption">~caption.html~: </td><td></td></tr><tr><td colspan="2"><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~radio start~--><td class="caption">~caption.html~: </td><td><input type="radio" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~checkbox start~--><td class="caption">~caption.html~: </td><td><input type="checkbox" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~submit start~--><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td><!--~end~--></tr><!--~end~-->
-
+      <tr><!--~image start~--><td class="caption">~caption.html~: </td><td><input type="file" name="~name~" /></td><!--~end~--><!--~textbox start~--><td class="caption">~caption.html~: </td><td><input type="text" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~password start~--><td class="caption">~caption.html~: </td><td><input type="password" name="~name~" value="~~~name~.attr~~" /></td><!--~end~--><!--~textarea start~--><td class="caption">~caption.html~: </td><td><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~html start~--><td class="caption">~caption.html~: </td><td></td></tr><tr><td colspan="2"><textarea rows="20" cols="50" name="~name~">~~~name~.html~~</textarea></td><!--~end~--><!--~radio start~--><td class="caption">~caption.html~: </td><td class="field"><input type="radio" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~checkbox start~--><td class="caption">~caption.html~: </td><td><input type="checkbox" name="~name~~~~name~.checked~~" /></td><!--~end~--><!--~submit start~--><td class="submit_row" colspan="2"><input type="submit" name="~name~" value="~caption.attr~" /></td><!--~end~--></tr><!--~end~-->
     </table>
   </form>
 <!--~end~--><!--~~end~~-->
index fc1263b..84755d4 100644 (file)
@@ -33,7 +33,11 @@ require_once('code/wfpl/upload.php');<!--~end~-->
 
 function ~form_name~_get_fields() {<!--~formats start~-->
        $~name~ = format_~format~($_REQUEST['~name~']);<!--~end~--><!--~image_upload start~-->
-       $~name~ = save_uploaded_image('~name~', $GLOBALS['upload_directory']);<!--~end~-->
+       if($_FILE['~name~'] && $_FILE['~name~']['error'] == 0) {
+               $~name~ = substr(save_uploaded_image('~name~', $GLOBALS['upload_directory']), strlen($GLOBALS['upload_directory']));
+       } else {
+               $~name~ = format_filename($_REQUEST['old_~name~']);
+       }<!--~end~-->
        <!--~tem_sets start~-->
        tem_set('~name~', $~name~);<!--~end~-->
 
diff --git a/run.php b/run.php
index 03d849a..dac7bc4 100644 (file)
--- a/run.php
+++ b/run.php
 
 require_once('code/wfpl/file_run.php');
 require_once('code/wfpl/http.php');
+require_once('code/wfpl/template.php');
 
 function run_php($basename = false) {
-       if($basename) {
-               $html_file = "$basename.html";
-               $php_file = "$basename.php";
-       } else {
-               $html_file = $_SERVER['REDIRECT_URL'];
-               $html_file = ereg_replace('.*/', '', $html_file);
-               if($html_file == '') {
-                       $html_file = 'index.html';
+       if(!$basename) {
+               $basename = $_SERVER['REDIRECT_URL'];
+               $basename = ereg_replace('.*/', '', $basename);
+               $basename = ereg_replace('\.html$', '', $basename);
+               if($basename == '') {
+                       $basename = 'index';
                }
-               $php_file = ereg_replace('\.html$', '.php', $html_file);
        }
-       if($php_file != $html_file && file_exists($php_file)) {
-               require_once('code/wfpl/template.php');
-               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;
-               }
-               if(file_exists($html_file)) tem_output();
-       } else {
+
+       $html_file = "$basename.html";
+       $php_file = "$basename.php";
+
+       if(!file_exists($php_file)) {
                if(file_exists($html_file)) {
                        readfile($html_file);
                } else {
@@ -97,6 +83,45 @@ function run_php($basename = false) {
                                echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>404</title></head><body><h1>404 File Not Found</h1></body></html>';
                        }
                }
+               exit();
+       }
+
+       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;
+       }
+
+       if($GLOBALS['wfpl_template']) {
+               if(file_exists('template.html')) {
+                       $tem = new tem();
+                       $tem->load("template.html");
+                       $sections = tem_top_subs();
+                       if($sections) foreach($sections as $name => $val) {
+                               $tem->set($name, $val);
+                       }
+
+                       if(file_exists("$basename.css")) {
+                               $tem->set('css_link', "$basename.css");
+                               $tem->sub('css_links');
+                       }
+
+                       $GLOBALS['wfpl_template'] = $tem;
+               }
+
+               if(function_exists('display_messages')) {
+                       display_messages();
+               }
+               tem_output();
        }
 }
 
index 0f2a237..46f8b3c 100644 (file)
 # the type you specified, it will convert the image for you.
 
 
-
 $GLOBALS['mime_to_ext'] = array(
        'text/plain' => 'txt',
        'text/html'  => 'html',
        'image/jpeg' => 'jpg',
+       'image/jpe' => 'jpg',
        'image/jpg'  => 'jpg',
        'image/gif'  => 'gif',
        'image/png'  => 'png',
@@ -116,7 +116,7 @@ function generate_filename($path, $mime = 'text/plain') {
        # replace symbols with underscores
        $filename = ereg_replace('[^a-z0-9_.]', '_', $filename);
 
-       # remove dots from the begning (no invisible files)
+       # remove dots from the beginning (no invisible files)
        $filename = ereg_replace('^\.*', '', $filename);
 
        # fix extension
@@ -156,12 +156,31 @@ function save_uploaded_file($key, $path) {
        }
 
        if(!move_uploaded_file($_FILES[$key]['tmp_name'], $filename)) {
-               die('file upload failed');
+               return false;
        }
 
        return $filename;
 }
 
+function path_to_convert() {
+       if(!isset($GLOBALS['path_to_convert'])) {
+               $convert = '/usr/local/bin/convert';
+               if(!file_exists($convert)) {
+                       $convert = '/usr/bin/convert';
+               }
+               if(!file_exists($convert)) {
+                       $convert = `which convert`;
+               }
+               if($convert == '' || !file_exists($convert)) {
+                       die("can't find imagemagick's 'convert' program");
+               }
+
+               $GLOBALS['path_to_convert'] = $convert;
+       }
+
+       return $GLOBALS['path_to_convert'];
+}
+               
 
 # returns new filename with .png extension
 function gif_to_png($filename, $new_filename = 'just change extension') {
@@ -174,17 +193,8 @@ function gif_to_png($filename, $new_filename = 'just change extension') {
                $new_filename .= '.png';
        }
 
-       $convert = '/usr/local/bin/convert';
-       if(!file_exists($convert)) {
-               $convert = '/usr/bin/convert';
-       }
-       if(!file_exists($convert)) {
-               $convert = `which convert`;
-       }
-       if(!file_exists($convert)) {
-               die("can't find imagemagick's 'convert' program");
-       }
-               
+       $convert = path_to_convert();
+
        $command = "$convert " . escapeshellarg($filename) . ' ' . escapeshellarg($new_filename);
 
        exec($command, $dummy, $ret);
@@ -195,6 +205,41 @@ function gif_to_png($filename, $new_filename = 'just change extension') {
        return $new_filename;
 }
 
+# make a thumbnail image.
+#
+# Thumbnail will have the same filename, except "_thumb" will be added right
+# before the dot preceding the extension. so foo.png yields foo_thumb.png
+#
+# Thumbnail will retain aspect ratio, and be either $max_width wide or
+# $max_height tall (or, if the aspect is just right, both)
+function make_thumbnail($filename, $max_width = '70', $max_height = '70') {
+       $thumb = ereg_replace('[.]([a-z]+)$', "_thumb.\\1", $filename);
+       if($thumb == $filename) {
+               die("couldn't make thumbnail because filename has no extension.");
+       }
+
+       $convert = path_to_convert();
+
+       # can't be too careful
+       $max_width = ereg_replace('[^0-9]', '', $max_width);
+       if($max_width == '') {
+               $max_width = '70';
+       }
+       $max_height = ereg_replace('[^0-9]', '', $max_height);
+       if($max_height == '') {
+               $max_height = '70';
+       }
+       
+       $command = "$convert -geometry ${max_width}x$max_height " . escapeshellarg($filename) . ' ' . escapeshellarg($thumb);
+
+       exec($command, $dummy, $ret);
+       if($ret != 0) {
+               die("Thumbnail creatin failed. convert did exit($ret)");
+       }
+
+       return $thumb;
+}
+
 # like save_uploaded_file() (above) except it converts gifs to pngs.
 #
 # FIXME: if a filename is passed in the end of path, we should check if the file type matches, and if not run convert.