poco.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. const NS = 'http://portablecontacts.net/spec/1.0';
  36. const USERNAME = 'preferredUsername';
  37. const DISPLAYNAME = 'displayName';
  38. const NOTE = 'note';
  39. public $preferredUsername;
  40. public $displayName;
  41. public $note;
  42. public $address;
  43. public $urls = array();
  44. function __construct($element = null)
  45. {
  46. if (empty($element)) {
  47. return;
  48. }
  49. $this->preferredUsername = ActivityUtils::childContent(
  50. $element,
  51. self::USERNAME,
  52. self::NS
  53. );
  54. $this->displayName = ActivityUtils::childContent(
  55. $element,
  56. self::DISPLAYNAME,
  57. self::NS
  58. );
  59. $this->note = ActivityUtils::childContent(
  60. $element,
  61. self::NOTE,
  62. self::NS
  63. );
  64. $this->address = $this->_getAddress($element);
  65. $this->urls = $this->_getURLs($element);
  66. }
  67. private function _getURLs($element)
  68. {
  69. $urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
  70. $urls = array();
  71. foreach ($urlEls as $urlEl) {
  72. $type = ActivityUtils::childContent(
  73. $urlEl,
  74. PoCoURL::TYPE,
  75. PoCo::NS
  76. );
  77. $value = ActivityUtils::childContent(
  78. $urlEl,
  79. PoCoURL::VALUE,
  80. PoCo::NS
  81. );
  82. $primary = ActivityUtils::childContent(
  83. $urlEl,
  84. PoCoURL::PRIMARY,
  85. PoCo::NS
  86. );
  87. $isPrimary = false;
  88. if (isset($primary) && $primary == 'true') {
  89. $isPrimary = true;
  90. }
  91. // @todo check to make sure a primary hasn't already been added
  92. array_push($urls, new PoCoURL($type, $value, $isPrimary));
  93. }
  94. return $urls;
  95. }
  96. private function _getAddress($element)
  97. {
  98. $addressEl = ActivityUtils::child(
  99. $element,
  100. PoCoAddress::ADDRESS,
  101. PoCo::NS
  102. );
  103. if (!empty($addressEl)) {
  104. $formatted = ActivityUtils::childContent(
  105. $addressEl,
  106. PoCoAddress::FORMATTED,
  107. self::NS
  108. );
  109. if (!empty($formatted)) {
  110. $address = new PoCoAddress();
  111. $address->formatted = $formatted;
  112. return $address;
  113. }
  114. }
  115. return null;
  116. }
  117. static function fromProfile(Profile $profile)
  118. {
  119. $poco = new PoCo();
  120. $poco->preferredUsername = $profile->nickname;
  121. $poco->displayName = $profile->getBestName();
  122. $poco->note = $profile->bio;
  123. $paddy = new PoCoAddress();
  124. $paddy->formatted = $profile->location;
  125. $poco->address = $paddy;
  126. if (!empty($profile->homepage)) {
  127. array_push(
  128. $poco->urls,
  129. new PoCoURL(
  130. 'homepage',
  131. $profile->homepage,
  132. true
  133. )
  134. );
  135. }
  136. return $poco;
  137. }
  138. static function fromGroup(User_group $group)
  139. {
  140. $poco = new PoCo();
  141. $poco->preferredUsername = $group->nickname;
  142. $poco->displayName = $group->getBestName();
  143. $poco->note = $group->description;
  144. $paddy = new PoCoAddress();
  145. $paddy->formatted = $group->location;
  146. $poco->address = $paddy;
  147. if (!empty($group->homepage)) {
  148. array_push(
  149. $poco->urls,
  150. new PoCoURL(
  151. 'homepage',
  152. $group->homepage,
  153. true
  154. )
  155. );
  156. }
  157. return $poco;
  158. }
  159. function getPrimaryURL()
  160. {
  161. foreach ($this->urls as $url) {
  162. if ($url->primary) {
  163. return $url;
  164. }
  165. }
  166. }
  167. function asString()
  168. {
  169. $xs = new XMLStringer(true);
  170. $this->outputTo($xs);
  171. return $xs->getString();
  172. }
  173. function outputTo($xo)
  174. {
  175. $xo->element(
  176. 'poco:preferredUsername',
  177. null,
  178. $this->preferredUsername
  179. );
  180. $xo->element(
  181. 'poco:displayName',
  182. null,
  183. $this->displayName
  184. );
  185. if (!empty($this->note)) {
  186. $xo->element('poco:note', null, common_xml_safe_str($this->note));
  187. }
  188. if (!empty($this->address)) {
  189. $this->address->outputTo($xo);
  190. }
  191. foreach ($this->urls as $url) {
  192. $url->outputTo($xo);
  193. }
  194. }
  195. /**
  196. * Output a Portable Contact as an array suitable for serializing
  197. * as JSON
  198. *
  199. * @return $array the PoCo array
  200. */
  201. function asArray()
  202. {
  203. $poco = array();
  204. $poco['preferredUsername'] = $this->preferredUsername;
  205. $poco['displayName'] = $this->displayName;
  206. if (!empty($this->note)) {
  207. $poco['note'] = $this->note;
  208. }
  209. if (!empty($this->address)) {
  210. $poco['addresses'] = $this->address->asArray();
  211. }
  212. if (!empty($this->urls)) {
  213. $urls = array();
  214. foreach ($this->urls as $url) {
  215. $urls[] = $url->asArray();
  216. }
  217. $poco['urls'] = $urls;
  218. }
  219. return $poco;
  220. }
  221. }