Skip to content

Instantly share code, notes, and snippets.

@dvygolov
Last active July 21, 2024 17:29
Show Gist options
  • Save dvygolov/1e277496c4d0ee3a08efd76000968f79 to your computer and use it in GitHub Desktop.
Save dvygolov/1e277496c4d0ee3a08efd76000968f79 to your computer and use it in GitHub Desktop.
Convert cookies from "name=value" format (usually used in asian FB accounts) to JSON format
<?php
echo "Asian FB Cookies to JSON Converter v1.2\n";
echo " by Yellow Web\n\n";
// Check if the filename is provided
if ($argc < 2) {
echo "Usage: php script.php filename\n";
exit(1);
}
// Get the filename from the command line argument
$filename = __DIR__."/".$argv[1];
if (!is_file($filename)){
echo "File: {$filename} DOES NOT EXIST!\n";
exit(1);
}
// Read the entire file into an array of lines
$lines = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
// Prepare the base cookie attributes
$baseCookie = [
"domain" => ".facebook.com",
"expirationDate" => strtotime("+6 months"),
"hostOnly" => false,
"httpOnly" => true,
"sameSite" => "None",
"secure" => true,
"session" => false,
"path" => "/"
];
// Process each line and convert cookies to JSON format
$newLines = [];
$i=0;
foreach ($lines as $line) {
$cookies = explode(';', $line);
$jsonCookies = [];
foreach ($cookies as $cookie) {
if (trim($cookie) == '') {
continue;
}
list($name, $value) = explode('=', trim($cookie), 2);
$cookieDetails = $baseCookie;
$cookieDetails['name'] = $name;
$cookieDetails['value'] = $value;
// Add to the JSON cookies array
$jsonCookies[] = $cookieDetails;
}
// Convert the cookies array to JSON string
$newLines[] = json_encode($jsonCookies, JSON_UNESCAPED_SLASHES);
$i++;
}
// Define the new file path
$newFilename = 'conv_' . basename($filename);
// Write all new lines to the new file
file_put_contents($newFilename, implode("\n", $newLines));
echo "Conversion completed. {$i} lines converted. Output written to {$newFilename}\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment