]> git.mxchange.org Git - friendica.git/blob - src/Model/Subscription.php
Improved error check, added "put"
[friendica.git] / src / Model / Subscription.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Util\Crypto;
27
28 class Subscription
29 {
30         /**
31          * Select a subscription record exists
32          *
33          * @param int   $applicationid
34          * @param int   $uid
35          * @param array $fields
36          *
37          * @return bool Does it exist?
38          */
39         public static function select(int $applicationid, int $uid, array $fields = [])
40         {
41                 return DBA::selectFirst('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
42         }
43
44         /**
45          * Check if a subscription record exists
46          *
47          * @param int   $applicationid
48          * @param int   $uid
49          *
50          * @return bool Does it exist?
51          */
52         public static function exists(int $applicationid, int $uid)
53         {
54                 return DBA::exists('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
55         }
56
57         /**
58          * Update a subscription record
59          *
60          * @param int   $applicationid
61          * @param int   $uid
62          * @param array $fields subscription fields
63          *
64          * @return bool result of update
65          */
66         public static function update(int $applicationid, int $uid, array $fields)
67         {
68                 return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
69         }
70
71         /**
72          * Insert or replace a subscription record
73          *
74          * @param array $fields subscription fields
75          *
76          * @return bool result of replace
77          */
78         public static function replace(array $fields)
79         {
80                 return DBA::replace('subscription', $fields);
81         }
82
83         /**
84          * Delete a subscription record
85          *
86          * @param int $applicationid
87          * @param int $uid
88          * @return bool
89          */
90         public static function delete(int $applicationid, int $uid)
91         {
92                 return DBA::delete('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
93         }
94
95         /**
96          * Fetch a VAPID key
97          *
98          * @return string
99          */
100         public static function getVapidKey(): string
101         {
102                 $keypair = DI::config()->get('system', 'ec_keypair');
103                 if (empty($keypair)) {
104                         $keypair = Crypto::newECKeypair();
105                         DI::config()->set('system', 'ec_keypair', $keypair);
106                 }
107                 return $keypair['vapid'];
108         }
109 }