start.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * ElggPG -- GNUPG encryption for Elgg
  4. *
  5. * @package Lorea
  6. * @subpackage ElggPG
  7. * @homepage https://lorea.org/plugin/elggpg
  8. * @copyright 2011-2013 Lorea Faeries <federation@lorea.org>
  9. * @license COPYING, http://www.gnu.org/licenses/agpl
  10. *
  11. * Copyright 2011-2013 Lorea Faeries <federation@lorea.org>
  12. *
  13. * This program is free software: you can redistribute it and/or
  14. * modify it under the terms of the GNU Affero General Public License
  15. * as published by the Free Software Foundation, either version 3 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public
  24. * License along with this program. If not, see
  25. * <http://www.gnu.org/licenses/>.
  26. */
  27. elgg_register_event_handler('init', 'system', 'elggpg_init');
  28. /**
  29. * GnuPG plugin initialization functions.
  30. */
  31. function elggpg_init() {
  32. // Register library
  33. elgg_register_library('elggpg', elgg_get_plugins_path() . 'elggpg/lib/elggpg.php');
  34. elgg_register_library('elggpg:send:override', elgg_get_plugins_path() . 'elggpg/lib/messages_send.php');
  35. // Extend CSS
  36. elgg_extend_view('css/elgg', 'elggpg/css');
  37. // elgg_extend_view('profile/status', 'elggpg/profile_elggpg');
  38. // Register a page handler, so we can have nice URLs
  39. elgg_register_page_handler('elggpg', 'elggpg_page_handler');
  40. // Add fingerprint in user details
  41. elgg_register_plugin_hook_handler('profile:fields', 'profile', 'elggpg_profile_fingerprint');
  42. // Register a notification handler to encrypt messages
  43. elgg_register_plugin_hook_handler('email', 'system', 'elggpg_send_email_handler');
  44. elgg_extend_view("core/settings/account/email", "elggpg/viewkey", 1);
  45. // Actions
  46. $actions_path = elgg_get_plugins_path() . 'elggpg/actions/elggpg';
  47. elgg_register_action("elggpg/pubkey_upload", "$actions_path/pubkey_upload.php");
  48. elgg_register_action("elggpg/pubkey_delete", "$actions_path/pubkey_delete.php");
  49. elgg_register_action("messages/send", "$actions_path/send_encrypted.php");
  50. // add a GPG link to owner blocks
  51. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'elggpg_owner_block_menu');
  52. // upgrade
  53. elgg_register_event_handler('upgrade', 'system', 'elggpg_run_upgrades');
  54. }
  55. /**
  56. * Page handler
  57. *
  58. * @param array $page Url parts
  59. *
  60. * @return bool
  61. */
  62. function elggpg_page_handler($page) {
  63. $pages_dir = elgg_get_plugins_path() . 'elggpg/pages/elggpg';
  64. switch($page[0]) {
  65. case 'owner':
  66. include("$pages_dir/owner.php");
  67. break;
  68. case 'raw':
  69. set_input('username', $page[1]);
  70. include("$pages_dir/raw.php");
  71. break;
  72. case 'download':
  73. set_input('username', $page[1]);
  74. include("$pages_dir/download.php");
  75. break;
  76. default:
  77. return false;
  78. }
  79. return true;
  80. }
  81. /**
  82. * It hijacks the email sender to encrypt the messages if needed.
  83. *
  84. * @param string $hook
  85. * @param string $type
  86. * @param bool $return
  87. * @param array $params Includes $from, $to, $subject, $body and $headers
  88. *
  89. * @return bool
  90. */
  91. function elggpg_send_email_handler($hook, $type, $return, $params) {
  92. $from = $params['from'];
  93. $to = $params['to'];
  94. $subject = $params['subject'];
  95. $body = $params['body'];
  96. $headers = $params['headers'];
  97. $receiver = current(get_user_by_email($to));
  98. // Format message
  99. $body = html_entity_decode($body, ENT_COMPAT, 'UTF-8'); // Decode any html entities
  100. $body = elgg_strip_tags($body); // Strip tags from message
  101. $body = preg_replace("/(\r\n|\r)/", "\n", $body); // Convert to unix line endings in body
  102. $body = preg_replace("/^From/", ">From", $body); // Change lines starting with From to >From
  103. $body = wordwrap($body);
  104. // Encrypting
  105. if (elgg_get_plugin_user_setting('encrypt_emails', $receiver->guid, 'elggpg') != 'no') {
  106. elgg_load_library('elggpg');
  107. $encrypted_body = elggpg_encrypt($body, $receiver, false);
  108. if ($encrypted_body) {
  109. $body = $encrypted_body;
  110. }
  111. }
  112. // The following code is the same that in elgg's
  113. $header_eol = "\r\n";
  114. if (elgg_get_config('broken_mta')) {
  115. // Allow non-RFC 2822 mail headers to support some broken MTAs
  116. $header_eol = "\n";
  117. }
  118. // Windows is somewhat broken, so we use just address for to and from
  119. if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
  120. // strip name from to and from
  121. if (strpos($to, '<')) {
  122. preg_match('/<(.*)>/', $to, $matches);
  123. $to = $matches[1];
  124. }
  125. if (strpos($from, '<')) {
  126. preg_match('/<(.*)>/', $from, $matches);
  127. $from = $matches[1];
  128. }
  129. }
  130. if (empty($headers)) {
  131. $headers = "From: $from{$header_eol}"
  132. . "Content-Type: text/plain; charset=UTF-8; format=flowed{$header_eol}"
  133. . "MIME-Version: 1.0{$header_eol}"
  134. . "Content-Transfer-Encoding: 8bit{$header_eol}";
  135. }
  136. // Sanitise subject by stripping line endings
  137. $subject = preg_replace("/(\r\n|\r|\n)/", " ", $subject);
  138. // this is because Elgg encodes everything and matches what is done with body
  139. $subject = html_entity_decode($subject, ENT_COMPAT, 'UTF-8'); // Decode any html entities
  140. if (is_callable('mb_encode_mimeheader')) {
  141. $subject = mb_encode_mimeheader($subject, "UTF-8", "B");
  142. }
  143. // stringify headers
  144. $headers_string = '';
  145. foreach ($headers as $key => $value) {
  146. $headers_string .= "$key: $value{$header_eol}";
  147. }
  148. return mail($to, $subject, $body, $headers_string);
  149. }
  150. /**
  151. * Add a fingerprint profile field
  152. *
  153. * @param string $hook
  154. * @param string $type
  155. * @param array $return
  156. * @param array $profile_defaults
  157. *
  158. * @return array
  159. */
  160. function elggpg_profile_fingerprint ($hook, $type, $return, $profile_defaults) {
  161. $return['openpgp_publickey'] = 'fingerprint';
  162. return $return;
  163. }
  164. /**
  165. * Add a menu item to an ownerblock
  166. *
  167. * @param string $hook
  168. * @param string $type
  169. * @param array $return
  170. * @param array $params
  171. *
  172. * @return array
  173. */
  174. function elggpg_owner_block_menu($hook, $type, $return, $params) {
  175. if ($params['entity']->guid == elgg_get_logged_in_user_guid()) {
  176. $url = "elggpg/owner/{$params['entity']->username}";
  177. /**
  178. if ($params['entity'] == elgg_get_logged_in_user_entity()) {
  179. $item = new ElggMenuItem('elggpg', elgg_echo('elggpg:manage'), $url);
  180. }
  181. $item->setSection('manage');
  182. */
  183. $item = new ElggMenuItem('elggpg', elgg_echo('elggpg:manage'), $url);
  184. $return[] = $item;
  185. }
  186. return $return;
  187. }
  188. /**
  189. * Process upgrades for the elggpg plugin
  190. */
  191. function elggpg_run_upgrades() {
  192. $path = elgg_get_plugins_path() . 'elggpg/upgrades/';
  193. $files = elgg_get_upgrade_files($path);
  194. foreach ($files as $file) {
  195. include "$path{$file}";
  196. }
  197. }