View source
<?php
function mobile_theme_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/help#mobile_theme':
$output .= '<p>' . t('Adds the ability to choose which theme is used when the user visits the site using a mobile device.') . '</p>';
$output .= theme('item_list', array(
'title' => t('Instructions'),
'type' => 'ol',
'items' => array(
t('Visit the <a href="@appearance">Appearance settings</a>.', array(
'@appearance' => url('admin/appearance/settings'),
)),
t('Choose the mobile theme that will be used when the user is on a mobile device.'),
t('You might want to install one of the alternative device detection method as outlined below.'),
),
));
$output .= theme('item_list', array(
'title' => t('Alternative detection methods'),
'items' => array(
'Browscap' => array(
'data' => t('Browscap'),
'children' => array(
t('Install the <a href="@browscap">Browscap</a> Drupal module.', array(
'@browscap' => 'http://drupal.org/project/browscap',
)),
t('Select Browscap in the <a href="@appearance">Appearance settings</a>.', array(
'@appearance' => url('admin/appearance/settings'),
)),
),
),
'Mobile Device Detect' => array(
'data' => t('Mobile Device Detect'),
'children' => array(
t('Visit <a href="@detectmobilebrowsers">http://detectmobilebrowsers.mobi</a>.', array(
'@detectmobilebrowsers' => 'http://detectmobilebrowsers.mobi',
)),
t('<a href="@download">Download the code</a> and extract it to the Mobile Theme module directory.', array(
'@download' => url('http://detectmobilebrowsers.mobi/?dl'),
)),
t('You should end up with <code>@path</code>.', array(
'@path' => drupal_get_path('module', 'mobile_theme') . '/mobile_device_detect.php',
)),
t('It is recommended you <a href="@upgrade">upgrade</a> if on a commercial site.', array(
'@upgrade' => drupal_get_path('module', 'mobile_theme') . '/upgrade.php',
)),
t('Select Mobile Device Detect in the <a href="@appearance">Appearance settings</a>.', array(
'@appearance' => url('admin/appearance/settings'),
)),
),
),
),
));
}
return $output;
}
function mobile_theme_custom_theme() {
$method = variable_get('mobile_theme_detection', 'mobile_theme_detect_php');
if (!function_exists($method)) {
$method = "mobile_theme_detect_php";
}
$mobile_device = $method();
if ($mobile_device) {
$theme = variable_get('mobile_theme_selection', 'default');
if ($theme != 'default') {
return $theme;
}
}
}
function mobile_theme_mobile_theme_detection() {
$detection_methods = array(
'mobile_theme_detect_php' => t('PHP'),
'mobile_theme_detect_getbrowser' => t('get_browser'),
'mobile_theme_detect_subdomain' => t('!m Subdomain', array(
'!m' => variable_get('mobile_theme_subdomain', 'm.'),
)),
);
if (module_exists('browscap')) {
$detection_methods['mobile_theme_detect_browscap'] = t('Browscap');
}
if (file_exists(drupal_get_path('module', 'mobile_theme') . '/mobile_device_detect.php')) {
$detection_methods['mobile_theme_detect_mobiledevicedetect'] = t('Mobile Device Detect');
}
return $detection_methods;
}
function mobile_theme_detect_getbrowser() {
$browser = get_browser(NULL, TRUE);
if (isset($browser['ismobiledevice'])) {
return $browser['ismobiledevice'];
}
}
function mobile_theme_detect_php() {
$mobile_browser = 0;
if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
$mobile_browser++;
}
if (isset($_SERVER['HTTP_ACCEPT']) && (strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'application/vnd.wap.xhtml+xml') > 0 || isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']))) {
$mobile_browser++;
}
$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
$mobile_agents = array(
'w3c ',
'acs-',
'alav',
'alca',
'amoi',
'audi',
'avan',
'benq',
'bird',
'blac',
'blaz',
'brew',
'cell',
'cldc',
'cmd-',
'dang',
'doco',
'eric',
'hipt',
'inno',
'ipaq',
'java',
'jigs',
'kddi',
'keji',
'leno',
'lg-c',
'lg-d',
'lg-g',
'lge-',
'maui',
'maxo',
'midp',
'mits',
'mmef',
'mobi',
'mot-',
'moto',
'mwbp',
'nec-',
'newt',
'noki',
'oper',
'palm',
'pana',
'pant',
'phil',
'play',
'port',
'prox',
'qwap',
'sage',
'sams',
'sany',
'sch-',
'sec-',
'send',
'seri',
'sgh-',
'shar',
'sie-',
'siem',
'smal',
'smar',
'sony',
'sph-',
'symb',
't-mo',
'teli',
'tim-',
'tosh',
'tsm-',
'upg1',
'upsi',
'vk-v',
'voda',
'wap-',
'wapa',
'wapi',
'wapp',
'wapr',
'webc',
'winw',
'winw',
'xda ',
'xda-',
);
if (in_array($mobile_ua, $mobile_agents)) {
$mobile_browser++;
}
if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']), 'OperaMini') > 0) {
$mobile_browser++;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows') > 0) {
$mobile_browser = 0;
}
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'windows phone') > 0) {
$mobile_browser++;
}
return $mobile_browser > 0;
}
function mobile_theme_detect_browscap() {
$browser = browscap_get_browser(NULL);
if (isset($browser['ismobiledevice'])) {
return $browser['ismobiledevice'] === TRUE;
}
}
function mobile_theme_detect_subdomain() {
$m = variable_get('mobile_theme_subdomain', 'm.');
return substr($_SERVER['HTTP_HOST'], 0, strlen($m)) === $m;
}
function mobile_theme_detect_mobiledevicedetect() {
if (module_load_include('php', 'mobile_theme', 'mobile_device_detect') != FALSE) {
return mobile_device_detect();
}
else {
drupal_set_message(t('<em>Mobile Device Detect</em> is not installed. Visit the <a href="@adminhelpmobiletheme">documentation on Mobile Theme</a> for more installation instructions.', array(
'@adminhelpmobiletheme' => url('admin/help/mobile_theme'),
)), 'warning');
return FALSE;
}
}
function mobile_theme_form_system_theme_settings_alter(&$form, $form_state, $form_id) {
if ($form['var']['#value'] == 'theme_settings') {
$form['mobile_theme'] = array(
'#type' => 'fieldset',
'#prefix' => '<div class="theme-settings-right">',
'#suffix' => '</div>',
'#title' => t('Mobile theme'),
'#description' => t('Configure how the site reacts to a mobile device.'),
'#weight' => -4,
);
$form['mobile_theme']['mobile_theme_detection'] = array(
'#type' => 'select',
'#title' => t('Detection method'),
'#description' => t('Choose which method will be used to detect mobile devices. Visit the <a href="@help">help documentation</a> for information on how to add other device detection methods.', array(
'@help' => url('admin/help/mobile_theme'),
)),
'#options' => module_invoke_all('mobile_theme_detection'),
'#default_value' => variable_get('mobile_theme_detection', 'mobile_theme_detect_php'),
);
$themes = array(
'default' => t('Default'),
);
$options = list_themes();
foreach ($options as $name => $attr) {
if ($attr->status) {
$themes[$name] = $attr->info['name'];
}
}
$form['mobile_theme']['mobile_theme_selection'] = array(
'#type' => 'select',
'#title' => 'Mobile theme',
'#description' => t('The theme to use when serving a mobile device.'),
'#options' => $themes,
'#default_value' => variable_get('mobile_theme_selection', 'default'),
);
$form['#submit'][] = 'mobile_theme_settings_submit';
}
}
function mobile_theme_settings_submit($form, $form_state) {
if (isset($form_state['values']['mobile_theme_selection'])) {
variable_set('mobile_theme_selection', $form_state['values']['mobile_theme_selection']);
}
if (isset($form_state['values']['mobile_theme_detection'])) {
variable_set('mobile_theme_detection', $form_state['values']['mobile_theme_detection']);
}
}