diff --git a/.build/.env b/.build/.env new file mode 100644 index 0000000..6ee9334 --- /dev/null +++ b/.build/.env @@ -0,0 +1,21 @@ +#!/usr/bin/env bash + +# See https://docs.docker.com/compose/environment-variables/#the-env-file + +# Nginx +#NGINX_HOST=localhost + +# PHP + +# See https://hub.docker.com/r/nanoninja/php-fpm/tags/ +#PHP_VERSION=latest + +# MySQL +# mySQL default credentials for the container (accordingly to config.php) +MYSQL_VERSION=5.7.22 +MYSQL_HOST=mysql +#MYSQL_DATABASE=test +MYSQL_ROOT_USER=root +MYSQL_ROOT_PASSWORD=root +MYSQL_USER=espresso +MYSQL_PASSWORD=lattecaffee diff --git a/.build/docker-compose.yml b/.build/docker-compose.yml new file mode 100644 index 0000000..c5b3256 --- /dev/null +++ b/.build/docker-compose.yml @@ -0,0 +1,53 @@ + +version: '3' +services: + web: +# build: +# context: . +# dockerfile: php_custom.Dockerfile + image: nginx:latest + container_name: guest_portal_nginx + ports: + - "80:80" + volumes: + - ./volumes/nginx/code:/code + - ./volumes/nginx/conf/site.conf:/etc/nginx/conf.d/000_site.conf + links: + - php + - mysqldb + depends_on: + - mysqldb + php: + build: + context: . + dockerfile: php_custom.Dockerfile +# image: php:7-fpm + container_name: guest_portal_php + volumes: + - ./volumes/nginx/code:/code + links: + - mysqldb + mysqldb: + image: mysql:${MYSQL_VERSION} + container_name: guest_portal_${MYSQL_HOST} + restart: always + env_file: + - ".env" + environment: + - MYSQL_DATABASE=${MYSQL_DATABASE} + - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} + - MYSQL_USER=${MYSQL_USER} + - MYSQL_PASSWORD=${MYSQL_PASSWORD} +# ports: +# - "8989:3306" + volumes: + - "./volumes/mysql/data:/var/lib/mysql" + - ./import_db.sql:/docker-entrypoint-initdb.d/dump.sql + + + + + + + + diff --git a/.build/import_db.sql b/.build/import_db.sql new file mode 100644 index 0000000..991e9cf --- /dev/null +++ b/.build/import_db.sql @@ -0,0 +1,84 @@ +-- MySQL dump 10.13 Distrib 8.0.16, for Win64 (x86_64) +-- +-- Host: 192.168.99.21 Database: portal +-- ------------------------------------------------------ +-- Server version 5.7.25 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; + SET NAMES utf8 ; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `sessions` +-- +CREATE DATABASE `portal`; +USE `portal`; +GRANT SELECT, INSERT, UPDATE, DELETE ON `portal`.* TO `espresso`@`%`; + +DROP TABLE IF EXISTS `sessions`; +/*!40101 SET @saved_cs_client = @@character_set_client */; + SET character_set_client = utf8mb4 ; +CREATE TABLE `sessions` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `username` varchar(45) DEFAULT NULL, + `mac` varchar(45) DEFAULT NULL, + `ap` varchar(45) DEFAULT NULL, + `timestamp` datetime DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `username_idx` (`username`), + KEY `id` (`id`), + CONSTRAINT `username` FOREIGN KEY (`username`) REFERENCES `users` (`username`) ON DELETE NO ACTION ON UPDATE NO ACTION +) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `sessions` +-- + +LOCK TABLES `sessions` WRITE; +/*!40000 ALTER TABLE `sessions` DISABLE KEYS */; +/*!40000 ALTER TABLE `sessions` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `users` +-- + +DROP TABLE IF EXISTS `users`; +/*!40101 SET @saved_cs_client = @@character_set_client */; + SET character_set_client = utf8mb4 ; +CREATE TABLE `users` ( + `username` varchar(45) NOT NULL, + `hash` varchar(255) DEFAULT NULL, + `sessions` int(11) NOT NULL DEFAULT '0', + `enabled` tinyint(1) NOT NULL DEFAULT '1', + PRIMARY KEY (`username`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `users` +-- + +LOCK TABLES `users` WRITE; +/*!40000 ALTER TABLE `users` DISABLE KEYS */; +/*!40000 ALTER TABLE `users` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2020-02-20 15:01:27 diff --git a/.build/php_custom.Dockerfile b/.build/php_custom.Dockerfile new file mode 100644 index 0000000..c7529d8 --- /dev/null +++ b/.build/php_custom.Dockerfile @@ -0,0 +1,7 @@ + + +FROM php:7.2-fpm +RUN docker-php-ext-install -j$(nproc) mysqli +RUN docker-php-ext-install -j$(nproc) pdo +RUN docker-php-ext-install -j$(nproc) pdo_mysql + diff --git a/.build/volumes/nginx/conf/site.conf b/.build/volumes/nginx/conf/site.conf new file mode 100644 index 0000000..6a551eb --- /dev/null +++ b/.build/volumes/nginx/conf/site.conf @@ -0,0 +1,26 @@ +# Inhalt der Datei: +server { + index index.php index.html; + server_name _; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + root /code; + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass php:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + location /config { + deny all; + return 404; + } + location /html { + deny all; + return 404; + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..73f2635 --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +# P0rtality - UniFi compatible Captive Portal + +## Features + +* Add & Remove Users +* COMING SOON - Easy way to use existing user database +* Get live Sessions of logged in Users +* Kick logged in Users +* basic session logging +* docker support + +![alt text](./documentation/images/guest_login.png "Guest Login Example") +![alt text](./documentation/images/admin_panel.png "Admin Panel") + +## Facts & Structure +* nginx & PHP runs Front- and Backend +* Mysql stores users, passwords and session-history +* conf/config.php contains most settings + +## Compatibilty +tested on Unifi + * 5.12.35 + * 5.12.66 + +## Installation + +### Docker +* adapt **src/conf/config.php** BEFORE proceeding! + * +* run **run.compose.sh** +* set IP of UniFi-Guest-Portal to the IP of your Docker-Host. (Unfi-Controller "Guest Conrol" - enable - "external Portal" - set IP) + +#### Testing +* Portal is available at /guest/s/default/ + + +nginx config, conf/config.php and all web-content is mounted in **volumes/nginx** afterwards. + + +### Manual Installation +* Make the contents of the **src**-folder available via web server. The root directory (where the .php files live) must be accessible under /guest/s/default/. PHP is needed. +* Prevent direct access to the directories **html** and **config**! +* import **.build/import_db.sql** into a **Medoo** compatible database. +* adapt **conf/config.php**. + +## Credits +this project uses +* Medoo for DB-Access (http://medoo.in/) +* parts of the UniFi API browser for Unifi-Controller API Logic (https://github.com/Art-of-WiFi/UniFi-API-browser) + +and is inspired by +* espresso-portal (https://github.com/emanuelepaiano/espresso-portal) \ No newline at end of file diff --git a/documentation/images/admin_panel.png b/documentation/images/admin_panel.png new file mode 100644 index 0000000..5dd09fa Binary files /dev/null and b/documentation/images/admin_panel.png differ diff --git a/documentation/images/guest_login.png b/documentation/images/guest_login.png new file mode 100644 index 0000000..96baac7 Binary files /dev/null and b/documentation/images/guest_login.png differ diff --git a/run_compose.sh b/run_compose.sh new file mode 100755 index 0000000..0a7661c --- /dev/null +++ b/run_compose.sh @@ -0,0 +1,11 @@ +mkdir .build/volumes/mysql +mkdir .build/volumes/mysql/data + +mkdir .build/volumes/nginx/code +mkdir .build/volumes/nginx/code/guest +mkdir .build/volumes/nginx/code/guest/s +mkdir .build/volumes/nginx/code/guest/s/default + +cp -R src/* .build/volumes/nginx/code/guest/s/default/ +cd .build +docker-compose -p guest_portal up -d diff --git a/src/Medoo.php b/src/Medoo.php new file mode 100644 index 0000000..f9f0979 --- /dev/null +++ b/src/Medoo.php @@ -0,0 +1,1266 @@ +database_type = strtolower($options['database_type']); + } + } + else + { + return false; + } + + if (isset($options['prefix'])) + { + $this->prefix = $options['prefix']; + } + + if (isset($options['option'])) + { + $this->option = $options['option']; + } + + if (isset($options['command']) && is_array($options['command'])) + { + $commands = $options['command']; + } + else + { + $commands = []; + } + + if (isset($options['dsn'])) + { + if (isset($options['dsn']['driver'])) + { + $attr = $options['dsn']; + } + else + { + return false; + } + } + else + { + if ( + isset($options['port']) && + is_int($options['port'] * 1) + ) + { + $port = $options['port']; + } + + $is_port = isset($port); + + switch ($this->database_type) + { + case 'mariadb': + case 'mysql': + $attr = [ + 'driver' => 'mysql', + 'dbname' => $options['database_name'] + ]; + + if (isset($options['socket'])) + { + $attr['unix_socket'] = $options['socket']; + } + else + { + $attr['host'] = $options['server']; + + if ($is_port) + { + $attr['port'] = $port; + } + } + + // Make MySQL using standard quoted identifier + $commands[] = 'SET SQL_MODE=ANSI_QUOTES'; + break; + + case 'pgsql': + $attr = [ + 'driver' => 'pgsql', + 'host' => $options['server'], + 'dbname' => $options['database_name'] + ]; + + if ($is_port) + { + $attr['port'] = $port; + } + + break; + + case 'sybase': + $attr = [ + 'driver' => 'dblib', + 'host' => $options['server'], + 'dbname' => $options['database_name'] + ]; + + if ($is_port) + { + $attr['port'] = $port; + } + + break; + + case 'oracle': + $attr = [ + 'driver' => 'oci', + 'dbname' => $options['server'] ? + '//' . $options['server'] . ($is_port ? ':' . $port : ':1521') . '/' . $options['database_name'] : + $options['database_name'] + ]; + + if (isset($options['charset'])) + { + $attr['charset'] = $options['charset']; + } + + break; + + case 'mssql': + if (strstr(PHP_OS, 'WIN')) + { + $attr = [ + 'driver' => 'sqlsrv', + 'server' => $options['server'], + 'database' => $options['database_name'] + ]; + } + else + { + $attr = [ + 'driver' => 'dblib', + 'host' => $options['server'], + 'dbname' => $options['database_name'] + ]; + } + + if ($is_port) + { + $attr['port'] = $port; + } + + // Keep MSSQL QUOTED_IDENTIFIER is ON for standard quoting + $commands[] = 'SET QUOTED_IDENTIFIER ON'; + + // Make ANSI_NULLS is ON for NULL value + $commands[] = 'SET ANSI_NULLS ON'; + break; + + case 'sqlite': + $this->pdo = new PDO('sqlite:' . $options['database_file'], null, null, $this->option); + + return $this; + } + } + + $driver = $attr['driver']; + + unset($attr['driver']); + + $stack = []; + + foreach ($attr as $key => $value) + { + if (is_int($key)) + { + $stack[] = $value; + } + else + { + $stack[] = $key . '=' . $value; + } + } + + $dsn = $driver . ':' . implode($stack, ';'); + + if ( + in_array($this->database_type, ['mariadb', 'mysql', 'pgsql', 'sybase', 'mssql']) && + $options['charset'] + ) + { + $commands[] = "SET NAMES '" . $options['charset'] . "'"; + } + + $this->pdo = new PDO( + $dsn, + $options['username'], + $options['password'], + $this->option + ); + + foreach ($commands as $value) + { + $this->pdo->exec($value); + } + } + catch (PDOException $e) { + throw new Exception($e->getMessage()); + } + } + + public function query($query) + { + if ($this->debug_mode) + { + echo $query; + + $this->debug_mode = false; + + return false; + } + + $this->logs[] = $query; + + return $this->pdo->query($query); + } + + public function exec($query) + { + if ($this->debug_mode) + { + echo $query; + + $this->debug_mode = false; + + return false; + } + + $this->logs[] = $query; + + return $this->pdo->exec($query); + } + + public function quote($string) + { + return $this->pdo->quote($string); + } + + protected function tableQuote($table) + { + return '"' . $this->prefix . $table . '"'; + } + + protected function columnQuote($string) + { + preg_match('/(\(JSON\)\s*|^#)?([a-zA-Z0-9_]*)\.([a-zA-Z0-9_]*)/', $string, $column_match); + + if (isset($column_match[ 2 ], $column_match[ 3 ])) + { + return '"' . $this->prefix . $column_match[ 2 ] . '"."' . $column_match[ 3 ] . '"'; + } + + return '"' . $string . '"'; + } + + protected function columnPush(&$columns) + { + if ($columns == '*') + { + return $columns; + } + + if (is_string($columns)) + { + $columns = [$columns]; + } + + $stack = []; + + foreach ($columns as $key => $value) + { + if (is_array($value)) + { + $stack[] = $this->columnPush($value); + } + else + { + preg_match('/([a-zA-Z0-9_\-\.]*)\s*\(([a-zA-Z0-9_\-]*)\)/i', $value, $match); + + if (isset($match[ 1 ], $match[ 2 ])) + { + $stack[] = $this->columnQuote( $match[ 1 ] ) . ' AS ' . $this->columnQuote( $match[ 2 ] ); + + $columns[ $key ] = $match[ 2 ]; + } + else + { + $stack[] = $this->columnQuote( $value ); + } + } + } + + return implode($stack, ','); + } + + protected function arrayQuote($array) + { + $temp = []; + + foreach ($array as $value) + { + $temp[] = is_int($value) ? $value : $this->pdo->quote($value); + } + + return implode($temp, ','); + } + + protected function innerConjunct($data, $conjunctor, $outer_conjunctor) + { + $haystack = []; + + foreach ($data as $value) + { + $haystack[] = '(' . $this->dataImplode($value, $conjunctor) . ')'; + } + + return implode($outer_conjunctor . ' ', $haystack); + } + + protected function fnQuote($column, $string) + { + return (strpos($column, '#') === 0 && preg_match('/^[A-Z0-9\_]*\([^)]*\)$/', $string)) ? + + $string : + + $this->quote($string); + } + + protected function dataImplode($data, $conjunctor, $outer_conjunctor = null) + { + $wheres = []; + + foreach ($data as $key => $value) + { + $type = gettype($value); + + if ( + preg_match("/^(AND|OR)(\s+#.*)?$/i", $key, $relation_match) && + $type == 'array' + ) + { + $wheres[] = 0 !== count(array_diff_key($value, array_keys(array_keys($value)))) ? + '(' . $this->dataImplode($value, ' ' . $relation_match[ 1 ]) . ')' : + '(' . $this->innerConjunct($value, ' ' . $relation_match[ 1 ], $conjunctor) . ')'; + } + else + { + if ( + is_int($key) && + preg_match('/([\w\.\-]+)\[(\>|\>\=|\<|\<\=|\!|\=)\]([\w\.\-]+)/i', $value, $match) + ) + { + $operator = $match[ 2 ]; + + $wheres[] = $this->columnQuote($match[ 1 ]) . ' ' . $operator . ' ' . $this->columnQuote($match[ 3 ]); + } + else + { + preg_match('/(#?)([\w\.\-]+)(\[(\>|\>\=|\<|\<\=|\!|\<\>|\>\<|\!?~)\])?/i', $key, $match); + $column = $this->columnQuote($match[ 2 ]); + + if (isset($match[ 4 ])) + { + $operator = $match[ 4 ]; + + if ($operator == '!') + { + switch ($type) + { + case 'NULL': + $wheres[] = $column . ' IS NOT NULL'; + break; + + case 'array': + $wheres[] = $column . ' NOT IN (' . $this->arrayQuote($value) . ')'; + break; + + case 'integer': + case 'double': + $wheres[] = $column . ' != ' . $value; + break; + + case 'boolean': + $wheres[] = $column . ' != ' . ($value ? '1' : '0'); + break; + + case 'string': + $wheres[] = $column . ' != ' . $this->fnQuote($key, $value); + break; + } + } + + if ($operator == '<>' || $operator == '><') + { + if ($type == 'array') + { + if ($operator == '><') + { + $column .= ' NOT'; + } + + if (is_numeric($value[ 0 ]) && is_numeric($value[ 1 ])) + { + $wheres[] = '(' . $column . ' BETWEEN ' . $value[ 0 ] . ' AND ' . $value[ 1 ] . ')'; + } + else + { + $wheres[] = '(' . $column . ' BETWEEN ' . $this->quote($value[ 0 ]) . ' AND ' . $this->quote($value[ 1 ]) . ')'; + } + } + } + + if ($operator == '~' || $operator == '!~') + { + if ($type != 'array') + { + $value = [$value]; + } + + $connector = ' OR '; + $stack = array_values($value); + + if (is_array($stack[0])) + { + if (isset($value['AND']) || isset($value['OR'])) + { + $connector = ' ' . array_keys($value)[0] . ' '; + $value = $stack[0]; + } + } + + $like_clauses = []; + + foreach ($value as $item) + { + $item = strval($item); + + if (!preg_match('/(\[.+\]|_|%.+|.+%)/', $item)) + { + $item = '%' . $item . '%'; + } + + $like_clauses[] = $column . ($operator === '!~' ? ' NOT' : '') . ' LIKE ' . $this->fnQuote($key, $item); + } + + $wheres[] = '(' . implode($connector, $like_clauses) . ')'; + } + + if (in_array($operator, ['>', '>=', '<', '<='])) + { + $condition = $column . ' ' . $operator . ' '; + + if (is_numeric($value)) + { + $condition .= $value; + } + elseif (strpos($key, '#') === 0) + { + $condition .= $this->fnQuote($key, $value); + } + else + { + $condition .= $this->quote($value); + } + + $wheres[] = $condition; + } + } + else + { + switch ($type) + { + case 'NULL': + $wheres[] = $column . ' IS NULL'; + break; + + case 'array': + $wheres[] = $column . ' IN (' . $this->arrayQuote($value) . ')'; + break; + + case 'integer': + case 'double': + $wheres[] = $column . ' = ' . $value; + break; + + case 'boolean': + $wheres[] = $column . ' = ' . ($value ? '1' : '0'); + break; + + case 'string': + $wheres[] = $column . ' = ' . $this->fnQuote($key, $value); + break; + } + } + } + } + } + + return implode($conjunctor . ' ', $wheres); + } + + protected function whereClause($where) + { + $where_clause = ''; + + if (is_array($where)) + { + $where_keys = array_keys($where); + $where_AND = preg_grep("/^AND\s*#?$/i", $where_keys); + $where_OR = preg_grep("/^OR\s*#?$/i", $where_keys); + + $single_condition = array_diff_key($where, array_flip( + ['AND', 'OR', 'GROUP', 'ORDER', 'HAVING', 'LIMIT', 'LIKE', 'MATCH'] + )); + + if ($single_condition != []) + { + $condition = $this->dataImplode($single_condition, ' AND'); + + if ($condition != '') + { + $where_clause = ' WHERE ' . $condition; + } + } + + if (!empty($where_AND)) + { + $value = array_values($where_AND); + $where_clause = ' WHERE ' . $this->dataImplode($where[ $value[ 0 ] ], ' AND'); + } + + if (!empty($where_OR)) + { + $value = array_values($where_OR); + $where_clause = ' WHERE ' . $this->dataImplode($where[ $value[ 0 ] ], ' OR'); + } + + if (isset($where[ 'MATCH' ])) + { + $MATCH = $where[ 'MATCH' ]; + + if (is_array($MATCH) && isset($MATCH[ 'columns' ], $MATCH[ 'keyword' ])) + { + $columns = str_replace('.', '"."', implode($MATCH[ 'columns' ], '", "')); + $keywords = $this->quote($MATCH[ 'keyword' ]); + + $where_clause .= ($where_clause != '' ? ' AND ' : ' WHERE ') . ' MATCH ("' . $columns . '") AGAINST (' . $keywords . ')'; + } + } + + if (isset($where[ 'GROUP' ])) + { + $where_clause .= ' GROUP BY ' . $this->columnQuote($where[ 'GROUP' ]); + + if (isset($where[ 'HAVING' ])) + { + $where_clause .= ' HAVING ' . $this->dataImplode($where[ 'HAVING' ], ' AND'); + } + } + + if (isset($where[ 'ORDER' ])) + { + $ORDER = $where[ 'ORDER' ]; + + if (is_array($ORDER)) + { + $stack = []; + + foreach ($ORDER as $column => $value) + { + if (is_array($value)) + { + $stack[] = 'FIELD(' . $this->columnQuote($column) . ', ' . $this->arrayQuote($value) . ')'; + } + else if ($value === 'ASC' || $value === 'DESC') + { + $stack[] = $this->columnQuote($column) . ' ' . $value; + } + else if (is_int($column)) + { + $stack[] = $this->columnQuote($value); + } + } + + $where_clause .= ' ORDER BY ' . implode($stack, ','); + } + else + { + $where_clause .= ' ORDER BY ' . $this->columnQuote($ORDER); + } + } + + if (isset($where[ 'LIMIT' ])) + { + $LIMIT = $where[ 'LIMIT' ]; + + if (is_numeric($LIMIT)) + { + $where_clause .= ' LIMIT ' . $LIMIT; + } + + if ( + is_array($LIMIT) && + is_numeric($LIMIT[ 0 ]) && + is_numeric($LIMIT[ 1 ]) + ) + { + if ($this->database_type === 'pgsql') + { + $where_clause .= ' OFFSET ' . $LIMIT[ 0 ] . ' LIMIT ' . $LIMIT[ 1 ]; + } + else + { + $where_clause .= ' LIMIT ' . $LIMIT[ 0 ] . ',' . $LIMIT[ 1 ]; + } + } + } + } + else + { + if ($where != null) + { + $where_clause .= ' ' . $where; + } + } + + return $where_clause; + } + + protected function selectContext($table, $join, &$columns = null, $where = null, $column_fn = null) + { + preg_match('/([a-zA-Z0-9_\-]*)\s*\(([a-zA-Z0-9_\-]*)\)/i', $table, $table_match); + + if (isset($table_match[ 1 ], $table_match[ 2 ])) + { + $table = $this->tableQuote($table_match[ 1 ]); + + $table_query = $table . ' AS ' . $this->tableQuote($table_match[ 2 ]); + } + else + { + $table = $this->tableQuote($table); + + $table_query = $table; + } + + $join_key = is_array($join) ? array_keys($join) : null; + + if ( + isset($join_key[ 0 ]) && + strpos($join_key[ 0 ], '[') === 0 + ) + { + $table_join = []; + + $join_array = [ + '>' => 'LEFT', + '<' => 'RIGHT', + '<>' => 'FULL', + '><' => 'INNER' + ]; + + foreach($join as $sub_table => $relation) + { + preg_match('/(\[(\<|\>|\>\<|\<\>)\])?([a-zA-Z0-9_\-]*)\s?(\(([a-zA-Z0-9_\-]*)\))?/', $sub_table, $match); + + if ($match[ 2 ] != '' && $match[ 3 ] != '') + { + if (is_string($relation)) + { + $relation = 'USING ("' . $relation . '")'; + } + + if (is_array($relation)) + { + // For ['column1', 'column2'] + if (isset($relation[ 0 ])) + { + $relation = 'USING ("' . implode($relation, '", "') . '")'; + } + else + { + $joins = []; + + foreach ($relation as $key => $value) + { + $joins[] = ( + strpos($key, '.') > 0 ? + // For ['tableB.column' => 'column'] + $this->columnQuote($key) : + + // For ['column1' => 'column2'] + $table . '."' . $key . '"' + ) . + ' = ' . + $this->tableQuote(isset($match[ 5 ]) ? $match[ 5 ] : $match[ 3 ]) . '."' . $value . '"'; + } + + $relation = 'ON ' . implode($joins, ' AND '); + } + } + + $table_name = $this->tableQuote($match[ 3 ]) . ' '; + + if (isset($match[ 5 ])) + { + $table_name .= 'AS ' . $this->tableQuote($match[ 5 ]) . ' '; + } + + $table_join[] = $join_array[ $match[ 2 ] ] . ' JOIN ' . $table_name . $relation; + } + } + + $table_query .= ' ' . implode($table_join, ' '); + } + else + { + if (is_null($columns)) + { + if (is_null($where)) + { + if ( + is_array($join) && + isset($column_fn) + ) + { + $where = $join; + $columns = null; + } + else + { + $where = null; + $columns = $join; + } + } + else + { + $where = $join; + $columns = null; + } + } + else + { + $where = $columns; + $columns = $join; + } + } + + if (isset($column_fn)) + { + if ($column_fn == 1) + { + $column = '1'; + + if (is_null($where)) + { + $where = $columns; + } + } + else + { + if (empty($columns)) + { + $columns = '*'; + $where = $join; + } + + $column = $column_fn . '(' . $this->columnPush($columns) . ')'; + } + } + else + { + $column = $this->columnPush($columns); + } + + return 'SELECT ' . $column . ' FROM ' . $table_query . $this->whereClause($where); + } + + protected function dataMap($index, $key, $value, $data, &$stack) + { + if (is_array($value)) + { + $sub_stack = []; + + foreach ($value as $sub_key => $sub_value) + { + if (is_array($sub_value)) + { + $current_stack = $stack[ $index ][ $key ]; + + $this->dataMap(false, $sub_key, $sub_value, $data, $current_stack); + + $stack[ $index ][ $key ][ $sub_key ] = $current_stack[ 0 ][ $sub_key ]; + } + else + { + $this->dataMap(false, preg_replace('/^[\w]*\./i', "", $sub_value), $sub_key, $data, $sub_stack); + + $stack[ $index ][ $key ] = $sub_stack; + } + } + } + else + { + if ($index !== false) + { + $stack[ $index ][ $value ] = $data[ $value ]; + } + else + { + if (preg_match('/[a-zA-Z0-9_\-\.]*\s*\(([a-zA-Z0-9_\-]*)\)/i', $key, $key_match)) + { + $key = $key_match[ 1 ]; + } + + $stack[ $key ] = $data[ $key ]; + } + } + } + + public function select($table, $join, $columns = null, $where = null) + { + $column = $where == null ? $join : $columns; + + $is_single_column = (is_string($column) && $column !== '*'); + + $query = $this->query($this->selectContext($table, $join, $columns, $where)); + + $stack = []; + + $index = 0; + + if (!$query) + { + return false; + } + + if ($columns === '*') + { + return $query->fetchAll(PDO::FETCH_ASSOC); + } + + if ($is_single_column) + { + return $query->fetchAll(PDO::FETCH_COLUMN); + } + + while ($row = $query->fetch(PDO::FETCH_ASSOC)) + { + foreach ($columns as $key => $value) + { + if (!is_array($value)) + { + $value = preg_replace('/^[\w]*\./i', "", $value); + } + + $this->dataMap($index, $key, $value, $row, $stack); + } + + $index++; + } + + return $stack; + } + + public function insert($table, $datas) + { + $stack = []; + $columns = []; + $fields = []; + + // Check indexed or associative array + if (!isset($datas[ 0 ])) + { + $datas = [$datas]; + } + + foreach ($datas as $data) + { + foreach ($data as $key => $value) + { + $columns[] = $key; + } + } + + $columns = array_unique($columns); + + foreach ($datas as $data) + { + $values = []; + + foreach ($columns as $key) + { + if (!isset($data[$key])) + { + $values[] = 'NULL'; + } + else + { + $value = $data[$key]; + + switch (gettype($value)) + { + case 'NULL': + $values[] = 'NULL'; + break; + + case 'array': + preg_match("/\(JSON\)\s*([\w]+)/i", $key, $column_match); + + $values[] = isset($column_match[ 0 ]) ? + $this->quote(json_encode($value)) : + $this->quote(serialize($value)); + break; + + case 'boolean': + $values[] = ($value ? '1' : '0'); + break; + + case 'integer': + case 'double': + case 'string': + $values[] = $this->fnQuote($key, $value); + break; + } + } + } + + $stack[] = '(' . implode($values, ', ') . ')'; + } + + foreach ($columns as $key) + { + $fields[] = $this->columnQuote(preg_replace("/^(\(JSON\)\s*|#)/i", "", $key)); + } + + return $this->exec('INSERT INTO ' . $this->tableQuote($table) . ' (' . implode(', ', $fields) . ') VALUES ' . implode(', ', $stack)); + } + + public function update($table, $data, $where = null) + { + $fields = []; + + foreach ($data as $key => $value) + { + preg_match('/([\w]+)(\[(\+|\-|\*|\/)\])?/i', $key, $match); + + if (isset($match[ 3 ])) + { + if (is_numeric($value)) + { + $fields[] = $this->columnQuote($match[ 1 ]) . ' = ' . $this->columnQuote($match[ 1 ]) . ' ' . $match[ 3 ] . ' ' . $value; + } + } + else + { + $column = $this->columnQuote(preg_replace("/^(\(JSON\)\s*|#)/i", "", $key)); + + switch (gettype($value)) + { + case 'NULL': + $fields[] = $column . ' = NULL'; + break; + + case 'array': + preg_match("/\(JSON\)\s*([\w]+)/i", $key, $column_match); + + $fields[] = $column . ' = ' . $this->quote( + isset($column_match[ 0 ]) ? json_encode($value) : serialize($value) + ); + break; + + case 'boolean': + $fields[] = $column . ' = ' . ($value ? '1' : '0'); + break; + + case 'integer': + case 'double': + case 'string': + $fields[] = $column . ' = ' . $this->fnQuote($key, $value); + break; + } + } + } + + return $this->exec('UPDATE ' . $this->tableQuote($table) . ' SET ' . implode(', ', $fields) . $this->whereClause($where)); + } + + public function delete($table, $where) + { + return $this->exec('DELETE FROM ' . $this->tableQuote($table) . $this->whereClause($where)); + } + + public function replace($table, $columns, $search = null, $replace = null, $where = null) + { + if (is_array($columns)) + { + $replace_query = []; + + foreach ($columns as $column => $replacements) + { + foreach ($replacements as $replace_search => $replace_replacement) + { + $replace_query[] = $column . ' = REPLACE(' . $this->columnQuote($column) . ', ' . $this->quote($replace_search) . ', ' . $this->quote($replace_replacement) . ')'; + } + } + + $replace_query = implode(', ', $replace_query); + $where = $search; + } + else + { + if (is_array($search)) + { + $replace_query = []; + + foreach ($search as $replace_search => $replace_replacement) + { + $replace_query[] = $columns . ' = REPLACE(' . $this->columnQuote($columns) . ', ' . $this->quote($replace_search) . ', ' . $this->quote($replace_replacement) . ')'; + } + + $replace_query = implode(', ', $replace_query); + $where = $replace; + } + else + { + $replace_query = $columns . ' = REPLACE(' . $this->columnQuote($columns) . ', ' . $this->quote($search) . ', ' . $this->quote($replace) . ')'; + } + } + + return $this->exec('UPDATE ' . $this->tableQuote($table) . ' SET ' . $replace_query . $this->whereClause($where)); + } + + public function get($table, $join = null, $columns = null, $where = null) + { + $column = $where == null ? $join : $columns; + + $is_single_column = (is_string($column) && $column !== '*'); + + $query = $this->query($this->selectContext($table, $join, $columns, $where) . ' LIMIT 1'); + + if ($query) + { + $data = $query->fetchAll(PDO::FETCH_ASSOC); + + if (isset($data[ 0 ])) + { + if ($is_single_column) + { + return $data[ 0 ][ preg_replace('/^[\w]*\./i', "", $column) ]; + } + + if ($column === '*') + { + return $data[ 0 ]; + } + + $stack = []; + + foreach ($columns as $key => $value) + { + if (!is_array($value)) + { + $value = preg_replace('/^[\w]*\./i', "", $value); + } + + $this->dataMap(0, $key, $value, $data[ 0 ], $stack); + } + + return $stack[ 0 ]; + } + else + { + return false; + } + } + else + { + return false; + } + } + + public function has($table, $join, $where = null) + { + $column = null; + + $query = $this->query('SELECT EXISTS(' . $this->selectContext($table, $join, $column, $where, 1) . ')'); + + if ($query) + { + return $query->fetchColumn() === '1'; + } + else + { + return false; + } + } + + public function count($table, $join = null, $column = null, $where = null) + { + $query = $this->query($this->selectContext($table, $join, $column, $where, 'COUNT')); + + return $query ? 0 + $query->fetchColumn() : false; + } + + public function max($table, $join, $column = null, $where = null) + { + $query = $this->query($this->selectContext($table, $join, $column, $where, 'MAX')); + + if ($query) + { + $max = $query->fetchColumn(); + + return is_numeric($max) ? $max + 0 : $max; + } + else + { + return false; + } + } + + public function min($table, $join, $column = null, $where = null) + { + $query = $this->query($this->selectContext($table, $join, $column, $where, 'MIN')); + + if ($query) + { + $min = $query->fetchColumn(); + + return is_numeric($min) ? $min + 0 : $min; + } + else + { + return false; + } + } + + public function avg($table, $join, $column = null, $where = null) + { + $query = $this->query($this->selectContext($table, $join, $column, $where, 'AVG')); + + return $query ? 0 + $query->fetchColumn() : false; + } + + public function sum($table, $join, $column = null, $where = null) + { + $query = $this->query($this->selectContext($table, $join, $column, $where, 'SUM')); + + return $query ? 0 + $query->fetchColumn() : false; + } + + public function action($actions) + { + if (is_callable($actions)) + { + $this->pdo->beginTransaction(); + + $result = $actions($this); + + if ($result === false) + { + $this->pdo->rollBack(); + } + else + { + $this->pdo->commit(); + } + } + else + { + return false; + } + } + + public function id() + { + if ($this->database_type == 'oracle') + { + return 0; + } + elseif ($this->database_type == 'mssql') + { + return $this->pdo->query('SELECT SCOPE_IDENTITY()')->fetchColumn(); + } + + return $this->pdo->lastInsertId(); + } + + public function debug() + { + $this->debug_mode = true; + + return $this; + } + + public function error() + { + return $this->pdo->errorInfo(); + } + + public function last() + { + return end($this->logs); + } + + public function log() + { + return $this->logs; + } + + public function info() + { + $output = [ + 'server' => 'SERVER_INFO', + 'driver' => 'DRIVER_NAME', + 'client' => 'CLIENT_VERSION', + 'version' => 'SERVER_VERSION', + 'connection' => 'CONNECTION_STATUS' + ]; + + foreach ($output as $key => $value) + { + $output[ $key ] = @$this->pdo->getAttribute(constant('PDO::ATTR_' . $value)); + } + + return $output; + } +} +?> \ No newline at end of file diff --git a/src/admin.php b/src/admin.php new file mode 100644 index 0000000..44b2b14 --- /dev/null +++ b/src/admin.php @@ -0,0 +1,67 @@ +user=$GLOBALS['unifiUser']; + $ubnController->password=$GLOBALS['unifiPass']; + $ubnController->baseurl=$GLOBALS['unifiServer']; + $ubnController->site=$GLOBALS['unifiSite']; + + $ubnController->login(); + + if(isset($_GET["unauthorize_guest"])) { + $ubnController->unauthorize_guest($_GET["unauthorize_guest"]); + } + + if(isset($_GET["delete"])) { + deleteUser($_GET["delete"]); + } + if(isset($_POST["user"])){ + if(isset($_POST["pass"])){ + $user=$_POST["user"]; + $pass=$_POST["pass"]; + + createUser($user, $pass); + } + } + require("./html/admin.html"); +} +else { + // admin wants to log in. check credentials + if(isset($_GET["login"])){ + if($_POST["user"] == $GLOBALS['adminUser'] && $_POST["pass"] == $GLOBALS['adminPass']){ + $_SESSION['isAdmin'] = 1; + $ubnController=new unifiapi; + $ubnController->user=$GLOBALS['unifiUser']; + $ubnController->password=$GLOBALS['unifiPass']; + $ubnController->baseurl=$GLOBALS['unifiServer']; + $ubnController->site=$GLOBALS['unifiSite']; + + $ubnController->login(); + require("./html/admin.html"); + } else { + $_SESSION["message"] = "Wrong User or Password"; + require("./html/login_admin.html"); + } + } else { + require("./html/login_admin.html"); + } +} + + + + +?> \ No newline at end of file diff --git a/src/adminer.php b/src/adminer.php new file mode 100644 index 0000000..08bfa5c --- /dev/null +++ b/src/adminer.php @@ -0,0 +1,861 @@ +$W){unset($gf[$z][$ld]);if(is_array($W)){$gf[$z][stripslashes($ld)]=$W;$gf[]=&$gf[$z][stripslashes($ld)];}else$gf[$z][stripslashes($ld)]=($uc?$W:stripslashes($W));}}}}function +bracket_escape($v,$Aa=false){static$Og=array(':'=>':1',']'=>':2','['=>':3','"'=>':4');return +strtr($v,($Aa?array_flip($Og):$Og));}function +min_version($qh,$Fd="",$g=null){global$f;if(!$g)$g=$f;$Of=$g->server_info;if($Fd&&preg_match('~([\d.]+)-MariaDB~',$Of,$C)){$Of=$C[1];$qh=$Fd;}return(version_compare($Of,$qh)>=0);}function +charset($f){return(min_version("5.5.3",0,$f)?"utf8mb4":"utf8");}function +script($Xf,$Ng="\n"){return"$Xf$Ng";}function +script_src($gh){return"\n";}function +nonce(){return' nonce="'.get_nonce().'"';}function +target_blank(){return' target="_blank" rel="noreferrer noopener"';}function +h($hg){return +str_replace("\0","�",htmlspecialchars($hg,ENT_QUOTES,'utf-8'));}function +nl_br($hg){return +str_replace("\n","
",$hg);}function +checkbox($E,$Y,$Oa,$pd="",$qe="",$Sa="",$qd=""){$K="".($qe?script("qsl('input').onclick = function () { $qe };",""):"");return($pd!=""||$Sa?"$K".h($pd)."":$K);}function +optionlist($ue,$Jf=null,$kh=false){$K="";foreach($ue +as$ld=>$W){$ve=array($ld=>$W);if(is_array($W)){$K.='';$ve=$W;}foreach($ve +as$z=>$X)$K.=''.h($X);if(is_array($W))$K.='';}return$K;}function +html_select($E,$ue,$Y="",$pe=true,$qd=""){if($pe)return"".(is_string($pe)?script("qsl('select').onchange = function () { $pe };",""):"");$K="";foreach($ue +as$z=>$X)$K.="";return$K;}function +select_input($xa,$ue,$Y="",$pe="",$Te=""){$wg=($ue?"select":"input");return"<$wg$xa".($ue?">