Project

General

Profile

1
<?php
2
/*
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
/**
19
 * @category     template
20
 * @package      template_DefaultTheme
21
 * @copyright    Manuela v.d.Decken <manuela@isteam.de>
22
 * @author       Manuela v.d.Decken <manuela@isteam.de>
23
 * @license      http://www.gnu.org/licenses/gpl.html
24
 * @revision     $Revision: 2 $
25
 * @lastmodified $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
26
 * @since        File available since 25.02.2017
27
 * @deprecated   no / since 0000/00/00
28
 * @description
29
 */
30
// -----------------------------------------------------------------------------
31

    
32
/**
33
 * delete directory tree
34
 * @param string $sBasedir  the absolute path including a trailing slash
35
 * @param bool   $bRemoveBasedir  (default) true = remove base directory || false = preserve it
36
 * @throws Exception
37
 * @descripion deletes a tree recursively from given basedir
38
 */
39
    function rebuildAccessFiles_delTree($sBasedir, $bRemoveBasedir = true)
40
    {
41
        $aSubDirs = glob($sBasedir.'*', GLOB_MARK|GLOB_ONLYDIR);
42
        foreach ($aSubDirs as $sSubDir) {
43
            rebuildAccessFiles_delTree($sSubDir, true);
44
        }
45
        array_map(
46
            function ($sFile) {
47
                if (!@unlink($sFile)) { throw new Exception('delTree: Unable to delete file'); }
48
            },
49
            glob($sBasedir.'*')
50
        );
51
        if ($bRemoveBasedir) {
52
            if (!rmdir($sBasedir)) { throw new Exception('delTree: Unable to remove directory'); }
53
        }
54
    }
55
// -----------------------------------------------------------------------------
56
/**
57
 * check if file is an access file
58
 * @param string $sFileName
59
 * @return bool
60
 * @throws Exception
61
 */
62
    function rebuildAccessFiles_isAccessFile($sFileName)
63
    {
64
        $bRetval = false;
65
        if (file_exists($sFileName)) {
66
            if (!is_readable($sFileName)) { throw new Exception('invalid filename ['.basename($sFileName).']'); }
67
            if (($sFile = file_get_contents($sFileName)) !== false) {
68
            // test content of this file
69
                $sPattern = '/^\s*?<\?php.*?\$i?page_?id\s*=\s*[0-9]+;.*?(?:require|include)'
70
                          . '(?:_once)?\s*\(\s*\'.*?index\.php\'\s?\);/siU';
71
                $bRetval = (bool) preg_match($sPattern, $sFile);
72
                unset($sFile);
73
            }
74
        }
75
        return $bRetval;
76
    }
77
// -----------------------------------------------------------------------------
78
// direct access code
79
// -----------------------------------------------------------------------------
80
    if (!defined('WB_PATH')) { require (dirname(dirname(dirname(__DIR__)))).'/config.php'; }
81
    $aJsonRespond = [];
82
    try {
83
    // check autentification
84
        if (!class_exists('admin', false)) { require(WB_PATH.'/framework/class.admin.php'); }
85
        $admin = new admin('Pages', 'pages_settings',false);
86
        if (!$admin->is_authenticated() || $admin->get_user_id() != 1) {
87
            throw new Exception('Access denied');
88
        }
89
        $iFilesCreated = 0;
90
    // Find all active Level 0 pages and delete the files and possible related directories
91
        $sql = 'SELECT `link` FROM `'.TABLE_PREFIX.'pages` WHERE `level`= 0';
92
        if (($oPages = $database->query($sql))) {
93
            while (($aPage = $oPages->fetchRow(MYSQLI_ASSOC))) {
94
            // santize path
95
                $sAccessDir = str_replace('\\', '/', WB_PATH.PAGES_DIRECTORY.$aPage['link']);
96
                $sAccessFile = $sAccessDir.PAGE_EXTENSION;
97
            // test if file is really an access file
98
                if (rebuildAccessFiles_isAccessFile($sAccessFile)) {
99
                // delete the subdir and it's content if exists
100
                    if (file_exists($sAccessDir.'/')) { rebuildAccessFiles_delTree($sAccessDir.'/'); }
101
                // delete the current accessfile
102
                    if (!@unlink($sAccessFile)) { throw new Exception('Unable to delete file'); }
103
                }
104
            }
105
        }
106
    // get all pages from database
107
        $sql = 'SELECT `page_id`, `link`, `level` FROM `'.TABLE_PREFIX.'pages` '
108
             . 'ORDER BY `link`';
109
        if (!($oPages = $database->query($sql))) { throw new Exception('Database access failed'); }
110
        while (($aPage = $oPages->fetchRow(MYSQLI_ASSOC))) {
111
            $sFilePath = WB_PATH.PAGES_DIRECTORY.$aPage['link'].PAGE_EXTENSION;
112
            $sDirPath = dirname($sFilePath).'/';
113
            if (!is_dir($sDirPath)) {
114
                // create missing directory
115
                if (!mkdir($sDirPath)) { throw new Exception('Unable to create new directory'); }
116
                if (OPERATING_SYSTEM == 'linux') { chmod($sDirPath, OCTAL_DIR_MODE); }
117
            }
118
            $iRepeats = preg_match_all('/\//', PAGES_DIRECTORY) + $aPage['level'];
119
            $sIndexLocation = str_repeat('../', $iRepeats);
120
            $sContent =
121
                '<?php'."\n".
122
                '// *** This file is generated by WebsiteBaker Ver.'.VERSION."\n".
123
                '// *** Creation date: '.date('c')."\n".
124
                '// *** Do not modify this file manually'."\n".
125
                '// *** WB will rebuild this file from time to time!!'."\n".
126
                '// *************************************************'."\n".
127
                "\t".'$page_id    = '.$aPage['page_id'].';'."\n".
128
                "\t".'require(\''.$sIndexLocation.'index.php\');'."\n".
129
                '// *************************************************'."\n";
130
            if (file_put_contents($sFilePath, $sContent) === false) {
131
                throw new Exception('Unable to write new file');
132
            }
133
            if (OPERATING_SYSTEM == 'linux') { chmod($sFilePath, OCTAL_FILE_MODE); }
134
            $iFilesCreated++;
135
        }
136
        $aJsonRespond['message'] = 'Rebuild done:: '.$iFilesCreated.' access files created';
137
        $aJsonRespond['success'] = true;
138
    } catch (Exception $e) {
139
        $aJsonRespond['message'] = 'Rebuild failed:: '.$e->getMessage().'!';
140
        $aJsonRespond['success'] = false;
141
    }
142
    // echo the json_respond to the ajax function
143
    exit(json_encode($aJsonRespond));
(14-14/17)