Make a post request usign CURL and PHP

  1.  
  2. $fields = array(
  3.         ‘a’=>’1′,
  4.         ‘b’=>’2′,
  5.         ‘c’=>’3′,
  6.         ‘d’=>’4′
  7. );
  8. $ch = curl_init();
  9. curl_setopt($ch,CURLOPT_URL, ‘http://mysite.com’);
  10. curl_setopt($ch,CURLOPT_POST, count($fields));
  11. curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields) );
  12. curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  13. $result = curl_exec($ch);
  14. curl_close($ch);
  15.  

Extract MP3 from SWF files on Linux (Ubuntu)

We will use a tool called swfextract contained on the package swftools, so first we will install it

  1.  
  2. sudo apt-get install swftools
  3.  

Once installed first we will see wich media we can extract from our swf file.

  1.  
  2. rene@rene-desktop:~/Music$ swfextract test.swf
  3. Objects in file test.swf:
  4.  [-i] 5 Shapes: ID(s) 7, 14, 22, 23, 28
  5.  [-j] 16 JPEGs: ID(s) 1-6, 11-13, 16-21, 27
  6.  [-s] 1 Sound: ID(s) 29
  7.  [-F] 1 Font: ID(s) 8
  8.  [-f] 1 Frame: ID(s) 0
  9.  [-m] 1 MP3 Soundstream
  10.  

Then just use the option between brackets and the ID the element you want to extract, in our example we will extract the sound with ID 29, but you can extract also images if you want

  1.  
  2. rene@rene-desktop:~/Music$ swfextract -s 29 test.swf -o test.mp3
  3.  

How to disable directory listing in apache?

First option is to edit the file /etc/apache2/sites-avalible/default (i am using ubuntu), then look for the following lines:

  1.  
  2. <Directory /var/www/>
  3.     Options Indexes FollowSymLinks MultiViews
  4.     AllowOverride None
  5.     Order allow,deny
  6.     allow from all
  7.     # This directive allows us to have apache2′s default start page
  8.     # in /apache2-default/, but still have / go to the right place
  9.     #RedirectMatch ^/$ /apache2-default/
  10. </Directory>
  11.  

And then take out the word Indexes, then your file will look like

  1.  
  2. <Directory /var/www/>
  3.     Options FollowSymLinks MultiViews
  4.     AllowOverride None
  5.     Order allow,deny
  6.     allow from all
  7.     # This directive allows us to have apache2′s default start page
  8.     # in /apache2-default/, but still have / go to the right place
  9.     #RedirectMatch ^/$ /apache2-default/
  10. </Directory>
  11.  

Finally we just reload apache

  1.  
  2. sudo /etc/init.d/apache2 restart
  3.  

If you want to use .htaccess, you have two options:

  1.  
  2. # first one is to deny access to indexes
  3. Options -Indexes
  4.  

This option will show a forbidden message:

Forbidden
  1.  
  2. # the second option is to show the directory as empty
  3. IndexIgnore *
  4.  
empty

See a list of loaded apache modules

If you want to see wich modules are loaded by apache, just open a terminal and type:

  1.  
  2. apache2ctl -M
  3.  

And you will get something like this:

  1.  
  2. rene@rene-desktop:~$ apache2ctl -M
  3. Loaded Modules:
  4.  core_module (static)
  5.  log_config_module (static)
  6.  logio_module (static)
  7.  version_module (static)
  8.  mpm_prefork_module (static)
  9.  http_module (static)
  10.  so_module (static)
  11.  alias_module (shared)
  12.  auth_basic_module (shared)
  13.  authn_file_module (shared)
  14.  authz_default_module (shared)
  15.  authz_groupfile_module (shared)
  16.  authz_host_module (shared)
  17.  authz_user_module (shared)
  18.  autoindex_module (shared)
  19.  cgi_module (shared)
  20.  deflate_module (shared)
  21.  dir_module (shared)
  22.  env_module (shared)
  23.  mime_module (shared)
  24.  negotiation_module (shared)
  25.  php5_module (shared)
  26.  reqtimeout_module (shared)
  27.  rewrite_module (shared)
  28.  setenvif_module (shared)
  29.  status_module (shared)
  30. Syntax OK
  31.  

Migrate Google Maps from V2 to V3

Google maps v2 has been deprecated since May 19, 2010 and will stop working on May 19, 2013, so here is a little guide on how to migrate.

On V3 we don’t need an API key, but it’s useful to keep track of your API calls (25,000 free), so to get the API key, log into https://code.google.com/apis/console/, click on services and activate google maps v3, once is done click on API access on the left, and go to Simple API at the very bottom, then click on edit allow referers, and set up like *.mysite.com/* that will allow all your subdomains and folders on your site.

Now that you have your API key, is time to replace the old URL, by the new one:

  1.  
  2. <!– V2 –>
  3.  
  4. <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=$yourKey" ></script>
  5.  
  6. <!– V3 –>
  7.  
  8. <script src="https://maps.googleapis.com/maps/api/js?key=$youKeu&v=3&sensor=true" type="text/javascript" ></script>
  9.  
  10. <!– "sensor" is required an indicates if a device (like GPS) is used to determine the user’s location –>
  11.  

First get rid of the old unload function, is not needed anymore.

  1.  
  2. /* V2 */
  3. window.onunload=function(){
  4.         GUnload();
  5. }
  6.  

Also get rid of old code used to load Vector Markup Language(VML) just needed for IE, VML has been deprecated on IE9 in favor Scalable Vector Graphic (SVG)

  1.  
  2. <!DOCTYPE html>
  3. <html lang="en" xmlns:v="urn:schemas-microsoft-com:vml>
  4. <head>
  5.         <style type="text/css">
  6.         v\:* {
  7.         behavior:url(#default#VML);
  8.         }
  9.         </style>

Now lets load the map itself.

  • First take out GBrowserIsCompatible is not supported anymore.
  • Replace GMmap2 by google.maps.Map.
  • GLatLng has been replaced by google.maps.LatLng.
  • Now take out addControl, setMapType, now all of these are passed as an object when map is created
  • For V2 we use these map types G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP on V3 they have been replace by google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID ,google.maps.MapTypeId.TERRAIN

  1.  
  2. /* V2 */
  3. if( GBrowserIsCompatible() ) {
  4.         map = new GMap2( $( ‘map’ ) );
  5.         map.addControl(new GMapTypeControl());
  6.         map.addControl(new GOverviewMapControl());
  7.         map.addControl(new GScaleControl());
  8.         map.addControl(new GLargeMapControl());
  9.         map.setCenter( new GLatLng( myLatitude, myLongitude ), 9 /*zoomLevel*/ );
  10.         map.setMapType( G_NORMAL_MAP );
  11.         //map.setMapType( G_SATELLITE_MAP );
  12.         //map.setMapType( G_HYBRID_MAP );
  13. }
  14.  
  15. /* V3 */
  16.  
  17. var mapOptions = {
  18.         zoom: 9 ),
  19.         center: new google.maps.LatLng(myLatitude, myLongitude),
  20.         scaleControl: true,
  21.         overviewMapControl: true,
  22.         overviewMapControlOptions:{opened:true},
  23.         mapTypeId: google.maps.MapTypeId.ROADMAP
  24. };
  25. map = new google.maps.Map($( ‘map’ ), mapOptions);
  26.  

In V3 creating custom icons is a lot easier, instead of creating a special icon object now we can just pass image as url.

  1.  
  2. /* v2 */
  3.  
  4. myIcon = new GIcon();
  5. myIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
  6. myIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  7. myIcon.iconSize = new GSize(12, 20);
  8. myIcon.shadowSize = new GSize(22, 20);
  9. myIcon.iconAnchor = new GPoint(6, 20);
  10. myIcon.infoWindowAnchor = new GPoint(5, 1);
  11. var point = new GLatLng( myLatitude, myLongitude );
  12. myMarker = new GMarker( point, myIcon );
  13.  
  14. /* v3 */
  15.  
  16. var properties = {
  17.         position: new google.maps.LatLng(myLatitude, myLongitude),
  18.         map: map,
  19.         title:‘Testing Icon’,
  20.         icon:‘icon.jpg’,
  21.         shadow:‘shadow.jpg’
  22. };
  23. var marker = new google.maps.Marker(properties);
  24.  
  25. /* icon and shadow can be just a string containg  the image file url, for must complex icons just pass and object with all the properties
  26.   var image = {
  27.     url: ‘images/icon.png’,
  28.     size: new google.maps.Size(20, 32),
  29.     origin: new google.maps.Point(0,0),
  30.     anchor: new google.maps.Point(0, 32)
  31.   };
  32. more info: https://developers.google.com/maps/documentation/javascript/overlays?hl=en#ComplexIcons
  33. */
  34.  
  35.  

clearOverlays has been removed from v3, so if you want remove all your markers from your map, now you have to manually keep track your markers an remove them.

  1.  
  2. /* v2 */
  3. map.clearOverlays();
  4.  
  5. /* v3 */
  6.  
  7. var overlays =  []; // Global array to store marker
  8.  
  9. overlays[overlays.length] = new google.maps.Marker(properties); // store a reference to the marker each time you create one
  10.  
  11. // delete all the markers on the array
  12. function clearOverlays() {
  13.         while(overlays[0]){
  14.                 var overlay = overlays.pop();
  15.                 overlay.setMap(null);
  16.                 overlay = null;
  17.         }
  18. }
  19.  

To create overlay on Google maps V3 now we have to use google.maps.Polyline inseatd of GPolyline.

  1.  
  2. /* V2 */
  3.  
  4. var points = [];
  5. points.push( new GLatLng( latitude1, longitude1 ) );
  6. points.push( new GLatLng( latitude2, longitude2 ) );
  7. points.push( new GLatLng( latitude3, longitude3 ) );
  8. map.addOverlay( new GPolyline( points ) );
  9.  
  10.  
  11. /* V3 */
  12.  
  13. var points = [];
  14. points.push( new google.maps.LatLng( latitude1, longitude1 ) );
  15. points.push( new google.maps.LatLng( latitude2, longitude2 ) );
  16. points.push( new google.maps.LatLng( latitude3, longitude3 ) );
  17. var area = new google.maps.Polyline({path: points, strokeColor: "#FF0000",strokeOpacity: 1.0,strokeWeight: 2});
  18. area.setMap(map);
  19.  
  20.  
On google maps v3 is a lot easier to create custom controls, now we only create a div element an insert it on Google maps.

  1.  
  2. /* V2 */
  3.  
  4. /* we used to create the custom control like */
  5.  
  6. SaveMapsStatus.prototype = new GControl();
  7.        
  8. SaveMapsStatus.prototype.initialize = function(map) {
  9.         var container = document.createElement("div");
  10.         container.id = ‘myId’;
  11.         container.innerHTML = ‘<a href="save.php" ><img src="icon_save.gif" /></a>’;
  12.         map.getContainer().appendChild(container);
  13.         return container;
  14. }
  15. SaveMapsStatus.prototype.getDefaultPosition = function() {
  16.         return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(80, 7));
  17. }
  18.  
  19. map.addControl(new SaveMapsStatus()); /* we used to add to the map, after it was created */
  20.  
  21. /* v3 */
  22. var container = document.createElement("div"); /* To change look, just add some style like container.style.border=” */
  23. container.id = ‘myId’;
  24. container.innerHTML = ‘<a href="save.php" ><img src="icon_save.gif" /></a>’; /* Custom HTML code */
  25. map.controls[google.maps.ControlPosition.TOP_LEFT].push(container);
  26.  

Check memory and hard drive usage on linux

Here are two very useful commands to see available memory and hard drive space on linux

To see available memory we will use free

  1.  
  2. # -h give us the output on a format readble for humans :)
  3. $ free -h
  4.  
  5.              total       used       free     shared    buffers     cached
  6. Mem:          7.8G       3.6G       4.2G         0B       356M       924M
  7. -/+ buffers/cache:       2.3G       5.4G
  8. Swap:          18G         0B        18G
  9.  
  10.  

Yo see hard drive usage we will use df

  1.  
  2.  
  3. # the same h stands for human readble
  4. $ df -h
  5.  
  6. Filesystem      Size  Used Avail Use% Mounted on
  7. /dev/sda3       193G   20G  163G  11% /
  8. udev            3.9G  4.0K  3.9G   1% /dev
  9. tmpfs           1.6G  1.1M  1.6G   1% /run
  10. none            5.0M     0  5.0M   0% /run/lock
  11. none            3.9G  1.5M  3.9G   1% /run/shm
  12. none            100M   12K  100M   1% /run/user
  13. cgroup          3.9G     0  3.9G   0% /sys/fs/cgroup
  14.  
  15.  

Run asp.net on ubuntu 12.10 linux

Thanks to mono we can run asp.net on our linux machines (i have to test some asp.net code), to do that we will use XSP, here the description took it from mono website.

XSP is a standalone web server written in C# that can be used to run your ASP.NET applications with minimal effort. XSP works under both the Mono and Microsoft runtimes. The code is available from our download page (look for XSP web server) or from the Anonymous SVN repository (module name: xsp).

To nstall it:

  1.  
  2. #install XSP
  3. sudo apt-get install mono-xsp2
  4.  
  5. # i have to use vb.net so, i installed the Visual Basic.NET Compiler
  6. sudo apt-get install mono-vbnc
  7.  

Once installed, just start XSP from the directory where your code is located, after that you can browse to http://localhost:8080 to see your web pages

  1.  
  2. $ cd /home/rene/asp/
  3. $ xsp2
  4. xsp2
  5. Listening on address: 0.0.0.0
  6. Root directory: /usr/share/asp.net2-demos
  7. Listening on port: 8080 (non-secure)
  8. Hit Return to stop the server.
  9. Application_Start
  10.  

Crypting data using 3des on c#

Today i have to use c# to crypt some data using 3des, so here is a pretty basic example to do it, i test it using mono.

  1.  
  2. using System;
  3. using System.Security.Cryptography; // used for TripleDESCryptoServiceProvider
  4. using System.Text; // used for Encoding.ASCII.GetBytes
  5.  
  6. public class Test
  7. {
  8.         static public void Main ()
  9.         {
  10.                 TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); // i create the 3des object
  11.                 byte[] key = Encoding.ASCII.GetBytes("123456781234567812345678"); // the key should be 24 chars long
  12.                 string data = "Here goes the data to encrypt";
  13.                 des.Mode = CipherMode.CBC; // We’ll use CBC
  14.                 des.Key = key; // I set the key
  15.                 byte[] iv = des.IV; // I store the randomly generated IV since i will need for decrypt
  16.                 byte[] inputArray = UTF8Encoding.UTF8.GetBytes(data);
  17.                 ICryptoTransform cTransform = des.CreateEncryptor();
  18.                 byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
  19.                 // i will print as readble data
  20.                 Console.WriteLine( (Convert.ToBase64String(resultArray)) );
  21.                 Console.WriteLine( (Convert.ToBase64String(iv)) );
  22.         }
  23. }
  24.  
  25.  

Edit less files using Jedit

When you try to edit a less file on jedit, by default there is no color scheme at all, that makes editing css files extremely hard.

To change the way jedit color code the actual file go to “Utilities > Buffer Options” and on “Edit mode” select the color scheme to css.

If you don’t want to do these each time you edit a less file, lets tell jedit to treat *.less files as css files, to do this, “Utilities>Options” and then choose “Editing” on “Change settings for mode” select css, then uncheck “use defaul settings” and chane “File name glob” to “*.{css,less}”.

Block spammers by IP

If your website is being attacked by spammers, you may want to block those users from your website. first thing we will do is to see how many connection by IP we have in our server, we will do this using netstat.

  1.  
  2. netstat -ntu | awk ‘ $5 ~ /^[0-9]/ {print $5}’ | cut -d: -f1 | sort | uniq -c | sort -n
  3.  

Once we have the IP address to block, we will use .htaccess to block those ip address like:

  1.  
  2. order allow,deny
  3. deny from 127.0.0.1
  4. deny from 127.0.0.2
  5. deny from 127.0.0.3
  6. allow from all
  7.