summaryrefslogtreecommitdiffstats
path: root/multiping.module
blob: 6ecd47a6249df23f1fa38af6fcf2a68010f5ba2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php

define("_multiping_when_active", 1);
define("_multiping_when_taxonomy", 2);
define("_multiping_when_nodetype", 4);
define("_multiping_when_frontpage", 8);


function _multiping_get_settings($defaults = FALSE) {
  static $settings = NULL;
  static $default_settings = array(
  'global' => array(
    'time_between_pings' => '10',
    'pingatonce' => false,
    ),
  );
  // Defaults requested?
  if ($defaults) {
    return $default_settings;
  }
  // merge settings from variable table with defaults
  if ($settings == NULL) {
    $settings = variable_get('multiping', array());
    foreach ($default_settings as $key => $value) {
      if (is_array($value)) {
        $settings[$key] = isset($settings[$key]) ? array_merge($value, $settings[$key]) : $value;
      }
      else if (!isset($settings[$key])) {
        $settings[$key] = $value;
      }
    }
  }
  // Return result
  return $settings;
}


/**
 * Implementation of hook_menu().
 */
function multiping_menu() {
  $items = array();
  $items['admin/settings/multiping'] = array( 
    'title' => 'Ping services',
    'description' => 'Configure when to ping which sites',
    'page callback' => 'multiping_admin',
    'page arguments' => array(),
    'access callback' => 'user_access',
    'access arguments' => array('admin pings'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/settings/multiping/pingall'] = array(
    'title' => 'Ping all',
    'page callback' => 'multiping_pingall',
    'access arguments' => array('admin pings'),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/settings/multiping/new'] = array(
    'title' => 'New ping service',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('multiping_edit_service', '0'),
    'access arguments' => array('admin pings'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/multiping/%/edit'] = array(
    'title' => 'Edit ping service',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('multiping_edit_service', 3),
    'access arguments' => array('admin pings'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/multiping/%/ping'] = array(
    'title' => 'Ping service',
    'page arguments' => array(3),
    'page callback' => 'multiping_ping',
    'access arguments' => array('admin pings'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/multiping/%/delete'] = array(
    'title' => 'Delete ping service',
    'page arguments' => array(3),
    'page callback' => 'multiping_delete',
    'access arguments' => array('admin pings'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
  * Implementation of hook_perm().
  */
function multiping_perm() {
  return array('admin pings');
}

function _multiping_doping($pingservice) {
  global $base_url;

  // Assemble site name
  $name=variable_get('site_name', '');
  $slogan=variable_get('site_slogan', '');
  if (strlen($slogan)>0)
    $name="$name - $slogan";
  // Determine rss url
  if (module_exists('taxonomy') 
    && ($pingservice->whentoping & _multiping_when_taxonomy)
    && count($pingservice->voc)>0 && $pingservice->submitmainrss==0) {
    // Taxonomy module exists, "ping only for nodes with taxonomy",
    // Some taxonomy elements are actually selected, "main rss" override is off
    $voc=unserialize($pingservice->voc);
    $rss_url="$base_url/taxonomy/term/".implode("+", $voc)."/0/feed";
  } 
  else {
    $rss_url="$base_url/rss.xml";
  }
  // Ping! Check service method
  if ($pingservice->method && strlen($pingservice->method)>0) {
    //watchdog("Multiping","XML ping: ".$pingservice->url);
    $result = xmlrpc($pingservice->url, $pingservice->method, $name, $base_url, $rss_url);
  } 
  else {
    $pingurl=strtr($pingservice->url, array(
      "%name" => urlencode($name),
      "%url" => urlencode("$base_url/"),
      "%rss" => urlencode($rss_url))
    );
    $result = drupal_http_request($pingurl);
    //watchdog("Multiping","Non-XML ping: ".$pingurl." Result: ".$result->code." Data: '".$result->data."'");
    if ($result->code==200) {
      if (strpos($result->data, "1")==0)
        $result = TRUE; else
        $result = FALSE;
    } 
    else $result = FALSE;
  }
  // On success: Update timestamp
  if ($result)
    db_query("UPDATE {multiping} SET lastping=%d WHERE id=%d", time(), $pingservice->id);
  return $result;
}


function _multiping_checkpings() {
  $settings=_multiping_get_settings();
  $result = db_query('SELECT * FROM {multiping}');
  while ($row = db_fetch_object($result)) {
    // Ping service active?
    if (! ($row->whentoping & _multiping_when_active)) {
      continue;
    }
    // Default query: Published nodes only
    $sql_from="{node} n";
    $sql_where="n.status>0";
    // Taxonomy selection
    if (module_exists('taxonomy') && ($row->whentoping & _multiping_when_taxonomy)) {
      // Find most recent date for nodes with given taxonomy
      $voc=unserialize($row->voc);
      $sql_from="$sql_from, {term_node} t";
      $sql_where="$sql_where AND t.nid=n.nid AND (t.tid=" . implode(" OR t.tid=", $voc) . ")";
    }
    // Nodetype selection
    if ($row->whentoping & _multiping_when_nodetype) {
      $nodetype=unserialize($row->nodetypes);
      $sql_where="$sql_where AND (n.type='".implode("' OR n.type='", $nodetype)."')";
    }
    // Promoted to front page?
    if ($row->whentoping & _multiping_when_frontpage) {
      $sql_where="$sql_where AND n.promote>0";
    }
    //watchdog("debug","Multiping ".$row->name.": SELECT * FROM ".$sql_from." WHERE ".$sql_where);
    // Get max creation and modification time
    $max_created = 0;
    $res_time = db_query('SELECT max(created) FROM {'.$sql_from.'} WHERE '.$sql_where);
    $row_time = db_fetch_array($res_time);
    if ($row_time) $max_created=$row_time['max(created)'];
    $max_changed = 0;
    $res_time = db_query('SELECT max(changed) FROM {'.$sql_from.'} WHERE '.$sql_where);
    $row_time = db_fetch_array($res_time);
    if ($row_time) $max_changed=$row_time['max(changed)'];
    if ($max_created>$max_changed)
      $lastmodified = $max_created; else
      $lastmodified = $max_changed;
    //watchdog("Multiping","lastmodified=".$lastmodified);
    // Check wether to ping service
    if ($lastmodified>$row->lastping) {
      // With every failure, exponentially increase retry time
      $retry_time=$settings['global']['time_between_pings']*pow(1.5,$row->failcount);
      if ($retry_time>24*60)  // Retry at least once a day
	$retry_time=24*60;
      if ($row->lastping+60*$retry_time>time()) {
	// Timeout not yet reached
	watchdog("Multiping", "Timeout for service @name not yet reached", array('@name' => $row->name));
      } elseif (_multiping_doping($row)) {
	watchdog("Multiping", 'Successfully notified %site.', array('%site' => $row->name));
	db_query("UPDATE {multiping} SET failcount='0' WHERE id=%d",$row->id);
      } else {
	watchdog("Multiping", 'Failed to notify %site.', array('%site' => $row->name), WATCHDOG_WARNING);
	db_query("UPDATE {multiping} SET failcount='%d' WHERE id=%d",
	  ($row->failcount)+1,$row->id);
      }
    }
  }
}


/**
  * Implementation of hook_cron().
  * Check if sites need to be pinged.
  */
function multiping_cron() {
  watchdog("Multiping","Cron run",array(),WATCHDOG_DEBUG);
  _multiping_checkpings();
}


/**
  * Implementation of hook_nodeapi().
  * If pingatonce is set, check pings.
  */
function multiping_nodeapi(&$node, $op, $a3=NULL, $a4=NULL) {
  //drupal_set_message("op=".$op);
  //watchdog("Multiping","op=$op");
  switch ($op) {
    case 'insert':
    case 'update':
      $settings=_multiping_get_settings();
      if ($settings['global']['pingatonce'])
	_multiping_checkpings();
      break;
  }
}


function multiping_edit_service($form_state, $id) {
  $edit=array();
  $edit['id']=$id;
  $edit['name']='';
  $edit['url']='http://';
  $edit['method']='weblogUpdates.ping';
  $edit['when_active']=1;
  $edit['when_taxonomy']=0;
  $edit['when_nodetype']=0;
  $edit['when_frontpage']=0;
  $edit['submitmainrss']=1;
  $edit['voc']='';
  $edit['nodetype']='';
  if ($id>0) {
    $result = db_query('SELECT * FROM {multiping} WHERE id=%d',$id);
    $row = db_fetch_object($result);
    if ($row) {
      $edit['name']=$row->name;
      $edit['url']=$row->url;
      $edit['method']=$row->method;
      $edit['when_active']=(($row->whentoping & _multiping_when_active) > 0);
      $edit['when_taxonomy']=(($row->whentoping & _multiping_when_taxonomy) > 0);
      $edit['when_nodetype']=(($row->whentoping & _multiping_when_nodetype) > 0);
      $edit['when_frontpage']=(($row->whentoping & _multiping_when_frontpage) > 0);
      $edit['submitmainrss']=$row->submitmainrss;
      $edit['voc']=unserialize($row->voc);
      $edit['nodetype']=unserialize($row->nodetypes);
    } else {
      watchdog("Multiping","Query for id=@id returned 0 rows", array('@id' => $id), WATCHDOG_WARNING);
      $id=0;
    }
  }
  $form=array();
  $form['id'] = array(
    '#type' => 'hidden',
    '#value' => $id,
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Site name'),
    '#default_value' => $edit['name'],
    '#size' => 50,
    '#maxlength' => 100,
    '#description' => t('Displayed name of the site to be pinged'),
    '#attributes' => NULL,
    '#required' => TRUE,
  );
  $form['url'] = array(
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#default_value' => $edit['url'],
    '#size' => 50,
    '#maxlength' => 255,
    '#description' => t('URL of the ping service. '.
      'The following replacements will be done:<br />'.
      '%name - Site name (and slogan)<br />'.
      '%url - Site base URL<br />'.
      '%rss - URL of RSS feed'),
    '#attributes' => NULL,
    '#required' => TRUE,
  );
  /* Todo: Submit which RSS feed? Whole feed or a feed containing only
     items of the selected taxonomies? */
  $form['method'] = array(
    '#type' => 'textfield',
    '#title' => t('Method name'),
    '#default_value' => $edit['method'],
    '#size' => 50,
    '#maxlength' => 50,
    '#description' => t('If this is an XML service: Name of the method to be called'),
    '#attributes' => NULL,
    '#required' => FALSE,
  );
  $options = array('1' => t('Always ping'), '0' => t('Don\'t ping'));
  if (module_exists('taxonomy')) {
   $options['2']=t('Ping only for nodes with the following taxonomy');
  }
  $form['when_active'] = array(
    '#type' => 'checkbox',
    '#title' => t('Service active'),
    '#default_value' => $edit['when_active'],
    '#description' => t('Activate or deactivate this service'),
  );
  if (module_exists('taxonomy')) {
    $form['when_taxonomy'] = array(
      '#type' => 'checkbox',
      '#title' => t('Ping only for selected taxonomies'),
      '#default_value' => $edit['when_taxonomy'],
      '#description' => t('Send a ping only if the node belongs to one of the follwing categories'),
    );
    $form['voc'] = array(
      '#type' => 'select',
      '#title' => t('Taxonomy terms when to ping'),
      '#default_value' => $edit['voc'],
      '#options' => taxonomy_form_all(),
      '#multiple' => true,
      '#description' => t('Ping this service if any of the taxonomy terms selected above matches the respective node'),
    );
    $form['submitmainrss'] = array(
      '#type' => 'checkbox',
      '#title' => t('Send main RSS feed in ping'),
      '#default_value' => $edit['submitmainrss'],
      '#description' => t('If checked, the main RSS feed is sent; otherwise, an RSS feed containing only articles from the selected taxonomies is used.'),
    );
  }
  $form['when_nodetype'] = array(
    '#type' => 'checkbox',
    '#title' => t('Ping only for selected node types'),
    '#default_value' => $edit['when_nodetype'],
    '#description' => t('Send a ping only if the node is one of the following node types'),
  );
  $form['nodetype'] = array(
    '#type' => 'select',
    '#title' => t('Node types when to ping'),
    '#default_value' => $edit['nodetype'],
    '#options' => node_get_types('names'),
    '#multiple' => true,
    '#description' => t('Ping this service if any of the node types selected above matches the respective node'),
  );
  $form['when_frontpage'] = array(
    '#type' => 'checkbox',
    '#title' => t('Ping only for nodes promoted to front page'),
    '#default_value' => $edit['when_frontpage'],
    '#description' => t('Send a ping only if the node is promoted to the front page'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}


function multiping_edit_service_submit($form_id, &$form_state) {
  $edit = $form_state['values']; // TODO: Is this ok? http://drupal.org/node/144132#process-params
  if ($edit['form_id']=='multiping_edit_service') {
    $edit['id'] = ($edit['id'] && is_numeric($edit['id'])) ? $edit['id'] : 0;
    $edit['name'] = ($edit['name']) ? $edit['name'] : '(no name)';
    $edit['url'] = ($edit['url']) ? $edit['url'] : 'http://localhost/';
    $edit['method'] = ($edit['method']) ? $edit['method'] : '';
    $edit['submitmainrss'] = ($edit['submitmainrss']) ? $edit['submitmainrss'] : 0; // Not present if false!
    if ($edit['submitmainrss']>0) $edit['submitmainrss']=1;
    $whentoping=$edit['when_active']*_multiping_when_active+
      $edit['when_taxonomy']*_multiping_when_taxonomy+
      $edit['when_nodetype']*_multiping_when_nodetype+
      $edit['when_frontpage']*_multiping_when_frontpage;
    if ($edit['id']==0) {
      db_query("INSERT INTO {multiping} (name,url,method,whentoping,submitmainrss,voc,nodetypes) ".
	"VALUES ('%s','%s','%s','%s','%s','%s','%s')",$edit['name'],$edit['url'],
	$edit['method'],$whentoping,$edit['submitmainrss'],
	serialize($edit['voc']),serialize($edit['nodetype']));
    } else {
      db_query("UPDATE {multiping} SET name='%s',url='%s',method='%s',whentoping='%s',submitmainrss='%s',voc='%s',nodetypes='%s' ".
	"WHERE id=%d",$edit['name'],$edit['url'],$edit['method'],$whentoping,$edit['submitmainrss'],serialize($edit['voc']),serialize($edit['nodetype']),$edit['id']);
    }
  }
  drupal_set_message("Changes saved.");
  #drupal_goto("admin/settings/multiping");
  $form_state['redirect']="admin/settings/multiping";
}


function multiping_settings() {
  $form=array();
  $settings=_multiping_get_settings();
  $form['global']['time_between_pings'] = array(
     '#type' => 'textfield',
     '#title' => t('Time between pings'),
     '#default_value' => $settings['global']['time_between_pings'],
     '#size' => 8,
     '#maxlength' => 4,
     '#description' => t('Minimum number of minutes between two pings to same site'),
     '#attributes' => NULL,
     '#required' => TRUE,
  );
  $form['pingatonce'] = array(
    '#type' => 'checkbox',
    '#title' => t('Ping after post'),
    '#default_value' => $settings['global']['pingatonce'],
    '#description' => t('Ping directly after post/update (instead of pinging during the cron job runs)'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}


function multiping_settings_submit($form_id, &$form_state) {
  $edit = $form_state['values']; // TODO: Is this ok? http://drupal.org/node/144132#process-params
  if ($edit['form_id']=='multiping_settings') {
    $settings=_multiping_get_settings();
    $settings['global']['time_between_pings']=$edit['time_between_pings'];
    $settings['global']['pingatonce']=$edit['pingatonce'];
    variable_set('multiping', $settings);
  }
  drupal_set_message("Changes saved.");
  #drupal_goto("admin/settings/multiping");
  $form_state['redirect']="admin/settings/multiping";
}


/**
  * Menu callback: Admin page
  */
function multiping_admin() {
  $output = "";
  // General settings
  $output .= drupal_get_form('multiping_settings', $form);
  // Table with services
  $header = array(t('Name'), t('Last update'), array('data' => t('Operations'), 'colspan' => '3'));
  $result = db_query('SELECT * FROM {multiping} ORDER BY id');
  while ($row = db_fetch_object($result)) {
    if ($row->lastping==0)
      $lastping=t('never'); else
      $lastping=format_date($row->lastping,'small');
    $rows[] = array(
      $row->name,
      $lastping,
      l(t('edit'),"admin/settings/multiping/".$row->id."/edit"),
      l(t('ping'),"admin/settings/multiping/".$row->id."/ping"),
      l(t('delete'),"admin/settings/multiping/".$row->id."/delete")
    );
  }
  $output .= theme('table',$header,$rows);
  // Links
  $output .= "<p>".l(t('Add service'),"admin/settings/multiping/new")."</p>";
  return $output;
}


function multiping_pingall() {
  $output = "<p>".t("Running all pings...")."</p>";
  $header = array(t('Name'), t('Status'));
  $result = db_query('SELECT * FROM {multiping} ORDER BY id');
  while ($row = db_fetch_object($result)) {
    $rows[] = array($row->name,_multiping_doping($row) ? t('Ok') : t('Failed'));
  }
  $output .= theme('table',$header,$rows);
  $output .= "<p>".l(t('Return'),"admin/settings/multiping")."</p>";
  return $output;
}


function multiping_ping($id) {
  watchdog("Multiping","ping @id", array('@id' => $id));
  if (!is_numeric($id)) {
    drupal_not_found();
    return;
  }
  drupal_set_message("Not yet implemented","warning");
  drupal_goto("admin/settings/multiping");
}


function multiping_delete($id) {
  watchdog("Multiping","delete @id", array('@id' => $id));
  if (!is_numeric($id)) {
    drupal_not_found();
    return;
  }
  db_query("DELETE FROM {multiping} WHERE id=%d",intval($id));
  drupal_set_message("Service deleted.");
  drupal_goto("admin/settings/multiping");
}

?>