PageRenderTime 68ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/tiki-export_tracker.php

https://gitlab.com/ElvisAns/tiki
PHP | 301 lines | 265 code | 24 blank | 12 comment | 95 complexity | d623b3a0d531a1b665bd3651e6ff777f MD5 | raw file
  1. <?php
  2. /**
  3. * @package tikiwiki
  4. */
  5. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  6. //
  7. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  8. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  9. // $Id$
  10. // still used for export from trackerfilter (only - Tiki 9)
  11. require_once('tiki-setup.php');
  12. $access->check_feature('feature_trackers');
  13. if (! isset($_REQUEST['trackerId'])) {
  14. $smarty->assign('msg', tra('No tracker indicated'));
  15. $smarty->display('error.tpl');
  16. die;
  17. }
  18. $trklib = TikiLib::lib('trk');
  19. @ini_set('max_execution_time', 0);
  20. $tracker_info = $trklib->get_tracker($_REQUEST['trackerId']);
  21. if (empty($tracker_info)) {
  22. $smarty->assign('msg', tra('No tracker indicated'));
  23. $smarty->display('error.tpl');
  24. die;
  25. }
  26. if ($t = $trklib->get_tracker_options($_REQUEST['trackerId'])) {
  27. $tracker_info = array_merge($tracker_info, $t);
  28. }
  29. $tikilib->get_perm_object($_REQUEST['trackerId'], 'tracker', $tracker_info);
  30. $access->check_permission('tiki_p_export_tracker', tra('Export Tracker'), 'tracker', $_REQUEST['trackerId']);
  31. $smarty->assign_by_ref('trackerId', $_REQUEST['trackerId']);
  32. $smarty->assign_by_ref('tracker_info', $tracker_info);
  33. $filters = [];
  34. if (! empty($_REQUEST['listfields'])) {
  35. if (is_string($_REQUEST['listfields'])) {
  36. $filters['fieldId'] = preg_split('/[,:]/', $_REQUEST['listfields']);
  37. } elseif (is_array($_REQUEST['listfields'])) {
  38. $filters['fieldId'] = $_REQUEST['listfields'];
  39. }
  40. } elseif (isset($_REQUEST['which']) && $_REQUEST['which'] == 'ls') {
  41. $filters['or'] = ['isSearchable' => 'y', 'isTblVisible' => 'y'];
  42. } elseif (isset($_REQUEST['which']) && $_REQUEST['which'] == 'list') {
  43. $filters['isTblVisible'] = 'y';
  44. }
  45. if ($tiki_p_admin_trackers != 'y') {
  46. $filters['isHidden'] = ['n', 'c'];
  47. }
  48. if ($tiki_p_tracker_view_ratings != 'y') {
  49. $filters['not'] = ['type' => 's'];
  50. }
  51. $filters['not'] = ['type' => 'h'];
  52. $fields = $trklib->list_tracker_fields($_REQUEST['trackerId'], 0, -1, 'position_asc', '', true, $filters);
  53. $listfields = [];
  54. foreach ($fields['data'] as $field) {
  55. $listfields[$field['fieldId']] = $field;
  56. }
  57. // If we have a list of displayed fields, we show only those, so the export matches the online display
  58. if (isset($_REQUEST["displayedFields"])) {
  59. $displayedFields = explode(':', $_REQUEST["displayedFields"]);
  60. $listfields = array_intersect_key($listfields, array_flip($displayedFields));
  61. }
  62. if (! isset($_REQUEST['which'])) {
  63. $_REQUEST['which'] = 'all';
  64. }
  65. if (! isset($_REQUEST['status'])) {
  66. $_REQUEST['status'] = '';
  67. }
  68. if (! isset($_REQUEST['initial'])) {
  69. $_REQUEST['initial'] = '';
  70. }
  71. $filterFields = [];
  72. $values = [];
  73. $exactValues = [];
  74. foreach ($_REQUEST as $key => $val) {
  75. if (substr($key, 0, 2) == 'f_' && ! empty($val) && (! is_array($val) || ! empty($val[0]))) {
  76. $fieldId = substr($key, 2);
  77. $filterFields[] = $fieldId;
  78. if (isset($_REQUEST["x_$fieldId"]) && $_REQUEST["x_$fieldId"] == 't') {
  79. $exactValues[] = '';
  80. if (is_array($val)) {
  81. $values[] = $val;
  82. } else {
  83. $values[] = urldecode($val);
  84. }
  85. } else {
  86. if (is_array($val)) {
  87. $exactValues[] = $val;
  88. } else {
  89. $exactValues[] = urldecode($val);
  90. }
  91. $values[] = '';
  92. }
  93. }
  94. }
  95. $smarty->assign_by_ref('listfields', $listfields);
  96. if (isset($_REQUEST['showStatus'])) {
  97. $showStatus = $_REQUEST['showStatus'] == 'on' ? 'y' : 'n';
  98. } else {
  99. $showStatus = 'n';
  100. }
  101. $smarty->assign_by_ref('showStatus', $showStatus);
  102. if (isset($_REQUEST['showItemId'])) {
  103. $showItemId = $_REQUEST['showItemId'] == 'on' ? 'y' : 'n';
  104. } else {
  105. $showItemId = 'n';
  106. }
  107. $smarty->assign_by_ref('showItemId', $showItemId);
  108. if (isset($_REQUEST['showCreated'])) {
  109. $showCreated = $_REQUEST['showCreated'] == 'on' ? 'y' : 'n';
  110. } else {
  111. $showCreated = 'n';
  112. }
  113. $smarty->assign_by_ref('showCreated', $showCreated);
  114. if (isset($_REQUEST['showLastModif'])) {
  115. $showLastModif = $_REQUEST['showLastModif'] == 'on' ? 'y' : 'n';
  116. } else {
  117. $showLastModif = 'n';
  118. }
  119. $smarty->assign_by_ref('showLastModif', $showLastModif);
  120. if (isset($_REQUEST['showLastModifBy'])) {
  121. $showLastModifBy = $_REQUEST['showLastModifBy'] == 'on' ? 'y' : 'n';
  122. } else {
  123. $showLastModifBy = 'n';
  124. }
  125. $smarty->assign_by_ref('showLastModifBy', $showLastModifBy);
  126. if (isset($_REQUEST['parse'])) {
  127. $parse = $_REQUEST['parse'] == 'on' ? 'y' : 'n';
  128. } else {
  129. $parse = 'n';
  130. }
  131. $smarty->assign_by_ref('parse', $parse);
  132. if (empty($_REQUEST['encoding'])) {
  133. $_REQUEST['encoding'] = 'ISO-8859-1';
  134. }
  135. if (empty($_REQUEST['separator'])) {
  136. $_REQUEST['separator'] = ',';
  137. }
  138. $smarty->assign_by_ref('separator', $_REQUEST['separator']);
  139. if (empty($_REQUEST['delimitorL'])) {
  140. $_REQUEST['delimitorL'] = '"';
  141. }
  142. $smarty->assign_by_ref('delimitorL', $_REQUEST['delimitorL']);
  143. if (empty($_REQUEST['delimitorR'])) {
  144. $_REQUEST['delimitorR'] = '"';
  145. }
  146. $smarty->assign_by_ref('delimitorR', $_REQUEST['delimitorR']);
  147. if (empty($_REQUEST['CR'])) {
  148. $_REQUEST['CR'] = '%%%';
  149. }
  150. $smarty->assign_by_ref('CR', $_REQUEST['CR']);
  151. $fp = null;
  152. if (! empty($_REQUEST['debug'])) {
  153. $fp = fopen($prefs['tmpDir'] . '/' . tra('tracker') . "_" . $_REQUEST['trackerId'] . ".csv", 'w');
  154. echo 'ouput:' . $prefs['tmpDir'] . '/' . tra('tracker') . "_" . $_REQUEST['trackerId'] . ".csv";
  155. } else {
  156. // Compression of the stream may corrupt files on windows
  157. if ($prefs['feature_obzip'] != 'y') {
  158. ob_end_clean();
  159. }
  160. ini_set('zlib.output_compression', 'Off');
  161. $extension = empty($_REQUEST['zip']) ? '.csv' : '.zip';
  162. if (! empty($_REQUEST['file'])) {
  163. if (preg_match('/' . $extension . '$/', $_REQUEST['file'])) {
  164. $file = $_REQUEST['file'];
  165. } else {
  166. $file = $_REQUEST['file'] . $extension;
  167. }
  168. } else {
  169. $file = tra('tracker') . '_' . $_REQUEST['trackerId'] . $extension;
  170. }
  171. if (! empty($_REQUEST['zip'])) {
  172. $tmpCsv = tempnam($prefs['tmpDir'], 'tracker_' . $_REQUEST['trackerId']) . '.csv';
  173. /*debug*/$tmpCsv = $prefs['tmpDir'] . '/' . 'tracker_' . $_REQUEST['trackerId'] . '.csv';
  174. if (! ($fp = fopen($tmpCsv, 'w'))) {
  175. $smarty->assign('msg', tra('The file cannot be opened') . ' ' . $tmpCsv);
  176. $smarty->display('error.tpl');
  177. die;
  178. }
  179. if (! ($archive = new ZipArchive())) {
  180. $smarty->assign('msg', tra('Problem zip initialisation'));
  181. $smarty->display('error.tpl');
  182. die;
  183. }
  184. $tmpZip = $prefs['tmpDir'] . '/' . $file;
  185. if (! ($archive->open($tmpZip, ZIPARCHIVE::OVERWRITE))) {
  186. $smarty->assign('msg', tra('The file cannot be opened') . ' ' . $prefs['tmpDir'] . '/' . $file);
  187. $smarty->display('error.tpl');
  188. die;
  189. }
  190. header('Content-Type: application/zip');
  191. header('Content-Transfer-Encoding: binary');
  192. } else {
  193. header("Content-type: text/comma-separated-values; charset:" . $_REQUEST['encoding']);
  194. }
  195. header("Content-Disposition: attachment; filename=$file");
  196. header("Expires: 0");
  197. header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
  198. header("Pragma: public");
  199. }
  200. $offset = 0;
  201. $maxRecords = 100;
  202. if ($tracker_info['defaultOrderKey'] == -1) {
  203. $sort_mode = 'lastModif';
  204. } elseif ($tracker_info['defaultOrderKey'] == -2) {
  205. $sort_mode = 'created';
  206. } elseif ($tracker_info['defaultOrderKey'] == -3) {
  207. $sort_mode = 'itemId';
  208. } elseif (isset($tracker_info['defaultOrderKey'])) {
  209. $sort_mode = 'f_' . $tracker_info['defaultOrderKey'];
  210. } else {
  211. $sort_mode = 'itemId';
  212. }
  213. if (isset($tracker_info['defaultOrderDir'])) {
  214. $sort_mode .= "_" . $tracker_info['defaultOrderDir'];
  215. } else {
  216. $sort_mode .= "_asc";
  217. }
  218. $heading = 'y';
  219. $smarty->assign_by_ref('heading', $heading);
  220. if (empty($_REQUEST['itemId'])) {
  221. while (($items = $trklib->list_items($_REQUEST['trackerId'], $offset, $maxRecords, $sort_mode, $listfields, $filterFields, $values, $_REQUEST['status'], $_REQUEST['initial'], $exactValues)) && ! empty($items['data'])) {
  222. // still need to filter the fields that are view only by the admin and the item creator
  223. if ($tracker_info['useRatings'] == 'y') {
  224. foreach ($items['data'] as $f => $v) {
  225. $items['data'][$f]['my_rate'] = $tikilib->get_user_vote("tracker." . $_REQUEST['trackerId'] . '.' . $items['data'][$f]['itemId'], $user);
  226. }
  227. }
  228. $smarty->assign_by_ref('items', $items["data"]);
  229. $data = $smarty->fetch('tiki-export_tracker_item.tpl');
  230. $data = preg_replace("/^\n/", "", $data);
  231. if (empty($_REQUEST['encoding']) || $_REQUEST['encoding'] == 'ISO-8859-1') {
  232. $data = utf8_decode($data);
  233. }
  234. $offset += $maxRecords;
  235. $heading = 'n';
  236. if (! empty($fp)) {
  237. fwrite($fp, $data);
  238. } else {
  239. echo $data;
  240. }
  241. if ($tracker_info['useAttachments'] == 'y' && ! empty($_REQUEST['zip'])) {
  242. foreach ($items['data'] as $v) {
  243. if (! $trklib->export_attachment($v['itemId'], $archive)) {
  244. $smarty->assign('msg', tra('Problem zip'));
  245. $smarty->display('error.tpl');
  246. die;
  247. }
  248. }
  249. }
  250. }
  251. } else {
  252. $items = [];
  253. $items[] = $trklib->get_tracker_item($_REQUEST['itemId']);
  254. $items[0]['field_values'] = $trklib->get_item_fields($_REQUEST['trackerId'], $_REQUEST['itemId'], $listfields);
  255. $smarty->assign_by_ref('items', $items);
  256. $data = $smarty->fetch('tiki-export_tracker_item.tpl');
  257. $data = preg_replace("/^\n/", "", $data);
  258. if (empty($_REQUEST['encoding']) || $_REQUEST['encoding'] == 'ISO-8859-1') {
  259. $data = utf8_decode($data);
  260. }
  261. if (! empty($fp)) {
  262. fwrite($fp, $data);
  263. } else {
  264. echo $data;
  265. }
  266. }
  267. if (! empty($fp)) {
  268. fclose($fp);
  269. }
  270. if (! empty($_REQUEST['zip'])) {
  271. $archive->addFile($tmpCsv, str_replace('.zip', '.csv', $file));
  272. $archive->close();
  273. readfile($tmpZip);
  274. unlink($tmpZip);
  275. unlink($tmpCsv);
  276. }
  277. die;