PluginProbe
404 Solution / 4.1.19
404 Solution v4.1.19
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / php / FileSync.php

FileSync.php in 404 Solution 4.1.19, at includes/php/FileSync.php

77 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 if (!defined('ABSPATH')) {
5 exit;
6 }
7
8 class ABJ_404_Solution_FileSync {
9
10 /** @var self|null */
11 private static $instance = null;
12
13 /** @return self */
14 public static function getInstance(): self {
15 if (self::$instance == null) {
16 self::$instance = new ABJ_404_Solution_FileSync();
17 }
18
19 return self::$instance;
20 }
21
22 /**
23 * @param string $key
24 * @return string
25 */
26 function getSyncFilePath(string $key): string {
27 $filePath = abj404_getUploadsDir() . 'SYNC_FILE_' . $key . '.txt';
28 return $filePath;
29 }
30
31 /**
32 * @param string $key
33 * @return string
34 */
35 function getOwnerFromFile(string $key): string {
36 $filePath = $this->getSyncFilePath($key);
37 $fileUtils = abj_service('functions');
38
39 // Fixed: TOCTOU race condition - catch exception instead of check-then-read
40 try {
41 $contents = $fileUtils->readFileContents($filePath, false);
42 return $contents;
43 } catch (Exception $e) {
44 // File doesn't exist or can't be read - return empty string
45 return "";
46 }
47 }
48
49 /**
50 * @param string $key
51 * @param string $uniqueID
52 * @return void
53 */
54 function writeOwnerToFile(string $key, string $uniqueID): void {
55 $filePath = $this->getSyncFilePath($key);
56
57 // Fixed: Check return value to handle write failures (disk full, permissions, etc.)
58 $result = @file_put_contents($filePath, $uniqueID, LOCK_EX);
59
60 if ($result === false) {
61 throw new Exception("Failed to write lock file: " . $filePath);
62 }
63 }
64
65 /**
66 * @param string $uniqueID
67 * @param string $key
68 * @return void
69 */
70 function releaseLock(string $uniqueID, string $key): void {
71 $filePath = $this->getSyncFilePath($key);
72 $fileUtils = abj_service('functions');
73 $fileUtils->safeUnlink($filePath);
74 }
75
76 }
77