Adding custom php pages to UNA

I would like to add custom php pages to my instance of UNA that retain the header to keep the same look and feel.  It seems like one way to do this is to add a custom page from Studio-->Pages-->Custom.  I have done this and added to a raw block an iframe like so:


        style="display: block;

        width: 100vw;

        height: 80vh;

        max-width: 100%;

        margin: 0;

        padding: 0;

        border: 0 none;

        box-sizing: border-box;">


This displays the page fine, but I would like to use the user profile cookies to authenticate the current user to the pages.  I have tried to read the $_COOKIE['memberID'] cookie as a test, but it does not seem to be accessible from the iframe.

Does anyone have other ideas on how to extend UNA in this way?  Or if there is a document/link I could read that would point me in the right direction, I don't mind trying to learn this way.

Thanks!

LeonidS Anton L Alex T⚜️ Alexey 

  • 1587
  • More
Replies (8)
    • I have since tried looking into service calls, but have not been able to crack the code.  I have tried this in the service block.  I have my module folder set up in modules/pc/classes/test/PcTestModule.php.  My class is below.  Unfortunately, this method does not return anything on the page.  Any other thoughts or am I approaching this wrong?
      image_transcoder.php?o=bx_froala_image&h=3779&dpx=2&t=1593730100


      <?php defined('BX_DOL') or die('hack attempt');

       * Copyright (c) UNA, Inc - https://unacms.com

       * MIT License - https://opensource.org/licenses/MIT

       *

       * @defgroup    

       * @ingroup     UnaModules

       *

       * @{

       */

      class PcTestModule extends BxDolModule

      {

          public function serviceTest() 

          {

              return "service test";

          }

      }


      • Hello mrochek !

        On the quick look I noticed that in your path modules/pc/classes/test/PcTestModule.php one directory is missing. It should have 3 levels: 1) modules (ok); 2) the name of vendor (possible "PC"); 3) the name of module (don't see). So first of all, you need to fix this part.

        • LeonidS Thank you for the response. I'm so sorry, I reversed the order in my comment. My structure looks like this: modules/pc/test/classes/PcTestModule.php.
          Do you have any other ideas?  Sorry for the confusion!

          • Does anyone have other ideas on how to extend UNA in this way?

            The correct way is learning to develop regular UNA apps as you mentioned above.

            Alternatively I am succesfully using this method:

            In general I took the output buffer and modify it before sending to browser. This way the original UNA code is not touched.

            For the modifications:
            first I create a raw block on the page that I want to process.
            (it will inherit all styling and needed js etc)
            Put bookmark on the block like :

             <!--changethiswithanythingXncs78r74293-->

            replace the bookmark with the result of your code and send page to browser.

            ps: dont use simplewords for the bookmarks.

            • Cem that is really interesting. It will be so helpful if you can create a short tutorial on how to do this in step-by-step approach for experienced users like me.

              • Hi Scholar;

                Before all I want to remind that this is not the normal way of doing things, but I am using it cause I have no time to properly learn UNA app development.

                So lets go:

                • Studio >pages create a page -here testpage- and insert a raw block in it
                  (you may put the raw block on any page, here only testing)
                • put your book mark inside the raw block here
                • take a copy of your original page php and edit your page php as follows:
                  <?php
                  require_once('./inc/header.inc.php');
                  require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
                  
                  $oTemplate = BxDolTemplate::getInstance();
                  $oPage = BxDolPage::getObjectInstanceByURI();
                  
                  $custompage=1; //with this switch you may close all your custom code and make page.php work as normal, just make it not 1 to go to normal
                  if ($custompage==1){
                  bx_import('BxDolLanguages');
                  $uri = $_SERVER['REQUEST_URI'];
                                  
                  if ($oPage) {
                      // $oPage->displayPage();
                        $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
                      $oTemplate->setPageType ($oPage->getType());
                     $oTemplate->setPageContent ('page_main_code', $oPage->getCode());}
                   else {
                   $oTemplate->displayPageNotFound();}
                              
                  ob_start();     
                        $oTemplate->getPageCode();
                      $pagetoedit = ob_get_clean();
                  // here edit the html code on $pagetoedit variable
                  // first we need to see if we are on the page that we want to insert our code, this is for not to lose time
                  // we assume that we created a page testpage > page.php?i=testpage
                  if stripos($uri, "testpage"){
                  $newhtml="<b>Hello world!</b>"
                  $editedpage=str_replace("<!--yourbookmarkXs48930-->",$newhtml,$pagetoedit);
                  }
                  //send the new page to browser
                  echo $editedpage;
                  
                  }else{ //go to normal process without custom code
                      if ($oPage) {
                        $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
                      $oTemplate->setPageType ($oPage->getType());
                     $oTemplate->setPageContent('page_main_code', $oPage->getCode());
                  $oTemplate->getPageCode();}
                   else {
                   $oTemplate->displayPageNotFound();} 
                  }
                  
                  /** @} */
                  
                  
                      
                  
                      
                  
                  

                you can process any code you like and return the results in your raw block.

                • that's extremely helpful, thank you very much, Cem 

                  • Hi, mrochek,

                    You said, "I have tried to read the $_COOKIE['memberID'] cookie as a test, but it does not seem to be accessible from the iframe." Is the document src for the iframe, rooted in your Una website? In other words, is the iframe src something like: 

                    src="https://{your.una.domain}/some/path/to/some-document.php"

                    or 

                    src="relative_path/to/some-document.php"

                    If YES, then try the sandbox > allow-same-orgin attribute in the iframe, like so:

                    sandbox="allow-same-origin"

                    You can string other sandbox attributes together with a space, to allow even more access from the iframe, e.g.:

                    sandbox="allow-same-origin allow-scripts"

                    Then from the iframe'd document, try window.parent.document.cookie to set or get non-HTTPOnly cookies. 

                    ASIDE: Are those cookies HTTPOnly? If so, this may be the problem. I am not that far along in my UNA build process to know. Is the domain you are accessing a subdomain? That could be a problem, too (different "origin"), depending how the subdomain is set up.

                    Otherwise, if NO or the cookies you wish the iframe'd document to access are HTTPOnly cookies, then that sandbox attribute will not help. You are not meant to be able to check cookies from the framed website in the parent document, nor parent cookes from the iframe'd website, if either of these situations apply. Possible solutions for the not-same-origin situation include the following.

                    I apologize, but I am not clear what you are pulling into your iframe—the header or footer files from your same site (and not a subdomain)? If so, I believe the above sandbox should work. Otherwise, see also other potential fixes, below.

                    If you control the site with the other document that does not have the same origin (i.e., another domain, including a subdomain of your una site), then:

                    1) you can put the following in the php headers of the pages or scripts you wish to access from your una site:

                    header("Access-Control-Allow-Origin: https://your.una.domain");

                    OR 

                    2) inside your .htaccess file of the other site, add this line:

                    Header set Access-Control-Allow-Origin https://your.una.domain

                    I don't think I would do both. Test one or the other.

                    More Troubleshooting. If you control the site with the other document, or if you are still having problems: you might test an XMLHttpRequest to the domain of the other page, whereby a CORS "cross-origin request," Origin: https://your.una.domain header will be sent to the server. The server will respond if authentication is needed, if the request is denied, or if you're all clear. It might send back an Access-Control-Allow-Origin:* which means your site (or any site) can access the called script or page, or it may specify your una site domain explicitly. (I'm not positive if this, alone, will help the other page read your una site domain cookies. Take a look at the return headers in your browser's developer/web inspector app; this will tell you how the contacted server considers your request. 

                    Continuing that line of troubleshooting, do the reverse (XMLHttpRequest) from the other page to your una site domain page, for example, and see what your site is sending in the way of headers to the framed page. It may be your una site is the one that needs to implement (1) or (2) above to allow the iframe'd site access to your domain.

                    HTH

                    Login or Join to comment.