+# converts (binary) integer values that correspond
+# to the SquirrelMail debug mode constants (see
+# include/constants.php) into those constant strings
+# (bitwise or'd if more than one is enabled)
+#
+# if the value passed in is not an integer, it is
+# returned unmolested
+#
+sub convert_debug_binary_integer_to_constants() {
+
+ my ($debug_mode) = @_;
+ if ($debug_mode =~ /^[^0-9]/) {
+ return $debug_mode;
+ }
+ $debug_mode = int($debug_mode);
+ $new_debug_mode = '';
+
+ # per include/constants.php, here are their values:
+ #
+ # 0 SM_DEBUG_MODE_OFF
+ # 1 SM_DEBUG_MODE_SIMPLE
+ # 512 SM_DEBUG_MODE_MODERATE
+ # 524288 SM_DEBUG_MODE_ADVANCED
+ # 536870912 SM_DEBUG_MODE_STRICT
+ #
+ if ($debug_mode & 1) {
+ $new_debug_mode .= ' | SM_DEBUG_MODE_SIMPLE';
+ }
+ if ($debug_mode & 512) {
+ $new_debug_mode .= ' | SM_DEBUG_MODE_MODERATE';
+ }
+ if ($debug_mode & 524288) {
+ $new_debug_mode .= ' | SM_DEBUG_MODE_ADVANCED';
+ }
+ if ($debug_mode & 536870912) {
+ $new_debug_mode .= ' | SM_DEBUG_MODE_STRICT';
+ }
+
+ $new_debug_mode =~ s/^ \| //;
+ if (!$new_debug_mode) {
+ $new_debug_mode = 'SM_DEBUG_MODE_OFF';
+ }
+
+ return $new_debug_mode;
+}
+
+# converts SquirrelMail debug mode constants (see
+# include/constants.php) into their corresponding
+# (binary) integer values
+#
+# if the value passed in is an integer already, it
+# is returned unmolested
+#
+sub convert_debug_constants_to_binary_integer() {
+
+ my ($debug_mode) = @_;
+ if ($debug_mode =~ /^[0-9]/) {
+ return $debug_mode;
+ }
+ $new_debug_mode = 0;
+
+ # per include/constants.php, here are their values:
+ #
+ # 0 SM_DEBUG_MODE_OFF
+ # 1 SM_DEBUG_MODE_SIMPLE
+ # 512 SM_DEBUG_MODE_MODERATE
+ # 524288 SM_DEBUG_MODE_ADVANCED
+ # 536870912 SM_DEBUG_MODE_STRICT
+ #
+ if ($debug_mode =~ /\bSM_DEBUG_MODE_OFF\b/) {
+ $new_debug_mode = 0;
+ }
+ if ($debug_mode =~ /\bSM_DEBUG_MODE_SIMPLE\b/) {
+ $new_debug_mode |= 1;
+ }
+ if ($debug_mode =~ /\bSM_DEBUG_MODE_MODERATE\b/) {
+ $new_debug_mode |= 512;
+ }
+ if ($debug_mode =~ /\bSM_DEBUG_MODE_ADVANCED\b/) {
+ $new_debug_mode |= 524288;
+ }
+ if ($debug_mode =~ /\bSM_DEBUG_MODE_STRICT\b/) {
+ $new_debug_mode |= 536870912;
+ }
+
+ return $new_debug_mode;
+}
+
+# Function to print n column numbered lists
+#
+# WARNING: the names in the list will be truncated
+# to fit in their respective columns based on the
+# screen width and number of columns.
+#
+# Expected arguments (in this order):
+#
+# * The start number to use for the list
+# * The number of columns to use
+# * The screen width
+# * Boolean (zero/one), indicating
+# whether or not to show item numbers
+# * The list of strings to be shown
+#
+# Returns: The number printed on screen of the last item in the list
+#
+sub print_multi_col_list {
+ my ($num, $cols, $screen_width, $show_numbering, @list) = @_;
+ my $x;
+ my $col_cnt = 0;
+ my $row_cnt = 0;
+ my $rows;
+ my $col_width;
+ my $total = 0;
+ my @layout = ();
+ my @numbers = ();
+
+ $rows = int(@list / $cols);
+ if (@list % $cols) { $rows++; }
+ if ($show_numbering) { $col_width = int(($screen_width - 2) / $cols) - 5; }
+ else { $col_width = int(($screen_width - 2) / $cols) - 2; }
+
+ # build the layout array so numbers run down each column
+ #
+ for ( $x = 0; $x < @list; $x++ ) {
+
+ $layout[$row_cnt][$col_cnt] = $list[$x];
+ $numbers[$row_cnt][$col_cnt] = $num++;
+
+ # move to next column
+ #
+ if ($row_cnt == $rows - 1) {
+ $row_cnt = 0;
+ $col_cnt++;
+ }
+ else { $row_cnt++; }
+
+ }
+
+ # if we filled up fewer rows than needed, recalc column width
+ #
+ if ($rows * $col_cnt == @list) { $col_cnt--; } # loop above ended right after increment
+ if ($col_cnt + 1 < $cols) {
+ if ($show_numbering) { $col_width = int(($screen_width - 2) / ($col_cnt + 1)) - 5; }
+ else { $col_width = int(($screen_width - 2) / ($col_cnt + 1)) - 2; }
+ }
+
+ # print it
+ # iterate rows
+ #
+ for ( $row_cnt = 0; $row_cnt <= $rows; $row_cnt++ ) {
+
+ # indent the row
+ #
+ print " ";
+
+ # iterate columns for this row
+ #
+ for ( $col_cnt = 0; $col_cnt <= $cols; $col_cnt++ ) {
+ if ($layout[$row_cnt][$col_cnt]) {
+ print " ";
+ if ($show_numbering) { printf "$WHT% 2u.$NRM", $numbers[$row_cnt][$col_cnt]; }
+ printf " %-$col_width." . $col_width . "s", $layout[$row_cnt][$col_cnt];
+ }
+ }
+ print "\n";
+ }
+
+
+ return $num - 1;
+}
+