/vb-ame3/upload/includes/ame_cache.php

https://github.com/rcdesign/vb-foreign_mod_fixes · PHP · 233 lines · 113 code · 48 blank · 72 comment · 8 complexity · 39dddff68c6ed951e9a25a1cc80da013 MD5 · raw file

  1. <?php
  2. /**
  3. * AME - The Automatic Media Embedder 3.0.1
  4. * Copyright ©2009-2010 All rights reserved by sweetsquared.com
  5. * This code may not be used in whole or part without explicit written
  6. * permission from Samuel Sweet [samuel@sweetsquared.com].
  7. * You may not distribute this or any of the associated files in whole or significant part
  8. * without explicit written permission from Samuel Sweet [samuel@sweetsquared.com]
  9. */
  10. if (!defined("AME"))
  11. {
  12. die("You cannot directly call this file from outside of the AME system.");
  13. }
  14. /**
  15. * This baby takes an array (i.e. data structure)
  16. * and writes it out into a php file which can be 'included'
  17. * into code instead of querying the DB and building the array up.
  18. * Note: This class asserts all data is correct!
  19. */
  20. class AME_file_cache
  21. {
  22. /**
  23. * Full path to writeable folder where we dump our load
  24. *
  25. * @var unknown_type
  26. */
  27. var $path;
  28. /**
  29. * The name of the file we will overwrite. We are appending .php
  30. *
  31. * @var unknown_type
  32. */
  33. var $filename;
  34. /**
  35. * The array we will be flattening out
  36. *
  37. * @var unknown_type
  38. */
  39. var $data;
  40. /**
  41. * The textual representation of the array
  42. *
  43. * @var unknown_type
  44. */
  45. var $content;
  46. /**
  47. * header. Used to include a defined check by default
  48. *
  49. * @var unknown_type
  50. */
  51. var $header;
  52. /**
  53. * Constructor
  54. *
  55. * @param string $path writeable folder
  56. * @param string $filename filename to write
  57. * @return AME_file_cache
  58. */
  59. function AME_file_cache($path = '', $filename = '')
  60. {
  61. $this->path = $path;
  62. $this->filename = $filename;
  63. $this->header = "if (!defined(AME))\n{\n\tdie(\"You cannot directly call this file from outside of the AME system.\");\n}";
  64. }
  65. /**
  66. * Verifies path is indeed a valid directory
  67. *
  68. * @param string $path
  69. * @return boolean
  70. */
  71. function verify_path_exists($path = '')
  72. {
  73. if (!$path && $this->path)
  74. {
  75. $path = $this->path;
  76. }
  77. return dir($path);
  78. }
  79. /**
  80. * Tests path is writeable
  81. *
  82. * @param string $path to test
  83. * @return boolean
  84. */
  85. function verify_path_writeable($path = '')
  86. {
  87. $result = false;
  88. if (!$path && $this->path)
  89. {
  90. $path = $this->path;
  91. }
  92. $this->write(
  93. array(
  94. 'path' => $path,
  95. 'filename' => 'testiname',
  96. 'content' => '//yo there'
  97. )
  98. );
  99. if (file_exists($path . "/testiname.php"))
  100. {
  101. unlink($path . "/testiname.php");
  102. $result = true;
  103. }
  104. return $result;
  105. }
  106. /**
  107. * Creates textual version of an array
  108. *
  109. * @param array $array array to convert
  110. * @param string $title i.e. $ameinfo
  111. * @return string textual content of array
  112. */
  113. function save($array, $root)
  114. {
  115. $this->data = $array;
  116. $this->root = $root;
  117. if (is_array($array))
  118. {
  119. $return = "<?php\n$root = array(\n";
  120. $return .= $this->write_array_sub($this->data, 1);
  121. $return .=");\n?>";
  122. }
  123. $this->content = $return;
  124. $this->write();
  125. }
  126. /**
  127. * designed to be overloaded to walk nested arrays
  128. *
  129. * @param array $array array to walk
  130. * @param int $depth depth of array (for recursion)
  131. * @return string textual representation of array and children
  132. */
  133. protected function write_array_sub($array, $depth = 1)
  134. {
  135. $pre = str_pad("\t", $depth * 3);
  136. $return = "";
  137. foreach($array as $key => $value)
  138. {
  139. if (is_array($value))
  140. {
  141. $return .= "$pre'$key' => array(\n";
  142. $return .= $this->write_array_sub($value, ($depth + 1));
  143. $return .= "$pre),\n";
  144. }
  145. else
  146. {
  147. $return .= "$pre'$key'\t\t\t=>'" . str_replace(array("\'", "'"), array("\\\'", "\'"), $value) . "',\n";
  148. }
  149. }
  150. return $return;
  151. }
  152. /**
  153. * Wrties $data to the $filename.php in the $path.
  154. * @param optional array that can override internal variables
  155. */
  156. protected function write($info = array())
  157. {
  158. $path = !$info['path'] && $this->path ? $this->path : $info['path'];
  159. $content = !$info['content'] && $this->content ? $this->content : $info['content'];
  160. $filename = !$info['filename'] && $this->filename ? $this->filename : $info['filename'];
  161. if ($content)
  162. {
  163. if (is_dir($path))
  164. {
  165. $fput = @fopen($path . $filename . ".php", "w");
  166. @fputs ($fput, $content);
  167. @fclose($fput);
  168. }
  169. }
  170. }
  171. }
  172. ?>