Skip to content

Instantly share code, notes, and snippets.

@spyesx
Created March 11, 2018 09:26
Show Gist options
  • Select an option

  • Save spyesx/707b5976e420d890cf4df8032ea7545b to your computer and use it in GitHub Desktop.

Select an option

Save spyesx/707b5976e420d890cf4df8032ea7545b to your computer and use it in GitHub Desktop.
Keepass import from CSV
<?php
$header = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<KeePassFile>
<Meta>
<Generator>KdbxWeb</Generator>
<HeaderHash>yW0Fz7nokpC3duCsku9B64+EAZZ7gvsmG0ZIIyZ8nKk=</HeaderHash>
<DatabaseName>jlb_import</DatabaseName>
<DatabaseNameChanged>2018-02-12T13:14:18Z</DatabaseNameChanged>
<DatabaseDescription/>
<DatabaseDescriptionChanged>2018-02-12T13:13:46Z</DatabaseDescriptionChanged>
<DefaultUserName/>
<DefaultUserNameChanged>2018-02-12T13:13:46Z</DefaultUserNameChanged>
<MaintenanceHistoryDays>365</MaintenanceHistoryDays>
<Color/>
<MasterKeyChanged>2018-02-12T13:14:05Z</MasterKeyChanged>
<MasterKeyChangeRec>-1</MasterKeyChangeRec>
<MasterKeyChangeForce>-1</MasterKeyChangeForce>
<RecycleBinEnabled>True</RecycleBinEnabled>
<RecycleBinUUID>15PzA1Uq39ZzwMqGscSOnA==</RecycleBinUUID>
<RecycleBinChanged>2018-02-12T13:13:46Z</RecycleBinChanged>
<EntryTemplatesGroup>AAAAAAAAAAAAAAAAAAAAAA==</EntryTemplatesGroup>
<EntryTemplatesGroupChanged>2018-02-12T13:13:46Z</EntryTemplatesGroupChanged>
<HistoryMaxItems>10</HistoryMaxItems>
<HistoryMaxSize>6291456</HistoryMaxSize>
<LastSelectedGroup>Uf27ig+STcGTl6hS6OmVvw==</LastSelectedGroup>
<LastTopVisibleGroup>QEBjAEBgAACAfttx/38AAA==</LastTopVisibleGroup>
<MemoryProtection>
<ProtectTitle>False</ProtectTitle>
<ProtectUserName>False</ProtectUserName>
<ProtectPassword>True</ProtectPassword>
<ProtectURL>False</ProtectURL>
<ProtectNotes>False</ProtectNotes>
</MemoryProtection>
<CustomIcons/>
<Binaries/>
<CustomData/>
</Meta>
<Root>
<Group>
<UUID>xaK/fXhFD+cHzmGLzP9uaQ==</UUID>
<Name>Import folder</Name>
<Notes/>
<IconID>49</IconID>
<Times>
<CreationTime>2018-02-12T13:13:46Z</CreationTime>
<LastModificationTime>2018-02-12T13:14:18Z</LastModificationTime>
<LastAccessTime>2018-02-12T13:14:18Z</LastAccessTime>
<ExpiryTime>2018-02-12T13:13:46Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2018-02-12T13:13:46Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence/>
<EnableAutoType>null</EnableAutoType>
<EnableSearching>null</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
<Group>
<UUID>15PzA1Uq39ZzwMqGscSOnA==</UUID>
<Name>Recycle Bin</Name>
<Notes/>
<IconID>43</IconID>
<Times>
<CreationTime>2018-02-12T13:13:46Z</CreationTime>
<LastModificationTime>2018-02-12T13:13:46Z</LastModificationTime>
<LastAccessTime>2018-02-12T13:13:46Z</LastAccessTime>
<ExpiryTime>2018-02-12T13:13:46Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2018-02-12T13:14:46Z</LocationChanged>
</Times>
<IsExpanded>True</IsExpanded>
<DefaultAutoTypeSequence/>
<EnableAutoType>False</EnableAutoType>
<EnableSearching>False</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
</Group>
<Group>
<UUID>Uf27ig+STcGTl6hS6OmVvw==</UUID>
<Name>Imports from CSV</Name>
<Notes/>
<IconID>48</IconID>
<Times>
<CreationTime>2018-02-12T13:14:58Z</CreationTime>
<LastModificationTime>2018-02-12T13:15:12Z</LastModificationTime>
<LastAccessTime>2018-02-12T13:14:58Z</LastAccessTime>
<ExpiryTime>4001-01-01T00:00:00Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2018-02-12T13:14:58Z</LocationChanged>
</Times>
<IsExpanded>False</IsExpanded>
<DefaultAutoTypeSequence/>
<EnableAutoType>null</EnableAutoType>
<EnableSearching>null</EnableSearching>
<LastTopVisibleEntry>AAAAAAAAAAAAAAAAAAAAAA==</LastTopVisibleEntry>
';
$footer = '
</Group>
</Group>
<DeletedObjects/>
</Root>
</KeePassFile>
';
function offsetIsSet($array, $offset)
{
return isset($array[$offset]) && !empty($array[$offset]) ? $array[$offset] : '';
}
function generate_entry($row)
{
return '
<Entry>
<UUID>'.base64_encode(uniqid()).'</UUID>
<IconID>0</IconID>
<ForegroundColor/>
<BackgroundColor/>
<OverrideURL/>
<Tags/>
<Times>
<CreationTime>2018-02-12T13:15:25Z</CreationTime>
<LastModificationTime>2018-02-12T13:15:34Z</LastModificationTime>
<LastAccessTime>2018-02-12T13:15:25Z</LastAccessTime>
<ExpiryTime>4001-01-01T00:00:00Z</ExpiryTime>
<Expires>False</Expires>
<UsageCount>0</UsageCount>
<LocationChanged>2018-02-12T13:15:25Z</LocationChanged>
</Times>
<String>
<Key>Title</Key>
<Value><![CDATA['.offsetIsSet($row, 0).' '.offsetIsSet($row, 1).' '.offsetIsSet($row, 2).' '.offsetIsSet($row, 3).' '.offsetIsSet($row, 4).' '.offsetIsSet($row, 5).' '.offsetIsSet($row, 6).']]></Value>
</String>
<String>
<Key>UserName</Key>
<Value/>
</String>
<String>
<Key>Password</Key>
<Value/>
</String>
<String>
<Key>URL</Key>
<Value/>
</String>
<String>
<Key>Notes</Key>
<Value><![CDATA['.offsetIsSet($row, 77).']]></Value>
</String>
<AutoType>
<Enabled>True</Enabled>
<DataTransferObfuscation>0</DataTransferObfuscation>
</AutoType>
<History>
</History>
</Entry>
';
}
$source = new SplFileObject("source.csv");
$source->setFlags(SplFileObject::READ_CSV);
$render = $header;
foreach ($source as $row)
{
if( isset($row[77]) )
{
$render .= generate_entry($row);
}
}
$render .= $footer;
// echo $render;
// $xml = simplexml_load_string($render, "SimpleXMLElement", LIBXML_NOCDATA);
$fo = fopen('./output.xml', 'w') or die('Cannot open file: output.xml');
fwrite($fo, $render);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment