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