Friday, April 27, 2012

Cara Buat Fitur Mute

Setelah saya menggunakan fitur mute yang sudah saya share sebelumnya, banyak sekali terjadi perubahan di twitter client saya, yang paling utama adalah url shorted berubah menjadi http://t.co semua, ini hal yang sederhana tapi saya pribadi tidak menginginkan fitur yang saya beri menjadi tidak berfungsi semestinya, nah dari situ saya menemukan triknya, agak pusing tapi menyenangkan...

Pertama, kamu buat file mute.php dan taruh di dalam folder common, isi file mute.php dengan code di bwah ini

code:

<?php

function muted_page($query) {
/*
mute landing page to show the mutedlist account
thanks for your respect to not change any information here :)
*/

$content = "<p>Muted Feature is help you to mute an annoying tweets from your timeline <img src='/smile/mv/sip.gif'><br/>";
$content .= "<br/>Just go to user profile page and click <b>`mute`</b> button to mute him/her tweet <img src='/smile/mv/ketawa.gif'>";
$content .= "</p>";

$act = $_REQUEST['act'];
$screen_name = $_REQUEST['user'];

if ($act != "" && $act != NULL) {
mute_save($screen_name,$act);
header('Location: '. BASE_URL . "mute");exit();

} else {
$muted_list = mute_read();
$content .= "<ul>";
foreach ($muted_list as $item) {
$content .= "<li>@$item&nbsp;";
$content .= "<a href='mute/?act=unmute&user={$item}'>Unmute</a></li>";
}
$content .= "</ul>";
theme('page', 'Muted List', $content);
}
}

function sensor($tl) {
/*
lembaga sensor tweets
here we censored any tweeps from mutedlist account
*/

$muted_list = mute_read();
$new_tl = array();

foreach ($tl as $item) {
$user = strtolower($item->from->screen_name);
if (!in_array($user,$muted_list)) $new_tl[strval($item->id)] = $item;
}
$hsl = json_decode(json_encode($new_tl));
return $hsl;
}

function mute_read() {
/* mutelist reader */

$muted_list = unserialize(base64_decode($_COOKIE['muted']));
if (!$muted_list) { return array();}
else { return json_decode($muted_list); }
}

function mute_save($new,$act) {
/* mutelist writer */

$new = strtolower($new);
$muted_list = mute_read();
if ($act == "mute") {
if (!in_array($new,$muted_list)) array_push($muted_list,$new);
} elseif ($act == "unmute") {
$tmp = array();
foreach ($muted_list as $item) {
if ($item != $new) array_push($tmp,$item);
}
$muted_list = $tmp;
}
$muted_list = json_encode($muted_list);
$duration = time() + (3600 * 24 * 365);
setcookie('muted', base64_encode(serialize($muted_list)), $duration, '/');
}

/* Remove mutedlist cookies when user logout */

if (!isset($GLOBALS['user'])) {
if(!array_key_exists('USER_AUTH', $_COOKIE)) {
$duration = time() - 3600;
setcookie("muted", NULL, $duration, '/');
setcookie("muted", NULL, $duration);
}
}

?>


Langkah selanjutnya, masuk ke file /common/twitter.php, tambahkan :

code pemanggilan file mute.php dengan cara menambahkan require 'mute.php'; di bagian awal file setelah code php

tambahkan code di dalam menu_register(array(

code:

'mute' => array(
'callback' => 'muted_page',
'security' => true,
), 


masih di dalam file yang sama, cari function twitter_home_page() dan pada code 

code:

$tl = twitter_standard_timeline($tl, 'friends');


danti dengan

code:

$tl = sensor(twitter_standard_timeline($tl, 'friends')); 


masih di dalam file yang sama, cari function theme_user_header($user) , tambahkan kode di bawah ini :

code:

if ($user->following !== false) {
$muted_list = mute_read();
if (in_array(strtolower($user->screen_name),$muted_list)) {
$out .= " | <a href='/mute/?act=unmute&user={$user->screen_name}'>Unmute</a>";
} else {
$out .= " | <a href='/mute/?act=mute&user={$user->screen_name}'>Mute</a>";
}
}


sebelum kode

code:

$out .= " | <a href='confirm/spam/{$user->screen_name}/{$user->id}'>Report Spam</a>";
$out .= " | <a href='search?query=%40{$user->screen_name}'>Search @{$user->screen_name}</a>";
$out .= "</div></div>";
return $out;
}


dan kalau bisa, jika agan menggunakan Embedly. tolong di cabut ya api keynya.
soalnya bentrok sama fitur ini....

Who's Online Dabr

[buat twitter who is online]

buat file count.php di root

Code:
<?php
error_reporting(E_ERROR | E_PARSE);
$dataFile = "onlineusers.txt";
if (user_is_authenticated()) {
    $user = user_current_username();
}
// this is the time in **minutes** to consider someone online before removing them from our file
// berapa menit tenggang waktu yg dibutuhkan untuk tahu user masih online atau tidak.
$sessionTime = 5;
if(!file_exists($dataFile)) {
    $fp = fopen($dataFile, "w+");
    fclose($fp);
}
$users = array();
$onusers = array();
// check up
$fp = fopen($dataFile, "r");
flock($fp, LOCK_SH);
while(!feof($fp)) {
    $users[] = rtrim(fgets($fp, 32));
}
flock($fp, LOCK_UN);
fclose($fp);
// clean up
$x = 0;
$alreadyIn = FALSE;
foreach($users as $key => $data) {
    list(,$lastvisit) = explode("|", $data);
    if(time() - $lastvisit >= $sessionTime * 60) {
        $users[$x] = "";
    } else {
        if(strpos($data, $user) !== FALSE) {
            $alreadyIn = TRUE;
            $users[$x] = "$user|" . time(); //updating
        }
    }
    $x++;
}
if($alreadyIn == FALSE) {
    $users[] = "$user|" . time();
}
// write up
$fp = fopen($dataFile, "w+");
flock($fp, LOCK_EX);
$i = 0;
foreach($users as $single) {
    if($single != "") {
        fwrite($fp, $single . "\r\n");
        $i++;
    }
}
flock($fp, LOCK_UN);
fclose($fp);
?>
edit file common/theme.php, tambahkan code :

Code:
if (user_is_authenticated()) {
        require_once("count.php");
    }
echo    '</body>
        </html>';
    exit();

sesudah
       
         </head>
         <body>';

      echo $body;
buat file online.php di root

Code:
<?php
$myFile = "onlineusers.txt";
$fsc = file($myFile);
$lines = count(file($myFile));
$content = "<div>".$lines." Online Users:<br />";
foreach($fsc as $line) {
    $array = explode("|", $line);
    $content .= $array[0] ."</a><br />";
}
$content .= "</div>";

?>
edit index.php

tmbahkan code 

Code:
'online' => array (
        'security' => true,
        'callback' => 'online_page',
    ),

di menu_register(


tambahkan juga function online page.
Code :

function online_page() {
    require_once("online.php");
    theme('page', 'Online Users', $content);
}
file onlineusers.txt akan otomatis terbuat. Chmod ke 777

kalo g otomatis tinggal buat file onlineusers.txt di root dan chmod ke 777

Mengatasi 404 Not Found dengan .htaccess d

Coba buat file .htaccess di folder root (/public_html/folder_tempat_script_dabr_yg_agan_simpan)

File .htaccess saya seperti ini
PHP Code:
# URL rewriting RewriteEngine on
RewriteCond 
%{REQUEST_FILENAME} !-f
RewriteCond 
%{REQUEST_FILENAME} !-d
RewriteRule 
^(.*)$ index.php?q=$[L,QSA]

<
IfModule header_module>
    
# 1 week cache
    
<FilesMatch "\.(png|ico)$">
    
Header set Cache-Control "max-age=604800, public"
    
</FilesMatch>
</
IfModule>  
Mudah2an bisa jalan, gan. Cmiwww...

Mengedit Footer

edit file common/theme.php
Jadi seperti ini
PHP Code:
....
....
function 
theme_page($title$content) {
        
$body theme('logo');
        
$body .= theme('menu_top');
        
$body .= $content;
        
$body .= theme('footer');
        if (
DEBUG_MODE == 'ON') {
                global 
$dabr_start$api_time;
                
$time microtime(1) - $dabr_start;
                
$body .= '<p>Processed in '.round($time4).' seconds ('.round($api_time $time 100).'% waiting for Twitter\'s API)</p>';
        }
        if (
$title == 'Login') {
                
$title 'mobile Twitter client';
                
$meta '<meta name="description" content="another mobile Twitter client" />';
        }
        
ob_start('ob_gzhandler');
        
header('Content-Type: text/html; charset=utf-8');
        echo    
'<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml">
                        <head>
                                <title>'
,Mikarai,' / ',$title,'</title>
                                <base href="'
,BASE_URL,'" />
                                '
.$meta.theme('css').'
                                <meta name="viewport" content="width=device-width; initial-scale=1.0;" />
                                <script type="text/javascript">'
.file_get_contents('js/ga.js').'</script>
                        </head>
                        <body>'
,  $body'</body>
                </html>'
;
        exit();
}
......
......
......
......

function 
theme_google_analytics() {
        global 
$GA_ACCOUNT;
        if (!
$GA_ACCOUNT) return '';
        
$googleAnalyticsImageUrl googleAnalyticsGetImageUrl();
        return 
"<img src='{$googleAnalyticsImageUrl}' />";
}

function 
theme_footer() {
        
$out '<div class="menu"><center><a href="followers">Followers</a> | <a href="/friends">Friends</a> | <a href="/lists">Lists</a> | <a href="/trends">Trends</a> | <a href="/search">Search</a> | <a href="/profile">Profile</a> | <a href="/about">About</a> | <a href="/settings">Settings</a><center><center><p><p><b>© Mikarai</b>.</center><b></p></p></div>';
    return 
$out;
}
?>  
Edit sesuka hati agan saja.. 

Menghilangkan About atau mengedit Menu di HomePage

Buka file common/user.php
Cari code ini
PHP Code:
function user_ensure_authenticated() {
    if (!
user_is_authenticated()) {
        
$content theme('login');
        
$content .= file_get_contents('about.html');
        
theme('page''Login'$content);
    }
}  
Cukup hilangkan aja kode
PHP Code:
$content .= file_get_contents('about.html');  

Mengatasi masalah Twitter Time Out

coba ganti: define('API_URL','http://api.twitter.com');
dengan yang ini: define('API_URL','http://api.twitter.com/1/');

ganti di config.php

 

Copyright © Zaki's Blog.

Managed by FHMZK