Ajax CSV upload to a Table with jQuery and PHP

So I had to write a new user importer for School Email and I was looking for a simple plugin in solution but didn’t find one so here is a simple way of uploading a csv file that is uploaded to a file and rendered to a table..

Projects used: CsvToTable & jQuery File Upload & Editable Grid *optional for live edits.

Grab those two projects, grab the below files and put them in a /js folder then put the .js files into your head..

Put the jQuery-File-Upload folder in your root, we will be uploading files to here.. You may want to add a layer of security in front of the upload function.

<head>
<script src="/js/csvToTable.js"></script>
<script src="/js/jquery.iframe-transport.js"></script>
<script src="/js/jquery.fileupload.js"></script>
</head>

At the bottom of your body add..

<div id="csvToTable"></div>
<div id="fileupload"></div>
<script>
var loggedIn = 1; // you will want to modify this if you require auth
$(function () {
  if(loggedIn == "1"){
    $('#fileupload').fileupload({
      dataType: 'json',
      acceptFileTypes: '/(csv)$/i',
      url: '/jQuery-File-Upload/php/index.php',
      done: function (e, data, Users) {
        $.each(data.result, function(index, file){
          $('#csvToTable').html("");
          $('#csvToTable').CSVToTable(file.url).bind("loadComplete",function() {
            console.log("Loaded data into table");
            /*

            Call functions to manipulate data here

            */
          });;
        });
      }
    });
  }
});
</script>

Want to make your table editor like the rest of the cool kids? Check out this jQuery plugin

Transfer videos from Youtube to Vimeo

Tomnomnom made a new set of scripts for automatically uploading from the Linux shell to Vimeo.

Use the scripts to create a super simple way of uploading videos you have downloaded from Youtube (see below) to write straight into Vimeo automatically..

Get started now!

Enjoy! 🙂

UPDATE: BELOW IS THE OLD INFORMATION LEFT FOR HISTORICAL PURPOSES.



Lately Iv’e felt a bit paranoid giving Google even control of my digital domain so I wanted to move all my videos to Vimeo and to keep a local copy for me. Google makes it easy for you to individually download your video files but I wanted to bulk copy them from my youtube account to my local box and at the same time upload them to vimeo…

This very rough and buggy PHP that allows you to move all of YOUR videos from Youtube to Vimeo. You should not use this script to steal anyones content. You should not use it for malicious purposes. The script is completely legal if used appropriately, it does not violate any of Google’s or Vimeo TOS. If you do use this to upload your files to Vimeo you should delete your videos from youtube as to avoid duplicate content.

Prereq’s & installation

1. php5 & curl..
2. php.ini for Apache w/ a large memory setting. edit memory setting in /etc/php5/apache2/php.ini or whatever
3. ssh/shell access (im too lazy to write a web front end <– scrap that I wrote a web front end cause I’m super cool.)..
4. Your videos must have a 720p download available else this is just a waste of everyones times. Let’s make video on the web better ey?
5. You get a Vimeo API key. http://vimeo.com/api/applications/new — When registering you will need to set it as a Write application and set a Callback URL as FullURL/vimeo/authenticate.php —
You also want to request perms to upload via — This can take up to a few days. After registration is click click Upload Access – request, then copy your consumer key and secret to the below options
6. Visit a URL where you extracted the files.

How it works

1. Query your YouTube account, gets a list of all videos
2. For each video get the ID, metadata (name, description etc.) and store that in an array
3. Get the video file for this Video
4. Upload the video file and metadata to Vimeo via the API
5. Return to Step 2, it’s a funking loopsi.

Limitations, because there be many

1. Unicorn rainbows are limited to 127 per session.
2. You can’t exclude certain videos
3. You can only do 50 videos at a time. // run it again w/ a higher value in the start-index if you want to do more than 50.
4. First upload fails right now, just a bug..
5. Breaks upload on many characters, just requires some encoding

Grab the source code here

Run by visiting /youtube/index.php

Note: Vimeo has a daily limit on the upload API, I think it’s ~10 videos a day..

Profanity filtering in AjaxChat

AjaxChat provide a Client side profanity filter but this is not cool because:

a) Big arrays of profanities on your client is going to kill javascript
b) You are going to be passing your users an array of profanities.

Fix? Do it server side. giggidy. Jump into /ajaxchat/lib/class/AJAXChat.php

Just above the insertCustomMessage function add

 function filterMessageText($text){
    $prof = array("badword1","badword2"); // extend this with your bad words..
    $replace = array();
    foreach ($prof as $word){
       $replace[] = str_repeat('*', strlen($word));
    }
    return str_ireplace($prof, $replace, $text);
  }

Go to line 1498 and add:

// Copies the original text to a value we can store in a separate table if we want to.
   $original_text = $text;
// Pass the text through the filter.
          $text = $this->filterMessageText($text);

Adding location awareness to Ushahidi

This post is for site admins who want to add location awareness to their ushahidi deployment. Location awareness allows ushahidi to know where the visitor is coming from and display them a map local to them. It requires the user to have an ip v4 address. It is not the best solution for this problem but for now it works…

First things first, visit your application/controllers/main.php and comment out these lines

$this->themes->js->latitude = Kohana::config('settings.default_lat');
$this->themes->js->longitude = Kohana::config('settings.default_lon');

Paste this below the lines you have commented out:

include_once 'DetectLocation.php'; // this isn't the best way but it works..
$this->themes->js->latitude = $lat;
$this->themes->js->longitude = $lng;

Next visit ipinfodb.com and grab their mysql database, extract it then import it into your mysql deployment, I used the database name ipinfo.

Create a new php file called DetectLocation.php in the root of your Ushahidi deployment.

Paste this into it

<?php
// Using data from http://ipinfodb.com/ -- this should be in the ipinfo database
// Code by John McLear, all free licenses attached.  Please re-use, modify & redistribute 

$dbuser = 'ipinfo';
// using a psuedo/differnet user because I'm cool like that.
$dbpass = '';
$dbhost = 'localhost';
$dbname = 'ipinfo';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

mysql_select_db($dbname) or die('Could not select database');

$ipAddress = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
// Get IP Addr of visitor

$query = "SELECT * FROM `ip_group_city` " .
         "WHERE `ip_start` <= INET_ATON( '$ipAddress' ) " .
         "ORDER BY ip_start DESC " .
         "LIMIT 1";

$resultLocation = mysql_query($query);

while ($row = mysql_fetch_array($resultLocation))
{
        $lat = $row['latitude'];
        $lng = $row['longitude'];
}
?>

Test it by visiting your Ushahidi deployment, your map should be centred on your location.

So quick recap. We are using a local mysql database to lookup locations by IP. This is fast, fast is good. It isn’t perfect. Perfect would be better. Feel free to improve upon, change/correct..

Note: You probably also want to do the same changes on reports.php and alerts.php