bootstrap.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. 'formatter' => 'plaintext',
  27. 'postdate' => 1344803344,
  28. 'opendiscussion' => true,
  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
  83. *
  84. * @return array
  85. */
  86. public static function getPasteAsJson($meta = array())
  87. {
  88. $example = self::getPaste();
  89. if (count($meta))
  90. $example['meta'] = $meta;
  91. $example['comments'] = array();
  92. $example['comment_count'] = 0;
  93. $example['comment_offset'] = 0;
  94. $example['@context'] = 'js/paste.jsonld';
  95. return json_encode($example);
  96. }
  97. /**
  98. * get example paste ID
  99. *
  100. * @return string
  101. */
  102. public static function getCommentId()
  103. {
  104. return self::$commentid;
  105. }
  106. /**
  107. * get example comment
  108. *
  109. * @return array
  110. */
  111. public static function getComment($meta = array())
  112. {
  113. $example = self::$comment;
  114. $example['meta'] = array_merge($example['meta'], $meta);
  115. return $example;
  116. }
  117. /**
  118. * get example comment
  119. *
  120. * @return array
  121. */
  122. public static function getCommentPost($meta = array())
  123. {
  124. $example = self::getComment($meta);
  125. $example['nickname'] = $example['meta']['nickname'];
  126. unset($example['meta']['nickname']);
  127. return $example;
  128. }
  129. /**
  130. * delete directory and all its contents recursively
  131. *
  132. * @param string $path
  133. * @throws Exception
  134. */
  135. public static function rmdir($path)
  136. {
  137. $path .= DIRECTORY_SEPARATOR;
  138. $dir = dir($path);
  139. while(false !== ($file = $dir->read())) {
  140. if($file != '.' && $file != '..') {
  141. if(is_dir($path . $file)) {
  142. self::rmdir($path . $file);
  143. } elseif(is_file($path . $file)) {
  144. if(!@unlink($path . $file)) {
  145. throw new Exception('Error deleting file "' . $path . $file . '".');
  146. }
  147. }
  148. }
  149. }
  150. $dir->close();
  151. if(!@rmdir($path)) {
  152. throw new Exception('Error deleting directory "' . $path . '".');
  153. }
  154. }
  155. /**
  156. * create a backup of the config file
  157. *
  158. * @return void
  159. */
  160. public static function confBackup()
  161. {
  162. if (!is_file(CONF . '.bak') && is_file(CONF))
  163. rename(CONF, CONF . '.bak');
  164. }
  165. /**
  166. * restor backup of the config file
  167. *
  168. * @return void
  169. */
  170. public static function confRestore()
  171. {
  172. if (is_file(CONF . '.bak'))
  173. rename(CONF . '.bak', CONF);
  174. }
  175. /**
  176. * create ini file
  177. *
  178. * @param string $pathToFile
  179. * @param array $values
  180. */
  181. public static function createIniFile($pathToFile, $values)
  182. {
  183. if (count($values)) {
  184. @unlink($pathToFile);
  185. $ini = fopen($pathToFile, 'a');
  186. foreach ($values as $section => $options) {
  187. fwrite($ini, "[$section]" . PHP_EOL);
  188. foreach($options as $option => $setting) {
  189. if (is_null($setting)) {
  190. continue;
  191. } elseif (is_string($setting)) {
  192. $setting = '"' . $setting . '"';
  193. } elseif (is_array($setting)) {
  194. foreach ($setting as $key => $value) {
  195. if (is_null($value)) {
  196. $value = 'null';
  197. } elseif (is_string($value)) {
  198. $value = '"' . $value . '"';
  199. } else {
  200. $value = var_export($value, true);
  201. }
  202. fwrite($ini, $option . "[$key] = $value" . PHP_EOL);
  203. }
  204. continue;
  205. } else {
  206. $setting = var_export($setting, true);
  207. }
  208. fwrite($ini, "$option = $setting" . PHP_EOL);
  209. }
  210. fwrite($ini, PHP_EOL);
  211. }
  212. fclose($ini);
  213. }
  214. }
  215. /**
  216. * a var_export that returns arrays without line breaks
  217. * by linus@flowingcreativity.net via php.net
  218. *
  219. * @param mixed $var
  220. * @param bool $return
  221. * @return void|string
  222. */
  223. public static function var_export_min($var, $return = false)
  224. {
  225. if (is_array($var)) {
  226. $toImplode = array();
  227. foreach ($var as $key => $value) {
  228. $toImplode[] = var_export($key, true) . ' => ' . self::var_export_min($value, true);
  229. }
  230. $code = 'array(' . implode(', ', $toImplode) . ')';
  231. if ($return) {
  232. return $code;
  233. } else {
  234. echo $code;
  235. }
  236. } else {
  237. return var_export($var, $return);
  238. }
  239. }
  240. }