2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
11 // | There are only two ways to violate the license:
13 // | 1. To redistribute this code in source form, with the copyright
14 // | notice or license removed or altered. (Distributing in compiled
15 // | forms without embedded copyright notices is permitted).
17 // | 2. To redistribute modified versions of this code in *any* form
18 // | that bears insufficient indications that the modifications are
19 // | not the work of the original author(s).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import('ttTeamHelper');
30 import('ttTimeHelper');
31 import('ttGroupExportHelper');
33 // ttOrgExportHelper handles export of organizations consisting of multiple groups
34 // into XML file for import (migrating) to another server.
35 class ttOrgExportHelper {
37 var $fileName = null; // Name of file with data.
39 // createDataFile creates a file with all data for the entire organization.
40 function createDataFile($compress = false) {
43 // Create a temporary file.
44 $dirName = dirname(TEMPLATE_DIR . '_c/.');
45 $tmp_file = tempnam($dirName, 'tt');
47 // Open the file for writing.
48 $file = fopen($tmp_file, 'wb');
49 if (!$file) return false;
51 // Write XML to the file.
52 fwrite($file, "<?xml version=\"1.0\"?>\n");
53 $org_part = "<org schema=\"".$this->getVersion()."\">\n";
54 fwrite($file, $org_part);
56 // Use ttGroupExportHelper to export all groups.
57 $groupExportHelper = new ttGroupExportHelper($user->group_id, $file, ' '); // 2 spaces indentation for home group.
58 $groupExportHelper->writeData();
60 fwrite($file, "</org>\n");
64 $this->fileName = tempnam($dirName, 'tt');
65 $this->compress($tmp_file, $this->fileName);
68 $this->fileName = $tmp_file;
73 // getFileName - returns file name.
74 function getFileName() {
75 return $this->fileName;
78 // compress - compresses the content of the $in file into $out file.
79 function compress($in, $out) {
80 // Initial checks of file names and permissions.
81 if (!file_exists($in) || !is_readable ($in))
83 if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
86 $in_file = fopen($in, 'rb');
88 if (function_exists('bzopen')) {
89 if (!$out_file = bzopen($out, 'w'))
92 while (!feof ($in_file)) {
93 $buffer = fread($in_file, 4096);
94 bzwrite($out_file, $buffer, 4096);
102 private function getVersion() {
103 $mdb2 = getConnection();
104 $sql = "select param_value from tt_site_config where param_name = 'version_db'";
105 $res = $mdb2->query($sql);
106 if (!is_a($res, 'PEAR_Error')) {
107 $val = $res->fetchRow();
108 return $val['param_value'];