function thumbnail_delete() { global $txp_user; $id = assert_int(gps('id')); $author = fetch('author', 'txp_image', 'id', $id); if (!has_privs('image.edit') && !($author === $txp_user && has_privs('image.edit.own'))) { image_list(gTxt('restricted_area')); return; } $t = new txp_thumb($id); if ($t->delete()) { callback_event('thumbnail_deleted', '', false, $id); update_lastmod(); image_edit(gTxt('thumbnail_deleted'), $id); } else { image_edit(array(gTxt('thumbnail_delete_failed'), E_ERROR), $id); } }
/** * Uploads an image. * * Can be used to upload a new image or replace an existing one. * If $id is specified, the image will be replaced. If $uploaded is set FALSE, * $file can take a local file instead of HTTP file upload variable. * * All uploaded files will included on the Images panel. * * @param array $file HTTP file upload variables * @param array $meta Image meta data, allowed keys 'caption', 'alt', 'category' * @param int $id Existing image's ID * @param bool $uploaded If FALSE, $file takes a filename instead of upload vars * @return array|string An array of array(message, id) on success, localized error string on error * @package Image * @example * print_r(image_data( * $_FILES['myfile'], * array( * 'caption' => '', * 'alt' => '', * 'category' => '', * ) * )); */ function image_data($file, $meta = array(), $id = 0, $uploaded = true) { global $txp_user, $event; $name = $file['name']; $error = $file['error']; $file = $file['tmp_name']; if ($uploaded) { $file = get_uploaded_file($file); if (get_pref('file_max_upload_size') < filesize($file)) { unlink($file); return upload_get_errormsg(UPLOAD_ERR_FORM_SIZE); } } if (empty($file)) { return upload_get_errormsg(UPLOAD_ERR_NO_FILE); } list($w, $h, $extension) = getimagesize($file); $ext = get_safe_image_types($extension); if (!$ext) { return gTxt('only_graphic_files_allowed'); } $name = substr($name, 0, strrpos($name, '.')) . $ext; $safename = doSlash($name); $meta = lAtts(array('category' => '', 'caption' => '', 'alt' => ''), (array) $meta, false); extract(doSlash($meta)); $q = "\n name = '{$safename}',\n ext = '{$ext}',\n w = {$w},\n h = {$h},\n alt = '{$alt}',\n caption = '{$caption}',\n category = '{$category}',\n date = now(),\n author = '" . doSlash($txp_user) . "'\n "; if (empty($id)) { $rs = safe_insert('txp_image', $q); if ($rs) { $id = $GLOBALS['ID'] = $rs; } $update = false; } else { $id = assert_int($id); $rs = safe_update('txp_image', $q, "id = {$id}"); $update = true; } if (!$rs) { return gTxt('image_save_error'); } $newpath = IMPATH . $id . $ext; if (shift_uploaded_file($file, $newpath) == false) { if (!$update) { safe_delete('txp_image', "id = {$id}"); } unset($GLOBALS['ID']); return $newpath . sp . gTxt('upload_dir_perms'); } @chmod($newpath, 0644); // GD is supported if (check_gd($ext)) { // Auto-generate a thumbnail using the last settings if (get_pref('thumb_w') > 0 || get_pref('thumb_h') > 0) { $t = new txp_thumb($id); $t->crop = (bool) get_pref('thumb_crop'); $t->hint = '0'; $t->width = (int) get_pref('thumb_w'); $t->height = (int) get_pref('thumb_h'); $t->write(); } } $message = gTxt('image_uploaded', array('{name}' => $name)); update_lastmod('image_uploaded', compact('id', 'name', 'ext', 'w', 'h', 'alt', 'caption', 'category', 'txpuser')); // call post-upload plugins with new image's $id callback_event('image_uploaded', $event, false, $id); return array($message, $id); }
function thumbnail_create() { $id = gps('id'); $width = gps('width'); $height = gps('height'); if (!is_numeric($width) && !is_numeric($height)) { image_edit(messenger('invalid_width_or_height', "({$width})/({$height})", ''), $id); return; } $crop = gps('crop'); $t = new txp_thumb($id); $t->crop = $crop == '1'; $t->hint = '0'; if (is_numeric($width)) { $t->width = $width; } if (is_numeric($height)) { $t->height = $height; } if ($t->write()) { image_edit(messenger('thumbnail', $id, 'saved'), $id); } else { image_edit(messenger('thumbnail', $id, 'not_saved'), $id); } }
function image_data($file, $meta = '', $id = '', $uploaded = true) { global $txpcfg, $extensions, $txp_user, $prefs, $file_max_upload_size, $event; extract($txpcfg); $name = $file['name']; $error = $file['error']; $file = $file['tmp_name']; if ($uploaded) { $file = get_uploaded_file($file); if ($file_max_upload_size < filesize($file)) { unlink($file); return upload_get_errormsg(UPLOAD_ERR_FORM_SIZE); } } if (empty($file)) { return upload_get_errormsg(UPLOAD_ERR_NO_FILE); } list($w, $h, $extension) = getimagesize($file); if ($file !== false && @$extensions[$extension]) { $ext = $extensions[$extension]; $name = substr($name, 0, strrpos($name, '.')) . $ext; $safename = doSlash($name); if ($meta == false) { $meta = array('category' => '', 'caption' => '', 'alt' => ''); } extract(doSlash($meta)); $q = "\n\t\t\t\tname = '{$safename}',\n\t\t\t\text = '{$ext}',\n\t\t\t\tw = {$w},\n\t\t\t\th = {$h},\n\t\t\t\talt = '{$alt}',\n\t\t\t\tcaption = '{$caption}',\n\t\t\t\tcategory = '{$category}',\n\t\t\t\tdate = now(),\n\t\t\t\tauthor = '" . doSlash($txp_user) . "'\n\t\t\t"; if (empty($id)) { $rs = safe_insert('txp_image', $q); $id = $GLOBALS['ID'] = mysql_insert_id(); } else { $id = assert_int($id); $rs = safe_update('txp_image', $q, "id = {$id}"); } if (!$rs) { return gTxt('image_save_error'); } else { $newpath = IMPATH . $id . $ext; if (shift_uploaded_file($file, $newpath) == false) { $id = assert_int($id); safe_delete('txp_image', "id = {$id}"); safe_alter('txp_image', "auto_increment = {$id}"); if (isset($GLOBALS['ID'])) { unset($GLOBALS['ID']); } return $newpath . sp . gTxt('upload_dir_perms'); } else { @chmod($newpath, 0644); // GD is supported if (check_gd($ext)) { // Auto-generate a thumbnail using the last settings if (isset($prefs['thumb_w'], $prefs['thumb_h'], $prefs['thumb_crop'])) { $width = intval($prefs['thumb_w']); $height = intval($prefs['thumb_h']); if ($width > 0 or $height > 0) { $t = new txp_thumb($id); $t->crop = $prefs['thumb_crop'] == '1'; $t->hint = '0'; $t->width = $width; $t->height = $height; $t->write(); } } } $message = gTxt('image_uploaded', array('{name}' => $name)); update_lastmod(); // call post-upload plugins with new image's $id callback_event('image_uploaded', $event, false, $id); return array($message, $id); } } } else { if ($file === false) { return upload_get_errormsg($error); } else { return gTxt('only_graphic_files_allowed'); } } }