0

I'm working on a drupal 6 module and need a way to save a static html version of a node to the server, using the user's permissions. Basically I need the equivalent of "wget -O out.htm http://mydrupalsite.org/node/1635" in drupal, that uses the activating user's permissions and login, but don't know enough about drupal to make this work without some pointers in the right direction. I've looked through the functions list and the module code for http://drupal.org/project/print, but no luck. Any help?

Thanks in advance.

EDIT: Clarification: What I'm trying to accomplish is have a sidebar link for users to use that saves a copy of the current page -exactly as apache just sent it the user- to the server. I'm trying to do this through drupal -without- using wget or any other system utility.

1

Is this for your site users to have a preview a static html page or node on your site?!

  • Cant you use drupal_render at a custom menu callback URL or Rule to get the HTML generated for 1 of your pages and save it to a file in your webroot or sites/all/files/SOMEDIR
  • then using RewriteCond in your Apache site configuration to ignore these static files so you can access them via a non-drupal URL ...
  • In drupal 7 at least there is like function node_view that lets you load a node for viewing, it respects user access rigts for visibility of stuff on the node.

There are programmatic ways to create and load a user (with assigned roles as well) in any version of Drupal. So you could tie all these bits together as:

Generate HTML fragment of node/page logic could be:

  • switch to some user by UID.
  • node_load() some node owned by user NID.
  • node_view() the node contents as some user.
  • drupal_render() get the raw HTML.
  • save to some filesystem location as .HTML file.
  • This -seems- to be exactly what I was looking for, but I'm getting empty nodes..? I'll do some research and post a second question if I don't figure it out. Thanks! – 1279343 Mar 4 '13 at 16:54
  • just fyi I reviewed this module "github_pages" that in its module file loads a page of content on a drupal site and sends it to github, that's most of the steps I outlined above ... drupal.org/node/1914740. you can see how he gets a page to save/export as HTML. – tenken Mar 7 '13 at 4:58
0

This would have HUGE security implications.

wget has no login challenge:response system like mail retrieval systems. The only way would be to embed the username and password, or a login hash, directly in the requested url, and to have Drupal authenticate the user from the parameters. This would require programming on your part to include this functionality, and would open your site to attack if you opened up programmatic access.

If you are only interested in looking at a few pages, I would suggest you log in, open the pages one by one, and 'View Source'. Use cut and past to assemble the file you need.

  • What are the security implications? wget can POST data and save cookies, so it can interact with Drupal the same way a browser does-- see my answer. I agree that using a browser is probably a better answer in most cases. – davidcl Mar 3 '13 at 20:49
  • @davidcl - If he is doing this through a module, it would mean that somewhere the user would have to store plain text username:passwords in the database. I assumed that the wget communication was between different sites. If he is looking at assembling page information within the same site, wget is not necessary (See answer from tanken). – Triskelion Mar 3 '13 at 21:04
  • I see what you're saying now. – davidcl Mar 3 '13 at 21:20
0

You actually can do this using wget, but it is probably more trouble than it is worth (why not just log in as the user and capture this data through the browser?)

From the wget man page:

   This example shows how to log to a server using POST and then proceed to download the desired pages, presumably only accessible to authorized users:

           # Log in to the server.  This can be done only once.
           wget --save-cookies cookies.txt \
                --post-data 'user=foo&password=bar' \
                http://server.com/auth.php

           # Now grab the page or pages we care about.
           wget --load-cookies cookies.txt \
                -p http://server.com/interesting/article.php

   If the server is using session cookies to track user authentication, the above will not work because --save-cookies will not save them (and neither will browsers) and the cookies.txt file will be empty.  In that case use
   --keep-session-cookies along with --save-cookies to force saving of session cookies.

So, for drupal:

wget --post-data 'name=xxx&pass=xxx&form_id=user_login' --save-cookies cookies.txt --keep-session-cookies http://example.com/user
wget --load-cookies cookies.txt http://example.com/interesting

I've tested this against a copy of D7 and it works. (By the way, the example in the man page suggests using -p on the second request; this will retrieve page assets like CSS, JS, and images. I think in most cases you don't want this, so I've excluded it from my drupal example.

0

If you have access to drush on the Drupal web server, you can use this wget command with drush uli to save authenticated session cookies for any Drupal user.

wget --save-cookies cookies.txt --keep-session-cookies \
  --post-data 'op=Log In' -O /dev/null \
  "$(drush uli)/login"

Now use those cookies to save the page or file that requires authentication.

wget --load-cookies cookies.txt \
  http://example.com/admin/reports/status

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.