<?php

/**
  * BLOG:CMS: PHP/MySQL Personal Content Management System (CMS)
  * http://blogcms.com/
  * ----------------------------------------------------------------
  *
  * Copyright (C) 2003-2005 Radek HULÁN
  * http://hulan.cz/contact/
  *
  * ----------------------------------------------------------------
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
**/

?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='cs' lang='cs'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<link rel='stylesheet' type='text/css' href='../skins/base/extreme.css' media='screen,projection' />
<title>RADEK HULÁN: Import articles from EasyBlog</title>
<style type='text/css'>
 form{text-align:right}
</style>
</head>
<body>
<div id='content'> <!-- start content -->
    <div id='menu' style='margin-top:10px'> 
      <p style='text-align:right'>Tato utilitka Vám umožní importovat data <a href='http://elka.cz/easyblog/'>EasyBlogu</a> do <a href='http://blogcms.com'>BLOG:CMS</a>.</p>
    </div>
    <div id='items' style='margin-top:10px'>
      <h1>Import dat</h1>

<?php

  global $_FILES;
  include ('../cfg.php');
  if (isset($_POST['import'])) {
    $filename = $_FILES['uploadfile']['tmp_name'];
    $arr=file($filename);
    echo "<h3>Pracuji na importu dat...</h3>";
    $types=array('***URL','***DATE','***TIME','***TITLE','***CATEGORY');
    $body='';
    $i=1;
    $kodovani=$_POST['kodovani'];
    foreach ($arr as $s) {
      $s=iconv($kodovani,'utf-8',$s);
      if (!(strpos($s,'***START')===false)) {
        $article=array();
        $body='';
      }
      foreach($types as $type) 
        if (!(strpos($s,$type)===false))
          if (!isset($article[$type])) $article[$type]=substr($s,strpos($s,$type)+strlen($type));
      if (!(strpos($s,'***BODY')===false)) {
        $body=' ';
        $s=substr($s,strpos($s,'***BODY')+7);
      }
      if (!(strpos($s,'***END')===false)) {
          // write article
          echo "<p>".$i++.". ".$article['***TITLE']."</p>";
          ereg("^([0-9]+)([\. ]+)([0-9]+)([\. ]+)([0-9]+)$",trim($article['***DATE']),$datum);
          ereg("^([0-9]+)([: ]+)([0-9]+)$",trim($article['***TIME']),$cas);
          $time = mktime($cas[1],$cas[3],0,$datum[3],$datum[1],$datum[5]);
          if ($time<365*30*24*60*60) $time=time();
          $timestamp = date('Y-m-d H:i:s',$time);
          $title=addslashes(trim($article['***TITLE']));
          $body=addslashes($body);
          // kategorie
          $category=trim($article['***CATEGORY']);
          $cid=0;
          if (!(strpos($category,'>')===false)){
            $category=substr($category,strpos($category,'>')+1);
            if (!(strpos($category,'<')===false)) $category=substr($category,0,strpos($category,'<'));
          }
          $category=addslashes(trim(strip_tags($category)));
          if ($category=='') $category='General';
          $query=sql_query("select catid from ".sql_table('category')." where cname='$category'");
          if ($row=sql_fetch_object($query)) {
            $cid=$row->catid;
          } else {
            $query="insert into ".sql_table('category')." (cblog,cname,cdesc) values (1,'$category','$category')";
            sql_query($query);
            $cid=sql_insert_id();
          } 
          $query="insert into ".sql_table('item').
             " (ititle, ibody, iblog, iauthor, itime, iclosed, idraft, icat) ".
             " VALUES( ".
             "'$title','$body',1,1,'$timestamp',0,0,".strval($cid).")";
          sql_query($query);
      }
      if ($body!='') $body.=$s;
    }
    sql_close();
    echo "<h1>Import ukončen!</h1>";
  }
?>

      <h3>1. Příprava</h3>
      <p>V EasyBlogu proveďte export Vašich článků s následující template.htm šablonou:</p>
      <pre>&lt;!-- %CONTENT_START% --&gt;
***START
***DATE&lt;!-- %DATE% --&gt;
***TIME&lt;!-- %TIME% --&gt;
***CATEGORY&lt;!-- %CATEGORY% --&gt;
***TITLE&lt;!-- %TITLE% --&gt;
***BODY&lt;!-- %CONTENT% --&gt;
***END
&lt;!-- %CONTENT_END% --&gt;</pre>

      <h3>2. V BLOG:CMS nemusíte vytvářet kategorie. </p>
      
      <p>Tento import skript vytvoří kategorie automaticky, a potom zařadí články do správných kategorií.</p>

      <h3>3. Vyberte si tento soubor</h3>
      <form method="post" enctype="multipart/form-data" action="easyblog.php">
      <div><fieldset><legend>Nastavení importu</legend>
        <p>Soubor ke transferu: 
          <input name="uploadfile" type="file" size="40" />
        </p>
        <p><label for='idkodovani'>Kódování tohoto souboru: </label>
           <select name='kodovani' id='idkodovani'>
             <option value='CP1250' />CP1250</option>
             <option value='ISO-8859-2' />ISO-8859-2</option>
             <option value='UTF-8' />UTF-8</option>
           </select>
        </p>
        <p>
         <input type='submit' name="import" value='Importuj to!' />
        </p>
      </fieldset>
      </div></form>

    </div>
</div> <!-- end content -->
</body></html>
