[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * PEdit:: provides a mechanism to store text in php variables |
---|
[42] | 4 | * which will be printed to the client browser under normal |
---|
[1] | 5 | * circumstances, but an authenticated user can 'edit' the document-- |
---|
| 6 | * data stored in vars will be shown in html form elements to be editied |
---|
[92] | 7 | * and saved. Posted data is stored in XML format in a specified data dir. |
---|
| 8 | * A copy of the previous version is saved with the unix |
---|
[1] | 9 | * timestamp as part of the filename. This allows reverting to previous versions. |
---|
| 10 | * |
---|
| 11 | * To use, include this file, initialize variables, |
---|
| 12 | * and call printing/editing functions where you want data and forms to |
---|
| 13 | * show up. Below is an example of use: |
---|
[92] | 14 | |
---|
| 15 | // Initialize PEdit object. |
---|
| 16 | require_once 'codebase/lib/PEdit.inc.php'; |
---|
| 17 | $pedit = new PEdit(array( |
---|
| 18 | 'data_dir' => COMMON_BASE . '/html/_pedit_data', |
---|
| 19 | 'authorized' => true, |
---|
| 20 | )); |
---|
| 21 | |
---|
| 22 | // Setup content data types. |
---|
| 23 | $pedit->set('title'); |
---|
| 24 | $pedit->set('content', array('type' => 'textarea')); |
---|
| 25 | |
---|
| 26 | // After setting all parameters and data, load the data. |
---|
| 27 | $pedit->start(); |
---|
| 28 | |
---|
| 29 | // Print content. |
---|
| 30 | echo $pedit->get('title'); |
---|
| 31 | echo $pedit->get('content'); |
---|
| 32 | |
---|
| 33 | // Print additional PEdit functionality. |
---|
| 34 | $pedit->formBegin(); |
---|
| 35 | $pedit->printAllForms(); |
---|
| 36 | $pedit->printVersions(); |
---|
| 37 | $pedit->formEnd(); |
---|
| 38 | |
---|
[42] | 39 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 40 | * @concept Beau Smith <beau@beausmith.com> |
---|
[92] | 41 | * @version 2.0 |
---|
[1] | 42 | */ |
---|
[92] | 43 | class PEdit { |
---|
[1] | 44 | |
---|
[92] | 45 | // PEdit object parameters. |
---|
| 46 | var $_params = array( |
---|
| 47 | 'data_dir' => '', |
---|
| 48 | 'character_set' => 'utf-8', |
---|
| 49 | 'versions_min_qty' => 20, |
---|
| 50 | 'versions_min_days' => 10, |
---|
| 51 | ); |
---|
[42] | 52 | |
---|
[92] | 53 | var $_data = array(); // Array to store loaded data. |
---|
| 54 | var $_data_file = ''; // Full file path to the pedit data file. |
---|
| 55 | var $_authorized = false; // User is authenticated to see extended functions. |
---|
| 56 | var $_data_loaded = false; |
---|
| 57 | var $op = ''; |
---|
[42] | 58 | |
---|
| 59 | /** |
---|
[92] | 60 | * Constructs a new PEdit object. Initializes what file is being operated with |
---|
| 61 | * (PHP_SELF) and what that operation is. The two |
---|
[1] | 62 | * operations that actually modify data (save, restore) are treated differently |
---|
[92] | 63 | * than view operations (versions, view, default). They die redirect so you see |
---|
[1] | 64 | * the page you just modified. |
---|
[42] | 65 | * |
---|
| 66 | * @access public |
---|
| 67 | * @param optional array $params A hash containing connection parameters. |
---|
| 68 | */ |
---|
[92] | 69 | function PEdit($params) |
---|
[1] | 70 | { |
---|
[92] | 71 | $this->setParam($params); |
---|
| 72 | |
---|
| 73 | if ($this->getParam('authorized') === true) { |
---|
[1] | 74 | $this->_authorized = true; |
---|
| 75 | } |
---|
[92] | 76 | |
---|
| 77 | // Setup PEAR XML libraries. |
---|
| 78 | require_once 'XML/Unserializer.php'; |
---|
| 79 | $this->xml_unserializer =& new XML_Unserializer(array( |
---|
| 80 | XML_UNSERIALIZER_OPTION_COMPLEXTYPE => 'array', |
---|
| 81 | )); |
---|
| 82 | require_once 'XML/Serializer.php'; |
---|
| 83 | $this->xml_serializer =& new XML_Serializer(array( |
---|
| 84 | XML_SERIALIZER_OPTION_INDENT => ' ', |
---|
| 85 | XML_SERIALIZER_OPTION_RETURN_RESULT => true |
---|
| 86 | )); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 91 | * |
---|
| 92 | * @access public |
---|
| 93 | * @param array $params Array of parameters (key => val pairs). |
---|
| 94 | */ |
---|
| 95 | function setParam($params) |
---|
| 96 | { |
---|
[128] | 97 | $app =& App::getInstance(); |
---|
| 98 | |
---|
[92] | 99 | if (isset($params) && is_array($params)) { |
---|
| 100 | // Merge new parameters with old overriding only those passed. |
---|
| 101 | $this->_params = array_merge($this->_params, $params); |
---|
| 102 | } else { |
---|
[128] | 103 | $app->logMsg(sprintf('Parameters are not an array: %s', $params), LOG_WARNING, __FILE__, __LINE__); |
---|
[92] | 104 | } |
---|
| 105 | } |
---|
[42] | 106 | |
---|
[92] | 107 | /** |
---|
| 108 | * Return the value of a parameter, if it exists. |
---|
| 109 | * |
---|
| 110 | * @access public |
---|
| 111 | * @param string $param Which parameter to return. |
---|
| 112 | * @return mixed Configured parameter value. |
---|
| 113 | */ |
---|
| 114 | function getParam($param) |
---|
| 115 | { |
---|
[128] | 116 | $app =& App::getInstance(); |
---|
| 117 | |
---|
[92] | 118 | if (isset($this->_params[$param])) { |
---|
| 119 | return $this->_params[$param]; |
---|
| 120 | } else { |
---|
[128] | 121 | $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__); |
---|
[92] | 122 | return null; |
---|
[1] | 123 | } |
---|
[92] | 124 | } |
---|
| 125 | |
---|
| 126 | /* |
---|
| 127 | * Load the pedit data and run automatic functions. |
---|
| 128 | * |
---|
| 129 | * @access public |
---|
| 130 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 131 | * @since 12 Apr 2006 12:43:47 |
---|
| 132 | */ |
---|
| 133 | function start($initialize_data_file=false) |
---|
| 134 | { |
---|
[128] | 135 | $app =& App::getInstance(); |
---|
| 136 | |
---|
[92] | 137 | if (!is_dir($this->getParam('data_dir'))) { |
---|
| 138 | trigger_error(sprintf('PEdit data directory not found: %s', $this->getParam('data_dir')), E_USER_WARNING); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | // The location of the data file. (i.e.: "COMMON_DIR/html/_pedit_data/news/index.xml") |
---|
| 142 | $this->_data_file = sprintf('%s%s.xml', $this->getParam('data_dir'), $_SERVER['PHP_SELF']); |
---|
[42] | 143 | |
---|
[92] | 144 | // op is used throughout the script to determine state. |
---|
[1] | 145 | $this->op = getFormData('op'); |
---|
[42] | 146 | |
---|
[92] | 147 | // Automatic functions based on state. |
---|
[1] | 148 | switch ($this->op) { |
---|
| 149 | case 'Save' : |
---|
| 150 | if ($this->_writeData()) { |
---|
[128] | 151 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 152 | } |
---|
| 153 | break; |
---|
| 154 | case 'Restore' : |
---|
[92] | 155 | if ($this->_restoreVersion(getFormData('version'))) { |
---|
[128] | 156 | $app->dieURL($_SERVER['PHP_SELF']); |
---|
[1] | 157 | } |
---|
| 158 | break; |
---|
[92] | 159 | case 'View' : |
---|
| 160 | $this->_data_file = sprintf('%s%s__%s.xml', $this->getParam('data_dir'), $_SERVER['PHP_SELF'], getFormData('version')); |
---|
[128] | 161 | $app->raiseMsg(sprintf(_("This is <em><strong>only a preview</strong></em> of version %s."), getFormData('version')), MSG_NOTICE, __FILE__, __LINE__); |
---|
[92] | 162 | break; |
---|
[1] | 163 | } |
---|
[92] | 164 | |
---|
| 165 | // Load data. |
---|
| 166 | $this->_loadDataFile(); |
---|
| 167 | |
---|
| 168 | if ($initialize_data_file === true) { |
---|
| 169 | $this->_createVersion(); |
---|
| 170 | $this->_initializeDataFile(); |
---|
| 171 | } |
---|
[1] | 172 | } |
---|
[42] | 173 | |
---|
[1] | 174 | /** |
---|
| 175 | * Stores a variable in the pedit data array with the content name, and type of form. |
---|
[42] | 176 | * |
---|
| 177 | * @access public |
---|
| 178 | * |
---|
[92] | 179 | * @param string $content The variable containing the text to store. |
---|
| 180 | * @param array $options Additional options to store with this data. |
---|
[1] | 181 | */ |
---|
[92] | 182 | function set($name, $options=array()) |
---|
[1] | 183 | { |
---|
[128] | 184 | $app =& App::getInstance(); |
---|
| 185 | |
---|
[92] | 186 | $name = preg_replace('/\s/', '_', $name); |
---|
| 187 | if (!isset($this->_data[$name])) { |
---|
| 188 | $this->_data[$name] = array_merge(array('content' => ''), $options); |
---|
| 189 | } else { |
---|
[128] | 190 | $app->logMsg(sprintf('Duplicate set data: %s', $name), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 191 | } |
---|
| 192 | } |
---|
| 193 | |
---|
[42] | 194 | /** |
---|
[92] | 195 | * Returns the contents of a data variable. The variable must first be 'set'. |
---|
[1] | 196 | * |
---|
[42] | 197 | * @access public |
---|
[92] | 198 | * @param string $name The name of the variable to return. |
---|
| 199 | * @return string The trimmed content of the named data. |
---|
[42] | 200 | */ |
---|
[92] | 201 | function get($name) |
---|
[1] | 202 | { |
---|
[92] | 203 | $name = preg_replace('/\s/', '_', $name); |
---|
[1] | 204 | if ($this->op != 'Edit' && $this->op != 'Versions' && isset($this->_data[$name]['content'])) { |
---|
[92] | 205 | return $this->_data[$name]['content']; |
---|
| 206 | } else { |
---|
| 207 | return ''; |
---|
[1] | 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
[42] | 211 | /** |
---|
[92] | 212 | * Prints the beginning <form> HTML tag, as well as hidden input forms. |
---|
[42] | 213 | * |
---|
[92] | 214 | * @return bool False if unauthorized or current page is a version. |
---|
[42] | 215 | */ |
---|
[92] | 216 | function formBegin() |
---|
[1] | 217 | { |
---|
[128] | 218 | $app =& App::getInstance(); |
---|
| 219 | |
---|
[93] | 220 | if (!$this->_authorized || empty($this->_data)) { |
---|
[92] | 221 | return false; |
---|
| 222 | } |
---|
[101] | 223 | ?> |
---|
| 224 | <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="sc-pedit-form"> |
---|
| 225 | <input type="hidden" name="filename" value="<?php echo $_SERVER['PHP_SELF']; ?>" /> |
---|
| 226 | <input type="hidden" name="file_hash" value="<?php echo $this->_fileHash(); ?>" /> |
---|
| 227 | <?php |
---|
[128] | 228 | $app->printHiddenSession(); |
---|
[92] | 229 | switch ($this->op) { |
---|
| 230 | case 'Edit' : |
---|
[101] | 231 | ?> |
---|
| 232 | <div class="sc-pedit-buttons"> |
---|
| 233 | <input type="submit" name="op" value="<?php echo _("Save"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Save"))?>" accesskey="<?php echo substr(_("Save"), 0, 1) ?>" /> |
---|
| 234 | <input type="submit" name="op" value="<?php echo _("Cancel"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Cancel"))?>" accesskey="<?php echo substr(_("Cancel"), 0, 1) ?>" /> |
---|
| 235 | </div> |
---|
| 236 | <?php |
---|
[92] | 237 | break; |
---|
| 238 | case 'View' : |
---|
[101] | 239 | ?> |
---|
| 240 | <input type="hidden" name="version" value="<?php echo getFormData('version'); ?>" /> |
---|
| 241 | <?php |
---|
[92] | 242 | break; |
---|
[1] | 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
[42] | 246 | /** |
---|
[92] | 247 | * Loops through the PEdit data array and prints all the HTML forms corresponding |
---|
| 248 | * to all pedit variables, in the order in which they were 'set'. |
---|
[1] | 249 | * |
---|
[42] | 250 | * @access public |
---|
| 251 | */ |
---|
[92] | 252 | function printAllForms() |
---|
[1] | 253 | { |
---|
[92] | 254 | if ($this->_authorized && $this->op == 'Edit' && is_array($this->_data) && $this->_data_loaded) { |
---|
| 255 | foreach ($this->_data as $name=>$d) { |
---|
| 256 | $this->printForm($name); |
---|
[1] | 257 | } |
---|
| 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
[42] | 261 | /** |
---|
| 262 | * Prints the HTML forms corresponding to pedit variables. Each variable |
---|
[1] | 263 | * must first be 'set'. |
---|
[42] | 264 | * |
---|
| 265 | * @access public |
---|
| 266 | * @param string $name The name of the variable. |
---|
[92] | 267 | * @param string $type Type of form to print. Currently only 'text' and 'textarea' supported. |
---|
[42] | 268 | */ |
---|
[92] | 269 | function printForm($name, $type='text') |
---|
[1] | 270 | { |
---|
[98] | 271 | if ($this->_authorized && $this->op == 'Edit' && $this->_data_loaded) { |
---|
[101] | 272 | ?> |
---|
| 273 | <div class="sc-pedit-item"> |
---|
| 274 | <?php |
---|
[92] | 275 | $type = (isset($this->_data[$name]['type'])) ? $this->_data[$name]['type'] : $type; |
---|
[1] | 276 | // Print edit form. |
---|
[92] | 277 | switch ($type) { |
---|
| 278 | case 'text' : |
---|
| 279 | default : |
---|
[101] | 280 | ?> |
---|
| 281 | <label><?php echo ucfirst(str_replace('_', ' ', $name)); ?></label> |
---|
[121] | 282 | <input type="text" name="_pedit_data[<?php echo $name; ?>]" id="sc-pedit-field-<?php echo $name; ?>" value="<?php echo oTxt($this->_data[$name]['content']); ?>" class="sc-full" /> |
---|
[101] | 283 | <?php |
---|
[1] | 284 | break; |
---|
| 285 | case 'textarea' : |
---|
[101] | 286 | ?> |
---|
| 287 | <label><?php echo ucfirst(str_replace('_', ' ', $name)); ?></label> |
---|
[121] | 288 | <textarea name="_pedit_data[<?php echo $name; ?>]" id="sc-pedit-field-<?php echo $name; ?>" rows="" cols="" class="full sc-tall"><?php echo oTxt($this->_data[$name]['content']); ?></textarea> |
---|
[101] | 289 | <?php |
---|
[1] | 290 | break; |
---|
| 291 | } |
---|
[101] | 292 | ?> |
---|
| 293 | </div> |
---|
| 294 | <?php |
---|
[1] | 295 | } |
---|
| 296 | } |
---|
[42] | 297 | |
---|
| 298 | /** |
---|
[1] | 299 | * Prints the endig </form> HTML tag, as well as buttons used during |
---|
[42] | 300 | * different operations. |
---|
| 301 | * |
---|
| 302 | * @return bool False if unauthorized or current page is a version. |
---|
| 303 | */ |
---|
[1] | 304 | function formEnd() |
---|
| 305 | { |
---|
[93] | 306 | if (!$this->_authorized || empty($this->_data)) { |
---|
[1] | 307 | // Don't show form elements for versioned documents. |
---|
| 308 | return false; |
---|
| 309 | } |
---|
| 310 | switch ($this->op) { |
---|
| 311 | case 'Edit' : |
---|
[101] | 312 | ?> |
---|
| 313 | <div class="sc-pedit-buttons"> |
---|
| 314 | <input type="submit" name="op" value="<?php echo _("Save"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Save"))?>" accesskey="<?php echo substr(_("Save"), 0, 1) ?>" /> |
---|
| 315 | <input type="submit" name="op" value="<?php echo _("Cancel"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Cancel"))?>" accesskey="<?php echo substr(_("Cancel"), 0, 1) ?>" /> |
---|
| 316 | </div> |
---|
| 317 | </form> |
---|
| 318 | <?php |
---|
[1] | 319 | break; |
---|
| 320 | case 'Versions' : |
---|
[101] | 321 | ?> |
---|
| 322 | <div class="sc-pedit-buttons"> |
---|
| 323 | <input type="submit" name="op" value="<?php echo _("Cancel"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Cancel"))?>" accesskey="<?php echo substr(_("Cancel"), 0, 1) ?>" /> |
---|
| 324 | </div> |
---|
| 325 | </form> |
---|
| 326 | <?php |
---|
[1] | 327 | break; |
---|
[92] | 328 | case 'View' : |
---|
[101] | 329 | ?> |
---|
| 330 | <div class="sc-pedit-buttons"> |
---|
| 331 | <input type="submit" name="op" value="<?php echo _("Restore"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Restore"))?>" accesskey="<?php echo substr(_("Restore"), 0, 1) ?>" /> |
---|
| 332 | <input type="submit" name="op" value="<?php echo _("Versions"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Versions"))?>" accesskey="<?php echo substr(_("Versions"), 0, 1) ?>" /> |
---|
| 333 | <input type="submit" name="op" value="<?php echo _("Cancel"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Cancel"))?>" accesskey="<?php echo substr(_("Cancel"), 0, 1) ?>" /> |
---|
| 334 | </div> |
---|
| 335 | </form> |
---|
| 336 | <?php |
---|
[92] | 337 | break; |
---|
[1] | 338 | default : |
---|
[101] | 339 | ?> |
---|
| 340 | <div class="sc-pedit-buttons"> |
---|
| 341 | <input type="submit" name="op" value="<?php echo _("Edit"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Edit"))?>" accesskey="<?php echo substr(_("Edit"), 0, 1) ?>" /> |
---|
| 342 | <input type="submit" name="op" value="<?php echo _("Versions"); ?>" title="<?php echo preg_replace('/^(\w)/i', '($1)', _("Versions"))?>" accesskey="<?php echo substr(_("Versions"), 0, 1) ?>" /> |
---|
| 343 | </div> |
---|
| 344 | </form> |
---|
| 345 | <?php |
---|
[1] | 346 | } |
---|
| 347 | } |
---|
[42] | 348 | |
---|
| 349 | /** |
---|
[92] | 350 | * Prints an HTML list of versions of current file, with the filesize |
---|
| 351 | * and links to view and restore the file. |
---|
| 352 | * |
---|
| 353 | * @access public |
---|
| 354 | */ |
---|
| 355 | function printVersions() |
---|
| 356 | { |
---|
[128] | 357 | $app =& App::getInstance(); |
---|
| 358 | |
---|
[92] | 359 | if ($this->_authorized && $this->op == 'Versions') { |
---|
| 360 | // Print versions and commands to view/restore. |
---|
| 361 | $version_files = $this->_getVersions(); |
---|
[101] | 362 | ?><h1><?php printf(_("%s saved versions of %s"), sizeof($version_files), basename($_SERVER['PHP_SELF'])); ?></h1><?php |
---|
[92] | 363 | if (is_array($version_files) && !empty($version_files)) { |
---|
[101] | 364 | ?> |
---|
| 365 | <table id="sc-pedit-versions-table"> |
---|
| 366 | <tr> |
---|
| 367 | <th><?php echo _("Date"); ?></th> |
---|
| 368 | <th><?php echo _("Time"); ?></th> |
---|
| 369 | <th><?php echo _("File size"); ?></th> |
---|
| 370 | <th><?php echo _("Version options"); ?></th> |
---|
| 371 | </tr> |
---|
| 372 | <?php |
---|
[92] | 373 | foreach ($version_files as $v) { |
---|
[101] | 374 | ?> |
---|
| 375 | <tr> |
---|
[128] | 376 | <td><?php echo date($app->getParam('date_format'), $v['unixtime']); ?></td> |
---|
| 377 | <td><?php echo date($app->getParam('time_format'), $v['unixtime']); ?></td> |
---|
[101] | 378 | <td><?php echo humanFileSize($v['filesize']); ?></td> |
---|
[128] | 379 | <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> |
---|
[101] | 380 | </tr> |
---|
| 381 | <?php |
---|
[92] | 382 | } |
---|
[101] | 383 | ?> |
---|
| 384 | </table> |
---|
[121] | 385 | <div class="sc-help"><?php printf(_("When there are more than %s versions, those over %s days old are deleted."), $this->getParam('versions_min_qty'), $this->getParam('versions_min_days')); ?></div> |
---|
[101] | 386 | <?php |
---|
[92] | 387 | } |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | /* |
---|
| 392 | * Returns a secreat hash for the current file. |
---|
| 393 | * |
---|
| 394 | * @access public |
---|
| 395 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 396 | * @since 12 Apr 2006 10:52:35 |
---|
| 397 | */ |
---|
| 398 | function _fileHash() |
---|
| 399 | { |
---|
[128] | 400 | $app =& App::getInstance(); |
---|
| 401 | |
---|
| 402 | return md5($app->getParam('signing_key') . $_SERVER['PHP_SELF']); |
---|
[92] | 403 | } |
---|
| 404 | |
---|
| 405 | /* |
---|
| 406 | * Load the XML data file into $this->_data. |
---|
| 407 | * |
---|
| 408 | * @access public |
---|
| 409 | * @return bool false on error |
---|
| 410 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 411 | * @since 11 Apr 2006 20:36:26 |
---|
| 412 | */ |
---|
| 413 | function _loadDataFile() |
---|
| 414 | { |
---|
[128] | 415 | $app =& App::getInstance(); |
---|
| 416 | |
---|
[92] | 417 | if (!file_exists($this->_data_file)) { |
---|
| 418 | if (!$this->_initializeDataFile()) { |
---|
[128] | 419 | $app->logMsg(sprintf('Initializing content file failed: %s', $this->_data_file), LOG_WARNING, __FILE__, __LINE__); |
---|
[92] | 420 | return false; |
---|
| 421 | } |
---|
| 422 | } |
---|
| 423 | $xml_file_contents = file_get_contents($this->_data_file); |
---|
| 424 | $status = $this->xml_unserializer->unserialize($xml_file_contents, false); |
---|
| 425 | if (PEAR::isError($status)) { |
---|
[128] | 426 | $app->logMsg(sprintf('XML_Unserialize error: %s', $status->getMessage()), LOG_WARNING, __FILE__, __LINE__); |
---|
[92] | 427 | return false; |
---|
| 428 | } |
---|
| 429 | $xml_file_data = $this->xml_unserializer->getUnserializedData(); |
---|
| 430 | |
---|
| 431 | // Only load data specified with set(), even though there may be more in the xml file. |
---|
| 432 | foreach ($this->_data as $name => $initial_data) { |
---|
| 433 | if (isset($xml_file_data[$name])) { |
---|
| 434 | $this->_data[$name] = array_merge($initial_data, $xml_file_data[$name]); |
---|
| 435 | } else { |
---|
| 436 | $this->_data[$name] = $initial_data; |
---|
| 437 | } |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | $this->_data_loaded = true; |
---|
| 441 | return true; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | /* |
---|
| 445 | * Start a new data file. |
---|
| 446 | * |
---|
| 447 | * @access public |
---|
| 448 | * @return The success value of both xml_serializer->serialize() and _filePutContents() |
---|
| 449 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 450 | * @since 11 Apr 2006 20:53:42 |
---|
| 451 | */ |
---|
| 452 | function _initializeDataFile() |
---|
| 453 | { |
---|
[128] | 454 | $app =& App::getInstance(); |
---|
| 455 | |
---|
| 456 | $app->logMsg(sprintf('Initializing data file: %s', $this->_data_file), LOG_INFO, __FILE__, __LINE__); |
---|
[92] | 457 | $xml_file_contents = $this->xml_serializer->serialize($this->_data); |
---|
| 458 | return $this->_filePutContents($this->_data_file, $xml_file_contents); |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | /** |
---|
[1] | 462 | * Saves the POSTed data by overwriting the pedit variables in the |
---|
[42] | 463 | * current file. |
---|
| 464 | * |
---|
| 465 | * @access private |
---|
| 466 | * @return bool False if unauthorized or on failure. True on success. |
---|
| 467 | */ |
---|
[1] | 468 | function _writeData() |
---|
| 469 | { |
---|
[128] | 470 | $app =& App::getInstance(); |
---|
| 471 | |
---|
[1] | 472 | if (!$this->_authorized) { |
---|
| 473 | return false; |
---|
| 474 | } |
---|
[92] | 475 | if ($this->_fileHash() != getFormData('file_hash')) { |
---|
[1] | 476 | // Posted data is NOT for this file! |
---|
[128] | 477 | $app->logMsg(sprintf('File_hash does not match current file.', null), LOG_WARNING, __FILE__, __LINE__); |
---|
[1] | 478 | return false; |
---|
| 479 | } |
---|
[42] | 480 | |
---|
[92] | 481 | // Scrub incoming data. Escape tags? |
---|
| 482 | $new_data = getFormData('_pedit_data'); |
---|
[1] | 483 | |
---|
[92] | 484 | if (is_array($new_data) && !empty($new_data)) { |
---|
[1] | 485 | // Make certain a version is created. |
---|
[92] | 486 | $this->_deleteOldVersions(); |
---|
| 487 | if (!$this->_createVersion()) { |
---|
[128] | 488 | $app->logMsg(sprintf('Failed creating new version of file.', null), LOG_NOTICE, __FILE__, __LINE__); |
---|
[1] | 489 | return false; |
---|
| 490 | } |
---|
[92] | 491 | |
---|
| 492 | // Collect posted data that is already specified in _data (by set()). |
---|
| 493 | foreach ($new_data as $name => $content) { |
---|
| 494 | if (isset($this->_data[$name])) { |
---|
| 495 | $this->_data[$name]['content'] = $content; |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | if (is_array($this->_data) && !empty($this->_data)) { |
---|
| 500 | $xml_file_contents = $this->xml_serializer->serialize($this->_data); |
---|
| 501 | return $this->_filePutContents($this->_data_file, $xml_file_contents); |
---|
| 502 | } |
---|
| 503 | } |
---|
| 504 | } |
---|
| 505 | |
---|
| 506 | /* |
---|
| 507 | * Writes content to the specified file. |
---|
| 508 | * |
---|
| 509 | * @access public |
---|
| 510 | * @param string $filename Path to file. |
---|
| 511 | * @param string $content Data to write into file. |
---|
| 512 | * @return bool Success or failure. |
---|
| 513 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 514 | * @since 11 Apr 2006 22:48:30 |
---|
| 515 | */ |
---|
| 516 | function _filePutContents($filename, $content) |
---|
| 517 | { |
---|
[128] | 518 | $app =& App::getInstance(); |
---|
| 519 | |
---|
[92] | 520 | // Ensure requested filename is within the pedit data dir. |
---|
| 521 | if (strpos($filename, $this->getParam('data_dir')) === false) { |
---|
[128] | 522 | $app->logMsg(sprintf('Failed writing file outside pedit _data_dir: %s', $filename), LOG_ERR, __FILE__, __LINE__); |
---|
[92] | 523 | return false; |
---|
| 524 | } |
---|
[42] | 525 | |
---|
[92] | 526 | // Recursively create directories. |
---|
| 527 | $subdirs = preg_split('!/!', str_replace($this->getParam('data_dir'), '', dirname($filename)), -1, PREG_SPLIT_NO_EMPTY); |
---|
| 528 | // Start with the pedit _data_dir base. |
---|
| 529 | $curr_path = $this->getParam('data_dir'); |
---|
| 530 | while (!empty($subdirs)) { |
---|
| 531 | $curr_path .= '/' . array_shift($subdirs); |
---|
| 532 | if (!is_dir($curr_path)) { |
---|
| 533 | if (!mkdir($curr_path)) { |
---|
[128] | 534 | $app->logMsg(sprintf('Failed mkdir: %s', $curr_path), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 535 | return false; |
---|
| 536 | } |
---|
[92] | 537 | } |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | // Open file for writing and truncate to zero length. |
---|
| 541 | if ($fp = fopen($filename, 'w')) { |
---|
| 542 | if (flock($fp, LOCK_EX)) { |
---|
| 543 | fwrite($fp, $content, strlen($content)); |
---|
| 544 | flock($fp, LOCK_UN); |
---|
[1] | 545 | } else { |
---|
[128] | 546 | $app->logMsg(sprintf('Could not lock file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 547 | return false; |
---|
| 548 | } |
---|
[92] | 549 | fclose($fp); |
---|
| 550 | // Success! |
---|
[128] | 551 | $app->logMsg(sprintf('Wrote to file: %s', $filename), LOG_DEBUG, __FILE__, __LINE__); |
---|
[92] | 552 | return true; |
---|
| 553 | } else { |
---|
[128] | 554 | $app->logMsg(sprintf('Could not open file for writing: %s', $filename), LOG_ERR, __FILE__, __LINE__); |
---|
[92] | 555 | return false; |
---|
[1] | 556 | } |
---|
| 557 | } |
---|
[42] | 558 | |
---|
| 559 | /** |
---|
| 560 | * Makes a copy of the current file with the unix timestamp appended to the |
---|
[92] | 561 | * filename. |
---|
[42] | 562 | * |
---|
| 563 | * @access private |
---|
[92] | 564 | * @return bool False on failure. True on success. |
---|
[42] | 565 | */ |
---|
[92] | 566 | function _createVersion() |
---|
[1] | 567 | { |
---|
[128] | 568 | $app =& App::getInstance(); |
---|
| 569 | |
---|
[1] | 570 | if (!$this->_authorized) { |
---|
| 571 | return false; |
---|
| 572 | } |
---|
[92] | 573 | if ($this->_fileHash() != getFormData('file_hash')) { |
---|
[1] | 574 | // Posted data is NOT for this file! |
---|
[128] | 575 | $app->logMsg(sprintf('File_hash does not match current file.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 576 | return false; |
---|
| 577 | } |
---|
[42] | 578 | |
---|
[92] | 579 | // Ensure current data file exists. |
---|
| 580 | if (!file_exists($this->_data_file)) { |
---|
[128] | 581 | $app->logMsg(sprintf('Data file does not yet exist: %s', $this->_data_file), LOG_NOTICE, __FILE__, __LINE__); |
---|
[92] | 582 | return false; |
---|
[1] | 583 | } |
---|
| 584 | |
---|
| 585 | // Do the actual copy. File naming scheme must be consistent! |
---|
[92] | 586 | // filename.php.xml becomes filename.php__1124124128.xml |
---|
| 587 | $version_file = sprintf('%s__%s.xml', preg_replace('/\.xml$/', '', $this->_data_file), time()); |
---|
| 588 | if (!copy($this->_data_file, $version_file)) { |
---|
[128] | 589 | $app->logMsg(sprintf('Failed copying new version: %s -> %s', $this->_data_file, $version_file), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 590 | return false; |
---|
| 591 | } |
---|
[42] | 592 | |
---|
[1] | 593 | return true; |
---|
| 594 | } |
---|
[92] | 595 | |
---|
| 596 | /* |
---|
| 597 | * Delete all versions older than versions_min_days if there are more than versions_min_qty or 100. |
---|
| 598 | * |
---|
| 599 | * @access public |
---|
| 600 | * @return bool False on failure. True on success. |
---|
| 601 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 602 | * @since 12 Apr 2006 11:08:11 |
---|
| 603 | */ |
---|
| 604 | function _deleteOldVersions() |
---|
| 605 | { |
---|
[128] | 606 | $app =& App::getInstance(); |
---|
| 607 | |
---|
[92] | 608 | $version_files = $this->_getVersions(); |
---|
| 609 | if (is_array($version_files) && sizeof($version_files) > $this->getParam('versions_min_qty')) { |
---|
| 610 | // Pop oldest ones off bottom of array. |
---|
| 611 | $oldest = array_pop($version_files); |
---|
| 612 | // Loop while minimum X qty && minimum X days worth but never more than 100 qty. |
---|
| 613 | while ((sizeof($version_files) > $this->getParam('versions_min_qty') |
---|
| 614 | && $oldest['unixtime'] < mktime(date('H'), date('i'), date('s'), date('m'), date('d') - $this->getParam('versions_min_days'), date('Y'))) |
---|
| 615 | || sizeof($version_files) > 100) { |
---|
| 616 | $del_file = dirname($this->_data_file) . '/' . $oldest['filename']; |
---|
| 617 | if (!unlink($del_file)) { |
---|
[128] | 618 | $app->logMsg(sprintf('Failed deleting version: %s', $del_file), LOG_ERR, __FILE__, __LINE__); |
---|
[92] | 619 | } |
---|
| 620 | $oldest = array_pop($version_files); |
---|
| 621 | } |
---|
| 622 | } |
---|
| 623 | } |
---|
[42] | 624 | |
---|
| 625 | /** |
---|
| 626 | * Returns an array of all archived versions of the current file, |
---|
[1] | 627 | * sorted with newest versions at the top of the array. |
---|
[42] | 628 | * |
---|
[1] | 629 | * @access private |
---|
[42] | 630 | * @return array Array of versions. |
---|
| 631 | */ |
---|
[1] | 632 | function _getVersions() |
---|
| 633 | { |
---|
[92] | 634 | $version_files = array(); |
---|
| 635 | $dir_handle = opendir(dirname($this->_data_file)); |
---|
| 636 | $curr_file_preg_pattern = sprintf('/^%s__(\d+).xml$/', preg_quote(basename($_SERVER['PHP_SELF']))); |
---|
| 637 | while ($dir_handle && ($version_file = readdir($dir_handle)) !== false) { |
---|
| 638 | if (!preg_match('/^\./', $version_file) && !is_dir($version_file) && preg_match($curr_file_preg_pattern, $version_file, $time)) { |
---|
| 639 | $version_files[] = array( |
---|
| 640 | 'filename' => $version_file, |
---|
[42] | 641 | 'unixtime' => $time[1], |
---|
[92] | 642 | 'filesize' => filesize(dirname($this->_data_file) . '/' . $version_file) |
---|
[1] | 643 | ); |
---|
| 644 | } |
---|
| 645 | } |
---|
| 646 | |
---|
[92] | 647 | if (is_array($version_files) && !empty($version_files)) { |
---|
| 648 | array_multisort($version_files, SORT_DESC); |
---|
| 649 | return $version_files; |
---|
[1] | 650 | } else { |
---|
| 651 | return array(); |
---|
| 652 | } |
---|
| 653 | } |
---|
[42] | 654 | |
---|
| 655 | /** |
---|
[1] | 656 | * Makes a version backup of the current file, then copies the specified |
---|
[42] | 657 | * archived version over the current file. |
---|
| 658 | * |
---|
| 659 | * @access private |
---|
[92] | 660 | * @param string $version Unix timestamp of archived version to restore. |
---|
| 661 | * @return bool False on failure. True on success. |
---|
[42] | 662 | */ |
---|
[92] | 663 | function _restoreVersion($version) |
---|
[1] | 664 | { |
---|
[128] | 665 | $app =& App::getInstance(); |
---|
| 666 | |
---|
[1] | 667 | if (!$this->_authorized) { |
---|
| 668 | return false; |
---|
| 669 | } |
---|
[92] | 670 | |
---|
| 671 | // The file to restore. |
---|
| 672 | $version_file = sprintf('%s__%s.xml', preg_replace('/\.xml$/', '', $this->_data_file), $version); |
---|
| 673 | |
---|
| 674 | // Ensure specified version exists. |
---|
| 675 | if (!file_exists($version_file)) { |
---|
[128] | 676 | $app->logMsg(sprintf('Cannot restore non-existant file: %s', $version_file), LOG_NOTICE, __FILE__, __LINE__); |
---|
[92] | 677 | return false; |
---|
| 678 | } |
---|
[42] | 679 | |
---|
[92] | 680 | // Make certain a version is created. |
---|
| 681 | if (!$this->_createVersion()) { |
---|
[128] | 682 | $app->logMsg(sprintf('Failed creating new version of file.', null), LOG_ERR, __FILE__, __LINE__); |
---|
[92] | 683 | return false; |
---|
| 684 | } |
---|
[42] | 685 | |
---|
[92] | 686 | // Do the actual copy. |
---|
| 687 | if (!copy($version_file, $this->_data_file)) { |
---|
[128] | 688 | $app->logMsg(sprintf('Failed copying old version: %s -> %s', $version_file, $this->_data_file), LOG_ERR, __FILE__, __LINE__); |
---|
[1] | 689 | return false; |
---|
| 690 | } |
---|
[92] | 691 | |
---|
| 692 | // Success! |
---|
[128] | 693 | $app->raiseMsg(sprintf(_("Page has been restored to version %s."), $version), MSG_SUCCESS, __FILE__, __LINE__); |
---|
[92] | 694 | return true; |
---|
[1] | 695 | } |
---|
[42] | 696 | |
---|
[1] | 697 | } // End class. |
---|
| 698 | |
---|
| 699 | ?> |
---|