bootstrap.php 8.0 KB

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