export.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * Export data specified by an OQL
  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. if (!defined('__DIR__')) define('__DIR__', dirname(__FILE__));
  25. require_once(__DIR__.'/../approot.inc.php');
  26. require_once(APPROOT.'/application/application.inc.php');
  27. require_once(APPROOT.'/application/nicewebpage.class.inc.php');
  28. require_once(APPROOT.'/application/csvpage.class.inc.php');
  29. require_once(APPROOT.'/application/xmlpage.class.inc.php');
  30. require_once(APPROOT.'/application/clipage.class.inc.php');
  31. require_once(APPROOT.'/application/startup.inc.php');
  32. try
  33. {
  34. // Do this before loging, in order to allow setting user credentials from within the file
  35. utils::UseParamFile();
  36. }
  37. catch(Exception $e)
  38. {
  39. echo "Error: ".$e->GetMessage()."<br/>\n";
  40. exit -2;
  41. }
  42. if (utils::IsModeCLI())
  43. {
  44. $sAuthUser = utils::ReadParam('auth_user', null, true /* Allow CLI */, 'raw_data');
  45. $sAuthPwd = utils::ReadParam('auth_pwd', null, true /* Allow CLI */, 'raw_data');
  46. if (UserRights::CheckCredentials($sAuthUser, $sAuthPwd))
  47. {
  48. UserRights::Login($sAuthUser); // Login & set the user's language
  49. }
  50. else
  51. {
  52. $oP = new CLIPage("iTop - Export");
  53. $oP->p("Access restricted or wrong credentials ('$sAuthUser')");
  54. $oP->output();
  55. exit -1;
  56. }
  57. }
  58. else
  59. {
  60. require_once(APPROOT.'/application/loginwebpage.class.inc.php');
  61. LoginWebPage::DoLogin(); // Check user rights and prompt if needed
  62. }
  63. ApplicationContext::SetUrlMakerClass('iTopStandardURLMaker');
  64. $sOperation = utils::ReadParam('operation', 'menu');
  65. $oAppContext = new ApplicationContext();
  66. $iActiveNodeId = utils::ReadParam('menu', -1);
  67. $currentOrganization = utils::ReadParam('org_id', '');
  68. // Main program
  69. $sExpression = utils::ReadParam('expression', '', true /* Allow CLI */, 'raw_data');
  70. if (strlen($sExpression) == 0)
  71. {
  72. $sQueryId = trim(utils::ReadParam('query', '', true /* Allow CLI */, 'raw_data'));
  73. if (strlen($sQueryId) > 0)
  74. {
  75. $oSearch = DBObjectSearch::FromOQL('SELECT QueryOQL WHERE id = :query_id', array('query_id' => $sQueryId));
  76. $oQueries = new DBObjectSet($oSearch);
  77. if ($oQueries->Count() > 0)
  78. {
  79. $oQuery = $oQueries->Fetch();
  80. $sExpression = $oQuery->Get('oql');
  81. }
  82. }
  83. }
  84. $sFormat = strtolower(utils::ReadParam('format', 'html', true /* Allow CLI */));
  85. $sFields = utils::ReadParam('fields', '', true, 'raw_data'); // CSV field list (allows to specify link set attributes, still not taken into account for XML export)
  86. $aFields = explode(',', $sFields);
  87. // Clean the list of columns (empty it if every string is empty)
  88. foreach($aFields as $index => $sField)
  89. {
  90. $aFields[$index] = trim($sField);
  91. if(strlen($aFields[$index]) == 0)
  92. {
  93. unset($aFields[$index]);
  94. }
  95. }
  96. $oP = null;
  97. if (!empty($sExpression))
  98. {
  99. try
  100. {
  101. $oFilter = DBObjectSearch::FromOQL($sExpression);
  102. $aArgs = array();
  103. foreach($oFilter->GetQueryParams() as $sParam => $foo)
  104. {
  105. $value = utils::ReadParam('arg_'.$sParam, null, true, 'raw_data');
  106. if (!is_null($value))
  107. {
  108. $aArgs[$sParam] = $value;
  109. }
  110. }
  111. $oFilter->SetInternalParams($aArgs);
  112. if ($oFilter)
  113. {
  114. $oSet = new CMDBObjectSet($oFilter, array(), $aArgs);
  115. switch($sFormat)
  116. {
  117. case 'html':
  118. $oP = new NiceWebPage("iTop - Export");
  119. // Integration within MS-Excel web queries + HTTPS + IIS:
  120. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  121. // Then the fix is to force the reset of header values Pragma and Cache-control
  122. header("Pragma:", true);
  123. header("Cache-control:", true);
  124. // The HTML output is made for pages located in the /pages/ folder
  125. // since this page is in a different folder, let's adjust the HTML 'base' attribute
  126. // to make the relative hyperlinks in the page work
  127. $sUrl = utils::GetAbsoluteUrlAppRoot();
  128. $oP->set_base($sUrl.'pages/');
  129. if(count($aFields) > 0)
  130. {
  131. $iSearch = array_search('id', $aFields);
  132. if ($iSearch !== false)
  133. {
  134. $bViewLink = true;
  135. unset($aFields[$iSearch]);
  136. }
  137. else
  138. {
  139. $bViewLink = false;
  140. }
  141. $sFields = implode(',', $aFields);
  142. $aExtraParams = array('menu' => false, 'display_limit' => false, 'zlist' => false, 'extra_fields' => $sFields, 'view_link' => $bViewLink);
  143. }
  144. else
  145. {
  146. $aExtraParams = array('menu' => false, 'display_limit' => false, 'zlist' => 'details');
  147. }
  148. $oResultBlock = new DisplayBlock($oFilter, 'list', false, $aExtraParams);
  149. $oResultBlock->Display($oP, 'expresult');
  150. break;
  151. case 'csv':
  152. $oP = new CSVPage("iTop - Export");
  153. $sFields = implode(',', $aFields);
  154. cmdbAbstractObject::DisplaySetAsCSV($oP, $oSet, array('fields' => $sFields));
  155. break;
  156. case 'spreadsheet':
  157. $oP = new WebPage("iTop - Export for spreadsheet");
  158. // Integration within MS-Excel web queries + HTTPS + IIS:
  159. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  160. // Then the fix is to force the reset of header values Pragma and Cache-control
  161. header("Pragma:", true);
  162. header("Cache-control:", true);
  163. $sFields = implode(',', $aFields);
  164. cmdbAbstractObject::DisplaySetAsHTMLSpreadsheet($oP, $oSet, array('fields' => $sFields));
  165. break;
  166. case 'xml':
  167. $oP = new XMLPage("iTop - Export", true /* passthrough */);
  168. cmdbAbstractObject::DisplaySetAsXML($oP, $oSet);
  169. break;
  170. default:
  171. $oP = new WebPage("iTop - Export");
  172. $oP->add("Unsupported format '$sFormat'. Possible values are: html, csv or xml.");
  173. }
  174. }
  175. }
  176. catch(Exception $e)
  177. {
  178. $oP = new WebPage("iTop - Export");
  179. $oP->p("Error the query can not be executed.");
  180. $oP->p($e->GetHtmlDesc());
  181. }
  182. }
  183. if (!$oP)
  184. {
  185. // Display a short message about how to use this page
  186. $bModeCLI = utils::IsModeCLI();
  187. if ($bModeCLI)
  188. {
  189. $oP = new CLIPage("iTop - Export");
  190. }
  191. else
  192. {
  193. $oP = new WebPage("iTop - Export");
  194. }
  195. $oP->p("General purpose export page.");
  196. $oP->p("Parameters:");
  197. $oP->p(" * expression: an OQL expression (URL encoded if needed)");
  198. $oP->p(" * query: (alternative to 'expression') the id of an entry from the query phrasebook");
  199. $oP->p(" * arg_xxx: (needed if the query has parameters) the value of the parameter 'xxx'");
  200. $oP->p(" * format: (optional, default is html) the desired output format. Can be one of 'html', 'spreadsheet', 'csv' or 'xml'");
  201. $oP->p(" * fields: (optional, no effect on XML format) list of fields (attribute codes) separated by a coma");
  202. }
  203. $oP->output();
  204. ?>