Passed
Pull Request — master (#41)
by Michael
13:46
created

archive.php (2 issues)

Labels
1
<?php declare(strict_types=1);
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @author         XOOPS Development Team
16
 */
17
18
/*
19
 * Display a list of all the published articles by month
20
 *
21
 * This page is called from the module's main menu.
22
 * It will shows a list of all the articles by month. We use the module's
23
 * option named "restrictindex" to show or hide stories according
24
 * to users permissions
25
 *
26
 * @package News
27
 * @author Xoops Modules Dev Team
28
 * @copyright   (c) XOOPS Project (https://xoops.org)
29
 *
30
 * Parameters received by this page :
31
 * @page_param  int     year    Optional, the starting year
32
 * @page_param  int     month   Optional, the starting month
33
 *
34
 * @page_title          "News Archives" - Year - Month - Module's name
35
 *
36
 * @template_name       news_archive.html
37
 *
38
 * Template's variables :
39
 * @template_var    array   years           Contains all the years we have information for
40
 *                                          Structure :
41
 *                                              number  int     Year (2004 for example)
42
 *                                              months  array   moths in the year (months when we have some articles)
43
 *                                                  Structure :
44
 *                                                  string  string  Month's name
45
 *                                                  number  int     Month's number (between 1 and 12)
46
 * @template_var    boolean show_articles   true or false
47
 * @template_var    string  lang_articles   Fixed text "Articles"
48
 * @template_var    array   currentmonth    Label of each month (from january to december)
49
 * @template_var    int     currentyear     Starting year
50
 * @template_var    string  lang_actions    Fixed text "Actions"
51
 * @template_var    string  lang_date       Fixed text "Date"
52
 * @template_var    string  lang_views      Fixed text "Views"
53
 * @template_var    array   stories         Contains all the stories to display
54
 *                                          Structure :
55
 *                                          title       string  Contains a link to see the topic and a link (with the story's title) to read the full story
56
 *                                          counter     int     Number of views for this article
57
 *                                          date        string  Article's publish date
58
 *                                          print_link  string  A link to the story's printable version
59
 *                                          mail_link   string  A mailto link to mail the story to a friend
60
 * @template_var    string  lang_printer    Fixed text "Printer Friendly Page"
61
 * @template_var    string  lang_sendstory  Fixed text "Send this Story to a Friend"
62
 * @template_var    string  lang_storytotal Text "There are xx article(s) in total"
63
 */
64
######################################################################
65
# Original version:
66
# [11-may-2001] Kenneth Lee - https://www.nexgear.com/
67
######################################################################
68
69
use Xmf\Request;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Request. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
70
use XoopsModules\News\{
71
    NewsStory,
72
    Utility
73
};
74
75
require_once \dirname(__DIR__, 2) . '/mainfile.php';
76
$GLOBALS['xoopsOption']['template_main'] = 'news_archive.tpl';
77
require_once XOOPS_ROOT_PATH . '/header.php';
78
// require_once XOOPS_ROOT_PATH . '/modules/news/class/class.newsstory.php';
79
require_once XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/calendar.php';
80
81
$lastyear  = 0;
82
$lastmonth = 0;
83
84
$months_arr = [
85
    1  => _CAL_JANUARY,
86
    2  => _CAL_FEBRUARY,
87
    3  => _CAL_MARCH,
88
    4  => _CAL_APRIL,
89
    5  => _CAL_MAY,
90
    6  => _CAL_JUNE,
91
    7  => _CAL_JULY,
92
    8  => _CAL_AUGUST,
93
    9  => _CAL_SEPTEMBER,
94
    10 => _CAL_OCTOBER,
95
    11 => _CAL_NOVEMBER,
96
    12 => _CAL_DECEMBER,
97
];
98
99
/** @var int $fromyear */
100
$fromyear  = Request::getInt('year', 0, 'GET');
101
/** @var int $frommonth */
102
$frommonth = Request::getInt('month', 0, 'GET');
103
104
$pgtitle = '';
105
if ($fromyear && $frommonth) {
106
    $pgtitle = sprintf(' - %d - %d', $fromyear, $frommonth);
107
}
108
$infotips   = Utility::getModuleOption('infotips');
109
$restricted = Utility::getModuleOption('restrictindex');
110
$dateformat = Utility::getModuleOption('dateformat');
111
if ('' === $dateformat) {
112
    $dateformat = 'm';
113
}
114
$myts = \MyTextSanitizer::getInstance();
115
$xoopsTpl->assign('xoops_pagetitle', htmlspecialchars(_NW_NEWSARCHIVES, ENT_QUOTES | ENT_HTML5) . $pgtitle . ' - ' . $xoopsModule->name('s'));
116
117
$useroffset = '';
118
if (is_object($xoopsUser)) {
119
    $timezone = $xoopsUser->timezone();
120
    if (isset($timezone)) {
121
        $useroffset = $xoopsUser->timezone();
122
    } else {
123
        $useroffset = $xoopsConfig['default_TZ'];
124
    }
125
}
126
$sql = 'SELECT published FROM ' . $xoopsDB->prefix('news_stories') . ' WHERE (published>0 AND published<=' . time() . ') AND (expired = 0 OR expired <= ' . time() . ') ORDER BY published DESC';
127
$result = Utility::queryAndCheck($xoopsDB, $sql);
128
$years  = [];
129
$months = [];
130
$i      = 0;
131
while ([$time] = $xoopsDB->fetchRow($result)) {
132
    $time = formatTimestamp($time, 'mysql', $useroffset);
133
    if (preg_match('/(\d{4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})/', $time, $datetime)) {
134
        $this_year  = (int)$datetime[1];
135
        $this_month = (int)$datetime[2];
136
        if (empty($lastyear)) {
137
            $lastyear = $this_year;
138
        }
139
        if (0 == $lastmonth) {
140
            $lastmonth                    = $this_month;
141
            $months[$lastmonth]['string'] = $months_arr[$lastmonth];
142
            $months[$lastmonth]['number'] = $lastmonth;
143
        }
144
        if ($lastyear != $this_year) {
145
            $years[$i]['number'] = $lastyear;
146
            $years[$i]['months'] = $months;
147
            $months              = [];
148
            $lastmonth           = 0;
149
            $lastyear            = $this_year;
150
            ++$i;
151
        }
152
        if ($lastmonth != $this_month) {
153
            $lastmonth                    = $this_month;
154
            $months[$lastmonth]['string'] = $months_arr[$lastmonth];
155
            $months[$lastmonth]['number'] = $lastmonth;
156
        }
157
    }
158
}
159
$years[$i]['number'] = $this_year;
160
$years[$i]['months'] = $months;
161
$xoopsTpl->assign('years', $years);
162
163
if (0 != $fromyear && 0 != $frommonth) {
164
    $xoopsTpl->assign('show_articles', true);
165
    $xoopsTpl->assign('lang_articles', _NW_ARTICLES);
166
    $xoopsTpl->assign('currentmonth', $months_arr[$frommonth]);
167
    $xoopsTpl->assign('currentyear', $fromyear);
168
    $xoopsTpl->assign('lang_actions', _NW_ACTIONS);
169
    $xoopsTpl->assign('lang_date', _NW_DATE);
170
    $xoopsTpl->assign('lang_views', _NW_VIEWS);
171
172
    // must adjust the selected time to server timestamp
173
    $timeoffset = (int)($useroffset - $xoopsConfig['server_TZ']);
174
    $monthstart = mktime(0 - $timeoffset, 0, 0, $frommonth, 1, $fromyear);
175
    $monthend   = mktime(23 - $timeoffset, 59, 59, $frommonth + 1, 0, $fromyear);
176
    $monthend   = ($monthend > time()) ? time() : $monthend;
177
178
    $count      = 0;
179
    $news       = new NewsStory();
180
    $storyarray = $news->getArchive($monthstart, $monthend, $restricted);
181
    $count      = count($storyarray);
0 ignored issues
show
It seems like $storyarray can also be of type null; however, parameter $value of count() does only seem to accept Countable|array, 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

181
    $count      = count(/** @scrutinizer ignore-type */ $storyarray);
Loading history...
182
    if (is_array($storyarray) && $count > 0) {
183
        foreach ($storyarray as $article) {
184
            $story     = [];
185
            $htmltitle = '';
186
            if ($infotips > 0) {
187
                $story['infotips'] = Utility::makeInfotips($article->hometext());
188
                $htmltitle         = ' title="' . $story['infotips'] . '"';
189
            }
190
            $story['title']      = "<a href='"
191
                                   . XOOPS_URL
192
                                   . '/modules/news/index.php?storytopic='
193
                                   . $article->topicid()
194
                                   . "'>"
195
                                   . $article->topic_title()
196
                                   . "</a>: <a href='"
197
                                   . XOOPS_URL
198
                                   . '/modules/news/article.php?storyid='
199
                                   . $article->storyid()
200
                                   . "'"
201
                                   . $htmltitle
202
                                   . '>'
203
                                   . $article->title()
204
                                   . '</a>';
205
            $story['counter']    = $article->counter();
206
            $story['date']       = formatTimestamp($article->published(), $dateformat, $useroffset);
207
            $story['print_link'] = XOOPS_URL . '/modules/news/print.php?storyid=' . $article->storyid();
208
            $story['mail_link']  = 'mailto:?subject=' . sprintf(_NW_INTARTICLE, $xoopsConfig['sitename']) . '&amp;body=' . sprintf(_NW_INTARTFOUND, $xoopsConfig['sitename']) . ':  ' . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . '/article.php?storyid=' . $article->storyid();
209
            $xoopsTpl->append('stories', $story);
210
        }
211
    }
212
    $xoopsTpl->assign('lang_printer', _NW_PRINTERFRIENDLY);
213
    $xoopsTpl->assign('lang_sendstory', _NW_SENDSTORY);
214
    $xoopsTpl->assign('lang_storytotal', sprintf(_NW_THEREAREINTOTAL, $count));
215
} else {
216
    $xoopsTpl->assign('show_articles', false);
217
}
218
219
$xoopsTpl->assign('lang_newsarchives', _NW_NEWSARCHIVES);
220
221
/**
222
 * Create the metadatas
223
 */
224
Utility::createMetaDatas();
225
226
require_once XOOPS_ROOT_PATH . '/footer.php';
227