Advertisement
Guest User

Untitled

a guest
Dec 21st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.82 KB | None | 0 0
  1. <?php
  2.  
  3. namespace VWA;
  4.  
  5. class Database
  6. {
  7. private $system, $host, $port, $name, $user, $pass, $pdo;
  8.  
  9. public function __construct($system, $host, $port, $name, $user, $pass)
  10. {
  11. $this->system = $system;
  12. $this->host = $host;
  13. $this->port = $port;
  14. $this->name = $name;
  15. $this->user = $user;
  16. $this->pass = $pass;
  17. $this->pdo = new \PDO($system.':host='.$host.';port='.$port.';dbname='.$name.';', $user, $pass);
  18. }
  19.  
  20. public function getPDO() { return $this->pdo; }
  21. public function getSystem() { return $this->system; }
  22.  
  23. public static function createParameterList($count)
  24. {
  25. if ($count == 0)
  26. {
  27. return '';
  28. }
  29. else if ($count == 1)
  30. {
  31. return '?';
  32. }
  33. else
  34. {
  35. $output = '?';
  36. for ($i = 1; $i < $count; $i++)
  37. {
  38. $output .= ', ?';
  39. }
  40.  
  41. return $output;
  42. }
  43. }
  44.  
  45. public function execute($query, $parameters = [])
  46. {
  47. $statement = $this->pdo->prepare($query);
  48.  
  49. if (count($parameters) > 0)
  50. {
  51. $statement->execute($parameters);
  52. }
  53. else
  54. {
  55. $statement->execute();
  56. }
  57.  
  58. echo '<br>';
  59. echo $query;
  60. echo '<br>';
  61.  
  62. return $statement;
  63. }
  64.  
  65. public function select($table, $columns, $condition = '', $parameters = [])
  66. {
  67. $query = 'SELECT ';
  68.  
  69. $first = true;
  70. foreach ($columns as $column)
  71. {
  72. if ($first)
  73. {
  74. $query .= $column;
  75. $first = false;
  76. }
  77. else
  78. {
  79. $query .= ', '.$column;
  80. }
  81. }
  82.  
  83. $query .= ' FROM '.$table;
  84.  
  85. if ($condition != '')
  86. {
  87. $query .= ' WHERE '.$condition;
  88. }
  89.  
  90. return $this->execute($query, $parameters)->fetchAll(\PDO::FETCH_ASSOC);
  91. }
  92.  
  93. public function selectUnique($table, $columns, $targetColumn, $parameter)
  94. {
  95. $result = $this->database->select('users', ['*'], $targetColumn.' = ?', [$parameter]);
  96. if (count($result) == 0)
  97. {
  98. return null;
  99. }
  100. else
  101. {
  102. return $result[0];
  103. }
  104. }
  105.  
  106. public function insert($table, $columns, $parameters = [])
  107. {
  108. return $this->insertMany($table, $columns, [$parameters]);
  109. }
  110.  
  111. public function insertMany($table, $columns, $rows = [[]])
  112. {
  113. $query = 'INSERT INTO '.$table.' (';
  114.  
  115. $first = true;
  116. foreach ($columns as $column)
  117. {
  118. if ($first)
  119. {
  120. $query .= $column;
  121. $first = false;
  122. }
  123. else
  124. {
  125. $query .= ', '.$column;
  126. }
  127. }
  128.  
  129. $query .= ') VALUES ';
  130.  
  131. $outputRow = function($row)
  132. {
  133. $output = '(';
  134. $first = true;
  135. foreach ($row as $value)
  136. {
  137. if ($first)
  138. {
  139. $output .= '?';
  140. $first = false;
  141. }
  142. else
  143. {
  144. $output .= ', ?';
  145. }
  146. }
  147.  
  148. $output .= ')';
  149.  
  150. return $output;
  151. };
  152.  
  153. $parameters = [];
  154. $first = true;
  155. foreach ($rows as $row)
  156. {
  157. if ($first)
  158. {
  159. $query .= $outputRow($row);
  160. $first = false;
  161. }
  162. else
  163. {
  164. $query .= ', '.$outputRow($row);
  165. }
  166.  
  167. foreach ($row as $value)
  168. {
  169. $parameters[] = $value;
  170. }
  171. }
  172.  
  173. echo '<br>START<br>';
  174.  
  175. echo $query;
  176. echo '<br>';
  177. print_r($parameters);
  178.  
  179. $this->execute($query, $parameters);
  180. return $this->pdo->lastInsertId();
  181. }
  182. }
  183.  
  184. class DataCache
  185. {
  186. private $fullCreate, $fullGet, $fullGetKey, $objects, $attachedCaches;
  187.  
  188. public function __construct($fullCreate, $fullGet, $fullGetKey = null)
  189. {
  190. if ($fullGetKey == null)
  191. {
  192. $this->fullGetKey = function($object) { return $object->getID(); };
  193. }
  194. else
  195. {
  196. $this->fullGetKey = $fullGetKey;
  197. }
  198.  
  199. $this->fullCreate = $fullCreate;
  200. $this->fullGet = $fullGet;
  201. $this->objects = [];
  202. $this->attachedCaches = [];
  203. }
  204.  
  205. public function attach($caches)
  206. {
  207. foreach ($caches as $cache)
  208. {
  209. $this->attachedCaches[] = $cache;
  210. }
  211. }
  212.  
  213. public function getKey($object)
  214. {
  215. return call_user_func($this->fullGetKey, $object);
  216. }
  217.  
  218. public function get($key)
  219. {
  220. return $this->getFirstOrNull($this->getMultiple([$key]));
  221. }
  222.  
  223. public function getMultiple($keys)
  224. {
  225. $objects = [];
  226. $uncachedKeys = [];
  227. foreach ($keys as $key)
  228. {
  229. if (array_key_exists($key, $this->objects))
  230. {
  231. $objects[] = $this->objects[$key];
  232. }
  233. else
  234. {
  235. $uncachedKeys[] = $key;
  236. }
  237. }
  238.  
  239. if (count($uncachedKeys) > 0)
  240. {
  241. $loadedObjects = call_user_func($this->fullGet, $keys);
  242. $this->putInCache($loadedObjects);
  243. $objects = array_merge($objects, $loadedObjects);
  244. }
  245.  
  246. return $objects;
  247. }
  248.  
  249. public function create($args)
  250. {
  251. return $this->getFirstOrNull($this->createMultiple([$args]));
  252. }
  253.  
  254. public function createMultiple($argLists)
  255. {
  256. $objects = call_user_func($this->fullCreate, $argLists);
  257. $this->putInCache($objects);
  258. return $objects;
  259. }
  260.  
  261. private function getFirstOrNull($collection)
  262. {
  263. if (count($collection) == 0)
  264. {
  265. return null;
  266. }
  267. else
  268. {
  269. return $collection[0];
  270. }
  271. }
  272.  
  273. private function putInCache($objects)
  274. {
  275. foreach ($objects as $object)
  276. {
  277. $this->objects[$this->getKey($object)] = $object;
  278. foreach ($this->attachedCaches as $cache)
  279. {
  280. $cache->objects[$cache->getKey($object)] = $object;
  281. }
  282. }
  283. }
  284. }
  285.  
  286. abstract class DataSource
  287. {
  288. public function accessUsers() { return $this->users; }
  289. public function accessUsersByEmail() { return $this->usersByEmail; }
  290. public function createUser($name, $email, $pass) { return $this->users->create(['name' => $name, 'email' => $email, 'pass' => $pass]); }
  291. protected abstract function createUsers($argLists);
  292. protected abstract function getUsers($ids);
  293. protected abstract function getUsersByEmail($emails);
  294. private $users, $usersByEmail;
  295.  
  296. public function accessTypes() { return $this->types; }
  297. public function createType($name) { return $this->types->create(['name' => $name]); }
  298. protected abstract function createTypes($argLists);
  299. protected abstract function getTypes($ids);
  300. private $types;
  301.  
  302. public function accessFields() { return $this->fields; }
  303. public function createField($typeID, $name, $fieldType) { return $this->fields->create(['typeID' => $typeID, 'name' => $name, 'fieldType' => $fieldType]); }
  304. protected abstract function createFields($argLists);
  305. protected abstract function getFields($ids);
  306. private $fields;
  307.  
  308. public function accessViews() { return $this->views; }
  309. public function createView($typeID, $name) { return $this->views->create(['typeID' => $typeID, 'name' => $name]); }
  310. protected abstract function createViews($argLists);
  311. protected abstract function getViews($ids);
  312. private $views;
  313.  
  314. protected function initialize()
  315. {
  316. $this->users = new DataCache(function($argLists) { return $this->createUsers($argLists); }, function($ids) { return $this->getUsers($ids); });
  317. $this->usersByEmail = new DataCache(function($argLists) { return $this->createUsers($argLists); }, function($emails) { return $this->getUsersByEmail($emails); }, function($user) { return $user->getEmail(); });
  318.  
  319. $this->users->attach([$this->usersByEmail]);
  320. $this->usersByEmail->attach([$this->users]);
  321.  
  322. $this->types = new DataCache(function($argLists) { return $this->createTypes($argLists); }, function($ids) { return $this->getTypes($ids); });
  323.  
  324. $this->fields = new DataCache(function($argLists) { return $this->createFields($argLists); }, function($ids) { return $this->getFields($ids); });
  325. }
  326. }
  327.  
  328. class TemporaryDataSource extends DataSource
  329. {
  330. public function __construct()
  331. {
  332. $this->initialize();
  333. }
  334.  
  335. private $userCount = 1;
  336.  
  337. protected function createUsers($argLists)
  338. {
  339. $users = [];
  340. foreach ($argLists as $argList)
  341. {
  342. $users[] = new User($this->userCount++, $argList['name'], $argList['email'], password_hash($argList['pass'], \PASSWORD_BCRYPT, ['cost' => 12]));
  343. }
  344.  
  345. return $users;
  346. }
  347.  
  348. protected function getUsers($ids) { return []; }
  349.  
  350. protected function getUsersByEmail($emails) { return []; }
  351.  
  352. private $typeCount = 1;
  353.  
  354. protected function createTypes($argLists)
  355. {
  356. $types = [];
  357. foreach ($argLists as $argList)
  358. {
  359. $types[] = new Type($this->typeCount++, $argList['name']);
  360. }
  361.  
  362. return $types;
  363. }
  364.  
  365. protected function getTypes($ids) { return []; }
  366.  
  367. private $fieldCount = 1;
  368.  
  369. protected function createFields($argLists)
  370. {
  371. $fields = [];
  372. foreach ($argLists as $argList)
  373. {
  374. $field = new Field($argList['typeID'], $this->fieldCount++, $argList['name'], $argList['fieldType']);
  375. Type::fromID($argList['typeID'])->_addFieldID($field->getID());
  376. $fields[] = $field;
  377. }
  378.  
  379. return $fields;
  380. }
  381.  
  382. protected function getFields($ids) { return []; }
  383. }
  384.  
  385. /*class DatabaseDataSource extends DataSource
  386. {
  387. private $database;
  388.  
  389. public function __construct($database)
  390. {
  391. $this->initialize();
  392. $this->database = $database;
  393. }
  394.  
  395. protected function loadUsersByID($ids)
  396. {
  397. $result = $this->database->select('users', ['name', 'email', 'pass'], 'id = IN('.createParameterList(count($ids)).')', $ids);
  398. if ($result == null)
  399. {
  400. return null;
  401. }
  402. else
  403. {
  404. return ['key' => $id, 'object' => new User($id, $result['name'], $result['email'], $result['pass'])];
  405. }
  406. }
  407.  
  408. protected function loadUsersByEmail($emails)
  409. {
  410. $result = $this->database->selectUnique('users', ['id', 'name', 'pass'], 'email', $email);
  411. if ($result == null)
  412. {
  413. return null;
  414. }
  415. else
  416. {
  417. return ['key' => $email, 'object' => new User($result['id'], $result['name'], $email, $result['pass'])];
  418. }
  419. }
  420.  
  421. protected function loadClassesByID($ids)
  422. {
  423. $result = $this->database->selectUnique('classes', ['name'], 'id', $id);
  424. if ($result == null)
  425. {
  426. return null;
  427. }
  428.  
  429. $class = new WebClass($result[0]['id'], $name);
  430.  
  431. $result = $this->database->select('fields', ['id', 'name', 'type'], 'class = ?', [$class->getID()]);
  432. foreach ($result as $row)
  433. {
  434. $class->internalAddField(new Field($class, $row['id'], $row['name'], $row['type']));
  435. }
  436.  
  437. return $class;
  438. }
  439.  
  440. protected function constructClasses($names)
  441. {
  442.  
  443. }
  444. }*/
  445.  
  446. class Core
  447. {
  448. private static $instance;
  449.  
  450. public static function getInstance()
  451. {
  452. return Core::$instance;
  453. }
  454.  
  455. private $data;
  456.  
  457. public function __construct($data)
  458. {
  459. Core::$instance = $this;
  460. $this->data = $data;
  461. }
  462.  
  463. public function getData()
  464. {
  465. return $this->data;
  466. }
  467.  
  468. public function login($email, $pass)
  469. {
  470. $user = User::fromEmail($email);
  471. if ($user == null)
  472. {
  473. return LoginResponse::UnknownEmail;
  474. }
  475. else if (!password_verify($pass, $user->getPassHash()))
  476. {
  477. return LoginResponse::InvalidPass;
  478. }
  479. else
  480. {
  481. session_destroy();
  482. session_start();
  483. $_SESSION['userID'] = $user->getID();
  484. return LoginResponse::Success;
  485. }
  486. }
  487.  
  488. public function logout()
  489. {
  490. session_destroy();
  491. $_SESSION = [];
  492. }
  493.  
  494. public function getCurrentUser()
  495. {
  496. if (!isset($_SESSION['userID']))
  497. {
  498. return null;
  499. }
  500. else
  501. {
  502. return User::fromID($_SESSION['userID']);
  503. }
  504. }
  505. }
  506.  
  507. abstract class LoginResponse
  508. {
  509. const UnknownEmail = 0;
  510. const InvalidPass = 1;
  511. const Success = 2;
  512. }
  513.  
  514. class User
  515. {
  516. public static function fromID($id)
  517. {
  518. return Core::getInstance()->getData()->accessUsers()->get($id);
  519. }
  520.  
  521. public static function fromEmail($email)
  522. {
  523. return Core::getInstance()->getData()->accessUsersByEmail()->get($email);
  524. }
  525.  
  526. public static function create($name, $email, $pass)
  527. {
  528. return Core::getInstance()->getData()->createUser($name, $email, $pass);
  529. }
  530.  
  531. private $id, $name, $email, $passHash;
  532.  
  533. public function __construct($id, $name, $email, $passHash)
  534. {
  535. $this->id = $id;
  536. $this->name = $name;
  537. $this->email = $email;
  538. $this->passHash = $passHash;
  539. }
  540.  
  541. public function getID() { return $this->id; }
  542. public function getName() { return $this->name; }
  543. public function getEmail() { return $this->email; }
  544. public function getPassHash() { return $this->passHash; }
  545. }
  546.  
  547. class Type
  548. {
  549. public static function fromID($id)
  550. {
  551. return Core::getInstance()->getData()->accessTypes()->get($id);
  552. }
  553.  
  554. public static function create($name)
  555. {
  556. return Core::getInstance()->getData()->createType($name);
  557. }
  558.  
  559. private $id, $name, $fields, $fieldIDs;
  560.  
  561. public function __construct($id, $name, $fieldIDs)
  562. {
  563. $this->id = $id;
  564. $this->name = $name;
  565. $this->fieldIDs = $fieldIDs;
  566. }
  567.  
  568. public function getID() { return $this->id; }
  569. public function getName() { return $this->name; }
  570. public function getFieldIDs() { return $this->fieldIDs; }
  571.  
  572. public function getFields()
  573. {
  574. $fields = [];
  575. foreach ($this->fieldIDs as $fieldID)
  576. {
  577. $fields[] = Field::fromID($fieldID);
  578. }
  579.  
  580. return $fields;
  581. }
  582.  
  583. public function _addFieldID($fieldID)
  584. {
  585. $this->fieldIDs[] = $fieldID;
  586. }
  587.  
  588. public function _removeFieldID($fieldID)
  589. {
  590. array_splice($this->fieldIDs, array_search($fieldID, $this->fieldIDs), 1);
  591. }
  592. }
  593.  
  594. class Field
  595. {
  596. public static function fromID($id)
  597. {
  598. return Core::getInstance()->getData()->accessFields()->get($id);
  599. }
  600.  
  601. public static function create($typeID, $name, $fieldType)
  602. {
  603. return Core::getInstance()->getData()->createField($typeID, $name, $fieldType);
  604. }
  605.  
  606. private $typeID, $id, $name, $fieldType;
  607.  
  608. public function __construct($typeID, $id, $name, $fieldType)
  609. {
  610. $this->typeID = $typeID;
  611. $this->id = $id;
  612. $this->name = $name;
  613. $this->fieldType = $fieldType;
  614. }
  615.  
  616. public function getTypeID() { return $this->typeID; }
  617. public function getType() { return Type::fromID($this->typeID); }
  618. public function getID() { return $this->id; }
  619. public function getName() { return $this->name; }
  620. public function getFieldType() { return $this->fieldType; }
  621. }
  622.  
  623. abstract class FieldType
  624. {
  625. const Boolean = 0;
  626. const Integer = 1;
  627. const BigInteger = 2;
  628. const Number = 3;
  629. const PreciseNumber = 4;
  630. const Text = 5;
  631. const Enumeration = 6;
  632. const Object = 7;
  633. }
  634.  
  635. class View
  636. {
  637. public static function fromID($id)
  638. {
  639. return Core::getInstance()->getData()->accessFields()->get($id);
  640. }
  641.  
  642. public static function create($typeID, $name, $fieldType)
  643. {
  644. return Core::getInstance()->getData()->createField($typeID, $name, $fieldType);
  645. }
  646.  
  647. private $typeID, $id, $name;
  648.  
  649. public function __construct($typeID, $id, $name)
  650. {
  651. $this->typeID = $typeID;
  652. $this->id = $id;
  653. $this->name = $name;
  654. }
  655.  
  656. public function getTypeID() { return $this->typeID; }
  657. public function getType() { return Type::fromID($this->typeID); }
  658. public function getID() { return $this->id; }
  659. public function getName() { return $this->name; }
  660. }
  661.  
  662. class Entity
  663. {
  664. private $typeID, $id, $fields;
  665.  
  666. public function __construct($typeID, $id)
  667. {
  668. $this->typeID = $typeID;
  669. $this->id = $id;
  670. $this->fields = [];
  671. }
  672.  
  673. public function getTypeID() { return $this->typeID; }
  674. public function getType() { return Type::fromID($this->typeID); }
  675. public function getID() { return $this->id; }
  676. public function getFields() { return $this->fields; }
  677. }
  678.  
  679. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement