mambax7/gwiki

View on GitHub
ajaxfilelist.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php
/**
 * ajaxfilelist.php - supply list of file attachments for a page
 *
 * @copyright  Copyright © 2013 geekwright, LLC. All rights reserved.
 * @license    gwiki/docs/license.txt  GNU General Public License (GPL)
 * @since      1.0
 * @author     Richard Griffith <richard@geekwright.com>
 * @package    gwiki
 */

use XoopsModules\Gwiki;
use XoopsModules\Gwiki\Helper;

require_once \dirname(__DIR__, 2) . '/mainfile.php';
$xoopsLogger->activated = false;

header('Pragma: public');
header('Cache-Control: no-cache');

/**
 * @param $string
 *
 * @return string
 */
function cleaner($string)
{
    $string = stripcslashes($string);
    $string = html_entity_decode($string);
    $string = strip_tags($string); // DANGER -- kills wiki text
    $string = trim($string);
    $string = stripslashes($string);

    return $string;
}

/**
 * @param $uid
 *
 * @return string
 */
function getUserName($uid)
{
    global $xoopsConfig;

    $uid = (int)$uid;

    if ($uid > 0) {
        /** @var \XoopsMemberHandler $memberHandler */
        $memberHandler = xoops_getHandler('member');
        $user          = $memberHandler->getUser($uid);
        if (is_object($user)) {
            return '<a href="' . XOOPS_URL . "/userinfo.php?uid=$uid\">" . htmlspecialchars($user->getVar('uname'), ENT_QUOTES) . '</a>';
        }
    }

    return $xoopsConfig['anonymous'];
}

$helper = Helper::getInstance();

// $_GET variables we use
unset($page, $bid, $id);
$page = isset($_GET['page']) ? cleaner($_GET['page']) : '';

$dir = basename(__DIR__);

$sql    = 'SELECT * FROM ' . $xoopsDB->prefix('gwiki_page_files') . ' WHERE keyword = \'' . $page . '\' ' . ' ORDER BY file_name ';
$result = $xoopsDB->query($sql);

$filess = [];

for ($i = 0, $iMax = $xoopsDB->getRowsNum($result); $i < $iMax; ++$i) {
    $row             = $xoopsDB->fetchArray($result);
    $row['iconlink'] = XOOPS_URL . '/modules/' . $dir . '/assets/icons/48px/' . $row['file_icon'] . '.png';
    $row['userlink'] = getUserName($row['file_uid']);
    $row['size']     = number_format($row['file_size']);
    $row['date']     = date($helper->getConfig('date_format'), $row['file_upload_date']);

    $files[] = $row;
}

$jsonimages = json_encode($files);
echo $jsonimages;
exit;