Current File : //opt/RZphp82/includes/data/File_Fortune/examples/phpFortune
#!@php-bin@
<?php
/**
 * PHP Fortune 
 *
 * A command-line script for pulling a random fortune from a fortune file.
 * /usr/share/fortune or /usr/share/games/fortunes are assumed as
 * the base directory. If no filename is provided, a random fortune from a
 * fortune file in the base directory will be pulled. If a valid fortune file
 * is provided, an optional index may also be used to pull a specific fortune
 * from the file.
 *
 * Usage:
 * <code>
 * // pull a random fortune from the fortunes fortune file
 * $ phpFortune fortunes
 *
 * // pull fortune 3 from the fortunes fortune file
 * $ phpFortune 3 fortunes
 *
 * // pull a random fortune from any fortune file in the base directory
 * $ phpFortune
 * </code>
 *
 * @author    Matthew Weier O'Phinney <mweierophinney@gmail.com>
 * @copyright 2005 - 2007, Matthew Weier O'Phinney
 * @version   @package_version@
 */
require_once 'File/Fortune.php';

if (is_dir('/usr/share/fortune')) {
    $base = '/usr/share/fortune';
} elseif (is_dir('/usr/share/games/fortunes')) {
    $base = '/usr/share/games/fortunes';
} else {
    echo "Cannot find base fortunes directory.\nNeither /usr/share/fortune nor /usr/share/games/fortunes exist\n";
    exit(1);
}

$file  = null;
$index = null;
if ($argc > 1) {
    $args = $argv;
    array_shift($args);
    $tmp = array_shift($args);
    if (is_numeric($tmp)) {
        // User provided an index
        $index = $tmp;
        if (count($args) > 1) {
            // Multiple files provided
            $file = $args;
        } else {
            // Single file provided
            $file = array_shift($args);
        }
    } else {
        if (count($args)) {
            // Multiple files provided
            $file = array_merge(array($tmp), $args);
        } else {
            // Single file provided
            $file = $tmp;
        }
    }
}

// Validate value of file(s) provided
if (null === $file) {
    // no file provided; use default directory
    $index = null;
    $file = $base;
} elseif (is_string($file)) {
    $file = phpFortune_checkFile($file);
    if (is_dir($file)) {
        $index = null;
    }
} elseif (is_array($file)) {
    foreach ($file as $key => $value) {
        $file[$key] = phpFortune_checkFile($value);
    }
    $index = null;
}

try {
    $fortunes = new File_Fortune($file);
    if (null === $index) {
        // Get random fortune
        echo $fortunes->getRandom();
    } else {
        // or grab by index
        $index = (int) $index;
        if (!isset($fortunes[$index])) {
            echo "Fortune '$index' does not exist in this fortune file";
        } else {
            echo $fortunes[$index];
        }
    }
} catch (File_Fortune_Exception $e) {
    echo "Unable to retrieve fortune: " . $e->getMessage() . "\n";
}

/**
 * Check that a fortune file exists and normalize path
 * 
 * @param  string $file 
 * @return string
 */
function phpFortune_checkFile($file)
{
    if ('/' != substr($file, 0, 1)) {
        $file = $base . '/' . $file;
    }
    if (!file_exists($file)) {
        echo "Invalid fortune file specified\n";
        exit(1);
    }

    return $file;
}