/*
 * Social Creative Core Library 1.5
 * http://www.social-creative.com/
 * Copyright(c) 2009, Social Creative, SARL.
 *
 * See the LICENCE.txt in root directory
 *
 */

/**
* For PHPBB3 mode, please follow these instructions in your Admin panel :
* - Enable the PHP in themes
* - Disable the CAPTCHA registration
* - Disable the mail validation registration
* - Activate users pictures in external site
* - Add these lines in forum root files and "adm" directory root files just AFTER "require($phpbb_root_path . 'includes/functions_module.' . $phpEx);" line :
*     require_once(dirname(__FILE__).'/../engine-php/SocialFusion.php') ;
*     social_fusion_cms () ;
* - If you want the url has no www. before, add these lines BEFORE first line :
*     $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] ;
*     if(substr($url, 7, 4) == 'www.') header('Location: '.substr($url, 0, 7).substr($url, 11, strlen($url)-11)) ;
* - Add these lines in the ucp.php file just BEFORE first line :
*     if($_GET['codeIdent'] != 'a6d8f4azd6azfa68f4a') {
*	      header('Location: index.php') ;
*	      exit ;
*     }
* - Add these lines in the ucp.php file just AFTER "social_fusion_cms () ;" line :
*     if($_GET['mode'] == 'changepassword') {
*	      $fusionPassword = get_fusion_password () ;
*	      $sql_ary = array(
*	          'user_password'		=> phpbb_hash($fusionPassword['password']),
*	          'user_passchg'		=> time(),
*	          'user_pass_convert'	=> 0,
*	      );
*	      $sql = 'UPDATE ' . USERS_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE username = \'' . $fusionPassword['userident'].'\'' ;
*	      $db->sql_query($sql) ;
*	      echo '{success: true}' ;
*	      exit ;
*     }
*     else if ($_GET['mode'] == 'changeavatar') {
*	     $fusionAvatar = get_fusion_avatar() ;
*	     $sql_ary = array(
*		    'user_avatar' => $fusionAvatar['avatar'],
*		    'user_avatar_type' => '2',
*		    'user_avatar_width' => '100',
*		    'user_avatar_height' => '100'
*	     ) ;
*	     $sql = 'UPDATE '.USERS_TABLE.' SET '.$db->sql_build_array('UPDATE', $sql_ary).' WHERE username = \''.$fusionAvatar['userident'].'\'' ;
*	     $db->sql_query($sql) ;
*	     echo '{success: true}' ;
*	     exit ;
*     }
* - In your template files, delete the connection and register buttons
* - Add your url forum in the next socialFusionItems var
*/

var socialFusionItems = [
	// CMS type, local url
	['phpbb3', 'http://treasures-city.com/forum']
] ;

var social_fusion = function () {
	var codeIdent = 'a6d8f4azd6azfa68f4a' ;
	var cms = [] ;
	var connLogin = '' ;
	var connPassword = '' ;
	var connAuto = '' ;
	var returnFunc = '' ;

	for(var i = 0; i < socialFusionItems.length; i++) {
		cms.push([false, socialFusionItems[i][0], socialFusionItems[i][1]]) ;
	}

	this.setLogin = function (params) {
		if(!params) return false ;
		params.login = params.login || '' ;
		params.password = params.password || '' ;

		if(params.login == '' || params.password == '') {
			return false ;
		}

		connLogin = params.login ;
		connPassword = params.password ;
		var auto = params.auto || false ;
		connAuto = (auto ? 'on' : 'off') ;
	} ;

	this.registration = function (params, func) {
		if(!params) return false ;
		returnFunc = func || returnFunc ;
		var canReturn = true ;

		for(var i = 0; i < cms.length; i++) {
			if(!cms[i][0]) {
				cms[i][0] = true ;
				canReturn = false ;
				switch(cms[i][1]) {
					case 'phpbb3' :
						registrationPhpbb3(cms[i][2], params) ;
					break ;
				}

				break ;
			}
		}

		if(canReturn) {
			returnFunc() ;
		}
	} ;

	this.lostpassword = function (func) {
		returnFunc = func || returnFunc ;
		var canReturn = true ;

		for(var i = 0; i < cms.length; i++) {
			if(!cms[i][0]) {
				cms[i][0] = true ;
				canReturn = false ;
				switch(cms[i][1]) {
					case 'phpbb3' :
						lostpasswordPhpbb3(cms[i][2]) ;
					break ;
				}

				break ;
			}
		}

		if(canReturn) {
			returnFunc() ;
		}
	} ;
	
	this.changeavatar = function (func) {
		returnFunc = func || returnFunc ;
		var canReturn = true ;

		for(var i = 0; i < cms.length; i++) {
			if(!cms[i][0]) {
				cms[i][0] = true ;
				canReturn = false ;
				switch(cms[i][1]) {
					case 'phpbb3' :
						changeavatarPhpbb3(cms[i][2]) ;
					break ;
				}

				break ;
			}
		}

		if(canReturn) {
			returnFunc() ;
		}
	} ;

	this.connection = function (func) {
		returnFunc = func || returnFunc ;
		var canReturn = true ;

		for(var i = 0; i < cms.length; i++) {
			if(!cms[i][0]) {
				cms[i][0] = true ;
				canReturn = false ;
				switch(cms[i][1]) {
					case 'phpbb3' :
						connectPhpbb3(cms[i][2]) ;
					break ;
				}

				break ;
			}
		}

		if(canReturn) {
			returnFunc() ;
		}
	} ;

	this.logout = function (func) {
		returnFunc = func || returnFunc ;
		var canReturn = true ;

		for(var i = 0; i < cms.length; i++) {
			if(!cms[i][0]) {
				cms[i][0] = true ;
				canReturn = false ;
				switch(cms[i][1]) {
					case 'phpbb3' :
						logoutPhpbb3(cms[i][2]) ;
					break ;
				}

				break ;
			}
		}

		if(canReturn) {
			returnFunc() ;
		}
	}

	function registrationPhpbb3 (url, params) {
		if(!params) return false ;

		$.post(url + '/ucp.php?mode=register&codeIdent=' + codeIdent, {}, function(response) {
			var form_token = response.match(/(name=\"form_token\" value=\"[a-z0-9]+)\"/gi) ;
			form_token = form_token[0].substr(25, form_token[0].length - 25) ;
			form_token = form_token.substr(0, form_token.length - 1) ;

			var creation_time = response.match(/(name=\"creation_time\" value=\"[0-9]+)\"/gi) ;
			creation_time = creation_time[0].substr(28, creation_time[0].length - 28) ;
			creation_time = creation_time.substr(0, creation_time.length - 1) ;

			$.post(url + '/ucp.php?mode=register&codeIdent=' + codeIdent, {agreed: 'J’accepte ces conditions', creation_time: creation_time, form_token: form_token}, function(response) {
				var form_token = response.match(/(name=\"form_token\" value=\"[a-z0-9]+)\"/gi) ;
				form_token = form_token[0].substr(25, form_token[0].length - 25) ;
				form_token = form_token.substr(0, form_token.length - 1) ;

				var creation_time = response.match(/(name=\"creation_time\" value=\"[0-9]+)\"/gi) ;
				creation_time = creation_time[0].substr(28, creation_time[0].length - 28) ;
				creation_time = creation_time.substr(0, creation_time.length - 1) ;

				var form = {
					username: params.pseudo,
					email: params.mail, email_confirm: params.mail,
					new_password: params.password, password_confirm: params.password,
					lang: USER.LANG, tz: '1', agreed: 'true', change_lang: '0', submit: 'Envoyer', creation_time: creation_time, form_token: form_token
				} ;

				setTimeout(function () {
					$.post(url + '/ucp.php?mode=register&codeIdent=' + codeIdent, form, function(response) {
						socialFusion.registration (params) ;
					}, 'html') ;
				}, 1000) ;
			}, 'html') ;
		}, 'html') ;
	}

	function lostpasswordPhpbb3 (url) {
		$.post(url + '/ucp.php?mode=changepassword&codeIdent=' + codeIdent, function(response) {
			socialFusion.lostpassword () ;
		}, 'html') ;
	}
	
	function changeavatarPhpbb3 (url) {
		$.post(url + '/ucp.php?mode=changeavatar&codeIdent=' + codeIdent, function(response) {
			socialFusion.changeavatar () ;
		}, 'html') ;
	}

	function connectPhpbb3 (url) {
		$.post(url + '/index.php', {}, function(response) {
			var sid = response.match(/(style.php\?sid=[a-z0-9]+)/gi) ;
			sid = sid[0].substr(14, sid[0].length - 14) ;

			$.post(url + '/ucp.php?mode=login&codeIdent=' + codeIdent, { username: connLogin, password: connPassword, redirect: 'index.php', sid: sid, autologin: connAuto, login: 'Connexion' }, function(response) {
				socialFusion.connection () ;
			}, 'html') ;
		}, 'html') ;
	}

	function logoutPhpbb3 (url) {
		$.post(url + '/index.php', {}, function(response) {
			var sid = response.match(/(style.php\?sid=[a-z0-9]+)/gi) ;
			sid = sid[0].substr(14, sid[0].length - 14) ;

			$.post(url + '/ucp.php?mode=logout&sid=' + sid + '&codeIdent=' + codeIdent, { }, function(response) {
				socialFusion.logout () ;
			}, 'html') ;
		}, 'html') ;
	}
} ;
socialFusion = new social_fusion() ;
