If you use Ruby on Rails and want to have access to ActiveRecord and all the other nifty features from within a standalone Ruby script (for instance, a cronjob script that checks integrity of your data), all you need to do is including the “environment” script that fully sets up the Rails environment. In this case I use the “../config” path because my script is located in the app’s “scripts” directory.
#!/usr/bin/ruby
require ’../config/environment’
# now all Rails features are available, all plugins loaded etc.
# demo
records = Model.find :all
for r in records
puts ‘Checking ‘ + r.name + ’ …’
end
Many browsers (including Firefox and Internet Explorer) have plugins for several file types installed so they can show the files’ content inline. This is useful for things like Flash videos, may be useful for other videos and audio files, is sometimes annoying for PDF files and certainly unwanted for binary files etc.
There’s a HTTP header that instructs browsers to show a dialog box instead of showing the files inline. It’s called Content-Disposition: attachment and is defined in RFC 2183.
If you want to make all files in a directory available for download (and not for viewing inline), you can simply put this .htacess file into the directory:
RewriteEngine on
RewriteRule (.*)$ - [E=DOWNLOAD:$1]
Header onsuccess set Content-Disposition "attachment; filename=%{DOWNLOAD}e" env=DOWNLOAD
You need mod_headers and mod_rewrite enabled and all other requirements for RewriteRules in .htaccess files, i.e. Options FollowSymlinks / SymLinksIfOwnerMatch and correct AllowOverride settings.
A quick scan of sites made with Typo3 told me that 99 of 330 pages are still vulnerable to the last Typo3 security bug that allows to read all .php files, including localconf.php (where the database password and other things ard stored) at the moment.
That means that about 30 % of all Typo3 pages show their database passwords (and other things) in clear text 5 days after the security bulletin has been sent out.
To send pingbacks from your site to other blogs/communities/etc., you can use the PHP CURL module:
<?
function send_pingback
($sourceURI, $targetURI)
{
$target = file_get_contents($targetURI);
// HTTP headers are now in $http_response_header, see docs of HTTP wrapper
foreach ($http_response_header as $header)
if (preg_match("/^X-Pingback:\s*([^\s]+)\s*$/", $header, $matches))
$serverURI = $matches[1];
if (!$serverURI) {
$target = html_entity_decode($target);
if (preg_match("/<link rel=\"pingback\" href=\"([^\"]+)\" ?\/?>/", $target, $matches))
$serverURI = $matches[1];
}
if (!$serverURI)
return;
$xml = ‘<?xml version="1.0"?><methodCall><methodName>pingback.ping</methodName><params>’.
‘<param><value><string>’.$sourceURI.‘</string></value></param>’.
‘<param><value><string>’.$targetURI.‘</string></value></param>’.
‘</params></methodCall>’;
$rq = curl_init();
curl_setopt($rq, CURLOPT_URL
, $serverURI);
curl_setopt($rq, CURLOPT_POST
, 1);
curl_setopt($rq, CURLOPT_POSTFIELDS
, $xml);
curl_exec($rq);
curl_close($rq);
}
send_pingback
("… source uri …", "… target uri …");
If you want to enable a Web site (for instance, a blog or community project) to receive pingbacks, you can use the PHP XML-RPC module.
All you need to do is to
- create an XML-RPC server
- register a function for pingback.ping
- call the server
- implement the function
The function itself, let’s call it do_pingback, only needs to process the pingback: verify its validity and register the ping somewhere, for instance in the database.
Sample code:
<?
/* /pingback-server.php */
require_once("include/config.php");
function do_pingback
($method, $params, $extra)
{
global $db;
list ($sourceURI, $targetURI) = $params;
// fetch the source URI to verify that the source does indeed link to the target
$source = file_get_contents($sourceURI);
if ($source == false)
return 16; // "The source URI does not exist."
$source = html_entity_decode($source);
if (strpos($source, $targetURI) === false)
return 17; // "The source URI does not contain a link to the target URI, and so cannot be used as a source."
// TODO: check own data to ensure that the target exists and is a valid entry
// register pingback (if not done already)
$status = $db->exec("INSERT INTO pingbacks (source, target) VALUES (".$db->quote($sourceURI).", ".$db->quote($targetURI).")");
if (PEAR
::isError($status))
return 48;
return "Ping registered, thanks";
}
$server = xmlrpc_server_create();
xmlrpc_server_register_method($server, "pingback.ping", "do_pingback");
if ($response = xmlrpc_server_call_method($server, $HTTP_RAW_POST_DATA, null)) {
header("Content-Type: text/xml");
print $response;
}
?>
Of course, you have to announce the pingback server on your Web site. I prefer the HTTP header solution, so I added
header("X-Pingback: http://".GU_DOMAIN
."/pingback-server.php");
to a global include.
I use this code for the pingback implementation on gimpusers.com / gimpusers.de.
If you need a HTML list with the markers (dots, squares, list-style-images, whatever you use) on the right side (for instance for a navigation menu that is aligned to a line on its right side), you can achieve this by using RTL (right-to-left) text:
<ul style="direction: rtl;">
<li>aligned to the right side</li>
<li>yes, it is</li>
</ul>
Demo:
- aligned to the right side
- yes, it is
In “1/3 Implementing an AutoSuggest feature using MySQL fulltext indices” I described how you can use a MyISAM full-text index to extract search words for an AutoSuggest feature. Then, in “2/3 myisam_suggest: an AutoComplete tool for MySQL fulltext indices” I wrote how to install and use myisam_suggest to extract these search words in practice.
Here you will find how to use AJAX AutoSuggest together with my tool “myisam_suggest”.
For a demo, see www.gimpusers.com. Enter some characters (e.g. “brus”) into the search field on the top of the page.
Read more »