Mention_url_profile.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * Table Definition for mention_url_profile
  19. */
  20. class Mention_url_profile extends Managed_DataObject
  21. {
  22. public $__table = 'mention_url_profile'; // table name
  23. public $profile_id; // int(4) not_null
  24. public $profileurl; // varchar(191) primary_key not_null not 255 because utf8mb4 takes more space
  25. public static function schemaDef()
  26. {
  27. return array(
  28. 'fields' => array(
  29. 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'matches exactly one profile id'),
  30. 'profileurl' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'URL of the profile'),
  31. ),
  32. 'primary key' => array('profileurl'),
  33. 'foreign keys' => array(
  34. 'mention_url_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
  35. ),
  36. );
  37. }
  38. public static function fromUrl($url, $depth = 0)
  39. {
  40. common_debug('MentionURL: trying to find a profile for ' . $url);
  41. $url = preg_replace('#https?://#', 'https://', $url);
  42. try {
  43. $profile = Profile::fromUri($url);
  44. } catch (UnknownUriException $ex) {}
  45. if (!($profile instanceof Profile)) {
  46. $profile = self::findProfileByProfileURL($url);
  47. }
  48. $url = str_replace('https://', 'http://', $url);
  49. if (!($profile instanceof Profile)) {
  50. try {
  51. $profile = Profile::fromUri($url);
  52. } catch (UnknownUriException $ex) {}
  53. }
  54. if (!($profile instanceof Profile)) {
  55. $profile = self::findProfileByProfileURL($url);
  56. }
  57. if (!($profile instanceof Profile)) {
  58. $hcard = mention_url_representative_hcard($url);
  59. if (!$hcard) {
  60. return null;
  61. }
  62. $mention_profile = new Mention_url_profile();
  63. $mention_profile->query('START TRANSACTION');
  64. $profile = new Profile();
  65. $profile->profileurl = $hcard['url'][0];
  66. $profile->fullname = $hcard['name'][0];
  67. preg_match('/\/([^\/]+)\/*$/', $profile->profileurl, $matches);
  68. if (!$hcard['nickname']) {
  69. $hcard['nickname'] = [$matches[1]];
  70. }
  71. $profile->nickname = $hcard['nickname'][0];
  72. $profile->created = common_sql_now();
  73. $mention_profile->profile_id = $profile->insert();
  74. if (!$mention_profile->profile_id) {
  75. $mention_profile->query('ROLLBACK');
  76. return null;
  77. }
  78. $mention_profile->profileurl = $profile->profileurl;
  79. if (!$mention_profile->insert()) {
  80. $mention_profile->query('ROLLBACK');
  81. if ($depth > 0) {
  82. return null;
  83. } else {
  84. return self::fromUrl($url, $depth+1);
  85. }
  86. } else {
  87. $mention_profile->query('COMMIT');
  88. }
  89. }
  90. return $profile;
  91. }
  92. protected static function findProfileByProfileURL($url)
  93. {
  94. $profile = Profile::getKV('profileurl', $url);
  95. if ($profile instanceof Profile) {
  96. $mention_profile = new Mention_url_profile();
  97. $mention_profile->profile_id = $profile->id;
  98. $mention_profile->profileurl = $profile->profileurl;
  99. $mention_profile->insert();
  100. }
  101. return $profile;
  102. }
  103. public function getProfile()
  104. {
  105. return Profile::getKV('id', $this->profile_id);
  106. }
  107. }