Recently in Code snippets Category
Some of us does not have a approved SMTP server on our development server, or are working outside the office. So how do you then send email when using Django as your python framework?
The answer is simple: Gmail
You will need:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '<user>@gmail.com'
EMAIL_HOST_PASSWORD = '<password>
EMAIL_PORT = 587
EMAIL_USE_TLS = True
That's it. Given, you can't use it in your production environment. But it works like a charm during development. And if you need help sending html mail with alternative content (html+txt), check out Ross Poultons article Easy Multi-Part E-Mails with Django
The answer is simple: Gmail
You will need:
- A gmail account
- Django (of course...)
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '<user>@gmail.com'
EMAIL_HOST_PASSWORD = '<password>
EMAIL_PORT = 587
EMAIL_USE_TLS = True
That's it. Given, you can't use it in your production environment. But it works like a charm during development. And if you need help sending html mail with alternative content (html+txt), check out Ross Poultons article Easy Multi-Part E-Mails with Django
Tradedoubler is one of Swedens largest ad networks. However, all ads are served over a none secure connection (https). This is fine for regular users, but if you are building a member only site with secure login (https) you are out of luck:
Serving both secure (https, all your content) and none secure (the ads) brings up a IE message asking the user if they want to "show unsecure objects" on the site. Every time the users loads a page. (In swedish: "Denna sida innehåller både säkra och osäkra objekt. Vill du visa osäkra objekt?")
So how do we solve this problem?
Usual Tradedoubler Ad code is something like this:
<script type="text/javascript">
var uri = 'http://impse.tradedoubler.com/imp?type(js)g(<id>)a(<id>)' + new String (Math.random()).substring (2, 11);
document.write('<sc'+'ript type="text/javascript" src="'+uri+'" charset="ISO-8859-1"></sc'+'ript>');
</script>
As you can see, the url is http://impse. None secure. But a little known fact is that tradedoubler ads can also be served over https://tracker.tradedoubler.com. As far as I know there is no difference.
So we do what many organisations do (such as google analytics). We let javascript check if the maste page was served over http and https. Such as this:
<script type="text/javascript">
var tdHost = ( ("https:" == document.location.protocol) ? "https://tracker." : "http://imp." );
var uri = tdHost + 'tradedoubler.com/imp?type(js)pool(<id>)a(<id> + new String (Math.random()).substring (2, 11);
document.write('<sc'+'ript type="text/javascript" src="'+uri+'" charset="ISO-8859-1"></sc'+'ript>');
</script>
Simple as that. If it's https it will use https://tracker.tradedoubler.com, if it's http it will use the regular http://imp.tradedoubler.com
The script is hardly perfect, but it works for most purposes. Tradedoubler has been notified of the problem, and hopefully they will implement a solution.
Good luck with your ad serving...
Serving both secure (https, all your content) and none secure (the ads) brings up a IE message asking the user if they want to "show unsecure objects" on the site. Every time the users loads a page. (In swedish: "Denna sida innehåller både säkra och osäkra objekt. Vill du visa osäkra objekt?")
So how do we solve this problem?
Usual Tradedoubler Ad code is something like this:
<script type="text/javascript">
var uri = 'http://impse.tradedoubler.com/imp?type(js)g(<id>)a(<id>)' + new String (Math.random()).substring (2, 11);
document.write('<sc'+'ript type="text/javascript" src="'+uri+'" charset="ISO-8859-1"></sc'+'ript>');
</script>
As you can see, the url is http://impse. None secure. But a little known fact is that tradedoubler ads can also be served over https://tracker.tradedoubler.com. As far as I know there is no difference.
So we do what many organisations do (such as google analytics). We let javascript check if the maste page was served over http and https. Such as this:
<script type="text/javascript">
var tdHost = ( ("https:" == document.location.protocol) ? "https://tracker." : "http://imp." );
var uri = tdHost + 'tradedoubler.com/imp?type(js)pool(<id>)a(<id> + new String (Math.random()).substring (2, 11);
document.write('<sc'+'ript type="text/javascript" src="'+uri+'" charset="ISO-8859-1"></sc'+'ript>');
</script>
Simple as that. If it's https it will use https://tracker.tradedoubler.com, if it's http it will use the regular http://imp.tradedoubler.com
The script is hardly perfect, but it works for most purposes. Tradedoubler has been notified of the problem, and hopefully they will implement a solution.
Good luck with your ad serving...
The web chat I am running has support for four public webcams, visible to everyone. The idea is that a user can occupy one slot for a maximum of five minutes. Unfortunately, as with any public system, there will always be those who misbehave. Therefore, when making the new webcam system, one of the new key features will be improved ban handling as well as user reporting abilities. This articles covers the latter.
What we came up with is a powerful, yet simple to use report system. This includes one-click reporting, data gathering and automatic snapshots of the webcam in question. After all, we want to see what the user saw when he pressed the report button.
In short, when a user sees any suspicious activity, he or she can simply click the report button. A snapshot is then generated and information such as nicknames and IPs of both reporter and the user being reported is collected.
Server setup
I am using two servers, one for the chat itself and one for the Flash Media Server. Flash Media Server, or FMS, is the software which makes it possible for us to broadcast webcams and streaming video in Flash. Both servers are running a web server with PHP installed and thanks to AmfPHP we can call PHP scripts on the chat server straight from FMS.
Requirements
Everything must be made server-side, for security reasons the client itself cannot generate the snapshot. The reports will be stored in the mySQL database on the main server, Chat.
Known problems
Flash Media Server cannot generate snapshots, only record video into Flash Live Video (.flv). This means we have to run FFMPEG (a free media encoder) from shell to convert FLV to Snapshots (.jpg).
Key players, or the system in short
The system consists of three major players. On the cam server we've got Flash Media Server and the PHP script generateJPG.php. generateJPG.phps sole mission is to do on-the-fly conversions between the Flash Live Video (.flv) and JPEG. On the chat server we've got the PHP function continueReport() which stores everything in the database. continueReport() is reachable from FMS thanks to AmfPHP.
The report life cycle simplified
When a user presses "Report" a function is executed in our Flash Media Server application. FMS records a one second long video clip which it stores locally for generateJPG.php. It then gathers the needed information (IP numbers, nicknames etc.) which it passes onto continueReport() on the chat server. continueReport() then downloads the snapshot in question using generateJPG.php and stores the JPG on disk, and the rest in our mySQL database.
In reality
However, the simplified version does not show connections and request replies between the scripts. In reality, this is what happens:
1. The user clicks on the report button, thus making a call to FMS.
2. FMS starts recording and stops, by timer, after 1 second.
3. FMS (using AmfPHP) sends a request to continueReport() and waits for a "OK" reply.
1. continueReport() receives the request and
makes a call to PHP on server Cam for a snapshot.
1. Cam's PHP run the shell program FFMPEG, thus
generating a .jpg snapshot which is echos back to Chat.
2. continueReport(), still running, now has the image as a
string and starts by saving the report data (IPs etc) in the
mySQL database. After that, it then stores the image on the disk.
3. continueReport(), which now is done with its operations,
send the "OK" reply to FMS.
4. FMS removes the temporary stream saved in step 2
To be continued...
And there you have it, connections and request replies included. I haven't had the time to explain and comment the source code yet, but when I do I will add another articles with at least the FMS source and a FFMPEG guide. If you are really interested, contact me and I'll hook you up.
What we came up with is a powerful, yet simple to use report system. This includes one-click reporting, data gathering and automatic snapshots of the webcam in question. After all, we want to see what the user saw when he pressed the report button.
In short, when a user sees any suspicious activity, he or she can simply click the report button. A snapshot is then generated and information such as nicknames and IPs of both reporter and the user being reported is collected.
Server setup
I am using two servers, one for the chat itself and one for the Flash Media Server. Flash Media Server, or FMS, is the software which makes it possible for us to broadcast webcams and streaming video in Flash. Both servers are running a web server with PHP installed and thanks to AmfPHP we can call PHP scripts on the chat server straight from FMS.
Requirements
Everything must be made server-side, for security reasons the client itself cannot generate the snapshot. The reports will be stored in the mySQL database on the main server, Chat.
Known problems
Flash Media Server cannot generate snapshots, only record video into Flash Live Video (.flv). This means we have to run FFMPEG (a free media encoder) from shell to convert FLV to Snapshots (.jpg).
Key players, or the system in short
The system consists of three major players. On the cam server we've got Flash Media Server and the PHP script generateJPG.php. generateJPG.phps sole mission is to do on-the-fly conversions between the Flash Live Video (.flv) and JPEG. On the chat server we've got the PHP function continueReport() which stores everything in the database. continueReport() is reachable from FMS thanks to AmfPHP.
The report life cycle simplified
When a user presses "Report" a function is executed in our Flash Media Server application. FMS records a one second long video clip which it stores locally for generateJPG.php. It then gathers the needed information (IP numbers, nicknames etc.) which it passes onto continueReport() on the chat server. continueReport() then downloads the snapshot in question using generateJPG.php and stores the JPG on disk, and the rest in our mySQL database.
In reality
However, the simplified version does not show connections and request replies between the scripts. In reality, this is what happens:
1. The user clicks on the report button, thus making a call to FMS.
2. FMS starts recording and stops, by timer, after 1 second.
3. FMS (using AmfPHP) sends a request to continueReport() and waits for a "OK" reply.
1. continueReport() receives the request and
makes a call to PHP on server Cam for a snapshot.
1. Cam's PHP run the shell program FFMPEG, thus
generating a .jpg snapshot which is echos back to Chat.
2. continueReport(), still running, now has the image as a
string and starts by saving the report data (IPs etc) in the
mySQL database. After that, it then stores the image on the disk.
3. continueReport(), which now is done with its operations,
send the "OK" reply to FMS.
4. FMS removes the temporary stream saved in step 2
To be continued...
And there you have it, connections and request replies included. I haven't had the time to explain and comment the source code yet, but when I do I will add another articles with at least the FMS source and a FFMPEG guide. If you are really interested, contact me and I'll hook you up.
Generally Movable Type 4 has nice URLs. However this is not the case for trackbacks and tag-clouds. This article describes how you clean them up, step by step using htaccess and mod_rewrite. I will not explain either htaccess or mod_rewrite but rather assume that you are familiar with them. If you are not, google them.
By default, the URLs look like this:
Trackback: http://bivald.com/cgi-bin/movabletype/mt-tb.cgi/<entry id>
Tag cloud: http://bivald.com/cgi-bin/movabletype/mt-search.cgi?tag=<tag>&blog_id=<blog id>
This is not beautiful. The idea of this article is to transform them into this:
Trackback: http://bivald.com/lessons-learned/trackback/13/
Tag cloud: http://bivald.com/lessons-learned/tag/<blog id>/<tag>
Getting started: HT Access and mod_rewrite
If you haven't got one already, create a .htaccess file in the parent folder of movable type. That is, if movable type is in:
> http://bivald.com/lessons-learned/ (public_html/lessons-learned)
The .htaccess should go into http://bivald.com/ (public_html/). If you are unfamiliar with htacces google it. Place the following two lines in it:
Make sure you copy them as three lines, starting with RewriteEngine, RewriteRule and RewriteRule. These lines assume two things, that your base url for movable type is lessons-learned and that your CGI files are in cgi-bin/movabletype. The first line enables mod_rewrite, google it if you aren't familiar with it, the two latter tells Apache to forward matches on URL lessons-learned/trackback and lessons-learned/tag to the matching .cgi-files.
What it does is to invisible convert lessons-learned/trackback/<entry-id> to cgi-bin/movabletype/mt-tb.cgi/<entry-id>
Save your file, and test it by going to:
http://www.yourdomain/your-mt/trackback/1 (replace 1 with a existing entry id). If you get a 500 Interal Server Error, you probably haven't got access to mod_rewrite. You should get the same page you would get for:
http://www.yourdomain/cgi-bin/movabletype/mt-tb.cgi/1
Getting Movable Type to print the new urls in templates.
We've now done half the work. The new URLs work, but if Movable Type doesn't print them they are of no use. Log in on Movable Type and choose Design -> Templates.
Open the Trackback template (Click "Template Modules" then "TrackBacks")
Replace "<$MTEntryTrackbackLink$>" with "http://bivald.com/lessons-learned/trackback/<$MTEntryID$>"
Edit template "Tags", change: "<$MTTagSearchLink$>&IncludeBlogs=<$MTBlogID$>" to "http://www.bivald.com/lessons-learned/tags/<$MTBlogID$>/<$MTTagName$>".
Then edit template "Sidebar - X Column Layout" (which ever you use), change: "<$MTTagSearchLink$>&IncludeBlogs=<$MTBlogID$>" to "http://www.bivald.com/lessons-learned/tags/<$MTBlogID$>/<$MTTagName$>".
Then republish your site. All in all, your trackback/tag links should be cleaner. Those a little more high-tech might want to edit lib/MT/Template/ContextHandlers.pm (and the corresponding functions) to make sure MT reports the correct URLs from the start, not just in the templates. But we'll save that for a rainy day..
By default, the URLs look like this:
Trackback: http://bivald.com/cgi-bin/movabletype/mt-tb.cgi/<entry id>
Tag cloud: http://bivald.com/cgi-bin/movabletype/mt-search.cgi?tag=<tag>&blog_id=<blog id>
This is not beautiful. The idea of this article is to transform them into this:
Trackback: http://bivald.com/lessons-learned/trackback/13/
Tag cloud: http://bivald.com/lessons-learned/tag/<blog id>/<tag>
Getting started: HT Access and mod_rewrite
If you haven't got one already, create a .htaccess file in the parent folder of movable type. That is, if movable type is in:
> http://bivald.com/lessons-learned/ (public_html/lessons-learned)
The .htaccess should go into http://bivald.com/ (public_html/). If you are unfamiliar with htacces google it. Place the following two lines in it:
RewriteEngine on
RewriteRule ^lessons-learned/trackback/([0-9]+)[\/]?$ cgi-bin/movabletype/mt-tb.cgi/$1 [L]
RewriteRule ^lessons-learned/tag/([0-9]+)/(.*) cgi-bin/movabletype/mt-search.cgi?tag=$2&IncludeBlogs=$1 [L]
Make sure you copy them as three lines, starting with RewriteEngine, RewriteRule and RewriteRule. These lines assume two things, that your base url for movable type is lessons-learned and that your CGI files are in cgi-bin/movabletype. The first line enables mod_rewrite, google it if you aren't familiar with it, the two latter tells Apache to forward matches on URL lessons-learned/trackback and lessons-learned/tag to the matching .cgi-files.
What it does is to invisible convert lessons-learned/trackback/<entry-id> to cgi-bin/movabletype/mt-tb.cgi/<entry-id>
Save your file, and test it by going to:
http://www.yourdomain/your-mt/trackback/1 (replace 1 with a existing entry id). If you get a 500 Interal Server Error, you probably haven't got access to mod_rewrite. You should get the same page you would get for:
http://www.yourdomain/cgi-bin/movabletype/mt-tb.cgi/1
Getting Movable Type to print the new urls in templates.
We've now done half the work. The new URLs work, but if Movable Type doesn't print them they are of no use. Log in on Movable Type and choose Design -> Templates.
Open the Trackback template (Click "Template Modules" then "TrackBacks")
Replace "<$MTEntryTrackbackLink$>" with "http://bivald.com/lessons-learned/trackback/<$MTEntryID$>"
Edit template "Tags", change: "<$MTTagSearchLink$>&IncludeBlogs=<$MTBlogID$>" to "http://www.bivald.com/lessons-learned/tags/<$MTBlogID$>/<$MTTagName$>".
Then edit template "Sidebar - X Column Layout" (which ever you use), change: "<$MTTagSearchLink$>&IncludeBlogs=<$MTBlogID$>" to "http://www.bivald.com/lessons-learned/tags/<$MTBlogID$>/<$MTTagName$>".
Then republish your site. All in all, your trackback/tag links should be cleaner. Those a little more high-tech might want to edit lib/MT/Template/ContextHandlers.pm (and the corresponding functions) to make sure MT reports the correct URLs from the start, not just in the templates. But we'll save that for a rainy day..
Movable Type is a great CMS/Blog system built on Perl and version 4 was recently release for betatesting. However, the release candidate is not without bugs. Here are some problems and solutions I came up with:
500 Internal Server error on rebuild/Image upload (specially PNG).
Either you are running out of memory, or you'r Image::Magick installation is either broken or missing. If you visit http://www.yourdomain.com/cgi-bin/movabletype/mt-check.cgi and scroll down to "Image::Magick", does it appear as valid? Or does it say it's missing.
Either way (there have been reports that users with apparently OK installations is helped by this too) try using NetPBM instead. Follow this guide:
http://sniptools.com/tutorials/thumbnailing-with-netpbm-and-movable-type
Using NetPBM with PNGs (... or File not found /path/to/mt/topnm"
Movable Types default NetPBM settings does not allow pngs (possible Movable Types default settings for Image::Magick either). Locate your Image.pm (In movabletype/MT folder), line 1XX (122 probably) and change:
my %Types = (jpg => 'jpeg', gif => 'gif');
To:
my %Types = (jpg => 'jpeg', gif => 'gif', png => 'png');
Otherwise you will get a "file not found: /users/home/etc/netpbm/topnm" error. Solution originally found on http://forums.sixapart.com/index.php?showtopic=54246
No entry ID on comments/commenting
Short answer: Make sure you have www in your CGIPath on mt-config.cfg (or remove it if you have).
Longer answer: This has to do with POST redirecting problems. Make sure your CGIPath in mt-config.cfg reflects your sites www-structure. If your adress is: http://www.domain.com/cgi-bin... You must include the www in CGIPath. But if your adress is http://domain.com/cgi-bin.. you must remove the www.
500 internal server error on commenting (... with NetPBM)
This could have several reasons. Start by turning off E-mail notifications (to make sure your sendmail isn't causing the trouble), then set "" to anyone. Also, see http://www.sixapart.com/movabletype/kb/comments/500_on_comment.html if their solution works.
If nothing else, you might have the same rather obscure error as me. NetPBM cannot produce CAPTCHA images, you will find this in your comment settings:
"No CAPTCHA provider is available in this system. Please check to see if Image::Magick is installed, and CaptchaImageSourceBase directive points to captcha-source directory under mt-static/images."
However, this does not stop MT from trying to initiate Captcha (even if it would later choose not to use it). Open lib/MT/App/Comments.pm and comment out (add # at the start of the line) in init(), from:
$app->init_captcha_providers();
to:
#$app->init_captcha_providers();
And your problem (might be solved), mine was.
500 Internal Server error on rebuild/Image upload (specially PNG).
Either you are running out of memory, or you'r Image::Magick installation is either broken or missing. If you visit http://www.yourdomain.com/cgi-bin/movabletype/mt-check.cgi and scroll down to "Image::Magick", does it appear as valid? Or does it say it's missing.
Either way (there have been reports that users with apparently OK installations is helped by this too) try using NetPBM instead. Follow this guide:
http://sniptools.com/tutorials/thumbnailing-with-netpbm-and-movable-type
Using NetPBM with PNGs (... or File not found /path/to/mt/topnm"
Movable Types default NetPBM settings does not allow pngs (possible Movable Types default settings for Image::Magick either). Locate your Image.pm (In movabletype/MT folder), line 1XX (122 probably) and change:
my %Types = (jpg => 'jpeg', gif => 'gif');
To:
my %Types = (jpg => 'jpeg', gif => 'gif', png => 'png');
Otherwise you will get a "file not found: /users/home/etc/netpbm/topnm" error. Solution originally found on http://forums.sixapart.com/index.php?showtopic=54246
No entry ID on comments/commenting
Short answer: Make sure you have www in your CGIPath on mt-config.cfg (or remove it if you have).
Longer answer: This has to do with POST redirecting problems. Make sure your CGIPath in mt-config.cfg reflects your sites www-structure. If your adress is: http://www.domain.com/cgi-bin... You must include the www in CGIPath. But if your adress is http://domain.com/cgi-bin.. you must remove the www.
500 internal server error on commenting (... with NetPBM)
This could have several reasons. Start by turning off E-mail notifications (to make sure your sendmail isn't causing the trouble), then set "" to anyone. Also, see http://www.sixapart.com/movabletype/kb/comments/500_on_comment.html if their solution works.
If nothing else, you might have the same rather obscure error as me. NetPBM cannot produce CAPTCHA images, you will find this in your comment settings:
"No CAPTCHA provider is available in this system. Please check to see if Image::Magick is installed, and CaptchaImageSourceBase directive points to captcha-source directory under mt-static/images."
However, this does not stop MT from trying to initiate Captcha (even if it would later choose not to use it). Open lib/MT/App/Comments.pm and comment out (add # at the start of the line) in init(), from:
$app->init_captcha_providers();
to:
#$app->init_captcha_providers();
And your problem (might be solved), mine was.
Recently I implemented support for double, stand-alone y-axis in Open Flash Charts. Turns out that John Glazebrook (main coder) has gotten "LOTS of requests for 2 Y axis" and my contribution will be included in the next release of Open Flash Charts. That way all users can benefit from my modifications..
Technical details (to other Open Flash Charts users)
If you want to try out double y-axis before the next release, download my open-flash-chart.swf and add this to your data file:
&y2_axis_colour=#164166& // Same as regular documentation
&y2_label_style=10,0x164166& // Same as regular documentation
&show_y2=true& // Do we use double axels?
&y2_lines=1& // Which of the lines should be drawn based on the y2-axel? (1,2,3 etc)
&y2_left=70& // Left margin (how far left should we push the regular content)
&y2_min=0& // Same as regular documentation
&y2_max=2400& // Same as regular documentation
&y2_legend=Free Ram (mb),12,0x164166& // Same as regular documentation
Good luck! If you got any questions, simply contact me
Technical details (to other Open Flash Charts users)
If you want to try out double y-axis before the next release, download my open-flash-chart.swf and add this to your data file:
&y2_axis_colour=#164166& // Same as regular documentation
&y2_label_style=10,0x164166& // Same as regular documentation
&show_y2=true& // Do we use double axels?
&y2_lines=1& // Which of the lines should be drawn based on the y2-axel? (1,2,3 etc)
&y2_left=70& // Left margin (how far left should we push the regular content)
&y2_min=0& // Same as regular documentation
&y2_max=2400& // Same as regular documentation
&y2_legend=Free Ram (mb),12,0x164166& // Same as regular documentation
Good luck! If you got any questions, simply contact me
