Location: PHPKode > projects > Moc10 PHP Library > library/Moc10/Pdf/Info.php
<?php
/**
 * Moc10 Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.TXT.
 * It is also available through the world-wide-web at this URL:
 * http://www.moc10phplibrary.com/LICENSE.TXT
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to hide@address.com so we can send you a copy immediately.
 *
 * @category   Moc10
 * @package    Moc10_Pdf
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 */

/**
 * Moc10_Pdf_Info
 *
 * @category   Moc10
 * @package    Moc10_Pdf
 * @author     Nick Sagona, III <hide@address.com>
 * @copyright  Copyright (c) 2009-2011 Moc 10 Media, LLC. (http://www.moc10media.com)
 * @license    http://www.moc10phplibrary.com/LICENSE.TXT     New BSD License
 * @version    1.9.7
 */

class Moc10_Pdf_Info
{

    /**
     * PDF info object index
     * @var int
     */
    public $index = 3;

    /**
     * PDF info title
     * @var string
     */
    public $title = 'Moc10 PDF';

    /**
     * PDF info author
     * @var string
     */
    public $author = 'Moc10 PDF';

    /**
     * PDF info subject
     * @var string
     */
    public $subject = 'Moc10 PDF';

    /**
     * PDF info creation date
     * @var string
     */
    public $create_date = null;

    /**
     * PDF info mod date
     * @var string
     */
    public $mod_date = null;

    /**
     * PDF info object data
     * @var string
     */
    protected $_data = null;

    /**
     * Constructor
     *
     * Instantiate a PDF info object.
     *
     * @param  string $str
     * @return void
     */
    public function __construct($str = null)
    {

        // Use default settings for a new PDF object and its info object.
        if (is_null($str)) {

            $this->create_date = date('D, M j, Y h:i A');
            $this->mod_date = date('D, M j, Y h:i A');
            $this->_data = "3 0 obj\n<</Creator(Moc10 PDF)/CreationDate([{pdf_create_date}])/ModDate([{pdf_mod_date}])/Author([{pdf_author}])/Title([{pdf_title}])/Subject([{pdf_subject}])/Producer(Moc10 PDF)>>\nendobj\n";

        } else {

            // Else, determine the info object index.
            $this->index = substr($str, 0, strpos($str, ' '));

            // Determine the Creator.
            if (strpos($str, '/Creator') !== false) {
                $crt = substr($str, strpos($str, '/Creator'));
                $crt = substr($crt, strpos($crt, '('));
                $crt = substr($crt, 0, strpos($crt, ')'));
                $crt =  str_replace('(', '', $crt);
                $str =  str_replace($crt, 'Moc10 PDF', $str);
            } else {
                $str =  str_replace('>>', '/Creator(Moc10 PDF)>>', $str);
            }

            // Determine the CreationDate.
            if (strpos($str, '/CreationDate') !== false) {
                $dt = substr($str, strpos($str, '/CreationDate'));
                $dt = substr($dt, strpos($dt, '('));
                $dt = substr($dt, 0, strpos($dt, ')'));
                $dt =  str_replace('(', '', $dt);
                $str =  str_replace($dt, '[{pdf_create_date}]', $str);
                $this->create_date = $dt;
            } else {
                $str =  str_replace('>>', '/CreationDate([{pdf_create_date}])>>', $str);
            }

            // Determine the ModDate.
            if (strpos($str, '/ModDate') !== false) {
                $dt = substr($str, strpos($str, '/ModDate'));
                $dt = substr($dt, strpos($dt, '('));
                $dt = substr($dt, 0, strpos($dt, ')'));
                $dt =  str_replace('(', '', $dt);
                $str =  str_replace($dt, '[{pdf_mod_date}]', $str);
                $this->mod_date = $dt;
            } else {
                $str =  str_replace('>>', '/ModDate([{pdf_mod_date}])>>', $str);
            }

            // Determine the Author.
            if (strpos($str, '/Author') !== false) {
                $auth = substr($str, strpos($str, '/Author'));
                $auth = substr($auth, strpos($auth, '('));
                $auth = substr($auth, 0, strpos($auth, ')'));
                $auth =  str_replace('(', '', $auth);
                $str =  str_replace($auth, '[{pdf_author}]', $str);
                $this->author = $auth;
            } else {
                $str =  str_replace('>>', '/Author([{pdf_author}])>>', $str);
            }

            // Determine the Title.
            if (strpos($str, '/Title') !== false) {
                $tle = substr($str, strpos($str, '/Title'));
                $tle = substr($tle, strpos($tle, '('));
                $tle = substr($tle, 0, strpos($tle, ')'));
                $tle =  str_replace('(', '', $tle);
                $str =  str_replace($tle, '[{pdf_title}]', $str);
                $this->title = $tle;
            } else {
                $str =  str_replace('>>', '/Title([{pdf_title}])>>', $str);
            }

            // Determine the Subject.
            if (strpos($str, '/Subject') !== false) {
                $subj = substr($str, strpos($str, '/Subject'));
                $subj = substr($subj, strpos($subj, '('));
                $subj = substr($subj, 0, strpos($subj, ')'));
                $subj =  str_replace('(', '', $subj);
                $str =  str_replace($subj, '[{pdf_subject}]', $str);
                $this->subject = $subj;
            } else {
                $str =  str_replace('>>', '/Subject([{pdf_subject}])>>', $str);
            }

            // Determine the Producer.
            if (strpos($str, '/Producer') !== false) {
                $prod = substr($str, strpos($str, '/Producer'));
                $prod = substr($prod, strpos($prod, '('));
                $prod = substr($prod, 0, strpos($prod, ')'));
                $prod =  str_replace('(', '', $prod);
                $str =  str_replace($prod, 'Moc 10 PDF', $str);
            } else {
                $str =  str_replace('>>', '/Producer(Moc10 PDF)>>', $str);
            }

            $this->_data = $str;

        }

    }

    /**
     * Method to print the PDF object.
     *
     * @return str
     */
    public function __toString()
    {

        // Set the CreationDate and the ModDate if they are null.
        if (is_null($this->create_date)) {
            $this->create_date = date('D, M j, Y h:i A');
        }
        if (is_null($this->mod_date)) {
            $this->mod_date = date('D, M j, Y h:i A');
        }

        // Swap out the placeholders.
        $data = str_replace('[{pdf_mod_date}]', $this->mod_date, $this->_data);
        $data = str_replace('[{pdf_create_date}]', $this->create_date, $data);
        $data = str_replace('[{pdf_author}]', $this->author, $data);
        $data = str_replace('[{pdf_title}]', $this->title, $data);
        $data = str_replace('[{pdf_subject}]', $this->subject, $data);

        return $data;

    }

}
Return current item: Moc10 PHP Library