<?php
#FB Comments for CMS - CMS Made Simple
#(c)2011 by Oliver Seddon (oliver@threefold.co.uk)
#This project's homepage is: http://dev.cmsmadesimple.org/projects/fbcomments
#
#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 fbcomments {
	var $comment_domain;
	var $comment_scheme;
	var $comment_width;
	var $comment_mobile;
	var $comment_numposts;
	var $comment_orderby;
	  
	function __constructor($comment_domain = '', $comment_scheme = 'light', $comment_width = '550', $comment_mobile = '', $comment_numposts = '10', $comment_orderby = 'social'){	
		$this->fbcomments($comment_domain, $comment_scheme, $comment_width, $comment_mobile, $comment_numposts, $comment_orderby);
	}
	 
	function fbcomments($comment_domain, $comment_scheme, $comment_width, $comment_mobile, $comment_numposts, $comment_orderby) {
		$this->comment_domain = $comment_domain;
		$this->comment_scheme = $comment_scheme;
		$this->comment_width = $comment_width;
		$this->comment_mobile = $comment_mobile;
		$this->comment_numposts = $comment_numposts;
		$this->comment_orderby = $comment_orderby;

		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="'. $comment_domain .'" data-colorscheme="'. $comment_scheme .'" data-width="'. $comment_width .'" data-mobile="'. $comment_mobile .'" data-numposts="'. $comment_numposts .'" data-order-by="'. $comment_orderby .'"></div>';
	}
	
	function showDebugInfo() {
		echo "<ul><li><strong>comment_domain:</strong> $this->comment_domain</li>\n";
		echo "<ul><li><strong>comment_scheme:</strong> $this->comment_scheme</li>\n";
		echo "<ul><li><strong>comment_width:</strong> $this->comment_width</li>\n";
		echo "<ul><li><strong>comment_mobile:</strong> $this->comment_mobile</li>\n";
		echo "<ul><li><strong>comment_numposts:</strong> $this->comment_numposts</li>\n";
		echo "<ul><li><strong>comment_orderby:</strong> $this->comment_orderby</li>\n";
		echo "</ul>\n";
	}
}

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


function smarty_cms_function_fbcomments($params, &$smarty) {
	$gCms = cmsms();
	$config =& $gCms->GetConfig();
	
	$debug = isset($params['debug']) ? $params['debug']:'false';
	$comment_domain = full_url_fbcomments($_SERVER);
	$comment_scheme = isset($params['comment_scheme']) ? $params['comment_scheme']:'light';
	$comment_width = isset($params['comment_width']) ? $params['comment_width']:'550';
	$comment_mobile = isset($params['comment_mobile']) ? $params['comment_mobile']:'';
	$comment_numposts = isset($params['comment_numposts']) ? $params['comment_numposts']:'10';
	$comment_orderby = isset($params['comment_orderby']) ? $params['comment_orderby']:'social';
	
	$fbcomments = new fbcomments($comment_domain, $comment_scheme, $comment_width, $comment_mobile, $comment_numposts, $comment_orderby);
	
	if($debug != 'false') {
		$fbcomments->showDebuginfo();
	}
}


function smarty_cms_help_function_fbcomments() {
?>
<h3>What does this do?</h3>
<p>The Comments box lets people comment on content on your site using their Facebook profile and shows this activity to their friends in news feed. It also contains built-in moderation tools and special social relevance ranking.</p>
<h3>How do I use it?</h3>
<p>Just insert the tag into your template/page like <code>{fbcomments comment_domain='https://www.facebook.com/zuck'}</code>.</p>
<h3>What parameters does it take?</h3>
<ul>
<li><em>(optional)</em> <tt>comment_scheme='light'</tt> - The color scheme used by the plugin. Can be "light" or "dark". Default is "light".</li>
<li><em>(optional)</em> <tt>comment_width='550'</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>comment_mobile=''</tt> - A boolean value that specifies whether to show the mobile-optimized version or not.</li>
<li><em>(optional)</em> <tt>comment_numposts='10'</tt> - The number of comments to show by default. The minimum value is 1. Default is "10".</li>
<li><em>(optional)</em> <tt>comment_orderby='social'</tt> - The order to use when displaying comments. Can be "social", "reverse_time", or "time". Default is "social".</li>
<li><em>(optional)</em> <tt>debug='false'</tt> - Enable debug mode. Default is "false".</li>
</ul>

<?php
}

function smarty_cms_about_function_fbcomments() {
?>
	<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
}
?>