'. t('FOAF, or Friend of a Friend, refers to a set of social networking standards and tools for online sharing and searching of user profile information. Users will find this useful if they want to make a list of their friends and be able to export or import their list among social networking applications.') .'
';
$output .= '
'. t('The FOAF module allows users to export FOAF documents based on their profile information. It also allows users to import profile information from external FOAF files, and even includes an option to let profiles auto sync when using distributed authentication. The FOAF module can export your buddy list if the buddy list module is enabled. You can download FOAF files in the view tab of a user profile if FOAF is enabled.') .'
'. t('For more information please read the configuration and customization handbook FOAF page.', array('%foaf' => 'http://www.drupal.org/handbook/modules/foaf/')) .'
';
return $output;
case 'admin/modules#description':
return t('Allows FOAF import / export');
}
}
/**
* Implementation of hook_menu
*/
function foaf_menu($may_cache) {
global $user;
$items = array();
if ($may_cache) {
$items[] = array('path' => 'foaf', 'title' => t('foaf'),
'access' => user_access('access content'),
'callback' => 'foaf_export',
'type' => MENU_CALLBACK);
}
return $items;
}
/**
* Implements hook_user
*/
function foaf_user($op, &$edit, &$user, $category = NULL) {
switch ($op) {
case 'login':
return foaf_login($user);
case 'validate':
if ($edit['foaf_url']) {
$user->foaf_url = $edit['foaf_url'];
return foaf_import($user);
}
break;
case 'view':
drupal_set_html_head("\n".'');
$output = '';
if (_foaf_allow_export($user)) {
//return array(t('Personal Information') => l(t('Download FOAF'), 'foaf/'.$user->uid, NULL, NULL, NULL, FALSE, TRUE));
return array(t('Personal Information') => array(0 => array(/*'title' => 'FOAF',*/ 'value' => l(t('Download FOAF'), 'foaf/'.$user->uid, NULL, NULL, NULL, FALSE, TRUE))));
}
break;
case 'form':
if ($category == 'account') {
return foaf_user_form($user);
}
break;
}
}
function foaf_settings() {
$form['foaf_export_default'] = array(
'#type' => 'radios',
'#title' => t('Export FOAF for users by default'),
'#default_value' => variable_get('foaf_export_default', 0),
'#options' => array(0 => t('no'), 1=> t('yes')),
'#description' => t('This can be overridden by individual users'),
);
$mapping = variable_get('mapping', NULL);
$form['mapping'] = array(
'#type' => 'fieldset',
'#title' => t('Field Mappings'),
);
$form['mapping']['foaf_name'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:name'),
'#default_value' => variable_get('foaf_name', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_title'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:title'),
'#default_value' => variable_get('foaf_title', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_firstName'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:firstName'),
'#default_value' => variable_get('foaf_firstName', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_surname'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:surname'),
'#default_value' => variable_get('foaf_surname', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_Organization'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:Organization'),
'#default_value' => variable_get('foaf_Organization', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_phone'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:phone'),
'#default_value' => variable_get('foaf_phone', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_aimChatID'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:aimChatID'),
'#default_value' => variable_get('foaf_aimChatID', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_icqChatID'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:icqChatID'),
'#default_value' => variable_get('foaf_icqChatID', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_msnChatID'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:msnChatID'),
'#default_value' => variable_get('foaf_msnChatID', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_yahooChatID'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:yahooChatID'),
'#default_value' => variable_get('foaf_yahooChatID', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_jabberID'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:jabberID'),
'#default_value' => variable_get('foaf_jabberID', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_workplaceHomepage'] = array(
'#type' => 'select',
'#title' => t('Field for foaf:workplaceHomepage'),
'#default_value' => variable_get('foaf_workplaceHomepage', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_street'] = array(
'#type' => 'select',
'#title' => t('Field for vCard:Street'),
'#default_value' => variable_get('foaf_street', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_locality'] = array(
'#type' => 'select',
'#title' => t('Field for vCard:Locality'),
'#default_value' => variable_get('foaf_locality', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_region'] = array(
'#type' => 'select',
'#title' => t('Field for vCard:Region'),
'#default_value' => variable_get('foaf_region', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_pcode'] = array(
'#type' => 'select',
'#title' => t('Field for vCard:Pcode'),
'#default_value' => variable_get('foaf_pcode', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_country'] = array(
'#type' => 'select',
'#title' => t('Field for vCard:Country'),
'#default_value' => variable_get('foaf_country', ''),
'#options' => _foaf_profile_fields(),
);
$form['mapping']['foaf_keywords'] = array(
'#type' => 'select',
'#title' => t('Field for bio:keywords'),
'#default_value' => variable_get('foaf_keywords', ''),
'#options' => _foaf_profile_fields(),
);
return $form;
}
function foaf_xmlrpc() {
return array(
array('foaf.getUrl',
'foaf_get_url',
array('string', 'string'),
t('returns the FOAF url for a user')));
}
function foaf_export($uid = null) {
if (!$uid || !$account = user_load(array('uid' => $uid))) {
drupal_not_found();
return;
}
if ( !_foaf_allow_export($account)) {
drupal_access_denied();
return;
}
$output .= ''."\n";
$output .= ''."\n";
$output .= ''."\n";
$output .= ' '."\n";
$output .= ' '. t('FOAF for %name at %site', array('%name' => $account->name, '%site' => variable_get('site_name', 'drupal'))) ."\n";
$output .= ' ' . t('Friend of a Friend description for %name', array('%name' => $account->name)) . "\n";
$output .= ' '."\n";
$output .= "\n";
//$name = ($val = _foaf_profile_get($uid, variable_get('foaf_name',''))) ? $val : $account->name;
$picture = ($account->picture) ? file_create_url($account->picture) : '';
// foaf:Person
$output.= ''."\n";
if ($firstName = _foaf_profile_get($uid, variable_get('foaf_firstName',''))) {
$output.= ' '.$firstName."\n";
}
if ($surname = _foaf_profile_get($uid, variable_get('foaf_surname',''))) {
$output.= ' '.$surname."\n";
}
// Guess foaf:name (if not present)
if (!$name = _foaf_profile_get($uid, variable_get('foaf_name',''))) {
if ($firstName && $surname) {
$name = $firstName . ' ' . $surname;
}
else {
$name = $account->name;
}
}
$output.= ' '.$name."\n";
if ($account->mail) {
$output.= ' '.sha1('mailto:' . $account->mail)."\n";
}
if ($picture) {
$output.= ' ' . "\n";
$output.= ' ' . "\n";
}
$output.= ' '.$account->name ."\n";
// weblog
if (module_exist('blog')) {
$output.= ' ' . "\n";
}
if ($title = _foaf_profile_get($uid, variable_get('foaf_title',''))) {
$output.= ' '.$title."\n";
}
if ($Organization = _foaf_profile_get($uid, variable_get('foaf_Organization',''))) {
$output.= ' '.$Organization."\n";
}
if ($phone = _foaf_profile_get($uid, variable_get('foaf_phone',''))) {
$output.= ' '."\n";
}
if ($aimChatID = _foaf_profile_get($uid, variable_get('foaf_aimChatID',''))) {
$output.= ' '.$aimChatID."\n";
}
if ($icqChatID = _foaf_profile_get($uid, variable_get('foaf_icqChatID',''))) {
$output.= ' '.$icqChatID."\n";
}
if ($msnChatID = _foaf_profile_get($uid, variable_get('foaf_msnChatID',''))) {
$output.= ' '.$msnChatID."\n";
}
if ($yahooChatID = _foaf_profile_get($uid, variable_get('foaf_yahooChatID',''))) {
$output.= ' '.$yahooChatID."\n";
}
if ($jabberID = _foaf_profile_get($uid, variable_get('foaf_jabberID',''))) {
$output.= ' '.$jabberID."\n";
}
if ($workplaceHomepage = _foaf_profile_get($uid, variable_get('foaf_workplaceHomepage',''))) {
$output.= ' '."\n";
}
$address = '';
if ($street = _foaf_profile_get($uid, variable_get('foaf_street',''))) {
$address.= ' '.$street."\n";
}
if ($locality = _foaf_profile_get($uid, variable_get('foaf_locality',''))) {
$address.= ' '.$locality."\n";
}
if ($region = _foaf_profile_get($uid, variable_get('foaf_region',''))) {
$address.= ' '.$region."\n";
}
if ($pcode = _foaf_profile_get($uid, variable_get('foaf_pcode',''))) {
$address.= ' '.$pcode."\n";
}
if ($country = _foaf_profile_get($uid, variable_get('foaf_country',''))) {
$address.= ' '.$country."\n";
}
if ($address) {
$output.= ' '."\n";
$output.= $address;
$output.= " \n";
}
if ($keywords = _foaf_profile_get($uid, variable_get('foaf_keywords', ''))) {
$output .= ' '.$keywords."\n";
}
if (function_exists('buddylist_get_buddies')) {
$buddies = buddylist_get_buddies($uid);
foreach ($buddies as $buddy) {
$output .= " \n";
$output .= " \n";
$output .= " ".$buddy->name."\n";
if ($buddy->mail) {
$output .= ' '.sha1('mailto:' . $buddy->mail)."\n";
}
if ($buddy->uid) {
$output .= ' ' ."\n";
}
$output .= " \n";
$output .= " \n";
}
}
$output .= "\n";
$output .= "\n";
header ('Content-Type: application/rdf+xml');
header ('Content-Disposition: attachment; filename="'.$account->name.'.rdf"');
print $output;
}
function foaf_import($account) {
if (!$account->foaf_url) {
return;
}
$fp = fopen($account->foaf_url,'r');
if (!$fp) {
// failed to open file (set allow_url_fopen = On)
return;
}
while (!feof($fp)) {
$data .= fread($fp, 4192);
}
fclose($fp);
$foaf = _foaf_parse($data);
// import the image
if ($img = $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:IMG'][0]['ATTRIBUTES']['RDF:RESOURCE']) {
_foaf_import_image($img, $account);
}
else if ($depiction = $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:DEPICTION'][0]['ATTRIBUTES']['RDF:RESOURCE']) {
_foaf_import_image($depiction, $account);
}
_foaf_profile_set($account->uid, variable_get('foaf_name',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:NAME'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_title',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:TITLE'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_firstName',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:FIRSTNAME'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_surname',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:SURNAME'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_Organization',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:ORGANIZATION'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_phone',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:PHONE'][0]['ATTRIBUTES']['RDF:RESOURCE']);
_foaf_profile_set($account->uid, variable_get('foaf_aimChatID',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:AIMCHATID'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_icqChatID',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:ICQCHATID'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_msnChatID',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:MSNCHATID'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_yahooChatID',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:YAHOOCHATID'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_jabberID',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:JABBERID'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_workplaceHomepage',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:WORKPLACEHOMEPAGE'][0]['ATTRIBUTES']['RDF:RESOURCE']);
_foaf_profile_set($account->uid, variable_get('foaf_street',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['VCARD:ADR'][0]['VCARD:STREET'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_locality',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['VCARD:ADR'][0]['VCARD:LOCALITY'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_region',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['VCARD:ADR'][0]['VCARD:REGION'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_pcode',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['VCARD:ADR'][0]['VCARD:PCODE'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_country',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['VCARD:ADR'][0]['VCARD:COUNTRY'][0]['VALUE']);
_foaf_profile_set($account->uid, variable_get('foaf_keywords',0), $foaf['RDF:RDF'][0]['FOAF:PERSON'][0]['BIO:KEYWORDS'][0]['VALUE']);
watchdog('user', t('FOAF: imported data from %url', array('%url' => $account->foaf_url)));
}
function foaf_login($user) {
if (!$user->foaf_url) {
if ($server = strrchr($user->name, '@')) {
$name = substr($user->name, 0, strlen($user->name) - strlen($server));
$server = substr($server, 1);
}
else {
return;
}
// send an xmlrpc message to the server to get a full foaf url
$result = xmlrpc($server, 'foaf.getUrl', $user->name);
if ($result !== FALSE) {
$user->foaf_url = $result;
}
}
foaf_import($user);
}
/**
* XML-RPC function to return the FOAF url for a user
*/
function foaf_get_url($username) {
if ($user = user_load(array('name' => $username, 'status' => 1))) {
return url('foaf/'.$user->uid, null,null, true);
}
}
function foaf_user_form($user) {
$foafuser = variable_get('foafuser', NULL);
$form['foafuser'] = array(
'#type' => 'fieldset',
'#title' => t('FOAF Settings'),
'#weight' => 0,
);
$form['foafuser']['foaf_url'] = array(
'#type' => 'textfield',
'#title' => t('URL for your FOAF file'),
'#default_value' => $user->foaf_url,
'#size' => 64,
'#maxlength' => 255,
);
$form['foafuser']['foaf_export'] = array(
'#type' => 'checkbox',
'#title' => t('Allow FOAF export'),
'#return_value' => 1,
'#default_value' => _foaf_allow_export($user),
);
return $form;
}
/**
* Helper functions
*/
function _foaf_profile_fields() {
$output = array('' => t('Select an option'));
$result = db_query("SELECT fid, title FROM {profile_fields} WHERE visibility=%d OR visibility=%d", PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS);
while ($row = db_fetch_object($result)) {
$output[$row->fid] = $row->title;
}
return $output;
}
function _foaf_profile_get($uid, $fid) {
return db_result(db_query("SELECT value FROM {profile_values} WHERE fid=%d AND uid=%d", $fid, $uid));
}
function _foaf_profile_set($uid, $fid, $value) {
if (!$uid || !$fid || !$value) {
return;
}
db_query("DELETE FROM {profile_values} WHERE fid = %d AND uid = %d", $fid, $uid);
db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $fid, $uid, $value);
}
/**
* note: this is a bit of a hack, since user_validate_picture expects the
* picture to have been http uploaded
*
* TODO - refactor user_validate_picture
*/
function _foaf_import_image($url, $account) {
$fp = fopen($url,'r');
if (!$fp) {
// failed to open file (set allow_url_fopen = On)
return;
}
while (!feof($fp)) {
$data .= fread($fp, 4192);
}
fclose($fp);
$tempdest = variable_get('user_picture_path', 'pictures') .'/tmp-picture-'. $account->uid;
$tempfile = file_save_data($data, $tempdest, 1);
// validate the file
$size = @getimagesize($tempfile);
list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85'));
if ((!in_array($size[2], array(1, 2, 3)))) {
file_delete($tempfile);
}
else if ($file->size > (variable_get('user_picture_file_size', '30') * 1000)) {
file_delete($tempfile);
}
else if ($size[0] > $maxwidth || $size[1] > $maxheight) {
file_delete($tempfile);
}
else {
switch($size[2]) {
case 1:
$ext = '.gif';
break;
case 2:
$ext = '.jpg';
break;
case 3:
$ext = '.png';
break;
}
$picture = variable_get('user_picture_path', 'pictures') .'/picture-'. $account->uid . $ext;
$result = file_move($tempfile, $picture, FILE_EXISTS_REPLACE);
if ($result) {
db_query("UPDATE {users} SET picture='%s' WHERE uid=%d", $tempfile, $account->uid);
}
}
}
/**
* THE FOLLOWING CODE COMES FROM Joel DeGan's Parser:
*
* http://peoplesdns.com/parse/parse.phps
*/
function _foaf_parse($data) {
$parser = xml_parser_create('ISO-8859-1');
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $values, $index);
xml_parser_free($parser);
$i = -1;
return _foaf_getchildren($values, $i);
}
// internal function: build a node of the tree
function _foaf_buildtag($thisvals, $vals, &$i, $type) {
if (isset($thisvals['attributes']))
$tag['ATTRIBUTES'] = $thisvals['attributes'];
// complete tag, just return it for storage in array
if ($type === 'complete')
$tag['VALUE'] = $thisvals['value'];
// open tag, recurse
else
$tag = array_merge($tag, _foaf_getchildren($vals, $i));
return $tag;
}
// internal function: build an nested array representing children
function _foaf_getchildren($vals, &$i) {
$children = array(); // Contains node data
// Node has CDATA before it's children
if ($i > -1 && isset($vals[$i]['value']))
$children['VALUE'] = $vals[$i]['value'];
// Loop through children, until hit close tag or run out of tags
while (++$i < count($vals)) {
$type = $vals[$i]['type'];
// 'cdata': Node has CDATA after one of it's children
// (Add to cdata found before in this case)
if ($type === 'cdata')
$children['VALUE'] .= $vals[$i]['value'];
// 'complete': At end of current branch
// 'open': Node has children, recurse
elseif ($type === 'complete' || $type === 'open') {
$tag = _foaf_buildtag($vals[$i], $vals, $i, $type);
if ($index_numeric) {
$tag['TAG'] = $vals[$i]['tag'];
$children[] = $tag;
} else
$children[$vals[$i]['tag']][] = $tag;
}
// 'close: End of node, return collected data
// Do not increment $i or nodes disappear!
elseif ($type === 'close')
break;
}
if ($collapse_dups)
foreach($children as $key => $value)
if (is_array($value) && (count($value) == 1))
$children[$key] = $value[0];
return $children;
}
function _foaf_allow_export($account) {
if (isset($account->foaf_export)) {
return $account->foaf_export;
}
else {
return variable_get('foaf_export_default', 0);
}
}
?>