<?php
function sanitizeFormInputs($request_arr)
{
    global $san_arr;
    $san_arr = array();
    foreach ($request_arr as $key => $val) {
        $san_arr[$key] = htmlspecialchars(trim($request_arr[$key]));
    }
}

function getFormField($key)
{
    global $san_arr;
    return isset($san_arr[$key]) ? $san_arr[$key] : "";
}

function chkIsNotEmpty($field_name, $err_tip = "")
{
    global $san_arr;
    global $err_msg;
    $is_ok = is_string($san_arr[$field_name]) ? $san_arr[$field_name] != "" : !empty($san_arr[$field_name]);
    if (!$is_ok) {
        $err_msg .= "<li>" . $err_tip . "</li>";
    }
    return $is_ok;
}

function setPageBlockNames($block_names_arr = array())
{
    global $show_block;
    foreach ($block_names_arr as $block_name) {
        $show_block[$block_name] = false;
    }
}

function setPageBlockShow($val)
{
    global $show_block;
    $show_block[$val] = true;
}

function setAllPageBlocksHide()
{
    global $show_block;
    foreach ($show_block as $key => $val) {
        $show_block[$key] = false;
    }
}

function isShowPageBlock($block_name)
{
    global $show_block;
    return $show_block[$block_name];
}

function connectDB()
{
    global $err_msg;
    global $san_arr;
    if ($rs = @mysql_connect($san_arr['dbhost'], $san_arr['dbuser'], $san_arr['dbpass'])) {
        return $rs;
    }
    $err_msg .= "<li>Could not connect</li>";
    return false;
}

function checkDB()
{
    global $san_arr;
    global $err_msg;
    if (!mysql_select_db($san_arr['dbname'])) {
        $err_msg .= "<li>Database not created, please create the database and press \"Next\"...</li>";
        return false;
    }
}

function queryDB($sql)
{
    global $err_msg;
    global $san_arr;
    if (mysql_query($sql)) {
        return true;
    }
    $err_msg .= "<li>Could not Create DB</li>";
    return false;
}

function parse_mysql_dump($ignoreerrors = false)
{
    global $san_arr;
    global $err_msg;
    global $CFG;
    if (!mysql_select_db($san_arr['dbname'])) {
        $err_msg .= "<li>Database not created, please create the database and press \"Next\"...</li>";
        return false;
    }
    $url = "./install/sql/anova.sql";
    dumpSql($url, $ignoreerrors);
    $CFG['admin']['module']['new']        = true;
    $CFG['admin']['module']['attachment'] = true;
    foreach ($CFG['admin']['module'] as $key => $value) {
        $url = "./install/sql/anova_" . $key . ".sql";
        dumpSql($url, $ignoreerrors);
    }
}

function dumpSql($url, $ignoreerrors)
{
    if (is_file($url)) {
        $file_content = file($url);
        $query        = "";
        foreach ($file_content as $sql_line) {
            $tsl = trim($sql_line);
            if ($sql_line != "" && substr($tsl, 0, 2) != "--" && substr($tsl, 0, 1) != "#") {
                $query .= $sql_line;
                if (preg_match("/;\\s*\$/", $sql_line)) {
                    $result = mysql_query($query);
                    if (!$result && !$ignoreerrors) {
                        exit(mysql_error());
                    }
                    $query = "";
                }
            }
        }
    }
}

function writeDBConfig()
{
    global $san_arr;
    global $err_msg;
    if (is_writable("./common/configs/config_db.inc.php") && ($handle = fopen("./common/configs/config_db.inc.php", "w"))) {
        $str = "<?php\r\n\$CFG['db']['hostname'] = '{$san_arr['dbhost']}';\r\n\$CFG['db']['name'] = '{$san_arr['dbname']}';\r\n\$CFG['db']['username'] = '{$san_arr['dbuser']}';\r\n\$CFG['db']['password'] = '{$san_arr['dbpass']}';\r\n?>";
        fwrite($handle, $str);
        fclose($handle);
    }
}

function setStepsErrorMsg($error_msg)
{
    echo "\t\t<div id=\"selMsgError\">\r\n\t\t\t<p>";
    echo $error_msg;
    echo "</p>\r\n\t\t</div>\r\n\t\t<table>\r\n\t\t\t<tr><td>\r\n\t\t\t\t<form id=\"selFormIndex\" method=\"post\" action=\"";
    echo $_SERVER['PHP_SELF'];
    echo "\">\r\n\t\t\t\t\t<div class=\"clsSubmitLeft\"><div class=\"clsSubmitRight\"><input type=\"submit\" class=\"clsInstalBtn\" name=\"submit_req\" id=\"submit_req\" value=\"Re-Check Now\" /></div></div>\r\n\t\t\t\t</form>\r\n\t\t\t</td></tr>\r\n\t\t</table>\r\n";
}

function WriteConfigEmailApi()
{
    global $san_arr;
    if (is_writable("./common/configs/config_email_api.inc.php") && ($handle = fopen("./common/configs/config_email_api.inc.php", "w"))) {
        $str = "<?php\r\n/**\r\n* @var\t\t\tstring Yahoo Secret Key\r\n* @cfg_key \t\tyahoo_secret\r\n*/\r\n\$CFG['admin']['email_api']['yahoo_secret'] = '{$san_arr['yahoo_secret']}';\r\n/**\r\n* @var\t\t\tstring Yahoo Consumer Key\r\n* @cfg_key \t\tyahoo_consumer\r\n*/\r\n\$CFG['admin']['email_api']['yahoo_consumer'] = '{$san_arr['yahoo_consumer']}';\r\n/**\r\n* @var\t\t\tstring Yahoo Appid\r\n* @cfg_key \t\tyahoo_appid\r\n*/\r\n\$CFG['admin']['email_api']['yahoo_appid'] = '{$san_arr['yahoo_appid']}';\r\n/**\r\n* @var\t\t\tstring Yahoo Appdata\r\n* @cfg_key \t\tyahoo_appdata\r\n*/\r\n\$CFG['admin']['email_api']['yahoo_appdata'] = 'Anova';\r\n?>";
        fwrite($handle, $str);
        fclose($handle);
    }
}

function WriteMSNApplication()
{
    global $san_arr;
    global $CFG;
    if (is_writable("./common/configs/MsnApplicationKey.xml") && ($handle = fopen("./common/configs/MsnApplicationKey.xml", "w"))) {
        $str = "<windowslivelogin>\r\n  <appid>{$san_arr['appid']}</appid>\r\n  <secret>{$san_arr['secret_key']}</secret>\r\n  <securityalgorithm>wsignin1.0</securityalgorithm>\r\n  <policyurl>{$CFG['site']['url']}api.policy.php</policyurl>\r\n  <returnurl>{$CFG['site']['url']}api.emails.php</returnurl>\r\n</windowslivelogin>";
        fwrite($handle, $str);
        fclose($handle);
    }
}

function chkIsDBExists()
{
    global $err_msg;
    global $san_arr;
    if (isset($san_arr['drp']) && mysql_select_db($san_arr['dbname'])) {
        mysql_query("DROP DATABASE " . $san_arr['dbname']);
    }
    if (mysql_select_db($san_arr['dbname'])) {
        $err_msg .= "<li>DB Already exists, if you want to drop the existing db press 'DROP AND CREATE', otherwise change the data base name and press 'Next' ";
        $err_msg .= "<form name=\"form_db_exists\" id=\"selFormDbExists\" method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "\">\r\n\t\t\t\t\t\t  \t<input type=\"hidden\" name=\"drp\" id=\"drp\" value=\"ok\" />\r\n\t\t\t\t\t\t  \t<input type=\"hidden\" name=\"dbhost\" id=\"dbhostdrp\" value=\"" . $san_arr['dbhost'] . "\" />\r\n\t\t\t\t\t\t  \t<input type=\"hidden\" name=\"dbuser\" id=\"dbuserdrp\" value=\"" . $san_arr['dbuser'] . "\" />\r\n\t\t\t\t\t\t  \t<input type=\"hidden\" name=\"dbpass\" id=\"dbpassdrp\" value=\"" . $san_arr['dbpass'] . "\" />\r\n\t\t\t\t\t\t  \t<input type=\"hidden\" name=\"dbname\" id=\"dbnamedrp\" value=\"" . $san_arr['dbname'] . "\" />\r\n\t\t\t\t\t\t  \t<input type=\"submit\" class=\"clsInstalBtn\" name=\"submit_db\" id=\"submit_db_drp\" value=\"DROP AND CREATE\" />\r\n\t\t\t\t\t\t  </form>";
        $err_msg .= "</li>";
        return true;
    }
    return false;
}

function chkIsWritableAll($folder_name = "", $all = false, $all_display = false)
{
    global $err_msg;
    $val = 0;
    if ($all && is_dir($folder_name)) {
        if ($handle = opendir($folder_name)) {
            if ($all_display) {
                while (false !== ($file = readdir($handle))) {
                    if ($file == "." || $file == "..") {
                        continue;
                    }
                    echo "\t\t  \t\t\t\t\t\t\t\t<tr>\r\n\t\t  \t\t\t\t\t\t\t\t\t<td>";
                    echo $folder_name . $file;
                    echo "</td>\r\n\t\t  \t\t\t\t\t\t\t\t\t<td>";
                    echo is_dir($folder_name . $file) ? "Folder" : "File";
                    echo "</td>\r\n\t\t  \t\t\t\t\t\t\t\t\t<td>\r\n";
                    if (!is_writable($folder_name . $file)) {
                        echo "<span class=\"clsRequirementsNotOK\">Error</span>";
                        ++$val;
                    } else {
                        echo "<span class=\"clsRequirementsOK\">Ok</span>";
                    }
                    echo "\t\t  \t\t\t\t\t\t\t\t\t</td>\r\n\t\t  \t\t\t\t\t\t\t\t</tr>\r\n";
                }
            } else {
                while (false !== ($file = readdir($handle))) {
                    if ($file == "." || $file == "..") {
                        continue;
                    }
                    if (!is_writable($folder_name . $file)) {
                        ++$val;
                    }
                }
                echo "\t\t  \t\t\t\t\t\t<tr>\r\n\t\t  \t\t\t\t\t\t\t<td>";
                echo $folder_name;
                echo "</td>\r\n\t\t  \t\t\t\t\t\t\t<td>Folder</td>\r\n\t\t  \t\t\t\t\t\t\t<td>";
                echo $val == 0 ? "<span class=\"clsRequirementsOK\">Ok</span>" : "<span class=\"clsRequirementsNotOK\">Error - files</span>";
                echo "</td>\r\n\t\t  \t\t\t\t\t\t</tr>\r\n";
            }
            closedir($handle);
        }
    } else if (is_dir($folder_name) && !$all) {
        echo "  \t\t\t\t\t\t\t\t<tr>\r\n\t\t\t\t  \t\t\t\t\t<td>";
        echo $folder_name;
        echo "</td>\r\n\t\t\t\t  \t\t\t\t\t<td>Folder</td>\r\n\t\t\t\t  \t\t\t\t\t<td>\r\n";
        if (is_writable($folder_name)) {
            echo "<span class=\"clsRequirementsOK\">Ok</span>";
            $val = 0;
        } else {
            echo "<span class=\"clsRequirementsNotOK\">Error</span>";
            $val = 1;
        }
        echo "\t\t\t\t\t\t\t\t\t</td>\r\n  \t\t\t\t\t\t\t\t</tr>\r\n";
    } else if (!is_dir($folder_name)) {
        echo "\t\t\t\t  \t\t\t\t<tr>\r\n\t\t\t\t  \t\t\t\t\t<td>";
        echo $folder_name;
        echo "</td>\r\n\t\t\t\t  \t\t\t\t\t<td>File</td>\r\n\t\t\t\t  \t\t\t\t\t<td>\r\n";
        if (is_writable($folder_name)) {
            echo "<span class=\"clsRequirementsOK\">Ok</span>";
            $val = 0;
        } else {
            echo "<span class=\"clsRequirementsNotOK\">Error</span>";
            $val = 1;
        }
        echo "\t\t\t\t\t\t\t\t\t</td>\r\n\t\t\t\t  \t\t\t\t</tr>\r\n";
    }
    if (0 < $val) {
        $err_msg = "Error found! Write permission missing for some files/folders";
    }
}

function chkIsForThisProduct($field_value, $prod_id)
{
    global $err_msg;
    $is_ok            = true;
    return $is_ok;
}

$show_block                   = $san_arr = array();
$err_msg                      = $host = "";
$CFG['admin']['product_name'] = "Anova Pro";
if ($CFG['admin']['module']['lite_version']) {
    $CFG['admin']['product_name'] = "Anova";
}
class installHandler extends FormHandler
{
}

?>