summaryrefslogtreecommitdiffstats
path: root/modules/drupal.module
blob: 20c9d9cf23f6cef5c4ff40a8dcd8ae5db0a64846 (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
<?php
// $Id$

/**
 * @file
 * Lets users log in using a Drupal ID and can notify a central server about your site.
 */

/**
 * Implementation of hook_help().
 */
function drupal_help($section) {
  switch ($section) {
    case 'admin/help#drupal':
      $output = '<p>'. t('The Drupal module uses the XML-RPC network communication protocol to connect your site with a central server that maintains a directory of client sites.') .'</p>';
      $output .= t('<p>Enabling the Drupal module will allow you to:</p>
<ul>
  <li>register your site with a server, including (optionally) posting information on your installed modules and themes and summary statistics on your number of posts and users, information that can help rank Drupal modules and themes</li>
  <li>enable other sites to register with your site</li>
  <li>allow members on all sites using the Drupal module to log in to your site without registering using their distributed identification</li>
  <li>allow members to log in to any other site that uses the Drupal module, using a login name that looks much like an e-mail address: <em>username@example.com</em></li>
</ul>
');
      $output .= '<p>'. t('The Drupal module administration page allows you to set the xml-rpc server page and other related options.') .'</p>';
      $output .= t('<p>You can</p>
<ul>
  <li>run your cron job at your site\'s <a href="%file-cron">cron page</a></li>
  <li>view your <a href="%file-xmlrpc">XML-RPC page</a>.</li>
  <li>administer Drupal <a href="%admin-settings-drupal">administer &gt;&gt; settings &gt;&gt; drupal</a>.</li>
</ul>
', array('%file-cron' => 'cron.php', '%file-xmlrpc' => 'xmlrpc.php', '%admin-settings-drupal' => url('admin/settings/drupal')));
      $output .= '<p>'. t('If you maintain a directory of sites, you can list them on a page using the <code>drupal_client_page()</code> function. Sample instructions:
<ul>
  <li>Enable the page module. Select create content &gt;&gt; page.</li>
  <li>For input format, select PHP code.</li>
  <li>Give the page a title. For body, put:
<pre>
&lt;?php
print drupal_client_page();
?&gt;
</pre>
  <li>Save the page.</li>
</ul>') . '</p>';

      $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%drupal">Drupal page</a>.', array('%drupal' => 'http://drupal.org/handbook/modules/drupal/')) .'</p>';
      return $output;
    case 'admin/modules#description':
      return t('Lets you register your site with a central server and improve ranking of Drupal projects by posting information on your installed modules and themes; also enables users to log in using a Drupal ID.');
    case 'admin/settings/drupal':
      return t('<p>Using this your site can "call home" to another Drupal server. By calling home to drupal.org and sending a list of your installed modules and themes, you help rank projects on drupal.org and so assist all Drupal administrators to find the best components for meeting their needs. If you want to register with a different server, you can change the Drupal XML-RPC server setting -- but the server has to be able to handle Drupal XML. Some XML-RPC servers may present directories of all registered sites. To get all your site information listed, go to the <a href="%site-settings">settings page</a> and set the site name, the e-mail address, the slogan, and the mission statement.</p>', array('%site-settings' => url('admin/settings')));
    case 'user/help#drupal':
      return variable_get('drupal_authentication_service', 0) ? t("<p><a href=\"%Drupal\">Drupal</a> is the name of the software that powers %this-site. There are Drupal web sites all over the world, and many of them share their registration databases so that users may freely log in to any Drupal site using a single <strong>Drupal ID</strong>.</p>
<p>So please feel free to log in to your account here at %this-site with a username from another Drupal site. The format of a Drupal ID is similar to an e-mail address: <strong>username</strong>@<em>server</em>. An example of a valid Drupal ID is <strong>mwlily</strong>@<em>drupal.org</em>.</p>", array('%Drupal' => 'http://drupal.org', '%this-site' => '<em>'. variable_get('site_name', 'this web site') .'</em>')) : '';
  }
}

/**
 * Implementation of hook_settings().
 */
function drupal_settings() {
  // Check if all required fields are present
  if ((variable_get('site_name', 'drupal') == 'drupal') || (variable_get('site_name', 'drupal') == '')) {
    form_set_error('drupal_directory', t('You must set the name of your site on the <a href="%url">administer &raquo; settings</a> page.', array('%url' => url('admin/settings'))));
  }
  else if (variable_get('site_mail', ini_get('sendmail_from')) == '') {
    form_set_error('drupal_directory', t('You must set an e-mail address for your site on the <a href="%url">administer &raquo; settings</a> page.', array('%url' => url('admin/settings'))));
  }
  else if (variable_get('site_slogan', '') == '') {
    form_set_error('drupal_directory', t('You must set your site slogan on the <a href="%url">administer &raquo; settings</a> page.', array('%url' => url('admin/settings'))));
  }
  else if (variable_get('site_mission', '') == '') {
    form_set_error('drupal_directory', t('You must set your site mission on the <a href="%url">administer &raquo; settings</a> page.' , array('%url' => url('admin/settings'))));
  }
  $options = array('1' => t('Enabled'), '0' => t('Disabled'));

  $form['drupal'] = array(
    '#type' => 'fieldset',
    '#title' => t('Post data to another site'),
    '#tree' => FALSE
  );

  $form['drupal']['drupal_register'] = array(
    '#type' => 'radios',
    '#title' => t('Register with a Drupal server'),
    '#default_value' => variable_get('drupal_register', 0),
    '#options' => $options,
    '#description' => t("If enabled, your Drupal site will register itself with the specified Drupal XML-RPC server. For this to work properly, you must set your site's name, e-mail address, slogan and mission statement. When the \"Drupal XML-RPC server\" field is set to \"%drupal-xml-rpc\", your web site will register itself with drupal.org. Requires the cron feature to be enabled.", array("%drupal-xml-rpc" => "http://drupal.org/xmlrpc.php", "%drupal-sites" => "http://drupal.org/drupal-sites/"))
  );

  $form['drupal']['drupal_server'] =  array(
    '#type' => 'textfield',
    '#title' => t('Drupal XML-RPC server'),
    '#default_value' => variable_get('drupal_server', 'http://drupal.org/xmlrpc.php'),
    '#description' => t('The URL of the Drupal XML-RPC server you wish to register with.')
  );

  $form['drupal']['drupal_system'] = array(
    '#type' => 'radios',
    '#title' => t('Send system information'),
    '#default_value' => variable_get('drupal_system', 0),
    '#options' => $options,
    '#description' => t("If enabled, your site will send information on its installed components (modules, themes, and theme engines). This information can help in compiling statistics on usage of Drupal projects.")
  );

  $form['drupal']['drupal_statistics'] = array(
    '#type' => 'radios',
    '#title' => t('Send statistics'),
    '#default_value' => variable_get('drupal_statistics', 0),
    '#options' => $options,
    '#description' => t("If enabled, your site will send summary statistics on the number of registered users and the total number of posts. No private information will be sent. These data help to improve the ranking statistics of Drupal projects.")
  );

  $form['services'] = array(
    '#type' => 'fieldset',
    '#title' => t('Receive data from other sites'),
    '#tree' => FALSE
  );

  $form['services']['drupal_client_service'] = array(
    '#type' => 'radios',
    '#title' => t('Allow other Drupal sites to register'),
    '#default_value' => variable_get('drupal_client_service', 0),
    '#options' => $options,
    '#description' => t('If enabled, your Drupal site will allow other sites to register with your site and send information to this site. This functionality can be used to maintain a list of related sites.')
  );

  $form['services']['drupal_authentication_service'] = array(
    '#type' => 'radios',
    '#title' => t('Authentication service'),
    '#default_value' => variable_get('drupal_authentication_service', 0),
    '#options' => $options,
    '#description' => t('If enabled, your Drupal site will accept logins with the user names of other Drupal sites, and likewise provide authentication for users logging into other Drupal sites, based on their user accounts here.')
  );

  return $form;
}

/**
 * Implementation of hook_cron(); handles pings to and from the site.
 */
function drupal_cron() {
  if (time() - variable_get('cron_last', 0) > 21600) {

    // If this site acts as a Drupal XML-RPC server, delete the sites that
    // stopped sending "ping" messages.
    if (variable_get('drupal_client_service', 0)) {
      $result = db_query("SELECT cid FROM {client} WHERE changed < %d", time() - 259200);
      while ($client = db_fetch_object($result)) {
        db_query("DELETE FROM {client_system} WHERE cid = %d", $client->cid);
        db_query("DELETE FROM {client} WHERE cid = %d", $client->cid);
      }
    }

    // If this site acts as a Drupal XML-RPC client, send a message to the
    // Drupal XML-RPC server.
    if (variable_get('drupal_register', 0) && variable_get('drupal_server', 0)) {
      drupal_notify(variable_get('drupal_server', ''));
    }
  }
}

/**
 * Callback function from drupal_xmlrpc() called when another site pings this one.
 */
function drupal_client_ping($client, $system) {
  /*
  ** Parse our parameters:
  */

  foreach (array('link', 'name', 'mail', 'slogan', 'mission') as $key) {
    $client[$key] = strip_tags($client[$key]);
  }

  /*
  ** Update the data in our database and send back a reply:
  */

  if ($client['link'] && $client['name'] && $client['mail'] && $client['slogan'] && $client['mission']) {
    $result = db_query(db_rewrite_sql("SELECT cid FROM {client} WHERE link = '%s'"), $client['link']);
    if (db_num_rows($result)) {
      $record = db_fetch_object($result);
      $client['cid'] = $record->cid;
      // We have an existing record.
      db_query("UPDATE {client} SET link = '%s', name = '%s', mail = '%s', slogan = '%s', mission = '%s', users = %d, nodes = %d, version = '%s', changed = '%s' WHERE cid = %d", $client['uid'], $client['link'], $client['name'], $client['mail'], $client['slogan'], $client['mission'], $client['users'], $client['nodes'], $client['version'], time(), $client['cid']);
    }
    else {
      $client['cid'] = db_next_id('{client}_cid');
      db_query("INSERT INTO {client} (cid, link, name, mail, slogan, mission, users, nodes, version, created, changed) VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $client['cid'], $client['link'], $client['name'], $client['mail'], $client['slogan'], $client['mission'], $client['users'], $client['nodes'], $client['version'], time(), time());
    }
    if (is_array($system)) {
      db_query("DELETE FROM {client_system} WHERE cid = %d", $client['cid']);
      foreach ($system as $item) {
        db_query("INSERT INTO {client_system} (cid, name, type) VALUES (%d, '%s', '%s')", $client['cid'], $item['name'], $item['type']);
      }
    }
    watchdog('client ping', t('Ping from %name (%link).', array('%name' => theme('placeholder', $client['name']), '%link' => theme('placeholder', $client['link']))), WATCHDOG_NOTICE, '<a href="'. check_url($client['link']) .'">view</a>');

    return TRUE;
  }
  else {
    return 0;
  }

}

/**
 * Formats a list of all clients.
 *
 * This function may be called from a custom page on sites that are
 * Drupal directory servers.
 */
function drupal_client_page($sort = 'name') {
  $result = db_query('SELECT * FROM {client} ORDER BY %s', $sort);

  $clients = array();
  while ($client = db_fetch_object($result)) {
    $clients[] = $client;
  }

  return theme('client_list', $clients);
}

/**
 * Theme a client list.
 */
function theme_client_list($clients) {
  // Note: All fields except the mission are treated as plain-text.
  // The mission is stripped of any HTML tags to keep the output simple and consistent.
  $output = "\n<dl>\n";
  foreach ($clients as $client) {
    $output .= '  <dt><a href="' . check_url($client->link) . '">' . check_plain($client->name) . '</a> - ' . check_plain($client->slogan) . "</dt>\n";
    $output .= '  <dd>' . strip_tags($client->mission) . "</dd>\n";
  }
  $output .= "</dl>\n";
  return $output;
}

/**
 * Implementation of hook_xmlrpc().
 */
function drupal_xmlrpc() {
  $xmlrpc = array();
  if (variable_get('drupal_client_service', 0)) {
    $xmlrpc[] = array(
      'drupal.client.ping',
      'drupal_client_ping',
      array('array', 'array', 'array'),
      t('Handling ping request')
    );
  }
  if (variable_get('drupal_authentication_service', 0)) {
    $xmlrpc[] = array(
      'drupal.login',
      'drupal_login',
      array('int', 'string', 'string'),
      t('Logging into a Drupal site')
    );
  }
  return $xmlrpc;
}

/**
 * Sends a ping to the Drupal directory server.
 */
function drupal_notify($server) {
  global $base_url;
  $client = array(
    'link' => $base_url,
    'name' => variable_get('site_name', ''),
    'mail' => variable_get('site_mail', ''),
    'slogan' => variable_get('site_slogan', ''),
    'mission' => variable_get('site_mission', ''),
    'version' => VERSION
  );
  if (variable_get('drupal_system', 0)) {
    $system = array();
    $result = db_query("SELECT name, type FROM {system} WHERE status = 1");
    while ($item = db_fetch_array($result)) {
      $system[] = $item;
    }
  }
  if (variable_get('drupal_statistics', 0)) {
    $users = db_fetch_object(db_query("SELECT COUNT(uid) AS count FROM {users}"));
    $client['users'] = $users->count;
    $nodes = db_fetch_object(db_query("SELECT COUNT(nid) AS count FROM {node}"));
    $client['nodes'] = $nodes->count;
  }
  $result = xmlrpc($server, 'drupal.client.ping', $client, $system);

  if ($result === FALSE) {
    watchdog('server ping', t('Failed to notify %server; error code: %errno; error message: %error_msg.', array('%server' => theme('placeholder', $server), '%errno' => theme('placeholder', xmlrpc_errno()), '%error_msg' => theme('placeholder', xmlrpc_error_msg()))), WATCHDOG_WARNING);
  }
}

/**
 * Implementation of hook_info().
 */
function drupal_info($field = 0) {
  $info['name'] = 'Drupal';
  $info['protocol'] = 'XML-RPC';

  if ($field) {
    return $info[$field];
  }
  else {
    return $info;
  }
}

/**
 * Implementation of hook_auth().
 */
function drupal_auth($username, $password, $server) {
  if (variable_get('drupal_authentication_service', 0)) {
    $result = xmlrpc("http://$server/xmlrpc.php", 'drupal.login', $username, $password);
    if ($result === FALSE) {
      drupal_set_message(t('Error %code : %message', array('%code' => theme('placeholder', xmlrpc_errno()), '%message' =>  theme('placeholder', xmlrpc_error_msg()))), 'error');
    }
    else {
      return $result;
    }
  }
}

/**
 * Implementation of hook_menu().
 */
function drupal_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    if (variable_get('drupal_authentication_service', 0)) {
      $items[] = array('path' => 'drupal', 'title' => t('Drupal'),
        'callback' => 'drupal_page_help', 'access' => TRUE,
        'type' => MENU_SUGGESTED_ITEM
      );
    }
  }
  return $items;
}

/**
 * Menu callback; print Drupal-authentication-specific information from user/help.
 */
function drupal_page_help() {
  return drupal_help('user/help#drupal');
}

/**
 * Callback function from drupal_xmlrpc() for authenticating remote clients.
 *
 * Remote clients are usually other Drupal instances.
 */
function drupal_login($username, $password) {
  if (variable_get('drupal_authentication_service', 0)) {
    if ($user = user_load(array('name' => $username, 'pass' => $password, 'status' => 1))) {
      return $user->uid;
    }
    else {
      return 0;
    }
  }
}