using popen instead of shell_exec. shell_exec is disabled in safe mode.
[squirrelmail.git] / plugins / fortune / functions.php
index 460e6e2ef44b62748aab5af9d48422d6f8678ce7..dddfdabdb5c2f0cc1dbc8aa09a59d3c91907a54b 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
+ * Declare configuration global and set default value
  */
-global $fortune_location, $fortune_options;
-
-/**
- * Load default config
- */
-include_once(SM_PATH . 'plugins/fortune/config_default.php');
+global $fortune_command;
+$fortune_command = '/usr/games/fortune -s';
 
 /**
  * Load site config
@@ -34,33 +31,33 @@ 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 $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;
-    }
-
-    echo "<center><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" bgcolor=\"$color[10]\">\n".
+    echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" bgcolor=\"$color[10]\" align=\"center\">\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) {
-        echo sprintf(_("%s is not found."),$fortune_location);
-    } else {
-        echo "<center><em>" . _("Today's Fortune") . "</em></center><pre>\n";
-        htmlspecialchars(system($fortune_command));
-        echo "</pre>\n";
+
+    /* open handle and get all command output*/
+    $handle = popen($fortune_command,'r');
+    $fortune = '';
+    while ($read = fread($handle,1024)) {
+        $fortune .= $read;
+    }
+    /* 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 "<div style=\"text-align: center;\"><em>" . _("Today's Fortune") . "</em></div><pre>\n" .
+            htmlspecialchars($fortune) .
+            "</pre>\n";
 
-    echo '</td></tr></table></td></tr></table></td></tr></table></center>';
+    echo '</td></tr></table></td></tr></table></td></tr></table>';
 }
 
 /**
@@ -90,4 +87,3 @@ function fortune_function_load() {
 
     $fortune_visible = getPref($data_dir, $username, 'fortune_visible');
 }
-?>