<?php

function smarty_cms_function_qr_image($params, &$smarty) {
	$gCms = cmsms();

	$qr_image = '';
	$start_img = '<img src=';
	$close_img = ' />';
	$width = isset($params['width']) ? $params['width'] : '100';
	$height = isset($params['height']) ? $params['height'] : '100';	
	$data_uri = isset($params['data_uri']) ? $params['data_uri'] : false;
	$path = isset($params['path']) ? $params['path'] : 'qr';

	// get image from google charts api
	$image_url_qr = 'https://chart.googleapis.com/chart?chs=' . $width . 'x' . $height . '&cht=qr&chl=' . $params['url'] . '&choe=UTF-8';
	
	// generate image
	$image_path_qr = $gCms -> config['image_uploads_path'] . '/' . $path . '/';
	$image_filename_qr = md5(serialize($params)) . '.gif';

	if(!file_exists($image_path_qr . $image_filename_qr) || $image_filename_qr == md5(serialize($params))) {
		@mkdir($image_path_qr, 0755, true);

		$image_to_fetch = file_get_contents($image_url_qr);
		$local_image_file = fopen($image_path_qr . $image_filename_qr, 'w');
		chmod($image_path_qr . $image_filename_qr, 0755);
		fwrite($local_image_file, $image_to_fetch);
		fclose($local_image_file);
	}		
			
	// if file exists construct image tag
	if(file_exists($image_path_qr)) {
		
		// if param data_uri is not set show base64 image
		if($data_uri != false) {
			$get_image = $image_path_qr . $image_filename_qr;
			$qr_image = $start_img .= '"data:image/gif;base64,' . base64_encode(file_get_contents($get_image)) . '"';
		// else show gif image
		} else {
			$qr_image = $start_img .= '"' . $gCms -> config['image_uploads_url'] . '/' . $path . '/' . $image_filename_qr . '"';
		}
		
		$qr_image .= ' width="' . $width . '"';
		$qr_image .= ' height="' . $height . '"';

		if(!empty($params['alt'])) {
			$alt = $params['alt'];
		} else {
			$alt = $params['url'];}	
		$qr_image .= ' alt="' . $alt . '"';

		if(!empty($params['title'])) {
			$title = $params['title'];
		} else {
			$title = $params['url'];
		}
		$qr_image .= ' title="' . $title . '"';

		if(!empty($params['class'])) {
			$qr_image .= ' class="' . $params['class'] . '"';
		}
		$qr_image .= $close_img;
		
	} else {

		$qr_image = '<p style="color:#f00">Oh no. Something is wrong.<br />Image was not found.</p>';
	}
	
	if(isset($params['assign'])) {
		$smarty -> assign(trim($params['assign']), $qr_image);
		return ;
	}
	return $qr_image;

}
function smarty_cms_help_function_qr_image() {

?>
<h3>What does this do?</h3>
<p>
	This plugin generates a simple QR Code using <a href="http://code.google.com/intl/en/apis/chart/image/docs/gallery/qr_codes.html" target="_blank">Google Charts API</a>.
	<br />
	QR Codes are popular among smartphone user, where with help of QR Code you are able to easily access or save url specified in that code.
</p>
<h3>How do I use it?</h3>
<p>
	Just insert the tag into your template/page like:
	<code>
		{qr_image url='http://www.domain.com' widht='120' height='120' title='some title' alt='alt text'}
	</code>
	<br />
</p>
<h3>What parameters does it take?</h3>
<p></p>
<ul>
	<li>
		<em> <strong>(required)</strong> </em>
		<tt> url </tt> - specify url to generate QR code from.
		<br />
		If using it with News or CGBlog module you could simply user url=$entry->url for example.
	</li>
	<li>
		<em> <em>(optional)</em> </em>
		<tt> width </tt> - is optional, default setting is 100px.
	</li>
	<li>
		<em>(optional)</em>
		<tt> height </tt> - is optional, default setting is 100px.
	</li>
	<li>
		<em>(optional)</em>
		<tt> path </tt> -  is optional. You can specify a path where QR images should be saved, default is /uploads/images/qr.
		<br />
		This path is relative to /uploads/images.
	</li>
	<li>
		<em>(optional)</em>
		<tt> alt </tt> - alt tag for generated QR image.
	</li>
	<li>
		<em>(optional)</em>
		<tt> title </tt> - title tag for generated QR image.
	</li>
	<li>
		<em>(optional)</em>
		<tt> data_uri </tt> - default is disabled. Setting data_uri='1' will generate a base64 encoded image (usefull if you want to reduce http requests.)
		<br />
		Please note that older browser do not support base64 encoded images.
	</li>
</ul>
<?php
}
function smarty_cms_about_function_qr_image() {
?>
<p>
	Author: Goran Ilic <a href="http://www.ich-mach-das.at" target="_blank">www.ich-mach-das.at</a>
</p>
<h3>Version 1.0</h3>
<h3>Version 1.1</h3>
<ul>
	<li>
		Change filename generation
	</li>
</ul>
<h3>Version 1.2</h3>
<ul>
	<li>
		Fix width and height params, if none was specified, image couldn't be generated.
	</li>
	<li>
		Create dir if path param was specified
	</li>
	<li>Generate new file if params were changed</li>
</ul>
<?php
}
?>