<?php

/*
 *    Launchpad robot
 *
 *    version: 0.03a
 *    developed: Karoly Gossler
 *    mail: connor [at) connor (dot] hu
 *    web: blog.connor.hu
 *    licence: GPLv2 :: http://www.gnu.org/licenses/gpl.txt
 */

class launchpad
{
  public 
$verbose;

  private 
$tmp_dir '.';
  private 
$cookie_file '/cookie.lp.txt';
  private 
$email;
  private 
$password;
  public 
$mailbox_user;
  public 
$mailbox_password;
  public 
$mailbox_domain;

  private 
$main_page 'https://launchpad.net/';
  private 
$trans_domain 'https://translations.launchpad.net';

  public function 
__construct($email$password)
  {
        
$this->email $email;
        
$this->password $password;
        
$this->mailbox_password $mailbox_password;
  }

  private 
$not_logged_in_text 'Not logged in ';

  public function 
is_logged_in()
  {
    
$fetcher = new http_agent('https://launchpad.net');
    
$fetcher->save_cookies $fetcher->load_cookies $this->tmp_dir $this->cookie_file;
        
$fetcher->referer 'https://launchpad.net';
    
$fetcher->fetch();

//        print_r($fetcher->get_headers());

    
return ! (bool) strpos($fetcher->result$this->not_logged_in_text);
  }

  private 
$email_form_name 'loginpage_email';
  private 
$passwd_form_name 'loginpage_password';
  private 
$login_button_name 'loginpage_submit_login';
  private 
$login_button_value 'Log In';

  public function 
login()
  {    
    
$form_vars = array(
      
$this->email_form_name => $this->email,
      
$this->passwd_form_name => $this->password,
      
$this->login_button_name => $this->login_button_value,
      );

    
$fetcher = new http_agent('https://launchpad.net/+login');
        
$fetcher->referer 'https://launchpad.net/+login';
    
$fetcher->save_cookies $fetcher->load_cookies $this->tmp_dir $this->cookie_file;
        
$fetcher->post_data $form_vars;
    
$fetcher->fetch();

//        print_r($fetcher->result);

    
return true;
  }

  private 
$distribution null;
  public function 
set_distribution($distribution)
  {
    if (!
in_array($distribution, array('dapper''edgy''feisty''gutsy')))
    {
      return 
false;
    }
    
$this->distribution $distribution;
  }

    public function 
get_languages()
    {
        
$url $this->trans_domain .'/ubuntu/'$this->distribution .'/';
        
$list = array();

    
$fetcher = new http_agent($url);
        
$fetcher->referer 'https://launchpad.net/ubuntu/'$this->distribution .'/';
    
$fetcher->save_cookies $fetcher->load_cookies $this->tmp_dir $this->cookie_file;
    
$fetcher->fetch();
        
$content $fetcher->result;

        
$dom = new DomDocument();
        @
$dom->loadhtml($content);
        
$xpath = new Domxpath($dom);

        
$result $xpath->query('//*[@id="languagestats"]/tbody/tr/td[1]');

        for (
$i 0$i $result->length$i++)
        {
            
$name $result->item($i)->childNodes->item(1)->nodeValue;
            
preg_match('!/([a-z\_A-Z]+)$!'$result->item($i)->childNodes->item(1)->getattribute('href'), $match);
            
$key $match[1];
            
$list[$key] = $name;
        }

        return 
$list;
    }

    public function 
get_templates($source_package)
    {
        
$url $this->trans_domain .'/ubuntu/'$this->distribution .'/+source/'$source_package .'/';
    
$fetcher = new http_agent($url);
        
$fetcher->referer 'https://launchpad.net/ubuntu/'$this->distribution .'/+source/'$source_package .'/';
    
$fetcher->save_cookies $fetcher->load_cookies $this->tmp_dir $this->cookie_file;
    
$fetcher->fetch();
        
$content $fetcher->result;

//        file_put_contents('tmp', $content);

//        $content = file_get_contents('tmp');

        
preg_match_all('!Template\ \"([^\"]+)\"!'$content$matches);
        return 
$matches[1];
    }

    public function 
export_po($source_name$template_name$export_dir)
    {
        
$url $this->trans_domain .'/ubuntu/'$this->distribution .'/+source/'$source_name .'/+pots/'$template_name .'/hu/+export';

    
$fetcher = new http_agent($url);
        
$fetcher->referer $url;
    
$fetcher->save_cookies $fetcher->load_cookies $this->tmp_dir $this->cookie_file;
    
$fetcher->post_data['format'] = 'PO';
    
$fetcher->fetch();

//        print_r($fetcher->get_headers());
        
if (strpos($fetcher->result'msgid ""') !== false)
        {
            
file_put_contents($export_dir .'hu_'$source_name .'_'$template_name .'.po'$fetcher->result);
        }
        else
        {
            while (
true)
            {
                echo 
'read mail';
                echo 
NL;

                
$link $this->read_mails($source_name$template_name);
                if (
$link !== false)
                {
                    
$fetcher->url $link;
                    
$fetcher->fetch();
                    
file_put_contents($export_dir .'hu_'$source_name .'_'$template_name .'.po'$fetcher->result);
                    break;
                }
                
sleep(60*2);
            }
        }
    }

    public function 
init_mailer($server$username$password)
    {
        
$this->mailbox_user $username;
        
$this->mailbox_password $password;
        
$this->mailbox_domain $server;
    }

    private function 
read_mails($source_name$template_name)
    {
        
$mbox imap_open('{'$this->mailbox_domain .':110/pop3/notls}INBOX'$this->mailbox_user$this->mailbox_password);

        
$mboxinfo imap_mailboxmsginfo($mbox);

        for (
$i 0$i $mboxinfo->Nmsgs$i++)
        {
            if (
imap_headerinfo($mbox$i+1)->subject == 'Translation download request: '$template_name)
            {
                
preg_match('!http(.*)!'imap_body($mbox$i+1), $matches);
                
$link $matches[0];
                
imap_delete($mbox$i+1);
                
imap_expunge($mbox);
                
imap_close($mbox);
                return 
trim($link);
            }
        }

        
imap_close($mbox);
        return 
false;
    }

    public function 
get_new_uploads($list_to)
    {
        
$start 0;

        while (
true)
        {
            
$url 'https://launchpad.net/ubuntu/'$this->distribution .'/+queue?queue_state=3&queue_text=&start='$start;

            
$fetcher = new http_agent($url);
            
$fetcher->referer 'https://launchpad.net/ubuntu/feisty';
            
$fetcher->save_cookies $fetcher->load_cookies $this->tmp_dir $this->cookie_file;
            
$fetcher->fetch();
            
$content $fetcher->result;

            
$dom = new DomDocument();
            @
$dom->loadhtml($content);
            
$xpath = new Domxpath($dom);

            
file_put_contents('tmp'$content);

            if (
$list_per_page === null)
            {
                
$list_per_page '//*[@id="maincontent"]/div[2]/div/table/tbody/tr/td/strong[2]';
                
$result $xpath->query($list_per_page);
                
$list_per_page = (int) $result->item(0)->nodeValue;
            }

            
$result $xpath->query('//*[@id="distroreleasequeue"]/tbody/tr');

            for (
$i 0$i $result->length$i++)
            {
                
$package_name trim($result->item($i)->childNodes->item(2)->childNodes->item(2)->nodeValue);
                
$version trim($result->item($i)->childNodes->item(4)->nodeValue);
                
$stamp strtotime($result->item($i)->childNodes->item(8)->childNodes->item(1)->nodeValue);

                if (
$stamp $list_to)
                {
                    return 
$list;
                }

                
$list[] = array(
                    
'package_name' => $package_name,
                    
'version' => $version,
                    
'upload_time' => $stamp,
                    );
            }

            
$start += $list_per_page;
        }

        return 
$list;
    }

    public function 
get_translations()
    {
        
$url =  $this->trans_domain .'/ubuntu/'$this->distribution .'/+lang/hu';

        
$next_link '//*[@id="maincontent"]/div[2]/table/tbody/tr/td[2]/a[@rel="next"]';
        do
        {
            
$fetcher = new http_agent($url);
            
$fetcher->referer 'https://launchpad.net/ubuntu/feisty';
            
$fetcher->save_cookies $fetcher->load_cookies $this->tmp_dir $this->cookie_file;
            
$fetcher->fetch();
            
$content $fetcher->result;

            
$dom = new DomDocument();
            @
$dom->loadhtml($content);
            
$xpath = new Domxpath($dom);

            
$result $xpath->query('//*[@id="translationstatuses"]/tbody/tr');

            foreach (
$result as $item)
            {
                
$i 0;
                
// TODO: nem kell ciklus
                
foreach ($item->getelementsbytagname('td') as $cell)
                {
                    switch(
$i)
                    {
                        case 
0:
                            
$link $cell->getelementsbytagname('a')->item(0)->getattribute('href');
                            
preg_match('!\/\+source\/([^\/]+)/!'$link$matches);
                            
$name $cell->getelementsbytagname('a')->item(0)->nodeValue;
                            break;

                        case 
1:
                            
$size $cell->nodeValue;
                            break;

                        case 
3:
                            
$todo = (int) $cell->nodeValue;
                            break;
                    }
                    
$i++;
                }
                
$list[] = array(
                    
'link' => $link,
                    
'source_name' => $matches[1],
                    
'name' => $name,
                    
'size' => $size,
                    
'todo' => $todo,
                    );
            }

            
$result $xpath->query($next_link);
            if (
$result->length)
            {
                
$url $result->item(0)->getattribute('href');
            }
            else
            {
                
$url false;
            }

        } while (
$url);

        return 
$list;
    }
}

?>