Plateforme web de commande de panier bio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
8.0 KiB

  1. <?php
  2. $requestUrl = trim(str_replace($_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']), '?');
  3. define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php');
  4. define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php');
  5. if (file_exists(CONFIG_FILE)) require_once CONFIG_FILE;
  6. if (!isset($config)) $config = [];
  7. $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
  8. $hasSupplier = isset($_REQUEST['supplier']) and !empty($_REQUEST['supplier']);
  9. $supplier = $_REQUEST['supplier'];
  10. if ($hasSupplier) {
  11. if (!isset($config[$supplier]))
  12. $config[$supplier] = [];
  13. $config[$supplier] = array_merge(
  14. [
  15. 'title' => '%supplier% <small>%event%</small>',
  16. 'description' => '',
  17. 'choices' => [],
  18. 'start' => 'now 00:00:00',
  19. 'frequency' => '1 day',
  20. 'password' => '',
  21. ],
  22. $config[$supplier]
  23. );
  24. $hasPassword = !empty($config[$supplier]['password']);
  25. if ($action === 'config') {
  26. if ($hasPassword) {
  27. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  28. header(sprintf('WWW-Authenticate: Basic realm="mon-panier-bio config %s"', $supplier));
  29. header('HTTP/1.0 401 Unauthorized');
  30. printf('Cette config est protégée par mot de passe !');
  31. exit;
  32. } elseif (
  33. ($_SERVER['PHP_AUTH_USER'] !== $supplier)
  34. or ($_SERVER['PHP_AUTH_PW'] !== $config[$supplier]['password'])
  35. ) {
  36. header('HTTP/1.0 403 Forbidden');
  37. printf('Cette config est protégée par mot de passe !');
  38. exit;
  39. }
  40. }
  41. foreach (array_keys($config[$supplier]) as $key)
  42. if (isset($_REQUEST[$key]))
  43. $config[$supplier][$key] = $_REQUEST[$key];
  44. }
  45. if (empty($config[$supplier]['start']))
  46. $config[$supplier]['start'] = 'now 00:00:00';
  47. if (is_string($config[$supplier]['choices']))
  48. $config[$supplier]['choices'] = explode(PHP_EOL, $config[$supplier]['choices']);
  49. if (!is_array($config[$supplier]['choices']))
  50. $config[$supplier]['choices'] = [];
  51. $config[$supplier]['choices'] = array_filter(
  52. $config[$supplier]['choices'],
  53. function ($choice) {
  54. return is_string($choice) and !empty(trim($choice));
  55. }
  56. );
  57. $config[$supplier]['choices'] = array_map('trim', $config[$supplier]['choices']);
  58. }
  59. $isConfig = false;
  60. if ($action === 'config') {
  61. $output = fopen(CONFIG_FILE, 'w+');
  62. if ($output) {
  63. if (flock($output, LOCK_EX)) {
  64. fwrite($output, '<?php' . PHP_EOL);
  65. fprintf(
  66. $output,
  67. '$config = %s;' . PHP_EOL,
  68. var_export($config, true)
  69. );
  70. flock($output, LOCK_UN);
  71. }
  72. fclose($output);
  73. }
  74. $isConfig = true;
  75. }
  76. $hasEvent = isset($_REQUEST['event']);
  77. if (!$isConfig and $hasSupplier) {
  78. if (!$hasEvent) {
  79. $now = new \DateTime('now');
  80. $current = new \DateTime($config[$supplier]['start']);
  81. $frequency = \DateInterval::createFromDateString($config[$supplier]['frequency']);
  82. $maxIterations = 1000;
  83. while (
  84. ($current->getTimestamp() < $now->getTimestamp())
  85. and ($maxIterations-- > 0)
  86. ) $current->add($frequency);
  87. $nextEvent = $current->format('Y-m-d');
  88. header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $nextEvent));
  89. die();
  90. }
  91. $event = $_REQUEST['event'];
  92. switch ($action) {
  93. case 'insert' :
  94. case 'delete' :
  95. $isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
  96. $output = fopen(DATA_FILE, 'a+');
  97. if (!$output) break;
  98. if (!flock($output, LOCK_EX)) break;
  99. if ($isBeginning)
  100. fwrite($output, '<?php' . PHP_EOL);
  101. $item = [];
  102. foreach (['name', 'choice', 'action'] as $field)
  103. $item[$field] = $_REQUEST[$field];
  104. $item['timestamp'] = time();
  105. $item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
  106. fprintf(
  107. $output,
  108. '$data[%s][%s][] = %s;' . PHP_EOL,
  109. var_export($supplier, true),
  110. var_export($event, true),
  111. str_replace(PHP_EOL, '', var_export($item, true))
  112. );
  113. flock($output, LOCK_UN);
  114. fclose($output);
  115. header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $event));
  116. die();
  117. }
  118. if (!isset($data)) $data = [];
  119. if (file_exists(DATA_FILE)) include DATA_FILE;
  120. $items = [];
  121. $allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
  122. usort($allItems, function ($a, $b) {
  123. $a = intval($a['timestamp']);
  124. $b = intval($b['timestamp']);
  125. if ($a === $b)
  126. return 0;
  127. return ($a < $b) ? -1 : 1;
  128. });
  129. foreach ($allItems as $item) {
  130. if ($item['action'] === 'insert') {
  131. $items[] = $item;
  132. } elseif ($item['action'] === 'delete') {
  133. foreach ($items as $index => $prevItem)
  134. if ($prevItem['hash'] === $item['hash'])
  135. unset($items[$index]);
  136. }
  137. }
  138. while (preg_match('/%([^%]+)%/i', $config[$supplier]['title'], $match))
  139. $config[$supplier]['title'] = str_replace(
  140. $match[0],
  141. ${$match[1]},
  142. $config[$supplier]['title']
  143. );
  144. }
  145. ?><!DOCTYPE html>
  146. <html lang="fr">
  147. <head>
  148. <meta charset="UTF-8" />
  149. <meta name="viewport" content="width=device-width, initial-scale=1" />
  150. <title><?php echo strip_tags($config[$supplier]['title']); ?></title>
  151. </head>
  152. <body>
  153. <?php if (!$hasSupplier) : ?>
  154. <p>pas de fournisseur</p>
  155. <?php else : ?>
  156. <?php if ($isConfig) : ?>
  157. <a href="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>">retour</a>
  158. <h1>config <?php echo $supplier; ?></h1>
  159. <form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
  160. <p>
  161. <label for="title">titre</label>
  162. <input type="text" name="title" value="<?php echo $config[$supplier]['title']; ?>" />
  163. </p>
  164. <p>
  165. <label for="description">description</label>
  166. <textarea name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
  167. </p>
  168. <p>
  169. <label for="choices">choix</label>
  170. <textarea name="choices" rows="10"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  171. </p>
  172. <p>
  173. <label for="start">début</label>
  174. <input type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  175. </p>
  176. <p>
  177. <label for="frequency">fréquence</label>
  178. <input type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  179. </p>
  180. <p>
  181. <label for="password">password</label>
  182. <input type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  183. </p>
  184. <button type="submit" name="action" value="config">config</button>
  185. </p>
  186. </form>
  187. <?php else : ?>
  188. <a href="<?php printf('%s?supplier=%s&action=config', $requestUrl, $supplier); ?>">config</a>
  189. <h1><?php echo $config[$supplier]['title']; ?></h1>
  190. <?php if (!empty($config[$supplier]['description'])) : ?>
  191. <p><?php echo $config[$supplier]['description']; ?></p>
  192. <?php endif; ?>
  193. <ul>
  194. <li>
  195. <form>
  196. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  197. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  198. <input type="text" name="name" required placeholder="nom" />
  199. <?php if (empty($config[$supplier]['choices'])) : ?>
  200. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  201. <?php else : ?>
  202. <select name="choice" required placeholder="choix">
  203. <option/>
  204. <?php foreach ($config[$supplier]['choices'] as $choice) : ?>
  205. <option><?php echo $choice; ?></option>
  206. <?php endforeach; ?>
  207. </select>
  208. <?php endif; ?>
  209. <button type="submit" name="action" value="insert">ajouter</button>
  210. </form>
  211. </li>
  212. <?php foreach ($items as $item) : ?>
  213. <li>
  214. <form>
  215. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  216. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  217. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  218. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  219. <?php echo $item['name']; ?>
  220. <?php if (!empty($item['choice'])) : ?>
  221. <?php echo $item['choice']; ?>
  222. <?php endif; ?>
  223. <button type="submit" name="action" value="delete">supprimer</button>
  224. </form>
  225. </li>
  226. <?php endforeach; ?>
  227. </ul>
  228. <?php endif; ?>
  229. <?php endif; ?>
  230. </body>
  231. </html>