]> git.mxchange.org Git - friendica.git/blob - src/Model/Subscription.php
4e176c8fa30a3a3b890f65fba1a0735b4693f9fb
[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  /**
23   * @see https://github.com/web-push-libs/web-push-php
24   * Possibly we should simply use this.
25   */
26
27 namespace Friendica\Model;
28
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Util\Crypto;
32
33 class Subscription
34 {
35         /**
36          * Select a subscription record exists
37          *
38          * @param int   $applicationid
39          * @param int   $uid
40          * @param array $fields
41          *
42          * @return bool Does it exist?
43          */
44         public static function select(int $applicationid, int $uid, array $fields = [])
45         {
46                 return DBA::selectFirst('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
47         }
48
49         /**
50          * Check if a subscription record exists
51          *
52          * @param int   $applicationid
53          * @param int   $uid
54          *
55          * @return bool Does it exist?
56          */
57         public static function exists(int $applicationid, int $uid)
58         {
59                 return DBA::exists('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
60         }
61
62         /**
63          * Update a subscription record
64          *
65          * @param int   $applicationid
66          * @param int   $uid
67          * @param array $fields subscription fields
68          *
69          * @return bool result of update
70          */
71         public static function update(int $applicationid, int $uid, array $fields)
72         {
73                 return DBA::update('subscription', $fields, ['application-id' => $applicationid, 'uid' => $uid]);
74         }
75
76         /**
77          * Insert or replace a subscription record
78          *
79          * @param array $fields subscription fields
80          *
81          * @return bool result of replace
82          */
83         public static function replace(array $fields)
84         {
85                 return DBA::replace('subscription', $fields);
86         }
87
88         /**
89          * Delete a subscription record
90          *
91          * @param int $applicationid
92          * @param int $uid
93          * @return bool
94          */
95         public static function delete(int $applicationid, int $uid)
96         {
97                 return DBA::delete('subscription', ['application-id' => $applicationid, 'uid' => $uid]);
98         }
99
100         /**
101          * Fetch a VAPID key
102          *
103          * @return string
104          */
105         public static function getVapidKey(): string
106         {
107                 $keypair = DI::config()->get('system', 'ec_keypair');
108                 if (empty($keypair)) {
109                         $keypair = Crypto::newECKeypair();
110                         DI::config()->set('system', 'ec_keypair', $keypair);
111                 }
112                 return $keypair['vapid-public'];
113         }
114 }