Author Topic: Export all contacts to third party software via VCARD automatically  (Read 7185 times)

Murz

  • Full Member
  • ***
  • Posts: 147
    • View Profile
Our company use FengOffice as database of contacts and organisations.
But for email we didn't use integrated modules, use mail applications (thunderbird, kmail, outlook, etc) and some other software.

And at now we need to duplicate current client database in each application that is not so good. And manually create records in mobile phones too.

Many software can work with vcard file as contacts database (for example, Akonadi), but at now I must manually select which contacts must be exported and update file manually. And I can't select all contacts in fengoffice, but only one page.

I have added the copy of function export_to_vcard() to file application/controllers/ContactController.class.php with some modifications:
Code: [Select]
    function export_to_vcard_all() {
        $contacts = array();
                $ids = explode(",", $ids);
                $allowed = Contacts::instance()->getAllowedContacts();
                foreach ($allowed as $c) {
                        $contacts[] = $c;
                }
        if (count($contacts) == 0) {
                        flash_error(lang("you must select the contacts from the grid"));
                        ajx_current("empty");
                        return;
                }

        $data = self::build_vcard($contacts);
        $name = (count($contacts) == 1 ? $contacts[0]->getDisplayName() : "contacts") . ".vcf";

        download_contents($data, 'text/x-vcard', $name, strlen($data), true);
        die();
    }

After this I can export all available to user contacts via url http://opengoo.domain.ru/index.php?c=contact&a=export_to_vcard_all

But I can't do this via cron or another automated process, because I need to export it via authenticated in FengOffice user.

Can anybody tell how I can authenticate in FengOffice via some params in url or via curl() functiona and cookie/session files?

cabeza

  • Administrator
  • Hero Member
  • *****
  • Posts: 1004
    • View Profile
    • Feng Office
Untested solution:

I would write a PHP script that starts like this

Code: [Select]
<?php
// This must run in Feng Office root
chdir(dirname(__FILE__));
define("CONSOLE_MODE"true);
define('PUBLIC_FOLDER''public');
include 
"init.php";

and then calls login
and then calls export_to_vcard_all

If you make it, let us know how ...

Edit:
PS: we'll include the function in the core

Murz

  • Full Member
  • ***
  • Posts: 147
    • View Profile
cabeza, thank's for info! I have written the working script, file is attached to message and must be placed in fengoffice root.
And file application/controllers/ContactController.class.php must be modified, after string 2240 you must add:
Code: [Select]
   function export_to_vcard_all() {
      $contacts_all = Contacts::instance()->getAllowedContacts();
      $user=logged_user();
      
//       var_export($user); die;
      if (count($contacts_all) == 0) {
        flash_error(lang("you must select the contacts from the grid"));
        ajx_current("empty");
        return;
      }

      $data = self::build_vcard($contacts_all);
      $name = "contacts_all_".$user->getUsername().".vcf";

      download_contents($data, 'text/x-vcard', $name, strlen($data), true);
      die();
    }

And contents of file export_contacts.php:
Code: [Select]
<?php
chdir
(dirname(__FILE__));
define("CONSOLE_MODE"true);
define('PUBLIC_FOLDER''public');
include 
"init.php";

include 
APPLICATION_PATH "/helpers/permissions.php";

session_commit(); // we don't need sessions
@set_time_limit(0); // don't limit execution of cron, if possible

if(!empty($_GET['token'])) {
  
$user Users::getByToken(array_var($_GET'token'));

  if(!empty(
$user)) {
    
CompanyWebsite::instance()->setLoggedUser($userfalsefalsefalse);
    
ContactController::export_to_vcard_all();
  } else 
"Can't find the user matched token ".array_var($_GET'token'); 

} elseif(empty(
$_REQUEST['username'])) {
    
?>

      <form name="gettoken" method="post">
        <input name="username">
        <input name="password" type="password">
        <input type="submit">
      </form>
    <?

} else {
  $user = Users::getByUsername(array_var($_REQUEST, 'username'));
  
  if(empty($user)) { echo "Can't find user ".array_var($_REQUEST, 'username'); die; }
  
  if($user->isValidPassword(array_var($_REQUEST, 'password'))) {
    echo "Token for user <b>".$user->getUsername()."</b> is: ".$user->getToken();
    echo "<br><a href='?token=".$user->getToken()."'>Url for export all contacts to vcard</a>";
  } else echo "Bad password for user ".array_var($_REQUEST, 'username');
}


After modification you can open http://domain.com/fengoffice/export_contacts.php, type in form username and password, and give the link to file with contacts.
« Last Edit: May 28, 2010, 02:32:59 am by Murz »

Murz

  • Full Member
  • ***
  • Posts: 147
    • View Profile
I can't attach files, got the error: The attachments upload directory is not writable. Your attachment or avatar cannot be saved.

Murz

  • Full Member
  • ***
  • Posts: 147
    • View Profile
If you use KDE with Akonadi you can simply add new resource (Vcard file) type the url like http://domain.com/fengoffice/export_contacts.php?token=b93af3fbd993a2f3df6e6a95fca135154b3bab00  and that's all, you got all contacts in KMail and other apps!

cabeza

  • Administrator
  • Hero Member
  • *****
  • Posts: 1004
    • View Profile
    • Feng Office
Thanks for sharing Murz .

Should be able to upload files now.

cabeza

  • Administrator
  • Hero Member
  • *****
  • Posts: 1004
    • View Profile
    • Feng Office
Re: Export all contacts to third party software via VCARD automatically
« Reply #6 on: July 09, 2010, 12:47:18 pm »
Hi Murz,

we are thinking of including this in the trunk. Is this the latest version?

Murz

  • Full Member
  • ***
  • Posts: 147
    • View Profile
Re: Export all contacts to third party software via VCARD automatically
« Reply #7 on: July 09, 2010, 05:20:30 pm »
At now it is last version, it works perfectly for our company needs, if I update something, I'll post updated version here.

Murz

  • Full Member
  • ***
  • Posts: 147
    • View Profile
Re: Export all contacts to third party software via VCARD automatically
« Reply #8 on: August 11, 2010, 06:28:49 am »
I have update the functionality of my script to version 2.

Now you can customize export scope via 'scope' GET parameter:
- all available contacts (default)
- only contacts, created by you: scope=created
- only contacts, to that you subscribed: scope=subscribed
- created by you and subscribed: scope=created_subscribed

URL example for 'scope=created_subscribed' is: http://domain.com/fengoffice/export_contacts.php?token=b93af3fbd993a2f3df6e6a95fca135154b3bab00&scope=created_subscribed

Patch for 1.7 version and new version of file is attached. You must rename export_contacts_v2.php to export_contacts.php because forum didn't allow to attach file with same name.

franponce87

  • Administrator
  • Hero Member
  • *****
  • Posts: 1819
    • View Profile
    • Email
Re: Export all contacts to third party software via VCARD automatically
« Reply #9 on: September 03, 2010, 05:05:35 pm »
Thanks once more Murz, we are going to check it out!
Would you like to install Feng Office Professional or Enterprise Edition in your servers? No problem! Read this article!

 

anything