<?php
(!defined('IN_PowerBB')) ? die() : '';
define('IN_ADMIN',true);
$CALL_SYSTEM				=	array();
$CALL_SYSTEM['GROUP'] 		    = 	true;
$CALL_SYSTEM['MODERATORS'] 	= 	true;

include('common.php');
define('CLASS_NAME','PowerBBBackupMOD');

class PowerBBBackupMOD
{
 var $Info;
	function run()
	{
		global $PowerBB;

		if ($PowerBB->_CONF['member_permission'])
		{

			if ($PowerBB->_CONF['rows']['group_info']['admincp_member'] == '0')
			{
			  $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['error_permission']);
			}

			if ($PowerBB->_GET['backup'])
			{
				if ($PowerBB->_GET['main'])
				{
			       $PowerBB->template->display('header');
					$this->_BackupMain();
				}
				elseif ($PowerBB->_GET['start'])
				{
					$this->_BackupStart();
				}
			}

			$PowerBB->template->display('footer');
        }
	}



	function _BackupMain()
	{
		global $PowerBB;
		$PowerBB->template->assign('filename','backup-'. gmdate('d-m-y').'.sql.gz');
    	$PowerBB->template->display('backup');
	}


	function _BackupStart()
	{
		global $PowerBB;

        /*
		$tables = $PowerBB->_POST['check'];
		$outta = "";
		foreach($tables as $table)
		{
			$query = @mysql_query("SHOW CREATE TABLE ". $table);
			$que = @mysql_fetch_array($query);
			$outta .= $que['Create Table'] . "\r\n\n";
			$query2 = @mysql_query("SELECT * FROM `$que[Table]`");
			while($result = @mysql_fetch_array($query2))
			{
				while($res = current($result))
				{
					$fields[] .= "`".key($result)."`";
					$values[] .= "'$res'";
					next($result);
				}
				$fields = join(", ", $fields);
				$values = join(", ", $values);
				$q = "INSERT INTO `$que[Table]` ($fields) VALUES ($values);";
				$outta .= $q . "\r\n\n";
				unset($fields);
				unset($values);
			}
		}
                $outta = str_replace("CHARSET=utf8", "CHARSET=utf8 AUTO_INCREMENT=1 ; \n\n", $outta);
                $outta = str_replace("AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=2", $outta);
                $outta = str_replace("AUTO_INCREMENT=19 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=19", $outta);
                $outta = str_replace("AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=12", $outta);
                $outta = str_replace("AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=6", $outta);
                $outta = str_replace("AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=9", $outta);
                $outta = str_replace("AUTO_INCREMENT=36 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=36", $outta);
                $outta = str_replace("AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=17", $outta);
                $outta = str_replace("AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=3", $outta);
                $outta = str_replace("AUTO_INCREMENT=169 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=169", $outta);
                $outta = str_replace("AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=7", $outta);
                $outta = str_replace("AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=9", $outta);
                $outta = str_replace("AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=9", $outta);
                $outta = str_replace("AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=9", $outta);
                $outta = str_replace("AUTO_INCREMENT=29 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1", "DEFAULT CHARSET=utf8 AUTO_INCREMENT=29", $outta);
              */

				        require("engine/config.php");
						require_once("includes/iam_backup.php");
						$filename = $config['db']['name'].'-'.$PowerBB->_POST['filename'];
						$backup = new iam_backup($config['db']['server'], $config['db']['name'], $config['db']['username'], $config['db']['password'], true, false, false);
						$backup->perform_backup();
						$conn = @mysql_pconnect($config['db']['server'], $config['db']['username'], $config['db']['password']);
						if(!@mysql_select_db($config['db']['name']))  // If db not set, return 0
						{
						echo "An error has occured. Could not select the MySQL Database";
						}
						$backup = new iam_backup($conn);
                       $backup->perform_backup();
                       $backup = new iam_backup($config['db']['server'], $config['db']['name'], $config['db']['username'], $config['db']['password'], true, false, true, $filename);
                       $backup->perform_backup();
                       $Adressbackup = $PowerBB->functions->GetForumAdress().$filename;
                       	$PowerBB->functions->msg('تم النسخ الاحتياطي لقاعدة البيانات بنجاح على المسار التالي : <br />'.$Adressbackup.'<br /><a href="admin.php?page=backup&amp;backup=1&amp;main=1"><b>عودة</b></a>');
                       	$linkfilename	= 	'<b><a href="'.$PowerBB->functions->GetForumAdress().$filename.'">لحفظ النسخة الإحتياطية على جهازك  أنقر هنا</a></b>';
                       	$PowerBB->functions->msg($linkfilename);


	}


}

?>
