install 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env php
  2. <?php
  3. umask(007);
  4. $user_info = posix_getpwuid(posix_geteuid());
  5. $wd = realpath( dirname( __FILE__ ) . "/../" );
  6. // Look for the $private_base and $public_base. Assume /var/www/ if no homedir.
  7. $homedir = ( $user_info['dir'] ? $user_info['dir'] : '/var/www/' );
  8. $private_base = ( file_exists( "$homedir/.tCMS_basedir" ) ) ? file_get_contents( "$homedir/.tCMS_basedir" ) : "";
  9. if( empty( $private_base ) ) {
  10. echo "[NOTE] Make sure the directories you install to are R+W accessible by your web server.\n";
  11. echo " On most shared hosts (cPanel, etc.) this is fine to leave in your home directory.\n\n";
  12. echo "Please enter the directory you wish for tCMS' private data files to be stored under.\n";
  13. echo "Default [" . $user_info['dir'] . "/.tCMS]: ";
  14. $input = get_string_from_stdin();
  15. if( empty( $input ) ) {
  16. $private_base = $user_info['dir'] . "/.tCMS";
  17. } else {
  18. # At least *try* to get the real path. If not, can't do much other than trust it works.
  19. $private_base = realpath( dirname( $input ) ) . "/" . basename( $input );
  20. }
  21. }
  22. echo "Please enter the directory you for tCMS' public data files.\n";
  23. echo "Default [" . $user_info['dir'] . "/public_html]: ";
  24. $input = get_string_from_stdin();
  25. if( empty( $input ) ) {
  26. $public_base = $user_info['dir'] . "/public_html";
  27. } else {
  28. $public_base = realpath( dirname( $input ) ) . "/" . basename( $input );
  29. }
  30. echo "[INFO] Installing private data to $private_base now...\n";
  31. $files = [ 'lib', 'templates' ];
  32. install( $private_base, $wd, $files );
  33. ensure_directories_exist( [ "$private_base/blog", "$private_base/microblog", "$private_base/conf" ] );
  34. # Make sure our webserver (and my horrible code) knows how to find your custom directory
  35. if( !file_exists($user_info['dir'] . "/.tCMS_basedir") ) {
  36. echo "[INFO]: For tCMS to know where the directory we installed to was, we will write the following\n";
  37. echo " into your home directory:\n";
  38. echo " '" . $user_info["dir"] . "/.tCMS_basedir'\n";
  39. echo " Please press [enter] to continue.";
  40. get_string_from_stdin(); # Mostly just to make the user confirm they read this.
  41. file_put_contents( $user_info['dir'] . "/.tCMS_basedir", realpath($private_base) );
  42. }
  43. # Create admin user
  44. if( !file_exists("$private_base/conf/users.json") ) {
  45. while( empty($username) ) {
  46. echo "Please enter in the name you'd like to login to tCMS as administrator (you can add more users later): ";
  47. $username = get_string_from_stdin();
  48. }
  49. while( empty($password) ) {
  50. echo "Please enter a password for the user: ";
  51. $password = get_string_from_stdin(1);
  52. $auth_hash = password_hash( $password, PASSWORD_DEFAULT );
  53. }
  54. echo "\n[INFO] Writing user configuration now...\n";
  55. file_put_contents( "$private_base/conf/users.json", json_encode( [ $username => [ 'auth_hash' => $auth_hash ] ] ) );
  56. }
  57. echo "[INFO] Installing public data to $public_base now...\n";
  58. $files = [ 'themed', 'fileshare', 'index.php', 'sys' ];
  59. install( $public_base, $wd, $files );
  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. ?>