spectre.class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Bibliotheca
  4. *
  5. * Copyright 2018-2023 Johannes Keßler
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  19. */
  20. /**
  21. * Class Spectre
  22. * API for Bibliotheca
  23. */
  24. class Spectre {
  25. /**
  26. * the global DB object
  27. *
  28. * @var mysqli
  29. */
  30. private mysqli $_DB;
  31. /**
  32. * The user object to query with
  33. *
  34. * @var Doomguy
  35. */
  36. private Doomguy $_User;
  37. /**
  38. * Allowed request params
  39. *
  40. * @var array
  41. */
  42. private array $_allowedRequests = array('default','list','add','addInfo');
  43. /**
  44. * Spectre constructor.
  45. *
  46. * @param mysqli $databaseConnectionObject
  47. * @param Doomguy $userObj
  48. */
  49. public function __construct(mysqli $databaseConnectionObject, Doomguy $userObj) {
  50. $this->_DB = $databaseConnectionObject;
  51. $this->_User = $userObj;
  52. }
  53. /**
  54. * Validate given request string
  55. *
  56. * @param string $request
  57. * @return bool
  58. */
  59. public function allowedRequests(string $request): bool {
  60. $ret = false;
  61. if(in_array($request, $this->_allowedRequests)) {
  62. $ret = true;
  63. }
  64. return $ret;
  65. }
  66. /**
  67. * With given data build the structure to create a add post
  68. * request
  69. *
  70. * @param array $data
  71. * @return array
  72. */
  73. public function buildAddStructure(array $data): array {
  74. $ret = array();
  75. if(!empty($data) && is_array($data)) {
  76. foreach($data as $k=>$v) {
  77. $ret[$k] = array('input' => $v['apiinfo']);
  78. }
  79. }
  80. return $ret;
  81. }
  82. /**
  83. * rewrite the data from curl into the format the
  84. * POST via web frontend creates
  85. * "The problem occurs when you have a form that uses both single file and HTML array feature."
  86. *
  87. * @param array $data
  88. * @return array
  89. */
  90. public function prepareFilesArray(array $data): array {
  91. $ret = array();
  92. if(!empty($data)) {
  93. foreach($data as $fieldName=>$fdata) {
  94. foreach($fdata as $k=>$v) {
  95. $ret[$k][$fieldName] = $v;
  96. }
  97. }
  98. }
  99. return $ret;
  100. }
  101. }