grousso

Google Contacts to LDAP (ldif) converter

Dec 15th, 2019
255
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3. // Google Contacts csv to ldif (LDAP) converter
  4. // Credits pc_magas, George Rousopoulos
  5. // You can transform greek to greeklish csv here https://www.datagen.gr/index.php?option=scripts&script=greek_to_greeklish
  6. // Input CSV
  7. $csv_file="contacts.csv";
  8.  
  9. // Output file
  10. $ldap="contacts.ldif";
  11.  
  12. /**
  13. * ###################### Do not edit ellow this line ##################
  14. */
  15.  
  16. $ldapHandler=fopen($ldap,"w");
  17.  
  18. $csv_file_handler=fopen($csv_file,"r");
  19.  
  20.  
  21. $recordHeader=",ou=Users,dc=mydomain,dc=gr".PHP_EOL."objectclass: inetOrgPerson".PHP_EOL;
  22.  
  23. $firstLineSkip=true;
  24.  
  25. if (($csv_file_handler=fopen($csv_file,"r")) !== FALSE) {
  26.     while (($data = fgetcsv($csv_file_handler, 1000, ",")) !== FALSE) {
  27.        
  28.         if($firstLineSkip) {
  29.          $firstLineSkip=false;
  30.          continue;  
  31.         }
  32.         $uniqid=uniqid();
  33.         $mail=trim($data[30]);
  34.         $cn=trim($data[0]);
  35.         $sn=trim($data[3]);
  36.         $givenName=$data[1];
  37.         $mobile=str_replace(' ','',$data[37]);
  38.         $mobile=str_replace('+30','',$mobile);
  39.         $mobile=str_replace('+','00',$mobile);
  40.         $phone=str_replace(' ','',$data[39]);
  41.         $phone=str_replace('+30','',$phone);
  42.         $phone=str_replace('+','00',$phone);
  43.  
  44.         $record='dn: uid='.$uniqid.''.$recordHeader;
  45.         $record.='cn: '.$cn.PHP_EOL;
  46.         $record.='sn: '.$cn.PHP_EOL;
  47.         $record.='givenName: '.$cn.PHP_EOL;
  48.                
  49.         if (!empty($mail)) {
  50.     $record.='mail: '.$mail.PHP_EOL;
  51. }
  52.         if (!empty($mobile)) {
  53.         $record.='mobile: '.$mobile.PHP_EOL;
  54. }      
  55.         if (!empty($phone)) {
  56.     $record.='telephoneNumber: '.$phone.PHP_EOL;
  57. }
  58.                
  59.         $record.='uid: '.$uniqid.PHP_EOL;
  60.  
  61.         fwrite($ldapHandler,$record.PHP_EOL.PHP_EOL);
  62.     }
  63. }
  64.  
  65. fclose($ldapHandler);
  66. fclose($csv_file_handler);
Comments
  • User was banned
Add Comment
Please, Sign In to add comment