if (@txpinterface == 'admin') { global $smd_thumb_event, $smd_thumb_styles, $smd_thumb_prevs; $smd_thumb_event = 'smd_thumbnail'; $smd_thumb_privs = '1,2,3'; // Plugin privs $smd_thumb_prevs = array('1'); // Privs for prefs $smd_thumb_styles = array( 'edit' => '.smd_selected { border:1px solid red; background:red; } #smd_thumbs img { padding:2px; margin:1px; border:1px solid black; } .thumbnail-alter { display:none; }', 'profile' => '.smd_hidden { display:none; } #smd_thumb_profiles { width:700px; margin:0 auto 18px; position:relative; } #smd_thumb_batch { float:right; } .smd_inactive { opacity:.3; } .smd_thumb_heading_active { cursor:pointer; } #smd_thumb_profile_form { padding-top:8px; } form#smd_thumb_profile_form td { border: 0; } .pref-label { text-align: right!important; } .smd_thumb_btn { position:absolute; right:10px; top:-12px; text-align:center; padding:3px; }', ); add_privs($smd_thumb_event, $smd_thumb_privs); add_privs('smd_thumb_profiles', $smd_thumb_privs); register_callback('smd_thumb_welcome', 'plugin_lifecycle.'.$smd_thumb_event); register_callback('smd_thumb_profiles', 'image_ui', 'extend_controls'); register_callback('smd_thumbs', 'image_ui', 'thumbnail'); register_callback('smd_thumb_edit', 'image_ui', 'thumbnail_edit'); register_callback('smd_thumb_create', 'image_ui', 'thumbnail_create'); register_callback('smd_thumb_generate', 'image_uploaded', 'image'); register_callback('smd_thumb_delete', 'image_deleted', 'image'); register_callback('smd_thumb_create_one', 'image', 'smd_thumb_create_one'); register_callback('smd_thumb_switch_active', 'image', 'smd_thumb_switch_active'); register_callback('smd_thumb_switch_pref', 'image', 'smd_thumb_switch_pref'); register_callback('smd_thumb_insert', 'image', 'smd_thumbnail_insert', 1); } else { global $path_to_site, $img_dir; if (!defined('IMPATH')) { define("IMPATH",$path_to_site.DS.$img_dir.DS); } } if (!defined('SMD_THUMB')) { define("SMD_THUMB", 'smd_thumbnail'); } if (!defined('SMD_THUMB_ACTIVE')) { define("SMD_THUMB_ACTIVE", 1); } if (!defined('SMD_THUMB_CROP')) { define("SMD_THUMB_CROP", 2); } if (!defined('SMD_THUMB_SHARP')) { define("SMD_THUMB_SHARP", 4); } // ------------------------------------------------------------- function smd_thumb_welcome($evt, $stp) { switch ($stp) { case 'installed': smd_thumb_table_install(); // Remove per-user prefs on upgrade from v0.1x to v0.20 safe_delete ('txp_prefs', "name IN ('smd_thumb_txp_create', 'smd_thumb_txp_delete', 'smd_thumb_auto_replace') AND user_name != ''"); break; case 'deleted': smd_thumb_table_remove(); break; } return; } // ------------------------ // Display the designated default thumbnail on the list page function smd_thumbs($evt, $stp, $dflt, $currimg) { extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method'))); if (smd_thumb_table_exist()) { $default = get_pref('smd_thumb_default_profile', '', 1); if ($default) { $row = safe_row('*', SMD_THUMB, "name='".sanitizeForUrl($default)."'"); if ($row) { $edit_url = '?event=image'.a.'step=image_edit'.a.'id='.$currimg['id'].a.'sort='.$sort. a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit; $out = smd_thumb_img($row, $currimg); return ($out) ? href($out, $edit_url) : gTxt('no'); } else { return gTxt('no'); } } else { return gTxt('no'); } } } // ------------------------ // Don't want the 'create' controls as they're handled in the edit portion of the screen function smd_thumb_create($evt, $stp, $dflt, $currimg) { return ' '; } // ------------------------ // With a lot of images in the database, doing it all in one hit would time out. // So it's done via ajax; one request per image function smd_thumb_create_group($type, $lst='') { switch ($type) { case 'all': $where = '1=1'; break; case 'sel': $where = ($lst) ? 'id IN ('.doSlash($lst).')' : '1=0'; break; case 'cat': $where = "category in ('".doSlash($lst)."')"; break; case 'usr': $where = "author in ('".doSlash($lst)."')"; break; } $images = safe_column('id', 'txp_image', $where); $count = count($images); echo ''; } // ------------------------ function smd_thumb_create_one() { $currimg = gps('smd_thumb_imgid'); assert_int($currimg); $rs = safe_rows('*', SMD_THUMB, '1=1 AND flags & '.SMD_THUMB_ACTIVE); $curr = safe_row('*', 'txp_image', "id=".doSlash($currimg)); if ($rs) { smd_thumb_make($rs, $curr, 1); send_xml_response(); } } // ------------------------ function smd_thumb_switch_active() { $name = doSlash(gps('smd_thumb_profile')); if ($name) { safe_update(SMD_THUMB, 'flags = flags ^ '.SMD_THUMB_ACTIVE, "name='$name'"); send_xml_response(array('smd_thumb_profile' => $name)); } } // ------------------------ function smd_thumb_switch_pref() { $name = doSlash(gps('smd_thumb_txptype')); $state = doSlash(gps('smd_thumb_state')); if ($name) { set_pref('smd_thumb_'.$name, $state, 'smd_thumb', PREF_HIDDEN, 'text_input'); send_xml_response(); } } // ------------------------ function smd_thumb_generate($evt, $stp) { $id = doSlash(gps('id')); $id = ($id) ? $id : $GLOBALS['ID']; $rs = safe_rows('*', SMD_THUMB, '1=1 AND flags & '.SMD_THUMB_ACTIVE); smd_thumb_make($rs, $id); } // ------------------------ function smd_thumb_delete($evt, $stp) { $ids = gps('selected'); $rs = safe_rows('*', SMD_THUMB, '1=1'); $images = safe_rows('*', 'txp_image', 'id IN (' . join(',',quote_list($ids)) . ')'); foreach ($images as $img) { smd_thumb_unmake($rs, $img); } } // ------------------------ function smd_thumb_make($rs, $currimg, $force=0) { // Wrapper for wet_thumb to allow multiple thumbnails $msg = ''; if (!class_exists('smd_thumb')) { class smd_thumb extends wet_thumb { var $m_ext; var $m_id; var $m_dir; var $m_dflt; var $width; var $height; var $force; // Constructor function smd_thumb ($id, $dir, $img_row, $is_default=false, $pro_w='', $pro_h='', $force=0) { $id = assert_int($id); if ($img_row) { extract($img_row); $this->m_id = $id; $this->m_ext = $ext; $this->m_dir = $dir; $this->force = $force; $this->width = $pro_w; $this->height = $pro_h; $this->m_dflt = $is_default; } $this->wet_thumb(); // construct base class instance } function write() { if ( !isset($this->m_ext) ) return false; $autor = get_pref('smd_thumb_auto_replace', '0'); $infile = IMPATH.$this->m_id.$this->m_ext; $outfile = IMPATH.$this->m_dir.DS.$this->m_id.$this->m_ext; if (!file_exists($outfile) || $autor || $this->force) { // If this is the default profile and the pref indicates, write a TXP thumb too if (($this->m_dflt == true) && (get_pref('smd_thumb_txp_create', '0'))) { $txp_thumb = IMPATH.$this->m_id.'t'.$this->m_ext; if ( parent::write ( $infile, $txp_thumb ) ) { safe_update('txp_image', "thumbnail = 1, thumb_w = $this->width, thumb_h = $this->height", 'id = '.$this->m_id); @chmod($outfile, 0644); } } if ( parent::write ( $infile, $outfile ) ) { @chmod($outfile, 0644); return true; } } return false; } } } // If passed only an ID, look up the rest of the image data if (!is_array($currimg)) { assert_int($currimg); $currimg = safe_row('*', 'txp_image', 'id='.$currimg); } // Create each thumbnail $pro_dflt = get_pref('smd_thumb_default_profile', ''); foreach ($rs as $row) { // Sanitize a little $width = (int) $row['width']; $height = (int) $row['height']; if ($width == 0) $width = ''; if ($height == 0) $height = ''; if ($width === '' && $height === '') continue; $crop = ($row['flags'] & SMD_THUMB_CROP) ? 1 : 0; $sharpen = ($row['flags'] & SMD_THUMB_SHARP) ? 1 : 0; $id = $currimg['id']; $is_dflt = ($row['name'] == $pro_dflt) ? true : false; $t = new smd_thumb($id, sanitizeForUrl($row['name']), $currimg, $is_dflt, $width, $height, $force); $t->extrapolate = true; // Allow bigger thumbs than original image $t->crop = ($crop == '1'); $t->sharpen = ($sharpen == '1'); $t->hint = '0'; $t->width = $width; $t->height = $height; $t->quality = $row['quality']; if ($t->write()) { $msg = gTxt('thumbnail_saved', array('{id}' => $id)); } else { $msg = array(gTxt('thumbnail_not_saved', array('{id}' => $id)), E_ERROR); } } return $msg; } // ------------------------ function smd_thumb_unmake($rs, $currimg) { $id = $currimg['id']; $ext = $currimg['ext']; $pro_dflt = get_pref('smd_thumb_default_profile', ''); $txp_del = get_pref('smd_thumb_txp_delete', '0'); foreach ($rs as $row) { $path = IMPATH.sanitizeForUrl($row['name']).DS.$id.$ext; if (file_exists($path)) { unlink($path); } // Also remove TXP's built-in thumb? if (($row['name'] == $pro_dflt) && ($txp_del == '1')) { $path = IMPATH.$id.'t'.$ext; if (file_exists($path)) { safe_update('txp_image', "thumbnail = 0, thumb_w = 0, thumb_h = 0", 'id = '.$id); unlink($path); } } } return ''; } // ------------------------ function smd_thumb_insert() { global $txpcfg,$txp_user,$img_dir,$path_to_site, $page, $sort, $dir, $crit, $search_method; if (!defined('IMPATH')) { define('IMPATH',$path_to_site.DS.$img_dir.DS); } extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method'))); include_once txpath.'/lib/txplib_misc.php'; extract($txpcfg); $id = assert_int(gps('id')); $profile = gps('smd_thumb_profile'); $thumb_ext = gps('smd_thumb_ext'); $author = fetch('author', 'txp_image', 'id', $id); if (!has_privs('image.edit') && !($author == $txp_user && has_privs('image.edit.own'))) { return; } $file = $_FILES['thefile']['tmp_name']; $name = $_FILES['thefile']['name']; $file = get_uploaded_file($file); if (empty($file)) { return; } list($w, $h, $extension) = getimagesize($file); $valid_exts = array( IMAGETYPE_GIF => '.gif', IMAGETYPE_JPEG => '.jpg', IMAGETYPE_PNG => '.png', IMAGETYPE_SWF => '.swf', ); $ext = isset($valid_exts[$extension]) ? $valid_exts[$extension] : ''; if (($file !== false) && $profile && $ext) { $newpath = IMPATH.sanitizeForUrl($profile).DS.$id.$ext; if (shift_uploaded_file($file, $newpath) == false) { } else { @chmod($newpath, 0644); // If the pref indicates, duplicate as a TXP thumb too if (get_pref('smd_thumb_txp_create', '0') == 1) { $txp_thumb = IMPATH.$id.'t'.$ext; if ( copy ( $newpath, $txp_thumb ) ) { safe_update('txp_image', "thumbnail = 1, thumb_w = $w, thumb_h = $h", 'id = '.$id); @chmod($txp_thumb, 0644); } } $message = gTxt('image_uploaded', array('{name}' => $name)); } } // Since the headers have been sent, resort to js to refresh the page $url = '?event=image&step=image_edit&id='.$id.'&sort='.$sort.'&dir='.$dir.'&page='.$page.'&search_method='.$search_method.'&crit='.$crit; echo << window.location.href="{$url}"; EOS; exit; } // ------------------------ function smd_thumb_edit($evt, $stp, $dflt, $currimg) { global $step, $file_max_upload_size, $txp_user, $smd_thumb_styles; extract(gpsa(array( 'id', 'page', 'sort', 'dir', 'crit', 'search_method', 'smd_thumbnail_size', 'smd_thumbnail_chosen_size', 'smd_thumbnail_delete', 'smd_step' ))); $id = ($id) ? $id : $GLOBALS['ID']; // Toggle profile panel if ($step == 'save_pane_state') { smd_thumbnail_save_pane_state(); return; } // Create/delete the selected thumbs depending on the button pressed if ($smd_step == 'smd_thumbnail_manage') { // Validate user $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; } // Grab the thumbnails to work on $where = ($smd_thumbnail_size=='all') ? '1=1 AND flags & '.SMD_THUMB_ACTIVE : "name='".doSlash($smd_thumbnail_chosen_size)."'"; $rs = safe_rows('*', SMD_THUMB, $where); // Do it if($smd_thumbnail_delete) { $msg = smd_thumb_unmake($rs, $currimg); } else { $msg = smd_thumb_make($rs, $currimg, 1); } } $ext = $currimg['ext']; echo << function smd_thumb_selector(sel) { var idx = 0; jQuery("#smd_thumbs img").each(function() { if (jQuery(this).hasClass(sel)) { jQuery(this).toggleClass('smd_selected'); if (jQuery(this).hasClass('smd_selected')) { jQuery("#smd_upload_thumbnail").attr('disabled', false); jQuery("#smd_thumb_profile").val(sel); idx = jQuery("#smd_thumbnail_size option[value='"+sel+"']").index(); jQuery("#smd_thumbnail_chosen_size").val(sel); } else { jQuery("#smd_upload_thumbnail").attr('disabled', true); jQuery("#smd_thumb_profile").val(''); jQuery("#smd_thumbnail_chosen_size").val(''); } } else { jQuery(this).removeClass('smd_selected'); } }); jQuery("#smd_thumbnail_size").attr("selectedIndex", idx); } function smd_thumb_select_changed() { obj = jQuery("#smd_thumbnail_size"); if (obj.attr("selectedIndex") == 0) { jQuery("#smd_upload_thumbnail").attr('disabled', true); jQuery("#smd_thumb_profile").val(''); jQuery("#smd_thumbnail_chosen_size").val(''); } else { jQuery("#smd_upload_thumbnail").attr('disabled', false); jQuery("#smd_thumb_profile").val(obj.val()); jQuery("#smd_thumbnail_chosen_size").val(obj.val()); } jQuery("#smd_thumbs img").each(function() { if (jQuery(this).hasClass(obj.val())) { jQuery(this).addClass('smd_selected'); } else { jQuery(this).removeClass('smd_selected'); } }); } jQuery(function() { jQuery("#smd_thumbs img").each(function() { var cls = jQuery(this).attr('class'); jQuery(this).click(function() { smd_thumb_selector(cls); }); }); jQuery("#smd_upload_thumbnail").attr('disabled', true); jQuery(".thumbnail-upload").prepend(''); }); EOC; // Inject styles echo ''; // Add thumbnails and creation controls if (smd_thumb_table_exist()) { $rs = safe_rows('*', SMD_THUMB, '1=1 ORDER BY name'); if ($rs) { $profiles = array('all' => smd_thumb_gTxt('smd_thumb_all_sizes')); $thumbs[] = '
'; 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[] = fInput('hidden', 'smd_step', 'smd_thumbnail_manage'); $out[] = fInput('hidden', 'smd_thumbnail_chosen_size', '', '', '', '', '', '', 'smd_thumbnail_chosen_size'); $out[] = selectInput('smd_thumbnail_size', $profiles, '', '', 'onchange="return smd_thumb_select_changed()";', 'smd_thumbnail_size'); $out[] = fInput('submit', '', gTxt('create'), 'smallerbox'); $out[] = fInput('submit', 'smd_thumbnail_delete', gTxt('delete'), 'smallerbox'); $out[] = join('', $thumbs); $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('"; $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('"; 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[] = '
'.(($rights) ? smd_thumb_gTxt('smd_thumb_profile_preftool_heading') : smd_thumb_gTxt('smd_thumb_profile_tool_heading')).'
'; $out[] = '
'.$pnlbtn.'
'; $out[] = '
'.smd_thumb_gTxt('smd_thumb_batch_legend').''; $out[] = '
'; $out[] = '
'; $out[] = startTable('smd_control'); $out[] = fInput('hidden', 'smd_thumb_selected', '', '', '', '', '', '', 'smd_thumb_selected'); $out[] = eInput($smd_thumb_event); $out[] = sInput('smd_thumb_create_group'); $out[] = tr(td(smd_thumb_gTxt('smd_thumb_batch_preamble').$grpbtn.$grpOpts)); $out[] = endTable(); $out[] = '
'; $out[] = '
'; if ($rights) { $out[] = '
'.smd_thumb_gTxt('smd_thumb_prefs').''; $out[] = '
'; $out[] = startTable('list'); if ($pro_dflt) { $txp_c = get_pref('smd_thumb_txp_create'); $txp_d = get_pref('smd_thumb_txp_delete'); $out[] = tr( tda(smd_thumb_gTxt('smd_thumb_txp_default_sync'), ' class="pref-label"') . tda( ''. smd_thumb_gTxt('smd_thumb_create'). ''. smd_thumb_gTxt('smd_thumb_delete') )); } $txp_ar = get_pref('smd_thumb_auto_replace'); $out[] = tr( tda(smd_thumb_gTxt('smd_thumb_txp_auto_replace'), ' class="pref-label"') . tda( '' )); $out[] = endTable(); $out[] = '
'; } $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[] = '
'.smd_thumb_gTxt('smd_thumb_profile_heading').'
'; $out[] = '
'.$prefbtn.'
'; // Main list of profiles $out[] = '
'; $out[] = startTable('list'); $out[] = $headings; if (smd_thumb_table_exist()) { $rs = safe_rows('*', SMD_THUMB, '1=1 ORDER BY name'); $pro_dflt = get_pref('smd_thumb_default_profile', '', 1); // New Profile row $out[]= ''; $out[] = td(sInput('smd_thumb_profile_save').fInput('text', 'smd_thumb_newname', (($step=='smd_thumb_profile_save') ? $smd_thumb_name : ''), 'smd_focus')) .td(fInput('text', 'smd_thumb_width', (($step=='smd_thumb_profile_save') ? $width : ''), '', '', '', '4')) .td(fInput('text', 'smd_thumb_height', (($step=='smd_thumb_profile_save') ? $height : ''), '', '', '', '4')) .td(fInput('text', 'smd_thumb_quality', (($step=='smd_thumb_profile_save') ? $quality : ''), '', '', '', '4')) .td(checkbox('smd_thumb_crop', '1', (($step=='smd_thumb_profile_save') ? $smd_thumb_crop : 0))) .td(checkbox('smd_thumb_sharpen', '1', (($step=='smd_thumb_profile_save') ? $smd_thumb_sharpen : 0))) .td(checkbox('smd_thumb_active_new', '1', 1)) .td(checkbox('smd_thumb_default', '1', 0)) .td(fInput('submit', 'smd_thumb_add', gTxt('add'), 'smallerbox')); $out[]= ''; // Remaining rows foreach ($rs as $row) { $link_edt = join_qs($qs).'&step=smd_thumb_profile_edit&smd_thumb_name='.$row['name']; $link_del = join_qs($qs).'&step=smd_thumb_profile_delete&smd_thumb_name='.$row['name']; $btnEdt = '[' . gTxt('edit') . ']'; $btnDel = ' $row['name'])).'\');">[' . gTxt('delete') . ']'; $active = ($row['flags'] & SMD_THUMB_ACTIVE) ? 1 : 0; $crop = ($row['flags'] & SMD_THUMB_CROP) ? 1 : 0; $sharpen = ($row['flags'] & SMD_THUMB_SHARP) ? 1 : 0; if ($step == 'smd_thumb_profile_edit' && $row['name'] == $smd_thumb_name) { $btnSave = fInput('submit', 'smd_thumb_save', gTxt('Save'), 'smallerbox'); $btnCancel = fInput('submit', 'smd_thumb_cancel', gTxt('Cancel'), 'smallerbox'); $out[] = tr( td(hInput('smd_thumb_name', $row['name']).fInput('text', 'smd_thumb_newname', $row['name'])) .td(fInput('text', 'smd_thumb_width', $row['width'], '', '', '', '4')) .td(fInput('text', 'smd_thumb_height', $row['height'], '', '', '', '4')) .td(fInput('text', 'smd_thumb_quality', $row['quality'], '', '', '', '4')) .td(checkbox('smd_thumb_crop', '1', $crop)) .td(checkbox('smd_thumb_sharpen', '1', $sharpen)) .td(checkbox('smd_thumb_active', '1', $active), '', $row['name']) .td(checkbox('smd_thumb_default', '1', (($row['name'] == $pro_dflt) ? 1 : 0))) .td($btnSave.$btnCancel) , ' id="smd_thumb_profile_edited"'); } else { $out[] = tr( td(href($row['name'], $link_edt)). td($row['width']). td($row['height']). td($row['quality']). td(($crop) ? gTxt('yes') : gTxt('no')). td(($sharpen) ? gTxt('yes') : gTxt('no')). td('', '', $row['name']). td(($row['name'] == $pro_dflt) ? gTxt('yes') : gTxt('no')). td($btnDel) , ' id="smd_thumb_profile_'.$row['name'].'" class="smd_thumb_row' . (($active) ? '' : ' smd_inactive').'"'); } } } $out[] = endTable(); $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, '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); }