Mention_url_profile.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. 'indexes' => array(
  37. 'mention_url_profile_profile_id_idx' => array('profile_id'),
  38. ),
  39. );
  40. }
  41. public static function fromUrl($url, $depth = 0)
  42. {
  43. common_debug('MentionURL: trying to find a profile for ' . $url);
  44. $url = preg_replace('#https?://#', 'https://', $url);
  45. try {
  46. $profile = Profile::fromUri($url);
  47. } catch (UnknownUriException $ex) {}
  48. if (!($profile instanceof Profile)) {
  49. $profile = self::findProfileByProfileURL($url);
  50. }
  51. $url = str_replace('https://', 'http://', $url);
  52. if (!($profile instanceof Profile)) {
  53. try {
  54. $profile = Profile::fromUri($url);
  55. } catch (UnknownUriException $ex) {}
  56. }
  57. if (!($profile instanceof Profile)) {
  58. $profile = self::findProfileByProfileURL($url);
  59. }
  60. if (!($profile instanceof Profile)) {
  61. $hcard = mention_url_representative_hcard($url);
  62. if (!$hcard) {
  63. return null;
  64. }
  65. $mention_profile = new Mention_url_profile();
  66. $mention_profile->query('START TRANSACTION');
  67. $profile = new Profile();
  68. $profile->profileurl = $hcard['url'][0];
  69. $profile->fullname = $hcard['name'][0];
  70. preg_match('/\/([^\/]+)\/*$/', $profile->profileurl, $matches);
  71. if (!$hcard['nickname']) {
  72. $hcard['nickname'] = [$matches[1]];
  73. }
  74. $profile->nickname = $hcard['nickname'][0];
  75. $profile->created = common_sql_now();
  76. $mention_profile->profile_id = $profile->insert();
  77. if (!$mention_profile->profile_id) {
  78. $mention_profile->query('ROLLBACK');
  79. return null;
  80. }
  81. $mention_profile->profileurl = $profile->profileurl;
  82. if (!$mention_profile->insert()) {
  83. $mention_profile->query('ROLLBACK');
  84. if ($depth > 0) {
  85. return null;
  86. } else {
  87. return self::fromUrl($url, $depth+1);
  88. }
  89. } else {
  90. $mention_profile->query('COMMIT');
  91. }
  92. }
  93. return $profile;
  94. }
  95. protected static function findProfileByProfileURL($url)
  96. {
  97. $profile = Profile::getKV('profileurl', $url);
  98. if ($profile instanceof Profile) {
  99. $mention_profile = new Mention_url_profile();
  100. $mention_profile->profile_id = $profile->id;
  101. $mention_profile->profileurl = $profile->profileurl;
  102. $mention_profile->insert();
  103. }
  104. return $profile;
  105. }
  106. public function getProfile()
  107. {
  108. return Profile::getKV('id', $this->profile_id);
  109. }
  110. }