]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Foreign_link.php
Don't trust local HTML either
[quix0rs-gnu-social.git] / classes / Foreign_link.php
1 <?php
2 /**
3  * Table Definition for foreign_link
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Foreign_link extends Managed_DataObject
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'foreign_link';                    // table name
13     public $user_id;                         // int(4)  primary_key not_null
14     public $foreign_id;                      // bigint(8)  primary_key not_null unsigned
15     public $service;                         // int(4)  primary_key not_null
16     public $credentials;                     // varchar(191)   not 255 because utf8mb4 takes more space
17     public $noticesync;                      // tinyint(1)   not_null default_1
18     public $friendsync;                      // tinyint(1)   not_null default_2
19     public $profilesync;                     // tinyint(1)   not_null default_1
20     public $last_noticesync;                 // datetime()
21     public $last_friendsync;                 // datetime()
22     public $created;                         // datetime()   not_null
23     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
24
25     /* the code above is auto generated do not remove the tag below */
26     ###END_AUTOCODE
27
28     public static function schemaDef()
29     {
30         return array(
31             'fields' => array(
32                 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'link to user on this system, if exists'),
33                 'foreign_id' => array('type' => 'int', 'size' => 'big', 'unsigned' => true, 'not null' => true, 'description' => 'link to user on foreign service, if exists'),
34                 'service' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to service'),
35                 'credentials' => array('type' => 'varchar', 'length' => 191, 'description' => 'authc credentials, typically a password'),
36                 'noticesync' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 1, 'description' => 'notice synchronization, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies'),
37                 'friendsync' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 2, 'description' => 'friend synchronization, bit 1 = sync outgoing, bit 2 = sync incoming'),
38                 'profilesync' => array('type' => 'int', 'size' => 'tiny', 'not null' => true, 'default' => 1, 'description' => 'profile synchronization, bit 1 = sync outgoing, bit 2 = sync incoming'),
39                 'last_noticesync' => array('type' => 'datetime', 'description' => 'last time notices were imported'),
40                 'last_friendsync' => array('type' => 'datetime', 'description' => 'last time friends were imported'),
41                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
42                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
43             ),
44             'primary key' => array('user_id', 'foreign_id', 'service'),
45             'foreign keys' => array(
46                 'foreign_link_user_id_fkey' => array('user', array('user_id' => 'id')),
47                 'foreign_link_foreign_id_fkey' => array('foreign_user', array('foreign_id' => 'id', 'service' => 'service')),
48                 'foreign_link_service_fkey' => array('foreign_service', array('service' => 'id')),
49             ),
50             'indexes' => array(
51                 'foreign_user_user_id_idx' => array('user_id'),
52             ),
53         );
54     }
55
56     static function getByUserID($user_id, $service)
57     {
58         if (empty($user_id) || empty($service)) {
59             throw new ServerException('Empty user_id or service for Foreign_link::getByUserID');
60         }
61
62         $flink = new Foreign_link();
63         $flink->service = $service;
64         $flink->user_id = $user_id;
65         $flink->limit(1);
66
67         if (!$flink->find(true)) {
68             throw new NoResultException($flink);
69         }
70
71         return $flink;
72     }
73
74     static function getByForeignID($foreign_id, $service)
75     {
76         if (empty($foreign_id) || empty($service)) {
77             throw new ServerException('Empty foreign_id or service for Foreign_link::getByForeignID');
78         }
79
80         $flink = new Foreign_link();
81         $flink->service = $service;
82         $flink->foreign_id = $foreign_id;
83         $flink->limit(1);
84
85         if (!$flink->find(true)) {
86             throw new NoResultException($flink);
87         }
88
89         return $flink;
90     }
91
92     function set_flags($noticesend, $noticerecv, $replysync, $friendsync)
93     {
94         if ($noticesend) {
95             $this->noticesync |= FOREIGN_NOTICE_SEND;
96         } else {
97             $this->noticesync &= ~FOREIGN_NOTICE_SEND;
98         }
99
100         if ($noticerecv) {
101             $this->noticesync |= FOREIGN_NOTICE_RECV;
102         } else {
103             $this->noticesync &= ~FOREIGN_NOTICE_RECV;
104         }
105
106         if ($replysync) {
107             $this->noticesync |= FOREIGN_NOTICE_SEND_REPLY;
108         } else {
109             $this->noticesync &= ~FOREIGN_NOTICE_SEND_REPLY;
110         }
111
112         if ($friendsync) {
113             $this->friendsync |= FOREIGN_FRIEND_RECV;
114         } else {
115             $this->friendsync &= ~FOREIGN_FRIEND_RECV;
116         }
117
118         $this->profilesync = 0;
119     }
120
121     // Convenience methods
122     function getForeignUser()
123     {
124         $fuser = new Foreign_user();
125         $fuser->service = $this->service;
126         $fuser->id = $this->foreign_id;
127
128         $fuser->limit(1);
129
130         if (!$fuser->find(true)) {
131             throw new NoResultException($fuser);
132         }
133
134         return $fuser;
135     }
136
137     function getUser()
138     {
139         return Profile::getByID($this->user_id)->getUser();
140     }
141
142     function getProfile()
143     {
144         return Profile::getByID($this->user_id);
145     }
146
147     // Make sure we only ever delete one record at a time
148     function safeDelete()
149     {
150         if (!empty($this->user_id)
151             && !empty($this->foreign_id)
152             && !empty($this->service))
153         {
154             return $this->delete();
155         } else {
156             common_debug(LOG_WARNING,
157                 'Foreign_link::safeDelete() tried to delete a '
158                 . 'Foreign_link without a fully specified compound key: '
159                 . var_export($this, true));
160             return false;
161         }
162     }
163 }