UpdatePersonValidator.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Util\ActivityPub\Validator;
  3. use Validator;
  4. use Closure;
  5. use Illuminate\Validation\Rule;
  6. use \App\Rules\SameHostDomain;
  7. class UpdatePersonValidator
  8. {
  9. public static function validate($payload)
  10. {
  11. $valid = Validator::make($payload, [
  12. '@context' => 'required',
  13. 'id' => 'required|string|url',
  14. 'type' => [
  15. 'required',
  16. Rule::in(['Update'])
  17. ],
  18. 'actor' => 'required|url',
  19. 'object' => 'required',
  20. 'object.id' => [
  21. 'required',
  22. 'url',
  23. 'same:actor',
  24. function (string $attribute, mixed $value, Closure $fail) use($payload) {
  25. self::sameHost($attribute, $value, $fail, $payload['actor']);
  26. },
  27. ],
  28. 'object.type' => [
  29. 'required',
  30. Rule::in(['Person'])
  31. ],
  32. 'object.publicKey' => 'required',
  33. 'object.publicKey.id' => [
  34. 'required',
  35. 'url',
  36. function (string $attribute, mixed $value, Closure $fail) use($payload) {
  37. self::sameHost($attribute, $value, $fail, $payload['actor']);
  38. },
  39. ],
  40. 'object.publicKey.owner' => [
  41. 'required',
  42. 'url',
  43. 'same:actor',
  44. function (string $attribute, mixed $value, Closure $fail) use($payload) {
  45. self::sameHost($attribute, $value, $fail, $payload['actor']);
  46. },
  47. ],
  48. 'object.publicKey.publicKeyPem' => 'required|string',
  49. 'object.url' => [
  50. 'required',
  51. 'url',
  52. function (string $attribute, mixed $value, Closure $fail) use($payload) {
  53. self::sameHost($attribute, $value, $fail, $payload['actor']);
  54. },
  55. ],
  56. 'object.summary' => 'required|string|nullable',
  57. 'object.preferredUsername' => 'required|string',
  58. 'object.name' => 'required|string|nullable',
  59. 'object.inbox' => [
  60. 'required',
  61. 'url',
  62. function (string $attribute, mixed $value, Closure $fail) use($payload) {
  63. self::sameHost($attribute, $value, $fail, $payload['actor']);
  64. },
  65. ],
  66. 'object.outbox' => [
  67. 'required',
  68. 'url',
  69. function (string $attribute, mixed $value, Closure $fail) use($payload) {
  70. self::sameHost($attribute, $value, $fail, $payload['actor']);
  71. },
  72. ],
  73. 'object.following' => [
  74. 'required',
  75. 'url',
  76. function (string $attribute, mixed $value, Closure $fail) use($payload) {
  77. self::sameHost($attribute, $value, $fail, $payload['actor']);
  78. },
  79. ],
  80. 'object.followers' => [
  81. 'required',
  82. 'url',
  83. function (string $attribute, mixed $value, Closure $fail) use($payload) {
  84. self::sameHost($attribute, $value, $fail, $payload['actor']);
  85. },
  86. ],
  87. 'object.manuallyApprovesFollowers' => 'required',
  88. 'object.icon' => 'sometimes|nullable',
  89. 'object.icon.type' => 'sometimes|required_with:object.icon.url,object.icon.mediaType|in:Image',
  90. 'object.icon.url' => 'sometimes|required_with:object.icon.type,object.icon.mediaType|url',
  91. 'object.icon.mediaType' => 'sometimes|required_with:object.icon.url,object.icon.type|in:image/jpeg,image/png,image/jpg',
  92. 'object.endpoints' => 'sometimes',
  93. 'object.endpoints.sharedInbox' => [
  94. 'sometimes',
  95. 'url',
  96. function (string $attribute, mixed $value, Closure $fail) use($payload) {
  97. self::sameHost($attribute, $value, $fail, $payload['actor']);
  98. },
  99. ]
  100. ])->passes();
  101. return $valid;
  102. }
  103. public static function sameHost(string $attribute, mixed $value, Closure $fail, string $comparedHost)
  104. {
  105. if(empty($value)) {
  106. $fail('The ' . $attribute . ' is invalid or empty');
  107. }
  108. $host = parse_url($value, PHP_URL_HOST);
  109. $idHost = parse_url($comparedHost, PHP_URL_HOST);
  110. if ($host !== $idHost) {
  111. $fail('The ' . $attribute . ' is invalid');
  112. }
  113. }
  114. }