Просмотр файла app/Classes/Metrika.php

Размер файла: 4.71Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Classes;
  6.  
  7. use App\Models\Counter;
  8. use App\Models\Counter24;
  9. use App\Models\Counter31;
  10. use App\Models\Online;
  11. use ErrorException;
  12. use Illuminate\Support\Facades\DB;
  13. use PDOException;
  14.  
  15. class Metrika
  16. {
  17. /**
  18. * Генерирует счетчик
  19. *
  20. * @param int $online
  21. *
  22. * @return void
  23. */
  24. public function getCounter(int $online): void
  25. {
  26. $counter = $this->getResultCounter();
  27.  
  28. if (! $counter) {
  29. $counter = (object) ['dayhosts' => 0, 'dayhits' => 0];
  30. }
  31.  
  32. // ----------------------------------------------------------------------//
  33. $img = imagecreatefrompng(public_path('assets/img/images/counter.png'));
  34. $color = imagecolorallocate($img, 62, 62, 62);
  35.  
  36. $pos = 66;
  37. if ($online >= 10 && $online < 100) {
  38. $pos = 54;
  39. }
  40. if ($online >= 100 && $online < 1000) {
  41. $pos = 42;
  42. }
  43.  
  44. imagettftext($img, 6, 0, 14, 7, $color, public_path('assets/fonts/font.ttf'), (string) formatShortNum($counter->dayhosts));
  45. imagettftext($img, 6, 0, 14, 13, $color, public_path('assets/fonts/font.ttf'), (string) formatShortNum($counter->dayhits));
  46. imagettftext($img, 12, 0, $pos, 13, $color, public_path('assets/fonts/font.ttf'), (string) $online);
  47.  
  48. imagepng($img, public_path('uploads/counters/counter_new.png'));
  49. imagedestroy($img);
  50.  
  51. try {
  52. rename(
  53. public_path('uploads/counters/counter_new.png'),
  54. public_path('uploads/counters/counter.png')
  55. );
  56. } catch (ErrorException $e) {
  57. //
  58. }
  59. }
  60.  
  61. /**
  62. * Сохраняет статистику
  63. *
  64. * @return void
  65. */
  66. public function saveStatistic(): void
  67. {
  68. session()->increment('hits');
  69.  
  70. if (session()->has('online') && session()->get('online') > SITETIME) {
  71. return;
  72. }
  73.  
  74. $period = date('Y-m-d H:00:00', SITETIME);
  75. $day = date('Y-m-d 00:00:00', SITETIME);
  76.  
  77. Online::query()->where('updated_at', '<', SITETIME - setting('timeonline'))->delete();
  78.  
  79. $user = getUser();
  80. $ip = getIp();
  81. $brow = getBrowser();
  82. $uid = md5($ip . $brow);
  83.  
  84. if ($user) {
  85. $user->update(['updated_at' => SITETIME]);
  86. }
  87.  
  88. try {
  89. $online = Online::query()
  90. ->where('uid', $uid)
  91. ->updateOrCreate([], [
  92. 'uid' => $uid,
  93. 'ip' => $ip,
  94. 'brow' => $brow,
  95. 'updated_at' => SITETIME,
  96. 'user_id' => $user->id ?? null,
  97. ]);
  98. $newHost = $online->wasRecentlyCreated;
  99. } catch (PDOException $e) {
  100. $newHost = false;
  101. }
  102.  
  103. // -----------------------------------------------------------//
  104. $counter = $this->getResultCounter();
  105. if (! $counter) {
  106. return;
  107. }
  108.  
  109. if (date('Y-m-d 00:00:00', strtotime($counter->period)) !== $day) {
  110. Counter31::query()->insertOrIgnore([
  111. 'period' => $period,
  112. 'hosts' => $counter->dayhosts,
  113. 'hits' => $counter->dayhits,
  114. ]);
  115.  
  116. $counter->update([
  117. 'dayhosts' => 0,
  118. 'dayhits' => 0,
  119. ]);
  120. }
  121.  
  122. if ($counter->period !== $period) {
  123. Counter24::query()->insertOrIgnore([
  124. 'period' => $period,
  125. 'hosts' => $counter->hosts24,
  126. 'hits' => $counter->hits24,
  127. ]);
  128.  
  129. $counter->update([
  130. 'period' => $period,
  131. 'hosts24' => 0,
  132. 'hits24' => 0,
  133. ]);
  134. }
  135.  
  136. // -----------------------------------------------------------//
  137. $hostsUpdate = [];
  138. if ($newHost) {
  139. $hostsUpdate = [
  140. 'allhosts' => DB::raw('allhosts + 1'),
  141. 'dayhosts' => DB::raw('dayhosts + 1'),
  142. 'hosts24' => DB::raw('hosts24 + 1'),
  143. ];
  144. }
  145.  
  146. $hits = session()->get('hits');
  147.  
  148. $hitsUpdate = [
  149. 'allhits' => DB::raw('allhits + ' . $hits),
  150. 'dayhits' => DB::raw('dayhits + ' . $hits),
  151. 'hits24' => DB::raw('hits24 + ' . $hits),
  152. ];
  153.  
  154. $counter->update(array_merge($hostsUpdate, $hitsUpdate));
  155.  
  156. session()->put('hits', 0);
  157. session()->put('online', strtotime('+30 seconds', SITETIME));
  158. }
  159.  
  160. /**
  161. * Returns counter result
  162. *
  163. * @return object|null
  164. */
  165. private function getResultCounter(): ?object
  166. {
  167. return Counter::query()->first();
  168. }
  169. }