2015-10-28 09:11:54 +09:00
|
|
|
<?php
|
2020-06-08 18:25:01 +09:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
defined('GNUSOCIAL') || die();
|
2015-10-28 09:11:54 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Table Definition for mention_url_profile
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Mention_url_profile extends Managed_DataObject
|
|
|
|
{
|
|
|
|
public $__table = 'mention_url_profile'; // table name
|
|
|
|
public $profile_id; // int(4) not_null
|
|
|
|
public $profileurl; // varchar(191) primary_key not_null not 255 because utf8mb4 takes more space
|
|
|
|
|
|
|
|
public static function schemaDef()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'fields' => array(
|
|
|
|
'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'matches exactly one profile id'),
|
|
|
|
'profileurl' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'URL of the profile'),
|
|
|
|
),
|
|
|
|
'primary key' => array('profileurl'),
|
|
|
|
'foreign keys' => array(
|
|
|
|
'mention_url_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:25:01 +09:00
|
|
|
public static function fromUrl($url, $depth = 0)
|
|
|
|
{
|
2015-10-28 09:11:54 +09:00
|
|
|
common_debug('MentionURL: trying to find a profile for ' . $url);
|
|
|
|
|
|
|
|
$url = preg_replace('#https?://#', 'https://', $url);
|
|
|
|
try {
|
|
|
|
$profile = Profile::fromUri($url);
|
2020-06-08 18:25:01 +09:00
|
|
|
} catch (UnknownUriException $ex) {}
|
2015-10-28 09:11:54 +09:00
|
|
|
|
2020-06-08 18:25:01 +09:00
|
|
|
if (!($profile instanceof Profile)) {
|
2015-10-28 09:11:54 +09:00
|
|
|
$profile = self::findProfileByProfileURL($url);
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = str_replace('https://', 'http://', $url);
|
2020-06-08 18:25:01 +09:00
|
|
|
if (!($profile instanceof Profile)) {
|
2015-10-28 09:11:54 +09:00
|
|
|
try {
|
|
|
|
$profile = Profile::fromUri($url);
|
2020-06-08 18:25:01 +09:00
|
|
|
} catch (UnknownUriException $ex) {}
|
2015-10-28 09:11:54 +09:00
|
|
|
}
|
|
|
|
|
2020-06-08 18:25:01 +09:00
|
|
|
if (!($profile instanceof Profile)) {
|
2015-10-28 09:11:54 +09:00
|
|
|
$profile = self::findProfileByProfileURL($url);
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:25:01 +09:00
|
|
|
if (!($profile instanceof Profile)) {
|
2015-10-28 09:11:54 +09:00
|
|
|
$hcard = mention_url_representative_hcard($url);
|
2020-06-08 18:25:01 +09:00
|
|
|
if (!$hcard) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-10-28 09:11:54 +09:00
|
|
|
|
|
|
|
$mention_profile = new Mention_url_profile();
|
2020-06-08 18:25:01 +09:00
|
|
|
$mention_profile->query('START TRANSACTION');
|
2015-10-28 09:11:54 +09:00
|
|
|
|
|
|
|
$profile = new Profile();
|
|
|
|
$profile->profileurl = $hcard['url'][0];
|
|
|
|
$profile->fullname = $hcard['name'][0];
|
|
|
|
preg_match('/\/([^\/]+)\/*$/', $profile->profileurl, $matches);
|
2020-06-08 18:25:01 +09:00
|
|
|
if (!$hcard['nickname']) {
|
|
|
|
$hcard['nickname'] = [$matches[1]];
|
|
|
|
}
|
2015-10-28 09:11:54 +09:00
|
|
|
$profile->nickname = $hcard['nickname'][0];
|
|
|
|
$profile->created = common_sql_now();
|
|
|
|
|
|
|
|
$mention_profile->profile_id = $profile->insert();
|
2020-06-08 18:25:01 +09:00
|
|
|
if (!$mention_profile->profile_id) {
|
2015-10-28 09:11:54 +09:00
|
|
|
$mention_profile->query('ROLLBACK');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mention_profile->profileurl = $profile->profileurl;
|
2020-06-08 18:25:01 +09:00
|
|
|
if (!$mention_profile->insert()) {
|
2015-10-28 09:11:54 +09:00
|
|
|
$mention_profile->query('ROLLBACK');
|
2020-06-08 18:25:01 +09:00
|
|
|
if ($depth > 0) {
|
2015-10-28 09:11:54 +09:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return self::fromUrl($url, $depth+1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$mention_profile->query('COMMIT');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $profile;
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:25:01 +09:00
|
|
|
protected static function findProfileByProfileURL($url)
|
|
|
|
{
|
2015-10-28 09:11:54 +09:00
|
|
|
$profile = Profile::getKV('profileurl', $url);
|
2020-06-08 18:25:01 +09:00
|
|
|
if ($profile instanceof Profile) {
|
2015-10-28 09:11:54 +09:00
|
|
|
$mention_profile = new Mention_url_profile();
|
|
|
|
$mention_profile->profile_id = $profile->id;
|
|
|
|
$mention_profile->profileurl = $profile->profileurl;
|
|
|
|
$mention_profile->insert();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $profile;
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:25:01 +09:00
|
|
|
public function getProfile()
|
|
|
|
{
|
2015-10-28 09:11:54 +09:00
|
|
|
return Profile::getKV('id', $this->profile_id);
|
|
|
|
}
|
|
|
|
}
|