bootstrap.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. error_reporting( E_ALL | E_STRICT );
  3. // change this, if your php files and data is outside of your webservers document root
  4. if (!defined('PATH')) define('PATH', '..' . DIRECTORY_SEPARATOR);
  5. if (!defined('CONF')) define('CONF', PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini');
  6. if (!defined('PUBLIC_PATH')) define('PUBLIC_PATH', '..');
  7. require PATH . 'lib/auto.php';
  8. class helper
  9. {
  10. /**
  11. * example ID of a paste
  12. *
  13. * @var string
  14. */
  15. private static $pasteid = '5e9bc25c89fb3bf9';
  16. /**
  17. * example paste
  18. *
  19. * @var array
  20. */
  21. private static $paste = array(
  22. 'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
  23. 'attachment' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  24. 'attachmentname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  25. 'meta' => array(
  26. 'postdate' => 1344803344,
  27. 'opendiscussion' => true,
  28. 'formatter' => 'plaintext',
  29. ),
  30. );
  31. /**
  32. * example ID of a comment
  33. *
  34. * @var string
  35. */
  36. private static $commentid = '5a52eebf11c4c94b';
  37. /**
  38. * example comment
  39. *
  40. * @var array
  41. */
  42. private static $comment = array(
  43. 'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
  44. 'meta' => array(
  45. 'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
  46. 'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
  47. 'postdate' => 1344803528,
  48. ),
  49. );
  50. /**
  51. * get example paste ID
  52. *
  53. * @return string
  54. */
  55. public static function getPasteId()
  56. {
  57. return self::$pasteid;
  58. }
  59. /**
  60. * get example paste
  61. *
  62. * @return array
  63. */
  64. public static function getPaste($meta = array())
  65. {
  66. $example = self::getPasteWithAttachment($meta);
  67. unset($example['attachment'], $example['attachmentname']);
  68. return $example;
  69. }
  70. /**
  71. * get example paste
  72. *
  73. * @return array
  74. */
  75. public static function getPasteWithAttachment($meta = array())
  76. {
  77. $example = self::$paste;
  78. $example['meta'] = array_merge($example['meta'], $meta);
  79. return $example;
  80. }
  81. /**
  82. * get example paste ID
  83. *
  84. * @return string
  85. */
  86. public static function getCommentId()
  87. {
  88. return self::$commentid;
  89. }
  90. /**
  91. * get example comment
  92. *
  93. * @return array
  94. */
  95. public static function getComment($meta = array())
  96. {
  97. $example = self::$comment;
  98. $example['meta'] = array_merge($example['meta'], $meta);
  99. return $example;
  100. }
  101. /**
  102. * delete directory and all its contents recursively
  103. *
  104. * @param string $path
  105. * @throws Exception
  106. */
  107. public static function rmdir($path)
  108. {
  109. $path .= DIRECTORY_SEPARATOR;
  110. $dir = dir($path);
  111. while(false !== ($file = $dir->read())) {
  112. if($file != '.' && $file != '..') {
  113. if(is_dir($path . $file)) {
  114. self::rmdir($path . $file);
  115. } elseif(is_file($path . $file)) {
  116. if(!@unlink($path . $file)) {
  117. throw new Exception('Error deleting file "' . $path . $file . '".');
  118. }
  119. }
  120. }
  121. }
  122. $dir->close();
  123. if(!@rmdir($path)) {
  124. throw new Exception('Error deleting directory "' . $path . '".');
  125. }
  126. }
  127. /**
  128. * create a backup of the config file
  129. *
  130. * @return void
  131. */
  132. public static function confBackup()
  133. {
  134. if (!is_file(CONF . '.bak') && is_file(CONF))
  135. rename(CONF, CONF . '.bak');
  136. }
  137. /**
  138. * restor backup of the config file
  139. *
  140. * @return void
  141. */
  142. public static function confRestore()
  143. {
  144. if (is_file(CONF . '.bak'))
  145. rename(CONF . '.bak', CONF);
  146. }
  147. /**
  148. * create ini file
  149. *
  150. * @param string $pathToFile
  151. * @param array $values
  152. */
  153. public static function createIniFile($pathToFile, $values)
  154. {
  155. if (count($values)) {
  156. @unlink($pathToFile);
  157. $ini = fopen($pathToFile, 'a');
  158. foreach ($values as $section => $options) {
  159. fwrite($ini, "[$section]" . PHP_EOL);
  160. foreach($options as $option => $setting) {
  161. if (is_null($setting)) {
  162. continue;
  163. } elseif (is_string($setting)) {
  164. $setting = '"' . $setting . '"';
  165. } else {
  166. $setting = var_export($setting, true);
  167. }
  168. fwrite($ini, "$option = $setting" . PHP_EOL);
  169. }
  170. fwrite($ini, PHP_EOL);
  171. }
  172. fclose($ini);
  173. }
  174. }
  175. /**
  176. * a var_export that returns arrays without line breaks
  177. * by linus@flowingcreativity.net via php.net
  178. *
  179. * @param mixed $var
  180. * @param bool $return
  181. * @return void|string
  182. */
  183. public static function var_export_min($var, $return = false)
  184. {
  185. if (is_array($var)) {
  186. $toImplode = array();
  187. foreach ($var as $key => $value) {
  188. $toImplode[] = var_export($key, true) . ' => ' . self::var_export_min($value, true);
  189. }
  190. $code = 'array(' . implode(', ', $toImplode) . ')';
  191. if ($return) {
  192. return $code;
  193. } else {
  194. echo $code;
  195. }
  196. } else {
  197. return var_export($var, $return);
  198. }
  199. }
  200. }