Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Upload a file using Ruby

The following code was used to upload an image file to the web server. Source code origin: Ruby Language Stuff | mod_ruby File upload scripts [zytrax.com]

file: file_upload.cgi
#!/usr/bin/ruby

# ruby script fragment
require 'cgi'
require 'stringio'

cgi = CGI.new()  # New CGI object
puts "Content-Type: text/plain"
puts
print '<result>'

# get uri of tx'd file (in tmp normally)
tmpfile = cgi.params['myfile'].first.path

# OR (functionally the same)
tmpfile = cgi.params['myfile'][0].path

# create a Tempfile reference
fromfile = cgi.params['myfile'].first

#displays the original file name as supplied in the form
puts fromfile.original_filename

# displays the content (mime) type e.g. text/html
puts fromfile.content_type

# create output file reference as original filename in our chosen directory
tofile = '/var/www/yourdomain.com/htdocs/r/'+fromfile.original_filename

# copy the file
# note the untaint prevents a security error
# cgi sets up an StringIO object if file < 10240
# or a Tempfile object following works for both
File.open(tofile.untaint, 'w') { |file| file << fromfile.read}
# when the page finishes the Tempfile/StringIO!) thing is deleted automatically

print '</result>'

file: file_upload.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>File upload</title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  </head>
  <body>
    <form name='fileupload' enctype="multipart/form-data" 
    action='/p/file_upload.cgi' method='post'>
    <input type='file' name='myfile' size="40" />
    <input type='submit' value"Send it"/>
    </form>
  </body>
</html>
  

Transforming an XML file into an XSL file

This XSL uses the XML for creating another XSL file which renders a web page containing records for a specific project, but I have found the projects I am working on use similar page layouts (ie. menu, body(articles), inputs, and buttons), so it makes sense to re-use a generic template.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="recordx">
    <xsl:variable name="colon"><xsl:text>:</xsl:text></xsl:variable>
    <xsl:element name="xsl:stylesheet">
      <xsl:attribute name="xmlns{$colon}xsl">
        <xsl:text>http://www.w3.org/1999/XSL/Transform</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="version">
        <xsl:text>1.0</xsl:text>
      </xsl:attribute>
      <xsl:element name="xsl:template">
      <xsl:attribute name="match">
        <xsl:value-of select="summary/project"/><xsl:text>page</xsl:text>
      </xsl:attribute>
      <xsl:copy-of select="menu/*"/>
      <xsl:copy-of select="articles/*"/>
      </xsl:element>
      <xsl:element name="xsl:template">
        <xsl:attribute name="match">
          <xsl:value-of select="summary/project"/><xsl:text>page/inputs</xsl:text>
      </xsl:attribute>
      <xsl:element name="div">
        <xsl:element name="dl"> 
          <xsl:for-each select="records/fields">
            <xsl:if test="c='true'">
              <xsl:element name="xsl:apply-templates">
                <xsl:attribute name="select"><xsl:copy-of select="field"/></xsl:attribute>
              </xsl:element>
            </xsl:if>
          </xsl:for-each>
        </xsl:element>
      </xsl:element>
      </xsl:element>      
      <xsl:call-template name="inputx" />      
    </xsl:element>
  </xsl:template>
  
  <xsl:template name="inputx">
    <xsl:apply-templates select="records/fields"/>
  </xsl:template>
  
  <xsl:template match="records/fields">
    <xsl:if test="c='true'">
    <xsl:element name="xsl:template">
      <xsl:attribute name="match"><xsl:value-of select="field"/></xsl:attribute>
        <dt>
          <xsl:element name="label">
            <xsl:attribute name="for"><xsl:text>title</xsl:text></xsl:attribute>
            <xsl:element name="xsl:value-of">
              <xsl:attribute name="select"><xsl:text>@title</xsl:text></xsl:attribute>
            </xsl:element>
          </xsl:element>
        </dt>
        <dd>
          <xsl:element name="input">
            <xsl:attribute name="type"><xsl:text>text</xsl:text></xsl:attribute>
            <xsl:attribute name="id"><xsl:text>title</xsl:text></xsl:attribute>
            <xsl:attribute name="size"><xsl:text>{@size}</xsl:text></xsl:attribute>            
          </xsl:element>
        </dd>
    </xsl:element>
    </xsl:if>
  </xsl:template>
  
</xsl:stylesheet>


... and here's the XML which is used by the XSL above.

<recordx>
<summary><project>books</project><parent_element>book_entry</parent_element></summary>
<menu>
  <div id="gboxes">
    <ul>
      <li><a href="/empsearch/devsearch.xml?id=all">home</a></li> 
      <li><a href="/main/development_blog.xml">development</a></li>
    </ul>
  </div>
</menu>
<articles>
  <div id="articles">
    <h1>www<xsl:value-of select="@title" /></h1>
    <div>
      <xsl:apply-templates select="inputs"></xsl:apply-templates>
    </div>
    <div>
      <xsl:apply-templates select="buttons"></xsl:apply-templates>
    </div>
    <span id="in1"></span>
  </div> 
</articles>
<records>
<fields id='17647'><field>author</field><c>true</c><r>true</r><u>true</u></fields>
<fields id='17675'><field>subject</field><c>true</c><r>true</r><u>false</u></fields>
<fields id='17699'><field>rating</field><c/><r/><u/></fields>
<fields id='17700'><field>last_modified</field><c/><r/><u/></fields>
</records>
</recordx>


Forward a page using PHP

Often, forwarding web pages is done with .htaccess. But sometimes you don't want to mess with editing .htaccess (or you may not have access to it). You can do HTML forwarding, but PHP allows you to use a more transparent method (and you'd like to retain referrer information). Just edit the code below and add it to the page in question.

- Thank to Rollie Hawk

<?php
  header("Location: http://domain.tld/page.php");
?>

Using Rio from Ruby to easily save a Web page to file

Found at http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/

# (sudo) gem install rio
require 'rubygems'
require 'rio'
# open an URI an copy the content into a file
rio('http://www.juretta.com/') > rio('juretta_index.html')


Cool syntax!

Wholinked

// description of your code here
Show the other websites linking to yours. Totally new and fully customizable, this simple cut-and-paste javascript queries the search engines for your links, then sends the results to your site's WhoLinked box for your visitors to see. Cool! http://wholinked.com
<script type="text/javascript"><!--
wholinked_text_color="99ccff";
wholinked_box_color="006699";
wholinked_hdln_color="99ccff";
wholinked_accent_color="000066";
wholinked_width="170px";
wholinked_font="Arial,Verdana,Sans-Serif";
wholinked_font_size="11px";
//--></script>
<script type="text/javascript" language="javascript"
src="http://wholinked.com/1.js"></script>


Adjust Page Number when Page Length Changes

In a web page you are display a certain page worth of results. If you allow the user to change the page length, you'll need to find the new page number that contains the first item on the current page. This is VB.Net code.

intOldPageLength = GetPageLengthValue()

' if there is a new page length value
If intOldPageLength <> m_intPageLength Then

   ' adjust the page number to compensate
   intItemCount = ((m_intPage - 1) * intOldPageLength) + 1
   m_intPage = intItemCount \ m_intPageLength
   If intItemCount - (m_intPage * m_intPageLength) > 0 Then
      m_intPage = m_intPage + 1
   End If

End If


The index of the first item on the current page happens to be a count of the items up to that point. So assume we are paging that amount and use the last page as our new current page. We apply standard paging logic of items integer divided by page length to determine the number of pages and add one if there is a remainder.

There are a couple formulas at work here worth noting.
Index of Last Item on page:
page number * page length
Index of First Item on page:
(index of last item of previous page) + 1
((page number - 1) * page length) + 1
Page Count based on page length:
number of items [integer divide] page length
if there is a remainder to the above then add 1
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS