2016-05-19 15:01:28 +01:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
2017-06-22 14:30:10 +01:00
|
|
|
|
use Cache;
|
|
|
|
|
use Twitter;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
use Normalizer;
|
2017-06-22 14:30:10 +01:00
|
|
|
|
use GuzzleHttp\Client;
|
2016-11-25 19:51:42 +00:00
|
|
|
|
use Laravel\Scout\Searchable;
|
2017-07-10 14:02:13 +01:00
|
|
|
|
use League\CommonMark\Converter;
|
|
|
|
|
use League\CommonMark\DocParser;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
use Jonnybarnes\IndieWeb\Numbers;
|
2017-07-10 14:02:13 +01:00
|
|
|
|
use League\CommonMark\Environment;
|
|
|
|
|
use League\CommonMark\HtmlRenderer;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-02-03 16:42:39 +00:00
|
|
|
|
use Jonnybarnes\EmojiA11y\EmojiModifier;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2017-07-10 14:02:13 +01:00
|
|
|
|
use Jonnybarnes\CommonmarkLinkify\LinkifyExtension;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
|
|
2017-03-04 14:04:21 +00:00
|
|
|
|
class Note extends Model
|
2016-05-19 15:01:28 +01:00
|
|
|
|
{
|
2016-11-25 19:51:42 +00:00
|
|
|
|
use Searchable;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
use SoftDeletes;
|
|
|
|
|
|
2017-08-20 21:36:06 +01:00
|
|
|
|
/**
|
|
|
|
|
* The reges for matching lone usernames.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private const USERNAMES_REGEX = '/\[.*?\](*SKIP)(*F)|@(\w+)/';
|
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
/**
|
|
|
|
|
* The database table used by the model.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $table = 'notes';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Define the relationship with tags.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public function tags()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany('App\Tag');
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 14:30:10 +01:00
|
|
|
|
/**
|
|
|
|
|
* Define the relationship with clients.
|
|
|
|
|
*
|
|
|
|
|
* @var array?
|
|
|
|
|
*/
|
2017-06-22 15:44:41 +00:00
|
|
|
|
public function client()
|
|
|
|
|
{
|
2017-06-22 14:30:10 +01:00
|
|
|
|
return $this->belongsTo('App\MicropubClient', 'client_id', 'client_url');
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
/**
|
|
|
|
|
* Define the relationship with webmentions.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public function webmentions()
|
|
|
|
|
{
|
|
|
|
|
return $this->morphMany('App\WebMention', 'commentable');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Definte the relationship with places.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public function place()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo('App\Place');
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 22:00:29 +00:00
|
|
|
|
/**
|
|
|
|
|
* Define the relationship with media.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function media()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany('App\Media');
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
/**
|
|
|
|
|
* We shall set a blacklist of non-modifiable model attributes.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $guarded = ['id'];
|
|
|
|
|
|
2016-12-06 20:38:52 +00:00
|
|
|
|
/**
|
|
|
|
|
* Hide the column used with Laravel Scout.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $hidden = ['searchable'];
|
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be mutated to dates.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
|
|
2016-12-06 20:38:52 +00:00
|
|
|
|
/**
|
|
|
|
|
* Set the attributes to be indexed for searching with Scout.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function toSearchableArray()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'note' => $this->note,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
/**
|
|
|
|
|
* Normalize the note to Unicode FORM C.
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function setNoteAttribute($value)
|
|
|
|
|
{
|
|
|
|
|
$this->attributes['note'] = normalizer_normalize($value, Normalizer::FORM_C);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pre-process notes for web-view.
|
|
|
|
|
*
|
|
|
|
|
* @param string
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getNoteAttribute($value)
|
|
|
|
|
{
|
2017-02-03 16:42:39 +00:00
|
|
|
|
$emoji = new EmojiModifier();
|
2017-07-10 14:02:13 +01:00
|
|
|
|
|
2017-08-20 21:36:06 +01:00
|
|
|
|
$html = $this->convertMarkdown($value);
|
2016-05-19 15:01:28 +01:00
|
|
|
|
$hcards = $this->makeHCards($html);
|
|
|
|
|
$hashtags = $this->autoLinkHashtag($hcards);
|
2017-02-03 16:42:39 +00:00
|
|
|
|
$modified = $emoji->makeEmojiAccessible($hashtags);
|
2016-05-19 15:01:28 +01:00
|
|
|
|
|
2017-02-03 16:42:39 +00:00
|
|
|
|
return $modified;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate the NewBase60 ID from primary ID.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getNb60idAttribute()
|
|
|
|
|
{
|
|
|
|
|
$numbers = new Numbers();
|
|
|
|
|
|
|
|
|
|
return $numbers->numto60($this->id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The Long URL for a note.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getLongurlAttribute()
|
|
|
|
|
{
|
|
|
|
|
return config('app.url') . '/notes/' . $this->nb60id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The Short URL for a note.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getShorturlAttribute()
|
|
|
|
|
{
|
|
|
|
|
return config('app.shorturl') . '/notes/' . $this->nb60id;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 14:30:10 +01:00
|
|
|
|
/**
|
|
|
|
|
* Get the ISO8601 value for mf2.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getIso8601Attribute()
|
|
|
|
|
{
|
|
|
|
|
return $this->updated_at->toISO8601String();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the ISO8601 value for mf2.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getHumandiffAttribute()
|
|
|
|
|
{
|
|
|
|
|
return $this->updated_at->diffForHumans();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-04 19:06:22 +01:00
|
|
|
|
/**
|
|
|
|
|
* Get the pubdate value for RSS feeds.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getPubdateAttribute()
|
|
|
|
|
{
|
|
|
|
|
return $this->updated_at->toRSSString();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
/**
|
2017-06-22 14:30:10 +01:00
|
|
|
|
* Get the latitude value.
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getLatitudeAttribute()
|
|
|
|
|
{
|
|
|
|
|
if ($this->place !== null) {
|
|
|
|
|
$lnglat = explode(' ', $this->place->location);
|
|
|
|
|
|
|
|
|
|
return $lnglat[1];
|
|
|
|
|
}
|
|
|
|
|
if ($this->location !== null) {
|
|
|
|
|
$pieces = explode(':', $this->location);
|
|
|
|
|
$latlng = explode(',', $pieces[0]);
|
|
|
|
|
|
|
|
|
|
return trim($latlng[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the longitude value.
|
2016-05-19 15:01:28 +01:00
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
2017-06-22 14:30:10 +01:00
|
|
|
|
public function getLongitudeAttribute()
|
|
|
|
|
{
|
|
|
|
|
if ($this->place !== null) {
|
|
|
|
|
$lnglat = explode(' ', $this->place->location);
|
|
|
|
|
|
|
|
|
|
return $lnglat[1];
|
|
|
|
|
}
|
|
|
|
|
if ($this->location !== null) {
|
|
|
|
|
$pieces = explode(':', $this->location);
|
|
|
|
|
$latlng = explode(',', $pieces[0]);
|
|
|
|
|
|
|
|
|
|
return trim($latlng[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the address for a note. This is either a reverse geo-code from the
|
|
|
|
|
* location, or is derived from the associated place.
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getAddressAttribute()
|
|
|
|
|
{
|
|
|
|
|
if ($this->place !== null) {
|
|
|
|
|
return $this->place->name;
|
|
|
|
|
}
|
|
|
|
|
if ($this->location !== null) {
|
|
|
|
|
return $this->reverseGeoCode((float) $this->latitude, (float) $this->longitude);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTwitterAttribute()
|
|
|
|
|
{
|
|
|
|
|
if ($this->in_reply_to == null || mb_substr($this->in_reply_to, 0, 20, 'UTF-8') !== 'https://twitter.com/') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-12 18:56:36 +01:00
|
|
|
|
$tweetId = basename($this->in_reply_to);
|
2017-06-22 14:30:10 +01:00
|
|
|
|
if (Cache::has($tweetId)) {
|
|
|
|
|
return Cache::get($tweetId);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
$oEmbed = Twitter::getOembed([
|
|
|
|
|
'id' => $tweetId,
|
|
|
|
|
'align' => 'center',
|
|
|
|
|
'omit_script' => true,
|
|
|
|
|
'maxwidth' => 550,
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Cache::put($tweetId, $oEmbed, ($oEmbed->cache_age / 60));
|
|
|
|
|
|
|
|
|
|
return $oEmbed;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-20 21:36:06 +01:00
|
|
|
|
/**
|
|
|
|
|
* Show a specific form of the note for twitter.
|
|
|
|
|
*/
|
|
|
|
|
public function getTwitterContentAttribute()
|
|
|
|
|
{
|
|
|
|
|
// find @mentions
|
|
|
|
|
preg_match_all(self::USERNAMES_REGEX, $this->getOriginal('note'), $matches);
|
|
|
|
|
if (count($matches[1]) === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if any @mentions have a contact associated with them
|
|
|
|
|
$count = 0;
|
|
|
|
|
foreach ($matches[1] as $match) {
|
|
|
|
|
$contact = Contact::where('nick', '=', mb_strtolower($match))->first();
|
|
|
|
|
if ($contact) {
|
|
|
|
|
$count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($count === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// swap in twitter usernames
|
|
|
|
|
$swapped = preg_replace_callback(
|
|
|
|
|
self::USERNAMES_REGEX,
|
|
|
|
|
function ($matches) {
|
|
|
|
|
try {
|
|
|
|
|
$contact = Contact::where('nick', '=', mb_strtolower($matches[1]))->firstOrFail();
|
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
|
//assume its an actual twitter handle
|
|
|
|
|
return $matches[0];
|
|
|
|
|
}
|
|
|
|
|
if ($contact->twitter) {
|
|
|
|
|
return '@' . $contact->twitter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $contact->name;
|
|
|
|
|
},
|
|
|
|
|
$this->getOriginal('note')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $this->convertMarkdown($swapped);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFacebookContentAttribute()
|
|
|
|
|
{
|
|
|
|
|
// find @mentions
|
|
|
|
|
preg_match_all(self::USERNAMES_REGEX, $this->getOriginal('note'), $matches);
|
|
|
|
|
if (count($matches[1]) === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if any @mentions have a contact associated with them
|
|
|
|
|
$count = 0;
|
|
|
|
|
foreach ($matches[1] as $match) {
|
|
|
|
|
$contact = Contact::where('nick', '=', mb_strtolower($match))->first();
|
|
|
|
|
if ($contact) {
|
|
|
|
|
$count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($count === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// swap in facebook usernames
|
|
|
|
|
$swapped = preg_replace_callback(
|
|
|
|
|
self::USERNAMES_REGEX,
|
|
|
|
|
function ($matches) {
|
|
|
|
|
try {
|
|
|
|
|
$contact = Contact::where('nick', '=', mb_strtolower($matches[1]))->firstOrFail();
|
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
|
//assume its an actual twitter handle
|
|
|
|
|
return $matches[0];
|
|
|
|
|
}
|
|
|
|
|
if ($contact->facebook) {
|
|
|
|
|
return '<a class="u-category h-card" href="https://facebook.com/' . $contact->facebook . '">' . $contact->name . '</a>';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $contact->name;
|
|
|
|
|
},
|
|
|
|
|
$this->getOriginal('note')
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $this->convertMarkdown($swapped);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-18 15:15:53 +01:00
|
|
|
|
/**
|
|
|
|
|
* Scope a query to select a note via a NewBase60 id.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
|
* @param string $nb60id
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
|
*/
|
|
|
|
|
public function scopeNb60($query, $nb60id)
|
|
|
|
|
{
|
|
|
|
|
$numbers = new Numbers();
|
|
|
|
|
|
|
|
|
|
return $query->where('id', $numbers->b60tonum($nb60id));
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
|
/**
|
|
|
|
|
* Take note that this method does two things, given @username (NOT [@username](URL)!)
|
|
|
|
|
* we try to create a fancy hcard from our contact info. If this is not possible
|
|
|
|
|
* due to lack of contact info, we assume @username is a twitter handle and link it
|
|
|
|
|
* as such.
|
|
|
|
|
*
|
|
|
|
|
* @param string The note’s text
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function makeHCards($text)
|
|
|
|
|
{
|
|
|
|
|
$hcards = preg_replace_callback(
|
2017-08-20 21:36:06 +01:00
|
|
|
|
self::USERNAMES_REGEX,
|
2016-05-19 15:01:28 +01:00
|
|
|
|
function ($matches) {
|
|
|
|
|
try {
|
|
|
|
|
$contact = Contact::where('nick', '=', mb_strtolower($matches[1]))->firstOrFail();
|
|
|
|
|
} catch (ModelNotFoundException $e) {
|
2016-11-25 15:53:14 +00:00
|
|
|
|
//assume its an actual twitter handle
|
2016-05-19 15:01:28 +01:00
|
|
|
|
return '<a href="https://twitter.com/' . $matches[1] . '">' . $matches[0] . '</a>';
|
|
|
|
|
}
|
2016-11-25 15:53:14 +00:00
|
|
|
|
$host = parse_url($contact->homepage, PHP_URL_HOST);
|
|
|
|
|
$contact->photo = (file_exists(public_path() . '/assets/profile-images/' . $host . '/image')) ?
|
|
|
|
|
'/assets/profile-images/' . $host . '/image'
|
2016-05-19 15:01:28 +01:00
|
|
|
|
:
|
|
|
|
|
'/assets/profile-images/default-image';
|
|
|
|
|
|
2016-11-25 15:53:14 +00:00
|
|
|
|
return trim(view('templates.mini-hcard', ['contact' => $contact])->render());
|
2016-05-19 15:01:28 +01:00
|
|
|
|
},
|
|
|
|
|
$text
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $hcards;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given a string and section, finds all hashtags matching
|
|
|
|
|
* `#[\-_a-zA-Z0-9]+` and wraps them in an `a` element with
|
|
|
|
|
* `rel=tag` set and a `href` of 'section/tagged/' + tagname without the #.
|
|
|
|
|
*
|
|
|
|
|
* @param string The note
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function autoLinkHashtag($text)
|
|
|
|
|
{
|
|
|
|
|
// $replacements = ["#tag" => "<a rel="tag" href="/tags/tag">#tag</a>]
|
|
|
|
|
$replacements = [];
|
|
|
|
|
$matches = [];
|
|
|
|
|
|
|
|
|
|
if (preg_match_all('/(?<=^|\s)\#([a-zA-Z0-9\-\_]+)/i', $text, $matches, PREG_PATTERN_ORDER)) {
|
|
|
|
|
// Look up #tags, get Full name and URL
|
|
|
|
|
foreach ($matches[0] as $name) {
|
|
|
|
|
$name = str_replace('#', '', $name);
|
|
|
|
|
$replacements[$name] =
|
2016-12-12 14:03:31 +00:00
|
|
|
|
'<a rel="tag" class="p-category" href="/notes/tagged/'
|
|
|
|
|
. Tag::normalizeTag($name)
|
|
|
|
|
. '">#'
|
|
|
|
|
. $name
|
|
|
|
|
. '</a>';
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Replace #tags with valid microformat-enabled link
|
|
|
|
|
foreach ($replacements as $name => $replacement) {
|
|
|
|
|
$text = str_replace('#' . $name, $replacement, $text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
2017-06-22 14:30:10 +01:00
|
|
|
|
|
2017-08-20 21:36:06 +01:00
|
|
|
|
private function convertMarkdown($text)
|
|
|
|
|
{
|
|
|
|
|
$environment = Environment::createCommonMarkEnvironment();
|
|
|
|
|
$environment->addExtension(new LinkifyExtension());
|
|
|
|
|
$converter = new Converter(new DocParser($environment), new HtmlRenderer($environment));
|
|
|
|
|
|
|
|
|
|
return $converter->convertToHtml($text);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-22 14:30:10 +01:00
|
|
|
|
/**
|
|
|
|
|
* Do a reverse geocode lookup of a `lat,lng` value.
|
|
|
|
|
*
|
|
|
|
|
* @param float The latitude
|
|
|
|
|
* @param float The longitude
|
|
|
|
|
* @return string The location HTML
|
|
|
|
|
*/
|
|
|
|
|
public function reverseGeoCode(float $latitude, float $longitude): string
|
|
|
|
|
{
|
|
|
|
|
$latlng = $latitude . ',' . $longitude;
|
|
|
|
|
|
|
|
|
|
return Cache::get($latlng, function () use ($latlng, $latitude, $longitude) {
|
|
|
|
|
$guzzle = new Client();
|
|
|
|
|
$response = $guzzle->request('GET', 'https://nominatim.openstreetmap.org/reverse', [
|
|
|
|
|
'query' => [
|
|
|
|
|
'format' => 'json',
|
|
|
|
|
'lat' => $latitude,
|
|
|
|
|
'lon' => $longitude,
|
|
|
|
|
'zoom' => 18,
|
|
|
|
|
'addressdetails' => 1,
|
|
|
|
|
],
|
|
|
|
|
'headers' => ['User-Agent' => 'jonnybarnes.uk via Guzzle, email jonny@jonnybarnes.uk'],
|
|
|
|
|
]);
|
|
|
|
|
$json = json_decode($response->getBody());
|
|
|
|
|
if (isset($json->address->town)) {
|
|
|
|
|
$address = '<span class="p-locality">'
|
|
|
|
|
. $json->address->town
|
|
|
|
|
. '</span>, <span class="p-country-name">'
|
|
|
|
|
. $json->address->country
|
|
|
|
|
. '</span>';
|
|
|
|
|
Cache::forever($latlng, $address);
|
|
|
|
|
|
|
|
|
|
return $address;
|
|
|
|
|
}
|
|
|
|
|
if (isset($json->address->city)) {
|
|
|
|
|
$address = $json->address->city . ', ' . $json->address->country;
|
|
|
|
|
Cache::forever($latlng, $address);
|
|
|
|
|
|
|
|
|
|
return $address;
|
|
|
|
|
}
|
|
|
|
|
if (isset($json->address->county)) {
|
|
|
|
|
$address = '<span class="p-region">'
|
|
|
|
|
. $json->address->county
|
|
|
|
|
. '</span>, <span class="p-country-name">'
|
|
|
|
|
. $json->address->country
|
|
|
|
|
. '</span>';
|
|
|
|
|
Cache::forever($latlng, $address);
|
|
|
|
|
|
|
|
|
|
return $address;
|
|
|
|
|
}
|
|
|
|
|
$adress = '<span class="p-country-name">' . $json->address->country . '</span>';
|
|
|
|
|
Cache::forever($latlng, $address);
|
|
|
|
|
|
|
|
|
|
return $address;
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
|
}
|