Skip to content

Instantly share code, notes, and snippets.

@mikestreety
Last active June 16, 2020 21:08
Show Gist options
  • Save mikestreety/c643afcb313d4b4ae3b46dcfeaf6175f to your computer and use it in GitHub Desktop.
Save mikestreety/c643afcb313d4b4ae3b46dcfeaf6175f to your computer and use it in GitHub Desktop.
<?php
// The name of the folder where the logins are
$folder = './passwords';
// The file name you want to save as
$title = 'Logins';
// A csv function - from http://webtricksandtreats.com/export-to-csv-php/
function convert_to_csv($input_array, $output_file_name, $delimiter) {
$temp_memory = fopen('php://memory', 'w');
foreach ($input_array as $line)
fputcsv($temp_memory, $line, $delimiter);
fseek($temp_memory, 0);
file_put_contents($output_file_name, $temp_memory);
}
// Set up a data array with header rows with the correct fields
$data = array();
$data[] = array(
'title' => 'title',
'location' => 'location',
'username' => 'username',
'password' => 'password',
'category' => 'category',
'notes' => 'notes'
);
// Make sure the folder definitely ends in a slash
$folder = rtrim($folder, '/') . '/';
// Find all the .item files in the specified folder, extract the
// required data and add it to the data array
foreach (glob($folder . '*.item') as $category) {
$category = json_decode(file_get_contents($category));
foreach (glob($folder . '/' . $category->uuid . '/*.item') as $login) {
$login = json_decode(file_get_contents($login));
$data[] = array(
'title' => $login->title ?? '',
'location' => $login->data->fields->url ?? '',
'username' => $login->data->fields->username ?? '',
'password' => $login->data->fields->password ?? '',
'category' => $category->title ?? '',
'notes' => $login->data->fields->note ?? ''
);
}
}
echo 'Nice work - thats ' . count($data) . ' logins ready for anything!' . PHP_EOL;
// Convert array to CSV and output
convert_to_csv($data, $title . '.csv', ',');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment