';
foreach ($rs as $row) {
if ($row['flags'] & SMD_THUMB_ACTIVE) {
$profiles[$row['name']] = $row['name'];
}
$thumbs[] = smd_thumb_img($row, $currimg);
}
$thumbs[] = '
';
$qs = array(
"event" => 'image',
"step" => 'image_edit',
"id" => $id,
"page" => $page,
"sort" => $sort,
"dir" => $dir,
"crit" => $crit,
"search_method" => $search_method,
);
$out[] = ''.smd_thumb_gTxt('thumbnail_heading').'';
$out[] = '';
$out[] = upload_form(gTxt('upload_thumbnail'), 'upload_thumbnail', 'smd_thumbnail_insert', 'image', $id, $file_max_upload_size, 'smd_upload_thumbnail', 'thumbnail-upload');
$out[] = '
';
return join('', $out);
}
}
return ' ';
}
// ------------------------
function smd_thumb_img($row, $currimg, $meta=array()) {
global $img_dir;
$dir = sanitizeForUrl($row['name']);
$id = $currimg['id'];
$ext = $currimg['ext'];
$path = IMPATH.$dir.DS.$id.$ext;
if (file_exists($path)) {
$uDate = filemtime($path);
$extras = '';
$w = ($row['width']) ? ' width="'.$row['width'].'"': '';
$h = ($row['height']) ? ' height="'.$row['height'].'"': '';
if (!isset($meta['class'])) {
$meta['class'] = $dir;
}
foreach ($meta as $key => $val) {
$extras .= ' '.$key.'="'.$val.'"';
}
return '
';
}
return '';
}
// ------------------------
function smd_thumb_profiles($evt, $stp, $dflt, $imglist) {
global $smd_thumb_event, $prefs, $step;
if (!has_privs(__FUNCTION__)) {
return;
}
extract(gpsa(array(
'page',
'sort',
'dir',
'crit',
'search_method',
'smd_thumb_add',
'smd_thumb_cancel',
'smd_thumb_save',
'smd_thumb_name',
'smd_thumb_newname',
'smd_thumb_width',
'smd_thumb_height',
'smd_thumb_quality',
'smd_thumb_active',
'smd_thumb_active_new',
'smd_thumb_crop',
'smd_thumb_sharpen',
'smd_thumb_default')));
// Sanitize
$quality = (is_numeric($smd_thumb_quality)) ? (($smd_thumb_quality<0) ? 75 : (($smd_thumb_quality>100) ? 75 : $smd_thumb_quality) ) : 75;
$width = (is_numeric($smd_thumb_width)) ? $smd_thumb_width : 0;
$height = (is_numeric($smd_thumb_height)) ? $smd_thumb_height : 0;
$newname = sanitizeForUrl($smd_thumb_newname);
$msg = '';
// Table control
if (gps('remove')) {
smd_thumb_table_remove();
} else {
if (!smd_thumb_table_exist()) {
smd_thumb_table_install();
}
}
// Toggle profile panel
if ($step == 'save_pane_state') {
smd_thumbnail_save_pane_state();
return;
}
if ($step == 'smd_thumb_create_all') {
smd_thumb_create_all();
}
// Save profile
if ($step == 'smd_thumb_profile_save') {
if (smd_thumb_table_exist()) {
if ($smd_thumb_cancel) {
// Do nothing
} else if ($smd_thumb_add) {
// Create new profile
$exists = safe_row('*', SMD_THUMB, "name='".doSlash($newname)."'");
if ($exists) {
$msg = smd_thumb_gTxt('profile_exists', array('{name}' => doSlash($newname)));
} else {
if ($newname) {
$flags = 0;
$flags = ($smd_thumb_active_new) ? $flags | SMD_THUMB_ACTIVE : $flags;
$flags = ($smd_thumb_crop) ? $flags | SMD_THUMB_CROP : $flags;
$flags = ($smd_thumb_sharpen) ? $flags | SMD_THUMB_SHARP : $flags;
safe_insert(SMD_THUMB,
"name='".doSlash($newname)."'".
", width='".doSlash($width)."'".
", height='".doSlash($height)."'".
", quality='".doSlash($quality)."'".
", flags='".doSlash($flags)."'"
);
// Alter the default thumbnail
if ($smd_thumb_default) {
set_pref('smd_thumb_default_profile', $newname, 'smd_thumb', PREF_HIDDEN);
}
// Create the thumbnail directory
@mkdir(IMPATH.$newname);
}
}
} else {
// Update existing profile
$name = sanitizeForUrl($smd_thumb_name);
$exists = safe_row('*', SMD_THUMB, "name='".doSlash($newname)."'");
if ($exists && strtolower($newname) != strtolower($name)) {
$msg = smd_thumb_gTxt('profile_exists', array('{name}' => doSlash($newname)));
} else {
if ($newname) {
$flags = 0;
$flags = ($smd_thumb_active) ? $flags | SMD_THUMB_ACTIVE : $flags;
$flags = ($smd_thumb_crop) ? $flags | SMD_THUMB_CROP : $flags;
$flags = ($smd_thumb_sharpen) ? $flags | SMD_THUMB_SHARP : $flags;
safe_update(
SMD_THUMB,
"name='".doSlash($newname)."'"
.", width='".doSlash($width)."'"
.", height='".doSlash($height)."'"
.", quality='".doSlash($quality)."'"
.", flags='".doSlash($flags)."'"
, "name='".doSlash($name)."'"
);
// Alter the default thumbnail
if ($smd_thumb_default) {
set_pref('smd_thumb_default_profile', $newname, 'smd_thumb', PREF_HIDDEN);
} else {
// Remove the default flag if this used to be the default
if (get_pref('smd_thumb_default_profile') == doSlash($name)) {
set_pref('smd_thumb_default_profile', '', 'smd_thumb', PREF_HIDDEN);
}
}
// Adjust the thumbnail directory if the name has changed
if ($newname != $name) {
rename(IMPATH.$name, IMPATH.$newname);
}
}
}
}
} else {
$msg = smd_thumb_gTxt('tables_not_installed');
}
}
// Delete profile
if ($step == 'smd_thumb_profile_delete') {
if (smd_thumb_table_exist()) {
if (!empty($smd_thumb_name)) {
$name = sanitizeForUrl($smd_thumb_name);
$ret = safe_delete(SMD_THUMB, "name='".doSlash($name)."'");
if ($ret) {
smd_thumb_rmdir(IMPATH.$name);
// Remove the default flag if this used to be the default
if (get_pref('smd_thumb_default_profile') == doSlash($name)) {
set_pref('smd_thumb_default_profile', '', 'smd_thumb', PREF_HIDDEN);
}
$msg = smd_thumb_gTxt('profile_deleted', array('{name}' => doSlash($name)));
}
}
}
}
$qs = array(
"event" => 'image',
"page" => $page,
"sort" => $sort,
"dir" => $dir,
"crit" => $crit,
"search_method" => $search_method,
);
$editFocus = ($step == 'smd_thumb_profile_edit') ? 'jQuery("#smd_thumb_profile_edited input[name=\'smd_thumb_newname\']").focus();' : '';
echo <<
function smd_thumb_togglenew() {
box = jQuery("#smd_thumb_profile_create");
if (box.css("display") == "none") {
box.show();
} else {
box.hide();
}
jQuery("input.smd_focus").focus();
return false;
}
function smd_thumb_switch_active(name) {
sendAsyncEvent(
{
event: textpattern.event,
step: 'smd_thumb_switch_active',
smd_thumb_profile: name
}, smd_thumb_switch_active_done);
}
function smd_thumb_switch_active_done(data) {
var smd_thumb_profile = jQuery('#smd_thumb_profile_'+jQuery(data).find('smd_thumb_profile').attr('value'));
smd_thumb_profile.toggleClass('smd_inactive');
if (smd_thumb_profile.hasClass('smd_inactive')) {
smd_thumb_profile.find('input:checkbox').removeAttr('checked');
} else {
smd_thumb_profile.find('input:checkbox').attr('checked', 'checked');
}
}
jQuery(function() {
jQuery('.smd_thumb_heading_active').click(function() {
jQuery('.smd_thumb_row input:checkbox').each(function() {
smd_thumb_switch_active(jQuery(this).parent().attr('class'));
});
});
{$editFocus}
});
EOC;
$newbtn = ''.smd_thumb_gTxt('new').' ';
$allbtn = ''.smd_thumb_gTxt('all_thumbs').'';
$headings = n.tr(
n.column_head('name', 'name', 'image', false, 'asc').
column_head('thumb_width', 'width', 'image', false).
column_head('thumb_height', 'height', 'image', false).
column_head(smd_thumb_gTxt('quality'), 'quality', 'image', false).
column_head('keep_square_pixels', 'crop', 'image', false).
column_head(smd_thumb_gTxt('sharpen'), 'sharpen', 'image', false).
column_head('active', 'active', 'image', false, '', '', '', 'smd_thumb_heading_active').
column_head('default', 'default', 'image', false).
column_head(smd_thumb_gTxt('actions'), 'actions', 'image', false)
);
$out[] = '';
return join('', $out);
}
// ------------------------
// Delete a thumbnail directory and its contents
function smd_thumb_rmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir.DS.$object) == "dir") smd_thumb_rmdir($dir.DS.$object); else unlink($dir.DS.$object);
}
}
reset($objects);
rmdir($dir);
}
}
// ------------------------
// Add thumbnail table if not already installed
// Flags: Bit 0=Active / 1=Crop / 2 = Sharpen
function smd_thumb_table_install() {
$GLOBALS['txp_err_count'] = 0;
$ret = '';
$sql = array();
$sql[] = "CREATE TABLE IF NOT EXISTS `".PFX.SMD_THUMB."` (
`name` varchar(32) NOT NULL default '',
`width` int(4) NOT NULL default '0',
`height` int(4) NOT NULL default '0',
`quality` tinyint(2) NULL default '75',
`flags` tinyint(2) NOT NULL default '0',
PRIMARY KEY (`name`)
) ENGINE=MyISAM";
if(gps('debug')) {
dmp($sql);
}
foreach ($sql as $qry) {
$ret = safe_query($qry);
if ($ret===false) {
$GLOBALS['txp_err_count']++;
echo "".$GLOBALS['txp_err_count'].". ".mysql_error()."
\n";
echo "\n";
}
}
// Backup the current thumb prefs
$exists = safe_row('*', 'txp_prefs', "name='smd_thumb_backup_w'");
if (!$exists) {
set_pref('smd_thumb_backup_w', get_pref('thumb_w'), 'smd_thumb', PREF_HIDDEN, 'text_input', 0);
set_pref('smd_thumb_backup_h', get_pref('thumb_h'), 'smd_thumb', PREF_HIDDEN, 'text_input', 1);
set_pref('smd_thumb_backup_c', get_pref('thumb_crop'), 'smd_thumb', PREF_HIDDEN, 'text_input', 2);
// Erase the current thumb prefs
set_pref('thumb_w', '', 'image', PREF_HIDDEN);
set_pref('thumb_h', '', 'image', PREF_HIDDEN);
set_pref('thumb_crop', '', 'image', PREF_HIDDEN);
}
}
// ------------------------
// Drop table if in database
function smd_thumb_table_remove() {
$ret = '';
$sql = array();
$GLOBALS['txp_err_count'] = 0;
if (smd_thumb_table_exist()) {
$sql[] = "DROP TABLE IF EXISTS " .PFX.SMD_THUMB. "; ";
if(gps('debug')) {
dmp($sql);
}
foreach ($sql as $qry) {
$ret = safe_query($qry);
if ($ret===false) {
$GLOBALS['txp_err_count']++;
echo "".$GLOBALS['txp_err_count'].". ".mysql_error()."
\n";
echo "\n";
}
}
}
// Restore the original thumb prefs
$exists = safe_row('*', 'txp_prefs', "name='smd_thumb_backup_w'");
if ($exists) {
set_pref('thumb_w', get_pref('smd_thumb_backup_w'), 'image', PREF_HIDDEN);
set_pref('thumb_h', get_pref('smd_thumb_backup_h'), 'image', PREF_HIDDEN);
set_pref('thumb_crop', get_pref('smd_thumb_backup_c'), 'image', PREF_HIDDEN);
}
// Erase the backup thumb prefs
safe_delete('txp_prefs', "name IN('smd_thumb_backup_w', 'smd_thumb_backup_h', 'smd_thumb_backup_c', 'smd_thumb_default_profile', 'pane_smd_thumbnail_profiles_visible', 'pane_smd_thumbnails_visible')");
}
// ------------------------
function smd_thumb_table_exist($all='') {
if ($all) {
$tbls = array(SMD_THUMB => 5);
$out = count($tbls);
foreach ($tbls as $tbl => $cols) {
if (gps('debug')) {
echo "++ TABLE ".$tbl." HAS ".count(@safe_show('columns', $tbl))." COLUMNS; REQUIRES ".$cols." ++".br;
}
if (count(@safe_show('columns', $tbl)) == $cols) {
$out--;
}
}
return ($out===0) ? 1 : 0;
} else {
if (gps('debug')) {
echo "++ TABLE ".SMD_THUMB." HAS ".count(@safe_show('columns', SMD_THUMB))." COLUMNS;";
}
return(@safe_show('columns', SMD_THUMB));
}
}
// -------------------------------------------------------------
function smd_thumbnail_save_pane_state() {
$panes = array('smd_thumbnail_profiles', 'smd_thumbnails');
$pane = gps('pane');
if (in_array($pane, $panes))
{
set_pref("pane_{$pane}_visible", (gps('visible') == 'true' ? '1' : '0'), 'smd_thumb', PREF_HIDDEN, 'yesnoradio', 0, PREF_PRIVATE);
send_xml_response();
} else {
send_xml_response(array('http-status' => '400 Bad Request'));
}
}
//*****************
// PUBLIC SIDE TAGS
//*****************
// ------------------------
function smd_thumbnail($atts) {
global $thisimage;
extract(lAtts(array(
'type' => @get_pref('smd_thumb_default_profile'),
'id' => '',
'name' => '',
'escape' => 'html',
'wraptag' => '',
'class' => '',
'html_id' => '',
'style' => '',
'link' => '',
'link_rel' => '',
'poplink' => 0,
),$atts));
if ($name) {
$name = doSlash($name);
$rs = safe_row('*', 'txp_image', "name = '$name' limit 1");
} elseif ($id) {
$id = (int) $id;
$rs = safe_row('*', 'txp_image', "id = $id limit 1");
} elseif ($thisimage) {
$id = (int) $thisimage['id'];
$rs = safe_row('*', 'txp_image', "id = $id limit 1");
} else {
trigger_error(gTxt('unknown_image'));
return;
}
if ($rs) {
extract($rs);
$thumbinfo = safe_row('*', SMD_THUMB, "name='".doSlash($type)."'");
if ($thumbinfo) {
$path = IMPATH.sanitizeForUrl($thumbinfo['name']).DS.$id.$ext;
if (file_exists($path)) {
if ($escape == 'html') {
$alt = htmlspecialchars($alt);
$caption = htmlspecialchars($caption);
}
$meta['alt'] = $alt;
if ($caption) { $meta['title'] = $caption; }
if ($html_id && !$wraptag) { $meta['id'] = $html_id; }
if ($class && !$wraptag) { $meta['class'] = $class; } else { $class=$type; }
if ($style) { $meta['style'] = $style; }
$out = smd_thumb_img($thumbinfo, $rs, $meta);
if ($link) {
$out = href($out, imagesrcurl($id, $ext), (!empty($link_rel) ? ' rel="'.$link_rel.'"' : '').' title="'.$caption.'"');
} elseif ($poplink) {
$out = ''.$out.'';
}
return ($wraptag) ? doTag($out, $wraptag, $class, '', $html_id) : $out;
}
}
}
trigger_error(gTxt('unknown_image'));
}
// ------------------------
function smd_if_thumbnail($atts, $thing) {
global $thisimage;
assert_image();
extract(lAtts(array(
'type' => @get_pref('smd_thumb_default_profile'),
),$atts));
$thumbinfo = safe_row('*', SMD_THUMB, "name='".doSlash($type)."'");
$ret = false;
if ($thumbinfo) {
$path = IMPATH.sanitizeForUrl($thumbinfo['name']).DS.$thisimage['id'].$thisimage['ext'];
$ret = file_exists($path);
}
return parse(EvalElse($thing, $ret));
}
// ------------------------
// Plugin-specific replacement strings - localise as required
// TODO: make these into a textpack when plugin composer supports them
function smd_thumb_gTxt($what, $atts = array()) {
$lang = array(
'actions' => 'Actions',
'all_sizes' => 'All sizes',
'all_thumbs' => 'Create all',
'delete_confirm' => 'Really delete profile {name}? It will delete ALL thumbnails of this type.',
'new' => 'New profile',
'prefs_installed' => 'Preferences installed',
'profile_deleted' => 'Profile "{name}" deleted',
'profile_exists' => 'Profile "{name}" already exists',
'profile_heading' => 'Thumbnail profiles',
'quality' => 'Quality (%)',
'sharpen' => 'Sharpen',
'tables_not_installed' => 'Tables not installed: try reinstalling the plugin',
'thumbnail_heading' => 'Thumbnails',
);
return strtr($lang[$what], $atts);
}