You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
10 KiB

11 years ago
11 years ago
Add code integrity check This PR implements the base foundation of the code signing and integrity check. In this PR implemented is the signing and verification logic, as well as commands to sign single apps or the core repository. Furthermore, there is a basic implementation to display problems with the code integrity on the update screen. Code signing basically happens the following way: - There is a ownCloud Root Certificate authority stored `resources/codesigning/root.crt` (in this PR I also ship the private key which we obviously need to change before a release :wink:). This certificate is not intended to be used for signing directly and only is used to sign new certificates. - Using the `integrity:sign-core` and `integrity:sign-app` commands developers can sign either the core release or a single app. The core release needs to be signed with a certificate that has a CN of `core`, apps need to be signed with a certificate that either has a CN of `core` (shipped apps!) or the AppID. - The command generates a signature.json file of the following format: ```json { "hashes": { "/filename.php": "2401fed2eea6f2c1027c482a633e8e25cd46701f811e2d2c10dc213fd95fa60e350bccbbebdccc73a042b1a2799f673fbabadc783284cc288e4f1a1eacb74e3d", "/lib/base.php": "55548cc16b457cd74241990cc9d3b72b6335f2e5f45eee95171da024087d114fcbc2effc3d5818a6d5d55f2ae960ab39fd0414d0c542b72a3b9e08eb21206dd9" }, "certificate": "-----BEGIN CERTIFICATE-----MIIBvTCCASagAwIBAgIUPvawyqJwCwYazcv7iz16TWxfeUMwDQYJKoZIhvcNAQEF\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTAx\nNDEzMTcxMFoXDTE2MTAxNDEzMTcxMFowEzERMA8GA1UEAwwIY29udGFjdHMwgZ8w\nDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANoQesGdCW0L2L+a2xITYipixkScrIpB\nkX5Snu3fs45MscDb61xByjBSlFgR4QI6McoCipPw4SUr28EaExVvgPSvqUjYLGps\nfiv0Cvgquzbx/X3mUcdk9LcFo1uWGtrTfkuXSKX41PnJGTr6RQWGIBd1V52q1qbC\nJKkfzyeMeuQfAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAvF/KIhRMQ3tYTmgHWsiM\nwDMgIDb7iaHF0fS+/Nvo4PzoTO/trev6tMyjLbJ7hgdCpz/1sNzE11Cibf6V6dsz\njCE9invP368Xv0bTRObRqeSNsGogGl5ceAvR0c9BG+NRIKHcly3At3gLkS2791bC\niG+UxI/MNcWV0uJg9S63LF8=\n-----END CERTIFICATE-----", "signature": "U29tZVNpZ25lZERhdGFFeGFtcGxl" } ``` `hashes` is an array of all files in the folder with their corresponding SHA512 hashes (this is actually quite cheap to calculate), the `certificate` is the certificate used for signing. It has to be issued by the ownCloud Root Authority and it's CN needs to be permitted to perform the required action. The `signature` is then a signature of the `hashes` which can be verified using the `certificate`. Steps to do in other PRs, this is already a quite huge one: - Add nag screen in case the code check fails to ensure that administrators are aware of this. - Add code verification also to OCC upgrade and unify display code more. - Add enforced code verification to apps shipped from the appstore with a level of "official" - Add enfocrced code verification to apps shipped from the appstore that were already signed in a previous release - Add some developer documentation on how devs can request their own certificate - Check when installing ownCloud - Add support for CRLs to allow revoking certificates **Note:** The upgrade checks are only run when the instance has a defined release channel of `stable` (defined in `version.php`). If you want to test this, you need to change the channel thus and then generate the core signature: ``` ➜ master git:(add-integrity-checker) ✗ ./occ integrity:sign-core --privateKey=resources/codesigning/core.key --certificate=resources/codesigning/core.crt Successfully signed "core" ``` Then increase the version and you should see something like the following: ![2015-11-04_12-02-57](https://cloud.githubusercontent.com/assets/878997/10936336/6adb1d14-82ec-11e5-8f06-9a74801c9abf.png) As you can see a failed code check will not prevent the further update. It will instead just be a notice to the admin. In a next step we will add some nag screen. For packaging stable releases this requires the following additional steps as a last action before zipping: 1. Run `./occ integrity:sign-core` once 2. Run `./occ integrity:sign-app` _for each_ app. However, this can be simply automated using a simple foreach on the apps folder.
11 years ago
Add code integrity check This PR implements the base foundation of the code signing and integrity check. In this PR implemented is the signing and verification logic, as well as commands to sign single apps or the core repository. Furthermore, there is a basic implementation to display problems with the code integrity on the update screen. Code signing basically happens the following way: - There is a ownCloud Root Certificate authority stored `resources/codesigning/root.crt` (in this PR I also ship the private key which we obviously need to change before a release :wink:). This certificate is not intended to be used for signing directly and only is used to sign new certificates. - Using the `integrity:sign-core` and `integrity:sign-app` commands developers can sign either the core release or a single app. The core release needs to be signed with a certificate that has a CN of `core`, apps need to be signed with a certificate that either has a CN of `core` (shipped apps!) or the AppID. - The command generates a signature.json file of the following format: ```json { "hashes": { "/filename.php": "2401fed2eea6f2c1027c482a633e8e25cd46701f811e2d2c10dc213fd95fa60e350bccbbebdccc73a042b1a2799f673fbabadc783284cc288e4f1a1eacb74e3d", "/lib/base.php": "55548cc16b457cd74241990cc9d3b72b6335f2e5f45eee95171da024087d114fcbc2effc3d5818a6d5d55f2ae960ab39fd0414d0c542b72a3b9e08eb21206dd9" }, "certificate": "-----BEGIN CERTIFICATE-----MIIBvTCCASagAwIBAgIUPvawyqJwCwYazcv7iz16TWxfeUMwDQYJKoZIhvcNAQEF\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTAx\nNDEzMTcxMFoXDTE2MTAxNDEzMTcxMFowEzERMA8GA1UEAwwIY29udGFjdHMwgZ8w\nDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANoQesGdCW0L2L+a2xITYipixkScrIpB\nkX5Snu3fs45MscDb61xByjBSlFgR4QI6McoCipPw4SUr28EaExVvgPSvqUjYLGps\nfiv0Cvgquzbx/X3mUcdk9LcFo1uWGtrTfkuXSKX41PnJGTr6RQWGIBd1V52q1qbC\nJKkfzyeMeuQfAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAvF/KIhRMQ3tYTmgHWsiM\nwDMgIDb7iaHF0fS+/Nvo4PzoTO/trev6tMyjLbJ7hgdCpz/1sNzE11Cibf6V6dsz\njCE9invP368Xv0bTRObRqeSNsGogGl5ceAvR0c9BG+NRIKHcly3At3gLkS2791bC\niG+UxI/MNcWV0uJg9S63LF8=\n-----END CERTIFICATE-----", "signature": "U29tZVNpZ25lZERhdGFFeGFtcGxl" } ``` `hashes` is an array of all files in the folder with their corresponding SHA512 hashes (this is actually quite cheap to calculate), the `certificate` is the certificate used for signing. It has to be issued by the ownCloud Root Authority and it's CN needs to be permitted to perform the required action. The `signature` is then a signature of the `hashes` which can be verified using the `certificate`. Steps to do in other PRs, this is already a quite huge one: - Add nag screen in case the code check fails to ensure that administrators are aware of this. - Add code verification also to OCC upgrade and unify display code more. - Add enforced code verification to apps shipped from the appstore with a level of "official" - Add enfocrced code verification to apps shipped from the appstore that were already signed in a previous release - Add some developer documentation on how devs can request their own certificate - Check when installing ownCloud - Add support for CRLs to allow revoking certificates **Note:** The upgrade checks are only run when the instance has a defined release channel of `stable` (defined in `version.php`). If you want to test this, you need to change the channel thus and then generate the core signature: ``` ➜ master git:(add-integrity-checker) ✗ ./occ integrity:sign-core --privateKey=resources/codesigning/core.key --certificate=resources/codesigning/core.crt Successfully signed "core" ``` Then increase the version and you should see something like the following: ![2015-11-04_12-02-57](https://cloud.githubusercontent.com/assets/878997/10936336/6adb1d14-82ec-11e5-8f06-9a74801c9abf.png) As you can see a failed code check will not prevent the further update. It will instead just be a notice to the admin. In a next step we will add some nag screen. For packaging stable releases this requires the following additional steps as a last action before zipping: 1. Run `./occ integrity:sign-core` once 2. Run `./occ integrity:sign-app` _for each_ app. However, this can be simply automated using a simple foreach on the apps folder.
11 years ago
Add code integrity check This PR implements the base foundation of the code signing and integrity check. In this PR implemented is the signing and verification logic, as well as commands to sign single apps or the core repository. Furthermore, there is a basic implementation to display problems with the code integrity on the update screen. Code signing basically happens the following way: - There is a ownCloud Root Certificate authority stored `resources/codesigning/root.crt` (in this PR I also ship the private key which we obviously need to change before a release :wink:). This certificate is not intended to be used for signing directly and only is used to sign new certificates. - Using the `integrity:sign-core` and `integrity:sign-app` commands developers can sign either the core release or a single app. The core release needs to be signed with a certificate that has a CN of `core`, apps need to be signed with a certificate that either has a CN of `core` (shipped apps!) or the AppID. - The command generates a signature.json file of the following format: ```json { "hashes": { "/filename.php": "2401fed2eea6f2c1027c482a633e8e25cd46701f811e2d2c10dc213fd95fa60e350bccbbebdccc73a042b1a2799f673fbabadc783284cc288e4f1a1eacb74e3d", "/lib/base.php": "55548cc16b457cd74241990cc9d3b72b6335f2e5f45eee95171da024087d114fcbc2effc3d5818a6d5d55f2ae960ab39fd0414d0c542b72a3b9e08eb21206dd9" }, "certificate": "-----BEGIN CERTIFICATE-----MIIBvTCCASagAwIBAgIUPvawyqJwCwYazcv7iz16TWxfeUMwDQYJKoZIhvcNAQEF\nBQAwIzEhMB8GA1UECgwYb3duQ2xvdWQgQ29kZSBTaWduaW5nIENBMB4XDTE1MTAx\nNDEzMTcxMFoXDTE2MTAxNDEzMTcxMFowEzERMA8GA1UEAwwIY29udGFjdHMwgZ8w\nDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANoQesGdCW0L2L+a2xITYipixkScrIpB\nkX5Snu3fs45MscDb61xByjBSlFgR4QI6McoCipPw4SUr28EaExVvgPSvqUjYLGps\nfiv0Cvgquzbx/X3mUcdk9LcFo1uWGtrTfkuXSKX41PnJGTr6RQWGIBd1V52q1qbC\nJKkfzyeMeuQfAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAvF/KIhRMQ3tYTmgHWsiM\nwDMgIDb7iaHF0fS+/Nvo4PzoTO/trev6tMyjLbJ7hgdCpz/1sNzE11Cibf6V6dsz\njCE9invP368Xv0bTRObRqeSNsGogGl5ceAvR0c9BG+NRIKHcly3At3gLkS2791bC\niG+UxI/MNcWV0uJg9S63LF8=\n-----END CERTIFICATE-----", "signature": "U29tZVNpZ25lZERhdGFFeGFtcGxl" } ``` `hashes` is an array of all files in the folder with their corresponding SHA512 hashes (this is actually quite cheap to calculate), the `certificate` is the certificate used for signing. It has to be issued by the ownCloud Root Authority and it's CN needs to be permitted to perform the required action. The `signature` is then a signature of the `hashes` which can be verified using the `certificate`. Steps to do in other PRs, this is already a quite huge one: - Add nag screen in case the code check fails to ensure that administrators are aware of this. - Add code verification also to OCC upgrade and unify display code more. - Add enforced code verification to apps shipped from the appstore with a level of "official" - Add enfocrced code verification to apps shipped from the appstore that were already signed in a previous release - Add some developer documentation on how devs can request their own certificate - Check when installing ownCloud - Add support for CRLs to allow revoking certificates **Note:** The upgrade checks are only run when the instance has a defined release channel of `stable` (defined in `version.php`). If you want to test this, you need to change the channel thus and then generate the core signature: ``` ➜ master git:(add-integrity-checker) ✗ ./occ integrity:sign-core --privateKey=resources/codesigning/core.key --certificate=resources/codesigning/core.crt Successfully signed "core" ``` Then increase the version and you should see something like the following: ![2015-11-04_12-02-57](https://cloud.githubusercontent.com/assets/878997/10936336/6adb1d14-82ec-11e5-8f06-9a74801c9abf.png) As you can see a failed code check will not prevent the further update. It will instead just be a notice to the admin. In a next step we will add some nag screen. For packaging stable releases this requires the following additional steps as a last action before zipping: 1. Run `./occ integrity:sign-core` once 2. Run `./occ integrity:sign-app` _for each_ app. However, this can be simply automated using a simple foreach on the apps folder.
11 years ago
  1. <?php
  2. /**
  3. * @author Adam Williamson <awilliam@redhat.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Christopher Schäpers <kondou@ts.unde.re>
  6. * @author Clark Tomlinson <fallen013@gmail.com>
  7. * @author Joas Schilling <nickvergessen@owncloud.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@owncloud.com>
  10. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Remco Brenninkmeijer <requist1@starmail.nl>
  13. * @author Robin Appelman <icewind@owncloud.com>
  14. * @author Robin McCorkell <robin@mccorkell.me.uk>
  15. * @author Roeland Jago Douma <rullzer@owncloud.com>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  18. *
  19. * @copyright Copyright (c) 2016, ownCloud, Inc.
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. namespace OC;
  36. use Assetic\Asset\AssetCollection;
  37. use Assetic\Asset\FileAsset;
  38. use Assetic\AssetWriter;
  39. use Assetic\Filter\CssImportFilter;
  40. use Assetic\Filter\CssMinFilter;
  41. use Assetic\Filter\CssRewriteFilter;
  42. use Assetic\Filter\JSqueezeFilter;
  43. use Assetic\Filter\SeparatorFilter;
  44. class TemplateLayout extends \OC_Template {
  45. private static $versionHash = '';
  46. /**
  47. * @var \OCP\IConfig
  48. */
  49. private $config;
  50. /**
  51. * @param string $renderAs
  52. * @param string $appId application id
  53. */
  54. public function __construct( $renderAs, $appId = '' ) {
  55. // yes - should be injected ....
  56. $this->config = \OC::$server->getConfig();
  57. // Decide which page we show
  58. if($renderAs == 'user') {
  59. parent::__construct( 'core', 'layout.user' );
  60. if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
  61. $this->assign('bodyid', 'body-settings');
  62. }else{
  63. $this->assign('bodyid', 'body-user');
  64. }
  65. // Code integrity notification
  66. $integrityChecker = \OC::$server->getIntegrityCodeChecker();
  67. if(\OC_User::isAdminUser(\OC_User::getUser()) && !$integrityChecker->hasPassedCheck()) {
  68. \OCP\Util::addScript('core', 'integritycheck-failed-notification');
  69. }
  70. // Add navigation entry
  71. $this->assign( 'application', '');
  72. $this->assign( 'appid', $appId );
  73. $navigation = \OC_App::getNavigation();
  74. $this->assign( 'navigation', $navigation);
  75. $settingsNavigation = \OC_App::getSettingsNavigation();
  76. $this->assign( 'settingsnavigation', $settingsNavigation);
  77. foreach($navigation as $entry) {
  78. if ($entry['active']) {
  79. $this->assign( 'application', $entry['name'] );
  80. break;
  81. }
  82. }
  83. foreach($settingsNavigation as $entry) {
  84. if ($entry['active']) {
  85. $this->assign( 'application', $entry['name'] );
  86. break;
  87. }
  88. }
  89. $userDisplayName = \OC_User::getDisplayName();
  90. $appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0;
  91. if ($appsMgmtActive) {
  92. $l = \OC::$server->getL10N('lib');
  93. $this->assign('application', $l->t('Apps'));
  94. }
  95. $this->assign('user_displayname', $userDisplayName);
  96. $this->assign('user_uid', \OC_User::getUser());
  97. $this->assign('appsmanagement_active', $appsMgmtActive);
  98. $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true);
  99. if (\OC_User::getUser() === false) {
  100. $this->assign('userAvatarSet', false);
  101. } else {
  102. $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
  103. }
  104. } else if ($renderAs == 'error') {
  105. parent::__construct('core', 'layout.guest', '', false);
  106. $this->assign('bodyid', 'body-login');
  107. } else if ($renderAs == 'guest') {
  108. parent::__construct('core', 'layout.guest');
  109. $this->assign('bodyid', 'body-login');
  110. } else {
  111. parent::__construct('core', 'layout.base');
  112. }
  113. // Send the language to our layouts
  114. $this->assign('language', \OC_L10N::findLanguage());
  115. if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
  116. if (empty(self::$versionHash)) {
  117. $v = \OC_App::getAppVersions();
  118. $v['core'] = implode('.', \OCP\Util::getVersion());
  119. self::$versionHash = md5(implode(',', $v));
  120. }
  121. } else {
  122. self::$versionHash = md5('not installed');
  123. }
  124. $useAssetPipeline = self::isAssetPipelineEnabled();
  125. if ($useAssetPipeline) {
  126. $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
  127. $this->generateAssets();
  128. } else {
  129. // Add the js files
  130. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  131. $this->assign('jsfiles', array());
  132. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  133. $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
  134. }
  135. foreach($jsFiles as $info) {
  136. $web = $info[1];
  137. $file = $info[2];
  138. $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  139. }
  140. // Add the css files
  141. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  142. $this->assign('cssfiles', array());
  143. $this->assign('printcssfiles', []);
  144. foreach($cssFiles as $info) {
  145. $web = $info[1];
  146. $file = $info[2];
  147. if (substr($file, -strlen('print.css')) === 'print.css') {
  148. $this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  149. } else {
  150. $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. * @param array $styles
  157. * @return array
  158. */
  159. static public function findStylesheetFiles($styles) {
  160. // Read the selected theme from the config file
  161. $theme = \OC_Util::getTheme();
  162. $locator = new \OC\Template\CSSResourceLocator(
  163. \OC::$server->getLogger(),
  164. $theme,
  165. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  166. array( \OC::$SERVERROOT => \OC::$WEBROOT ));
  167. $locator->find($styles);
  168. return $locator->getResources();
  169. }
  170. /**
  171. * @param array $scripts
  172. * @return array
  173. */
  174. static public function findJavascriptFiles($scripts) {
  175. // Read the selected theme from the config file
  176. $theme = \OC_Util::getTheme();
  177. $locator = new \OC\Template\JSResourceLocator(
  178. \OC::$server->getLogger(),
  179. $theme,
  180. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  181. array( \OC::$SERVERROOT => \OC::$WEBROOT ));
  182. $locator->find($scripts);
  183. return $locator->getResources();
  184. }
  185. public function generateAssets() {
  186. $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT);
  187. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  188. $jsHash = self::hashFileNames($jsFiles);
  189. if (!file_exists("$assetDir/assets/$jsHash.js")) {
  190. $jsFiles = array_map(function ($item) {
  191. $root = $item[0];
  192. $file = $item[2];
  193. // no need to minifiy minified files
  194. if (substr($file, -strlen('.min.js')) === '.min.js') {
  195. return new FileAsset($root . '/' . $file, array(
  196. new SeparatorFilter(';')
  197. ), $root, $file);
  198. }
  199. return new FileAsset($root . '/' . $file, array(
  200. new JSqueezeFilter(),
  201. new SeparatorFilter(';')
  202. ), $root, $file);
  203. }, $jsFiles);
  204. $jsCollection = new AssetCollection($jsFiles);
  205. $jsCollection->setTargetPath("assets/$jsHash.js");
  206. $writer = new AssetWriter($assetDir);
  207. $writer->writeAsset($jsCollection);
  208. }
  209. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  210. // differentiate between screen stylesheets and printer stylesheets
  211. $screenCssFiles = array_filter($cssFiles, function($cssFile) {
  212. return substr_compare($cssFile[2], 'print.css', -strlen('print.css')) !== 0;
  213. });
  214. $screenCssAsset = $this->generateCssAsset($screenCssFiles);
  215. $printCssFiles = array_filter($cssFiles, function($cssFile) {
  216. return substr_compare($cssFile[2], 'print.css', -strlen('print.css')) === 0;
  217. });
  218. $printCssAsset = $this->generateCssAsset($printCssFiles);
  219. $this->append('jsfiles', \OC::$server->getURLGenerator()->linkTo('assets', "$jsHash.js"));
  220. $this->append('cssfiles', $screenCssAsset);
  221. $this->append('printcssfiles', $printCssAsset);
  222. }
  223. /**
  224. * generates a single css asset file from an array of css files if at least one of them has changed
  225. * otherwise it just returns the path to the old asset file
  226. * @param $files
  227. * @return string
  228. */
  229. private function generateCssAsset($files) {
  230. $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT);
  231. $hash = self::hashFileNames($files);
  232. if (!file_exists("$assetDir/assets/$hash.css")) {
  233. $files = array_map(function ($item) {
  234. $root = $item[0];
  235. $file = $item[2];
  236. $assetPath = $root . '/' . $file;
  237. $sourceRoot = \OC::$SERVERROOT;
  238. $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT));
  239. return new FileAsset(
  240. $assetPath,
  241. array(
  242. new CssRewriteFilter(),
  243. new CssMinFilter(),
  244. new CssImportFilter()
  245. ),
  246. $sourceRoot,
  247. $sourcePath
  248. );
  249. }, $files);
  250. $cssCollection = new AssetCollection($files);
  251. $cssCollection->setTargetPath("assets/$hash.css");
  252. $writer = new AssetWriter($assetDir);
  253. $writer->writeAsset($cssCollection);
  254. }
  255. return \OC::$server->getURLGenerator()->linkTo('assets', "$hash.css");
  256. }
  257. /**
  258. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  259. * @param string $filePath Absolute path
  260. * @return string Relative path
  261. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  262. */
  263. public static function convertToRelativePath($filePath) {
  264. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  265. if(count($relativePath) !== 2) {
  266. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  267. }
  268. return $relativePath[1];
  269. }
  270. /**
  271. * @param array $files
  272. * @return string
  273. */
  274. private static function hashFileNames($files) {
  275. foreach($files as $i => $file) {
  276. try {
  277. $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2];
  278. } catch (\Exception $e) {
  279. $files[$i] = $file[0].'/'.$file[2];
  280. }
  281. }
  282. sort($files);
  283. // include the apps' versions hash to invalidate the cached assets
  284. $files[] = self::$versionHash;
  285. return hash('md5', implode('', $files));
  286. }
  287. }