| 1 |
<?php |
<?php |
| 2 |
// $Id: follow.module,v 1.5 2009/10/30 22:00:08 pwolanin Exp $ |
// $Id: follow.module,v 1.6 2009/11/02 20:25:53 pwolanin Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 17 |
switch ($path) { |
switch ($path) { |
| 18 |
case 'follow': |
case 'follow': |
| 19 |
case 'user/%/follow': |
case 'user/%/follow': |
| 20 |
return t('Please copy and paste urls for each service you would like to display in the block.'); |
case 'admin/config/services/follow': |
| 21 |
|
return t('Please copy and paste the url for your public profile or page for each service you would like to display in the block. Links need to match the domain of the service in question.'); |
| 22 |
case 'admin/settings/follow': |
case 'admin/settings/follow': |
| 23 |
return t('Here you can set what the default titles are for the "follow" blocks. If you would like a custom title, edit the individual blocks <a href="!href">here</a>.', array('!href' => url('admin/build/block'))); |
return t('Here you can set what the default titles are for the "follow" blocks. If you would like a custom title, edit the individual blocks <a href="!href">here</a>.', array('!href' => url('admin/build/block'))); |
| 24 |
} |
} |
| 233 |
if ($setting == FOLLOW_NAME) { |
if ($setting == FOLLOW_NAME) { |
| 234 |
$account = user_load($uid); |
$account = user_load($uid); |
| 235 |
// Set plain to TRUE for realname module support. |
// Set plain to TRUE for realname module support. |
| 236 |
return t('Follow !name on', array('!name' => theme('username', $account, array('plain' => TRUE)))); |
return t('Follow !name on', array('!name' => theme('username', array('account' => $account)))); |
| 237 |
} |
} |
| 238 |
return t('Follow me on'); |
return t('Follow me on'); |
| 239 |
} |
} |
| 283 |
$output = '<div class="follow-links clearfix">'; |
$output = '<div class="follow-links clearfix">'; |
| 284 |
|
|
| 285 |
foreach($links as $link) { |
foreach($links as $link) { |
| 286 |
$title = $networks[$link->name]; |
$title = $networks[$link->name]['title']; |
| 287 |
$output .= theme('follow_link', array('link' => $link, 'title' => $title)); |
$output .= theme('follow_link', array('link' => $link, 'title' => $title)); |
| 288 |
} |
} |
| 289 |
|
|
| 312 |
'class' => implode(' ', $classes), |
'class' => implode(' ', $classes), |
| 313 |
'title' => follow_link_title($link->uid) .' '. $title, |
'title' => follow_link_title($link->uid) .' '. $title, |
| 314 |
); |
); |
| 315 |
|
$link->options['attributes'] = $attributes; |
| 316 |
return l($title, $link->url, array('attributes' => $attributes)) . "\n"; |
return l($title, $link->path, $link->options) . "\n"; |
| 317 |
} |
} |
| 318 |
|
|
| 319 |
/** |
/** |
| 376 |
// Put all our existing links at the top, sorted by weight. |
// Put all our existing links at the top, sorted by weight. |
| 377 |
if (is_array($links)) { |
if (is_array($links)) { |
| 378 |
foreach ($links as $name => $link) { |
foreach ($links as $name => $link) { |
| 379 |
$title = $networks[$name]; |
$title = $networks[$name]['title']; |
| 380 |
$form['follow_links'][$name] = _follow_links_form_link($link, $title); |
$form['follow_links'][$name] = _follow_links_form_link($link, $title, $uid); |
| 381 |
// Unset this specific network so we don't add the same one again below. |
// Unset this specific network so we don't add the same one again below. |
| 382 |
unset($networks[$name]); |
unset($networks[$name]); |
| 383 |
} |
} |
| 384 |
} |
} |
| 385 |
// Now add all the empty ones. |
// Now add all the empty ones. |
| 386 |
foreach ($networks as $name => $title) { |
foreach ($networks as $name => $info) { |
| 387 |
$link = new stdClass(); |
$link = new stdClass(); |
| 388 |
$form['follow_links'][$name] = _follow_links_form_link($link, $title); |
$link->name = $name; |
| 389 |
|
$form['follow_links'][$name] = _follow_links_form_link($link, $info['title'], $uid); |
| 390 |
} |
} |
| 391 |
|
|
| 392 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); |
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit')); |
| 397 |
/** |
/** |
| 398 |
* Helper function to create an individual link form element. |
* Helper function to create an individual link form element. |
| 399 |
*/ |
*/ |
| 400 |
function _follow_links_form_link($link, $title) { |
function _follow_links_form_link($link, $title, $uid) { |
| 401 |
$elements = array(); |
$elements = array(); |
| 402 |
|
|
| 403 |
$elements['name'] = array( |
$elements['name'] = array( |
| 415 |
} |
} |
| 416 |
$elements['url'] = array( |
$elements['url'] = array( |
| 417 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 418 |
|
'#follow_network' => $link->name, |
| 419 |
|
'#follow_uid' => $uid, |
| 420 |
'#default_value' => isset($link->url) ? $link->url : '', |
'#default_value' => isset($link->url) ? $link->url : '', |
| 421 |
'#element_validate' => array('follow_url_validate'), |
'#element_validate' => array('follow_url_validate'), |
| 422 |
); |
); |
| 425 |
} |
} |
| 426 |
|
|
| 427 |
/** |
/** |
| 428 |
|
* Like drupal_http_build_query() but without urlencodings. |
| 429 |
|
*/ |
| 430 |
|
function follow_build_query(array $query, $parent = '') { |
| 431 |
|
$params = array(); |
| 432 |
|
|
| 433 |
|
foreach ($query as $key => $value) { |
| 434 |
|
$key = ($parent ? $parent . '[' . $key . ']' : $key); |
| 435 |
|
|
| 436 |
|
// Recurse into children. |
| 437 |
|
if (is_array($value)) { |
| 438 |
|
$params[] = follow_build_query($value, $key); |
| 439 |
|
} |
| 440 |
|
// If a query parameter value is NULL, only append its key. |
| 441 |
|
elseif (!isset($value)) { |
| 442 |
|
$params[] = $key; |
| 443 |
|
} |
| 444 |
|
else { |
| 445 |
|
$params[] = $key . '=' . $value; |
| 446 |
|
} |
| 447 |
|
} |
| 448 |
|
return implode('&', $params); |
| 449 |
|
} |
| 450 |
|
|
| 451 |
|
/** |
| 452 |
|
* Build a url for use in the form. |
| 453 |
|
*/ |
| 454 |
|
function follow_build_url($path, $options) { |
| 455 |
|
$url = $path; |
| 456 |
|
if (!empty($options['query'])) { |
| 457 |
|
$url .= (strpos($path, '?') !== FALSE ? '&' : '?') . follow_build_query($options['query']); |
| 458 |
|
} |
| 459 |
|
if (!empty($options['fragment'])) { |
| 460 |
|
$url .= '#' . $options['fragment']; |
| 461 |
|
} |
| 462 |
|
return $url; |
| 463 |
|
} |
| 464 |
|
|
| 465 |
|
/** |
| 466 |
|
* Split a Drupal path or external link into path and options like a menu link. |
| 467 |
|
*/ |
| 468 |
|
function follow_parse_url($url) { |
| 469 |
|
$parsed_url = parse_url($url); |
| 470 |
|
$defaults = array( |
| 471 |
|
'scheme' => '', |
| 472 |
|
'host' => '', |
| 473 |
|
'port' => '', |
| 474 |
|
'path' => '/', |
| 475 |
|
'query' => '', |
| 476 |
|
'fragment' => '', |
| 477 |
|
); |
| 478 |
|
$parsed_url += $defaults; |
| 479 |
|
$options = array('query' => array(), 'fragment' => $parsed_url['fragment']); |
| 480 |
|
// Parse the query string into an array. |
| 481 |
|
parse_str($parsed_url['query'], $options['query']); |
| 482 |
|
if ($parsed_url['scheme']) { |
| 483 |
|
$parsed_url['scheme'] .= '://'; |
| 484 |
|
} |
| 485 |
|
// Throw away port for now. |
| 486 |
|
$path = $parsed_url['scheme'] . $parsed_url['host'] . $parsed_url['path']; |
| 487 |
|
return array('path' => $path, 'options' => $options); |
| 488 |
|
} |
| 489 |
|
|
| 490 |
|
/** |
| 491 |
* Validates the url field to verify it's actually a url. |
* Validates the url field to verify it's actually a url. |
| 492 |
*/ |
*/ |
| 493 |
function follow_url_validate($form) { |
function follow_url_validate($form) { |
| 494 |
$url = trim($form['#value']); |
$url = trim($form['#value']); |
| 495 |
if($url && !preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i", $url)) { |
$networks = follow_networks_load($form['#follow_uid']); |
| 496 |
form_error($form, t('The specified url is invalid.')); |
$info = $networks[$form['#follow_network']]; |
| 497 |
|
$regex = follow_build_url_regex($info); |
| 498 |
|
$parsed = follow_parse_url($url); |
| 499 |
|
if($url && !preg_match($regex, $parsed['path'])) { |
| 500 |
|
if (!empty($info['domain'])) { |
| 501 |
|
$message = t('The specified url is invalid for the domain %domain. Make sure you use http://.', array('%domain' => $info['domain'])); |
| 502 |
|
} |
| 503 |
|
else { |
| 504 |
|
$message = t('The specified path is invalid. Please enter a path on this site (e.g. rss.xml or taxonomy/term/1/feed).'); |
| 505 |
|
} |
| 506 |
|
form_error($form, $message); |
| 507 |
|
} |
| 508 |
|
} |
| 509 |
|
|
| 510 |
|
/** |
| 511 |
|
* Build a regex to validate the url based on a known service url. |
| 512 |
|
*/ |
| 513 |
|
function follow_build_url_regex($network_info) { |
| 514 |
|
|
| 515 |
|
if (!empty($network_info['domain'])) { |
| 516 |
|
// An external link. |
| 517 |
|
return '@^https?://([a-z0-9\-_.]+\\.|)' . str_replace('.', '\\.', $network_info['domain']) . '/@i'; |
| 518 |
} |
} |
| 519 |
|
// An internal link should not have ':'. |
| 520 |
|
return '@^[^:]+$@'; |
| 521 |
} |
} |
| 522 |
|
|
| 523 |
/** |
/** |
| 629 |
$result = db_query($sql, array(':uid' => $uid)); |
$result = db_query($sql, array(':uid' => $uid)); |
| 630 |
|
|
| 631 |
foreach ($result as $link) { |
foreach ($result as $link) { |
| 632 |
|
$link->options = unserialize($link->options); |
| 633 |
|
$link->url = follow_build_url($link->path, $link->options); |
| 634 |
$links[$link->name] = $link; |
$links[$link->name] = $link; |
| 635 |
} |
} |
| 636 |
|
|
| 644 |
* A link object to be saved. |
* A link object to be saved. |
| 645 |
*/ |
*/ |
| 646 |
function follow_link_save($link) { |
function follow_link_save($link) { |
| 647 |
|
$parsed = follow_parse_url($link->url); |
| 648 |
|
$link->path = $parsed['path']; |
| 649 |
|
$link->options = $parsed['options']; |
| 650 |
|
|
| 651 |
if (isset($link->lid)) { |
if (isset($link->lid)) { |
| 652 |
drupal_write_record('follow_links', $link, 'lid'); |
drupal_write_record('follow_links', $link, 'lid'); |
| 653 |
} |
} |
| 704 |
*/ |
*/ |
| 705 |
function follow_default_networks($uid) { |
function follow_default_networks($uid) { |
| 706 |
$networks = array( |
$networks = array( |
| 707 |
'facebook' => t('Facebook'), |
'facebook' => array( |
| 708 |
'virb' => t('Virb'), |
'title' => t('Facebook'), |
| 709 |
'myspace' => t('MySpace'), |
'domain' => 'facebook.com', |
| 710 |
'twitter' => t('Twitter'), |
), |
| 711 |
'picasa' => t('Picasa'), |
'virb' => array( |
| 712 |
'flickr' => t('Flickr'), |
'title' => t('Virb'), |
| 713 |
'youtube' => t('YouTube'), |
'domain' => 'virb.com', |
| 714 |
'vimeo' => t('Vimeo'), |
), |
| 715 |
'bliptv' => t('blip.tv'), |
'myspace' => array( |
| 716 |
'lastfm' => t('last.fm'), |
'title' => t('MySpace'), |
| 717 |
'linkedin' => t('LinkedIn'), |
'domain' => 'myspace.com', |
| 718 |
'delicious' => t('Delicious'), |
), |
| 719 |
'tumblr' => t('Tumblr'), |
'twitter' => array( |
| 720 |
|
'title' => t('Twitter'), |
| 721 |
|
'domain' => 'twitter.com', |
| 722 |
|
), |
| 723 |
|
'picasa' => array( |
| 724 |
|
'title' => t('Picasa'), |
| 725 |
|
'domain' => 'picasaweb.google.com', |
| 726 |
|
), |
| 727 |
|
'flickr' => array( |
| 728 |
|
'title' => t('Flickr'), |
| 729 |
|
'domain' => 'flickr.com', |
| 730 |
|
), |
| 731 |
|
'youtube' => array( |
| 732 |
|
'title' => t('YouTube'), |
| 733 |
|
'domain' => 'youtube.com', |
| 734 |
|
), |
| 735 |
|
'vimeo' => array( |
| 736 |
|
'title' => t('Vimeo'), |
| 737 |
|
'domain' => 'vimeo.com', |
| 738 |
|
), |
| 739 |
|
'bliptv' => array( |
| 740 |
|
'title' => t('blip.tv'), |
| 741 |
|
'domain' => 'blip.tv', |
| 742 |
|
), |
| 743 |
|
'lastfm' => array( |
| 744 |
|
'title' => t('last.fm'), |
| 745 |
|
'domain' => 'last.fm', |
| 746 |
|
), |
| 747 |
|
'linkedin' => array( |
| 748 |
|
'title' => t('LinkedIn'), |
| 749 |
|
'domain' => 'linkedin.com', |
| 750 |
|
), |
| 751 |
|
'delicious' => array( |
| 752 |
|
'title' => t('Delicious'), |
| 753 |
|
'domain' => 'delicious.com', |
| 754 |
|
), |
| 755 |
|
'tumblr' => array( |
| 756 |
|
'title' => t('Tumblr'), |
| 757 |
|
'domain' => 'tumblr.com', |
| 758 |
|
), |
| 759 |
); |
); |
| 760 |
if ($uid == 0) { |
if ($uid == 0) { |
| 761 |
$networks['self'] = t('This site'); |
$networks['self'] = array( |
| 762 |
|
'title' => t('This site (RSS)'), |
| 763 |
|
'domain' => '', |
| 764 |
|
); |
| 765 |
} |
} |
| 766 |
return $networks; |
return $networks; |
| 767 |
} |
} |