Procházet zdrojové kódy

* Move earray to Data
* Add SecureString
* Add SQL::FromDSN() and make sure that passwords are passed as secure strings

Nathaniel van Diepen před 7 roky
rodič
revize
dc8143121f
4 změnil soubory, kde provedl 64 přidání a 5 odebrání
  1. 3 2
      Data/earray.class.php
  2. 30 0
      Data/securestring.class.php
  3. 2 2
      ORM/relationship.class.php
  4. 29 1
      sql.class.php

+ 3 - 2
earray.class.php → Data/earray.class.php

@@ -1,6 +1,7 @@
 <?php
-	namespace Juju {
-		require_once('events.trait.php');
+	namespace Juju\Data {
+		require_once(realpath(dirname(__DIR__).'/events.trait.php'));
+		use Juju\Events;
 
 		class EArray implements \ArrayAccess {
 			use Events;

+ 30 - 0
Data/securestring.class.php

@@ -0,0 +1,30 @@
+<?php
+	namespace Juju\Data {
+		class SecureString implements \JsonSerializable {
+			private $data;
+			private $password;
+			private $iv;
+			private $method;
+			private static $methods = null;
+			private function __construct(string $data){
+				if(is_null(self::$methods)){
+					self::$methods = openssl_get_cipher_methods();
+				}
+				$this->method = self::$methods[random_int(0, count(self::$methods))];
+				$ivlen = openssl_cipher_iv_length($this->method);
+				$this->password = openssl_random_pseudo_bytes($ivlen);
+				$this->iv = openssl_random_pseudo_bytes($ivlen);
+				$this->data = openssl_encrypt($data, $this->method, $this->password, \OPENSSL_RAW_DATA, $this->iv);
+			}
+			public function __toString(){
+				return openssl_decrypt($this->data, $this->method, $this->password, \OPENSSL_RAW_DATA, $this->iv);
+			}
+			public function jsonSerialize(){
+				return "{$this}";
+			}
+			public static function from(string $data){
+				return new self($data);
+			}
+		}
+	}
+?>

+ 2 - 2
ORM/relationship.class.php

@@ -1,7 +1,7 @@
 <?php
 	namespace Juju\ORM {
-		require_once(realpath(dirname(__DIR__).'/earray.class.php'));
-		use \Juju\EArray;
+		require_once(realpath(dirname(__DIR__).'/Data/earray.class.php'));
+		use \Juju\Data\EArray;
 		use \Juju\ORM;
 
 		class Relationship extends EArray {

+ 29 - 1
sql.class.php

@@ -1,7 +1,9 @@
 <?php
 	namespace Juju {
 		require_once('SQL/query.class.php');
+		require_once('Data/securestring.class.php');
 		use \Juju\SQL\Query;
+		use \Juju\Data\SecureString;
 
 		/**
 		* SQL class. Used for handling SQL connections
@@ -23,9 +25,35 @@
 			private $sql;
 			public $queries = [];
 			private static $connections = [];
+			public static function FromDSN(string $dsnstring){
+				$dsnstring = explode(':', $dsnstring)[1];
+				$dsn = explode(';', $dsnstring);
+				$dsn = array_reduce($dsn, function($dsn, $item){
+					$item = explode('=', $item);
+					$dsn[$item[0]] = $item[1];
+					return $dsn;
+				});
+				if(!isset($dsn['host'])){
+					throw new \Exception("DSN {$dsnstring} missing host");
+				}
+				if(!isset($dsn['dbname'])){
+					throw new \Exception("DSN {$dsnstring} missing dbname");
+				}
+				if(!isset($dsn['user'])){
+					$dsn['user'] = $dsn['dbname'];
+				}
+				if(!isset($dsn['pass'])){
+					$dsn['pass'] = $dsn['user'];
+				}
+				$dsn['pass'] = SecureString::from($dsn['pass']);
+				return new SQL($dsn['host'], $dsn['user'], $dsn['pass'], $dsn['dbname']);
+			}
 			public function __construct($server,$user,$pass,$db){
 				$this->guid = uniqid();
-				$this->sql = new \mysqli('p:'.$server,$user,$pass,$db) or die('Unable to connect to mysql');
+				$this->sql = new \mysqli('p:'.$server,$user,"{$pass}",$db);;
+				if($this->sql->connect_error){
+					throw new \Exception('Mysqli Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
+				}
 				self::$connections[] = $sql;
 			}
 			public function __destruct(){