<commit_before><?php
/**
 * Minds Blog API
 *
 * @version 1
 * @author <NAME>
 */

namespace Minds\Controllers\api\v1;

use Minds\Api\Exportable;
use Minds\Api\Factory;
use Minds\Common\Access;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Core\Router\Exceptions\UnverifiedEmailException;
use Minds\Helpers;
use Minds\Interfaces;
use Minds\Core\Blogs\Delegates\CreateActivity;
use Minds\Entities\User;

class blog implements Interfaces\Api
{
    /**
     * Returns the conversations or conversation
     * @param array $pages
     *
     * API:: /v1/blog/:filter
     */
    public function get($pages)
    {
        $response = [];

        if (!isset($pages[0])) {
            $pages[0] = "top";
        }

        $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 12;
        $offset = isset($_GET['offset']) ? (string) $_GET['offset'] : '';

        $repository = new Core\Blogs\Repository();
        $trending = new Core\Blogs\Trending();
        $manager = new Core\Blogs\Manager();
        $headerManager = new Core\Blogs\Header();

        switch ($pages[0]) {
            case "all":
                if (!Core\Session::isAdmin()) {
                    $response['entities'] = new Exportable([]);
                    $response['load-next'] = '';
                    break;
                }

                $blogs = $repository->getList([
                    'limit' => $limit,
                    'offset' => $offset,
                    'all' => true,
                ]);

                $response['entities'] = new Exportable($blogs);
                $response['load-next'] = $blogs->getPagingToken();
                break;

            case "trending":
            case "top":
                $blogs = $trending->getList([
                    'limit' => $limit,
                    'offset' => $offset,
                    'rating' => isset($_GET['rating']) ? (int) $_GET['rating'] : 1,
                ]);

                $response['entities'] = new Exportable($blogs);
                $response['load-next'] = $blogs->getPagingToken();
                break;

            case "network":
            case "owner":
                $opts = [
                    'limit' => $limit,
                    'offset' => $offset,
                ];

                $guid = isset($pages[1]) ? $pages[1] : Core\Session::getLoggedInUserGuid();

                if (isset($pages[1]) && !is_numeric($pages[1])) {
                    $lookup = new Core\Data\lookup();
                    $guid = key($lookup->get(strtolower($pages[1])));
                }

                if ($pages[0] === 'network') {
                    $opts['network'] = $guid;
                } elseif ($pages[0] === 'owner') {
                    $opts['container'] = $guid;
                }

                $blogs = $repository->getList($opts);

                $export = [];
                foreach ($blogs as $blog) {
                    if ($blog->getOwnerGuid() != Core\Session::getLoggedInUserGuid() && $blog->getAccessId() != Access::PUBLIC) {
                        continue;
                    }
                    $export[] = $blog;
                }
                //$export = array_slice($export, 0, $limit);

                $response['entities'] = new Exportable($export);
                $response['load-next'] = $blogs->getPagingToken();
                break;

            case "next":
                if (!isset($pages[1])) {
                    return Factory::response([
                        'status' => 'error',
                        'message' => 'Not blog reference guid provided'
                    ]);
                }

                $blog = $manager->get($pages[1]);
                $response['blog'] = $manager->getNext($blog);
                break;

            case "header":
                $blog = $manager->get($pages[1]);
                $header = $headerManager->read($blog);

                header('Content-Type: image/jpeg');
                header('Expires: ' . date('r', time() + 864000));
                header("Pragma: public");
                header("Cache-Control: public");

                try {
                    echo $header->read();
                } catch (\Exception $e) {
                }

                exit;

            default:
                if (is_numeric($pages[0]) || Core\Luid::isValid($pages[0])) {
                    $blog = $manager->get($pages[0]);

                    if (
                        !$blog ||
                        Helpers\Flags::shouldFail($blog) ||
                        !Core\Security\ACL::_()->read($blog)
                        || ($blog->getTimeCreated() > time() && !$blog->canEdit())
                    ) {
                        break;
                    }

                    $response['blog'] = $blog;
                }
                break;
        }

        return Factory::response($response);
    }<commit_msg>(feat): export require login flag for blogs<commit_after><?php
/**
 * Minds Blog API
 *
 * @version 1
 * @author <NAME>
 */

namespace Minds\Controllers\api\v1;

use Minds\Api\Exportable;
use Minds\Api\Factory;
use Minds\Common\Access;
use Minds\Core;
use Minds\Core\Di\Di;
use Minds\Core\Router\Exceptions\UnverifiedEmailException;
use Minds\Helpers;
use Minds\Interfaces;
use Minds\Core\Blogs\Delegates\CreateActivity;
use Minds\Entities\User;

class blog implements Interfaces\Api
{
    /**
     * Returns the conversations or conversation
     * @param array $pages
     *
     * API:: /v1/blog/:filter
     */
    public function get($pages)
    {
        $response = [];

        if (!isset($pages[0])) {
            $pages[0] = "top";
        }

        $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 12;
        $offset = isset($_GET['offset']) ? (string) $_GET['offset'] : '';

        $repository = new Core\Blogs\Repository();
        $trending = new Core\Blogs\Trending();
        $manager = new Core\Blogs\Manager();
        $headerManager = new Core\Blogs\Header();

        switch ($pages[0]) {
            case "all":
                if (!Core\Session::isAdmin()) {
                    $response['entities'] = new Exportable([]);
                    $response['load-next'] = '';
                    break;
                }

                $blogs = $repository->getList([
                    'limit' => $limit,
                    'offset' => $offset,
                    'all' => true,
                ]);

                $response['entities'] = new Exportable($blogs);
                $response['load-next'] = $blogs->getPagingToken();
                break;

            case "trending":
            case "top":
                $blogs = $trending->getList([
                    'limit' => $limit,
                    'offset' => $offset,
                    'rating' => isset($_GET['rating']) ? (int) $_GET['rating'] : 1,
                ]);

                $response['entities'] = new Exportable($blogs);
                $response['load-next'] = $blogs->getPagingToken();
                break;

            case "network":
            case "owner":
                $opts = [
                    'limit' => $limit,
                    'offset' => $offset,
                ];

                $guid = isset($pages[1]) ? $pages[1] : Core\Session::getLoggedInUserGuid();

                if (isset($pages[1]) && !is_numeric($pages[1])) {
                    $lookup = new Core\Data\lookup();
                    $guid = key($lookup->get(strtolower($pages[1])));
                }

                if ($pages[0] === 'network') {
                    $opts['network'] = $guid;
                } elseif ($pages[0] === 'owner') {
                    $opts['container'] = $guid;
                }

                $blogs = $repository->getList($opts);

                $export = [];
                foreach ($blogs as $blog) {
                    if ($blog->getOwnerGuid() != Core\Session::getLoggedInUserGuid() && $blog->getAccessId() != Access::PUBLIC) {
                        continue;
                    }
                    $export[] = $blog;
                }
                //$export = array_slice($export, 0, $limit);

                $response['entities'] = new Exportable($export);
                $response['load-next'] = $blogs->getPagingToken();
                break;

            case "next":
                if (!isset($pages[1])) {
                    return Factory::response([
                        'status' => 'error',
                        'message' => 'Not blog reference guid provided'
                    ]);
                }

                $blog = $manager->get($pages[1]);
                $response['blog'] = $manager->getNext($blog);
                break;

            case "header":
                $blog = $manager->get($pages[1]);
                $header = $headerManager->read($blog);

                header('Content-Type: image/jpeg');
                header('Expires: ' . date('r', time() + 864000));
                header("Pragma: public");
                header("Cache-Control: public");

                try {
                    echo $header->read();
                } catch (\Exception $e) {
                }

                exit;

            default:
                if (is_numeric($pages[0]) || Core\Luid::isValid($pages[0])) {
                    $blog = $manager->get($pages[0]);

                    if (
                        !$blog ||
                        Helpers\Flags::shouldFail($blog) ||
                        !Core\Security\ACL::_()->read($blog)
                        || ($blog->getTimeCreated() > time() && !$blog->canEdit())
                    ) {
                        break;
                    }

                    $response['blog'] = $blog;

                    if (!Core\Session::isLoggedIn()) {
                        $owner = Di::_()->get('EntitiesBuilder')->single($blog->owner_guid);
                        $response['require_login'] = !$this->checkBalance($owner);
                    }
                }
                break;
        }

        return Factory::response($response);
    }
{'fraction_non_alphanumeric': 0.1566015393724097, 'fraction_numerical': 0.005723307677126505, 'mean_word_length': 1.2883269361029577, 'pattern_counts': {'":': 14, '<': 7, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1177591', 'n_tokens_mistral': 2816, 'n_tokens_neox': 2592, 'n_words': 484}