Completed
Push — master ( 1cb1c1...b2ea65 )
by El
05:50
created

lib/Persistence/AbstractPersistence.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
17
/**
18
 * AbstractPersistence
19
 *
20
 * persists data in PHP files
21
 */
22
abstract class AbstractPersistence
23
{
24
    /**
25
     * path in which to persist something
26
     *
27
     * @access private
28
     * @static
29
     * @var    string
30
     */
31
    private static $_path = 'data';
32
33
    /**
34
     * set the path
35
     *
36
     * @access public
37
     * @static
38
     * @param  string $path
39
     * @return void
40
     */
41 116
    public static function setPath($path)
42
    {
43 116
        self::$_path = $path;
44 116
    }
45
46
    /**
47
     * get the path
48
     *
49
     * @access public
50
     * @static
51
     * @param  string $filename
52
     * @return string
53
     */
54 32
    public static function getPath($filename = null)
55
    {
56 32
        if (strlen($filename)) {
57 32
            return self::$_path . DIRECTORY_SEPARATOR . $filename;
58
        } else {
59 1
            return self::$_path;
60
        }
61
    }
62
63
    /**
64
     * checks if the file exists
65
     *
66
     * @access protected
67
     * @static
68
     * @param  string $filename
69
     * @return bool
70
     */
71 48
    protected static function _exists($filename)
72
    {
73 48
        self::_initialize();
74 46
        return is_file(self::$_path . DIRECTORY_SEPARATOR . $filename);
75
    }
76
77
    /**
78
     * prepares path for storage
79
     *
80
     * @access protected
81
     * @static
82
     * @throws Exception
83
     * @return void
84
     */
85 48
    protected static function _initialize()
86
    {
87
        // Create storage directory if it does not exist.
88 48
        if (!is_dir(self::$_path)) {
89 3
            if (!@mkdir(self::$_path)) {
90 1
                throw new Exception('unable to create directory ' . self::$_path, 10);
91
            }
92
        }
93
94
        // Create .htaccess file if it does not exist.
95 47
        $file = self::$_path . DIRECTORY_SEPARATOR . '.htaccess';
96 47
        if (!is_file($file)) {
97 27
            $writtenBytes = @file_put_contents(
98
                $file,
99 27
                'Allow from none' . PHP_EOL .
100 27
                'Deny from all' . PHP_EOL,
101 27
                LOCK_EX
102
            );
103 27 View Code Duplication
            if ($writtenBytes === false || $writtenBytes < 30) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104 1
                throw new Exception('unable to write to file ' . $file, 11);
105
            }
106
        }
107 46
    }
108
109
    /**
110
     * store the data
111
     *
112
     * @access protected
113
     * @static
114
     * @param  string $filename
115
     * @param  string $data
116
     * @throws Exception
117
     * @return string
118
     */
119 45
    protected static function _store($filename, $data)
120
    {
121 45
        self::_initialize();
122 45
        $file         = self::$_path . DIRECTORY_SEPARATOR . $filename;
123 45
        $writtenBytes = @file_put_contents($file, $data, LOCK_EX);
124 45 View Code Duplication
        if ($writtenBytes === false || $writtenBytes < strlen($data)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125 1
            throw new Exception('unable to write to file ' . $file, 13);
126
        }
127 44
        @chmod($file, 0640); // protect file access
128 44
        return $file;
129
    }
130
}
131