';
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('smd_thumb_thumbnail_heading').'';
$out[] = '';
$out[] = upload_form(smd_thumb_gTxt('smd_thumb_upload'), '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, $prefs;
$dir = sanitizeForUrl($row['name']);
$id = $currimg['id'];
$ext = $currimg['ext'];
$path = IMPATH.$dir.DS.$id.$ext;
if (file_exists($path)) {
$extras = '';
if (isset($meta['forcew']) || isset($meta['forceh'])) {
$dims = getimagesize($path);
if (isset($meta['forcew']) && !$row['width']) {
$row['width'] = $dims[0];
}
if (isset($meta['forceh']) && !$row['height']) {
$row['height'] = $dims[1];
}
unset($meta['forcew']);
unset($meta['forceh']);
}
$w = ($row['width']) ? ' width="'.$row['width'].'"': '';
$h = ($row['height']) ? ' height="'.$row['height'].'"': '';
$uDate = '';
if (!isset($meta['stamp'])) {
$uDate = '?'.filemtime($path);
}
unset($meta['stamp']);
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, $smd_thumb_styles, $smd_thumb_prevs, $txp_user;
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',
'smd_thumb_selected',
'smd_thumb_cat_selected',
'smd_thumb_usr_selected',
'smd_thumb_group_type',
)));
// 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_group') {
switch ($smd_thumb_group_type) {
case 'all':
smd_thumb_create_group('all');
break;
case 'sel':
smd_thumb_create_group('sel', rtrim($smd_thumb_selected, ','));
break;
case 'cat':
smd_thumb_create_group('cat', $smd_thumb_cat_selected);
break;
case 'usr':
smd_thumb_create_group('usr', $smd_thumb_usr_selected);
break;
}
set_pref('pane_smd_thumb_group_type', $smd_thumb_group_type, 'smd_thumb', PREF_HIDDEN, 'text_input', 0, PREF_PRIVATE);
$step = 'smd_thumb_prefs';
}
// 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('smd_thumb_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('smd_thumb_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('smd_thumb_tables_not_installed');
}
$step = 'smd_thumb_profile';
}
// Delete profile
if ($step == 'smd_thumb_profile_delete') {
if (smd_thumb_table_exist()) {
$txp_del = get_pref('smd_thumb_txp_delete');
$the_dflt = get_pref('smd_thumb_default_profile', '', 1);
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);
// Delete all TXP thumbs too?
if (($name == $the_dflt) && ($txp_del == '1')) {
$rs = safe_rows('id, ext', 'txp_image', 'thumbnail = 1');
foreach ($rs as $row) {
$path = IMPATH.$row['id'].'t'.$row['ext'];
if (file_exists($path)) {
unlink($path);
}
}
safe_update('txp_image', 'thumbnail = 0, thumb_w = 0, thumb_h = 0', 'thumbnail = 1');
}
// 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('smd_thumb_profile_deleted', array('{name}' => doSlash($name)));
}
}
}
$step = 'smd_thumb_profile';
}
$qs = array(
"event" => 'image',
"page" => $page,
"sort" => $sort,
"dir" => $dir,
"crit" => $crit,
"search_method" => $search_method,
);
// Inject styles
echo '';
$privs = safe_field('privs', 'txp_users', "name = '".doSlash($txp_user)."'");
$rights = in_array($privs, $smd_thumb_prevs) ? true : false;
if ($step == 'smd_thumb_prefs') {
// ** THUMBNAIL PREFS **
if (smd_thumb_table_exist()) {
$pro_dflt = get_pref('smd_thumb_default_profile', '', 1);
// Subselect lists for type 'cat' and 'usr'
$rs = getTree('root', 'image');
$allCats = $rs ? treeSelectInput('smd_thumb_cat_selected', $rs, '') : '';
$allCats = "
".str_replace(array("\n", '-'), array('', '-'), str_replace('', '<\/', addslashes($allCats)))."<\/span>";
$rs = safe_column('name', 'txp_users', "privs not in(0,6) order by name asc");
$allUsrs = $rs ? selectInput('smd_thumb_usr_selected', $rs, '', true) : '';
$allUsrs = "".str_replace(array("\n", '-'), array('', '-'), str_replace('', '<\/', addslashes($allUsrs)))."<\/span>";
echo <<
function smd_thumb_switch_pref(obj, name) {
var state = (jQuery(obj).attr('checked')) ? 1 : 0;
sendAsyncEvent(
{
event: textpattern.event,
step: 'smd_thumb_switch_pref',
smd_thumb_txptype: name,
smd_thumb_state: state
});
}
function smd_thumb_copy_selected() {
jQuery("#images_form tbody .multi-edit input:checked").each(function() {
jQuery("#smd_thumb_selected").val(jQuery("#smd_thumb_selected").val() + jQuery(this).val() + ',');
});
}
function smd_thumb_subsel(obj) {
obj = jQuery(obj);
dest = jQuery('#smd_thumb_subsel');
item = obj.val();
switch (item) {
case 'all':
case 'sel':
dest.empty();
break;
case 'cat':
dest.empty().prepend('{$allCats}');
break;
case 'usr':
dest.empty().prepend('{$allUsrs}');
break;
}
}
jQuery(function() {
jQuery('#smd_thumb_group_type').change();
});
EOC;
$pnlbtn = ''.smd_thumb_gTxt('smd_thumb_btn_pnl').'';
$grpbtn = '';
$grpTypes = array(
'sel' => smd_thumb_gTxt('smd_thumb_bySel'),
'cat' => smd_thumb_gTxt('smd_thumb_byCat'),
'usr' => smd_thumb_gTxt('smd_thumb_byUsr'),
'all' => smd_thumb_gTxt('smd_thumb_byAll'),
);
$grpOpts = selectInput('smd_thumb_group_type', $grpTypes, get_pref('pane_smd_thumb_group_type', 'all', 1), '', 'onchange="smd_thumb_subsel(this);"', 'smd_thumb_group_type').'';
$out[] = '';
}
} else {
// ** THUMBNAIL PROFILES **
$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('smd_thumb_new').' ';
$prefbtn = ''.(($rights) ? smd_thumb_gTxt('smd_thumb_btn_tools_prefs') : smd_thumb_gTxt('smd_thumb_btn_tools')).'';
$headings = n.''.tr(tdcs($newbtn, 9)).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('smd_thumb_quality'), 'quality', 'image', false).
column_head('keep_square_pixels', 'crop', 'image', false).
column_head(smd_thumb_gTxt('smd_thumb_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('smd_thumb_actions'), 'actions', 'image', false)
).'';
$out[] = '';
$out[] = '
'.$prefbtn.'
';
// Main list of profiles
$out[] = '
';
}
return join('', $out);
}
// ------------------------
// Useful function for other plugins to use to retrieve a list of current profiles
function smd_thumb_get_profiles($active=1, $order='name') {
$allowedOrders = array('name', 'width', 'height','quality');
$order = in_array($order, $allowedOrders) ? $order : 'name';
$where[] = '1=1';
if ($active) {
$where[] = 'flags & '.SMD_THUMB_ACTIVE;
}
$rs = safe_rows('*', SMD_THUMB, join(' AND ', $where) . ' ORDER BY '.$order);
return $rs;
}
// ------------------------
// 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,
'add_stamp' => 0,
'force_size' => '',
),$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 = $thisimage;
} 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;
$force_size = do_list($force_size);
if (in_array('width', $force_size)) {
$meta['forcew'] = 1;
}
if (in_array('height', $force_size)) {
$meta['forceh'] = 1;
}
if ($add_stamp == 0) { $meta['stamp'] = 1; } // Negative logic since we want the stamp on by default for the admin side
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(
'smd_thumb_actions' => 'Actions',
'smd_thumb_all_sizes' => 'All sizes',
'smd_thumb_all_thumbs' => 'Create',
'smd_thumb_batch_legend' => 'Create / recreate thumbnails',
'smd_thumb_batch_preamble' => 'For all active profiles: ',
'smd_thumb_btn_pnl' => 'Profiles',
'smd_thumb_btn_tools_prefs' => 'Tools / Prefs',
'smd_thumb_btn_tools' => 'Tools',
'smd_thumb_byAll' => 'All',
'smd_thumb_byUsr' => 'By author',
'smd_thumb_byCat' => 'By category',
'smd_thumb_bySel' => 'With selected',
'smd_thumb_create' => 'Creation',
'smd_thumb_create_group_confirm' => 'Really create thumbnails for ALL active profiles? Any existing thumbs will be overwritten.',
'smd_thumb_delete' => 'Deletion',
'smd_thumb_delete_confirm' => 'Really delete profile {name}? It will delete ALL thumbnails of this type.',
'smd_thumb_new' => 'New profile',
'smd_thumb_prefs' => 'Preferences',
'smd_thumb_profile_deleted' => 'Profile "{name}" deleted',
'smd_thumb_profile_exists' => 'Profile "{name}" already exists',
'smd_thumb_profile_heading' => 'Thumbnail profiles',
'smd_thumb_profile_preftool_heading' => 'Thumbnail tools and prefs',
'smd_thumb_profile_tool_heading' => 'Thumbnail tools',
'smd_thumb_quality' => 'Quality (%)',
'smd_thumb_sharpen' => 'Sharpen',
'smd_thumb_tables_not_installed' => 'Tables not installed: try reinstalling the plugin',
'smd_thumb_thumbnail_heading' => 'Thumbnails',
'smd_thumb_txp_auto_replace' => 'Recreate thumbnails on re-upload of main image: ',
'smd_thumb_txp_default_sync' => 'Keep TXP thumbnails in sync with default profile on: ',
'smd_thumb_upload' => 'Replace selected thumbnail',
);
return strtr($lang[$what], $atts);
}