Example #1
0
File: Admin.php Project: eadz/chyrp
 /**
  * Function: import_movabletype
  * MovableType importing.
  */
 public function import_movabletype()
 {
     if (empty($_POST)) {
         redirect("/admin/?action=import");
     }
     if (!Visitor::current()->group->can("add_post")) {
         show_403(__("Access Denied"), __("You do not have sufficient privileges to import content."));
     }
     $config = Config::current();
     $trigger = Trigger::current();
     $dbcon = $dbsel = false;
     if ($link = @mysql_connect($_POST['host'], $_POST['username'], $_POST['password'])) {
         $dbcon = true;
         $dbsel = @mysql_select_db($_POST['database'], $link);
     }
     if (!$dbcon or !$dbsel) {
         Flash::warning(__("Could not connect to the specified MovableType database."), "/admin/?action=import");
     }
     mysql_query("SET NAMES 'utf8'");
     $get_authors = mysql_query("SELECT * FROM mt_author ORDER BY author_id ASC", $link) or error(__("Database Error"), mysql_error());
     $users = array();
     while ($author = mysql_fetch_array($get_authors)) {
         # Try to figure out if this author is the same as the person doing the import.
         if ($author["author_name"] == Visitor::current()->login or $author["author_nickname"] == Visitor::current()->login or $author["author_nickname"] == Visitor::current()->full_name or $author["author_url"] == Visitor::current()->website or $author["author_email"] == Visitor::current()->email) {
             $users[$author["author_id"]] = Visitor::current();
         } else {
             $users[$author["author_id"]] = User::add($author["author_name"], $author["author_password"], $author["author_email"], $author["author_nickname"] != $author["author_name"] ? $author["author_nickname"] : "", $author["author_url"], $author["author_can_create_blog"] == "1" ? Visitor::current()->group : null, $author["author_created_on"], false);
         }
     }
     $get_posts = mysql_query("SELECT * FROM mt_entry ORDER BY entry_id ASC", $link) or error(__("Database Error"), mysql_error());
     $posts = array();
     while ($post = mysql_fetch_array($get_posts)) {
         $posts[$post["entry_id"]] = $post;
     }
     foreach ($posts as $post) {
         $body = $post["entry_text"];
         if (!empty($post["entry_text_more"])) {
             $body .= "\n\n<!--more-->\n\n" . $post["entry_text_more"];
         }
         $regexp_url = preg_quote($_POST['media_url'], "/");
         if (!empty($_POST['media_url']) and preg_match_all("/{$regexp_url}([^\\.\\!,\\?;\"\\'<>\\(\\)\\[\\]\\{\\}\\s\t ]+)\\.([a-zA-Z0-9]+)/", $body, $media)) {
             foreach ($media[0] as $matched_url) {
                 $filename = upload_from_url($matched_url);
                 $body = str_replace($matched_url, $config->url . $config->uploads_path . $filename, $body);
             }
         }
         $status_translate = array(1 => "draft", 2 => "public", 3 => "draft", 4 => "draft");
         $clean = oneof($post["entry_basename"], sanitize($post["entry_title"]));
         if (empty($post["entry_class"]) or $post["entry_class"] == "entry") {
             $new_post = Post::add(array("title" => $post["entry_title"], "body" => $body, "imported_from" => "movabletype"), $clean, Post::check_url($clean), "text", @$users[$post["entry_author_id"]], false, $status_translate[$post["entry_status"]], oneof(@$post["entry_authored_on"], @$post["entry_created_on"], datetime()), $post["entry_modified_on"], "", false);
             $trigger->call("import_movabletype_post", $post, $new_post, $link);
         } elseif (@$post["entry_class"] == "page") {
             $new_page = Page::add($post["entry_title"], $body, null, 0, true, 0, $clean, Page::check_url($clean));
             $trigger->call("import_movabletype_page", $post, $new_page, $link);
         }
     }
     mysql_close($link);
     Flash::notice(__("MovableType content successfully imported!"), "/admin/?action=import");
 }