Completed
Pull Request — master (#16)
by Michael
01:51
created

include/update_function.php (7 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
 * News functions
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright   {@link https://xoops.org/ XOOPS Project}
13
 * @license     GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
14
 * @author      Voltan
15
 * @package     News
16
 */
17
18
function xoops_module_update_news()
19
{
20
    require_once XOOPS_ROOT_PATH . '/modules/news/class/utility.php';
21
    global $xoopsDB;
22
    $errors = 0;
23
24
    //0) Rename all tables
25
26
    if (NewsUtility::existTable($xoopsDB->prefix('stories_files'))) {
27
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('stories_files') . ' RENAME ' . $xoopsDB->prefix('news_stories_files'));
28
        $result = $xoopsDB->queryF($sql);
29
        if (!$result) {
30
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
31
            ++$errors;
32
        }
33 View Code Duplication
    } else {
34
35
        // 1) Create, if it does not exists, the stories_files table
36
        if (!NewsUtility::existTable($xoopsDB->prefix('news_stories_files'))) {
37
            $sql = 'CREATE TABLE ' . $xoopsDB->prefix('news_stories_files') . " (
38
              fileid INT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
39
              filerealname VARCHAR(255) NOT NULL DEFAULT '',
40
              storyid INT(8) UNSIGNED NOT NULL DEFAULT '0',
41
              date INT(10) NOT NULL DEFAULT '0',
42
              mimetype VARCHAR(64) NOT NULL DEFAULT '',
43
              downloadname VARCHAR(255) NOT NULL DEFAULT '',
44
              counter INT(8) UNSIGNED NOT NULL DEFAULT '0',
45
              PRIMARY KEY  (fileid),
46
              KEY storyid (storyid)
47
            ) ENGINE=MyISAM;";
48
            if (!$xoopsDB->queryF($sql)) {
49
                echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED1;
50
                ++$errors;
51
            }
52
        }
53
    }
54
55 View Code Duplication
    if (NewsUtility::existTable($xoopsDB->prefix('stories'))) {
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...
56
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('stories') . ' RENAME ' . $xoopsDB->prefix('news_stories'));
57
        $result = $xoopsDB->queryF($sql);
58
        if (!$result) {
59
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
60
            ++$errors;
61
        }
62
    }
63
64 View Code Duplication
    if (NewsUtility::existTable($xoopsDB->prefix('topics'))) {
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...
65
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('topics') . ' RENAME ' . $xoopsDB->prefix('news_topics'));
66
        $result = $xoopsDB->queryF($sql);
67
        if (!$result) {
68
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
69
            ++$errors;
70
        }
71
    }
72
73 View Code Duplication
    if (NewsUtility::existTable($xoopsDB->prefix('stories_files'))) {
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...
74
        $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('stories_files') . ' RENAME ' . $xoopsDB->prefix('news_stories_files'));
75
        $result = $xoopsDB->queryF($sql);
76
        if (!$result) {
77
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
78
            ++$errors;
79
        }
80
    }
81
82
    // 2) Change the topic title's length, in the topics table
83
    $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' CHANGE topic_title topic_title VARCHAR( 255 ) NOT NULL;');
84
    $result = $xoopsDB->queryF($sql);
85
    if (!$result) {
86
        echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED2;
87
        ++$errors;
88
    }
89
90
    // 2.1) Add the new fields to the topic table
91
    if (!NewsUtility::existField('menu', $xoopsDB->prefix('news_topics'))) {
92
        NewsUtility::addField("menu TINYINT( 1 ) DEFAULT '0' NOT NULL", $xoopsDB->prefix('news_topics'));
93
    }
94
    if (!NewsUtility::existField('topic_frontpage', $xoopsDB->prefix('news_topics'))) {
95
        NewsUtility::addField("topic_frontpage TINYINT( 1 ) DEFAULT '1' NOT NULL", $xoopsDB->prefix('news_topics'));
96
    }
97
    if (!NewsUtility::existField('topic_rssurl', $xoopsDB->prefix('news_topics'))) {
98
        NewsUtility::addField('topic_rssurl VARCHAR( 255 ) NOT NULL', $xoopsDB->prefix('news_topics'));
99
    }
100
    if (!NewsUtility::existField('topic_description', $xoopsDB->prefix('news_topics'))) {
101
        NewsUtility::addField('topic_description TEXT NOT NULL', $xoopsDB->prefix('news_topics'));
102
    }
103
    if (!NewsUtility::existField('topic_color', $xoopsDB->prefix('news_topics'))) {
104
        NewsUtility::addField("topic_color varchar(6) NOT NULL default '000000'", $xoopsDB->prefix('news_topics'));
105
    }
106
107
    // 3) If it does not exists, create the table stories_votedata
108 View Code Duplication
    if (!NewsUtility::existTable($xoopsDB->prefix('news_stories_votedata'))) {
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...
109
        $sql = 'CREATE TABLE ' . $xoopsDB->prefix('news_stories_votedata') . " (
110
              ratingid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
111
              storyid INT(8) UNSIGNED NOT NULL DEFAULT '0',
112
              ratinguser INT(11) NOT NULL DEFAULT '0',
113
              rating TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
114
              ratinghostname VARCHAR(60) NOT NULL DEFAULT '',
115
              ratingtimestamp INT(10) NOT NULL DEFAULT '0',
116
              PRIMARY KEY  (ratingid),
117
              KEY ratinguser (ratinguser),
118
              KEY ratinghostname (ratinghostname),
119
              KEY storyid (storyid)
120
            ) ENGINE=MyISAM;";
121
        if (!$xoopsDB->queryF($sql)) {
122
            echo '<br>' . _AM_NEWS_UPGRADEFAILED . ' ' . _AM_NEWS_UPGRADEFAILED3;
123
            ++$errors;
124
        }
125
    }
126
127
    // 4) Create the four new fields for the votes in the story table
128
    if (!NewsUtility::existField('rating', $xoopsDB->prefix('news_stories'))) {
129
        NewsUtility::addField("rating DOUBLE( 6, 4 ) DEFAULT '0.0000' NOT NULL", $xoopsDB->prefix('news_stories'));
130
    }
131
    if (!NewsUtility::existField('votes', $xoopsDB->prefix('news_stories'))) {
132
        NewsUtility::addField("votes INT( 11 ) UNSIGNED DEFAULT '0' NOT NULL", $xoopsDB->prefix('news_stories'));
133
    }
134
    if (!NewsUtility::existField('keywords', $xoopsDB->prefix('news_stories'))) {
135
        NewsUtility::addField('keywords VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
136
    }
137
    if (!NewsUtility::existField('description', $xoopsDB->prefix('news_stories'))) {
138
        NewsUtility::addField('description VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
139
    }
140
    if (!NewsUtility::existField('pictureinfo', $xoopsDB->prefix('news_stories'))) {
141
        NewsUtility::addField('pictureinfo VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
142
    }
143
    if (!NewsUtility::existField('subtitle', $xoopsDB->prefix('news_stories'))) {
144
        NewsUtility::addField('subtitle VARCHAR(255) NOT NULL', $xoopsDB->prefix('news_stories'));
145
    }
146
147
    // 5) Add some indexes to the topics table
148
    $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' ADD INDEX ( `topic_title` );');
149
    $result = $xoopsDB->queryF($sql);
150
    $sql    = sprintf('ALTER TABLE ' . $xoopsDB->prefix('news_topics') . ' ADD INDEX ( `menu` );');
151
    $result = $xoopsDB->queryF($sql);
152
153
    // 6) Make files and folders
154
    $dir = XOOPS_ROOT_PATH . '/uploads/news';
155 View Code Duplication
    if (!@mkdir($dir) && !is_dir($dir)) {
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...
156
        throw new \RuntimeException('The directory ' . $dir . ' could not be created.');
157
    }
158
    if (!is_writable($dir)) {
159
        chmod($dir, 0777);
160
    }
161
162
    $dir = XOOPS_ROOT_PATH . '/uploads/news/file';
163 View Code Duplication
    if (!@mkdir($dir) && !is_dir($dir)) {
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...
164
        throw new \RuntimeException('The directory ' . $dir . ' could not be created.');
165
    }
166
    if (!is_writable($dir)) {
167
        chmod($dir, 0777);
168
    }
169
170
    $dir = XOOPS_ROOT_PATH . '/uploads/news/image';
171 View Code Duplication
    if (!@mkdir($dir) && !is_dir($dir)) {
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...
172
        throw new \RuntimeException('The directory ' . $dir . ' could not be created.');
173
    }
174
    if (!is_writable($dir)) {
175
        chmod($dir, 0777);
176
    }
177
178
    // Copy index.html files on uploads folders
179
    $indexFile = XOOPS_ROOT_PATH . '/modules/news/include/index.html';
180
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/index.html');
181
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/file/index.html');
182
    copy($indexFile, XOOPS_ROOT_PATH . '/uploads/news/image/index.html');
183
184
    return true;
185
}
186