Changeset 42 for trunk/lib/PEdit.inc.php


Ignore:
Timestamp:
Dec 18, 2005 12:16:03 AM (18 years ago)
Author:
scdev
Message:

detabbed all files ;P

File:
1 edited

Legend:

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

    r41 r42  
    22/**
    33 * PEdit:: provides a mechanism to store text in php variables
    4  * which will be printed to the client browser under normal 
     4 * which will be printed to the client browser under normal
    55 * circumstances, but an authenticated user can 'edit' the document--
    66 * data stored in vars will be shown in html form elements to be editied
     
    1616 *  require_once 'codebase/lib/PEdit.inc.php';
    1717 *  $p = new PEdit($auth->hasClearance('pedit'));
    18  * 
     18 *
    1919 *  $title = <<<P_E_D_I_T_title
    2020 *  Using Burritos to Improve Student Learnin'
    2121 *  P_E_D_I_T_title;
    2222 *  $p->set($title, 'title', 'textbox');
    23  * 
     23 *
    2424 *  // Begin content. Include a header or something right here.
    2525 *  $p->printContent('title');
    26  * 
     26 *
    2727 *  // Prints beginning form tags and special hidden forms. (Only happens if page is NOT a archived version.)
    2828 *  $p->formBegin();
    29  * 
     29 *
    3030 *  // Print editing form elements. (Only happens if op == Edit.)
    3131 *  $p->printForm('title');
    32  * 
     32 *
    3333 *  // Print versions list. (Only happens if op == Versions.)
    3434 *  $p->printVersions();
    35  * 
     35 *
    3636 *  // Prints ending form tags and command buttons.(Only happens if page is NOT a archived version.)
    3737 *  $p->formEnd();
    3838 *
    39  * @author  Quinn Comendant <quinn@strangecode.com> 
    40  * @concept Beau Smith <beau@beausmith.com> 
     39 * @author  Quinn Comendant <quinn@strangecode.com>
     40 * @concept Beau Smith <beau@beausmith.com>
    4141 * @version 1.1
    4242 */
     
    5151    // Tags that are not stripped from the POSTed data.
    5252    var $allowed_tags = '<p><h1><h2><h3><h4><h5><h6><div><br><hr><a><img><i><em><b><strong><small><blockquote><ul><ol><li><dl><dt><dd><map><area><table><tr><td>';
    53    
    54    
    55     /** 
     53
     54
     55    /**
    5656     * Constructs a new PEdit object. Initializes what file is being operated on
    57      * (SCRIPT_FILENAME) and what that operation is. The two 
     57     * (SCRIPT_FILENAME) and what that operation is. The two
    5858     * operations that actually modify data (save, restore) are treated differently
    5959     * than view operations (versions, '' - default). They die redirect so you see
    6060     * the page you just modified.
    61      * 
    62      * @access public 
    63      * 
    64      * @param optional array $params  A hash containing connection parameters. 
    65      */ 
     61     *
     62     * @access public
     63     *
     64     * @param optional array $params  A hash containing connection parameters.
     65     */
    6666    function PEdit($authorized=false)
    6767    {
     
    6969            $this->_authorized = true;
    7070        }
    71        
     71
    7272        $this->_filename = $_SERVER['SCRIPT_FILENAME'];
    7373        if (empty($this->_filename)) {
     
    7575            die;
    7676        }
    77        
     77
    7878        $this->op = getFormData('op');
    79        
     79
    8080        switch ($this->op) {
    8181        case 'Save' :
     
    9191        }
    9292    }
    93    
     93
    9494    /**
    9595     * Stores a variable in the pedit data array with the content name, and type of form.
    96      * 
    97      * @access public 
    98      * 
     96     *
     97     * @access public
     98     *
    9999     * @param string $content         The variable containing the text to store.
    100      * @param string $name            The name of the variable. 
    101      * @param string $type            The type of form element to use. 
    102      * @param optional int $form_size The size of the form element. 
     100     * @param string $name            The name of the variable.
     101     * @param string $type            The type of form element to use.
     102     * @param optional int $form_size The size of the form element.
    103103     */
    104104    function set($content, $name, $type, $form_size=null)
    105105    {
    106106        $this->_data[$name] = array(
    107             'type' => $type, 
    108             'content' => $content, 
     107            'type' => $type,
     108            'content' => $content,
    109109            'form_size' => $form_size
    110110        );
    111111    }
    112    
     112
    113113    /**
    114114     * Stores a checkbox variable in the pedit data array with the content name, and type of form.
    115      * 
    116      * @access public 
    117      * 
     115     *
     116     * @access public
     117     *
    118118     * @param string $content            The variable containing the text to store.
    119      * @param string $name               The name of the variable. 
    120      * @param string $corresponding_text The text that corresponds to this checkbox. 
     119     * @param string $name               The name of the variable.
     120     * @param string $corresponding_text The text that corresponds to this checkbox.
    121121     */
    122122    function setCheckbox($content, $name, $corresponding_text)
     
    124124        if (isset($content) && isset($name) && isset($corresponding_text)) {
    125125            $this->_data[$name] = array(
    126                 'type' => 'checkbox', 
    127                 'content' => $content, 
     126                'type' => 'checkbox',
     127                'content' => $content,
    128128                'corresponding_text' => $corresponding_text
    129129            );
     
    131131    }
    132132
    133     /** 
     133    /**
    134134     * Tests if we are should display page contents.
    135      * 
    136      * @access public 
     135     *
     136     * @access public
    137137     *
    138138     * @return bool        true if we are displaying page normally, false if editing page, or viewing versions.
    139      */ 
     139     */
    140140    function displayMode()
    141141    {
     
    144144        }
    145145    }
    146    
    147 
    148     /** 
     146
     147
     148    /**
    149149     * Prints an HTML list of versions of current file, with the filesize
    150      * and links to view and restore the file. 
    151      * 
    152      * @access public 
    153      */ 
     150     * and links to view and restore the file.
     151     *
     152     * @access public
     153     */
    154154    function printVersions()
    155155    {
     
    168168                    <td nowrap="nowrap"><p>&nbsp;&nbsp;&nbsp;[<a href="<?php echo App::oHREF($_SERVER['PHP_SELF'] . '?op=Restore&with_file=' . $v['filename'] . '&file_hash=' . md5('frog_guts' . $this->_filename)); ?>"><?php echo _("restore"); ?></a>]</p></td>
    169169                    </tr>
    170                     <?php   
     170                    <?php
    171171                }
    172172                ?></table><?php
     
    176176    }
    177177
    178     /** 
     178    /**
    179179     * Returns the contents of a data variable. The variable must first be 'set'.
    180      * 
    181      * @access public 
    182      * 
    183      * @param string $name   The name of the variable to return. 
     180     *
     181     * @access public
     182     *
     183     * @param string $name   The name of the variable to return.
    184184     *
    185185     * @return string        The trimmed content of the named data.
    186      */ 
     186     */
    187187    function getContent($name, $preserve_html=true)
    188188    {
     
    198198    }
    199199
    200     /** 
     200    /**
    201201     * Prints the contents of a data variable. The variable must first be 'set'.
    202      * 
    203      * @access public 
    204      * 
    205      * @param string $name      The name of the variable to print. 
    206      */ 
     202     *
     203     * @access public
     204     *
     205     * @param string $name      The name of the variable to print.
     206     */
    207207    function printContent($name, $preserve_html=true)
    208208    {
    209209        echo $this->getContent($name, $preserve_html);
    210210    }
    211    
    212     /** 
    213      * Prints the HTML forms corresponding to pedit variables. Each variable 
     211
     212    /**
     213     * Prints the HTML forms corresponding to pedit variables. Each variable
    214214     * must first be 'set'.
    215      * 
    216      * @access public 
    217      * 
    218      * @param string $name      The name of the variable. 
    219      */ 
     215     *
     216     * @access public
     217     *
     218     * @param string $name      The name of the variable.
     219     */
    220220    function printForm($name)
    221221    {
     
    245245        }
    246246    }
    247    
    248     /** 
     247
     248    /**
    249249     * Loops through the PEdit data array and prints all the HTML forms corresponding
    250250     * to all pedit variables, in the order in which they were 'set'.
    251      * 
    252      * @access public 
    253      */ 
     251     *
     252     * @access public
     253     */
    254254    function printAllForms()
    255255    {
     
    260260        }
    261261    }
    262    
    263     /** 
    264      * Prints the beginning <form> HTML tag, as well as hidden input forms. 
    265      * 
    266      * @return bool  False if unauthorized or current page is a version. 
    267      */ 
     262
     263    /**
     264     * Prints the beginning <form> HTML tag, as well as hidden input forms.
     265     *
     266     * @return bool  False if unauthorized or current page is a version.
     267     */
    268268    function formBegin()
    269269    {
     
    289289        }
    290290    }
    291    
    292     /** 
     291
     292    /**
    293293     * Prints the endig </form> HTML tag, as well as buttons used during
    294      * different operations. 
    295      * 
    296      * @return bool  False if unauthorized or current page is a version. 
    297      */ 
     294     * different operations.
     295     *
     296     * @return bool  False if unauthorized or current page is a version.
     297     */
    298298    function formEnd()
    299299    {
     
    330330        }
    331331    }
    332    
    333     /** 
     332
     333    /**
    334334     * Saves the POSTed data by overwriting the pedit variables in the
    335      * current file. 
    336      * 
    337      * @access private 
    338      * 
    339      * @return bool  False if unauthorized or on failure. True on success. 
    340      */ 
     335     * current file.
     336     *
     337     * @access private
     338     *
     339     * @return bool  False if unauthorized or on failure. True on success.
     340     */
    341341    function _writeData()
    342342    {
     
    368368                }
    369369            }
    370            
     370
    371371            // Search and replace all blocks.
    372372            $whole_file = preg_replace($search, $replace, $whole_file);
     
    383383                return false;
    384384            }
    385            
     385
    386386            // Open file for writing and truncate to zero length.
    387387            if (is_writable($this->_filename) && $fp = fopen($this->_filename, 'w')) {
     
    402402        }
    403403    }
    404    
    405     /** 
    406      * Makes a copy of the current file with the unix timestamp appended to the 
     404
     405    /**
     406     * Makes a copy of the current file with the unix timestamp appended to the
    407407     * filename. Deletes old versions based on threshold of age and qty.
    408      * 
    409      * @access private 
    410      * 
     408     *
     409     * @access private
     410     *
    411411     * @param optional boolean $do_cleanup    Set to false to turn off the
    412412     *                                        cleanup routine.
    413      * 
    414      * @return bool  False if unauthorized or on failure. True on success. 
    415      */ 
     413     *
     414     * @return bool  False if unauthorized or on failure. True on success.
     415     */
    416416    function _createVersion($do_cleanup=true)
    417417    {
     
    425425        }
    426426        $versions = $this->_getVersions();
    427        
     427
    428428        // Clean up old versions.
    429429        if (is_array($versions) && sizeof($versions) > $this->versions_min_qty && $do_cleanup) {
     
    431431            $oldest = array_pop($versions);
    432432            // Loop while minimum X qty && minimum X days worth but never more than 100 qty.
    433             while ((sizeof($versions) > $this->versions_min_qty 
    434             && $oldest['unixtime'] < mktime(date('H'),date('i'),date('s'),date('m'),date('d')-$this->versions_min_days,date('Y'))) 
     433            while ((sizeof($versions) > $this->versions_min_qty
     434            && $oldest['unixtime'] < mktime(date('H'),date('i'),date('s'),date('m'),date('d')-$this->versions_min_days,date('Y')))
    435435            || sizeof($versions) > 100) {
    436436                unlink(dirname($this->_filename) . '/' . $oldest['filename']);
     
    444444            return false;
    445445        }
    446        
     446
    447447        return true;
    448448    }
    449    
    450     /** 
    451      * Returns an array of all archived versions of the current file, 
     449
     450    /**
     451     * Returns an array of all archived versions of the current file,
    452452     * sorted with newest versions at the top of the array.
    453      * 
     453     *
    454454     * @access private
    455455     *
    456      * @return array  Array of versions. 
    457      */ 
     456     * @return array  Array of versions.
     457     */
    458458    function _getVersions()
    459459    {
     
    464464                preg_match('/.+__(\d+)\.php/', $version, $time);
    465465                $versions[] = array(
    466                     'filename' => $version, 
    467                     'unixtime' => $time[1], 
     466                    'filename' => $version,
     467                    'unixtime' => $time[1],
    468468                    'filesize' => filesize(dirname($this->_filename) . '/' . $version)
    469469                );
     
    478478        }
    479479    }
    480    
    481     /** 
     480
     481    /**
    482482     * Makes a version backup of the current file, then copies the specified
    483      * archived version over the current file. 
    484      * 
    485      * @access private 
    486      * 
     483     * archived version over the current file.
     484     *
     485     * @access private
     486     *
    487487     * @param string $with_file    Filename of archived version to restore.
    488488     *
    489      * @return bool  False if unauthorized. True on success. 
    490      */ 
     489     * @return bool  False if unauthorized. True on success.
     490     */
    491491    function _restoreVersion($with_file)
    492492    {
     
    494494            return false;
    495495        }
    496            
     496
    497497        if (is_writable($this->_filename)) {
    498498            // Make certain a version is created.
     
    501501                return false;
    502502            }
    503        
    504             // Do the actual copy. 
     503
     504            // Do the actual copy.
    505505            if (!copy(dirname($this->_filename) . '/' . $with_file, $this->_filename)) {
    506506                App::logMsg(sprintf('PEdit error: failed copying old version: %s', $with_file), LOG_NOTICE, __FILE__, __LINE__);
    507507                return false;
    508508            }
    509            
     509
    510510            // Success!
    511511            return true;
     
    515515        }
    516516    }
    517    
     517
    518518} // End class.
    519519
Note: See TracChangeset for help on using the changeset viewer.