Example #1
0
function fs_lazy_get_latest_version_info($url, $type, $default_check_interval, &$error, $force_check = false)
{
    $fsdb = fs_get_db_conn();
    if (!$fsdb->is_connected()) {
        return null;
    }
    $version_check_enabled = fs_get_system_option($type . "_version_check_enabled", 'true');
    if ($version_check_enabled != 'false' || $force_check) {
        $last_version_check_time = (int) fs_get_system_option($type . '_last_version_check_time');
        $version_check_interval = (int) fs_get_system_option($type . '_version_check_interval', $default_check_interval);
        $now = time();
        $d = $last_version_check_time + $version_check_interval;
        if ($now > $d || $force_check) {
            // in case of an a freeze due to server networking problem, we don't want subsequent calls to check again.
            // for this reason, I deactivate the version check for the specifed type before testing, and reactivate it after
            // if the script hangs, the user will probably reload and this time it will work because the check
            // will never be re-activated.
            fs_update_system_option($type . "_version_check_enabled", 'false');
            // time to check
            $err = '';
            $info = fs_get_latest_version_info($url, $err);
            if ($err != '') {
                $error = sprintf(fs_r('Error getting version information from %s : %s'), $url, $err);
                return null;
            }
            fs_update_system_option($type . '_last_version_check_time', $now);
            // the fetch was successful, we can re-activate the version check
            fs_update_system_option($type . "_version_check_enabled", 'true');
            // if data is null we have a networking problem. just pretend everything is fine
            // so the server will not continue trying till enough time passed
            if ($info != null) {
                fs_update_system_option($type . '_last_version_info_on_server', serialize($info));
            }
            return $info;
        } else {
            // use cached result from last check
            $info = fs_get_system_option($type . '_last_version_info_on_server');
            if ($info) {
                return unserialize($info);
            } else {
                // if for some reason we have n data in the cache, try again, this time with force_check = true.
                return fs_lazy_get_latest_version_info($url, $type, $error, $default_check_interval, true);
            }
        }
    } else {
        return null;
        // user does not want version check. assume we are good.
    }
}
Example #2
0
function fs_install($upgrade_if_needed = false)
{
    $fp = fopen(__FILE__, "r");
    if (!$fp) {
        return "Error opening lock file";
    }
    if (flock($fp, LOCK_EX + LOCK_NB) == FALSE) {
        fs_e("Operation already in progress");
        return false;
    }
    $fsdb =& fs_get_db_conn();
    if (!isset($fsdb)) {
        die('db object not initialized');
    }
    $res = fs_install_impl($fsdb, $upgrade_if_needed);
    flock($fp, LOCK_UN);
    return $res;
}
Example #3
0
function fs_recalculate_match_bots()
{
    $fsdb =& fs_get_db_conn();
    $useragents = fs_useragents_table();
    $bots = fs_bots_table();
    $res = $fsdb->get_results("SELECT ua.id id,count(wildcard) c\n\t\t\t\t\t\t\t\tFROM {$bots} RIGHT JOIN {$useragents} ua ON useragent \n\t\t\t\t\t\t\t\tREGEXP wildcard GROUP BY useragent");
    if ($res === false) {
        return $fsdb->last_error;
    }
    if (count($res) > 0) {
        foreach ($res as $r) {
            $useragent_id = $r->id;
            $count = $r->c;
            if ($fsdb->query("UPDATE {$useragents} SET match_bots='{$count}' WHERE id='{$useragent_id}'") === false) {
                return $fsdb->last_error;
            }
        }
    }
    return true;
}
Example #4
0
function fs_init_language()
{
    global $FS_LANG;
    global $fs_gettext;
    $fsdb =& fs_get_db_conn();
    $current_lang = null;
    if ($fsdb->is_connected()) {
        $current_lang = fs_get_option('current_language');
    }
    if (empty($current_lang)) {
        $current_lang = FS_DEFAULT_LANG;
    }
    if ($FS_LANG == $current_lang) {
        return;
    }
    $transfile = FS_ABS_PATH . '/i18n/firestats-' . $current_lang . '.po';
    if (file_exists($transfile)) {
        $fs_gettext = new fs_gettext($transfile);
    } else {
        $fs_gettext = new fs_gettext();
    }
    $FS_LANG = $current_lang;
}
Example #5
0
function fs_recalculate_search_engine_terms($value, $chunk = 1000)
{
    require_once FS_ABS_PATH . '/php/db-common.php';
    $fsdb =& fs_get_db_conn();
    $urls = fs_urls_table();
    if ($value == 0) {
        if (false === $fsdb->get_results("UPDATE `{$urls}` SET `search_engine_id` = NULL, `search_terms` = NULL")) {
            return fs_db_error();
        }
    }
    $value = $fsdb->escape($value);
    $res = $fsdb->get_results("SELECT id,url from {$urls} LIMIT {$chunk} OFFSET {$value}");
    if ($res === false) {
        return fs_db_error();
    }
    if (count($res) > 0) {
        foreach ($res as $r) {
            $id = $r->id;
            $ref = $r->url;
            $engine = null;
            $p = array();
            $terms = fs_get_search_terms3($ref, $p, $engine);
            if ($terms !== false && $terms != '') {
                $terms = $fsdb->escape($terms);
                $r2 = $fsdb->query("UPDATE `{$urls}` SET `search_engine_id`='{$engine->id}', `search_terms` = {$terms} WHERE `id` = '{$id}'");
                if ($r2 === false) {
                    return fs_db_error();
                }
            }
        }
    }
    return count($res);
}
Example #6
0
function fs_get_url_id($url)
{
    $fsdb =& fs_get_db_conn();
    $urls = fs_urls_table();
    $url = $fsdb->escape($url);
    $sql = "SELECT `id` FROM `{$urls}` WHERE `md5` = MD5({$url})";
    return $fsdb->get_var($sql);
}
Example #7
0
function fs_delete_user($id)
{
    if (!fs_is_admin()) {
        return "Access denied : fs_delete_user";
    }
    $fsdb =& fs_get_db_conn();
    $users = fs_users_table();
    $id = $fsdb->escape($id);
    $sql = "DELETE FROM `{$users}` WHERE `id`={$id}";
    $r = $fsdb->query($sql);
    if ($r === false) {
        return fs_db_error();
    }
    return true;
}
Example #8
0
/**
 * System information includes:
 * Unique firestats id
 * FireStats version
 * Installation time
 * PHP version
 * MySQL version
 * Server software (apache? IIS? which version?)
 * Memory limit
 * Number of sites monitored
 * Number of sites monitored from each type (how many wordpress blogs, how many drupals etc).
 */
function fs_get_sysinfo()
{
    require_once dirname(__FILE__) . '/db-common.php';
    $s = array();
    $s["FIRESTATS_ID"] = fs_get_system_option('firestats_id');
    $s["FIRESTATS_VERSION"] = FS_VERSION;
    $s["INSTALLATION_TIME"] = fs_get_system_option('first_run_time');
    $s["PHP_VERSION"] = phpversion();
    $s["MYSQL_VERSION"] = fs_mysql_version();
    $s["SERVER_SOFTWARE"] = $_SERVER["SERVER_SOFTWARE"];
    $s["MEMOEY_LIMIT"] = ini_get('memory_limit');
    $sites_table = fs_sites_table();
    $sql = "SELECT type,COUNT(type) c from {$sites_table} GROUP BY type";
    $fsdb =& fs_get_db_conn();
    $res = $fsdb->get_results($sql);
    if ($res === false) {
        return $s;
    }
    $total = 0;
    if (count($res) > 0) {
        foreach ($res as $r) {
            $s["NUM_SITES_{$r->type}"] = $r->c;
            $total += $r->c;
        }
    }
    $s["NUM_SITES"] = $total;
    return $s;
}
Example #9
0
function fs_systest_database_test()
{
    require_once FS_ABS_PATH . '/php/db-common.php';
    $db = fs_get_db_status();
    $errors = array();
    if ($db['status'] == FS_DB_NOT_CONFIGURED) {
        $errors[] = fs_systest_error("fatal", "Database is not configured");
    } else {
        // database is configured, we can do some serious tests.
        $mysql_version = fs_mysql_version();
        if (!fs_mysql_newer_than("4.0.17")) {
            $errors[] = fs_systest_error("fatal", "Your MySQL database version is <b>{$mysql_version}</b>, FireStats requires <b>4.0.17</b> or newer");
        } else {
            if (!fs_mysql_newer_than("4.1.14")) {
                $errors[] = fs_systest_error("warning", "Your MySQL database version is <b>{$mysql_version}</b>, Some features of FireStats reqruires <b>4.1.14</b> or newer");
            }
        }
        if ($db['status'] != FS_DB_VALID && $db['status'] != FS_DB_NOT_CONFIGURED) {
            $errors[] = fs_systest_error("fatal", fs_get_database_status_message($db));
        } else {
            $fsdb =& fs_get_db_conn();
            $tables = fs_get_tables_list();
            $except = array(fs_pending_date_table());
            // don't check this one for InnoDB.
            $res = $fsdb->get_results("SHOW TABLE STATUS");
            if ($res === false) {
                $errors[] = fs_systest_error("fatal", "Error querying database");
            } else {
                $bad_tables = "";
                $found = array();
                foreach ($res as $t) {
                    if (in_array($t->Name, $tables) === true) {
                        $found[$t->Name] = true;
                        if (in_array($t->Name, $except) === false) {
                            if (isset($t->Engine) && $t->Engine != "InnoDB" || isset($t->Type) && $t->Type != "InnoDB") {
                                if ($bad_tables == "") {
                                    $bad_tables .= $t->Name;
                                } else {
                                    $bad_tables .= ", " . $t->Name;
                                }
                            }
                        }
                    }
                }
                foreach ($tables as $t) {
                    if (!(isset($found[$t]) && $found[$t])) {
                        $errors[] = fs_systest_error("fatal", "missing table <b>{$t}</b>");
                    }
                }
                if ($bad_tables != "") {
                    $errors[] = fs_systest_error("fatal", "Some of your tables are not using the InnoDB engine, which is required by FireStats. wierd things may happen <b>({$bad_tables})</b>");
                }
            }
        }
    }
    return $errors;
}
Example #10
0
function fs_rebuild_countries_calc_max()
{
    $fsdb =& fs_get_db_conn();
    $hits = fs_hits_table();
    $count = $fsdb->get_var("SELECT COUNT(DISTINCT(IP)) c FROM `{$hits}`");
    if ($count === null) {
        return fs_db_error();
    } else {
        return $count;
    }
}
Example #11
0
<?php

define('FS_NO_SESSION', true);
require_once dirname(__FILE__) . '/init.php';
require_once dirname(__FILE__) . '/db-hit.php';
if (!defined('FS_COMMIT_MAX_CHUNK_SIZE')) {
    define('FS_COMMIT_MAX_CHUNK_SIZE', 1000);
}
/**
 * This flushed the hits from the pending table to the actual database.
 * this can be seriously optimized.
 */
$fsdb =& fs_get_db_conn();
$pending = fs_pending_date_table();
while (true) {
    $sql = "SELECT COUNT(*) FROM `{$pending}`";
    $c = $fsdb->get_var($sql);
    if ($c === false) {
        die(fs_db_error());
    }
    if ((int) $c === 0) {
        break;
    }
    $sql = "SELECT * FROM `{$pending}` LIMIT 0," . FS_COMMIT_MAX_CHUNK_SIZE;
    $res = $fsdb->get_results($sql);
    if ($res === false) {
        die(fs_db_error());
    }
    foreach ($res as $d) {
        $_SERVER['REMOTE_ADDR'] = $d->ip;
        $_SERVER['HTTP_USER_AGENT'] = $d->useragent;
Example #12
0
function fs_check_database(&$response)
{
    $fsdb =& fs_get_db_conn();
    if (!$fsdb->is_connected()) {
        ajax_error($response, fs_r('Error connecting to database'));
        return false;
    }
    return true;
}
Example #13
0
function fs_db_error($rollback = false)
{
    $fsdb =& fs_get_db_conn();
    $msg = sprintf(fs_r('Database error: %s'), $fsdb->last_error) . '<br/><br/>' . sprintf('SQL Query:<br/>%s', $fsdb->last_query);
    if ($rollback) {
        $fsdb->query("ROLLBACK");
    }
    return $msg;
}
Example #14
0
function fs_debug_rollback()
{
    $fsdb =& fs_get_db_conn();
    $msg = sprintf(fs_r('Database error: %s'), $fsdb->last_error) . '<br/>' . sprintf('SQL: %s', $fsdb->last_query);
    $fsdb->query("ROLLBACK");
    echo $msg;
    return;
}