Пример #1
0
function fs_install_impl(&$fsdb, $upgrade_if_needed = false)
{
    $db_status_arr = fs_get_db_status($fsdb);
    $db_status = $db_status_arr['status'];
    $db_version = $db_status_arr['ver'];
    $msg = fs_get_database_status_message($db_status_arr);
    if ($db_status == FS_DB_VALID) {
        return true;
    } else {
        if ($db_status == FS_DB_NOT_CONFIGURED || $db_status == FS_DB_IS_NEWER_THAN_CODE) {
            echo $msg;
            return false;
        } else {
            if ($db_status == FS_DB_GENERAL_ERROR || $db_status == FS_DB_CONNECTION_ERROR) {
                echo $msg . " : " . $fsdb->debug();
                return false;
            } else {
                if ($db_status == FS_DB_NOT_INSTALLED) {
                    if (!fs_db_install($fsdb)) {
                        return false;
                    }
                } else {
                    if ($db_status == FS_DB_NEED_UPGRADE && $upgrade_if_needed) {
                        if (!fs_db_upgrade($fsdb, $db_version)) {
                            return false;
                        }
                        fs_do_action("db_upgraded");
                        // after a db upgrade (major releases only) reset donation status for users that didn't donate
                        $donation = fs_get_option('donation_status');
                        if ($donation != 'donated') {
                            fs_update_option('donation_status', '');
                            fs_update_option('last_nag_time', time());
                        }
                    }
                }
            }
        }
    }
    return true;
}
Пример #2
0
function fs_output_database_table()
{
    ?>
<table>
	<?php 
    if (!fs_db_valid()) {
        ?>
	<tr>
		<td colspan='2'>
			<div class="fwrap">
				<?php 
        echo fs_r('Database status') . " : <b style='color:red'>" . fs_get_database_status_message() . "</b>";
        ?>
<br/>

				<?php 
        $st = fs_get_db_status();
        if ($st['status'] == FS_DB_NEED_UPGRADE) {
            ?>
					<div id="database_upgrade_div">
					<span class="notice"><?php 
            fs_e('Click to upgrade');
            ?>
</span>
					<button id="upgrade_db" class="button" onclick="upgradeDatabase()"><?php 
            fs_e('Upgrade');
            ?>
</button>
					</div>
				<?php 
        }
        ?>
			</div>
		</td>
	</tr>
	<?php 
    }
    ?>
</table>

<div id="database_table_config_div">
<table>


	<tr>
		<td colspan="2">
			<div class="fwrap">
			<?php 
    $cfg_source = fs_get_config_source_desc();
    $cfg_source_div = "<div id='config_source'><b>{$cfg_source}</b></div>";
    echo sprintf(fs_r('Configuration source : %s'), $cfg_source_div);
    echo '<div id="switch_to_external_system">';
    if (fs_should_show_use_wp_button()) {
        ?>
					<br/>
					<?php 
        fs_e('To use wordpress database, click this button.');
        ?>
<br/>
					<?php 
        echo '<div class="notice">' . fs_r('FireStats database configuration will be lost.') . '</div>';
        ?>
<br/>
					<button class="button" onclick="useWordpressDB()"><?php 
        fs_e('Use Wordpress database');
        ?>
</button>	
				<?php 
    }
    echo '</div>';
    ?>
			</div> <!-- fwrap -->
		</td>
	</tr>
	<tr>
		<td colspan="2">
			<div class="fwrap">
				<?php 
    fs_e('To perform one of the following actions:');
    ?>
				<ul>
					<li><?php 
    fs_e('Attach to an existing FireStats installation');
    ?>
</li>
					<li><?php 
    fs_e('Install FireStats into an existing database');
    ?>
</li>
					<li><?php 
    fs_e('Create a new database and install FireStats there');
    ?>
</li>
				</ul>
				<?php 
    fs_e('Follow instructions in the help');
    ?>
			</div>	
		</td>
	</tr>
	<tr>
		<?php 
    global $fs_config;
    ?>
		<td><?php 
    fs_e('Database host');
    ?>
</td>
		<td id="holder_database_host">
			<input type="text" size="30" name="text_database_host" id="text_database_host"  onkeypress="return trapEnter(event,'testDBConnection()')" value="<?php 
    print $fs_config['DB_HOST'];
    ?>
"/>
		</td>
	</tr>
	<tr>
		<td><?php 
    fs_e('Database name');
    ?>
</td>
		<td id="holder_database_name">
			<input type="text" size="30" name="text_database_name" id="text_database_name"  onkeypress="return trapEnter(event,'testDBConnection()')" value="<?php 
    print $fs_config['DB_NAME'];
    ?>
"/>
		</td>
	</tr>
	<tr>
		<td><?php 
    fs_e('Database user name');
    ?>
</td>
		<td id="holder_database_user">
			<input type="text" size="30" name="text_database_user" id="text_database_user"  onkeypress="return trapEnter(event,'testDBConnection()')" value="<?php 
    print $fs_config['DB_USER'];
    ?>
"/>
		</td>
	</tr>
	<tr>
		<td><?php 
    fs_e('Database password');
    ?>
</td>
		<td id="holder_database_pass">
			<input type="password" size="30" name="text_database_pass"  onkeypress="return trapEnter(event,'testDBConnection()')" id="text_database_pass" value="<?php 
    // don't send password, too risky
    ?>
"/>
		</td>
	</tr>
	<tr>
		<td><?php 
    fs_e('Tables prefix');
    ?>
</td>
		<td id="holder_database_prefix">
			<input type="text" size="30" name="text_database_prefix" id="text_database_prefix" value="<?php 
    print $fs_config['DB_PREFIX'];
    ?>
"/>
		</td>
	</tr>
</table>

<table>
	<tr>
		<td><button class="button" onclick="testDBConnection()"><?php 
    fs_e('Test connection');
    ?>
</button></td>
		<td><div id="advanced_feedback"></div></td>
	</tr>
</table>

</div>

<div style="display:none" id="install_tables_id" class="fwrap">	
<table>
	<tr>
		<td><?php 
    fs_e('Click to install and switch to new FireStats tables');
    ?>
</td>
	</tr>
	<tr>
		<td><button class="button" onclick="installDBTables()"><?php 
    fs_e('Install tables');
    ?>
</button></td>
	</tr>
</table>
</div>


<div id="use_database_id" style="display:none" class="fwrap">	
<table>
	<tr>
		<td><?php 
    fs_e('Click to use this FireStats database');
    ?>
</td>
	</tr>
	<tr>
		<td>
			<button class="button" onclick="attachToDatabase()"><?php 
    fs_e('Use this database');
    ?>
</button>
		</td>
	</tr>
</table>
</div>


<div id="create_db_id" style="display:none" class="fwrap">
<table>
	<tr>
		<td colspan="2">
			<?php 
    echo fs_r('To create a new database, an administrator user should be used in the above fields.') . '<br/>';
    echo fs_r('However, For security reason, it is recommended not to use a database administrator user for day to day operations.') . '<br/>';
    echo fs_r('Enter new user name and password for FireStats, this user will only have access to the FireStats database') . '<br/>';
    echo fs_r('If you will not specify user and password the Admin user and password will be used.') . '<br/>';
    ?>
		</td>
	</tr>
</table>
<table>
    <tr>
        <td><?php 
    fs_e('FireStats User name');
    ?>
</td>
        <td><input type="text" size="30" id="text_database_firestats_user" value=""/></td>
    </tr>
    <tr>
        <td><?php 
    fs_e('FireStats password');
    ?>
</td>
        <td><input type="password" size="30" id="text_database_firestats_pass" value=""/></td>
    </tr>
</table>
<table>
	<tr>
		<td><button class="button" onclick="createNewDatabase()"><?php 
    fs_e('Create database');
    ?>
</button></td>
		<td><div id="new_db_feedback"></div></td>
	</tr>
</table>
</div>
<?php 
}
Пример #3
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;
}
Пример #4
0
function fs_add_hit_immediate__($user_id, $site_id, $time = null)
{
    if (!fs_db_valid()) {
        return fs_get_database_status_message();
    }
    $fsdb =& fs_get_db_conn();
    $d = fs_get_hit_data($fsdb, $user_id, $site_id);
    $user_id = $d->user_id;
    $site_id = $d->site_id;
    $remoteaddr = $d->remoteaddr;
    $useragent = $d->useragent;
    $url = $d->url;
    $referer = $d->referer;
    if ($time === null) {
        $time = "NOW()";
    } else {
        $time = $fsdb->escape($time);
    }
    $useragents = fs_useragents_table();
    $urls = fs_urls_table();
    $excluded_ips = fs_excluded_ips_table();
    if ($fsdb->query("START TRANSACTION") === false) {
        return fs_debug_rollback();
    }
    // insert to user agent table (no duplicates)
    $ret = $fsdb->query("INSERT IGNORE INTO `{$useragents}` (`useragent`,`md5`) VALUES ({$useragent} ,MD5(`useragent`))");
    if ($ret === false) {
        return fs_debug_rollback();
    }
    // if we actually inserted a new useragent, we need to match it against existing filters.
    if ($ret > 0) {
        $bots = fs_bots_table();
        $ret = $fsdb->get_row("SELECT ua.id id,count(wildcard) c\n\t\t\t\tFROM {$bots} RIGHT JOIN {$useragents} ua \n\t\t\t\tON useragent REGEXP wildcard \n\t\t\t\tWHERE useragent = {$useragent}\n\t\t\t\tGROUP BY useragent");
        if ($ret === false) {
            return fs_debug_rollback();
        }
        $ret = $fsdb->query("UPDATE {$useragents} SET match_bots='{$ret->c}' WHERE id='{$ret->id}'");
        if ($ret === false) {
            return fs_debug_rollback();
        }
    }
    $save_excluded = fs_get_save_excluded_records() === 'true';
    $c = $fsdb->get_var("SELECT COUNT(ip) FROM `{$excluded_ips}` WHERE `ip` = " . $remoteaddr);
    if ($c === false) {
        return fs_debug_rollback();
    }
    $c = (int) $c;
    $excluded_ip = $c > 0 ? 1 : 0;
    $excluded_users = fs_get_local_option('firestats_excluded_users');
    if ($excluded_users === false) {
        return fs_debug_rollback();
    }
    $excluded_user = $user_id && $excluded_users && in_array($user_id, explode(",", $excluded_users)) ? 1 : 0;
    // get index of useragent in table, can't use LAST_INSERT_ID() here because of the no-dups policy
    $ua_info = $fsdb->get_row("SELECT id,match_bots from `{$useragents}` WHERE `useragent` = {$useragent}");
    $excluded_useragent = $ua_info->match_bots > 0;
    // check if we want to save this
    if (!$save_excluded && ($excluded_useragent || $excluded_user || $excluded_ip)) {
        return true;
    }
    $useragent_id = $ua_info->id;
    if ($useragent_id === false) {
        return fs_debug_rollback();
    }
    // insert to urls table (no duplicates)
    $url = $url ? "{$url}" : "''";
    if ($fsdb->query("INSERT IGNORE INTO `{$urls}` (`url`,`md5`,`add_time`,`host`) \n\t\t\t\t\t VALUES ({$url},MD5(url),{$time},substring_index(substring_index(`url`,'/',3),'/',-1))") === false) {
        return fs_debug_rollback();
    }
    // get index of url in table, can't use LAST_INSERT_ID() here because of the no-dups policy
    $url_id = $fsdb->get_var("SELECT id from " . fs_urls_table() . " WHERE `url` = {$url}");
    if ($url_id === false) {
        return fs_debug_rollback();
    }
    if ($url_id == null) {
        return fs_debug_rollback();
    }
    // update site id of url to current site id.
    // this is only done for the url and not for the referrer:
    // we don't know the site id of the referrer. if it will appear as a url it will be assigned the site_id.
    if (false === $fsdb->get_var("UPDATE `{$urls}` SET `site_id` = {$site_id} WHERE `id` = {$url_id}")) {
        return fs_debug_rollback();
    }
    // insert referers into urls table (no duplicates)
    $referer = $referer ? "{$referer}" : "''";
    require_once FS_ABS_PATH . '/php/searchengines.php';
    $search_engine_id = "NULL";
    $search_terms = "NULL";
    $referrer_breakdown = null;
    if (isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])) {
        $search_params = fs_get_search_terms_and_engine($_SERVER['HTTP_REFERER'], $referrer_breakdown);
        if ($search_params) {
            $id = $search_params->engine_id;
            $terms = $search_params->search_terms;
            if (!empty($id)) {
                $search_engine_id = $fsdb->escape($id);
            }
            if (!empty($terms)) {
                $search_terms = $fsdb->escape($terms);
            }
        }
    }
    $has_scheme = isset($referrer_breakdown['scheme']);
    $optional_host = $has_scheme ? ",`host`" : "";
    $optional_host_query = $has_scheme ? ",substring_index(substring_index(`url`,'/',3),'/',-1)" : "";
    if ($fsdb->query("INSERT IGNORE INTO `{$urls}`(`url`,`md5`,`add_time`,`search_engine_id`,`search_terms` {$optional_host}) VALUES ({$referer},MD5(url),{$time},{$search_engine_id} ,{$search_terms} {$optional_host_query})") === false) {
        return fs_debug_rollback();
    }
    // get index of url in table, can't use LAST_INSERT_ID() here because of the no-dups policy
    $referer_id = $fsdb->get_var("SELECT id from {$urls} WHERE `url` = {$referer}");
    if ($referer_id === false) {
        return fs_debug_rollback();
    }
    if ($referer_id == null) {
        echo "FireStats : Error getting referrer id  ";
        return fs_debug_rollback();
    }
    require_once dirname(__FILE__) . '/ip2country.php';
    $ip2c_res = fs_ip2c($d->ip_address, true);
    $ccode = $ip2c_res ? $fsdb->escape($ip2c_res) : "NULL";
    // insert to database.
    $sql = "INSERT IGNORE INTO " . fs_hits_table() . "\n\t\t\t(site_id,ip,timestamp,url_id,referer_id,useragent_id,session_id,excluded_ip,excluded_user,user_id,country_code) \n\t\t\t\t\tVALUES ({$site_id},\n\t\t\t\t\t\t\t{$remoteaddr},\n\t\t\t\t\t\t\t{$time},\n\t\t\t\t\t\t\t{$url_id},\n\t\t\t\t\t\t\t{$referer_id},\n\t\t\t\t\t\t\t{$useragent_id},\n\t\t\t\t\t\t\t" . (isset($session_id) ? "{$session_id}" : "NULL") . ",\n\t\t\t\t\t\t\t{$excluded_ip},\n\t\t\t\t\t\t\t{$excluded_user},\n\t\t\t\t\t\t\t" . ($user_id ? "{$user_id}" : "NULL") . ",\n\t\t\t\t\t\t\t{$ccode}\n\t\t\t\t\t\t\t)";
    if ($fsdb->query($sql) === false) {
        return fs_debug_rollback();
    }
    if ($fsdb->query("COMMIT") === false) {
        return fs_debug_rollback();
    }
    return true;
}