3 # This form requires wfpl. See: http://sametwice.com/wfpl
5 define('ADMIN_IMAGES_DB_FIELDS', 'image,name,caption,sizes');
7 # Set this to the path to your uploads directory. It can be relative to the
8 # location of this script. IT MUST END WITH A SLASH
9 $GLOBALS['upload_directory'] = 'cms_images/';
11 $GLOBALS['image_max_width'] = '704';
12 $GLOBALS['image_max_height'] = '1900';
13 $GLOBALS['image_thumb_max_width'] = '70';
14 $GLOBALS['image_thumb_max_height'] = '70';
15 $GLOBALS['image_file_name'] = uniqid() . getmypid() . '.jpg'; # comment this out to use uploader's filename
18 require_once(DOCROOT . 'inc/wfpl/format.php');
19 require_once(DOCROOT . 'inc/wfpl/upload.php');
22 function format_width_height($str) {
23 $fields = explode('x', $str);
24 if(count($fields) != 2) {
28 list($width, $height) = $fields;
29 $width = format_int_0($width);
30 $height = format_int_0($height);
32 return "${width}x$height";
35 function admin_images_get_fields() {
38 $data['name'] = format_oneline(_REQUEST_cut('name'));
39 $data['caption'] = format_oneline(_REQUEST_cut('caption'));
40 $data['sizes'] = str_replace(' ', '', strtolower(format_unix(_REQUEST_cut('sizes'))));
42 if($_FILES['image'] && $_FILES['image']['error'] == 0) {
43 $data['image'] = convert_uploaded_image('image', $GLOBALS['upload_directory'] . $GLOBALS['image_file_name'], $GLOBALS['image_max_width'], $GLOBALS['image_max_height'], $GLOBALS['image_thumb_max_width'], $GLOBALS['image_thumb_max_height']);
45 if(_REQUEST_cut('delete_image') == 'Yes') {
48 $data['image'] = format_image_w_h_thumb_w_h(_REQUEST_cut('old_image'));
51 unset($_FILES['image']);
57 function admin_images_main() {
58 session_auth_must('admin_images');
60 $id = _REQUEST_cut('edit_id');
62 return admin_images_main_form($id);
65 $id = _REQUEST_cut('admin_images_delete_id');
67 return admin_images_main_delete($id);
70 if(_REQUEST_cut('new')) {
71 return admin_images_main_form();
74 if(_REQUEST_cut('list')) {
75 return admin_images_main_listing();
78 $id = _REQUEST_cut('id');
80 return admin_images_main_display($id);
83 if(isset($_POST['name'])) {
84 return admin_images_main_form();
88 return admin_images_main_listing();
91 function admin_images_main_display($id) {
92 $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
94 message("Error: Broken Link (Image #$id not found)");
95 return './admin_images';
98 # Find pages that have this image on it
100 $references = db_get_assocs('cms_pages', 'title,filename', 'where content like "%%%s%%" order by concat(nav_title,title)', substr(enc_image_src($data['image']), 0, -4)); # FIXME test that this works for smaller images
102 $data['references'] = array(
103 'data' => $references,
104 'count' => count($references));
108 # display smaller versions with instructions and example code
110 if($data['image'] && $data['sizes']) {
111 $big_src = enc_image_src($data['image']);
112 $row = explode("\n", $data['sizes']);
113 foreach($row as $max_hw) {
114 $max_hw = format_width_height($max_hw);
118 list($max_width, $max_height) = explode('x', $max_hw);
119 $src = str_replace('.', "-$max_width-$max_height.", $big_src);
120 $dimensions = image_dimensions($src);
122 list($width, $height) = explode('x', $dimensions);
125 $height = $max_height;
130 'max_width' => $max_width,
131 'max_height' => $max_height,
133 'height' => $height);
137 $data['smaller'] = $smaller;
142 tem_set('display', $data);
145 function admin_images_main_delete($id) {
146 $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
148 $filenames = array();
149 $space = strpos($data['image'], ' ');
150 $dot = strpos($data['image'], '.');
151 if ($space !== false && $dot !== false && $dot < $space) {
152 $base = substr($data['image'], 0, $dot);
153 $ext = substr($data['image'], $dot, $space - $dot);
154 $filenames[] = "$base$ext";
155 $filenames[] = "{$base}_thumb$ext";
156 $sizes = explode("\n", $data['sizes']);
157 foreach ($sizes as $max_hw) {
158 $max_hw = format_width_height($max_hw);
162 list($max_width, $max_height) = explode('x', $max_hw);
163 $filenames[] = "$base-{$max_width}x$max_height$ext"; # old naming scheme
164 $filenames[] = "$base-{$max_width}-$max_height$ext"; # new namich scheme
167 foreach ($filenames as $filename) {
168 if (file_exists($filename)) {
172 db_delete('cms_images', 'where id=%i', $id);
173 message('Image deleted.');
175 message("Couldn't find image to delete. Maybe it's already been deleted?");
177 return './admin_images';
180 function admin_images_main_listing() {
181 $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by name, caption');
182 tem_set('listings', $listing_rows);
185 function admin_images_main_form($id = false) {
190 if(isset($_POST['name'])) {
191 $data = admin_images_get_fields();
194 # Note: If you change this to re-display the form in some cases, be sure to handle image uploads well (don't make them upload it again.)
196 # resize image as needed
197 if($data['image'] && $data['sizes']) {
198 $big_src = enc_image_src($data['image']);
199 $row = explode("\n", $data['sizes']);
200 foreach($row as $max_hw) {
201 $max_hw = format_width_height($max_hw);
205 list($max_width, $max_height) = explode('x', $max_hw);
206 $src = str_replace('.', "-$max_width-$max_height.", $big_src);
207 if(($_FILES['image'] && $_FILES['image']['error'] == 0) || !file_exists($src)) {
208 imagemagick_convert($big_src, $src, "-geometry $max_hw", 'Resizing image');
215 db_update_assoc('cms_images', $data, 'where id=%i', $id);
216 message('Image updated.');
219 db_insert_assoc('cms_images', $data);
220 message('Image saved. Next time you open a page editor, this image will be availble in the "Insert Image" dialog.');
221 $saved_id = db_auto_id();
224 # return user to display page where they can see instructions, etc
225 return "./admin_images";
228 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
229 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
231 # form not submitted, set default values:
232 $data = array('sizes' => '275x500');
235 tem_set('upload_max_filesize', upload_max_filesize());
237 tem_set('form', $data);