diff --git a/sites/all/modules/unl/unl.module b/sites/all/modules/unl/unl.module index 2bb1ca00ed53dc809659731309ce095e5148b9b5..19f749866292c6b049c3265d9158c9e97080c6a6 100644 --- a/sites/all/modules/unl/unl.module +++ b/sites/all/modules/unl/unl.module @@ -606,21 +606,8 @@ function unl_cron() { $sites_to_create = array(); foreach ($data as $site) { - $full_department = $site->department; - - $department = $full_department; - $department = strtr($department, array(' ' => '')); - $department = strtolower($department); - $department = preg_replace('/[^[a-z0-9]/', '', $department); - - $site_name = $site->site_name; - $site_name = strtr($site_name, array(' ' => '')); - $site_name = strtolower($site_name); - $site_name = preg_replace('/[^[a-z0-9]/', '', $site_name); - - $path = $department . '/' . $site_name; - - $db_prefix = unl_create_db_prefix($site); + $path = unl_sanitize_url_part($site->department) . '/' . unl_sanitize_url_part($site->site_name); + $db_prefix = unl_create_db_prefix($site->site_name); $sites_to_create[] = array( 'site_path' => $path, @@ -630,7 +617,7 @@ function unl_cron() { 'site_admin' => $site->site_admin ? $site->site_admin : '', 'migration_url' => $site->migration_url ? $site->migration_url : '', 'migration_path' => $site->migration_path ? $site->migration_path : '', - 'department' => $full_department, + 'department' => $site->department, ); db_set_active('wdn_registry'); @@ -660,30 +647,44 @@ function unl_cron() { } // Creates a db_prefix short enough to not go over MySQL's max table name length -function unl_create_db_prefix($site) { +function unl_create_db_prefix($site_name) { $parent_prefix = '_' . $GLOBALS['databases']['default']['default']['prefix']; - $site_name = strtolower($site->site_name); - $site_name = preg_replace('/[^[a-z0-9]/', '', $site_name); - $site_name = explode(' ', $site_name); + $site_name = strtolower($site_name); + $site_name = preg_replace('/[^[a-z0-9]/', '_', $site_name); + $site_name = explode('_', $site_name); foreach ($site_name as $site_word) { $site_words[$site_word] = strlen($site_word); } do { - $db_prefix = ''; + $db_prefix = array(); $found = FALSE; foreach ($site_words as $site_word => $length) { - $db_prefix .= substr($site_word, 0, $length); + $db_prefix[] = substr($site_word, 0, $length); if (max($site_words) == $length && !$found) { $found = TRUE; $site_words[$site_word] = $length-1; } } - } while (strlen($db_prefix . $parent_prefix) > 32); + $db_prefix = implode('_', $db_prefix); + $db_prefix = preg_replace('/_+/', '_', $db_prefix); + $db_prefix = preg_replace('/(^_)|(_$)/', '', $db_prefix); + } while (strlen($db_prefix . '_' . $parent_prefix) > 32); return $db_prefix; } +/** + * Custom function to sanitize user created URLs + */ +function unl_sanitize_url_part($url_part) { + $url_part = preg_replace('/[^a-z0-9]/', '-', $url_part); + $url_part = preg_replace('/-+/', '-', $url_part); + $url_part = preg_replace('/(^-)|(-$)/', '', $url_part); + + return $url_part; +} + function unl_get_alternate_base_uris() { $shared_prefix = unl_get_shared_db_prefix(); if (is_array($GLOBALS['databases']['default']['default']['prefix'])) { diff --git a/sites/all/modules/unl/unl_site_creation.php b/sites/all/modules/unl/unl_site_creation.php index 21936c8a248e5b21e43babedf2dfd751caa539e0..892e7e148dbcce223936d7b895661b6c54d14bd8 100644 --- a/sites/all/modules/unl/unl_site_creation.php +++ b/sites/all/modules/unl/unl_site_creation.php @@ -63,13 +63,17 @@ function unl_site_create_submit($form, &$form_state) { $site_path = $form_state['values']['site_path']; $clean_url = $form_state['values']['clean_url']; + $db_prefix = unl_create_db_prefix($site_path); + + $site_path = explode('/', $site_path); + foreach (array_keys($site_path) as $i) { + $site_path[$i] = unl_sanitize_url_part($site_path[$i]); + } + $site_path = implode('/', $site_path); $uri = url($site_path, array('absolute' => TRUE, 'https' => FALSE)); $clean_url = intval($clean_url); - $db_prefix = explode('/', $site_path); - $db_prefix = implode('_', $db_prefix); - db_insert('unl_sites')->fields(array( 'site_path' => $site_path, 'uri' => $uri, @@ -262,8 +266,7 @@ function unl_site_remove($site_id) { } $uri = $uri[0]; - $sites_subdir = _unl_get_sites_subdir($uri); - $sites_subdir = strtr($sites_subdir, array('/' => '.')); + $sites_subdir = unl_get_sites_subdir($uri); $sites_subdir = DRUPAL_ROOT . '/sites/' . $sites_subdir; $sites_subdir = realpath($sites_subdir); @@ -292,17 +295,6 @@ function unl_site_remove($site_id) { return TRUE; } -function _unl_get_sites_subdir($uri) { - $path_parts = parse_url($uri); - if (substr($path_parts['host'], -7) == 'unl.edu') { - $path_parts['host'] = 'unl.edu'; - } - $sites_subdir = $path_parts['host'] . $path_parts['path']; - $sites_subdir = strtr($sites_subdir, array('/' => '.')); - - return $sites_subdir; -} - function unl_aliases_page() { $page = array(); $page[] = drupal_get_form('unl_site_alias_create');