We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29059
    • 88 Posts
    hello all,
    a MODX Revolution 2.4.2-pl noob here needing help please.

    i'm not a PHP'er either, i'm a front-end sorta guy.

    so here's the situation...
    our organization wants to use the MUUT.com forum, which is easily embeddable via Javascript & JQuery. the catch is, we need to use our own user database & user registration/authentication/login rather than using the built-in MUUT's.

    the cool thing is, they (MUUT) allow for this via what they call "Federated ID" PHP code.
    the problem is, i can't figure out how to configure Muut's PHP code with ModX's requirements. it's probably easy for someone who knows PHP really well; unfortunately, i don't & i'm the only one to do this.

    note: the basic embed works great. according to Muut, tho, if i use the below PHP code, the forum will bypass using Muut's user db & will use our own instead.

    here is the Muut PHP code...
    <?php
    	$user = array(
    		'user' => array(
    			"id" => "johndoe",
    			"displayname" => "John Doe",
    			"email" => "[email protected]",
    			"avatar" => "//gravatar.com/avatar/e5fb96fe7ec4ac3d4fa675422f8d1fb9",
    			"is_admin" => true,
    			),
    		);
    	$message = base64_encode(json_encode($user));
    	$timestamp = time();
    	$signature = sha1('MY_SECRET_MUUT_KEY_GOES_HERE' . ' ' . $message . ' ' . $timestamp);
    ?>

    the Muut support suggest i try something like this (which dosn't work)...
    <?php
    	$muutuser = array(
    		'user' => array(
    			"id" => $modx->username,
    			"displayname" => $modx->fullname,
    			"email" => $modx->email,
    			"avatar" => $modx->photo,
    			"is_admin" => false,
    			),
    		);
    	$message = base64_encode(json_encode($muutuser));
    	$timestamp = time();
    	$signature = sha1('MY_SECRET_MUUT_KEY_GOES_HERE' . ' ' . $message . ' ' . $timestamp);
    ?>

    so i dug in to the ModX documentation & found this page @
    https://rtfm.modx.com/revolution/2.x/administering-your-site/security/users#Users-GrabbingtheUserviatheAPI
    ...based on what i saw there, i came up with this (but it doesn't work either)...
    <?php
    	$profile = $modx->user->getOne('Profile');
    	$muutUser = array(
    		'user' => array(
    			"id" => $modx->user->get('username'),
    			"displayname" => return $profile ? $profile->get('fullname') : '',
    			"email" => return $profile ? $profile->get('email') : '',
    			"avatar" => return $profile ? $profile->get('photo') : '',
    			"is_admin" => true,
    		),
    	);
    	$message = base64_encode(json_encode($muutUser));
    	$timestamp = time();
    	$signature = sha1('MY_SECRET_KEY' . ' ' . $message . ' ' . $timestamp);
    ?>

    ...that right there is the part i'm needing help with; to match up Muut's id, displayname, email, & avatar with ModX's username, fullname, email, & photo.

    MORE INFO:::
    just so you know, in case i've done something wrong elsewhere...

    1) i created a "TEMPLATE" called "muutPageTpl" with "Clear Cache" checked, and put this code in it (note: Muut docs gave me the meta code)...
    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    
    <title>[[++site_name]] - [[*pagetitle]]</title>
    <base href="[[++site_url]]" />
    
    [[$muutHeader]]
    
    </head>
    <body>
    
    [[muutUserBridge]]
    
    [[$muutForumEmbed]]
    
    </body>
    </html>


    2) i created a "CHUNK" called "muutHeader" with "Clear Cache" checked, and put this code in it (required parts by Muut)...
    	<link rel="stylesheet" href="//cdn.muut.com/1/moot.css" />
    	<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    	<script src="//cdn.muut.com/1/moot.min.js"></script>


    3) i created another "CHUNK" called "muutForumEmbed" with "Clear Cache" checked, and put this code in it (required parts by Muut)...
    <a id="embedded-muut-forum" href="https://muut.com/i/sanctuary">Sanctuary Community</a>
    	<script>
    		$("#embedded-muut-forum").muut({
    			login_url: 'http://sanctuaryinternational.com/community/index.php',
    			api: {
    				key: 'MY_SHORT_MUUT_KEY_GOES_HERE',
    				message: '<?php echo $message; ?>',
    				timestamp: '<?php echo $timestamp; ?>',
    				signature: '<?php echo $signature; ?>',
    			}
    		});
    	</script>


    4) i created a "SNIPPET" called "muutUserBridge" with "Cleared Cache" checked, and put that problematic PHP code above in it.

    5) and i created a "RESOURCE/PAGE" called "muuttest" with the "muutPageTpl" selected as its Template. the other options for that resource is...
    - Hide From Menus: UNchecked
    - Published: Checked
    - Parent Resource: none
    - Resource Type: Document
    - Content Type: HTML (there's no PHP option!)
    - Content Disposition: Inline
    - Container: UNchecked
    - Searchable: Checked
    - Rich Text: Checked
    - Freeze URL: UNchecked
    - Cacheable: Checked
    - Empty Cache: Checked
    - Deleted: UNchecked
    - created Resource Group: Checked

    6) i've done these options too...
    - Manage > Clear Cache
    - Manage > Clear Cache > Refresh URL's
    - Manage > Flush Your Permissions

    THANK YOU VERY MUCH FOR YOUR TIME & ATTENTION!!!
    .peace.:.jason.

    This question has been answered by syberknight. See the first response.

      • 46886
      • 1,154 Posts
      Hi, I have really wanted to reply to you but I don't know php and so can't do so much. I was hoping someone else would jump in but since no one so far, let's get the ball rolling and see what we can do. With forums we have a native system which is not widely adopted (that's what I use) and some people use Vanilla or older bbcode, so this is a bit new.

      We got some really, really good people who are knowledgeable af and ready to help, but time and understanding are issues. I think we can work this out. Keep in mind I am not a coder and so I will sometimes use language which isn't precise.

      Right now I think what we care about is grabbing the user via the API. If we have a script which is successfully grabbing the user db or whatever, then all is well and you then only need to worry about connecting the Muut terminology with the db terminology. Let's grab the data first of all, once we got it we can always put it through some steps if necessary.

      Now we have to be careful with Modx default user data, the terminology gets a bit wacky. We got (as I remember) First Name, Last Name, Full Name, and an ID number. Personally I would suggest to use the ID number for connecting at the most basic level. Get (user ID=X) > Get (value=Display Name), something like this. Also I would ask during development that you simplify your code to only worry about grabbing the main data.

      Keep in mind stuff like avatar and display name are values which will change and which you will presumably need to allow users to change. Avatars and register date are not automatic in Modx, you would need to add them, but most is handled by the extended profile. All these can be changed using the update profile snippet. I am concerned with your code that you are asking for a value which may not exist.

      Ok I have one assumption or question, is it true that you need the db to re-assign or add names to values, so that inside Muut the value of a term like [avatar] will connect with the db value...does this make sense? As I mentioned above, avatars are not standard, but you are trying to connect the Modx user db value of 'fullname' to the Muut value of 'displayname', is this correct?

      Let me try to break this down:

      Get Modx user db
      Convert db values to Muut terminology

      I feel that for some or many of the user values, you may find Modx doesn't make these by default. Not sure about your needs. You can make new fields as you wish and name them as well, which could make this process easier actually, if the name of the value is the same as Muut then it can't be that tough to connect them. You definitely can have a lot of freedom in this area, I have like 3 different snippets in my register form grabbing stuff or defining stuff (like registered on date).

      Pls let me know where you are in this process now. When you are ready, I would suggest a new question in these forums, people will understand and be able to answer if the title is how to grab the user table via API.
        • 46886
        • 1,154 Posts
        Oh wait looks like date created has been added to Revo! https://github.com/modxcms/revolution/commit/89c79f7ae886184dd4136cfe09663bf5c019ee7d

        That's a little easier then.
          • 29059
          • 88 Posts
          hello @nuan88...

          THANK YOU for your reply, i really appreciate it. yeah i was beginning to wonder why i hadn't gotten any replies yet. i mean, i know there probably aren't any active forum members who've integrated MUUT into ModX before, but i really thought my question/problem was solely with PHP code.

          regarding other forum systems, i've looked at what seems like all of them, & my team/boss fell in love with MUUT's, so we're already paid up for a year with them. since they have this "Federated ID" method, i didn't think i'd have any trouble getting it to work - obviously i was wrong.

          i totally agree with you that this is probably all about the "Grabbing the User via the API" code & getting that to mesh with the MUUT code. but my poor attempts at translating that into something that works isn't going so well (reference- https://rtfm.modx.com/revolution/2.x/administering-your-site/security/users#Users-GrabbingtheUserviatheAPIiatheAPI[/url]).

          after i got ModX installed, i started going thru all the "Login" docs & tutorials @ https://rtfm.modx.com/extras/revo/login
          ...so i have been able to get all that to work OUTSIDE of MUUT, just using some blank test pages (and i must say, those docs & tutorials are woefully lacking enough detail for a non-programmer to use).

          i've also scoured all the ModX "Extras" as well as Google'ing, StackOverflow'ing, etc but nothing's sparked any wisdom for me yet (speaking of the Extras, SO many of them are old, makes me nervous about trying them out).

          but anyways, about your question - i don't "think" MUUT is trying to re-assign anything (i could be wrong), but it seems to me that PHP code snippet is acting as more of a "bridge" idea - translating or connecting their terminology to ModX's (or viceversa).

          i'll attach an annotated screenshot to this post in hopes it'll explain it better. let me know if it doesn't & i'll attempt it with more words instead ;-)

          as far as where i'm at right now, besides having a little less hair & fingernails now, i'm still where i was with the original post above, except now i know my PHP attempt does not work.

          a little aside from that, i've been looking for a good Extra/Template/Tutorial to help me make a nice user register/login & profile/update page; rather than making my own from scratch - just to try & save some time. but i fear i haven't found anything that's current & makes sense yet. i did find this one tutorial where the guy instructs to use the blog/article stuff as a basis & configure it so that each user would basically be a blog post - but i don't wanna do that. so if you know of any templates/extras/tutorials for that, that could save me time, i'd appreciate the referral.

          THANKS AGAIN!!!
          i look forward to your (or anybody's) response.
          .peace.:.jason.
            • 46886
            • 1,154 Posts
            I have heard that Subscribe is good, pls take a look at it.

            Ok let me try to check this out, its true the documentation is lacking to be honest. And could we start with the basic code for grabbing user data, can we get that to work for you.

            The difference is user data sort of fundamental to the user, and then stuff which is only included in the profile...you may find you are not getting profile data in there, you gotta stick it in the register snippet or no value is made. The basic register snippet i believe doesn't have avatar upload, this must be added I believe. My suggestion was, in adding this, then call the data as avatar not photo, just for convenience. Well, it may not be a better way.

            Pls take a look inside the manager at the user data. That plus any special fields you make would be basically all you would have....I feel I am not getting something here. Photo is a defined value, so its...not going to be in the JSON file when you define a value for it, I think. My point is there is no standard avatar uploading except in the update profile snippet, what I did for this was have users register and then get sent directly to update profile to populate their info.
              • 29059
              • 88 Posts
              hi @nuan88,

              thanks again for your time & thoughts on this.

              i'm wondering if we are having some language misunderstandings.
              i believe i'm good with regards to user registration & login & profile editing in ModX... i think (i've been trying so many things i might have gotten confused, so tonight i will go back thru it & make sure). thanks for that "Subscribe" Extra - it looks pretty cool; i'll check it out tonight too.

              the MAIN issue, as i understand it, is this one PHP snippet to get the MUUT user objects to connect to the MODX user objects.

              stripped down, here is the MUUT php snippet i have to use...
              <?php
              $muutUser = array(
                 'user' => array(
                      "id" => "THE_MODX_USERNAME_NEEDS_TO_GO_HERE",
                      "displayname" => "THE_MODX_FULLNAME_NEEDS_TO_GO_HERE",
                      "email" => "THE_MODX_EMAIL_NEEDS_TO_GO_HERE",
                      "avatar" => "THE_MODX_PHOTO_NEEDS_TO_GO_HERE",
                 ),
               );

              ...as you can see in the above snippet, there are 4 User Fields i need to put in there, replacing all the uppercase text like "THE_MODX_USER-FIELD_NEEDS_TO_GO_HERE" with the correct MODX php code for those 4 User Fields. does that make sense?

              now, i read the MODX "User" documentation about using the "Grabbing the User via the API" for this sort of thing, but (to me, since i'm not a php programmer) it's not a straight copy/paste plug'n'play sorta thing.
              here is the instructions & code from the docs...
              The current user can be retrieved in the API via the $modx->user reference. 
              For example, this snippet outputs the username of the user:
                   return $modx->user->get('username');
              
              Note that to grab Profile fields, you'll need to first get the modUserProfile object via the Profile alias. 
              For example, this snippet grabs the email of the user and returns it:
                   $profile = $modx->user->getOne('Profile');
                   return $profile ? $profile->get('email') : '';
              
              If the User is not logged in, $modx->user will still be available as an object, but will return 0 as the ID and (Anonymous) as the username.

              ...SO, given that information, i deduce that i'd need this code...
              $modx->user->get('username')
                   $profile = $modx->user->getOne('Profile');
                        return $profile ? $profile->get('fullname') : ''
                        return $profile ? $profile->get('email') : ''
                        return $profile ? $profile->get('photo') : ''
              


              ...BUT i cannot figure out how to fit that code in to the above MUUT code where i have "THE_MODX_USER-FIELD_NEEDS_TO_GO_HERE" texts - does that all make better sense?

              and again, i am still grateful for any help you or anybody can give me towards this.
              .peace.:.jason.
                • 46886
                • 1,154 Posts
                I am sorry I haven't been able to help more. I feel the title of this thread is confusing and that's why more people haven't commented.

                Could you pls make a new thread with the title something like "Php for grabbing modx user via API" pls? There are people who can help here and I don't know why they haven't popped up yet.

                I really feel its the title of this thread, people will feel they don't have an answer. However if you ask about the php to get the user, several people probably will have some good info.

                I will still try to answer your questions in this thread, but the key i think to moving forward is for your php question to get answered.
                  • 46886
                  • 1,154 Posts
                  Ok so pls do that, it can get more attention and a faster answer. This is a php question about the user data.

                  Now let me try to help here.

                  I just checked on my site, and this code
                  <?php
                  $profile = $modx->user->getOne('Profile');
                       return $profile ? $profile->get('email') : '';


                  You can save it as a snippet and call in in a resource, then open the resource and you should get some output.

                  When placed in a snippet and called in a resource [[!snippet]] did output my email when I was logged it, otherwise it returns nothing. I found i can return only one value or I get an error, expect T_RETURN.

                  So, we can get some user data. But while i can get id and fullname to output, I cannot get username. Can you use id for Muut's id? Its unique and unchangable...

                  What you were doing was defining Muut's data with Modx's, as in, Muut(ID) will be Modx(username), but i think you will need to get the Modx profile first as the above.

                  You might be able to just <get> the Profile and then tell Muut to use Modx's data...but looking at your first post, I see you already got this far...let me try this:

                  <?php
                  $profile = $modx->user->getOne('Profile');
                  $muutuser = array(
                  'user' => array(
                  "id" => $profile ? $profile->get('id'),
                  "displayname" => $profile ? $profile->get('fullname'),
                  "email" => $profile ? $profile->get('email'),
                  "is_admin" => false,
                  ),
                  );

                  See if that works, i don't know if all the gets are right or not. I know its pretty close to what you did above, but username isn't working for me so i think it might not work period. [ed. note: nuan88 last edited this post 8 years, 2 months ago.]
                    • 46886
                    • 1,154 Posts
                    Ok I posted a question about the username, not sure if you saw it. http://forums.modx.com/thread/99368/username-in-modx-is-username

                    It turns out its saved elsewhere for some reason. with this copy paste solution to get it

                    <?php
                    // The current user can be retrieved in the API via the $modx->user reference. 
                    // For example, this snippet outputs the username of the user:
                        
                    $modx_username = $modx->user->get('username');
                    $profile = $modx->user->getOne('Profile');
                    $profile_email = $profile ? $profile->get('email') : 'no email found';
                    $output ='<p>Username: '. $modx_username .' - ( '. $profile_email .' )</p>';
                    return $output;


                    For photo, let me try again. There is no native photo uploader in Modx. Here is a page where someone shows their working solution as they develop it just how they like it. I can send you what my dev did on my site too, given a little time.

                    http://stackoverflow.com/questions/13860061/modx-user-profile-photo-upload-dont-remove-original-uploaded-photo-entry

                    If you name the db value as "photo", then it may well work the way email and fullname work and go into the same db table as the others. Username is a special case here, most main user will go into the same place.

                    If you name it something different, it will go into a JSON string, where all extended fields surrounding the user are placed. This may make your php a little different as you may need to pull up the extra info fields. You also have to figure out uploading the photo in register which might be tricky, I didn't do that for some reason, (I think the profile isn't created yet or something) else you need to use upload profile straight off, and send the new user to a new page to upload the photo and add extra info.
                    • discuss.answer
                      • 29059
                      • 88 Posts
                      hi @nuan88,

                      my apologies for the delay.

                      Dude! i REALLY appreciate you sticking in there & continuing to try & help me with this. now you can rest your eyes from this issue! i was up very very late last night with a friend, whom i owe big time now, and we solved it!
                      he's a super-smart backend programmer & knows php & stuff amazingly well. and even for him, it took quite a few hours (after bringing him up to speed on the basics of how ModX works), but he finally found the magic combination.

                      if curious, i'll paste the code in below - "all" of it ended up having to be in one snippet. no matter how we tried, the javascript bit in the html just wouldn't work in a chunk, template, or page - because it's calling those 3 PHP values, & i guess ModX requires "all" php to be in a snippet. so he figured a clever workaround (as far as i'm concerned). that's alright with me; now i'll just stick the snippet call in a template for that page, or maybe even just in a page itself since it'll just be on one page (the MUUT Forum uses Ajax niftiness to do it's thang).

                      here's the winning code...
                      <?php
                      $username = $modx->user->get('username');
                      $profile = $modx->user->getOne('Profile');
                      	$email = $profile->get('email');
                      	$fullname = $profile->get('fullname');
                      	$photo = $profile->get('photo');
                      
                      $muutuser = array(
                      	'user' => array(
                      		"id" => "$username",
                      		"displayname" => "$fullname",
                      		"email" => "$email",
                      		"avatar" => "//sanctuaryinternational.com/$photo",
                      		"is_admin" => false,
                      	),
                      );
                      
                      $message = base64_encode(json_encode($muutuser));
                      $timestamp = time();
                      $signature = sha1('MY_SECRET_MUUT_KEY_GOES_HERE' . ' ' . $message . ' ' . $timestamp);
                      
                      echo '
                      	<link rel="stylesheet" href="//cdn.muut.com/1/moot.css" />
                      	
                      	<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
                      	<script src="//cdn.muut.com/1/moot.min.js"></script>
                      
                      	<a id="embedded-muut-forum" href="//muut.com/i/sanctuary">Sanctuary Community</a>
                      	<script>
                      		$("#embedded-muut-forum").muut({
                      			login_url: "//sanctuaryinternational.com/the_login_page.html",
                      			api: {
                      				key: "MY_SHORT_MUUT_KEY_GOES_HERE",
                      				message: "'.$message.'",
                      				timestamp: "'.$timestamp.'",
                      				signature: "'.$signature.'"
                      			}
                      		});
                      	</script>
                      ';


                      now, about the other things that's come up in this thread...

                      thank you for the feedback on post titles; i will consider that for future posts. i am surprised tho - i really try to come up with descriptive post titles whenever i make a post somewhere; i thought this one would garner traffic from some PHP experts.

                      about the photo upload - i followed the ModX Docs Login tutorials & have a very very basic implementation working well. i've not yet experimented with the issue you described & linked to the stack post about, but i'll cross that bridge probably tomorrow.

                      so anyway, i'm just SO excited to have this little but crucial piece working! but again, i really really really appreciate you & your time'n'efforts!!!
                      .peace.:.jason.