Advertisement
Guest User

Untitled

a guest
May 10th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.64 KB | None | 0 0
  1. <?php
  2.  
  3. class Database
  4. {
  5.    
  6.     private $host;
  7.     private $user;
  8.     private $pass;
  9.     private $schema;
  10.     private $prefix;
  11.     private $conn; 
  12.  
  13.     public function __construct ($host, $user, $pass, $schema, $prefix='')
  14.     {
  15.         $this->host = $host;
  16.         $this->user = $user;
  17.         $this->pass = $pass;
  18.         $this->schema = $schema;
  19.         $this->prefix = $prefix;
  20.        
  21.         $this->conn = mysql_connect ($host, $user, $pass, true);
  22.         mysql_select_db($schema, $this->conn);
  23.     }
  24.    
  25.     public function prefix ($tableName)
  26.     {
  27.         return ($this->prefix . $tableName);
  28.     }
  29.    
  30.     public function query ($query)
  31.     {
  32.         $result = mysql_query($query, $this->conn);
  33.         if (!$result)
  34.             die (__("<strong>ERROR</strong>: MySQL Error: " . mysql_error($this->conn) . "<br />SQL query: " . $query));
  35.        
  36.         return $result;
  37.     }
  38.    
  39.     /* Returns a nested array of all the rows for a database query
  40.    */
  41.     public function getResults ($query)
  42.     {
  43.         $result = $this->query($query);
  44.         $output = array();
  45.        
  46.         while ($row = mysql_fetch_assoc($result))
  47.         {
  48.             array_push($output, $row);
  49.         }
  50.        
  51.         return $output;
  52.     }
  53.    
  54.     /* Returns a single value for a database query
  55.    * If the query yields multiple rows, return the value from the first field of the first row
  56.    */
  57.     public function getScalar ($query)
  58.     {
  59.         $result = $this->query($query);
  60.        
  61.         $output = mysql_fetch_row($result);
  62.         return $output[0];
  63.     }
  64.    
  65.     public function tableExists ($table)
  66.     {
  67.         $query = sprintf("SHOW TABLES FROM `%s` LIKE '%s'", $this->schema, $this->prefix($table));
  68.         $result = $this->query($query);
  69.        
  70.         if (mysql_num_rows($result) == 1)
  71.             return true;
  72.         else
  73.             return false;
  74.     }
  75.        
  76. } // Database
  77.  
  78. class User
  79. {
  80.     private $user_id;
  81.     private $user_login;
  82.     private $user_pass;
  83.     private $user_nicename;
  84.     private $user_email;
  85.     private $user_url;
  86.     private $user_registered;
  87.     private $user_activation_key;
  88.     private $user_status;
  89.     private $display_name;
  90.     private $meta;
  91.        
  92.     public function __construct ($name, $realname, $password, $email, $url, $note, $role)
  93.     {
  94.         $this->user_login = $name;
  95.         $this->user_pass = $password;
  96.         $this->user_nicename = $name;
  97.         $this->user_email = $email;
  98.         $this->user_url = $url;
  99.         $this->user_registered = date("Y-m-d H:i:s");
  100.         $this->user_activation_key = '';
  101.         $this->user_status = '0';
  102.         $this->display_name = $name;
  103.        
  104.         $this->meta["nickname"] = $name;
  105.         $realname = explode(' ', $realname, 2);
  106.         if ($realname[0] != "")
  107.             $this->meta["first_name"] = $realname[0];
  108.         if ($realname[1] != "")
  109.             $this->meta["last_name"] = $realname[1];
  110.         if ($note != "")
  111.             $this->meta["description"] = $note;
  112.        
  113.         $this->meta["capabilities"] = serialize(array($role => 1));
  114.         switch ($role)
  115.         {
  116.             case "administrator":
  117.                 $this->meta["user_level"] = 10;
  118.                 break;
  119.             case "editor":
  120.                 $this->meta["user_level"] = 7;
  121.                 break;
  122.             case "author":
  123.                 $this->meta["user_level"] = 2;
  124.                 break;
  125.             case "contributor":
  126.                 $this->meta["user_level"] = 1;
  127.                 break;
  128.             case "subscriber":
  129.                 $this->meta["user_level"] = 0;
  130.                 break;
  131.         }      
  132.     }
  133.    
  134.     public function writeToWp ($database)
  135.     {
  136.         // Insert the new user
  137.         $query = sprintf("INSERT INTO `%s` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_url`, `user_registered`, `user_activation_key`, `user_status`, `display_name`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $database->prefix("users"), addslashes($this->user_login), addslashes($this->user_pass), addslashes($this->user_nicename), addslashes($this->user_email), addslashes($this->user_url), addslashes($this->user_registered), addslashes($this->user_activation_key), addslashes($this->user_status), addslashes($this->display_name));
  138.         $database->query($query);
  139.        
  140.         // Get the user's ID
  141.         $query = sprintf("SELECT `ID` FROM `%s` WHERE `user_login`='%s'", $database->prefix("users"), $this->user_login);
  142.         $this->user_id = $database->getScalar($query);
  143.        
  144.         // Insert the user's meta information
  145.         foreach ($this->meta as $key => $value)
  146.         {
  147.             switch ($key)
  148.             {
  149.                 case "capabilities":
  150.                 case "user_level":
  151.                     $key = $database->prefix($key);
  152.                     break;
  153.                 default:
  154.                     break;
  155.             }
  156.            
  157.             $query = sprintf("INSERT INTO `%s` (`user_id`, `meta_key`, `meta_value`) VALUES ('%s', '%s', '%s')", $database->prefix("usermeta"), $this->user_id, addslashes($key), addslashes($value));
  158.             $database->query($query);
  159.         }
  160.        
  161.         // Return the new user's ID
  162.         return $this->user_id;     
  163.     }
  164. } // Author
  165.  
  166. class Category
  167. {
  168.     private $cat_ID;
  169.     private $cat_name;
  170.     private $category_nicename;
  171.     private $category_description;
  172.     private $category_parent;
  173.     private $category_count;
  174.     private $link_count;
  175.     private $posts_private;
  176.     private $links_private;
  177.    
  178.     private $posts;
  179.     private $links;
  180.    
  181.     public function __construct ($name, $description, $parent=0)
  182.     {
  183.         $this->cat_name = $description;
  184.         $this->category_nicename = strtolower($name);
  185.         $this->category_description = '';
  186.         $this->category_parent = $parent;
  187.         $this->category_count = 0;
  188.         $this->link_count = 0;
  189.         $this->posts_private = 0;
  190.         $this->links_private = 0;
  191.         $this->posts = array();
  192.         $this->links = array();
  193.     }
  194.    
  195.     public function writeToWp ($database)
  196.     {
  197.         // Insert the category
  198.         $query = sprintf("INSERT INTO `%s` (`cat_name`, `category_nicename`, `category_description`, `category_parent`, `category_count`, `link_count`, `posts_private`, `links_private`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $database->prefix("categories"), addslashes($this->cat_name), addslashes($this->category_nicename), addslashes($this->category_description), addslashes($this->category_parent), addslashes($this->category_count), addslashes($this->link_count), addslashes($this->posts_private), addslashes($this->links_private));
  199.         $database->query($query);
  200.        
  201.         // Get the new category ID
  202.         $query = sprintf("SELECT `cat_ID` FROM `%s` WHERE `category_nicename` = '%s'", $database->prefix("categories"), addslashes($this->category_nicename));
  203.         $this->cat_ID = $database->getScalar($query);
  204.        
  205.         return $this->cat_ID;
  206.     }
  207.    
  208. }
  209.  
  210. class Post
  211. {
  212.     private $ID;
  213.     private $post_author;
  214.     private $post_date;
  215.     private $post_date_gmt;
  216.     private $post_content;
  217.     private $post_title;
  218.     private $post_category;
  219.     private $post_excerpt;
  220.     private $post_status;
  221.     private $comment_status;
  222.     private $ping_status;
  223.     private $post_password;
  224.     private $post_name;
  225.     private $to_ping;  
  226.     private $pinged;
  227.     private $post_modified;
  228.     private $post_modified_gmt;
  229.     private $post_content_filtered;
  230.     private $post_parent;
  231.     private $guid;
  232.     private $menu_order;
  233.     private $post_type;
  234.     private $post_mime_type;
  235.     private $comment_count;
  236.    
  237.     public function __construct ($title, $urltitle, $body, $more, $author, $time, $offset, $closed, $draft, $cat)
  238.     {
  239.         $this->post_author = $author;
  240.         $this->post_date = $time;
  241.         // Get the GMT time
  242.         $timestamp = strtotime($time);
  243.         $timestamp -= $offset * 60 * 60;
  244.         $this->post_date_gmt = date("Y-m-d H:i:s", $timestamp);
  245.         $this->post_content = $more == "" ? $body : $body . "\n<!--more-->\n" . $more;
  246.         $this->post_title = $title;
  247.         $this->post_category = $cat;
  248.         $this->post_excerpt = '';
  249.         $this->post_status = $draft == 0 ? "publish" : "draft";
  250.         $this->comment_status = $closed == 0 ? "open" : "closed";
  251.         $this->ping_status = "open";
  252.         $this->post_password = '';
  253.         $this->post_name = $urltitle == '' ? $this->sanitize_title_with_dashes($title) : $urltitle;
  254.         $this->to_ping = '';
  255.         $this->pinged = '';
  256.         $this->post_modified = date("Y-m-d H:i:s");
  257.         $this->post_modified_gmt = gmdate("Y-m-d H:i:s");
  258.         $this->post_content_filtered = '';
  259.         $this->post_parent = 0;
  260.         $this->guid = '';
  261.         $this->menu_order = 0;
  262.         $this->post_type = "post";
  263.         $this->post_mime_type = '';
  264.         $this->comment_count = 0;
  265.     }
  266.    
  267.     public function writeToWp ($database)
  268.     {
  269.         // Insert post
  270.         $query = sprintf("INSERT INTO `%s` (`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_category`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $database->prefix("posts"), addslashes($this->post_author), $this->post_date, $this->post_date_gmt, addslashes($this->post_content),     addslashes($this->post_title), $this->post_category, addslashes($this->post_excerpt), $this->post_status, $this->comment_status, $this->ping_status, addslashes($this->post_password), addslashes($this->post_name), addslashes($this->to_ping), addslashes($this->pinged), $this->post_modified, $this->post_modified_gmt, $this->post_content_filtered, $this->post_parent, addslashes($this->guid), $this->menu_order, addslashes($this->post_type), $this->post_mime_type, $this->comment_count);
  271.         $database->query($query);
  272.        
  273.         // Get the post's ID
  274.         $query = sprintf("SELECT `ID` FROM `%s` WHERE `post_date_gmt`='%s' AND `post_author`=%s", $database->prefix("posts"), $this->post_date_gmt, $this->post_author);
  275.         $this->ID = $database->getScalar($query);
  276.        
  277.         // Insert post2cat
  278.         $query = sprintf("INSERT INTO `%s` (`post_id`, `category_id`) VALUES (%s, %s)", $database->prefix("post2cat"), $this->ID, $this->post_category);
  279.         $database->query($query);
  280.        
  281.         return $this->ID;
  282.     }
  283.    
  284.     private function sanitize_title_with_dashes($title)
  285.     {
  286.         $title = strip_tags($title);
  287.         // Preserve escaped octets.
  288.         $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
  289.         // Remove percent signs that are not part of an octet.
  290.         $title = str_replace('%', '', $title);
  291.         // Restore octets.
  292.         $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
  293.    
  294.         $title = remove_accents($title);
  295.         if (seems_utf8($title)) {
  296.             if (function_exists('mb_strtolower')) {
  297.                 $title = mb_strtolower($title, 'UTF-8');
  298.             }
  299.             $title = utf8_uri_encode($title);
  300.         }
  301.    
  302.         $title = strtolower($title);
  303.         $title = preg_replace('/&.+?;/', '', $title); // kill entities
  304.         $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
  305.         $title = preg_replace('/\s+/', '-', $title);
  306.         $title = preg_replace('|-+|', '-', $title);
  307.         $title = trim($title, '-');
  308.    
  309.         return $title;
  310.     }
  311.    
  312. }
  313.  
  314. class Comment
  315. {
  316.     private $comment_ID;
  317.     private $comment_post_ID;
  318.     private $comment_author;
  319.     private $comment_author_email;
  320.     private $comment_author_url;
  321.     private $comment_author_IP;
  322.     private $comment_date;
  323.     private $comment_date_gmt;
  324.     private $comment_content;
  325.     private $comment_karma;
  326.     private $comment_approved;
  327.     private $comment_agent;
  328.     private $comment_type;
  329.     private $comment_parent;
  330.     private $user_id;
  331.    
  332.     public function __construct ($body, $user, $mail, $email, $member, $item, $time, $offset, $ip, $type='comment')
  333.     {
  334.         $this->comment_post_ID = $item;
  335.         $this->comment_author = $user;
  336.         if (preg_match("/.+@.+/", $mail))
  337.             $this->comment_author_email = $mail;
  338.         else
  339.         {
  340.             $this->comment_author_email = $email;
  341.             $this->comment_author_url = $mail;
  342.         }
  343.         $this->comment_author_IP = $ip;
  344.         $this->comment_date = $time;
  345.         // Get the GMT time
  346.         $timestamp = strtotime($time);
  347.         $timestamp -= $offset * 60 * 60;
  348.         $this->comment_date_gmt = date("Y-m-d H:i:s", $timestamp);
  349.         $this->comment_content = $body;
  350.         $this->comment_karma = 0;
  351.         $this->comment_approved = 1;
  352.         $this->comment_agent = '';
  353.         $this->comment_type = $type;
  354.         $this->comment_parent = 0;
  355.         $this->user_id = $member;
  356.     }
  357.    
  358.     public function writeToWp ($database)
  359.     {
  360.         $query = sprintf("INSERT INTO `%s` (`comment_post_ID`, `comment_author`, `comment_author_email`, `comment_author_url`, `comment_author_IP`, `comment_date`, `comment_date_gmt`, `comment_content`, `comment_karma`, `comment_approved`, `comment_agent`, `comment_type`, `comment_parent`, `user_id`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $database->prefix("comments"), addslashes($this->comment_post_ID), addslashes($this->comment_author), addslashes($this->comment_author_email), addslashes($this->comment_author_url), addslashes($this->comment_author_IP), addslashes($this->comment_date), addslashes($this->comment_date_gmt), addslashes($this->comment_content), addslashes($this->comment_karma), addslashes($this->comment_approved), addslashes($this->comment_agent), addslashes($this->comment_type), addslashes($this->comment_parent), addslashes($this->user_id));
  361.         $database->query($query);
  362.     }  
  363. }
  364.  
  365. class Link
  366. {
  367.     private $link_id;
  368.     private $link_url;
  369.     private $link_name;
  370.     private $link_image;
  371.     private $link_target;
  372.     private $link_category;
  373.     private $link_description;
  374.     private $link_visible;
  375.     private $link_owner;
  376.     private $link_rating;
  377.     private $link_updated;
  378.     private $link_rel;
  379.     private $link_notes;
  380.     private $link_rss;
  381.     private $cat_ID;
  382.    
  383.     public function __construct ($owner, $group, $url, $text, $title)
  384.     {
  385.         $this->link_url = $url;
  386.         $this->link_name = $text;
  387.         $this->link_image = '';
  388.         $this->link_target = '';
  389.         $this->link_description = $title;
  390.         $this->link_category = 0;
  391.         $this->link_visible = 'Y';
  392.         $this->link_owner = $owner;
  393.         $this->link_rating = 0;
  394.         $this->link_updated = "0000-00-00 00:00:00";
  395.         $this->link_rel = '';
  396.         $this->link_notes = '';
  397.         $this->link_rss = '';
  398.         $this->cat_ID = $group;
  399.     }
  400.    
  401.     public function writeToWp ($database)
  402.     {
  403.         // Insert link
  404.         $query = sprintf("INSERT INTO `%s` (`link_url`, `link_name`, `link_image`, `link_target`, `link_category`, `link_description`, `link_visible`, `link_owner`, `link_rating`, `link_updated`, `link_rel`, `link_notes`, `link_rss`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $database->prefix("links"), addslashes($this->link_url), addslashes($this->link_name), addslashes($this->link_image), addslashes($this->link_target), addslashes($this->link_category), addslashes($this->link_description), addslashes($this->link_visible), addslashes($this->link_owner), addslashes($this->link_rating), addslashes($this->link_updated), addslashes($this->link_rel), addslashes($this->link_notes), addslashes($this->link_rss));
  405.         $database->query($query);
  406.        
  407.         // Get link ID
  408.         $query = sprintf("SELECT `link_id` FROM `%s` WHERE `link_url`='%s' AND `link_name`='%s' AND `link_owner`=%d", $database->prefix("links"), addslashes($this->link_url), addslashes($this->link_name), $this->link_owner);
  409.         $this->link_id = $database->getScalar($query);
  410.        
  411.         // Insert link2cat
  412.         $query = sprintf("INSERT INTO `%s` (`link_id`, `category_id`) VALUES (%d, %d)", $database->prefix("link2cat"), $this->link_id, $this->cat_ID);
  413.         $database->query($query);
  414.        
  415.         return $this->link_id;
  416.     }
  417. }
  418.  
  419. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement