เข้ามาเพิ่มคับ สำหรับที่ใช้ Host ซึ่งไม่เปิด allow_url_fopen แต่มี curl ให้ใช้ (เช่น dreamhost) ผมแก้ปัญหาให้แล้ว ลองดูกันนะ ส่วนคนที่ไม่มีทั้ง allow_url_fopen และ curl หมดสิทธิ์คับ
ezauto.php
<?php
$blog_email = '';
$gmail_login = '';
$gmail_pass = '';
///////////////////////////////////////////
define( 'BLOG_EMAIL' , $blog_email );
define( 'GMAIL_LOGIN' , $gmail_login );
define( 'GMAIL_PASSWORD' , $gmail_pass );
define( 'cURL_EXISTS' , function_exists( 'curl_init' ));
///////////////////////////////////////////
/**
* Checking for ability to call url
* Script will not run if not pass this section
*/
if(!cURL_EXISTS && ( ini_get( 'allow_url_fopen' ) == 0 ))
{
echo '<span style="color:red">Error : cURL and allow_url_fopen are disabled ,'
. ' this mean YOU CANNOT RUN THIS SCRIPT ON THIS SERVER (So Sad T_T ) </span>';
exit;
}
///////////////////////////////////////////
@set_time_limit(0);
require_once 'class.phpmailer.php' ;
$url_line_num = file_get_contents( 'count.txt' );
$urllist = import_url_list( 'url.txt' );
// if it seem to ran out of url - stop the script
if( count( $urllist ) <= $url_line_num )
{
echo '<span style="color:red">Error : All provided articles was run out , Please add some new articles .</span>';
exit;
}
///////////////////////////////////////////
/**
* Core Process
*/
$url = $urllist[$url_line_num];
$detail = parse_url( $url );
$mode = $detail['host'];
switch ($mode):
case strpos( $mode , 'ezinearticles' ) !== false:
$article = ezinearticles( $url );
break;
case strpos( $mode , 'directdownloadcenter' ) !== false:
$article = ddlc( $url );
break;
// we can add new site here
default :
echo '<span style="color:red">Error : Unknow Driver , please ensure you are using the support sites.</span>';
exit;
endswitch;
//print_r($article);
/**
* Send Email to Blogger (Post to Blogger)
*/
$post_title = $article['title'];
$post_body = $article['body'];
if( $post_title )
{
sendmail( BLOG_EMAIL , BLOG_EMAIL , 'ea2blog' , GMAIL_LOGIN , GMAIL_PASSWORD ,$post_title, $post_body );
echo 'The provided article was sent to ' . BLOG_EMAIL ;
echo '<h1>'. $post_title .'</h1>';
echo '<p>'. substr( $post_body , 0 , 255 ) .' ..........</p>';
}
else
{
echo '<span style="color:red">Error : Article was not scrapped properly</span>';
}
record(); // update count file
exit;
////////////////////////////////
// Driver
///////////////////////////////
/**
* @name - Direct Download Center
* @note - Coldmoney referral by default
* @domain - directdownloadcenter.com
*/
function ddlc($url){
if(!$url) return false;
$detail = parse_url($url);
$ref = isset($detail['query']) ? $detail['query']:'u=coldmoney';
$url = $detail['scheme'] . '://' . $detail['host'] . $detail['path'];
$page = curl_get($url) ;
preg_match("|<div\sclass=\"dot_border\">\s+<h2>(.*)</h2>\s+(.*)<table width=100%><tr>(.*)<td align=right>\s+</td></tr></table>\s+</div>|s", $page, $match);
$title = trim($match[1]);
$review = $match[2];
$downloadlink = str_replace('.html', '.html?' . $ref, trim(strip_tags($match[3],'<a>')));
$body = $review . $downloadlink;
$article['title'] = $title;
$article['body'] = $body;
return isset($article) ? $article : false ;
}
/**
* @name - EzineArticles
* @domain - ezinearticles.com
*/
function ezinearticles($url){
$content = curl_get($url);
// Get Title , Author Name
if (preg_match('%<font\\sclass="art_title">(.*)</font><br><font\\sclass="copyright">By\\s<a\\shref="http://ezinearticles\\.com/\\?expert=.*">(.*)</a>%', $content, $i1)) {
$title = $i1[1];
$author = $i1[2];
}
// Get Body and Author Bio
if (preg_match('%<div\\sid="body">(.*)</div><div>[a-z0-9<>\\s="]*<div\\sid="sig"\\sclass="sig">(.*)<div>%', $content, $i2)) {
$body = $i2[1];
$bio = $i2[2];
}
$article['title'] = $title;
$article['body'] = 'By '.$author."<br/>\n".$body.$bio;
return isset($article) ? $article : false ;
}
////////////////////////////////
// Function
///////////////////////////////
/**
* Note. This Function is Originally Modify from PHPMailer Class
* Mod for Gmail by ScripDD (http://www.scriptdd.com/)
*/
function sendmail($to_name,$to_email,$from_name,$email_user_send,$email_pass_send,$subject,$body_html,$body_text='') {
$mail = new PHPMailer();
$mail->From=$email_user_send;
$mail->FromName=$from_name;
$mail->AddAddress($to_email,$to_name);
$mail->Subject=$subject;
$mail->Body=$body_html;
$mail->AltBody=$body_text;
$mail->IsHTML(true);
$mail->IsSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth=true;
//$mail->SMTPDebug = true;
$mail->Username=$email_user_send;
$mail->Password=$email_pass_send;
$mail->Send();
$mail->ClearAddresses();
}
function curl_get( $url , $referer='' ){
if( cURL_EXISTS )
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)' );
if($referer !== '' ) curl_setopt ($ch, CURLOPT_REFERER, $referer);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch);
curl_close($ch);
return $result;
}
else
{
return file_get_contents($url);
}
}
function record(){
if (file_exists('count.txt'))
{
$fil = fopen('count.txt', 'r');
$dat = fread($fil, filesize('count.txt'));
$hits = $dat+1;
fclose($fil);
$fil = fopen('count.txt', 'w');
fwrite($fil, $dat+1);
}
else
{
$fil = fopen('count.txt', 'w');
fwrite($fil, 1);
$hits = '1';
fclose($fil);
}
}
function import_url_list($file){
if(file_exists($file)){
$urllist = file($file);
foreach($urllist as $line)
{
if( trim($line) !== '' ) $url[] = trim($line);
}
return $url;
}
else{return false;}
}
?>
Change Note
- เพิ่มการเช็คว่า Host สามารถดึงหน้าเว็บจาก url ได้หรอไม่ ถ้าดึงไม่ได้ แสดง Error แล้วหยุดสคริปท์
- หาก url ที่เตรียมไว้ถูกโพสท์ไปหมดแล้ว จะแสดง Error แล้วเตือนให้ใส่ url เพิ่ม
- เพิ่ม function curl_get() สำหรับจัดการ ระหว่าง file_get_contents และ curl (ใช้ curl ถ้ามี)
- เพิ่มเงื่อนไข ให้ส่ง email เฉพาะที่มีชื่อบทความ และตัวบทความ (Bug) หากมีอันใดอันหนึ่งว่างๆ จะไม่โพสท์
- เปลี่ยนชื่อผู้รับ email ให้เป็น email address ของบล็อกที่ส่ง ทั้งนี้เพื่อให้สามารถเข้าไปเช็คดูว่าบทความใดบล็อกใดโพสท์ไปแล้วบ้าง โดยการเข้าไปดูในโฟลเดอร์ Sent Mail ใน Gmail
ปล. ยังไม่ได้ลองนะคับ มีปัญหาแจ้งด้วย