2012-08-25 22:49:11 +00:00
|
|
|
<?php
|
2016-07-21 15:09:48 +00:00
|
|
|
|
|
|
|
use PrivateBin\serversalt;
|
|
|
|
|
2012-08-25 22:49:11 +00:00
|
|
|
error_reporting( E_ALL | E_STRICT );
|
|
|
|
|
|
|
|
// change this, if your php files and data is outside of your webservers document root
|
2016-05-22 16:35:07 +00:00
|
|
|
if (!defined('PUBLIC_PATH')) define('PUBLIC_PATH', '..');
|
2015-08-27 21:30:35 +00:00
|
|
|
if (!defined('PATH')) define('PATH', '..' . DIRECTORY_SEPARATOR);
|
2015-09-22 21:21:31 +00:00
|
|
|
if (!defined('CONF')) define('CONF', PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini');
|
2016-05-22 16:35:07 +00:00
|
|
|
if (!is_file(CONF)) copy(CONF . '.sample', CONF);
|
2012-08-25 22:49:11 +00:00
|
|
|
|
2016-07-21 15:09:48 +00:00
|
|
|
require PATH . 'vendor/autoload.php';
|
2012-08-25 22:49:11 +00:00
|
|
|
|
|
|
|
class helper
|
|
|
|
{
|
2015-09-21 20:32:52 +00:00
|
|
|
/**
|
|
|
|
* example ID of a paste
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $pasteid = '5e9bc25c89fb3bf9';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* example paste
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $paste = array(
|
|
|
|
'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
|
2015-09-26 10:29:27 +00:00
|
|
|
'attachment' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
|
|
|
|
'attachmentname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
|
2015-09-21 20:32:52 +00:00
|
|
|
'meta' => array(
|
2015-10-03 13:52:37 +00:00
|
|
|
'formatter' => 'plaintext',
|
2015-09-21 20:32:52 +00:00
|
|
|
'postdate' => 1344803344,
|
|
|
|
'opendiscussion' => true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* example ID of a comment
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $commentid = '5a52eebf11c4c94b';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* example comment
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $comment = array(
|
|
|
|
'data' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}',
|
|
|
|
'meta' => array(
|
|
|
|
'nickname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}',
|
|
|
|
'vizhash' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGUlEQVQokWOsl5/94983CNKQMjnxaOePf98MeKwPfNjkLZ3AgARab6b9+PeNEVnDj3/ff/z7ZiHnzsDA8Pv7H2TVPJw8EAYLAwb48OaVgIgYKycLsrYv378wMDB8//qdCVMDRA9EKSsnCwRBxNsepaLboMFlyMDAICAi9uHNK24GITQ/MDAwoNhgIGMLtwGrzegaLjw5jMz9+vUdnN17uwDCQDhJgk0O07yvX9+teDX1x79v6DYIsIjgcgMaYGFgYOBg4kJx2JejkAiBxAw+PzAwMNz4dp6wDXDw4MdNNOl0rWYsNkD89OLXI/xmo9sgzatJjAYmBgYGDiauD3/ePP18nVgb4MF89+M5ZX6js293wUMpnr8KTQMAxsCJnJ30apMAAAAASUVORK5CYII=',
|
|
|
|
'postdate' => 1344803528,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get example paste ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getPasteId()
|
|
|
|
{
|
|
|
|
return self::$pasteid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get example paste
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getPaste($meta = array())
|
2015-09-26 10:29:27 +00:00
|
|
|
{
|
|
|
|
$example = self::getPasteWithAttachment($meta);
|
|
|
|
unset($example['attachment'], $example['attachmentname']);
|
|
|
|
return $example;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get example paste
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getPasteWithAttachment($meta = array())
|
2015-09-21 20:32:52 +00:00
|
|
|
{
|
|
|
|
$example = self::$paste;
|
2016-07-06 09:37:13 +00:00
|
|
|
$example['meta']['salt'] = serversalt::generate();
|
2015-09-21 20:32:52 +00:00
|
|
|
$example['meta'] = array_merge($example['meta'], $meta);
|
|
|
|
return $example;
|
|
|
|
}
|
|
|
|
|
2015-10-18 09:08:28 +00:00
|
|
|
/**
|
|
|
|
* get example paste
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getPasteAsJson($meta = array())
|
|
|
|
{
|
|
|
|
$example = self::getPaste();
|
2016-07-06 09:37:13 +00:00
|
|
|
// the JSON shouldn't contain the salt
|
|
|
|
unset($example['meta']['salt']);
|
2015-10-18 09:08:28 +00:00
|
|
|
if (count($meta))
|
|
|
|
$example['meta'] = $meta;
|
|
|
|
$example['comments'] = array();
|
|
|
|
$example['comment_count'] = 0;
|
|
|
|
$example['comment_offset'] = 0;
|
|
|
|
$example['@context'] = 'js/paste.jsonld';
|
|
|
|
return json_encode($example);
|
|
|
|
}
|
|
|
|
|
2015-09-21 20:32:52 +00:00
|
|
|
/**
|
|
|
|
* get example paste ID
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getCommentId()
|
|
|
|
{
|
|
|
|
return self::$commentid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get example comment
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getComment($meta = array())
|
|
|
|
{
|
|
|
|
$example = self::$comment;
|
|
|
|
$example['meta'] = array_merge($example['meta'], $meta);
|
|
|
|
return $example;
|
|
|
|
}
|
|
|
|
|
2015-10-03 13:52:37 +00:00
|
|
|
/**
|
|
|
|
* get example comment
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getCommentPost($meta = array())
|
|
|
|
{
|
|
|
|
$example = self::getComment($meta);
|
|
|
|
$example['nickname'] = $example['meta']['nickname'];
|
|
|
|
unset($example['meta']['nickname']);
|
|
|
|
return $example;
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:41:10 +00:00
|
|
|
/**
|
|
|
|
* delete directory and all its contents recursively
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-08-27 19:41:21 +00:00
|
|
|
public static function rmdir($path)
|
|
|
|
{
|
|
|
|
$path .= DIRECTORY_SEPARATOR;
|
|
|
|
$dir = dir($path);
|
|
|
|
while(false !== ($file = $dir->read())) {
|
|
|
|
if($file != '.' && $file != '..') {
|
|
|
|
if(is_dir($path . $file)) {
|
|
|
|
self::rmdir($path . $file);
|
|
|
|
} elseif(is_file($path . $file)) {
|
|
|
|
if(!@unlink($path . $file)) {
|
|
|
|
throw new Exception('Error deleting file "' . $path . $file . '".');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$dir->close();
|
|
|
|
if(!@rmdir($path)) {
|
|
|
|
throw new Exception('Error deleting directory "' . $path . '".');
|
|
|
|
}
|
|
|
|
}
|
2015-08-29 08:41:10 +00:00
|
|
|
|
2015-09-22 21:21:31 +00:00
|
|
|
/**
|
|
|
|
* create a backup of the config file
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function confBackup()
|
|
|
|
{
|
|
|
|
if (!is_file(CONF . '.bak') && is_file(CONF))
|
|
|
|
rename(CONF, CONF . '.bak');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* restor backup of the config file
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function confRestore()
|
|
|
|
{
|
|
|
|
if (is_file(CONF . '.bak'))
|
|
|
|
rename(CONF . '.bak', CONF);
|
|
|
|
}
|
|
|
|
|
2015-08-29 08:41:10 +00:00
|
|
|
/**
|
|
|
|
* create ini file
|
|
|
|
*
|
|
|
|
* @param string $pathToFile
|
|
|
|
* @param array $values
|
|
|
|
*/
|
|
|
|
public static function createIniFile($pathToFile, $values)
|
|
|
|
{
|
|
|
|
if (count($values)) {
|
|
|
|
@unlink($pathToFile);
|
|
|
|
$ini = fopen($pathToFile, 'a');
|
|
|
|
foreach ($values as $section => $options) {
|
|
|
|
fwrite($ini, "[$section]" . PHP_EOL);
|
|
|
|
foreach($options as $option => $setting) {
|
|
|
|
if (is_null($setting)) {
|
|
|
|
continue;
|
|
|
|
} elseif (is_string($setting)) {
|
|
|
|
$setting = '"' . $setting . '"';
|
2015-09-27 01:03:55 +00:00
|
|
|
} elseif (is_array($setting)) {
|
|
|
|
foreach ($setting as $key => $value) {
|
|
|
|
if (is_null($value)) {
|
|
|
|
$value = 'null';
|
|
|
|
} elseif (is_string($value)) {
|
|
|
|
$value = '"' . $value . '"';
|
|
|
|
} else {
|
|
|
|
$value = var_export($value, true);
|
|
|
|
}
|
|
|
|
fwrite($ini, $option . "[$key] = $value" . PHP_EOL);
|
|
|
|
}
|
|
|
|
continue;
|
2015-08-29 08:41:10 +00:00
|
|
|
} else {
|
|
|
|
$setting = var_export($setting, true);
|
|
|
|
}
|
|
|
|
fwrite($ini, "$option = $setting" . PHP_EOL);
|
|
|
|
}
|
|
|
|
fwrite($ini, PHP_EOL);
|
|
|
|
}
|
|
|
|
fclose($ini);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* a var_export that returns arrays without line breaks
|
|
|
|
* by linus@flowingcreativity.net via php.net
|
|
|
|
*
|
|
|
|
* @param mixed $var
|
|
|
|
* @param bool $return
|
|
|
|
* @return void|string
|
|
|
|
*/
|
|
|
|
public static function var_export_min($var, $return = false)
|
|
|
|
{
|
|
|
|
if (is_array($var)) {
|
|
|
|
$toImplode = array();
|
|
|
|
foreach ($var as $key => $value) {
|
|
|
|
$toImplode[] = var_export($key, true) . ' => ' . self::var_export_min($value, true);
|
|
|
|
}
|
|
|
|
$code = 'array(' . implode(', ', $toImplode) . ')';
|
|
|
|
if ($return) {
|
|
|
|
return $code;
|
|
|
|
} else {
|
|
|
|
echo $code;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return var_export($var, $return);
|
|
|
|
}
|
|
|
|
}
|
2016-07-06 07:01:10 +00:00
|
|
|
}
|