GGshow reloaded GGshow reloaded

October 19, 2015

PHP 400 Quota Exceeded in Analytics Data Import

Issue:

  • Google Analytics Data Import failed after 50 upload.
  • Unable to upload large file for Analytics Data Import.
  • Intermittent error 400 with Management API.
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/upload/analytics/v3/management/accounts/.../webproperties/.../customDataSources/.../uploads?uploadType=media: (400) Quota exceeded.' in /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php:110 Stack trace: #0 /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 /base/data/home/apps/.../google-api-php-client/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array)

Reason:
There are limits and quotas for Google Analytics Configuration and Reporting API. When daily project requests and daily write requests exceeded limit, API will return error 400.

Solution:

  • Fine-tune your application for quota usage; or
  • Request additional quota; or
  • Go to bed, try again tomorrow 🙂

Reference:
Configuration and Reporting API Limits and Quotas

Filed under: Google Cloud Platform,PHP — Tags: , , , , — GG @ 5:14 pm

PHP 403 Insufficient Permission in Analytics Data Import

Issue:

PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/upload/analytics/v3/management/accounts/.../webproperties/.../customDataSources/.../uploads?uploadType=media: (403) Insufficient Permission' in /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php:110 Stack trace: #0 /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client)) #1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request)) #2 /base/data/home/apps/.../google-api-php-client/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array) #3 /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php(46): Google_Task_Runner->run() #4 /base/data/ in /base/data/home/apps/.../google-api-php-client/src/Google/Http/REST.php on line 110

Reason:
Insufficient permission to perform an API query

Solution:

  • Ensure sufficient account permission granted for the service account (???@developer.gserviceaccount.com).
  • Ensure proper scopes defined in Service Account Authentication (Google_Auth_AssertionCredentials).
    Use Google_Service_Analytics::ANALYTICS ( or “https://www.googleapis.com/auth/analytics” ) instead of Google_Service_Analytics::ANALYTICS_READONLY ( or “https://www.googleapis.com/auth/analytics.readonly” ) if you need to perform an edit operation, e.g. Data Import.

Reference:

April 23, 2015

Google cloud storage for static website hosting

Synopsis

  • Using Google Cloud Storage as low cost yet reliable website hosting service for static webpages / files.

Requirements

  1. To host a static website on Google Cloud Storage, you need to create a Cloud Storage bucket with the domain/subdomain name as the bucket name.
  2. To create a domain named bucket or any bucket name contains dot (.), you need to pass the domain name ownership verification at Google Webmaster Tools.
  3. There are several methods to verify your site, however since you are creating a new storage to host your web files, I assume that you do not have an existing web hosting storage to store your HTML files at the moment, options by adding Meta tag, HTML file, Google Analytics or Tag Manager code might not be possible. Hence, adding new DNS records could be the only option that works for everyone.

Steps

  1. Creating DNS records
    Two new DNS records need to be created.

    1. A CNAME record of your subdomain hostname (e.g. www) with canonical name c.storage.googleapis.com.
      Creating CNAME record for Google Cloud Storage bucket
    2. Another TXT or CNAME record need to be created for Google Webmaster Tools domain ownership verification.
  2. Verify domain ownership
    Add your site at Google Webmaster Tools and verify the site ownership.
    Google Webmaster Tools site ownership verification
  3. Create bucket
    At Google Developers Console, under Storage > Cloud Storage, create a new bucket.
    Creating new Google Cloud Storage bucket
  4. Upload your web files
    Upload files to Google Cloud Storage bucket
  5. Set files permission
    From Storage Browser, enable public link option for all web files.
    Google Cloud Storage public link
  6. Edit bucket website configuration
    Configure main page and custom 404 page for your website.
    Google Cloud Storage website configuration, main page and custom 404 page

Useful gsutil / gsutil.py commands
You can also change file permission and website configuration using gsutil command.

Configuring main page and custom 404 page:

gsutil web set -m index.html -e 404.html gs://www.ggshow.com

Enabling public read file permission for a single file:

gsutil acl set public-read gs://www.ggshow.com/file.html

Enabling public read file permission for all files in bucket:

gsutil acl set public-read gs://www.ggshow.com/*.*

Uploading local files to bucket

gsutil cp -R \var\www gs://www.ggshow.com

Copying an object (file or folder) from one bucket to another bucket

gsutil cp -R gs://www.ggshow.com/object gs://mybackup.ggshow.com

Moving files from one folder to another folder

gsutil -m mv  gs://www.ggshow.com/folder1/*.* gs://www.ggshow.com/folder2

List files in bucket

gsutil ls gs://www.ggshow.com

Reference

Grayscaling your website

A quick solution to convert webpages into black and white using CSS grayscale filter, supported by all major web browsers including MSIE, Firefox, Chrome, Safari. Suitable for making mourning page, paying tribute, & etc.

Demo

Convert the entire page into black & white:

body {
  filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale");
  filter: gray;
  -webkit-filter: grayscale(1);
}

Convert selected elements into black & white:

.grayscale {
  filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale");
  filter: gray;
  -webkit-filter: grayscale(1);
}

And add grayscale class to selected HTML elements. E.g.:

<div class="grayscale">
 ...
<div>

Print webpages in black & white:

@media print {
  .grayscale {
    filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\'><feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/></filter></svg>#grayscale");
    filter: gray;
    -webkit-filter: grayscale(1);
  }
}

CDN options
CSS also available on CDN.

Grayscale all:

<link rel="stylesheet" type="text/css" href="http://cdn.ggshow.com/styles/grayscale-all.css">

Grayscale selected:

<link rel="stylesheet" type="text/css" href="http://cdn.ggshow.com/styles/grayscale.css">

Grayscale print:

<link rel="stylesheet" type="text/css" href="http://cdn.ggshow.com/styles/grayscale-print.css">

Or using CSS import method:

Grayscale all:

@import url("http://cdn.ggshow.com/styles/grayscale-all.css");

Grayscale selected:

@import url("http://cdn.ggshow.com/styles/grayscale.css");

Grayscale print:

@import url("http://cdn.ggshow.com/styles/grayscale-print.css");
Filed under: CSS,HTML,Uncategorized — Tags: , , , , — GG @ 6:59 pm

March 9, 2015

How to block a specific network port in Linux?

Question:

How to block a specific network port in Linux?

Answer:

Use iptables commands.
E.g. to block port 21 & 22:

iptables -A OUTPUT -p tcp --dport 21 -j REJECT
iptables -A OUTPUT -p tcp --dport 22 -j REJECT

To block a port permanently

  1. save iptables to a file,
    iptables-save > /etc/sysconfig/iptables
  2. add this line to /etc/rc.local
    iptables-restore < /etc/sysconfig/iptables
Filed under: Linux,Web Servers — Tags: , , — GG @ 5:27 pm

January 4, 2014

Chord Canvas

This is a HTML5 build of Dynamic Chord Diagam.

Usage:

  • Include JavaScript at HTML head.
    <script src="http://commondatastorage.googleapis.com/ggshow/files/chordcanvas/chordcanvas-1-0-0.min.js"></script>
  • Creating/rendering chord instance.
    var myChord = new ChordCanvas(value, size);
    myChord.render();

Simplified method:

  • <script>new ChordCanvas("C,X,3,2,0,1,0","100").render();</script>

Customize Color:

  • var chord1 = new ChordCanvas("C,X,3,2,0,1,0","150");
    chord1.backgroundcolor = "#CCF";
    chord1.bordercolor = "";
    chord1.titlecolor = "#FFF";
    chord1.labelcolor = "#00F";
    chord1.chordcolor = "#00F";
    chord1.titlebackgroundcolor = "#00F";
    chord1.render();

Overwrite chord container display style:

  • Chord container has block display by default, you can overwrite the display style by .chord-container class selector. e.g:
    <style>
    .chord-container {
        display:inline-block;
    }
    </style>

Examples:

Source:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Chord Canvas - Example 01</title>
<style>
.chord-container {
display:inline-block;
}
</style>
<script src="http://commondatastorage.googleapis.com/ggshow/files/chordcanvas/chordcanvas-1-0-0.min.js"></script>
</head>
<body>
<h1>Chord Canvas Examples</h1>
<h3>Basic</h3>
<script>new ChordCanvas("C,X,3,2,0,1,0","100").render();</script>
<script>new ChordCanvas("Am,X,0,2,2,1,0","100").render();</script>
<script>new ChordCanvas("F,X,X,3,2,1,1","100").render();</script>
<script>new ChordCanvas("G,X,0,2,2,1,0","100").render();</script>
<br />
<script>new ChordCanvas("C#7-5,X,X,3,4,2,3","100").render();</script>
<script>new ChordCanvas("Dâ™­7-9,X,4,3,4,3,X","100").render();</script>
<script>new ChordCanvas("Eâ™­,11,10,8,8,8,11","100").render();</script>
<script>new ChordCanvas("Bâ™­m,X,X,11,10,11,9","100").render();</script>
<h3>Customize Size</h3>
<script>new ChordCanvas("C,X,3,2,0,1,0","150").render();</script>
<script>new ChordCanvas("C,X,3,2,0,1,0","250").render();</script>
<script>new ChordCanvas("C,X,3,2,0,1,0","").render();</script>
<h3>Customize Color</h3>
<script>
var chord1 = new ChordCanvas("C,X,3,2,0,1,0","150");
chord1.backgroundcolor = "#CCF";
chord1.bordercolor = "";
chord1.titlecolor = "#FFF";
chord1.labelcolor = "#00F";
chord1.chordcolor = "#00F";
chord1.titlebackgroundcolor = "#00F";
chord1.render();

var chord2 = new ChordCanvas("C,8,10,10,9,8,8","150");
chord2.backgroundcolor = "#F00";
chord2.bordercolor = "#F00";
chord2.titlecolor = "#F00";
chord2.labelcolor = "#000";
chord2.chordcolor = "#FFF";
chord2.titlebackgroundcolor = "#FFF";
chord2.render();
</script>
</body>
</html>
Filed under: CSS,HTML5,JavaScript — Tags: , , , , , , — GG @ 8:44 am

b2evolution to WordPress migration

I’ve been looking for b2evolution to WordPress importer for long time. What I can find on the web are only migration script for older version of b2evo/wp, hence I’ve decided to write one on my own, for importing data from b2evolution 4.0.5 to WordPress 3.7.1. I’ve tested the script with WordPress 3.8.0 too. I’m hereby sharing it to whoever needs it.

Instruction

  1. Place this file at the same directory with your wp-config.php
  2. Access the URL at web browser, e.g.: http://localhost/b2wp.php
  3. Fill up the form with database connection detail, and submit.
  4. Select import options, and start import.

Warning

  1. Backup all your databases safely before proceed.
  2. Try it with a fresh installation of WordPress instead of production site.
  3. Use this script at your own risk 🙂

Download

  • b2wp.php – Tested with b2evo 4.0.5 to wp 3.7.1/3.8.0
  • b2wp_kp.zip – provided by KP for b2evo 5.1.2 to wp 4.
<?php
/*
* b2wp.php
* Title        : B2evolution to WordPress Importer
* Version      : 1.0.2
* Author URL   : http://www.ggshow.com
* Description  : Import Authors, Categories, Posts, Tags, and Comments, from
*               B2evolution database to WordPress database.
*/

function slug($str) {
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $str)));
}

$output = '';
$step = 1;
require_once( dirname(__FILE__) . '/wp-config.php' );
$target['db_host'] = DB_HOST;
$target['db_user'] = DB_USER;
$target['db_password'] = DB_PASSWORD;
$target['db_name'] = DB_NAME;
$wp_con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$target['status'] = (mysqli_connect_errno()) ? 'Error' : 'Connected';
$output .= ($target['status'] == 'Error') ? 'Error connecting WordPress database!<br />' : '';
$source['db_host'] = ($_POST['source_dbhost'] == '') ? 'localhost' : htmlentities($_POST['source_dbhost']);
$source['db_user'] = htmlentities($_POST['source_dbuser']);
$source['db_password'] = htmlentities($_POST['source_dbpassword']);
$source['db_name'] = htmlentities($_POST['source_dbname']);
if (!empty($_POST['source_dbname'])) {
$b2_con = new mysqli($source['db_host'], $source['db_user'], $source['db_password'], $source['db_name']);
$source['status'] = (mysqli_connect_errno()) ? 'Error' : 'Connected';
$output .= ($source['status'] == 'Error') ? 'Error connecting B2evolution database!<br />' : '';
} else {
$source['status'] = '';
$output .= 'Please fill up B2evolution database connection detail!<br />';
}
$target['table_prefix'] = (!empty($_POST['target_table_prefix'])) ? $wp_con->real_escape_string($_POST['target_table_prefix']) : 'wp_';
$source['table_prefix'] = (!empty($_POST['source_table_prefix'])) ? $wp_con->real_escape_string($_POST['source_table_prefix']) : 'evo_';
if ($source['status'] == 'Connected') {
$sql = " SHOW TABLES LIKE \"" . $source['table_prefix'] . "%\"";
$res = $b2_con->query($sql);
if ($res->num_rows <= 0) {
$source['status'] = 'Error';
$output .= "Invalid B2evo table prefix.<br />";
} else {
$sql = "SELECT DISTINCT `" . $source['table_prefix'] . "blogs`.* FROM `" . $source['table_prefix'] . "blogs`";
$res = $b2_con->query($sql);
$b2_blog_selected = ($_POST['b2_blog'] == '') ? 1 : $_POST['b2_blog'];
$b2_blog_select = '<select name="b2_blog">';
while ($row = $res->fetch_array()) {
$b2_blog_selected_str = '';
if ($row['blog_ID'] == $b2_blog_selected) {
$b2_blog_selected_str = 'selected';
$b2_blog_selected_row = $row;
}
$b2_blog_select .= '<option value="' . $row['blog_ID'] . '" ' . ($b2_blog_selected_str) . ' >' . $row['blog_name'] . '</option>';
}
$b2_blog_select .= '</select>';
}
}
if ($target['status'] == 'Connected') {
$sql = " SHOW TABLES LIKE \"" . $target['table_prefix'] . "%\"";
$res = $wp_con->query($sql);
if ($res->num_rows <= 0) {
$target['status'] = 'Error';
$output .= "Invalid WordPress table prefix.<br />";
} else {
$sql = "SELECT DISTINCT `" . $target['table_prefix'] . "users`.* FROM `" . $target['table_prefix'] . "users`";
$res = $wp_con->query($sql);
$b2_user_selected = ($_POST['b2_user'] == '') ? 0 : $_POST['b2_user'];
$b2_user_select = '<select name="b2_user">';
$b2_user_select .= '<option value="0">Import B2evo users</option>';
while ($row = $res->fetch_array()) {
if ($row['ID'] == $b2_user_selected) {
$b2_user_selected_str = 'selected';
$b2_user_selected_row = $row;
} else {
$b2_user_selected_str = '';
}
$b2_user_select .= '<option value="' . $row['ID'] . '" ' . ($b2_user_selected_str) . ' >Assign posts to ' . $row['user_login'] . '</option>';
}
$b2_user_select .= '</select>';
}
}
if ($source['status'] == 'Connected' && $target['status'] == 'Connected') {
$step = 2;
}
if ($step >= 2) {
$b2_cat_selected = ($_POST['b2_cat'] == '') ? 0 : $_POST['b2_cat'];
$b2_cat_select = '<select name="b2_cat">';
$b2_cat_select .= '<option value="1" ' . ($b2_cat_selected == 1 ? 'selected' : '') . '>a: Uncategorized</option>';
$b2_cat_select .= '<option value="2" ' . ($b2_cat_selected == 2 ? 'selected' : '') . '>b: Create a new category</option>';
$b2_cat_select .= '<option value="3" ' . ($b2_cat_selected == 3 ? 'selected' : '') . '>c: Import B2evo categories</option>';
$b2_cat_select .= '<option value="4" ' . ($b2_cat_selected == 4 ? 'selected' : '') . '>b+c</option>';
$b2_cat_select .= '</select>';
$b2_checbox_tags = '<input type="checkbox" value="1" name="b2_tags" ' . (($_POST['b2_tags'] == 1) ? 'checked' : '') . ' />';
$b2_checbox_comments = '<input type="checkbox" value="1" name="b2_comments" ' . (($_POST['b2_comments'] == 1) ? 'checked' : '') . ' />';
if ($_POST['step'] == '3' && $target['status'] != 'Error' && $source['status'] != 'Error') {
$step = 3;
}
}
if ($step >= 3) {
if ($b2_cat_selected == 1) {
$root_id = 1;
$taxonomy[1] = 1;
} else if ($b2_cat_selected == 2 || $b2_cat_selected == 4) {
unset($data);
$row = $b2_blog_selected_row;
$data['blog_name'] = $wp_con->real_escape_string($row['blog_name']);
$data['blog_urlname'] = slug($wp_con->real_escape_string($row['blog_urlname']));
$data['blog_description'] = empty($row['blog_description'])?$wp_con->real_escape_string($row['blog_tagline']):$wp_con->real_escape_string($row['blog_description']);
$sql = " INSERT INTO `" . $target['table_prefix'] . "terms` (`name`,`slug`,`term_group`) VALUES ('" . $data['blog_name'] . "','" . $data['blog_urlname'] . "','0');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$root_id = $wp_con->insert_id;
$newcat[0] = $root_id;
$sql = " INSERT INTO `" . $target['table_prefix'] . "term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES ('" . $newcat[0] . "','category','" . $data['blog_description'] . "',0,0);";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$id = $wp_con->insert_id;
$taxonomy[$newcat[0]] = $id;
$output .= "1 new category created!<br />";
} else {
$root_id = 0;
$taxonomy[0] = 1;
}
if ($b2_cat_selected == 3 || $b2_cat_selected == 4) {
$sql = "SELECT DISTINCT `" . $source['table_prefix'] . "categories`.* FROM `" . $source['table_prefix'] . "categories` WHERE `cat_blog_ID` = " . $wp_con->real_escape_string($b2_blog_selected);
$res = $b2_con->query($sql);
$count = 0;
while ($row = $res->fetch_array()) {
$sql = " INSERT INTO `" . $target['table_prefix'] . "terms` (`name`,`slug`,`term_group`) VALUES ('" . $wp_con->real_escape_string($row['cat_name']) . "','" . $wp_con->real_escape_string($row['cat_urlname']) . "','0');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$id = $wp_con->insert_id;
$newcat[$row['cat_ID']] = $id;
$count++;
}
$sql = "SELECT DISTINCT `" . $source['table_prefix'] . "categories`.* FROM `" . $source['table_prefix'] . "categories` WHERE `cat_blog_ID` = " . $wp_con->real_escape_string($b2_blog_selected);
$res = $b2_con->query($sql);
while ($row = $res->fetch_array()) {
$data['parent_id'] = (isset($newcat[$row['cat_parent_ID']]) ? $newcat[$row['cat_parent_ID']] : $root_id);
$data['description'] = $wp_con->real_escape_string($row['cat_description']);
$sql = " INSERT INTO `" . $target['table_prefix'] . "term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES ('" . $newcat[$row['cat_ID']] . "','category','" . $data['description'] . "','" . $data['parent_id'] . "',0);";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$id = $wp_con->insert_id;
$taxonomy[$newcat[$row['cat_ID']]] = $id;
}
$output .= $count . " categories imported!<br />";
}
if ($b2_cat_selected != 1) {
$sql = "UPDATE `" . $target['table_prefix'] . "options` SET `option_value`='' WHERE `option_name`='category_children';";
$wp_con->query(trim(str_replace("\n", "", $sql)));
}
if ($wp_user_selected == 0) {
$sql = " SELECT DISTINCT `" . $source['table_prefix'] . "users`.* FROM `" . $source['table_prefix'] . "users` INNER JOIN `" . $source['table_prefix'] . "blogusers` ON `" . $source['table_prefix'] . "users`.`user_ID` = `" . $source['table_prefix'] . "blogusers`.`bloguser_user_ID` WHERE `" . $source['table_prefix'] . "blogusers`.`bloguser_blog_ID` = " . $b2_con->real_escape_string($b2_blog_selected) . " AND `user_login`<>'admin'";
$res = $b2_con->query($sql);
$count = 0;
while ($row = $res->fetch_array()) {
$sql = " SELECT * FROM `" . $target['table_prefix'] . "users` WHERE `user_login` = '" . $wp_con->real_escape_string($row['user_login']) . "';";
$pres = $wp_con->query(trim(str_replace("\n", "", $sql)));
if ($prow = $pres->fetch_array()) {
$id = $prow['ID'];
} else {
$sql = " INSERT INTO `" . $target['table_prefix'] . "users` (`user_login`,`user_pass`,`user_nicename`,`user_email`,`user_url`,`user_registered`,`user_status`,`display_name`) VALUES ('" . $wp_con->real_escape_string($row['user_login']) . "','" . $wp_con->real_escape_string($row['user_pass']) . "','" . $wp_con->real_escape_string($row['user_login']) . "','" . $wp_con->real_escape_string($row['user_email']) . "','" . $wp_con->real_escape_string($row['user_url']) . "','" . $wp_con->real_escape_string($row['dateYMDhour']) . "','0','" . $wp_con->real_escape_string($row['user_login']) . "');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$id = $wp_con->insert_id;
$sql = " INSERT INTO `" . $target['table_prefix'] . "usermeta` (`user_id`,`meta_key`,`meta_value`) VALUES ('" . $id . "', 'first_name', '" . $wp_con->real_escape_string($row['user_firstname']) . "');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$sql = " INSERT INTO `" . $target['table_prefix'] . "usermeta` (`user_id`,`meta_key`,`meta_value`) VALUES ('" . $id . "', 'last_name', '" . $wp_con->real_escape_string($row['user_lastname']) . "');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$sql = " INSERT INTO `" . $target['table_prefix'] . "usermeta` (`user_id`,`meta_key`,`meta_value`) VALUES ('" . $id . "', 'nickname', '" . $wp_con->real_escape_string($row['user_nickname']) . "');";
$wp_con->query(trim(str_replace("\n", "", $sql)));
$count++;
}
$newuser[$row['user_ID']] = $id;
}
if ($count > 0) {
$output .= $count . " users imported!<br />";
}
} else {
$newuser[0] = $wp_user_selected;
}
$sql = "SELECT DISTINCT `" . $source['table_prefix'] . "items__item`.* FROM `" . $source['table_prefix'] . "items__item` INNER JOIN `" . $source['table_prefix'] . "categories` ON `" . $source['table_prefix'] . "items__item`.`post_main_cat_ID` = `" . $source['table_prefix'] . "categories`.`cat_ID` WHERE `" . $source['table_prefix'] . "categories`.`cat_blog_ID` = " . $b2_blog_selected;
$res = $b2_con->query($sql);
$pcount = 0;
$tcount = 0;
$ccount = 0;
while ($row = $res->fetch_array()) {
unset($data);
if ($b2_user_selected == 0) {
$data['user'] = isset($newuser[$row['post_creator_user_ID']]) ? $newuser[$row['post_creator_user_ID']] : 1;
} else {
$data['user'] = $b2_user_selected;
}
$data['category'] = isset($newcat[$row['post_main_cat_ID']]) ? $newcat[$row['post_main_cat_ID']] : $root_id;
switch ($row['post_status']) {
case 'published':
case 'deprecated':
$data['status'] = 'publish';
break;
case 'protected':
case 'private':
$data['status'] = 'private';
break;
case 'redirected':
$row['post_content'] = "<a href='".htmlentities($row['post_url'])."'>".htmlentities($row['post_url'])."</a>";
default:
$data['status'] = 'draft';
}
$data['date'] = $row['post_datecreated'];
$data['date_gmt'] = gmdate("Y-m-d G:i:s ", strtotime($data['date']));
$data['modified'] = $row['post_datemodified'];
$data['modified_gmt'] = gmdate("Y-m-d G:i:s ", strtotime($data['modified']));
$psql = "SELECT COUNT(1) AS comment_count FROM `" . $source['table_prefix'] . "comments` WHERE `" . $source['table_prefix'] . "comments`.`comment_post_ID` = " . $row['post_ID'];
$pres = $b2_con->query($psql);
$prow = $pres->fetch_array();
$data['comment_count'] = $prow['comment_count'];
$data['post_content'] = $row['post_content'];
$data['post_excerpt'] = $row['post_excerpt'];
$data['title'] = $row['post_title'];
$psql = "INSERT INTO `" . $target['table_prefix'] . "posts` (`post_author`,`post_content`,`post_title`,`post_excerpt`,`post_status`,`post_date`,`post_date_gmt`,`post_modified`,`post_modified_gmt`,`comment_count`, `to_ping`, `pinged`, `post_content_filtered`) VALUES ('" . $data['user'] . "','" . $wp_con->real_escape_string($data['post_content']) . "','" . $wp_con->real_escape_string($data['title']) . "','" . $wp_con->real_escape_string($data['post_excerpt']) . "','" . $data['status'] . "','" . $data['date'] . "','" . $data['date_gmt'] . "','" . $data['modified'] . "','" . $data['modified_gmt'] . "','" . $data['comment_count'] . "', '', '', '');";
$wp_con->query(trim(str_replace("\n", "", $psql)));
$id = $wp_con->insert_id;
$eid = $row['post_ID'];
$psql = "INSERT INTO `wp_term_relationships` (`object_id`,`term_taxonomy_id`,`term_order`) VALUES (" . $id . "," . $taxonomy[$data['category']] . ",0);";
$wp_con->query(trim(str_replace("\n", "", $psql)));
$psql = "SELECT * FROM `" . $source['table_prefix'] . "postcats` WHERE `postcat_post_ID` = " . $row['post_ID'];
$pres = $b2_con->query($psql);
while ($prow = $pres->fetch_array()) {
$psql = "INSERT INTO `wp_term_relationships` (`object_id`,`term_taxonomy_id`,`term_order`) VALUES (" . $id . "," . $taxonomy[$newcat[$prow['postcat_cat_ID']]] . ",0);";
$wp_con->query(trim(str_replace("\n", "", $psql)));
}
$pcount++;
if ($_POST['b2_tags'] == 1) {
$psql = "SELECT `itag_itm_ID`, LOWER(cast(`" . $source['table_prefix'] . "items__tag`.`tag_name` as char)) AS tag_name FROM `" . $source['table_prefix'] . "items__tag` INNER JOIN `" . $source['table_prefix'] . "items__itemtag` ON `itag_tag_ID` = `tag_ID` WHERE `itag_itm_ID` = " . $row['post_ID'];
$pres = $b2_con->query($psql);
while ($prow = $pres->fetch_array()) {
unset($data);
$data['tag_name'] = $wp_con->real_escape_string($prow['tag_name']);
$data['tag_slug'] = slug($data['tag_name']);
$tsql = "SELECT * FROM `" . $target['table_prefix'] . "terms` WHERE `slug` = '" . $data['tag_slug'] . "';";
$tres = $wp_con->query(trim(str_replace("\n", "", $tsql)));
if ($trow = $tres->fetch_array()) {
$tid = $trow['term_id'];
} else {
$tsql = "INSERT INTO `" . $target['table_prefix'] . "terms` (`name`,`slug`,`term_group`) VALUES ('" . $data['tag_name'] . "','" . $data['tag_slug'] . "',0);";
$wp_con->query(trim(str_replace("\n", "", $tsql)));
$tid = $wp_con->insert_id;
$tcount++;
}
$tsql = "SELECT * FROM `" . $target['table_prefix'] . "term_taxonomy` WHERE `term_id` = '" . $tid . "' AND `taxonomy`='post_tag';";
$tres = $wp_con->query(trim(str_replace("\n", "", $tsql)));
if ($trow = $tres->fetch_array()) {
$ttid = $trow['term_taxonomy_id'];
} else {
$tsql = "INSERT INTO `" . $target['table_prefix'] . "term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES (" . $tid . ",'post_tag','',0,0);";
$wp_con->query(trim(str_replace("\n", "", $tsql)));
$ttid = $wp_con->insert_id;
}
$tsql = "SELECT * FROM `" . $target['table_prefix'] . "term_relationships` WHERE `object_id` = '" . $id . "' AND `term_taxonomy_id`='" . $ttid . "';";
$tres = $wp_con->query(trim(str_replace("\n", "", $tsql)));
if ($trow = $tres->fetch_array()) {

} else {
$tsql = "INSERT INTO `" . $target['table_prefix'] . "term_relationships` (`object_id`,`term_taxonomy_id`,`term_order`) VALUES (" . $id . "," . $ttid . ",0);";
$wp_con->query(trim(str_replace("\n", "", $tsql)));
}
}
}
if ($_POST['b2_comments'] == 1) {
$psql = "SELECT `" . $source['table_prefix'] . "comments`.* , `" . $source['table_prefix'] . "users`.`user_nickname`, `" . $source['table_prefix'] . "users`.`user_url`, `" . $source['table_prefix'] . "users`.`user_email` FROM `" . $source['table_prefix'] . "comments` LEFT JOIN `" . $source['table_prefix'] . "users` ON `" . $source['table_prefix'] . "comments`.`comment_author_ID` = `" . $source['table_prefix'] . "users`.`user_ID` WHERE `" . $source['table_prefix'] . "comments`.`comment_post_ID`=" . $eid;
$pres = $b2_con->query($psql);
while ($prow = $pres->fetch_array()) {
unset($data);
if ($prow['comment_author_ID'] != NULL) {
$data['author'] = $wp_con->real_escape_string($prow['user_nickname']);
$data['url'] = $wp_con->real_escape_string($prow['user_url']);
$data['email'] = $wp_con->real_escape_string($prow['user_email']);
} else {
$data['author'] = $wp_con->real_escape_string($prow['comment_author']);
$data['url'] = $wp_con->real_escape_string($prow['comment_author_url']);
$data['email'] = $wp_con->real_escape_string($prow['comment_author_email']);
}
$data['time'] = $wp_con->real_escape_string($prow['comment_date']);
$data['gm_time'] = gmdate("Y-m-d G:i:s ", strtotime($data['time']));
$data['comment_type'] = $wp_con->real_escape_string($prow['comment_type']);
switch ($data['comment_type']) {
case 'trackback':
break;
case 'pingback':
case 'linkback':
$data['comment_type'] = 'pingback';
break;
case 'comment':
default:
$data['comment_type'] = '';
break;
}
$data['comment_status'] = $wp_con->real_escape_string($prow['comment_status']);
switch ($data['comment_status']) {
case 'published':
case 'deprecated':
$data['comment_status'] = '1';
break;
case 'protected':
case 'private':
$data['comment_status'] = 'private';
break;
case 'draft':
case 'redirected':
default:
$data['comment_status'] = '0';
}
$data['comment_author_IP'] = $wp_con->real_escape_string($prow['comment_author_IP']);
$data['comment_content'] = $wp_con->real_escape_string($prow['comment_content']);
$data['comment_karma'] = $wp_con->real_escape_string($prow['comment_karma']);
$csql = "INSERT INTO `" . $target['table_prefix'] . "comments` (`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_type`) VALUES ('" . $id . "','" . $data['author'] . "','" . $data['email'] . "','" . $data['url'] . "','" . $data['comment_author_IP'] . "','" . $data['time'] . "','" . $data['gm_time'] . "','" . $data['comment_content'] . "','" . $data['comment_karma'] . "','" . $data['comment_status'] . "','" . $data['comment_type'] . "');";
$wp_con->query(trim(str_replace("\n", "", $csql)));
$ccount++;
}
}
}
$output .= $pcount . " posts imported!<br />";
if ($_POST['b2_tags'] == 1) {
$output .= $tcount . " tags imported!<br />";
}
if ($_POST['b2_comments'] == 1) {
$output .= $ccount . " comments imported!<br />";
}
}
mysqli_free_result($res);
mysqli_free_result($pres);
mysqli_free_result($tres);
mysqli_close($wp_con);
mysqli_close($b2_con);
?>
<!DOCTYPE html>
<html>
<head>
<title>b2wp - b2evolution to wordpress importer</title>
<meta charset="UTF-8">
<style>
body { font: 10pt sans-serif; }
table { width: 480px; border: #ccc 1px solid; margin:10px auto; }
th, td { width: 160px; }
caption { font: bold 16pt sans-serif; }
</style>
</head>

<body>
<form action="" method="post">
<table>
<caption>B2WP</caption>
<tr>
<th></th>
<th>B2evolution</th>
<th>Wordpress</th>
</tr>
<tr>
<td>Host</td>
<td><input name="source_dbhost" type="text" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['db_host']; ?>" /></td>
<td><input type="text" disabled value="<?php echo $target['db_host']; ?>" /></td>
</tr>
<tr>
<td>User</td>
<td><input name="source_dbuser" type="text" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['db_user']; ?>" /></td>
<td><input type="text" disabled value="<?php echo $target['db_user']; ?>" /></td>
</tr>
<tr>
<td>Password</td>
<td><input name="source_dbpassword" type="password" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['db_password']; ?>" /></td>
<td><input type="password" disabled value="<?php echo $target['db_password']; ?>" /></td>
</tr>
<tr>
<td>Database</td>
<td><input name="source_dbname" type="text" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['db_name']; ?>" /></td>
<td><input type="text" disabled value="<?php echo $target['db_name']; ?>" /></td>
</tr>
<tr>
<td>Table Prefix</td>
<td><input type="text" name="source_table_prefix" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $source['table_prefix']; ?>" /></td>
<td><input type="text" name="target_table_prefix" <?php if ($step >= 2) echo 'readonly'; ?> value="<?php echo $target['table_prefix']; ?>" /></td>
</tr>
<tr>
<td>Status</td>
<td><input type="text" disabled value="<?php echo $source['status']; ?>" /></td>
<td><input type="text" disabled value="<?php echo $target['status']; ?>" /></td>
</tr>
<?php if ($step == 1) { ?>
<tr>
<td></td>
<td colspan="2"><input type="submit" value="Connect" /></td>
</tr>
<?php } else if ($step >= 2) { ?>
<tr>
<td colspan="3"><input type="hidden" name="step" value="3"/><hr /></td>
</tr>
<tr>
<td>B2evo Blog</td>
<td colspan="2">
<?php echo $b2_blog_select; ?>
</td>
</tr>
<tr>
<td valign="top">Import Options</td>
<td colspan="2">
<input type="checkbox" checked disabled> <label for="b2_posts">Posts</label><br />
<input type="checkbox" checked disabled> <label for="b2_user">Authors</label> <?php echo $b2_user_select; ?><br />
<input type="checkbox" checked disabled> <label for="b2_cat">Categories</label> <?php echo $b2_cat_select; ?><br />
<?php echo $b2_checbox_tags; ?> <label for="b2_tags">Tags</label><br />
<?php echo $b2_checbox_comments; ?> <label for="b2_comments">Comments</label><br />
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
</td>
</tr>
<tr>
<td></td>
<td colspan="2">
<input type="submit" value="Import" />
<input type="button" value="Back" onclick="location.href='b2wp.php';" />
</td>
</tr>
<?php } ?>
<?php if (!empty($output)) { ?>
<tr>
<td colspan="3"><hr /><?php echo $output; ?></td>
</tr>
<?php } ?>
</table>
</form>
</body>
</html>

This is NOT a WordPress importer plugin, this is simply a PHP script to extract content (Authors, Categories, Posts, Tags, and Comments) from a b2evolution database and insert into WordPress database. This migration script does NOT catered importing of uploaded media files. Feel free to modify it as per your needs. You might want to try these WordPress plugins Add From Server or Import External Images for migrating your uploaded files.

Special thanks
Thanks to RealNitro’s b2evo2wp.php script, it was my primary source of reference for understanding b2evo/wp database schema.
Thanks to KP for sharing b2evolution 5.1.2 to WorPress 4 migration script.

Filed under: MySQL,PHP,Web — Tags: , , , , — GG @ 6:53 am

November 18, 2013

PHP client denied by server configuration

Issue:

  • AH01630: client denied by server configuration
  • Forbidden
    You don’t have permission to access /___ on this server.
    Apache/2.4.6 (Ubuntu) Server at www.___.com Port ___

Reason:

  • New way to do access control in Apache 2.4 and above.

Solution:

  • Edit Apache configuration , replace Allow from all to Require all granted

More Info:

Filed under: PHP,Web — Tags: , , , — GG @ 3:15 pm

October 19, 2013

PHP none could be negotiated

Issue:
Negotiation: discovered file(s) matching request: /var/www/index (None could be negotiated).

Reason:
PHP mime type has not been enabled.

Solution:
Edit Apache configuration (apache2.conf / httpd.conf / mime.conf), add or uncomment line below:

AddType application/x-httpd-php .php
Filed under: PHP — Tags: , , — GG @ 3:27 pm

August 7, 2013

PHP unexpected end of file

Issue:

  • Parse error: syntax error, unexpected end of file in ___ on line ___

Reason:

  • short_open_tags has been disabled in PHP configuration on the server.
  • Short form of PHP open tag (<? ?>) is not allowed.
  • PHP does not recognize code between short tag <? and ?>

Solution:

  • Avoid using short tag, replace short tag (<? and ?>) with full tag (<?php and ?>), and <?=… ?> with <?php echo …; ?>.

    or

  • For PHP v5.4.0 or newer
    Edit php.ini
    find
    short_open_tag = Off
    change it to:
    short_open_tag = On

    If short_open_tag is enabled, you will not be able to use JSAPI tag (<?js=…?>) or XML tag (<?xml … ?>) inline.
    You will be required to echo/print it with PHP, for example: <?php echo ‘<?xml version=”1.0″?>’; ?>.

More Info:

Older Posts »

© 2024 GGSHOW | Powered by WordPress