Changeset 42 for trunk/lib/PEdit.inc.php
- Timestamp:
- Dec 18, 2005 12:16:03 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/PEdit.inc.php
r41 r42 2 2 /** 3 3 * 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 5 5 * circumstances, but an authenticated user can 'edit' the document-- 6 6 * data stored in vars will be shown in html form elements to be editied … … 16 16 * require_once 'codebase/lib/PEdit.inc.php'; 17 17 * $p = new PEdit($auth->hasClearance('pedit')); 18 * 18 * 19 19 * $title = <<<P_E_D_I_T_title 20 20 * Using Burritos to Improve Student Learnin' 21 21 * P_E_D_I_T_title; 22 22 * $p->set($title, 'title', 'textbox'); 23 * 23 * 24 24 * // Begin content. Include a header or something right here. 25 25 * $p->printContent('title'); 26 * 26 * 27 27 * // Prints beginning form tags and special hidden forms. (Only happens if page is NOT a archived version.) 28 28 * $p->formBegin(); 29 * 29 * 30 30 * // Print editing form elements. (Only happens if op == Edit.) 31 31 * $p->printForm('title'); 32 * 32 * 33 33 * // Print versions list. (Only happens if op == Versions.) 34 34 * $p->printVersions(); 35 * 35 * 36 36 * // Prints ending form tags and command buttons.(Only happens if page is NOT a archived version.) 37 37 * $p->formEnd(); 38 38 * 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> 41 41 * @version 1.1 42 42 */ … … 51 51 // Tags that are not stripped from the POSTed data. 52 52 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 /** 56 56 * 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 58 58 * operations that actually modify data (save, restore) are treated differently 59 59 * than view operations (versions, '' - default). They die redirect so you see 60 60 * 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 */ 66 66 function PEdit($authorized=false) 67 67 { … … 69 69 $this->_authorized = true; 70 70 } 71 71 72 72 $this->_filename = $_SERVER['SCRIPT_FILENAME']; 73 73 if (empty($this->_filename)) { … … 75 75 die; 76 76 } 77 77 78 78 $this->op = getFormData('op'); 79 79 80 80 switch ($this->op) { 81 81 case 'Save' : … … 91 91 } 92 92 } 93 93 94 94 /** 95 95 * 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 * 99 99 * @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. 103 103 */ 104 104 function set($content, $name, $type, $form_size=null) 105 105 { 106 106 $this->_data[$name] = array( 107 'type' => $type, 108 'content' => $content, 107 'type' => $type, 108 'content' => $content, 109 109 'form_size' => $form_size 110 110 ); 111 111 } 112 112 113 113 /** 114 114 * 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 * 118 118 * @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. 121 121 */ 122 122 function setCheckbox($content, $name, $corresponding_text) … … 124 124 if (isset($content) && isset($name) && isset($corresponding_text)) { 125 125 $this->_data[$name] = array( 126 'type' => 'checkbox', 127 'content' => $content, 126 'type' => 'checkbox', 127 'content' => $content, 128 128 'corresponding_text' => $corresponding_text 129 129 ); … … 131 131 } 132 132 133 /** 133 /** 134 134 * Tests if we are should display page contents. 135 * 136 * @access public 135 * 136 * @access public 137 137 * 138 138 * @return bool true if we are displaying page normally, false if editing page, or viewing versions. 139 */ 139 */ 140 140 function displayMode() 141 141 { … … 144 144 } 145 145 } 146 147 148 /** 146 147 148 /** 149 149 * 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 */ 154 154 function printVersions() 155 155 { … … 168 168 <td nowrap="nowrap"><p> [<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> 169 169 </tr> 170 <?php 170 <?php 171 171 } 172 172 ?></table><?php … … 176 176 } 177 177 178 /** 178 /** 179 179 * 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. 184 184 * 185 185 * @return string The trimmed content of the named data. 186 */ 186 */ 187 187 function getContent($name, $preserve_html=true) 188 188 { … … 198 198 } 199 199 200 /** 200 /** 201 201 * 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 */ 207 207 function printContent($name, $preserve_html=true) 208 208 { 209 209 echo $this->getContent($name, $preserve_html); 210 210 } 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 214 214 * 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 */ 220 220 function printForm($name) 221 221 { … … 245 245 } 246 246 } 247 248 /** 247 248 /** 249 249 * Loops through the PEdit data array and prints all the HTML forms corresponding 250 250 * to all pedit variables, in the order in which they were 'set'. 251 * 252 * @access public 253 */ 251 * 252 * @access public 253 */ 254 254 function printAllForms() 255 255 { … … 260 260 } 261 261 } 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 */ 268 268 function formBegin() 269 269 { … … 289 289 } 290 290 } 291 292 /** 291 292 /** 293 293 * 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 */ 298 298 function formEnd() 299 299 { … … 330 330 } 331 331 } 332 333 /** 332 333 /** 334 334 * 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 */ 341 341 function _writeData() 342 342 { … … 368 368 } 369 369 } 370 370 371 371 // Search and replace all blocks. 372 372 $whole_file = preg_replace($search, $replace, $whole_file); … … 383 383 return false; 384 384 } 385 385 386 386 // Open file for writing and truncate to zero length. 387 387 if (is_writable($this->_filename) && $fp = fopen($this->_filename, 'w')) { … … 402 402 } 403 403 } 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 407 407 * filename. Deletes old versions based on threshold of age and qty. 408 * 409 * @access private 410 * 408 * 409 * @access private 410 * 411 411 * @param optional boolean $do_cleanup Set to false to turn off the 412 412 * 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 */ 416 416 function _createVersion($do_cleanup=true) 417 417 { … … 425 425 } 426 426 $versions = $this->_getVersions(); 427 427 428 428 // Clean up old versions. 429 429 if (is_array($versions) && sizeof($versions) > $this->versions_min_qty && $do_cleanup) { … … 431 431 $oldest = array_pop($versions); 432 432 // 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'))) 435 435 || sizeof($versions) > 100) { 436 436 unlink(dirname($this->_filename) . '/' . $oldest['filename']); … … 444 444 return false; 445 445 } 446 446 447 447 return true; 448 448 } 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, 452 452 * sorted with newest versions at the top of the array. 453 * 453 * 454 454 * @access private 455 455 * 456 * @return array Array of versions. 457 */ 456 * @return array Array of versions. 457 */ 458 458 function _getVersions() 459 459 { … … 464 464 preg_match('/.+__(\d+)\.php/', $version, $time); 465 465 $versions[] = array( 466 'filename' => $version, 467 'unixtime' => $time[1], 466 'filename' => $version, 467 'unixtime' => $time[1], 468 468 'filesize' => filesize(dirname($this->_filename) . '/' . $version) 469 469 ); … … 478 478 } 479 479 } 480 481 /** 480 481 /** 482 482 * 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 * 487 487 * @param string $with_file Filename of archived version to restore. 488 488 * 489 * @return bool False if unauthorized. True on success. 490 */ 489 * @return bool False if unauthorized. True on success. 490 */ 491 491 function _restoreVersion($with_file) 492 492 { … … 494 494 return false; 495 495 } 496 496 497 497 if (is_writable($this->_filename)) { 498 498 // Make certain a version is created. … … 501 501 return false; 502 502 } 503 504 // Do the actual copy. 503 504 // Do the actual copy. 505 505 if (!copy(dirname($this->_filename) . '/' . $with_file, $this->_filename)) { 506 506 App::logMsg(sprintf('PEdit error: failed copying old version: %s', $with_file), LOG_NOTICE, __FILE__, __LINE__); 507 507 return false; 508 508 } 509 509 510 510 // Success! 511 511 return true; … … 515 515 } 516 516 } 517 517 518 518 } // End class. 519 519
Note: See TracChangeset
for help on using the changeset viewer.