PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/phpESP/admin/include/where/download.inc

#
PHP | 114 lines | 84 code | 12 blank | 18 comment | 10 complexity | 1390d583da4dc4f6a842380df00c016d MD5 | raw file
  1. <?php
  2. /* $Id: download.inc 901 2006-03-27 23:10:41Z greggmc $ */
  3. /* vim: set tabstop=4 shiftwidth=4 expandtab: */
  4. // Download patch submitted by Matthew Gregg
  5. // <greggmc@musc.edu>
  6. // Modified by James Flemer
  7. // <jflemer@alum.rpi.edu>
  8. $sid = -1;
  9. if (!empty($_GET['sid']))
  10. $sid = intval($_GET['sid']);
  11. else if(!empty($_POST['sid']))
  12. $sid = intval($_POST['sid']);
  13. $sql = "SELECT name FROM ".$GLOBALS['ESPCONFIG']['survey_table']." WHERE id = $sid";
  14. $result = execute_sql($sql);
  15. if (record_count($result) < 1) {
  16. db_close($result);
  17. echo mkerror(_('Invalid survey ID.'));
  18. return;
  19. }
  20. $name = $result->fields[0];
  21. db_close($result);
  22. /* check ACLs for permissions */
  23. $srealm = auth_get_survey_realm($sid);
  24. if (isset($_GET['test'])) {
  25. /* check ACL to see if user is allowed to test
  26. * _this_ survey */
  27. if($_SESSION['acl']['superuser'] != 'Y' &&
  28. !auth_is_owner($sid, $_SESSION['acl']['username']) &&
  29. !in_array($srealm, array_intersect(
  30. $_SESSION['acl']['pdesign'],
  31. $_SESSION['acl']['pall'])) &&
  32. !auth_no_access(_('to access this survey'))) {
  33. return;
  34. }
  35. } else {
  36. /* check ACL to see if user is allowed to export
  37. * _this_ survey */
  38. if($_SESSION['acl']['superuser'] != 'Y' &&
  39. !auth_is_owner($sid, $_SESSION['acl']['username']) &&
  40. !in_array($srealm, array_intersect(
  41. $_SESSION['acl']['pdata'],
  42. $_SESSION['acl']['pall'])) &&
  43. !auth_no_access(_('to access this survey'))) {
  44. return;
  45. }
  46. }
  47. function printCommonHeaders() {
  48. header("Pragma: public");
  49. header("Expires: 0");
  50. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  51. header("Cache-Control: private",false);
  52. header("Content-Transfer-Encoding: ascii");
  53. }
  54. $type = isset($_GET['type']) ? $_GET['type'] : 'csv_full_header';
  55. // default to csv_full_header
  56. switch($type) {
  57. case 'spss':
  58. //NOT FULLY IMPLEMENT YET
  59. header("Content-Transfer-Encoding: ascii");
  60. printCommonHeaders();
  61. header("Content-Type: text/comma-separated-values");
  62. header("Content-Disposition: attachment; filename={$name}.csv");
  63. header("Content-Type: application/txt");
  64. $output = survey_generate_results($type, $sid);
  65. //Do something with output
  66. foreach ($output as $row ) {
  67. echo(implode(',', $row) . "\r\n");
  68. }
  69. break;
  70. case 'xml':
  71. //NOT FULLY IMPLEMENT YET
  72. header("Content-Transfer-Encoding: ascii");
  73. printCommonHeaders();
  74. header("Content-Disposition: attachment; filename=$name.xml");
  75. header("Content-Type: text/xml");
  76. $output = survey_generate_results($type, $sid);
  77. //Do something with output
  78. break;
  79. case 'tab':
  80. // TAB
  81. header("Content-Transfer-Encoding: ascii");
  82. printCommonHeaders();
  83. header("Content-Disposition: attachment; filename=$name.txt");
  84. header("Content-Type: text/tab-separated-values");
  85. $output = survey_generate_results($type, $sid);
  86. foreach ($output as $row ) {
  87. echo(implode(chr(9), $row) . "\r\n");
  88. }
  89. break;
  90. default:
  91. // CSV, csv_full_header old method, csv_short_header new method
  92. header("Content-Transfer-Encoding: ascii");
  93. printCommonHeaders();
  94. header("Content-Disposition: attachment; filename=$name.csv");
  95. header("Content-Type: text/comma-separated-values");
  96. $output = survey_generate_results($type, $sid);
  97. foreach ($output as $row ) {
  98. echo(implode(',', $row) . "\r\n");
  99. }
  100. }
  101. return;
  102. ?>