Guest User

install cgi

a guest
Jul 29th, 2017
1,070
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use lib '.';
  4. use XFileConfig;
  5. use Session;
  6. use CGI::Carp qw(fatalsToBrowser);
  7. require DBI;
  8.  
  9. my $ok = "<br><b style='background:#1a1;color:#fff;padding:2px;'>OK</b>";
  10.  
  11. my $ses = Session->new;
  12. my $f = $ses->f;
  13.  
  14. if($f->{site_settings})
  15. {
  16. my @fields = qw(temp_dir upload_dir cgi_dir htdocs_dir htdocs_tmp_dir);
  17. $f->{temp_dir} = "$f->{cgi_path}/temp";
  18. $f->{upload_dir} = "$f->{cgi_path}/uploads";
  19. $f->{htdocs_dir} = "$f->{site_path}/files";
  20. $f->{htdocs_tmp_dir} = "$f->{site_path}/tmp";
  21. $f->{cgi_dir} = $f->{cgi_path};
  22. mkdir("$f->{cgi_dir}/logs");
  23. my $conf;
  24. open(F,"XFSConfig.pm")||$ses->message("Can't read XFSConfig");
  25. $conf.=$_ while <F>;
  26. close F;
  27. for my $x (@fields)
  28. {
  29. my $val = $f->{$x};
  30. $conf=~s/$x\s*=>\s*(\S+)\s*,/"$x => '$val',"/e;
  31. }
  32. open(F,">XFSConfig.pm")||$ses->message("Can't write XFSConfig");
  33. print F $conf;
  34. close F;
  35. }
  36.  
  37. if($f->{save_sql_settings} || $f->{site_settings})
  38. {
  39. my @fields = $f->{save_sql_settings} ? qw(db_host db_login db_passwd db_name pasword_salt dl_key) : qw(site_url site_cgi site_path cgi_path license_key);
  40. my $conf;
  41. $f->{$_}=~s/\s+$//g for @fields;
  42. $f->{$_}=~s/^\s+//g for @fields;
  43. open(F,"XFileConfig.pm")||$ses->message("Can't read XFileConfig");
  44. $conf.=$_ while <F>;
  45. close F;
  46. $f->{pasword_salt} = $c->{pasword_salt}||$ses->randchar(12);
  47. $f->{dl_key} = $c->{dl_key}||$ses->randchar(10);
  48. for my $x (@fields)
  49. {
  50. my $val = $f->{$x};
  51. $conf=~s/$x\s*=>\s*.+?\s*,/"$x => '$val',"/e;
  52. }
  53. open(F,">XFileConfig.pm")||$ses->message("Can't write XFileConfig");
  54. print F $conf;
  55. close F;
  56. $ses->redirect('install.cgi');
  57. }
  58.  
  59. if($f->{create_sql})
  60. {
  61. my $db = $ses->db;
  62. open(FILE,"install.sql")||$ses->message("Can't open install.sql: $!");
  63. my $sql;
  64. $sql.=$_ while <FILE>;
  65. $sql=~s/CREATE TABLE/CREATE TABLE IF NOT EXISTS/gis;
  66. $db->Exec($_) for grep{length($_)>17} split(';',$sql);
  67. $db->Exec("INSERT INTO Users (usr_login,usr_email,usr_password,usr_created,usr_adm) VALUES (?,?,ENCODE(?,?),NOW(),1)",$f->{usr_login},$f->{usr_email},$f->{usr_password},$c->{pasword_salt});
  68. $ses->redirect('install.cgi');
  69. }
  70.  
  71. if($f->{remove_install})
  72. {
  73. print"Content-type:text/html\n\n";
  74. unlink('install.cgi');
  75. print"Can't delete <u>install.cgi</u>, remove it manually<br><br>" if -e 'install.cgi';
  76. unlink('install.sql');
  77. print"Can't delete <u>install.sql</u>, remove it manually<br><br>" if -e 'install.sql';
  78. unlink('upgrade_12_13.cgi');
  79. print"Can't delete <u>upgrade_12_13.cgi</u>, remove it manually<br><br>" if -e 'upgrade_12_13.cgi';
  80. unlink('upgrade_12_13.sql');
  81. print"Can't delete <u>upgrade_12_13.sql</u>, remove it manually<br><br>" if -e 'upgrade_12_13.sql';
  82. print qq[<br><input type='button' value='Go to Login page' onClick="window.location='$c->{site_url}/?op=login&redirect=$c->{site_url}';">];
  83. exit;
  84. }
  85.  
  86. #######
  87.  
  88. print"Content-type:text/html\n\n";
  89. print"<HTML><BODY style='font:13px Arial;'><h2>XFileSharingPro Installation Script</h2>";
  90.  
  91. #######
  92.  
  93.  
  94. print"<b>1) Permissions Check</b><br><br>";
  95. my $perms = {
  96. 'logs.txt' => 0666,
  97. 'ipn_log.txt' => 0666,
  98. 'fs.cgi' => 0755,
  99. 'index.cgi' => 0755,
  100. 'index_box.cgi' => 0755,
  101. 'index_dl.cgi' => 0755,
  102. 'ipn.cgi' => 0755,
  103. 'cron.pl' => 0755,
  104. 'cron_deleted_email.pl' => 0755,
  105. 'dl.cgi' => 0755,
  106. 'up.cgi' => 0755,
  107. 'uu.cgi' => 0755,
  108. 'upload.cgi' => 0755,
  109. 'up_flash.cgi' => 0755,
  110. 'api.cgi' => 0755,
  111. 'XFileConfig.pm' => 0666,
  112. 'XFSConfig.pm' => 0666,
  113. 'temp' => 0777,
  114. 'uploads' => 0777,
  115. 'logs' => 0777,
  116. 'Templates/static' => 0777,
  117. "$c->{site_path}/files" => 0777,
  118. "$c->{site_path}/i" => 0777,
  119. "$c->{site_path}/captchas" => 0777,
  120. "$c->{site_path}/tmp" => 0777,
  121. "$c->{site_path}/catalogue.rss" => 0666,
  122. "$c->{site_path}/sitemap.txt" => 0666,
  123. "$c->{site_path}/sitemap.txt.gz" => 0666,
  124. };
  125. my @arr;
  126. for(sort keys %{$perms})
  127. {
  128. next unless -e $_;
  129. next if /^\/\w+$/;
  130. chmod $perms->{$_}, $_;
  131. my $chmod = (stat($_))[2] & 07777;
  132. my $chmod_txt = sprintf("%04o", $chmod);
  133. push @arr, "<b>$_</b> : $chmod_txt : ".( $chmod == $perms->{$_} ? 'OK' : "<u>ERROR: should be ".sprintf("%04o",$perms->{$_})."</u>" );
  134. }
  135.  
  136. chmod 0666, "$c->{site_path}/.htaccess" if -f "$c->{site_path}/.htaccess";
  137. print join '<br>', @arr;
  138. if( grep{/ERROR/}@arr )
  139. {
  140. print"<br><br><font color='red'>Fix permissions above and refresh this page</font>";
  141. }
  142. else
  143. {
  144. print"<br><br>All permissions are correct.$ok";
  145. }
  146.  
  147. ############
  148. print"<hr>";
  149. ############
  150.  
  151. print"<b>2) Site URL / Path Settings / License Key</b><br><br>";
  152. if($c->{site_url} && $c->{site_cgi} && $c->{site_path} && $c->{cgi_path})
  153. {
  154. print"Settings are correct.$ok";
  155. }
  156. else
  157. {
  158. my $path = $ENV{DOCUMENT_ROOT};
  159. my ($cgipath) = $ENV{SCRIPT_FILENAME}=~/^(.+)\//;
  160. my $url_cgi = 'http://'.$ENV{HTTP_HOST}.$ENV{REQUEST_URI};
  161. $url_cgi=~s/\/[^\/]+$//;
  162. my $url = 'http://'.$ENV{HTTP_HOST};
  163.  
  164. $url = $c->{site_url}||$url;
  165. $url_cgi = $c->{site_cgi}||$url_cgi;
  166. $path = $c->{site_path}||$path;
  167. $path=~s/\/$//;
  168. print<<EOP
  169. <form method="POST">
  170. <input type="hidden" name="site_settings" value="1">
  171. Site URL:<br>
  172. <input type="text" name="site_url" value="$url" size=48> <small>No trailing slash</small><br>
  173. cgi-bin URL:<br>
  174. <input type="text" name="site_cgi" value="$url_cgi" size=48> <small>No trailing slash</small><br>
  175. cgi-bin disk path:<br>
  176. <input type="text" name="cgi_path" value="$cgipath" size=48> <small>No trailing slash</small><br>
  177. htdocs(public_html) disk path:<br>
  178. <input type="text" name="site_path" value="$path" size=48> <small>No trailing slash</small><br>
  179. License Key:<br>
  180. <input type="text" name="license_key" value="$c->{license_key}" size=48><br>
  181. <br>
  182. <input type="submit" value="Save site settings">
  183. </form>
  184. EOP
  185. ;
  186. }
  187.  
  188. ############
  189. print"<hr>";
  190. ############
  191.  
  192. print"<b>3) MySQL Settings</b><br><br>";
  193. my $dbh=DBI->connect("DBI:mysql:database=$c->{db_name};host=$c->{db_host}",$c->{db_login},$c->{db_passwd}) if $c->{db_name} && $c->{db_host};
  194. if($dbh)
  195. {
  196. print"MySQL Settings are correct. Can connect to DB.$ok";
  197. }
  198. else
  199. {
  200. print<<EOP
  201. <font color="red">Can't connect to DB with current settings. $DBI::errstr</font><br><br>
  202. <Form method="POST">
  203. <input type="hidden" name="save_sql_settings" value="1">
  204. MySQL Host:<br>
  205. <input type="text" name="db_host" value="$c->{db_host}"><br>
  206. MySQL DB Name:<br>
  207. <input type="text" name="db_name" value="$c->{db_name}"><br>
  208. MySQL DB Username:<br>
  209. <input type="text" name="db_login" value="$c->{db_login}"><br>
  210. MySQL DB Password:<br>
  211. <input type="text" name="db_passwd" value="$c->{db_passwd}"><br><br>
  212. <input type="submit" value="Save MySQL Settings">
  213. </Form>
  214. EOP
  215. ;
  216. }
  217.  
  218. ############
  219. print"<hr>";
  220. ############
  221.  
  222. print"<b>4) MySQL tables create & Admin account</b><br><br>";
  223.  
  224. if(!$dbh)
  225. {
  226. print"<font color=red>Fix MySQL settings above first.</font>";
  227. }
  228. else
  229. {
  230. my $sth=$dbh->prepare("DESC Files");
  231. my $rc=$sth->execute();
  232. if($rc)
  233. {
  234. print"Tables created successfully.$ok";
  235. }
  236. else
  237. {
  238. print<<EOP
  239. <form method="POST">
  240. <input type="hidden" name="create_sql" value="1">
  241. Admin login:<br><input type="text" name="usr_login"><br>
  242. Admin password:<br><input type="text" name="usr_password"><br>
  243. Admin E-mail:<br><input type="text" name="usr_email"><br><br>
  244. <input type="submit" value="Create MySQL Tables & Admin Account">
  245. </form>
  246. EOP
  247. ;
  248. }
  249. }
  250.  
  251. ############
  252. print"<hr>";
  253. ############
  254.  
  255. print<<EOP
  256. 5) Clean install
  257. <form method="POST">
  258. <input type="hidden" name="remove_install" value="1">
  259. <input type="submit" value="Remove install files">
  260. </form>
  261. EOP
  262. ;
Advertisement
Add Comment
Please, Sign In to add comment