From 77529ccdf10d22340558d3192d536f1b3ff04945 Mon Sep 17 00:00:00 2001
From: Zach Prezkuta <fermion@gmx.com>
Date: Thu, 6 Sep 2012 17:24:34 -0600
Subject: [PATCH] allow individual choice of mobile themes

---
 boot.php                                      |  9 ++++++
 include/auth.php                              |  3 +-
 include/oauth.php                             |  1 +
 include/security.php                          |  1 +
 mod/admin.php                                 | 22 ++++++++-------
 mod/community.php                             |  4 ++-
 mod/directory.php                             |  4 ++-
 mod/home.php                                  |  2 ++
 mod/login.php                                 |  5 +++-
 mod/manage.php                                |  1 +
 mod/register.php                              |  2 ++
 mod/search.php                                |  4 ++-
 mod/settings.php                              | 28 +++++++++++++++----
 view/field_themeselect.tpl                    |  2 +-
 view/settings_display.tpl                     |  1 +
 view/theme/frost-mobile/field_themeselect.tpl |  2 +-
 view/theme/frost/field_themeselect.tpl        |  2 +-
 17 files changed, 69 insertions(+), 24 deletions(-)

diff --git a/boot.php b/boot.php
index 74707fbc9..93942aa3f 100644
--- a/boot.php
+++ b/boot.php
@@ -1088,9 +1088,12 @@ if(! function_exists('profile_load')) {
 
 		$a->profile = $r[0];
 
+		$a->profile['mobile-theme'] = get_pconfig($profile_uid, 'system', 'mobile_theme');
+
 
 		$a->page['title'] = $a->profile['name'] . " @ " . $a->config['sitename'];
 		$_SESSION['theme'] = $a->profile['theme'];
+		$_SESSION['mobile-theme'] = $a->profile['mobile-theme'];
 
 		/**
 		 * load/reload current theme info
@@ -1510,6 +1513,12 @@ if(! function_exists('current_theme')) {
 		if($is_mobile) {
 			$system_theme = ((isset($a->config['system']['mobile-theme'])) ? $a->config['system']['mobile-theme'] : '');
 			$theme_name = ((isset($_SESSION) && x($_SESSION,'mobile-theme')) ? $_SESSION['mobile-theme'] : $system_theme);
+
+			if($theme_name === '---') {
+				// user has selected to have the mobile theme be the same as the normal one
+				$system_theme = '';
+				$theme_name = '';
+			}
 		}
 		if(!$is_mobile || ($system_theme === '' && $theme_name === '')) {
 			$system_theme = ((isset($a->config['system']['theme'])) ? $a->config['system']['theme'] : '');
diff --git a/include/auth.php b/include/auth.php
index cba6a67a7..f10704eda 100644
--- a/include/auth.php
+++ b/include/auth.php
@@ -10,14 +10,13 @@ function nuke_session() {
 	unset($_SESSION['administrator']);
 	unset($_SESSION['cid']);
 	unset($_SESSION['theme']);
+	unset($_SESSION['mobile-theme']);
 	unset($_SESSION['page_flags']);
 	unset($_SESSION['submanage']);
 	unset($_SESSION['my_url']);
 	unset($_SESSION['my_address']);
 	unset($_SESSION['addr']);
 	unset($_SESSION['return_url']);
-	unset($_SESSION['theme']);
-	unset($_SESSION['page_flags']);
 }
 
 
diff --git a/include/oauth.php b/include/oauth.php
index 2724dcf7c..103d4c2fa 100644
--- a/include/oauth.php
+++ b/include/oauth.php
@@ -145,6 +145,7 @@ class FKOAuth1 extends OAuthServer {
 		}
 		$_SESSION['uid'] = $record['uid'];
 		$_SESSION['theme'] = $record['theme'];
+		$_SESSION['mobile-theme'] = get_pconfig($record['uid'], 'system', 'mobile_theme');
 		$_SESSION['authenticated'] = 1;
 		$_SESSION['page_flags'] = $record['page-flags'];
 		$_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $record['nickname'];
diff --git a/include/security.php b/include/security.php
index 4621148cd..10bb692bb 100644
--- a/include/security.php
+++ b/include/security.php
@@ -6,6 +6,7 @@ function authenticate_success($user_record, $login_initial = false, $interactive
 
 	$_SESSION['uid'] = $user_record['uid'];
 	$_SESSION['theme'] = $user_record['theme'];
+	$_SESSION['mobile-theme'] = get_pconfig($user_record['uid'], 'system', 'mobile_theme');
 	$_SESSION['authenticated'] = 1;
 	$_SESSION['page_flags'] = $user_record['page-flags'];
 	$_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $user_record['nickname'];
diff --git a/mod/admin.php b/mod/admin.php
index cc13e27b1..db4d4cff2 100644
--- a/mod/admin.php
+++ b/mod/admin.php
@@ -329,11 +329,11 @@ function admin_page_site_post(&$a){
 	}
 	set_config('system','language', $language);
 	set_config('system','theme', $theme);
-        if ( $theme_mobile === '---' ) {
-            del_config('system','mobile-theme');
-        } else {
-    	    set_config('system','mobile-theme', $theme_mobile);
-        }
+	if ( $theme_mobile === '---' ) {
+		del_config('system','mobile-theme');
+	} else {
+		set_config('system','mobile-theme', $theme_mobile);
+	}
 	set_config('system','maximagesize', $maximagesize);
 	set_config('system','max_image_length', $maximagelength);
 	set_config('system','jpeg_quality', $jpegimagequality);
@@ -399,16 +399,18 @@ function admin_page_site(&$a) {
 	/* Installed themes */
 	$theme_choices = array();
 	$theme_choices_mobile = array();
-        $theme_choices_mobile["---"] = t("Don't apply a special theme for mobile devices.");
+	$theme_choices_mobile["---"] = t("No special theme for mobile devices");
 	$files = glob('view/theme/*');
 	if($files) {
 		foreach($files as $file) {
 			$f = basename($file);
 			$theme_name = ((file_exists($file . '/experimental')) ?  sprintf("%s - \x28Experimental\x29", $f) : $f);
-                        $theme_choices[$f] = $theme_name;
-                        if (file_exists($file . '/mobile')) {
-                            $theme_choices_mobile[$f] = $theme_name;
-                        }
+            if (file_exists($file . '/mobile')) {
+                $theme_choices_mobile[$f] = $theme_name;
+            }
+			else {
+                $theme_choices[$f] = $theme_name;
+			}
 		}
 	}
 	
diff --git a/mod/community.php b/mod/community.php
index 354f68528..4f6c3d3c9 100644
--- a/mod/community.php
+++ b/mod/community.php
@@ -1,8 +1,10 @@
 <?php
 
 function community_init(&$a) {
-	if(! local_user())
+	if(! local_user()) {
 		unset($_SESSION['theme']);
+		unset($_SESSION['mobile-theme']);
+	}
 
 
 }
diff --git a/mod/directory.php b/mod/directory.php
index 8a5685c9a..833a4f81c 100644
--- a/mod/directory.php
+++ b/mod/directory.php
@@ -9,8 +9,10 @@ function directory_init(&$a) {
 		$a->page['aside'] .= findpeople_widget();
 
 	}
-	else
+	else {
 		unset($_SESSION['theme']);
+		unset($_SESSION['mobile-theme']);
+	}
 
 
 }
diff --git a/mod/home.php b/mod/home.php
index 0320c1b39..6ed36b763 100644
--- a/mod/home.php
+++ b/mod/home.php
@@ -22,6 +22,8 @@ function home_content(&$a) {
 
 	if(x($_SESSION,'theme'))
 		unset($_SESSION['theme']);
+	if(x($_SESSION,'mobile-theme'))
+		unset($_SESSION['mobile-theme']);
 
 	$o .= '<h1>' . ((x($a->config,'sitename')) ? sprintf( t("Welcome to %s") ,$a->config['sitename']) : "" ) . '</h1>';
 	if(file_exists('home.html'))
diff --git a/mod/login.php b/mod/login.php
index 10b4d3001..d09fc1868 100644
--- a/mod/login.php
+++ b/mod/login.php
@@ -3,8 +3,11 @@
 function login_content(&$a) {
 	if(x($_SESSION,'theme'))
 		unset($_SESSION['theme']);
+	if(x($_SESSION,'mobile-theme'))
+		unset($_SESSION['mobile-theme']);
+
 	if(local_user())
 		goaway(z_root());
 	return login(($a->config['register_policy'] == REGISTER_CLOSED) ? false : true);
 
-}
\ No newline at end of file
+}
diff --git a/mod/manage.php b/mod/manage.php
index 96d420c78..4bc7a3bab 100644
--- a/mod/manage.php
+++ b/mod/manage.php
@@ -63,6 +63,7 @@ function manage_post(&$a) {
 	unset($_SESSION['administrator']);
 	unset($_SESSION['cid']);
 	unset($_SESSION['theme']);
+	unset($_SESSION['mobile-theme']);
 	unset($_SESSION['page_flags']);
 	unset($_SESSION['return_url']);
 	if(x($_SESSION,'submanage'))
diff --git a/mod/register.php b/mod/register.php
index de86808cb..6bf287d42 100644
--- a/mod/register.php
+++ b/mod/register.php
@@ -193,6 +193,8 @@ function register_content(&$a) {
 
 	if(x($_SESSION,'theme'))
 		unset($_SESSION['theme']);
+	if(x($_SESSION,'mobile-theme'))
+		unset($_SESSION['mobile-theme']);
 
 
 	$username     = ((x($_POST,'username'))     ? $_POST['username']     : ((x($_GET,'username'))     ? $_GET['username']              : ''));
diff --git a/mod/search.php b/mod/search.php
index 8f73b9244..300eb912c 100644
--- a/mod/search.php
+++ b/mod/search.php
@@ -50,8 +50,10 @@ function search_init(&$a) {
 		$a->page['aside'] .= search_saved_searches();
 
 	}
-	else
+	else {
 		unset($_SESSION['theme']);
+		unset($_SESSION['mobile-theme']);
+	}
 
 
 
diff --git a/mod/settings.php b/mod/settings.php
index c0244e697..a7b2791a9 100644
--- a/mod/settings.php
+++ b/mod/settings.php
@@ -236,17 +236,22 @@ function settings_post(&$a) {
 		check_form_security_token_redirectOnErr('/settings/display', 'settings_display');
 
 		$theme = ((x($_POST,'theme')) ? notags(trim($_POST['theme']))  : $a->user['theme']);
+		$mobile_theme = ((x($_POST,'mobile_theme')) ? notags(trim($_POST['mobile_theme']))  : '');
 		$nosmile = ((x($_POST,'nosmile')) ? intval($_POST['nosmile'])  : 0);  
 		$browser_update   = ((x($_POST,'browser_update')) ? intval($_POST['browser_update']) : 0);
 		$browser_update   = $browser_update * 1000;
 		if($browser_update < 10000)
-			$browser_update = 40000;
+			$browser_update = 10000;
 
 		$itemspage_network   = ((x($_POST,'itemspage_network')) ? intval($_POST['itemspage_network']) : 40);
 		if($itemspage_network > 100)
-					$itemspage_network = 40;
+			$itemspage_network = 100;
 
 
+		if($mobile_theme !== '') {
+			set_pconfig(local_user(),'system','mobile_theme',$mobile_theme);
+		}
+
 		set_pconfig(local_user(),'system','update_interval', $browser_update);
 		set_pconfig(local_user(),'system','itemspage_network', $itemspage_network);
 		set_pconfig(local_user(),'system','no_smilies',$nosmile);
@@ -497,10 +502,11 @@ function settings_post(&$a) {
 	require_once('include/profile_update.php');
 	profile_change();
 
-	$_SESSION['theme'] = $theme;
+	//$_SESSION['theme'] = $theme;
 	if($email_changed && $a->config['register_policy'] == REGISTER_VERIFY) {
 
 		// FIXME - set to un-verified, blocked and redirect to logout
+		// Why? Are we verifying people or email addresses?
 
 	}
 
@@ -704,6 +710,9 @@ function settings_content(&$a) {
 		$default_theme = get_config('system','theme');
 		if(! $default_theme)
 			$default_theme = 'default';
+		$default_mobile_theme = get_config('system','mobile-theme');
+		if(! $mobile_default_theme)
+			$mobile_default_theme = 'none';
 
 		$allowed_themes_str = get_config('system','allowed_themes');
 		$allowed_themes_raw = explode(',',$allowed_themes_str);
@@ -715,19 +724,27 @@ function settings_content(&$a) {
 
 		
 		$themes = array();
+		$mobile_themes = array("---" => t('No special theme for mobile devices'));
 		$files = glob('view/theme/*');
 		if($allowed_themes) {
 			foreach($allowed_themes as $th) {
 				$f = $th;
 				$is_experimental = file_exists('view/theme/' . $th . '/experimental');
 				$unsupported = file_exists('view/theme/' . $th . '/unsupported');
+				$is_mobile = file_exists('view/theme/' . $th . '/mobile');
 				if (!$is_experimental or ($is_experimental && (get_config('experimentals','exp_themes')==1 or get_config('experimentals','exp_themes')===false))){ 
 					$theme_name = (($is_experimental) ?  sprintf("%s - \x28Experimental\x29", $f) : $f);
-					$themes[$f]=$theme_name;
+					if($is_mobile) {
+						$mobile_themes[$f]=$theme_name;
+					}
+					else {
+						$themes[$f]=$theme_name;
+					}
 				}
 			}
 		}
 		$theme_selected = (!x($_SESSION,'theme')? $default_theme : $_SESSION['theme']);
+		$mobile_theme_selected = (!x($_SESSION,'mobile-theme')? $default_mobile_theme : $_SESSION['mobile-theme']);
 		
 		$browser_update = intval(get_pconfig(local_user(), 'system','update_interval'));
 		$browser_update = (($browser_update == 0) ? 40 : $browser_update / 1000); // default if not set: 40 seconds
@@ -753,7 +770,8 @@ function settings_content(&$a) {
 			'$baseurl' => $a->get_baseurl(true),
 			'$uid' => local_user(),
 		
-			'$theme'	=> array('theme', t('Display Theme:'), $theme_selected, '', $themes),
+			'$theme'	=> array('theme', t('Display Theme:'), $theme_selected, '', $themes, 'preview'),
+			'$mobile_theme'	=> array('mobile_theme', t('Mobile Theme:'), $mobile_theme_selected, '', $mobile_themes, ''),
 			'$ajaxint'   => array('browser_update',  t("Update browser every xx seconds"), $browser_update, t('Minimum of 10 seconds, no maximum')),
 			'$itemspage_network'   => array('itemspage_network',  t("Number of items to display per page:"), $itemspage_network, t('Maximum of 100 items')),
 			'$nosmile'	=> array('nosmile', t("Don't show emoticons"), $nosmile, ''),
diff --git a/view/field_themeselect.tpl b/view/field_themeselect.tpl
index 5847d8664..1b3ede36c 100644
--- a/view/field_themeselect.tpl
+++ b/view/field_themeselect.tpl
@@ -1,7 +1,7 @@
 	<script>$(function(){ previewTheme($("#id_$field.0")[0]); });</script>
 	<div class='field select'>
 		<label for='id_$field.0'>$field.1</label>
-		<select name='$field.0' id='id_$field.0' onchange="previewTheme(this);" >
+		<select name='$field.0' id='id_$field.0' {{ if $field.5=='preview' }}onchange="previewTheme(this);"{{ endif }} >
 			{{ for $field.4 as $opt=>$val }}<option value="$opt" {{ if $opt==$field.2 }}selected="selected"{{ endif }}>$val</option>{{ endfor }}
 		</select>
 		<span class='field_help'>$field.3</span>
diff --git a/view/settings_display.tpl b/view/settings_display.tpl
index 70895a1f8..24fc11027 100644
--- a/view/settings_display.tpl
+++ b/view/settings_display.tpl
@@ -4,6 +4,7 @@
 <input type='hidden' name='form_security_token' value='$form_security_token'>
 
 {{inc field_themeselect.tpl with $field=$theme }}{{endinc}}
+{{inc field_themeselect.tpl with $field=$mobile_theme }}{{endinc}}
 {{inc field_input.tpl with $field=$ajaxint }}{{endinc}}
 {{inc field_input.tpl with $field=$itemspage_network }}{{endinc}}
 {{inc field_checkbox.tpl with $field=$nosmile}}{{endinc}}
diff --git a/view/theme/frost-mobile/field_themeselect.tpl b/view/theme/frost-mobile/field_themeselect.tpl
index 612ab1030..3aa973d47 100644
--- a/view/theme/frost-mobile/field_themeselect.tpl
+++ b/view/theme/frost-mobile/field_themeselect.tpl
@@ -1,7 +1,7 @@
 
 	<div class='field select'>
 		<label for='id_$field.0'>$field.1</label>
-		<select name='$field.0' id='id_$field.0' onchange="previewTheme(this);" >
+		<select name='$field.0' id='id_$field.0' {{ if $field.5=='preview' }}onchange="previewTheme(this);"{{ endif }} >
 			{{ for $field.4 as $opt=>$val }}<option value="$opt" {{ if $opt==$field.2 }}selected="selected"{{ endif }}>$val</option>{{ endfor }}
 		</select>
 		<span class='field_help'>$field.3</span>
diff --git a/view/theme/frost/field_themeselect.tpl b/view/theme/frost/field_themeselect.tpl
index 612ab1030..3aa973d47 100644
--- a/view/theme/frost/field_themeselect.tpl
+++ b/view/theme/frost/field_themeselect.tpl
@@ -1,7 +1,7 @@
 
 	<div class='field select'>
 		<label for='id_$field.0'>$field.1</label>
-		<select name='$field.0' id='id_$field.0' onchange="previewTheme(this);" >
+		<select name='$field.0' id='id_$field.0' {{ if $field.5=='preview' }}onchange="previewTheme(this);"{{ endif }} >
 			{{ for $field.4 as $opt=>$val }}<option value="$opt" {{ if $opt==$field.2 }}selected="selected"{{ endif }}>$val</option>{{ endfor }}
 		</select>
 		<span class='field_help'>$field.3</span>