Click.php
1 year ago
Click_Processing_Job.php
1 year ago
Config_File_Manager.php
1 year ago
Link_Rule.php
1 year ago
Link_Rule_Finder.php
1 year ago
Click_Processing_Job.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Click_Tracking; |
| 4 | |
| 5 | use IAWP\Cron_Job; |
| 6 | use IAWP\Models\Visitor; |
| 7 | use IAWP\Payload_Validator; |
| 8 | use IAWP\Utils\Security; |
| 9 | /** @internal */ |
| 10 | class Click_Processing_Job extends Cron_Job |
| 11 | { |
| 12 | protected $name = 'iawp_click_processing'; |
| 13 | protected $interval = 'every_minute'; |
| 14 | public function handle() : void |
| 15 | { |
| 16 | // Periodically recreate the config file |
| 17 | \IAWP\Click_Tracking\Config_File_Manager::recreate(); |
| 18 | if (\IAWPSCOPED\iawp_is_free()) { |
| 19 | self::unschedule(); |
| 20 | return; |
| 21 | } |
| 22 | $job_id = \rand(); |
| 23 | $original_file = \IAWPSCOPED\iawp_path_to('iawp-click-data.php'); |
| 24 | $job_file = \IAWPSCOPED\iawp_path_to("iawp-click-data-{$job_id}.php"); |
| 25 | if (!\is_file($original_file)) { |
| 26 | return; |
| 27 | } |
| 28 | $original_handle = \fopen($original_file, 'r'); |
| 29 | if ($original_handle === \false) { |
| 30 | return; |
| 31 | } |
| 32 | \fclose($original_handle); |
| 33 | \rename($original_file, $job_file); |
| 34 | $job_handle = \fopen($job_file, 'r'); |
| 35 | if ($job_handle === \false) { |
| 36 | return; |
| 37 | } |
| 38 | // Skip the first line which is just a php exit; statement |
| 39 | \fgets($job_handle); |
| 40 | while (($json = \fgets($job_handle)) !== \false) { |
| 41 | $event = \json_decode($json, \true); |
| 42 | $event['href'] = Security::string($event['href']); |
| 43 | $event['classes'] = Security::string($event['classes']); |
| 44 | if (\is_null($event)) { |
| 45 | continue; |
| 46 | } |
| 47 | $payload_validator = Payload_Validator::new($event['payload'], $event['signature']); |
| 48 | if (!$payload_validator->is_valid() || \is_null($payload_validator->resource())) { |
| 49 | continue; |
| 50 | } |
| 51 | $click = \IAWP\Click_Tracking\Click::new(['href' => $event['href'], 'classes' => $event['classes'], 'resource_id' => $payload_validator->resource()['id'], 'visitor_id' => Visitor::fetch_visitor_id_by_hash($event['visitor_token']), 'created_at' => \DateTime::createFromFormat('U', $event['created_at'])]); |
| 52 | $click->track(); |
| 53 | } |
| 54 | \fclose($job_handle); |
| 55 | \unlink($job_file); |
| 56 | } |
| 57 | } |
| 58 |