PageRenderTime 914ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/src/artica-make/setup_atmailopen.pas

https://github.com/rsd/artica-1.5
Pascal | 277 lines | 223 code | 44 blank | 10 comment | 35 complexity | d8548644873c2983780224c4ffe02487 MD5 | raw file
  1. unit setup_atmailopen;
  2. {$MODE DELPHI}
  3. //{$mode objfpc}{$H+}
  4. {$LONGSTRINGS ON}
  5. //ln -s /usr/lib/libmilter/libsmutil.a /usr/local/lib/libsmutil.a
  6. interface
  7. uses
  8. Classes, SysUtils,strutils,RegExpr in 'RegExpr.pas',
  9. unix,IniFiles,setup_libs,distridetect,
  10. setup_suse_class,
  11. install_generic,
  12. setup_ubuntu_class,zsystem;
  13. type
  14. tatmail=class
  15. private
  16. libs:tlibs;
  17. distri:tdistriDetect;
  18. install:tinstall;
  19. source_folder,cmd:string;
  20. webserver_port:string;
  21. artica_admin:string;
  22. artica_password:string;
  23. ldap_suffix:string;
  24. mysql_server:string;
  25. mysql_admin:string;
  26. mysql_password:string;
  27. ldap_server:string;
  28. SYS:Tsystem;
  29. function SetConfigValues(key:string;value:string):boolean;
  30. public
  31. constructor Create();
  32. procedure Free;
  33. procedure xinstall();
  34. procedure PatchingLogonForm();
  35. function SetConfig():boolean;
  36. function CreateDatabase():boolean;
  37. END;
  38. implementation
  39. constructor tatmail.Create();
  40. begin
  41. distri:=tdistriDetect.Create();
  42. libs:=tlibs.Create;
  43. install:=tinstall.Create;
  44. SYS:=TSystem.Create();
  45. source_folder:='';
  46. webserver_port:=install.lighttpd.LIGHTTPD_LISTEN_PORT();
  47. artica_admin:=install.openldap.get_LDAP('admin');
  48. artica_password:=install.openldap.get_LDAP('password');
  49. ldap_suffix:=install.openldap.get_LDAP('suffix');
  50. ldap_server:=install.openldap.get_LDAP('server');
  51. mysql_server:=install.SYS.MYSQL_INFOS('mysql_server');
  52. mysql_admin:=install.SYS.MYSQL_INFOS('database_admin');
  53. mysql_password:=install.SYS.MYSQL_INFOS('password');
  54. end;
  55. //#########################################################################################
  56. procedure tatmail.Free();
  57. begin
  58. libs.Free;
  59. end;
  60. //#########################################################################################
  61. procedure tatmail.xinstall();
  62. var
  63. CODE_NAME:string;
  64. begin
  65. CODE_NAME:='APP_ATOPENMAIL';
  66. if DirectoryExists(ParamStr(2)) then source_folder:=ParamStr(2);
  67. install.INSTALL_STATUS('APP_ATOPENMAIL',10);
  68. install.INSTALL_STATUS('APP_ATOPENMAIL',30);
  69. install.INSTALL_STATUS(CODE_NAME,40);
  70. install.INSTALL_PROGRESS(CODE_NAME,'{downloading}');
  71. if length(source_folder)=0 then source_folder:=libs.COMPILE_GENERIC_APPS('atmailopen');
  72. if not DirectoryExists(source_folder) then begin
  73. writeln('Install atmailopen failed...');
  74. install.INSTALL_PROGRESS(CODE_NAME,'{failed}');
  75. install.INSTALL_STATUS('APP_ATOPENMAIL',110);
  76. exit;
  77. end;
  78. writeln('Install simple atmailopen extracted on "'+source_folder+'"');
  79. install.INSTALL_STATUS('APP_ATOPENMAIL',50);
  80. install.INSTALL_PROGRESS(CODE_NAME,'{installing}');
  81. install.INSTALL_STATUS('APP_ATOPENMAIL',60);
  82. writeln('Installing in /usr/share/atmailopen ....');
  83. ForceDirectories('/usr/share/atmailopen');
  84. fpsystem('/bin/cp -rf ' + source_folder + '/* /usr/share/atmailopen/');
  85. if not FileExists('/usr/share/atmailopen/libs/Atmail/Config.php.default') then begin
  86. writeln('Installing failed to stat /usr/share/atmailopen/libs/Atmail/Config.php.default');
  87. install.INSTALL_STATUS('APP_ATOPENMAIL',110);
  88. exit;
  89. end;
  90. fpsystem('/bin/chown -R www-data:www-data /usr/share/atmailopen');
  91. fpsystem('/bin/chmod -R 744 /usr/share/atmailopen');
  92. if DirectoryExists('/usr/share/artica-postfix/mail') then fpsystem('/bin/rm -f /usr/share/artica-postfix/mail');
  93. fpsystem('/bin/ln -s /usr/share/atmailopen /usr/share/artica-postfix/mail');
  94. PatchingLogonForm();
  95. if not SetConfig then begin
  96. install.INSTALL_STATUS('APP_ATOPENMAIL',110);
  97. exit;
  98. end;
  99. install.INSTALL_STATUS('APP_ATOPENMAIL',90);
  100. writeln('Creating database and table on '+ mysql_server + ' mysql server');
  101. if not CreateDatabase() then begin
  102. writeln('failed...');
  103. install.INSTALL_PROGRESS(CODE_NAME,'{failed}');
  104. install.INSTALL_STATUS('APP_ATOPENMAIL',110);
  105. exit;
  106. end;
  107. writeln('success');
  108. install.INSTALL_PROGRESS(CODE_NAME,'{installed}');
  109. install.INSTALL_STATUS('APP_ATOPENMAIL',100);
  110. end;
  111. //#########################################################################################
  112. procedure tatmail.PatchingLogonForm();
  113. var
  114. targetfile:string;
  115. l:TstringList;
  116. i:integer;
  117. found:boolean;
  118. tofind:string;
  119. begin
  120. writeln('Patching logon form for single imap server');
  121. targetfile:='/usr/share/atmailopen/html/login-light.html';
  122. tofind:='<input name="MailServer" type="text" class="logininput" id="MailServer">';
  123. found:=false;
  124. if not FileExists(targetfile) then begin
  125. writeln('unable to sat '+targetfile);
  126. exit;
  127. end;
  128. tofind:='<input name="MailServer" type="text" class="logininput" id="MailServer">';
  129. l:=TstringList.Create;
  130. l.LoadFromFile(targetfile);
  131. For i:=0 to l.Count-1 do begin
  132. if pos(tofind,l.Strings[i])>0 then begin
  133. writeln('Patching line ' + IntToStr(i));
  134. l.Strings[i]:=AnsiReplaceText(l.Strings[i],tofind,'<input name="MailServer" type="hidden" class="logininput" id="MailServer" value="127.0.0.1"><strong>Local</strong');
  135. found:=true;
  136. break;
  137. end;
  138. end;
  139. if found then l.SaveToFile(targetfile);
  140. l.free;
  141. end;
  142. //#########################################################################################
  143. function tatmail.SetConfig():boolean;
  144. var
  145. EnableVirtualDomainsInMailBoxes:integer;
  146. begin
  147. result:=false;
  148. EnableVirtualDomainsInMailBoxes:=0;
  149. if Not FileExists('/usr/share/atmailopen/libs/Atmail/Config.php') then begin
  150. if Not FileExists('/usr/share/atmailopen/libs/Atmail/Config.php.default') then begin
  151. writeln('Unable to stat /usr/share/atmailopen/libs/Atmail/Config.php.default');
  152. exit;
  153. end;
  154. fpsystem('/bin/cp -r /usr/share/atmailopen/libs/Atmail/Config.php.default /usr/share/atmailopen/libs/Atmail/Config.php');
  155. fpsystem('/bin/chown www-data:www-data /usr/share/atmailopen/libs/Atmail/Config.php');
  156. end;
  157. if Not FileExists('/usr/share/atmailopen/libs/Atmail/Config.php') then begin
  158. writeln('Unable to stat /usr/share/atmailopen/libs/Atmail/Config.php');
  159. exit;
  160. end;
  161. if not TryStrToInt(SYS.GET_INFO('EnableVirtualDomainsInMailBoxes'),EnableVirtualDomainsInMailBoxes) then EnableVirtualDomainsInMailBoxes:=0;
  162. writeln('Set default settings on Config.php');
  163. if not SetConfigValues('installed','1') then exit;
  164. if not SetConfigValues('ldap_server',ldap_server) then exit;
  165. if not SetConfigValues('base_dn',ldap_suffix) then exit;
  166. if not SetConfigValues('ldap_passwd',artica_password) then exit;
  167. if not SetConfigValues('sql_type','mysql') then exit;
  168. if not SetConfigValues('sql_user',mysql_admin) then exit;
  169. if not SetConfigValues('sql_pass',mysql_password) then exit;
  170. if not SetConfigValues('sql_host',mysql_server) then exit;
  171. if not SetConfigValues('install_dir','/usr/share/atmailopen') then exit;
  172. if not SetConfigValues('user_dir','/usr/share/atmailopen') then exit;
  173. if not SetConfigValues('GlobalAbook','1') then exit;
  174. if not SetConfigValues('mailserver','127.0.0.1') then exit;
  175. if not SetConfigValues('smtphost','127.0.0.1') then exit;
  176. if not SetConfigValues('login_rememberme','0') then exit;
  177. if not SetConfigValues('mailserver_auth',IntToStr(EnableVirtualDomainsInMailBoxes)) then exit;
  178. SetConfigValues('footer_msg',SYS.GET_INFO('ATMailFooterMsg'));
  179. result:=true;
  180. end;
  181. //#########################################################################################
  182. function tatmail.SetConfigValues(key:string;value:string):boolean;
  183. var
  184. targetfile:string;
  185. l:TstringList;
  186. i:integer;
  187. found:boolean;
  188. RegExpr:TRegExpr;
  189. newConfig:string;
  190. begin
  191. result:=false;
  192. found:=false;
  193. if not FileExists('/usr/share/atmailopen/libs/Atmail/Config.php') then exit;
  194. l:=TstringList.Create;
  195. RegExpr:=TRegExpr.Create;
  196. RegExpr.Expression:=key+'''\s+=>.+';
  197. newConfig:=''''+key+''' => '''+ value+''',';
  198. l.LoadFromFile('/usr/share/atmailopen/libs/Atmail/Config.php');
  199. For i:=0 to l.Count-1 do begin
  200. if RegExpr.Exec(l.Strings[i]) then begin
  201. l.Strings[i]:=newConfig;
  202. writeln('Starting......: @Mail Editing line ',i, ' for ',key);
  203. found:=true;
  204. break;
  205. end;
  206. end;
  207. if found then begin
  208. l.SaveToFile('/usr/share/atmailopen/libs/Atmail/Config.php');
  209. end else begin
  210. writeln('Unable to find ' + key);
  211. end;
  212. l.free;
  213. RegExpr.free;
  214. result:=found;
  215. end;
  216. //#########################################################################################
  217. function tatmail.CreateDatabase():boolean;
  218. var mysql_text:string;
  219. begin
  220. mysql_text:='/usr/share/atmailopen/install/atmail.mysql';
  221. if not FileExists(mysql_text) then begin
  222. writeln('Unable to stat '+ mysql_text);
  223. end;
  224. install.LOGS.Enable_echo:=true;
  225. if install.LOGS.IF_DATABASE_EXISTS('atmail') then begin
  226. writeln('Delete Database atmail');
  227. install.LOGS.QUERY_SQL(pChar('DROP DATABASE atmail;'),'');
  228. end;
  229. writeln('Creating database atmail and associated tables....');
  230. install.LOGS.EXECUTE_SQL_FILE(mysql_text,'atmail');
  231. result:=true;
  232. end;
  233. end.