messages and such
[glcas.git] / lib / plugins / 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 $MENU_ITEMS["ds"]["title"] = "DataStores";
7 $MENU_ITEMS["ds"]["link"] = "/ds/control";
8
9
10 function ds_contentPane()
11 {
12         global $URL_COMPONENTS;
13         if(isset($URL_COMPONENTS[1])) {
14                 switch($URL_COMPONENTS[1]) {
15                         case "browse":
16                                 $dsbr = $URL_COMPONENTS[2];
17                                 echo "<h3>Browsing, $dsbr</h3><br>";
18                                 break;
19                         case "createds":
20                                 ds_createDS($_REQUEST["dsname"], $_REQUEST["dsloc"]);
21                                 echo "DS Created, <a href=\"".urlCreate("/ds/control")."\">return</a>";
22                                 break;
23                         case "control":
24                         default:
25                                 ds_controlPane();
26                                 // go thru the db and list the components
27                 }
28         }
29 }
30
31 function ds_controlPane()
32 {
33         echo "<table>";
34         echo "<tr><td>DataStore Name</td><td>DataStore Path</td><td>Size</td><td>Usage</td><td>Control</td></tr>";
35         
36         echo "</table>";
37         echo "<form method=\"post\" action=\"".urlCreate("/ds/createds")."\">";
38         echo "DataStore Name: <input type=\"text\" name=\"dsname\">";
39         echo "DataStore Location: <input type=\"text\" name=\"dsloc\">";
40         echo "<input type=\"submit\" name=\"create\" value=\"create\">";
41         echo "</form>";
42         
43 }
44
45 function ds_leftMenu()
46 {
47         global $GLOBAL_BASE_URL;
48         echo "<b>Browse</b><br>";
49         $dss = ds_listDS();
50         //echo "<pre>";
51         //print_r($dss);
52         //echo "</pre>";
53         foreach ($dss as $dsl) {
54                 $dsname = $dsl["dsname"];
55                 echo "<a href=\"".urlCreate("/ds/browse/$dsname")."\">$dsname</a><br>";
56         }
57 }
58
59 function ds_contentUrls()
60 {
61         $calls["needs_base_page"] = true;
62         $calls["content_pane_function"] = "ds_contentPane";
63         $calls["left_menu_function"] = "ds_leftMenu";
64         
65         return $calls;
66 }
67
68 function ds_createDS($ds_name, $store_location)
69 {
70         db_createTable("datastores", "dsname", "dslocation");
71         db_createTable("datastores_files", "dsname", "fileowner", "filename", "dsfname");
72         
73         $data = db_selectData("datastores", "dsname", "$ds_name");
74         if(isset($data[0]["dsname"])) return false;
75         
76         db_insertData("datastores", "$ds_name", "$store_location");
77 }
78
79 function ds_listDS()
80 {
81         return db_selectData("datastores");
82 }
83
84 function ds_deleteFile($ds_name, $file_name, $file_tag)
85 {
86         $dlk = db_selectData("datastores_files", "filename", $file_name);
87         $afname = "";
88         foreach($dlk as $kmm) {
89                 if($kmm["fileowner"] == $file_tag) {
90                         // we got ya
91                         $afname = $kmm["dsfname"];
92                         if(file_exists($afname)) unlink($afname);
93                         //echo "name was: $afname\n";
94                 }
95         }
96         //echo "now delete name was: $afname\n";
97         if($afname != "") db_deleteData("datastores_files", "dsfname", $afname);
98 }
99
100 // returns a file name to the location a file can be created
101 function ds_fileds($ds_name, $file_name, $file_tag)
102 {
103
104         $data = db_selectData("datastores", "dsname", "$ds_name");
105         
106         $dtime = time();
107         $made_file_name = "$dtime-".md5($file_name)."-".basename($file_name);
108         
109         $act_fname = $data[0]["dslocation"]."/".basename($made_file_name);
110         
111         db_insertData("datastores_files", "$ds_name", "$file_tag", "$file_name", "$act_fname");
112         
113         return $act_fname;
114 }
115
116 function ds_getFileList($ds_name, $file_tag)
117 {
118         $list = db_selectData("datastores_files", "fileowner", "$file_tag");
119         
120         return $list;
121 }
122
123
124 function ds_deleteDS($ds_name)
125 {
126         db_deleteData("datastores", "dsname", "$ds_name");
127         db_deleteData("datastores_files", "dsname", $ds_name);
128 }
129
130 function ds_downloadAndStore($ds_name, $file_id, $file_name, $file_url)
131 {
132         
133 }
134
135 ?>