bootstrap.php 8.0 KB

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