PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_easyblog/classes/trackback.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 372 lines | 201 code | 23 blank | 148 comment | 21 complexity | cea313ed6242c89f39084d670f3483d0 MD5 | raw file
  1. <?php
  2. /**
  3. * PHP Class to handle TrackBacks (send/ping, receive, retreive, detect, seed, etc...)
  4. *
  5. * <code><?php
  6. * include('trackback_cls.php');
  7. * $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
  8. * ?></code>
  9. *
  10. * ==============================================================================
  11. *
  12. * @version $Id: trackback_cls.php,v 1.2 2004/12/11 18:54:32 Ran Exp $
  13. * @copyright Copyright (c) 2004 Ran Aroussi (http://www.blogish.org)
  14. * @author Ran Aroussi <ran@blogish.org>
  15. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  16. *
  17. * ==============================================================================
  18. */
  19. defined('_JEXEC') or die('Restricted access');
  20. class EasyBlogTrackback
  21. {
  22. var $blog_name = ''; // Default blog name used throughout the class (ie. BLOGish)
  23. var $author = ''; // Default author name used throughout the class (ie. Ran Aroussi)
  24. var $encoding = ''; // Default encoding used throughout the class (ie. UTF-8)
  25. var $get_id = ''; // Retreives and holds $_GET['id'] (if not empty)
  26. var $post_id = ''; // Retreives and holds $_POST['id'] (if not empty)
  27. var $url = ''; // Retreives and holds $_POST['url'] (if not empty)
  28. var $title = ''; // Retreives and holds $_POST['title'] (if not empty)
  29. var $expert = ''; // Retreives and holds $_POST['expert'] (if not empty)
  30. function EasyBlogTrackback($blog_name, $author, $encoding = "UTF-8")
  31. {
  32. $this->blog_name = $blog_name;
  33. $this->author = $author;
  34. $this->encoding = $encoding;
  35. }
  36. function ping($tb, $url, $title = "", $excerpt = "")
  37. {
  38. $response = "";
  39. $reason = "";
  40. // Set default values
  41. if (empty($title))
  42. {
  43. $title = "Trackbacking your entry...";
  44. }
  45. if (empty($excerpt))
  46. {
  47. $excerpt = "I found your entry interesting do I've added a Trackback to it on my weblog :)";
  48. }
  49. // Parse the target
  50. $target = parse_url($tb);
  51. if ((isset($target["query"])) && ($target["query"] != ""))
  52. {
  53. $target["query"] = "?" . $target["query"];
  54. }
  55. else
  56. {
  57. $target["query"] = "";
  58. }
  59. if ((isset($target["port"]) && !is_numeric($target["port"])) || (!isset($target["port"])))
  60. {
  61. $target["port"] = 80;
  62. }
  63. if( !isset( $target["host"] ) )
  64. return false;
  65. // Open the socket
  66. $tb_sock = fsockopen($target["host"], $target["port"]);
  67. // Something didn't work out, return
  68. if (!is_resource($tb_sock))
  69. {
  70. return '$trackback->ping: can\'t connect to: ' . $tb . '.';
  71. }
  72. // Put together the things we want to send
  73. $tb_send = "url=" . rawurlencode($url) . "&title=" . rawurlencode($title) . "&blog_name=" . rawurlencode($this->blog_name) . "&excerpt=" . rawurlencode($excerpt);
  74. // Send the trackback
  75. fputs($tb_sock, "POST " . @$target["path"] . $target["query"] . " HTTP/1.1\r\n");
  76. fputs($tb_sock, "Host: " . $target["host"] . "\r\n");
  77. fputs($tb_sock, "Content-type: application/x-www-form-urlencoded\r\n");
  78. fputs($tb_sock, "Content-length: " . strlen($tb_send) . "\r\n");
  79. fputs($tb_sock, "Connection: close\r\n\r\n");
  80. fputs($tb_sock, $tb_send);
  81. // Gather result
  82. while (!feof($tb_sock)) {
  83. $response .= fgets($tb_sock, 128);
  84. }
  85. // Close socket
  86. fclose($tb_sock);
  87. // Did the trackback ping work
  88. strpos($response, '<error>0</error>') ? $return = true : $return = false;
  89. // send result
  90. return $return;
  91. }
  92. function receive($success = false, $err_response = "")
  93. {
  94. if (!$success && empty($err_response))
  95. {
  96. $err_response = JText::_('An error occured while tring to log your trackback...');
  97. }
  98. // Start response to trackbacker...
  99. $return = '<?xml version="1.0" encoding="' . $this->encoding . '"?>' . "\n";
  100. $return .= "<response> \n";
  101. // Send back response...
  102. if ($success)
  103. {
  104. // Trackback received successfully...
  105. $return .= " <error>0</error> \n";
  106. }
  107. else
  108. {
  109. // Something went wrong...
  110. $return .= " <error>1</error> \n";
  111. $return .= " <message>" . $this->xml_safe($err_response) . "</message>\n";
  112. }
  113. // End response to trackbacker...
  114. $return .= "</response>";
  115. return $return;
  116. }
  117. /**
  118. * Feteched trackback information and produces an RSS-0.91 feed.
  119. *
  120. * <code><?php
  121. * // 1
  122. * header('Content-Type: text/xml'); // MUST be the 1st line
  123. * // 2
  124. * include('trackback_cls.php');
  125. * $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
  126. * // 3
  127. * $tb_id = $trackback->get_id;
  128. * // 4
  129. * Do whatever to get trackback information by ID (search db, etc...)
  130. * if (GOT_TRACKBACK_INFO) {
  131. * // Successful - pass trackback info as array()...
  132. * $tb_info = array('title' => string TRACKBACK_TITLE,
  133. * 'expert' => string TRACKBACK_EXPERT,
  134. * 'permalink' => string PERMALINK_URL,
  135. * 'trackback' => string TRACKBACK_URL
  136. * );
  137. * echo $trackback->fetch(true, $tb_info);
  138. * } else {
  139. * // Something went wrong - tell my why...
  140. * echo $trackback->fetch(false, string RESPONSE);
  141. * }
  142. * ?></code>
  143. *
  144. * @param boolean $success
  145. * @param string $response
  146. * @return string XML response to the caller
  147. */
  148. function fetch($success = false, $response = "")
  149. {
  150. if (!$success && empty($response)) {
  151. $response = "An error occured while tring to retreive trackback information...";
  152. }
  153. // Start response to caller
  154. $return = '<?xml version="1.0" encoding="' . $this->encoding . '"?>' . "\n";
  155. $return .= "<response> \n";
  156. // Send back response...
  157. if ($success) {
  158. // Trackback retreived successfully...
  159. // Sending back an RSS (0.91) - trackback information from $response (array)...
  160. $return .= " <error>0</error> \n";
  161. $return .= " <rss version=\"0.91\"> \n";
  162. $return .= " <channel> \n";
  163. $return .= " <title>" . $this->xml_safe($response['title']) . "</title> \n";
  164. $return .= " <link>" . $this->xml_safe($response['trackback']) . "</link> \n";
  165. $return .= " <description>" . $this->xml_safe($response['expert']) . "</description> \n";
  166. $return .= " <item> \n";
  167. $return .= " <title>" . $this->xml_safe($response['title']) . "</title> \n";
  168. $return .= " <link>" . $this->xml_safe($response['permalink']) . "</link> \n";
  169. $return .= " <description>" . $this->xml_safe($response['expert']) . "</description> \n";
  170. $return .= " </item> \n";
  171. $return .= " </channel> \n";
  172. $return .= " </rss> \n";
  173. } else {
  174. // Something went wrong - provide reason from $response (string)...
  175. $return .= " <error>1</error> \n";
  176. $return .= " <message>" . $this->xml_safe($response) . "</message>\n";
  177. }
  178. // End response to trackbacker
  179. $return .= "</response>";
  180. return $return;
  181. }
  182. /**
  183. * Produces embedded RDF representing metadata about the entry,
  184. * allowing clients to auto-discover the TrackBack Ping URL.
  185. *
  186. * NOTE: DATE should be string in RFC822 Format - Use RFC822_from_datetime().
  187. *
  188. * <code><?php
  189. * include('trackback_cls.php');
  190. * $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
  191. *
  192. * echo $trackback->rdf_autodiscover(string DATE, string TITLE, string EXPERT, string PERMALINK, string TRACKBACK [, string AUTHOR]);
  193. * ?></code>
  194. *
  195. * @param string $RFC822_date
  196. * @param string $title
  197. * @param string $expert
  198. * @param string $permalink
  199. * @param string $trackback
  200. * @param string $author
  201. * @return string
  202. */
  203. function rdf_autodiscover($RFC822_date, $title, $expert, $permalink, $trackback, $author = "")
  204. {
  205. if (!$author) {
  206. $author = $this->author;
  207. }
  208. $return = "<!-- \n";
  209. $return .= "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" \n";
  210. $return .= " xmlns:dc=\"http://purl.org/dc/elements/1.1/\" \n";
  211. $return .= " xmlns:trackback=\"http://madskills.com/public/xml/rss/module/trackback/\"> \n";
  212. $return .= "<rdf:Description \n";
  213. $return .= " rdf:about=\"" . $this->xml_safe($permalink) . "\" \n";
  214. $return .= " dc:identifier=\"" . $this->xml_safe($permalink) . "\" \n";
  215. $return .= " trackback:ping=\"" . $this->xml_safe($trackback) . "\" \n";
  216. $return .= " dc:title=\"" . $this->xml_safe($title) . "\" \n";
  217. $return .= " dc:subject=\"TrackBack\" \n";
  218. $return .= " dc:description=\"" . $this->xml_safe($this->cut_short($expert)) . "\" \n";
  219. $return .= " dc:creator=\"" . $this->xml_safe($author) . "\" \n";
  220. $return .= " dc:date=\"" . $RFC822_date . "\" /> \n";
  221. $return .= "</rdf:RDF> \n";
  222. $return .= "--> \n";
  223. return $return;
  224. }
  225. /**
  226. * Search text for links, and searches links for trackback URLs.
  227. *
  228. * <code><?php
  229. *
  230. * include('trackback_cls.php');
  231. * $trackback = new Trackback('BLOGish', 'Ran Aroussi', 'UTF-8');
  232. *
  233. * if ($tb_array = $trackback->auto_discovery(string TEXT)) {
  234. * // Found trackbacks in TEXT. Looping...
  235. * foreach($tb_array as $tb_key => $tb_url) {
  236. * // Attempt to ping each one...
  237. * if ($trackback->ping($tb_url, string URL, [string TITLE], [string EXPERT])) {
  238. * // Successful ping...
  239. * echo "Trackback sent to <i>$tb_url</i>...\n";
  240. * } else {
  241. * // Error pinging...
  242. * echo "Trackback to <i>$tb_url</i> failed....\n";
  243. * }
  244. * }
  245. * } else {
  246. * // No trackbacks in TEXT...
  247. * echo "No trackbacks were auto-discover...\n"
  248. * }
  249. * ?></code>
  250. *
  251. * @param string $text
  252. * @return array Trackback URLs.
  253. */
  254. function auto_discovery($text)
  255. {
  256. // Get a list of UNIQUE links from text...
  257. // ---------------------------------------
  258. // RegExp to look for (0=>link, 4=>host in 'replace')
  259. $reg_exp = "/(http)+(s)?:(\\/\\/)((\\w|\\.)+)(\\/)?(\\S+)?/i";
  260. // Make sure each link ends with [sapce]
  261. $text = eregi_replace("www.", "http://www.", $text);
  262. $text = eregi_replace("http://http://", "http://", $text);
  263. $text = eregi_replace("\"", " \"", $text);
  264. $text = eregi_replace("'", " '", $text);
  265. $text = eregi_replace(">", " >", $text);
  266. // Create an array with unique links
  267. $uri_array = array();
  268. if (preg_match_all($reg_exp, strip_tags($text, "<a>"), $array, PREG_PATTERN_ORDER)) {
  269. foreach($array[0] as $key => $link) {
  270. foreach((array(",", ".", ":", ";")) as $t_key => $t_value) {
  271. $link = trim($link, $t_value);
  272. }
  273. $uri_array[] = ($link);
  274. }
  275. $uri_array = array_unique($uri_array);
  276. }
  277. // Get the trackback URIs from those links...
  278. // ------------------------------------------
  279. // Loop through the URIs array and extract RDF segments
  280. $rdf_array = array(); // <- holds list of RDF segments
  281. foreach($uri_array as $key => $link) {
  282. if ($link_content = implode("", @file($link))) {
  283. preg_match_all('/(<rdf:RDF.*?<\/rdf:RDF>)/sm', $link_content, $link_rdf, PREG_SET_ORDER);
  284. for ($i = 0; $i < count($link_rdf); $i++) {
  285. if (preg_match('|dc:identifier="' . preg_quote($link) . '"|ms', $link_rdf[$i][1])) {
  286. $rdf_array[] = trim($link_rdf[$i][1]);
  287. }
  288. }
  289. }
  290. }
  291. // Loop through the RDFs array and extract trackback URIs
  292. $tb_array = array(); // <- holds list of trackback URIs
  293. if (!empty($rdf_array)) {
  294. for ($i = 0; $i < count($rdf_array); $i++) {
  295. if (preg_match('/trackback:ping="([^"]+)"/', $rdf_array[$i], $array)) {
  296. $tb_array[] = trim($array[1]);
  297. }
  298. }
  299. }
  300. // Return Trackbacks
  301. return $tb_array;
  302. }
  303. /**
  304. * Other Useful functions used in this class
  305. */
  306. /**
  307. * Converts MySQL datetime to a standart RFC 822 date format
  308. *
  309. * @param string $datetime
  310. * @return string RFC 822 date
  311. */
  312. function RFC822_from_datetime($datetime)
  313. {
  314. $timestamp = mktime(
  315. substr($datetime, 8, 2), substr($datetime, 10, 2), substr($datetime, 12, 2),
  316. substr($datetime, 4, 2), substr($datetime, 6, 2), substr($datetime, 0, 4)
  317. );
  318. return date("r", $timestamp);
  319. }
  320. /**
  321. * Converts a string into an XML-safe string (replaces &, <, >, " and ')
  322. *
  323. * @param string $string
  324. * @return string
  325. */
  326. function xml_safe($string)
  327. {
  328. return htmlspecialchars($string, ENT_QUOTES);
  329. }
  330. /**
  331. * Cuts a string short (with "...") accroding to $max_length...
  332. *
  333. * @param string $string
  334. * @param integer $max_length
  335. * @return string
  336. */
  337. function cut_short($string, $max_length = 255)
  338. {
  339. if (strlen($string) > $max_length) {
  340. $string = substr($string, 0, $max_length) . '...';
  341. }
  342. return $string;
  343. }
  344. }