forked from anuko/timetracker
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathttOrgExportHelper.class.php
More file actions
88 lines (71 loc) · 2.45 KB
/
ttOrgExportHelper.class.php
File metadata and controls
88 lines (71 loc) · 2.45 KB
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
<?php
/* Copyright (c) Anuko International Ltd. https://www.anuko.com
License: See license.txt */
import('ttTeamHelper');
import('ttTimeHelper');
import('ttGroupExportHelper');
// ttOrgExportHelper handles export of organizations consisting of multiple groups
// into XML file for import (migrating) to another server.
class ttOrgExportHelper {
var $fileName = null; // Name of file with data.
// createDataFile creates a file with all data for the entire organization.
function createDataFile($compress = false) {
global $user;
// Create a temporary file.
$tmp_file = tempnam(APP_TMP_DIR, 'export_');
// Open the file for writing.
$file = fopen($tmp_file, 'wb');
if (!$file) return false;
// Write XML to the file.
fwrite($file, "<?xml version=\"1.0\"?>\n");
$org_part = "<org schema=\"".$this->getVersion()."\">\n";
fwrite($file, $org_part);
// Use ttGroupExportHelper to export all groups.
$groupExportHelper = new ttGroupExportHelper($user->group_id, $file, ' '); // 2 spaces indentation for home group.
$groupExportHelper->writeData();
fwrite($file, "</org>\n");
fclose($file);
if ($compress) {
$this->fileName = tempnam(APP_TMP_DIR, 'export_');
$this->compress($tmp_file, $this->fileName);
unlink($tmp_file);
} else
$this->fileName = $tmp_file;
return true;
}
// getFileName - returns file name.
function getFileName() {
return $this->fileName;
}
// compress - compresses the content of the $in file into $out file.
function compress($in, $out) {
// Initial checks of file names and permissions.
if (!file_exists($in) || !is_readable ($in))
return false;
if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
return false;
$in_file = fopen($in, 'rb');
if (function_exists('bzopen')) {
if (!$out_file = bzopen($out, 'w'))
return false;
while (!feof ($in_file)) {
$buffer = fread($in_file, 4096);
bzwrite($out_file, $buffer, 4096);
}
bzclose($out_file);
}
fclose ($in_file);
return true;
}
private function getVersion() {
$mdb2 = getConnection();
$sql = "select param_value from tt_site_config where param_name = 'version_db'";
$res = $mdb2->query($sql);
if (!is_a($res, 'PEAR_Error')) {
$val = $res->fetchRow();
return $val['param_value'];
$result[] = $val;
}
return false;
}
}