2010-03-20 21:06:22 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* StatusNet, the distributed open-source microblogging tool
|
|
|
|
*
|
|
|
|
* An activity
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* LICENCE: This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category Feed
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @copyright 2010 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('STATUSNET')) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
class PoCo
|
|
|
|
{
|
|
|
|
const NS = 'http://portablecontacts.net/spec/1.0';
|
|
|
|
|
|
|
|
const USERNAME = 'preferredUsername';
|
|
|
|
const DISPLAYNAME = 'displayName';
|
|
|
|
const NOTE = 'note';
|
|
|
|
|
|
|
|
public $preferredUsername;
|
|
|
|
public $displayName;
|
|
|
|
public $note;
|
|
|
|
public $address;
|
|
|
|
public $urls = array();
|
|
|
|
|
|
|
|
function __construct($element = null)
|
|
|
|
{
|
|
|
|
if (empty($element)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->preferredUsername = ActivityUtils::childContent(
|
|
|
|
$element,
|
|
|
|
self::USERNAME,
|
|
|
|
self::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->displayName = ActivityUtils::childContent(
|
|
|
|
$element,
|
|
|
|
self::DISPLAYNAME,
|
|
|
|
self::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->note = ActivityUtils::childContent(
|
|
|
|
$element,
|
|
|
|
self::NOTE,
|
|
|
|
self::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->address = $this->_getAddress($element);
|
|
|
|
$this->urls = $this->_getURLs($element);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getURLs($element)
|
|
|
|
{
|
|
|
|
$urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
|
|
|
|
$urls = array();
|
|
|
|
|
|
|
|
foreach ($urlEls as $urlEl) {
|
|
|
|
|
|
|
|
$type = ActivityUtils::childContent(
|
|
|
|
$urlEl,
|
|
|
|
PoCoURL::TYPE,
|
|
|
|
PoCo::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$value = ActivityUtils::childContent(
|
|
|
|
$urlEl,
|
|
|
|
PoCoURL::VALUE,
|
|
|
|
PoCo::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$primary = ActivityUtils::childContent(
|
|
|
|
$urlEl,
|
|
|
|
PoCoURL::PRIMARY,
|
|
|
|
PoCo::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
$isPrimary = false;
|
|
|
|
|
|
|
|
if (isset($primary) && $primary == 'true') {
|
|
|
|
$isPrimary = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @todo check to make sure a primary hasn't already been added
|
|
|
|
|
|
|
|
array_push($urls, new PoCoURL($type, $value, $isPrimary));
|
|
|
|
}
|
|
|
|
return $urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _getAddress($element)
|
|
|
|
{
|
|
|
|
$addressEl = ActivityUtils::child(
|
|
|
|
$element,
|
|
|
|
PoCoAddress::ADDRESS,
|
|
|
|
PoCo::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($addressEl)) {
|
|
|
|
$formatted = ActivityUtils::childContent(
|
|
|
|
$addressEl,
|
|
|
|
PoCoAddress::FORMATTED,
|
|
|
|
self::NS
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($formatted)) {
|
|
|
|
$address = new PoCoAddress();
|
|
|
|
$address->formatted = $formatted;
|
|
|
|
return $address;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-10-06 02:48:41 +01:00
|
|
|
static function fromProfile(Profile $profile)
|
2010-03-20 21:06:22 +00:00
|
|
|
{
|
|
|
|
$poco = new PoCo();
|
|
|
|
|
|
|
|
$poco->preferredUsername = $profile->nickname;
|
|
|
|
$poco->displayName = $profile->getBestName();
|
|
|
|
|
|
|
|
$poco->note = $profile->bio;
|
|
|
|
|
|
|
|
$paddy = new PoCoAddress();
|
|
|
|
$paddy->formatted = $profile->location;
|
|
|
|
$poco->address = $paddy;
|
|
|
|
|
|
|
|
if (!empty($profile->homepage)) {
|
|
|
|
array_push(
|
|
|
|
$poco->urls,
|
|
|
|
new PoCoURL(
|
|
|
|
'homepage',
|
|
|
|
$profile->homepage,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $poco;
|
|
|
|
}
|
|
|
|
|
2013-10-06 02:48:41 +01:00
|
|
|
static function fromGroup(User_group $group)
|
2010-03-20 21:06:22 +00:00
|
|
|
{
|
|
|
|
$poco = new PoCo();
|
|
|
|
|
|
|
|
$poco->preferredUsername = $group->nickname;
|
|
|
|
$poco->displayName = $group->getBestName();
|
|
|
|
|
|
|
|
$poco->note = $group->description;
|
|
|
|
|
|
|
|
$paddy = new PoCoAddress();
|
|
|
|
$paddy->formatted = $group->location;
|
|
|
|
$poco->address = $paddy;
|
|
|
|
|
|
|
|
if (!empty($group->homepage)) {
|
|
|
|
array_push(
|
|
|
|
$poco->urls,
|
|
|
|
new PoCoURL(
|
|
|
|
'homepage',
|
|
|
|
$group->homepage,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $poco;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPrimaryURL()
|
|
|
|
{
|
|
|
|
foreach ($this->urls as $url) {
|
|
|
|
if ($url->primary) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function asString()
|
|
|
|
{
|
|
|
|
$xs = new XMLStringer(true);
|
2010-12-27 17:46:25 +00:00
|
|
|
$this->outputTo($xs);
|
|
|
|
return $xs->getString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function outputTo($xo)
|
|
|
|
{
|
|
|
|
$xo->element(
|
2010-03-20 21:06:22 +00:00
|
|
|
'poco:preferredUsername',
|
|
|
|
null,
|
|
|
|
$this->preferredUsername
|
|
|
|
);
|
|
|
|
|
2010-12-27 17:46:25 +00:00
|
|
|
$xo->element(
|
2010-03-20 21:06:22 +00:00
|
|
|
'poco:displayName',
|
|
|
|
null,
|
|
|
|
$this->displayName
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($this->note)) {
|
2010-12-27 17:46:25 +00:00
|
|
|
$xo->element('poco:note', null, common_xml_safe_str($this->note));
|
2010-03-20 21:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->address)) {
|
2010-12-27 17:46:25 +00:00
|
|
|
$this->address->outputTo($xo);
|
2010-03-20 21:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->urls as $url) {
|
2010-12-27 17:46:25 +00:00
|
|
|
$url->outputTo($xo);
|
2010-03-20 21:06:22 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-18 06:36:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Output a Portable Contact as an array suitable for serializing
|
|
|
|
* as JSON
|
|
|
|
*
|
|
|
|
* @return $array the PoCo array
|
|
|
|
*/
|
|
|
|
|
|
|
|
function asArray()
|
|
|
|
{
|
|
|
|
$poco = array();
|
|
|
|
|
|
|
|
$poco['preferredUsername'] = $this->preferredUsername;
|
|
|
|
$poco['displayName'] = $this->displayName;
|
|
|
|
|
|
|
|
if (!empty($this->note)) {
|
|
|
|
$poco['note'] = $this->note;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->address)) {
|
|
|
|
$poco['addresses'] = $this->address->asArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($this->urls)) {
|
|
|
|
|
|
|
|
$urls = array();
|
|
|
|
|
|
|
|
foreach ($this->urls as $url) {
|
|
|
|
$urls[] = $url->asArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
$poco['urls'] = $urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $poco;
|
|
|
|
}
|
|
|
|
|
2010-03-20 21:06:22 +00:00
|
|
|
}
|
2011-02-18 06:36:14 +00:00
|
|
|
|