_binary From eb589b4ff4e23e337f6a7e3416c5eabe7bdacb65 Mon Sep 17 00:00:00 2001
From: Anirudh Mallya <anirudh.mallya09@gmail.com>
Date: Thu, 3 Jun 2021 02:01:26 -0700
Subject: [PATCH] Adding a pastebin like service to yioop

---
 src/controllers/GroupController.php           | 32 ++++++-
 .../components/SocialComponent.php            | 55 ++++++++++--
 src/css/search.css                            | 14 +++
 src/locale/en_US/configure.ini                |  4 +-
 src/models/GroupModel.php                     |  6 +-
 src/views/elements/WikiElement.php            | 88 +++++++++++++++++++
 6 files changed, 187 insertions(+), 12 deletions(-)

diff --git a/src/controllers/GroupController.php b/src/controllers/GroupController.php
index 2e9fbef2b..dc56f597a 100644
--- a/src/controllers/GroupController.php
+++ b/src/controllers/GroupController.php
@@ -63,6 +63,7 @@ class GroupController extends Controller implements CrawlConstants
     {
         $data = [];
         $signin_model = $this->model("signin");
+        $group_model = $this->model("group");
         if (!C\PROFILE) {
             return $this->configureRequest();
         }
@@ -77,6 +78,28 @@ class GroupController extends Controller implements CrawlConstants
         $data['SCRIPT'] = "";
         $token_okay = $this->checkCSRFToken(C\CSRF_TOKEN, $user_id);
         $data[C\CSRF_TOKEN] = $this->generateCSRFToken($user_id);
+        if (!empty($_REQUEST['group_id'])) {
+            $page_inf = $group_model->getPageInfoByName($_REQUEST['group_id'],
+                $_REQUEST['page_name'],"en-US","edit");
+        }
+        if ($page_inf
+            && strpos($page_inf['PAGE'],"page_type=share") !== false
+            && !array_key_exists('page_type', $_REQUEST)) {
+            $_REQUEST['page_type'] = "share";
+        }
+        if (!empty($_REQUEST['share_page']) && $page_inf
+            && strpos($page_inf['PAGE'],"page_type=share") !== false
+            && strpos($page_inf['PAGE'], $_REQUEST['page_type']) !== false
+            && !array_key_exists('page', $_REQUEST)) {
+            $token_okay = false;
+        } elseif (!empty($_REQUEST['share_page']) && $page_inf
+            && !empty($_REQUEST['page'])
+            && strpos($page_inf['PAGE'],"page_type=share") !== false
+            && (strpos($page_inf['PAGE'], "page_type=" . $_REQUEST['page_type'])
+                !== false)
+            && $_REQUEST['share_page'] != $_REQUEST['page']) {
+            $token_okay = false;
+        }
         if (!$token_okay) {
             $keep_fields = ["a", "arg", "f", "callback", "group_id",
                 "just_group_id", "just_user_id", "just_thread", "limit", "n",
@@ -87,7 +110,8 @@ class GroupController extends Controller implements CrawlConstants
                 if (isset($request[$field])) {
                     if ($field == "arg" && (!in_array($request[$field],
                         ["diff", "history", "read", "pages", "media",
-                        "source", "statistics"]) )) {
+                        "source", "statistics"])) &&
+                        empty($request['share_page'])) {
                         continue;
                     }
                     $_REQUEST[$field] =
@@ -95,6 +119,10 @@ class GroupController extends Controller implements CrawlConstants
                 }
             }
             $_REQUEST["c"] = "group";
+            if (!empty($request["share_page"])) {
+                $_REQUEST["a"] = $request['a'];
+                $_REQUEST["share_page"] = $request['share_page'];
+            }
         }
         $data = array_merge($data, $this->processSession());
         $data['MENU'] = 'groupmenu';
@@ -236,4 +264,4 @@ class GroupController extends Controller implements CrawlConstants
                 \seekquarry\yioop\library\webExit();
         }
     }
-}
+}
\ No newline at end of file
diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index a311070ae..19a594f99 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -2523,7 +2523,18 @@ class SocialComponent extends Component implements CrawlConstants
                         }
                     }
                 }
-                $$field = $tmp;
+                $page_inf = $group_model->getPageInfoByName(
+                    $_REQUEST['group_id'],$_REQUEST['page_name'],
+                    "en-US","edit");
+                if ($field == 'share_page' && ((!empty($_REQUEST['page_type'])
+                    && $_REQUEST['page_type']=="share")
+                    || (empty($_REQUEST['page_type']) && $page_inf
+                        && strpos($page_inf['PAGE'],"page_type=share")
+                        !== false))) {
+                    $page = $tmp;
+                } else {
+                    $$field = $tmp;
+                }
                 if ($user_id == C\PUBLIC_USER_ID) {
                     $_SESSION['LAST_ACTIVITY'][$field] = $tmp;
                 }
@@ -3459,7 +3470,8 @@ EOD;
                 || isset($_REQUEST['sort'])) {
                 $action = "wikiupdate_".
                     "group=" . $group_id . "&page=" . $page_name;
-                if (!$parent->checkCSRFTime(C\CSRF_TOKEN, $action)) {
+                if (!$parent->checkCSRFTime(C\CSRF_TOKEN, $action) &&
+                    $head_object["page_type"] != "share") {
                     $data['SCRIPT'] .= "doMessage('<h1 class=\"red\" >".
                         tl('social_component_wiki_edited_elsewhere').
                         "</h1>');";
@@ -4123,7 +4135,11 @@ EOD;
             $data['SUB_PATH'] = htmlentities($sub_path);
         }
         $data['ORIGINAL_SUB_PATH'] = $sub_path;
-        $data["CAN_EDIT"] = false;
+        if (!empty($_REQUEST['share_page']) && $_REQUEST['arg'] == "edit") {
+            $data["CAN_EDIT"] = true;
+        } else {
+            $data["CAN_EDIT"] = false;
+        }
         if ((isset($_REQUEST['c'])) && $_REQUEST['c'] == "api") {
             //wiki help request
             $data['MODE'] = 'api';
@@ -4141,7 +4157,8 @@ EOD;
             "page_alias" => tl('social_component_page_alias'),
             "media_list" => tl('social_component_media_list'),
             "presentation" => tl('social_component_presentation'),
-            "url_shortener" => tl('social_component_url_shortener')
+            "url_shortener" => tl('social_component_url_shortener'),
+            "share" => tl('social_component_share')
         ];
         $data['page_borders'] = [
             "solid-border" => tl('social_component_solid'),
@@ -4185,6 +4202,7 @@ EOD;
             'resource_filter' => 'file_name',
             "revert" => 'int',
             "group_name" => 'string',
+            "share_page" => 'string'
         ];
         $strings_array = [
             "page_name" => C\TITLE_LEN,
@@ -4274,19 +4292,25 @@ EOD;
             is_settings = {$data['settings']};
             is_page_alias = ('page_alias'=='{$data['current_page_type']}');
             is_url_shortener = ('url_shortener'=='{$data['current_page_type']}');
+            is_share = ('share'=='{$data['current_page_type']}');
             setDisplay('page-settings', is_settings || is_page_alias);
-            setDisplay("media-list-page", is_media_list && !is_page_alias && !is_url_shortener);
-            setDisplay("page-container", !is_media_list && !is_page_alias && !is_url_shortener);
-            setDisplay("non-alias-type", !is_page_alias && !is_url_shortener);
+            setDisplay("media-list-page", is_media_list && !is_page_alias &&
+                !is_url_shortener && !is_share);
+            setDisplay("page-container", !is_media_list && !is_page_alias &&
+                !is_url_shortener && !is_share);
+            setDisplay("non-alias-type", !is_page_alias && !is_url_shortener &&
+                !is_share);
             setDisplay("alias-type", is_page_alias);
             setDisplay("url-shortener", is_url_shortener);
             setDisplay("short-url-label", is_url_shortener);
+            setDisplay("share-bin", is_share);
             if (mode == 'edit') {
                 setDisplay('save-container', !is_media_list || is_settings);
                 setDisplay('resource-upload-form', is_media_list);
             }
             $init_toggle_settings
-            setDisplay("page-resources", !is_page_alias);
+            setDisplay("page-resources", !is_page_alias && !is_url_shortener &&
+                !is_share);
             ptype.onchange = function() {
                 var cur_type = ptype.options[ptype.selectedIndex].value;
                 if (cur_type == "media_list") {
@@ -4297,6 +4321,7 @@ EOD;
                     setDisplay("alias-type", false);
                     setDisplay("url-shortener", false);
                     setDisplay("short-url-label", false);
+                    setDisplay("share-bin", false);
                     setDisplay("page-resources", true);
                     if (mode == 'edit') {
                         setDisplay("resource-upload-form", true);
@@ -4309,6 +4334,7 @@ EOD;
                     setDisplay("alias-type", true);
                     setDisplay("url-shortener", false);
                     setDisplay("short-url-label", false);
+                    setDisplay("share-bin", false);
                     setDisplay("page-resources", false);
                 } else if (cur_type == "url_shortener") {
                      $toggle_settings_false
@@ -4318,8 +4344,20 @@ EOD;
                      setDisplay("alias-type", false);
                      setDisplay("url-shortener", true);
                      setDisplay("short-url-label", true);
+                     setDisplay("share-bin", false);
                      setDisplay("page-resources", false);
                      setDisplay("resource_msg", false);
+                }  else if (cur_type == "share") {
+                    $toggle_settings_false
+                    setDisplay("media-list-page", false);
+                    setDisplay("page-container", false);
+                    setDisplay("non-alias-type", false);
+                    setDisplay("alias-type", false);
+                    setDisplay("url-shortener", false);
+                    setDisplay("short-url-label", false);
+                    setDisplay("share-bin", true);
+                    setDisplay("page-resources", false);
+                    setDisplay("resource_msg", false);
                 } else {
                     setDisplay("page-container", true);
                     setDisplay("media-list-page", false);
@@ -4328,6 +4366,7 @@ EOD;
                     setDisplay("alias-type", false);
                     setDisplay("url-shortener", false);
                     setDisplay("short-url-label", false);
+                    setDisplay("share-bin", false);
                     setDisplay("page-resources", true);
                     if (mode == 'edit') {
                         setDisplay("resource-upload-form", false);
diff --git a/src/css/search.css b/src/css/search.css
index e1da4665f..deb805ac3 100755
--- a/src/css/search.css
+++ b/src/css/search.css
@@ -461,6 +461,20 @@ a.hover-link:hover
     height: 4in;
     width: 100%;
 }
+.tall-text-area.share-bin-text-area
+{
+    height: 4in;
+    width: 95%;
+}
+.rownr
+{
+    color: rgb(105,105,105);
+    text-align: right;
+    vertical-align: top;
+    border:0px;
+    outline:none;
+    resize: none;
+}
 .small-text-area
 {
     height: 2.5in;
diff --git a/src/locale/en_US/configure.ini b/src/locale/en_US/configure.ini
index b3956bf6f..6dcbc84e8 100644
--- a/src/locale/en_US/configure.ini
+++ b/src/locale/en_US/configure.ini
@@ -493,6 +493,7 @@ social_component_page_alias = "Page Alias"
 social_component_media_list = "Media List"
 social_component_presentation = "Presentation"
 social_component_url_shortener = "Url Shortener"
+social_component_share = "Share-bin"
 social_component_solid = "Solid"
 social_component_dashed = "Dashed"
 social_component_none = "None"
@@ -1601,6 +1602,8 @@ trending_element_yearly_trend = "Monthly %s for %s for the Last Year"
 ;
 ; WikiElement.php
 wiki_element_places = "Places:"
+wiki_element_share_label = "Paste Raw Data:"
+wiki_element_savebutton = "Save"
 wiki_element_redirect_to = "Redirects to:"
 wiki_element_page_no_exist = "%s page does not exist."
 wiki_element_create_edit = "You can create this page by selecting edit from the side menu."
@@ -1629,7 +1632,6 @@ wiki_element_page_footer = "Footer Page Name:"
 wiki_element_archive_info = "To archive a page so it won&#039;t appear in search results delete its text and save."
 wiki_element_edit_reason = "Edit Reason:"
 wiki_element_closebutton = "Close"
-wiki_element_savebutton = "Save"
 wiki_element_media_list = "Media List Page"
 wiki_element_ml_description = "The uploaded items below will all appear when this page is read as a gallery or media list."
 wiki_element_page_resources = "Page Resources"
diff --git a/src/models/GroupModel.php b/src/models/GroupModel.php
index 73aa0de82..d1720a3b4 100644
--- a/src/models/GroupModel.php
+++ b/src/models/GroupModel.php
@@ -1297,7 +1297,11 @@ class GroupModel extends Model implements MediaConstants
         $pubdate = time();
         $parser = new WikiParser($base_address, $additional_substitutions);
         $links_relationships = $parser->fetchLinks($page);
-        $parsed_page = $parser->parse($page);
+        if (strpos($page, "page_type=share") !== false) {
+            $parsed_page = $page;
+        } else {
+            $parsed_page = $parser->parse($page);
+        }
         if ($page_id = $this->getPageId($group_id, $page_name, $locale_tag)) {
             //can only add and use resources for a page that exists
             $parsed_page = $this->insertResourcesParsePage($group_id, $page_id,
diff --git a/src/views/elements/WikiElement.php b/src/views/elements/WikiElement.php
index fa1f0ab05..3a4b383d4 100644
--- a/src/views/elements/WikiElement.php
+++ b/src/views/elements/WikiElement.php
@@ -196,6 +196,84 @@ class WikiElement extends Element implements CrawlConstants
         if (isset($data["HEAD"]['page_type']) && $data["HEAD"]['page_type'] ==
             'media_list') {
             $this->renderResources($data, true, $logged_in);
+        } elseif (!empty($data["PAGE"]) && isset($data["HEAD"]['page_type']) &&
+            $data["HEAD"]['page_type'] == 'share') {
+            ?><div id="share-bin-readonly" style="display:flex;">
+            <textarea id="share_bin_helper" class="rownr" rows="20" cols="2" value="1"
+                style="display:inline-block;overflow:hidden;resize: none;"
+                readonly>
+            </textarea>
+            <textarea id="share_textarea"
+                class="tall-text-area share-bin-text-area"
+                name="share_readonly"
+                style="overflow-y:hidden;;display:inline-block;resize:none;"
+                onscroll="javascript:scroll_changed(this);" readonly>
+                <?= $data["PAGE"] ?></textarea>
+            </div>
+            <form id="editpageForm" method="post"
+            enctype="multipart/form-data">
+            <input type="hidden" name="c" value="<?=$data['CONTROLLER']
+            ?>" />
+            <input type="hidden" name="a" value="wiki" />
+            <input type="hidden" name="arg" value="edit" />
+            <div id="share-bin">
+            <div class="top-margin">
+            <label><b><?=tl("wiki_element_share_label")
+            ?></b></label>
+            </div>
+            <textarea id="share_page"
+                class="tall-text-area" name="share_page" style="resize:none;">
+                <?= $data["PAGE"] ?></textarea>
+            </div>
+            <div id="save-container" class="top-margin center">
+                <button class="button-box" type="submit">
+                <?=tl("wiki_element_savebutton")?></button>
+            </div>
+            </form>
+            <script>
+            function input_changed()
+            {
+                obj_rownr = document.getElementById('share_bin_helper');
+                obj_txt = document.getElementById('share_textarea');
+                cntline = Math.floor(parseInt(obj_txt.style.height)/15);
+                console.log(parseInt(obj_txt.style.height));
+                console.log(cntline);
+                if (cntline == 0) cntline = 1;
+                tmp_arr = obj_rownr.value.split('\n');
+                cntline_old = parseInt(tmp_arr[tmp_arr.length - 1], 10);
+                if (cntline != cntline_old) {
+                    obj_rownr.cols = cntline.toString().length;
+                    populate_rownr(obj_rownr, cntline);
+                    scroll_changed(obj_txt);
+                }
+            }
+            function populate_rownr(obj, cntline)
+            {
+                tmpstr = '';
+                for (i = 1; i <= cntline; i++)
+                {
+                    tmpstr = tmpstr + i.toString() + '.\n';
+                }
+                obj.value = tmpstr;
+            }
+            function textAreaAdjust(elementStr)
+            {
+                element = document.getElementById(elementStr);
+                element.style.height = "4in";
+                element.style.height = (25+element.scrollHeight)+"px";
+            }
+            window.textAreaAdjust("share_textarea");
+            input_changed();
+            function scroll_changed(obj_txt)
+            {
+                obj_rownr = document.getElementById('share_textarea');
+                scrollsync(obj_txt,obj_rownr);
+            }
+
+            function scrollsync(obj1, obj2)
+            {
+                obj2.scrollTop = obj1.scrollTop;
+            }</script><?php
         } elseif (!empty($data["PAGE"])) {
             $sub_path = (empty($data['SUB_PATH'])) ? "": $data['SUB_PATH'];
             $media_id = "";
@@ -500,6 +578,16 @@ class WikiElement extends Element implements CrawlConstants
             <?=$shortenedURL?></b></label>
             </div>
             </div>
+            <div id='share-bin'>
+            <div class="top-margin">
+            <label><b><?=tl('wiki_element_share_label')
+            ?></b></label>
+            </div>
+            <textarea id="share_page"
+                class="tall-text-area" name="share_page"
+                data-buttons="none">
+                <?=$data['PAGE']?></textarea>
+            </div>
             <div id='non-alias-type'>
             <div class="top-margin">
             <label for="page-border"><b><?=tl('wiki_element_page_border')
-- 
2.29.2.windows.3

