This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
ApplicationState.php
263 lines (240 loc) · 7.48 KB
/
ApplicationState.php
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/*
* Copyright 2007-2016 Abstrium <contact (at) pydio.com>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com/>.
*/
namespace Pydio\Core\Services;
use Pydio\Core\Model\RepositoryInterface;
use Pydio\Core\Utils\Vars\PathUtils;
use Pydio\Log\Core\Logger;
defined('AJXP_EXEC') or die('Access not allowed');
/**
* Various helpers to get application state, global folders, etc.
* @package Pydio\Core\Utils
*/
class ApplicationState
{
/**
* @var null|string
*/
private static $restBase = null;
/**
* @var string
*/
private static $sapiType = "session";
/**
* @var string
*/
private static $minisiteHash = null;
/**
* @var bool
*/
public static $silenceInstantMessages = false;
/**
* @param string $restBase
*/
public static function setSapiRestBase($restBase){
self::$sapiType = "rest";
self::$restBase = $restBase;
}
public static function setSapiTypeCLI(){
self::$sapiType = "cli";
}
/**
* @return bool
*/
public static function sapiIsCli(){
return php_sapi_name() === "cli" || self::$sapiType === "cli";
}
/**
* @return null|string
*/
public static function getSapiRestBase(){
return self::$restBase;
}
/**
* @return bool
*/
public static function sapiUsesSession(){
return !self::sapiIsCli() && self::$restBase === null;
}
/**
* @param $hash
*/
public static function setStateMinisite($hash){
self::$minisiteHash = $hash;
}
/**
* @return bool
*/
public static function hasMinisiteHash(){
return self::$minisiteHash !== null;
}
/**
* @return string
*/
public static function getMinisiteHash(){
return self::$minisiteHash;
}
/**
* Check if data/cache/first_run_passed file exists or not
* @return bool
*/
public static function detectApplicationFirstRun()
{
return !file_exists(AJXP_CACHE_DIR . "/first_run_passed") && !file_exists(AJXP_DATA_PATH . "/plugins/boot.conf/first_run_passed");
}
/**
* Touch data/cache/first_run_passed file
*/
public static function setApplicationFirstRunPassed()
{
if(!is_file(AJXP_DATA_PATH . "/plugins/boot.conf")){
@mkdir(AJXP_DATA_PATH . "/plugins/boot.conf", 0666, true);
}
@file_put_contents(AJXP_DATA_PATH . "/plugins/boot.conf/first_run_passed", "true");
}
/**
* Search include path for a given file
* @static
* @param string $file
* @return bool
*/
public static function searchIncludePath($file)
{
$ps = explode(PATH_SEPARATOR, ini_get('include_path'));
foreach ($ps as $path) {
if (@file_exists($path . DIRECTORY_SEPARATOR . $file)) return true;
}
if (@file_exists($file)) return true;
return false;
}
/**
* @static
* @param $from
* @param $to
* @return string
*/
public static function getTravelPath($from, $to)
{
$from = explode('/', $from);
$to = explode('/', $to);
$relPath = $to;
foreach ($from as $depth => $dir) {
// find first non-matching dir
if ($dir === $to[$depth]) {
// ignore this directory
array_shift($relPath);
} else {
// get number of remaining dirs to $from
$remaining = count($from) - $depth;
if ($remaining > 1) {
// add traversals up to first matching dir
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
} else {
$relPath[0] = './' . $relPath[0];
}
}
}
return implode('/', $relPath);
}
/**
* Build the current server URL
* @param bool $withURI Wether to return the "path" part in scheme://host[/path]
* @param bool $forceInternal Wether to force server local IP detection, or to use external URL if it is set in the configuration
* @static
* @return string
*/
public static function detectServerURL($withURI = false, $forceInternal = false)
{
$setUrl = ConfService::getGlobalConf("SERVER_URL");
if (!empty($setUrl) && !$forceInternal) {
return (string)$setUrl;
}
if (self::sapiIsCli()) {
Logger::debug("WARNING, THE SERVER_URL IS NOT SET, WE CANNOT BUILD IT WHEN WORKING IN CLI");
return "";
}
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http');
$port = (($protocol === 'http' && $_SERVER['SERVER_PORT'] == 80 || $protocol === 'https' && $_SERVER['SERVER_PORT'] == 443)
? "" : ":" . $_SERVER['SERVER_PORT']);
$name = $_SERVER["SERVER_NAME"];
if (!$withURI) {
return "$protocol://$name$port";
} else {
$uri = dirname($_SERVER["REQUEST_URI"]);
$apiBase = self::getSapiRestBase();
if (!empty($apiBase)) {
// apiBase must be /path/to/pydio/endpoint => remove endpoint
$uri = PathUtils::forwardSlashDirname(rtrim($apiBase));
if(empty($uri)) $uri = "/";
}
return "$protocol://$name$port" . $uri;
}
}
/**
* @param RepositoryInterface $repository
* @return string
*/
public static function getWorkspaceShortcutURL($repository)
{
if (empty($repository)) {
return "";
}
$repoSlug = $repository->getSlug();
$skipHistory = ConfService::getGlobalConf("SKIP_USER_HISTORY", "conf");
if ($skipHistory) {
$prefix = "/ws-";
} else {
$prefix = "?goto=";
}
return trim(self::detectServerURL(true), "/") . $prefix . $repoSlug;
}
/**
* Try to load the tmp dir from the CoreConf AJXP_TMP_DIR, or the constant AJXP_TMP_DIR,
* or the sys_get_temp_dir
* @static
* @return mixed|null|string
*/
public static function getTemporaryFolder()
{
$conf = ConfService::getGlobalConf("AJXP_TMP_DIR");
if (!empty($conf)) {
return $conf;
}
if (defined("AJXP_TMP_DIR") && AJXP_TMP_DIR != "") {
return AJXP_TMP_DIR;
}
return realpath(sys_get_temp_dir());
}
/**
* Try to set an ini config, without errors
* @static
* @param string $paramName
* @param string $paramValue
* @return void
*/
public static function safeIniSet($paramName, $paramValue)
{
$current = ini_get($paramName);
if ($current == $paramValue) return;
@ini_set($paramName, $paramValue);
}
}