export.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. $sFields = trim(utils::ReadParam('fields', '', true, 'raw_data')); // CSV field list (allows to specify link set attributes, still not taken into account for XML export)
  71. if (strlen($sExpression) == 0)
  72. {
  73. $sQueryId = trim(utils::ReadParam('query', '', true /* Allow CLI */, 'raw_data'));
  74. if (strlen($sQueryId) > 0)
  75. {
  76. $oSearch = DBObjectSearch::FromOQL('SELECT QueryOQL WHERE id = :query_id', array('query_id' => $sQueryId));
  77. $oQueries = new DBObjectSet($oSearch);
  78. if ($oQueries->Count() > 0)
  79. {
  80. $oQuery = $oQueries->Fetch();
  81. $sExpression = $oQuery->Get('oql');
  82. if (strlen($sFields) == 0)
  83. {
  84. $sFields = trim($oQuery->Get('fields'));
  85. }
  86. }
  87. }
  88. }
  89. $sFormat = strtolower(utils::ReadParam('format', 'html', true /* Allow CLI */));
  90. $aFields = explode(',', $sFields);
  91. // Clean the list of columns (empty it if every string is empty)
  92. foreach($aFields as $index => $sField)
  93. {
  94. $aFields[$index] = trim($sField);
  95. if(strlen($aFields[$index]) == 0)
  96. {
  97. unset($aFields[$index]);
  98. }
  99. }
  100. $oP = null;
  101. if (!empty($sExpression))
  102. {
  103. try
  104. {
  105. $oFilter = DBObjectSearch::FromOQL($sExpression);
  106. // Check and adjust column names
  107. //
  108. foreach($aFields as $index => $sField)
  109. {
  110. if (preg_match('/^(.*)\.(.*)$/', $sField, $aMatches))
  111. {
  112. $sClassAlias = $aMatches[1];
  113. $sAttCode = $aMatches[2];
  114. }
  115. else
  116. {
  117. $sClassAlias = $oFilter->GetClassAlias();
  118. $sAttCode = $sField;
  119. $aFields[$index] = $sClassAlias.'.'.$sAttCode;
  120. }
  121. $sClass = $oFilter->GetClassName($sClassAlias);
  122. if (!MetaModel::IsValidAttCode($sClass, $sAttCode))
  123. {
  124. throw new CoreException("Invalid field specification $sField: $sAttCode is not a valid attribute for $sClass");
  125. }
  126. }
  127. // Read query parameters
  128. //
  129. $aArgs = array();
  130. foreach($oFilter->GetQueryParams() as $sParam => $foo)
  131. {
  132. $value = utils::ReadParam('arg_'.$sParam, null, true, 'raw_data');
  133. if (!is_null($value))
  134. {
  135. $aArgs[$sParam] = $value;
  136. }
  137. }
  138. $oFilter->SetInternalParams($aArgs);
  139. if ($oFilter)
  140. {
  141. $oSet = new CMDBObjectSet($oFilter, array(), $aArgs);
  142. switch($sFormat)
  143. {
  144. case 'html':
  145. $oP = new NiceWebPage("iTop - Export");
  146. // Integration within MS-Excel web queries + HTTPS + IIS:
  147. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  148. // Then the fix is to force the reset of header values Pragma and Cache-control
  149. header("Pragma:", true);
  150. header("Cache-control:", true);
  151. // The HTML output is made for pages located in the /pages/ folder
  152. // since this page is in a different folder, let's adjust the HTML 'base' attribute
  153. // to make the relative hyperlinks in the page work
  154. $sUrl = utils::GetAbsoluteUrlAppRoot();
  155. $oP->set_base($sUrl.'pages/');
  156. if(count($aFields) > 0)
  157. {
  158. $iSearch = array_search('id', $aFields);
  159. if ($iSearch !== false)
  160. {
  161. $bViewLink = true;
  162. unset($aFields[$iSearch]);
  163. }
  164. else
  165. {
  166. $bViewLink = false;
  167. }
  168. $sFields = implode(',', $aFields);
  169. $aExtraParams = array('menu' => false, 'display_limit' => false, 'zlist' => false, 'extra_fields' => $sFields, 'view_link' => $bViewLink);
  170. }
  171. else
  172. {
  173. $aExtraParams = array('menu' => false, 'display_limit' => false, 'zlist' => 'details');
  174. }
  175. $oResultBlock = new DisplayBlock($oFilter, 'list', false, $aExtraParams);
  176. $oResultBlock->Display($oP, 'expresult');
  177. break;
  178. case 'csv':
  179. $oP = new CSVPage("iTop - Export");
  180. $sFields = implode(',', $aFields);
  181. cmdbAbstractObject::DisplaySetAsCSV($oP, $oSet, array('fields' => $sFields));
  182. break;
  183. case 'spreadsheet':
  184. $oP = new WebPage("iTop - Export for spreadsheet");
  185. // Integration within MS-Excel web queries + HTTPS + IIS:
  186. // MS-IIS set these header values with no-cache... while Excel fails to do the job if using HTTPS
  187. // Then the fix is to force the reset of header values Pragma and Cache-control
  188. header("Pragma:", true);
  189. header("Cache-control:", true);
  190. $sFields = implode(',', $aFields);
  191. cmdbAbstractObject::DisplaySetAsHTMLSpreadsheet($oP, $oSet, array('fields' => $sFields));
  192. break;
  193. case 'xml':
  194. $oP = new XMLPage("iTop - Export", true /* passthrough */);
  195. cmdbAbstractObject::DisplaySetAsXML($oP, $oSet);
  196. break;
  197. default:
  198. $oP = new WebPage("iTop - Export");
  199. $oP->add("Unsupported format '$sFormat'. Possible values are: html, csv, spreadsheet or xml.");
  200. }
  201. }
  202. }
  203. catch(Exception $e)
  204. {
  205. $oP = new WebPage("iTop - Export");
  206. $oP->p("Error the query can not be executed.");
  207. if ($e instanceof CoreException)
  208. {
  209. $oP->p($e->GetHtmlDesc());
  210. }
  211. else
  212. {
  213. $oP->p($e->getMessage());
  214. }
  215. }
  216. }
  217. if (!$oP)
  218. {
  219. // Display a short message about how to use this page
  220. $bModeCLI = utils::IsModeCLI();
  221. if ($bModeCLI)
  222. {
  223. $oP = new CLIPage("iTop - Export");
  224. }
  225. else
  226. {
  227. $oP = new WebPage("iTop - Export");
  228. }
  229. $oP->p("General purpose export page.");
  230. $oP->p("Parameters:");
  231. $oP->p(" * expression: an OQL expression (URL encoded if needed)");
  232. $oP->p(" * query: (alternative to 'expression') the id of an entry from the query phrasebook");
  233. $oP->p(" * arg_xxx: (needed if the query has parameters) the value of the parameter 'xxx'");
  234. $oP->p(" * format: (optional, default is html) the desired output format. Can be one of 'html', 'spreadsheet', 'csv' or 'xml'");
  235. $oP->p(" * fields: (optional, no effect on XML format) list of fields (attribute codes, or alias.attcode) separated by a coma");
  236. }
  237. $oP->output();
  238. ?>