<?php
/**
  * lib-mentarisms.php
  * include dulu file ini untuk dapat menggunakan class Mentarisms
  * versi 0.01 Jumat, 23 Juni 2006
  * beta.uliansyah@gmail.com
  **/
error_reporting(1);
ini_set('max_execution_time',0);

define("MENTARISMS_URL""http://www.klub-mentari.com/index.php");
define("MENTARISMS_LOGIN""http://www.klub-mentari.com/login.php");
define("MENTARISMS_KIRIM_SMS""http://www.klub-mentari.com/sms/xt_send_sms2.php");
define("MENTARISMS_REDIRECT""sms/send_sms2.php");
define("MENTARISMS_FORMSMS""http://www.klub-mentari.com/sms/send_sms2.php");
define("MENTARISMS_REFERER""http://www.klub-mentari.com/sms/send_sms2.php");
define("MENTARISMS_LOGOUT""http://www.klub-mentari.com/logout.php");

define("MENTARISMS_USERAGENT""Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
define("MENTARI_COOKIE_PATH""./"); // consider using /tmp or /var/tmp or other world-written directory

class Mentarisms {
    var 
$username;
    var 
$password;
    var 
$mentari;

    var 
$version;
    var 
$created;
    var 
$cookie_string;
    var 
$cookie;
    var 
$proxy_host;
    var 
$proxy_auth;
    var 
$sid;
    var 
$is_login;
    var 
$sent;
    var 
$ch// curl handle
    
var $return_status = array();
    var 
$status;

    function 
Mentarisms() {
    
$this->version "0.01";
    
$this->created true;
        if (!
extension_loaded('curl')) {
            if (isset(
$_ENV["NUMBER_OF_PROCESSORS"]) and ($_ENV["NUMBER_OF_PROCESSORS"] > 1)) {
                
$this->created false;
                
$a = array(
                    
"action"         => "constructing Mentarisms object",
                    
"status"         => "failed",
                    
"message"         => "lib-Mentarisms: Using a multithread server. Ensure php_curl.dll has been enabled (uncommented) in your php.ini."
                
);
                
array_unshift($this->return_status$a);

            } else {
                if (!
dl('php_curl.dll') && !dl('curl.so')) {
                    
$this->created false;
                    
$a = array(
                        
"action"         => "constructing Mentarisms object",
                        
"status"         => "failed",
                        
"message"         => "lib-Mentarisms: unable to load curl extension."
                    
);
                    
array_unshift($this->return_status$a);
                }
            }
        }
        if (!
function_exists("curl_setopt")) {
            
$this->created false;
            
$a = array(
                
"action"         => "constructing Mentarisms object",
                
"status"         => "failed",
                
"message"         => "lib-Mentarisms: No curl."
            
);
            
array_unshift($this->return_status$a);
        }
        
$this->username $username;
        
$this->password $password;
        
$this->proxy_host "";
        
$this->proxy_auth "";
        
$this->use_session 2;
        if (
$this->created == true) {
            
$a = array(
                
"action"         => "constructing Mentarisms object",
                
"status"         => "success",
                
"message"         => "lib-Mentarisms: Constructing completed."
            
);
            
array_unshift($this->return_status$a);
        }
    }

    function 
setProxy($proxy_host$proxy_user$proxy_pass) {
        if (
strlen($this->proxy_host) > 0) {
            
$this->proxy_host $proxy_host;
            if (
strlen($proxy_user) > || strlen($proxy_pass) > 0) {
                
$this->proxy_auth $proxy_user.":".$proxy_pass;
            }
            
$a = array(
                
"action"         => "set proxy",
                
"status"         => "success",
                
"message"         => "lib-Mentarisms: Proxy set."
            
);
            
array_unshift($this->return_status$a);
        } else {
            
$a = array(
                
"action"         => "set proxy",
                
"status"         => "failed",
                
"message"         => "lib-Mentarisms: no hostname supplied."
            
);
            
array_unshift($this->return_status$a);
        }
    }

    function 
CURL_PROXY($cc) {
        
        if (
strlen($this->proxy_host) > 0) {
            
curl_setopt($ccCURLOPT_PROXY$this->proxy_host);
            if (
strlen($this->proxy_auth) > 0)
                
curl_setopt($ccCURLOPT_PROXYUSERPWD$this->proxy_auth);
        }
    }

    function 
Login($username$password){
        
$this->cookie MENTARI_COOKIE_PATH."cookie.txt";
        
$this->username $username;
        
$this->password $password;
            
        
/* looking for sid */
        
$this->ch curl_init();
        
curl_setopt($this->chCURLOPT_URLMENTARISMS_URL);
        
$this->CURL_PROXY($this->ch);
        
curl_setopt($this->chCURLOPT_HEADER1); // baca header
        
curl_setopt($this->chCURLOPT_USERAGENTMENTARISMS_USERAGENT);
        
curl_setopt($this->chCURLOPT_COOKIE$this->cookie_string);
        
curl_setopt($this->chCURLOPT_COOKIEJAR$this->cookie);
        
curl_setopt($this->chCURLOPT_COOKIEFILE$this->cookie);
        
curl_setopt($this->chCURLOPT_RETURNTRANSFER1);
        
$indexContent curl_exec($this->ch);
        
$ereg_sid ereg("PHPSESSID\=([0-9a-z]+)"$indexContent$ketemu_sid);
        
$this->sid $ketemu_sid[1];

        
/* Login */
        
curl_setopt($this->chCURLOPT_URLMENTARISMS_LOGIN);
        
$this->CURL_PROXY($this->ch);
        
curl_setopt($this->chCURLOPT_POST1);
        
curl_setopt($this->chCURLOPT_USERAGENTMENTARISMS_USERAGENT);
        
curl_setopt($this->chCURLOPT_COOKIE$this->cookie_string);
        
curl_setopt($this->chCURLOPT_COOKIEJAR$this->cookie.txt);
        
curl_setopt($this->chCURLOPT_COOKIEFILE$this->cookie.txt);
        
curl_setopt($this->chCURLOPT_FOLLOWLOCATION1);
        
curl_setopt($this->chCURLOPT_RETURNTRANSFER1);
        
$loginPost "username=".urlencode($this->username);
        
$loginPost .= "&password=".urlencode($this->password);
        
$loginPost .= "&sid=".urlencode($this->sid);
        
$loginPost .= "&redirect=".urlencode("index.php");
        
$loginPost .= "&login=Login";
        
curl_setopt($this->chCURLOPT_POSTFIELDS$loginPost);
        
$loginContent curl_exec($this->ch);
        
//$this->lihatErrorCurl($this->ch);
        //curl_close($this->ch);
    
}

    function 
kirimSms($recipient$message) {
        
// FORM SMS
        
curl_setopt($this->chCURLOPT_URLMENTARISMS_FORMSMS);
        
curl_setopt($this->chCURLOPT_POST1);
        
curl_setopt($this->chCURLOPT_USERAGENTMENTARISMS_USERAGENT);
        
curl_setopt($this->chCURLOPT_COOKIEJAR$cookiefile);
        
curl_setopt($this->chCURLOPT_RETURNTRANSFER1); // langsung return
        
curl_setopt($this->chCURLOPT_VERBOSE1); 
        
curl_setopt($this->chCURLOPT_FOLLOWLOCATION1);
        
$formSMSContent curl_exec ($this->ch);
        if(empty(
$this->mentari)) {
            
$ereg_mentari ereg("<span class\=\"genmed\">\+([0-9a-z]+)"$formSMSContent$ketemu_mentari);
            
$this->mentari $ketemu_mentari[1];
            
//echo "\$mentari = $ketemu_mentari[1]<br />";    
        
}
        
        
$this->recipient $recipient;
        
$this->message $message;
        
curl_setopt($this->chCURLOPT_URLMENTARISMS_KIRIM_SMS);
        
curl_setopt($this->chCURLOPT_REFERERMENTARISMS_REFERER);
        
$this->CURL_PROXY($this->ch);
        
curl_setopt ($this->chCURLOPT_POST1);
        
curl_setopt ($this->chCURLOPT_COOKIE$this->cookie_string);
        
curl_setopt($this->chCURLOPT_RETURNTRANSFER1);
        
$kirimPost  "recipient=".urlencode($this->recipient);
        
$kirimPost .= "&message=".urlencode($this->message);
        
$kirimPost .= "&jml_kar=125";
        
$kirimPost .= "&username=".urlencode($this->mentari);
        
$kirimPost .= "&user_id=&submit=".urlencode("Kirim SMS");
        
curl_setopt ($this->chCURLOPT_POSTFIELDS$kirimPost);
        
$kirimContent curl_exec($this->ch);
        
$this->lihatErrorCurl($this->ch);
        
//curl_close($this->ch);
        
if(ereg("sedang masuk antrian"$kirimContent)) { $this->status "terkirim"; }
        elseif(
ereg("Mohon maaf SMS yang Anda kirim hari ini sudah mencapai 10 SMS"$kirimContent)) { $this->status "over-limit"; }
        elseif(
ereg("404"$kirimContent)) { $this->status "down"; }
        else { 
$this->status "error"; }
    }

    function 
terkirim() {
        return 
$this->terkirim;
    }

    function 
debug() {
        print 
implode("<br>\n"$this->return_status);
    }
    
    function 
status() {
        return 
$this->status;
    }

    function 
lihatErrorCurl($ch) {
    if (
curl_errno($this->ch)) {
        echo 
"cURL info:"print_r(curl_getinfo($this->ch));
        echo 
"<br>";
        echo 
"cURL error number:" .curl_errno($this->ch);
        echo 
"<br>";
        echo 
"cURL error:" curl_error($this->ch);
        }
    }
}    



/*******************************************************************
Mulai baris ini, ditulis oleh The One and Only, Yeni Setiawan
contact: yenisetiawan@gmail.com (http://the.sandalian.com)
*******************************************************************/

// CURL harus aktip saat menggunakan script ini.
function get_content_of_url($url){
    
$ohyeah curl_init();
    
curl_setopt($ohyeahCURLOPT_RETURNTRANSFER1);
    
curl_setopt($ohyeahCURLOPT_URL$url);
    
$data curl_exec($ohyeah);
    
curl_close($ohyeah);
    return 
$data;
}

// Mendapatkan string yang diapit suatu tag/string tertentu
function get_between_tag($tag1,$tag2,$string) {  
    if (
eregi("$tag1(.*)$tag2"$string$out)) {  
        
$outdata $out[1];
    }  
    return 
$outdata;  


// Bersihkan noda yang membandel
function clean($txt){
    
$txt str_replace("\n","",$txt);
    
$txt str_replace("\r","",$txt);
    
$txt str_replace("\t","",$txt);
    return 
trim($txt);
}

// Ini adalah URL 21 untuk teater di Yogyakarta, belum dicoba dengan kota lain
$url_bioskop "http://21cineplex.com/theater.cfm?city_id=75";

// Let's the good work begin!
$data get_content_of_url($url_bioskop);
$data get_between_tag('<tr><th>Title</th>','<div style="text-align:right">',$data);
$datas explode("\n",$data);
foreach(
$datas as $data){
    if(
eregi('<tr class=',$data)){
        
$film[] = ucwords(strtolower(trim(strip_tags($data))));
    }
    else{
        continue;
    }
}

// '---' adalah pemisah antara isi sms dengan banner dari klub-mentari.com
$isi_sms 'Today: '.implode(", ",$film).' ---';

// Saatnya kirim SMS
$mentari = new Mentarisms();

// Tuliskan username dan password di klub-mentari.com
$mentari->Login('username-di-klubmentari.com''password-di-klubmentari.com');

// Kirimkan ke nomer henpon sodara/sodari
$mentari->kirimSms('6285643939xxx'$isi_sms);
?>