Bootstrap.php 8.1 KB

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