aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Update/_1260.php
blob: ca30886c0a10a867c77c7819a29da0f54fd47798 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Zotlabs\Update;

class _1260 {
	public function run() {

		$has_sodium = function_exists('sodium_crypto_sign_keypair');
		$has_bcmath = function_exists('bcadd');
		$has_gmp = function_exists('gmp_add');

		if (!$has_sodium) {
			return UPDATE_FAILED;
		}

		if (!($has_gmp || $has_bcmath)) {
			hz_syslog('gothere');
			return UPDATE_FAILED;
		}

		dbq("START TRANSACTION");

		$r1 = dbq("ALTER TABLE channel ADD channel_epubkey text NOT NULL");
		$r2 = dbq("ALTER TABLE channel ADD channel_eprvkey text NOT NULL");

		$channels = dbq("select channel_id from channel where true");
		if ($channels) {
			foreach ($channels as $channel) {
				$keys = sodium_crypto_sign_keypair();
				$pubkey = sodium_bin2base64(sodium_crypto_sign_publickey($keys), SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
				$prvkey = sodium_bin2base64(sodium_crypto_sign_secretkey($keys), SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING);
				q("update channel set channel_epubkey = '%s', channel_eprvkey = '%s' where channel_id = %d",
					dbesc($pubkey),
					dbesc($prvkey),
					intval($channel['channel_id'])
				);
			}
		}

		if ($r1 && $r2) {
			dbq("COMMIT");
			return UPDATE_SUCCESS;
		}

		dbq("ROLLBACK");
		return UPDATE_FAILED;
	}

	public function verify() {
		$columns = db_columns('channel');
		return in_array('channel_epubkey', $columns) && in_array('channel_eprvkey', $columns);
	}
}