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(INC_WFPL . 'format.php');
19 require_once(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'] = 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 if(!logged_in_as_admin()) {
59 $_REQUEST['url'] = this_url();
63 $id = _REQUEST_cut('edit_id');
65 return admin_images_main_form($id);
68 $id = _REQUEST_cut('admin_images_delete_id');
70 return admin_images_main_delete($id);
73 if(_REQUEST_cut('new')) {
74 return admin_images_main_form();
77 if(_REQUEST_cut('list')) {
78 return admin_images_main_listing();
81 $id = _REQUEST_cut('id');
83 return admin_images_main_display($id);
86 if(isset($_POST['name'])) {
87 return admin_images_main_form();
91 return admin_images_main_listing();
94 function admin_images_main_display($id) {
95 $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
97 message("Error: Broken Link (Image #$id not found)");
98 return './admin_images';
101 # Find pages that have this image on it
103 $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
105 $data['references'] = array(
106 'data' => $references,
107 'count' => count($references));
111 # display smaller versions with instructions and example code
113 if($data['image'] && $data['sizes']) {
114 $big_src = enc_image_src($data['image']);
115 $row = explode("\n", $data['sizes']);
116 foreach($row as $max_hw) {
117 $max_hw = format_width_height($max_hw);
121 list($max_width, $max_height) = explode('x', $max_hw);
122 $src = str_replace('.', "-$max_width-$max_height.", $big_src);
123 $dimensions = image_dimensions($src);
125 list($width, $height) = explode('x', $dimensions);
128 $height = $max_height;
133 'max_width' => $max_width,
134 'max_height' => $max_height,
136 'height' => $height);
140 $data['smaller'] = $smaller;
145 tem_set('display', $data);
148 function admin_images_main_delete($id) {
149 $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
151 $filenames = array();
152 $space = strpos($data['image'], ' ');
153 $dot = strpos($data['image'], '.');
154 if ($space !== false && $dot !== false && $dot < $space) {
155 $base = substr($data['image'], 0, $dot);
156 $ext = substr($data['image'], $dot, $space - $dot);
157 $filenames[] = "$base$ext";
158 $filenames[] = "{$base}_thumb$ext";
159 $sizes = explode("\n", $data['sizes']);
160 foreach ($sizes as $max_hw) {
161 $max_hw = format_width_height($max_hw);
165 list($max_width, $max_height) = explode('x', $max_hw);
166 $filenames[] = "$base-{$max_width}x$max_height$ext"; # old naming scheme
167 $filenames[] = "$base-{$max_width}-$max_height$ext"; # new namich scheme
170 foreach ($filenames as $filename) {
171 if (file_exists($filename)) {
175 db_delete('cms_images', 'where id=%i', $id);
176 message('Image deleted.');
178 message("Couldn't find image to delete. Maybe it's already been deleted?");
180 return './admin_images';
183 function admin_images_main_listing() {
184 $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by name, caption');
185 tem_set('listings', $listing_rows);
188 function admin_images_main_form($id = false) {
193 if(isset($_POST['name'])) {
194 $data = admin_images_get_fields();
197 # 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.)
199 # resize image as needed
200 if($data['image'] && $data['sizes']) {
201 $big_src = enc_image_src($data['image']);
202 $row = explode("\n", $data['sizes']);
203 foreach($row as $max_hw) {
204 $max_hw = format_width_height($max_hw);
208 list($max_width, $max_height) = explode('x', $max_hw);
209 $src = str_replace('.', "-$max_width-$max_height.", $big_src);
210 if(($_FILES['image'] && $_FILES['image']['error'] == 0) || !file_exists($src)) {
211 imagemagick_convert($big_src, $src, "-geometry $max_hw", 'Resizing image');
218 db_update_assoc('cms_images', $data, 'where id=%i', $id);
219 message('Image updated.');
222 db_insert_assoc('cms_images', $data);
223 message('Image saved. Next time you open a page editor, this image will be availble in the "Insert Image" dialog.');
224 $saved_id = db_auto_id();
227 # return user to display page where they can see instructions, etc
228 return "./admin_images";
231 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
232 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
234 # form not submitted, set default values:
235 $data = array('sizes' => '275x500');
238 tem_set('upload_max_filesize', upload_max_filesize());
240 tem_set('form', $data);