<?php
#FB Send 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/fbsend
#
#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 fbsend {
	var $send_domain;
	var $send_scheme;
	var $send_kid;
	var $send_ref;
	  
	function __constructor($send_domain = '', $send_scheme = 'light', $send_kid = 'false', $send_ref = ''){	
		$this->fbsend($send_domain, $send_scheme, $send_kid, $send_ref);
	}
	 
	function fbsend($send_domain, $send_scheme, $send_kid, $send_ref) {
		$this->send_domain = $send_domain;
		$this->send_scheme = $send_scheme;
		$this->send_kid = $send_kid;
		$this->send_ref = $send_ref;
	
		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-post" data-href="'. $send_domain .'" data-colorscheme="'. $send_scheme .'" data-kid-directed-site="'. $send_kid .'" data-ref="'. $send_ref .'"></div>';
	}
	
	function showDebugInfo() {
		echo "<ul><li><strong>send_domain:</strong> $this->send_domain</li>\n";
		echo "<ul><li><strong>send_scheme:</strong> $this->send_scheme</li>\n";
		echo "<ul><li><strong>send_kid:</strong> $this->send_kid</li>\n";
		echo "<ul><li><strong>send_ref:</strong> $this->send_ref</li>\n";
		echo "</ul>\n";
	}
}

function url_origin_fbsend($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_fbsend($s, $use_forwarded_host=false) {
    return url_origin_fbsend($s, $use_forwarded_host) . $s['REQUEST_URI'];
}


function smarty_cms_function_fbsend($params, &$smarty) {
	$gCms = cmsms();
	$config =& $gCms->GetConfig();
	
	$debug = isset($params['debug']) ? $params['debug']:'false';
	$send_domain = full_url($_SERVER);
	$send_scheme = isset($params['send_scheme']) ? $params['send_scheme']:'';
	$send_kid = isset($params['send_kid']) ? $params['send_kid']:'';
	$send_ref = isset($params['send_ref']) ? $params['send_ref']:'';
	
	$fbsend = new fbsend($send_domain, $send_scheme, $send_kid, $send_ref);
	
	if($debug != 'false') {
		$fbsend->showDebuginfo();
	}
}


function smarty_cms_help_function_fbsend() {
?>
<h3>What does this do?</h3>
<p>The Send button lets people privately send content on your site to one or more friends in a Facebook message, to an email address, or share it with a Facebook group.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template/page like <code>{fbsend}</code>.</p>
<h3>What parameters does it take?</h3>
<ul>
<li><em>(optional)</em> <tt>send_scheme='light'</tt> - The color scheme used by the plugin. Can be "light" or "dark". Default is "light".</li>
<li><em>(optional)</em> <tt>send_kid='false'</tt> - If your web site or online service, or a portion of your service, is directed to children under 13 you must enable this.</li>
<li><em>(optional)</em> <tt>send_ref=''</tt> - A label for tracking referrals which must be less than 50 characters, and can contain alphanumeric characters and some punctuation (currently +/=-.:_).</li>
<li><em>(optional)</em> <tt>debug='false'</tt> - Enable debug mode. Default is "false".</li>
</ul>

<?php
}

function smarty_cms_about_function_fbsend() {
?>
	<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.</p>
<?php
}
?>