1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Nextcloud - namespace OCA\Nextnote |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016, Sander Brand ([email protected]) |
6
|
|
|
* @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected]) |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\OwnNote\Utility; |
25
|
|
|
|
26
|
|
|
use OCP\IL10N; |
27
|
|
|
|
28
|
|
|
class Utils { |
29
|
|
|
/** |
30
|
|
|
* Gets the unix epoch UTC timestamp |
31
|
|
|
* @return int |
32
|
|
|
*/ |
33
|
|
|
public static function getTime() { |
34
|
|
|
return (new \DateTime())->getTimestamp(); |
35
|
|
|
} |
36
|
|
|
/** |
37
|
|
|
* @return int the current unix time in milliseconds |
38
|
|
|
*/ |
39
|
|
|
public static function getMicroTime() { |
40
|
|
|
return microtime(true); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $uid |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public static function getNameByUid($uid){ |
48
|
|
|
$um = \OC::$server->getUserManager(); |
49
|
|
|
$u = $um->get($uid); |
50
|
|
|
return $u->getDisplayName(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Splits a string in parts of 5Mb |
55
|
|
|
* @param $str |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function splitContent($str) { |
59
|
|
|
$maxlength = 2621440; // 5 Megs (2 bytes per character) |
60
|
|
|
$count = 0; |
61
|
|
|
$strarray = array(); |
62
|
|
|
while (true) { |
63
|
|
|
if (strlen($str) <= $maxlength) { |
64
|
|
|
$strarray[$count++] = $str; |
65
|
|
|
return $strarray; |
66
|
|
|
} else { |
67
|
|
|
$strarray[$count++] = substr($str, 0, $maxlength); |
68
|
|
|
$str = substr($str, $maxlength); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return $strarray; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $filetime \DateTime |
76
|
|
|
* @param $now \DateTime |
77
|
|
|
* @return mixed|string |
78
|
|
|
*/ |
79
|
|
|
public function getTimeString($filetime, $now) { |
80
|
|
|
$l = \OCP\Util::getL10N('ownnote'); |
81
|
|
|
$difftime = $filetime->diff($now); |
82
|
|
|
$years = $difftime->y; |
83
|
|
|
$months = $difftime->m; |
84
|
|
|
$days = $difftime->d; |
85
|
|
|
$hours = $difftime->h; |
86
|
|
|
$minutes = $difftime->i; |
87
|
|
|
$seconds = $difftime->s; |
88
|
|
|
$timestring = ""; |
89
|
|
View Code Duplication |
if ($timestring == "" && $years == 1) $timestring = str_replace('#', $years, $l->t("# year ago")); |
|
|
|
|
90
|
|
View Code Duplication |
if ($timestring == "" && $years > 0) $timestring = str_replace('#', $years, $l->t("# years ago")); |
|
|
|
|
91
|
|
View Code Duplication |
if ($timestring == "" && $months == 1) $timestring = str_replace('#', $months, $l->t("# month ago")); |
|
|
|
|
92
|
|
View Code Duplication |
if ($timestring == "" && $months > 0) $timestring = str_replace('#', $months, $l->t("# months ago")); |
|
|
|
|
93
|
|
View Code Duplication |
if ($timestring == "" && $days == 1) $timestring = str_replace('#', $days, $l->t("# day ago")); |
|
|
|
|
94
|
|
View Code Duplication |
if ($timestring == "" && $days > 0) $timestring = str_replace('#', $days, $l->t("# days ago")); |
|
|
|
|
95
|
|
View Code Duplication |
if ($timestring == "" && $hours == 1) $timestring = str_replace('#', $hours, $l->t("# hour ago")); |
|
|
|
|
96
|
|
View Code Duplication |
if ($timestring == "" && $hours > 0) $timestring = str_replace('#', $hours, $l->t("# hours ago")); |
|
|
|
|
97
|
|
View Code Duplication |
if ($timestring == "" && $minutes == 1) $timestring = str_replace('#', $minutes, $l->t("# minute ago")); |
|
|
|
|
98
|
|
View Code Duplication |
if ($timestring == "" && $minutes > 0) $timestring = str_replace('#', $minutes, $l->t("# minutes ago")); |
|
|
|
|
99
|
|
View Code Duplication |
if ($timestring == "" && $seconds == 1) $timestring = str_replace('#', $seconds, $l->t("# second ago")); |
|
|
|
|
100
|
|
View Code Duplication |
if ($timestring == "" && $seconds > 0) $timestring = str_replace('#', $seconds, $l->t("# seconds ago")); |
|
|
|
|
101
|
|
|
return $timestring; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $haystack |
107
|
|
|
* @param $needle |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function startsWith($haystack, $needle) { |
111
|
|
|
return $needle === "" || strripos($haystack, $needle, -strlen($haystack)) !== false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $string |
116
|
|
|
* @param $test |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
|
|
public function endsWith($string, $test) { |
120
|
|
|
$strlen = strlen($string); |
121
|
|
|
$testlen = strlen($test); |
122
|
|
|
if ($testlen > $strlen) return false; |
123
|
|
|
return substr_compare($string, $test, $strlen - $testlen, $testlen, true) === 0; |
124
|
|
|
} |
125
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.