ConfigCacheService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace App\Services;
  3. use App\Models\ConfigCache as ConfigCacheModel;
  4. use Cache;
  5. use Illuminate\Database\QueryException;
  6. class ConfigCacheService
  7. {
  8. const CACHE_KEY = 'config_cache:_v0-key:';
  9. const PROTECTED_KEYS = [
  10. 'filesystems.disks.s3.key',
  11. 'filesystems.disks.s3.secret',
  12. 'filesystems.disks.spaces.key',
  13. 'filesystems.disks.spaces.secret',
  14. 'captcha.secret',
  15. 'captcha.sitekey',
  16. ];
  17. public static function get($key)
  18. {
  19. $cacheKey = self::CACHE_KEY.$key;
  20. $ttl = now()->addHours(12);
  21. if (! config('instance.enable_cc')) {
  22. return config($key);
  23. }
  24. try {
  25. return Cache::remember($cacheKey, $ttl, function () use ($key) {
  26. $allowed = [
  27. 'app.name',
  28. 'app.short_description',
  29. 'app.description',
  30. 'app.rules',
  31. 'pixelfed.max_photo_size',
  32. 'pixelfed.max_album_length',
  33. 'pixelfed.image_quality',
  34. 'pixelfed.media_types',
  35. 'pixelfed.open_registration',
  36. 'federation.activitypub.enabled',
  37. 'instance.stories.enabled',
  38. 'pixelfed.oauth_enabled',
  39. 'pixelfed.import.instagram.enabled',
  40. 'pixelfed.bouncer.enabled',
  41. 'federation.activitypub.authorized_fetch',
  42. 'pixelfed.enforce_email_verification',
  43. 'pixelfed.max_account_size',
  44. 'pixelfed.enforce_account_limit',
  45. 'uikit.custom.css',
  46. 'uikit.custom.js',
  47. 'uikit.show_custom.css',
  48. 'uikit.show_custom.js',
  49. 'about.title',
  50. 'pixelfed.cloud_storage',
  51. 'account.autofollow',
  52. 'account.autofollow_usernames',
  53. 'config.discover.features',
  54. 'instance.has_legal_notice',
  55. 'instance.avatar.local_to_cloud',
  56. 'pixelfed.directory',
  57. 'app.banner_image',
  58. 'pixelfed.directory.submission-key',
  59. 'pixelfed.directory.submission-ts',
  60. 'pixelfed.directory.has_submitted',
  61. 'pixelfed.directory.latest_response',
  62. 'pixelfed.directory.is_synced',
  63. 'pixelfed.directory.testimonials',
  64. 'instance.landing.show_directory',
  65. 'instance.landing.show_explore',
  66. 'instance.admin.pid',
  67. 'instance.banner.blurhash',
  68. 'autospam.nlp.enabled',
  69. 'instance.curated_registration.enabled',
  70. 'federation.migration',
  71. 'pixelfed.max_caption_length',
  72. 'pixelfed.max_bio_length',
  73. 'pixelfed.max_name_length',
  74. 'pixelfed.min_password_length',
  75. 'pixelfed.max_avatar_size',
  76. 'pixelfed.max_altext_length',
  77. 'pixelfed.allow_app_registration',
  78. 'pixelfed.app_registration_rate_limit_attempts',
  79. 'pixelfed.app_registration_rate_limit_decay',
  80. 'pixelfed.app_registration_confirm_rate_limit_attempts',
  81. 'pixelfed.app_registration_confirm_rate_limit_decay',
  82. 'instance.embed.profile',
  83. 'instance.embed.post',
  84. 'captcha.enabled',
  85. 'captcha.secret',
  86. 'captcha.sitekey',
  87. 'captcha.active.login',
  88. 'captcha.active.register',
  89. 'captcha.triggers.login.enabled',
  90. 'captcha.triggers.login.attempts',
  91. 'federation.custom_emoji.enabled',
  92. 'pixelfed.optimize_image',
  93. 'pixelfed.optimize_video',
  94. 'pixelfed.max_collection_length',
  95. 'media.delete_local_after_cloud',
  96. 'instance.user_filters.max_user_blocks',
  97. 'instance.user_filters.max_user_mutes',
  98. 'instance.user_filters.max_domain_blocks',
  99. 'filesystems.disks.s3.key',
  100. 'filesystems.disks.s3.secret',
  101. 'filesystems.disks.s3.region',
  102. 'filesystems.disks.s3.bucket',
  103. 'filesystems.disks.s3.visibility',
  104. 'filesystems.disks.s3.url',
  105. 'filesystems.disks.s3.endpoint',
  106. 'filesystems.disks.s3.use_path_style_endpoint',
  107. 'filesystems.disks.spaces.key',
  108. 'filesystems.disks.spaces.secret',
  109. 'filesystems.disks.spaces.region',
  110. 'filesystems.disks.spaces.bucket',
  111. 'filesystems.disks.spaces.visibility',
  112. 'filesystems.disks.spaces.url',
  113. 'filesystems.disks.spaces.endpoint',
  114. 'filesystems.disks.spaces.use_path_style_endpoint',
  115. 'instance.stats.total_local_posts',
  116. // 'system.user_mode'
  117. ];
  118. if (! config('instance.enable_cc')) {
  119. return config($key);
  120. }
  121. if (! in_array($key, $allowed)) {
  122. return config($key);
  123. }
  124. $protect = false;
  125. $protected = null;
  126. if (in_array($key, self::PROTECTED_KEYS)) {
  127. $protect = true;
  128. }
  129. $v = config($key);
  130. $c = ConfigCacheModel::where('k', $key)->first();
  131. if ($c) {
  132. if ($protect) {
  133. return decrypt($c->v) ?? config($key);
  134. } else {
  135. return $c->v ?? config($key);
  136. }
  137. }
  138. if (! $v) {
  139. return;
  140. }
  141. if ($protect && $v) {
  142. $protected = encrypt($v);
  143. }
  144. $cc = new ConfigCacheModel;
  145. $cc->k = $key;
  146. $cc->v = $protect ? $protected : $v;
  147. $cc->save();
  148. return $v;
  149. });
  150. } catch (Exception|QueryException $e) {
  151. return config($key);
  152. }
  153. }
  154. public static function put($key, $val)
  155. {
  156. $exists = ConfigCacheModel::whereK($key)->first();
  157. $protect = false;
  158. $protected = null;
  159. if (in_array($key, self::PROTECTED_KEYS)) {
  160. $protect = true;
  161. $protected = encrypt($val);
  162. }
  163. if ($exists) {
  164. $exists->v = $protect ? $protected : $val;
  165. $exists->save();
  166. Cache::put(self::CACHE_KEY.$key, $val, now()->addHours(12));
  167. return self::get($key);
  168. }
  169. $cc = new ConfigCacheModel;
  170. $cc->k = $key;
  171. $cc->v = $protect ? $protected : $val;
  172. $cc->save();
  173. Cache::put(self::CACHE_KEY.$key, $val, now()->addHours(12));
  174. return self::get($key);
  175. }
  176. }