/[drupal]/contributions/modules/trackback/trackback.module
ViewVC logotype

Contents of /contributions/modules/trackback/trackback.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.75 - (show annotations) (download) (as text)
Thu Dec 6 17:53:49 2007 UTC (23 months, 3 weeks ago) by zorac
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.74: +128 -686 lines
File MIME type: text/x-php
Dividing the module file.
1 <?php
2 // $Id: trackback.module,v 1.74 2007/11/05 19:50:30 zorac Exp $
3
4 define('TRACKBACK_PATH', './'. drupal_get_path('module', 'trackback'));
5 define('TRACKBACK_WITH_SPAM', module_exists('spam'));
6
7 if (TRACKBACK_WITH_SPAM) {
8 include_once TRACKBACK_PATH .'/trackback.spam.inc';
9 }
10
11 /**
12 * Implementation of hook_help().
13 */
14 function trackback_help($path, $arg) {
15 if ($path == 'admin/help#trackback') {
16 include_once TRACKBACK_PATH .'/trackback.admin.inc';
17 return _trackback_help($path, $arg);
18 }
19 }
20
21 /**
22 * Implementation of hook_theme()
23 */
24 function trackback_theme() {
25 return array(
26 'trackback' => array(
27 'arguments' => array('trackback' => NULL, 'links' => array())
28 ),
29 'trackbacks' => array(
30 'arguments' => array('trackbacks' => NULL)
31 ),
32 'trackback_url' => array(
33 'arguments' => array('url' => NULL)
34 ),
35 'trackback_admin_table' => array(
36 'arguments' => array('form' => NULL)
37 )
38 );
39 }
40
41 function theme_trackback($trackback, $links = array()) {
42 $output = '<div class="trackback" id="trackback-'. $trackback->trid .'">'."\n";
43 $output .= '<h3 class="title">'. l($trackback->subject, $trackback->url) .'</h3>'."\n";
44 $output .= '<span class="submitted">'. t('from %sitename on @date', array('%sitename' => $trackback->name, '@date' => format_date($trackback->created))) .'</span>'."\n";
45 $output .= '<div class="content">'. check_markup($trackback->excerpt) .'</div>'."\n";
46 if ($links) {
47 $output .= '<div class="links">'. theme('links', $links) .'</div>'."\n";
48 }
49 $output .= '</div>'."\n";
50 return $output;
51 }
52
53 function theme_trackbacks($trackbacks) {
54 $output = '<div id="trackbacks">'."\n";
55 $output .= $trackbacks ."\n";
56 $output .= '</div>'."\n";
57 return $output;
58 }
59
60 function theme_trackback_url($url) {
61 return '<div id="trackback-url">'. theme('box', t('Trackback URL for this post:'), $url) .'</div>';
62 }
63
64 /**
65 * Invoke a hook_trackback() operation in all modules.
66 *
67 * @param &$trackback
68 * A trackback object.
69 * @param $op
70 * A string containing the name of the trackback operation.
71 * @return
72 * The returned value of the invoked hooks.
73 */
74 function trackback_invoke_trackback(&$trackback, $op) {
75 $return = array();
76 foreach (module_implements('trackback') as $name) {
77 $function = $name .'_trackback';
78 $result = $function($trackback, $op);
79 if (isset($result)) {
80 if (is_array($result)) {
81 $return = array_merge($return, $result);
82 }
83 else {
84 $return[] = $result;
85 }
86 }
87 }
88 return $return;
89 }
90
91 function trackback_render($node) {
92 $result = db_query('SELECT * FROM {trackback_received} WHERE nid=%d AND status=1 ORDER BY created DESC', $node->nid);
93 if ($tb = db_fetch_object($result)) {
94 $link = (user_access('administer trackbacks') || node_access('update', $node));
95 $received = '';
96 do {
97 $received .= theme('trackback', $tb, $link ? module_invoke_all('link', 'trackback', $tb, FALSE) : array());
98 } while ($tb = db_fetch_object($result));
99 return theme('trackbacks', $received);
100 }
101 }
102
103 function trackback_form_alter(&$form, $form_state, $form_id) {
104 if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
105 $form['workflow']['trackback'] = array(
106 '#type' => 'radios',
107 '#title' => t('Trackbacks'),
108 '#options' => array(t('Disabled'), t('Enabled')),
109 '#default_value' => _trackback_node_type($form['#node_type']->type),
110 '#description' => t('Enable trackbacks for this node type.')
111 );
112 }
113 else if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
114 $node = $form['#node'];
115 if (_trackback_node_type($node->type)) {
116 $form['trackback'] = array(
117 '#type' => 'fieldset',
118 '#title' => t('Trackbacks'),
119 '#collapsible' => TRUE
120 );
121 $form['trackback']['can_receive'] = array(
122 '#type' => 'checkbox',
123 '#title' => t('Allow Trackbacks'),
124 '#default_value' => isset($node->can_receive) ? $node->can_receive : 1,
125 '#description' => t('Allow other posts to send trackbacks to this content.')
126 );
127 $form['trackback']['trackback_urls'] = array(
128 '#type' => 'textarea',
129 '#title' => t('Send Trackbacks'),
130 '#default_value' => isset($node->trackback_urls) ? $node->trackback_urls : '',
131 '#cols' => 80,
132 '#rows' => 4,
133 '#description' => t('Enter one URL per line for each trackback you wish to send.')
134 );
135
136 // if there are any past successful trackbacks from this posting, add them to the node editing page.
137 // if there are any past unsuccessful trackbacks from this posting, add checkmarks to enable resending them
138 $past_successes_listing = array();
139 $options = array();
140 if (isset($node->nid)) {
141 $result = db_query('SELECT url, successful FROM {trackback_sent} WHERE nid = %d', $node->nid);
142 while ($url = db_fetch_object($result)) {
143 if ($url->successful) {
144 $past_successes_listing[] = $url->url;
145 }
146 else {
147 $options[$url->url] = $url->url;
148 }
149 }
150 }
151
152 // add listing of successfully trackbacked URLs
153 if (count($past_successes_listing)) {
154 $form['trackback'][] = array(
155 '#value' => theme('item_list', $past_successes_listing, t('Successful URLs'))
156 );
157 //t('These URLs have been successfuly pinged by this post.')
158 }
159
160 // add listing of unsuccessfully trackbacked URLs
161 if (count($options)) {
162 $form['trackback']['trackback_urls_to_retry'] = array(
163 '#type' => 'checkboxes',
164 '#title' => t('Unsuccessful URLs'),
165 '#default_value' => array(),
166 '#options' => $options,
167 '#description' => t('Attempts to ping these URLs with this post have failed. Mark a check next to the trackback URLs you wish to retry for this post.')
168 );
169 }
170 }
171 }
172 }
173
174 function trackback_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
175 switch ($op) {
176 case 'load':
177 if (_trackback_node_type($node->type)) {
178 $q = db_fetch_object(db_query('SELECT * FROM {trackback_node} WHERE nid=%d', $node->nid));
179 return array('can_receive' => $q ? $q->can_receive : 1);
180 }
181 break;
182
183 case 'alter':
184 if (empty($node->in_preview)) {
185 if (!empty($node->can_receive)) {
186 $url = url('node/'. $node->nid, array('absolute' => TRUE));
187 $tb_url = url('trackback/'. $node->nid, array('absolute' => TRUE));
188 $autodetect = "\n<!--\n";
189 $autodetect .= '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">'."\n";
190 $autodetect .= '<rdf:Description rdf:about="'. $url .'" dc:identifier="'. $url .'" dc:title="'. strtr(check_plain($node->title), array('--' => '&mdash;')) .'" trackback:ping="'. $tb_url .'" />'."\n";
191 $autodetect .= '</rdf:RDF>';
192 $autodetect .= "\n-->\n";
193 if ($teaser) {
194 $node->teaser .= $autodetect;
195 }
196 else {
197 $node->trackback = array(
198 'autodetect' => $autodetect,
199 'url' => theme('trackback_url', $tb_url)
200 );
201 }
202 }
203 if (!$teaser && variable_get('trackback_view', 0) == 0) {
204 $node->trackback['received'] = trackback_render($node);
205 }
206 if (!empty($node->trackback)) {
207 $node->body .= implode('', $node->trackback);
208 }
209 }
210 break;
211
212 case 'validate':
213 if (!empty($node->trackback_urls)) {
214 foreach (explode("\n", $node->trackback_urls) as $url) {
215 $url = trim($url);
216 if ($url && !_trackback_valid_url($url)) {
217 form_set_error('trackback_urls', t('The trackback url %url is not a valid url.', array('%url' => $url)));
218 }
219 }
220 }
221 break;
222
223 case 'insert':
224 case 'update':
225 if (_trackback_node_type($node->type)) {
226 include_once TRACKBACK_PATH .'/trackback.ping.inc';
227 register_shutdown_function('_trackback_send', $node);
228 $cron = ($node->status && variable_get('trackback_auto_detection_enabled', 0) == 2);
229 db_lock_table('trackback_node');
230 db_query("UPDATE {trackback_node} SET awaiting_cron=%d, can_receive=%d WHERE nid=%d", $cron, $node->can_receive, $node->nid);
231 if (!db_affected_rows()) {
232 db_query("INSERT INTO {trackback_node}(nid, awaiting_cron, can_receive) VALUES(%d, %d, %d)", $node->nid, $cron, $node->can_receive);
233 }
234 db_unlock_tables();
235 }
236 break;
237
238 case 'delete':
239 if (TRACKBACK_WITH_SPAM) {
240 db_query("DELETE FROM {spam_tracker} USING ({spam_tracker}, {trackback_received}) WHERE {spam_tracker}.source='trackback' AND {spam_tracker}.id={trackback_received}.trid AND {trackback_received}.nid=%d", $node->nid);
241 }
242 db_query("DELETE FROM {trackback_node} WHERE nid=%d", $node->nid);
243 db_query("DELETE FROM {trackback_sent} WHERE nid=%d", $node->nid);
244 db_query("DELETE FROM {trackback_received} WHERE nid=%d", $node->nid);
245 break;
246 }
247 }
248
249 function trackback_link($type, $node = NULL, $teaser = FALSE) {
250 $links = array();
251 switch ($type) {
252 case 'node':
253 if ($teaser) {
254 $count = db_result(db_query("SELECT count(*) FROM {trackback_received} WHERE nid=%d AND status=1", $node->nid));
255 if ($count) {
256 $links[] = array(
257 'title' => format_plural($count, '1 trackback', '@count trackbacks'),
258 'href' => _trackback_path($node),
259 'fragment' => 'trackbacks'
260 );
261 }
262 }
263 break;
264
265 case 'trackback':
266 if (!$teaser) {
267 $links['trackback_edit'] = array(
268 'title' => t('edit'),
269 'href' => 'trackback/edit/'. $node->trid
270 );
271 }
272 $links['trackback_delete'] = array(
273 'title' => t('delete'),
274 'href' => 'trackback/delete/'. $node->trid
275 );
276 if (TRACKBACK_WITH_SPAM) {
277 $links += _trackback_spam_link($node);
278 }
279 break;
280 }
281 return $links;
282 }
283
284 function trackback_menu() {
285 $items['admin/content/trackback'] = array(
286 'title' => 'Trackbacks',
287 'description' => 'List and edit site trackbacks and the trackback moderation queue.',
288 'page callback' => 'drupal_get_form',
289 'page arguments' => array('trackback_admin_overview'),
290 'access arguments' => array('administer trackbacks'),
291 'file' => 'trackback.admin.inc'
292 );
293
294 // Tabs:
295 $items['admin/content/trackback/list'] = array(
296 'title' => 'List',
297 'type' => MENU_DEFAULT_LOCAL_TASK,
298 'weight' => -10
299 );
300
301 // Subtabs:
302 $items['admin/content/trackback/list/new'] = array(
303 'title' => 'Published trackbacks',
304 'type' => MENU_DEFAULT_LOCAL_TASK,
305 'weight' => -10
306 );
307 $items['admin/content/trackback/list/approval'] = array(
308 'title' => 'Approval queue',
309 'page arguments' => array('trackback_admin_overview', 'approval'),
310 'type' => MENU_LOCAL_TASK
311 );
312 $items['admin/content/trackback/list/spam'] = array(
313 'title' => 'Spam',
314 'page arguments' => array('trackback_admin_overview', 'spam'),
315 'access callback' => '_trackback_spam_access',
316 'weight' => 10,
317 'type' => MENU_LOCAL_TASK
318 );
319 $items['admin/content/trackback/settings'] = array(
320 'title' => 'Settings',
321 'page arguments' => array('trackback_configure'),
322 'weight' => 10,
323 'type' => MENU_LOCAL_TASK
324 );
325
326 // Other stuff:
327 $items['trackback/%node'] = array(
328 'title' => 'Trackbacks',
329 'page callback' => 'trackback_page',
330 'page arguments' => array(1),
331 'access callback' => '_trackback_access',
332 'access arguments' => array('receive', 1),
333 'type' => MENU_CALLBACK,
334 'file' => 'trackback.ping.inc'
335 );
336 $items['trackback/edit/%trackback'] = array(
337 'title' => 'Moderate trackback',
338 'page callback' => 'drupal_get_form',
339 'page arguments' => array('trackback_edit', 2),
340 'access callback' => '_trackback_access',
341 'access arguments' => array('edit', NULL, 2),
342 'type' => MENU_CALLBACK,
343 'file' => 'trackback.admin.inc'
344 );
345 $items['trackback/delete/%trackback'] = array(
346 'title' => 'Delete trackback',
347 'page callback' => 'drupal_get_form',
348 'page arguments' => array('trackback_delete', 2),
349 'access callback' => '_trackback_access',
350 'access arguments' => array('edit', NULL, 2),
351 'type' => MENU_CALLBACK,
352 'file' => 'trackback.admin.inc'
353 );
354 $items['node/%node/trackback'] = array(
355 'title' => 'Trackbacks',
356 'page callback' => 'trackback_view_page',
357 'page arguments' => array(1),
358 'access callback' => '_trackback_access',
359 'access arguments' => array('page', 1),
360 'weight' => 2,
361 'type' => MENU_LOCAL_TASK
362 );
363 return $items;
364 }
365
366 function trackback_perm() {
367 return array('administer trackbacks');
368 }
369
370 function _trackback_access($op, $node, $trackback = NULL) {
371 switch ($op) {
372 case 'page':
373 if (variable_get('trackback_view', 0) != 1) {
374 return FALSE;
375 }
376 case 'view':
377 return node_access('view', $node) && db_result(db_query('SELECT count(*) FROM {trackback_received} WHERE nid=%d AND status=1', $node->nid));
378 case 'edit':
379 if (user_access('administer trackbacks')) {
380 return TRUE;
381 }
382 if (empty($node) && !empty($trackback->nid)) {
383 $node = node_load($trackback->nid);
384 }
385 return node_access('update', $node);
386 case 'receive':
387 return !empty($node->can_receive) && node_access('view', $node);
388 }
389 }
390
391 function _trackback_spam_access() {
392 return TRACKBACK_WITH_SPAM && user_access('administer trackbacks');
393 }
394
395 function trackback_load($trid) {
396 return db_fetch_object(db_query('SELECT * FROM {trackback_received} WHERE trid=%d', $trid));
397 }
398
399 function trackback_view_page($node) {
400 drupal_set_title(check_plain($node->title));
401 return trackback_render($node);
402 }
403
404 function trackback_cron() {
405 // query for all nodes where
406 $result = db_query('SELECT t.nid, n.status FROM {trackback_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE t.awaiting_cron = 1');
407
408 while ($node = db_fetch_object($result)) {
409 // First things first, let's unset the 'awaiting_cron' bit in the {trackback_node} table.
410 db_query('UPDATE {trackback_node} SET awaiting_cron = 0 WHERE nid = %d', $node->nid);
411 if ($node->status) {
412 include_once TRACKBACK_PATH .'/trackback.ping.inc';
413 $node = node_load($node->nid);
414 _trackback_ping($node, trackback_urls_via_nodebody($node));
415 }
416 }
417 }
418
419 function trackback_block($op = 'list', $delta = 0, $edit = array()) {
420 if ($op == 'list') {
421 $blocks[0]['info'] = t('Recent trackbacks');
422 if (variable_get('trackback_view', 0) == 2) {
423 $blocks[1]['info'] = t('Trackbacks');
424 $blocks[1]['cache'] = BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_ROLE;
425 }
426 return $blocks;
427 }
428
429 if ($delta == 0) {
430 switch ($op) {
431 case 'configure':
432 $form['trackback_display_number'] = array(
433 '#type' => 'select',
434 '#title' => t('Number of trackbacks to display'),
435 '#default_value' => variable_get('trackback_display_number', 10),
436 '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
437 '#description' => t('How many trackbacks are displayed in the recent trackbacks block')
438 );
439 return $form;
440
441 case 'save':
442 variable_set('trackback_display_number', $edit['trackback_display_number']);
443 break;
444
445 case 'view':
446 $result = db_query_range('SELECT * FROM {trackback_received} WHERE status=1 ORDER BY created DESC', 0, variable_get('trackback_display_number', 10));
447 $items = array();
448 while ($tb = db_fetch_object($result)) {
449 $items[] = _trackback_path($tb, truncate_utf8($tb->subject, 128, FALSE, TRUE)) .'<br />'. t('%time ago', array('%time' => format_interval(time() - $tb->created)));
450 }
451 $block['subject'] = t('Recent trackbacks');
452 $block['content'] = theme('item_list', $items);
453 return $block;
454 }
455 }
456 else if ($op == 'view' && variable_get('trackback_view', 0) == 2) {
457 if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
458 $node = node_load(arg(1));
459 if (_trackback_access('view', $node)) {
460 $block['subject'] = t('Trackbacks');
461 $block['content'] = trackback_render($node);
462 return $block;
463 }
464 }
465 }
466 }
467
468 function _trackback_valid_url($url) {
469 $uc = '[a-z0-9;/?:@&=+$,_.!~*\'()%-]';
470 return preg_match('`^(http|https)://'. $uc .'+(#'. $uc .'*)?$`i', $url);
471 }
472
473 // Code that adds configurability for trackback features.
474 function _trackback_node_type($type) {
475 static $types = array('story', 'forum', 'blog');
476 return variable_get('trackback_'. $type, in_array($type, $types) ? 1 : 0);
477 }
478
479 function _trackback_path($obj, $text = NULL, $opt = array()) {
480 static $prefix;
481
482 if (!isset($prefix)) {
483 $prefix = variable_get('trackback_view', 0) == 1 ? '/trackback' : '';
484 }
485 if (!isset($text)) {
486 return 'node/'. $obj->nid . $prefix;
487 }
488 if ($obj->status) {
489 $opt += array('fragment' => 'trackback-'. $obj->trid);
490 return l($text, 'node/'. $obj->nid . $prefix, $opt);
491 }
492 return l($text, 'trackback/edit/'. $obj->trid, $opt);
493 }
494
495 function _trackback_operation($action, $trackback, $clear = TRUE) {
496 if (is_numeric($trackback)) {
497 $trackback = trackback_load($trackback);
498 }
499 if ($trackback) {
500 switch ($action) {
501 case 'publish':
502 db_query('UPDATE {trackback_received} SET status=1 WHERE trid=%d', $trackback->trid);
503 $trackback->status = 1;
504 trackback_invoke_trackback($trackback, $action);
505 watchdog('trackback', 'Published trackback %subject.', array('%subject' => $trackback->subject), WATCHDOG_NOTICE, _trackback_path($trackback, t('view')));
506 break;
507
508 case 'unpublish':
509 db_query('UPDATE {trackback_received} SET status=0 WHERE trid=%d', $trackback->trid);
510 $trackback->status = 0;
511 trackback_invoke_trackback($trackback, $action);
512 watchdog('trackback', 'Unpublished trackback %subject.', array('%subject' => $trackback->subject), WATCHDOG_NOTICE, _trackback_path($trackback, t('view')));
513 break;
514
515 case 'delete':
516 db_query('DELETE FROM {trackback_received} WHERE trid=%d', $trackback->trid);
517 trackback_invoke_trackback($trackback, $action);
518 watchdog('trackback', 'Deleted trackback %subject. The trackback was posted to !link.', array('%subject' => $trackback->subject, '!link' => l(url('node/'. $trackback->nid, array('absolute' => TRUE)), 'node/'. $trackback->nid)));
519 break;
520
521 default:
522 $action($trackback);
523 break;
524 }
525 if ($clear) {
526 cache_clear_all();
527 }
528 }
529 return $trackback;
530 }

  ViewVC Help
Powered by ViewVC 1.1.2