Import contacts from web mails and other social networks v0.4

Release Notes

This module wraps the OpenInviter PHP library and provides a friendly interface for Elgg installations to add the import capability. I have exposed only a handful of supported plugins in the form since these were known to work at the time of implementation as well as were considered important. You can tweak the code to add more plugin support.

Feel free to report bugs or requests to me.

TODOs:
- Admin setting to customize the services supported on a site
- Way to customize the invitation message while sending invites
- Add translation for strings

For version 0.4:
- Added Twitter by renaming the twitter class to twitter_ex.

For version 0.3:
- Updated to the latest OpenInviter libraries
- Added sub-menu under the Friends using Two page layout (just like invitefriends)

NOTE: OpenInviter Requirements: 
- Your server will need to have PHP5 installed with DOMDocument support and either cURL or WGET.

 

  • thanks@Prashant Juvekar for update

    not urgent, but is it possible to add translation also for..... in next version:

    Choose a Provider/enter/subit/this password will not stored.... and so long :-)

     


  • Thank you! Great plugin.

  • Prashant,

    Thanks! Greaaaaaaaaaat plugin, working fine on 1.6.1 until now.

    regards,

    jose

  • Hi,

    Translated strings for my language.

    Going to share translations lines For version 0.4.

    Follow the steps below.

    Any problem with files, just send a message to me.

    1- Save files:

    1.1 - mod/contact_importer/import.php

    1.2 - mod/contact_importer/languages/en.php

    1.3 - mod/contact_importer/OpenInviter/openinviter.php

     

    2- Copy here and paste it in "mod/contact_importer/import.php"

    <?php

        /**
        * Contact Importer Plugin (using OpenInviter)
        *
        * @package ElggContactImporter
        * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
        * @author Prashant Juvekar
        * @copyright SocialTrak, 2009
        * @link http://www.socialtrak.com
        */

        require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");
       
        global $CONFIG;
           
        gatekeeper();
       
        set_context('friends');
        set_page_owner($_SESSION['guid']);
       
        //set the title
        $body = elgg_view_title(elgg_echo('contact_importer:title'));

        $body .= "<div style='padding:10px;'>";

        // setup some defaults
        $oi_provider = $_POST['oi_provider'];
        if ( $oi_provider == null || empty($oi_provider) ) {
            $oi_provider = "gmail";
        }
        $oi_username = $_POST['oi_username'];
        $oi_password = $_POST['oi_password'];
        $oi_session_id = $_POST['oi_session_id'];

        // include and initialize openinviter   
        global $CONFIG;
        include ( $CONFIG->path . 'mod/contact_importer/OpenInviter/openinviter.php');
        $inviter=new OpenInviter();
        $oi_services=$inviter->getPlugins();

        // lookup the provider name
        foreach ($oi_services as $type=>$providers)    {
            foreach ($providers as $provider=>$details) {
                if ( $oi_provider == $provider ) {
                    $oi_provider_name = $details['name'];
                }
            }
        }

        function get_web_mails () {
            return array (
                'aol' => 'AOL',
                'gmail' => 'Gmail',
                'hotmail' => 'Live/Hotmail',
                'yahoo' => 'Yahoo!',
            );
        }
       
        function get_social_networks () {
            return array (
                'facebook' => 'Facebook',
                'linkedin' => 'LinkedIn',
                'twitter_ex' => 'Twitter',
                'orkut' => 'Orkut',
            );
        }


        function echo_OpenInviter_Inputs_form ($oi_services, $oi_provider, $oi_username) {

    // START TRANSLATION LINES 1
    $provider = elgg_echo('contact_importer:provider');
    $username = elgg_echo('contact_importer:username');
    $password = elgg_echo('contact_importer:password');
    $note = elgg_echo('contact_importer:note');
    $submit = elgg_echo('contact_importer:submit');
    // END TRANSLATION LINES 1

            $body  = "<form action='' method='POST' name='OpenInviter_Inputs'>";
            $body .= "<table>";

            $body .= "<tr>";
            $body .= "<td style='width:160px'><label>$provider</label></td><tr><td></td>";

            $body .= "<td>";
            foreach ( get_web_mails() as $provider => $details )   
            {
                if ( $oi_provider == $provider ) {
                    $body .= "<input type='radio' name='oi_provider' value='{$provider}' checked='checked'>&nbsp;&nbsp;{$details}</input><br />";
                } else {
                    $body .= "<input type='radio' name='oi_provider' value='{$provider}'>&nbsp;&nbsp;{$details}</input><br />";
                }
            }
            $body .= "<br />";
            foreach ( get_social_networks() as $provider => $details )   
            {
                if ( $oi_provider == $provider ) {
                    $body .= "<input type='radio' name='oi_provider' value='{$provider}' checked='checked'>&nbsp;&nbsp;{$details}</input><br />";
                } else {
                    $body .= "<input type='radio' name='oi_provider' value='{$provider}'>&nbsp;&nbsp;{$details}</input><br />";
                }
            }
            $body .= "<br /";
            $body .= "</td>";

            $body .= "</tr>";

            $body .= "<tr>";
            $body .= "<td><label>$username</label></td>";
            $body .= "<td><input type='text' name='oi_username' value='{$oi_username}' / ></td>";
            $body .= "</tr>";

            $body .= "<tr>";
            $body .= "<td><label>$password</label></td>";
            $body .= "<td><input type='password' name='oi_password' / ></td>";
            $body .= "<td style='color:red;font-size:11px;padding-left:4px;vertical-align:middle;'>$note</td>";
            $body .= "</tr>";

            $body .= "</table>";
           
            $body .= "<input type='submit' name='submit' value='$submit' />";
            $body .= "<input type='hidden' name='oi_step' value='Inputs' />";
           
            $body .= "</form>";   
           
            return $body;
        }

        function echo_OpenInviter_Invite_form ($oi_provider, $oi_provider_name, $oi_username, $oi_contacts, $oi_session_id) {

    // START TRANSLATION LINES 2
    $sendinvites = elgg_echo('contact_importer:sendinvites');
    $your = elgg_echo('contact_importer:your');
    $contacts = elgg_echo('contact_importer:contacts');
    $nothave = elgg_echo('contact_importer:nothave');
    $found = elgg_echo('contact_importer:found');
    $selectall = elgg_echo('contact_importer:selectall');
    $unselectall = elgg_echo('contact_importer:unselectall');
    $invite = elgg_echo('contact_importer:invite');
    $name = elgg_echo('contact_importer:name');
    $email = elgg_echo('contact_importer:email');
    // END TRANSLATION LINES 2

            $body .=
                "<script type='text/javascript'>
                    function selectAll(count,which)
                    {
                        for( z=1; z<=count; z++ )
                        {
                            var check_obj = document.getElementById('check_'+z)
                            if ( check_obj ) {
                                check_obj.checked = which;
                            }
                           }
                    }
                </script>";
       
            $body .= "<form action='' method='POST' name='OpenInviter_Invite'>";
            if (count($oi_contacts)==0)
            {
                $body .= "<table class='contactImportTable'>";
                $body .= "<H2>$your \"{$oi_provider_name}\" $contacts</H2>";
                $body .= "<tr class='contactImportTableOddRow'>";
                $body .= "<td align='center' style='padding:20px;' colspan='".($plugType=='email'? "3":"2")."'>";
                $body .= "<label class='OpenInviter_Error'>$nothave</label>";
                $body .= "</td>";
                $body .= "</tr>";
                $body .= "</table>";
            }
            else
            {
                $body .= "<input type='submit' name='send' value='$sendinvites' class='thButton'>" . "&nbsp;&nbsp";
                $body .= "<H2>$your \"{$oi_provider_name}\" $contacts ($found " . count($oi_contacts) . ")</H2>";
                $body .= "<a href='javascript:none(0)' onclick='selectAll(".count($oi_contacts).",true)'>$selectall</a>" . "&nbsp;&nbsp";
                $body .= "<a href='javascript:none(0)' onclick='selectAll(".count($oi_contacts).",false)'>$unselectall</a>";
                $body .= "<table class='contactImportTable'>";
                $body .= "<tr class='contactImportTableDesc'>";
                //$body .= "<td style='font-weight:bold'><input type='checkbox' onChange='toggleAll(this)' name='toggle_all' title='Select/Deselect all' checked>$invite</td>";
                $body .= "<td style='font-weight:bold'>$invite &nbsp;</td>";
                $body .= "<td style='font-weight:bold'>$name</td>".($plugType == 'email' ?"<td style='font-weight:bold'>$email</td>":"")."</tr>";
                $odd=true;$counter=0;
                foreach ($oi_contacts as $email=>$name)
                {
                    $counter++;
                    if ($odd) {
                        $class='contactImportTableOddRow';
                    } else {
                        $class='contactImportTableEvenRow';
                    }
                    $body .= "<tr class='{$class}'>";
                    $body .= "<td><input name='check_{$counter}' id='check_{$counter}' value='{$counter}' type='checkbox' class='thCheckbox' checked>";
                    $body .= "<input type='hidden' name='email_{$counter}' value='{$email}'>";
                    $body .= "<input type='hidden' name='name_{$counter}' value='{$name}'>";
                    $body .= "</td><td>{$name}</td>".($plugType == 'email' ?"<td>{$email}</td>":"")."</tr>";
                    $odd=!$odd;
                }
                $body .= "</table>";
                $body .= "<a href='javascript:none(0)' onclick='selectAll(".count($oi_contacts).",true)'>$selectall</a>" . "&nbsp;&nbsp";
                $body .= "<a href='javascript:none(0)' onclick='selectAll(".count($oi_contacts).",false)'>$unselectall</a><br />";
                $body .= "<input type='submit' name='send' value='$sendinvites' class='thButton'>" . "&nbsp;&nbsp";

                $body .= "<input type='hidden' name='oi_step' value='Invite' />";
                $body .= "<input type='hidden' name='oi_provider' value='{$oi_provider}'>";
                $body .= "<input type='hidden' name='oi_username' value='{$oi_username}'>";
                $body .= "<input type='hidden' name='oi_session_id' value='{$oi_session_id}'>";
            }
            $body .= "</form>";
           
            return $body;
        }
       
        function ers($ers)
        {
            if (!empty($ers))
            {
                $contents="<table cellspacing='0' cellpadding='0' style='border:1px solid red;' align='center' class='tbErrorMsgGrad'><tr><td valign='middle' style='padding:3px' valign='middle' class='tbErrorMsg'><img src='/images/ers.gif'></td><td valign='middle' style='color:red;padding:5px;'>";
                foreach ($ers as $key=>$error)
                    $contents.="{$error}<br >";
                $contents.="</td></tr></table><br >";
                return $contents;
            }
        }
       
    // START TRANSLATION LINES 3
    $loginfailed = elgg_echo('contact_importer:loginfailed');
    $unableget = elgg_echo('contact_importer:unableget');
    $notselect = elgg_echo('contact_importer:notselect');
    $sentfollowing = elgg_echo('contact_importer:sentfollowing');
    $errorssending = elgg_echo('contact_importer:errorssending');
    // END TRANSLATION LINES 3

        function oks($oks)
        {
            if (!empty($oks))
            {
                $contents="<table border='0' cellspacing='0' cellpadding='10' style='border:1px solid #5897FE;' align='center' class='tbInfoMsgGrad'><tr><td valign='middle' valign='middle' class='tbInfoMsg'><img src='/images/oks.gif' ></td><td valign='middle' style='color:#5897FE;padding:5px;'>    ";
                foreach ($oks as $key=>$msg)
                    $contents.="{$msg}<br >";
                $contents.="</td></tr></table><br >";
                return $contents;
            }
        }

        if ( !isset($_POST['oi_step']) ) {
       
            $body .= echo_OpenInviter_Inputs_form($oi_services, $oi_provider, $oi_username);
           
        } else if ( $_POST['oi_step'] == "Inputs" ) {

            $ers=array();$oks=array();$import_ok=false;$done=false;
            $inviter->startPlugin($oi_provider);
            $internal=$inviter->getInternalError();
            if ($internal)
            {
                $body .= echo_OpenInviter_Inputs_form($oi_services, $oi_provider, $oi_username);
                $body .= "<label class='OpenInviter_Error'>" . $internal . "</label>";
            }
            elseif (!$inviter->login($oi_username,$oi_password))
            {
                $body .= echo_OpenInviter_Inputs_form($oi_services, $oi_provider, $oi_username);
                $internal=$inviter->getInternalError();
                if ( $internal )
                    $body .= "<label class='OpenInviter_Error'>" . $internal . "</label>";
                else
                    $body .= "<label class='OpenInviter_Error'>" . "$loginfailed" . "</label>";
            }
            elseif (false===$contacts=$inviter->getMyContacts())
            {
                $body .= echo_OpenInviter_Inputs_form($oi_services, $oi_provider, $oi_username);
                $body .= "<label class='OpenInviter_Error'>" . "$unableget" . "</label>";
            }
            else
            {
                // success -> show the contacts
                $body .= echo_OpenInviter_Invite_form ($oi_provider, $oi_provider_name, $oi_username, $contacts, $inviter->plugin->getSessionID());
            }

        } else if ( $_POST['oi_step'] == "Invite" ) {
            $inviter->startPlugin($oi_provider);
            $internal=$inviter->getInternalError();
            if ($internal)
            {
                $body .= echo_OpenInviter_Inputs_form($oi_services, $oi_provider, $oi_username);
                $body .= "<label class='OpenInviter_Error'>" . $internal . "</label>";
            }
            else
            {
                $selected_contacts=array(); $invitee_list="";
                foreach ($_POST as $key=>$val) {
                    if (strpos($key,'check_')!==false) {
                        $selected_contacts[$_POST['email_'.$val]]=$_POST['name_'.$val];
                        $invitee_list .= "&nbsp;&nbsp;&nbsp;&nbsp;- " . $_POST['name_'.$val] . "<br />";
                    } elseif (strpos($key,'email_')!==false) {
                        $temp=explode('_',$key);$counter=$temp[1];
                        if (is_numeric($temp[1])) $contacts[$val]=$_POST['name_'.$temp[1]];
                    }
                }
                if (count($selected_contacts)==0) {
                    $body .= "<label class='OpenInviter_Error'>" . "$notselect" . "</label>";
                } else {

                    // Compose the message
                    $message['subject'] =
                        sprintf(
                            elgg_echo ('contact_importer:email:subject'),
                            $CONFIG->site->name
                        );
                    $message['body'] =
                        sprintf(
                            elgg_echo ('contact_importer:email:mailbody'),
                            $CONFIG->site->name,
                            $_SESSION['user']->name,
                            $CONFIG->wwwroot
                        );

                    $sendMessage=$inviter->sendMessage($oi_session_id,$message,$selected_contacts);
                    $inviter->logout();
                   
                    if ($sendMessage===-1)
                    {
                        $message_subject=$_SESSION['user']->name.": ".$message['subject'];
                        $message_body=$message['body'];
                        $headers="From: {$_SESSION['user']->email}";
                        foreach ($selected_contacts as $email=>$name) {
                            mail($email,$message_subject,$message_body,$headers);
                        }
                        $body .= "<label class='OpenInviter_Info'>$sentfollowing<br />" . $invitee_list . "</label>";
                    }
                    elseif ($sendMessage===false)
                    {
                        $internal=$inviter->getInternalError();
                        if ( $internal ) {
                            $body .= "<label class='OpenInviter_Error'>" . $internal . "</label>";
                        } else {
                            $body .= "<label class='OpenInviter_Error'>$errorssending</label>";
                        }
                    }
                    else
                    {
                        $body .= "<label class='OpenInviter_Info'>$sentfollowing<br />" . $invitee_list . "</label>";
                    }
                }
            }       
        }

        $body .= "</div>";

        $body = elgg_view_layout('two_column_left_sidebar','',$body);
       
        // Finally draw the page
        page_draw(elgg_echo('contact_importer:title'), $body);
    ?>

     

    3 - Copy here and paste it in "languages/en.php"

    <?php

        /**
        * Contact Importer Plugin (using OpenInviter)
        *
        * @package ElggContactImporter
        * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
        * @author Prashant Juvekar
        * @copyright SocialTrak, 2009
        * @link http://www.socialtrak.com
        */

        $english = array(

            'contact_importer:plugin:name' => 'Import Contacts',
            'contact_importer:title' => 'Import Contacts',

            'contact_importer:email:subject' => 'Invitation to join %s',
            'contact_importer:email:mailbody' => '
    You have been invited to join %s by %s.
    To join, click the following link:

        %s',

        // TRANSLATION LINES 1 mod/contact_importer/import.php
            'contact_importer:provider' => 'Choose a Provider',
            'contact_importer:username' => 'Enter Username',
            'contact_importer:password' => 'Enter Password',
            'contact_importer:submit' => 'Submit',
            'contact_importer:note' => 'This password will not be stored by us.',
           
        // TRANSLATION LINES 2 mod/contact_importer/import.php
            'contact_importer:sendinvites' => 'Send Invites',
            'contact_importer:your' => 'Your',
            'contact_importer:contacts' => 'contacts',
            'contact_importer:nothave' => 'You do not have any contacts in your address book.',
            'contact_importer:found' => 'Found',
            'contact_importer:selectall' => 'Select All',
            'contact_importer:unselectall' => 'Unselect All',
            'contact_importer:invite' => 'Invite?',
            'contact_importer:name' => 'Name',
            'contact_importer:email' => 'E-mail',
           
        // TRANSLATION LINES 3 mod/contact_importer/import.php
            'contact_importer:loginfailed' => 'Login failed. Please check the email and password you have provided and try again.',
            'contact_importer:unableget' => 'Unable to get contacts.',
            'contact_importer:notselect' => 'You did not select any contacts to invite.',
            'contact_importer:sentfollowing' => 'Invitation emails were sent successfully to the following:',
            'contact_importer:errorssending' => 'There were errors while sending your invites.<br>Please try again later!',
           
         // START TRANSLATION LINES mod/contact_importer/OpenInviter/openinviter.php
            'contact_importer:internalerror1' => 'Please enter the full email, not just the username.',
            'contact_importer:internalerror2' => 'Please enter just the username, not the full email.',
            'contact_importer:internalerror3' => 'is not a valid domain for this provider.',
           
        );
                       
        add_translation("en",$english);
    ?>

     

    4- Open file "mod/contact_importer/OpenInviter/openinviter.php"

    4.1 - Find and Replace (near line 418)

        private function checkLoginCredentials($user)
            {
            $is_email=$this->plugin->isEmail($user);

    With

        private function checkLoginCredentials($user)
            {
           
    // START TRANSLATION LINES OPENINVITER
    $internalerror1 = elgg_echo('contact_importer:internalerror1');
    $internalerror2 = elgg_echo('contact_importer:internalerror2');
    $internalerror3 = elgg_echo('contact_importer:internalerror3');
    // END TRANSLATION LINES OPENINVITER

            $is_email=$this->plugin->isEmail($user);

    4.2 - Find and Replace (near line 425)

                    $this->internalError="Please enter the full email, not just the username";

    With

                    $this->internalError="$internalerror1";

    4.2 - Find and Replace (near line 430)

                    $this->internalError="Please enter just the username, not the full email";

    With

                    $this->internalError="$internalerror2";

    4.2 - Find and Replace (near line 441)

                    $this->internalError="<b>{$user_domain}</b> is not a valid domain for this provider";

    With

                    $this->internalError="<b>{$user_domain}</b> $internalerror3";

     

    Hope it helps users for translating to their language.

    Regards,

    Jose

     

     

  • Hello Prashant thanks a lot for the nice work.

    I am based in Germany and for me is more important to let users import contacts from services such as GMX.DE, WEB.DE, and XING.COM - rather than AOL, ORKUT and such...

    Is there any documentation about  how to enable these services, or can you let me know how to do it?

    Thanks for the advise

  • This is GREAT. It works on elgg 1.6.1

    An idea, is there a way of stating to the user to add the person as a friend, or creating a special link that sets up a friendship of the person who has joined with the person who has invited them?

    Anyway, great plugin and does what it says.
    (I tested this only with Gmail)

Stats

  • Category: Uncategorized
  • License: GNU General Public License (GPL) version 2
  • Updated: 2014-11-17
  • Downloads: 5685
  • Recommendations: 3

Other Projects

View Prashant Juvekar's plugins