diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | composer.json | 3 | ||||
-rw-r--r-- | composer.lock | 49 | ||||
-rw-r--r-- | public/test.php | 41 | ||||
-rw-r--r-- | public/test3.php | 12 | ||||
-rw-r--r-- | w/class/art2.php | 97 | ||||
-rw-r--r-- | w/class/config.1.php | 2 | ||||
-rw-r--r-- | w/class/config.php | 2 | ||||
-rw-r--r-- | w/class/controller.php | 2 | ||||
-rw-r--r-- | w/class/controllerart.php | 12 | ||||
-rw-r--r-- | w/class/controllerdb.php | 34 | ||||
-rw-r--r-- | w/class/dbengine.php | 309 | ||||
-rw-r--r-- | w/class/modelart.php | 53 | ||||
-rw-r--r-- | w/class/modeldb.php | 17 | ||||
-rw-r--r-- | w/class/router.php | 2 | ||||
-rw-r--r-- | w/class/sleekdbw.php | 180 | ||||
-rw-r--r-- | w/fn/w.fn.php | 14 | ||||
-rw-r--r-- | w/view/templates/edit.php | 7 | ||||
-rw-r--r-- | w/view/templates/edittopbar.php | 2 | ||||
-rw-r--r-- | w/view/templates/read.php | 2 | ||||
-rw-r--r-- | w/view/templates/readcreate.php | 4 | ||||
-rw-r--r-- | w/w.index.php | 12 |
22 files changed, 776 insertions, 81 deletions
@@ -1,6 +1,7 @@ vendor/* .vscode/* w/w.config.json +w_database/* error_log public/w/media/* public/w/css/* diff --git a/composer.json b/composer.json index 5736db0..de5a123 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "require": { "michelf/php-markdown": "^1.8", - "league/plates": "3.*" + "league/plates": "3.*", + "rakibtg/sleekdb": "^1.0" } } diff --git a/composer.lock b/composer.lock index afbea40..1685300 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "43442b947acbb3ce39a2fdb5e9173c7b", + "content-hash": "32b0a4f1101de417365ae3e8c472be11", "packages": [ { "name": "league/plates", @@ -106,6 +106,53 @@ "markdown" ], "time": "2018-01-15T00:49:33+00:00" + }, + { + "name": "rakibtg/sleekdb", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/rakibtg/SleekDB.git", + "reference": "a8a536e9efd9bd34b2bea0961475778be19b0371" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/rakibtg/SleekDB/zipball/a8a536e9efd9bd34b2bea0961475778be19b0371", + "reference": "a8a536e9efd9bd34b2bea0961475778be19b0371", + "shasum": "" + }, + "require": { + "php": ">=5.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "SleekDB\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "rakibtg", + "email": "rakibtg@gmail.com" + } + ], + "description": "SleekDB - A 30Kb NoSQL Database made using PHP", + "keywords": [ + "SleekDB", + "api", + "database", + "flatfile", + "framework", + "micro", + "nosql", + "php", + "rest" + ], + "time": "2018-08-19T10:25:39+00:00" } ], "packages-dev": [], diff --git a/public/test.php b/public/test.php index 7af0a01..35c8575 100644 --- a/public/test.php +++ b/public/test.php @@ -1,15 +1,40 @@ <?php -function test() + + +require('../w/class/art2.php'); + +$art = new Art2(['id' => 'rr']); +$art->reset(); +$art->hydrate((['description' => 'fdsfs', 'secure' => 0])); + +var_dump($art); + +$artencoded = json_encode($art); + +var_dump($artencoded); + +class Person { - static $count = 0; + public $id; + public $name; + public $table = ['coucou', 'lol', 'chouette']; + public $date; - $count++; - echo $count; - if ($count < 10) { - test(); + public function __construct(array $data) + { + $this->id = $data['id']; + $this->name = $data['name']; + $this->date = new DateTimeImmutable(); } - $count--; } -test(); +$person = new Person(array('id' => 1, 'name' => 'Amir')); +$jsonperson = json_encode($person); + +var_dump($jsonperson); + +var_dump(json_decode($jsonperson)); + + + ?>
\ No newline at end of file diff --git a/public/test3.php b/public/test3.php index dcdb979..327e2dd 100644 --- a/public/test3.php +++ b/public/test3.php @@ -1,5 +1,17 @@ <?php +$path = '../w/class/'; +$lengh = strlen($path); +$array = []; +foreach (glob($path . '*.php') as $filename) { + $array[] = substr(substr($filename, $lengh), 0, -4); +} + +var_dump($array); + + + + diff --git a/w/class/art2.php b/w/class/art2.php index b6c7125..fd7a403 100644 --- a/w/class/art2.php +++ b/w/class/art2.php @@ -2,32 +2,32 @@ class Art2 { - private $id; - private $title; - private $description; - private $tag; - private $date; - private $datecreation; - private $datemodif; - private $daterender; - private $css; - private $quickcss; - private $javascript; - private $html; - private $header; - private $section; - private $nav; - private $aside; - private $footer; - private $render; - private $secure; - private $invitepassword; - private $interface; - private $linkfrom; - private $linkto; - private $template; - private $affcount; - private $editcount; + protected $id; + protected $title; + protected $description; + protected $tag; + protected $date; + protected $datecreation; + protected $datemodif; + protected $daterender; + protected $css; + protected $quickcss; + protected $javascript; + protected $html; + protected $header; + protected $section; + protected $nav; + protected $aside; + protected $footer; + protected $render; + protected $secure; + protected $invitepassword; + protected $interface; + protected $linkfrom; + protected $linkto; + protected $template; + protected $affcount; + protected $editcount; const LEN = 255; @@ -37,6 +37,7 @@ class Art2 const DEBUT = '(?id='; const FIN = ')'; const TABS = ['section', 'css', 'header', 'html', 'nav', 'aside', 'footer', 'javascript']; + const VAR_DATE = ['date', 'datecreation', 'datemodif', 'daterender']; @@ -165,6 +166,19 @@ class Art2 return $datas; } + public function dry() + { + $array = []; + foreach (get_class_vars(__class__) as $var => $value) { + if(in_array($var, self::VAR_DATE)) { + $array[$var] = $this->$var('string'); + } else { + $array[$var] = $this->$var(); + } + } + return $array; + } + // _____________________________________________________ G E T ____________________________________________________ @@ -250,7 +264,7 @@ class Art2 return $this->css; } - public function quickcss($type = 'json') + public function quickcss($type = 'array') { if ($type == 'json') { return json_encode($this->quickcss); @@ -318,7 +332,7 @@ class Art2 $parser = new MarkdownExtra; // id in headers - $parser->header_id_func = function ($header) { + $parser->headerid_func = function ($header) { return preg_replace('/[^\w]/', '', strtolower($header)); }; $section = $parser->transform($section); @@ -400,7 +414,7 @@ class Art2 return $this->interface; } - public function linkfrom($option = 'json') + public function linkfrom($option = 'array') { if ($option == 'json') { $linkfrom = json_encode($this->linkfrom); @@ -413,7 +427,7 @@ class Art2 } - public function linkto($option = 'json') + public function linkto($option = 'array') { if ($option == 'json') { $linkto = json_encode($this->linkto); @@ -426,7 +440,7 @@ class Art2 } - public function template($type = 'json') + public function template($type = 'array') { if ($type == 'json') { return json_encode($this->template); @@ -678,6 +692,27 @@ class Art2 } + // __________________________________ C O U N T E R S ______________________________ + + + public function addeditcount() + { + $this->editcount ++; + } + + public function addaffcount() + { + $this->affcount ++; + } + + public function updateedited() + { + $now = new DateTimeImmutable(null, timezone_open("Europe/Paris")); + $this->setdatemodif($now); + $this->addeditcount(); + } + + } diff --git a/w/class/config.1.php b/w/class/config.1.php index 3b4cde0..14ff6e3 100644 --- a/w/class/config.1.php +++ b/w/class/config.1.php @@ -2,7 +2,7 @@ -class Config +class Config1 { private $host; private $dbname; diff --git a/w/class/config.php b/w/class/config.php index 674aa66..3dd93b2 100644 --- a/w/class/config.php +++ b/w/class/config.php @@ -8,7 +8,7 @@ abstract class Config protected static $dbname; protected static $user; protected static $password; - protected static $arttable; + protected static $arttable = 'artstore'; protected static $domain; protected static $admin; protected static $editor; diff --git a/w/class/controller.php b/w/class/controller.php index 62b1f49..c97c99f 100644 --- a/w/class/controller.php +++ b/w/class/controller.php @@ -13,8 +13,6 @@ class Controller $this->initconfig(); } - - public function setuser() { $this->usermanager = new Modeluser; diff --git a/w/class/controllerart.php b/w/class/controllerart.php index 301c9ab..064474a 100644 --- a/w/class/controllerart.php +++ b/w/class/controllerart.php @@ -15,10 +15,10 @@ class Controllerart extends Controllerdb public function importart() { - if($this->artmanager->exist($this->art->id())) { - $this->art = $this->artmanager->get($this->art); - $this->art->autotaglistupdate($this->artmanager->taglist($this->artmanager->getlister(['id', 'title', 'description', 'tag']), $this->art->autotaglist())); - + $art = $this->artmanager->get($this->art); + if($art !== false) { + $this->art = $art; + //$this->art->autotaglistupdate($this->artmanager->taglist($this->artmanager->getlister(['id', 'title', 'description', 'tag']), $this->art->autotaglist())); return true; } else { return false; @@ -56,11 +56,9 @@ class Controllerart extends Controllerdb public function add() { - echo '<h2>Add</h2>'; - - $this->art->reset(); $this->artmanager->add($this->art); + $this->redirect('?id=' . $this->art->id() . '&aff=edit'); } public function delete() diff --git a/w/class/controllerdb.php b/w/class/controllerdb.php index 5d75d39..5952627 100644 --- a/w/class/controllerdb.php +++ b/w/class/controllerdb.php @@ -4,17 +4,47 @@ class Controllerdb extends Controller { protected $artmanager; + protected $database; + protected $artstore; - public function __construct() { + + public function __construct() + { parent::__construct(); + $this->artmanager = new Modelart(); + + } + + public function dbinit() + { + $this->database = new SleekDBw(__DIR__ . '/../../w_database', [ + 'auto_cache' => false + ]); + $this->artstore = $this->database->store(Config::arttable()); + + } + + + public function fetch() + { + $datas = $this->artstore->fetch(); + return $datas; + } + + public function desktop() + { - $this->artmanager = new Modelart(); + + $this->dbinit(); + var_dump( $this->fetch()); + } + // if (isset($_POST['actiondb'])) { // $app->setbdd($config); diff --git a/w/class/dbengine.php b/w/class/dbengine.php new file mode 100644 index 0000000..7895a1a --- /dev/null +++ b/w/class/dbengine.php @@ -0,0 +1,309 @@ +<?php + +class Dbengine +{ + public function __construct(string $dataDir) + { + $this->init($dataDir); + } + + public function init($dataDir) + { + // Handle directory path ending. + if (substr($dataDir, -1) !== '/') $dataDir = $dataDir . '/'; + + if (!file_exists($dataDir)) { + // The directory was not found, create one. + if (!mkdir($dataDir, 0777, true)) throw new \Exception('Unable to create the data directory at ' . $dataDir); + } + // Check if PHP has write permission in that directory. + if (!is_writable($dataDir)) throw new \Exception('Data directory is not writable at "' . $dataDir . '." Please change data directory permission.'); + // Finally check if the directory is readable by PHP. + if (!is_readable($dataDir)) throw new \Exception('Data directory is not readable at "' . $dataDir . '." Please change data directory permission.'); + // Set the data directory. + $this->dataDirectory = $dataDir; + } + + // Initialize the store. + public function store($storeName = false) + { + if (!$storeName or empty($storeName)) throw new \Exception('Store name was not valid'); + $this->storeName = $storeName; + // Boot store. + $this->bootStore(); + // Initialize variables for the store. + $this->initVariables(); + return $this; + } + + + public function insert(Art2 $art) + { + $artdata = $art->dry(); + $storableJSON = json_encode($artdata); + if ($storableJSON === false) throw new \Exception('Unable to encode the art object'); + $storePath = $this->storePath . $art->id() . '.json'; + if (!file_put_contents($storePath, $storableJSON)) { + throw new \Exception("Unable to write the object file! Please check if PHP has write permission."); + } + return true; + } + + public function get($id) + { + $filepath = $this->storePath . $id . '.json'; + if (file_exists($filepath)) { + $data = json_decode(file_get_contents($filepath), true); + if ($data !== false) return $data; + } else { + return false; + } + } + + public function update($id, $data) + { + foreach ($data as $key => $value) { + // Do not update the _id reserved index of a store. + if ($key != 'id') { + $data[$key] = $value; + } + } + $storePath = $this->storePath . $id . '.json'; + if (file_exists($storePath)) { + // Wait until it's unlocked, then update data. + file_put_contents($storePath, json_encode($data), LOCK_EX); + } + return true; + } + + + public function getidlist() + { + $lengh = strlen($this->storePath); + $list = []; + foreach (glob($listPath . '*.json') as $filename) { + $list[] = substr(substr($filename, $lengh), 0, -5); + } + return $list; + } + + // Method to boot a store. + protected function bootStore() + { + $store = trim($this->storeName); + // Validate the store name. + if (!$store || empty($store)) throw new \Exception('Invalid store name was found'); + // Prepare store name. + if (substr($store, -1) !== '/') $store = $store . '/'; + // Store directory path. + $this->storePath = $this->dataDirectory . $store; + // Check if the store exists. + if (!file_exists($this->storePath)) { + // The directory was not found, create one with cache directory. + if (!mkdir($this->storePath, 0777, true)) throw new \Exception('Unable to create the store path at ' . $this->storePath); + } + // Check if PHP has write permission in that directory. + if (!is_writable($this->storePath)) throw new \Exception('Store path is not writable at "' . $this->storePath . '." Please change store path permission.'); + // Finally check if the directory is readable by PHP. + if (!is_readable($this->storePath)) throw new \Exception('Store path is not readable at "' . $this->storePath . '." Please change store path permission.'); + } + + // Init data that SleekDB required to operate. + protected function initVariables() + { + // Set empty results + $this->results = []; + // Set a default limit + $this->limit = 0; + // Set a default skip + $this->skip = 0; + // Set default conditions + $this->conditions = []; + // Set default group by value + $this->orderBy = [ + 'order' => false, + 'field' => '_id' + ]; + // Set the default search keyword as an empty string. + $this->searchKeyword = ''; + } + + + + +// ______________________________________ analyse _______________________________ + + + // Find store objects with conditions, sorting order, skip and limits. + protected function findStoreDocuments() + { + $found = []; + $searchRank = []; + // Start collecting and filtering data. + foreach ($this->getidlist() as $id) { + // Collect data of current iteration. + $data = $this->get($id); + if (!empty($data)) { + // Filter data found. + if (empty($this->conditions)) { + // Append all data of this store. + $found[] = $data; + } else { + // Append only passed data from this store. + $storePassed = true; + // Iterate each conditions. + foreach ($this->conditions as $condition) { + // Check for valid data from data source. + $validData = true; + $fieldValue = ''; + try { + $fieldValue = $this->getNestedProperty($condition['fieldName'], $data); + } catch (\Exception $e) { + $validData = false; + $storePassed = false; + } + if ($validData === true) { + // Check the type of rule. + if ($condition['condition'] === '=') { + // Check equal. + if ($fieldValue != $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '!=') { + // Check not equal. + if ($fieldValue == $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '>') { + // Check greater than. + if ($fieldValue <= $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '>=') { + // Check greater equal. + if ($fieldValue < $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '<') { + // Check less than. + if ($fieldValue >= $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '<=') { + // Check less equal. + if ($fieldValue > $condition['value']) $storePassed = false; + } + } + } + // Check if current store is updatable or not. + if ($storePassed === true) { + // Append data to the found array. + $found[] = $data; + } + } + } + } + if (count($found) > 0) { + // Check do we need to sort the data. + if ($this->orderBy['order'] !== false) { + // Start sorting on all data. + $found = $this->sortArray($this->orderBy['field'], $found, $this->orderBy['order']); + } + // If there was text search then we would also sort the result by search ranking. + if (!empty($this->searchKeyword)) { + $found = $this->performSerach($found); + } + // Skip data + if ($this->skip > 0) $found = array_slice($found, $this->skip); + // Limit data. + if ($this->limit > 0) $found = array_slice($found, 0, $this->limit); + } + return $found; + } + + + + // Sort store objects. + protected function sortArray($field, $data, $order = 'ASC') + { + $dryData = []; + // Check if data is an array. + if (is_array($data)) { + // Get value of the target field. + foreach ($data as $value) { + $dryData[] = $this->getNestedProperty($field, $value); + } + } + // Descide the order direction. + if (strtolower($order) === 'asc') asort($dryData); + else if (strtolower($order) === 'desc') arsort($dryData); + // Re arrange the array. + $finalArray = []; + foreach ($dryData as $key => $value) { + $finalArray[] = $data[$key]; + } + return $finalArray; + } + + // Get nested properties of a store object. + protected function getNestedProperty($field = '', $data) + { + if (is_array($data) and !empty($field)) { + // Dive deep step by step. + foreach (explode('.', $field) as $i) { + // If the field do not exists then insert an empty string. + if (!isset($data[$i])) { + $data = ''; + throw new \Exception('"' . $i . '" index was not found in the provided data array'); + break; + } else { + // The index is valid, collect the data. + $data = $data[$i]; + } + } + return $data; + } + } + + // Do a search in store objects. This is like a doing a full-text search. + protected function performSerach($data = []) + { + if (empty($data)) return $data; + $nodesRank = []; + // Looping on each store data. + foreach ($data as $key => $value) { + // Looping on each field name of search-able fields. + foreach ($this->searchKeyword['field'] as $field) { + try { + $nodeValue = $this->getNestedProperty($field, $value); + // The searchable field was found, do comparison against search keyword. + similar_text(strtolower($nodeValue), strtolower($this->searchKeyword['keyword']), $perc); + if ($perc > 50) { + // Check if current store object already has a value, if so then add the new value. + if (isset($nodesRank[$key])) $nodesRank[$key] += $perc; + else $nodesRank[$key] = $perc; + } + } catch (\Exception $e) { + continue; + } + } + } + if (empty($nodesRank)) { + // No matched store was found against the search keyword. + return []; + } + // Sort nodes in descending order by the rank. + arsort($nodesRank); + // Map original nodes by the rank. + $nodes = []; + foreach ($nodesRank as $key => $value) { + $nodes[] = $data[$key]; + } + return $nodes; + } + + + + + +} + + + + + + + + + +?>
\ No newline at end of file diff --git a/w/class/modelart.php b/w/class/modelart.php index ec68eb4..dfb6476 100644 --- a/w/class/modelart.php +++ b/w/class/modelart.php @@ -14,21 +14,52 @@ class Modelart extends Modeldb - public function exist($id) + public function exist(Art2 $art) { + $artdata = $this->artstore->get($art->id()); + if($artdata === false) { + return false; + } else { + return true; + } + + } - $req = $this->bdd->prepare(' SELECT COUNT(*) FROM ' . Config::arttable() . ' WHERE id = :id '); - $req->execute(array('id' => $id)); - $donnees = $req->fetch(PDO::FETCH_ASSOC); - return (bool)$donnees['COUNT(*)']; + public function add(Art2 $art) + { + $this->artstore->insert($art); + } + + public function get(Art2 $art) + { + $artdata = $this->artstore->get($art->id()); + if($artdata !== false) { + $art = new Art2($artdata); + return $art; + } else { + return false; + } } + public function update(Art2 $art) + { + $art->updateedited(); + $this->artstore->update($art->id(), $art->dry()); + } + public function exist3($id) + { + $req = $this->bdd->prepare(' SELECT COUNT(*) FROM ' . Config::arttable() . ' WHERE id = :id '); + $req->execute(array('id' => $id)); + $donnees = $req->fetch(PDO::FETCH_ASSOC); - public function add(Art2 $art) + return (bool)$donnees['COUNT(*)']; + } + + public function add3(Art2 $art) { if ($this->exist($art->id())) { @@ -82,7 +113,7 @@ class Modelart extends Modeldb $req->closeCursor(); } - public function get(Art2 $art) + public function get3(Art2 $art) { $req = $this->bdd->prepare('SELECT * FROM ' . Config::arttable() . ' WHERE id = :id '); @@ -98,7 +129,7 @@ class Modelart extends Modeldb - public function update(Art2 $art) + public function update3(Art2 $art) { $now = new DateTimeImmutable(null, timezone_open("Europe/Paris")); @@ -139,7 +170,9 @@ class Modelart extends Modeldb } - public function getlister(array $selection = ['id'], array $opt = []) + + + public function getlister3(array $selection = ['id'], array $opt = []) { // give an array using SELECTION columns and sort and desc OPTIONS @@ -162,7 +195,7 @@ class Modelart extends Modeldb } } - public function getlisterwhere(array $select = ['id'], array $whereid = [], $by = 'id', $order = 'DESC') + public function getlisterwhere3(array $select = ['id'], array $whereid = [], $by = 'id', $order = 'DESC') { // give an array using SELECTION columns and sort and desc OPTIONS diff --git a/w/class/modeldb.php b/w/class/modeldb.php index 346a275..6064b4f 100644 --- a/w/class/modeldb.php +++ b/w/class/modeldb.php @@ -4,14 +4,25 @@ class Modeldb extends Model /** @var PDO */ protected $bdd; protected $arttable; + protected $database; + protected $artstore; - protected static $db; public function __construct() { - $this->setbdd(); - + //$this->setbdd(); //self::setdb(); + + $this->dbinit(); } + + + public function dbinit() + { + $this->database = new Dbengine(__DIR__ . '/../../w_database'); + $this->artstore = $this->database->store(Config::arttable()); + + } + diff --git a/w/class/router.php b/w/class/router.php index 32a4c96..27ac4e4 100644 --- a/w/class/router.php +++ b/w/class/router.php @@ -27,6 +27,8 @@ class Router 'art action=logout' => ['art', 'logout', 'art'], 'home action=logout' => ['home', 'logout', 'home'], 'action=logout' => ['home', 'logout'], + 'aff=db' => ['db', 'desktop'], + 'aff=db action=add' => ['db', 'add'], 'aff=media' => ['media', 'desktop'], 'aff=media action=addmedia' => ['media', 'addmedia'], 'aff=admin' => ['admin', 'desktop'], diff --git a/w/class/sleekdbw.php b/w/class/sleekdbw.php new file mode 100644 index 0000000..dd77752 --- /dev/null +++ b/w/class/sleekdbw.php @@ -0,0 +1,180 @@ +<?php + +use SleekDB\SleekDB; + + +class SleekDBw extends SleekDB +{ + + public function getStoreIdList() + { + $listPath = $this->storePath . 'data/'; + $lengh = strlen($listPath); + $list = []; + foreach (glob($listPath . '*.json') as $filename) { + $list[] = substr(substr($filename, $lengh), 0, -5); + } + return $list; + } + + public function getbyid($id) + { + return $this->getStoreDocumentById($id); + } + + public function updatebyid($id, $data) + { + foreach ($data as $key => $value) { + // Do not update the _id reserved index of a store. + if ($key != '_id') { + $data[$key] = $value; + } + } + $storePath = $this->storePath . 'data/' . $id . '.json'; + if (file_exists($storePath)) { + // Wait until it's unlocked, then update data. + file_put_contents($storePath, json_encode($data), LOCK_EX); + } + // Check do we need to wipe the cache for this store. + if ($this->deleteCacheOnCreate === true) $this->_emptyAllCache(); + return true; + } + + + + + + + // Writes an object in a store. + protected function writeInStore($storeData) + { + // Cast to array + $storeData = (array)$storeData; + // Check if it has _id key + if (empty($storeData['_id'])) throw new \Exception('Lack of id in this object'); + if (in_array($storeData['_id'], $this->getStoreIdList())) throw new \Exception('Id already used'); + // Add the system ID with the store data array. + $id = $storeData['_id']; + // Prepare storable data + $storableJSON = json_encode($storeData); + if ($storableJSON === false) throw new \Exception('Unable to encode the data array, please provide a valid PHP associative array'); + // Define the store path + $storePath = $this->storePath . 'data/' . $id . '.json'; + if (!file_put_contents($storePath, $storableJSON)) { + throw new \Exception("Unable to write the object file! Please check if PHP has write permission."); + } + return $storeData; + } + + + // Find store objects with conditions, sorting order, skip and limits. + protected function findStoreDocuments() + { + $found = []; + $storeIdList = $this->getStoreIdList(); + $searchRank = []; + // Start collecting and filtering data. + foreach ($storeIdList as $id) { + // Collect data of current iteration. + $data = $this->getStoreDocumentById($id); + if (!empty($data)) { + // Filter data found. + if (empty($this->conditions)) { + // Append all data of this store. + $found[] = $data; + } else { + // Append only passed data from this store. + $storePassed = true; + // Iterate each conditions. + foreach ($this->conditions as $condition) { + // Check for valid data from data source. + $validData = true; + $fieldValue = ''; + try { + $fieldValue = $this->getNestedProperty($condition['fieldName'], $data); + } catch (\Exception $e) { + $validData = false; + $storePassed = false; + } + if ($validData === true) { + // Check the type of rule. + if ($condition['condition'] === '=') { + // Check equal. + if ($fieldValue != $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '!=') { + // Check not equal. + if ($fieldValue == $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '>') { + // Check greater than. + if ($fieldValue <= $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '>=') { + // Check greater equal. + if ($fieldValue < $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '<') { + // Check less than. + if ($fieldValue >= $condition['value']) $storePassed = false; + } else if ($condition['condition'] === '<=') { + // Check less equal. + if ($fieldValue > $condition['value']) $storePassed = false; + } + } + } + // Check if current store is updatable or not. + if ($storePassed === true) { + // Append data to the found array. + $found[] = $data; + } + } + } + } + if (count($found) > 0) { + // Check do we need to sort the data. + if ($this->orderBy['order'] !== false) { + // Start sorting on all data. + $found = $this->sortArray($this->orderBy['field'], $found, $this->orderBy['order']); + } + // If there was text search then we would also sort the result by search ranking. + if (!empty($this->searchKeyword)) { + $found = $this->performSerach($found); + } + // Skip data + if ($this->skip > 0) $found = array_slice($found, $this->skip); + // Limit data. + if ($this->limit > 0) $found = array_slice($found, 0, $this->limit); + } + return $found; + } + + + + // Method to boot a store. + protected function bootStore() + { + $store = trim($this->storeName); + // Validate the store name. + if (!$store || empty($store)) throw new \Exception('Invalid store name was found'); + // Prepare store name. + if (substr($store, -1) !== '/') $store = $store . '/'; + // Store directory path. + $this->storePath = $this->dataDirectory . $store; + // Check if the store exists. + if (!file_exists($this->storePath)) { + // The directory was not found, create one with cache directory. + if (!mkdir($this->storePath, 0777, true)) throw new \Exception('Unable to create the store path at ' . $this->storePath); + // Create the cache directory. + if (!mkdir($this->storePath . 'cache', 0777, true)) throw new \Exception('Unable to create the store\'s cache directory at ' . $this->storePath . 'cache'); + // Create the data directory. + if (!mkdir($this->storePath . 'data', 0777, true)) throw new \Exception('Unable to create the store\'s data directory at ' . $this->storePath . 'data'); + // Check if PHP has write permission in that directory. + if (!is_writable($this->storePath)) throw new \Exception('Store path is not writable at "' . $this->storePath . '." Please change store path permission.'); + // Finally check if the directory is readable by PHP. + if (!is_readable($this->storePath)) throw new \Exception('Store path is not readable at "' . $this->storePath . '." Please change store path permission.'); + } + } + + + + +} + +?>
\ No newline at end of file diff --git a/w/fn/w.fn.php b/w/fn/w.fn.php index 558ae67..8bfdee8 100644 --- a/w/fn/w.fn.php +++ b/w/fn/w.fn.php @@ -138,4 +138,18 @@ function str_clean(string $string) +function changekey($array, $oldkey, $newkey) +{ + if (!array_key_exists($oldkey, $array)) + return $array; + + $keys = array_keys($array); + $keys[array_search($oldkey, $keys)] = $newkey; + + return array_combine($keys, $array); +} + + + + ?>
\ No newline at end of file diff --git a/w/view/templates/edit.php b/w/view/templates/edit.php index 39c2104..4db6706 100644 --- a/w/view/templates/edit.php +++ b/w/view/templates/edit.php @@ -21,11 +21,10 @@ <form action="?id=<?= $art->id() ?>&action=update" method="post" id="artedit"> - - - <?php $this->insert('edittopbar', ['config' => $config, 'art' => $art]) ?> - <?php $this->insert('editsidebar', ['config' => $config, 'art' => $art]) ?> + + <?php $this->insert('edittopbar', ['art' => $art]) ?> + <?php $this->insert('editsidebar', ['art' => $art]) ?> <style>textarea{font-size: <?= Config::fontsize() ?>px}</style> diff --git a/w/view/templates/edittopbar.php b/w/view/templates/edittopbar.php index 7e7f9e8..ffd1c0f 100644 --- a/w/view/templates/edittopbar.php +++ b/w/view/templates/edittopbar.php @@ -4,7 +4,7 @@ <!-- <input type="submit" name="action" value="display" accesskey="c" onclick="document.getElementById('artedit').submit();" form="artedit"> --> <!-- <input type="submit" name="action" value="delete" onclick="confirmSubmit(event, 'Delete this article', 'artedit')" form="artedit"> --> <a href="?id=<?= $art->id() ?>" target="_blank">👁</a> - + <a href="?id=<?= $art->id() ?>&aff=log" target="_blank">¶</a> <span id="headid"><?= $art->id() ?></span> <label for="fontsize">Font-size</label> diff --git a/w/view/templates/read.php b/w/view/templates/read.php index 5c544b3..4964a47 100644 --- a/w/view/templates/read.php +++ b/w/view/templates/read.php @@ -1,4 +1,4 @@ -<?php $this->layout('layout', ['title' => $art->title()]) ?> +<?php $this->layout('layout', ['title' => $art->title(), 'description' => $art->description()]) ?> diff --git a/w/view/templates/readcreate.php b/w/view/templates/readcreate.php index fd5df18..dc36420 100644 --- a/w/view/templates/readcreate.php +++ b/w/view/templates/readcreate.php @@ -1,3 +1,5 @@ -<form action="/?id=<?= $id ?>&action=add"> +<form action="./" method="get"> +<input type="hidden" name="id" value="<?= $id ?>"> +<input type="hidden" name="action" value="add"> <input type="submit" value="create"> </form>
\ No newline at end of file diff --git a/w/w.index.php b/w/w.index.php index 2ab4880..8727591 100644 --- a/w/w.index.php +++ b/w/w.index.php @@ -32,14 +32,12 @@ spl_autoload_register('class_autoloader'); // var_dump($artmanager->getlisterwhere(['id', 'description'], ['55', '44', 'lol'])); +try { + $router = new Router; -Configtest::setinfo('coucou'); - - -$router = new Router; - - - +} catch(Exception $e) { + echo 'Exception reçue : ', $e->getMessage(), "\n"; +} |