grousso icon

Google Contacts to LDAP (ldif) converter

grousso | PRO | 12/15/19 08:37:11 AM UTC | 0 ⭐ | 228 👁️ | Never ⏰ | []
PHP |

1.79 KB

|

None

|

0 👍

/

0 👎

#!/usr/bin/env php
<?php
// Google Contacts csv to ldif (LDAP) converter
// Credits pc_magas, George Rousopoulos
// You can transform greek to greeklish csv here https://www.datagen.gr/index.php?option=scripts&script=greek_to_greeklish
// Input CSV
$csv_file="contacts.csv";
 
// Output file
$ldap="contacts.ldif";
 
/**
* ###################### Do not edit ellow this line ##################
*/
 
$ldapHandler=fopen($ldap,"w");
 
$csv_file_handler=fopen($csv_file,"r");
 
 
$recordHeader=",ou=Users,dc=mydomain,dc=gr".PHP_EOL."objectclass: inetOrgPerson".PHP_EOL;
 
$firstLineSkip=true;
 
if (($csv_file_handler=fopen($csv_file,"r")) !== FALSE) {
    while (($data = fgetcsv($csv_file_handler, 1000, ",")) !== FALSE) {
        
        if($firstLineSkip) {
         $firstLineSkip=false;
         continue;   
        }
        $uniqid=uniqid();
        $mail=trim($data[30]);
        $cn=trim($data[0]);
        $sn=trim($data[3]);
        $givenName=$data[1];
        $mobile=str_replace(' ','',$data[37]);
        $mobile=str_replace('+30','',$mobile);
        $mobile=str_replace('+','00',$mobile);
        $phone=str_replace(' ','',$data[39]);
        $phone=str_replace('+30','',$phone);
        $phone=str_replace('+','00',$phone);
 
        $record='dn: uid='.$uniqid.''.$recordHeader;
        $record.='cn: '.$cn.PHP_EOL;
        $record.='sn: '.$cn.PHP_EOL;
        $record.='givenName: '.$cn.PHP_EOL;
                
        if (!empty($mail)) {
    $record.='mail: '.$mail.PHP_EOL;
}
        if (!empty($mobile)) {
        $record.='mobile: '.$mobile.PHP_EOL;
}       
        if (!empty($phone)) {
    $record.='telephoneNumber: '.$phone.PHP_EOL;
}
                
        $record.='uid: '.$uniqid.PHP_EOL;
 
        fwrite($ldapHandler,$record.PHP_EOL.PHP_EOL);
    }
}
 
fclose($ldapHandler);
fclose($csv_file_handler);

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎