| 1 |
|
<?php |
| 2 |
|
// $Id;$ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* |
| 6 |
|
*/ |
| 7 |
|
function _trackback_send($nid) { |
| 8 |
|
if (!is_numeric($nid)) |
| 9 |
|
return FALSE; |
| 10 |
|
|
| 11 |
|
if (!trackback_node_enabled($nid)) |
| 12 |
|
return FALSE; |
| 13 |
|
|
| 14 |
|
drupal_set_message(__function__ .'() NID='. var_export($nid, TRUE)); |
| 15 |
|
$node = node_load($nid); |
| 16 |
|
|
| 17 |
|
$urls = array(); |
| 18 |
|
|
| 19 |
|
if (variable_get('trackback_auto_detection', 'auto') == 'user' && !empty($node->trackback_urls)) { |
| 20 |
|
// Use user provided URLs |
| 21 |
|
foreach (explode("\n", $node->trackback_urls) as $url) { |
| 22 |
|
if ($url = trim($url)) { |
| 23 |
|
$urls[] = $url; |
| 24 |
|
} |
| 25 |
|
} |
| 26 |
|
} |
| 27 |
|
else { |
| 28 |
|
// Use auto-discovery feature |
| 29 |
|
$urls = _trackback_extract_urls($node); |
| 30 |
|
} |
| 31 |
|
$retry = array(); |
| 32 |
|
/* |
| 33 |
|
if (isset($node->trackback_urls_to_retry)) { |
| 34 |
|
$retry = array_diff($node->trackback_urls_to_retry, array(0)); |
| 35 |
|
} |
| 36 |
|
*/ |
| 37 |
|
|
| 38 |
|
$already_sent = trackback_get_sent($node, 'successful'); |
| 39 |
|
$urls = array_diff($urls, $already_sent); |
| 40 |
|
|
| 41 |
|
// drupal_set_message(__function__ .'() FINAL='. var_export($urls, TRUE)); |
| 42 |
|
|
| 43 |
|
foreach($urls as $url) { |
| 44 |
|
$pos = strpos($url, '#'); |
| 45 |
|
if ($pos !== FALSE) |
| 46 |
|
$url = substr($url, 0, $pos); |
| 47 |
|
|
| 48 |
|
drupal_set_message(__function__ .'() Sending to ='. var_export($url, TRUE)); |
| 49 |
|
|
| 50 |
|
if (valid_url($url, TRUE)) { |
| 51 |
|
if (_trackback_pingback_send($node->nid, $url, TRUE)) { |
| 52 |
|
drupal_set_message(t('Pingback successfully sent to %url', array('%url' => $url))); |
| 53 |
|
} |
| 54 |
|
} |
| 55 |
|
} |
| 56 |
|
// _trackback_ping($node, $urls, $retry); |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
/** |
| 60 |
|
* |
| 61 |
|
*/ |
| 62 |
|
function _trackback_extract_urls($node) { |
| 63 |
|
$return = array(); |
| 64 |
|
|
| 65 |
|
// First, grab anything that looks like a url from the body of the node. |
| 66 |
|
$node = _trackback_build_content(drupal_clone($node)); |
| 67 |
|
$content = drupal_render($node->content); |
| 68 |
|
$pattern = '((?:http|https)://[a-z0-9;/?:@&=+#$,_.!~*()%-]+)'; |
| 69 |
|
|
| 70 |
|
if (variable_get('trackback_auto_detection', 'auto') == 'auto-links') { |
| 71 |
|
$content = strip_tags($content, '<a>'); // remove comment. |
| 72 |
|
$pattern = '<a\s+[^>]*href\s*=\s*(?:"|\')'. $pattern; |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
if (preg_match_all('`'. $pattern .'`i', $content, $parsed_urls)) { |
| 76 |
|
$return = array_unique($parsed_urls[1]); |
| 77 |
|
// drupal_set_message(__function__ .'()'. var_export($return, TRUE)); |
| 78 |
|
} |
| 79 |
|
return $return; |
| 80 |
|
} |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
/** |
| 84 |
|
* Discover if given TARGET is a trackback source or not |
| 85 |
|
*/ |
| 86 |
|
function _trackback_trackback_discover($target) { |
| 87 |
|
$return = ''; |
| 88 |
|
|
| 89 |
|
// Do something here.. |
| 90 |
|
|
| 91 |
|
return $return; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
/** |
| 97 |
|
* Discover a pingback server with pingback autodiscovery schemes. |
| 98 |
|
* @param $target the absolute URL to search for its server. This should have passed check_url() first. |
| 99 |
|
* @return the PingbackServer to send request |
| 100 |
|
*/ |
| 101 |
|
function _trackback_pingback_discover($target) { |
| 102 |
|
// drupal_set_message(__function__); |
| 103 |
|
$server = ''; |
| 104 |
|
//#1: send a HEAD to check for X-Pingback header |
| 105 |
|
$r = drupal_http_request($target, array(), 'HEAD'); |
| 106 |
|
if (empty($r->error)) { |
| 107 |
|
if (is_array($r->headers) && isset($r->headers['X-Pingback'])) { |
| 108 |
|
$server = $r->headers['X-Pingback']; |
| 109 |
|
} |
| 110 |
|
else { |
| 111 |
|
//#2: search for <link rel="pingback" href="(server)" /> tags |
| 112 |
|
$get = drupal_http_request($target); |
| 113 |
|
if (empty($get->error)) { |
| 114 |
|
//this regexp is the one provided in the spec |
| 115 |
|
if (preg_match('#<link rel="pingback" href="([^"]+)" ?/?>#', $get->data, $matches)) { |
| 116 |
|
$server = $matches[1]; |
| 117 |
|
} |
| 118 |
|
} |
| 119 |
|
} |
| 120 |
|
} |
| 121 |
|
if (!empty($server)) { |
| 122 |
|
// Replacements of tokens as described in http://www.hixie.ch/specs/pingback/pingback |
| 123 |
|
return check_url($server); |
| 124 |
|
} |
| 125 |
|
return ''; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
/** |
| 129 |
|
* Send pingbacks. Does nothing if the target does not have a pingback server. |
| 130 |
|
* @param $nid the source node ID. |
| 131 |
|
* @param $target the target absolute URL. |
| 132 |
|
* @param $source_is_absolute if this value is set to TRUE, $nid is interpreted as an absolute URL (which may originate not from the host site). |
| 133 |
|
* @return TRUE on success, FALSE otherwise. |
| 134 |
|
*/ |
| 135 |
|
function _trackback_pingback_send($nid, $target) { |
| 136 |
|
$source = url("node/$nid", array('absolute' => TRUE)); |
| 137 |
|
|
| 138 |
|
//server autodiscovery |
| 139 |
|
$server = _trackback_pingback_discover($target); |
| 140 |
|
drupal_set_message(__function__ .'() NID='. $nid . '..TARGET='.$target. ' SERVER='.$server); |
| 141 |
|
// return TRUE; |
| 142 |
|
|
| 143 |
|
if (!empty($server)) { |
| 144 |
|
if (xmlrpc($server, 'pingback.ping', $source, $target)) { |
| 145 |
|
trackback_sent($nid, $target, TRUE, 'Pingback'); |
| 146 |
|
watchdog('trackback', 'Pingback: Pingback to %target from %source succeeded.', array('%source' => $source, '%target' => $target)); |
| 147 |
|
return TRUE; |
| 148 |
|
} |
| 149 |
|
else { |
| 150 |
|
watchdog('trackback', 'Pingback: Pingback to %target from %source failed. Error @errno: @description', array('%source' => $source, '%target' => $target, '@errno' => xmlrpc_errno(), '@description' => xmlrpc_error_msg()), WATCHDOG_WARNING); |
| 151 |
|
return FALSE; |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
|
// watchdog('pingback', 'Server not found', array(), WATCHDOG_WARNING); |
| 155 |
|
return FALSE; |
| 156 |
|
} |