Delitwt

By guiralantoine Uploaded Nov 5, 2009
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the tr.im Jetpack Feature.
 *
 * The Initial Developer of the Original Code is
 * Guiral Antoine <guiral.antoine@gmail.com>
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */


// import clipboard from the future

jetpack.future.import("slideBar");
jetpack.future.import("storage.simple");
var storage = jetpack.storage.simple;



var theLoginDiv = "<div id=\"login\" style=\"display:none\"></div>";
var theDeliciousDiv = '<div id="deliciousLogin" ><form><fieldset><legend>Delicious login</legend><p><label>Login</label><input type="text" name="loginDelicious" value="'+storage['loginDelicious']+'"/></p><p><label>Password</label><input type="password" name="passwordDelicious" value="'+storage['passwordDelicious']+'"/></p><input type="submit"/></fieldset></form></div>';
var theTwitterDiv = '<div id="twitterLogin" ><form><fieldset><legend>Twitter login</legend><p><label>Login</label><input type="text" name="loginTwitter" value="'+storage['loginTwitter']+'"/></p><p><label>Password</label><input type="password" name="passwordTwitter" value="'+storage['passwordTwitter']+'"/></p><input type="submit"/></fieldset></form></div>';
var theBitlyDiv = '<div id="bitlyLogin" ><form><fieldset><legend>Bitly login</legend><p><label>Login</label><input type="text" name="loginBitly" value="'+storage['loginBitly']+'"/></p><p><label>API Key</label><input type="text" name="apiKey" value="'+storage['apiKey']+'"/></p><input type="submit"/></fieldset></form></div>';
//var theHTML = "<html><head><style>body{text-align: center} #trim{ color: #666; font-size: 20px; font-family: helvetica, arial, sans-serif; }</style></head><body>"+theLoginDiv+theTagsDiv+"<span id='trim'>DeliTwt</span></body></html>";
var theHTML = "<html><head><style>body{text-align: center} #trim{ color: #666; font-size: 20px; font-family: helvetica, arial, sans-serif; }</style></head><body><span id='trim'>DeliTwt</span></body></html>";





jetpack.statusBar.append({
    html: theHTML,
  
    onReady: function(doc) {
        $(doc).click(function(){

            if(storage['loginDelicious']=='' || storage['passwordDelicious']==''){
                jetpack.notifications.show({
                    title: "Delicious",
                    body: 'A field was empty. Check your sidebar',
                    icon: "http://tr.im/favicon.ico"
                })
            }else{
                if(storage['loginTwitter']=='' || storage['passwordTwitter']==''){
                    jetpack.notifications.show({
                        title: "Twitter",
                        body: 'A field was empty. Check your sidebar',
                        icon: "http://tr.im/favicon.ico"
                    })
                }else{
                    if(storage['loginBitly']=='' || storage['apiKey']==''){
                        jetpack.notifications.show({
                            title: "Bitly",
                            body: 'A field was empty. check your sidebar',
                            icon: "http://tr.im/favicon.ico"
                        })
                    }else{
                        $.post("http://buz.im/ag.jetpack.delitwt/delitwt.php",{
                            url:jetpack.tabs.focused.url,
                            title:jetpack.tabs.focused.contentDocument.title,
                            content:jetpack.tabs.focused.contentDocument.getSelection(),
                            loginDelicious:storage['loginDelicious'],
                            passwordDelicious:storage['passwordDelicious'],
                            loginTwitter:storage['loginTwitter'],
                            passwordTwitter:storage['passwordTwitter'],
                            loginBitly:storage['loginBitly'],
                            apiKey:storage['apiKey']
                        },function(data){
                            jetpack.notifications.show({
                                title: "DeliTwt",
                                body: data,
                                icon: "http://tr.im/favicon.ico"
                            });
                        })
                    }
                }
            }




        })
        

    //    var trim = new Trim(doc);
    }
});



jetpack.slideBar.append({
    onReady: function (slide) {

        loginDelicious=storage['loginDelicious'],
        passwordDelicious=storage['passwordDelicious'],
        loginTwitter=storage['loginTwitter'],
        passwordTwitter=storage['passwordTwitter'],
        loginBitly=storage['loginBitly'],
        apiKey=storage['apiKey']
        if(loginDelicious=='undefined'){
            storage['loginDelicious']='';
        }
        if(passwordDelicious=='undefined'){
            storage['passwordDelicious']='';
        }
        if(loginTwitter=='undefined'){
            storage['loginTwitter']='';
        }
        if(passwordTwitter=='undefined'){
            storage['passwordTwitter']='';
        }
        if(loginBitly=='undefined'){
            storage['loginBitly']='';
        }
        if(apiKey=='undefined'){
            storage['apiKey']='';
        }


        $(slide.contentDocument).find("form").each(function(){
            $(this).submit(function(){

                $(this).find('p>input').each(function(){
                    input=$(this)
                    inputType=input.attr('type')

                    storage[input.attr('name')]=input.val()
                })
                jetpack.notifications.show({
                    title: "DeliTwt",
                    body: 'Saved',
                    icon: "http://tr.im/favicon.ico"
                })
                return false;
            })
        })
    },
    width: 350,
    persist: true,
    html: '<body>'+theDeliciousDiv+theTwitterDiv+theBitlyDiv+'</body>'
});


Server side : 
<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require_once 'Twitter.php';
require_once 'Delicious.php';
require_once 'YQL.php';
require_once 'Bitly.php';


$url=$_POST['url'];
$content=$_POST['content'];
$title=$_POST['title'];
$loginDelicious=$_POST['loginDelicious'];
$passwordDelicious=$_POST['passwordDelicious'];
$loginTwitter=$_POST['loginTwitter'];
$passwordTwitter=$_POST['passwordTwitter'];
$loginBitly=$_POST['loginBitly'];
$apiKey=$_POST['apiKey'];

$bitly = new Bitly($loginBitly, $apiKey);
$shortUrl= $bitly->shorten($url);
if($shortUrl!=false) {
    $tags=YQL::getTags($content);
    $tags=implode(' ',$tags);
    $tags=str_replace('"', '', $tags);
    $tagsTwit=str_replace(' ', ' #',' '.$tags);

    $d = new Delicious();
    $d->setUsername($loginDelicious);
    $d->setPassword($passwordDelicious);
    if($title==''){
        $title=$url;
    }
    if($tagsTwit==' #'){
        $tagsTwit='';
    }
    $bkmarks= $d->addPost($url, $title, $tags,$content);
    if($bkmarks!=false) {
        $stringTwit=$shortUrl.' bookmarké avec delitwt ';

        $tailleStringTwit=strlen($stringTwit);

        while(strlen($tagsTwit)>(140-$tailleStringTwit-1)) {
            $tabTags=explode(' ', $tagsTwit);
            array_pop($tabTags);
            $tagsTwit=implode(' ',$tags);
        }
        $stringTwit.=$tagsTwit;
        $sendTwit=Twitter::update($loginTwitter, $passwordTwitter, $stringTwit);
        if($sendTwit!=false){
            echo $stringTwit;
        }else{
            echo 'Somethings wrong with Twitter. You\'re link was bookmarked aniway.';
        }
    }else{
        echo 'Somethings wrong with Delicious';
    }

}else {
    echo 'Somethings wrong with Bit.ly';
}




?>