<commit_before><?php

use ArtLibs\Controller;
use ArtLibs\Files;
use ArtLibs\User;
use ArtLibs\Article;
use ArtLibs\Category;
use Symfony\Component\HttpFoundation\File\UploadedFile;


class Views extends Controller
{
    /**
     * @param $params
     * @param $app
     */
    public function viewRoot($params, $app)
    {
        $app->setTemplateData(
            array(
                'title' => 'Start',
                'body_content' => 'The start page loaded from controller/view.php.'
            )
        );

        $this->display($app, 'home.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewHome($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Home',
        ));

        if ($app->getRequest()->getMethod() == "POST") {
            $email = $app->getRequest()->request->get('email');
            $pass = $app->getRequest()->request->get('password');

            $user = new User($app, $email, $pass);

            if (!$user->isAuthenticated()) {
                $app->setTemplateData(array('content_message' => 'Login unsuccessful, try again.'));
                $this->display($app, 'frm_login.twig');
                return;
            } else {
                $app->setTemplateData(array('content_message' => 'Login successful.'));
            }
        }

        $user_info = $app->getSession()->get('user_info');

        if ($user_info['utype'] == 1) {
            $articles = Article::getArticles($app, 0);
            if ($articles) {
                $app->setTemplateData(array('subtitle' => 'Active Articles', 'articles' => $articles));
            }
        }

        $this->display($app, 'uhome.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewArticle($params, $app)
    {
        $app->setTemplateData(array('title' => 'Not found'));
        $article = false;

        if (isset($params['aid'])) {
            $aid = $params['aid'];
            $article = Article::getArticleById($aid, $app);
        } elseif (isset($params['aurl'])) {
            $aurl = $params['aurl'];
            $article = Article::getArticleByUrl($aurl, $app);
        }

        if ($article) {
            $app->setTemplateData(array(
                'title' => $article['title'],
                'subtitle' => $article['subtitle'],
                'body' => stripslashes($article['body']),
                'article' => $article
            ));
        }

        $this->display($app, 'list_article.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function showFile($params, $app)
    {
        $app->setTemplateData(array('title' => 'Not found'));
        $file = false;

        if (isset($params['fname'])) {
            $fname = $params['fname'];
            $file = Files::getFile($app, $fname, 0);
        }

        if (!$file) {
            $this->display($app, 'list_article.twig');
            return;
        }

        $file_dir = $app->getConfManager()->getUserVar()['files_dir'];
        $file_path = $file['path'];
        $mtype = $file['mtype'];

        if ($params['opt'] == "view") {
            $this->fileResponse($app, $file_dir . "/" . $file_path, false, $mtype);
        } else {
            $this->fileResponse($app, $file_dir . "/" . $file_path, true, $mtype);
        }
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewArticleList($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Articles List',
        ));

        $user_info = $app->getSession()->get('user_info');

        if ($user_info['utype'] == 1) {
            if ($app->getRequest()->getMethod() == "POST") {
                $article_data = array(
                    'uid' => $user_info['id'],
                    'title' => trim($app->getRequest()->request->get('title')),
                    'subtitle' => trim($app->getRequest()->request->get('subtitle')),
                    'url' => strtolower(trim($app->getRequest()->request->get('aurl'))),
                    'category_id' => trim($app->getRequest()->request->get('category')),
                    'body' => addslashes(trim($app->getRequest()->request->get('abody'))),
                    'state' => addslashes(trim($app->getRequest()->request->get('state'))),
                );

                $invalid_chars = array(" ", "\n", "/", "\\", "$", "#", "@", "^", "&", "*");
                $article_data['url'] = str_replace($invalid_chars, "_", $article_data['url']);

                if ($app->getRequest()->request->get('editval')) {
                    $aid = trim($app->getRequest()->request->get('editval'));
                    $app->setTemplateData(array('content_message' => (Article::updateArticle($article_data, $aid, $app)) ? "Article updated successfully" : "Article update failed"));
                } elseif (Article::addArticle($article_data, $app)) {
                    $app->setTemplateData(array('content_message' => "New article added successfully."));
                } else {
                    $app->setTemplateData(array('content_message' => "Article couldn't be saved."));
                }
            }

            $articles = Article::getArticles($app);
            if ($articles) {
                $app->setTemplateData(array('articles' => $articles));
            }
        } else {
            $app->setTemplateData(array('content_message' => 'Not found or accessible'));
        }

        $this->display($app, 'list_article.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function frmArticle($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Add new article',
        ));

        $user_info = $app->getSession()->get('user_info');
        if ($user_info['utype'] == 1) {
            if (isset($params['opt']) && isset($params['aid'])) {
                $action = $params['opt'];
                $aid = $params['aid'];
                $app->setTemplateData(array('article' => Article::getArticleById($aid, $app), 'action' => "edit"));
            }

            $categories = Category::getCategories($app, 0);

            if ($categories) {
                $app->setTemplateData(array('categories' => $categories));
            }
        }

        $this->display($app, 'frm_article.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewCategoryList($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Category List',
        ));

        $user_info = $app->getSession()->get('user_info');

        if ($user_info['utype'] == 1) {
            if (isset($params[2])) {
                $action = $params[1];
                $cat_id = $params[2];

                if ($action == "edit") {
                    $cat_pre = Category::getCategoryById($cat_id, $app);
                    $app->setTemplateData(array('action' => 'edit', 'cat_id' => $cat_id, 'cat_pre' => $cat_pre));
                } elseif ($action == "enable") {
                    $app->setTemplateData(
                        array(
                            'content_message' => (Category::setState(0, $cat_id, $app)) ? 'Category is ' . $params[1] . 'd.' : 'State change failed'
                        )
                    );
                } elseif ($action == "disable") {
                    $app->setTemplateData(
                        array(
                            'content_message' => (Category::setState(1, $cat_id, $app)) ? 'Category is ' . $params[1] . 'd.' : 'State change failed'
                        )
                    );
                }
            }

            if ($app->getRequest()->getMethod() == "POST") {
                $category = array(
                    'catname' => trim($app->getRequest()->request->get('catname')),
                );

                if ($app->getRequest()->request->get('editval')) {
                    $cid = $app->getRequest()->request->get('editval');
                    $app->setTemplateData(
                        array(
                            'content_message' => (Category::updateCategory($cid, $category, $app)) ? 'Category successfully updated' : 'Category save failed'
                        )
                    );
                } elseif (Category::addCategory($category, $app)) {
                    $app->setTemplateData(array('content_message' => 'New category successfully added'));
                } else {
                    $app->setTemplateData(array('content_message' => 'New category save failed'));
                }
            }

            $categories = Category::getCategories($app);
            if ($categories) {
                $app->setTemplateData(array('categories' => $categories));
            }
        } else {
            $app->setTemplateData(array('content_message' => 'Not found or accessible'));
        }

        $this->display($app, 'list_category.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewFilesList($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Files List',
        ));

        $file_dir = Files::setUploadDir($app);

        $user_info = $app->getSession()->get('user_info');

        if ($user_info['utype'] == 1) {
            if (isset($params['opt']) && isset($params['fid'])) {
                $action = $params['opt'];
                $file_id = $params['fid'];

                if ($action == "enable") {
                    $app->setTemplateData(
                        array(
                            'content_message' => (Files::setState(0, $file_id, $app)) ? 'File is ' . $action . 'd.' : 'State change failed'
                        )
                    );
                } elseif ($action == "disable") {
                    $app->setTemplateData(
                        array(
                            'content_message' => (Files::setState(1, $file_id, $app)) ? 'File is ' . $action . 'd.' : 'State change failed'
                        )
                    );
                }
            }

            if ($app->getRequest()->getMethod() == "POST") {
                $file_info = array(
                    'name' => trim($app->getRequest()->request->get('filename')),
                    'state' => 0,
                );
                $uploaded_file = $app->getRequest()->files->get('filecontent');

                $moved = false;

                if ($uploaded_file instanceof UploadedFile && $uploaded_file->getError() == 0) {
                    $file_info['name'] = ($file_info['name'] == "") ? Files::setProperName($uploaded_file->getClientOriginalName(), $app) : Files::setProperName($file_info['name'], $app);
                    $file_info['mtype'] = $uploaded_file->getMimeType();
                    $file_info['ftype'] = Files::getFileExt($uploaded_file->getClientOriginalName());
                    $file_info['path'] = $file_info['name'];

                    try {<commit_msg>SQL file updated, file table added, sql and code re-formatting<commit_after><?php

use ArtLibs\Controller;
use ArtLibs\Files;
use ArtLibs\User;
use ArtLibs\Article;
use ArtLibs\Category;
use Symfony\Component\HttpFoundation\File\UploadedFile;


class Views extends Controller
{
    /**
     * @param $params
     * @param $app
     */
    public function viewRoot($params, $app)
    {
        $app->setTemplateData(
            array(
                'title' => 'Start',
                'body_content' => 'The start page loaded from controller/view.php.'
            )
        );

        $this->display($app, 'home.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewHome($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Home',
        ));

        if ($app->getRequest()->getMethod() == "POST") {
            $email = $app->getRequest()->request->get('email');
            $pass = $app->getRequest()->request->get('password');

            $user = new User($app, $email, $pass);

            if (!$user->isAuthenticated()) {
                $app->setTemplateData(array('content_message' => 'Login unsuccessful, try again.'));
                $this->display($app, 'frm_login.twig');
                return;
            } else {
                $app->setTemplateData(array('content_message' => 'Login successful.'));
            }
        }

        $user_info = $app->getSession()->get('user_info');

        if ($user_info['utype'] == 1) {
            $articles = Article::getArticles($app, 0);
            if ($articles) {
                $app->setTemplateData(array('subtitle' => 'Active Articles', 'articles' => $articles));
            }
        }

        $this->display($app, 'uhome.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewArticle($params, $app)
    {
        $app->setTemplateData(array('title' => 'Not found'));
        $article = false;

        if (isset($params['aid'])) {
            $aid = $params['aid'];
            $article = Article::getArticleById($aid, $app);
        } elseif (isset($params['aurl'])) {
            $aurl = $params['aurl'];
            $article = Article::getArticleByUrl($aurl, $app);
        }

        if ($article) {
            $app->setTemplateData(array(
                'title' => $article['title'],
                'subtitle' => $article['subtitle'],
                'body' => stripslashes($article['body']),
                'article' => $article
            ));
        }

        $this->display($app, 'list_article.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function showFile($params, $app)
    {
        $app->setTemplateData(array('title' => 'Not found'));
        $file = false;

        if (isset($params['fname'])) {
            $fname = $params['fname'];
            $file = Files::getFile($app, $fname, 0);
        }

        if (!$file) {
            $this->display($app, 'list_article.twig');
            return;
        }

        $file_dir = $app->getConfManager()->getUserVar()['files_dir'];
        $file_path = $file['path'];
        $mtype = $file['mtype'];

        if ($params['opt'] == "view") {
            $this->fileResponse($app, $file_dir . "/" . $file_path, false, $mtype);
        } else {
            $this->fileResponse($app, $file_dir . "/" . $file_path, true, $mtype);
        }
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewArticleList($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Articles List',
        ));

        $user_info = $app->getSession()->get('user_info');

        if ($user_info['utype'] == 1) {
            if ($app->getRequest()->getMethod() == "POST") {
                $article_data = array(
                    'uid' => $user_info['id'],
                    'title' => trim($app->getRequest()->request->get('title')),
                    'subtitle' => trim($app->getRequest()->request->get('subtitle')),
                    'url' => strtolower(trim($app->getRequest()->request->get('aurl'))),
                    'category_id' => trim($app->getRequest()->request->get('category')),
                    'body' => addslashes(trim($app->getRequest()->request->get('abody'))),
                    'state' => addslashes(trim($app->getRequest()->request->get('state'))),
                );

                $invalid_chars = array(" ", "\n", "/", "\\", "$", "#", "@", "^", "&", "*");
                $article_data['url'] = str_replace($invalid_chars, "_", $article_data['url']);

                if ($app->getRequest()->request->get('editval')) {
                    $aid = trim($app->getRequest()->request->get('editval'));
                    $app->setTemplateData(array('content_message' => (Article::updateArticle($article_data, $aid, $app)) ? "Article updated successfully" : "Article update failed"));
                } elseif (Article::addArticle($article_data, $app)) {
                    $app->setTemplateData(array('content_message' => "New article added successfully."));
                } else {
                    $app->setTemplateData(array('content_message' => "Article couldn't be saved."));
                }
            }

            $articles = Article::getArticles($app);
            if ($articles) {
                $app->setTemplateData(array('articles' => $articles));
            }
        } else {
            $app->setTemplateData(array('content_message' => 'Not found or accessible'));
        }

        $this->display($app, 'list_article.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function frmArticle($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Add new article',
        ));

        $user_info = $app->getSession()->get('user_info');
        if ($user_info['utype'] == 1) {
            if (isset($params['opt']) && isset($params['aid'])) {
                $action = $params['opt'];
                $aid = $params['aid'];
                $app->setTemplateData(array('article' => Article::getArticleById($aid, $app), 'action' => "edit"));
            }

            $categories = Category::getCategories($app, 0);

            if ($categories) {
                $app->setTemplateData(array('categories' => $categories));
            }
        }

        $this->display($app, 'frm_article.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewCategoryList($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Category List',
        ));

        $user_info = $app->getSession()->get('user_info');

        if ($user_info['utype'] == 1) {
            if (isset($params[2])) {
                $action = $params[1];
                $cat_id = $params[2];

                if ($action == "edit") {
                    $cat_pre = Category::getCategoryById($cat_id, $app);
                    $app->setTemplateData(array('action' => 'edit', 'cat_id' => $cat_id, 'cat_pre' => $cat_pre));
                } elseif ($action == "enable") {
                    $app->setTemplateData(
                        array(
                            'content_message' => (Category::setState(0, $cat_id, $app)) ? 'Category is ' . $params[1] . 'd.' : 'State change failed'
                        )
                    );
                } elseif ($action == "disable") {
                    $app->setTemplateData(
                        array(
                            'content_message' => (Category::setState(1, $cat_id, $app)) ? 'Category is ' . $params[1] . 'd.' : 'State change failed'
                        )
                    );
                }
            }

            if ($app->getRequest()->getMethod() == "POST") {
                $category = array(
                    'catname' => trim($app->getRequest()->request->get('catname')),
                );

                if ($app->getRequest()->request->get('editval')) {
                    $cid = $app->getRequest()->request->get('editval');
                    $app->setTemplateData(
                        array(
                            'content_message' => (Category::updateCategory($cid, $category, $app)) ? 'Category successfully updated' : 'Category save failed'
                        )
                    );
                } elseif (Category::addCategory($category, $app)) {
                    $app->setTemplateData(array('content_message' => 'New category successfully added'));
                } else {
                    $app->setTemplateData(array('content_message' => 'New category save failed'));
                }
            }

            $categories = Category::getCategories($app);
            if ($categories) {
                $app->setTemplateData(array('categories' => $categories));
            }
        } else {
            $app->setTemplateData(array('content_message' => 'Not found or accessible'));
        }

        $this->display($app, 'list_category.twig');
    }

    /**
     * @param $params
     * @param $app
     */
    public function viewFilesList($params, $app)
    {
        $app->setTemplateData(array(
            'title' => 'Files List',
        ));

        $file_dir = Files::setUploadDir($app);

        $user_info = $app->getSession()->get('user_info');

        if ($user_info['utype'] == 1) {
            if (isset($params['opt']) && isset($params['fid'])) {
                $action = $params['opt'];
                $file_id = $params['fid'];

                if ($action == "enable") {
                    $app->setTemplateData(
                        array(
                            'content_message' => (Files::setState(0, $file_id, $app)) ? 'File is ' . $action . 'd.' : 'State change failed'
                        )
                    );
                } elseif ($action == "disable") {
                    $app->setTemplateData(
                        array(
                            'content_message' => (Files::setState(1, $file_id, $app)) ? 'File is ' . $action . 'd.' : 'State change failed'
                        )
                    );
                }
            }

            if ($app->getRequest()->getMethod() == "POST") {
                $file_info = array(
                    'name' => trim($app->getRequest()->request->get('filename')),
                    'state' => 0,
                );
                $uploaded_file = $app->getRequest()->files->get('filecontent');

                $moved = false;

                if ($uploaded_file instanceof UploadedFile && $uploaded_file->getError() == 0) {
                    try {
                        $file_info['name'] = ($file_info['name'] == "") ? Files::setProperName($uploaded_file->getClientOriginalName(), $app) : Files::setProperName($file_info['name'], $app);
                        $file_info['mtype'] = $uploaded_file->getMimeType();
                        $file_info['ftype'] = Files::getFileExt($uploaded_file->getClientOriginalName());
                        $file_info['path'] = $file_info['name'];
{'fraction_non_alphanumeric': 0.17727272727272728, 'fraction_numerical': 0.0017101710171017102, 'mean_word_length': 1.6811052123552124, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 315, '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': '775575', 'n_tokens_mistral': 6362, 'n_tokens_neox': 6046, 'n_words': 1027}