<?php

global $lang, $sql_global_link;

$result = run_query("SELECT id, name, calculate FROM " . $config['table_prefix'] . "calc WHERE calculate LIKE '%mysql_%' AND calculate NOT LIKE '%mysql_connect(%' AND calculate NOT LIKE '%mysql_select_db(%' AND name NOT LIKE 'Button %'");
while ($row = sql_fetch_assoc($result)) {
    run_query("UPDATE " . $config["table_prefix"] . "calc SET calculate = '" . form_sql(sql_functions_replace($row['calculate'])) . "' WHERE id=" . $row['id']);
}

$result = run_query("SELECT calc.id id, buttons.name name, calculate FROM " . $config['table_prefix'] . "calc_cond calc_cond, " . $config['table_prefix'] . "calc calc, " . $config['table_prefix'] . "buttons buttons WHERE calc_cond.type='24' and calc_cond.calc_id=calc.id and buttons.id=calc_cond.param AND calculate LIKE '%mysql_%' AND calculate NOT LIKE '%mysql_connect(%' AND calculate NOT LIKE '%mysql_select_db(%'");
while ($row = sql_fetch_assoc($result)) {
    run_query("UPDATE " . $config["table_prefix"] . "calc SET calculate = '" . form_sql(sql_functions_replace($row['calculate'])) . "' WHERE id=" . $row['id']);
}

$result = run_query("SELECT id, description, command FROM " . $config['table_prefix'] . "cron WHERE command LIKE '%mysql_%' AND command NOT LIKE '%mysql_connect(%' AND command NOT LIKE '%mysql_select_db(%'");
while ($row = sql_fetch_assoc($result)) {
    run_query("UPDATE " . $config["table_prefix"] . "cron SET command = '" . form_sql(sql_functions_replace($row['command'])) . "' WHERE id=" . $row['id']);
}

$result = run_query("SELECT id, name, code FROM " . $config['table_prefix'] . "reports WHERE code LIKE '%mysql_%' AND code NOT LIKE '%mysql_connect(%' AND code NOT LIKE '%mysql_select_db(%'");
while ($row = sql_fetch_assoc($result)) {
    run_query("UPDATE " . $config["table_prefix"] . "reports SET code = '" . form_sql(sql_functions_replace($row['code'])) . "' WHERE id=" . $row['id']);
}

$result = run_query("SELECT id, name FROM " . $config['table_prefix'] . "external_files");
while ($row = sql_fetch_assoc($result)) {
    $name = $row['name'];
    $slash = (substr($name, 0, 1) != '/') ? '/' : '';
    if (is_file($config['site_path'] . $slash . $name)) {
        $file = @file_get_contents($config['site_path'] . $slash . $name);
        if ((strripos($name, '.php') !== false) AND (strripos($file, 'mysql_connect(') === false) AND (strripos($file,
                    'mysql_select_db(') === false)
        ) {
            @file_put_contents($config['site_path'] . $slash . $name, sql_functions_replace($file));
        }
    }
}