Browse Source

Add models for every case, so much work..

ohartl 9 năm trước cách đây
mục cha
commit
297cebd57b

+ 6 - 0
include/php/models/AbstractMultiRedirect.php

@@ -0,0 +1,6 @@
+<?php
+
+abstract class AbstractMultiRedirect extends AbstractRedirect
+{
+
+}

+ 255 - 0
include/php/models/AbstractRedirect.php

@@ -0,0 +1,255 @@
+<?php
+
+abstract class AbstractRedirect extends AbstractModel
+{
+
+	/**
+	 * @inheritdoc
+	 */
+	public static $table = DBT_ALIASES;
+
+	/**
+	 * @inheritdoc
+	 */
+	public static $idAttribute = DBC_ALIASES_ID;
+
+
+	/**
+	 * @inheritdoc
+	 */
+	protected function setupDbMapping($childMapping = array())
+	{
+		$thisMapping = array(
+			'source' => DBC_ALIASES_SOURCE,
+			'destination' => DBC_ALIASES_DESTINATION,
+		);
+
+		if(defined('DBC_ALIASES_MULTI_SOURCE')){
+			$thisMapping['multiHash'] = DBC_ALIASES_MULTI_SOURCE;
+		}
+
+		return array_replace(
+			parent::setupDbMapping($thisMapping),
+			$childMapping
+		);
+	}
+
+
+	/**
+	 * @inheritdoc
+	 */
+	protected function preSave($data)
+	{
+		$data = parent::preSave($data);
+
+		$data['source'] = emailsToString($data['source']);
+		$data['destination'] = emailsToString($data['destination']);
+
+		return $data;
+	}
+
+
+	/**
+	 * @inheritdoc
+	 */
+	protected function __construct($data)
+	{
+		parent::__construct($data);
+
+		$source = stringToEmails($data[DBC_ALIASES_SOURCE]);
+		$destination = stringToEmails($data[DBC_ALIASES_DESTINATION]);
+
+		if(static::class === Alias::class || static::class === Redirect::class){
+			$source = $source[0];
+		}
+
+		if(static::class === Alias::class || static::class === MultiAlias::class){
+			$destination = $destination[0];
+		}
+
+		$this->setSource($source);
+		$this->setDestination($destination);
+
+		if(defined('DBC_ALIASES_MULTI_SOURCE')){
+			$this->setMultiHash($data[DBC_ALIASES_MULTI_SOURCE]);
+		}
+	}
+
+
+	/**
+	 * @inheritdoc
+	 */
+	public static function create($data)
+	{
+		if(static::class !== AbstractRedirect::class){
+			return parent::create($data);
+		}
+
+		$hasMultipleSources = array_key_exists(DBC_ALIASES_SOURCE, $data)
+			&& strpos($data[DBC_ALIASES_SOURCE], ',') !== false;
+
+		$hasMultipleDestinations = array_key_exists(DBC_ALIASES_DESTINATION, $data)
+			&& strpos($data[DBC_ALIASES_DESTINATION], ',') !== false;
+
+		if(defined('DBC_ALIASES_MULTI_SOURCE') && $hasMultipleSources
+		){
+			if($hasMultipleDestinations){
+				return MultiRedirect::create($data);
+			}
+			else{
+				return MultiAlias::create($data);
+			}
+		}
+		else{
+			if($hasMultipleDestinations){
+				return Redirect::create($data);
+			}
+			else{
+				return Alias::create($data);
+			}
+		}
+	}
+
+
+	/**
+	 * @return array|string
+	 */
+	public function getSource()
+	{
+		return $this->getAttribute('source');
+	}
+
+
+	/**
+	 * @param string|array $value
+	 */
+	public function setSource($value)
+	{
+		if(is_array($value)){
+			$this->setAttribute('source', array_map('strtolower', $value));
+		}
+		else{
+			$this->setAttribute('source', strtolower($value));
+		}
+	}
+
+
+	/**
+	 * @return array|string
+	 */
+	public function getDestination()
+	{
+		return $this->getAttribute('destination');
+	}
+
+
+	/**
+	 * @param string|array $value
+	 */
+	public function setDestination($value)
+	{
+		if(is_array($value)){
+			$this->setAttribute('destination', array_map('strtolower', $value));
+		}
+		else{
+			$this->setAttribute('destination', strtolower($value));
+		}
+	}
+
+	/**
+	 * @return string
+	 */
+	public function getMultiHash()
+	{
+		return $this->getAttribute('multiHash');
+	}
+
+
+	/**
+	 * @param string $value
+	 */
+	public function setMultiHash($value)
+	{
+		$this->setAttribute('multiHash', $value);
+	}
+
+
+	/**
+	 * @inheritdoc
+	 */
+	public static function findAll($orderBy = array(DBC_ALIASES_SOURCE))
+	{
+		return parent::findAll($orderBy);
+	}
+
+
+	private static $baseSqlQueryCache = null;
+
+	private static function generateRedirectBaseQuery()
+	{
+		if(is_null(static::$baseSqlQueryCache)){
+			if(defined('DBC_ALIASES_MULTI_SOURCE')){
+				static::$baseSqlQueryCache = "SELECT r.* FROM (
+		SELECT
+			GROUP_CONCAT(g.`".static::$idAttribute."` ORDER BY g.`".static::$idAttribute."` SEPARATOR ',') AS `".static::$idAttribute."`,
+			GROUP_CONCAT(g.`".DBC_ALIASES_SOURCE."` SEPARATOR ',') AS `".DBC_ALIASES_SOURCE."`,
+			g.`".DBC_ALIASES_DESTINATION."`,
+			g.`".DBC_ALIASES_MULTI_SOURCE."`
+		FROM `".static::$table."` AS g
+		WHERE g.`".DBC_ALIASES_MULTI_SOURCE."` IS NOT NULL
+		GROUP BY g.`".DBC_ALIASES_MULTI_SOURCE."`
+	UNION
+		SELECT
+			s.`".DBC_ALIASES_ID."`,
+			s.`".DBC_ALIASES_SOURCE."`,
+			s.`".DBC_ALIASES_DESTINATION."`,
+			s.`".DBC_ALIASES_MULTI_SOURCE."`
+		FROM `".static::$table."` AS s
+		WHERE s.`".DBC_ALIASES_MULTI_SOURCE."` IS NULL
+	) AS r";
+			}
+			else{
+				static::$baseSqlQueryCache = "SELECT * FROM `".static::$table."`";
+			}
+		}
+
+		return static::$baseSqlQueryCache;
+	}
+
+
+	public static function findMultiAll($orderBy = array(DBC_ALIASES_SOURCE))
+	{
+		$sql = static::generateRedirectBaseQuery()
+			.static::sqlHelperOrderBy($orderBy);
+
+		return static::findAllRaw($sql);
+	}
+
+
+	public static function findMultiWhere($conditions = array(), $conditionConnector = 'AND', $orderBy = null, $limit = 0)
+	{
+		$sql = static::generateRedirectBaseQuery()
+			.static::sqlHelperWhere($conditions, $conditionConnector)
+			.static::sqlHelperOrderBy($orderBy)
+			.static::sqlHelperLimit($limit);
+
+		if($limit === 1){
+			return static::findRaw($sql);
+		}
+
+		return static::findAllRaw($sql);
+	}
+
+
+	public static function findMultiWhereFirst($conditions = array(), $conditionConnector = 'AND', $orderBy = null)
+	{
+		return static::findMultiWhere($conditions, $conditionConnector, $orderBy, 1);
+	}
+
+
+	public static function findMulti($id)
+	{
+		return static::findMultiWhereFirst(array(static::$idAttribute, $id));
+	}
+
+}

+ 10 - 0
include/php/models/Alias.php

@@ -0,0 +1,10 @@
+<?php
+
+/**
+ * @method string getSource()
+ * @method string getDestination()
+ */
+class Alias extends Redirect
+{
+
+}

+ 90 - 0
include/php/models/Domain.php

@@ -0,0 +1,90 @@
+<?php
+
+class Domain extends AbstractModel
+{
+
+	/**
+	 * @inheritdoc
+	 */
+	public static $table = DBT_DOMAINS;
+
+	/**
+	 * @inheritdoc
+	 */
+	public static $idAttribute = DBC_DOMAINS_ID;
+
+
+	/**
+	 * @inheritdoc
+	 */
+	protected function setupDbMapping($childMapping = array())
+	{
+		return array_replace(
+			parent::setupDbMapping(
+				array(
+					'domain' => DBC_DOMAINS_DOMAIN,
+				)
+			),
+			$childMapping
+		);
+	}
+
+
+	/**
+	 * @inheritdoc
+	 */
+	protected function __construct($data)
+	{
+		parent::__construct($data);
+
+		$this->setDomain($data[DBC_DOMAINS_DOMAIN]);
+	}
+
+
+	/**
+	 * @return string
+	 */
+	public function getDomain()
+	{
+		return $this->getAttribute('domain');
+	}
+
+
+	/**
+	 * @param string $value
+	 */
+	public function setDomain($value)
+	{
+		$this->setAttribute('domain', strtolower($value));
+	}
+
+
+	/**
+	 * @return int
+	 */
+	public function countUsers()
+	{
+		global $db;
+
+		if(!$result = $db->query("SELECT COUNT(`".DBC_USERS_ID."`) FROM `".DBT_USERS."` WHERE `".DBC_USERS_DOMAIN."` = '{$this->getDomain()}'")){
+			dbError($db->error);
+		}
+
+		return $result->fetch_array(MYSQLI_NUM)[0];
+	}
+
+
+	/**
+	 * @return int
+	 */
+	public function countRedirects()
+	{
+		global $db;
+
+		if(!$result = $db->query("SELECT COUNT(`".DBC_ALIASES_ID."`) FROM `".DBT_ALIASES."` WHERE `".DBC_ALIASES_SOURCE."` LIKE '%@{$this->getDomain()}%'")){
+			dbError($db->error);
+		}
+
+		return $result->fetch_array(MYSQLI_NUM)[0];
+	}
+}

+ 10 - 0
include/php/models/MultiAlias.php

@@ -0,0 +1,10 @@
+<?php
+
+/**
+ * @method array getSource()
+ * @method string getDestination()
+ */
+class MultiAlias extends AbstractMultiRedirect
+{
+
+}

+ 10 - 0
include/php/models/MultiRedirect.php

@@ -0,0 +1,10 @@
+<?php
+
+/**
+ * @method array getSource()
+ * @method array getDestination()
+ */
+class MultiRedirect extends AbstractMultiRedirect
+{
+
+}

+ 10 - 0
include/php/models/Redirect.php

@@ -0,0 +1,10 @@
+<?php
+
+/**
+ * @method string getSource()
+ * @method array getDestination()
+ */
+class Redirect extends AbstractRedirect
+{
+
+}

+ 139 - 37
include/php/models/User.php

@@ -1,87 +1,120 @@
 <?php
 
-class User
+class User extends AbstractModel
 {
-	const ROLE_USER = 'user';
-	const ROLE_ADMIN = 'admin';
 
 	/**
-	 * @var int|string
+	 * @inheritdoc
 	 */
-	private $id;
+	public static $table = DBT_USERS;
 
 	/**
-	 * @var string
+	 * @inheritdoc
 	 */
-	private $username;
+	public static $idAttribute = DBC_USERS_ID;
+
+
+	const ROLE_USER = 'user';
+	const ROLE_ADMIN = 'admin';
+
 
 	/**
-	 * @var string
+	 * @inheritdoc
 	 */
-	private $domain;
+	protected function setupDbMapping($childMapping = array())
+	{
+		$thisMappings = array(
+			'username' => DBC_USERS_USERNAME,
+			'domain' => DBC_USERS_DOMAIN,
+		);
+
+		if(defined('DBC_USERS_MAILBOXLIMIT')){
+			$thisMappings['mailboxLimit'] = DBC_USERS_MAILBOXLIMIT;
+		}
+
+		return array_replace(
+			parent::setupDbMapping($thisMappings),
+			$childMapping
+		);
+	}
+
 
 	/**
-	 * @var int
+	 * @inheritdoc
 	 */
-	private $mailboxLimit = 0;
+	protected function __construct($data)
+	{
+		parent::__construct($data);
+
+		$this->setUsername($data[DBC_USERS_USERNAME]);
+		$this->setDomain($data[DBC_USERS_DOMAIN]);
+		$this->setPasswordHash($data[DBC_USERS_PASSWORD]);
+		$this->setMailboxLimit(defined('DBC_USERS_MAILBOXLIMIT') ? intval($data[DBC_USERS_MAILBOXLIMIT]) : 0);
+
+		$this->setAttribute('role', static::getRoleByEmail($this->getEmail()));
+	}
+
 
 	/**
-	 * @var string
+	 * @return string
 	 */
-	private $role;
+	public function getUsername()
+	{
+		return $this->getAttribute('username');
+	}
 
 
 	/**
-	 * User constructor.
-	 *
-	 * @param array $userData
+	 * @param string $value
 	 */
-	function __construct($userData)
+	public function setUsername($value)
 	{
-		$this->id = $userData[DBC_USERS_ID];
-		$this->username = $userData[DBC_USERS_USERNAME];
-		$this->domain = $userData[DBC_USERS_DOMAIN];
-		$this->role = static::getRoleByEmail($this->getEmail());
+		$this->setAttribute('username', strtolower($value));
+	}
 
-		if(defined('DBC_USERS_MAILBOXLIMIT')){
-			$this->mailboxLimit = $userData[DBC_USERS_MAILBOXLIMIT];
-		}
+
+	/**
+	 * @return string
+	 */
+	public function getDomain()
+	{
+		return $this->getAttribute('domain');
 	}
 
 
 	/**
-	 * @return int|string
+	 * @param string $value
 	 */
-	public function getId()
+	public function setDomain($value)
 	{
-		return $this->id;
+		$this->setAttribute('domain', strtolower($value));
 	}
 
 
 	/**
 	 * @return string
 	 */
-	public function getUsername()
+	public function getEmail()
 	{
-		return $this->username;
+		return $this->getUsername().'@'.$this->getDomain();
 	}
 
 
 	/**
 	 * @return string
 	 */
-	public function getDomain()
+	public function getPasswordHash()
 	{
-		return $this->domain;
+		return $this->getAttribute('password_hash');
 	}
 
 
 	/**
-	 * @return string
+	 * @param string $value
 	 */
-	public function getEmail()
+	public function setPasswordHash($value)
 	{
-		return $this->username.'@'.$this->domain;
+		$this->setAttribute('password_hash', $value);
 	}
 
 
@@ -90,7 +123,43 @@ class User
 	 */
 	public function getMailboxLimit()
 	{
-		return $this->mailboxLimit;
+		return $this->getAttribute('mailboxLimit');
+	}
+
+
+	/**
+	 * @param int $value
+	 */
+	public function setMailboxLimit($value)
+	{
+		$this->setAttribute('mailboxLimit', $value);
+	}
+
+
+	/**
+	 * Get mailbox limit default via database default value
+	 *
+	 * @return int
+	 */
+	public static function getMailboxLimitDefault()
+	{
+		global $db;
+
+		if(defined('DBC_USERS_MAILBOXLIMIT')){
+
+			$sql = "SELECT DEFAULT(".DBC_USERS_MAILBOXLIMIT.") FROM `".static::$table."` LIMIT 1";
+			if(!$result = $db->query($sql)){
+				dbError($db->error, $sql);
+			}
+
+			if($result->num_rows === 1){
+				$row = $result->fetch_array();
+
+				return intval($row[0]);
+			}
+		}
+
+		return 0;
 	}
 
 
@@ -99,7 +168,7 @@ class User
 	 */
 	public function getRole()
 	{
-		return $this->role;
+		return $this->getAttribute('role');
 	}
 
 
@@ -132,6 +201,39 @@ class User
 	{
 		Auth::validateNewPassword($password, $passwordRepeated);
 
-		Auth::changeUserPassword($this->id, $password);
+		Auth::changeUserPassword($this->getId(), $password);
+	}
+
+
+	/**
+	 * @inheritdoc
+	 */
+	public static function findAll($orderBy = array(DBC_USERS_DOMAIN, DBC_USERS_USERNAME))
+	{
+		return parent::findAll($orderBy);
+	}
+
+
+	/**
+	 * @param string $email
+	 *
+	 * @return static|null
+	 */
+	public static function findByEmail($email)
+	{
+		$emailInParts = explode("@", $email);
+		if(count($emailInParts) !== 2){
+			return null;
+		}
+		$username = $emailInParts[0];
+		$domain = $emailInParts[1];
+
+		return static::findWhereFirst(
+			array(
+				array('username', $username),
+				array('domain', $domain)
+			)
+		);
 	}
+
 }