install 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env php
  2. <?php
  3. $user_info = posix_getpwuid(posix_geteuid());
  4. $wd = realpath( dirname( __FILE__ ) . "/../" );
  5. // Look for the $private_base and $public_base. Assume /var/www/ if no homedir.
  6. $homedir = ( $user_info['dir'] ? $user_info['dir'] : '/var/www/' );
  7. $private_base = ( file_exists( "$homedir/.tCMS_basedir" ) ) ? file_get_contents( "$homedir/.tCMS_basedir" ) : "";
  8. if( empty( $private_base ) ) {
  9. echo "[NOTE] Make sure the directories you install to are R+W accessible by your web server.\n";
  10. echo " On most shared hosts (cPanel, etc.) this is fine to leave in your home directory.\n\n";
  11. echo "Please enter the directory you wish for tCMS' private data files to be stored under.\n";
  12. echo "Default [" . $user_info['dir'] . "/.tCMS]: ";
  13. $input = get_string_from_stdin();
  14. if( empty( $input ) ) {
  15. $private_base = $user_info['dir'] . "/.tCMS";
  16. } else {
  17. # At least *try* to get the real path. If not, can't do much other than trust it works.
  18. $private_base = realpath( dirname( $input ) ) . "/" . basename( $input );
  19. }
  20. }
  21. echo "Please enter the directory you for tCMS' public data files.\n";
  22. echo "Default [" . $user_info['dir'] . "/public_html]: ";
  23. $input = get_string_from_stdin();
  24. if( empty( $input ) ) {
  25. $public_base = $user_info['dir'] . "/public_html";
  26. } else {
  27. $public_base = realpath( dirname( $input ) ) . "/" . basename( $input );
  28. }
  29. echo "[INFO] Installing private data to $private_base now...\n";
  30. $files = [ 'lib', 'templates' ];
  31. install( $private_base, $wd, $files );
  32. ensure_directories_exist( [ "$private_base/blog", "$private_base/microblog", "$private_base/conf" ] );
  33. # Make sure our webserver (and my horrible code) knows how to find your custom directory
  34. if( !file_exists($user_info['dir'] . "/.tCMS_basedir") ) {
  35. echo "[INFO]: For tCMS to know where the directory we installed to was, we will write the following\n";
  36. echo " into your home directory:\n";
  37. echo " '" . $user_info["dir"] . "/.tCMS_basedir'\n";
  38. echo " Please press [enter] to continue.";
  39. get_string_from_stdin(); # Mostly just to make the user confirm they read this.
  40. file_put_contents( $user_info['dir'] . "/.tCMS_basedir", $private_base );
  41. }
  42. # Create admin user
  43. if( !file_exists("$private_base/conf/users.json") ) {
  44. while( empty($username) ) {
  45. echo "Please enter in the name you'd like to login to tCMS as administrator (you can add more users later): ";
  46. $username = get_string_from_stdin();
  47. }
  48. while( empty($password) ) {
  49. echo "Please enter a password for the user: ";
  50. $password = get_string_from_stdin(1);
  51. $auth_hash = password_hash( $password, PASSWORD_DEFAULT );
  52. }
  53. echo "\n[INFO] Writing user configuration now...\n";
  54. file_put_contents( "$private_base/conf/users.json", json_encode( [ $username => [ 'auth_hash' => $auth_hash ] ] ) );
  55. }
  56. echo "[INFO] Installing public data to $public_base now...\n";
  57. $files = [ 'css', 'fileshare', 'img', 'index.php', 'sys' ];
  58. install( $public_base, $wd, $files );
  59. ensure_directories_exist( [ "$public_base/css/custom", "$public_base/img/icon/custom" ] );
  60. echo "[INFO] Done.\n";
  61. exit(0);
  62. function get_string_from_stdin($noecho=0) {
  63. if( $noecho ) system('stty -echo');
  64. $STDIN = fopen( "php://stdin", "r" );
  65. $input = fgets( $STDIN, 4096 ); # Not gonna read something longer than the maximum path length for ext
  66. $input = rtrim( $input ); # Trim any trailing spaces
  67. fclose( $STDIN );
  68. if( $noecho ) system('stty echo');
  69. return $input;
  70. }
  71. function install( $to, $from, $files2copy ) {
  72. if( empty($to) || empty($from) || empty($files2copy) ) die("Bad arguments passed to 'install' function");
  73. if( file_exists( $to ) && !is_dir( $to ) ) {
  74. echo "[FATAL] $to already exists but is not a directory! Stopping here.\n";
  75. exit(1);
  76. }
  77. if ( !file_exists( $to ) ) mkdir( $to );
  78. # XXX Prolly won't work on directories
  79. foreach ( $files2copy as $file ) {
  80. # Referencing it by path as some systems have aliases on this command
  81. $cmd = "/bin/cp -rf '$from/$file' '$to/'";
  82. echo "exec `$cmd`\n";
  83. $out = shell_exec($cmd);
  84. echo $out;
  85. }
  86. return;
  87. }
  88. function ensure_directories_exist( $dirs ) {
  89. if( empty( $dirs ) ) die("Bad arguments passed to 'ensure_directories_exist' function");
  90. foreach ( $dirs as $dir ) {
  91. if ( !file_exists( $dir ) ) {
  92. echo "exec `/bin/mkdir '$dir'`\n";
  93. mkdir( $dir );
  94. }
  95. }
  96. return;
  97. }
  98. ?>