install 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/php
  2. <?php
  3. echo "[NOTE] Make sure the directories you install to are R+W accessible by your web server.\n";
  4. $user_info = posix_getpwuid(posix_geteuid());
  5. echo "Please enter the directory you wish for tCMS' private data files to be stored under.\n";
  6. echo "Default [" . $user_info['dir'] . "/.tCMS]: ";
  7. $input = get_string_from_stdin();
  8. if( empty( $input ) ) {
  9. $private_base = $user_info['dir'] . "/.tCMS";
  10. } else {
  11. # At least *try* to get the real path. If not, can't do much other than trust it works.
  12. $private_base = realpath( dirname( $input ) ) . "/" . basename( $input );
  13. }
  14. echo "Please enter the directory you wish for tCMS' public data files to be stored under.\n";
  15. echo "Default [" . $user_info['dir'] . "/public_html]: ";
  16. $input = get_string_from_stdin();
  17. if( empty( $input ) ) {
  18. $public_base = $user_info['dir'] . "/public_html";
  19. } else {
  20. $public_base = realpath( dirname( $input ) ) . "/" . basename( $input );
  21. }
  22. # Make sure our webserver (and my horrible code) knows how to find your custom directory
  23. if( $private_base != $user_info['dir'] . "/.tCMS" ) {
  24. echo "[WARN]: For tCMS to know where this directory we install to is, we will write the following\n";
  25. echo " into your document root for the default/virtual host you control on your Web Server:\n";
  26. echo " '$public_base/basedir'\n";
  27. echo " Please add an HTACCESS or similar blocker to prevent public acess to this file if you\n";
  28. echo " wish to prevent this information disclosure. Please press [enter] to continue.";
  29. get_string_from_stdin(); # Mostly just to make the user confirm they read this.
  30. }
  31. while( empty($username) ) {
  32. echo "Please enter in the name you'd like to login to tCMS as (you can add more users later): ";
  33. $username = get_string_from_stdin();
  34. }
  35. while( empty($password) ) {
  36. echo "Please enter a password for the user: ";
  37. $password = get_string_from_stdin(1);
  38. }
  39. $wd = realpath( dirname( __FILE__ ) . "/../" );
  40. echo "[INFO] Installing private data to $private_base now...\n";
  41. $files = [ 'lib', 'templates' ];
  42. install( $private_base, $wd, $files );
  43. ensure_directories_exist( [ "$private_base/blog", "$private_base/microblog", "$private_base/conf" ] );
  44. echo "[INFO] Writing user configuration and generating 4096 bit RSA keypair now...\n";
  45. $openssl_config = [
  46. 'private_key_bits' => 4096,
  47. 'digest_alg' => "sha512",
  48. 'private_key_type' => OPENSSL_KEYTYPE_RSA,
  49. ];
  50. $cryptkeeper = openssl_pkey_new( $openssl_config );
  51. openssl_pkey_export( $cryptkeeper, $privkey, $password );
  52. $pubkey = openssl_pkey_get_details($cryptkeeper);
  53. $pubkey = $pubkey["key"];
  54. file_put_contents( "$private_base/conf/users.json", json_encode( [ $username => [ 'pubkey' => $pubkey ] ] ) );
  55. echo "*** PLEASE COPY THE BELOW PRIVATE KEY AND SAVE IT SOMEWHERE SAFE!!!! ***\n";
  56. echo "*** THIS IS IMPORTANT AS THIS KEY WILL BE USED FOR YOUR LOGIN! ***\n\n";
  57. echo "$privkey\n\n";
  58. echo "Please press [ENTER] to continue.";
  59. get_string_from_stdin();
  60. echo "[INFO] Installing public data to $public_base now...\n";
  61. $files = [ 'css', 'fileshare', 'img', 'index.php', 'sys' ];
  62. install( $public_base, $wd, $files );
  63. ensure_directories_exist( [ "$public_base/css/custom", "$public_base/img/icon/custom" ] );
  64. if( $private_base != $user_info['dir'] . "/.tCMS" ) {
  65. file_put_contents( "$public_base/basedir", $private_base );
  66. }
  67. echo "[INFO] Done.\n";
  68. exit(0);
  69. function get_string_from_stdin($noecho=0) {
  70. if( $noecho ) system('stty -echo');
  71. $STDIN = fopen( "php://stdin", "r" );
  72. $input = fgets( $STDIN, 4096 ); # Not gonna read something longer than the maximum path length for ext
  73. $input = rtrim( $input ); # Trim any trailing spaces
  74. fclose( $STDIN );
  75. if( $noecho ) system('stty echo');
  76. return $input;
  77. }
  78. function install( $to, $from, $files2copy ) {
  79. if( file_exists( $to ) && !is_dir( $to ) ) {
  80. echo "[FATAL] $to already exists but is not a directory! Stopping here.\n";
  81. exit(1);
  82. }
  83. if ( !file_exists( $to ) ) mkdir( $to );
  84. # XXX Prolly won't work on directories
  85. foreach ( $files2copy as $file ) {
  86. # Referencing it by path as some systems have aliases on this command
  87. $cmd = "/bin/cp -rf '$from/$file' '$to/'";
  88. echo "exec `$cmd`\n";
  89. $out = shell_exec($cmd);
  90. echo $out;
  91. }
  92. return;
  93. }
  94. function ensure_directories_exist( $dirs ) {
  95. foreach ( $dirs as $dir ) {
  96. if ( !file_exists( $dir ) ) {
  97. echo "exec `/bin/mkdir '$dir'`\n";
  98. mkdir( $dir );
  99. }
  100. }
  101. return;
  102. }
  103. ?>