/administrator/components/com_akeeba/akeeba/utils/tempvars.php

https://bitbucket.org/dreamriks/gift · PHP · 121 lines · 75 code · 18 blank · 28 comment · 20 complexity · 365b7c2df74980709189aca2ba27a489 MD5 · raw file

  1. <?php
  2. /**
  3. * Akeeba Engine
  4. * The modular PHP5 site backup engine
  5. * @copyright Copyright (c)2009-2011 Nicholas K. Dionysopoulos
  6. * @license GNU GPL version 3 or, at your option, any later version
  7. * @package akeebaengine
  8. * @version $Id: tempvars.php 409 2011-01-24 09:30:22Z nikosdion $
  9. */
  10. // Protection against direct access
  11. defined('AKEEBAENGINE') or die('Restricted access');
  12. /**
  13. * Temporary variables management class. Everything is stored serialized in an INI
  14. * file on the temporary directory.
  15. */
  16. class AEUtilTempvars
  17. {
  18. /**
  19. * Returns the fully qualified path to the storage file
  20. * @return unknown_type
  21. */
  22. static public function get_storage_filename($tag = null)
  23. {
  24. if(!empty($tag)) {
  25. $basename = 'akeeba_'.$tag.'.php';
  26. } else {
  27. $basename = 'akeeba_storage.php';
  28. }
  29. $registry =& AEFactory::getConfiguration();
  30. return $registry->get('akeeba.basic.temporary_directory').DIRECTORY_SEPARATOR.$basename;
  31. }
  32. /**
  33. * Resets the storage. This method removes all stored values.
  34. * @return bool True on success
  35. */
  36. public static function reset($tag = null)
  37. {
  38. static $storage_filename = null;
  39. if(empty($storage_filename) || !empty($tag))
  40. {
  41. $storage_filename = AEUtilTempvars::get_storage_filename($tag);
  42. }
  43. return @unlink($storage_filename);
  44. }
  45. public static function set(&$value, $tag = null)
  46. {
  47. static $storage_filename = null;
  48. static $temporary_storage_filename = null;
  49. if(empty($storage_filename) || !empty($tag))
  50. {
  51. $storage_filename = AEUtilTempvars::get_storage_filename($tag);
  52. }
  53. // Remove old file (if exists)
  54. if(file_exists($storage_filename)) @unlink($storage_filename);
  55. // Open the new file
  56. $fp = @fopen($storage_filename, 'wb');
  57. if( $fp === false ) return false;
  58. // Add a header
  59. fputs($fp, "<?php die('Access denied'); ?>\n");
  60. fwrite($fp, self::encode($value));
  61. fclose($fp);
  62. return true;
  63. }
  64. public static function &get($tag = null)
  65. {
  66. static $storage_filename = null;
  67. if(empty($storage_filename) || !is_null($tag))
  68. {
  69. $storage_filename = AEUtilTempvars::get_storage_filename($tag);
  70. }
  71. $ret = false;
  72. // Open the file
  73. $fp = @fopen($storage_filename, 'rb');
  74. if( $fp === false ) return $ret;
  75. // Throw away the first line; it's just a php header to deter web access
  76. $ret = @fgets($fp);
  77. // The next line is what I need
  78. $ret = @fread($fp, filesize($storage_filename) );
  79. if($ret !== false)
  80. {
  81. $ret = self::decode($ret);
  82. }
  83. fclose($fp);
  84. return $ret;
  85. }
  86. public static function encode(&$data)
  87. {
  88. // Should I base64-encode?
  89. if( function_exists('base64_encode') && function_exists('base64_decode') ) {
  90. return base64_encode($data);
  91. } elseif( function_exists('convert_uuencode') && function_exists('convert_uudecode') ) {
  92. return convert_uuencode($data);
  93. } else return $data;
  94. }
  95. public static function decode(&$data)
  96. {
  97. if( function_exists('base64_encode') && function_exists('base64_decode') ) {
  98. return base64_decode($data);
  99. } elseif( function_exists('convert_uuencode') && function_exists('convert_uudecode') ) {
  100. return convert_uudecode($data);
  101. } else return $data;
  102. }
  103. }