Location: PHPKode > projects > Buzzword > buzzword-0.8.12/includes/common.inc
<?php

/*
 * buzzword
 * Copyright (c) 2003 Jon Tai
 *
 * $Id: common.inc 314 2004-04-13 06:58:55Z bradt $
 *
 * This file is part of buzzword.
 *
 * buzzword is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * buzzword is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with buzzword; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * Get a variable defined by $key from $_REQUEST
 * @param string $key the key to retrieve from $_REQUEST
 * @return string a sanitized version of the data at the key provided 
 */
function get_request_var($key) {
    if (empty($_REQUEST[$key]))
        return FALSE;
    return trim((get_magic_quotes_gpc()) ? stripslashes($_REQUEST[$key]) : $_REQUEST[$key]);
}

/**
 * Returns the array given by $key from the $_REQUEST variables
 * @param string $key the key to retrieve from $_REQUEST
 * @return array a sanitized version of the data at the key provided
 */
function get_request_array($key) {
    $array = array();

    if (empty($_REQUEST[$key]))
        return $array;

    while (list($data_key, $data_val) = each($_REQUEST[$key]))
        $array[$data_key] = trim((get_magic_quotes_gpc()) ? stripslashes($data_val) : $data_val);
    return $array;
}

/**
 * Displays an error message and exits the file gracefully
 * @param string $error the error message to display
 */
function display_error($error) {
    global $BUZZWORD_VERSION, $BUZZWORD_PAGE_TITLE, $BUZZWORD_BREADCRUMBS, $BUZZWORD_HOOKS;

    include_once '../includes/header.inc';
    echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n";
    echo "<tr>\n";
    echo "<td id=\"content\">Error: $error</td>\n";
    echo "</tr>\n";
    echo "</table>\n";
    include_once '../includes/footer.inc';
}

/**
 * Send "304 Not Modified" headers
 * @param int the last modified time to send to the browser in unix timestamp
 * format
 */
function send_cache_headers($last_modified) {
    $gmdate = gmdate('D, d M Y H:i:s', $last_modified).' GMT';

    // force clients to revalidate each time the content is requested
    header('Cache-Control: no-cache, must-revalidate');
    header('Pragma: no-cache');

    // if client sent a "If-Modified-Since" header, and it matches 
    // the last modification date of this object, send a "304 Not Modified"
    if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
        if ($gmdate == $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
            header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
            exit;
        }
    }

    // send a "Last-Modified" header in hopes that the client will 
    // send an "If-Modified-Since" next time
    header('Last-Modified: '.$gmdate);
}

/**
 * Send a email to the administrator of the buzzword installation defined in
 * <code>buzzword_admin_email</code>
 */
function send_admin_email($from, $from_email, $subj, $msg, $backtrack_url) {
    if (verify_email_address(get_pref('buzzword_admin_email'))) {
        // TODO:  Stick all this url manip into a common function
        $dirs = explode('/', $_SERVER['PHP_SELF']);
        array_pop($dirs);
        array_pop($dirs);

        $url = "http://{$_SERVER['SERVER_NAME']}".implode('/', $dirs);

        $msg .= "\n\n--\nThis message was sent from $backtrack_url.\n";
        $msg .= "If you want to stop receiving these emails, set your ";
        $msg .= "buzzword_admin_email to nothing in ";
        $msg .= "$url/admin/preferences.php.";

        mail(get_pref('buzzword_admin_email'), $subj, $msg, 
            "X-mailer: buzzword/".$BUZZWORD_VERSION, "From: $from <$from_email>");
    }
}

/**
 * Returns true if $email is in the proper hide@address.com format
 * @param string $email the email address to verify
 * @return bool true if $email is valid, false otherwise
 */
function verify_email_address($email) {
    // Thanks W. Jason Gilmore of Developer.com
    // http://www.developer.com/lang/php/article.php/3290141
    return eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)"
                     ."(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email);
}

?>
Return current item: Buzzword