Mention_url_profile.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. if (!defined('GNUSOCIAL')) { exit(1); }
  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. common_debug('MentionURL: trying to find a profile for ' . $url);
  40. $url = preg_replace('#https?://#', 'https://', $url);
  41. try {
  42. $profile = Profile::fromUri($url);
  43. } catch(UnknownUriException $ex) {}
  44. if(!($profile instanceof Profile)) {
  45. $profile = self::findProfileByProfileURL($url);
  46. }
  47. $url = str_replace('https://', 'http://', $url);
  48. if(!($profile instanceof Profile)) {
  49. try {
  50. $profile = Profile::fromUri($url);
  51. } catch(UnknownUriException $ex) {}
  52. }
  53. if(!($profile instanceof Profile)) {
  54. $profile = self::findProfileByProfileURL($url);
  55. }
  56. if(!($profile instanceof Profile)) {
  57. $hcard = mention_url_representative_hcard($url);
  58. if(!$hcard) return null;
  59. $mention_profile = new Mention_url_profile();
  60. $mention_profile->query('BEGIN');
  61. $profile = new Profile();
  62. $profile->profileurl = $hcard['url'][0];
  63. $profile->fullname = $hcard['name'][0];
  64. preg_match('/\/([^\/]+)\/*$/', $profile->profileurl, $matches);
  65. if(!$hcard['nickname']) $hcard['nickname'] = array($matches[1]);
  66. $profile->nickname = $hcard['nickname'][0];
  67. $profile->created = common_sql_now();
  68. $mention_profile->profile_id = $profile->insert();
  69. if(!$mention_profile->profile_id) {
  70. $mention_profile->query('ROLLBACK');
  71. return null;
  72. }
  73. $mention_profile->profileurl = $profile->profileurl;
  74. if(!$mention_profile->insert()) {
  75. $mention_profile->query('ROLLBACK');
  76. if($depth > 0) {
  77. return null;
  78. } else {
  79. return self::fromUrl($url, $depth+1);
  80. }
  81. } else {
  82. $mention_profile->query('COMMIT');
  83. }
  84. }
  85. return $profile;
  86. }
  87. protected static function findProfileByProfileURL($url) {
  88. $profile = Profile::getKV('profileurl', $url);
  89. if($profile instanceof Profile) {
  90. $mention_profile = new Mention_url_profile();
  91. $mention_profile->profile_id = $profile->id;
  92. $mention_profile->profileurl = $profile->profileurl;
  93. $mention_profile->insert();
  94. }
  95. return $profile;
  96. }
  97. public function getProfile() {
  98. return Profile::getKV('id', $this->profile_id);
  99. }
  100. }