3 # This form requires wfpl. See: http://sametwice.com/wfpl
5 define('ADMIN_IMAGES_DB_FIELDS', 'image,name,caption,sizes');
7 require_once(DOCROOT . 'inc/wfpl/format.php');
8 require_once(DOCROOT . 'inc/wfpl/upload.php');
11 function format_width_height($str) {
12 $fields = explode('x', $str);
13 if(count($fields) != 2) {
17 list($width, $height) = $fields;
18 $width = format_int_0($width);
19 $height = format_int_0($height);
21 return "${width}x$height";
24 function admin_images_get_fields() {
27 $data['name'] = format_oneline(_REQUEST_cut('name'));
28 $data['caption'] = format_oneline(_REQUEST_cut('caption'));
30 if($_FILES['image'] && $_FILES['image']['error'] == 0 && file_exists($_FILES['image']['tmp_name'])) {
31 $image_fn_ext = path_or_mime_to_ext($_FILES['image']['name'], $_FILES['image']['type']);
32 $image_fn_ext = ext_to_web_image_ext($image_fn_ext);
33 $image_fn_base = sha1_file($_FILES['image']['tmp_name']);
34 if (strlen($image_fn_base) == 40) {
35 $image_fn_base = substr($image_fn_base, 0, 16);
36 $image_filename = 'cms_images/' . $image_fn_base . '.' . $image_fn_ext;
37 $data['image'] = convert_uploaded_image('image', $image_filename);
40 if(_REQUEST_cut('delete_image') == 'Yes') {
43 $data['image'] = format_image_w_h(_REQUEST_cut('old_image'));
46 unset($_FILES['image']);
52 function admin_images_main() {
53 session_auth_must('admin_images');
55 $id = _REQUEST_cut('edit_id');
57 return admin_images_main_form($id);
60 $id = _REQUEST_cut('admin_images_delete_id');
62 return admin_images_main_delete($id);
65 if(_REQUEST_cut('new')) {
66 return admin_images_main_form();
69 if(_REQUEST_cut('list')) {
70 return admin_images_main_listing();
73 $id = _REQUEST_cut('id');
75 return admin_images_main_display($id);
78 if(isset($_POST['name'])) {
79 return admin_images_main_form();
83 return admin_images_main_listing();
86 function admin_images_main_display($id) {
87 $data = db_get_assoc('cms_images', 'id,'.ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
89 message("Error: Broken Link (Image #$id not found)");
90 return './admin_images';
93 # Find pages that have this image on it
95 $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));
97 $data['references'] = array(
98 'data' => $references,
99 'count' => count($references));
103 tem_set('display', $data);
106 function admin_images_main_delete($id) {
107 $data = db_get_assoc('cms_images', 'image,sizes', 'where id=%i', $id);
109 $src = enc_image_src($data['image']);
111 $filenames = array($src);
112 foreach ($GLOBALS['wfpl_image_widths'] as $w) {
113 $filenames [] = substr($src, 0, -4) . 'w' . $w . substr($src, -4);
115 foreach ($filenames as $filename) {
116 if (file_exists($filename)) {
121 db_delete('cms_images', 'where id=%i', $id);
122 message('Image deleted.');
124 message("Couldn't find image to delete. Maybe it's already been deleted?");
126 return './admin_images';
129 function admin_images_main_listing() {
130 $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by name, caption');
131 tem_set('listings', $listing_rows);
134 function admin_images_main_form($id = false) {
139 if(isset($_POST['name'])) {
140 $data = admin_images_get_fields();
143 # 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.)
147 db_update_assoc('cms_images', $data, 'where id=%i', $id);
148 message('Image updated.');
151 db_insert_assoc('cms_images', $data);
152 message('Image saved. Next time you open a page editor, this image will be availble in the "Insert Image" dialog.');
153 $saved_id = db_auto_id();
156 # return user to display page where they can see instructions, etc
157 return "./admin_images";
160 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
161 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
163 # form not submitted, set default values:
164 $data = array('sizes' => '275x500');
167 tem_set('upload_max_filesize', upload_max_filesize());
169 tem_set('form', $data);