Merge pull request #1735 from sumpfralle/olsr-jsoninfo-parser-handle-empty-result
[oweals/luci.git] / applications / luci-app-openvpn / luasrc / controller / openvpn.lua
index 8695dae848ef89f0faea2a805dd9878d2af559d5..55c29d1c2ac29c14534a95b09cef47d6307e2b34 100644 (file)
@@ -1,22 +1,54 @@
---[[
-LuCI - Lua Configuration Interface
+-- Copyright 2008 Steven Barth <steven@midlink.org>
+-- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
+-- Licensed to the public under the Apache License 2.0.
 
-Copyright 2008 Steven Barth <steven@midlink.org>
-Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
+module("luci.controller.openvpn", package.seeall)
+
+function index()
+       entry( {"admin", "vpn", "openvpn"}, cbi("openvpn"), _("OpenVPN") )
+       entry( {"admin", "vpn", "openvpn", "basic"},    cbi("openvpn-basic"),    nil ).leaf = true
+       entry( {"admin", "vpn", "openvpn", "advanced"}, cbi("openvpn-advanced"), nil ).leaf = true
+       entry( {"admin", "vpn", "openvpn", "file"},     form("openvpn-file"),    nil ).leaf = true
+       entry( {"admin", "vpn", "openvpn", "upload"},   call("ovpn_upload"))
+end
 
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
+function ovpn_upload()
+       local fs     = require("nixio.fs")
+       local http   = require("luci.http")
+       local util   = require("luci.util")
+       local uci    = require("luci.model.uci").cursor()
+       local upload = http.formvalue("ovpn_file")
+       local name   = http.formvalue("instance_name2")
+       local file   = "/etc/openvpn/" ..name.. ".ovpn"
 
-       http://www.apache.org/licenses/LICENSE-2.0
+       if name and upload then
+               local fp
 
-$Id$
-]]--
+               http.setfilehandler(
+                       function(meta, chunk, eof)
+                               local data = util.trim(chunk:gsub("\r\n", "\n")) .. "\n"
+                               data = util.trim(data:gsub("[\128-\255]", ""))
 
-module("luci.controller.openvpn", package.seeall)
+                               if not fp and meta and meta.name == "ovpn_file" then
+                                       fp = io.open(file, "w")
+                               end
+                               if fp and data then
+                                       fp:write(data)
+                               end
+                               if fp and eof then
+                                       fp:close()
+                               end
+                       end
+               )
 
-function index()
-       entry( {"admin", "services", "openvpn"}, cbi("openvpn"), _("OpenVPN") )
-       entry( {"admin", "services", "openvpn", "basic"},    cbi("openvpn-basic"),    nil ).leaf = true
-       entry( {"admin", "services", "openvpn", "advanced"}, cbi("openvpn-advanced"), nil ).leaf = true
+               if fs.access(file) then
+                       if not uci:get_first("openvpn", name) then
+                               uci:set("openvpn", name, "openvpn")
+                               uci:set("openvpn", name, "config", file)
+                               uci:save("openvpn")
+                               uci:commit("openvpn")
+                       end
+               end
+       end
+       http.redirect(luci.dispatcher.build_url('admin/vpn/openvpn'))
 end