120 lines
5.3 KiB
PHP
120 lines
5.3 KiB
PHP
<?php
|
|
// ─────────────────────────────────────────────────────────────────────────
|
|
// One-off permission fixer for the PayPal integration's data/ directory.
|
|
// Upload to /pp-integration/ and open in a browser:
|
|
// https://dev.hotlunches.net/pp-integration/fix_perms.php
|
|
// It makes data/ writable so onboarding can save school_merchants.json etc.
|
|
// DELETE THIS FILE once data/ is writable.
|
|
// ─────────────────────────────────────────────────────────────────────────
|
|
header('Content-Type: text/plain');
|
|
|
|
$base = __DIR__;
|
|
$dataDir = $base . '/data';
|
|
$dataFiles = ['school_merchants.json', 'pending_onboarding.json'];
|
|
|
|
function describe($p) {
|
|
if (!file_exists($p)) return '(missing)';
|
|
clearstatcache(true, $p);
|
|
$perms = substr(sprintf('%o', fileperms($p)), -4);
|
|
$owner = fileowner($p);
|
|
$group = filegroup($p);
|
|
if (function_exists('posix_getpwuid')) { $o = posix_getpwuid($owner); if ($o) $owner = $o['name'] . " ($owner)"; }
|
|
if (function_exists('posix_getgrgid')) { $g = posix_getgrgid($group); if ($g) $group = $g['name'] . " ($group)"; }
|
|
return "perms=$perms owner=$owner group=$group writable=" . (is_writable($p) ? 'YES' : 'NO');
|
|
}
|
|
|
|
function testWrite($dir) {
|
|
clearstatcache();
|
|
$probe = $dir . '/_write_probe.txt';
|
|
$ok = @file_put_contents($probe, 'ok ' . date('c'));
|
|
if ($ok !== false) @unlink($probe);
|
|
return $ok !== false;
|
|
}
|
|
|
|
echo "=== data/ permission fixer ===\n";
|
|
echo "Script dir : $base\n";
|
|
echo "Data dir : $dataDir\n\n";
|
|
|
|
echo "PHP runs as:\n";
|
|
echo " getmyuid() : " . getmyuid() . "\n";
|
|
echo " getmygid() : " . getmygid() . "\n";
|
|
if (function_exists('posix_geteuid')) {
|
|
echo " effective uid : " . posix_geteuid() . "\n";
|
|
if (function_exists('posix_getpwuid')) {
|
|
$pw = posix_getpwuid(posix_geteuid());
|
|
echo " effective user : " . ($pw['name'] ?? '?') . "\n";
|
|
}
|
|
}
|
|
echo " get_current_user(): " . get_current_user() . "\n\n";
|
|
|
|
echo "Before:\n";
|
|
echo " parent : " . describe($base) . "\n";
|
|
echo " data : " . describe($dataDir) . "\n\n";
|
|
|
|
if (testWrite($dataDir)) {
|
|
echo "data/ is ALREADY writable. Nothing to do. You can delete fix_perms.php.\n";
|
|
exit;
|
|
}
|
|
|
|
// ── Attempt 1: chmod the directory (works if PHP owns it) ──────────────────
|
|
echo "--- Attempt 1: chmod 0777 data/ ---\n";
|
|
$r = @chmod($dataDir, 0777);
|
|
echo " chmod: " . ($r ? 'OK' : 'FAILED') . "\n";
|
|
foreach ($dataFiles as $f) {
|
|
$fp = $dataDir . '/' . $f;
|
|
if (file_exists($fp)) { @chmod($fp, 0666); }
|
|
}
|
|
echo " data now: " . describe($dataDir) . "\n";
|
|
|
|
if (testWrite($dataDir)) {
|
|
echo "\nSUCCESS: data/ is now writable. Re-run onboarding, then DELETE fix_perms.php.\n";
|
|
exit;
|
|
}
|
|
|
|
// ── Attempt 2: recreate data/ so it is owned by the PHP user ───────────────
|
|
// Only possible if PHP can write the parent directory.
|
|
echo "\n--- Attempt 2: recreate data/ as PHP-owned ---\n";
|
|
if (is_writable($base)) {
|
|
// preserve existing JSON in memory
|
|
$preserved = [];
|
|
foreach ($dataFiles as $f) {
|
|
$fp = $dataDir . '/' . $f;
|
|
if (file_exists($fp)) $preserved[$f] = file_get_contents($fp);
|
|
}
|
|
$backup = $base . '/data_old_' . date('Ymd_His');
|
|
if (@rename($dataDir, $backup)) {
|
|
echo " moved old data/ -> " . basename($backup) . " (kept as backup)\n";
|
|
if (@mkdir($dataDir, 0777, true)) {
|
|
echo " created fresh data/ (owned by PHP user)\n";
|
|
foreach ($preserved as $f => $c) @file_put_contents($dataDir . '/' . $f, $c);
|
|
if (testWrite($dataDir)) {
|
|
echo " data now: " . describe($dataDir) . "\n";
|
|
echo "\nSUCCESS via recreate. Old data kept in " . basename($backup) . ".\n";
|
|
echo "Re-run onboarding, then DELETE fix_perms.php (and the backup folder if you like).\n";
|
|
exit;
|
|
}
|
|
echo " still not writable; restoring original...\n";
|
|
// best-effort restore
|
|
@array_map('unlink', glob($dataDir . '/*'));
|
|
@rmdir($dataDir);
|
|
}
|
|
@rename($backup, $dataDir);
|
|
echo " restored original data/\n";
|
|
} else {
|
|
echo " rename failed (parent not writable enough by PHP)\n";
|
|
}
|
|
} else {
|
|
echo " parent dir is not writable by PHP — cannot recreate automatically.\n";
|
|
}
|
|
|
|
// ── Could not fix from PHP ─────────────────────────────────────────────────
|
|
echo "\n=========================================================\n";
|
|
echo "Could not fix automatically (PHP does not own the folder).\n";
|
|
echo "Use your hosting control panel's File Manager (or an FTP client):\n";
|
|
echo " 1. Go to the 'pp-integration' folder.\n";
|
|
echo " 2. Right-click the 'data' folder -> Change Permissions / CHMOD.\n";
|
|
echo " 3. Set it to 777 (read/write/execute for all).\n";
|
|
echo " 4. Also set the two .json files inside to 666.\n";
|
|
echo "Then reload onboard.php?debug=1 to confirm 'is_writable: yes'.\n";
|
|
echo "=========================================================\n";
|