initial import from onelab svn codebase
[plewww.git] / modules / drupal.module
1 <?php
2 // $Id: drupal.module 144 2007-03-28 07:52:20Z thierry $
3
4 /**
5  * @file
6  * Lets users log in using a Drupal ID and can notify a central server about your site.
7  */
8
9 /**
10  * Implementation of hook_help().
11  */
12 function drupal_help($section) {
13   switch ($section) {
14     case 'admin/help#drupal':
15       $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>';
16       $output .= t('<p>Enabling the Drupal module will allow you to:</p>
17 <ul>
18   <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>
19   <li>enable other sites to register with your site</li>
20   <li>allow members on all sites using the Drupal module to log in to your site without registering using their distributed identification</li>
21   <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>
22 </ul>
23 ');
24       $output .= '<p>'. t('The Drupal module administration page allows you to set the xml-rpc server page and other related options.') .'</p>';
25       $output .= t('<p>You can</p>
26 <ul>
27   <li>run your cron job at your site\'s <a href="%file-cron">cron page</a></li>
28   <li>view your <a href="%file-xmlrpc">XML-RPC page</a>.</li>
29   <li>administer Drupal <a href="%admin-settings-drupal">administer &gt;&gt; settings &gt;&gt; drupal</a>.</li>
30 </ul>
31 ', array('%file-cron' => 'cron.php', '%file-xmlrpc' => 'xmlrpc.php', '%admin-settings-drupal' => url('admin/settings/drupal')));
32       $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:
33 <ul>
34   <li>Enable the page module. Select create content &gt;&gt; page.</li>
35   <li>For input format, select PHP code.</li>
36   <li>Give the page a title. For body, put:
37 <pre>
38 &lt;?php
39 print drupal_client_page();
40 ?&gt;
41 </pre>
42   <li>Save the page.</li>
43 </ul>') . '</p>';
44
45       $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>';
46       return $output;
47     case 'admin/modules#description':
48       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.');
49     case 'admin/settings/drupal':
50       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')));
51     case 'user/help#drupal':
52       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>
53 <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>')) : '';
54   }
55 }
56
57 /**
58  * Implementation of hook_settings().
59  */
60 function drupal_settings() {
61   // Check if all required fields are present
62   if ((variable_get('site_name', 'drupal') == 'drupal') || (variable_get('site_name', 'drupal') == '')) {
63     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'))));
64   }
65   else if (variable_get('site_mail', ini_get('sendmail_from')) == '') {
66     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'))));
67   }
68   else if (variable_get('site_slogan', '') == '') {
69     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'))));
70   }
71   else if (variable_get('site_mission', '') == '') {
72     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'))));
73   }
74   $options = array('1' => t('Enabled'), '0' => t('Disabled'));
75
76   $form['drupal'] = array(
77     '#type' => 'fieldset',
78     '#title' => t('Post data to another site'),
79     '#tree' => FALSE
80   );
81
82   $form['drupal']['drupal_register'] = array(
83     '#type' => 'radios',
84     '#title' => t('Register with a Drupal server'),
85     '#default_value' => variable_get('drupal_register', 0),
86     '#options' => $options,
87     '#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/"))
88   );
89
90   $form['drupal']['drupal_server'] =  array(
91     '#type' => 'textfield',
92     '#title' => t('Drupal XML-RPC server'),
93     '#default_value' => variable_get('drupal_server', 'http://drupal.org/xmlrpc.php'),
94     '#description' => t('The URL of the Drupal XML-RPC server you wish to register with.')
95   );
96
97   $form['drupal']['drupal_system'] = array(
98     '#type' => 'radios',
99     '#title' => t('Send system information'),
100     '#default_value' => variable_get('drupal_system', 0),
101     '#options' => $options,
102     '#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.")
103   );
104
105   $form['drupal']['drupal_statistics'] = array(
106     '#type' => 'radios',
107     '#title' => t('Send statistics'),
108     '#default_value' => variable_get('drupal_statistics', 0),
109     '#options' => $options,
110     '#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.")
111   );
112
113   $form['services'] = array(
114     '#type' => 'fieldset',
115     '#title' => t('Receive data from other sites'),
116     '#tree' => FALSE
117   );
118
119   $form['services']['drupal_client_service'] = array(
120     '#type' => 'radios',
121     '#title' => t('Allow other Drupal sites to register'),
122     '#default_value' => variable_get('drupal_client_service', 0),
123     '#options' => $options,
124     '#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.')
125   );
126
127   $form['services']['drupal_authentication_service'] = array(
128     '#type' => 'radios',
129     '#title' => t('Authentication service'),
130     '#default_value' => variable_get('drupal_authentication_service', 0),
131     '#options' => $options,
132     '#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.')
133   );
134
135   return $form;
136 }
137
138 /**
139  * Implementation of hook_cron(); handles pings to and from the site.
140  */
141 function drupal_cron() {
142   if (time() - variable_get('cron_last', 0) > 21600) {
143
144     // If this site acts as a Drupal XML-RPC server, delete the sites that
145     // stopped sending "ping" messages.
146     if (variable_get('drupal_client_service', 0)) {
147       $result = db_query("SELECT cid FROM {client} WHERE changed < %d", time() - 259200);
148       while ($client = db_fetch_object($result)) {
149         db_query("DELETE FROM {client_system} WHERE cid = %d", $client->cid);
150         db_query("DELETE FROM {client} WHERE cid = %d", $client->cid);
151       }
152     }
153
154     // If this site acts as a Drupal XML-RPC client, send a message to the
155     // Drupal XML-RPC server.
156     if (variable_get('drupal_register', 0) && variable_get('drupal_server', 0)) {
157       drupal_notify(variable_get('drupal_server', ''));
158     }
159   }
160 }
161
162 /**
163  * Callback function from drupal_xmlrpc() called when another site pings this one.
164  */
165 function drupal_client_ping($client, $system) {
166   /*
167   ** Parse our parameters:
168   */
169
170   foreach (array('link', 'name', 'mail', 'slogan', 'mission') as $key) {
171     $client[$key] = strip_tags($client[$key]);
172   }
173
174   /*
175   ** Update the data in our database and send back a reply:
176   */
177
178   if ($client['link'] && $client['name'] && $client['mail'] && $client['slogan'] && $client['mission']) {
179     $result = db_query(db_rewrite_sql("SELECT cid FROM {client} WHERE link = '%s'"), $client['link']);
180     if (db_num_rows($result)) {
181       $record = db_fetch_object($result);
182       $client['cid'] = $record->cid;
183       // We have an existing record.
184       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']);
185     }
186     else {
187       $client['cid'] = db_next_id('{client}_cid');
188       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());
189     }
190     if (is_array($system)) {
191       db_query("DELETE FROM {client_system} WHERE cid = %d", $client['cid']);
192       foreach ($system as $item) {
193         db_query("INSERT INTO {client_system} (cid, name, type) VALUES (%d, '%s', '%s')", $client['cid'], $item['name'], $item['type']);
194       }
195     }
196     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>');
197
198     return TRUE;
199   }
200   else {
201     return 0;
202   }
203
204 }
205
206 /**
207  * Formats a list of all clients.
208  *
209  * This function may be called from a custom page on sites that are
210  * Drupal directory servers.
211  */
212 function drupal_client_page($sort = 'name') {
213   $result = db_query('SELECT * FROM {client} ORDER BY %s', $sort);
214
215   $clients = array();
216   while ($client = db_fetch_object($result)) {
217     $clients[] = $client;
218   }
219
220   return theme('client_list', $clients);
221 }
222
223 /**
224  * Theme a client list.
225  */
226 function theme_client_list($clients) {
227   // Note: All fields except the mission are treated as plain-text.
228   // The mission is stripped of any HTML tags to keep the output simple and consistent.
229   $output = "\n<dl>\n";
230   foreach ($clients as $client) {
231     $output .= '  <dt><a href="' . check_url($client->link) . '">' . check_plain($client->name) . '</a> - ' . check_plain($client->slogan) . "</dt>\n";
232     $output .= '  <dd>' . strip_tags($client->mission) . "</dd>\n";
233   }
234   $output .= "</dl>\n";
235   return $output;
236 }
237
238 /**
239  * Implementation of hook_xmlrpc().
240  */
241 function drupal_xmlrpc() {
242   $xmlrpc = array();
243   if (variable_get('drupal_client_service', 0)) {
244     $xmlrpc[] = array(
245       'drupal.client.ping',
246       'drupal_client_ping',
247       array('array', 'array', 'array'),
248       t('Handling ping request')
249     );
250   }
251   if (variable_get('drupal_authentication_service', 0)) {
252     $xmlrpc[] = array(
253       'drupal.login',
254       'drupal_login',
255       array('int', 'string', 'string'),
256       t('Logging into a Drupal site')
257     );
258   }
259   return $xmlrpc;
260 }
261
262 /**
263  * Sends a ping to the Drupal directory server.
264  */
265 function drupal_notify($server) {
266   global $base_url;
267   $client = array(
268     'link' => $base_url,
269     'name' => variable_get('site_name', ''),
270     'mail' => variable_get('site_mail', ''),
271     'slogan' => variable_get('site_slogan', ''),
272     'mission' => variable_get('site_mission', ''),
273     'version' => VERSION
274   );
275   if (variable_get('drupal_system', 0)) {
276     $system = array();
277     $result = db_query("SELECT name, type FROM {system} WHERE status = 1");
278     while ($item = db_fetch_array($result)) {
279       $system[] = $item;
280     }
281   }
282   if (variable_get('drupal_statistics', 0)) {
283     $users = db_fetch_object(db_query("SELECT COUNT(uid) AS count FROM {users}"));
284     $client['users'] = $users->count;
285     $nodes = db_fetch_object(db_query("SELECT COUNT(nid) AS count FROM {node}"));
286     $client['nodes'] = $nodes->count;
287   }
288   $result = xmlrpc($server, 'drupal.client.ping', $client, $system);
289
290   if ($result === FALSE) {
291     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);
292   }
293 }
294
295 /**
296  * Implementation of hook_info().
297  */
298 function drupal_info($field = 0) {
299   $info['name'] = 'Drupal';
300   $info['protocol'] = 'XML-RPC';
301
302   if ($field) {
303     return $info[$field];
304   }
305   else {
306     return $info;
307   }
308 }
309
310 /**
311  * Implementation of hook_auth().
312  */
313 function drupal_auth($username, $password, $server) {
314   if (variable_get('drupal_authentication_service', 0)) {
315     $result = xmlrpc("http://$server/xmlrpc.php", 'drupal.login', $username, $password);
316     if ($result === FALSE) {
317       drupal_set_message(t('Error %code : %message', array('%code' => theme('placeholder', xmlrpc_errno()), '%message' =>  theme('placeholder', xmlrpc_error_msg()))), 'error');
318     }
319     else {
320       return $result;
321     }
322   }
323 }
324
325 /**
326  * Implementation of hook_menu().
327  */
328 function drupal_menu($may_cache) {
329   $items = array();
330   if ($may_cache) {
331     if (variable_get('drupal_authentication_service', 0)) {
332       $items[] = array('path' => 'drupal', 'title' => t('Drupal'),
333         'callback' => 'drupal_page_help', 'access' => TRUE,
334         'type' => MENU_SUGGESTED_ITEM
335       );
336     }
337   }
338   return $items;
339 }
340
341 /**
342  * Menu callback; print Drupal-authentication-specific information from user/help.
343  */
344 function drupal_page_help() {
345   return drupal_help('user/help#drupal');
346 }
347
348 /**
349  * Callback function from drupal_xmlrpc() for authenticating remote clients.
350  *
351  * Remote clients are usually other Drupal instances.
352  */
353 function drupal_login($username, $password) {
354   if (variable_get('drupal_authentication_service', 0)) {
355     if ($user = user_load(array('name' => $username, 'pass' => $password, 'status' => 1))) {
356       return $user->uid;
357     }
358     else {
359       return 0;
360     }
361   }
362 }
363
364