<?php
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
global $mainframe;
require_once( $mainframe->getPath( 'class' ) );
// the class name must be the same as the file name without the .php at the end
class cf_cb_registration  {
	//the next 3 fields must be defined for every plugin
	var $result_TITLE = "CB Registration";
	var $result_TOOLTIP = "Synchronize the submitted data with this form with the default CB users table, please fill in the plugin config and assure the enabled field is marked then mark the checkbox here to get the plugin working!";
	var $plugin_name = "cf_cb_registration"; // must be the same as the class name
	var $event = "ONSUBMIT"; // must be defined and in Uppercase, should be ONSUBMIT or ONLOAD
	// the next function must exist and will have the backend config code
	function show_conf($row, $id, $form_id, $option){
	global $mosConfig_live_site, $database;
	$tabs = new mosTabs(1);
	$database->setQuery( "SELECT * FROM #__comprofiler_fields WHERE published='1' AND `table`='#__comprofiler' AND name <>'NA'" );
	$fields = $database->loadObjectList();
	$paramslist = "";
	if(!$row->params){
		foreach($fields as $field){
			$paramslist .= "".$field->name."=\n";
		}
		$paramslist .= "name=
		username=
		email=
		pass=
		vpass=
		emailuser=
		emailadmins=";
		$row->params = $paramslist;
	}
	$paramsvalues = mosParseParams( $row->params );
	?>
	<?php echo "<script  type=\"text/javascript\" "
    . "src=\"$mosConfig_live_site/includes/js/overlib_mini.js\"></script>"; ?>
	<form action="index2.php" method="post" name="adminForm" id="adminForm" class="adminForm">
	<?php
	$tabs->startPane( 'chronoforms2' );
	$tabs->startTab( 'General', 'general2' );
	?>
	<table border="0" cellpadding="3" cellspacing="0">
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "" ); ?></td>
			<td><strong><?php echo "Name Field"; ?>:</strong> </td>
			<td></td>
			<td><input type="text" class="inputbox" size="50" maxlength="50" name="params[name]" value="<?php echo $paramsvalues->name; ?>" /></td>
		</tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "" ); ?></td>
			<td><strong><?php echo "UserName Field"; ?>:</strong> </td>
			<td></td>
			<td><input type="text" class="inputbox" size="50" maxlength="50" name="params[username]" value="<?php echo $paramsvalues->username; ?>" /></td>
		</tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "" ); ?></td>
			<td><strong><?php echo "Email Field"; ?>:</strong> </td>
			<td></td>
			<td><input type="text" class="inputbox" size="50" maxlength="50" name="params[email]" value="<?php echo $paramsvalues->email; ?>" /></td>
		</tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "" ); ?></td>
			<td><strong><?php echo "Password Field"; ?>:</strong> </td>
			<td></td>
			<td><input type="text" class="inputbox" size="50" maxlength="50" name="params[pass]" value="<?php echo $paramsvalues->pass; ?>" /></td>
		</tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "useless till now" ); ?></td>
			<td><strong><?php echo "Verify Password Field"; ?>:</strong> </td>
			<td></td>
			<td><input type="text" class="inputbox" size="50" maxlength="50" name="params[vpass]" value="<?php echo $paramsvalues->vpass; ?>" /></td>
		</tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "" ); ?></td>
			<td><strong><?php echo "Email user ?"; ?>:</strong> </td>
			<td></td>
			<td>
			<select name="params[emailuser]" id="params[emailuser]">
				<option<?php if($paramsvalues->emailuser == 'No'){ echo ' selected';} ?> value="No">No</option>
				<option<?php if($paramsvalues->emailuser == 'Yes'){ echo ' selected';} ?> value="Yes">Yes</option>
			</select>
		</td>
		</tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "" ); ?></td>
			<td><strong><?php echo "Email admins ?"; ?>:</strong> </td>
			<td></td>
			<td>
			<select name="params[emailadmins]" id="params[emailadmins]">
				<option<?php if($paramsvalues->emailadmins == 'No'){ echo ' selected';} ?> value="No">No</option>
				<option<?php if($paramsvalues->emailadmins == 'Yes'){ echo ' selected';} ?> value="Yes">Yes</option>
			</select>
		</td>
		</tr>
		<?php foreach($fields as $field){ $fieldname = $field->name; ?>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( $field->name." field" ); ?></td>
			<td><strong><?php echo $field->name." Field"; ?>:</strong> </td>
			<td></td>
			<td><input type="text" class="inputbox" size="50" maxlength="50" name="params[<?php echo $field->name; ?>]" value="<?php echo $paramsvalues->$fieldname; ?>" /></td>
		</tr>
		<?php } ?>
	</table>
	<?php
			$tabs->endTab();
			$tabs->startTab(  'extra code', 'extracode' );
	?>
	<table border="0" cellpadding="3" cellspacing="0">
		<tr><td colspan="4">Extra code</td></tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "Execute some code just before the plugin code is executed" ); ?></td>
			<td><strong><?php echo "Extra before onsubmit code"; ?>:</strong> </td>
			<td></td>
			<td><textarea name="extra4" rows="10" cols="75"><?php echo $row->extra4; ?></textarea></td>
		</tr>
		<tr style="background-color:#c9c9c9 ">
			<td><?php echo mosToolTip( "Execute some code just after the plugin code is executed" ); ?></td>
			<td><strong><?php echo "Extra after onsubmit code"; ?>:</strong> </td>
			<td></td>
			<td><textarea name="extra5" rows="10" cols="75"><?php echo $row->extra5; ?></textarea></td>
		</tr>
	</table>
	<?php
		$tabs->endTab();
		$tabs->endPane();
	?>
	<input type="hidden" name="id" value="<?php echo $id; ?>" />
	<input type="hidden" name="form_id" value="<?php echo $form_id; ?>" />
	<input type="hidden" name="name" value="<?php echo $this->plugin_name; ?>" />
	<input type="hidden" name="event" value="<?php echo $this->event; ?>" />
	<input type="hidden" name="option" value="<?php echo $option; ?>" />
	<input type="hidden" name="task" value="save_conf" />
	
	</form>
	<?php
	}
	// this function must exist and may not be changed unless you need to customize something
	function save_conf( $option ) {
		global $database;		
		
		$row = new mosChronoContact_Plugins( $database );
		if (!$row->bind( $_POST )) {
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		
		$params = mosGetParam( $_POST, 'params', '' );
		if (is_array( $params )) {
			$txt = array();
			foreach ( $params as $k=>$v) {
				$txt[] = "$k=$v";
			}
			$row->params = implode( "\n", $txt );
		}
		if (!$row->store()) {
			echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
			exit();
		}
		mosRedirect( "index2.php?option=".$option, "Config Saved" );
	}
	
	function onsubmit( $option, $params, $row ) {
		global $database, $acl;
		global $mosConfig_sitename, $mosConfig_live_site, $mosConfig_useractivation, $mosConfig_allowUserRegistration;
		global $mosConfig_mailfrom, $mosConfig_fromname, $mosConfig_mailfrom, $mosConfig_fromname;
	
		if ( $mosConfig_allowUserRegistration == 0 ) {
			mosNotAuth();
			return;
		}
	
		// simple spoof check security
		//josSpoofCheck();
	
		$row = new mosUser( $database );
		
		$row->name		= mosGetParam($_POST, $params->name , '');
		$row->email		= mosGetParam($_POST, $params->email , '');
		$row->username	= mosGetParam($_POST, $params->username , '');
		$row->password	= mosGetParam($_POST, $params->pass , '');
	
		/*if (!$row->bind( $_POST, 'usertype' )) {
			mosErrorAlert( $row->getError() );
		}*/
	
		$row->name		= trim( $row->name );
		$row->email		= trim( $row->email );
		$row->username	= trim( $row->username );
		$row->password	= trim( $row->password );
	
		mosMakeHtmlSafe($row);
	
		$row->id 		= 0;
		$row->usertype 	= '';
		$row->gid 		= $acl->get_group_id( 'Registered', 'ARO' );
	
		if ( $mosConfig_useractivation == 1 ) {
			$row->activation = md5( mosMakePassword() );
			$row->block = '1';
		}
	
		if (!$row->check()) {
			echo "<script> alert('".html_entity_decode($row->getError())."'); window.history.go(-1); </script>\n";
			exit();
		}
	
		$pwd 				= $row->password;
	
		$salt				= mosMakePassword(16);
		$crypt				= md5($row->password.$salt);
		$row->password		= $crypt.':'.$salt;
	
		$row->registerDate 	= date( 'Y-m-d H:i:s' );
	
		if (!$row->store()) {
			echo "<script> alert('".html_entity_decode($row->getError())."'); window.history.go(-1); </script>\n";
			exit();
		}
		$row->checkin();
		
		/********************CB part*************************/
		$database->setQuery( "SELECT * FROM #__comprofiler_fields WHERE published='1' AND `table`='#__comprofiler' AND name <>'NA'" );
		$fields = $database->loadObjectList();
		$fields2 = array('id', 'user_id');
		$fields3 = array();
		foreach($fields as $field){
			$fields2[] = $field->name;
			$fieldname = $field->name;
			$fields3[] = mosGetParam($_POST, $params->$fieldname, '');
		}
		$database->setQuery( "INSERT INTO #__comprofiler (".implode(",",$fields2).") VALUES  ('".$row->id."','".$row->id."','".implode("','",$fields3)."');" );
		if (!$database->query()) {
			echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>";
		}
		/**********************************************/
		$name 		= trim($row->name);
		$email 		= trim($row->email);
		$username 	= trim($row->username);
	
		$subject 	= sprintf (_SEND_SUB, $name, $mosConfig_sitename);
		$subject 	= html_entity_decode($subject, ENT_QUOTES);
	
		if ($mosConfig_useractivation == 1){
			$message = sprintf (_USEND_MSG_ACTIVATE, $name, $mosConfig_sitename, $mosConfig_live_site."/index.php?option=com_registration&task=activate&activation=".$row->activation, $mosConfig_live_site, $username, $pwd);
		} else {
			$message = sprintf (_USEND_MSG, $name, $mosConfig_sitename, $mosConfig_live_site);
		}
	
		$message = html_entity_decode($message, ENT_QUOTES);
	
		// check if Global Config `mailfrom` and `fromname` values exist
		if ($mosConfig_mailfrom != '' && $mosConfig_fromname != '') {
			$adminName2 	= $mosConfig_fromname;
			$adminEmail2 	= $mosConfig_mailfrom;
		} else {
		// use email address and name of first superadmin for use in email sent to user
			$query = "SELECT name, email"
			. "\n FROM #__users"
			. "\n WHERE LOWER( usertype ) = 'superadministrator'"
			. "\n OR LOWER( usertype ) = 'super administrator'"
			;
			$database->setQuery( $query );
			$rows = $database->loadObjectList();
			$row2 			= $rows[0];
	
			$adminName2 	= $row2->name;
			$adminEmail2 	= $row2->email;
		}
		if($params->emailuser == 'Yes'){
		// Send email to user
		mosMail($adminEmail2, $adminName2, $email, $subject, $message);
		}
	
		// Send notification to all administrators
		$subject2 = sprintf (_SEND_SUB, $name, $mosConfig_sitename);
		$message2 = sprintf (_ASEND_MSG, $adminName2, $mosConfig_sitename, $row->name, $email, $username);
		$subject2 = html_entity_decode($subject2, ENT_QUOTES);
		$message2 = html_entity_decode($message2, ENT_QUOTES);
	
		// get email addresses of all admins and superadmins set to recieve system emails
		$query = "SELECT email, sendEmail"
		. "\n FROM #__users"
		. "\n WHERE ( gid = 24 OR gid = 25 )"
		. "\n AND sendEmail = 1"
		. "\n AND block = 0"
		;
		$database->setQuery( $query );
		$admins = $database->loadObjectList();
		if($params->emailadmins == 'Yes'){
			foreach ( $admins as $admin ) {
				// send email to admin & super admin set to recieve system emails
				mosMail($adminEmail2, $adminName2, $admin->email, $subject2, $message2);
			}
		}
		
		if ( $mosConfig_useractivation == 1 ){
			echo _REG_COMPLETE_ACTIVATE;
		} else {
			echo _REG_COMPLETE;
		}
	}

}
?>