<?php
#FB Share Button for CMS - CMS Made Simple
#(c)2011 by Oliver Seddon (oliver@threefold.co.uk)
#This project's homepage is: http://dev.cmsmadesimple.org/projects/fbshare
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

class fbshare {
	var $share_domain;
	var $share_layout;
	var $share_width;
	  
	function __constructor($share_domain = '', $share_layout = 'icon_link', $share_width = ''){	
		$this->fbshare($share_domain, $share_layout, $share_width);
	}
	 
	function fbshare($share_domain, $share_layout, $share_width) {
		$this->share_domain = $share_domain;
		$this->share_layout = $share_layout;
		$this->share_width = $share_width;
	
		echo '<div id="fb-root"></div>
		<script>
			(function(d, s, id) {
				var js, fjs = d.getElementsByTagName(s)[0];
				if (d.getElementById(id)) return;
				js = d.createElement(s); js.id = id;
				js.src = "//connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.0";
				fjs.parentNode.insertBefore(js, fjs);
			}(document, \'script\', \'facebook-jssdk\'));
		</script>

		<div class="fb-share-button" data-href="'. $share_domain .'" data-layout="'. $share_layout .'" data-width="'. $share_width .'"></div>';
	}
	
	function showDebugInfo() {
		echo "<ul><li><strong>share_domain:</strong> $this->share_domain</li>\n";
		echo "<ul><li><strong>share_layout:</strong> $this->share_layout</li>\n";
		echo "<ul><li><strong>share_width:</strong> $this->share_width</li>\n";
		echo "</ul>\n";
	}
}

function url_origin_fbshare($s, $use_forwarded_host=false) {
    $ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
    $sp = strtolower($s['SERVER_PROTOCOL']);
    $protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
    $port = $s['SERVER_PORT'];
    $port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
    $host = ($use_forwarded_host && isset($s['HTTP_X_FORWARDED_HOST'])) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
    $host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
    return $protocol . '://' . $host;
}
function full_url_fbshare($s, $use_forwarded_host=false) {
    return url_origin_fbshare($s, $use_forwarded_host) . $s['REQUEST_URI'];
}


function smarty_cms_function_fbshare($params, &$smarty) {
	$gCms = cmsms();
	$config =& $gCms->GetConfig();
	
	$debug = isset($params['debug']) ? $params['debug']:'false';
	$share_domain = full_url_fbshare($_SERVER);
	$share_layout = isset($params['share_layout']) ? $params['share_layout']:'';
	$share_width = isset($params['share_width']) ? $params['share_width']:'';
	
	$fbshare = new fbshare($share_domain, $share_layout, $share_width);
	
	if($debug != 'false') {
		$fbshare->showDebuginfo();
	}
}


function smarty_cms_help_function_fbshare() {
?>
<h3>What does this do?</h3>
<p>The Share button lets people add a personalized message to links before sharing on their timeline, in groups, or to their friends via a Facebook Message. If your app is native to iOS or Android, we recommend that you use the native Share Dialog on iOS and Share Dialog on Android instead. We also provide a web based Share Dialog which can also publish Open Graph actions.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template/page like <code>{fbshare}</code>.</p>
<h3>What parameters does it take?</h3>
<ul>
<li><em>(optional)</em> <tt>share_layout='icon_link'</tt> - Selects one of the different layouts that are available for the plugin. Can be one of "box_count", "button_count", "button", "link", "icon_link", or "icon". Default is "icon_link".</li>
<li><em>(optional)</em> <tt>share_width=''</tt> - The width of the plugin. The layout you choose affects whether the width value will have any effect.</li>
<li><em>(optional)</em> <tt>debug='false'</tt> - Enable debug mode. Default is "false".</li>
</ul>

<?php
}

function smarty_cms_about_function_fbshare() {
?>
	<p><strong>Author:</strong> Oliver Seddon <a href=\"mailto:oliver@threefold.co.uk\">oliver@threefold.co.uk</a><br /><br /></p>
    <p>Version 1.0<br /><br /></p>
    <p>Change History:<br />
    1.0 - Alpha version released.<br />
	1.1 - Bug fix.<br />
	1.2 - Bug fix.</p>
<?php
}
?>