Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2013-10-09 18:23:04

etc
Developer
Registered: 2010-11-11
Posts: 5,064
Website GitHub

Re: Overwrite the thumbnail size with a URL parameter

I would rather do it separately:

if($size === '') $size = array(100, 100);
elseif(strpos($size, 'x') === false) $size = array((int) $size, (int) $size);
else $size = array_map('intval', explode('x', $size));

if($size[0] != $prefs['thumb_w'])
	set_pref('thumb_w', $prefs['thumb_w'] = $size[0], 'image');

if($size[1] != $prefs['thumb_h'])
	set_pref('thumb_h', $prefs['thumb_h'] = $size[1], 'image');

Offline

#14 2013-10-09 18:50:20

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Overwrite the thumbnail size with a URL parameter

etc wrote:

strpos($size, 'x') === false

x can not be the first byte (drop the identical check).

Last edited by Gocom (2013-10-09 18:50:40)

Offline

#15 2013-10-09 19:04:42

etc
Developer
Registered: 2010-11-11
Posts: 5,064
Website GitHub

Re: Overwrite the thumbnail size with a URL parameter

Why not? thumbsize=x10 is as meaningless as thumbsize=10x.

Offline

#16 2013-10-09 19:28:25

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Overwrite the thumbnail size with a URL parameter

etc wrote:

Why not? thumbsize=x10 is as meaningless as thumbsize=10x.

Of course it is… but you can just do !strpos($size, 'x'), or if (strpos($size, 'x')) if you flip the inverted conditions around. If you want the variables to work independently, the easiest way without messy code is two parameters, too.

It being a single parameter, I would rather do what Gil first had. Its much cleaner, readable and gets the job done.

Last edited by Gocom (2013-10-09 19:31:29)

Offline

#17 2013-10-09 19:32:34

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Overwrite the thumbnail size with a URL parameter

THE BLUE DRAGON wrote:

Will adding it like this to the “if” statement will be good?

That works, yes.

Offline

#18 2013-10-09 19:52:46

etc
Developer
Registered: 2010-11-11
Posts: 5,064
Website GitHub

Re: Overwrite the thumbnail size with a URL parameter

Gocom wrote:

It being a single parameter, I would rather do what Gil first had. Its much cleaner, readable and gets the job done.

This would yield two queries (for thumb_w and thumb_h) even if there were only one parameter changed. For the rest, Gil should check if the values are not negative, and so on, but he’s a big boy. :)

Offline

#19 2013-10-10 01:09:38

jstubbs
Moderator
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: Overwrite the thumbnail size with a URL parameter

@Gil – yes please do submit this as a TXP Tip! Thanks!

Offline

#20 2013-10-10 02:56:17

robhert
Member
From: Perú
Registered: 2007-04-27
Posts: 206
Website

Re: Overwrite the thumbnail size with a URL parameter

Hey Gil! Please, share it with us. I’m lost in PHP and plugin composer. ;)

Offline

#21 2013-10-11 22:35:24

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 619
Website

Re: Overwrite the thumbnail size with a URL parameter

OK no tip this time, I made it better and now it’s a plugin with an admin UI, yeay!

Here’s my plugin code, I used the code for the UI from “ebl_image_edit” plugin.
Please check it and let me know if it good and safe to use, if it is then I will publish it.
In case and you find any problem in the code please let me know and if you can please provide a fix code since I don’t know SQL at all and just used a code from an other plugin to make it happen.

Some little explanation and questions about the plugin and it’s structure:
  1. The plugin will work together with “bot_image_upload” plugin, set it load order to 6, to run after it.
  2. The plugin let you set a thumbnail size base on each “bot_image_upload” button field ID, or base on section.
  3. Base on section overwrites the base on field which gives much more control.
  4. The plugin does not modify any code from “bot_image_upload” plugin, so there is no need to worry about it.
  5. Of course you don’t have to use “bot_image_upload” plugin for this, you can simply add the “thumbsize” parameter with a size value in the image tab manually or using an other plugin.
  6. I called it “gil_thumb_size” should I change the prefix to “ggs” or I can use “gil” even that it’s my first name only?
  7. Do you think “thumb_size” is a good name to go with, or you may got anything better to suggest for this one?
  8. I’m including both Jukka and Oleg in the author name together with mine, is it fine with you guys?
  9. The plugin creates a database table name “txp_gil_thumb_size” to save the sizes in it.
  10. There are 3 “register_callback“s:
    • first one is for the gui
    • second for the JS in the write tab to work with “bot_image_upload”
    • and third for settings the thumbnail size in the image tab
if(@txpinterface == 'admin'){
	add_privs('gil_thumb_size', '1');
	register_tab('extensions', 'gil_thumb_size', 'Thumbnail Size');
	register_callback('gil_thumb_size_gui', 'gil_thumb_size');
	register_callback('gil_thumb_size_js','article');
	register_callback('gil_thumb_size_php','image');
}


// Creates the gui
function gil_thumb_size_gui(){
	$step = gps('step');
	$message = (is_callable($step)) ? $step() : '';

	echo pagetop('gil_thumb_size Preferences', $message).n.
		gil_thumb_sizes();
}

// Set database table and gui list
function gil_thumb_sizes(){
	if(!mysql_num_rows(mysql_query("SHOW TABLES LIKE 'txp_gil_thumb_size'"))==1){
		$creatTable = safe_query("CREATE TABLE `".PFX."txp_gil_thumb_size` (".
			"`name` varchar(64) NOT NULL,".
			"`width` varchar(16) NOT NULL,".
			"`height` varchar(16) NOT NULL,".
			"`section` varchar(1) NOT NULL default '0',".
			"UNIQUE KEY `name` (`name`)".
			");");

		// Insert example thumbnail size for article-image field
		$rs = safe_insert('txp_gil_thumb_size', "`name` = 'article-image', `width` = '100',`height` = '100', `section` = '0'");
	}

	// Create H1 and table head
	echo n.hed(gTxt('Thumbnail Sizes'), 1).
		n.n.startTable('list','','txp-list').
		n.'<thead>'.tr(
			n.hCell('Field ID or Section name').
			n.hCell(gTxt('width')).
			n.hCell().
			n.hCell(gTxt('height')).
			n.hCell(gTxt('section')).
			n.hCell(gTxt('delete'))
		).'</thead>';

	// Building the saved sizes as table rows
	$rs = safe_rows_start('*', 'txp_gil_thumb_size', '1=1 ORDER BY `name`');
	$out = '';
	if($rs){
		while($a = nextRow($rs)){ 
			$isChecked = ($a['section'] == 1) ? 'yes' : 'no';
			$out .= n.tr(
				td( htmlspecialchars($a['name'])).
				td( htmlspecialchars($a['width'])).
				td( htmlspecialchars(' X ')).
				td( htmlspecialchars($a['height'])).
				td( $isChecked).
				td( dLink('gil_thumb_size', 'gil_thumb_delete', 'sizename', $a['name']))
			);
		}
	}
	echo form($out);

	// Create a form row for creating new sizes
	echo n.tr(
		form(
			td( fInput('text', 'name', '', 'edit','','',20) ).
			td( fInput('text', 'width', '', 'edit','','',5) ).
			td( htmlspecialchars(' X ')).
			td( fInput('text', 'height', '', 'edit','','',5) ).
			td( '<input name="gts_section" class="checkbox" type="checkbox">').
			td( fInput('submit', 'add', gTxt('add'), 'smallerbox') ).
			n.eInput('gil_thumb_size').
			n.sInput('gil_thumb_save')
		)
	);
	echo n.endTable();
}

// Gets the saves sizes
function gil_thumb_select($section = '0'){
	$rs = safe_rows_start('*', 'txp_gil_thumb_size', 'section='.$section.' ORDER BY `name`');

	if($rs){
		while($a = nextRow($rs)){
			$width	= $a['width'];
			$height	= $a['height'];
			$name 	= $a['name'];
			$out .= "case '".htmlspecialchars($name)."' : thumbnail = '".$width."x".$height."'; break;".n;
		}
	}

	return $out;
}

// Save new sizes
function gil_thumb_save(){
	extract(doSlash(psa(array('name','width','height','gts_section'))));

	if($name && is_numeric($width) && is_numeric($height)){ 
		$gts_section = ($gts_section == "on") ? 1 : 0;

		$rs = @safe_insert('txp_gil_thumb_size', "
			`name`	 = '$name',
			`width`	 = '$width',
			`height` = '$height',
			`section`  = '$gts_section'");

		return ($rs) ?
			'New thumb size created' : 
			'<b>Error:</b> Duplicate Name';
	}else{
		return '<b>Width</b> and <b>Height</b> must be numeric values';
	}

	return FALSE;
}

// Delete sizes
function gil_thumb_delete(){
	$name = ps('sizename');
	return (safe_delete('txp_gil_thumb_size', "`name` = '".doSlash($name)."'")) ? 
		"<b>Deleted</b> $name" : 
		"<b>Error</b> Unable to delete $name"; 
}

// Write tab javascript
function gil_thumb_size_js(){
	$gts_fields = gil_thumb_select(0);
	$gts_sections = gil_thumb_select(1);

	echo "
		<script>
			$(document).ready(function(){

				// Set the 'thumbsize' variable with default thumbnail size value
				var thumbsize = '100x100';

				// Set thumbnail size base on field ID
				$('.bot_add_image').each(function(){
					switch($(this).parents('p').find('input').attr('id')){
".$gts_fields."
					}
					$(this).parents('p').find('.bot_add_image').attr('data-thumbsize',thumbsize);
				});

				// Set thumbnail size base on section name
				// This overwrites the previous settings which were base on field ID
				$('#section').change(function(){
					switch($(this).val()){
".$gts_sections."
					}
					$('.bot_add_image').attr('data-thumbsize',thumbsize);
				}).change();

				// Assign the thumbsize parameter with the value to bot_image_upload iframe
				var check_for_bot_iu_iframe;
				$('.bot_add_image').click(function(){
					check_for_bot_iu_iframe = setInterval(function(){
						if($('#bot_iu_iframe').length > 0){
							$('#bot_iu_iframe').attr('src',$('#bot_iu_iframe').attr('src')+'&thumbsize='+thumbsize);
							clearInterval(check_for_bot_iu_iframe);
						}
					},100);
				});

			});
		</script>
	";
}

// Images tab PHP
function gil_thumb_size_php(){
	global $prefs;
	$defaultSize = '100x100';
	$currentSize = $prefs['thumb_w'].'x'.$prefs['thumb_h'];
	$newSize = gps('thumbsize');

	if(strpos($newSize,'x') && $newSize != $currentSize){
		$newSize = explode('x',$newSize);
		set_pref('thumb_w', (int) $newSize[0], 'image');
		set_pref('thumb_h', (int) $newSize[1], 'image');
	}else if(strpos($defaultSize,'x') && $currentSize != $defaultSize){
		$defaultSize = explode('x',$defaultSize);
		set_pref('thumb_w', (int) $defaultSize[0], 'image');
		set_pref('thumb_h', (int) $defaultSize[1], 'image');
	}
}

Offline

#22 2013-10-12 08:24:51

THE BLUE DRAGON
Member
From: Israel
Registered: 2007-11-16
Posts: 619
Website

Re: Overwrite the thumbnail size with a URL parameter

BTW: the reason I toke the code out of the CLASS it is because I don’t know how to call the save and delete functions from the html buttons in these lines of code:
n.sInput('gil_thumb_save')
td( dLink('gil_thumb_size', 'gil_thumb_delete', 'sizename', $a['name']))
now it works because it is not OO in a CLASS, but if you can help me make it work in a class then I will wrap it all in it again.
Sorry I just don’t know the syntax, even that it’s fine like that without a class like most of the other plugins.

I must say that I have learned a lot, and it is a nice achievement for someone how don’t really know PHP, by using all your help of course.
(a lot of things are equals to JS which makes it more easy for me to understand)

Offline

#23 2013-10-12 11:10:54

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Overwrite the thumbnail size with a URL parameter

Some comments:

$message = (is_callable($step)) ? $step() : '';

Remote code execution vulnerability. Allows calling any function on the server. Step always must be verified using bouncer().

bq. $step = gps('step');

There is no CSRF token check, see Textpattern’s form(), tInput(), form_token() and bouncer() functions. E.g.

$steps = array(
    'save'   => true,
    'delete' => true,
));

if ($step && bouncer($step, $steps))
{
    $message = $this->$step();
}

txp_

txp prefix is reserved by Textpattern core. Use your own, gil etc, instead.

mysql_num_rows(mysql_query(

Should use the DB layer Textpattern comes with. These will break if we stop using the deprecated classic MySQL extension. This also generates an additional notice if the query fails, since mysql_query will return FALSE, but mysql_num_rows expects a resource.

$out .= "case '".htmlspecialchars($name)."' : thumbnail = '".$width."x".$height."'; break;".n;

You have to sanitize the name for JavaScript, while now you are sanitizing it for HTML. Add an single quote to the name and the code breaks. Use escape_js() on variables and wrap the JavaScript block itself to script_js(), or you could just return JSON objects.

$out .=return $out;

References undefined variable. Need to define it before you can reference it. Defining has to happen outside the condition.

SHOW TABLES LIKE 'txp_gil_thumb_size'

This fails if the database uses prefixes. Wrap table names to safe_pfx() when referencing a table in a query.

"`width` varchar(16) NOT NULL," "`height` varchar(16) NOT NULL,". "`section` varchar(1) NOT NULL default '0',".

All of these are integers; field should be integer too.

is_numeric($width) && is_numeric($height)

PHP’s is_numeric() doesn’t work for checking whether a variable is integer-like. You are better off type casting.

THE BLUE DRAGON wrote:

buttons in these lines of code:

You do it when you are calling the step method. E.g.

$this->$step();

Last edited by Gocom (2013-10-12 11:12:37)

Offline

#24 2013-10-12 13:17:18

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,305

Re: Overwrite the thumbnail size with a URL parameter

Reserved gil as your developer prefix, Gil.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

Board footer

Powered by FluxBB