Passed
Push — master ( 9be5ca...356350 )
by Morris
13:26 queued 10s
created

Helper::calcExpireDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Miguel Prokop <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Robin McCorkell <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Vincent Petry <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
31
namespace OC\Share;
32
33
class Helper extends \OC\Share\Constants {
34
35
	/**
36
	 * get default expire settings defined by the admin
37
	 * @return array contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
38
	 */
39
	public static function getDefaultExpireSetting() {
40
		$config = \OC::$server->getConfig();
41
42
		$defaultExpireSettings = ['defaultExpireDateSet' => false];
43
44
		// get default expire settings
45
		$defaultExpireDate = $config->getAppValue('core', 'shareapi_default_expire_date', 'no');
46
		if ($defaultExpireDate === 'yes') {
47
			$enforceExpireDate = $config->getAppValue('core', 'shareapi_enforce_expire_date', 'no');
48
			$defaultExpireSettings['defaultExpireDateSet'] = true;
49
			$defaultExpireSettings['expireAfterDays'] = (int)$config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
50
			$defaultExpireSettings['enforceExpireDate'] = $enforceExpireDate === 'yes';
51
		}
52
53
		return $defaultExpireSettings;
54
	}
55
56
	public static function calcExpireDate() {
57
		$expireAfter = \OC\Share\Share::getExpireInterval() * 24 * 60 * 60;
58
		$expireAt = time() + $expireAfter;
59
		$date = new \DateTime();
60
		$date->setTimestamp($expireAt);
61
		$date->setTime(0, 0, 0);
62
		//$dateString = $date->format('Y-m-d') . ' 00:00:00';
63
64
		return $date;
65
	}
66
67
	/**
68
	 * calculate expire date
69
	 * @param array $defaultExpireSettings contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
70
	 * @param int $creationTime timestamp when the share was created
71
	 * @param int $userExpireDate expire timestamp set by the user
72
	 * @return mixed integer timestamp or False
73
	 */
74
	public static function calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate = null) {
75
		$expires = false;
76
		$defaultExpires = null;
77
78
		if (!empty($defaultExpireSettings['defaultExpireDateSet'])) {
79
			$defaultExpires = $creationTime + $defaultExpireSettings['expireAfterDays'] * 86400;
80
		}
81
82
83
		if (isset($userExpireDate)) {
84
			// if the admin decided to enforce the default expire date then we only take
85
			// the user defined expire date of it is before the default expire date
86
			if ($defaultExpires && !empty($defaultExpireSettings['enforceExpireDate'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $defaultExpires of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
87
				$expires = min($userExpireDate, $defaultExpires);
88
			} else {
89
				$expires = $userExpireDate;
90
			}
91
		} elseif ($defaultExpires && !empty($defaultExpireSettings['enforceExpireDate'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $defaultExpires of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
92
			$expires = $defaultExpires;
93
		}
94
95
		return $expires;
96
	}
97
98
	/**
99
	 * Strips away a potential file names and trailing slashes:
100
	 * - http://localhost
101
	 * - http://localhost/
102
	 * - http://localhost/index.php
103
	 * - http://localhost/index.php/s/{shareToken}
104
	 *
105
	 * all return: http://localhost
106
	 *
107
	 * @param string $remote
108
	 * @return string
109
	 */
110
	protected static function fixRemoteURL($remote) {
111
		$remote = str_replace('\\', '/', $remote);
112
		if ($fileNamePosition = strpos($remote, '/index.php')) {
113
			$remote = substr($remote, 0, $fileNamePosition);
114
		}
115
		$remote = rtrim($remote, '/');
116
117
		return $remote;
118
	}
119
120
	/**
121
	 * check if two federated cloud IDs refer to the same user
122
	 *
123
	 * @param string $user1
124
	 * @param string $server1
125
	 * @param string $user2
126
	 * @param string $server2
127
	 * @return bool true if both users and servers are the same
128
	 */
129
	public static function isSameUserOnSameServer($user1, $server1, $user2, $server2) {
130
		$normalizedServer1 = strtolower(\OC\Share\Share::removeProtocolFromUrl($server1));
131
		$normalizedServer2 = strtolower(\OC\Share\Share::removeProtocolFromUrl($server2));
132
133
		if (rtrim($normalizedServer1, '/') === rtrim($normalizedServer2, '/')) {
134
			// FIXME this should be a method in the user management instead
135
			\OCP\Util::emitHook(
136
					'\OCA\Files_Sharing\API\Server2Server',
137
					'preLoginNameUsedAsUserName',
138
					['uid' => &$user1]
139
			);
140
			\OCP\Util::emitHook(
141
					'\OCA\Files_Sharing\API\Server2Server',
142
					'preLoginNameUsedAsUserName',
143
					['uid' => &$user2]
144
			);
145
146
			if ($user1 === $user2) {
147
				return true;
148
			}
149
		}
150
151
		return false;
152
	}
153
}
154