2012022501.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * ElggPG -- Upgrade from 1.7 to 1.8
  4. *
  5. * @package Lorea
  6. * @subpackage ElggPG
  7. *
  8. * Copyright 2011-2013 Lorea Faeries <federation@lorea.org>
  9. *
  10. * This file is part of the ElggPG plugin for Elgg.
  11. *
  12. * ElggPG is free software: you can redistribute it and/or modify it
  13. * under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * ElggPG is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public
  23. * License along with this program. If not, see
  24. * <http://www.gnu.org/licenses/>.
  25. */
  26. $items = elgg_get_entities(array(
  27. 'type' => 'user',
  28. 'limit' => 5,
  29. 'order_by' => 'e.time_created asc',
  30. ));
  31. // if not items, no upgrade required
  32. if (!$items) {
  33. return;
  34. }
  35. $local_version = (int)elgg_get_plugin_setting('version', 'elggpg');
  36. if (2012022501 <= $local_version) {
  37. error_log("ElggPG requires no upgrade");
  38. // no upgrade required
  39. return;
  40. }
  41. global $MIGRATED;
  42. $MIGRATED = 0;
  43. /**
  44. * Sets the opengpg_publickey for users having a public key
  45. *
  46. * @param ElggObject $item
  47. * @return bool
  48. */
  49. function elggpg_2012022501($user) {
  50. // it is necessary to load the gpg library to make sure gpg path is set.
  51. global $MIGRATED;
  52. $MIGRATED += 1;
  53. if ($MIGRATED % 100 == 0) {
  54. error_log(" * elggpg $user->guid");
  55. }
  56. elgg_load_library('elggpg');
  57. $user_fp = current(elgg_get_metadata(array(
  58. 'guid' => $user->guid,
  59. 'metadata_name' => 'openpgp_publickey',
  60. )));
  61. $gnupg = new gnupg();
  62. if (!$user_fp && $user->email) {
  63. try {
  64. $info = $gnupg->keyinfo($user->email);
  65. $fingerprint = $info[0]['subkeys'][0]['fingerprint'];
  66. if ($fingerprint) {
  67. create_metadata($user->guid, "openpgp_publickey", $fingerprint, 'text', $user->guid, ACCESS_LOGGEDIN);
  68. }
  69. }
  70. catch (Exception $e) {
  71. // no encryption key
  72. }
  73. }
  74. return true;
  75. }
  76. $previous_access = elgg_set_ignore_access(true);
  77. $options = array(
  78. 'type' => 'user',
  79. 'limit' => 0,
  80. );
  81. $batch = new ElggBatch('elgg_get_entities', $options, 'elggpg_2012022501', 100);
  82. elgg_set_ignore_access($previous_access);
  83. if ($batch->callbackResult) {
  84. error_log("Elgg elggpg upgrade (2012022501) succeeded");
  85. elgg_set_plugin_setting('version', 2012022501, 'elggpg');
  86. } else {
  87. error_log("Elgg elggpg upgrade (2012022501) failed");
  88. }