lhttpsettings.pp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. { Web server settings
  2. Copyright (C) 2006 Micha Nelissen
  3. This library is Free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or (at your
  6. option) any later version.
  7. This program is diStributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  10. for more details.
  11. You should have received a Copy of the GNU Library General Public License
  12. along with This library; if not, Write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. This license has been modified. See file LICENSE.ADDON for more information.
  15. Should you find these sources without a LICENSE File, please contact
  16. me at [email protected]
  17. }
  18. unit lHTTPSettings;
  19. {$mode objfpc}{$H+}
  20. interface
  21. function GetMimeFile: string;
  22. function GetPort: Word;
  23. function GetHTTPPath: string;
  24. function GetCGIPath: string;
  25. function GetCGIRoot: string;
  26. function GetScriptPathPrefix: string;
  27. function GetPHPCGIBinary: string;
  28. function GetPHPCGIPort: Word;
  29. function GetPHPCGIEnv: string;
  30. procedure InitSettings;
  31. implementation
  32. uses
  33. Classes, SysUtils, IniFiles, Process;
  34. const
  35. DEF_PORT = '3880';
  36. DEF_PHPCGI_PORT = '4665';
  37. DEF_PHPCGI_ENV = 'PHP_FCGI_CHILDREN=5:PHP_FCGI_MAX_REQUESTS=10000';
  38. var
  39. SettingsFile: TIniFile;
  40. HomeDir: string;
  41. CurDir: string;
  42. InitedSettings: Boolean = False;
  43. function CreateDefaultIni(const aFilePath: string): TIniFile;
  44. procedure AddPath(const aName, aPath: string; const CD: Boolean = True);
  45. begin
  46. Result.WriteString('PATH', aName, aPath);
  47. if CD and not DirectoryExists(aPath) then
  48. if not CreateDir(aPath) then
  49. Writeln('Unable to create directory: ', aPath);
  50. end;
  51. procedure CopyFile(const FromF, ToF: string);
  52. const
  53. MAX_SIZE = 65536;
  54. var
  55. f1, f2: TFileStream;
  56. n: Integer;
  57. Buf: array[0..MAX_SIZE-1] of Byte;
  58. begin
  59. f1:=TFileStream.Create(FromF, fmOpenRead);
  60. f2:=TFileStream.Create(ToF, fmCreate);
  61. while f1.Position < f1.Size do begin
  62. n:=f1.Read(Buf, MAX_SIZE);
  63. f2.Write(Buf, n);
  64. end;
  65. f1.Free;
  66. f2.Free;
  67. end;
  68. procedure AddFile(const aName, aFile: string);
  69. begin
  70. Result.WriteString('PATH', aName, aFile);
  71. if not FileExists(aFile) then begin
  72. if FileExists(CurDir + 'mime.types') then
  73. CopyFile(CurDir + 'mime.types', aFile)
  74. else if FileExists('/etc/mime.types') then
  75. CopyFile('/etc/mime.types', aFile)
  76. else
  77. Writeln('Warning! File does not exist: ', aFile);
  78. end;
  79. end;
  80. function GetPHPCGI: string;
  81. const
  82. DIRS: array[1..5] of string = ('/usr/bin/', '/usr/local/bin/', '/usr/lib/',
  83. '/usr/lib/bin/', '/opt/bin/');
  84. FILES: array[1..3] of string = ('php-cgi', 'php5-cgi', 'php4-cgi');
  85. var
  86. i, j: Integer;
  87. begin
  88. Result:='';
  89. for i:=1 to High(DIRS) do
  90. for j:=1 to High(FILES) do
  91. if FileExists(DIRS[i] + FILES[j])
  92. and (FileGetAttr(DIRS[i] + FILES[j]) and faDirectory <> faDirectory) then
  93. Exit(DIRS[i] + FILES[j]);
  94. end;
  95. begin
  96. Writeln('Creating default configuration file in: ', aFilePath);
  97. if not DirectoryExists(ExtractFilePath(aFilePath)) then
  98. CreateDir(ExtractFilePath(aFilePath));
  99. Result:=TIniFile.Create(aFilePath);
  100. AddPath('httpdir', HomeDir + 'http_docs');
  101. AddPath('cgiroot', HomeDir + 'cgi-bin');
  102. {$ifndef WINDOWS}
  103. AddPath('cgipath', '/usr/local/bin:/usr/bin:/bin:' + HomeDir + 'bin', False);
  104. {$else}
  105. AddPath('cgipath', HomeDir + 'cgi-bin', False);
  106. {$endif}
  107. AddFile('mimetypes', HomeDir + 'mime.types');
  108. Result.WriteString('PATH', 'cgiprefix', 'cgi-bin' + PathDelim);
  109. {$ifndef WINDOWS}
  110. Result.WriteString('PATH', 'phpcgibin', GetPHPCGI);
  111. {$else}
  112. Result.WriteString('PATH', 'phpcgibin', 'php-cgi.exe');
  113. {$endif}
  114. Result.WriteString('NET', 'port', DEF_PORT);
  115. Result.WriteString('NET', 'phpcgiport', DEF_PHPCGI_PORT);
  116. Result.WriteString('ENV', 'phpcgienv', DEF_PHPCGI_ENV);
  117. Result.UpdateFile;
  118. end;
  119. procedure InitSettings;
  120. const
  121. INI_NAME = 'fphttpd.ini';
  122. var
  123. SearchPaths: TStringList;
  124. i: Integer;
  125. begin
  126. if not InitedSettings then begin
  127. SearchPaths:=TStringList.Create;
  128. SearchPaths.Add(HomeDir);
  129. {$ifndef WINDOWS}
  130. SearchPaths.Add('/etc/');
  131. SearchPaths.Add('/usr/local/etc/');
  132. {$endif}
  133. SearchPaths.Add(ExtractFilePath(ParamStr(0)) + PathDelim);
  134. for i:=0 to SearchPaths.Count-1 do
  135. if FileExists(SearchPaths[i] + INI_NAME) then begin
  136. Writeln('Loading settings from file: ', SearchPaths[i] + INI_NAME);
  137. SettingsFile:=TIniFile.Create(SearchPaths[i] + INI_NAME);
  138. SearchPaths.Free;
  139. InitedSettings:=True;
  140. Exit;
  141. end;
  142. // no file found, create default one in home
  143. SettingsFile:=CreateDefaultIni(HomeDir + INI_NAME);
  144. SearchPaths.Free;
  145. InitedSettings:=True;
  146. end;
  147. end;
  148. procedure FreeSettings;
  149. begin
  150. if InitedSettings then
  151. SettingsFile.Free;
  152. end;
  153. function GetMimeFile: string;
  154. begin
  155. Result:=SettingsFile.ReadString('PATH', 'mimetypes', '');
  156. end;
  157. function GetPort: Word;
  158. begin
  159. Result:=Word(StrToInt(SettingsFile.ReadString('NET', 'port', DEF_PORT)));
  160. end;
  161. function GetHTTPPath: string;
  162. begin
  163. Result:=SettingsFile.ReadString('PATH', 'httpdir', '') + PathDelim;
  164. end;
  165. function GetCGIPath: string;
  166. begin
  167. Result:=SettingsFile.ReadString('PATH', 'cgipath', '') + PathDelim;
  168. end;
  169. function GetCGIRoot: string;
  170. begin
  171. Result:=SettingsFile.ReadString('PATH', 'cgiroot', '') + PathDelim;
  172. end;
  173. function GetScriptPathPrefix: string;
  174. begin
  175. Result:=SettingsFile.ReadString('PATH', 'cgiprefix', '') + PathDelim;
  176. end;
  177. function GetPHPCGIBinary: string;
  178. begin
  179. Result:=SettingsFile.ReadString('PATH', 'phpcgibin', '');
  180. end;
  181. function GetPHPCGIPort: Word;
  182. begin
  183. Result:=Word(StrToInt(SettingsFile.ReadString('NET', 'phpcgiport', DEF_PORT)));
  184. end;
  185. function GetPHPCGIEnv: string;
  186. begin
  187. Result:=SettingsFile.ReadString('ENV', 'phpcgienv', DEF_PHPCGI_ENV);
  188. end;
  189. initialization
  190. CurDir:=ExtractFilePath(ParamStr(0));
  191. {$ifdef WINDOWS}
  192. HomeDir:=GetEnvironmentVariable('HOMEDRIVE') +
  193. GetEnvironmentVariable('HOMEPATH') + PathDelim + 'fphttpd' + PathDelim;
  194. {$else}
  195. HomeDir:=GetEnvironmentVariable('HOME') + PathDelim + '.fphttpd' + PathDelim;
  196. {$endif}
  197. finalization
  198. FreeSettings;
  199. end.