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

Contents of /contributions/modules/site_network/site_network.module

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


Revision 1.3 - (show annotations) (download) (as text)
Thu Dec 27 17:31:48 2007 UTC (23 months ago) by karpuz
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +2 -2 lines
File MIME type: text/x-php
Fix search/replace error (thx to wwwebernet for spotting this)
1 <?php
2 // $Id: site_network.module,v 1.2 2007/10/09 09:41:57 karpuz Exp $
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 site_network_help($path, $arg) {
13 switch ($path) {
14 case 'admin/help#site_network':
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>If you maintain a directory of sites, you can list them on a page using the <code>site_network_client_page()</code> function. Sample instructions: </p>
26 <ul>
27 <li>Ensure that you have the page content type enabled, and you can use PHP in an input format.</li>
28 <li>Select create content &gt;&gt; page.</li>
29 <li>For input format, select PHP code.</li>
30 <li>Give the page a title. For body, put:
31 <pre>
32 &lt;?php
33 print site_network_client_page();
34 ?&gt;
35 </pre>
36 <li>Save the page.</li>
37 </ul>');
38
39 $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>';
40 return $output;
41 case 'admin/settings/distributed-authentication':
42 return '<p>'. t('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">site information settings page</a> and set the site name, the e-mail address, the slogan, and the mission statement.', array('@site-settings' => url('admin/settings/site-information'))) .'</p>';
43 case 'user/register':
44 if (!user_access('administer users')) {
45 return t('Note: if you have an account with another Drupal site, you may be able to <a href="!login">log in</a> with its username and password instead of registering.', array('!login' => url('user/login')));
46 }
47 break;
48 }
49 }
50
51 /**
52 * Implementation of hook_theme()
53 */
54 function site_network_theme() {
55 return array(
56 'client_list' => array(
57 'arguments' => array('clients' => NULL),
58 ),
59 );
60 }
61
62 function site_network_form_alter(&$form, $form_state, $form_id) {
63 if ($form_id == 'user_login_block' || $form_id == 'user_login') {
64 // Splice in our validate handler for authentication if user is performing a distributed login.
65 // Remove the local authentication handler added by user.module
66 if (!empty($form_state['post']['name']) && site_network_is_distributed_login($form_state['post']['name'])) {
67 $key = array_search('user_login_authenticate_validate', $form['#validate']);
68 $form['#validate']['key'] = 'site_network_distributed_validate';
69 }
70 }
71 }
72
73 /**
74 * When login form is shown on full page, let users know that Drupal IDs are accepted.
75 *
76 * @return void
77 **/
78 function site_network_form_user_login_alter(&$form, $form_state) {
79 if (variable_get('site_network_authentication_service', FALSE)) {
80 $form['name']['#description'] = t('Enter your @s username, or a Drupal ID from another web site.', array('@s' => variable_get('site_name', 'Drupal')));
81 }
82 }
83
84 /**
85 * Given a username, determine if user is attempting a distributed login.
86 *
87 * @return boolean
88 **/
89 function site_network_is_distributed_login($name) {
90 return variable_get('site_network_authentication_service', FALSE) && (strpos($name, '@') || variable_get('site_network_default_da_server', ''));
91 }
92
93 /**
94 * A custom validate handler on the login form. Checks supplied username/password against a remote Drupal site.
95 *
96 * @return boolean
97 **/
98 function site_network_distributed_validate($form, &$form_state) {
99 global $user;
100
101 if ($user->uid) {
102 return;
103 }
104
105 $name = $form_state['values']['name'];
106 $pass = trim($form_state['values']['pass']);
107 // Strip name and server from ID:
108 if ($server = strrchr($name, '@')) {
109 $name = substr($name, 0, strlen($name) - strlen($server));
110 $server = substr($server, 1);
111 }
112
113 if (site_network_auth($name, $pass, $server)) {
114 // We have a successful authentication. Login or register the user.
115 if ($server) {
116 $name .= '@'. $server;
117 }
118 user_external_login_register($name, 'site_network');
119 }
120 }
121
122 /**
123 * Implementation of hook_cron(); handles pings to and from the site.
124 */
125 function site_network_cron() {
126 if (time() - variable_get('cron_last', 0) > 21600) {
127
128 // If this site acts as a Drupal XML-RPC server, delete the sites that
129 // stopped sending "ping" messages.
130 if (variable_get('site_network_client_service', 0)) {
131 $result = db_query("SELECT cid FROM {client} WHERE changed < %d", time() - 259200);
132 while ($client = db_fetch_object($result)) {
133 db_query("DELETE FROM {client_system} WHERE cid = %d", $client->cid);
134 db_query("DELETE FROM {client} WHERE cid = %d", $client->cid);
135 }
136 }
137
138 // If this site acts as a Drupal XML-RPC client, send a message to the
139 // Drupal XML-RPC server.
140 if (variable_get('site_network_register', 0) && variable_get('site_network_server', 0)) {
141 site_network_notify(variable_get('site_network_server', ''));
142 }
143 }
144 }
145
146 /**
147 * Callback function from site_network_xmlrpc() called when another site pings this one.
148 */
149 function site_network_client_ping($client, $system) {
150 /*
151 ** Parse our parameters:
152 */
153
154 foreach (array('link', 'name', 'mail', 'slogan', 'mission') as $key) {
155 $client[$key] = strip_tags($client[$key]);
156 }
157
158 /*
159 ** Update the data in our database and send back a reply:
160 */
161
162 if ($client['link'] && $client['name'] && $client['mail'] && $client['slogan'] && $client['mission']) {
163 $result = db_query("SELECT cid FROM {client} WHERE link = '%s'", $client['link']);
164 if ($record = db_fetch_object($result)) {
165 $client['cid'] = $record->cid;
166 // We have an existing record.
167 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']);
168 }
169 else {
170 db_query("INSERT INTO {client} (link, name, mail, slogan, mission, users, nodes, version, created, changed) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $client['link'], $client['name'], $client['mail'], $client['slogan'], $client['mission'], $client['users'], $client['nodes'], $client['version'], time(), time());
171 $client['cid'] = db_last_insert_id('client', 'cid');
172 }
173 if (is_array($system)) {
174 db_query("DELETE FROM {client_system} WHERE cid = %d", $client['cid']);
175 foreach ($system as $item) {
176 db_query("INSERT INTO {client_system} (cid, name, type) VALUES (%d, '%s', '%s')", $client['cid'], $item['name'], $item['type']);
177 }
178 }
179 watchdog('client ping', 'Ping from %name (%link).', array('%name' => $client['name'], '%link' => $client['link']), WATCHDOG_NOTICE, '<a href="'. check_url($client['link']) .'">view</a>');
180
181 return TRUE;
182 }
183 else {
184 return 0;
185 }
186
187 }
188
189 /**
190 * Formats a list of all clients.
191 *
192 * This function may be called from a custom page on sites that are
193 * Drupal directory servers.
194 */
195 function site_network_client_page($sort = 'name') {
196 $result = db_query('SELECT * FROM {client} ORDER BY %s', $sort);
197
198 $clients = array();
199 while ($client = db_fetch_object($result)) {
200 $clients[] = $client;
201 }
202
203 return theme('client_list', $clients);
204 }
205
206 /**
207 * Theme a client list.
208 */
209 function theme_client_list($clients) {
210 // Note: All fields except the mission are treated as plain-text.
211 // The mission is stripped of any HTML tags to keep the output simple and consistent.
212 $output = "\n<dl>\n";
213 foreach ($clients as $client) {
214 $output .= ' <dt><a href="'. check_url($client->link) .'">'. check_plain($client->name) .'</a> - '. check_plain($client->slogan) ."</dt>\n";
215 $output .= ' <dd>'. strip_tags($client->mission) ."</dd>\n";
216 }
217 $output .= "</dl>\n";
218 return $output;
219 }
220
221 /**
222 * Implementation of hook_xmlrpc().
223 */
224 function site_network_xmlrpc() {
225 $xmlrpc = array();
226 if (variable_get('site_network_client_service', 0)) {
227 $xmlrpc[] = array(
228 'site_network.client.ping',
229 'site_network_client_ping',
230 array('array', 'array', 'array'),
231 t('Handling ping request')
232 );
233 }
234 if (variable_get('site_network_authentication_service', 0)) {
235 $xmlrpc[] = array(
236 'site_network.login',
237 'site_network_login',
238 array('int', 'string', 'string'),
239 t('Logging into a Drupal site')
240 );
241 }
242 return $xmlrpc;
243 }
244
245 /**
246 * Sends a ping to the Drupal directory server.
247 */
248 function site_network_notify($server) {
249 global $base_url;
250 $client = array(
251 'link' => $base_url,
252 'name' => variable_get('site_name', ''),
253 'mail' => variable_get('site_mail', ''),
254 'slogan' => variable_get('site_slogan', ''),
255 'mission' => variable_get('site_mission', ''),
256 'version' => VERSION
257 );
258 if (variable_get('site_network_system', 0)) {
259 $system = array();
260 $result = db_query("SELECT name, type FROM {system} WHERE status = 1");
261 while ($item = db_fetch_array($result)) {
262 $system[] = $item;
263 }
264 }
265 if (variable_get('site_network_statistics', 0)) {
266 $users = db_fetch_object(db_query("SELECT COUNT(uid) AS count FROM {users}"));
267 $client['users'] = $users->count;
268 $nodes = db_fetch_object(db_query("SELECT COUNT(nid) AS count FROM {node}"));
269 $client['nodes'] = $nodes->count;
270 }
271 $result = xmlrpc($server, 'site_network.client.ping', $client, $system);
272
273 if ($result === FALSE) {
274 watchdog('server ping', 'Failed to notify %server; error code: %errno; error message: %error_msg.', array('%server' => $server, '%errno' => xmlrpc_errno(), '%error_msg' => xmlrpc_error_msg()), WATCHDOG_WARNING);
275 }
276 }
277
278 /**
279 * Attempt to authenticate using the presented credentials and Drupal site.
280 *
281 * @return boolean
282 **/
283 function site_network_auth($username, $password, $server = FALSE) {
284 if (variable_get('site_network_authentication_service', 0)) {
285 if (!$server) {
286 $server = variable_get('site_network_default_da_server', '');
287 }
288 else if (variable_get('site_network_default_da_server_only', 0)) {
289 if (variable_get('site_network_default_da_server', '') != $server) {
290 return;
291 }
292 }
293 if (!empty($server)) {
294 $result = xmlrpc("http://$server/xmlrpc.php", 'site_network.login', $username, $password);
295 if ($result === FALSE) {
296 drupal_set_message(t('Error %code: %message', array('%code' => xmlrpc_errno(), '%message' => xmlrpc_error_msg())), 'error');
297 }
298 else {
299 return $result;
300 }
301 }
302 }
303 }
304
305 /**
306 * Implementation of hook_menu().
307 */
308 function site_network_menu() {
309 $items['admin/settings/sites-registry'] = array(
310 'title' => 'Sites registry',
311 'description' => 'Register with another Drupal site (drupal.org by default) for statistics sharing, or set up your server to be a central server for registrations.',
312 'page callback' => 'drupal_get_form',
313 'page arguments' => array('site_network_sites_registry_settings'),
314 'access arguments' => array('administer site configuration'),
315 'file' => 'site_network.admin.inc',
316 );
317 $items['admin/settings/distributed-authentication'] = array(
318 'title' => 'Distributed authentication',
319 'description' => 'Allow your site to accept logins from other Drupal sites using the site_network module such as drupal.org.',
320 'page callback' => 'drupal_get_form',
321 'page arguments' => array('site_network_distributed_authentication_settings'),
322 'access arguments' => array('administer site configuration'),
323 'file' => 'site_network.admin.inc',
324 );
325 if (variable_get('site_network_authentication_service', 0)) {
326 $items['site_network/help'] = array(
327 'title' => t('External login tips'),
328 'page callback' => 'site_network_page_help',
329 'type' => MENU_CALLBACK,
330 'file' => 'site_network.pages.inc',
331 );
332 }
333 return $items;
334 }
335
336 /**
337 * Callback function from site_network_xmlrpc() for authenticating remote clients.
338 *
339 * Remote clients are usually other Drupal instances.
340 */
341 function site_network_login($username, $password) {
342 if (variable_get('site_network_authentication_service', 0)) {
343 return user_authenticate($username, $password);
344 }
345 }
346
347

  ViewVC Help
Powered by ViewVC 1.1.2