summaryrefslogtreecommitdiff
path: root/modules/drupal/drupal.module
blob: a0fa4a59560effd92883a5d357724a15673a0f00 (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
<?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':
      return t("
<p>The \"Drupal\" module features a capability whereby other drupal sites may <em>call home</em> to report their existence. In turn, this enables a pod of Drupal sites to find, cooperate and advertise each other.</p>
<p>Currently, the main application of this feature is the <a href=\"%drupal-sites\">Drupal sites</a> page. By default, fresh Drupal installations can use <a href=\"%Drupal\">drupal.org</a> as their <em>directory server</em> and report their existence. This reporting occurs via scheduled <a href=\"%xml-rpc\">XML-RPC</a> pings.</p>
<p>Drupal administrators should simply enable this feature to get listed on the <a href=\"%drupal-sites\">Drupal sites</a> page. Just set your site's name, e-mail address, slogan and mission statement on the <a href=\"%site-settings\">administer &raquo; settings</a> page. Then make sure that the field called <em>Drupal XML-RPC server</em> on the <a href=\"%drupal-settings\">administer &raquo; settings &raquo; drupal</a> page is set to %drupal-xml-rpc, and enable this feature using the dropdown directly below.</p>
<p>The listing of your site will occur shortly after your site's next cron run. Note that cron.php should be called using the domain name which you want to have listed at <a href=\"%Drupal\">drupal.org</a>. For example, don't kick off cron by requesting http://127.0.0.1/cron.php. Instead, use a publicly accessible domain name such as http://www.example.com/cron.php.</p>
<p>Also note that your installation need not use drupal.org as its directory server. For example, this feature is perfectly capable of aggregating pings from all of your departmental drupal installations sites within an enterprise.</p>", array('%drupal-sites' => 'http://www.drupal.org/drupal-sites', '%Drupal' => 'http://www.drupal.org', '%drupal-xml-rpc' => 'http://www.drupal.org/xmlrpc.php', '%xml-rpc' => 'http://www.xmlrpc.com/', '%site-settings' => url('admin/settings'), '%drupal-settings' => url('admin/settings/drupal')));
    case 'admin/modules#description':
      return t('Lets users log in using a Drupal ID and can notify a central server about your site.');
    case 'admin/settings/drupal':
      return t("Using this your Drupal site can \"call home\" and add itself to the Drupal directory. If you want it to add itself to a different directory server you can change the <a href=\"%xml-rpc-setting\">Drupal XML-RPC server</a> setting -- but the directory server has to be able to handle Drupal XML. To get all your site information listed, go to the <a href=\"%site-settings\">administer &raquo; settings</a> page and set the site name, the e-mail address, the slogan, and the mission statement.", array('%xml-rpc-setting' => url('admin/settings/drupal'), '%site-settings' => url('admin/settings')));
    case 'user/help#drupal':
      return t("<p><a href=\"%Drupal\">Drupal</a> is the name of the software which 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 login to any Drupal site using a single <strong>Drupal ID</strong>.</p>
<p>So please feel free to login to your account here at %this-site with a username from another Drupal site. The format of a Drupal ID is similar to an email address: <strong>username</strong>@<em>server</em>. An example of a valid Drupal ID is <strong>mwlily</strong>@<em>www.drupal.org</em>.</p>", array('%Drupal' => 'http://www.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 for the Drupal directory
  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'))));

  $output = form_textfield(t('Drupal XML-RPC server'), 'drupal_server', variable_get('drupal_server', 'http://www.drupal.org/xmlrpc.php'), 55, 128, t('The URL of your root Drupal XML-RPC server.'));
  $output .= form_radios(t('Drupal directory'), 'drupal_directory', variable_get('drupal_directory', 0), array(t('Disabled'), t('Enabled')), t("If enabled, your Drupal site will make itself known to the Drupal directory at 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 get listed on the <a href=\"%drupal-sites\">Drupal sites</a> page. Requires the cron feature to be enabled.", array("%drupal-xml-rpc" => "http://www.drupal.org/xmlrpc.php", "%drupal-sites" => "http://www.drupal.org/drupal-sites/")));

  return $output;
}

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

    // If this site acts as a Drupal XML-RPC server, delete the sites that
    // stopped sending "ping" messages.
    db_query("DELETE FROM {directory} WHERE timestamp < '". (time() - 259200) ."'");

    // If this site acts as a Drupal XML-RPC client, send a message to the
    // Drupal XML-RPC server.
    if (variable_get('drupal_directory', 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_directory_ping($arguments) {

  /*
  ** Parse our parameters:
  */

  $argument = $arguments->getparam(0);
  $link = strip_tags($argument->scalarval());
  $argument = $arguments->getparam(1);
  $name = trim(strip_tags($argument->scalarval()));
  $argument = $arguments->getparam(2);
  $mail = strip_tags($argument->scalarval());
  $argument = $arguments->getparam(3);
  $slogan = strip_tags($argument->scalarval());
  $argument = $arguments->getparam(4);
  $mission = strip_tags($argument->scalarval());

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

  if ($link && $name && $mail && $slogan && $mission) {
    db_query("DELETE FROM {directory} WHERE link = '%s' OR mail = '%s'", $link, $mail);
    db_query("INSERT INTO {directory} (link, name, mail, slogan, mission, timestamp) VALUES ('%s', '%s', '%s', '%s', '%s', %d)", $link, $name, $mail, $slogan, $mission, time());

    watchdog('directory ping', t('Ping from %name (%link).', array('%name' => "<em>$name</em>", '%link' => "<em>$link</em>")));

    return new xmlrpcresp(new xmlrpcval(1, 'int'));
  }
  else {
    return new xmlrpcresp(new xmlrpcval(0, 'int'));
  }

}

/**
 * Formats a list of all sites indexed in the directory.
 *
 * This function may be called from a custom page on sites that are
 * Drupal directory servers.
 */
function drupal_directory_page($sort = 'name') {
  $result = db_query('SELECT * FROM {directory} ORDER BY '. $sort);

  while ($site = db_fetch_object($result)) {
    $output .= "<a href=\"$site->link\">$site->name</a> - $site->slogan<div style=\"padding-left: 20px;\">$site->mission</div><br />";
  }

  return $output;
}

/**
 * Implementation of hook_xmlrpc().
 */
function drupal_xmlrpc() {
  return array('drupal.site.ping' => array('function' => 'drupal_directory_ping'), 'drupal.login' => array('function' => 'drupal_login'));
}

/**
 * Sends a ping to the Drupal directory server.
 */
function drupal_notify($server) {
  global $base_url;

  $url = parse_url($server);

  $client = new xmlrpc_client($url['path'], $url['host'], 80);

  $message = new xmlrpcmsg('drupal.site.ping', array(new xmlrpcval($base_url, 'string'), new xmlrpcval(variable_get('site_name', ''), 'string'), new xmlrpcval(variable_get('site_mail', ''), 'string'), new xmlrpcval(variable_get('site_slogan', ''), 'string'), new xmlrpcval(variable_get('site_mission', ''), 'string')));

  $result = $client->send($message, 5);

  if (!$result || $result->faultCode()) {
    watchdog('directory ping', t('Failed to notify %url at %path: %error.', array('%url' => '<em>'. $url["host"] .'</em>', '%path' => '<em>'. $url["path"] .'</em>', '%error' => '<em>'. $result->faultString() .'</em>')), 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) {

  $message = new xmlrpcmsg('drupal.login', array(new xmlrpcval($username, 'string'), new xmlrpcval($password, 'string')));

  // TODO remove hard coded Port 80
  // TODO manage server/path such that HTTP_HOST/xml.rpc.php is not assumed
  $client = new xmlrpc_client('/xmlrpc.php', $server, 80);
  $result = $client->send($message, 5);
  if ($result && !$result->faultCode()) {
    $value = $result->value();
    $login = $value->scalarval();
  }

  return $login;
}

/**
 * Implementation of hook_menu().
 */
function drupal_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $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() {
  print theme('page', drupal_help('user/help#drupal'));
}

/**
 * Callback function from drupal_xmlrpc() for authenticating remote clients.
 *
 * Remote clients are usually other Drupal instances.
 */
function drupal_login($arguments) {
  $argument = $arguments->getparam(0);
  $username = $argument->scalarval();
  $argument = $arguments->getparam(1);
  $password = $argument->scalarval();

  if ($user = user_load(array('name' => $username, 'pass' => $password, 'status' => 1))) {
    return new xmlrpcresp(new xmlrpcval($user->uid, 'int'));
  }
  else {
    return new xmlrpcresp(new xmlrpcval(0, 'int'));
  }
}

?>