SharedObjectMgrAbstract.php
5.47 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* @class SharedObjectMgrAbstract
* @version
*
*/
abstract class SharedObjectMgrAbstract {
protected $rootdirname = "";
private static $headerdirname = "header";
private static $datadirname = "data";
function __construct() {
if (!is_dir($this->getRootDirPath())) {
mkdir($this->getRootDirPath());
chgrp($this->getRootDirPath(), APACHE_USER);
chmod($this->getRootDirPath(), 0775);
}
}
abstract protected function getDataInfo($object_id);
abstract protected function addData($src_object_path, $dst_data_path, $newId, $newName, $newDescription);
public function getDescription($folder_name, $object_id) {
$result = array();
$headerFilePath = $this->getHeaderFilePath($folder_name, $object_id);
$headerFile = new SharedObjectHeaderFile($headerFilePath);
//Get info contained in data file
$result = $this->getDataInfo($object_id);
//Get info contained in header file
$header_info = $headerFile->getInfo();
foreach ($header_info as $key => $value) {
//if (!array_key_exists($key, $result) && empty($value))
// continue;
$result[$key] = $value;
}
return $result;
}
public function add($object_name, $folder_name, $src_object_path, $description, $sharedBy, $newId) {
$dst_folder_path = $this->getFolderDirPath($folder_name);
if (!is_dir($dst_folder_path))
return array('success' => false, 'message' => 'Cannot get folder path');
$dst_header_path = $this->getHeaderDirPath($folder_name);
if (!is_dir($dst_header_path)) {
mkdir($dst_header_path);
chgrp($dst_header_path, APACHE_USER);
chmod($dst_header_path, 0775);
}
$dst_data_path = $this->getDataDirPath($folder_name);
if (!is_dir($dst_data_path)) {
mkdir($dst_data_path);
chgrp($dst_data_path, APACHE_USER);
chmod($dst_data_path, 0775);
}
$headerFilePath = $this->getHeaderFilePath($folder_name, $newId);
$headerFile = new SharedObjectHeaderFile($headerFilePath);
$headerFile->setName($object_name);
$headerFile->setDescription($description);
$headerFile->setSharedBy($sharedBy);
date_default_timezone_set("UTC");
$headerFile->setSharedDate(date("c"));
$result = $this->addData($src_object_path, $dst_data_path, $newId, $object_name, $description);
if (!$result['success'])
return $result;
return array('success' => TRUE, 'object_id' => $newId);
}
public function delete($folder_name, $objectId) {
$dst_folder_path = $this->getFolderDirPath($folder_name);
if (!is_dir($dst_folder_path))
return array('success' => false, 'message' => 'Cannot get folder path');
$dst_header_path = $this->getHeaderDirPath($folder_name);
if (is_dir($dst_header_path)) {
$headerFilePath = $this->getHeaderFilePath($folder_name, $objectId);
if (is_file($headerFilePath)) {
unlink($headerFilePath);
}
}
$dst_data_path = $this->getDataDirPath($folder_name);
if (is_dir($dst_data_path)) {
$dataFilePath = $this->getDataFilePath($folder_name, $objectId);
if (is_file($dataFilePath)) {
unlink($dataFilePath);
}
}
return array('success' => TRUE, 'object_id' => $objectId);
}
public function createFolder($folder_name) {
$folder_path = $this->getFolderDirPath($folder_name);
if (is_dir($folder_path)) {
return FALSE;
}
mkdir($folder_path);
chgrp($folder_path, APACHE_USER);
chmod($folder_path, 0775);
return is_dir($folder_path);
}
public function getRootDirPath() {
return SHAREDPATH . "/" . $this->rootdirname;
}
public function getFolderDirPath($folder_name) {
return $this->getRootDirPath() . "/" . $folder_name;
}
public function getHeaderDirPath($folder_name) {
return $this->getFolderDirPath($folder_name) . "/" . self::$headerdirname;
}
public function getDataDirPath($folder_name) {
return $this->getFolderDirPath($folder_name) . "/" . self::$datadirname;
}
public function getHeaderFilePath($folder_name, $object_id) {
return $this->getHeaderDirPath($folder_name) . "/" . $object_id . ".xml";
}
public function getDataFilePath($folder_name, $object_id) {
return $this->getDataDirPath($folder_name) . "/" . $object_id . ".xml";
}
public function scanObjects() {
$root_dir = $this->getRootDirPath();
$folders = $this->scanDir($root_dir, TRUE, FALSE);
$result = array();
foreach ($folders as $folder_name) {
$header_path = $this->getHeaderDirPath($folder_name);
if (!is_dir($header_path)) {
mkdir($header_path);
chgrp($header_path, APACHE_USER);
chmod($header_path, 0775);
}
$header_files = $this->scanDir($header_path, FALSE, TRUE);
$data_path = $this->getDataDirPath($folder_name);
if (!is_dir($data_path)) {
mkdir($data_path);
chgrp($data_path, APACHE_USER);
chmod($data_path, 0775);
}
$data_files = $this->scanDir($data_path, FALSE, TRUE);
foreach ($header_files as $header_file) {
//Check if we have header and data files
if (!in_array($header_file, $data_files))
continue;
if (!array_key_exists($folder_name, $result))
$result[$folder_name] = array();
$result[$folder_name][] = basename($header_file, ".xml");;
}
//Add empty folders
if (!array_key_exists($folder_name, $result))
$result[$folder_name] = array();
}
return $result;
}
private function scanDir($dir, $getDirs, $getFiles) {
$dirs = array_diff(scandir($dir), Array( ".", ".." ));
$result = array();
foreach($dirs as $d){
if($getDirs && is_dir($dir."/".$d))
$result[] = $d;
if($getFiles && !is_dir($dir."/".$d))
$result[] = $d;
}
return $result;
}
}
?>