Sep
20

AMV files in Linux – revisited

I just did a re-write of one of my most popular hits on this blog. It was all documented in the comments of the script’s page but it’s a bit of a mess so here’s the definitive step-by-step walkthrough to converting your videos to AMV format in Linux (I use Ubuntu 10.04) If you can’t make out the pictures in this post click on them to make them bigger – remember, they’re only pictures, not real dialog windows… ;oP

  1. Open a terminal window
  2. sudo apt-get install subversion
    svn checkout http://amv-codec-tools.googlecode.com/svn/trunk/ amv-codec-tools
    cd amv-codec-tools/AMVmuxer
    make build
    Allow this to finish, it takes a good few minutes
  3. sudo apt-get install nautilus-actions
  4. cd
    gedit
  5. Paste the following code into that file
    #!/bin/bash
    for uri in $@; do
    INFILE=$uri
    FORMAT=${INFILE:(-4)}
    OUTFILE=${INFILE/$FORMAT/.amv}
    cd ~/amv-codec-tools/AMVmuxer ;
    ./ffmpeg/ffmpeg -i "$INFILE" -f amv -r 16 -s 160x120 -ac 1 -ar 22050 -qmin 3 -qmax 3 "$OUTFILE" ;
    zenity --title "$FORMAT to .amv File Conversion Complete" --info --text="${INFILE/file:\/\// } has converted \n\nCheck ${OUTFILE/file:\/\// }";
    done

    …save file as ‘convert to amv’ and close the file
  6. back in the terminal…nautilus-actions-config-tool
  7. In the window that pops up, click new and make it look like this (don’t worry about the shred file and wipe free space stuff, we’re only interested in the Convert to amv stuff):


    note the different tabs that are open, also note where quotation marks have been used
  8. Save the configuration
  9. Close the nautilus actions configuration tool window
  10. Close the terminal window
  11. In nautilus navigate to a file you want converted
  12. Right click on it, you should see this menu itemgo ahead and select that
  13. After some time, how long depends on the size of the file, you’ll get a dialog box looking something like this
  14. Go ahead and play the file (I use GNOME MPlayer for amv files “sudo apt-get install gnome-mplayer” but vlc plays them too. You may need codecs.)

Hopefully that’ll be you sorted but there are a few points to note…

  1. Spaces in file or path names – it can’t handle them
  2. You can do multiple files on the one click, it’ll do them one at a time
  3. Some file types won’t convert without messing with ffmpeg – notably mp4/mp3/3gp – these can be made to work but that’s outside the scope of this, importantly flv files convert perfectly so your youtube downloads will work ;oP
  4. This was written using Ubuntu – I can’t be certain it’ll work with other distros

go stick your amv files onto your iteddy or ipod-esque thingy and play to your hearts content!

Sep
18

NT News

In a similar vein to my last post I’ve had a request from my very dear friend @Rantz to do something along the same lines for the Northern Territory’s favourite chip-wrapper.

So I did…

If anyone else would like their own piss-take re-branding of a site they like/loath feel free to drop me a line but for now here’s the NT News version…
Download (Downloaded 76 times)

Sep
11

Pee and what now?

I’ve created a quick tongue in cheek user script to help folks remember what the P&J is all about. Install it and navigate to any P&J page

Download (Downloaded 158 times)

The download link should ask to install it on Chrome.
In Firefox with Greasemonkey installed it should show the script with a button on the top right to install.
If that above doesn’t happen save it to the desktop then drag it onto an open firefox window and greasemonkey should sort it out then.

Aug
29

Custom smilies


I use my own smilies around the web but I always forget the full link to the pics whenever I’m posting to a forum or whatever. I don’t need to remember them though as a long time ago I made the script below, saved it as index.php and put it in the folder where I hold my smilies… Now I just click a bookmark and open that page for a quick copy/paste. Feel free to use for your own uses, it’s essentially making a list of files in the directory and displaying them as per the html in the echo statements…

<script>
function SelectAll(element)
{
element.focus();
element.select();
}
</script>
<?php
    $path = getcwd() ;
    $dir_handle = @opendir($path) or die("Error opening $path");
    echo "<table style='text-align:center;font-family:sans-serif;'><tr><th>Image</th><th>Web Link</th><th>Forum Link</th><th>Image</th></tr>\n";
    while ($file = readdir($dir_handle)) {
	    if ("$file" != "." && "$file" != ".." && "$file" != "index.php") {
			echo "<tr><td><a href=\"http://the-kid.org/images/smilies/".htmlspecialchars(urlencode($file))." \"><img src=\"".htmlspecialchars(urlencode($file))."\" /></a></td>";
			echo "<td><input onClick=\"SelectAll(this);\" size=\"50\" type=\"text\" value=\"<img src='http://the-kid.org/images/smilies/".htmlspecialchars(urlencode($file))."'/>\" /></td>";
			echo "<td><input onClick=\"SelectAll(this);\" size=\"50\" type=\"text\" value=\"[img]http://the-kid.org/images/smilies/".htmlspecialchars(urlencode($file))."[/img]\" /></td>
			<td><a href=\"http://the-kid.org/images/smilies/".htmlspecialchars(urlencode($file))." \"><img src=\"".htmlspecialchars(urlencode($file))."\" /></a></td>
			</tr>\n";
    	}
    }
    echo "</table>";
    closedir($dir_handle);
?>

Aug
21

How to make a simple web proxy in PHP

I recently tried to follow this link and was met with the equivalent of a 403!

I was like, “I’m not having that!” and was about to turn on my VPN when I thought, “my webserver’s located in the states, why not just make a proxy?”

So I did a very, very, simple web proxy…

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>BTK's Web Proxy</title>
    </head>
    <body>
        <div style="text-align:center;">
            <form method="GET" action="<?=$_SERVER['REQUEST_URI']?>">
                <input type="url" name="url" placeholder="Type URL of site"/><input type="submit" Value="Load url" />
            </form>
        </div>
        <hr/>
        <?php
            $url = $_GET['url'];
            if (!empty($url))
            {
                // check we're only getting files served by a website (i.e. not ../../../passwords.txt from this server etc.)
                if(preg_match('/^https?:/i', $url))
                {
                    $contents = file_get_contents($url);
                    if($contents === FALSE)
                    {
                        echo "<h2>Sorry <pre>{$url}</pre> cannot be read</h2>\n";
                    }
                    //display contents of url
                    else
                    { ?>
                        <?=$contents?>
                        <script>for (var i=0; i<document.links.length; i++) document.links[i].href="<?=$_SERVER['PHP_SELF']?>?url="+document.links[i].href;</script>
                    <?php }
                }
                else
                {
                    echo "<h2><pre>$url</pre> is an invalid URL</h2>\n";
                }
            }
        ?>
    </body>
</html>

Essentially this is a form that takes an url and displays the contents of the url. Using html5 we have the input type of “url” which handles our validating, though as this is down to the support of the client(non-supporting clients render this as a type=”text” field and do no validation) we have some validation on the server too. There’s also nothing to stop someone just editing the page URL in their address bar to try to access a local file etc.

So now I can see the restricted content. Result!

I added a little javascript for ease to rewrite all the links, so you can click through and have the pages parsed by the web proxy.

Aug
19

How to move Firebug to bottom of Firefox browser window

A recent update to firebug moved the icon to open the console to the top right of firefox. Everyone hates it as far as I am aware and there’s no setting to alter it. However you can do the following…

  1. Right-click in the bottom bar or near the firebug icon to get a menu
  2. Select “Customise…”
  3. Ignoring the window that pops up, drag the firebug icon to the bottom pane
  4. Click “Done” in the window that popped up

…you’re welcome!

Aug
03

PHP Random Letter Function

I think the title says it best. A simple php function to produce a random letter or string of random letters…

function randLetter($howMany=1)
{
    for($i=0;$i<$howMany;$i++)
    {
        if (rand(0,1)) $randStr .= chr(rand(97,122));
        else $randStr .= chr(rand(65, 90));
    }
    return $randStr;
}

Jun
27

Reading Apache logs with PHP in Ubuntu

I was recently the subject of a hacker, it used my servers to send spam emails via a web-script. To find the script I had to check out my server logs. Turns out that this is a bit of a pain to do. I decided to write a wee script to read them and display the contents for me online. A web-page that fills a table with the details.

Sounds simple right?

Well it *is*… kinda. There are a number of issues with this.

  1. The log file is huge
  2. Apache isn’t allowed to read it
  3. The log files rotate

…being the main ones. So this is how we handle that…

Firstly we don’t read the whole file, we choose how many calls we want to return (I found 500 to be adequate for my needs). To get this I decided to use the linux/unix tail command rather than any file handling stuff within PHP.
Secondly we alter permissions on the log file. Finding the log itself might be a bit problematic but on mine the log files are found in the /var/log/apache2/ directory. The file needing it’s permissions changed is other_vhosts_access.log on my installation, using the command:
sudo chmod 0644 /var/log/apache2/other_vhosts_access.log
should sort that out. Doing a ls -l on the /var/log/apache2/ directory should show it as having -rw-r–r– privileges (owner read/write, group read, others read)
The log rotation may not happen on your installation but I didn’t bother to address this on mine. Once it’s rotated I just redo that permissions change for the “new” log file to re-enable reading of it.

Here’s the script…

<?php
$me = $_SERVER['REMOTE_ADDR'];
$self = $_SERVER['SERVER_ADDR'];
$output = shell_exec('tail /var/log/apache2/other_vhosts_access.log -n 500');
$stuff = explode("
",$output);
array_pop($stuff);
?>
<title>Apache Logs</title>
<style>
html {
    font-family:monospace;
}
.tbl {
        table-layout:fixed;
        border-top: 5px solid #333;
        border-collapse: collapse;
        background: #fff;
        width:100%;
         }
.tbl td {
        border-bottom: 1px dashed #333;
        padding: 2px 5px;
        text-overflow:ellipsis;
        overflow:hidden;
        white-space:nowrap;
 }
 </style>
Visits from your current IP (<?=$me?>) are highlighted in <span style="background:yellow;">yellow</span><br/>
Attempts to POST data (from other IPs) are highlighted in <span style="background:pink;">pink</span><br/>
Jobs run from the server itself are <span style="background:silver;color:grey;">greyed out</span><br/>
Showing the last <strong><?=count($stuff)?></strong> site hits.
<pre><?php// echo $output ?></pre>
<table class="tbl" border="1" style='font-family:sans-serif;'>
    <tr>
        <th width="150px">Domain</th>
        <th width="150px">IP</th>
        <th width="200px">Date</th>
        <th>Request</th>
        <th>UserAgent</th>
    </tr>
<?
    foreach($stuff as $line) {
        $linearray = explode(" ", $line);
        $array2 = explode('"', $line);
        $domain = $linearray[0];
        $ip = $linearray[1];
        $date = $linearray[4].$linearray[5];
        $request = $array2[1];
        $useragent = $array2[5];
        //var_dump($linearray);
        //var_dump($array2);
        $style = "";
        if ($ip == $me) $style = " style='background:yellow;'";
        elseif ($ip == $self) $style = " style='color:grey;background:silver;'";
        elseif ($linearray[6] == '"POST') $style = " style='background:pink;'";
        ?>
    <tr<?=$style?>>
        <td><?=$domain?></td>
        <td><?=$ip?></td>
        <td><?=$date?></td>
        <td><?=$request?></td>
        <td><?=$useragent?></td>
    </tr>
        <?
    }
?>
</table>

Which gives me output something like (click for bigger)…

Occasionally I’ll navigate to this file and it’ll be empty, saying it’s showing 0 site hits. This is after the logs have been rotated and I need to re-do the chmod command above. There’s no way I can see to script this, perhaps a cron job for root… That’s not a big enough issue for me to worry about. For my needs this works fine.

Apr
26

Javascript countdown or countup timer…

Another one for the “why does everyone make it more difficult than it needs be” section, if that section existed…

I wanted a simple countdown timer. It’s going to be used in a little thing I’m doing at blogshares and will essentially count down the time left before another event fires… All I want is a number displaying how many seconds are left before the event fires. I can prime this number myself.

The internet has a plethora of countdown timers, but they all want an actual date to end on, not just a number of seconds. So here’s my offering using jQuery, just put the number from which you want to begin in the span. Simples…
Count down:

    
    10

Count up:

    
    0

...and here they are working (I've used 100 so that by the time you get here they might still be running, if not refresh and scroll!):


Counting down: 100
Counting up: 0

Sep
21

Twitter @Anywhere PHP server-side validation

I’ve been working on a little twitter thing for blogshares. I was going to just use the regular API from twitter as I do for many other projects. However there’s one issue. Blogshares runs squid and it has issues with php5-curl. So there’s NO cURL functionality in blogshares.

I know!

Initially I was going to use readfile() instead, but that’s a huge PITA. All existing libs assume you have cURL installed(and why not!) so I’d need to write my own classes and wrappers for the API. Not immediately but eventually. Even for a simple log-in it’s just a hideous workaround, imho…*headdesk*

Step in Twitter’s @Anywhere service. A javascript library hosted on twitter that allows access to the API client-side from, well, anywhere. It gets across the usual same-origin security issues by being hosted at twitter. But there are of course huge security issues with client-based POSTS. Anyone with firebug or greasemonkey can alter the javascript/html of your page on the fly and let your app post whatever they like.

When twitter signs in a user via @Anywhere they set a cookie on your site with user details. The format is userid:signature where the signature is a sha1 encoded string based on your app’s secret key, known only to you and twitter. This means when the javascript recognises the person’s back from twitter OAuthing it can post to some script on your site…

The only example twitter has for this validation checking is for Ruby, so here’s how in PHP…

    $consumer_secret = 'YOUR APP CONSUMER SECRET';
    $twituserid = explode(":",$_COOKIE['twitter_anywhere_identity']);
    $sha = sha1("{$twituserid[0]}{$consumer_secret}");
    if ($sha === $twituserid[1])
    {
        # Successful login... handle it here
    }
    else
    {
        # Unsuccessful login... reject user.
    }

The point here is that nobody else knows your secret key except twitter. So a scripter cannot easily spoof your app and mess with someone’s twitter account claiming to be you as they can’t create that signature without it. Even if they do crack your secret key you can generate a new one on twitter in about 5 seconds!

@Anywhere is a great tool and I’m well impressed with how well it’s integrated(ing).

Older posts «