3 XUP/XProfile parser and verification class
4 ------------------------------------------
6 $xuser = new xprofile("http://example.org/profile.xml");
8 if ($xuser->login($password)) {
10 echo $xuser->info["nickname"];
13 Should work with the older PEAR and the HTTP request class (PD) from
18 if (!class_exists("http_request")) {
19 include_once("http.php"); # from upgradephp/contrib/
20 //include_once("http/request.php"); # from PEAR
24 //define("XPROFILE_SITE", "example.com"); // preferred identifier of website
25 define("XPROFILE_SITE", preg_replace("/^www\./","",$_SERVER["SERVER_NAME"]));
28 #-- retrieves and verifies xml user profile
31 #-- constructor takes URL of profile or "user@domain" shortcut
32 function xprofile($url, $xml=NULL, $uu0=0, $_redir=1) {
35 if (strpos($url, "@") && !strpos($url, "/")) {
36 $un = strtok($url, "@");
37 $url = "http://" . strtok("@") . "/xprofile/" . $un;
44 if (empty($xml) && !strncmp($url, "http://", 7)) {
45 list($xml, $head) = $this->http("GET", $url);
46 if (!$xml) { return; }
50 $p = new xprofile_parser($xml);
52 if (isset($p->control) || isset($p->info)) {
53 $this->control = (array)$p->control;
54 $this->info = (array)$p->info;
55 $this->text = (array)$p->text; // already lcased
58 if ($this->url != $this->control["self"]) {
59 $this->url = $this->control["self"];
61 $this->xprofile($this->url, NULL, 0, 0);
66 #-- verifies a user as owner of profile instance
67 function login($password) {
68 if ($login = $this->control["login"]) {
71 "password" => $password,
72 "site" => XPROFILE_SITE,
74 list($result, $headers) = $this->http("POST", $login, $params);
75 // should come without appended \r or \n (as per spec, but hey)
76 return(rtrim($result) === "1");
81 function http($method, $url, $formdata=array()) {
82 if (class_exists("http_request")) {
83 $req = new http_request();
84 $req->setMethod($method);
86 $req->addHeader("Accept", "xml/user-profile, text/boolean, */*; q=0.1");
88 $req->params += $formdata;
89 $req->_postData = $formdata; // PEAR tries to annoy us
91 $r = $req->sendRequest();
92 if ($r && ($r->getResponseStatus() == 200)) {
93 return array($r->getResponseBody(), $r->getResponseHeader());
96 elseif ($method=="GET") {
97 return array(file_get_contents($url), array());
99 elseif ($method=="POST") {
102 return array(NULL, NULL);
108 class xprofile_parser extends easy_xml {
110 function xprofile_parser($xml) {
113 $this->xmlns["urn:mime:xml/user-profile"] = "xup";
114 $this->xmlns2["xup"] = "";
121 function start($xp, &$tag, &$attr) {
122 parent::start($xp, $tag, $attr);
124 unset($this->text_meta);
125 if ($tag == "link") {
126 $this->{$p}[strtolower($attr["rel"])] = $attr["href"];
128 elseif ($tag == "meta") {
129 $name = strtolower($attr["name"]);
130 if (isset($attr["content"])) {
131 $this->{$p}[$name] = $attr["content"];
133 elseif ($this->parent == "text") {
134 $this->text_meta = $name;
138 #-- text:meta content
139 function cdata($xp, $text) {
140 if (isset($this->text_meta)) {
141 $this->text[$this->text_meta] = $text;
142 unset($this->text_meta);