Changeset 136 for trunk/lib/PEdit.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/PEdit.inc.php
r124 r136 1 1 <?php 2 2 /** 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 4 7 * which will be printed to the client browser under normal 5 8 * circumstances, but an authenticated user can 'edit' the document-- … … 11 14 * To use, include this file, initialize variables, 12 15 * 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: 14 23 15 24 // Initialize PEdit object. … … 37 46 $pedit->formEnd(); 38 47 39 * @author Quinn Comendant <quinn@strangecode.com>40 * @concept Beau Smith <beau@beausmith.com>41 * @version 2.042 48 */ 43 49 class PEdit { … … 95 101 function setParam($params) 96 102 { 103 $app =& App::getInstance(); 104 97 105 if (isset($params) && is_array($params)) { 98 106 // Merge new parameters with old overriding only those passed. 99 107 $this->_params = array_merge($this->_params, $params); 100 108 } 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__); 102 110 } 103 111 } … … 112 120 function getParam($param) 113 121 { 122 $app =& App::getInstance(); 123 114 124 if (isset($this->_params[$param])) { 115 125 return $this->_params[$param]; 116 126 } 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__); 118 128 return null; 119 129 } … … 129 139 function start($initialize_data_file=false) 130 140 { 141 $app =& App::getInstance(); 142 131 143 if (!is_dir($this->getParam('data_dir'))) { 132 144 trigger_error(sprintf('PEdit data directory not found: %s', $this->getParam('data_dir')), E_USER_WARNING); … … 143 155 case 'Save' : 144 156 if ($this->_writeData()) { 145 App::dieURL($_SERVER['PHP_SELF']);157 $app->dieURL($_SERVER['PHP_SELF']); 146 158 } 147 159 break; 148 160 case 'Restore' : 149 161 if ($this->_restoreVersion(getFormData('version'))) { 150 App::dieURL($_SERVER['PHP_SELF']);162 $app->dieURL($_SERVER['PHP_SELF']); 151 163 } 152 164 break; 153 165 case 'View' : 154 166 $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__); 156 168 break; 157 169 } … … 176 188 function set($name, $options=array()) 177 189 { 190 $app =& App::getInstance(); 191 178 192 $name = preg_replace('/\s/', '_', $name); 179 193 if (!isset($this->_data[$name])) { 180 194 $this->_data[$name] = array_merge(array('content' => ''), $options); 181 195 } 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__); 183 197 } 184 198 } … … 208 222 function formBegin() 209 223 { 224 $app =& App::getInstance(); 225 210 226 if (!$this->_authorized || empty($this->_data)) { 211 227 return false; … … 216 232 <input type="hidden" name="file_hash" value="<?php echo $this->_fileHash(); ?>" /> 217 233 <?php 218 App::printHiddenSession();234 $app->printHiddenSession(); 219 235 switch ($this->op) { 220 236 case 'Edit' : … … 345 361 function printVersions() 346 362 { 363 $app =& App::getInstance(); 364 347 365 if ($this->_authorized && $this->op == 'Versions') { 348 366 // Print versions and commands to view/restore. … … 362 380 ?> 363 381 <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> 366 384 <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> 368 386 </tr> 369 387 <?php … … 386 404 function _fileHash() 387 405 { 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']); 389 409 } 390 410 … … 399 419 function _loadDataFile() 400 420 { 421 $app =& App::getInstance(); 422 401 423 if (!file_exists($this->_data_file)) { 402 424 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__); 404 426 return false; 405 427 } … … 408 430 $status = $this->xml_unserializer->unserialize($xml_file_contents, false); 409 431 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__); 411 433 return false; 412 434 } … … 436 458 function _initializeDataFile() 437 459 { 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__); 439 463 $xml_file_contents = $this->xml_serializer->serialize($this->_data); 440 464 return $this->_filePutContents($this->_data_file, $xml_file_contents); … … 450 474 function _writeData() 451 475 { 476 $app =& App::getInstance(); 477 452 478 if (!$this->_authorized) { 453 479 return false; … … 455 481 if ($this->_fileHash() != getFormData('file_hash')) { 456 482 // 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__); 458 484 return false; 459 485 } … … 466 492 $this->_deleteOldVersions(); 467 493 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__); 469 495 return false; 470 496 } … … 496 522 function _filePutContents($filename, $content) 497 523 { 524 $app =& App::getInstance(); 525 498 526 // Ensure requested filename is within the pedit data dir. 499 527 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__); 501 529 return false; 502 530 } … … 510 538 if (!is_dir($curr_path)) { 511 539 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__); 513 541 return false; 514 542 } … … 522 550 flock($fp, LOCK_UN); 523 551 } 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__); 525 553 return false; 526 554 } 527 555 fclose($fp); 528 556 // 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__); 530 558 return true; 531 559 } 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__); 533 561 return false; 534 562 } … … 544 572 function _createVersion() 545 573 { 574 $app =& App::getInstance(); 575 546 576 if (!$this->_authorized) { 547 577 return false; … … 549 579 if ($this->_fileHash() != getFormData('file_hash')) { 550 580 // 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__); 552 582 return false; 553 583 } … … 555 585 // Ensure current data file exists. 556 586 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__); 558 588 return false; 559 589 } … … 563 593 $version_file = sprintf('%s__%s.xml', preg_replace('/\.xml$/', '', $this->_data_file), time()); 564 594 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__); 566 596 return false; 567 597 } … … 580 610 function _deleteOldVersions() 581 611 { 612 $app =& App::getInstance(); 613 582 614 $version_files = $this->_getVersions(); 583 615 if (is_array($version_files) && sizeof($version_files) > $this->getParam('versions_min_qty')) { … … 590 622 $del_file = dirname($this->_data_file) . '/' . $oldest['filename']; 591 623 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__); 593 625 } 594 626 $oldest = array_pop($version_files); … … 637 669 function _restoreVersion($version) 638 670 { 671 $app =& App::getInstance(); 672 639 673 if (!$this->_authorized) { 640 674 return false; … … 646 680 // Ensure specified version exists. 647 681 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__); 649 683 return false; 650 684 } … … 652 686 // Make certain a version is created. 653 687 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__); 655 689 return false; 656 690 } … … 658 692 // Do the actual copy. 659 693 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__); 661 695 return false; 662 696 } 663 697 664 698 // 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__); 666 700 return true; 667 701 }
Note: See TracChangeset
for help on using the changeset viewer.