Guest User

Untitled

a guest
Dec 20th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.10 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 User
  185. {
  186. private $id, $name, $email, $pass;
  187.  
  188. public function __construct($id, $name, $email, $pass)
  189. {
  190. $this->id = $id;
  191. $this->name = $name;
  192. $this->email = $email;
  193. $this->pass = $pass;
  194. }
  195.  
  196. public function getID() { return $this->id; }
  197. public function getName() { return $this->name; }
  198. public function getEmail() { return $this->email; }
  199. public function getPass() { return $this->pass; }
  200. }
  201.  
  202. abstract class LoginResponse
  203. {
  204. const UnknownEmail = 0;
  205. const InvalidPass = 1;
  206. const Success = 2;
  207. }
  208.  
  209. class Type
  210. {
  211. public static function createFromID($id)
  212. {
  213. return Core::getInstance()->getData()->getTypeByID($id);
  214. }
  215.  
  216. private $id, $name, $fields;
  217.  
  218. public function __construct($id, $name, $fields = [])
  219. {
  220. $this->id = $id;
  221. $this->name = $name;
  222. $this->fields = $fields;
  223. }
  224.  
  225. public function getID() { return $this->id; }
  226. public function getName() { return $this->name; }
  227. public function getFields() { return $this->fields; }
  228.  
  229. /*public function addFields($fields)
  230. {
  231. $rows = [];
  232. foreach ($fields as $field)
  233. {
  234. $rows[] = [$this->id, $field[0], $field[1]];
  235. }
  236.  
  237. global $vwa;
  238. $lastID = $vwa->getDatabase()->insertMany('fields', ['class', 'name', 'type'], $rows);
  239.  
  240. $offset = count($fields) - 1;
  241. $i = 0;
  242. foreach ($fields as $field)
  243. {
  244. $this->fields[] = new Field($this, $lastID - $offset + $i);
  245. $i++;
  246. }
  247. }
  248.  
  249. public function addField($field)
  250. {
  251. $this->addFields([$field]);
  252. }
  253.  
  254. public function internalAddField($field)
  255. {
  256. $this->fields[] = $field;
  257. }*/
  258. }
  259.  
  260. class Field
  261. {
  262. private $parentTypeID, $id, $name, $type;
  263.  
  264. public function __construct($parentTypeID, $id, $name, $type)
  265. {
  266. $this->parentTypeID = $parentTypeID;
  267. $this->id = $id;
  268. $this->name = $name;
  269. $this->type = $type;
  270. }
  271.  
  272. public function getParentTypeID() { return $this->parentTypeID; }
  273. public function getParentType() { return Type::createFromID($this->parentTypeID); }
  274. public function getID() { return $this->id; }
  275. public function getName() { return $this->name; }
  276. public function getType() { return $this->type; }
  277. }
  278.  
  279. abstract class FieldType
  280. {
  281. const Boolean = 0;
  282. const Integer = 1;
  283. const BigInteger = 2;
  284. const Number = 3;
  285. const PreciseNumber = 4;
  286. const Text = 5;
  287. const Enumeration = 6;
  288. const Object = 7;
  289. }
  290.  
  291. class Entity
  292. {
  293. private $parentTypeID, $id, $fields;
  294.  
  295. public function __construct($parentTypeID, $id)
  296. {
  297. $this->parentTypeID = $parentTypeID;
  298. $this->id = $id;
  299. $this->fields = [];
  300. }
  301.  
  302. public function getParentTypeID() { return $this->parentTypeID; }
  303. public function getParentType() { return Type::createFromID($this->parentTypeID); }
  304. public function getID() { return $this->id; }
  305. public function getFields() { return $this->fields; }
  306. }
  307.  
  308. class DataCache
  309. {
  310. private $fullCreate, $fullGet, $getKey, $objects, $attachedCaches;
  311.  
  312. public function __construct($fullCreate, $fullGet, $getKey = null)
  313. {
  314. if ($getKey == null)
  315. {
  316. $this->getKey = function($object) { return $object->getID(); };
  317. }
  318. else
  319. {
  320. $this->getKey = $getKey;
  321. }
  322.  
  323. $this->fullCreate = $fullCreate;
  324. $this->fullGet = $fullGet;
  325. $this->objects = [];
  326. $this->attachedCaches = [];
  327. }
  328.  
  329. public function attach($caches)
  330. {
  331. foreach ($caches as $cache)
  332. {
  333. $this->attachedCaches[] = $cache;
  334. }
  335. }
  336.  
  337. public function getKey($object)
  338. {
  339. return $this->getKey($object);
  340. }
  341.  
  342. public function get($key)
  343. {
  344. return $this->getFirstOrNull($this->getMultiple([$key]));
  345. }
  346.  
  347. public function getMultiple($keys)
  348. {
  349. $objects = [];
  350. $uncachedKeys = [];
  351. foreach ($keys as $key)
  352. {
  353. if (array_key_exists($key, $this->objects))
  354. {
  355. $objects[$key] = $this->objects[$key];
  356. }
  357. else
  358. {
  359. $uncachedKeys[] = $key;
  360. }
  361. }
  362.  
  363. if (count($uncachedKeys) > 0)
  364. {
  365. $loadedObjects = $this->fullGet($keys);
  366. $this->putInCache($loadedObjects);
  367. $objects = array_merge($objects, $loadedObjects);
  368. }
  369.  
  370. return $objects;
  371. }
  372.  
  373. public function create($args)
  374. {
  375. return $this->getFirstOrNull(createMultiple([$args]));
  376. }
  377.  
  378. public function createMultiple($argLists)
  379. {
  380. $objects = $this->fullCreate($argLists);
  381. $this->putInCache($objects);
  382. return $objects;
  383. }
  384.  
  385. private function getFirstOrNull($collection)
  386. {
  387. if (count($collection) == 0)
  388. {
  389. return null;
  390. }
  391. else
  392. {
  393. return $collection[0];
  394. }
  395. }
  396.  
  397. private function putInCache($objects)
  398. {
  399. foreach ($objects as $object)
  400. {
  401. $this->objects[$this->getKey($object)] = $object;
  402. foreach ($this->attachedCaches as $cache)
  403. {
  404. $cache->objects[$cache->getKey($object)] = $object;
  405. }
  406. }
  407. }
  408. }
  409.  
  410. abstract class DataSource
  411. {
  412. private $users, $usersByEmail;
  413. protected abstract function createUsers($argLists);
  414. protected abstract function getUsers($ids);
  415. protected abstract function getUsersByEmail($emails);
  416.  
  417. private $types;
  418. protected abstract function createTypes($argLists);
  419. protected abstract function getTypes($ids);
  420.  
  421. protected function initialize()
  422. {
  423. $this->users = new DataCache(callable $this->createUsers, $this->getUsers);
  424. $this->usersByEmail = new DataCache($this->createUsers, $this->getUsersByEmail, function($user) { return $user->getEmail(); });
  425.  
  426. $this->users->attach([$this->usersByEmail]);
  427. $this->usersByEmail->attach([$this->users]);
  428.  
  429. $this->types = new DataCache($this->createTypes, $this->getTypes);
  430. }
  431. }
  432.  
  433. class TemporaryDataSource extends DataSource
  434. {
  435. public function __construct()
  436. {
  437. $this->initialize();
  438. }
  439.  
  440. protected function createUsers($argLists)
  441. {
  442. $users = [];
  443. foreach ($argLists as $argList)
  444. {
  445. $users[] = new User($argList['id'], $argList['name'], $argList['email'], $argList['pass']);
  446. }
  447.  
  448. return $users;
  449. }
  450.  
  451. protected function getUsers($ids) { return []; }
  452.  
  453. protected function getUsersByEmail($emails) { return []; }
  454.  
  455. protected function createTypes($argLists)
  456. {
  457. $types = [];
  458. foreach ($argLists as $argList)
  459. {
  460. $types[] = new Type($argList['id'], $argList['name']);
  461. }
  462.  
  463. return $types;
  464. }
  465.  
  466. protected function getTypes($ids) { return []; }
  467. }
  468.  
  469. /*class DatabaseDataSource extends DataSource
  470. {
  471. private $database;
  472.  
  473. public function __construct($database)
  474. {
  475. $this->initialize();
  476. $this->database = $database;
  477. }
  478.  
  479. protected function loadUsersByID($ids)
  480. {
  481. $result = $this->database->select('users', ['name', 'email', 'pass'], 'id = IN('.createParameterList(count($ids)).')', $ids);
  482. if ($result == null)
  483. {
  484. return null;
  485. }
  486. else
  487. {
  488. return ['key' => $id, 'object' => new User($id, $result['name'], $result['email'], $result['pass'])];
  489. }
  490. }
  491.  
  492. protected function loadUsersByEmail($emails)
  493. {
  494. $result = $this->database->selectUnique('users', ['id', 'name', 'pass'], 'email', $email);
  495. if ($result == null)
  496. {
  497. return null;
  498. }
  499. else
  500. {
  501. return ['key' => $email, 'object' => new User($result['id'], $result['name'], $email, $result['pass'])];
  502. }
  503. }
  504.  
  505. protected function loadClassesByID($ids)
  506. {
  507. $result = $this->database->selectUnique('classes', ['name'], 'id', $id);
  508. if ($result == null)
  509. {
  510. return null;
  511. }
  512.  
  513. $class = new WebClass($result[0]['id'], $name);
  514.  
  515. $result = $this->database->select('fields', ['id', 'name', 'type'], 'class = ?', [$class->getID()]);
  516. foreach ($result as $row)
  517. {
  518. $class->internalAddField(new Field($class, $row['id'], $row['name'], $row['type']));
  519. }
  520.  
  521. return $class;
  522. }
  523.  
  524. protected function constructClasses($names)
  525. {
  526.  
  527. }
  528. }*/
  529.  
  530. class Core
  531. {
  532. private static $instance;
  533.  
  534. public static function getInstance()
  535. {
  536. return Core::$instance;
  537. }
  538.  
  539. private $data;
  540.  
  541. public function __construct($data)
  542. {
  543. Core::$instance = $this;
  544. $this->data = $data;
  545. }
  546.  
  547. public function getData()
  548. {
  549. return $this->data;
  550. }
  551.  
  552. public function login($email, $pass)
  553. {
  554. $user = $this->data->getUserByEmail($email);
  555. if ($user == null)
  556. {
  557. return LoginResponse::UnknownEmail;
  558. }
  559. else if (!password_verify($pass, $user->pass))
  560. {
  561. return LoginResponse::InvalidPass;
  562. }
  563. else
  564. {
  565. session_destroy();
  566. session_start();
  567. $_SESSION['user'] = $user;
  568. return LoginResponse::Success;
  569. }
  570. }
  571.  
  572. public function logout()
  573. {
  574. session_destroy();
  575. }
  576.  
  577. public function getCurrentUser()
  578. {
  579. if (!isset($_SESSION['user']))
  580. {
  581. return null;
  582. }
  583. else
  584. {
  585. return $_SESSION['user'];
  586. }
  587. }
  588.  
  589. public function addClass($name, $fields = [])
  590. {
  591. $id = $this->database->insert('classes', ['name'], [$name]);
  592. $class = new WebClass($id, $name);
  593.  
  594. if (count($fields) > 0)
  595. {
  596. $class->addFields($fields);
  597. }
  598.  
  599. return $class;
  600. }
  601.  
  602. public function getClass($name)
  603. {
  604. $result = $this->database->select('classes', ['id'], 'name = ?', [$name]);
  605. if (count($result) == 0)
  606. {
  607. return null;
  608. }
  609.  
  610. $class = new WebClass($result[0]['id'], $name);
  611.  
  612. $result = $this->database->select('fields', ['id', 'name', 'type'], 'class = ?', [$class->getID()]);
  613. foreach ($result as $row)
  614. {
  615. $class->internalAddField(new Field($class, $row['id'], $row['name'], $row['type']));
  616. }
  617.  
  618. return $class;
  619. }
  620. }
  621.  
  622.  
  623.  
  624. new Core(new TemporaryDataSource());
  625.  
  626.  
  627.  
  628.  
  629. ?>
Add Comment
Please, Sign In to add comment