isundil пре 10 година
комит
22279219f0

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+/access_log
+/error_log
+.directory
+*~

+ 1 - 0
.htaccess

@@ -0,0 +1 @@
+ 

+ 1 - 0
content/index.php

@@ -0,0 +1 @@
+ 

+ 1 - 0
content/plugins/index.php

@@ -0,0 +1 @@
+ 

+ 1 - 0
content/theme/index.php

@@ -0,0 +1 @@
+ 

+ 1 - 0
content/theme_back/index.php

@@ -0,0 +1 @@
+ 

+ 15 - 0
core/autoload.php

@@ -0,0 +1,15 @@
+<?php
+
+function __autoload($className)
+{
+    list($namespace, $class) = explode('\\', $className, 2);
+    $path = null;
+    switch ($namespace)
+    {
+    case "Entity":
+        $path = "core/models/".$class.".php";
+    }
+    include_once ($path);
+    return $path;
+}
+

+ 1 - 0
core/config.inc.php

@@ -0,0 +1 @@
+<? return array("mysql:host=localhost;dbname=ecom", "root", "", "my_"); ?>

+ 1 - 0
core/controller/index.php

@@ -0,0 +1 @@
+ 

+ 1 - 0
core/index.php

@@ -0,0 +1 @@
+ 

+ 28 - 0
core/models/Address.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Entity;
+
+class Address extends ModelBase
+{
+    protected function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}address` (
+            `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+            `user_id` INTEGER(11) UNSIGNED NULL,
+            `addressName` VARCHAR(255) NOT NULL,
+            `fullName` VARCHAR(255) NOT NULL,
+            `streetAddress` TEXT NOT NULL,
+            `city` VARCHAR(128) NOT NULL,
+            `zipCode` VARCHAR(16) NOT NULL,
+            `country` VARCHAR(128) NOT NULL,
+            `lastUsed` DATETIME NOT NULL,
+            FOREIGN KEY (`user_id`) REFERENCES `{$dbPrefix}user`(id),
+            UNIQUE(`user_id`, `addressName`)
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+}
+

+ 61 - 0
core/models/Admin.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace Entity;
+
+class Admin extends ModelBase
+{
+    /**
+     * Role -> 0 : no access
+     *      -> 1 : read access
+     *      -> 2 : +write access
+     *      -> 3 : +delete /disable access
+     *
+     * [0] -> admin (add user, change site configuration)
+     * [1] -> cms (add / modify / delete pages)
+     * [2] -> products
+     * [3] -> orders
+     * [4] -> translations
+     *
+     * '*' Is the site admin, have all rights and no one can change this role
+    **/
+    public function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}admin` (
+            `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+            `email` VARCHAR(128) NOT NULL UNIQUE,
+            `password` VARCHAR(64),
+            `role` VARCHAR(8) NOT NULL DEFAULT '00000',
+            `lastConnect` DATETIME NOT NULL,
+            `lastConnectIp` VARCHAR(42) NOT NULL
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+
+    public function __construct($id = null)
+    {
+        $this->lastConnect = new \DateTime();
+        $this->lastConnectIp = $_SERVER["REMOTE_ADDR"];
+        parent::__construct($id);
+    }
+
+    public function checkPassword($value)
+    {
+        return password_verify($value, $this->password);
+    }
+
+    public function setPassword($value)
+    {
+        parent::__set("password", password_hash($value, PASSWORD_BCRYPT));
+    }
+
+    public function __set($key, $value)
+    {
+        if ($key == "password")
+            return $this->setPassword($value);
+        return parent::__set($key, $value);
+    }
+}
+

+ 21 - 0
core/models/Cart.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace Entity;
+
+class Cart extends ModelBase
+{
+    protected function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}cart` (
+            `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+            `user_id` INTEGER(11) UNSIGNED NULL,
+            `created` DATETIME NOT NULL,
+            FOREIGN KEY (`user_id`) REFERENCES `{$dbPrefix}user`(id)
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+}
+

+ 23 - 0
core/models/CartProduct.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace Entity;
+
+class CartProduct extends ModelBase
+{
+    protected function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}cart_product` (
+            `cart_id` INTEGER(11) UNSIGNED NOT NULL,
+            `product_id` INTEGER(11) UNSIGNED NULL,
+            `quantity` INTEGER(10) UNSIGNED NOT NULL,
+            FOREIGN KEY (`cart_id`) REFERENCES `{$dbPrefix}cart`(id),
+            FOREIGN KEY (`product_id`) REFERENCES `{$dbPrefix}product`(id),
+            UNIQUE(`cart_id`, `product_id`)
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+}
+

+ 20 - 0
core/models/Category.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace Entity;
+
+class Category extends ModelBase
+{
+    protected function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}category` (
+            `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+            `parent_id` INTEGER(11) UNSIGNED NULL,
+            FOREIGN KEY (`parent_id`) REFERENCES `{$dbPrefix}category`(id)
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+}
+

+ 23 - 0
core/models/Cms.php

@@ -0,0 +1,23 @@
+<?php
+
+namespace Entity;
+
+class Cms extends ModelBase
+{
+    protected function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}cms` (
+            `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+            `lang` VARCHAR(8) NULL,
+            `shurl` VARCHAR(255) NOT NULL,
+            `title` TEXT NOT NULL ,
+            `content` LONGTEXT NOT NULL,
+            UNIQUE(`lang`, `shurl`)
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+}
+

+ 21 - 0
core/models/Config.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace Entity;
+
+class Config extends ModelBase
+{
+    protected function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}config` (
+            `lang` VARCHAR(8) NULL,
+            `key` VARCHAR(64) NOT NULL,
+            `value` TEXT NULL,
+            UNIQUE(`lang`, `key`)
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+}
+

+ 45 - 0
core/models/Meta.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace Entity;
+
+class Meta extends ModelBase
+{
+    private $entity;
+
+    protected function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}meta` (
+            `entity_id` INTEGER(11) UNSIGNED NOT NULL,
+            `entityType` VARCHAR(32) NOT NULL,
+            `lang` VARCHAR(8) NULL,
+            `key` VARCHAR(64) NOT NULL,
+            `value` TEXT NULL,
+            UNIQUE(`entity_id`, `entityType`, `lang`)
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+
+    public function __get($key)
+    {
+        if ($key == "entity")
+            return $this->entity;
+        return parent::__get($key);
+    }
+
+    public function __set($key, $value)
+    {
+        if ($key == "entity")
+        {
+            parent::__set("entityId", $value->id);
+            parent::__set("entityType", get_class($value));
+            return $value;
+        }
+        else if ($key == "entityId" || $key == "entityType")
+            throw new \Exception("Cannot access private field {$key}");
+        return parent::__set($key, $value);
+    }
+}
+

+ 130 - 0
core/models/ModelBase.php

@@ -0,0 +1,130 @@
+<?php
+
+namespace Entity;
+
+abstract class ModelBase
+{
+	private $fieldsValues = array();
+	protected static $dbo = null;
+    private $changed = array();
+    private static $config = null;
+    private $id;
+
+    protected abstract function install();
+
+	public function __construct($id = null)
+	{
+        self::init();
+        $this->id = null;
+        if ($id !== null)
+            $this->selectById($id);
+	}
+
+	public static function init()
+	{
+		if (self::$dbo !== null)
+            return true;
+        self::$config = @include("core/config.inc.php");
+        if (empty(self::$config))
+            return false;
+        self::$dbo = new \PDO(self::$config[0], self::$config[1], self::$config[2]);
+        return true;
+	}
+
+    public static function getDbPrefix()
+    {
+        return self::$config[3];
+    }
+
+    public static function setup()
+    {
+        $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config");
+
+        self::init();
+        self::$dbo->beginTransaction();
+        try
+        {
+            foreach ($tables as $i)
+            {
+                $i = "Entity\\".$i;
+                $table = new $i();
+                if ($table->install() != true)
+                    throw new \Exception();
+            }
+        }
+        catch (\Exception $e)
+        {
+            echo $e->getMessage();
+            self::$dbo->rollBack();
+            return false;
+        }
+        self::$dbo->commit();
+        return true;
+    }
+
+    public function getMeta($lang=null)
+    {
+        $fetcher = new \Entity\Meta();
+        return $fetcher->query(array("type" => get_class()));
+    }
+
+    public function __get($key)
+    {
+        if ($key == "meta")
+            return $this->getMeta();
+        if (!isset($this->fieldsValues[$key]))
+            return null;
+        return $this->fieldsValues[$key];
+    }
+
+    public function __set($key, $value)
+    {
+        if ($value instanceof \DateTime)
+            $value = $value->format("Y-m-d");
+        $this->fieldsValues[$key] = $value;
+        $this->changed[$key] = self::$dbo->quote($value);
+        return $value;
+    }
+
+    public function save()
+    {
+        $className = new \ReflectionClass($this);
+        $className = strtolower($className->getShortName());
+        //TODO send pre-hook
+        if ($this->id === null)
+        {
+            if (empty ($this->changed))
+            {
+                //TODO
+                return true;
+            }
+            else
+            {
+                $query = "INSERT INTO `{$this->getDbPrefix()}{$className}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
+                $statement = self::$dbo->prepare($query);
+                $result = $statement->execute();
+                if (!$result)
+                    throw new \Exception($statement->errorInfo()[2]);
+                $this->changed = array();
+            }
+                //TODO get last id
+        }
+        else
+        {
+            if ($this->id === false)
+                throw new \Exception("Cannot update private row");
+        }
+        //TODO send hook
+    }
+
+    //TODO
+    public function selectById($id)
+    {
+    }
+
+    private function populate($data)
+    {
+        $this->id = FALSE;
+    }
+}
+

+ 24 - 0
core/models/Product.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace Entity;
+
+class Product extends ModelBase
+{
+    protected function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}product` (
+            `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+            `parent` INTEGER(11) UNSIGNED NULL,
+            `shurl` VARCHAR(255) NOT NULL,
+            `priceExcl` FLOAT NULL,
+            `priceIncl` FLOAT NULL,
+            `ean` VARCHAR(13) NULL,
+            FOREIGN KEY (`parent`) REFERENCES `{$dbPrefix}product`(id)
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+}
+

+ 52 - 0
core/models/User.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace Entity;
+
+class User extends ModelBase
+{
+    public function install()
+    {
+        $dbPrefix = $this->getDbPrefix();
+        $result = self::$dbo->exec("CREATE TABLE IF NOT EXISTS `{$dbPrefix}user` (
+            `id` INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
+            `email` VARCHAR(128) NOT NULL UNIQUE,
+            `password` VARCHAR(64),
+            `sexe` ENUM('MALE', 'FEMALE') NULL,
+            `optin` BOOLEAN DEFAULT FALSE,
+            `register` DATETIME NOT NULL,
+            `lastConnect` DATETIME NOT NULL,
+            `lastVisit` DATETIME NOT NULL,
+            `registerIp` VARCHAR(42) NOT NULL,
+            `lastConnectIp` VARCHAR(42) NOT NULL,
+            `lastVisitIp` VARCHAR(42) NOT NULL
+        )");
+        if ($result === false)
+            throw new \Exception(get_class().": ".self::$dbo->errorInfo()[2]);
+        return true;
+    }
+
+    public function __construct($id = null)
+    {
+        $this->register = $this->lastVisit = $this->lastConnect = new \DateTime();
+        $this->registerIp = $this->lastVisitIp = $this->lastConnectIp = $_SERVER["REMOTE_ADDR"];
+        parent::__construct($id);
+    }
+
+    public function checkPassword($value)
+    {
+        return password_verify($value, $this->password);
+    }
+
+    public function setPassword($value)
+    {
+        parent::__set("password", password_hash($value, PASSWORD_BCRYPT));
+    }
+
+    public function __set($key, $value)
+    {
+        if ($key == "password")
+            return $this->setPassword($value);
+        return parent::__set($key, $value);
+    }
+}
+

+ 1 - 0
core/models/cms.php

@@ -0,0 +1 @@
+ 

+ 1 - 0
core/models/index.php

@@ -0,0 +1 @@
+ 

+ 1 - 0
core/setup/index.php

@@ -0,0 +1 @@
+ 

+ 15 - 0
index.php

@@ -0,0 +1,15 @@
+<?php
+
+chdir(dirname(__FILE__));
+require_once("core/autoload.php");
+
+Entity\ModelBase::init();
+
+//var_dump(Entity\ModelBase::setup());
+$user = new \Entity\User();
+$user->password = "test";
+$user->email = "isundill@gmail.com";
+$user->sexe = "MALE";
+$user->optin = true;
+$user->save();
+