<?php
/**
 * File handling the adding new business
 *
 *
 * PHP version 5.0
 *
 * @category	###Oddbodd###
 * @package		###Index###
 * @author 		selvaraj_47ag04
 * @copyright	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id: $
 * @since 		2008-04-18
 */
/**
 * File having common configuration variables required for the entire project
 */
require_once('../common/configs/config.inc.php'); //configurations
require_once('../common/configs/config_email_api.inc.php'); //configurations
$CFG['mods']['include_files'][] = 'common/classes/class_Parser.lib.php';
$CFG['lang']['include_files'][] = 'languages/%s/admin/editApiConfig.php';
$CFG['html']['header'] = 'admin/html_header.php';
$CFG['html']['footer'] = 'admin/html_footer.php';
//compulsory
$CFG['mods']['is_include_only']['html_header'] = false;
$CFG['html']['is_use_header'] = false;
/**
 * To include application top file
 */
require($CFG['site']['project_path'].'common/application_top.inc.php');
//-------------- Class EditApiConfigFormHandler begins --------------->>>>>
class EditApiConfig extends FormHandler
	{
		/**
		 * EditApiConfig::getMsnApiId()
		 * To get msn  details
		 * @return
		 */
		public function getMsnApiId()
			{
				$objDOM = new DOMDocument();
			    $objDOM->load("../common/configs/MsnApplicationKey.xml"); //make sure path is correct


			    $note = $objDOM->getElementsByTagName("windowslivelogin");
			  	foreach( $note as $value )
			  		{
					    $tasks = $value->getElementsByTagName("appid");
					    $task  = $tasks->item(0)->nodeValue;
					    $key = $value->getElementsByTagName("secret");
					    $secret_key = $key->item(0)->nodeValue;
					}
				$this->setFormField('msn_appid',stripcslashes( $task));
				$this->setFormField('secret_key', stripcslashes($secret_key));
			}

		/**
		 * EditApiConfig::setYahooApiDetails()
		 * To set yahoo details
		 * @return void
		 */
		public function setYahooApiDetails()
			{
				$content = '<?php ' . "\n".
						   '/**' . "\n" .
 						   '* @var			string Yahoo Secret Key ' . "\n" .
 						   '* @cfg_key 		yahoo_secret' . "\n" .
                           '*/' . "\n" .
                           '$CFG[\'admin\'][\'email_api\'][\'yahoo_secret\'] = \''.addslashes($this->getFormField('yahoo_secret')). '\';' . "\n" .
                           '/**' . "\n" .
 						   '* @var			string Yahoo Consumer Key ' . "\n" .
 						   '* @cfg_key 		yahoo_consumer' . "\n" .
                           '*/' . "\n" .
                           '$CFG[\'admin\'][\'email_api\'][\'yahoo_consumer\'] = \''.addslashes($this->getFormField('yahoo_consumer')). '\';' . "\n" .
						   '/**' . "\n" .
						   '* @var			string Yahoo Appid' . "\n".
 						   '* @cfg_key 		yahoo_appid' . "\n".
						   '*/' . "\n" .
 						   '$CFG[\'admin\'][\'email_api\'][\'yahoo_appid\'] = \''.addslashes($this->getFormField('yahoo_appid')).'\';' . "\n" .
 						   '/**' . "\n" .
						   '* @var			string Yahoo Appdata' . "\n".
 						   '* @cfg_key 		yahoo_appdata' . "\n".
						   '*/' . "\n" .
						   '$CFG[\'admin\'][\'email_api\'][\'yahoo_appdata\'] = \''.addslashes($this->getFormField('yahoo_appdata')).'\';' . "\n" .
						   '?>';

				$yahoo_file = '../common/configs/config_email_api.inc.php';
				$handler = fopen($yahoo_file,'w');
				fwrite($handler,$content);
				fclose($handler);
			}

		/**
		 * EditApiConfig::setMSNApiDetails()
		 * To set msn api details
		 * @return void
		 */
		public function setMSNApiDetails()
			{
				if ($handle = fopen('../common/configs/MsnApplicationKey.xml', 'w'))
					{
						$str = '<windowslivelogin>
								  <appid>'.addslashes($this->getFormField('msn_appid')).'</appid>
								  <secret>'.addslashes($this->getFormField('secret_key')).'</secret>
									<securityalgorithm>wsignin1.0</securityalgorithm>
									<policyurl>'.$this->CFG['site']['url'].'api.policy.php</policyurl>
									<returnurl>'.$this->CFG['site']['url'].'api.emails.php</returnurl>
								</windowslivelogin>';
						fwrite($handle, $str);
						fclose($handle);
					}
			}
	}
//<<<<<-------------- Class EditApiConfigFormHandler begins ---------------//
//-------------------- Code begins -------------->>>>>//
$EditApiConfig = new EditApiConfig();
$EditApiConfig->setPageBlockNames(array('block_config_form'));
//default form fields and values...
$EditApiConfig->setFormField('yahoo_secret', $CFG['admin']['email_api']['yahoo_secret']);
$EditApiConfig->setFormField('yahoo_appid', $CFG['admin']['email_api']['yahoo_appid']);
$EditApiConfig->setFormField('yahoo_consumer', $CFG['admin']['email_api']['yahoo_consumer']);
$EditApiConfig->setFormField('yahoo_appdata', $CFG['admin']['email_api']['yahoo_appdata']);
$EditApiConfig->setFormField('msn_appid', '');
$EditApiConfig->setFormField('secret_key', '');
$EditApiConfig->getMsnApiId();
$EditApiConfig->setAllPageBlocksHide();
$EditApiConfig->setPageBlockShow('block_config_form'); //default page block. show it. All others hidden
$EditApiConfig->sanitizeFormInputs($_REQUEST);
if (isDemoSite() AND $EditApiConfig->isFormPOSTed($_POST, 'config_submit'))
	{
		$EditApiConfig->setAllPageBlocksHide();
		$EditApiConfig->setCommonErrorMsg($LANG['no_permission_for_demo_user']);
		$EditApiConfig->setPageBlockShow('block_msg_form_error');
		$EditApiConfig->setPageBlockShow('block_config_form');
	}
elseif ($EditApiConfig->isFormPOSTed($_POST, 'config_submit'))
	{
		$EditApiConfig->chkIsNotEmpty('yahoo_secret', $LANG['common_err_tip_compulsory']);
		$EditApiConfig->chkIsNotEmpty('yahoo_consumer', $LANG['common_err_tip_compulsory']);
		$EditApiConfig->chkIsNotEmpty('yahoo_appid', $LANG['common_err_tip_compulsory']);
		$EditApiConfig->chkIsNotEmpty('yahoo_appdata', $LANG['common_err_tip_compulsory']);
		if( $CFG['feature']['msn'] )
			{
				$EditApiConfig->chkIsNotEmpty('msn_appid', $LANG['common_err_tip_compulsory']);
				$EditApiConfig->chkIsNotEmpty('secret_key', $LANG['common_err_tip_compulsory']);
			}
		if($EditApiConfig->isValidFormInputs())
			{
				$EditApiConfig->setYahooApiDetails();
				if( $CFG['feature']['msn'] )
					{
						$EditApiConfig->setMSNApiDetails();
					}
				$EditApiConfig->setAllPageBlocksHide();
				$EditApiConfig->setCommonSuccessMsg($LANG['general_config_success_msg']);
				$EditApiConfig->setPageBlockShow('block_msg_form_success');
				$EditApiConfig->setPageBlockShow('block_config_form');
			}
		else
			{
				$EditApiConfig->setCommonErrorMsg($LANG['common_msg_error_sorry']);
				$EditApiConfig->setPageBlockShow('block_msg_form_error');
				$EditApiConfig->setPageBlockShow('block_config_form');
			}
	}
//<<<<--------------------Code Ends----------------------//
//--------------------Page block templates begins-------------------->>>>>//
//include the header file
$EditApiConfig->includeHeader();
//include the content of the page
setTemplateFolder('admin/');
$smartyObj->display('editApiConfig.tpl');
//includ the footer of the page
//<<<<<<--------------------Page block templates Ends--------------------//
$EditApiConfig->includeFooter();
?>