diff --git a/README.md b/README.md index 867e071..f947294 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,17 @@ A simple PHP WakeOnLan UI ###### Installation 1. Clone the repo and copy the `html` folder into your webroot -2. Change the lines below in `config.php` to fit your mysql setup *(DO NOT CREATE DB)* +2. Create SQL user: + +```bash +$ sudo mysql +mysql> CREATE DATABASE 'php-wol'; +mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password'; +mysql> GRANT ALL PRIVILEGES ON 'php-wol'.* TO 'username'@'localhost'; +mysql> FLUSH PRIVILEGES; + +``` +4. Change the lines below in `config.php` to fit your mysql setup ``` private $servername = "localhost"; diff --git a/html/db.php b/html/db.php index e0388ec..984d111 100644 --- a/html/db.php +++ b/html/db.php @@ -1,20 +1,19 @@ $this->servername, - "username" => $this->username, - "password" => $this->password, - "database" => $this->database, - ] = include('./config.php'); - } + // Get configuration + if (file_exists('./config.php')) { + $config = include('./config.php'); + $this->servername = $config['servername']; + $this->username = $config['username']; + $this->password = $config['password']; + $this->database = $config['database']; + } // Create connection try { @@ -22,21 +21,26 @@ function __construct() { } catch (PDOException $e) { die('Connection failed: ' . $e->getMessage()); } - - //check if database exists - $st = $this->prepare("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$this->database}'"); - $st->execute(); - if($st->rowCount() <= 0) $this->create(); - else $this->query("USE `{$this->database}`"); + // eventually create schema + $this->create(); } /** - * creates a new database + * creates schema if it does not exists */ function create() { - $this->query("CREATE DATABASE `{$this->database}`"); + //check if database exists + $st = $this->prepare("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$this->database}'"); + $st->execute(); + if($st->rowCount() <= 0) { + $this->query("CREATE DATABASE `{$this->database}`"); + } $this->query("USE `{$this->database}`"); - $this->query(<<prepare("SELECT 1 FROM server"); + $st->execute(); + if($st->rowCount() <= 0) { + $q = <<query(<<query($q); + } + + $st = $this->prepare("SELECT 1 FROM user"); + $st->execute(); + if($st->rowCount() <= 0) { + $q = <<query("INSERT INTO `user` (`id`, `username`, `password`, `salt`, `level`) VALUES (1, 'admin', 0xa28d2e6472c306c6c57138f5f2c6ff25498e5d3ccd9a63a1bc591f4b5fe1dedd4b4044bcf1cc0b61767f6d7ce8198d94eb432d03fa082261ab4741734f07e9b2, 0xb2c17f0341921fe13306e0d6b4c952d90ad12ce176e962f5d7f28254c51e4345accaafc5d1f003fe346ab8a1bf52330317d1007237ca8f78abd9420d4b5524a3, 3)"); +EOL; + $this->query($q); + } + + // create admin + $st = $this->prepare("SELECT admin FROM user"); + $st->execute(); + if($st->rowCount() <= 0) { + $this->query("INSERT INTO `user` (`id`, `username`, `password`, `salt`, `level`) VALUES (1, 'admin', 0xa28d2e6472c306c6c57138f5f2c6ff25498e5d3ccd9a63a1bc591f4b5fe1dedd4b4044bcf1cc0b61767f6d7ce8198d94eb432d03fa082261ab4741734f07e9b2, 0xb2c17f0341921fe13306e0d6b4c952d90ad12ce176e962f5d7f28254c51e4345accaafc5d1f003fe346ab8a1bf52330317d1007237ca8f78abd9420d4b5524a3, 3)"); + } + + } } diff --git a/html/index.html b/html/index.html index 733b3da..cd3ff90 100644 --- a/html/index.html +++ b/html/index.html @@ -1,8 +1,12 @@ - Admin Page + Wake-On-Lan + + + + diff --git a/html/login.php b/html/login.php index c135ea3..b9386ca 100644 --- a/html/login.php +++ b/html/login.php @@ -21,9 +21,9 @@ if ($auth->hasLevel()) { if ($_POST['newpassword'] != $_POST['newpassword2']) $toReturn['msg'] = 'passwords didnt match'; - else if ($auth->login($auth->getUsername(), $_POST['password'])) + else if (!$auth->login($auth->getUsername(), $_POST['password'])) { $toReturn['msg'] = 'wrong password'; - else { + } else { $auth->updatePassword($_POST['newpassword']); $toReturn['response'] = true; } diff --git a/html/wake.php b/html/wake.php index 625cb65..0dfa13b 100644 --- a/html/wake.php +++ b/html/wake.php @@ -8,6 +8,9 @@ } else if(isset($_POST['mac'], $_POST['broadcast'])) { $broadcast = $_POST['broadcast']; + if(empty($broadcast)) { + $broadcast = '255.255.255.255'; + } $mac_array = explode(':', $_POST['mac']); $hwaddr = ''; diff --git a/html/wol.png b/html/wol.png new file mode 100644 index 0000000..8af903c Binary files /dev/null and b/html/wol.png differ