EncryptedContentHelper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Kanboard\Plugin\EncryptedContent\Helper;
  3. require_once __DIR__.'/../vendor/autoload.php';
  4. use Kanboard\Core\Base;
  5. use Defuse\Crypto\Key;
  6. use Defuse\Crypto\Crypto;
  7. /**
  8. *
  9. * EncryptedContent helper
  10. * @author Valentino Pesce
  11. *
  12. */
  13. class EncryptedContentHelper extends Base
  14. {
  15. protected function loadEncryptionKeyFromConfig()
  16. {
  17. $key = $this->request->getRawValue('key');
  18. if (!empty($key)) {
  19. $keyAscii = rtrim($key);
  20. return Key::loadFromAsciiSafeString($keyAscii);
  21. }
  22. }
  23. /**
  24. * Display a input field
  25. *
  26. * @access public
  27. * @param string $type HMTL input tag type
  28. * @param string $name Field name
  29. * @param array $values Form values
  30. * @param array $attributes HTML attributes
  31. * @param string $class CSS class
  32. * @return string
  33. */
  34. public function input($type, $name, $values = array(), array $attributes = array(), $class = '')
  35. {
  36. $html = '<input type="'.$type.'" name="'.$name.'" id="form-'.$name.'" class="'.$class.'" ';
  37. $html .= implode(' ', $attributes).'>';
  38. return $html;
  39. }
  40. /**
  41. * Display a markdown editor
  42. *
  43. * @access public
  44. * @param string $name Field name
  45. * @param array $values Form values
  46. * @param array $attributes
  47. * @return string
  48. */
  49. public function renderEncryptedtextEditor($name, $values = array(), array $attributes = array())
  50. {
  51. if (!isset($values[$name])) {
  52. $content = null;
  53. } elseif (ctype_xdigit($values[$name])) {
  54. $content = Crypto::decrypt($values[$name], $this->loadEncryptionKeyFromConfig());
  55. } elseif ($values[$name]) {
  56. $content = Crypto::encrypt($values[$name], $this->loadEncryptionKeyFromConfig());
  57. }
  58. $params = array(
  59. 'name' => $name,
  60. 'required' => 'required',
  61. 'tabindex' => isset($attributes['tabindex']) ? $attributes['tabindex'] : '-1',
  62. 'labelPreview' => t('Preview'),
  63. 'labelWrite' => t('Write'),
  64. 'placeholder' => t('Write your text in Markdown'),
  65. 'autofocus' => isset($attributes['autofocus']) && $attributes['autofocus'],
  66. 'suggestOptions' => array(
  67. 'triggers' => array(
  68. '#' => $this->helper->url->to('TaskAjaxController', 'suggest', array('search' => 'SEARCH_TERM')),
  69. )
  70. ),
  71. );
  72. if (isset($values['project_id'])) {
  73. $params['suggestOptions']['triggers']['@'] = $this->helper->url->to('UserAjaxController', 'mention', array('project_id' => $values['project_id'], 'search' => 'SEARCH_TERM'));
  74. }
  75. $html = '<div class="js-text-editor" data-params=\''.json_encode($params, JSON_HEX_APOS).'\'>';
  76. $html .= '<script type="text/template">'.$content.'</script>';
  77. $html .= '</div>';
  78. return $html;
  79. }
  80. public function renderDecrypt($value)
  81. {
  82. $key = $this->loadEncryptionKeyFromConfig();
  83. if (!empty($key) && ctype_xdigit($value)) {
  84. return Crypto::decrypt($value, $this->loadEncryptionKeyFromConfig());
  85. }
  86. return t('Unlock to view the content');
  87. }
  88. public function EncryptedValue($value)
  89. {
  90. if ($value) {
  91. return Crypto::encrypt($value, $this->loadEncryptionKeyFromConfig());
  92. }
  93. }
  94. public function generateNewRandomKey()
  95. {
  96. $key = Key::createNewRandomKey();
  97. return $key->saveToAsciiSafeString();
  98. }
  99. }