summaryrefslogtreecommitdiff
path: root/download.inc
blob: 1f27c5b464afbb342ad4df6b8bcefd653d7304d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
// $Id$

function textbook_companion_download_example_file()
{
  $example_file_id = arg(2);
  $root_path = textbook_companion_path();

  $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE id = %d LIMIT 1", $example_file_id);
  $example_file_data = db_fetch_object($example_files_q);
  header('Content-Type: ' . $example_file_data->filemime);
  header('Content-disposition: attachment; filename="' . $example_file_data->filename . '"');
  header('Content-Length: ' . filesize($root_path . $example_file_data->filepath));
  readfile($root_path . $example_file_data->filepath);
}

function textbook_companion_download_dependency_file()
{
  $dependency_file_id = arg(2);
  $root_path = textbook_companion_path();

  $dependency_file_q = db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = %d LIMIT 1", $dependency_file_id);
  $dependency_file_data = db_fetch_object($dependency_file_q);
  header('Content-Type: ' . $dependency_file_data->filemime);
  header('Content-disposition: attachment; filename="' . $dependency_file_data->filename . '"');
  header('Content-Length: ' . filesize($root_path . $dependency_file_data->filepath));
  ob_clean();
  readfile($root_path . $dependency_file_data->filepath);
  exit;
}

function textbook_companion_download_example()
{
  $example_id = arg(2);
  $root_path = textbook_companion_path();

  /* get example data */
  $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE id = %d", $example_id);
  $example_data = db_fetch_object($example_q);
  $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $example_data->chapter_id);
  $chapter_data = db_fetch_object($chapter_q);
  $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_id);
  $example_dependency_files_q = db_query("SELECT * FROM {textbook_companion_example_dependency} WHERE example_id = %d", $example_id);

  $EX_PATH = 'EX' . $example_data->number . '/';

  /* zip filename */
  $zip_filename = $root_path . 'zip-' . time() . '-' . rand(0, 999999) . '.zip';

  /* creating zip archive on the server */
  $zip = new ZipArchive;
  $zip->open($zip_filename, ZipArchive::CREATE);

  while ($example_files_row = db_fetch_object($example_files_q))
  {
      $zip->addFile($root_path . $example_files_row->filepath, $EX_PATH . $example_files_row->filename);
  }
  /* dependency files */
  while ($example_dependency_files_row = db_fetch_object($example_dependency_files_q))
  {
    $dependency_file_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = %d LIMIT 1", $example_dependency_files_row->dependency_id));
    if ($dependency_file_data)
      $zip->addFile($root_path . $dependency_file_data->filepath, $EX_PATH . 'DEPENDENCIES/' . $dependency_file_data->filename);
  }
  $zip_file_count = $zip->numFiles;
  $zip->close();

  if ($zip_file_count > 0)
  {
    /* download zip file */
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename="EX' . $example_data->number . '.zip"');
    header('Content-Length: ' . filesize($zip_filename));
    readfile($zip_filename);
    unlink($zip_filename);
  } else {
    drupal_set_message("There are no files in this examples to download", 'error');
    drupal_goto('textbook_run');
  }
}

function textbook_companion_download_chapter()
{
  $chapter_id = arg(2);
  $root_path = textbook_companion_path();

  /* get example data */
  $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE id = %d", $chapter_id);
  $chapter_data = db_fetch_object($chapter_q);
  $CH_PATH = 'CH' . $chapter_data->number . '/';

  /* zip filename */
  $zip_filename = $root_path . 'zip-' . time() . '-' . rand(0, 999999) . '.zip';

  /* creating zip archive on the server */
  $zip = new ZipArchive;
  $zip->open($zip_filename, ZipArchive::CREATE);

  $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 1", $chapter_id);
  while ($example_row = db_fetch_object($example_q))
  {
    $EX_PATH = 'EX' . $example_row->number . '/';
    $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_row->id);
    $example_dependency_files_q = db_query("SELECT * FROM {textbook_companion_example_dependency} WHERE example_id = %d", $example_row->id);
    while ($example_files_row = db_fetch_object($example_files_q))
    {
      $zip->addFile($root_path . $example_files_row->filepath, $CH_PATH . $EX_PATH . $example_files_row->filename);
    }
    /* dependency files */
    while ($example_dependency_files_row = db_fetch_object($example_dependency_files_q))
    {
      $dependency_file_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = %d LIMIT 1", $example_dependency_files_row->dependency_id));
      if ($dependency_file_data)
        $zip->addFile($root_path . $dependency_file_data->filepath, $CH_PATH . $EX_PATH . 'DEPENDENCIES/' . $dependency_file_data->filename);
    }
  }
  $zip_file_count = $zip->numFiles;
  $zip->close();

  if ($zip_file_count > 0)
  {
    /* download zip file */
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename="CH' . $chapter_data->number . '.zip"');
    header('Content-Length: ' . filesize($zip_filename));
    readfile($zip_filename);
    unlink($zip_filename);
  } else {
    drupal_set_message("There are no examples in this chapter to download", 'error');
    drupal_goto('textbook_run');
  }
}

function textbook_companion_download_book()
{
  $book_id = arg(2);
  $root_path = textbook_companion_path();
  /* get example data */
  $book_q = db_query("SELECT * FROM {textbook_companion_preference} WHERE id = %d", $book_id);
  $book_data = db_fetch_object($book_q);
  $zipname = str_replace(' ','_',($book_data->book));
  $BK_PATH = $zipname . '/';

  /* zip filename */
  $zip_filename = $root_path . 'zip-' . time() . '-' . rand(0, 999999) . '.zip';

  /* creating zip archive on the server */
  $zip = new ZipArchive;
  $zip->open($zip_filename, ZipArchive::CREATE);

  $chapter_q = db_query("SELECT * FROM {textbook_companion_chapter} WHERE preference_id = %d", $book_id);
  while ($chapter_row = db_fetch_object($chapter_q))
  {
    $CH_PATH = 'CH' . $chapter_row->number . '/';
    $example_q = db_query("SELECT * FROM {textbook_companion_example} WHERE chapter_id = %d AND approval_status = 1", $chapter_row->id);
    while ($example_row = db_fetch_object($example_q))
    {
      $EX_PATH = 'EX' . $example_row->number . '/';
      $example_files_q = db_query("SELECT * FROM {textbook_companion_example_files} WHERE example_id = %d", $example_row->id);
      $example_dependency_files_q = db_query("SELECT * FROM {textbook_companion_example_dependency} WHERE example_id = %d", $example_row->id);
      while ($example_files_row = db_fetch_object($example_files_q))
      {
        $zip->addFile($root_path . $example_files_row->filepath, $BK_PATH . $CH_PATH . $EX_PATH . $example_files_row->filename);
      }
      /* dependency files */
      while ($example_dependency_files_row = db_fetch_object($example_dependency_files_q))
      {
        $dependency_file_data = db_fetch_object(db_query("SELECT * FROM {textbook_companion_dependency_files} WHERE id = %d LIMIT 1", $example_dependency_files_row->dependency_id));
        if ($dependency_file_data)
          $zip->addFile($root_path . $dependency_file_data->filepath, $BK_PATH . $CH_PATH . $EX_PATH . 'DEPENDENCIES/' . $dependency_file_data->filename);
      }
      $query = "SELECT * FROM textbook_companion_dependency_files WHERE preference_id = %d";
      $result = db_query($query, $book_id);
      while($row = db_fetch_object($result)) {
          $zip->addFile($root_path . $row->filepath, $BK_PATH . 'DEPENDENCIES/' . $row->filename);
      }
    }
  }
  $zip_file_count = $zip->numFiles;
  $zip->close();

  if ($zip_file_count > 0)
  {
    /* download zip file */
    global $user;
    if($user->uid){
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename="' . str_replace(' ','_',($book_data->book)) . '.zip"');
        header('Content-Length: ' . filesize($zip_filename));
	ob_clean();
        readfile($zip_filename);
        unlink($zip_filename);
    }else{
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename="' . str_replace(' ','_',($book_data->book)) . '.zip"');
        header('Content-Length: ' . filesize($zip_filename));
        header("Content-Transfer-Encoding: binary");
        header('Expires: 0');
        header('Pragma: no-cache');
        ob_end_flush();
        ob_clean();
        flush();
        readfile($zip_filename);
        unlink($zip_filename);
    }
  } else {
    drupal_set_message("There are no examples in this book to download", 'error');
    drupal_goto('textbook_run');
  }
}