]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/MentionURL/classes/Mention_url_profile.php
Merge branch 'normalized_openid' into 'master'
[quix0rs-gnu-social.git] / plugins / MentionURL / classes / Mention_url_profile.php
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
17 if (!defined('GNUSOCIAL')) { exit(1); }
18
19 /**
20  * Table Definition for mention_url_profile
21  */
22
23 class Mention_url_profile extends Managed_DataObject
24 {
25     public $__table = 'mention_url_profile'; // table name
26     public $profile_id;                      // int(4) not_null
27     public $profileurl;                      // varchar(191) primary_key not_null not 255 because utf8mb4 takes more space
28
29     public static function schemaDef()
30     {
31         return array(
32             'fields' => array(
33                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'matches exactly one profile id'),
34                 'profileurl' => array('type' => 'varchar', 'not null' => true, 'length' => 191, 'description' => 'URL of the profile'),
35             ),
36             'primary key' => array('profileurl'),
37             'foreign keys' => array(
38                 'mention_url_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
39             ),
40         );
41     }
42
43     public static function fromUrl($url, $depth=0) {
44         common_debug('MentionURL: trying to find a profile for ' . $url);
45
46         $url = preg_replace('#https?://#', 'https://', $url);
47         try {
48             $profile = Profile::fromUri($url);
49         } catch(UnknownUriException $ex) {}
50
51         if(!($profile instanceof Profile)) {
52             $profile = self::findProfileByProfileURL($url);
53         }
54
55         $url = str_replace('https://', 'http://', $url);
56         if(!($profile instanceof Profile)) {
57             try {
58                 $profile = Profile::fromUri($url);
59             } catch(UnknownUriException $ex) {}
60         }
61
62         if(!($profile instanceof Profile)) {
63             $profile = self::findProfileByProfileURL($url);
64         }
65
66         if(!($profile instanceof Profile)) {
67             $hcard = mention_url_representative_hcard($url);
68             if(!$hcard) return null;
69
70             $mention_profile = new Mention_url_profile();
71             $mention_profile->query('BEGIN');
72
73             $profile = new Profile();
74             $profile->profileurl = $hcard['url'][0];
75             $profile->fullname = $hcard['name'][0];
76             preg_match('/\/([^\/]+)\/*$/', $profile->profileurl, $matches);
77             if(!$hcard['nickname']) $hcard['nickname'] = array($matches[1]);
78             $profile->nickname = $hcard['nickname'][0];
79             $profile->created = common_sql_now();
80
81             $mention_profile->profile_id = $profile->insert();
82             if(!$mention_profile->profile_id) {
83                 $mention_profile->query('ROLLBACK');
84                 return null;
85             }
86
87             $mention_profile->profileurl = $profile->profileurl;
88             if(!$mention_profile->insert()) {
89                 $mention_profile->query('ROLLBACK');
90                 if($depth > 0) {
91                     return null;
92                 } else {
93                     return self::fromUrl($url, $depth+1);
94                 }
95             } else {
96                 $mention_profile->query('COMMIT');
97             }
98         }
99
100         return $profile;
101     }
102
103     protected static function findProfileByProfileURL($url) {
104         $profile = Profile::getKV('profileurl', $url);
105         if($profile instanceof Profile) {
106             $mention_profile = new Mention_url_profile();
107             $mention_profile->profile_id = $profile->id;
108             $mention_profile->profileurl = $profile->profileurl;
109             $mention_profile->insert();
110         }
111
112         return $profile;
113     }
114
115     public function getProfile() {
116         return Profile::getKV('id', $this->profile_id);
117     }
118 }