Location: PHPKode > scripts > pastebin > pastebin/pastebin.class.php
<?php

/*
* Copyright (c) 2008, Simon Paarlberg (hide@address.com) PGPkey:CA83E278
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
* Usage:
* 
* $pastebin = new pastebin('private');
* 
* // To send a message
* if($pastebin->sendMessage('TITLE', "MESSAGE"))
*     print "OK";
* else
*     print "NOK";
* 
* // To get a list of the messages
* var_dump($pastebin->getList());
* 
* // To get the wanted message
* print $pastebin->getMessage('d7789cbc3');
* 
*/

class pastebin {

    private $subbin = '';
           
    function __construct($subbin) {
        $this->subbin = $subbin;
    }
        
    public function getSubbin() {
        return $this->subbin;
    }
        
    public function sendMessage($poster,$code) {
            
        $adr = $this->getURL() . "/pastebin.php";
        $post = "format=text&code2=" . urlencode($code) .
            "&poster={$poster}&paste=Send&expiry=d";

        $result = $this->getPage($adr, $post);
            
        return strpos($result, '<h1>Posted by ' .
            $poster)===false?false:true;
        }
        
        public function getMessage($id) {
            return $this->getPage($this->getURL() .
               "/pastebin.php?dl=" . $id);
    }
        
    public function getList() {

        $adr = $this->getURL();
        $html = $this->getPage($adr);

        // Removes the HTML (the ugly way)
        $html = substr($html,strpos($html,'<div id="menu">'));
        $html = substr($html,strpos($html,'<ul>'));
        $html = substr($html,0,strpos($html,'</ul>')+5);
            
        // At this point we sould have a nice html-list
        $returnArray = array();
            
        $xml = new SimpleXMLElement($html);

        foreach($xml as $entry) {
            $title = (string)$entry->a[0];
            if(empty($title))
                continue;
                
            $time = (string)$entry;
            if(empty($time))
                continue;
                
            $href = (string)$entry->a['href'];
            // Removes the basepath from the url
            $id = str_replace($this->getURL(),'',$href);
            // And the beginning slash
            $id = substr($id,1);
            if(empty($id))
                continue;
                
            $returnArray[$id] = array(
                'title' => $title,
                'time' => $time,
            );
        }
        
        $xml = null;
        return $returnArray;
    }
        
    private function getURL() {
        return "http://{$this->subbin}.pastebin.com";
    }
        
    private function getPage($page, $post=''){
            
        $ch = curl_init($page);
            
        curl_setopt ($ch, CURLOPT_HEADER, 0);
        curl_setopt ($ch, CURLOPT_VERBOSE, 1);
        if(!empty($post)){
            curl_setopt ($ch, CURLOPT_POST, 1);
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
        }
        curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
        
        ob_start();
        curl_exec ($ch);
        $html = ob_get_contents();
        ob_end_clean();
        
        curl_close ($ch);

        return $html;
    }

}
Return current item: pastebin