Feng Forum

Other Topics => Development => Community Contributions => : Murz May 25, 2010, 11:03:25 AM

: Export all contacts to third party software via VCARD automatically
: Murz May 25, 2010, 11:03:25 AM
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:
:
    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?
: Re: Export all contacts to third party software via VCARD automatically
: cabeza May 26, 2010, 01:29:51 PM
Untested solution:

I would write a PHP script that starts like this

:
<?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
: Re: Export all contacts to third party software via VCARD automatically
: Murz May 27, 2010, 10:28:10 AM
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:
:
   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:
:
<?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.
: Re: Export all contacts to third party software via VCARD automatically
: Murz May 27, 2010, 10:34:00 AM
I can't attach files, got the error: The attachments upload directory is not writable. Your attachment or avatar cannot be saved.
: Re: Export all contacts to third party software via VCARD automatically
: Murz May 27, 2010, 10:48:44 AM
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!
: Re: Export all contacts to third party software via VCARD automatically
: cabeza May 27, 2010, 11:49:43 AM
Thanks for sharing Murz .

Should be able to upload files now.
: Re: Export all contacts to third party software via VCARD automatically
: cabeza July 09, 2010, 12:47:18 PM
Hi Murz,

we are thinking of including this in the trunk. Is this the latest version?
: Re: Export all contacts to third party software via VCARD automatically
: Murz 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.
: Re: Export all contacts to third party software via VCARD automatically
: Murz 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.
: Re: Export all contacts to third party software via VCARD automatically
: franponce87 September 03, 2010, 05:05:35 PM
Thanks once more Murz, we are going to check it out!