<?php
#FB Like for CMS - CMS Made Simple
#(c)2011 by Oliver Seddon (oliver@threefold.co.uk)
#This project's homepage is: http://dev.cmsmadesimple.org/projects/fblike
#
#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 fblike {
	var $like_domain;
	var $like_width;
	var $like_layout;
	var $like_action;
	var $like_faces;
	var $like_share;
	var $like_scheme;
	var $like_kid;
	var $like_ref;
	  
	function __constructor($like_domain = '', $like_width = '', $like_layout = 'standard', $like_action = 'like', $like_faces = 'false', $like_share = 'false', $like_scheme = 'light', $like_kid = 'false', $like_ref = ''){	
		$this->fblike($like_domain, $like_width, $like_layout, $like_action, $like_faces, $like_share, $like_scheme, $like_kid, $like_ref);
	}
	 
	function fblike($like_domain, $like_width, $like_layout, $like_action, $like_faces, $like_share, $like_scheme, $like_kid, $like_ref) {
		$this->like_domain = $like_domain;
		$this->like_width = $like_width;
		$this->like_layout = $like_layout;
		$this->like_action = $like_action;
		$this->like_faces = $like_faces;
		$this->like_share = $like_share;
		$this->like_scheme = $like_scheme;
		$this->like_kid = $like_kid;
		$this->like_ref = $like_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-like" data-href="'. $like_domain .'" data-width="'. $like_width .'" data-layout="'. $like_layout .'" data-action="'. $like_action .'" data-show-faces="'. $like_faces .'" data-share="'. $like_share .'" data-colorscheme="'. $like_scheme .'" data-kid-directed-site="'. $like_kid .'" data-ref="'. $like_ref .'"></div>';
	}
	
	function showDebugInfo() {
		echo "<ul><li><strong>like_domain:</strong> $this->like_domain</li>\n";
		echo "<ul><li><strong>like_width:</strong> $this->like_width</li>\n";
		echo "<li><strong>like_layout:</strong> $this->like_layout</li>\n";
		echo "<li><strong>like_action:</strong> $this->like_action</li>\n";
		echo "<li><strong>like_faces:</strong> $this->like_faces</li>\n";
		echo "<li><strong>like_share:</strong> $this->like_share</li>\n";
		echo "<li><strong>like_scheme:</strong> $this->like_scheme</li>\n";
		echo "<li><strong>like_kid:</strong> $this->like_kid</li>\n";
		echo "<li><strong>like_ref:</strong> $this->like_ref</li>\n";
		echo "</ul>\n";
	}
}

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


function smarty_cms_function_fblike($params, &$smarty) {
	$gCms = cmsms();
	$config =& $gCms->GetConfig();
	
	$debug = isset($params['debug']) ? $params['debug']:'false';
	$like_domain = full_url_fblike($_SERVER);
	$like_width = isset($params['like_width']) ? $params['like_width']:'';
	$like_layout = isset($params['like_layout']) ? $params['like_layout']:'standard';
	$like_action = isset($params['like_action']) ? $params['like_action']:'like';
	$like_faces = isset($params['like_faces']) ? $params['like_faces']:'false';
	$like_share = isset($params['like_share']) ? $params['like_share']:'false';
	$like_scheme = isset($params['like_scheme']) ? $params['like_scheme']:'light';
	$like_kid = isset($params['like_kid']) ? $params['like_kid']:'false';
	$like_ref = isset($params['like_ref']) ? $params['like_ref']:'';
	
	$fblike = new fblike($like_domain, $like_width, $like_layout, $like_action, $like_faces, $like_share, $like_scheme, $like_kid, $like_ref);
	
	if($debug != 'false') {
		$fblike->showDebuginfo();
	}
}


function smarty_cms_help_function_fblike() {
?>
<h3>What does this do?</h3>
<p>The Like button is the quickest way for people to share content with their friends.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template/page like <code>{fblike}</code>.</p>
<h3>What parameters does it take?</h3>
<ul>
<li><em>(optional)</em> <tt>like_width='100'</tt> - The width of the plugin. The layout you choose affects the minimum and default widths you can use.</li>
<li><em>(optional)</em> <tt>like_layout='standard'</tt> - Selects one of the different layouts that are available for the plugin. Can be one of "standard", "button_count", "button" or "box_count". Default is "standard".</li>
<li><em>(optional)</em> <tt>like_action='like'</tt> - The verb to display on the button. Can be either "like" or "recommend". Default is "like".</li>
<li><em>(optional)</em> <tt>like_faces='false'</tt> - Specifies whether to display profile photos below the button (standard layout only). Default is "false".</li>
<li><em>(optional)</em> <tt>like_share='false'</tt> - Specifies whether to include a share button beside the Like button. Default is "false".</li>
<li><em>(optional)</em> <tt>like_scheme='light'</tt> - The colour scheme used by the plugin for any text outside of the button itself. Can be "light" or "dark". Default is "light".</li>
<li><em>(optional)</em> <tt>like_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. Default is "false".</li>
<li><em>(optional)</em> <tt>like_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_fblike() {
?>
	<p><strong>Author:</strong> Oliver Seddon <a href=\"mailto:oliver@threefold.co.uk\">oliver@threefold.co.uk</a><br /><br /></p>
    <p>Version 1.1<br /><br /></p>
    <p>Change History:<br />
    1.0 - Alpha version released.<br />
	1.1 - Bug fix.</p>
<?php
}
?>