sponsors.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Carbon\CarbonImmutable;
  11. require_once __DIR__.'/vendor/autoload.php';
  12. function getMaxHistoryMonthsByAmount($amount): int
  13. {
  14. if ($amount >= 50) {
  15. return 6;
  16. }
  17. if ($amount >= 20) {
  18. return 4;
  19. }
  20. return 2;
  21. }
  22. function getHtmlAttribute($rawValue): string
  23. {
  24. return str_replace(
  25. ['​', "\r"],
  26. '',
  27. trim(htmlspecialchars((string) $rawValue), "  \n\r\t\v\0"),
  28. );
  29. }
  30. function getOpenCollectiveSponsors(): string
  31. {
  32. $customSponsorOverride = [
  33. // For consistency and equity among sponsors, as of now, we kindly ask our sponsors
  34. // to provide an image having a width/height ratio between 1/1 and 2/1.
  35. // By default, we'll show the member picture from OpenCollective, and will resize it if bigger
  36. 662698 => [
  37. // alt attribute
  38. 'name' => 'Non Gamstop Casinos',
  39. // title attribute
  40. 'description' => 'Casinos not on Gamstop',
  41. // src attribute
  42. 'image' => 'https://lgcnews.com/wp-content/uploads/2018/01/LGC-logo-v8-temp.png',
  43. // href attribute
  44. 'website' => 'https://lgcnews.com/',
  45. ],
  46. 663069 => [
  47. // alt attribute
  48. 'name' => 'Ставки на спорт Favbet',
  49. // href attribute
  50. 'website' => 'https://www.favbet.ua/uk/',
  51. ],
  52. 676798 => [
  53. // alt attribute
  54. 'name' => 'Top Casinos Canada',
  55. // title attribute
  56. 'description' => 'Top Casinos Canada',
  57. // src attribute
  58. 'image' => 'https://topcasino.net/img/topcasino-logo-cover.png',
  59. // href attribute
  60. 'website' => 'https://topcasino.net/',
  61. ],
  62. ];
  63. $members = json_decode(file_get_contents('https://opencollective.com/carbon/members/all.json'), true);
  64. foreach ($members as &$member) {
  65. $member = array_merge($member, $customSponsorOverride[$member['MemberId']] ?? []);
  66. }
  67. // Adding sponsors paying via other payment methods
  68. $members[] = [
  69. 'MemberId' => 1,
  70. 'createdAt' => '2019-01-01 02:00',
  71. 'type' => 'ORGANIZATION',
  72. 'role' => 'BACKER',
  73. 'tier' => 'backer+',
  74. 'isActive' => true,
  75. 'totalAmountDonated' => 1000,
  76. 'currency' => 'USD',
  77. 'lastTransactionAt' => CarbonImmutable::now()->format('Y-m-d').' 02:00',
  78. 'lastTransactionAmount' => 25,
  79. 'profile' => 'https://tidelift.com/',
  80. 'name' => 'Tidelift',
  81. 'description' => 'Get professional support for Carbon',
  82. 'image' => 'https://carbon.nesbot.com/docs/sponsors/tidelift-brand.png',
  83. 'website' => 'https://tidelift.com/subscription/pkg/packagist-nesbot-carbon?utm_source=packagist-nesbot-carbon&utm_medium=referral&utm_campaign=docs',
  84. ];
  85. $members[] = [
  86. 'MemberId' => 2,
  87. 'createdAt' => '2024-11-14 02:00',
  88. 'type' => 'ORGANIZATION',
  89. 'role' => 'BACKER',
  90. 'tier' => 'backer+ yearly',
  91. 'isActive' => true,
  92. 'totalAmountDonated' => 170,
  93. 'currency' => 'USD',
  94. 'lastTransactionAt' => '2024-11-14 02:00',
  95. 'lastTransactionAmount' => 170,
  96. 'profile' => 'https://www.slotozilla.com/nz/free-spins',
  97. 'name' => 'Slotozilla',
  98. 'description' => 'Slotozilla website',
  99. 'image' => 'https://carbon.nesbot.com/docs/sponsors/slotozilla.png',
  100. 'website' => 'https://www.slotozilla.com/nz/free-spins',
  101. ];
  102. $list = array_filter($members, static fn (array $member): bool => $member['totalAmountDonated'] > 3 && $member['role'] !== 'HOST' && (
  103. $member['totalAmountDonated'] > 100 ||
  104. $member['lastTransactionAt'] > CarbonImmutable::now()
  105. ->subMonthsNoOverflow(getMaxHistoryMonthsByAmount($member['lastTransactionAmount']))
  106. ->format('Y-m-d h:i') ||
  107. $member['isActive'] && $member['lastTransactionAmount'] >= 30
  108. ));
  109. $list = array_map(static function (array $member): array {
  110. $createdAt = CarbonImmutable::parse($member['createdAt']);
  111. $lastTransactionAt = CarbonImmutable::parse($member['lastTransactionAt']);
  112. if ($createdAt->format('d H:i:s.u') > $lastTransactionAt->format('d H:i:s.u')) {
  113. $createdAt = $createdAt
  114. ->setDay($lastTransactionAt->day)
  115. ->modify($lastTransactionAt->format('H:i:s.u'));
  116. }
  117. $isYearly = str_contains(strtolower($member['tier'] ?? ''), 'yearly');
  118. $monthlyContribution = (float) (
  119. ($isYearly && $lastTransactionAt > CarbonImmutable::parse('-1 year'))
  120. ? ($member['lastTransactionAmount'] / 11.2) // 11.2 instead of 12 to include the discount for yearly subscription
  121. : ($member['totalAmountDonated'] / ceil($createdAt->floatDiffInMonths()))
  122. );
  123. if (!$isYearly) {
  124. if (
  125. $lastTransactionAt->isAfter('last month') &&
  126. $member['lastTransactionAmount'] > $monthlyContribution
  127. ) {
  128. $monthlyContribution = (float) $member['lastTransactionAmount'];
  129. }
  130. if ($lastTransactionAt->isBefore('-75 days')) {
  131. $days = min(120, $lastTransactionAt->diffInDays('now') - 70);
  132. $monthlyContribution *= 1 - $days / 240;
  133. }
  134. }
  135. $yearlyContribution = (float) (
  136. $isYearly
  137. ? (12 * $monthlyContribution)
  138. : ($member['totalAmountDonated'] / max(1, $createdAt->floatDiffInYears()))
  139. );
  140. $status = null;
  141. $rank = 0;
  142. if ($monthlyContribution > 50 || $yearlyContribution > 900) {
  143. $status = 'sponsor';
  144. $rank = 5;
  145. } elseif ($monthlyContribution > 29 || $yearlyContribution > 700) {
  146. $status = 'sponsor';
  147. $rank = 4;
  148. } elseif ($monthlyContribution > 14.5 || $yearlyContribution > 500) {
  149. $status = 'backerPlus';
  150. $rank = 3;
  151. } elseif ($monthlyContribution > 4.5 || $yearlyContribution > 80) {
  152. $status = 'backer';
  153. $rank = 2;
  154. } elseif ($member['totalAmountDonated'] > 0) {
  155. $status = 'helper';
  156. $rank = 1;
  157. }
  158. return array_merge($member, [
  159. 'star' => ($monthlyContribution > 98 || $yearlyContribution > 800),
  160. 'status' => $status,
  161. 'rank' => $rank,
  162. 'monthlyContribution' => $monthlyContribution,
  163. 'yearlyContribution' => $yearlyContribution,
  164. ]);
  165. }, $list);
  166. usort($list, static function (array $a, array $b): int {
  167. return ($b['star'] <=> $a['star'])
  168. ?: ($b['rank'] <=> $a['rank'])
  169. ?: ($b['monthlyContribution'] <=> $a['monthlyContribution'])
  170. ?: ($b['totalAmountDonated'] <=> $a['totalAmountDonated']);
  171. });
  172. $membersByUrl = [];
  173. $output = '';
  174. $extra = '';
  175. foreach ($list as $member) {
  176. $url = $member['website'] ?? $member['profile'];
  177. if (isset($membersByUrl[$url]) || !\in_array($member['status'], ['sponsor', 'backerPlus'], true)) {
  178. continue;
  179. }
  180. $membersByUrl[$url] = $member;
  181. $href = htmlspecialchars($url);
  182. $src = $customSponsorImages[$member['MemberId'] ?? ''] ?? $member['image'] ?? (strtr($member['profile'], ['https://opencollective.com/' => 'https://images.opencollective.com/']).'/avatar/256.png');
  183. [$x, $y] = @getimagesize($src) ?: [0, 0];
  184. $validImage = ($x && $y);
  185. $src = $validImage ? htmlspecialchars($src) : 'https://opencollective.com/static/images/default-guest-logo.svg';
  186. $height = match ($member['status']) {
  187. 'sponsor' => 64,
  188. 'backerPlus' => 42,
  189. 'backer' => 32,
  190. default => 24,
  191. };
  192. $rel = match ($member['status']) {
  193. 'sponsor', 'backerPlus' => '',
  194. default => ' rel="sponsored"',
  195. };
  196. $width = min($height * 2, $validImage ? round($x * $height / $y) : $height);
  197. if (!str_contains($href, 'utm_source') && !preg_match('/^https?:\/\/(?:www\.)?(?:onlinekasyno-polis\.pl|zonaminecraft\.net|slotozilla\.com)(\/.*)?/', $href)) {
  198. $href .= (!str_contains($href, '?') ? '?' : '&amp;').'utm_source=opencollective&amp;utm_medium=github&amp;utm_campaign=Carbon';
  199. }
  200. $title = getHtmlAttribute(($member['description'] ?? null) ?: $member['name']);
  201. $alt = getHtmlAttribute($member['name']);
  202. if ($member['star']) {
  203. $width *= 1.5;
  204. $height *= 1.5;
  205. }
  206. $link = "\n".'<a title="'.$title.'" href="'.$href.'" target="_blank"'.$rel.'>'.
  207. '<img alt="'.$alt.'" src="'.$src.'" width="'.$width.'" height="'.$height.'">'.
  208. '</a>';
  209. if ($member['rank'] >= 5) {
  210. $output .= $link;
  211. continue;
  212. }
  213. $extra .= $link;
  214. }
  215. $github = [
  216. 8343178 => 'ssddanbrown',
  217. ];
  218. foreach ($github as $avatar => $user) {
  219. $extra .= "\n".'<a title="'.$user.'" href="https://github.com/'.$user.'" target="_blank">'.
  220. '<img alt="'.$user.'" src="https://avatars.githubusercontent.com/u/'.$avatar.'?s=128&v=4" width="42" height="42">'.
  221. '</a>';
  222. }
  223. return $output.'<details><summary>See more</summary>'.$extra.'</details>';
  224. }
  225. file_put_contents('readme.md', preg_replace_callback(
  226. '/(<!-- <open-collective-sponsors> -->)[\s\S]+(<!-- <\/open-collective-sponsors> -->)/',
  227. static function (array $match): string {
  228. return $match[1].getOpenCollectiveSponsors().$match[2];
  229. },
  230. file_get_contents('readme.md'),
  231. ));