filedump.php 11.5 KB
Newer Older
Chris Graham's avatar
Chris Graham committed
1 2 3
<?php /*

 Composr
Chris Graham's avatar
Chris Graham committed
4
 Copyright (c) ocProducts, 2004-2016
Chris Graham's avatar
Chris Graham committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license    http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright  ocProducts Ltd
 * @package    filedump
 */

/**
 * Hook class.
 */
class Hook_commandr_fs_filedump
{
    /**
     * Standard constructor.
     */
    public function __construct()
    {
        require_code('resource_fs');
    }

    /**
     * Get complete path.
     *
     * @param  array $meta_dir The current meta-directory path
     * @return array A pair: Complete path, Relative path
     */
    private function get_complete_path($meta_dir)
    {
        $path = get_custom_file_base() . '/uploads/filedump';
        $subpath = '';
        foreach ($meta_dir as $meta_dir_section) {
            if ($subpath != '') {
                $subpath .= '/';
            }
            $subpath .= filter_naughty($meta_dir_section);
        }
        if ($subpath != '') {
            $path .= '/' . $subpath;
        }
        return array($path, $subpath);
    }

    /**
     * Standard Commandr-fs listing function for commandr_fs hooks.
     *
     * @param  array $meta_dir The current meta-directory path
     * @param  string $meta_root_node The root node of the current meta-directory
     * @param  object $commandr_fs A reference to the Commandr filesystem object
     * @return ~array The final directory listing (false: failure)
     */
    public function listing($meta_dir, $meta_root_node, &$commandr_fs)
    {
        list($path, $place) = $this->get_complete_path($meta_dir);

        $listing = array();
        if (is_dir($path)) {
            $dh = opendir($path);
            while (($file = readdir($dh)) !== false) {
                if (($file != '.') && ($file != '..') && ($file != '.git')) {
                    $listing[] = array(
                        $file,
75
                        is_dir($path . '/' . $file) ? COMMANDR_FS_DIR : COMMANDR_FS_FILE,
76
                        null/*don't calculate a filesize*/,
Chris Graham's avatar
Chris Graham committed
77 78 79 80 81 82 83
                        filemtime($path . '/' . $file),
                    );
                }
            }

            // Folder meta
            $listing[] = array(
84 85
                RESOURCE_FS_SPECIAL_DIRECTORY_FILE,
                COMMANDR_FS_FILE,
Chris Graham's avatar
Chris Graham committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
                null,
                filemtime($path),
            );

            return $listing;
        }

        return false; // Directory doesn't exist
    }

    /**
     * Standard Commandr-fs directory creation function for commandr_fs hooks.
     *
     * @param  array $meta_dir The current meta-directory path
     * @param  string $meta_root_node The root node of the current meta-directory
     * @param  string $new_dir_name The new directory name
     * @param  object $commandr_fs A reference to the Commandr filesystem object
     * @return boolean Success?
     */
    public function make_directory($meta_dir, $meta_root_node, $new_dir_name, &$commandr_fs)
    {
        $new_dir_name = filter_naughty($new_dir_name);

        list($path, $place) = $this->get_complete_path($meta_dir);

        if ((is_dir($path)) && (!file_exists($path . '/' . $new_dir_name)) && (is_writable_wrap($path))) {
Chris Graham's avatar
Chris Graham committed
112
            $ret = @mkdir($path . '/' . $new_dir_name, 0777) or warn_exit(do_lang_tempcode('WRITE_ERROR_DIRECTORY', escape_html($path . '/' . $new_dir_name)));
Chris Graham's avatar
Chris Graham committed
113
            fix_permissions($path . '/' . $new_dir_name);
Chris Graham's avatar
Chris Graham committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            sync_file($path . '/' . $new_dir_name);
            return $ret;
        } else {
            return false; // Directory exists
        }
    }

    /**
     * Standard Commandr-fs directory removal function for commandr_fs hooks.
     *
     * @param  array $meta_dir The current meta-directory path
     * @param  string $meta_root_node The root node of the current meta-directory
     * @param  string $dir_name The directory name
     * @param  object $commandr_fs A reference to the Commandr filesystem object
     * @return boolean Success?
     */
    public function remove_directory($meta_dir, $meta_root_node, $dir_name, &$commandr_fs)
    {
        $dir_name = filter_naughty($dir_name);

        list($path, $place) = $this->get_complete_path($meta_dir);

        if ((is_dir($path)) && (file_exists($path . '/' . $dir_name)) && (is_writable_wrap($path . '/' . $dir_name))) {
            require_code('files');
            deldir_contents($path . '/' . $dir_name);
            $ret = @rmdir($path . '/' . $dir_name) or warn_exit(do_lang_tempcode('WRITE_ERROR', escape_html($path . '/' . $dir_name)));
            sync_file($path . '/' . $dir_name);

            // Cleanup from DB
Chris Graham's avatar
Chris Graham committed
143
            $test = $GLOBALS['SITE_DB']->query_select_value_if_there('filedump', 'description', array('name' => cms_mb_substr($dir_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)));
Chris Graham's avatar
Chris Graham committed
144 145
            if (!is_null($test)) {
                delete_lang($test);
Chris Graham's avatar
Chris Graham committed
146
                $GLOBALS['SITE_DB']->query_delete('filedump', array('name' => cms_mb_substr($dir_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)), '', 1);
Chris Graham's avatar
Chris Graham committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
            }

            return true;
        } else {
            return false; // Directory doesn't exist
        }
    }

    /**
     * Standard Commandr-fs file removal function for commandr_fs hooks.
     *
     * @param  array $meta_dir The current meta-directory path
     * @param  string $meta_root_node The root node of the current meta-directory
     * @param  string $file_name The file name
     * @param  object $commandr_fs A reference to the Commandr filesystem object
     * @return boolean Success?
     */
    public function remove_file($meta_dir, $meta_root_node, $file_name, &$commandr_fs)
    {
        $file_name = filter_naughty($file_name);

        list($path, $place) = $this->get_complete_path($meta_dir);

170
        if ($file_name == RESOURCE_FS_SPECIAL_DIRECTORY_FILE) {
Chris Graham's avatar
Chris Graham committed
171 172 173 174 175 176
            // What if folder meta...

            $dir_name = basename($place);
            $place = dirname($place);

            // Cleanup from DB
Chris Graham's avatar
Chris Graham committed
177
            $test = $GLOBALS['SITE_DB']->query_select_value_if_there('filedump', 'description', array('name' => cms_mb_substr($dir_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)));
Chris Graham's avatar
Chris Graham committed
178 179
            if (!is_null($test)) {
                delete_lang($test);
Chris Graham's avatar
Chris Graham committed
180
                $GLOBALS['SITE_DB']->query_delete('filedump', array('name' => cms_mb_substr($dir_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)), '', 1);
Chris Graham's avatar
Chris Graham committed
181 182 183 184 185 186 187 188 189 190
            }

            return true;
        }

        if ((is_dir($path)) && (file_exists($path . '/' . $file_name)) && (is_writable_wrap($path . '/' . $file_name))) {
            $ret = @unlink($path . '/' . $file_name) or intelligent_write_error($path . '/' . $file_name);
            sync_file($path . '/' . $file_name);

            // Cleanup from DB
Chris Graham's avatar
Chris Graham committed
191
            $test = $GLOBALS['SITE_DB']->query_select_value_if_there('filedump', 'description', array('name' => cms_mb_substr($file_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)));
Chris Graham's avatar
Chris Graham committed
192 193
            if (!is_null($test)) {
                delete_lang($test);
Chris Graham's avatar
Chris Graham committed
194
                $GLOBALS['SITE_DB']->query_delete('filedump', array('name' => cms_mb_substr($file_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)), '', 1);
Chris Graham's avatar
Chris Graham committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
            }

            return $ret;
        } else {
            return false; // File doesn't exist
        }
    }

    /**
     * Standard Commandr-fs file reading function for commandr_fs hooks.
     *
     * @param  array $meta_dir The current meta-directory path
     * @param  string $meta_root_node The root node of the current meta-directory
     * @param  string $file_name The file name
     * @param  object $commandr_fs A reference to the Commandr filesystem object
     * @return ~string The file contents (false: failure)
     */
    public function read_file($meta_dir, $meta_root_node, $file_name, &$commandr_fs)
    {
        $file_name = filter_naughty($file_name);

        list($path, $place) = $this->get_complete_path($meta_dir);

218
        if ($file_name == RESOURCE_FS_SPECIAL_DIRECTORY_FILE) {
Chris Graham's avatar
Chris Graham committed
219 220 221 222 223
            // What if folder meta...

            $dir_name = basename($place);
            $place = dirname($place);

Chris Graham's avatar
Chris Graham committed
224
            $rows = table_to_portable_rows('filedump', array('id'), array('name' => cms_mb_substr($dir_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)));
Chris Graham's avatar
Chris Graham committed
225 226 227 228 229 230 231 232 233 234 235 236 237
            if (array_key_exists(0, $rows)) {
                $output = $rows[0];
            } else {
                $output = array('description' => '', 'the_member' => remap_resource_id_as_portable('member', get_member()));
            }

            return json_encode($output);
        }

        if ((is_dir($path)) && (file_exists($path . '/' . $file_name)) && (is_readable($path . '/' . $file_name))) {
            $data = file_get_contents($path . '/' . $file_name);

            $output = array('data' => base64_encode($data));
Chris Graham's avatar
Chris Graham committed
238
            $rows = table_to_portable_rows('filedump', array('id'), array('name' => cms_mb_substr($file_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)));
Chris Graham's avatar
Chris Graham committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
            if (array_key_exists(0, $rows)) {
                $output += $rows[0];
            } else {
                $output += array('description' => '', 'the_member' => remap_resource_id_as_portable('member', get_member()));
            }

            return json_encode($output);
        } else {
            return false; // File doesn't exist
        }
    }

    /**
     * Standard Commandr-fs file writing function for commandr_fs hooks.
     *
     * @param  array $meta_dir The current meta-directory path
     * @param  string $meta_root_node The root node of the current meta-directory
     * @param  string $file_name The file name
     * @param  string $contents The new file contents
     * @param  object $commandr_fs A reference to the Commandr filesystem object
     * @return boolean Success?
     */
    public function write_file($meta_dir, $meta_root_node, $file_name, $contents, &$commandr_fs)
    {
        $file_name = filter_naughty($file_name);

        list($path, $place) = $this->get_complete_path($meta_dir);

267
        if ($file_name == RESOURCE_FS_SPECIAL_DIRECTORY_FILE) {
Chris Graham's avatar
Chris Graham committed
268 269 270 271 272 273 274 275
            // What if folder meta...

            $dir_name = basename($place);
            $place = dirname($place);

            $input = json_decode($contents, true);

            if (count($input) != 0) {
Chris Graham's avatar
Chris Graham committed
276
                table_from_portable_rows('filedump', array($input), array('name' => cms_mb_substr($dir_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)), TABLE_REPLACE_MODE_BY_EXTRA_FIELD_DATA);
Chris Graham's avatar
Chris Graham committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290
            }

            return true;
        }

        if ((is_dir($path)) && (((file_exists($path . '/' . $file_name)) && (is_writable_wrap($path . '/' . $file_name))) || ((!file_exists($path . '/' . $file_name)) && (is_writable_wrap($path))))) {
            $input = json_decode($contents, true);

            $data = @base64_decode($input['data']);
            if ($data === false) {
                $data = $input['data'];
            }
            unset($input['data']);

291 292
            require_code('files');
            cms_file_put_contents_safe($path . '/' . $file_name, $data, FILE_WRITE_FIX_PERMISSIONS | FILE_WRITE_SYNC_FILE);
Chris Graham's avatar
Chris Graham committed
293 294

            if (count($input) != 0) {
Chris Graham's avatar
Chris Graham committed
295
                table_from_portable_rows('filedump', array($input), array('name' => cms_mb_substr($file_name, 0, 80), 'path' => cms_mb_substr($place, 0, 80)), TABLE_REPLACE_MODE_BY_EXTRA_FIELD_DATA);
Chris Graham's avatar
Chris Graham committed
296 297 298 299 300 301 302 303
            }

            return true;
        } else {
            return false; // File doesn't exist
        }
    }
}