PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/administrator/components/com_akeeba/akeeba/utils/securesettings.php

https://github.com/CCI-Studios/Wee-Magazine
PHP | 144 lines | 79 code | 20 blank | 45 comment | 27 complexity | f9fb0e82ce672baf30ce95fb610254eb MD5 | raw file
  1. <?php
  2. /**
  3. * @package AkeebaBackup
  4. * @copyright Copyright (c)2009-2012 Nicholas K. Dionysopoulos
  5. * @license GNU General Public License version 3, or later
  6. *
  7. * @since 3.2
  8. */
  9. // Protection against direct access
  10. defined('AKEEBAENGINE') or die();
  11. /**
  12. * Implements encrypted settings handling features
  13. * @author nicholas
  14. */
  15. class AEUtilSecuresettings
  16. {
  17. /**
  18. * Gets the configured server key, automatically loading the server key storage file
  19. * if required.
  20. * @return string
  21. */
  22. public static function getKey()
  23. {
  24. if(defined('AKEEBA_SERVERKEY')) return base64_decode(AKEEBA_SERVERKEY);
  25. $filename = dirname(__FILE__).'/../serverkey.php';
  26. if(file_exists($filename)) {
  27. include_once $filename;
  28. }
  29. if(defined('AKEEBA_SERVERKEY')) return base64_decode(AKEEBA_SERVERKEY);
  30. return '';
  31. }
  32. /**
  33. * Do the server options allow us to use settings encryption?
  34. * @return bool
  35. */
  36. public static function supportsEncryption()
  37. {
  38. // Do we have the encypt.php plugin?
  39. $filename = dirname(__FILE__).'/../utils/encrypt.php';
  40. if(!file_exists($filename)) return false;
  41. // Did the user intentionally disable settings encryption?
  42. $useEncryption = AEPlatform::getInstance()->get_platform_configuration_option('useencryption', -1);
  43. if($useEncryption == 0) return false;
  44. // Do we have base64_encode/_decode required for encryption?
  45. if(!function_exists('base64_encode') || !function_exists('base64_decode')) return false;
  46. // Pre-requisites met. We can encrypt and decrypt!
  47. return true;
  48. }
  49. /**
  50. * Gets the preferred encryption mode. Currently, if mcrypt is installed and activated we will
  51. * use AES128.
  52. * @return string
  53. */
  54. public static function preferredEncryption()
  55. {
  56. if(function_exists('mcrypt_module_open')) {
  57. return 'AES128';
  58. } else {
  59. return 'CTR128';
  60. }
  61. }
  62. /**
  63. * Encrypts the settings using the automatically detected preferred algorithm
  64. * @param $settingsINI string The raw settings INI string
  65. * @return string The encrypted data to store in the database
  66. */
  67. public static function encryptSettings($settingsINI, $key = null)
  68. {
  69. // Do we really support encryption?
  70. if(!self::supportsEncryption()) return $settingsINI;
  71. // Does any of the preferred encryption engines exist?
  72. $encryption = self::preferredEncryption();
  73. if(empty($encryption)) return $settingsINI;
  74. // Do we have a non-empty key to begin with?
  75. if(empty($key)) $key = self::getKey();
  76. if(empty($key)) return $settingsINI;
  77. if($encryption == 'AES128') {
  78. $encrypted = AEUtilEncrypt::AESEncryptCBC($settingsINI, $key, 128);
  79. if(empty($encrypted)) {
  80. $encryption = 'CTR128';
  81. } else {
  82. // Note: CBC returns the encrypted data as a binary string and requires Base 64 encoding
  83. $settingsINI = '###AES128###'.base64_encode($encrypted);
  84. }
  85. }
  86. if($encryption == 'CTR128') {
  87. $encrypted = AEUtilEncrypt::AESEncryptCtr($settingsINI, $key, 128);
  88. if(empty($encrypted)) {
  89. $encryption = '';
  90. } else {
  91. // Note: CTR returns the encrypted data readily encoded in Base 64
  92. $settingsINI = '###CTR128###'.$encrypted;
  93. }
  94. }
  95. return $settingsINI;
  96. }
  97. /**
  98. * Decrypts the encrypted settings and returns the plaintext INI string
  99. * @param $encrypted string The encrypted data
  100. * @return string The decrypted data
  101. */
  102. public static function decryptSettings($encrypted, $key = null)
  103. {
  104. if(substr($encrypted, 0, 12) == '###AES128###') {
  105. $mode = 'AES128';
  106. } elseif(substr($encrypted, 0, 12) == '###CTR128###') {
  107. $mode = 'CTR128';
  108. } else {
  109. return $encrypted;
  110. }
  111. if(empty($key)) $key = self::getKey();
  112. $encrypted = substr($encrypted, 12);
  113. switch($mode) {
  114. case 'AES128':
  115. $encrypted = base64_decode($encrypted);
  116. $decrypted = AEUtilEncrypt::AESDecryptCBC($encrypted, $key, 128);
  117. break;
  118. case 'CTR128':
  119. $decrypted = AEUtilEncrypt::AESDecryptCtr($encrypted, $key, 128);
  120. break;
  121. }
  122. return $decrypted;
  123. }
  124. }