<?php
require_once('tiki-setup.php');

if($tiki_p_admin != 'y') {
    $smarty->assign('msg',tra("You dont have permission to use this feature"));
    $smarty->display("styles/$style_base/error.tpl");
    die;
}


require ("lib/webmail/mimeDecode.php");

function parse_output(&$obj, &$parts,$i) {  
  if(!empty($obj->parts)) {    
    for($i=0; $i<count($obj->parts); $i++)      
      parse_output($obj->parts[$i], $parts,$i);  
  }else{    
    $ctype = $obj->ctype_primary.'/'.$obj->ctype_secondary;    
    switch($ctype) {    
      case 'application/x-phpwiki':
         $aux["body"] = $obj->body;  
         $ccc=$obj->headers["content-type"];
         $items = split(';',$ccc);
         foreach($items as $item) {
           $portions = split('=',$item);
           if(isset($portions[0])&&isset($portions[1])) {
             $aux[trim($portions[0])]=trim($portions[1]);
           }
         }
         
         
         $parts[]=$aux;
         break;
      case 'application/x-tikiwiki':
         $aux["body"] = $obj->body;  
         $ccc=$obj->headers["content-type"];
         $items = split(';',$ccc);
         foreach($items as $item) {
           $portions = split('=',$item);
           if(isset($portions[0])&&isset($portions[1])) {
             $aux[trim($portions[0])]=trim($portions[1]);
           }
         }
         $parts[]=$aux;
         break;
         
    }  
  }
}

function compare_import_versions($a1,$a2) {
  return $a1["version"]-$a2["version"];
}


$smarty->assign('result','y');
if(isset($_REQUEST["import"])) {
	check_ticket('import-phpwiki');
$path = $_REQUEST["path"];
$h=opendir("$path/");
$lines=Array();
while(false !== $file=readdir($h)) {
  if(is_file("$path/$file")) {
    $fp=fopen("$path/$file","r");
    $full=fread($fp,filesize("$path/$file"));
    //$full=htmlspecialchars($full);
    fclose($fp);
    $params = array('input' => $full,
                  'crlf'  => "\r\n", 
                  'include_bodies' => TRUE,
                  'decode_headers' => TRUE, 
                  'decode_bodies'  => TRUE
                  );  

    $output = Mail_mimeDecode::decode($params);    
	parse_output($output, $parts,0);  
    usort($parts,'compare_import_versions');
    $last_part='';
    $last_part_ver=0;
    foreach($parts as $part) {
      if($part["version"]>$last_part_ver) {
        $last_part_ver=$part["version"];
        $last_part=$part["body"];
      }
      if(isset($part["pagename"])) {
        // Parse the body replacing links to Tiki links
        
        $part["body"]=preg_replace("/ (http:\/\/[^ ]+) /"," [$1] ",$part["body"]);
        $part["body"]=preg_replace("/\[(http:\/\/[^\]]+)\]/","{img src=$1}",$part["body"]);
        $part["body"]=preg_replace("/\[([^\|\]]+)\]/","(($1))",$part["body"]);
        //$part["body"]=preg_replace("/\[([^\|]+)\|([^\]]+)\]/","(($1|$2))",$part["body"]);
        $part["body"]=preg_replace("/\[([^\|]+)\|([^\]]+)\]/","(($2|$1))",$part["body"]);


        $pagename=urldecode($part["pagename"]);
        $version=urldecode($part["version"]);
        $author=urldecode($part["author"]);
        $lastmodified=$part["lastmodified"];
        if(isset($part["description"])) {
          $description = $part["description"];
        } else {
          $description = '';
        }
        $authorid=urldecode($part["author_id"]);
        if(isset($part["hits"])) {
          $hits=urldecode($part["hits"]);
        } else {
          $hits = 0;
        }
        $ex=substr($part["body"],0,25);
        //print(strlen($part["body"]));
        $msg='';
        if($_REQUEST["remo"]=='y') {
          $tikilib->remove_all_versions($pagename,'');    
        }
        if($tikilib->page_exists($pagename)) {
          if($_REQUEST["crunch"]=='n') {
            $msg='<b>'.tra('page not added (Exists)').'</b>';
          } else {
            $msg='<b>'.tra('overwriting old page').'</b>';
            $tikilib->update_page($pagename,$part["body"],tra('updated by the phpwiki import process'),$author,$authorid,$description);
          }
        } else {
          $msg=tra('page created');
          $tikilib->create_page($pagename,$hits,$part["body"],$lastmodified,tra('created from phpwiki import'),$author,$authorid,$description);
        }
        $aux["page"]=$pagename;
        $aux["version"]=$version;
        $aux["part"]=$ex;
        $aux["msg"]=$msg;
        
        $lines[]=$aux;
      }
    }
    
    unset($parts);
  }
}
closedir($h);
$smarty->assign('lines',$lines);
$smarty->assign('result','y');
}

ask_ticket('import-phpwiki');
	
$smarty->assign('mid','tiki-import_phpwiki.tpl');
$smarty->display("styles/$style_base/tiki.tpl");

?>
