updates and such
[glcas.git] / lib / datastore.php
1 <?php
2 global $BASE_URLS, $MENU_ITEMS, $GLOBAL_BASE_URL;
3
4 $BASE_URLS["ds"]["base"] = "ds"; // ap for apt-proxy
5 $BASE_URLS["ds"]["function"] = "ds_contentUrls"; // the page builder function for us
6
7 // the datastore post url
8 $BASE_URLS["dsp"]["base"] = "dsp"; // ap for apt-proxy
9 $BASE_URLS["dsp"]["function"] = "ds_contentPostUrls"; // the page builder function for us
10
11 $MENU_ITEMS["ds"]["title"] = "DataStores";
12 $MENU_ITEMS["ds"]["link"] = "/ds/control";
13
14
15 function ds_contentPostUrls()
16 {
17         $calls["needs_base_page"] = false;
18         $calls["page_builder"] = "ds_postContent";
19         
20         return $calls;
21 }
22
23 function ds_postContent()
24 {
25         global $URL_COMPONENTS;
26         $call = $URL_COMPONENTS[1];
27         
28         switch($call) {
29                 case "createds":
30                         $dsname = $_REQUEST["dsname"];
31                         $dsloc = $_REQUEST["dsloc"];
32                         ds_createDS($dsname, $dsloc);
33                         msg_addMessage("#008800", "DataStore, $dsname created at $dsloc");
34                         break;
35         }
36                 
37         $loc = urlCreate("/ds/control");
38         header("Location: $loc");
39         
40         return 0;
41         
42 }
43
44 function ds_contentPane()
45 {
46         global $URL_COMPONENTS;
47         if(isset($URL_COMPONENTS[1])) {
48                 switch($URL_COMPONENTS[1]) {
49                         case "browse":
50                                 $dsbr = $URL_COMPONENTS[2];
51                                 echo "<h3>Browsing, $dsbr</h3><br>";
52                                 break;
53                         case "createds":
54                                 ds_createDS($_REQUEST["dsname"], $_REQUEST["dsloc"]);
55                                 echo "DS Created, <a href=\"".urlCreate("/ds/control")."\">return</a>";
56                                 break;
57                         case "control":
58                         default:
59                                 ds_controlPane();
60                                 // go thru the db and list the components
61                 }
62         }
63 }
64
65 function ds_controlPane()
66 {
67         global $GLOBAL_BASE_URL;
68         echo "<h3>DataStores</h3>";
69         $dss = ds_listDS();
70         
71         if($dss != false) {
72                 echo "<table border=1>";
73                 echo "<tr><td>DataStore Name</td><td>DataStore Path</td><td>Size</td><td>Usage</td><td>Control</td></tr>";
74                 foreach($dss as $dsl) {
75                         $dsname = $dsl["dsname"];
76                         $dspath = $dsl["dslocation"];
77                         echo "<tr><td>$dsname</td><td>$dspath</td></tr>";
78                 }
79                 echo "</table>";
80         }
81         echo "<h3>Create DataStore</h3>";
82         echo "<form method=\"post\" action=\"".urlCreate("/dsp/createds")."\">";
83         echo "DataStore Name: <input type=\"text\" name=\"dsname\"><br>";
84         echo "DataStore Location: <input type=\"text\" name=\"dsloc\"><br>";
85         echo "<input type=\"submit\" name=\"create\" value=\"create\">";
86         echo "</form>";
87         
88 }
89
90 function ds_leftMenu()
91 {
92         global $GLOBAL_BASE_URL;
93         echo "<h3>Browse</h3>";
94         $dss = ds_listDS();
95         //echo "<pre>";
96         //print_r($dss);
97         //echo "</pre>";
98         foreach ($dss as $dsl) {
99                 $dsname = $dsl["dsname"];
100                 echo "<a href=\"".urlCreate("/ds/browse/$dsname")."\">$dsname</a><br>";
101         }
102 }
103
104 function ds_contentUrls()
105 {
106         $calls["needs_base_page"] = true;
107         $calls["content_pane_function"] = "ds_contentPane";
108         $calls["left_menu_function"] = "ds_leftMenu";
109         
110         return $calls;
111 }
112
113 function ds_createDS($ds_name, $store_location)
114 {
115         db_createTable("datastores", "dsname", "dslocation");
116         db_createTable("datastores_files", "dsname", "fileowner", "filename", "dsfname");
117         
118         $data = db_selectData("datastores", "dsname", "$ds_name");
119         if(isset($data[0]["dsname"])) return false;
120         
121         db_insertData("datastores", "$ds_name", "$store_location");
122 }
123
124 function ds_listDS()
125 {
126         return db_selectData("datastores");
127 }
128
129 function ds_deleteFile($ds_name, $file_name, $file_tag)
130 {
131         $dlk = db_selectData("datastores_files", "filename", $file_name);
132         $afname = "";
133         foreach($dlk as $kmm) {
134                 if($kmm["fileowner"] == $file_tag) {
135                         // we got ya
136                         $afname = $kmm["dsfname"];
137                         if(file_exists($afname)) unlink($afname);
138                         //echo "name was: $afname\n";
139                 }
140         }
141         //echo "now delete name was: $afname\n";
142         if($afname != "") db_deleteData("datastores_files", "dsfname", $afname);
143 }
144
145 // returns a file name to the location a file can be created
146 function ds_fileds($ds_name, $file_name, $file_tag)
147 {
148
149         $data = db_selectData("datastores", "dsname", "$ds_name");
150         
151         $dtime = time();
152         $made_file_name = "$dtime-".md5($file_name)."-".basename($file_name);
153         
154         $act_fname = $data[0]["dslocation"]."/".basename($made_file_name);
155         
156         db_insertData("datastores_files", "$ds_name", "$file_tag", "$file_name", "$act_fname");
157         
158         return $act_fname;
159 }
160
161 function ds_getFileList($ds_name, $file_tag)
162 {
163         $list = db_selectData("datastores_files", "fileowner", "$file_tag");
164         
165         return $list;
166 }
167
168
169 function ds_deleteDS($ds_name)
170 {
171         db_deleteData("datastores", "dsname", "$ds_name");
172         db_deleteData("datastores_files", "dsname", $ds_name);
173 }
174
175 function ds_downloadAndStore($ds_name, $file_id, $file_name, $file_url)
176 {
177         
178 }
179
180 ?>