Commit d5bc97f0 authored by Daniel Kraft's avatar Daniel Kraft
Browse files

Rewrite identity page in a more modular way.

Rewrite the identity page logic to be more modular, with a "handler"
class to process the various possible fields.
parent e479f8d5
......@@ -35,42 +35,249 @@ function sanitiseLink ($href)
return "http://$href";
}
/* Helper routine to check a GPG fingerprint and format it nicely. This
makes all hex characters upper case, removes spaces and colons, and
puts back spaces to group the characters in a uniform way. Finally,
it formats the final digits (as in the usual GPG fingerprint key ID)
in <strong> tags. */
function formatGPG ($fpr)
/* ************************************************************************** */
/* Field handlers. */
/**
* Basic class for a field handler.
*/
abstract class FieldHandler
{
/** Label to display. */
private $label;
/**
* Construct it. It only sets the label. Information about the
* actual data to display is handled by subclasses.
* @param label The label to display.
*/
public function __construct ($label)
{
$this->label = $label;
}
/**
* Print out HTML for this field in the <dl>.
* @param html HtmlOutput object.
* @param data JSON data of the id value.
*/
public function output (HtmlOutput $html, $data)
{
$content = $this->processContent ($html, $data);
if ($content !== NULL)
{
echo "<dt>" . $html->escape ($this->label) . "</dt>\n";
echo "<dd>$content</dd>\n";
}
}
/**
* Subclasses must implement extraction of the "content" (as HTML text)
* to display. NULL should be returned if this field is not served
* by the given data.
* @param html HtmlOutput object to use.
* @param data JSON data of the id value.
* @return Processed content.
*/
abstract protected function processContent (HtmlOutput $html, $data);
}
/**
* Basic field that just prints the literal value.
*/
class BasicField extends FieldHandler
{
/** Key in the JSON data to extract. */
protected $key;
/**
* Construct the field.
* @param label The label to display.
* @param key The key for data extraction.
*/
public function __construct ($label, $key)
{
parent::__construct ($label);
$this->key = $key;
}
/**
* Get the content to show.
* @param html HtmlOutput object to use.
* @param data JSON data of the id value.
* @return Processed content.
*/
protected function processContent (HtmlOutput $html, $data)
{
if (!isset ($data->{$this->key}))
return NULL;
return $html->escape ($data->{$this->key});
}
}
/**
* Field for website.
*/
class WebsiteField extends BasicField
{
/**
* Get the content to show.
* @param html HtmlOutput object to use.
* @param data JSON data of the id value.
* @return Processed content.
*/
protected function processContent (HtmlOutput $html, $data)
{
if (!isset ($data->{$this->key}))
return NULL;
$val = $data->{$this->key};
$text = $html->escape ($val);
$link = $html->escape (sanitiseLink ($val));
return "<a href='$link'>$text</a>";
}
}
/**
* Field for email or crypto-addresses. We create a link and prepend
* a given "protocol" to it.
*/
class ProtocolledField extends BasicField
{
$fpr = strtoupper ($fpr);
$fpr = preg_replace ("/[ :]/", "", $fpr);
if (!preg_match ("/^[0-9A-F]{40}$/", $fpr))
return NULL;
/** The protocol to prepend. */
private $protocol;
$fpr = preg_replace ("/(.{4})/", "$1 ", $fpr);
$fpr = preg_replace ("/(.{4} .{4}) $/", "<strong>$1</strong>", $fpr);
/**
* Construct the field.
* @param label The label to display.
* @param key The key for data extraction.
* @param protocol The protocol to prepent.
*/
public function __construct ($label, $key, $protocol)
{
parent::__construct ($label, $key);
$this->protocol = $protocol;
}
/**
* Get the content to show.
* @param html HtmlOutput object to use.
* @param data JSON data of the id value.
* @return Processed content.
*/
protected function processContent (HtmlOutput $html, $data)
{
if (!isset ($data->{$this->key}))
return NULL;
$proto = $html->escape ($this->protocol);
$val = $html->escape ($data->{$this->key});
return "<a href='$proto:$val'>$val</a>";
}
return $fpr;
}
/**
* Field for GPG fingerprint.
*/
class GPG_Field extends BasicField
{
/**
* Get the content to show.
* @param html HtmlOutput object to use.
* @param data JSON data of the id value.
* @return Processed content.
*/
protected function processContent (HtmlOutput $html, $data)
{
if (!isset ($data->{$this->key}))
return NULL;
$val = $data->{$this->key};
if (!is_object ($val))
return NULL;
if (!isset ($val->v) || $val->v !== "pka1")
return NULL;
if (!isset ($val->fpr))
return NULL;
$formatted = $this->formatGPG ($val->fpr);
if ($formatted === NULL)
return NULL;
$href = NULL;
$content = "";
if (isset ($val->uri))
{
$href = $val->uri;
$href = $html->escape (sanitiseLink ($href));
$content .= "<a href='$href'>";
}
$content .= $formatted;
if ($href !== NULL)
$content .= "</a>";
return $content;
}
/**
* Helper routine to check a GPG fingerprint and format it nicely. This
* makes all hex characters upper case, removes spaces and colons, and
* puts back spaces to group the characters in a uniform way. Finally,
* it formats the final digits (as in the usual GPG fingerprint key ID)
* in <strong> tags.
* @param fpr The fingerprint to format.
* @return The processed fingerprint.
*/
private function formatGPG ($fpr)
{
$fpr = strtoupper ($fpr);
$fpr = preg_replace ("/[ :]/", "", $fpr);
if (!preg_match ("/^[0-9A-F]{40}$/", $fpr))
return NULL;
$fpr = preg_replace ("/(.{4})/", "$1 ", $fpr);
$fpr = preg_replace ("/(.{4} .{4}) $/", "<strong>$1</strong>", $fpr);
return $fpr;
}
}
/* ************************************************************************** */
?>
<h1><?php echo $html->escape ("$namePrefix/$identityName"); ?></h1>
<?php
$displayInfo = array ("name" => "Real Name",
"nick" => "Nickname",
"website" => "Website",
"email" => "Email",
"gpg" => "OpenPGP",
"bitmessage" => "Bitmessage",
"xmpp" => "XMPP",
"bitcoin" => "Bitcoin",
"namecoin" => "Namecoin",
"litecoin" => "Litecoin",
"ppcoin" => "PPCoin");
$fields = array (new BasicField ("Real Name", "name"),
new BasicField ("Nickname", "nick"),
new WebsiteField ("Website", "website"),
new ProtocolledField ("Email", "email", "mailto"),
new GPG_Field ("OpenPGP", "gpg"),
new BasicField ("Bitmessage", "bitmessage"),
new BasicField ("XMPP", "xmpp"),
new ProtocolledField ("Bitcoin", "bitcoin", "bitcoin"),
new ProtocolledField ("Namecoin", "namecoin", "namecoin"),
new ProtocolledField ("Huntercoin", "huntercoin",
"huntercoin"),
new ProtocolledField ("Litecoin", "litecoin", "litecoin"),
new BasicField ("Peercoin", "ppcoin"));
if ($identityPage)
{
?>
......@@ -81,60 +288,8 @@ public profile information registered:</p>
<?php
echo "<dl class='dl-horizontal'>\n";
foreach ($displayInfo as $key => $label)
{
if (isset ($identityPage->$key))
{
$content = NULL;
switch ($key)
{
case "website":
$text = $html->escape ($identityPage->$key);
$href = $html->escape (sanitiseLink ($identityPage->$key));
$content = "<a href='$href'>$text</a>";
break;
case "email":
$text = $html->escape ($identityPage->$key);
$content = "<a href='mailto:$text'>$text</a>";
break;
case "gpg":
if (is_object ($identityPage->$key)
&& isset ($identityPage->$key->v)
&& $identityPage->$key->v === "pka1"
&& isset ($identityPage->$key->fpr))
{
$val = formatGPG ($identityPage->$key->fpr);
if ($val !== NULL)
{
$href = NULL;
$content = "";
if (isset ($identityPage->$key->uri))
{
$href = $identityPage->$key->uri;
$href = $html->escape (sanitiseLink ($href));
$content .= "<a href='$href'>";
}
$content .= $val;
if ($href !== NULL)
$content .= "</a>";
}
}
break;
default:
$content = $html->escape ($identityPage->$key);
break;
}
if ($content !== NULL)
{
echo "<dt>" . $html->escape ($label) . "</dt>\n";
echo "<dd>$content</dd>\n";
}
}
}
foreach ($fields as $f)
$f->output ($html, $identityPage);
echo "</dl>\n";
}
else
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment