Blame view

php/classes/SharedObjectMgrAbstract.php 4.43 KB
169f14d2   Benjamin Renard   Add shared object...
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
<?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);
	
	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);
		
		if (!$result['success'])
			return $result;
		
		return array('success' => TRUE, 'object_id' => $newId);
	}
	
	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;
	}
}

?>