Issues (10)

lib/Persistence/DataStore.php (1 issue)

Checks if the types of the passed arguments in a function/method call are compatible.

Bug Minor
1
<?php
2
/**
3
 * PrivateBin
4
 *
5
 * a zero-knowledge paste bin
6
 *
7
 * @link      https://github.com/PrivateBin/PrivateBin
8
 * @copyright 2012 S├ębastien SAUVAGE (sebsauvage.net)
9
 * @license   https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
10
 * @version   1.1
11
 */
12
13
namespace PrivateBin\Persistence;
14
15
use Exception;
16
use PrivateBin\Json;
17
18
/**
19
 * DataStore
20
 *
21
 * Handles data storage for Data\Filesystem.
22
 */
23
class DataStore extends AbstractPersistence
24
{
25
    /**
26
     * first line in file, to protect its contents
27
     *
28
     * @const string
29
     */
30
    const PROTECTION_LINE = '<?php http_response_code(403); /*';
31
32
    /**
33
     * store the data
34
     *
35
     * @access public
36
     * @static
37
     * @param  string $filename
38
     * @param  array  $data
39
     * @return bool
40
     */
41 39
    public static function store($filename, $data)
42
    {
43 39
        $path = self::getPath();
44 39
        if (strpos($filename, $path) === 0) {
45 39
            $filename = substr($filename, strlen($path));
46
        }
47
        try {
48 39
            self::_store(
49 39
                $filename,
50 39
                self::PROTECTION_LINE . PHP_EOL . Json::encode($data)
51
            );
52 38
            return true;
53 2
        } catch (Exception $e) {
54 2
            return false;
55
        }
56
    }
57
58
    /**
59
     * get the data
60
     *
61
     * @access public
62
     * @static
63
     * @param  string $filename
64
     * @return array|false $data
65
     */
66 29
    public static function get($filename)
67
    {
68 29
        return Json::decode(
69 29
            substr(
70 29
                file_get_contents($filename),
71 29
                strlen(self::PROTECTION_LINE . PHP_EOL)
72
            )
73
        );
74
    }
75
76
    /**
77
     * rename a file, prepending the protection line at the beginning
78
     *
79
     * @access public
80
     * @static
81
     * @param  string $srcFile
82
     * @param  string $destFile
83
     * @param  string $prefix (optional)
84
     * @return void
85
     */
86 3
    public static function prependRename($srcFile, $destFile, $prefix = '')
87
    {
88
        // don't overwrite already converted file
89 3
        if (!is_readable($destFile)) {
90 3
            $handle = fopen($srcFile, 'r', false, stream_context_create());
91 3
            file_put_contents($destFile, $prefix . self::PROTECTION_LINE . PHP_EOL);
92 3
            file_put_contents($destFile, $handle, FILE_APPEND);
93 3
            fclose($handle);
0 ignored issues
show
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
            fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
94
        }
95 3
        unlink($srcFile);
96 3
    }
97
}
98