JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
stylus helper for ckeditor sizing
[wfpl-cms.git] / admin_images.php
1 <?php
2
3 # This form requires wfpl. See: http://sametwice.com/wfpl
4
5 define('ADMIN_IMAGES_DB_FIELDS', 'image,name,caption,sizes');
6
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/';
10
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
16
17
18 require_once(DOCROOT . 'inc/wfpl/format.php');
19 require_once(DOCROOT . 'inc/wfpl/upload.php');
20
21 # example: 200x300
22 function format_width_height($str) {
23         $fields = explode('x', $str);
24         if(count($fields) != 2) {
25                 return '';
26         }
27
28         list($width, $height) = $fields;
29         $width = format_int_0($width);
30         $height = format_int_0($height);
31
32         return "${width}x$height";
33 }
34
35 function admin_images_get_fields() {
36         $data = array();
37
38         $data['name'] = format_oneline(_REQUEST_cut('name'));
39         $data['caption'] = format_oneline(_REQUEST_cut('caption'));
40         $data['sizes'] = format_unix(_REQUEST_cut('sizes'));
41
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']);
44         } else {
45                 if(_REQUEST_cut('delete_image') == 'Yes') {
46                         $data['image'] = '';
47                 } else {
48                         $data['image'] = format_image_w_h_thumb_w_h(_REQUEST_cut('old_image'));
49                 }
50         }
51         unset($_FILES['image']);
52
53         return $data;
54 }
55
56
57 function admin_images_main() {
58         session_auth_must('admin_images');
59
60         $id = _REQUEST_cut('edit_id');
61         if($id) {
62                 return admin_images_main_form($id);
63         }
64
65         $id = _REQUEST_cut('admin_images_delete_id');
66         if($id) {
67                 return admin_images_main_delete($id);
68         }
69
70         if(_REQUEST_cut('new')) {
71                 return admin_images_main_form();
72         }
73
74         if(_REQUEST_cut('list')) {
75                 return admin_images_main_listing();
76         }
77
78         $id = _REQUEST_cut('id');
79         if($id) {
80                 return admin_images_main_display($id);
81         }
82
83         if(isset($_POST['name'])) {
84                 return admin_images_main_form();
85         }
86
87         # default action:
88         return admin_images_main_listing();
89 }
90
91 function admin_images_main_display($id) {
92         $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
93         if(!$data) {
94                 message("Error: Broken Link (Image #$id not found)");
95                 return './admin_images';
96         }
97
98         # Find pages that have this image on it
99         if($data['image']) {
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
101                 if($references) {
102                         $data['references'] = array(
103                                 'data' => $references,
104                                 'count' => count($references));
105                 }
106         }
107
108         # display smaller versions with instructions and example code
109         $smaller == array();
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);
115                         if($max_hw == '') {
116                                 continue;
117                         }
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);
121                         if($dimensions) {
122                                 list($width, $height) = explode('x', $dimensions);
123                         } else {
124                                 $width = $max_width;
125                                 $height = $max_height;
126                         }
127
128                         $smaller[] = array(
129                                 'src' => $src,
130                                 'max_width' => $max_width,
131                                 'max_height' => $max_height,
132                                 'width' => $width,
133                                 'height' => $height);
134                 }
135         }
136         if($smaller) {
137                 $data['smaller'] = $smaller;
138         } else {
139                 tem_set('no_sizes');
140         }
141
142         tem_set('display', $data);
143 }
144
145 function admin_images_main_delete($id) {
146         $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
147         if ($data) {
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);
159                                 if($max_hw == '') {
160                                         continue;
161                                 }
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
165                         }
166                 }
167                 foreach ($filenames as $filename) {
168                         if (file_exists($filename)) {
169                                 unlink($filename);
170                         }
171                 }
172                 db_delete('cms_images', 'where id=%i', $id);
173                 message('Image deleted.');
174         } else {
175                 message("Couldn't find image to delete. Maybe it's already been deleted?");
176         }
177         return './admin_images';
178 }
179
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);
183 }
184
185 function admin_images_main_form($id = false) {
186         if($id) {
187                 tem_set('id', $id);
188         }
189
190         if(isset($_POST['name'])) {
191                 $data = admin_images_get_fields();
192
193                 # save anything
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.)
195
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);
202                                 if($max_hw == '') {
203                                         continue;
204                                 }
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');
209                                 }
210                         }
211                 }
212
213                 # save to database
214                 if($id) {
215                         db_update_assoc('cms_images', $data, 'where id=%i', $id);
216                         message('Image updated.');
217                         $saved_id = $id;
218                 } else {
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();
222                 }
223
224                 # return user to display page where they can see instructions, etc
225                 return "./admin_images";
226
227         } elseif($id) {
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);
230         } else {
231                 # form not submitted, set default values:
232                 $data = array('sizes' => '275x500');
233         }
234
235         tem_set('upload_max_filesize', upload_max_filesize());
236
237         tem_set('form', $data);
238 }