[ UP ] port cgi to lighttpd web server, and update the lighttpd's config files.
[archserver.git] / cgi / shares / edit_share.lua
blob0780c3892a17c017621ecae9257d92f6d0456c2a
1 #!/usr/bin/lua
3 dofile "libutil.lua"
5 function filteruser (all, rm)
6 left = ""
7 for a in string.gmatch(all, "(%S+)") do
8 find = nil
9 for r in string.gmatch(rm, "(%S+)") do
10 if a == r then find = true end
11 end
12 if not find then left = left.." "..a end
13 end
14 return left
15 end
17 function display(share,path,comment,readlist, writelist)
18 local smbuser, alluser = get_samba_user(), ""
19 for u in pairs(smbuser) do alluser = alluser.." "..u end
21 alluser = filteruser(alluser, readlist.." "..writelist),
23 print([[
24 <form method="post" name="myform" action="shares_edit_share.cgi">
25 <h3>编辑共享</h3>
26 <table cellspacing="1" cellpadding="2" border="0" width="40%">
27 <tr><td class="color_table_heading" align="right">共享名称</td>
28 <td class="color_table_row1">
29 <input type="text" name="share" id="_share">
30 </td><tr>
31 <tr><td class="color_table_heading" align="right">共享目录</td>
32 <td class="color_table_row2">
33 <input type="text" name="path" id="_path">
34 </td></tr>
35 <tr><td class="color_table_heading" align="right">
36 共享描述</td>
37 <td class="color_table_row1">
38 <input type="text" name="comment" id="_comment">
39 </td></tr>
40 </table>
41 <hr>
42 ]])
43 print([[
44 <script language="JavaScript">
45 <!--
46 document.myform.share.value="]]..share..[[";
47 document.myform.path.value="]]..path..[[";
48 document.myform.comment.value="]]..comment..[[";
49 //-->
50 </script>
51 ]])
53 ui_message_info([[<strong>用户和组访问此共享的权限: </strong>选定一项或多项然后点击添加或移除(按住shift或ctrl可以多选),或双击进行添加和移除。</p>]])
55 print([[
56 <table border="0" width="50%">
57 <tr>
58 <td width="30%">
59 <strong>所有用户和组</strong>
60 <select style="width:100%;" multiple name="list1" size="10" ondblclick="moveOption(document.myform.list1, document.myform.list2)"> ]])
62 for auser in string.gmatch(alluser, "(%S+)") do
63 print([[<option value="]]..auser..[[">]]..auser..[[</option>]])
64 end
66 print([[
67 </select>
68 </td>
69 <td width="5%" align="center">
70 <input type="button" value="添加" onclick="moveOption(document.myform.list1, document.myform.list2)"><br><br>
71 <input type="button" value="全选" onclick="moveAllOption(document.myform.list1, document.myform.list2)"><br><br>
72 <input type="button" value="删除" onclick="moveOption(document.myform.list2, document.myform.list1)"><br><br>
73 <input type="button" value="全删" onclick="moveAllOption(document.myform.list2, document.myform.list1)">
74 </td>
75 <td width="30%">
76 <strong>只读用户和组</strong>
77 <select style="width:100%;" multiple name="list2" size="10" ondblclick="moveOption(document.myform.list2, document.myform.list3)"> ]])
79 for ruser in string.gmatch(readlist, "(%S+)") do
80 print([[<option value="]]..ruser..[[">]]..ruser..[[</option>]])
81 end
83 print([[
84 </select>
85 </td>
86 <td width="5%" align="center">
87 <input type="button" value="添加" onclick="moveOption(document.myform.list2, document.myform.list3)"><br><br>
88 <input type="button" value="全选" onclick="moveAllOption(document.myform.list2, document.myform.list3)"><br><br>
89 <input type="button" value="删除" onclick="moveOption(document.myform.list3, document.myform.list2)"><br><br>
90 <input type="button" value="全删" onclick="moveAllOption(document.myform.list3, document.myform.list2)">
91 </td>
92 <td width="30%">
93 <strong>读写用户和组</strong>
94 <select style="width:100%;" multiple name="list3" size="10" ondblclick="moveOption(document.myform.list3, document.myform.list2)"> ]])
96 for wuser in string.gmatch(writelist, "(%S+)") do
97 print([[<option value="]]..wuser..[[">]]..wuser..[[</option>]])
98 end
100 print([[
101 </select>
102 </td>
103 </tr>
104 </table>
105 <hr>
107 <input type=hidden name="readlist" id="readlist">
108 <input type=hidden name="writelist" id="writelist">
109 <input type=hidden name=todo value="create_share">
110 <input type="submit" value="提交设置">
111 </form>
113 <script language="JavaScript">
114 <!--
116 document.getElementById('readlist').readOnly= true;
117 document.getElementById('writelist').readOnly= true;
118 document.myform.readlist.value=getvalue(document.myform.list2);
119 document.myform.writelist.value=getvalue(document.myform.list3);
121 function moveOption(e1, e2){
122 var e3 = document.myform.list3;
123 try{
124 for(var i = 0; i < e1.options.length; i++){
125 if(e1.options[i].selected){
126 var e = e1.options[i];
127 e1.remove(i);
128 e2.options.add(new Option(e.text, e.value));
129 i = i - 1;
133 document.myform.readlist.value=getvalue(document.myform.list2);
134 document.myform.writelist.value=getvalue(document.myform.list3);
136 catch(e){}
139 function getvalue(geto){
140 var allvalue = "";
141 for(var i = 0; i < geto.options.length; i++){
142 allvalue += geto.options[i].value + ",";
144 return allvalue;
147 function moveAllOption(e1, e2){
148 var e3 = document.myform.list3;
149 try{
150 for(var i = 0;i < e1.options.length; i++){
151 var e = e1.options[i];
152 e2.options.add(new Option(e.text, e.value));
153 e1.remove(i);
154 i = i - 1;
158 document.myform.readlist.value=getvalue(document.myform.list2);
159 document.myform.writelist.value=getvalue(document.myform.list3);
161 catch(e){}
163 //-->
164 </script>
169 --<strong>只读用户和组:</strong><textarea cols="65" rows="2" name="readlist" id="readlist" value="" /> </textarea><br>
170 --<strong>读写用户和组:</strong><textarea cols="65" rows="2" name="writelist" id="writelist" value="" /> </textarea><br>
172 -----------------------------------
173 -- CGI OUTPUT
174 -----------------------------------
175 qp = ui_getqp()
177 local sh_list = nil
179 local share = qp.share or ""
180 local path = qp.path or ""
181 local comment = qp.comment or ""
182 local readlist, writelist = "", ""
185 if qp.todo =="create_share" then
186 local err,ret,path
188 if qp.share=="" or qp.path == "" then ui_message_err("共享名称和共享目录不能为空.") return end
189 path = qp.path
190 if string.sub(path,1,6) ~= "/home/" then path="/home/"..qp.path end
191 err,ret = myexec("sudo /bin/mkdir -p "..path.." && sudo chmod 777 "..path)
192 smbshare.set(qp.share,path,qp.readlist,qp.writelist,qp.comment)
193 print("共享设置 ... <font color=green><strong>成功</strong></font>")
195 elseif qp.todo == "edit_create" then
196 sh_list = smbshare.get()
197 if sh_list[qp.share] then
198 ui_message_err("共享 <strong>\""..qp.share.."\" </strong>已存在")
199 return
202 elseif qp.todo == "edit_update" then
203 sh_list = smbshare.get()
204 if not sh_list[qp.share] then
205 ui_message_err("共享 <strong>\""..qp.share.."\" </strong>不存在")
206 return
209 if (sh_list[qp.share].readlist) then
210 readlist = string.gsub(sh_list[qp.share].readlist, ",", " ")
213 if (sh_list[qp.share].writelist) then
214 writelist = string.gsub(sh_list[qp.share].writelist, ",", " ")
218 display(share,path,comment,readlist,writelist)