Example #1
0
 static function createConfig($host = 'localhost', $user, $password, $name = 'lychee', $prefix = '')
 {
     # Check dependencies
     Module::dependencies(isset($host, $user, $password, $name));
     $database = new mysqli($host, $user, $password);
     if ($database->connect_errno) {
         return 'Warning: Connection failed!';
     }
     # Check if database exists
     if (!$database->select_db($name)) {
         # Database doesn't exist
         # Check if user can create the database
         $result = Database::createDatabase($database, $name);
         if ($result === false) {
             return 'Warning: Creation failed!';
         }
     }
     # Escape data
     $host = mysqli_real_escape_string($database, $host);
     $user = mysqli_real_escape_string($database, $user);
     $password = mysqli_real_escape_string($database, $password);
     $name = mysqli_real_escape_string($database, $name);
     $prefix = mysqli_real_escape_string($database, $prefix);
     # Save config.php
     $config = "<?php\n\n###\n# @name\t\t\tConfiguration\n# @author\t\tTobias Reich\n# @copyright\t2014 Tobias Reich\n###\n\nif(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');\n\n# Database configuration\n\$dbHost = '{$host}'; # Host of the database\n\$dbUser = '******'; # Username of the database\n\$dbPassword = '******'; # Password of the database\n\$dbName = '{$name}'; # Database name\n\$dbTablePrefix = '{$prefix}'; # Table prefix\n\$lang = 'en'; # Language\n\n?>";
     # Save file
     if (file_put_contents(LYCHEE_CONFIG_FILE, $config) === false) {
         return 'Warning: Could not create file!';
     }
     return true;
 }
Example #2
0
 /**
  * Creates the configuration file.
  * @return true|string Returns true when successful.
  *                     Warning: Connection failed!
  *                     Warning: Creation failed!
  *                     Warning: Could not create file!
  */
 public static function create($host, $user, $password, $name = 'lychee', $prefix = '')
 {
     // Open a new connection to the MySQL server
     $connection = Database::connect($host, $user, $password);
     // Check if the connection was successful
     if ($connection === false) {
         return 'Warning: Connection failed!';
     }
     // Check if user can create the database before saving the configuration
     if (Database::createDatabase($connection, $name) === false) {
         return 'Warning: Creation failed!';
     }
     // Escape data
     $host = mysqli_real_escape_string($connection, $host);
     $user = mysqli_real_escape_string($connection, $user);
     $password = mysqli_real_escape_string($connection, $password);
     $name = mysqli_real_escape_string($connection, $name);
     $prefix = mysqli_real_escape_string($connection, $prefix);
     // Save config.php
     $config = "<?php\n\n// Database configuration\n\$dbHost = '{$host}'; // Host of the database\n\$dbUser = '******'; // Username of the database\n\$dbPassword = '******'; // Password of the database\n\$dbName = '{$name}'; // Database name\n\$dbTablePrefix = '{$prefix}'; // Table prefix\n\n?>";
     // Save file
     if (@file_put_contents(LYCHEE_CONFIG_FILE, $config) === false) {
         return 'Warning: Could not create file!';
     }
     return true;
 }
Example #3
0
 static function connect($host = 'localhost', $user, $password, $name = 'lychee')
 {
     # Check dependencies
     Module::dependencies(isset($host, $user, $password, $name));
     $database = new mysqli($host, $user, $password);
     # Check connection
     if ($database->connect_errno) {
         exit('Error: ' . $database->connect_error);
     }
     # Avoid sql injection on older MySQL versions by using GBK
     if ($database->server_version < 50500) {
         $database->set_charset('GBK');
     } else {
         $database->set_charset("utf8");
     }
     # Check database
     if (!$database->select_db($name)) {
         if (!Database::createDatabase($database, $name)) {
             exit('Error: Could not create database!');
         }
     }
     # Check tables
     $query = Database::prepare($database, 'SELECT * FROM ?, ?, ?, ? LIMIT 0', array(LYCHEE_TABLE_PHOTOS, LYCHEE_TABLE_ALBUMS, LYCHEE_TABLE_SETTINGS, LYCHEE_TABLE_LOG));
     if (!$database->query($query)) {
         if (!Database::createTables($database)) {
             exit('Error: Could not create tables!');
         }
     }
     return $database;
 }
Example #4
0
 static function createConfig($host = 'localhost', $user, $password, $name = 'lychee', $prefix = '', $type = 'pgsql')
 {
     # Check dependencies
     Module::dependencies(isset($host, $user, $password, $name));
     try {
         if ($type == 'mysql') {
             $database_create = new PDO($type . ":host=localhost", $user, $password);
         } else {
             if ($type == 'pgsql') {
                 $database_create = new PDO($type . ":host=localhost;dbname='template1'", $user, $password);
             } else {
                 exit('Warning: Unknown database type: ' . $type);
             }
         }
     } catch (PDOException $e) {
         exit('Warning: Connection to create DB failed: ' . $e->getMessage());
     }
     # Check if user can create the database, and whether it exists
     $result = Database::createDatabase($database_create, $name);
     if ($result === false) {
         return 'Warning: Creation failed!';
     }
     # connect to newly created database
     try {
         $database = new PDO($type . ':host=localhost;dbname=' . $name, $user, $password);
     } catch (PDOException $e) {
         exit('Warning: Connection failed: ' . $e->getMessage());
     }
     #}
     # Escape data
     $host = $database->quote($host);
     $user = $database->quote($user);
     $password = $database->quote($password);
     $name = $database->quote($name);
     # FIXME: stricter check for prefix (i.e., only characters and potentially numbers)
     $prefix = $database->quote($prefix);
     # Save config.php
     $config = "<?php\n\n###\n# @name\t\t\tConfiguration\n# @author\t\tTobias Reich\n# @copyright\t2015 Tobias Reich\n###\n\nif(!defined('LYCHEE')) exit('Error: Direct access is not allowed!');\n\n# Database configuration\n\$dbType = '{$type}'; # Database type (mysql / pgsql)\n\$dbHost = {$host}; # Host of the database\n\$dbUser = {$user}; # Username of the database\n\$dbPassword = {$password}; # Password of the database\n\$dbName = {$name}; # Database name\n\$dbTablePrefix = {$prefix}; # Table prefix\n\n?>";
     # Save file
     if (file_put_contents(LYCHEE_CONFIG_FILE, $config) === false) {
         return 'Warning: Could not create file!';
     }
     return true;
 }
Example #5
0
 function insertProductItemToDatabase($productItems, $row, $page)
 {
     static $count = 0;
     $db = new Database();
     $conn = $db->connectDatabase();
     $db->createDatabase($conn, $this->databaseName());
     $db->selectDatabase($conn, $this->databaseName());
     $this->createProductTable($conn);
     foreach ($productItems as $key => $data) {
         if (!isset($data['brand'])) {
             $data['brand'] = '';
         }
         if (!isset($data['description'])) {
             $data['description'] = '';
         }
         $data['url'] = $this->scraper->merchantUrl . $data['url'];
         $data['category'] = $this->item['category'];
         $data['merchant'] = $this->merchantName;
         $this->insertTable($conn, $data);
         $count++;
         $this->printResult($row, $page, $count, $data);
     }
     $this->logLastInsert($row, $page);
 }
Example #6
0
<?php

include '../common/Database.class.php';
$dbHelper = new Database();
$database = "contactdb";
$tbl_member = "member";
$sql = 'CREATE DATABASE IF NOT EXISTS contactdb';
$dbHelper->createDatabase($database, $sql);
$sql = 'CREATE TABLE IF NOT EXISTS  member ( ' . 'id int(10) UNSIGNED NOT NULL AUTO_INCREMENT, ' . 'email varchar(255) NOT NULL UNIQUE, ' . 'password int(10) NOT NULL, ' . 'PRIMARY KEY (id) ' . ') ENGINE=InnoDB DEFAULT CHARSET=latin1';
$dbHelper->createTable($database, $tbl_member, $sql);
Example #7
0
 /**
  * Create the database and connect to it. This can be called if the
  * initial database connection is not successful because the database
  * does not exist.
  * @param string $connect Connection string
  * @param string $username Database username
  * @param string $password Database Password
  * @param string $database Database to which to create
  * @return boolean Returns true if successful
  */
 static function createDatabase($connect, $username, $password, $database)
 {
     return DB::$globalConn->createDatabase($connect, $username, $password, $database);
 }