3 # This form requires wfpl. See: http://sametwice.com/wfpl
5 define('ADMIN_IMAGES_DB_FIELDS', 'image,name,caption');
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') {
44 unset($_FILES['image']);
50 function admin_images_main() {
51 session_auth_must('admin_images');
53 $id = _REQUEST_cut('edit_id');
55 return admin_images_main_form($id);
58 $id = _REQUEST_cut('admin_images_delete_id');
60 return admin_images_main_delete($id);
63 if(_REQUEST_cut('new')) {
64 return admin_images_main_form();
67 if(_REQUEST_cut('list')) {
68 return admin_images_main_listing();
71 if(isset($_POST['name'])) {
72 return admin_images_main_form();
76 return admin_images_main_listing();
79 function admin_images_main_delete($id) {
80 $data = db_get_assoc('cms_images', 'image', 'where id=%i', $id);
82 $src = enc_image_src($data['image']);
84 $filenames = array($src);
85 foreach ($GLOBALS['wfpl_image_widths'] as $w) {
86 $filenames [] = substr($src, 0, -4) . 'w' . $w . substr($src, -4);
88 foreach ($filenames as $filename) {
89 if (file_exists($filename)) {
94 db_delete('cms_images', 'where id=%i', $id);
95 message('Image deleted.');
97 message("Couldn't find image to delete. Maybe it's already been deleted?");
99 return './admin_images';
102 function admin_images_main_listing() {
105 'age' => 'created_at desc',
106 'name' => "coalesce(nullif(name, ''), caption), created_at",
107 'caption' => "coalesce(nullif(caption, ''), name), created_at"
109 if (isset($_REQUEST['sort'])) {
110 foreach ($sorts as $s => $sql) {
111 if ($_REQUEST['sort'] == $s) {
117 tem_set("sort_by_$sort_by");
118 $listing_rows = db_get_assocs('cms_images', 'id,image,name,caption', 'order by ' . $sorts[$sort_by]);
119 tem_set('listings', $listing_rows);
122 function admin_images_main_form($id = false) {
127 if(isset($_POST['name'])) {
128 $data = admin_images_get_fields();
131 # 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.)
135 db_update_assoc('cms_images', $data, 'where id=%i', $id);
136 message('Image updated.');
138 return "./admin_images";
140 $data['created_at'] = time();
141 db_insert_assoc('cms_images', $data);
142 message('Image saved. Next time you open a page editor, this image will be availble in the "Insert Image" dialog.');
143 $saved_id = db_auto_id();
144 return "./admin_images?sort=age";
148 # we've recieved an edit id, but no data. So we grab the values to be edited from the database
149 $data = db_get_assoc('cms_images', ADMIN_IMAGES_DB_FIELDS, 'where id=%i', $id);
151 # form not submitted, set default values:
155 tem_set('upload_max_filesize', upload_max_filesize());
157 tem_set('form', $data);