Move oc.js to a proper class

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2016-10-25 20:49:48 +02:00
parent 35c7ecef6a
commit d5589a15d5
No known key found for this signature in database
GPG Key ID: 1E152838F164D13B
4 changed files with 337 additions and 4 deletions

View File

@ -0,0 +1,90 @@
<?php
/**
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Core\Controller;
use bantu\IniGetWrapper\IniGetWrapper;
use OC\Template\JSConfigHelper;
use OCP\App\IAppManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
class OCJSController extends Controller {
/** @var JSConfigHelper */
private $helper;
/**
* OCJSController constructor.
*
* @param string $appName
* @param IRequest $request
* @param IL10N $l
* @param \OC_Defaults $defaults
* @param IAppManager $appManager
* @param IUserSession $session
* @param IConfig $config
* @param IGroupManager $groupManager
* @param IniGetWrapper $iniWrapper
* @param IURLGenerator $urlGenerator
*/
public function __construct($appName,
IRequest $request,
IL10N $l,
\OC_Defaults $defaults,
IAppManager $appManager,
IUserSession $session,
IConfig $config,
IGroupManager $groupManager,
IniGetWrapper $iniWrapper,
IURLGenerator $urlGenerator) {
parent::__construct($appName, $request);
$this->helper = new JSConfigHelper(
$l,
$defaults,
$appManager,
$session->getUser(),
$config,
$groupManager,
$iniWrapper,
$urlGenerator
);
}
/**
* @NoCSRFRequired
* @PublicPage
*/
public function getConfig() {
$data = $this->helper->getConfig();
return new DataDisplayResponse($data, Http::STATUS_OK, ['Content-type' => 'text/javascript']);
}
}

View File

@ -51,6 +51,7 @@ $application->registerRoutes($this, [
['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'],
['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'],
['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'],
['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'],
],
'ocs' => [
['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'],
@ -66,9 +67,6 @@ $application->registerRoutes($this, [
// Search
$this->create('search_ajax_search', '/core/search')
->actionInclude('core/search/ajax/search.php');
// oC JS config
$this->create('js_config', '/core/js/oc.js')
->actionInclude('core/js/config.php');
// Routing
$this->create('core_ajax_preview', '/core/preview')
->actionInclude('core/ajax/preview.php');

View File

@ -0,0 +1,245 @@
<?php
/**
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Template;
use bantu\IniGetWrapper\IniGetWrapper;
use OCP\App\IAppManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
class JSConfigHelper {
/** @var IL10N */
private $l;
/** @var \OC_Defaults */
private $defaults;
/** @var IAppManager */
private $appManager;
/** @var IUser */
private $currentUser;
/** @var IConfig */
private $config;
/** @var IGroupManager */
private $groupManager;
/** @var IniGetWrapper */
private $iniWrapper;
/** @var IURLGenerator */
private $urlGenerator;
/**
* JSConfigHelper constructor.
*
* @param IL10N $l
* @param \OC_Defaults $defaults
* @param IAppManager $appManager
* @param IUser|null $currentUser
* @param IConfig $config
* @param IGroupManager $groupManager
* @param IniGetWrapper $iniWrapper
* @param IURLGenerator $urlGenerator
*/
public function __construct(IL10N $l,
\OC_Defaults $defaults,
IAppManager $appManager,
$currentUser,
IConfig $config,
IGroupManager $groupManager,
IniGetWrapper $iniWrapper,
IURLGenerator $urlGenerator) {
$this->l = $l;
$this->defaults = $defaults;
$this->appManager = $appManager;
$this->currentUser = $currentUser;
$this->config = $config;
$this->groupManager = $groupManager;
$this->iniWrapper = $iniWrapper;
$this->urlGenerator = $urlGenerator;
}
public function getConfig() {
if ($this->currentUser !== null) {
$uid = $this->currentUser->getUID();
} else {
$uid = null;
}
// Get the config
$apps_paths = [];
if ($this->currentUser === null) {
$apps = $this->appManager->getInstalledApps();
} else {
$apps = $this->appManager->getEnabledAppsForUser($this->currentUser);
}
foreach($apps as $app) {
$apps_paths[$app] = \OC_App::getAppWebPath($app);
}
$defaultExpireDateEnabled = $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
$defaultExpireDate = $enforceDefaultExpireDate = null;
if ($defaultExpireDateEnabled) {
$defaultExpireDate = (int) $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
$enforceDefaultExpireDate = $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
}
$outgoingServer2serverShareEnabled = $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes';
$countOfDataLocation = 0;
$dataLocation = str_replace(\OC::$SERVERROOT .'/', '', $this->config->getSystemValue('datadirectory', ''), $countOfDataLocation);
if($countOfDataLocation !== 1 || !$this->groupManager->isAdmin($uid)) {
$dataLocation = false;
}
$array = [
"oc_debug" => $this->config->getSystemValue('debug', false) ? 'true' : 'false',
"oc_isadmin" => $this->groupManager->isAdmin($uid) ? 'true' : 'false',
"oc_dataURL" => is_string($dataLocation) ? "\"".$dataLocation."\"" : 'false',
"oc_webroot" => "\"".\OC::$WEBROOT."\"",
"oc_appswebroots" => str_replace('\\/', '/', json_encode($apps_paths)), // Ugly unescape slashes waiting for better solution
"datepickerFormatDate" => json_encode($this->l->l('jsdate', null)),
"dayNames" => json_encode([
(string)$this->l->t('Sunday'),
(string)$this->l->t('Monday'),
(string)$this->l->t('Tuesday'),
(string)$this->l->t('Wednesday'),
(string)$this->l->t('Thursday'),
(string)$this->l->t('Friday'),
(string)$this->l->t('Saturday')
]),
"dayNamesShort" => json_encode([
(string)$this->l->t('Sun.'),
(string)$this->l->t('Mon.'),
(string)$this->l->t('Tue.'),
(string)$this->l->t('Wed.'),
(string)$this->l->t('Thu.'),
(string)$this->l->t('Fri.'),
(string)$this->l->t('Sat.')
]),
"dayNamesMin" => json_encode([
(string)$this->l->t('Su'),
(string)$this->l->t('Mo'),
(string)$this->l->t('Tu'),
(string)$this->l->t('We'),
(string)$this->l->t('Th'),
(string)$this->l->t('Fr'),
(string)$this->l->t('Sa')
]),
"monthNames" => json_encode([
(string)$this->l->t('January'),
(string)$this->l->t('February'),
(string)$this->l->t('March'),
(string)$this->l->t('April'),
(string)$this->l->t('May'),
(string)$this->l->t('June'),
(string)$this->l->t('July'),
(string)$this->l->t('August'),
(string)$this->l->t('September'),
(string)$this->l->t('October'),
(string)$this->l->t('November'),
(string)$this->l->t('December')
]),
"monthNamesShort" => json_encode([
(string)$this->l->t('Jan.'),
(string)$this->l->t('Feb.'),
(string)$this->l->t('Mar.'),
(string)$this->l->t('Apr.'),
(string)$this->l->t('May.'),
(string)$this->l->t('Jun.'),
(string)$this->l->t('Jul.'),
(string)$this->l->t('Aug.'),
(string)$this->l->t('Sep.'),
(string)$this->l->t('Oct.'),
(string)$this->l->t('Nov.'),
(string)$this->l->t('Dec.')
]),
"firstDay" => json_encode($this->l->l('firstday', null)) ,
"oc_config" => json_encode([
'session_lifetime' => min($this->config->getSystemValue('session_lifetime', $this->iniWrapper->getNumeric('session.gc_maxlifetime')), $this->iniWrapper->getNumeric('session.gc_maxlifetime')),
'session_keepalive' => $this->config->getSystemValue('session_keepalive', true),
'version' => implode('.', \OCP\Util::getVersion()),
'versionstring' => \OC_Util::getVersionString(),
'enable_avatars' => $this->config->getSystemValue('enable_avatars', true) === true,
'lost_password_link'=> $this->config->getSystemValue('lost_password_link', null),
'modRewriteWorking' => (getenv('front_controller_active') === 'true'),
]),
"oc_appconfig" => json_encode([
'core' => [
'defaultExpireDateEnabled' => $defaultExpireDateEnabled,
'defaultExpireDate' => $defaultExpireDate,
'defaultExpireDateEnforced' => $enforceDefaultExpireDate,
'enforcePasswordForPublicLink' => \OCP\Util::isPublicLinkPasswordRequired(),
'sharingDisabledForUser' => \OCP\Util::isSharingDisabledForUser(),
'resharingAllowed' => \OCP\Share::isResharingAllowed(),
'remoteShareAllowed' => $outgoingServer2serverShareEnabled,
'federatedCloudShareDoc' => $this->urlGenerator->linkToDocs('user-sharing-federated'),
'allowGroupSharing' => \OC::$server->getShareManager()->allowGroupSharing()
]
]),
"oc_defaults" => json_encode([
'entity' => $this->defaults->getEntity(),
'name' => $this->defaults->getName(),
'title' => $this->defaults->getTitle(),
'baseUrl' => $this->defaults->getBaseUrl(),
'syncClientUrl' => $this->defaults->getSyncClientUrl(),
'docBaseUrl' => $this->defaults->getDocBaseUrl(),
'docPlaceholderUrl' => $this->defaults->buildDocLinkToKey('PLACEHOLDER'),
'slogan' => $this->defaults->getSlogan(),
'logoClaim' => $this->defaults->getLogoClaim(),
'shortFooter' => $this->defaults->getShortFooter(),
'longFooter' => $this->defaults->getLongFooter(),
'folder' => \OC_Util::getTheme(),
]),
];
if ($this->currentUser !== null) {
$array['oc_userconfig'] = json_encode([
'avatar' => [
'version' => (int)$this->config->getUserValue($uid, 'avatar', 'version', 0),
]
]);
}
// Allow hooks to modify the output values
\OC_Hook::emit('\OCP\Config', 'js', array('array' => &$array));
$result = '';
// Echo it
foreach ($array as $setting => $value) {
$result .= 'var '. $setting . '='. $value . ';' . PHP_EOL;
}
return $result;
}
}

View File

@ -142,7 +142,7 @@ class TemplateLayout extends \OC_Template {
$jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
$this->assign('jsfiles', array());
if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
$this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
$this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
}
foreach($jsFiles as $info) {
$web = $info[1];