-- วึ่งผมจะมาแนะนำ 1 ในวิธีการสร้างสือกลางในการแชร์ข้อมูลโดยผ่าน facebook ครับ
เราจะเริ่มจาการสร้าง ปุ่ม กด เเชร์ ขึ้นมาก่อน
โค้ด: เลือกทั้งหมด
<div class="facebook hide-mobile">
<div id="fb-root"></div>
<script>
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id))
return;
js = d.createElement(s);
js.id = id;
js.src = 'https://connect.facebook.net/th_TH/sdk.js#xfbml=1&version=v2.12&appId={APP_ID_FAC}&autoLogAppEvents=1';
fjs.parentNode.insertBefore(js, fjs);
}
(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-share-button" data-href="{OG_URL}" data-layout="button" ></div>
<br>
ส่วนที่ เป็นส่วนเชื่อมต่อส่งข้อมูล ระกว่าง facebook กับ phpb
โค้ด: เลือกทั้งหมด
<!DOCTYPE html>
<html>
<head>
<link rel="canonical" href="{OG_URL}" />
<meta property="og:type" content="website" />
<meta property="og:locale" content="th_TH" />
<meta property="og:title" content="{OG_PAGE_TITLE}" />
<meta property="og:site_name" content="{SITENAME}"/>
<meta property="og:url" content="{OG_URL}" />
<meta property="og:description" content="{OG_DESCRIPTION}"/>
<!-- BEGIN og_row_img -->
<meta property="{og_row_img.META_TYPE}" content="{og_row_img.OG_IMAGE}"/>
<!-- END og_row_img -->
<!-- BEGIN og_row_img_url -->
<meta property="{og_row_img_url.META_TYPE_URL}" content="{og_row_img_url.OG_IMAGE}"/>
<!-- END og_row_img_url -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<!-- BEGIN og_row_img -->
<img src="{og_row_img.OG_IMAGE}" alt="Smiley face" height="120" width="120">
<!-- END og_row_img -->
</body>
</html>
โค้ด: เลือกทั้งหมด
<?php
namespace mindphp\m_share_facebook\controller;
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
class viewtopic_share {
protected $config;
protected $helper;
protected $template;
protected $user;
protected $db;
protected $auth;
protected $cache;
protected $dispatcher;
protected $root_path;
protected $php_ext;
protected $request;
protected $phpbb_container;
protected $mod_facebook_share_hits;
public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $helper, \phpbb\template\template $template, \phpbb\user $user, \phpbb\db\driver\driver_interface $db, \phpbb\auth\auth $auth, \phpbb\cache\service $cache, \phpbb\event\dispatcher_interface $dispatcher, \phpbb\request\request $request, $mod_facebook_share_hits, $root_path, $php_ext) {
$this->config = $config;
$this->helper = $helper;
$this->template = $template;
$this->user = $user;
$this->db = $db;
$this->auth = $auth;
$this->cache = $cache;
$this->dispatcher = $dispatcher;
$this->root_path = $root_path;
$this->php_ext = $php_ext;
$this->request = $request;
$this->mod_facebook_share_hits = $mod_facebook_share_hits;
}
public function base($post = 0) { //รับค่า post id จาก event
global $phpbb_container, $phpEx, $phpbb_root_path;
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
$db = $this->db;
$user = $this->user;
$auth = $this->auth;
$cache = $this->cache;
$template = $this->template;
$phpbb_content_visibility = $this->phpbb_content_visibility;
$phpbb_dispatcher = $this->dispatcher;
$config = $this->config;
$php_ext = $this->php_ext;
$forum_id = request_var('f', 0);
$topic_id = request_var('t', 0);
$post_id = request_var('p', 0);
$voted_id = request_var('vote_id', array('' => 0));
$HTTP_USER_AGENT = $this->request->server('HTTP_USER_AGENT', '');
$debug = request_var('debug', 0);
file_put_contents('user_agent.txt', $HTTP_USER_AGENT);
if (preg_match("/facebook/i", $HTTP_USER_AGENT) || $debug == 1) {
// $voted_id = (sizeof($voted_id) > 1) ? array_unique($voted_id) : $voted_id;
$sql_array = array(
'SELECT' => '*',
'FROM' => array(
POSTS_TABLE => 'p',
),
'LEFT_JOIN' => array(
array(
'FROM' => array(TOPICS_TABLE => 't'),
'ON' => 'p.topic_id = t.topic_id',
)
),
'WHERE' => 'p.post_id = ' . $post . ' OR p.topic_id = ' . $topic_id,
);
$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query($sql);
$topic_data = $db->sql_fetchrow($result);
$og_attachments = array();
//echo $sql;
$sql2 = 'SELECT *
FROM ' . ATTACHMENTS_TABLE . "
WHERE topic_id = '" . $topic_data['topic_id'] . "' AND post_msg_id = '" . $topic_data['post_id'] . "' ";
$result2 = $db->sql_query($sql2);
while ($data = $db->sql_fetchrow($result2)) {
$og_attachments[] = $data; //บันทึกข้อมูล รูปจากที่มีการแนบไฟล์ จาก post นั้น
// echo " ".$data['attach_id'];
}
// #ต้องเอา post ไปหา attachments
// เก็บเป็น array $og_attachments
$og_url = generate_board_url();
$a_row_img = array();
#ดึงรุปงาน [img]
$post = $topic_data['post_text'];
//ดึงรูปจาก bbcode img
preg_match_all('/src="(.+?)&sid=(.+?)"/', $post, $img);
if (is_array($img[1])) {
foreach ($img[1] as $ki => $img_value) {
$img_value = str_replace('./../../', $og_url . '/', $img_value);
$a_row_img[] = $img_value;
}
}
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $post, $img);
if (is_array($img[1])) {
foreach ($img[1] as $ki => $img_value) {
$img_value = str_replace('./../../', $og_url . '/', $img_value);
$a_row_img[] = $img_value;
}
}
// ดึงรูปจากที่มีการแทรกไฟล์
if (is_array($og_attachments)) {
$a_row_img = array();
foreach ($og_attachments as $key2 => $value2) {
if (in_array($value2['mimetype'], array('image/gif', 'image/jpeg', 'image/png', 'image/bmp'))) {
$a_row_img[] = './download/file.php?id=' . $value2['attach_id'];
$a_row_img[] = generate_board_url() . '/download/file.php?id=' . $value2['attach_id'];
print_r($value2);
}
}
}
// ดึงรูปภาพจากค่า config
if (!empty($this->config['fb_url_image'])) {
$logo_img = $this->config['fb_url_image'];
}
if (empty($a_row_img)) {
$a_row_img = array($logo_img);
}
// ลบไฟล์ซ้ำ
$a_row_img = array_unique($a_row_img);
$a_img = array();
foreach ($a_row_img as $img) {
$a_img[] = str_replace('./', $og_url . '/', $img);
}
$a_img = array_unique($a_img);
// ลบรูปที่ เป็นยิ้มออก
$a_smiley_url = array();
$board_url = generate_board_url();
//ตัดไอคอลยิ้ม
$sql = 'SELECT smiley_url FROM ' . SMILIES_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
$a_smiley_url[] = $board_url . '/images/smilies/' . $row['smiley_url'];
}
//-----
$a_img_remove_smile = array();
foreach ($a_img as $k => $v) {
if (!in_array($v, $a_smiley_url)) {
$a_img_remove_smile[] = $v;
}
}
$a_img = $a_img_remove_smile;
// ถ้าไม่มีรูปก็มาดึงรูปจากส่วนนี้
if (empty($a_img)) {
$a_img = array(($logo_img) ? $logo_img : 'ใส่รูป หรือลิ้งค์ รู)ปที่ต้องการ');
}
// print_r($a_img);
$limit = 3;
$i = 0;
// ลูปส่งค่ารูปไปแสดง
foreach ($a_img as $k => $img) {
$i++;
if (strpos($img, "ttps://")) {
$template->assign_block_vars('og_row_img', array(
'META_TYPE' => 'og:image',
'OG_IMAGE' => $img,
)
);
$template->assign_block_vars('og_row_img_url', array(
'META_TYPE_URL' => 'og:image:url',
'OG_IMAGE' => $img,
)
);
} else {
$template->assign_block_vars('og_row_img', array(
'META_TYPE' => 'og:image',
'OG_IMAGE' => $img,
)
);
$template->assign_block_vars('og_row_img_url', array(
'META_TYPE_URL' => 'og:image:url',
'OG_IMAGE' => $img,
)
);
}
//เมือถึงกำหนดที่จะแสดงรูป ให้หยุดการทำงาน
if ($i >= $limit) {
break;
}
}
$og_desc = ''; // หาจาก content
$og_url = generate_board_url();
$viewtopic_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=" . $topic_data['forum_id'] . "&t=" . $topic_data['topic_id'] . "&p=" . $topic_data['post_id'] . "#p" . $topic_data['post_id'] . (($start == 0) ? '' : "&start=$start") . ((strlen($u_sort_param)) ? "&$u_sort_param" : '') . (($highlight_match) ? "&hilit=$highlight" : '')); //สร้าง url ที่จะส่งโพส
$viewtopic_url_share = str_replace('./../../viewtopic.php', './app.php/m_share_facebook/viewtopic_share.php', $viewtopic_url);
$template->assign_vars(array(
'OG_URL' => str_replace('./', $og_url . '/', $viewtopic_url_share), //ลิ้งไปยังโพสนั้น
'OG_DESCRIPTION' => $topic_data['post_text'], //เนื้อหาภายในโพส
'OG_PAGE_TITLE' => $topic_data['post_subject'], //หัวข้อโพส
));
} else {
redirect(append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t=$topic_id" . "&p=$post_id" . (($forum_id) ? "&f=$forum_id" : ''))); //ถ้าอยู่นอกเงือนไข
}
return $this->helper->render('viewtopic_share_body.html'); //ส่งไปแทมเพลต
}
}
อิธิบายการทำงาน
1.ส่ววนี้จะเป็นการตรวจสอบข้อมูล จาก post_id ที่ไ้ดรับมา เพื่อดึงข้อมูลที่จำเป็นออกมา
$sql_array = array(
'SELECT' => '*',
'FROM' => array(
POSTS_TABLE => 'p',
),
'LEFT_JOIN' => array(
array(
'FROM' => array(TOPICS_TABLE => 't'),
'ON' => 'p.topic_id = t.topic_id',
)
),
'WHERE' => 'p.post_id = ' . $post . ' OR p.topic_id = ' . $topic_id,
);
$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query($sql);
$topic_data = $db->sql_fetchrow($result);
2.เมื่อเราได้ข้อมูลจาก post_id มาแล้ว เราจะทำดึง ข้อมูล ของ topic_id มาใช่ด้วย สำหรับค้นหาไฟล์ ที่แนบ อยู่ภายในโพสนั้น
$sql2 = 'SELECT *
FROM ' . ATTACHMENTS_TABLE . "
WHERE topic_id = '" . $topic_data['topic_id'] . "' AND post_msg_id = '" . $topic_data['post_id'] . "' ";
$result2 = $db->sql_query($sql2);
while ($data = $db->sql_fetchrow($result2)) {
$og_attachments[] = $data; //บันทึกข้อมูล รูปจากที่มีการแนบไฟล์ จาก post นั้น
}
3.ในส่วนนี้จะเป้นส่วนที่จะหารูปภาพ มาจากการใช้ BBcode img โดยการใช้ ฟั่งก์ชั่น preg_match_all
4. ส่ววนี้เป็นดึงรูปมาจาก ไฟล์ที่เราแนบไฟล์
$post = $topic_data['post_text'];
//ดึงรูปจาก bbcode img
preg_match_all('/src="(.+?)&sid=(.+?)"/', $post, $img);
if (is_array($img[1])) {
foreach ($img[1] as $ki => $img_value) {
$img_value = str_replace('./../../', $og_url . '/', $img_value);
$a_row_img[] = $img_value;
}
}
preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $post, $img);
if (is_array($img[1])) {
foreach ($img[1] as $ki => $img_value) {
$img_value = str_replace('./../../', $og_url . '/', $img_value);
$a_row_img[] = $img_value;
}
}
5. ถ้าหากไม่มีไฟล์ที่แนบ กับ รูปภาพที่มีอยู่อยู่ใน BBcode img เราจะไปดึงรูปที่เป็น config เอาไว มาใช้// ดึงรูปจากที่มีการแทรกไฟล์
if (is_array($og_attachments)) {
$a_row_img = array();
foreach ($og_attachments as $key2 => $value2) {
if (in_array($value2['mimetype'], array('image/gif', 'image/jpeg', 'image/png', 'image/bmp'))) {
$a_row_img[] = './download/file.php?id=' . $value2['attach_id'];
$a_row_img[] = generate_board_url() . '/download/file.php?id=' . $value2['attach_id'];
print_r($value2);
}
}
}
if (!empty($this->config['fb_url_image'])) {
$logo_img = $this->config['fb_url_image'];
}
6. เป้นการเอาไอคอล รูป ยิ้มออก// ลบไฟล์ซ้ำ
$a_row_img = array_unique($a_row_img);
$a_img = array();
foreach ($a_row_img as $img) {
$a_img[] = str_replace('./', $og_url . '/', $img);
}
7.ทำการบันทึกข้อมูลรูปภาพ เพื่อส่งไปยัง Facebook แสดง// ลบรูปที่ เป็นยิ้มออก
$a_smiley_url = array();
$board_url = generate_board_url();
//ตัดไอคอลยิ้ม
$sql = 'SELECT smiley_url FROM ' . SMILIES_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
$a_smiley_url[] = $board_url . '/images/smilies/' . $row['smiley_url'];
}
foreach ($a_img as $k => $img) {
$i++;
if (strpos($img, "ttps://")) {
$template->assign_block_vars('og_row_img', array(
'META_TYPE' => 'og:image',
'OG_IMAGE' => $img,
)
);
$template->assign_block_vars('og_row_img_url', array(
'META_TYPE_URL' => 'og:image:url',
'OG_IMAGE' => $img,
)
);
} else {
$template->assign_block_vars('og_row_img', array(
'META_TYPE' => 'og:image',
'OG_IMAGE' => $img,
)
);
$template->assign_block_vars('og_row_img_url', array(
'META_TYPE_URL' => 'og:image:url',
'OG_IMAGE' => $img,
)
);
}
//เมือถึงกำหนดที่จะแสดงรูป ให้หยุดการทำงาน
if ($i >= $limit) {
break;
}
}