Calendar-Plugin now with search function

"Markus Robert Kessler" <[email protected]> Wed, 22 Jul 2026 13:48:10 +0200
Newsgroups gmane.mail.squirrelmail.user
Message-ID <58c6612fce449427fc3d594090e23b44.squirrel@webmail.dipl-ing-kessler.de>
Hi Paul et al.,

below please find a section that can be added to

    plugins/calendar/calendar.php

to have a search field and the needed search algorithm for the web ui.

As mentioned, I moved my new code to a function to be added to above php.
This is for better overview.

The relevant function,

    search_engine()

should be called last. So we have this order:

displayPageHeader($color, 'None');
calendar_header();
readcalendardata();
startcalendar();
drawmonthview();
endcalendar();
search_engine();

Since this mailing list's majordomo does not forward emails with
attachments, I add the function here as plain text.
Hope I could help!

Best regards,

Markus







######################################################################

function search_engine() {
    // 1. Globale SquirrelMail-Variablen laden
    global $data_dir, $username, $default_charset;

    // Fallback definieren, falls das Charset nicht gesetzt sein sollte
    $charset =3D !empty($default_charset) ? $default_charset : 'ISO-8859-1';

    // ----------------------------------------------------
    // 2. Suchformular ausgeben
    // ----------------------------------------------------
    $search_term_input =3D isset($_GET['cal_search']) ?
htmlspecialchars($_GET['cal_search'], ENT_COMPAT | ENT_HTML401,
$charset) : '';
    $label_search =3D _("Search");

    echo '<br />' . "\n" .
         '<form method=3D"GET" action=3D"calendar.php">' . "\n" .
         '  <table align=3D"center" cellspacing=3D"1" cellpadding=3D"2"
border=3D"0">' . "\n" .
         '    <tr>' . "\n" .
         '      <td>' . "\n" .
         '        <strong>' . $label_search . ':</strong> ' . "\n" .
         '        <input type=3D"text" name=3D"cal_search" value=3D"' .
$search_term_input . '" size=3D"20" />' . "\n" .
         '        <input type=3D"submit" value=3D"' . $label_search . '" />=
' .
"\n" .
         '      </td>' . "\n" .
         '    </tr>' . "\n" .
         '  </table>' . "\n" .
         '</form>' . "\n";

    // ----------------------------------------------------
    // 3. Suche ausf=FChren & Ergebnisse verarbeiten
    // ----------------------------------------------------
    if (!empty($_GET['cal_search'])) {
        $search_term =3D $_GET['cal_search'];

        // Suchbegriff bei Bedarf an das Server-Charset anpassen
        if (function_exists('mb_convert_encoding')) {
            $search_term_converted =3D mb_convert_encoding($search_term,
$charset, 'UTF-8');
        } else {
            $search_term_converted =3D $search_term;
        }

        $search_pattern =3D $data_dir . $username . '.*.cal';
        $cal_files =3D glob($search_pattern);
        $results =3D [];

        if (is_array($cal_files)) {
            foreach ($cal_files as $file) {
                if (!file_exists($file)) continue;

                $handle =3D fopen($file, "r");
                if ($handle) {
                    while (($line =3D fgets($handle)) !=3D=3D false) {
                        $line =3D trim($line);
                        if (empty($line)) continue;

                        // Format: MMDDYYYY|HHMM|0|0|Text||
                        $fields =3D explode('|', $line);

                        // Vergleich inkl. Charset-Anpassung
                        if (isset($fields[4]) && stripos($fields[4],
$search_term_converted) !=3D=3D false) {
                            $date_raw =3D $fields[0]; // MMDDYYYY

                            $month =3D substr($date_raw, 0, 2);
                            $day   =3D substr($date_raw, 2, 2);
                            $year  =3D substr($date_raw, 4, 4);

                            $time_raw =3D str_pad(trim($fields[1]), 4, '0',
STR_PAD_LEFT);
                            $sort_key =3D $year . $month . $day . $time_raw;
                            $formatted_time =3D substr($time_raw, 0, 2) .
':' . substr($time_raw, 2, 2);

                            // Text unter Ber=FCcksichtigung des Charsets
absichern
                            $clean_text =3D htmlspecialchars($fields[4],
ENT_COMPAT | ENT_HTML401, $charset);

                            $results[] =3D [
                                'sort_key'     =3D> $sort_key,
                                'link_params'  =3D>
"year=3D$year&month=3D$month&day=3D$day",
                                'display_date' =3D> "$day.$month.$year",
                                'time'         =3D> $formatted_time,
                                'text'         =3D> $clean_text
                            ];
                        }
                    }
                    fclose($handle);
                }
            }

            // Nach Datum absteigend sortieren (neueste zuerst)
            usort($results, function($a, $b) {
                return strcmp($b['sort_key'], $a['sort_key']);
            });
        }

        // ----------------------------------------------------
        // 4. Ergebnisse als Tabelle ausgeben
        // ----------------------------------------------------
        $search_term_display =3D htmlspecialchars($search_term, ENT_COMPAT |
ENT_HTML401, $charset);

        echo '<br />' . "\n";
        echo '<table align=3D"center" cellspacing=3D"1" cellpadding=3D"4"
border=3D"0" bgcolor=3D"#ababab" width=3D"80%">' . "\n";
        echo '  <tr bgcolor=3D"#dcdcdc">' . "\n";
        echo '    <td colspan=3D"3"><strong>' . sprintf(_("Search results"),
$search_term_display) . '</strong></td>' . "\n";
        echo '  </tr>' . "\n";

        if (empty($results)) {
            echo '  <tr bgcolor=3D"#ffffff"><td colspan=3D"3">' . _("No
results") . '</td></tr>' . "\n";
        } else {
            foreach ($results as $res) {
                echo '  <tr bgcolor=3D"#ffffff">' . "\n";
                echo '    <td width=3D"15%">' . $res['display_date'] .
'</td>' . "\n";
                echo '    <td width=3D"10%">' . $res['time'] . '</td>' . "\=
n";
                echo '    <td><a href=3D"day.php?' . $res['link_params'] .
'">' . $res['text'] . '</a></td>' . "\n";
                echo '  </tr>' . "\n";
            }
        }
        echo '</table>' . "\n";
    }
}



-----
squirrelmail-users mailing list
Posting guidelines: http://squirrelmail.org/postingguidelines
List address: [email protected]
List archives: http://news.gmane.org/gmane.mail.squirrelmail.user
List info (subscribe/unsubscribe/change options): https://lists.sourceforge=
.net/lists/listinfo/squirrelmail-users