| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  | <?php | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  |  * StatusNet - the distributed open-source microblogging tool | 
					
						
							|  |  |  |  * Copyright (C) 2010, StatusNet, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Some utilities for generating hint data | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DiscoveryHints { | 
					
						
							| 
									
										
										
										
											2013-09-30 17:13:03 +02:00
										 |  |  |     static function fromXRD(XML_XRD $xrd) | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |     { | 
					
						
							|  |  |  |         $hints = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-04 14:46:45 +02:00
										 |  |  |         if (Event::handle('StartDiscoveryHintsFromXRD', array($xrd, &$hints))) { | 
					
						
							|  |  |  |             foreach ($xrd->links as $link) { | 
					
						
							|  |  |  |                 switch ($link->rel) { | 
					
						
							|  |  |  |                 case WebFingerResource_Profile::PROFILEPAGE: | 
					
						
							|  |  |  |                     $hints['profileurl'] = $link->href; | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 case Salmon::REL_SALMON: | 
					
						
							|  |  |  |                 case Salmon::NS_MENTIONS:   // XXX: deprecated, remove in the future
 | 
					
						
							|  |  |  |                 case Salmon::NS_REPLIES:    // XXX: deprecated, remove in the future
 | 
					
						
							|  |  |  |                     $hints['salmon'] = $link->href; | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 case Discovery::UPDATESFROM: | 
					
						
							|  |  |  |                     if (empty($link->type) || $link->type == 'application/atom+xml') { | 
					
						
							|  |  |  |                         $hints['feedurl'] = $link->href; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 case Discovery::HCARD: | 
					
						
							|  |  |  |                 case Discovery::MF2_HCARD: | 
					
						
							|  |  |  |                     $hints['hcard'] = $link->href; | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 default: | 
					
						
							|  |  |  |                     break; | 
					
						
							| 
									
										
										
										
											2012-05-04 12:01:13 -04:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-10-04 14:46:45 +02:00
										 |  |  |             Event::handle('EndDiscoveryHintsFromXRD', array($xrd, &$hints)); | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $hints; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static function fromHcardUrl($url) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $client = new HTTPClient(); | 
					
						
							|  |  |  |         $client->setHeader('Accept', 'text/html,application/xhtml+xml'); | 
					
						
							| 
									
										
										
										
											2016-01-11 02:36:59 +01:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $response = $client->get($url); | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-11 02:36:59 +01:00
										 |  |  |             if (!$response->isOk()) { | 
					
						
							|  |  |  |                 return null; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-02-07 01:52:20 +01:00
										 |  |  |         } catch (HTTP_Request2_Exception $e) { | 
					
						
							| 
									
										
										
										
											2016-01-11 02:36:59 +01:00
										 |  |  |             // Any HTTPClient error that might've been thrown
 | 
					
						
							| 
									
										
										
										
											2016-02-07 01:52:20 +01:00
										 |  |  |             common_log(LOG_ERR, __METHOD__ . ':'.$e->getMessage()); | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return self::hcardHints($response->getBody(), | 
					
						
							| 
									
										
										
										
											2016-01-13 14:00:05 +01:00
										 |  |  |                                 $response->getEffectiveUrl()); | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static function hcardHints($body, $url) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  |         $hcard = self::_hcard($body, $url); | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-19 15:28:06 -05:00
										 |  |  |         if (empty($hcard)) { | 
					
						
							|  |  |  |             return array(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |         $hints = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  |         // XXX: don't copy stuff into an array and then copy it again
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         if (array_key_exists('nickname', $hcard) && !empty($hcard['nickname'][0])) { | 
					
						
							|  |  |  |             $hints['nickname'] = $hcard['nickname'][0]; | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         if (array_key_exists('name', $hcard) && !empty($hcard['name'][0])) { | 
					
						
							|  |  |  |             $hints['fullname'] = $hcard['name'][0]; | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-21 10:18:13 -07:00
										 |  |  |         if (array_key_exists('photo', $hcard) && count($hcard['photo'])) { | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  |             $hints['avatar'] = $hcard['photo'][0]; | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         if (array_key_exists('note', $hcard) && !empty($hcard['note'][0])) { | 
					
						
							|  |  |  |             $hints['bio'] = $hcard['note'][0]; | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         if (array_key_exists('adr', $hcard) && !empty($hcard['adr'][0])) { | 
					
						
							|  |  |  |             $hints['location'] = $hcard['adr'][0]['value']; | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         if (array_key_exists('url', $hcard) && !empty($hcard['url'][0])) { | 
					
						
							|  |  |  |             $hints['homepage'] = $hcard['url'][0]; | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $hints; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  |     static function _hcard($body, $url) | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         $mf2 = new Mf2\Parser($body, $url); | 
					
						
							|  |  |  |         $mf2 = $mf2->parse(); | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         if (empty($mf2['items'])) { | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  |             return null; | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         $hcards = array(); | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         foreach ($mf2['items'] as $item) { | 
					
						
							|  |  |  |             if (!in_array('h-card', $item['type'])) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |             // We found a match, return it immediately
 | 
					
						
							|  |  |  |             if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) { | 
					
						
							|  |  |  |                 return $item['properties']; | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |             // Let's keep all the hcards for later, to return one of them at least
 | 
					
						
							|  |  |  |             $hcards[] = $item['properties']; | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         // No match immediately for the url we expected, but there were h-cards found
 | 
					
						
							|  |  |  |         if (count($hcards) > 0) { | 
					
						
							|  |  |  |             return $hcards[0]; | 
					
						
							| 
									
										
										
										
											2010-03-18 20:52:00 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 21:41:06 +02:00
										 |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2010-03-16 11:25:18 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } |