synchro_exec.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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;
  54. }
  55. function ReadMandatoryParam($oP, $sParam)
  56. {
  57. global $aPageParams;
  58. assert(isset($aPageParams[$sParam]));
  59. assert($aPageParams[$sParam]['mandatory']);
  60. $sValue = utils::ReadParam($sParam, null, true /* Allow CLI */);
  61. if (is_null($sValue))
  62. {
  63. $oP->p("ERROR: Missing argument '$sParam'\n");
  64. UsageAndExit($oP);
  65. }
  66. return trim($sValue);
  67. }
  68. /////////////////////////////////
  69. // Main program
  70. if (utils::IsModeCLI())
  71. {
  72. $oP = new CLIPage(Dict::S("TitleSynchroExecution"));
  73. // Next steps:
  74. // specific arguments: 'csvfile'
  75. //
  76. $sAuthUser = ReadMandatoryParam($oP, 'auth_user');
  77. $sAuthPwd = ReadMandatoryParam($oP, 'auth_pwd');
  78. $sCsvFile = ReadMandatoryParam($oP, 'data_sources');
  79. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  80. {
  81. UserRights::Login($sAuthUser); // Login & set the user's language
  82. }
  83. else
  84. {
  85. $oP->p("Access restricted or wrong credentials ('$sAuthUser')");
  86. exit;
  87. }
  88. }
  89. else
  90. {
  91. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  92. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  93. $oP = new WebPage(Dict::S("TitleSynchroExecution"));
  94. $sDataSourcesList = utils::ReadParam('data_sources', null);
  95. if ($sDataSourcesList == null)
  96. {
  97. UsageAndExit($oP);
  98. }
  99. }
  100. try
  101. {
  102. //////////////////////////////////////////////////
  103. //
  104. // Security
  105. //
  106. if (!UserRights::IsAdministrator())
  107. {
  108. throw new SecurityException(Dict::Format('UI:Error:ActionNotAllowed', $sClass));
  109. }
  110. foreach(explode(',', $sDataSourcesList) as $iSDS)
  111. {
  112. $oSynchroDataSource = MetaModel::GetObject('SynchroDataSource', $iSDS, true);
  113. $aResults = array();
  114. $oSynchroDataSource->Synchronize($aResults);
  115. }
  116. }
  117. catch(SecurityException $e)
  118. {
  119. $oP->add($e->getMessage());
  120. }
  121. catch(Exception $e)
  122. {
  123. $oP->add((string)$e);
  124. }
  125. $oP->output();
  126. ?>