|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Statamic\Addons\Permissions\Commands; |
| 4 | + |
| 5 | +use Statamic\Extend\Command; |
| 6 | +use Statamic\API\Path; |
| 7 | + |
| 8 | +class FixCommand extends Command |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The name and signature of the console command. |
| 12 | + * |
| 13 | + * @var string |
| 14 | + */ |
| 15 | + protected $signature = 'permissions:fix'; |
| 16 | + |
| 17 | + /** |
| 18 | + * The console command description. |
| 19 | + * |
| 20 | + * @var string |
| 21 | + */ |
| 22 | + protected $description = 'Fix file permissions!'; |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a new command instance. |
| 26 | + */ |
| 27 | + public function __construct() |
| 28 | + { |
| 29 | + parent::__construct(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Execute the console command. |
| 34 | + * |
| 35 | + * @return mixed |
| 36 | + */ |
| 37 | + public function handle() |
| 38 | + { |
| 39 | + // Get the Configuration or Defaults |
| 40 | + $site_root = escapeshellarg(Path::makeFull('/')); |
| 41 | + $shell_path = getenv('PERMISSIONS_SHELL_PATH') ? getenv('PERMISSIONS_SHELL_PATH') : '/usr/bin'; |
| 42 | + $user = getenv('PERMISSIONS_USER') ? getenv('PERMISSIONS_USER') : 'www-data'; |
| 43 | + $group = getenv('PERMISSIONS_GROUP') ? getenv('PERMISSIONS_GROUP') : 'www-data'; |
| 44 | + $folder_permissions = getenv('PERMISSIONS_FOLDERS') ? getenv('PERMISSIONS_FOLDERS') : '775'; |
| 45 | + $file_permissions = getenv('PERMISSIONS_FILES') ? getenv('PERMISSIONS_FILES') : '664'; |
| 46 | + $lock_statamic = getenv('PERMISSIONS_LOCK_STATAMIC') ? getenv('PERMISSIONS_LOCK_STATAMIC') : false; |
| 47 | + $locked_user = getenv('PERMISSIONS_LOCKED_USER') ? getenv('PERMISSIONS_LOCKED_USER') : 'root'; |
| 48 | + $locked_group = getenv('PERMISSIONS_LOCKED_GROUP') ? getenv('PERMISSIONS_LOCKED_GROUP') : 'root'; |
| 49 | + |
| 50 | + // Set Up Enviroment for the Shell |
| 51 | + putenv('SHELL=' . $shell_path); |
| 52 | + |
| 53 | + // Fix File Ownership |
| 54 | + $this->info('Fixing ownership, this may take a while ...'); |
| 55 | + shell_exec('chown -R ' . $user . ':' . $group .' ' . $site_root); |
| 56 | + |
| 57 | + // Fix Directory Permissions |
| 58 | + $this->info('Fixing directory permissions, this may take a while ...'); |
| 59 | + shell_exec('find ' . $site_root . ' -type d -exec chmod ' . $folder_permissions . ' {} \;'); |
| 60 | + |
| 61 | + // Fix File Permissions |
| 62 | + $this->info('Fixing file permissions, this may take a while ...'); |
| 63 | + shell_exec('find ' . $site_root . ' -type f -exec chmod ' . $file_permissions . ' {} \;'); |
| 64 | + |
| 65 | + // Lock Statamic |
| 66 | + if ($lock_statamic) { |
| 67 | + $this->info('Securing git folder, this may take a while ...'); |
| 68 | + shell_exec('chown -R ' . $locked_user . ':' . $locked_group .' ' . $site_root . '.git'); |
| 69 | + shell_exec('chmod -R 770 ' . $site_root . '.git'); |
| 70 | + |
| 71 | + $this->info('Securing Statamic directory, this may take a while ...'); |
| 72 | + shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'statamic'); |
| 73 | + shell_exec('find ' . $site_root . 'statamic -type d -exec chmod 755 {} \;'); |
| 74 | + shell_exec('find ' . $site_root . 'statamic -type f -exec chmod 640 {} \;'); |
| 75 | + shell_exec('find ' . $site_root . 'statamic/resources/dist -type f -exec chmod 644 {} \;'); |
| 76 | + |
| 77 | + $this->info('Securing addons directory, this may take a while ...'); |
| 78 | + shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/addons'); |
| 79 | + shell_exec('find ' . $site_root . 'site/addons -type d -exec chmod 755 {} \;'); |
| 80 | + shell_exec('find ' . $site_root . 'site/addons -type f -exec chmod 640 {} \;'); |
| 81 | + shell_exec('find ' . $site_root . 'site/addons/*/resources/assets -type f -exec chmod 644 {} \;'); |
| 82 | + |
| 83 | + $this->info('Securing content directory, this may take a while ...'); |
| 84 | + shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/content'); |
| 85 | + shell_exec('find ' . $site_root . 'site/content -type d -exec chmod 770 {} \;'); |
| 86 | + shell_exec('find ' . $site_root . 'site/content -type f -exec chmod 660 {} \;'); |
| 87 | + |
| 88 | + $this->info('Securing database directory, this may take a while ...'); |
| 89 | + shell_exec('chown -R ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'site/database'); |
| 90 | + shell_exec('find ' . $site_root . 'site/database -type d -exec chmod 770 {} \;'); |
| 91 | + shell_exec('find ' . $site_root . 'site/database -type f -exec chmod 660 {} \;'); |
| 92 | + |
| 93 | + $this->info('Securing settings directory, this may take a while ...'); |
| 94 | + shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/settings'); |
| 95 | + shell_exec('find ' . $site_root . 'site/settings -type d -exec chmod 770 {} \;'); |
| 96 | + shell_exec('find ' . $site_root . 'site/settings -type f -exec chmod 660 {} \;'); |
| 97 | + |
| 98 | + $this->info('Securing storage directory, this may take a while ...'); |
| 99 | + shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/storage'); |
| 100 | + shell_exec('find ' . $site_root . 'site/storage -type d -exec chmod 770 {} \;'); |
| 101 | + shell_exec('find ' . $site_root . 'site/storage -type f -exec chmod 660 {} \;'); |
| 102 | + |
| 103 | + $this->info('Securing tests directory, this may take a while ...'); |
| 104 | + shell_exec('chown -R ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'site/tests'); |
| 105 | + shell_exec('find ' . $site_root . 'site/tests -type d -exec chmod 770 {} \;'); |
| 106 | + shell_exec('find ' . $site_root . 'site/tests -type f -exec chmod 660 {} \;'); |
| 107 | + |
| 108 | + $this->info('Securing themes directory, this may take a while ...'); |
| 109 | + shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/themes'); |
| 110 | + shell_exec('find ' . $site_root . 'site/themes -type d -exec chmod 755 {} \;'); |
| 111 | + shell_exec('find ' . $site_root . 'site/themes -type f -exec chmod 640 {} \;'); |
| 112 | + shell_exec('find ' . $site_root . 'site/themes/*/assets -type f -exec chmod 644 {} \;'); |
| 113 | + shell_exec('find ' . $site_root . 'site/themes/*/dist -type f -exec chmod 644 {} \;'); |
| 114 | + shell_exec('find ' . $site_root . 'site/themes/*/css -type f -exec chmod 644 {} \;'); |
| 115 | + shell_exec('find ' . $site_root . 'site/themes/*/js -type f -exec chmod 644 {} \;'); |
| 116 | + shell_exec('find ' . $site_root . 'site/themes/*/vendor -type f -exec chmod 644 {} \;'); |
| 117 | + |
| 118 | + $this->info('Securing users directory, this may take a while ...'); |
| 119 | + shell_exec('chown -R ' . $locked_user . ':' . $group .' ' . $site_root . 'site/users'); |
| 120 | + shell_exec('find ' . $site_root . 'site/users -type d -exec chmod 770 {} \;'); |
| 121 | + shell_exec('find ' . $site_root . 'site/users -type f -exec chmod 660 {} \;'); |
| 122 | + |
| 123 | + $this->info('Securing local directory, this may take a while ...'); |
| 124 | + shell_exec('find ' . $site_root . 'local -type d -exec chmod 770 {} \;'); |
| 125 | + shell_exec('find ' . $site_root . 'local -type f -exec chmod 660 {} \;'); |
| 126 | + |
| 127 | + $this->info('Securing base files, this may take a while ...'); |
| 128 | + shell_exec('chown ' . $locked_user . ':' . $group .' ' . $site_root . '.env'); |
| 129 | + shell_exec('chmod 640 ' . $site_root . '.env'); |
| 130 | + shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . '.gitignore'); |
| 131 | + shell_exec('chmod 660 ' . $site_root . '.gitignore'); |
| 132 | + shell_exec('chown ' . $locked_user . ':' . $group .' ' . $site_root . '.htaccess'); |
| 133 | + shell_exec('chmod 754 ' . $site_root . '.htaccess'); |
| 134 | + shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'favicon.ico'); |
| 135 | + shell_exec('chown ' . $locked_user . ':' . $group .' ' . $site_root . 'index.php'); |
| 136 | + shell_exec('chmod 754 ' . $site_root . 'index.php'); |
| 137 | + shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'phpunit.xml'); |
| 138 | + shell_exec('chmod 660 ' . $site_root . 'phpunit.xml'); |
| 139 | + shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'please'); |
| 140 | + shell_exec('chmod 660 ' . $site_root . 'please'); |
| 141 | + shell_exec('chown ' . $locked_user . ':' . $locked_group .' ' . $site_root . 'robots.txt'); |
| 142 | + } |
| 143 | + |
| 144 | + // Display an All Clear Message :) |
| 145 | + $this->info('All done! If the script reported any "Operation not permitted" errors, try running it again with sudo.'); |
| 146 | + } |
| 147 | +} |
0 commit comments