Removed old import and export classes.
[timetracker.git] / WEB-INF / lib / ttOrgExportHelper.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('ttTeamHelper');
30 import('ttTimeHelper');
31 import('ttGroupExportHelper');
32
33 // ttOrgExportHelper handles export of organizations consisting of multiple groups
34 // into XML file for import (migrating) to another server.
35 class ttOrgExportHelper {
36
37   var $fileName = null; // Name of file with data.
38
39   // createDataFile creates a file with all data for the entire organization.
40   function createDataFile($compress = false) {
41     global $user;
42
43     // Create a temporary file.
44     $dirName = dirname(TEMPLATE_DIR . '_c/.');
45     $tmp_file = tempnam($dirName, 'tt');
46
47     // Open the file for writing.
48     $file = fopen($tmp_file, 'wb');
49     if (!$file) return false;
50
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);
55
56     // Use ttGroupExportHelper to export all groups.
57     $groupExportHelper = new ttGroupExportHelper($user->group_id, $file, '  ');  // 2 spaces indentation for home group.
58     $groupExportHelper->writeData();
59
60     fwrite($file, "</org>\n");
61     fclose($file);
62
63     if ($compress) {
64       $this->fileName = tempnam($dirName, 'tt');
65       $this->compress($tmp_file, $this->fileName);
66       unlink($tmp_file);
67     } else
68       $this->fileName = $tmp_file;
69
70     return true;
71   }
72
73   // getFileName - returns file name.
74   function getFileName() {
75     return $this->fileName;
76   }
77
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))
82       return false;
83     if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
84       return false;
85
86     $in_file = fopen($in, 'rb');
87
88     if (function_exists('bzopen')) {
89       if (!$out_file = bzopen($out, 'w'))
90         return false;
91
92       while (!feof ($in_file)) {
93         $buffer = fread($in_file, 4096);
94         bzwrite($out_file, $buffer, 4096);
95       }
96       bzclose($out_file);
97     }
98     fclose ($in_file);
99     return true;
100   }
101
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'];
109         $result[] = $val;
110     }
111     return false;
112   }
113 }