synchro_exec.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Import web service
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. //
  25. // Known limitations
  26. // - reconciliation is made on the first column
  27. //
  28. // Known issues
  29. // - ALMOST impossible to troubleshoot when an externl key has a wrong value
  30. // - no character escaping in the xml output (yes !?!?!)
  31. // - not outputing xml when a wrong input is given (class, attribute names)
  32. //
  33. require_once('../approot.inc.php');
  34. require_once(APPROOT.'/application/application.inc.php');
  35. require_once(APPROOT.'/application/webpage.class.inc.php');
  36. require_once(APPROOT.'/application/csvpage.class.inc.php');
  37. require_once(APPROOT.'/application/clipage.class.inc.php');
  38. require_once(APPROOT.'/application/startup.inc.php');
  39. function UsageAndExit($oP)
  40. {
  41. global $aPageParams;
  42. $bModeCLI = utils::IsModeCLI();
  43. if ($bModeCLI)
  44. {
  45. $oP->p("USAGE:\n");
  46. $oP->p("php -q synchro_exec.php auth_user=<login> auth_pwd=<password> data_sources=<comma_separated_list_of_data_sources>\n");
  47. }
  48. else
  49. {
  50. $oP->p("The parameter 'data_sources' is mandatory, and must contain a comma separated list of data sources\n");
  51. }
  52. $oP->output();
  53. exit -2;
  54. }
  55. function ReadMandatoryParam($oP, $sParam)
  56. {
  57. $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */);
  58. if (is_null($sValue))
  59. {
  60. $oP->p("ERROR: Missing argument '$sParam'\n");
  61. UsageAndExit($oP);
  62. }
  63. return trim($sValue);
  64. }
  65. /////////////////////////////////
  66. // Main program
  67. if (utils::IsModeCLI())
  68. {
  69. $oP = new CLIPage(Dict::S("TitleSynchroExecution"));
  70. // Next steps:
  71. // specific arguments: 'csvfile'
  72. //
  73. $sAuthUser = ReadMandatoryParam($oP, 'auth_user');
  74. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
  75. $sDataSourcesList = ReadMandatoryParam($oP, 'data_sources');
  76. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  77. {
  78. UserRights::Login($sAuthUser); // Login & set the user's language
  79. }
  80. else
  81. {
  82. $oP->p("Access restricted or wrong credentials ('$sAuthUser')");
  83. $oP->output();
  84. exit -1;
  85. }
  86. }
  87. else
  88. {
  89. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  90. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  91. $oP = new WebPage(Dict::S("TitleSynchroExecution"));
  92. $sDataSourcesList = utils::ReadParam('data_sources', null, true);
  93. $bSimulate = (utils::ReadParam('simulate', '0', true) == '1');
  94. if ($sDataSourcesList == null)
  95. {
  96. UsageAndExit($oP);
  97. }
  98. }
  99. try
  100. {
  101. //////////////////////////////////////////////////
  102. //
  103. // Security
  104. //
  105. if (!UserRights::IsAdministrator())
  106. {
  107. throw new SecurityException(Dict::Format('UI:Error:ActionNotAllowed', $sClass));
  108. }
  109. foreach(explode(',', $sDataSourcesList) as $iSDS)
  110. {
  111. $oSynchroDataSource = MetaModel::GetObject('SynchroDataSource', $iSDS, false);
  112. if ($oSynchroDataSource == null)
  113. {
  114. $oP->p("The data source (id=$iSDS) does not exist. Exiting...");
  115. $oP->output();
  116. exit -3;
  117. }
  118. else
  119. {
  120. $aResults = array();
  121. if ($bSimulate)
  122. {
  123. CMDBSource::Query('START TRANSACTION');
  124. }
  125. $oSynchroDataSource->Synchronize($aResults, null);
  126. foreach ($aResults as $sMessage)
  127. {
  128. $oP->p("results: $sMessage");
  129. }
  130. if ($bSimulate)
  131. {
  132. CMDBSource::Query('ROLLBACK');
  133. }
  134. }
  135. }
  136. }
  137. catch(SecurityException $e)
  138. {
  139. $oP->add($e->getMessage());
  140. }
  141. catch(Exception $e)
  142. {
  143. $oP->add((string)$e);
  144. }
  145. $oP->output();
  146. ?>