1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_svx.hxx"
27 #include "docrecovery.hxx"
28 #include "osl/file.hxx"
29 #include "rtl/bootstrap.hxx"
30 #include "rtl/strbuf.hxx"
31 #include "tools/appendunixshellword.hxx"
32 #include <string>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <pwd.h>
37 
38 #define RCFILE ".crash_reportrc"
39 
40 using namespace ::std;
41 
get_home_dir()42 static const char *get_home_dir()
43 {
44 	struct passwd *ppwd = getpwuid( getuid() );
45 
46 	return ppwd ? (ppwd->pw_dir ? ppwd->pw_dir : "/") : "/";
47 }
48 
read_line(FILE * fp,string & rLine)49 static bool read_line( FILE *fp, string& rLine )
50 {
51 	char szBuffer[1024];
52 	bool bSuccess = false;
53 	bool bEOL = false;
54 	string	line;
55 
56 
57 	while ( !bEOL && fgets( szBuffer, sizeof(szBuffer), fp ) )
58 	{
59 		int	len = strlen(szBuffer);
60 
61 		bSuccess = true;
62 
63 		while ( len && szBuffer[len - 1] == '\n' )
64 		{
65 			szBuffer[--len] = 0;
66 			bEOL = true;
67 		}
68 
69 		line.append( szBuffer );
70 	}
71 
72 	rLine = line;
73 	return bSuccess;
74 }
75 
trim_string(const string & rString)76 static string trim_string( const string& rString )
77 {
78 	string temp = rString;
79 
80 	while ( temp.length() && (temp[0] == ' ' || temp[0] == '\t') )
81 		temp.erase( 0, 1 );
82 
83 	string::size_type	len = temp.length();
84 
85 	while ( len && (temp[len-1] == ' ' || temp[len-1] == '\t') )
86 	{
87 		temp.erase( len - 1, 1 );
88 		len = temp.length();
89 	}
90 
91 	return temp;
92 }
93 
get_profile_string(const char * pFileName,const char * pSectionName,const char * pKeyName,const char * pDefault=NULL)94 static string get_profile_string( const char *pFileName, const char *pSectionName, const char *pKeyName, const char *pDefault = NULL )
95 {
96 	FILE	*fp = fopen( pFileName, "r" );
97 	string	retValue = pDefault ? pDefault : "";
98 
99 	if ( fp )
100 	{
101 		string line;
102 		string section;
103 
104 		while ( read_line( fp, line ) )
105 		{
106 			line = trim_string( line );
107 
108 			if ( line.length() && line[0] == '[' )
109 			{
110 				line.erase( 0, 1 );
111 				string::size_type end = line.find( ']', 0 );
112 
113 				if ( string::npos != end )
114 					section = trim_string( line.substr( 0, end ) );
115 			}
116 			else
117 			{
118 
119 				string::size_type iEqualSign = line.find( '=', 0 );
120 
121 				if ( iEqualSign != string::npos )
122 				{
123 					string	keyname = line.substr( 0, iEqualSign );
124 					keyname = trim_string( keyname );
125 
126                     string  value = line.substr( iEqualSign + 1, string::npos );
127 					value = trim_string( value );
128 
129 					if (
130 						0 == strcasecmp( section.c_str(), pSectionName ) &&
131 						0 == strcasecmp( keyname.c_str(), pKeyName )
132 						 )
133 					{
134 						retValue = value;
135 						break;
136 					}
137 				}
138 			}
139 		}
140 
141 		fclose( fp );
142 	}
143 
144 	return retValue;
145 }
146 
get_profile_bool(const char * pFileName,const char * pSectionName,const char * pKeyName)147 static bool get_profile_bool( const char *pFileName, const char *pSectionName, const char *pKeyName )
148 {
149 	string	str = get_profile_string( pFileName, pSectionName, pKeyName );
150 
151 	if ( !strcasecmp( str.c_str(), "true" ) )
152 		return true;
153 	return false;
154 }
155 
get_profile_String(const char * pFileName,const char * pSectionName,const char * pKeyName,const char * =NULL)156 static String get_profile_String( const char *pFileName, const char *pSectionName, const char *pKeyName, const char * = NULL )
157 {
158 	string	str = get_profile_string( pFileName, pSectionName, pKeyName );
159 	String	result( str.c_str(), RTL_TEXTENCODING_UTF8 );
160 
161 	return result;
162 }
163 
164 namespace svx{
165     namespace DocRecovery{
166 
ReadParams()167 		bool ErrorRepSendDialog::ReadParams()
168 		{
169 			string	sRCFile = get_home_dir();
170 
171 			sRCFile += "/";
172 			sRCFile += string(RCFILE);
173 
174 			maEMailAddrED.SetText( get_profile_String( sRCFile.c_str(), "Options", "ReturnAddress" ) );
175 			maParams.maHTTPProxyServer = get_profile_String( sRCFile.c_str(), "Options", "ProxyServer" );
176 			maParams.maHTTPProxyPort = get_profile_String( sRCFile.c_str(), "Options", "ProxyPort" );
177 			maParams.miHTTPConnectionType = get_profile_bool( sRCFile.c_str(), "Options", "UseProxy" ) ? 2 : 1;
178 			maContactCB.Check( get_profile_bool( sRCFile.c_str(), "Options", "AllowContact" ) );
179 
180 			return true;
181 		}
182 
SaveParams()183 		bool ErrorRepSendDialog::SaveParams()
184 		{
185 			bool success = false;
186 			string	sRCFile = get_home_dir();
187 
188 			sRCFile += "/";
189 			sRCFile += string(RCFILE);
190 
191 			FILE *fp = fopen( sRCFile.c_str(), "w" );
192 
193 			if ( fp )
194 			{
195 				fprintf( fp, "[Options]\n" );
196 				fprintf( fp, "UseProxy=%s\n", 2 == maParams.miHTTPConnectionType ? "true" : "false" );
197 				fprintf( fp, "ProxyServer=%s\n", ByteString( maParams.maHTTPProxyServer, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
198 				fprintf( fp, "ProxyPort=%s\n", ByteString( maParams.maHTTPProxyPort, RTL_TEXTENCODING_UTF8 ).GetBuffer() );
199 				fprintf( fp, "ReturnAddress=%s\n", ByteString( GetEMailAddress(), RTL_TEXTENCODING_UTF8 ).GetBuffer() );
200 				fprintf( fp, "AllowContact=%s\n", IsContactAllowed() ? "true" : "false" );
201 				fclose( fp );
202 			}
203 
204 			return success;
205 		}
206 
SendReport()207 		bool ErrorRepSendDialog::SendReport()
208 		{
209 			ByteString	strSubject( GetDocType(), RTL_TEXTENCODING_UTF8 );
210 
211 #if defined( LINUX ) || defined (MACOSX )
212 			setenv( "ERRORREPORT_SUBJECT", strSubject.GetBuffer(), 1 );
213 #else
214 			static ::rtl::OString	strEnvSubject = "ERRORREPORT_SUBJECT";
215 			strEnvSubject += "=";
216 			strEnvSubject += strSubject.GetBuffer();
217 			putenv( (char *)strEnvSubject.getStr() );
218 #endif
219 
220 			char szBodyFile[L_tmpnam] = "";
221 			FILE *fp = fopen( tmpnam( szBodyFile ), "w" );
222 
223 			if ( fp )
224 			{
225 				ByteString	strUTF8( GetUsing(), RTL_TEXTENCODING_UTF8 );
226 
227 				fwrite( strUTF8.GetBuffer(), 1, strUTF8.Len(), fp );
228 				fclose( fp );
229 #if defined( LINUX ) || defined (MACOSX)
230 				setenv( "ERRORREPORT_BODYFILE", szBodyFile, 1 );
231 #else
232 			static ::rtl::OString	strEnvBodyFile = "ERRORREPORT_BODYFILE";
233 			strEnvBodyFile += "=";
234 			strEnvBodyFile += szBodyFile;
235 			putenv( (char *)strEnvBodyFile.getStr() );
236 #endif
237 			}
238 
239             int ret = -1;
240             rtl::OUString path1(
241                 RTL_CONSTASCII_USTRINGPARAM(
242                     "$OOO_BASE_DIR/program/crashrep"));
243             rtl::Bootstrap::expandMacros(path1);
244             rtl::OString path2;
245             if ((osl::FileBase::getSystemPathFromFileURL(path1, path1) ==
246                  osl::FileBase::E_None) &&
247                 path1.convertToString(
248                     &path2, osl_getThreadTextEncoding(),
249                     (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
250                      RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
251             {
252                 rtl::OStringBuffer cmd;
253                 tools::appendUnixShellWord(&cmd, path2);
254                 cmd.append(RTL_CONSTASCII_STRINGPARAM(" -debug -load -send -noui"));
255                 ret = system(cmd.getStr());
256             }
257 
258 			if ( szBodyFile[0] )
259 			{
260 				unlink( szBodyFile );
261 			}
262 
263 			return -1 != ret;
264 		}
265 
266 
267 	}	// namespace DocRecovery
268 }	// namespace svx
269