1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svl.hxx"
30 
31 #include <svl/folderrestriction.hxx>
32 #include "osl/process.h"
33 #include "tools/urlobj.hxx"
34 #include "unotools/localfilehelper.hxx"
35 
36 //-----------------------------------------------------------------------------
37 
38 static void convertStringListToUrls (
39 	const String& _rColonSeparatedList, ::std::vector< String >& _rTokens, bool _bFinalSlash )
40 {
41 	const sal_Unicode s_cSeparator =
42 #if defined(WNT)
43 		';'
44 #else
45 		':'
46 #endif
47             ;
48 	xub_StrLen nTokens = _rColonSeparatedList.GetTokenCount( s_cSeparator );
49 	_rTokens.resize( 0 ); _rTokens.reserve( nTokens );
50 	for ( xub_StrLen i=0; i<nTokens; ++i )
51 	{
52 		// the current token in the list
53 		String sCurrentToken = _rColonSeparatedList.GetToken( i, s_cSeparator );
54 		if ( !sCurrentToken.Len() )
55 			continue;
56 
57 		INetURLObject aCurrentURL;
58 
59 		String sURL;
60 		if ( ::utl::LocalFileHelper::ConvertPhysicalNameToURL( sCurrentToken, sURL ) )
61 			aCurrentURL = INetURLObject( sURL );
62 		else
63 		{
64 			// smart URL parsing, assuming FILE protocol
65 			aCurrentURL = INetURLObject( sCurrentToken, INET_PROT_FILE );
66 		}
67 
68 		if ( _bFinalSlash )
69 			aCurrentURL.setFinalSlash( );
70 		else
71 			aCurrentURL.removeFinalSlash( );
72 		_rTokens.push_back( aCurrentURL.GetMainURL( INetURLObject::NO_DECODE ) );
73 	}
74 }
75 
76 /** retrieves the value of an environment variable
77 	@return <TRUE/> if and only if the retrieved string value is not empty
78 */
79 static bool getEnvironmentValue( const sal_Char* _pAsciiEnvName, ::rtl::OUString& _rValue )
80 {
81 	_rValue = ::rtl::OUString();
82 	::rtl::OUString sEnvName = ::rtl::OUString::createFromAscii( _pAsciiEnvName );
83 	osl_getEnvironment( sEnvName.pData, &_rValue.pData );
84 	return _rValue.getLength() != 0;
85 }
86 
87 //-----------------------------------------------------------------------------
88 
89 namespace svt
90 {
91 
92     void getUnrestrictedFolders( ::std::vector< String >& _rFolders )
93     {
94         _rFolders.resize( 0 );
95         ::rtl::OUString sRestrictedPathList;
96         if ( getEnvironmentValue( "RestrictedPath", sRestrictedPathList ) )
97 		{
98             // append a final slash. This ensures that when we later on check
99             // for unrestricted paths, we don't allow paths like "/home/user35" just because
100             // "/home/user3" is allowed - with the final slash, we make it "/home/user3/".
101             convertStringListToUrls( sRestrictedPathList, _rFolders, true );
102 		}
103     }
104 
105 } // namespace svt
106 
107