PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/apps/files_encryption/lib/helper.php

https://github.com/sezuan/core
PHP | 245 lines | 108 code | 46 blank | 91 comment | 17 complexity | fc9d5c16f6ba39fcb5165ee701698471 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Florin Peter
  6. * @copyright 2013 Florin Peter <owncloud@florin-peter.de>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Encryption;
  23. /**
  24. * @brief Class to manage registration of hooks an various helper methods
  25. * @package OCA\Encryption
  26. */
  27. class Helper {
  28. /**
  29. * @brief register share related hooks
  30. *
  31. */
  32. public static function registerShareHooks() {
  33. \OCP\Util::connectHook('OCP\Share', 'pre_shared', 'OCA\Encryption\Hooks', 'preShared');
  34. \OCP\Util::connectHook('OCP\Share', 'post_shared', 'OCA\Encryption\Hooks', 'postShared');
  35. \OCP\Util::connectHook('OCP\Share', 'post_unshare', 'OCA\Encryption\Hooks', 'postUnshare');
  36. }
  37. /**
  38. * @brief register user related hooks
  39. *
  40. */
  41. public static function registerUserHooks() {
  42. \OCP\Util::connectHook('OC_User', 'post_login', 'OCA\Encryption\Hooks', 'login');
  43. \OCP\Util::connectHook('OC_User', 'post_setPassword', 'OCA\Encryption\Hooks', 'setPassphrase');
  44. \OCP\Util::connectHook('OC_User', 'pre_setPassword', 'OCA\Encryption\Hooks', 'preSetPassphrase');
  45. \OCP\Util::connectHook('OC_User', 'post_createUser', 'OCA\Encryption\Hooks', 'postCreateUser');
  46. \OCP\Util::connectHook('OC_User', 'post_deleteUser', 'OCA\Encryption\Hooks', 'postDeleteUser');
  47. }
  48. /**
  49. * @brief register filesystem related hooks
  50. *
  51. */
  52. public static function registerFilesystemHooks() {
  53. \OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Encryption\Hooks', 'postRename');
  54. }
  55. /**
  56. * @brief register app management related hooks
  57. *
  58. */
  59. public static function registerAppHooks() {
  60. \OCP\Util::connectHook('OC_App', 'pre_disable', 'OCA\Encryption\Hooks', 'preDisable');
  61. }
  62. /**
  63. * @brief setup user for files_encryption
  64. *
  65. * @param Util $util
  66. * @param string $password
  67. * @return bool
  68. */
  69. public static function setupUser($util, $password) {
  70. // Check files_encryption infrastructure is ready for action
  71. if (!$util->ready()) {
  72. \OCP\Util::writeLog('Encryption library', 'User account "' . $util->getUserId()
  73. . '" is not ready for encryption; configuration started', \OCP\Util::DEBUG);
  74. if (!$util->setupServerSide($password)) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. /**
  81. * @brief enable recovery
  82. *
  83. * @param $recoveryKeyId
  84. * @param $recoveryPassword
  85. * @internal param \OCA\Encryption\Util $util
  86. * @internal param string $password
  87. * @return bool
  88. */
  89. public static function adminEnableRecovery($recoveryKeyId, $recoveryPassword) {
  90. $view = new \OC\Files\View('/');
  91. if ($recoveryKeyId === null) {
  92. $recoveryKeyId = 'recovery_' . substr(md5(time()), 0, 8);
  93. \OC_Appconfig::setValue('files_encryption', 'recoveryKeyId', $recoveryKeyId);
  94. }
  95. if (!$view->is_dir('/owncloud_private_key')) {
  96. $view->mkdir('/owncloud_private_key');
  97. }
  98. if (
  99. (!$view->file_exists("/public-keys/" . $recoveryKeyId . ".public.key")
  100. || !$view->file_exists("/owncloud_private_key/" . $recoveryKeyId . ".private.key"))
  101. ) {
  102. $keypair = \OCA\Encryption\Crypt::createKeypair();
  103. \OC_FileProxy::$enabled = false;
  104. // Save public key
  105. if (!$view->is_dir('/public-keys')) {
  106. $view->mkdir('/public-keys');
  107. }
  108. $view->file_put_contents('/public-keys/' . $recoveryKeyId . '.public.key', $keypair['publicKey']);
  109. // Encrypt private key empty passphrase
  110. $encryptedPrivateKey = \OCA\Encryption\Crypt::symmetricEncryptFileContent($keypair['privateKey'], $recoveryPassword);
  111. // Save private key
  112. $view->file_put_contents('/owncloud_private_key/' . $recoveryKeyId . '.private.key', $encryptedPrivateKey);
  113. \OC_FileProxy::$enabled = true;
  114. // Set recoveryAdmin as enabled
  115. \OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 1);
  116. $return = true;
  117. } else { // get recovery key and check the password
  118. $util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), \OCP\User::getUser());
  119. $return = $util->checkRecoveryPassword($recoveryPassword);
  120. if ($return) {
  121. \OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 1);
  122. }
  123. }
  124. return $return;
  125. }
  126. /**
  127. * @brief disable recovery
  128. *
  129. * @param $recoveryPassword
  130. * @return bool
  131. */
  132. public static function adminDisableRecovery($recoveryPassword) {
  133. $util = new Util(new \OC_FilesystemView('/'), \OCP\User::getUser());
  134. $return = $util->checkRecoveryPassword($recoveryPassword);
  135. if ($return) {
  136. // Set recoveryAdmin as disabled
  137. \OC_Appconfig::setValue('files_encryption', 'recoveryAdminEnabled', 0);
  138. }
  139. return $return;
  140. }
  141. /**
  142. * @brief checks if access is public/anonymous user
  143. * @return bool
  144. */
  145. public static function isPublicAccess() {
  146. if (\OCP\USER::getUser() === false
  147. || (isset($_GET['service']) && $_GET['service'] == 'files'
  148. && isset($_GET['t']))
  149. ) {
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }
  155. /**
  156. * @brief Format a path to be relative to the /user/files/ directory
  157. * @param string $path the absolute path
  158. * @return string e.g. turns '/admin/files/test.txt' into 'test.txt'
  159. */
  160. public static function stripUserFilesPath($path) {
  161. $trimmed = ltrim($path, '/');
  162. $split = explode('/', $trimmed);
  163. $sliced = array_slice($split, 2);
  164. $relPath = implode('/', $sliced);
  165. return $relPath;
  166. }
  167. /**
  168. * @brief redirect to a error page
  169. */
  170. public static function redirectToErrorPage() {
  171. $location = \OC_Helper::linkToAbsolute('apps/files_encryption/files', 'error.php');
  172. $post = 0;
  173. if(count($_POST) > 0) {
  174. $post = 1;
  175. }
  176. header('Location: ' . $location . '?p=' . $post);
  177. exit();
  178. }
  179. /**
  180. * check requirements for encryption app.
  181. * @return bool true if requirements are met
  182. */
  183. public static function checkRequirements() {
  184. $result = true;
  185. //openssl extension needs to be loaded
  186. $result &= extension_loaded("openssl");
  187. // we need php >= 5.3.3
  188. $result &= version_compare(phpversion(), '5.3.3', '>=');
  189. return (bool) $result;
  190. }
  191. /**
  192. * @brief glob uses different pattern than regular expressions, escape glob pattern only
  193. * @param unescaped path
  194. * @return escaped path
  195. */
  196. public static function escapeGlobPattern($path) {
  197. return preg_replace('/(\*|\?|\[)/', '[$1]', $path);
  198. }
  199. }