"Fix permissions on files in the Drupal web directory.", 'arguments' => array( ), 'options' => array( ), 'examples' => array( 'drush permissions', 'drush perms', ), 'aliases' => array('perms'), // Just need the root directory. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT, ); return $items; } /** * Implementation of hook_drush_help(). * * This function is called whenever a drush user calls * 'drush help ' * * @param * A string with the help section (prepend with 'drush:') * * @return * A string with the help text for your command. */ function perms_drush_help($section) { switch ($section) { case 'drush:permissions': return dt("This command will Fix permissions on files in the Drupal web directory."); } } /* * Implementation of drush_hook_COMMAND_validate(). */ function drush_perms_permissions_validate() { $name = posix_getpwuid(posix_geteuid()); if ($name['name'] !== 'root') { return drush_set_error( 'ERROR_MUST_BE_ROOT', dt('You must be root to run this command by prefixing it with "sudo".') ); } } /** * Drush command callback. * * The function name should be same as command name but with dashes turned to * underscores and 'drush_commandfile_' prepended, where 'commandfile' is * taken from the file 'commandfile.drush.inc', which in this case is 'sandwich'. * Note also that a simplification step is also done in instances where * the commandfile name is the same as the beginning of the command name, * "drush_example_example_foo" is simplified to just "drush_example_foo". * To also implement a hook that is called before your command, implement * "drush_hook_pre_example_foo". For a list of all available hooks for a * given command, run drush in --debug mode. * * If for some reason you do not want your hook function to be named * after your command, you may define a 'callback' item in your command * object that specifies the exact name of the function that should be * called. However, the specified callback function must still begin * with "drush_commandfile_" (e.g. 'callback' => "drush_example_foo_execute"). * All hook functions are still called (e.g. drush_example_pre_foo_execute, * and so on.) * * In this function, all of Drupal's API is (usually) available, including * any functions you have added in your own modules/themes. * */ function drush_perms_permissions() { // Set some defaults. $perms_dirs = 0775; $perms_files = 0664; $perms_settings = 0460; $owner_user = 'www-data'; $owner_group = 'devs'; // Get the Drupal root. if (!$drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT')) { return drush_set_error( 'ERROR_CANNOT_GET_ROOT', dt('Cannot determine the Drupal root directory.') ); } // Create an iteration of files starting at the Drupal root. $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($drupal_root), RecursiveIteratorIterator::SELF_FIRST ); // Iterate through all of the files. foreach ($iterator as $current_file) { // Check file type. Is it a regular file or a directory? if($iterator->isFile()) { // Act differently if this is a settings.php file. if ($current_file->getFilename() == 'settings.php') { // Set desired permissions on settings.php files. chmod($current_file->getPathname(), $perms_settings); chown($current_file->getPathname(), $owner_user); chgrp($current_file->getPathname(), $owner_group); } else { // Set desired permissions on all other files. chmod($current_file->getPathname(), $perms_files); } } else if ($iterator->isDir()) { // Set permissions on the directory. chmod($current_file->getPathname(), $perms_dirs); } } }