Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/PEdit.inc.php

    r124 r136  
    11<?php
    22/**
    3  * PEdit:: provides a mechanism to store text in php variables
     3 * PEdit.inc.php
     4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
     5 *
     6 * PEdit provides a mechanism to store text in php variables
    47 * which will be printed to the client browser under normal
    58 * circumstances, but an authenticated user can 'edit' the document--
     
    1114 * To use, include this file, initialize variables,
    1215 * and call printing/editing functions where you want data and forms to
    13  * show up. Below is an example of use:
     16 * show up.
     17 *
     18 * @author  Quinn Comendant <quinn@strangecode.com>
     19 * @concept Beau Smith <beau@beausmith.com>
     20 * @version 2.0
     21 *
     22 * Example of use:
    1423 
    1524 // Initialize PEdit object.
     
    3746 $pedit->formEnd();
    3847
    39  * @author  Quinn Comendant <quinn@strangecode.com>
    40  * @concept Beau Smith <beau@beausmith.com>
    41  * @version 2.0
    4248 */
    4349class PEdit {
     
    95101    function setParam($params)
    96102    {
     103        $app =& App::getInstance();
     104
    97105        if (isset($params) && is_array($params)) {
    98106            // Merge new parameters with old overriding only those passed.
    99107            $this->_params = array_merge($this->_params, $params);
    100108        } else {
    101             App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_WARNING, __FILE__, __LINE__);
     109            $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_WARNING, __FILE__, __LINE__);
    102110        }
    103111    }
     
    112120    function getParam($param)
    113121    {
     122        $app =& App::getInstance();
     123   
    114124        if (isset($this->_params[$param])) {
    115125            return $this->_params[$param];
    116126        } else {
    117             App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
     127            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
    118128            return null;
    119129        }
     
    129139    function start($initialize_data_file=false)
    130140    {
     141        $app =& App::getInstance();
     142
    131143        if (!is_dir($this->getParam('data_dir'))) {
    132144            trigger_error(sprintf('PEdit data directory not found: %s', $this->getParam('data_dir')), E_USER_WARNING);
     
    143155        case 'Save' :
    144156            if ($this->_writeData()) {
    145                 App::dieURL($_SERVER['PHP_SELF']);
     157                $app->dieURL($_SERVER['PHP_SELF']);
    146158            }
    147159            break;
    148160        case 'Restore' :
    149161            if ($this->_restoreVersion(getFormData('version'))) {
    150                 App::dieURL($_SERVER['PHP_SELF']);
     162                $app->dieURL($_SERVER['PHP_SELF']);
    151163            }
    152164            break;
    153165        case 'View' :
    154166            $this->_data_file = sprintf('%s%s__%s.xml', $this->getParam('data_dir'), $_SERVER['PHP_SELF'], getFormData('version'));
    155             App::raiseMsg(sprintf(_("This is <em><strong>only a preview</strong></em> of version %s."), getFormData('version')), MSG_NOTICE, __FILE__, __LINE__);
     167            $app->raiseMsg(sprintf(_("This is <em><strong>only a preview</strong></em> of version %s."), getFormData('version')), MSG_NOTICE, __FILE__, __LINE__);
    156168            break;
    157169        }
     
    176188    function set($name, $options=array())
    177189    {
     190        $app =& App::getInstance();
     191
    178192        $name = preg_replace('/\s/', '_', $name);
    179193        if (!isset($this->_data[$name])) {
    180194            $this->_data[$name] = array_merge(array('content' => ''), $options);
    181195        } else {
    182             App::logMsg(sprintf('Duplicate set data: %s', $name), LOG_NOTICE, __FILE__, __LINE__);
     196            $app->logMsg(sprintf('Duplicate set data: %s', $name), LOG_NOTICE, __FILE__, __LINE__);
    183197        }
    184198    }
     
    208222    function formBegin()
    209223    {
     224        $app =& App::getInstance();
     225
    210226        if (!$this->_authorized || empty($this->_data)) {
    211227            return false;
     
    216232        <input type="hidden" name="file_hash" value="<?php echo $this->_fileHash(); ?>" />
    217233        <?php
    218         App::printHiddenSession();
     234        $app->printHiddenSession();
    219235        switch ($this->op) {
    220236        case 'Edit' :
     
    345361    function printVersions()
    346362    {
     363        $app =& App::getInstance();
     364
    347365        if ($this->_authorized && $this->op == 'Versions') {
    348366            // Print versions and commands to view/restore.
     
    362380                    ?>
    363381                    <tr>
    364                         <td><?php echo date(App::getParam('date_format'), $v['unixtime']); ?></td>
    365                         <td><?php echo date(App::getParam('time_format'), $v['unixtime']); ?></td>
     382                        <td><?php echo date($app->getParam('date_format'), $v['unixtime']); ?></td>
     383                        <td><?php echo date($app->getParam('time_format'), $v['unixtime']); ?></td>
    366384                        <td><?php echo humanFileSize($v['filesize']); ?></td>
    367                         <td class="sc-nowrap"><a href="<?php echo App::oHREF($_SERVER['PHP_SELF'] . '?op=View&version=' . $v['unixtime'] . '&file_hash=' . $this->_fileHash()); ?>"><?php echo _("View"); ?></a> <?php echo _("or"); ?> <a href="<?php echo App::oHREF($_SERVER['PHP_SELF'] . '?op=Restore&version=' . $v['unixtime'] . '&file_hash=' . $this->_fileHash()); ?>"><?php echo _("Restore"); ?></a></td>
     385                        <td class="sc-nowrap"><a href="<?php echo $app->oHREF($_SERVER['PHP_SELF'] . '?op=View&version=' . $v['unixtime'] . '&file_hash=' . $this->_fileHash()); ?>"><?php echo _("View"); ?></a> <?php echo _("or"); ?> <a href="<?php echo $app->oHREF($_SERVER['PHP_SELF'] . '?op=Restore&version=' . $v['unixtime'] . '&file_hash=' . $this->_fileHash()); ?>"><?php echo _("Restore"); ?></a></td>
    368386                    </tr>
    369387                    <?php
     
    386404    function _fileHash()
    387405    {
    388         return md5(App::getParam('signing_key') . $_SERVER['PHP_SELF']);
     406        $app =& App::getInstance();
     407
     408        return md5($app->getParam('signing_key') . $_SERVER['PHP_SELF']);
    389409    }
    390410
     
    399419    function _loadDataFile()
    400420    {
     421        $app =& App::getInstance();
     422
    401423        if (!file_exists($this->_data_file)) {
    402424            if (!$this->_initializeDataFile()) {
    403                 App::logMsg(sprintf('Initializing content file failed: %s', $this->_data_file), LOG_WARNING, __FILE__, __LINE__);
     425                $app->logMsg(sprintf('Initializing content file failed: %s', $this->_data_file), LOG_WARNING, __FILE__, __LINE__);
    404426                return false;
    405427            }
     
    408430        $status = $this->xml_unserializer->unserialize($xml_file_contents, false);   
    409431        if (PEAR::isError($status)) {
    410             App::logMsg(sprintf('XML_Unserialize error: %s', $status->getMessage()), LOG_WARNING, __FILE__, __LINE__);
     432            $app->logMsg(sprintf('XML_Unserialize error: %s', $status->getMessage()), LOG_WARNING, __FILE__, __LINE__);
    411433            return false;
    412434        }
     
    436458    function _initializeDataFile()
    437459    {
    438         App::logMsg(sprintf('Initializing data file: %s', $this->_data_file), LOG_INFO, __FILE__, __LINE__);
     460        $app =& App::getInstance();
     461
     462        $app->logMsg(sprintf('Initializing data file: %s', $this->_data_file), LOG_INFO, __FILE__, __LINE__);
    439463        $xml_file_contents = $this->xml_serializer->serialize($this->_data);
    440464        return $this->_filePutContents($this->_data_file, $xml_file_contents);
     
    450474    function _writeData()
    451475    {
     476        $app =& App::getInstance();
     477
    452478        if (!$this->_authorized) {
    453479            return false;
     
    455481        if ($this->_fileHash() != getFormData('file_hash')) {
    456482            // Posted data is NOT for this file!
    457             App::logMsg(sprintf('File_hash does not match current file.', null), LOG_WARNING, __FILE__, __LINE__);
     483            $app->logMsg(sprintf('File_hash does not match current file.', null), LOG_WARNING, __FILE__, __LINE__);
    458484            return false;
    459485        }
     
    466492            $this->_deleteOldVersions();
    467493            if (!$this->_createVersion()) {
    468                 App::logMsg(sprintf('Failed creating new version of file.', null), LOG_NOTICE, __FILE__, __LINE__);
     494                $app->logMsg(sprintf('Failed creating new version of file.', null), LOG_NOTICE, __FILE__, __LINE__);
    469495                return false;
    470496            }
     
    496522    function _filePutContents($filename, $content)
    497523    {
     524        $app =& App::getInstance();
     525
    498526        // Ensure requested filename is within the pedit data dir.
    499527        if (strpos($filename, $this->getParam('data_dir')) === false) {
    500             App::logMsg(sprintf('Failed writing file outside pedit _data_dir: %s', $filename), LOG_ERR, __FILE__, __LINE__);
     528            $app->logMsg(sprintf('Failed writing file outside pedit _data_dir: %s', $filename), LOG_ERR, __FILE__, __LINE__);
    501529            return false;
    502530        }
     
    510538            if (!is_dir($curr_path)) {
    511539                if (!mkdir($curr_path)) {
    512                     App::logMsg(sprintf('Failed mkdir: %s', $curr_path), LOG_ERR, __FILE__, __LINE__);
     540                    $app->logMsg(sprintf('Failed mkdir: %s', $curr_path), LOG_ERR, __FILE__, __LINE__);
    513541                    return false;
    514542                }
     
    522550                flock($fp, LOCK_UN);
    523551            } else {
    524                 App::logMsg(sprintf('Could not lock file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__);
     552                $app->logMsg(sprintf('Could not lock file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__);
    525553                return false;
    526554            }
    527555            fclose($fp);
    528556            // Success!
    529             App::logMsg(sprintf('Wrote to file: %s', $filename), LOG_DEBUG, __FILE__, __LINE__);
     557            $app->logMsg(sprintf('Wrote to file: %s', $filename), LOG_DEBUG, __FILE__, __LINE__);
    530558            return true;
    531559        } else {
    532             App::logMsg(sprintf('Could not open file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__);
     560            $app->logMsg(sprintf('Could not open file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__);
    533561            return false;
    534562        }
     
    544572    function _createVersion()
    545573    {
     574        $app =& App::getInstance();
     575
    546576        if (!$this->_authorized) {
    547577            return false;
     
    549579        if ($this->_fileHash() != getFormData('file_hash')) {
    550580            // Posted data is NOT for this file!
    551             App::logMsg(sprintf('File_hash does not match current file.', null), LOG_ERR, __FILE__, __LINE__);
     581            $app->logMsg(sprintf('File_hash does not match current file.', null), LOG_ERR, __FILE__, __LINE__);
    552582            return false;
    553583        }
     
    555585        // Ensure current data file exists.
    556586        if (!file_exists($this->_data_file)) {
    557             App::logMsg(sprintf('Data file does not yet exist: %s', $this->_data_file), LOG_NOTICE, __FILE__, __LINE__);
     587            $app->logMsg(sprintf('Data file does not yet exist: %s', $this->_data_file), LOG_NOTICE, __FILE__, __LINE__);
    558588            return false;
    559589        }
     
    563593        $version_file = sprintf('%s__%s.xml', preg_replace('/\.xml$/', '', $this->_data_file), time());
    564594        if (!copy($this->_data_file, $version_file)) {
    565             App::logMsg(sprintf('Failed copying new version: %s -> %s', $this->_data_file, $version_file), LOG_ERR, __FILE__, __LINE__);
     595            $app->logMsg(sprintf('Failed copying new version: %s -> %s', $this->_data_file, $version_file), LOG_ERR, __FILE__, __LINE__);
    566596            return false;
    567597        }
     
    580610    function _deleteOldVersions()
    581611    {
     612        $app =& App::getInstance();
     613
    582614        $version_files = $this->_getVersions();
    583615        if (is_array($version_files) && sizeof($version_files) > $this->getParam('versions_min_qty')) {
     
    590622                $del_file = dirname($this->_data_file) . '/' . $oldest['filename'];
    591623                if (!unlink($del_file)) {
    592                     App::logMsg(sprintf('Failed deleting version: %s', $del_file), LOG_ERR, __FILE__, __LINE__);
     624                    $app->logMsg(sprintf('Failed deleting version: %s', $del_file), LOG_ERR, __FILE__, __LINE__);
    593625                }
    594626                $oldest = array_pop($version_files);
     
    637669    function _restoreVersion($version)
    638670    {
     671        $app =& App::getInstance();
     672
    639673        if (!$this->_authorized) {
    640674            return false;
     
    646680        // Ensure specified version exists.
    647681        if (!file_exists($version_file)) {
    648             App::logMsg(sprintf('Cannot restore non-existant file: %s', $version_file), LOG_NOTICE, __FILE__, __LINE__);
     682            $app->logMsg(sprintf('Cannot restore non-existant file: %s', $version_file), LOG_NOTICE, __FILE__, __LINE__);
    649683            return false;
    650684        }
     
    652686        // Make certain a version is created.
    653687        if (!$this->_createVersion()) {
    654             App::logMsg(sprintf('Failed creating new version of file.', null), LOG_ERR, __FILE__, __LINE__);
     688            $app->logMsg(sprintf('Failed creating new version of file.', null), LOG_ERR, __FILE__, __LINE__);
    655689            return false;
    656690        }
     
    658692        // Do the actual copy.
    659693        if (!copy($version_file, $this->_data_file)) {
    660             App::logMsg(sprintf('Failed copying old version: %s -> %s', $version_file, $this->_data_file), LOG_ERR, __FILE__, __LINE__);
     694            $app->logMsg(sprintf('Failed copying old version: %s -> %s', $version_file, $this->_data_file), LOG_ERR, __FILE__, __LINE__);
    661695            return false;
    662696        }
    663697
    664698        // Success!
    665         App::raiseMsg(sprintf(_("Page has been restored to version %s."), $version), MSG_SUCCESS, __FILE__, __LINE__);
     699        $app->raiseMsg(sprintf(_("Page has been restored to version %s."), $version), MSG_SUCCESS, __FILE__, __LINE__);
    666700        return true;
    667701    }
Note: See TracChangeset for help on using the changeset viewer.