t12n of fortune output
[squirrelmail.git] / plugins / fortune / functions.php
index fe1b9c627428c134400fde15faa36bee89564224..dbf066d8dccab6b746e51aed0d93726cceb89b10 100644 (file)
@@ -1,8 +1,9 @@
 <?php
+
 /**
  * Fortune plugin functions
  *
- * @copyright (c) 2004-2005 The SquirrelMail Project Team
+ * @copyright &copy; 2004-2006 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package plugins
  */
 
 /**
- * Declare configuration globals
- */
-global $fortune_location, $fortune_options;
-
-/**
- * Load default config
+ * Declare configuration global and set default value
  */
-include_once(SM_PATH . 'plugins/fortune/config_default.php');
+global $fortune_command;
+$fortune_command = '/usr/games/fortune -s';
 
 /**
  * Load site config
@@ -34,33 +31,29 @@ if (file_exists(SM_PATH . 'config/fortune_config.php')) {
  * @since 1.5.1
  */
 function fortune_function() {
-    global $fortune_visible, $color, $fortune_location, $fortune_options;
+    global $oTemplate, $fortune_visible, $color, $fortune_command;
 
     if (!$fortune_visible) {
         return;
     }
 
-    $exist = file_exists($fortune_location);
-
-    if ($fortune_options!='') {
-        $fortune_command=$fortune_location . ' ' . $fortune_options;
-    } else {
-        $fortune_command=$fortune_location;
+    /* open handle and get all command output*/
+    $handle = popen($fortune_command,'r');
+    $fortune = '';
+    while ($read = fread($handle,1024)) {
+        $fortune .= $read;
     }
-
-    echo "<center><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" bgcolor=\"$color[10]\">\n".
-        "<tr><td><table width=\"100%\" cellpadding=\"2\" cellspacing=\"1\" border=\"0\" bgcolor=\"$color[5]\">\n".
-        "<tr><td align=\"center\">\n";
-    echo '<table><tr><td>';
-    if (!$exist) {
-        printf(_("%s is not found."),$fortune_location);
-    } else {
-        echo "<center><em>" . _("Today's Fortune") . "</em></center><pre>\n" .
-            htmlspecialchars(shell_exec($fortune_command)) .
-            "</pre>\n";
+    /* if pclose return != 0, popen command failed. Yes, I know that it is broken when --enable-sigchild is used */
+    if (pclose($handle)) {
+        // %s shows executed fortune cookie command.
+        $fortune = sprintf(_("Unable to execute \"%s\"."),$fortune_command);
     }
 
-    echo '</td></tr></table></td></tr></table></td></tr></table></center>';
+    $oTemplate->assign('color', $color);
+    $oTemplate->assign('fortune', htmlspecialchars($fortune));
+    $output = $oTemplate->fetch('plugins/fortune/mailbox_index_before.tpl');
+    return array('mailbox_index_before' => $output);
+
 }
 
 /**
@@ -90,4 +83,3 @@ function fortune_function_load() {
 
     $fortune_visible = getPref($data_dir, $username, 'fortune_visible');
 }
-?>