# wt-security/2.4.14/lib/DB.php

WebTotem Security, version 2.4.14. 140 lines.

- Page: https://pluginprobe.com/plugins/wt-security/2.4.14/code/lib/DB.php
- Raw: https://pluginprobe.com/plugins/wt-security/2.4.14/raw/lib/DB.php
- Modified: 2022-09-07T11:53:30+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wt-security/2.4.14/code/lib/DB.php#L10-L20`.

```php
<?php
if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
	if (!headers_sent()) {
		/* Report invalid access if possible. */
		header('HTTP/1.1 403 Forbidden');
	}
	exit(1);
}

/**
 * WebTotem Database class for Wordpress.
 */
class WebTotemDB {

    const WTOTEM_TABLE_SETTINGS = 'wtotem_settings';
    const WTOTEM_TABLE_BLOCKED_LIST = 'wtotem_blocked_list';

	/**
	 * Creating a database with plugin settings.
	 */
	public static function install () {
		global $wpdb;

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        $settings_table = self::add_prefix(self::WTOTEM_TABLE_SETTINGS);
		if($wpdb->get_var("show tables like '$settings_table'") != $settings_table) {

			$sql = "CREATE TABLE " . $settings_table . " (
			  id mediumint(9) NOT NULL AUTO_INCREMENT,
			  name tinytext NOT NULL,
			  value longtext,
			  UNIQUE KEY id (id)
			);";

			dbDelta($sql);
		}

        $blocked_list_table = self::add_prefix(self::WTOTEM_TABLE_BLOCKED_LIST);
        if($wpdb->get_var("show tables like '$blocked_list_table'") != $blocked_list_table) {

            $sql = "CREATE TABLE " . $blocked_list_table . " (
			  id mediumint(9) NOT NULL AUTO_INCREMENT,
			  ip tinytext NOT NULL,
			  reason tinytext,
			  blockedTime tinytext,
			  UNIQUE KEY id (id)
			);";

            dbDelta($sql);
        }

		return true;
	}

	/**
	 * Add (or update) data to the table.
	 */
	public static function setData ($options, $table, $where = false) {
		global $wpdb;

        $table_name = self::getTable($table);

		if($where && $current = self::getData($where, $table)){
            $options['id'] = $current['id'];
		}

		$wpdb->replace( $table_name, $options );
	}

	/**
	 * Delete data from the table.
	 */
	public static function deleteData ($params, $table) {
		global $wpdb;

		$table_name = self::getTable($table);
		$wpdb->delete( $table_name, $params );
	}

	/**
	 * Getting values from the table.
	 *
	 * @param array $options
	 *    Option name.
	 *
	 * @return array
	 */
	public static function getData ($options, $table) {
		global $wpdb;
		$table_name = self::getTable($table);

        foreach ($options as $key => $value){
            $where[] = $key . " = '" . $value . "'";
        }
        $where = implode(' AND ', $where);

		$options = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table_name WHERE $where" ) );

		return (array) $options ?: [];
	}

	/**
	 * Deleting wtotem tables.
	 */
	public static function uninstall() {
        $tables = [ self::WTOTEM_TABLE_SETTINGS, self::WTOTEM_TABLE_BLOCKED_LIST ];
        foreach ($tables as $table) {
            global $wpdb;
            $wpdb->query('DROP TABLE IF EXISTS `' . self::add_prefix($table) . '`');
        }
	}

	/**
	 * Returns the table with the site prefix added.
	 *
	 * @param string $table
	 *    Table name.
	 * @return string
	 */
	public static function add_prefix($table) {
		global $wpdb;
		return $wpdb->prefix . $table;
	}

    /**
     * Get table name.
     */
    private static function getTable($name) {
        switch ($name) {
            case 'settings':
                return self::add_prefix(self::WTOTEM_TABLE_SETTINGS);
            case 'blocked_list':
                return self::add_prefix(self::WTOTEM_TABLE_BLOCKED_LIST);
        }

        throw new \OutOfBoundsException('Unknown key: ' . $name);
    }

}
```
