summaryrefslogtreecommitdiff
path: root/horde/turba2/create_default_histories.php
blob: 5b263913d126c8027668fd9b45d21ce6c02b43ba (plain)
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4.  * $Horde: turba/scripts/upgrades/create_default_histories.php,v 1.1.2.3 2006/04/18 16:27:10 jan Exp $
  5. *
  6. * Copyright 1999-2006 Charles J. Hagenbuch <chuck@horde.org>
  7. *
  8. * See the enclosed file LICENSE for license information (ASL). If you
  9. * did not receive this file, see http://www.horde.org/licenses/asl.php.
  10. */
  11. @define('AUTH_HANDLER', true);
  12. @define('HORDE_BASE', dirname(__FILE__) . '/../../..');
  13. @define('TURBA_BASE', dirname(__FILE__) . '/../..');
  14. require_once HORDE_BASE . '/lib/core.php';
  15. // Do CLI checks and environment setup first.
  16. require_once 'Horde/CLI.php';
  17. // Make sure no one runs this from the web.
  18. if (!Horde_CLI::runningFromCLI()) {
  19. exit("Must be run from the command line\n");
  20. }
  21. // Load the CLI environment - make sure there's no time limit, init
  22. // some variables, etc.
  23. Horde_CLI::init();
  24. // Load Horde's base.php to ensure we have a pushed application on
  25. // the registry stack, and to load the authentication configuration
  26. // without having to load Turba's base.php before we are authenticated.
  27. require_once HORDE_BASE . '/lib/base.php';
  28. // Authenticate as administrator. We need to authenticate *before* we
  29. // include Turba's base.php since Turba_Driver objects will be created
  30. // during that script. The drivers will, of course be cached by using
  31. // the singleton pattern, so the factory method would never be called after
  32. // we are authenticated...which breaks the code dealing with shares in
  33. // Turba_Driver.
  34. if ($conf['auth']['admins']) {
  35. $auth = Auth::singleton($conf['auth']['driver']);
  36. $auth->setAuth($conf['auth']['admins'][0], array());
  37. }
  38. // Load Turba's base.php and get a fresh copy of cfgSources if we are
  39. // authenticated.
  40. require_once TURBA_BASE . '/lib/base.php';
  41. if ($auth->isAuthenticated()) {
  42. require TURBA_BASE . '/config/sources.php';
  43. }
  44. $history = &Horde_History::singleton();
  45. // Make sure we grab any shares.
  46. $shares = $GLOBALS['turba_shares']->listAllShares();
  47. // Run through every contact source.
  48. $sources = Turba::permissionsFilter($cfgSources, 'source', PERMS_EDIT);
  49. // Add the shared sources
  50. foreach ($shares as $share) {
  51. $name = $share->get('sourceType') . ':' . $share->get('uid');
  52. if (!isset($sources[$name])) {
  53. list($srcType, $user) = explode(':', $name, 2);
  54. if ($user != Auth::getAuth()) {
  55. $newSrc = $GLOBALS['cfgSources'][$srcType];
  56. $newSrc['title'] = $share->get('name');
  57. $sources[$name] = $newSrc;
  58. $GLOBALS['cfgSources'][$name] = $newSrc;
  59. }
  60. }
  61. }
  62. foreach ($sources as $key => $curSource) {
  63. $driver = &Turba_Driver::singleton($key);
  64. if (is_a($driver, 'PEAR_Error')) {
  65. var_dump($driver);
  66. exit;
  67. }
  68. echo "Creating default histories for $key ...\n";
  69. // List all contacts.
  70. $results = $driver->search(array());
  71. if (is_a($results, 'PEAR_Error')) {
  72. var_dump($results);
  73. exit;
  74. }
  75. while ($object = $results->next()) {
  76. $id = 'turba:' . ($object->getValue('__owner') ? $object->getValue('__owner') : Auth::getAuth()) . ':' . $object->getValue('__uid');
  77. /* Get the contact's history. */
  78. $log = $history->getHistory($id);
  79. foreach ($log->getData() as $entry)
  80. {
  81. if ($entry['action'] == 'add') {
  82. continue 2;
  83. }
  84. }
  85. // If there isn't an add entry, add one at the current time.
  86. $result = $history->log($id, array('action' => 'add'), true);
  87. if (is_a($result, 'PEAR_Error')) {
  88. var_dump($result);
  89. exit;
  90. }
  91. }
  92. }
  93. echo "\n** Default histories successfully created ***\n";