drupal.module 9.05 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
Kjartan's avatar
Kjartan committed
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4
function drupal_help($section = "admin/help#drupal") {
Dries's avatar
 
Dries committed
5
  $output = "";
Dries's avatar
 
Dries committed
6

Dries's avatar
 
Dries committed
7
  switch ($section) {
Dries's avatar
 
Dries committed
8
    case 'admin/help#drupal':
Dries's avatar
 
Dries committed
9
      $output .= "<p>The \"Drupal\" module features a capability whereby other drupal sites  may <i>call home</i> to report their existence.  In turn, this enables a pod of Drupal sites to find, cooperate and advertise each other.</p>";
Dries's avatar
 
Dries committed
10 11 12
      $output .= "<p>Currently, the main application of this feature is the %drupal-sites.  By default, fresh Drupal installations can use %Drupal as their <i>directory server</i> and report their existence.  This reporting occurs via scheduled %xml-rpc pings.</p>";
      $output .= "<p>Drupal administrators should simply enable this feature to get listed on the %drupal-sites; just set your site's name, e-mail address, slogan and mission statement.  Then make sure that the field called <i>Drupal XML-RPC server</i> on the %drupal-settings page is set to http://www.drupal.org/xmlrpc.php, and enable this feature using the dropdown directly below.</p>";
      $output .= "<p>The listing of your site will occur shortly after your site's next %cron. Note that cron.php should be called using the domain name which you want to have listed at %Drupal.  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.mydomain.org/cron.php.</p>";
Dries's avatar
 
Dries committed
13
      $output .= "<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>";
Dries's avatar
 
Dries committed
14
      $output .= t($output,  array("%drupal-sites" => "<a href=\"http://www.drupal.org/node/view/3\">". t("Drupal sites page") ."</a>", "%Drupal" => "<a href=\"http://www.drupal.org\">drupal.org</a>", "%xml-rpc" => "<a href=\"http://www.xmlrpc.com/\">XLM-RPC</a>", "%drupal-settings" => l(t("Administration &raquo; site configuration &raquo; modules &raquo; drupal"), "admin/system/modules/drupal"), "%cron" => l(t("cron run"), "admin/system/help#cron") ));
Dries's avatar
 
Dries committed
15
      break;
Dries's avatar
 
Dries committed
16
    case 'admin/system/modules#description':
Dries's avatar
 
Dries committed
17
      $output = t("Lets users log in using a Drupal ID and can notify drupal.org about your site.");
Dries's avatar
 
Dries committed
18 19
      break;
    case 'admin/system/modules/drupal':
Dries's avatar
 
Dries committed
20
      $output = 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 %Drupal-setting setting -- but the directory server has to be able to handle Drupal XML. To get a full site listing go to the %general and set:<ul><li>The site name,</li><li>The site E-Mail address,</li><li>The site slogan, and</li><li>The site mission statement.</li></ul>", array("%Drupal-setting" => l(t("Drupal XML-RPC server"), "admin/system/modules/drupal"), "%general" => l(t("site configuration"), "admin/system")));
Dries's avatar
 
Dries committed
21
      break;
Dries's avatar
 
Dries committed
22
    case 'user/help#drupal':
Dries's avatar
 
Dries committed
23
      $site = variable_get("site_name", "this web site");
Dries's avatar
 
Dries committed
24

Dries's avatar
 
Dries committed
25 26
      $output = "<p>%drupal is the name of the software which powers %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 <b>Drupal ID</b>.</p>\n";
  $output .= "<p>So please feel free to login to your account here at %site with a username from another Drupal site. The format of a Drupal ID is similar to an  email address: <b>username</b>@<i>server</i>. An example of valid Drupal ID is <b>mwlily</b>@<i>www.drupal.org</i>.</p>";
Dries's avatar
 
Dries committed
27

Dries's avatar
 
Dries committed
28 29 30
      $output = t($output, array("%drupal" => "<a href=\"http://www.drupal.org\">Drupal</a>", "%site" => "<i>$site</i>"));
      break;
  }
Dries's avatar
 
Dries committed
31 32

  return $output;
33 34
}

35
function drupal_settings() {
Dries's avatar
 
Dries committed
36
  $output .= form_textfield("Drupal XML-RPC server", "drupal_server", variable_get("drupal_server", "http://www.drupal.org/xmlrpc.php"), 55, 128, "The URL of your root Drupal XML-RPC server.");
Dries's avatar
 
Dries committed
37
  $output .= form_select("Drupal directory", "drupal_directory", variable_get("drupal_directory", 0), array("Disabled", "Enabled"), "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 have to set your site's name, e-mail address, slogan and mission statement.  When the \"Drupal XML-RPC server\" field is set to \"http://www.drupal.org/xmlrpc.php\", your web site will get listed on <a href=\"http://www.drupal.org/\">http://www.drupal.org/</a>.  Requires crontab.");
Dries's avatar
 
Dries committed
38 39 40 41 42

  return $output;
}

function drupal_cron() {
Dries's avatar
 
Dries committed
43 44
  if (time() - variable_get("drupal_cron_last", 0) > 21600) {
    variable_set("drupal_cron_last", time());
Dries's avatar
 
Dries committed
45

Dries's avatar
 
Dries committed
46 47 48 49
    /*
    ** If this site acts as a Drupal XML-RPC server, delete the sites that
    ** stopped sending "ping" messages.
    */
Dries's avatar
 
Dries committed
50

Dries's avatar
 
Dries committed
51
    db_query("DELETE FROM {directory} WHERE timestamp < '". (time() - 259200) ."'");
Dries's avatar
 
Dries committed
52

Dries's avatar
 
Dries committed
53 54 55 56
    /*
    ** If this site acts as a Drupal XML-RPC client, send a message to the
    ** Drupal XML-RPC server.
    */
Dries's avatar
 
Dries committed
57

Dries's avatar
 
Dries committed
58 59 60
    if (variable_get("drupal_directory", 0) && variable_get("drupal_server", 0)) {
      drupal_notify(variable_get("drupal_server", ""));
    }
Dries's avatar
 
Dries committed
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
  }
}

function drupal_directory_ping($arguments) {

  /*
  ** Parse our parameters:
  */

  $argument = $arguments->getparam(0);
  $link = strip_tags($argument->scalarval());
  $argument = $arguments->getparam(1);
  $name = 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) {
Dries's avatar
 
Dries committed
86 87
    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());
Dries's avatar
 
Dries committed
88 89 90 91 92 93 94 95 96 97 98 99

    watchdog("message", "directory: ping from '$name' ($link)");

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

}

function drupal_directory_page() {
Dries's avatar
 
Dries committed
100
  $result = db_query("SELECT * FROM {directory} ORDER BY name");
Dries's avatar
 
Dries committed
101 102 103 104 105 106 107 108 109

  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;
}

function drupal_xmlrpc() {
Dries's avatar
Dries committed
110
  return array("drupal.site.ping" => array("function" => "drupal_directory_ping"), "drupal.login" => array("function" => "drupal_login"));
Dries's avatar
 
Dries committed
111 112 113
}

function drupal_notify($server) {
Dries's avatar
 
Dries committed
114
  global $base_url;
Dries's avatar
 
Dries committed
115 116 117 118 119

  $url = parse_url($server);

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

Dries's avatar
 
Dries committed
120
  $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")));
Dries's avatar
 
Dries committed
121 122 123 124 125 126 127 128 129

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

  if (!$result || $result->faultCode()) {
    watchdog("error", "failed to notify '". $url["host"] ."' at '". $url["path"] ."': ". $result->faultString());
  }

}

Dries's avatar
 
Dries committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
function drupal_info($field = 0) {
  $info["name"] = "Drupal";
  $info["protocol"] = "XML-RPC";

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

function drupal_auth($username, $password, $server) {

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

146 147
  // TODO remove hard coded Port 80
  // TODO manage server/path such that HTTP_HOST/xml.rpc.php is not assumed
Dries's avatar
 
Dries committed
148 149 150 151 152 153 154 155 156 157 158
  $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;
}

function drupal_page() {
Dries's avatar
 
Dries committed
159 160

  theme("header");
Dries's avatar
 
Dries committed
161
  theme("box", "Drupal", drupal_help("user/help#drupal"));
Dries's avatar
 
Dries committed
162
  theme("footer");
Dries's avatar
 
Dries committed
163 164
}

Dries's avatar
Dries committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
function drupal_login($arguments) {
   // an XML-RPC method called by external clients (usually other Drupal instances)
  $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"));
  }
}

180 181 182 183 184 185 186 187
function drupal_user($type, $edit, $user) {

  $module = "drupal";
  $name = module_invoke($module, "info", "name");
  switch ($type) {
    case "view_private":
      $result = user_get_authname($user, $module);
      if ($result) {
Dries's avatar
 
Dries committed
188
        $output .= form_item(t("$name ID"), $result);
189 190
      }
      else {
Dries's avatar
 
Dries committed
191
        // TODO: use a variation of $base_url instead of $HTTP_HOST below
Dries's avatar
 
Dries committed
192
        $output .= form_item(t("$name ID"), "$user->name@". $_SERVER["HTTP_HOST"]);
193 194 195
      }
      return $output;
  }
Dries's avatar
 
Dries committed
196 197
}

Dries's avatar
 
Dries committed
198
?>