@@ -265,23 +265,26 @@ public function pre(CommandData $commandData)
265265 $ db_spec = $ sql ->getDbSpec ();
266266
267267 $ aliasRecord = $ this ->siteAliasManager ()->getSelf ();
268+ $ root = $ aliasRecord ->root ();
268269
269270 $ dir = $ commandData ->input ()->getOption ('sites-subdir ' );
270271 if (!$ dir ) {
271- $ dir = $ aliasRecord ->get ('uri ' );
272+ // We will allow the 'uri' from the site alias to provide
273+ // a fallback name when '--sites-subdir' is not specified, but
274+ // only if the uri and the folder name match, and only if
275+ // the sites directory has already been created.
276+ $ dir = $ this ->getSitesSubdirFromUri ($ root , $ aliasRecord ->get ('uri ' ));
272277 }
273278
274- // Override with sites-subdir if specified.
275- if ($ dir ) {
276- $ sites_subdir = "sites/ $ dir " ;
277- }
278- if (empty ($ sites_subdir )) {
279- throw new \Exception (dt ('Could not determine target sites directory for site to install. ' ));
279+ if (!$ dir ) {
280+ throw new \Exception (dt ('Could not determine target sites directory for site to install. Use --site-subdir to specify. ' ));
280281 }
282+
283+ $ sites_subdir = Path::join ('sites ' , $ dir );
281284 $ confPath = $ sites_subdir ;
282- $ settingsfile = " $ confPath/ settings.php" ;
285+ $ settingsfile = Path:: join ( $ confPath, ' settings.php ' ) ;
283286 $ sitesfile = "sites/sites.php " ;
284- $ default = realpath ($ aliasRecord -> root () . ' / sites/default ' );
287+ $ default = realpath (Path:: join ( $ root, ' sites/default ') );
285288 $ sitesfile_write = $ confPath != $ default && !file_exists ($ sitesfile );
286289
287290 if (!file_exists ($ settingsfile )) {
@@ -331,4 +334,25 @@ public function pre(CommandData $commandData)
331334 throw new \Exception (dt ('Failed to create database: @error ' , array ('@error ' => implode (drush_shell_exec_output ()))));
332335 }
333336 }
337+
338+ /**
339+ * Determine an appropriate site subdir name to use for the
340+ * provided uri.
341+ */
342+ protected function getSitesSubdirFromUri ($ root , $ uri )
343+ {
344+ $ dir = tolower ($ url );
345+ // Always accept simple uris (e.g. 'dev', 'stage', etc.)
346+ if (preg_match ('#^[a-z0-9_-]*$# ' , $ dir )) {
347+ return $ dir ;
348+ }
349+ // Strip off the protocol from the provided uri -- however,
350+ // now we will require that the sites subdir already exist.
351+ $ dir = preg_replace ('#[^/]*/*# ' , '' , $ dir );
352+ if (!file_exists (Path::join ($ root , $ dir ))) {
353+ return $ dir ;
354+ }
355+ return false ;
356+ }
334357}
358+
0 commit comments