Page 1 of 1

Hook - useragent info for commenters?

PostPosted: Wed Nov 25, 2009 5:15 am
by Stingray
Fascinating...can this function be used to insert a bit of code into any of the PivotX files, or is it just for placing something in a template (if I can ask this without hijacking the thread)?

For instance, I "hacked" the old Pivot to show some basic useragent info along with comments. I was able to do the same with PivotX. Kevin and hans have seen the results of my efforts. I had to hack it in because I wasn't sure how to do it as an extension or even if it could be done as an extension; in fact, I had to make some changes to specific PivotX files so that the useragent string would be written to the entry file or entry db entry then interpreted to display the appropriate information (specifically forms.php, module_comments.php, and module_smarty.php). If I could write an extension to hook that information in there, it would certainly work out better. Can this be done?

If there is a better way to do what I'm trying to describe, please let me know. If this deserves it's own topic, well, go ahead and split my response off (I believe phpbb3 can do that).

Re: Hook - useragent info for commenters?

PostPosted: Wed Nov 25, 2009 8:56 am
by hansfn
This was serious thread hijacking so I split the post off ;-)

There aren't hooks for everything (yet, at least). I don't think whether you did to forms.php can be done with hooks yet, and I have added the user agent info to the comments so basically your change to module_comments.php is already part of PivotX core - get PivotX Latest. You should never edit module_smarty.php or any other core files (but you know this). Write an extension/snippet that defines the needed extra template tag or one that modifies the behavior of an existing template tag. (I can probably implement your changes to the comments tag - other people might want to display the user agent too. It's not that easy to modify this tag cleanly.)

NB! I wasn't able to unzip http://geoffrey.famwagner.com/pivotx-fi ... agentx.zip so I could see exactky what you have done.

PS! These are the hooks that are already defined in templates. It should give you a hint about what is possible (now):

Code: Select all
body-begin
body-end
content-after
content-before
content-begin
content-end
dashboard-before-comments
dashboard-before-entries
dashboard-before-events
dashboard-before-forumposts
dashboard-before-news
dashboard-before-pages
dashboard-before-warnings
dashboard-bottom
dashboard-top
entry-body-before
entry-bottom
entry-introduction-before
entry-keywords-before
error-after
error-before
footer-after
footer-before
footer-begin
footer-end
head-begin
head-end
html-end
logo-after
logo-before
mainmenu-after
mainmenu-before
media-after
media-before
media-line-file
media-line-folder
media-line-image
page-body-before
page-bottom
page-introduction-before
page-keywords-before
sitename-after
sitename-before
title-after
title-before
usermenu-after
usermenu-before

Re: Hook - useragent info for commenters?

PostPosted: Wed Nov 25, 2009 7:56 pm
by Stingray
hansfn wrote:This was serious thread hijacking so I split the post off ;-)


Yeah, I was afraid my exuberance would qualify as a hijacking...sorry about that.

hansfn wrote:There aren't hooks for everything (yet, at least).


Cool...

hansfn wrote: I don't think whether you did to forms.php can be done with hooks yet, and I have added the user agent info to the comments so basically your change to module_comments.php is already part of PivotX core - get PivotX Latest. You should never edit module_smarty.php or any other core files (but you know this).


That the useragent string is saved with the entry (in latest) is good; makes turning my "hack" into an extension much easier. I would assume you would have to make a change to forms.php so that the useragent string remains with a comment should the comment be edited. That's why I changed the file.

I'll look at the latest when we get back home, er, I mean, moved. By that time, I think you're planning to have 2.1 out, so the better for me.

Also, what I know is never edit any core files without first backing them up. And I always put comments around my changes, including commenting out any original code that was changed or replaced. My changes haven't messed up the functionality of Pivot or PivotX so far (at least as far as we could determine; that duplicate comment stuff was weird and corrected itself and my changes are still there).

hansfn wrote:Write an extension/snippet that defines the needed extra template tag or one that modifies the behavior of an existing template tag. (I can probably implement your changes to the comments tag - other people might want to display the user agent too. It's not that easy to modify this tag cleanly.)


Like I wrote, with the changes you've already made to latest, I should be able to write this into a workable extension. I'd just need to figure out how to write an extension, but looking at other extnesions and any available documentation should help with that.

hansfn wrote:NB! I wasn't able to unzip http://geoffrey.famwagner.com/pivotx-fi ... agentx.zip so I could see exactky what you have done.


Strange, me neither...I'll copy and paste the readme file here for you to see. There are some things in there that I want to clean up as it's just a reuse of old code, though they are working for now. I've matured as a php author since I first wrote this (though I'm still an amateur at it). I will especially be looking at useragents.inc.php. Also, right off the bat, I see I need to correct the paths information for the include_once statement.

Code: Select all
NOTE: This hack is meant for PivotX v2.0.  It was written and tested with
PivotX v2.0.  It may work in later versions...it may even be a standard
feature in later versions...who knows...

First, place the files in the .zip file (which this file was included in)
in the appropriate directories.  Place all the files in the pics/useragent/
directory in a useragent/ directory in pivotx/pics/.  Place the
useragents.inc.php file in the pivotx/includes/ directory.

Next, make backups of the following files:
   pivotx/modules/modules_comments.php
   pivotx/modules/modules_smarty.php
   pivotx/forms.php

Then, follow the hack directions below...

OPEN pivotx/modules/modules_comments.php
FIND
    $temp_comment = array(
        'entry_uid' => intval($_POST['piv_code']),
        'name' => entify($_POST['piv_name']),
        'email' =>entify($_POST['piv_email']),
        'url' => entify($_POST['piv_url']),
        'ip' => $_SERVER['REMOTE_ADDR'],
        'date' => format_date("", "%year%-%month%-%day%-%hour24%-%minute%"),
        'comment' => $comment_text,
ADD (inserted into a list of array keys)
        'useragent' => $_SERVER['HTTP_USER_AGENT'],


OPEN pivotx/modules/modules_smarty.php
FIND
                    /**
                     * substite all of the parameters into the comment, and add it to the output.
                     */
ADD
                    // get translate ua function
                    include_once($Paths['pivot_path']."includes/useragent.inc.php");

                    // make images for useragents
                    if ($temp_row['useragent']) {
                       $agentimgs = translateua($temp_row['useragent']);
                       $agentimg = explode("|", $agentimgs);
                       $osimg = $agentimg[0];
                       $browserimg = $agentimg[1];
                    } else {
                       $osimg = "<img src=\"pivotx/pics/useragent/unknown.png\" border=0 alt=\"Unknown/Unspecified OS\" title=\"Unknown/Unspecified OS\" />";
                       $browserimg = "<img src=\"pivotx/pics/useragent/unknown.png\" border=0 alt=\"Unknown/Unspecified Browser\" title=\"Unknown/Unspecified Browser\" />";
                    }
FIND
                    $this_tag= str_replace("%email-to-name%", $emailtoname, $this_tag);
                    $this_tag= str_replace("%gravatar%", $gravatar, $this_tag);
                    $this_tag= str_replace("%editlink%", $editlink, $this_tag);
ADD
                    $this_tag= str_replace("%os%", $osimg, $this_tag);
                    $this_tag= str_replace("%browser%", $browserimg, $this_tag);


OPEN pivotx/forms.php
FIND
    $form->add( array(
        'type' => 'textarea',
        'name' => 'comment',
        'label' => __('Comment'),
        'error' => __('Error'),
        'size' => 20,
        'cols' => 50,
        'rows' => 8,
        'validation' => 'string|minlen=2|maxlen=4000'
    ));
ADD
    $form->add( array(
        'type' => 'text_readonly',
        'name' => 'useragent',
        'label' => __('Useragent'),
        'size' => 40,
        'text' => makeJtip(__('Useragent'), __('This is the useragent string of the commentor\'s browser.')),
    ));


Now, simply place %os% and %browser% in your entry templates in the
places where you would like this information displayed.

Re: Hook - useragent info for commenters?

PostPosted: Thu Nov 26, 2009 3:03 am
by Stingray
hansfn wrote:NB! I wasn't able to unzip http://geoffrey.famwagner.com/pivotx-fi ... agentx.zip so I could see exactky what you have done.


Hmm...I've created the archive file in both linux and Windows, I can open the created archive files from both in either, but once I upload the archive file (from either OS), it can't be opened. I can't tell if the problem occurs when I upload the file or if something's happening at my host's end. Even if I download the archive files via ftp and try to open them, I get the error.

I'll have to look into this more when I can find the time.

Re: Hook - useragent info for commenters?

PostPosted: Sat Nov 28, 2009 4:53 pm
by Stingray
I don't know what's going on; I don't recall if there has ever been an issue downloading .zip archive files from my host before, but now they can't be, either via html or ftp. I can create them and unpack them just fine, so long as they aren't uploaded. I even tried creating the archive via my host's cpanel file management system, and they still cannot be unpacked after download.

The same goes for .rar archives.

However, .tar.gz archives are fine. Such archives have been on my site, now, for a couple of days. We've since flown to Colorado for a visit, so I have been unable to mention it until now.

I haven't tried simply changing the extension, uploading, and including instructions to change it back to a .zip file after download.