move code up one directory
[atutor.git] / inbox / export.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14 $_user_location = 'public';
15
16 define('AT_INCLUDE_PATH', '../include/');
17 require (AT_INCLUDE_PATH.'vitals.inc.php');
18
19 if (!$_SESSION['valid_user']) {
20         require(AT_INCLUDE_PATH.'header.inc.php');
21         $msg->printInfos('INVALID_USER');
22         require(AT_INCLUDE_PATH.'footer.inc.php');
23         exit;
24 } else if (isset($_POST['export'], $_POST['messages'])) {
25         $inbox_messages = $sent_messages = '';
26
27         $my_display_name = get_display_name($_SESSION['member_id']);
28
29         // inbox messages
30         if ($_POST['messages'] == 1 || $_POST['messages'] == 2) {
31                 $sql = "SELECT * FROM ".TABLE_PREFIX."messages WHERE to_member_id=$_SESSION[member_id] ORDER BY date_sent";
32                 $result = mysql_query($sql, $db);
33                 while ($row = mysql_fetch_assoc($result)) {
34                         $msg  = _AT('from')   .': ' . get_display_name($row['from_member_id']) . "\r\n";
35                         $msg .= _AT('to')     .': ' . $my_display_name . "\r\n";
36                         $msg .= _AT('subject').': ' . $row['subject'] . "\r\n";
37                         $msg .= _AT('date')   .': ' . $row['date_sent'] . "\r\n";
38                         $msg .= _AT('body')   .': ' . $row['body'] . "\r\n";
39                         $msg .= "\r\n=============================================\r\n\r\n";
40
41                         $inbox_messages .= $msg;
42                 }
43         }
44
45         // sent messages
46         if ($_POST['messages'] == 1 || $_POST['messages'] == 3) {
47                 $sql = "SELECT * FROM ".TABLE_PREFIX."messages_sent WHERE from_member_id=$_SESSION[member_id] ORDER BY date_sent";
48                 $result = mysql_query($sql, $db);
49                 while ($row = mysql_fetch_assoc($result)) {
50                         $msg  = _AT('from')   .': ' . $my_display_name  . "\r\n";
51                         $msg .= _AT('to')     .': ' . get_display_name($row['from_member_id']). "\r\n";
52                         $msg .= _AT('subject').': ' . $row['subject'] . "\r\n";
53                         $msg .= _AT('date')   .': ' . $row['date_sent'] . "\r\n";
54                         $msg .= _AT('body')   .': ' . $row['body'] . "\r\n";
55                         $msg .= "\r\n=============================================\r\n\r\n";
56
57                         $sent_messages .= $msg;
58                 }
59         }
60
61         if ($inbox_messages && $sent_messages) {
62                 // add the two to a zip file
63                 require(AT_INCLUDE_PATH.'classes/zipfile.class.php'); // for zipfile
64                 $zipfile = new zipfile();
65                 $zipfile->add_file($inbox_messages, _AT('inbox').'.txt');
66                 $zipfile->add_file($sent_messages, _AT('sent_messages').'.txt');
67                 $zipfile->close();
68                 $zipfile->send_file(_AT('inbox').'-'.date('Ymd'));
69                 exit;
70         } else if ($inbox_messages) {
71                 header('Content-Type: text/plain');
72                 header('Content-Disposition: attachment; filename="'._AT('inbox').'-'.date('Ymd').'.txt"');
73                 header('Expires: 0');
74                 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
75                 header('Pragma: public');
76                 header('Content-Length: '.strlen($inbox_messages));
77
78                 echo $inbox_messages;
79                 exit;
80         } else if ($sent_messages) {
81                 header('Content-Type: text/plain');
82                 header('Content-Disposition: attachment; filename="'._AT('sent_messages').'-'.date('Ymd').'.txt"');
83                 header('Expires: 0');
84                 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
85                 header('Pragma: public');
86                 header('Content-Length: '.strlen($sent_messages));
87
88                 echo $sent_messages;
89                 exit;
90         } // else. nothing to export
91
92 }
93
94 require(AT_INCLUDE_PATH.'header.inc.php');
95 ?>
96
97 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"/>
98 <div class="input-form">
99         <div class="row">
100                 <?php echo _AT('export'); ?><br />
101                 <input type="radio" name="messages" value="1" id="m1" checked="checked"><label for="m1"><?php echo _AT('all'); ?></label><br />
102                 <input type="radio" name="messages" value="2" id="m2"><label for="m2"><?php echo _AT('inbox'); ?></label><br />
103                 <input type="radio" name="messages" value="3" id="m3"><label for="m3"><?php echo _AT('sent_messages'); ?></label><br />
104         </div>
105
106         <div class="row buttons">
107                 <input type="submit" name="export" value="<?php echo _AT('export'); ?>"/>
108         </div>
109 </div>
110 </form>
111 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>