719c486b50f0ace363ffab3c1182852d12c8bcc5
[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 - this class is a future replacement for ttExportHelper.
34 // Currently, it is work in progress.
35 // When done, it should handle export of organizations containing multiple groups.
36 class ttOrgExportHelper {
37
38   var $fileName = null; // Name of file with data.
39
40   // createDataFile creates a file with all data for the entire organization.
41   function createDataFile($compress = false) {
42     global $user;
43
44     // Create a temporary file.
45     $dirName = dirname(TEMPLATE_DIR . '_c/.');
46     $tmp_file = tempnam($dirName, 'tt');
47
48     // Open the file for writing.
49     $file = fopen($tmp_file, 'wb');
50     if (!$file) return false;
51
52     // Write XML to the file.
53     fwrite($file, "<?xml version=\"1.0\"?>\n");
54     $org_part = "<org schema=\"".$this->getVersion()."\">\n";
55     fwrite($file, $org_part);
56
57     // Use ttGroupExportHelper to export all groups.
58     $groupExportHelper = new ttGroupExportHelper($user->group_id, $file, '  ');  // 2 spaces indentation for home group.
59     $groupExportHelper->writeData();
60
61     fwrite($file, "</org>\n");
62     fclose($file);
63
64     if ($compress) {
65       $this->fileName = tempnam($dirName, 'tt');
66       $this->compress($tmp_file, $this->fileName);
67       unlink($tmp_file);
68     } else
69       $this->fileName = $tmp_file;
70
71     return true;
72   }
73
74   // getFileName - returns file name.
75   function getFileName() {
76     return $this->fileName;
77   }
78
79   // compress - compresses the content of the $in file into $out file.
80   function compress($in, $out) {
81     // Initial checks of file names and permissions.
82     if (!file_exists($in) || !is_readable ($in))
83       return false;
84     if ((!file_exists($out) && !is_writable(dirname($out))) || (file_exists($out) && !is_writable($out)))
85       return false;
86
87     $in_file = fopen($in, 'rb');
88
89     if (function_exists('bzopen')) {
90       if (!$out_file = bzopen($out, 'w'))
91         return false;
92
93       while (!feof ($in_file)) {
94         $buffer = fread($in_file, 4096);
95         bzwrite($out_file, $buffer, 4096);
96       }
97       bzclose($out_file);
98     }
99     fclose ($in_file);
100     return true;
101   }
102
103   private function getVersion() {
104     $mdb2 = getConnection();
105     $sql = "select param_value from tt_site_config where param_name = 'version_db'";
106     $res = $mdb2->query($sql);
107     if (!is_a($res, 'PEAR_Error')) {
108       $val = $res->fetchRow();
109       return $val['param_value'];
110         $result[] = $val;
111     }
112     return false;
113   }
114 }