poco.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * An activity
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Feed
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Zach Copley <zach@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. class PoCo
  34. {
  35. public $widgetOpts;
  36. public $scoped;
  37. const NS = 'http://portablecontacts.net/spec/1.0';
  38. const USERNAME = 'preferredUsername';
  39. const DISPLAYNAME = 'displayName';
  40. const NOTE = 'note';
  41. public $preferredUsername;
  42. public $displayName;
  43. public $note;
  44. public $address;
  45. public $urls = array();
  46. function __construct($element = null)
  47. {
  48. if (empty($element)) {
  49. return;
  50. }
  51. $this->preferredUsername = ActivityUtils::childContent(
  52. $element,
  53. self::USERNAME,
  54. self::NS
  55. );
  56. $this->displayName = ActivityUtils::childContent(
  57. $element,
  58. self::DISPLAYNAME,
  59. self::NS
  60. );
  61. $this->note = ActivityUtils::childContent(
  62. $element,
  63. self::NOTE,
  64. self::NS
  65. );
  66. $this->address = $this->_getAddress($element);
  67. $this->urls = $this->_getURLs($element);
  68. }
  69. private function _getURLs($element)
  70. {
  71. $urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
  72. $urls = array();
  73. foreach ($urlEls as $urlEl) {
  74. $type = ActivityUtils::childContent(
  75. $urlEl,
  76. PoCoURL::TYPE,
  77. PoCo::NS
  78. );
  79. $value = ActivityUtils::childContent(
  80. $urlEl,
  81. PoCoURL::VALUE,
  82. PoCo::NS
  83. );
  84. $primary = ActivityUtils::childContent(
  85. $urlEl,
  86. PoCoURL::PRIMARY,
  87. PoCo::NS
  88. );
  89. $isPrimary = false;
  90. if (isset($primary) && $primary == 'true') {
  91. $isPrimary = true;
  92. }
  93. // @todo check to make sure a primary hasn't already been added
  94. array_push($urls, new PoCoURL($type, $value, $isPrimary));
  95. }
  96. return $urls;
  97. }
  98. private function _getAddress($element)
  99. {
  100. $addressEl = ActivityUtils::child(
  101. $element,
  102. PoCoAddress::ADDRESS,
  103. PoCo::NS
  104. );
  105. if (!empty($addressEl)) {
  106. $formatted = ActivityUtils::childContent(
  107. $addressEl,
  108. PoCoAddress::FORMATTED,
  109. self::NS
  110. );
  111. if (!empty($formatted)) {
  112. $address = new PoCoAddress();
  113. $address->formatted = $formatted;
  114. return $address;
  115. }
  116. }
  117. return null;
  118. }
  119. static function fromProfile(Profile $profile)
  120. {
  121. $poco = new PoCo();
  122. $poco->preferredUsername = $profile->nickname;
  123. $poco->displayName = $profile->getBestName();
  124. $poco->note = $profile->bio;
  125. $paddy = new PoCoAddress();
  126. $paddy->formatted = $profile->location;
  127. $poco->address = $paddy;
  128. if (!empty($profile->homepage)) {
  129. array_push(
  130. $poco->urls,
  131. new PoCoURL(
  132. 'homepage',
  133. $profile->homepage,
  134. true
  135. )
  136. );
  137. }
  138. return $poco;
  139. }
  140. static function fromGroup(User_group $group)
  141. {
  142. $poco = new PoCo();
  143. $poco->preferredUsername = $group->nickname;
  144. $poco->displayName = $group->getBestName();
  145. $poco->note = $group->description;
  146. $paddy = new PoCoAddress();
  147. $paddy->formatted = $group->location;
  148. $poco->address = $paddy;
  149. if (!empty($group->homepage)) {
  150. array_push(
  151. $poco->urls,
  152. new PoCoURL(
  153. 'homepage',
  154. $group->homepage,
  155. true
  156. )
  157. );
  158. }
  159. return $poco;
  160. }
  161. function getPrimaryURL()
  162. {
  163. foreach ($this->urls as $url) {
  164. if ($url->primary) {
  165. return $url;
  166. }
  167. }
  168. }
  169. function asString()
  170. {
  171. $xs = new XMLStringer(true);
  172. $this->outputTo($xs);
  173. return $xs->getString();
  174. }
  175. function outputTo($xo)
  176. {
  177. $xo->element(
  178. 'poco:preferredUsername',
  179. null,
  180. $this->preferredUsername
  181. );
  182. $xo->element(
  183. 'poco:displayName',
  184. null,
  185. $this->displayName
  186. );
  187. if (!empty($this->note)) {
  188. $xo->element('poco:note', null, common_xml_safe_str($this->note));
  189. }
  190. if (!empty($this->address)) {
  191. $this->address->outputTo($xo);
  192. }
  193. foreach ($this->urls as $url) {
  194. $url->outputTo($xo);
  195. }
  196. }
  197. /**
  198. * Output a Portable Contact as an array suitable for serializing
  199. * as JSON
  200. *
  201. * @return $array the PoCo array
  202. */
  203. function asArray()
  204. {
  205. $poco = array();
  206. $poco['preferredUsername'] = $this->preferredUsername;
  207. $poco['displayName'] = $this->displayName;
  208. if (!empty($this->note)) {
  209. $poco['note'] = $this->note;
  210. }
  211. if (!empty($this->address)) {
  212. $poco['addresses'] = $this->address->asArray();
  213. }
  214. if (!empty($this->urls)) {
  215. $urls = array();
  216. foreach ($this->urls as $url) {
  217. $urls[] = $url->asArray();
  218. }
  219. $poco['urls'] = $urls;
  220. }
  221. return $poco;
  222. }
  223. }