From d81572f79656908a290adc913d4e89d5b2cb0a5f Mon Sep 17 00:00:00 2001 From: pdontthink Date: Wed, 10 Sep 2008 06:48:00 +0000 Subject: [PATCH] RPC requests should use their own template sets with different content-type. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@13274 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- class/template/Template.class.php | 111 +++++++++++------ config/conf.pl | 136 +++++++++++++++++---- config/config_default.php | 15 +++ include/init.php | 16 ++- include/load_prefs.php | 19 ++- include/options/display.php | 2 +- src/redirect.php | 3 +- templates/default/rpc_response_error.tpl | 35 ------ templates/default/rpc_response_success.tpl | 35 ------ 9 files changed, 229 insertions(+), 143 deletions(-) delete mode 100644 templates/default/rpc_response_error.tpl delete mode 100644 templates/default/rpc_response_success.tpl diff --git a/class/template/Template.class.php b/class/template/Template.class.php index e02bc05f..f6718178 100644 --- a/class/template/Template.class.php +++ b/class/template/Template.class.php @@ -213,7 +213,7 @@ class Template // get template file cache // - $this->template_file_cache = Template::cache_template_file_hierarchy(); + $this->template_file_cache = Template::cache_template_file_hierarchy($template_set_id); } @@ -319,6 +319,42 @@ class Template } + /** + * Determine what the RPC template set is. + * + * NOTE that if the default setting cannot be found in the + * main SquirrelMail configuration settings that the value + * of $default is returned. + * + * @param string $default The template set ID to use if + * the default setting cannot be + * found in SM config (optional; + * defaults to "default_rpc"). + * + * @return string The ID of the RPC template set. + * + * @static + * + */ + function get_rpc_template_set($default='default_rpc') { + +// FIXME: do we want to place any restrictions on the ID such as +// making sure no slashes included? + + // values are in main SM config file + // + global $rpc_templateset; + $rpc_templateset = (!isset($rpc_templateset) + ? $default : $rpc_templateset); + + // FIXME: note that it is possible for this to + // point to an invalid (nonexistent) template set + // and that error will not be caught here + // + return $rpc_templateset; + + } + /** * Allow template set to override plugin configuration by either * adding or removing plugins. @@ -645,6 +681,14 @@ class Template * constructed and stored in session before being returned * to the caller. * + * @param string $template_set_id The template set for which + * the cache should be built. + * This function will save more + * than one set's files, so it + * may be called multiple times + * with different values for this + * argument. When regenerating, + * all set caches are dumped. * @param boolean $regenerate_cache When TRUE, the file hierarchy * is reloaded and stored fresh * (optional; default FALSE). @@ -655,12 +699,12 @@ class Template * empty - no additional files). * * @return array Template file hierarchy array, whose keys - * are all the template file names (with path - * information relative to the template set's - * base directory, e.g., "css/style.css") - * found in all parent template sets including - * the ultimate fall-back template set. - * Array values are sub-arrays with the + * are all the template file names for the given + * template set ID (with path information relative + * to the template set's base directory, e.g., + * "css/style.css") found in all parent template + * sets including the ultimate fall-back template + * set. Array values are sub-arrays with the * following key-value pairs: * * PATH -- file path, relative to SM_PATH @@ -670,7 +714,8 @@ class Template * @static * */ - function cache_template_file_hierarchy($regenerate_cache=FALSE, + function cache_template_file_hierarchy($template_set_id, + $regenerate_cache=FALSE, $additional_files=array()) { sqGetGlobalVar('template_file_hierarchy', $template_file_hierarchy, @@ -679,51 +724,39 @@ class Template if ($regenerate_cache) unset($template_file_hierarchy); - if (!empty($template_file_hierarchy)) { + if (!empty($template_file_hierarchy[$template_set_id])) { // have to add additional files if given before returning // if (!empty($additional_files)) { - $template_file_hierarchy = array_merge($template_file_hierarchy, - $additional_files); + $template_file_hierarchy[$template_set_id] + = array_merge($template_file_hierarchy[$template_set_id], + $additional_files); + sqsession_register($template_file_hierarchy, 'template_file_hierarchy'); } - return $template_file_hierarchy; + return $template_file_hierarchy[$template_set_id]; } // nothing in cache apparently, so go build it now // - // FIXME: not sure if there is any possibility that - // this could be called when $sTemplateID has - // yet to be defined... throw error for now, - // but if the error occurs, it's a coding error - // rather than a configuration error - // - global $sTemplateID; - if (empty($sTemplateID)) { - - trigger_error('Template set ID unknown', E_USER_ERROR); + $template_file_hierarchy[$template_set_id] = Template::catalog_template_files($template_set_id); - } else { - - $template_file_hierarchy = Template::catalog_template_files($sTemplateID); - - // additional files, if any - // - if (!empty($additional_files)) { - $template_file_hierarchy = array_merge($template_file_hierarchy, - $additional_files); - } - - sqsession_register($template_file_hierarchy, - 'template_file_hierarchy'); + // additional files, if any + // + if (!empty($additional_files)) { + $template_file_hierarchy[$template_set_id] + = array_merge($template_file_hierarchy[$template_set_id], + $additional_files); + } - return $template_file_hierarchy; + sqsession_register($template_file_hierarchy, + 'template_file_hierarchy'); - } + return $template_file_hierarchy[$template_set_id]; } @@ -885,7 +918,9 @@ class Template ) ); $this->template_file_cache - = $this->cache_template_file_hierarchy(FALSE, $file_list); + = $this->cache_template_file_hierarchy($this->template_set_id, + FALSE, + $file_list); return TRUE; } diff --git a/config/conf.pl b/config/conf.pl index 4f3206ea..84bd2ab4 100755 --- a/config/conf.pl +++ b/config/conf.pl @@ -368,6 +368,14 @@ while ( $line = ) { } close FILE; +# RPC template sets aren't included in user interface skin list, +# so add the one from the config file here +# +if ($rpc_templateset =~ /_rpc$/) { + $templateset_name[$#templateset_name + 1] = $rpc_templateset; + $templateset_id[$#templateset_id + 1] = $rpc_templateset; +} + # FIXME: unknown introduction date $useSendmail = 'false' if ( lc($useSendmail) ne 'true' ); $sendmail_path = "/usr/sbin/sendmail" if ( !$sendmail_path ); @@ -3154,26 +3162,44 @@ sub command_templates { print " "; } if ( $templateset_id[$count] eq $templateset_fallback ) { - print "f "; + print "f"; + } else { + print " "; + } + if ( $templateset_id[$count] eq $rpc_templateset ) { + print "r "; } else { print " "; } if ( $count < 10 ) { print " "; } + if ( $count < 100 ) { + print " "; + } $name = $templateset_name[$count]; - $num_spaces = 35 - length($name); - for ( $i = 0 ; $i < $num_spaces ; $i++ ) { - $name = $name . " "; + + # present RPC template sets differently + # + if ( $templateset_id[$count] =~ /_rpc$/ ) { + $name = $name . " (not shown in user interface; used for RPC interface only)"; + } else { + + $num_spaces = 35 - length($name); + for ( $i = 0 ; $i < $num_spaces ; $i++ ) { + $name = $name . " "; + } + $name = $name . "($templateset_id[$count])"; + } - print " $count. $name"; - print "($templateset_id[$count])\n"; + print " $count. $name\n"; $count++; - } - print "\n d = default template set\n" - . " f = fallback template set\n\n"; + } + print "\n d = default template set\n" + . " f = fallback template set\n" + . " r = RPC template set\n\n"; $menu_text = ".-------------------------------------.\n" . "| t (detect template set) |\n" @@ -3181,6 +3207,7 @@ sub command_templates { . "| - N (remove template set) |\n" . "| m N (mark default template set) |\n" . "| f N (set fallback template set) |\n" + . "| r N (set RPC template set) |\n" . "| l (list template sets/skins) |\n" . "| d (done) |\n" . "|-------------------------------------|\n" @@ -3205,26 +3232,44 @@ sub command_templates { print " "; } if ( $templateset_id[$count] eq $templateset_fallback ) { - print "f "; + print "f"; + } else { + print " "; + } + if ( $templateset_id[$count] eq $rpc_templateset ) { + print "r "; } else { print " "; } if ( $count < 10 ) { print " "; } + if ( $count < 100 ) { + print " "; + } $name = $templateset_name[$count]; - $num_spaces = 35 - length($name); - for ( $i = 0 ; $i < $num_spaces ; $i++ ) { - $name = $name . " "; + + # present RPC template sets differently + # + if ( $templateset_id[$count] =~ /_rpc$/ ) { + $name = $name . " (not shown in user interface; used for RPC interface only)"; + } else { + + $num_spaces = 35 - length($name); + for ( $i = 0 ; $i < $num_spaces ; $i++ ) { + $name = $name . " "; + } + $name = $name . "($templateset_id[$count])"; + } - print " $count. $name"; - print "($templateset_id[$count])\n"; + print " $count. $name\n"; $count++; } print "\n d = default template set\n" - . " f = fallback template set\n\n"; + . " f = fallback template set\n" + . " r = RPC template set\n\n"; # mark default template set # @@ -3236,6 +3281,10 @@ sub command_templates { print "Cannot set default template set to $input. That template set does not exist.\n"; $templateset_default = $old_def; } + if ( $templateset_default =~ /_rpc$/ ) { + print "Cannot set default template set to $input. That template set is intended for the RPC interface only.\n"; + $templateset_default = $old_def; + } # set fallback template set # @@ -3247,6 +3296,25 @@ sub command_templates { print "Cannot set fallback template set to $input. That template set does not exist.\n"; $templateset_fallback = $old_def; } + if ( $templateset_fallback =~ /_rpc$/ ) { + print "Cannot set fallback template set to $input. That template set is intended for the RPC interface only.\n"; + $templateset_fallback = $old_def; + } + + # set RPC template set + # + } elsif ( $input =~ /^\s*r\s*[0-9]+/i ) { + $old_def = $rpc_templateset; + $input =~ s/^\s*r\s*//; + $rpc_templateset = $templateset_id[$input]; + if ( $rpc_templateset =~ /^\s*$/ ) { + print "Cannot set RPC template set to $input. That template set does not exist.\n"; + $rpc_templateset = $old_def; + } + if ( $rpc_templateset !~ /_rpc$/ ) { + print "Cannot set fallback template set to $input. That template set is not intended for the RPC interface.\n"; + $rpc_templateset = $old_def; + } # add template set # @@ -3284,15 +3352,22 @@ sub command_templates { if ( $found != 1) { print "** Found template set: $filename\n"; $def = $files[$cnt]; - $def =~ s/_/ /g; - $def = lc($def); - #$def =~ s/(^\w+)/ucfirst $1/eg; - #$def =~ s/(\s+)(\w+)/$1 . ucfirst $2/eg; - $def =~ s/(^\w+)|(\s+)(\w+)/ucfirst $1 . $2 . ucfirst $3/eg; - print " What is it's name (as shown to your users)? [$def]: "; - $nm = ; - $nm =~ s/^\s+|\s+$|[\n\r]//g; - if ( $nm eq '' ) { $nm = $def; } + + # no user-friendly names needed for RPC template sets + # + if ( $def =~ /_rpc$/ ) { + $nm = $def; + } else { + $def = lc($def); + $def =~ s/_/ /g; + #$def =~ s/(^\w+)/ucfirst $1/eg; + #$def =~ s/(\s+)(\w+)/$1 . ucfirst $2/eg; + $def =~ s/(^\w+)|(\s+)(\w+)/ucfirst $1 . $2 . ucfirst $3/eg; + print " What is it's name (as shown to your users)? [$def]: "; + $nm = ; + $nm =~ s/^\s+|\s+$|[\n\r]//g; + if ( $nm eq '' ) { $nm = $def; } + } $templateset_id[ $#templateset_id + 1 ] = $filename; $templateset_name[ $#templateset_name + 1 ] = $nm; } @@ -3306,6 +3381,7 @@ sub command_templates { print " Removing \"$filename\" (template set directory not found)\n"; if ( $templateset_default eq $filename ) { $templateset_default = 'default'; } if ( $templateset_fallback eq $filename ) { $templateset_fallback = 'default'; } + if ( $rpc_templateset eq $filename ) { $rpc_templateset = 'default_rpc'; } $offset = 0; @new_templateset_name = (); @new_templateset_id = (); @@ -3345,6 +3421,8 @@ sub command_templates { print "You cannot remove the default template set!\n"; } elsif ( $templateset_id[$rem_num] eq $templateset_fallback ) { print "You cannot remove the fallback template set!\n"; + } elsif ( $templateset_id[$rem_num] eq $rpc_templateset ) { + print "You cannot remove the RPC template set!\n"; } else { $count = 0; @new_templateset_name = (); @@ -4741,7 +4819,15 @@ sub save_data { if ( $templateset_fallback eq '' ) { $templateset_fallback = 'default'; } print CF "\$templateset_fallback = '$templateset_fallback';\n"; + if ( $rpc_templateset eq '' ) { $rpc_templateset = 'default_rpc'; } + print CF "\$rpc_templateset = '$rpc_templateset';\n"; + for ( $count = 0 ; $count <= $#templateset_name ; $count++ ) { + + # don't include RPC template sets + # + if ( $templateset_id[$count] =~ /_rpc$/ ) { next; } + print CF "\$aTemplateSet[$count]['ID'] = '" . $templateset_id[$count] . "';\n"; # escape theme name so it can contain single quotes. $esc_name = $templateset_name[$count]; diff --git a/config/config_default.php b/config/config_default.php index 74956084..1586533a 100644 --- a/config/config_default.php +++ b/config/config_default.php @@ -769,11 +769,26 @@ $icon_themes[3]['NAME'] = 'XP Style Icons'; * being used. (Must be the "ID" of the desired * template set) * + * $rpc_templateset sets the skin that will be used by default + * when a user is making an RPC request. This + * does not usually need to be changed, but + * can be used to change the RPC protocol that + * is used to respond to RPC requets. (Must be + * the "ID" of the desired template set and + * should NOT be a template set that is included + * in the $aTemplateSet list; PLEASE NOTE that + * new RPC template sets should always be named + * "_rpc", as SquirrelMail will assume + * any template sets ending in "_rpc" are intended + * for its RPC interface only) + * * @global string $templateset_default * @global string $templateset_fallback + * @global string $rpc_templateset */ $templateset_default = 'default'; $templateset_fallback = 'default'; +$rpc_templateset = 'default_rpc'; $aTemplateSet[0]['ID'] = 'default'; $aTemplateSet[0]['NAME'] = 'Default'; diff --git a/include/init.php b/include/init.php index 1ca78e0e..e325da8a 100644 --- a/include/init.php +++ b/include/init.php @@ -501,7 +501,7 @@ switch (PAGE_NAME) { // reset template file cache // $sTemplateID = Template::get_default_template_set(); - Template::cache_template_file_hierarchy(TRUE); + Template::cache_template_file_hierarchy($sTemplateID, TRUE); /** * Make sure icon variables are setup for the login page. @@ -577,9 +577,13 @@ switch (PAGE_NAME) { /* * $sTemplateID is not initialized when a user is not logged in, so we * will use the config file defaults here. If the neccesary variables - * are net set, force a default value. + * are not set, force a default value. */ - $sTemplateID = Template::get_default_template_set(); + if (PAGE_NAME == 'squirrelmail_rpc') { + $sTemplateID = Template::get_rpc_template_set(); + } else { + $sTemplateID = Template::get_default_template_set(); + } $oTemplate = Template::construct_template($sTemplateID); set_up_language($squirrelmail_language, true); @@ -682,7 +686,11 @@ switch (PAGE_NAME) { * so we shouldn't change it here. */ if (!isset($sTemplateID)) { - $sTemplateID = Template::get_default_template_set(); + if (PAGE_NAME == 'squirrelmail_rpc') { + $sTemplateID = Template::get_rpc_template_set(); + } else { + $sTemplateID = Template::get_default_template_set(); + } $icon_theme_path = !$use_icons ? NULL : Template::calculate_template_images_directory($sTemplateID); } diff --git a/include/load_prefs.php b/include/load_prefs.php index 65fc2734..35d19d46 100644 --- a/include/load_prefs.php +++ b/include/load_prefs.php @@ -32,7 +32,11 @@ $custom_css = getPref($data_dir, $username, 'custom_css', 'none' ); // template set setup // $sDefaultTemplateID = Template::get_default_template_set(); -$sTemplateID = getPref($data_dir, $username, 'sTemplateID', $sDefaultTemplateID); +if (PAGE_NAME == 'squirrelmail_rpc') { + $sTemplateID = Template::get_rpc_template_set(); +} else { + $sTemplateID = getPref($data_dir, $username, 'sTemplateID', $sDefaultTemplateID); +} // load user theme @@ -391,10 +395,17 @@ do_hook('loading_prefs', $null); // check user prefs template selection against templates actually available // $found_templateset = false; -for ($i = 0; $i < count($aTemplateSet); ++$i){ - if ($aTemplateSet[$i]['ID'] == $sTemplateID) { +if (PAGE_NAME == 'squirrelmail_rpc') { + // RPC skins have no in-memory list + if (is_dir(SM_PATH . Template::calculate_template_file_directory($sTemplateID))) { $found_templateset = true; - break; + } +} else { + for ($i = 0; $i < count($aTemplateSet); ++$i){ + if ($aTemplateSet[$i]['ID'] == $sTemplateID) { + $found_templateset = true; + break; + } } } diff --git a/include/options/display.php b/include/options/display.php index ef7d2663..53ad6701 100644 --- a/include/options/display.php +++ b/include/options/display.php @@ -479,7 +479,7 @@ function save_option_template($option) { // global $sTemplateID; $sTemplateID = $option->new_value; - Template::cache_template_file_hierarchy(TRUE); + Template::cache_template_file_hierarchy($sTemplateID, TRUE); } diff --git a/src/redirect.php b/src/redirect.php index 354cbc9f..dd9df62d 100644 --- a/src/redirect.php +++ b/src/redirect.php @@ -139,7 +139,8 @@ if ( sqgetGlobalVar('HTTP_ACCEPT', $http_accept, SQ_SERVER) && // having just logged in, need to synch the template file cache // so the right template set is displayed (per user prefs) require(SM_PATH . 'include/load_prefs.php'); -Template::cache_template_file_hierarchy(TRUE); +global $sTemplateID; +Template::cache_template_file_hierarchy($sTemplateID, TRUE); /* Complete autodetection of Javascript. */ checkForJavascript(); diff --git a/templates/default/rpc_response_error.tpl b/templates/default/rpc_response_error.tpl deleted file mode 100644 index 4e2dd3b0..00000000 --- a/templates/default/rpc_response_error.tpl +++ /dev/null @@ -1,35 +0,0 @@ -';*/ -echo ''; -?> - - ERROR - - - diff --git a/templates/default/rpc_response_success.tpl b/templates/default/rpc_response_success.tpl deleted file mode 100644 index b89575f7..00000000 --- a/templates/default/rpc_response_success.tpl +++ /dev/null @@ -1,35 +0,0 @@ -';*/ -echo ''; -?> - - OK - - - -- 2.25.1