functions.presets.inc.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. function presets($_action, $_kind, $_object)
  3. {
  4. if ($_SESSION['mailcow_cc_role'] !== 'admin') {
  5. $_SESSION['return'][] = [
  6. 'type' => 'danger',
  7. 'log' => [__FUNCTION__, $_action, $_data_log],
  8. 'msg' => 'access_denied',
  9. ];
  10. return false;
  11. }
  12. global $lang;
  13. if ($_action === 'get') {
  14. $kind = strtolower(trim($_kind));
  15. $langSection = 'admin';
  16. $presetsPath = __DIR__ . '/presets/' . $kind;
  17. if (!in_array($kind, ['admin-rspamd', 'mailbox-sieve'], true)) {
  18. return [];
  19. }
  20. if ($kind === 'mailbox-sieve') {
  21. $langSection = 'mailbox';
  22. }
  23. if ($_object !== 'all') {
  24. return getPresetFromFilePath($presetsPath . '/' . $_object . '.yml', $langSection);
  25. }
  26. $presets = [];
  27. foreach (glob($presetsPath . '/*.yml') as $filename) {
  28. $presets[] = getPresetFromFilePath($filename, $langSection);
  29. }
  30. return $presets;
  31. }
  32. return [];
  33. }
  34. function getPresetFromFilePath($filePath, $langSection)
  35. {
  36. global $lang;
  37. $preset = Spyc::YAMLLoad($filePath);
  38. $preset = ['name' => basename($filePath, '.yml')] + $preset;
  39. /* get translated headlines */
  40. if (isset($preset['headline']) && strpos($preset['headline'], 'lang.') === 0) {
  41. $langTextName = trim(substr($preset['headline'], 5));
  42. if (isset($lang[$langSection][$langTextName])) {
  43. $preset['headline'] = $lang[$langSection][$langTextName];
  44. }
  45. }
  46. return $preset;
  47. }