JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
remove obsolete type="text/javascript" from script tags
[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(INC_WFPL . 'format.php');
19 require_once(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         if(!logged_in_as_admin()) {
59                 $_REQUEST['url'] = this_url();
60                 return 'admin_login';
61         }
62
63         $id = _REQUEST_cut('edit_id');
64         if($id) {
65                 return admin_images_main_form($id);
66         }
67
68         $id = _REQUEST_cut('admin_images_delete_id');
69         if($id) {
70                 return admin_images_main_delete($id);
71         }
72
73         if(_REQUEST_cut('new')) {
74                 return admin_images_main_form();
75         }
76
77         if(_REQUEST_cut('list')) {
78                 return admin_images_main_listing();
79         }
80
81         $id = _REQUEST_cut('id');
82         if($id) {
83                 return admin_images_main_display($id);
84         }
85
86         if(isset($_POST['name'])) {
87                 return admin_images_main_form();
88         }
89
90         # default action:
91         return admin_images_main_listing();
92 }
93
94 function admin_images_main_display($id) {
95         $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
96         if(!$data) {
97                 message("Error: Broken Link (Image #$id not found)");
98                 return './admin_images';
99         }
100
101         # Find pages that have this image on it
102         if($data['image']) {
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
104                 if($references) {
105                         $data['references'] = array(
106                                 'data' => $references,
107                                 'count' => count($references));
108                 }
109         }
110
111         # display smaller versions with instructions and example code
112         $smaller == array();
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);
118                         if($max_hw == '') {
119                                 continue;
120                         }
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);
124                         if($dimensions) {
125                                 list($width, $height) = explode('x', $dimensions);
126                         } else {
127                                 $width = $max_width;
128                                 $height = $max_height;
129                         }
130
131                         $smaller[] = array(
132                                 'src' => $src,
133                                 'max_width' => $max_width,
134                                 'max_height' => $max_height,
135                                 'width' => $width,
136                                 'height' => $height);
137                 }
138         }
139         if($smaller) {
140                 $data['smaller'] = $smaller;
141         } else {
142                 tem_set('no_sizes');
143         }
144
145         tem_set('display', $data);
146 }
147
148 function admin_images_main_delete($id) {
149         $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
150         if ($data) {
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);
162                                 if($max_hw == '') {
163                                         continue;
164                                 }
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
168                         }
169                 }
170                 foreach ($filenames as $filename) {
171                         if (file_exists($filename)) {
172                                 unlink($filename);
173                         }
174                 }
175                 db_delete('cms_images', 'where id=%i', $id);
176                 message('Image deleted.');
177         } else {
178                 message("Couldn't find image to delete. Maybe it's already been deleted?");
179         }
180         return './admin_images';
181 }
182
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);
186 }
187
188 function admin_images_main_form($id = false) {
189         if($id) {
190                 tem_set('id', $id);
191         }
192
193         if(isset($_POST['name'])) {
194                 $data = admin_images_get_fields();
195
196                 # save anything
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.)
198
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);
205                                 if($max_hw == '') {
206                                         continue;
207                                 }
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');
212                                 }
213                         }
214                 }
215
216                 # save to database
217                 if($id) {
218                         db_update_assoc('cms_images', $data, 'where id=%i', $id);
219                         message('Image updated.');
220                         $saved_id = $id;
221                 } else {
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();
225                 }
226
227                 # return user to display page where they can see instructions, etc
228                 return "./admin_images";
229
230         } elseif($id) {
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);
233         } else {
234                 # form not submitted, set default values:
235                 $data = array('sizes' => '275x500');
236         }
237
238         tem_set('upload_max_filesize', upload_max_filesize());
239
240         tem_set('form', $data);
241 }