JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
db_upgrade rework, admin_header, cleanup
[wfpl-cms.git] / admin_header.php
diff --git a/admin_header.php b/admin_header.php
new file mode 100644 (file)
index 0000000..66430ad
--- /dev/null
@@ -0,0 +1,72 @@
+<?php
+
+require_once('inc/wfpl/upload.php');
+
+$GLOBALS['upload_directory'] = 'image/';
+
+
+function admin_header_get_fields() {
+       $data = array();
+
+       $data['width'] = (int) format_int_0(_REQUEST_cut('width'));
+       $data['caption'] = format_oneline(_REQUEST_cut('caption'));
+
+       if ($data['width'] < 100 || $data['width'] > WFPL_SITE_WIDTH) {
+               $data['width'] = WFPL_SITE_WIDTH;
+       }
+
+       $old = persistent_get('wfplcms_header_image');
+       if ($_FILES['image'] && $_FILES['image']['error'] == 0) {
+               if ($old) {
+                       $data['version'] = 1 + $old['version'];
+               } else {
+                       $data['version'] = 1;
+               }
+               $ext = generate_filename($_FILES['image']['name'], $_FILES['image']['type']);
+               $ext = substr($ext, strrpos($ext, '.') + 1);
+               $ext = strtolower($ext);
+               if ($ext !== 'jpg' && $ext !== 'jpeg') {
+                       $ext = 'png';
+               }
+               $dest = "{$GLOBALS['upload_directory']}header_v{$data['version']}.$ext";
+               $image = convert_uploaded_image('image', $dest, $data['width'], 2000);
+               # FIXME use autoresizer instead, and call identify to get aspect ratio
+               $image = explode(' ', $image);
+               $data['url'] = $image[0];
+               $data['width'] = $image[1];
+               $data['height'] = $image[2];
+       } else if ($old) {
+               $data['url'] = $old['url'];
+               if ($data['width'] == $old['width']) {
+                       $data['height'] = $old['height'];
+               } else {
+                       $data['height'] = $old['height'] / $old['width'] * $data['width'];
+               }
+       }
+       unset($_FILES['image']);
+
+       return $data;
+}
+
+
+function admin_header_main() {
+       session_auth_must('admin_header');
+
+       if (isset($_REQUEST['width'])) {
+               $data = admin_header_get_fields();
+               if (isset($data['url'])) {
+                       persistent_set('wfplcms_header_image', $data);
+                       message('Header image updated');
+                       return './admin_header';
+               }
+       } else {
+               $data = persistent_get('wfplcms_header_image');
+               if (!$data) {
+                       $data = array('width' => '');
+               }
+       }
+
+       tem_set('width_max', WFPL_SITE_WIDTH);
+
+       tem_set('form', $data);
+}
\ No newline at end of file