Archive for the ‘PHP’ Category.

mirc.php for GeSHi

Seems there were a minor issue with the mirc.php file from GeSHi causing all code entries set to be parsed as mIRC scripts to stop with an error. Fixed (or rather made a dirty hack) it so my mIRC scripts once again could be highlighted :)

<?php
/*************************************************************************************
 * mirc.php
 * -----
 * Author: Alberto 'Birckin' de Areba (Birckin@hotmail.com)
 * Copyright: (c) 2006 Alberto de Areba
 * Release Version: 1.0.7.20
 * Date Started: 2006/05/29
 *
 * mIRC Scripting language file for GeSHi.
 *
 * CHANGES
 * -------
 * 2006/05/29 (1.0.0)
 *   -  First Release
 *
 * 2007/12/12 (1.0.1) - Brian Schmidt Pedersen
 *   -  Removed the forward slash from all mIRC keywords, since in actual
 *       mIRC scripts, they're often written without the slash.
 *   -  Entry 6 (timer parsing) under REGEXPS caused GeSHi to halt with an error
 *       simple solution was to remove the slash(es)
 *
 *************************************************************************************
 *
 *     This file is part of GeSHi.
 *
 *   GeSHi 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.
 *
 *   GeSHi 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 GeSHi; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 ************************************************************************************/


$language_data = array (
    'LANG_NAME' => 'mIRC Scripting',
    'COMMENT_SINGLE' => array(1 => ';'),
    'COMMENT_MULTI' => array(),
    'CASE_KEYWORDS' => GESHI_CAPS_NO_CHANGE,
    'QUOTEMARKS' => array(),
    'ESCAPE_CHAR' => '',
    'KEYWORDS' => array(
        1 => array(
            'alias', 'menu', 'dialog'
        ),
        2 => array(
            'if', 'elseif', 'else', 'while', 'return', 'goto'
        ),
        3 => array(
            'action', 'ajinvite', 'alias', 'aline', 'amsg', 'ame', 'anick', 'aop',
            'auser', 'avoice', 'auto', 'autojoin', 'away', 'background', 'ban',
            'beep', 'channel', 'clear', 'clearall', 'clipboard', 'close',
            'closemsg', 'color', 'copy', 'creq', 'ctcp', 'ctcpreply', 'ctcps',
            'dcc', 'dde', 'ddeserver', 'debug', 'describe', 'disable',
            'disconnect', 'dlevel', 'dll', 'dns', 'dqwindow', 'ebeeps', 'echo',
            'editbox', 'emailaddr', 'enable', 'events', 'exit', 'filter',
            'findtext', 'finger', 'flash', 'flood', 'flush', 'flushini',
            'font', 'fsend', 'fserve', 'fullname', 'ghide', 'gload', 'gmove',
            'gopts', 'gplay', 'gpoint', 'gqreq', 'groups', 'gshow', 'gsize',
            'gstop', 'gtalk', 'gunload', 'guser', 'halt', 'haltdef', 'help', 'hop', 'ignore',
            'inc', 'invite', 'join', 'kick', 'linesep', 'links', 'list', 'load',
            'loadbuf', 'localinfo', 'log', 'me', 'mdi', 'mkdir', 'mnick',
            'mode', 'msg', 'names', 'nick', 'noop', 'notice', 'notify',
            'omsg', 'onotice', 'part', 'partall', 'pdcc', 'perform', 'ping',
            'play', 'pop', 'protect', 'pvoice', 'qmsg', 'qme', 'query',
            'queryrn', 'quit', 'raw', 'remini', 'remote', 'remove', 'rename',
            'renwin', 'resetidle', 'rlevel', 'rmdir', 'run', 'ruser', 'save',
            'savebuf', 'save', 'saveini', 'say', 'server', 'set', 'showmirc', 'sline',
            'sound', 'speak', 'splay', 'sreq', 'strip', 'time',
            'timers', 'timestamp', 'titlebar', 'tnick', 'tokenize', 'topic',
            'ulist', 'unload', 'updatenl', 'url', 'uwho', 'var', 'window', 'winhelp',
            'write', 'writeini', 'who', 'whois', 'whowas'
        )
    ),
    'SYMBOLS' => array(
        '(', ')', '{', '}', '[', ']', '|'
    ),
    'CASE_SENSITIVE' => array(
        GESHI_COMMENTS => true,
        1 => false,
        2 => false
    ),
    'STYLES' => array(
        'KEYWORDS' => array(
            1 => 'color: #994444;',
            2 => 'color: #000000; font-weight: bold;',
            3 => 'color: #990000; font-weight: bold;'
        ),
        'COMMENTS' => array(
            1 => 'color: #808080; font-style: italic;'
        ),
        'ESCAPE_CHAR' => array(),
        'BRACKETS' => array(
            0 => 'color: #FF0000;'
        ),
        'STRINGS' => array(),
        'NUMBERS' => array(
            0 => ''
        ),
        'METHODS' => array(),
        'SYMBOLS' => array(
            0 => 'color: #FF0000;'
        ),
        'REGEXPS' => array(
            0 => 'color: #000099;',
            1 => 'color: #990000;',
            2 => 'color: #888800;',
            3 => 'color: #888800;',
            4 => 'color: #000099;',
            5 => 'color: #000099;',
            6 => 'color: #990000; font-weight: bold;'
        ),
        'SCRIPT' => array()
    ),
    'URLS' => array(
        1 => '',
        2 => '',
        3 => '',
        4 => ''
    ),
    'OOLANG' => false,
    'OBJECT_SPLITTERS' => array(),
    'REGEXPS' => array(
        0 => '\$[^$][^ ,\(\)]*',
        1 => '(%|&).+?[^ ,\)]*',
        2 => '(#|@).+?[^ ,\)]*',
        3 => '-[a-z\d]+',
        4 => '(on|ctcp) (!|@|&)?(\d|\*):[a-zA-Z]+:',
        /*
        4 => array(
            GESHI_SEARCH => '((on|ctcp) (!|@|&)?(\d|\*):(Action|Active|Agent|AppActive|Ban|Chat|Close|Connect|Ctcp|CtcpReply|DccServer|DeHelp|DeOp|DeVoice|Dialog|Dns|Error|Exit|FileRcvd|FileSent|GetFail|Help|Hotlink|Input|Invite|Join|KeyDown|KeyUp|Kick|Load|Logon|MidiEnd|Mode|Mp3End|Nick|NoSound|Notice|Notify|Op|Open|Part|Ping|Pong|PlayEnd|Quit|Raw|RawMode|SendFail|Serv|ServerMode|ServerOp|Signal|Snotice|Start|Text|Topic|UnBan|Unload|Unotify|User|Mode|Voice|Wallops|WaveEnd):)',
            GESHI_REPLACE => '\\1',
            GESHI_MODIFIERS => 'i',
            GESHI_BEFORE => '',
            GESHI_AFTER => ''
        ),
        */

        5 => 'raw (\d|\*):',
        6 => 'timer(?!s\b)[0-9a-zA-Z_]+'
    ),
    'STRICT_MODE_APPLIES' => GESHI_NEVER,
    'SCRIPT_DELIMITERS' => array(),
    'HIGHLIGHT_STRICT_BLOCK' => array()
);
?>

class IMDb

<?php
require_once('HTTP/Request.php');
class imdb
{
    var $id             = '';

    var $host           = 'www.imdb.com';
    var $agent          = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
    var $body           = '';

    var $title          = '';
    var $year           = '';
    var $aka            = '';
    var $genre          = '';

    function open($id)
    {
        $this->id = $id;
        $req =& new HTTP_Request('');
        $req->addHeader('User-Agent', $this->agent);
        $req->setURL('http://'.$this->host.'/title/tt'.$this->id.'/');

        if (!PEAR::isError($req->sendRequest())) {
            $this->body = $req->getResponseBody();
        }
    }

    function title()
    {
        if ($this->title == '') {
            $this->title = stristr($this->body, '<title>');
            $title_e = strpos(strtolower($this->title), '</title>');
            $this->title = substr($this->title, 7, $title_e - 7);
            $this->title = substr($this->title, 0, strpos(strtolower($this->title), '(', 0) - 1);
        }
        return $this->title;
    }

    function year()
    {
        if ($this->year == '') {
            $title_e = strpos(strtolower($this->body), ')</title>');
            $this->year = substr($this->body, $title_e - 4, 4);
        }
        return $this->year;
    }

    function aka()
    {
        if ($this->aka == '') {
            $aka_s = strpos(strtolower($this->body), 'also known as');
            if ($aka_s == 0) {
                return array();
            }
            $aka_e = strpos(strtolower($this->body), "\n", $aka_s);
            $arr_aka_all = explode('<br>', str_replace('&#32;', ' ', substr($this->body, $aka_s, $aka_e - $aka_s)));
            array_shift($arr_aka_all);
            foreach ($arr_aka_all as $str_aka) {
                $arr_aka = explode(' (', $str_aka);
                if (count($arr_aka) < 2) {
                    continue;
                }
                $this->aka[] = array(
                    'title'     => $arr_aka[0],
                    'year'      => substr($arr_aka[1], 0, strlen($arr_aka[1])-1),
                    'country'   => (strlen(trim($arr_aka[2])) != 0) ? substr(trim($arr_aka[2]), 0, strlen(trim($arr_aka[2]))-1) : NULL,
                    'comment'   => (strlen(trim($arr_aka[3])) != 0) ? substr(trim($arr_aka[3]), 0, strlen(trim($arr_aka[3]))-1) : NULL
                );
            }
        }
        return $this->aka;
    }

    function genre()
    {
        if ($this->genre == '') {
            $genre_s = strpos(strtolower($this->body), '/sections/genres/');
            if ($genre_s == 0) {
                return array();
            }
            $genre_s = strpos(strtolower($this->body), '>', $genre_s);
            $genre_e = strpos(strtolower($this->body), "\n", $genre_s);
            $this->genre = explode(' / ', substr(strip_tags(substr($this->body, $genre_s + 1, $genre_e - $genre_s + 1)), 0, strlen(strip_tags(substr($this->body, $genre_s + 1, $genre_e - $genre_s + 1))) - 7));
        }
        return $this->genre;
    }

}
?>

class SlidePager

<?php
class SlidePager
{
    /**
     * Personalised/simplified version of PEAR::Pager (sliding) class.
     * Example:
     *
     * require_once 'class.SlidePager.php';
     *
     * $params = array(
     *     'pagerData'     => $items,
     *     'urlVar'        => 'num',
     *     'urlPrepend'    => base_url() . '/page',
     *     'urlAppend'     => '/',
     *     'perPage'       => 10,
     *     'buffer'        => 4,
     *     'linkClass'     => 'paginator',
     *     'spanClass'     => 'paginator',
     *     'currentClass'  => 'pagiCurrent'
     * );
     *
     * $pager =& new SlidePager;
     * $pager->Pager($params);
     * $arr_posts = $pager->getData();
     */



    /**
     * @var integer total number of items
     * @access private
     */

    var $totalItems;

    /**
     * @var integer number of items per page
     * @access private
     */

    var $perPage        = 10;

    /**
     * @var integer number of items
     * @access private
     */

    var $buffer         = 4;

    /**
     * @var integer current (default) page number
     * @access private
     */

    var $currentPage    = 1;

    /**
     * @var integer total (default) number of pages
     * @access private
     */

    var $totalPages     = 1;

    /**
     * @var string string/file to prepend current page number
     * @access private
     */

    var $urlPrepend     = '';

    /**
     * @var string string to append current page number
     * @access private
     */

    var $urlAppend      = '';

    /**
     * @var string GET variable name which stores current page number
     * @access private
     */

    var $urlVar         = 'num';

    /**
     * @var array data to be paged
     * @access private
     */

    var $pagerData      = null;

    /**
     * @var string the finished paginator
     * @access public
     */

    var $pager          = '';

    /**
     * @var integer previous page numer
     * @access private
     */

    var $prevPage;

    /**
     * @var integer next page number
     * @access private
     */

    var $nextPage;

    /**
     *
     *
     */

    var $pagerSize;
    var $linkClass      = '';
    var $spanClass      = '';
    var $currentClass   = '';

    function Pager($options)
    {
        foreach ($options as $key => $value) {
            $this->{$key} = $value;
        }
        $this->currentPage = (isset($_REQUEST[$this->urlVar]) && $_REQUEST[$this->urlVar] != 0) ? $_REQUEST[$this->urlVar] : 1;
        $this->pagerSize = (2 * $this->buffer) + 1;
        $this->totalItems = count($this->pagerData);
        $this->totalPages = ceil($this->totalItems / $this->perPage);
        $this->prevPage = ($this->currentPage <= 1) ? NULL : $this->currentPage - 1;
        $this->nextPage = ($this->currentPage >= $this->totalPages) ? NULL : $this->currentPage + 1;
        if ($this->totalPages > 1) {
            if ($this->currentPage > 1) {
                $pagerPrev = '<a href="' . $this->urlPrepend . '1' . $this->urlAppend . '" class="' . $this->linkClass . '" title="First page">&lt;&lt;</a>';
                $pagerPrev .= '<a href="' . $this->urlPrepend . $this->prevPage . $this->urlAppend . '" class="' . $this->linkClass . '" title="Previous page (' . $this->prevPage . ')">&lt;</a>';
            }
            else {
                $pagerPrev = '<span class="' . $this->spanClass . '">&lt;&lt;</span>';
                $pagerPrev .= '<span class="' . $this->spanClass . '">&lt;</span>';
            }

            if ($this->currentPage < $this->totalPages) {
                $pagerNext = '<a href="' . $this->urlPrepend . $this->nextPage . $this->urlAppend . '" class="' . $this->linkClass . '" title="Next page (' . $this->nextPage . ')">&gt;</a>';
                $pagerNext .= '<a href="' . $this->urlPrepend . $this->totalPages . $this->urlAppend . '" class="' . $this->linkClass . '" title="Last page">&gt;&gt;</a>';
            }
            else {
                $pagerNext = '<span class="' . $this->spanClass . '">&gt;</span>';
                $pagerNext .= '<span class="' . $this->spanClass . '">&gt;&gt;</span>';
            }

            if (($this->currentPage - $this->buffer) <= 1) {
                $pagerOffset = 1;
                $pagerMax = ($this->totalPages < $this->pagerSize) ? $this->totalPages : $this->pagerSize;
            }
            elseif (($this->currentPage + $this->buffer) > $this->totalPages) {
                $pagerOffset = ((($this->totalPages - $this->pagerSize) + 1) <= 1) ? 1 : (($this->totalPages - $this->pagerSize) + 1);
                $pagerMax = (($this->pagerSize + $pagerOffset) > $this->totalPages) ? $this->totalPages : ($this->pagerSize + $pagerOffset);
            }
            else {
                $pagerOffset = (($this->currentPage + $this->buffer) > $this->totalPages) ? ($this->totalPages - ($this->pagerSize + 1)) : $this->currentPage - $this->buffer;
                $pagerMax = (($this->pagerSize + $pagerOffset) > $this->totalPages) ? $this->totalPages : (($this->pagerSize + $pagerOffset) - 1);
            }

            $pagerNumbers = '';
            for ($i = $pagerOffset; $i <= $pagerMax; $i++) {
                $pagerNumbers .= ($i == $this->currentPage) ? '<span class="' . $this->currentClass . '">' . $i . '</span>' : '<a href="' . $this->urlPrepend . $i . $this->urlAppend . '" class="' . $this->linkClass . '" title="Go to page ' . $i . '">' . $i . '</a>';
            }
            $this->pager = $pagerPrev . $pagerNext . $pagerNumbers;
        }
    }

    function GetData()
    {
        $itemsOffset = ($this->currentPage * $this->perPage) - $this->perPage;
        $itemsMax = (($itemsOffset + $this->perPage) > $this->totalItems) ? $this->totalItems : ($itemsOffset + $this->perPage);
        for ($i = $itemsOffset; $i < $itemsMax; $i++) {
            $data[] = $this->pagerData[$i];
        }
        return $data;
    }
}
?>

function array_natsort()

<?php
/**
 * @author Brian Schmidt
 * @version 1.0
 * @return array
 * @param $arrData Array containing data to sort
 * @param $strIndex Name of column to use as an index
 * @param $strSortBy Column to sort the array by
 * @param $strSortType String containing either asc or desc [default to asc]
 * @access public
 * @desc Naturally sorts an array using the column $strSortBy
 * @example $arr_sorted = array_natsort(array(array('a' => 10, 'b' => 'foo'), array('a' => 2, 'b' => 'foo'), array('a' => 11, 'b' => 'foo')), 'a', 'a');
 */

function array_natsort($arrData, $strIndex, $strSortBy, $strSortType = FALSE)
{
    if (!is_array($arrData) || !$strIndex || !$strSortBy) {
        return $arrData;
    }
    $arrSort = $arrResult = array();
    foreach ($arrData as $arrRow) {
        $arrSort[$arrRow[$strIndex]] = $arrRow[$strSortBy];
    }
    natsort($arrSort);
    if ($strSortType == "desc") {
        arsort($arrSort);
    }
    foreach ($arrSort as $arrSortKey => $arrSorted) {
        foreach ($arrData as $arrOriginal) {
            if ($arrOriginal[$strIndex]==$arrSortKey) {
                array_push($arrResult, $arrOriginal);
            }
        }
    }
    return $arrResult;
}
?>

function calc_age()

<?php
/**
 * @author Brian Schmidt
 * @version 1.0
 * @return array
 * @param string $str_birthday
 * @access public
 * @desc Returns the current age from a rough calculation of the birthdate.
 * @example echo calc_age('1976-12-13 19:00');
 */

function calc_age($str_birthday)
{
    if (!$str_birthday) {
        return FALSE;
    }
    $int_age = floor((time()-strtotime($str_birthday))/(60*60*24*365.22222222222));
    return $int_age;
}
?>