-
Notifications
You must be signed in to change notification settings - Fork 32
/
csv.php
151 lines (120 loc) · 4.22 KB
/
csv.php
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
<?php
//
// Excel export module
// - Support ".csv" with Unicode-Characters.
//
// The working encoding concept was found on:
// - http://forum.de.selfhtml.org/archiv/2007/6/t154117/
//
include ("include/dbconnect.php");
// Check if we can produce the Unicode-Excel.
$use_utf_16LE = function_exists('mb_convert_encoding');
function add($value, $first = false) {
global $use_utf_16LE;
// Remove whitespaces, Replace newlines and escape ["] character
$res = trim($value);
$res = str_replace("\r", "", $res);
$res = str_replace("\n", ", ", $res);
$res = str_replace('"', '""', $res);
// Add to result
if($use_utf_16LE) {
$res = ($first ? "" : "\t" ) . '"'.$res.'"';
print mb_convert_encoding( $res, 'UTF-16LE', 'UTF-8');
} else { // Fallback to ISO-8859-1
$res = ($first ? "" : ";" ) . '"'.$res.'"';
print utf8_decode($res);
}
}
$sql = "SELECT $table.*, b_month_lookup.bmonth_num FROM $month_from_where ORDER BY lastname, firstname ASC";
/*
echo $sql;
SELECT addr_addressbook.*, b_month_lookup .bmonth_num, amonth_num amonth_num FROM addr_addressbook LEFT OUTER JOIN addr_month_lookup
b_month_lookup ON addr_addressbook.bmonth = b_month_lookup.bmonth
LEFT OUTER JOIN (SELECT bmonth AS amonth, bmonth_short AS amonth_short, bmonth_num AS amonth_num FROM addr_month_lookup) AS
a_month_lookup ON addr_addressbook.amonth = a_month_lookup.amonth
WHERE addr_addressbook.domain_id = 0 AND addr_addressbook.deprecated is null ORDER BY lastname, firstname ASC
*/
$result = mysql_query($sql);
$resultsnumber = mysql_numrows($result);
// Header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
Header("Content-Type: application/vnd.ms-excel");
Header("Content-disposition: attachement; filename=export-".date("Ymd").($group_name != "" ? "-".$group_name : "").".csv");
Header("Content-Transfer-Encoding: 8bit");
if($use_utf_16LE)
print chr(255).chr(254);
# Name + Geburtstag
add(ucfmsg("LASTNAME"), true);
add(ucfmsg("FIRSTNAME"));
add(ucfmsg("BIRTHDAY"));
# Home contact
add(ucfmsg("ADDRESS"));
if($zip_pattern != "")
{
add(ucfmsg("ZIP"));
add(ucfmsg("CITY"));
}
add(ucfmsg("PHONE_HOME"));
add(ucfmsg("PHONE_MOBILE"));
add(ucfmsg("E_MAIL_HOME"));
# Work contact
add(ucfmsg("PHONE_WORK"));
add(ucfmsg("FAX"));
add(ucfmsg("E_MAIL_OFFICE"));
# 2nd contact
add(ucfmsg("2ND_ADDRESS"));
add(ucfmsg("2ND_PHONE"));
if($use_utf_16LE)
print mb_convert_encoding( "\n", 'UTF-16LE', 'UTF-8');
else
echo "\r\n";
while ($myrow = mysql_fetch_array($result))
{
# Name + Geburtstag
add($myrow["lastname"], true);
add($myrow["firstname"]);
$day = $myrow["bday"];
$year = $myrow["byear"];
if(false) // verbose month
{
// $month = $myrow["bmonth"];
add( ($day > 0 ? "$day. ":"").($month != null ? $month : "")." $year");
} else {
$month = $myrow["bmonth_num"];
add( ($day > 0 ? "$day.":"").($month != null ? "$month." : "")."$year");
}
# Home contact
if($zip_pattern != "")
{
$address = "";
$zip = "";
$city = "";
preg_match( "/(.*)(\b".$zip_pattern."\b)(.*)/m"
, str_replace("\r", "", str_replace("\n", ", ", trim($myrow["address"]))), $matches);
if(count($matches) > 1)
$address = preg_replace("/,$/", "", trim($matches[1]));
if(count($matches) > 2)
$zip = $matches[2];
if(count($matches) > 3)
$city = preg_replace("/^,/", "", trim($matches[3]));
add($address);
add($zip);
add($city);
}
else add($myrow["address"]);
# Privat contact
add($myrow["home"]);
add($myrow["mobile"]);
add($myrow["email"]);
# Work contact
add($myrow["work"]);
add($myrow["fax"]);
add($myrow["email2"]);
# 2nd contact
add($myrow["address2"]);
add($myrow["phone2"]);
if($use_utf_16LE)
print mb_convert_encoding( "\n", 'UTF-16LE', 'UTF-8');
else
echo "\r\n";
}
?>