/**
 * Compares profile tables of Dolphin and phpBB. Shows result of comparison
 *
 * @return int
 * 		0 - Tables compared successfully, No difference found.
 * 		1 - Tables compared successfully, there have been found profiles that are not in phpBB users table.
 * 		<other> - An error occured while comparing.
 */
function ModuleCompareDatabases()
{
    function debugPrintProfileInfo($ID, $nickname, $email, $registeredEmail, $status)
    {
        global $bottom_result;
        global $site;
        static $ColoredRow = false;
        $ColoredRow = !$ColoredRow;
        $HTMLcode = "<tr bgcolor=\"" . ($ColoredRow ? '#EEEEEE' : '#FFFFFF') . "\" height=\"20\">\r\n\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t<a href=\"{$site['url']}profile_edit.php?ID=" . addslashes($ID) . "\" target=\"_blank\"> [{$ID}] </a>\r\n\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t[" . addslashes(htmlspecialchars($nickname)) . "]\r\n\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t[" . addslashes(htmlspecialchars($email)) . "]\r\n\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t[" . addslashes(htmlspecialchars($registeredEmail)) . "]\r\n\t\t\t\t\t\t</td>\r\n\t\t\t\t\t\t<td>\r\n\t\t\t\t\t\t\t" . addslashes(htmlspecialchars($status)) . "\r\n\t\t\t\t\t\t</td>\r\n\t\t\t\t\t</tr>";
        $bottom_result .= $HTMLcode;
    }
    global $mods;
    global $bottom_result;
    global $site;
    $bottom_result .= '<table>
		<tr>
			<td align="center" colspan="5">List of profiles not included into module database:</td>
		</tr>
		<tr style="font-weight:bold">
			<td>ID</td>
			<td>Password</td>
			<td>Email</td>
			<td>Email of nickname owner</td>
			<td>Status</td>
		</tr>';
    $mysqlLink = mysql_pconnect($mods['phpbb']['Database']['Host'], $mods['phpbb']['Database']['Username'], $mods['phpbb']['Database']['Password']);
    if (!$mysqlLink) {
        modules_err("ModuleCompareDatabases error:\n mysql_pconnect\n\n" . mysql_error($mysqlLink));
    }
    mysql_select_db($mods['phpbb']['Database']['Name'], $mysqlLink) or modules_err("ModuleCompareDatabases error:\n mysql_select_db({$mods['phpbb']['Database']['Name']}\n\n)" . mysql_error($mysqlLink));
    $queryGetAllUsers = "SELECT `username`,\r\n\t\t\t\t\t\t\t\t`user_email`\r\n\t\t\t\t\t\t FROM {$mods['phpbb']['Database']['TablePrefix']}users";
    $dbresult = mysql_query($queryGetAllUsers, $mysqlLink) or modules_err("ModuleCompareDatabases error: mysql_query({$queryGetAllUsers})\n\n" . mysql_error($mysqlLink));
    if (!$dbresult) {
        modules_err("ModuleCompareDatabases() error:\n empty result on query {$queryGetAllUsers} \n\n" . mysql_error($mysqlLink));
    }
    $phpBBUsers = array();
    while ($row = mysql_fetch_assoc($dbresult)) {
        $phpBBUsers[strtolower($row['username'])] = $row['user_email'];
    }
    $dbresult = db_res("SELECT `Status`, `ID`, `NickName`, `Email` FROM `Profiles`");
    $missedProfiles = array();
    while ($profileInfo = mysql_fetch_assoc($dbresult)) {
        if (!array_key_exists(strtolower($profileInfo['NickName']), $phpBBUsers) || $profileInfo['Email'] != $phpBBUsers[strtolower($profileInfo['NickName'])]) {
            $profileInfo['RegisteredEMail'] = $phpBBUsers[strtolower($profileInfo['NickName'])];
            $missedProfiles[] = $profileInfo;
        }
    }
    sort($missedProfiles);
    foreach ($missedProfiles as $profile) {
        debugPrintProfileInfo($profile['ID'], $profile['NickName'], $profile['Email'], $profile['RegisteredEMail'], $profile['Status']);
    }
    $bottom_result .= "</table><br />";
    return count($missedProfiles) > 0 ? 1 : 0;
}
/**
*
*	Synchronyzes _certain_ profiles with modules users set
*/
function synchronizeProfiles($idList, $isAdmin)
{
    global $dir;
    global $PHPBIN;
    $CommandLineArgs = new CCommandLineArgs();
    $CommandLineArgs->AddArgument((int) $isAdmin);
    foreach ($idList as $id) {
        $CommandLineArgs->AddArgument($id);
    }
    if (chdir($dir['root'] . 'modules/')) {
        $scriptReturnValue = 'value was not set';
        exec("{$PHPBIN} -f refresh.php " . $CommandLineArgs->GetCommandLine(), $scriptOutput, $scriptReturnValue);
        if ($scriptReturnValue !== 0) {
            echo "refresh.php returned: <br />\n";
            foreach ($scriptOutput as $outputLine) {
                echo $outputLine . '<br />';
            }
            modules_err("synchronizeProfiles(): exec({$PHPBIN} -f refresh.php " . $CommandLineArgs->GetCommandLine() . ") returned " . $scriptReturnValue);
        }
    } else {
        modules_err("synchronizeProfiles(): chdir({$dir['root']}modules/) returned false");
    }
    /* DEBUG
    	echo "exec({$PHPBIN} -f refresh.php ".$CommandLineArgs->GetCommandLine().")";
    	echo "<br />Return value = ".$scriptReturnValue."<br >";
    	foreach ($scriptOutput as $outputLine)
    	{
    		echo $outputLine.'<br />';
    	}
    	echo '------------------------------------------ <br /><br />';
    	//*/
}