Move xml to namespace

Relocate xml from include to src
Related to #3878
This commit is contained in:
Adam Magness 2017-11-10 00:00:50 -05:00
parent 037d6ac17b
commit f245bc8359
12 changed files with 5394 additions and 5137 deletions

View file

@ -13,10 +13,9 @@ use Friendica\Core\System;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Database\DBM;
use Friendica\Util\Xml;
use dba;
use xml;
use DomXPath;
use DOMDocument;
@ -124,7 +123,7 @@ class Probe {
return array();
}
$links = xml::element_to_array($xrd);
$links = Xml::element_to_array($xrd);
if (!isset($links["xrd"]["link"])) {
logger("No xrd data found for ".$host, LOGGER_DEBUG);
return array();
@ -706,7 +705,7 @@ class Probe {
return false;
}
$xrd_arr = xml::element_to_array($xrd);
$xrd_arr = Xml::element_to_array($xrd);
if (!isset($xrd_arr["xrd"]["link"])) {
logger("No XML webfinger links for ".$url, LOGGER_DEBUG);
return false;

View file

@ -6,10 +6,9 @@
namespace Friendica;
use Friendica\Core\Config;
use Friendica\Util\Xml;
use xml;
use dba;
use DomXPath;
use DOMDocument;
@ -204,17 +203,17 @@ class ParseUrl
$doc = new DOMDocument();
@$doc->loadHTML($body);
xml::deleteNode($doc, "style");
xml::deleteNode($doc, "script");
xml::deleteNode($doc, "option");
xml::deleteNode($doc, "h1");
xml::deleteNode($doc, "h2");
xml::deleteNode($doc, "h3");
xml::deleteNode($doc, "h4");
xml::deleteNode($doc, "h5");
xml::deleteNode($doc, "h6");
xml::deleteNode($doc, "ol");
xml::deleteNode($doc, "ul");
Xml::deleteNode($doc, "style");
Xml::deleteNode($doc, "script");
Xml::deleteNode($doc, "option");
Xml::deleteNode($doc, "h1");
Xml::deleteNode($doc, "h2");
Xml::deleteNode($doc, "h3");
Xml::deleteNode($doc, "h4");
Xml::deleteNode($doc, "h5");
Xml::deleteNode($doc, "h6");
Xml::deleteNode($doc, "ol");
Xml::deleteNode($doc, "ul");
$xpath = new DomXPath($doc);

View file

@ -13,12 +13,12 @@ use Friendica\Core\Config;
use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Util\Xml;
use dba;
use DOMDocument;
use DomXPath;
use ostatus;
use xml;
require_once "include/Contact.php";
require_once "include/enotify.php";
@ -402,17 +402,17 @@ class DFRN
$mail = $doc->createElement("dfrn:mail");
$sender = $doc->createElement("dfrn:sender");
xml::add_element($doc, $sender, "dfrn:name", $owner['name']);
xml::add_element($doc, $sender, "dfrn:uri", $owner['url']);
xml::add_element($doc, $sender, "dfrn:avatar", $owner['thumb']);
Xml::add_element($doc, $sender, "dfrn:name", $owner['name']);
Xml::add_element($doc, $sender, "dfrn:uri", $owner['url']);
Xml::add_element($doc, $sender, "dfrn:avatar", $owner['thumb']);
$mail->appendChild($sender);
xml::add_element($doc, $mail, "dfrn:id", $item['uri']);
xml::add_element($doc, $mail, "dfrn:in-reply-to", $item['parent-uri']);
xml::add_element($doc, $mail, "dfrn:sentdate", datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME));
xml::add_element($doc, $mail, "dfrn:subject", $item['title']);
xml::add_element($doc, $mail, "dfrn:content", $item['body']);
Xml::add_element($doc, $mail, "dfrn:id", $item['uri']);
Xml::add_element($doc, $mail, "dfrn:in-reply-to", $item['parent-uri']);
Xml::add_element($doc, $mail, "dfrn:sentdate", datetime_convert('UTC', 'UTC', $item['created'] . '+00:00' , ATOM_TIME));
Xml::add_element($doc, $mail, "dfrn:subject", $item['title']);
Xml::add_element($doc, $mail, "dfrn:content", $item['body']);
$root->appendChild($mail);
@ -437,11 +437,11 @@ class DFRN
$suggest = $doc->createElement("dfrn:suggest");
xml::add_element($doc, $suggest, "dfrn:url", $item['url']);
xml::add_element($doc, $suggest, "dfrn:name", $item['name']);
xml::add_element($doc, $suggest, "dfrn:photo", $item['photo']);
xml::add_element($doc, $suggest, "dfrn:request", $item['request']);
xml::add_element($doc, $suggest, "dfrn:note", $item['note']);
Xml::add_element($doc, $suggest, "dfrn:url", $item['url']);
Xml::add_element($doc, $suggest, "dfrn:name", $item['name']);
Xml::add_element($doc, $suggest, "dfrn:photo", $item['photo']);
Xml::add_element($doc, $suggest, "dfrn:request", $item['request']);
Xml::add_element($doc, $suggest, "dfrn:note", $item['note']);
$root->appendChild($suggest);
@ -489,18 +489,18 @@ class DFRN
$relocate = $doc->createElement("dfrn:relocate");
xml::add_element($doc, $relocate, "dfrn:url", $owner['url']);
xml::add_element($doc, $relocate, "dfrn:name", $owner['name']);
xml::add_element($doc, $relocate, "dfrn:addr", $owner['addr']);
xml::add_element($doc, $relocate, "dfrn:avatar", $owner['avatar']);
xml::add_element($doc, $relocate, "dfrn:photo", $photos[4]);
xml::add_element($doc, $relocate, "dfrn:thumb", $photos[5]);
xml::add_element($doc, $relocate, "dfrn:micro", $photos[6]);
xml::add_element($doc, $relocate, "dfrn:request", $owner['request']);
xml::add_element($doc, $relocate, "dfrn:confirm", $owner['confirm']);
xml::add_element($doc, $relocate, "dfrn:notify", $owner['notify']);
xml::add_element($doc, $relocate, "dfrn:poll", $owner['poll']);
xml::add_element($doc, $relocate, "dfrn:sitepubkey", Config::get('system','site_pubkey'));
Xml::add_element($doc, $relocate, "dfrn:url", $owner['url']);
Xml::add_element($doc, $relocate, "dfrn:name", $owner['name']);
Xml::add_element($doc, $relocate, "dfrn:addr", $owner['addr']);
Xml::add_element($doc, $relocate, "dfrn:avatar", $owner['avatar']);
Xml::add_element($doc, $relocate, "dfrn:photo", $photos[4]);
Xml::add_element($doc, $relocate, "dfrn:thumb", $photos[5]);
Xml::add_element($doc, $relocate, "dfrn:micro", $photos[6]);
Xml::add_element($doc, $relocate, "dfrn:request", $owner['request']);
Xml::add_element($doc, $relocate, "dfrn:confirm", $owner['confirm']);
Xml::add_element($doc, $relocate, "dfrn:notify", $owner['notify']);
Xml::add_element($doc, $relocate, "dfrn:poll", $owner['poll']);
Xml::add_element($doc, $relocate, "dfrn:sitepubkey", Config::get('system','site_pubkey'));
$root->appendChild($relocate);
@ -539,17 +539,17 @@ class DFRN
$root->setAttribute("xmlns:ostatus", NAMESPACE_OSTATUS);
$root->setAttribute("xmlns:statusnet", NAMESPACE_STATUSNET);
xml::add_element($doc, $root, "id", System::baseUrl()."/profile/".$owner["nick"]);
xml::add_element($doc, $root, "title", $owner["name"]);
Xml::add_element($doc, $root, "id", System::baseUrl()."/profile/".$owner["nick"]);
Xml::add_element($doc, $root, "title", $owner["name"]);
$attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
xml::add_element($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
Xml::add_element($doc, $root, "generator", FRIENDICA_PLATFORM, $attributes);
$attributes = array("rel" => "license", "href" => "http://creativecommons.org/licenses/by/3.0/");
xml::add_element($doc, $root, "link", "", $attributes);
Xml::add_element($doc, $root, "link", "", $attributes);
$attributes = array("rel" => "alternate", "type" => "text/html", "href" => $alternatelink);
xml::add_element($doc, $root, "link", "", $attributes);
Xml::add_element($doc, $root, "link", "", $attributes);
if ($public) {
@ -557,26 +557,26 @@ class DFRN
ostatus::hublinks($doc, $root, $owner["nick"]);
$attributes = array("rel" => "salmon", "href" => System::baseUrl()."/salmon/".$owner["nick"]);
xml::add_element($doc, $root, "link", "", $attributes);
Xml::add_element($doc, $root, "link", "", $attributes);
$attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-replies", "href" => System::baseUrl()."/salmon/".$owner["nick"]);
xml::add_element($doc, $root, "link", "", $attributes);
Xml::add_element($doc, $root, "link", "", $attributes);
$attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-mention", "href" => System::baseUrl()."/salmon/".$owner["nick"]);
xml::add_element($doc, $root, "link", "", $attributes);
Xml::add_element($doc, $root, "link", "", $attributes);
}
// For backward compatibility we keep this element
if ($owner['page-flags'] == PAGE_COMMUNITY) {
xml::add_element($doc, $root, "dfrn:community", 1);
Xml::add_element($doc, $root, "dfrn:community", 1);
}
// The former element is replaced by this one
xml::add_element($doc, $root, "dfrn:account_type", $owner["account-type"]);
Xml::add_element($doc, $root, "dfrn:account_type", $owner["account-type"]);
/// @todo We need a way to transmit the different page flags like "PAGE_PRVGROUP"
xml::add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
Xml::add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
$author = self::add_author($doc, $owner, $authorelement, $public);
$root->appendChild($author);
@ -620,9 +620,9 @@ class DFRN
$attributes = array("dfrn:updated" => $namdate);
}
xml::add_element($doc, $author, "name", $owner["name"], $attributes);
xml::add_element($doc, $author, "uri", System::baseUrl().'/profile/'.$owner["nickname"], $attributes);
xml::add_element($doc, $author, "dfrn:handle", $owner["addr"], $attributes);
Xml::add_element($doc, $author, "name", $owner["name"], $attributes);
Xml::add_element($doc, $author, "uri", System::baseUrl().'/profile/'.$owner["nickname"], $attributes);
Xml::add_element($doc, $author, "dfrn:handle", $owner["addr"], $attributes);
$attributes = array("rel" => "photo", "type" => "image/jpeg",
"media:width" => 175, "media:height" => 175, "href" => $owner['photo']);
@ -631,13 +631,13 @@ class DFRN
$attributes["dfrn:updated"] = $picdate;
}
xml::add_element($doc, $author, "link", "", $attributes);
Xml::add_element($doc, $author, "link", "", $attributes);
$attributes["rel"] = "avatar";
xml::add_element($doc, $author, "link", "", $attributes);
Xml::add_element($doc, $author, "link", "", $attributes);
if ($hidewall) {
xml::add_element($doc, $author, "dfrn:hide", "true");
Xml::add_element($doc, $author, "dfrn:hide", "true");
}
// The following fields will only be generated if the data isn't meant for a public feed
@ -648,7 +648,7 @@ class DFRN
$birthday = feed_birthday($owner['uid'], $owner['timezone']);
if ($birthday) {
xml::add_element($doc, $author, "dfrn:birthday", $birthday);
Xml::add_element($doc, $author, "dfrn:birthday", $birthday);
}
// Only show contact details when we are allowed to
@ -664,26 +664,26 @@ class DFRN
if (DBM::is_result($r)) {
$profile = $r[0];
xml::add_element($doc, $author, "poco:displayName", $profile["name"]);
xml::add_element($doc, $author, "poco:updated", $namdate);
Xml::add_element($doc, $author, "poco:displayName", $profile["name"]);
Xml::add_element($doc, $author, "poco:updated", $namdate);
if (trim($profile["dob"]) > '0001-01-01') {
xml::add_element($doc, $author, "poco:birthday", "0000-".date("m-d", strtotime($profile["dob"])));
Xml::add_element($doc, $author, "poco:birthday", "0000-".date("m-d", strtotime($profile["dob"])));
}
xml::add_element($doc, $author, "poco:note", $profile["about"]);
xml::add_element($doc, $author, "poco:preferredUsername", $profile["nickname"]);
Xml::add_element($doc, $author, "poco:note", $profile["about"]);
Xml::add_element($doc, $author, "poco:preferredUsername", $profile["nickname"]);
$savetz = date_default_timezone_get();
date_default_timezone_set($profile["timezone"]);
xml::add_element($doc, $author, "poco:utcOffset", date("P"));
Xml::add_element($doc, $author, "poco:utcOffset", date("P"));
date_default_timezone_set($savetz);
if (trim($profile["homepage"]) != "") {
$urls = $doc->createElement("poco:urls");
xml::add_element($doc, $urls, "poco:type", "homepage");
xml::add_element($doc, $urls, "poco:value", $profile["homepage"]);
xml::add_element($doc, $urls, "poco:primary", "true");
Xml::add_element($doc, $urls, "poco:type", "homepage");
Xml::add_element($doc, $urls, "poco:value", $profile["homepage"]);
Xml::add_element($doc, $urls, "poco:primary", "true");
$author->appendChild($urls);
}
@ -691,33 +691,33 @@ class DFRN
$keywords = explode(",", $profile["pub_keywords"]);
foreach ($keywords AS $keyword) {
xml::add_element($doc, $author, "poco:tags", trim($keyword));
Xml::add_element($doc, $author, "poco:tags", trim($keyword));
}
}
if (trim($profile["xmpp"]) != "") {
$ims = $doc->createElement("poco:ims");
xml::add_element($doc, $ims, "poco:type", "xmpp");
xml::add_element($doc, $ims, "poco:value", $profile["xmpp"]);
xml::add_element($doc, $ims, "poco:primary", "true");
Xml::add_element($doc, $ims, "poco:type", "xmpp");
Xml::add_element($doc, $ims, "poco:value", $profile["xmpp"]);
Xml::add_element($doc, $ims, "poco:primary", "true");
$author->appendChild($ims);
}
if (trim($profile["locality"].$profile["region"].$profile["country-name"]) != "") {
$element = $doc->createElement("poco:address");
xml::add_element($doc, $element, "poco:formatted", formatted_location($profile));
Xml::add_element($doc, $element, "poco:formatted", formatted_location($profile));
if (trim($profile["locality"]) != "") {
xml::add_element($doc, $element, "poco:locality", $profile["locality"]);
Xml::add_element($doc, $element, "poco:locality", $profile["locality"]);
}
if (trim($profile["region"]) != "") {
xml::add_element($doc, $element, "poco:region", $profile["region"]);
Xml::add_element($doc, $element, "poco:region", $profile["region"]);
}
if (trim($profile["country-name"]) != "") {
xml::add_element($doc, $element, "poco:country", $profile["country-name"]);
Xml::add_element($doc, $element, "poco:country", $profile["country-name"]);
}
$author->appendChild($element);
@ -744,9 +744,9 @@ class DFRN
$contact = get_contact_details_by_url($contact_url, $item["uid"]);
$author = $doc->createElement($element);
xml::add_element($doc, $author, "name", $contact["name"]);
xml::add_element($doc, $author, "uri", $contact["url"]);
xml::add_element($doc, $author, "dfrn:handle", $contact["addr"]);
Xml::add_element($doc, $author, "name", $contact["name"]);
Xml::add_element($doc, $author, "uri", $contact["url"]);
Xml::add_element($doc, $author, "dfrn:handle", $contact["addr"]);
/// @Todo
/// - Check real image type and image size
@ -757,7 +757,7 @@ class DFRN
"media:width" => 80,
"media:height" => 80,
"href" => $contact["photo"]);
xml::add_element($doc, $author, "link", "", $attributes);
Xml::add_element($doc, $author, "link", "", $attributes);
$attributes = array(
"rel" => "avatar",
@ -765,7 +765,7 @@ class DFRN
"media:width" => 80,
"media:height" => 80,
"href" => $contact["photo"]);
xml::add_element($doc, $author, "link", "", $attributes);
Xml::add_element($doc, $author, "link", "", $attributes);
return $author;
}
@ -790,13 +790,13 @@ class DFRN
return false;
}
if ($r->type) {
xml::add_element($doc, $entry, "activity:object-type", $r->type);
Xml::add_element($doc, $entry, "activity:object-type", $r->type);
}
if ($r->id) {
xml::add_element($doc, $entry, "id", $r->id);
Xml::add_element($doc, $entry, "id", $r->id);
}
if ($r->title) {
xml::add_element($doc, $entry, "title", $r->title);
Xml::add_element($doc, $entry, "title", $r->title);
}
if ($r->link) {
@ -815,16 +815,16 @@ class DFRN
foreach ($link->attributes() AS $parameter => $value) {
$attributes[$parameter] = $value;
}
xml::add_element($doc, $entry, "link", "", $attributes);
Xml::add_element($doc, $entry, "link", "", $attributes);
}
}
} else {
$attributes = array("rel" => "alternate", "type" => "text/html", "href" => $r->link);
xml::add_element($doc, $entry, "link", "", $attributes);
Xml::add_element($doc, $entry, "link", "", $attributes);
}
}
if ($r->content) {
xml::add_element($doc, $entry, "content", bbcode($r->content), array("type" => "html"));
Xml::add_element($doc, $entry, "content", bbcode($r->content), array("type" => "html"));
}
return $entry;
@ -863,7 +863,7 @@ class DFRN
$attributes["title"] = trim($matches[4]);
}
xml::add_element($doc, $root, "link", "", $attributes);
Xml::add_element($doc, $root, "link", "", $attributes);
}
}
}
@ -893,7 +893,7 @@ class DFRN
if ($item['deleted']) {
$attributes = array("ref" => $item['uri'], "when" => datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00', ATOM_TIME));
return xml::create_element($doc, "at:deleted-entry", "", $attributes);
return Xml::create_element($doc, "at:deleted-entry", "", $attributes);
}
if (!$single) {
@ -944,7 +944,7 @@ class DFRN
$attributes = array("ref" => $parent_item, "type" => "text/html",
"href" => $parent[0]['plink'],
"dfrn:diaspora_guid" => $parent[0]['guid']);
xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
Xml::add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
}
// Add conversation data. This is used for OStatus
@ -967,23 +967,23 @@ class DFRN
"href" => $conversation_href,
"ref" => $conversation_uri);
xml::add_element($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
Xml::add_element($doc, $entry, "ostatus:conversation", $conversation_uri, $attributes);
xml::add_element($doc, $entry, "id", $item["uri"]);
xml::add_element($doc, $entry, "title", $item["title"]);
Xml::add_element($doc, $entry, "id", $item["uri"]);
Xml::add_element($doc, $entry, "title", $item["title"]);
xml::add_element($doc, $entry, "published", datetime_convert("UTC", "UTC", $item["created"] . "+00:00", ATOM_TIME));
xml::add_element($doc, $entry, "updated", datetime_convert("UTC", "UTC", $item["edited"] . "+00:00", ATOM_TIME));
Xml::add_element($doc, $entry, "published", datetime_convert("UTC", "UTC", $item["created"] . "+00:00", ATOM_TIME));
Xml::add_element($doc, $entry, "updated", datetime_convert("UTC", "UTC", $item["edited"] . "+00:00", ATOM_TIME));
// "dfrn:env" is used to read the content
xml::add_element($doc, $entry, "dfrn:env", base64url_encode($body, true));
Xml::add_element($doc, $entry, "dfrn:env", base64url_encode($body, true));
// The "content" field is not read by the receiver. We could remove it when the type is "text"
// We keep it at the moment, maybe there is some old version that doesn't read "dfrn:env"
xml::add_element($doc, $entry, "content", (($type == 'html') ? $htmlbody : $body), array("type" => $type));
Xml::add_element($doc, $entry, "content", (($type == 'html') ? $htmlbody : $body), array("type" => $type));
// We save this value in "plink". Maybe we should read it from there as well?
xml::add_element(
Xml::add_element(
$doc,
$entry,
"link",
@ -995,50 +995,50 @@ class DFRN
// "comment-allow" is some old fashioned stuff for old Friendica versions.
// It is included in the rewritten code for completeness
if ($comment) {
xml::add_element($doc, $entry, "dfrn:comment-allow", intval($item['last-child']));
Xml::add_element($doc, $entry, "dfrn:comment-allow", intval($item['last-child']));
}
if ($item['location']) {
xml::add_element($doc, $entry, "dfrn:location", $item['location']);
Xml::add_element($doc, $entry, "dfrn:location", $item['location']);
}
if ($item['coord']) {
xml::add_element($doc, $entry, "georss:point", $item['coord']);
Xml::add_element($doc, $entry, "georss:point", $item['coord']);
}
if (($item['private']) || strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
xml::add_element($doc, $entry, "dfrn:private", (($item['private']) ? $item['private'] : 1));
Xml::add_element($doc, $entry, "dfrn:private", (($item['private']) ? $item['private'] : 1));
}
if ($item['extid']) {
xml::add_element($doc, $entry, "dfrn:extid", $item['extid']);
Xml::add_element($doc, $entry, "dfrn:extid", $item['extid']);
}
if ($item['bookmark']) {
xml::add_element($doc, $entry, "dfrn:bookmark", "true");
Xml::add_element($doc, $entry, "dfrn:bookmark", "true");
}
if ($item['app']) {
xml::add_element($doc, $entry, "statusnet:notice_info", "", array("local_id" => $item['id'], "source" => $item['app']));
Xml::add_element($doc, $entry, "statusnet:notice_info", "", array("local_id" => $item['id'], "source" => $item['app']));
}
xml::add_element($doc, $entry, "dfrn:diaspora_guid", $item["guid"]);
Xml::add_element($doc, $entry, "dfrn:diaspora_guid", $item["guid"]);
// The signed text contains the content in Markdown, the sender handle and the signatur for the content
// It is needed for relayed comments to Diaspora.
if ($item['signed_text']) {
$sign = base64_encode(json_encode(array('signed_text' => $item['signed_text'],'signature' => $item['signature'],'signer' => $item['signer'])));
xml::add_element($doc, $entry, "dfrn:diaspora_signature", $sign);
Xml::add_element($doc, $entry, "dfrn:diaspora_signature", $sign);
}
xml::add_element($doc, $entry, "activity:verb", construct_verb($item));
Xml::add_element($doc, $entry, "activity:verb", construct_verb($item));
if ($item['object-type'] != "") {
xml::add_element($doc, $entry, "activity:object-type", $item['object-type']);
Xml::add_element($doc, $entry, "activity:object-type", $item['object-type']);
} elseif ($item['id'] == $item['parent']) {
xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
Xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_NOTE);
} else {
xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_COMMENT);
Xml::add_element($doc, $entry, "activity:object-type", ACTIVITY_OBJ_COMMENT);
}
$actobj = self::create_activity($doc, "activity:object", $item['object']);
@ -1056,7 +1056,7 @@ class DFRN
if (count($tags)) {
foreach ($tags as $t) {
if (($type != 'html') || ($t[0] != "@")) {
xml::add_element($doc, $entry, "category", "", array("scheme" => "X-DFRN:".$t[0].":".$t[1], "term" => $t[2]));
Xml::add_element($doc, $entry, "category", "", array("scheme" => "X-DFRN:".$t[0].":".$t[1], "term" => $t[2]));
}
}
}
@ -1077,7 +1077,7 @@ class DFRN
);
if (DBM::is_result($r) && ($r[0]["forum"] || $r[0]["prv"])) {
xml::add_element(
Xml::add_element(
$doc,
$entry,
"link",
@ -1087,7 +1087,7 @@ class DFRN
"href" => $mention)
);
} else {
xml::add_element(
Xml::add_element(
$doc,
$entry,
"link",
@ -1705,7 +1705,7 @@ class DFRN
$obj_element = $obj_doc->createElementNS(NAMESPACE_ATOM1, $element);
$activity_type = $xpath->query("activity:object-type/text()", $activity)->item(0)->nodeValue;
xml::add_element($obj_doc, $obj_element, "type", $activity_type);
Xml::add_element($obj_doc, $obj_element, "type", $activity_type);
$id = $xpath->query("atom:id", $activity)->item(0);
if (is_object($id)) {
@ -1719,7 +1719,7 @@ class DFRN
$links = $xpath->query("atom:link", $activity);
if (is_object($links)) {
foreach ($links AS $link) {
foreach ($links as $link) {
$obj_element->appendChild($obj_doc->importNode($link, true));
}
}

View file

@ -17,10 +17,10 @@ use Friendica\Core\PConfig;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Network\Probe;
use Friendica\Util\Xml;
use dba;
use SimpleXMLElement;
use xml;
require_once 'include/items.php';
require_once 'include/bb2diaspora.php';
@ -672,7 +672,7 @@ class Diaspora
if (!in_array($fieldname, array("parent_author_signature", "target_author_signature"))
|| ($orig_type == "relayable_retraction")
) {
xml::copy($entry, $fields, $fieldname);
Xml::copy($entry, $fields, $fieldname);
}
}
@ -1912,7 +1912,7 @@ class Diaspora
"title" => "",
"content" => $parent_body));
return xml::from_array($xmldata, $xml, true);
return Xml::from_array($xmldata, $xml, true);
}
/**
@ -2344,7 +2344,7 @@ class Diaspora
"id" => $contact["url"]."/".$contact["name"],
"link" => $link));
return xml::from_array($xmldata, $xml, true);
return Xml::from_array($xmldata, $xml, true);
}
/**
@ -3039,7 +3039,7 @@ class Diaspora
$namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
return xml::from_array($xmldata, $xml, false, $namespaces);
return Xml::from_array($xmldata, $xml, false, $namespaces);
}
/**
@ -3172,7 +3172,7 @@ class Diaspora
{
$data = array($type => $message);
return xml::from_array($data, $xml);
return Xml::from_array($data, $xml);
}
/**

411
src/Util/Xml.php Normal file
View file

@ -0,0 +1,411 @@
<?php
/**
* @file xrc/Util/Xml.php
*/
namespace Friendica\Util;
/**
* @brief This class contain methods to work with XML data
*/
class Xml
{
/**
* @brief Creates an XML structure out of a given array
*
* @param array $array The array of the XML structure that will be generated
* @param object $xml The createdXML will be returned by reference
* @param bool $remove_header Should the XML header be removed or not?
* @param array $namespaces List of namespaces
* @param bool $root interally used parameter. Mustn't be used from outside.
*
* @return string The created XML
*/
public static function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
if ($root) {
foreach ($array as $key => $value) {
foreach ($namespaces as $nskey => $nsvalue) {
$key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
}
if (is_array($value)) {
$root = new SimpleXMLElement("<".$key."/>");
self::from_array($value, $root, $remove_header, $namespaces, false);
} else {
$root = new SimpleXMLElement("<".$key.">".xmlify($value)."</".$key.">");
}
$dom = dom_import_simplexml($root)->ownerDocument;
$dom->formatOutput = true;
$xml = $dom;
$xml_text = $dom->saveXML();
if ($remove_header) {
$xml_text = trim(substr($xml_text, 21));
}
return $xml_text;
}
}
foreach ($array as $key => $value) {
if (!isset($element) && isset($xml)) {
$element = $xml;
}
if (is_integer($key)) {
if (isset($element)) {
if (is_scalar($value)) {
$element[0] = $value;
} else {
/// @todo: handle nested array values
}
}
continue;
}
$element_parts = explode(":", $key);
if ((count($element_parts) > 1) && isset($namespaces[$element_parts[0]])) {
$namespace = $namespaces[$element_parts[0]];
} elseif (isset($namespaces[""])) {
$namespace = $namespaces[""];
} else {
$namespace = null;
}
// Remove undefined namespaces from the key
if ((count($element_parts) > 1) && is_null($namespace)) {
$key = $element_parts[1];
}
if (substr($key, 0, 11) == "@attributes") {
if (!isset($element) || !is_array($value)) {
continue;
}
foreach ($value as $attr_key => $attr_value) {
$element_parts = explode(":", $attr_key);
if ((count($element_parts) > 1) && isset($namespaces[$element_parts[0]])) {
$namespace = $namespaces[$element_parts[0]];
} else {
$namespace = null;
}
$element->addAttribute($attr_key, $attr_value, $namespace);
}
continue;
}
if (!is_array($value)) {
$element = $xml->addChild($key, xmlify($value), $namespace);
} elseif (is_array($value)) {
$element = $xml->addChild($key, null, $namespace);
self::from_array($value, $element, $remove_header, $namespaces, false);
}
}
}
/**
* @brief Copies an XML object
*
* @param object $source The XML source
* @param object $target The XML target
* @param string $elementname Name of the XML element of the target
*/
public static function copy(&$source, &$target, $elementname)
{
if (count($source->children()) == 0) {
$target->addChild($elementname, xmlify($source));
} else {
$child = $target->addChild($elementname);
foreach ($source->children() as $childfield => $childentry) {
self::copy($childentry, $child, $childfield);
}
}
}
/**
* @brief Create an XML element
*
* @param object $doc XML root
* @param string $element XML element name
* @param string $value XML value
* @param array $attributes array containing the attributes
*
* @return object XML element object
*/
public static function create_element($doc, $element, $value = "", $attributes = array())
{
$element = $doc->createElement($element, xmlify($value));
foreach ($attributes as $key => $value) {
$attribute = $doc->createAttribute($key);
$attribute->value = xmlify($value);
$element->appendChild($attribute);
}
return $element;
}
/**
* @brief Create an XML and append it to the parent object
*
* @param object $doc XML root
* @param object $parent parent object
* @param string $element XML element name
* @param string $value XML value
* @param array $attributes array containing the attributes
*/
public static function add_element($doc, $parent, $element, $value = "", $attributes = array())
{
$element = self::create_element($doc, $element, $value, $attributes);
$parent->appendChild($element);
}
/**
* @brief Convert an XML document to a normalised, case-corrected array
* used by webfinger
*
* @param object $xml_element The XML document
* @param integer $recursion_depth recursion counter for internal use - default 0
* internal use, recursion counter
*
* @return array | sring The array from the xml element or the string
*/
public static function element_to_array($xml_element, &$recursion_depth=0)
{
// If we're getting too deep, bail out
if ($recursion_depth > 512) {
return(null);
}
if (!is_string($xml_element)
&& !is_array($xml_element)
&& (get_class($xml_element) == 'SimpleXMLElement')
) {
$xml_element_copy = $xml_element;
$xml_element = get_object_vars($xml_element);
}
if (is_array($xml_element)) {
$result_array = array();
if (count($xml_element) <= 0) {
return (trim(strval($xml_element_copy)));
}
foreach ($xml_element as $key => $value) {
$recursion_depth++;
$result_array[strtolower($key)] = self::element_to_array($value, $recursion_depth);
$recursion_depth--;
}
if ($recursion_depth == 0) {
$temp_array = $result_array;
$result_array = array(
strtolower($xml_element_copy->getName()) => $temp_array,
);
}
return ($result_array);
} else {
return (trim(strval($xml_element)));
}
}
/**
* @brief Convert the given XML text to an array in the XML structure.
*
* Xml::to_array() will convert the given XML text to an array in the XML structure.
* Link: http://www.bin-co.com/php/scripts/xml2array/
* Portions significantly re-written by mike@macgirvin.com for Friendica
* (namespaces, lowercase tags, get_attribute default changed, more...)
*
* Examples: $array = Xml::to_array(file_get_contents('feed.xml'));
* $array = Xml::to_array(file_get_contents('feed.xml', true, 1, 'attribute'));
*
* @param object $contents The XML text
* @param boolean $namespaces True or false include namespace information
* in the returned array as array elements.
* @param integer $get_attributes 1 or 0. If this is 1 the function will get the attributes as well as the tag values -
* this results in a different array structure in the return value.
* @param string $priority Can be 'tag' or 'attribute'. This will change the way the resulting
* array sturcture. For 'tag', the tags are given more importance.
*
* @return array The parsed XML in an array form. Use print_r() to see the resulting array structure.
*/
public static function to_array($contents, $namespaces = true, $get_attributes = 1, $priority = 'attribute')
{
if (!$contents) {
return array();
}
if (!function_exists('xml_parser_create')) {
logger('Xml::to_array: parser function missing');
return array();
}
libxml_use_internal_errors(true);
libxml_clear_errors();
if ($namespaces) {
$parser = @xml_parser_create_ns("UTF-8", ':');
} else {
$parser = @xml_parser_create();
}
if (! $parser) {
logger('Xml::to_array: xml_parser_create: no resource');
return array();
}
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
// http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
@xml_parse_into_struct($parser, trim($contents), $xml_values);
@xml_parser_free($parser);
if (! $xml_values) {
logger('Xml::to_array: libxml: parse error: ' . $contents, LOGGER_DATA);
foreach (libxml_get_errors() as $err) {
logger('libxml: parse: ' . $err->code . " at " . $err->line . ":" . $err->column . " : " . $err->message, LOGGER_DATA);
}
libxml_clear_errors();
return;
}
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array; // Reference
// Go through the tags.
$repeated_tag_index = array(); // Multiple tags with same name will be turned into an array
foreach ($xml_values as $data) {
unset($attributes, $value); // Remove existing values, or there will be trouble
// This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data); // We could use the array by itself, but this cooler.
$result = array();
$attributes_data = array();
if (isset($value)) {
if ($priority == 'tag') {
$result = $value;
} else {
$result['value'] = $value; // Put the value in a assoc array if we are in the 'Attribute' mode
}
}
//Set the attributes too.
if (isset($attributes) and $get_attributes) {
foreach ($attributes as $attr => $val) {
if ($priority == 'tag') {
$attributes_data[$attr] = $val;
} else {
$result['@attributes'][$attr] = $val; // Set all the attributes in a array called 'attr'
}
}
}
// See tag status and do the needed.
if ($namespaces && strpos($tag, ':')) {
$namespc = substr($tag, 0, strrpos($tag, ':'));
$tag = strtolower(substr($tag, strlen($namespc)+1));
$result['@namespace'] = $namespc;
}
$tag = strtolower($tag);
if ($type == "open") { // The starting of the tag '<tag>'
$parent[$level-1] = &$current;
if (!is_array($current) || (!in_array($tag, array_keys($current)))) { // Insert New tag
$current[$tag] = $result;
if ($attributes_data) {
$current[$tag. '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level] = 1;
$current = &$current[$tag];
} else { // There was another element with the same tag name
if (isset($current[$tag][0])) { // If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else { // This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag], $result); // This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;
if (isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}
} elseif ($type == "complete") { // Tags that ends in 1 line '<tag />'
//See if the key is already taken.
if (!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if ($priority == 'tag' and $attributes_data) {
$current[$tag. '_attr'] = $attributes_data;
}
} else { // If taken, put all things inside a list(array)
if (isset($current[$tag][0]) and is_array($current[$tag])) { // If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;
} else { // If it is not an array...
$current[$tag] = array($current[$tag], $result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if ($priority == 'tag' and $get_attributes) {
if (isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}
if ($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; // 0 and 1 indexes are already taken
}
}
} elseif ($type == 'close') { // End of tag '</tag>'
$current = &$parent[$level-1];
}
}
return($xml_array);
}
/**
* @brief Delete a node in a XML object
*
* @param object $doc XML document
* @param string $node Node name
*/
public static function deleteNode(&$doc, $node)
{
$xpath = new DomXPath($doc);
$list = $xpath->query("//".$node);
foreach ($list as $child) {
$child->parentNode->removeChild($child);
}
}
}