| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @brief widgets, a plugin for Dotclear 2 |
|---|
| 4 | * |
|---|
| 5 | * @package Dotclear |
|---|
| 6 | * @subpackage Plugins |
|---|
| 7 | * |
|---|
| 8 | * @copyright Olivier Meunier & Association Dotclear |
|---|
| 9 | * @copyright GPL-2.0-only |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | if (!defined('DC_RC_PATH')) {return;} |
|---|
| 13 | |
|---|
| 14 | class defaultWidgets |
|---|
| 15 | { |
|---|
| 16 | public static function search($w) |
|---|
| 17 | { |
|---|
| 18 | global $core; |
|---|
| 19 | |
|---|
| 20 | if ($core->blog->settings->system->no_search) { |
|---|
| 21 | return; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | if ($w->offline) { |
|---|
| 25 | return; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
|---|
| 29 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
|---|
| 30 | return; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | $value = isset($GLOBALS['_search']) ? html::escapeHTML($GLOBALS['_search']) : ''; |
|---|
| 34 | |
|---|
| 35 | return $w->renderDiv($w->content_only, $w->class, 'id="search"', |
|---|
| 36 | ($w->title ? $w->renderTitle('<label for="q">' . html::escapeHTML($w->title) . '</label>') : '') . |
|---|
| 37 | '<form action="' . $core->blog->url . '" method="get" role="search">' . |
|---|
| 38 | '<fieldset>' . |
|---|
| 39 | '<p><input type="text" size="10" maxlength="255" id="q" name="q" value="' . $value . '" ' . |
|---|
| 40 | ($w->placeholder ? 'placeholder="' . html::escapeHTML($w->placeholder) . '"' : '') . |
|---|
| 41 | ' aria-label="' . __('Search') . '"/> ' . |
|---|
| 42 | '<input type="submit" class="submit" value="ok" /></p>' . |
|---|
| 43 | '</fieldset>' . |
|---|
| 44 | '</form>'); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | public static function navigation($w) |
|---|
| 48 | { |
|---|
| 49 | global $core; |
|---|
| 50 | |
|---|
| 51 | if ($w->offline) { |
|---|
| 52 | return; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
|---|
| 56 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
|---|
| 57 | return; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | $res = |
|---|
| 61 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
|---|
| 62 | '<nav role="navigation"><ul>'; |
|---|
| 63 | |
|---|
| 64 | if ($core->url->type != 'default') { |
|---|
| 65 | $res .= |
|---|
| 66 | '<li class="topnav-home">' . |
|---|
| 67 | '<a href="' . $core->blog->url . '">' . __('Home') . '</a>' . |
|---|
| 68 | '</li>'; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | $res .= |
|---|
| 72 | '<li class="topnav-arch">' . |
|---|
| 73 | '<a href="' . $core->blog->url . $core->url->getURLFor("archive") . '">' . |
|---|
| 74 | __('Archives') . '</a></li>' . |
|---|
| 75 | '</ul></nav>'; |
|---|
| 76 | |
|---|
| 77 | return $w->renderDiv($w->content_only, $w->class, 'id="topnav"', $res); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | public static function categories($w) |
|---|
| 81 | { |
|---|
| 82 | global $core, $_ctx; |
|---|
| 83 | |
|---|
| 84 | if ($w->offline) { |
|---|
| 85 | return; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
|---|
| 89 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
|---|
| 90 | return; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | $rs = $core->blog->getCategories(array('post_type' => 'post', 'without_empty' => !$w->with_empty)); |
|---|
| 94 | if ($rs->isEmpty()) { |
|---|
| 95 | return; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | $res = |
|---|
| 99 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : ''); |
|---|
| 100 | |
|---|
| 101 | $ref_level = $level = $rs->level - 1; |
|---|
| 102 | while ($rs->fetch()) { |
|---|
| 103 | $class = ''; |
|---|
| 104 | if (($core->url->type == 'category' && $_ctx->categories instanceof record && $_ctx->categories->cat_id == $rs->cat_id) |
|---|
| 105 | || ($core->url->type == 'post' && $_ctx->posts instanceof record && $_ctx->posts->cat_id == $rs->cat_id)) { |
|---|
| 106 | $class = ' class="category-current"'; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | if ($rs->level > $level) { |
|---|
| 110 | $res .= str_repeat('<ul><li' . $class . '>', $rs->level - $level); |
|---|
| 111 | } elseif ($rs->level < $level) { |
|---|
| 112 | $res .= str_repeat('</li></ul>', -($rs->level - $level)); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | if ($rs->level <= $level) { |
|---|
| 116 | $res .= '</li><li' . $class . '>'; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | $res .= |
|---|
| 120 | '<a href="' . $core->blog->url . $core->url->getURLFor('category', $rs->cat_url) . '">' . |
|---|
| 121 | html::escapeHTML($rs->cat_title) . '</a>' . |
|---|
| 122 | ($w->postcount ? ' <span>(' . ($w->subcatscount ? $rs->nb_total : $rs->nb_post) . ')</span>' : ''); |
|---|
| 123 | |
|---|
| 124 | $level = $rs->level; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | if ($ref_level - $level < 0) { |
|---|
| 128 | $res .= str_repeat('</li></ul>', -($ref_level - $level)); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | return $w->renderDiv($w->content_only, 'categories ' . $w->class, '', $res); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | public static function bestof($w) |
|---|
| 135 | { |
|---|
| 136 | global $core, $_ctx; |
|---|
| 137 | |
|---|
| 138 | if ($w->offline) { |
|---|
| 139 | return; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
|---|
| 143 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
|---|
| 144 | return; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | $params = array( |
|---|
| 148 | 'post_selected' => true, |
|---|
| 149 | 'no_content' => true, |
|---|
| 150 | 'order' => 'post_dt ' . strtoupper($w->orderby) |
|---|
| 151 | ); |
|---|
| 152 | |
|---|
| 153 | $rs = $core->blog->getPosts($params); |
|---|
| 154 | |
|---|
| 155 | if ($rs->isEmpty()) { |
|---|
| 156 | return; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | $res = |
|---|
| 160 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
|---|
| 161 | '<ul>'; |
|---|
| 162 | |
|---|
| 163 | while ($rs->fetch()) { |
|---|
| 164 | $class = ''; |
|---|
| 165 | if ($core->url->type == 'post' && $_ctx->posts instanceof record && $_ctx->posts->post_id == $rs->post_id) { |
|---|
| 166 | $class = ' class="post-current"'; |
|---|
| 167 | } |
|---|
| 168 | $res .= ' <li' . $class . '><a href="' . $rs->getURL() . '">' . html::escapeHTML($rs->post_title) . '</a></li> '; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | $res .= '</ul>'; |
|---|
| 172 | |
|---|
| 173 | return $w->renderDiv($w->content_only, 'selected ' . $w->class, '', $res); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public static function langs($w) |
|---|
| 177 | { |
|---|
| 178 | global $core, $_ctx; |
|---|
| 179 | |
|---|
| 180 | if ($w->offline) { |
|---|
| 181 | return; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | if (($w->homeonly == 1 && $core->url->type != 'default' && $core->url->type != 'lang') || |
|---|
| 185 | ($w->homeonly == 2 && ($core->url->type == 'default' || $core->url->type == 'lang'))) { |
|---|
| 186 | return; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | $rs = $core->blog->getLangs(); |
|---|
| 190 | |
|---|
| 191 | if ($rs->count() <= 1) { |
|---|
| 192 | return; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | $langs = l10n::getISOcodes(); |
|---|
| 196 | $res = |
|---|
| 197 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
|---|
| 198 | '<ul>'; |
|---|
| 199 | |
|---|
| 200 | while ($rs->fetch()) { |
|---|
| 201 | $l = ($_ctx->cur_lang == $rs->post_lang) ? '<strong>%s</strong>' : '%s'; |
|---|
| 202 | |
|---|
| 203 | $lang_name = isset($langs[$rs->post_lang]) ? $langs[$rs->post_lang] : $rs->post_lang; |
|---|
| 204 | |
|---|
| 205 | $res .= |
|---|
| 206 | ' <li>' . |
|---|
| 207 | sprintf($l, |
|---|
| 208 | '<a href="' . $core->blog->url . $core->url->getURLFor('lang', $rs->post_lang) . '" ' . |
|---|
| 209 | 'class="lang-' . $rs->post_lang . '">' . |
|---|
| 210 | $lang_name . '</a>') . |
|---|
| 211 | ' </li>'; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | $res .= '</ul>'; |
|---|
| 215 | |
|---|
| 216 | return $w->renderDiv($w->content_only, 'langs ' . $w->class, '', $res); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | public static function subscribe($w) |
|---|
| 220 | { |
|---|
| 221 | global $core; |
|---|
| 222 | |
|---|
| 223 | if ($w->offline) { |
|---|
| 224 | return; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
|---|
| 228 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
|---|
| 229 | return; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | $type = ($w->type == 'atom' || $w->type == 'rss2') ? $w->type : 'rss2'; |
|---|
| 233 | $mime = $type == 'rss2' ? 'application/rss+xml' : 'application/atom+xml'; |
|---|
| 234 | |
|---|
| 235 | $p_title = __('This blog\'s entries %s feed'); |
|---|
| 236 | $c_title = __('This blog\'s comments %s feed'); |
|---|
| 237 | |
|---|
| 238 | $res = |
|---|
| 239 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
|---|
| 240 | '<ul>'; |
|---|
| 241 | |
|---|
| 242 | $res .= |
|---|
| 243 | '<li><a type="' . $mime . '" ' . |
|---|
| 244 | 'href="' . $core->blog->url . $core->url->getURLFor('feed', $type) . '" ' . |
|---|
| 245 | 'title="' . sprintf($p_title, ($type == 'atom' ? 'Atom' : 'RSS')) . '" class="feed">' . |
|---|
| 246 | __('Entries feed') . '</a></li>'; |
|---|
| 247 | |
|---|
| 248 | if ($core->blog->settings->system->allow_comments || $core->blog->settings->system->allow_trackbacks) { |
|---|
| 249 | $res .= |
|---|
| 250 | '<li><a type="' . $mime . '" ' . |
|---|
| 251 | 'href="' . $core->blog->url . $core->url->getURLFor('feed', $type . '/comments') . '" ' . |
|---|
| 252 | 'title="' . sprintf($c_title, ($type == 'atom' ? 'Atom' : 'RSS')) . '" class="feed">' . |
|---|
| 253 | __('Comments feed') . '</a></li>'; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | $res .= '</ul>'; |
|---|
| 257 | |
|---|
| 258 | return $w->renderDiv($w->content_only, 'syndicate ' . $w->class, '', $res); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | public static function feed($w) |
|---|
| 262 | { |
|---|
| 263 | global $core; |
|---|
| 264 | |
|---|
| 265 | if (!$w->url) { |
|---|
| 266 | return; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | if ($w->offline) { |
|---|
| 270 | return; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
|---|
| 274 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
|---|
| 275 | return; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | $limit = abs((integer) $w->limit); |
|---|
| 279 | |
|---|
| 280 | try { |
|---|
| 281 | $feed = feedReader::quickParse($w->url, DC_TPL_CACHE); |
|---|
| 282 | if ($feed == false || count($feed->items) == 0) { |
|---|
| 283 | return; |
|---|
| 284 | } |
|---|
| 285 | } catch (Exception $e) { |
|---|
| 286 | return; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | $res = |
|---|
| 290 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
|---|
| 291 | '<ul>'; |
|---|
| 292 | |
|---|
| 293 | $i = 0; |
|---|
| 294 | foreach ($feed->items as $item) { |
|---|
| 295 | $title = isset($item->title) && strlen(trim($item->title)) ? $item->title : ''; |
|---|
| 296 | $link = isset($item->link) && strlen(trim($item->link)) ? $item->link : ''; |
|---|
| 297 | |
|---|
| 298 | if (!$link && !$title) { |
|---|
| 299 | continue; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | if (!$title) { |
|---|
| 303 | $title = substr($link, 0, 25) . '...'; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | $li = $link ? '<a href="' . html::escapeHTML($item->link) . '">' . $title . '</a>' : $title; |
|---|
| 307 | $res .= ' <li>' . $li . '</li> '; |
|---|
| 308 | $i++; |
|---|
| 309 | if ($i >= $limit) { |
|---|
| 310 | break; |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | $res .= '</ul>'; |
|---|
| 315 | |
|---|
| 316 | return $w->renderDiv($w->content_only, 'feed ' . $w->class, '', $res); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | public static function text($w) |
|---|
| 320 | { |
|---|
| 321 | global $core; |
|---|
| 322 | |
|---|
| 323 | if ($w->offline) { |
|---|
| 324 | return; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
|---|
| 328 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
|---|
| 329 | return; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | $res = ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . $w->text; |
|---|
| 333 | |
|---|
| 334 | return $w->renderDiv($w->content_only, 'text ' . $w->class, '', $res); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | public static function lastposts($w) |
|---|
| 338 | { |
|---|
| 339 | global $core, $_ctx; |
|---|
| 340 | |
|---|
| 341 | if ($w->offline) { |
|---|
| 342 | return; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
|---|
| 346 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
|---|
| 347 | return; |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | $params['limit'] = abs((integer) $w->limit); |
|---|
| 351 | $params['order'] = 'post_dt desc'; |
|---|
| 352 | $params['no_content'] = true; |
|---|
| 353 | |
|---|
| 354 | if ($w->category) { |
|---|
| 355 | if ($w->category == 'null') { |
|---|
| 356 | $params['sql'] = ' AND P.cat_id IS NULL '; |
|---|
| 357 | } elseif (is_numeric($w->category)) { |
|---|
| 358 | $params['cat_id'] = (integer) $w->category; |
|---|
| 359 | } else { |
|---|
| 360 | $params['cat_url'] = $w->category; |
|---|
| 361 | } |
|---|
| 362 | } |
|---|
| 363 | |
|---|
| 364 | if ($w->tag) { |
|---|
| 365 | $params['meta_id'] = $w->tag; |
|---|
| 366 | $rs = $core->meta->getPostsByMeta($params); |
|---|
| 367 | } else { |
|---|
| 368 | $rs = $core->blog->getPosts($params); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | if ($rs->isEmpty()) { |
|---|
| 372 | return; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | $res = |
|---|
| 376 | ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . |
|---|
| 377 | '<ul>'; |
|---|
| 378 | |
|---|
| 379 | while ($rs->fetch()) { |
|---|
| 380 | $class = ''; |
|---|
| 381 | if ($core->url->type == 'post' && $_ctx->posts instanceof record && $_ctx->posts->post_id == $rs->post_id) { |
|---|
| 382 | $class = ' class="post-current"'; |
|---|
| 383 | } |
|---|
| 384 | $res .= '<li' . $class . '><a href="' . $rs->getURL() . '">' . |
|---|
| 385 | html::escapeHTML($rs->post_title) . '</a></li>'; |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | $res .= '</ul>'; |
|---|
| 389 | |
|---|
| 390 | return $w->renderDiv($w->content_only, 'lastposts ' . $w->class, '', $res); |
|---|
| 391 | } |
|---|
| 392 | |
|---|
| 393 | public static function lastcomments($w) |
|---|
| 394 | { |
|---|
| 395 | global $core; |
|---|
| 396 | |
|---|
| 397 | if ($w->offline) { |
|---|
| 398 | return; |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | if (($w->homeonly == 1 && $core->url->type != 'default') || |
|---|
| 402 | ($w->homeonly == 2 && $core->url->type == 'default')) { |
|---|
| 403 | return; |
|---|
| 404 | } |
|---|
| 405 | |
|---|
| 406 | $params['limit'] = abs((integer) $w->limit); |
|---|
| 407 | $params['order'] = 'comment_dt desc'; |
|---|
| 408 | $rs = $core->blog->getComments($params); |
|---|
| 409 | |
|---|
| 410 | if ($rs->isEmpty()) { |
|---|
| 411 | return; |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | $res = ($w->title ? $w->renderTitle(html::escapeHTML($w->title)) : '') . '<ul>'; |
|---|
| 415 | |
|---|
| 416 | while ($rs->fetch()) { |
|---|
| 417 | $res .= '<li class="' . |
|---|
| 418 | ((boolean) $rs->comment_trackback ? 'last-tb' : 'last-comment') . |
|---|
| 419 | '"><a href="' . $rs->getPostURL() . '#c' . $rs->comment_id . '">' . |
|---|
| 420 | html::escapeHTML($rs->post_title) . ' - ' . |
|---|
| 421 | html::escapeHTML($rs->comment_author) . |
|---|
| 422 | '</a></li>'; |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | $res .= '</ul>'; |
|---|
| 426 | |
|---|
| 427 | return $w->renderDiv($w->content_only, 'lastcomments ' . $w->class, '', $res); |
|---|
| 428 | } |
|---|
| 429 | } |
|---|