Thread: [Comoblog-commit] comoblog/batch comoblog_batch.php,NONE,1.1 easymoblog_batch.php,1.1.1.1,NONE
Status: Inactive
Brought to you by:
markwallis
|
From: Mark W. \(a. serialmonkey\) <mar...@us...> - 2005-09-02 01:35:06
|
Update of /cvsroot/comoblog/comoblog/batch In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16048 Added Files: comoblog_batch.php Removed Files: easymoblog_batch.php Log Message: Batch script rename easy->co --- easymoblog_batch.php DELETED --- --- NEW FILE: comoblog_batch.php --- <?php @ignore_user_abort(true); // includes //////////////////////////////////////////////////////////////// // // include configuration file require_once (dirname(__FILE__).'/../include/config.inc.php'); // include PEAR libraries include (dirname(__FILE__).'/../include/pear/Net/POP3.php'); include (dirname(__FILE__).'/../include/pear/Mail/mimeDecode.php'); // // end includes //////////////////////////////////////////////////////////// @set_time_limit(CFG_BATCH_TIMEOUT); // functions /////////////////////////////////////////////////////////////// // function topics_list () { $query = "select * from ".CFG_MYSQL_TABPREFIX."topics"; $res = mysql_query($query); $topics = array(); if ($res && mysql_num_rows($res) > 0) { $topics = array(); while ($row = mysql_fetch_assoc($res)) $topics[] = $row; return ($topics); } return (false); } function post_add ($post) { // check if subject contains topic keyword $topics = topics_list (); if ($topics) { for ($t = 0; $t < count($topics); $t++) { if (eregi ('\['.$topics[$t]['topic_keyword'].'\]', $post['post_mail_subject'])) { $post['topic_id'] = $topics[$t]['topic_id']; $post['post_mail_subject'] = trim(eregi_replace ('\['.$topics[$t]['topic_keyword'].'\]','', $post['post_mail_subject'])); } } } // replace img cid foreach ($post['images_cid'] as $k => $v) { $new_v = str_replace ('<', '', $v); $post['images_cid'][$k] = str_replace ('>', '', $new_v); } // clean up text and html if ($post['post_ctype'] == 'text/plain') $post['post_mail_body'] = clean_up_txt($post['post_mail_body'], true); else $post['post_mail_body'] = clean_up_html($post['post_mail_body'], true); // apply filters global $POST_FILTERS; if (count($POST_FILTERS) > 0) { for ($filter_cnt = 0; $filter_cnt < count($POST_FILTERS); $filter_cnt++) { include (CFG_BASE_PATH.'/modules/'.$POST_FILTERS[$filter_cnt].'/'.$POST_FILTERS[$filter_cnt].'_post_filter.php'); } } // current timestamp $now = time(); // count images $howmany_images = count($post['images']); $query = " insert into ".CFG_MYSQL_TABPREFIX."posts ( post_added, post_ctype, post_images, post_mail_from, post_mail_date, post_mail_subject, post_mail_body, post_mail_user_agent, topic_id) values ( '".$now."', '".addslashes($post['post_ctype']) ."', '".$howmany_images ."', '".addslashes($post['post_mail_from']) ."', '".strtotime($post['post_mail_date']) ."', '".addslashes($post['post_mail_subject']) ."', '".addslashes($post['post_mail_body']) ."', '".addslashes($post['post_mail_user_agent']) ."', '".$post['topic_id'] ."') "; $res = mysql_query($query); $post_id = mysql_insert_id(); for ($i = 0; $i < $howmany_images; $i++) { if ($post['images_mime'][$i] == 'image/jpg') $post['images_extension'][$i] = 'jpg'; elseif ($post['images_mime'][$i] == 'image/gif') $post['images_extension'][$i] = 'gif'; elseif ($post['images_mime'][$i] == 'image/png') $post['images_extension'][$i] = 'png'; //echo "+++".$post['images_cid'][$i]."+++"; //echo $post['post_mail_body']; if ($post['images_cid'][$i] != '' && strpos($post['post_mail_body'],$post['images_cid'][$i]) == true) $img_display = 'inline'; else $img_display = 'attach'; //echo $img_display; $query = " insert into ".CFG_MYSQL_TABPREFIX."images ( post_id, img_extension, img_mime, img_display ) values ( '".$post_id ."', '".$post['images_extension'][$i]."', '".$post['images_mime'][$i] ."', '".$img_display ."' ) "; $res = mysql_query($query); $post['images_id'][$i] = mysql_insert_id (); // copy image file, delete tmp file $copy = copy($post['images_tmp'][$i], CFG_BASE_PATH.'/img/posts/'.$post['images_id'][$i].'.'.$post['images_extension'][$i]); @unlink ($post['images_tmp'][$i]); // create thumbnail if image is too large $thumb = create_thumbnail($post['images_id'][$i], $post['images_extension'][$i], CFG_IMG_MAX_W, CFG_IMG_MAX_H, CFG_GD_VERSION); } for ($i = 0; $i < $howmany_images; $i++) { // aggiorno body sostituendo cid con images_id $post['post_mail_body'] = str_replace ('cid:', CFG_SITE_URL.'img/posts/', $post['post_mail_body']); $post['post_mail_body'] = str_replace ($post['images_cid'][$i], $post['images_id'][$i].'.'.$post['images_extension'][$i], $post['post_mail_body']); } $query = " update ".CFG_MYSQL_TABPREFIX."posts set post_mail_body = '".addslashes($post['post_mail_body'])."' where post_id = '".$post_id."' "; $res = mysql_query($query); return ($post_id); } // // end functions /////////////////////////////////////////////////////////// // begin script /////////////////////////////////////////////////////////// // if (!isset($BATCH_LOG)) { $BATCH_LOG = true; } if ($BATCH_LOG) { Header ('Content-type: text/plain'); } $date = getdate(time()); if ($BATCH_LOG) { echo "------------------------------------------------------------\n"; echo " EasyMoblog ".CFG_VERSION." batch - ".$date['year']."-".$date['mon']."-".$date['mday']." ".$date['hours'].":".$date['minutes'].":".$date['seconds']."\n"; echo "------------------------------------------------------------\n"; } // pop3 connection and log-in $pop3 =& new Net_POP3(); $pop3->connect(CFG_MAIL_POP, 110); $pop3->login(CFG_MAIL_USER, CFG_MAIL_PW); $howmany_msg = $pop3->numMsg(); if ($howmany_msg == 0) { if ($BATCH_LOG) echo "\n ** No mail found **"; // pop3 disconnect $pop3->disconnect(); } else { // message list $list = $pop3->getListing(); for ($c = 0; $c < count($list); $c++) { $msg[$c] = $pop3->getMsg($list[$c]['msg_id']); $pop3->deleteMsg($list[$c]['msg_id']); } // pop3 disconnect $pop3->disconnect(); // posts counter $post_cnt = 0; // decode all messages using Mail_mimeDecode for ($msg_cnt = 0; $msg_cnt < count($list); $msg_cnt++) { $myDecoder[$msg_cnt] = new Mail_mimeDecode ($msg[$msg_cnt]); $params['include_bodies'] = true; $params['decode_bodies'] = true; $params['decode_headers'] = true; $decoded_msg[$msg_cnt] = $myDecoder[$msg_cnt]->decode($params); //print_r ($decoded_msg[$msg_cnt]); // uncomment for debug /* HEADER FILTERS */ if (CFG_ALLOWED_FROM != '') { if ($BATCH_LOG) echo "\n+checking From Headers... (msg ".$msg_cnt.")"; $allowed_from = explode (',',CFG_ALLOWED_FROM); $ignore = 1; for ($f = 0; $f < count($allowed_from); $f++) { if (eregi($allowed_from[$f], $decoded_msg[$msg_cnt]->headers['from'])) { $ignore = 0; break; } } } /* END HEADER FILTERS */ if ($ignore > 0) { if ($BATCH_LOG) echo "\n -- message ".$msg_cnt." ignored (cause: From header not allowed)"; } else { $posts[$post_cnt]['msg_id'] = $msg_cnt; /////////////////////////////////////////////////////////////////////////////////////////////// // MSG HEADERS - START // // subject $posts[$post_cnt]['post_mail_subject'] = $decoded_msg[$msg_cnt]->headers['subject']; // date $posts[$post_cnt]['post_mail_date'] = $decoded_msg[$msg_cnt]->headers['date']; // from $posts[$post_cnt]['post_mail_from'] = stripslashes($decoded_msg[$msg_cnt]->headers['from']); // user-agent || x-mailer if ($decoded_msg[$msg_cnt]->headers['user-agent'] == '') $posts[$post_cnt]['post_mail_user_agent'] = $decoded_msg[$msg_cnt]->headers['x-mailer']; else $posts[$post_cnt]['post_mail_user_agent'] = $decoded_msg[$msg_cnt]->headers['user-agent']; // mime type (ctype_primary/ctype_secondary) $posts[$post_cnt]['post_ctype'] = strtolower($decoded_msg[$msg_cnt]->ctype_primary . "/" . $decoded_msg[$msg_cnt]->ctype_secondary); // // MSG HEADERS - END /////////////////////////////////////////////////////////////////////////////////////////////// $posts[$post_cnt]['images'] = array(); $posts[$post_cnt]['images_mime'] = array(); $posts[$post_cnt]['images_cid'] = array(); $posts[$post_cnt]['images_tmp'] = array(); if (is_array($decoded_msg[$msg_cnt]->parts)) { // multipart for ($p = 0; $p < count($decoded_msg[$msg_cnt]->parts); $p++) { $text_plain = 0; $text_html = 0; if (is_array($decoded_msg[$msg_cnt]->parts[$p]->parts)) { // multipart for ($pp = 0; $pp < count($decoded_msg[$msg_cnt]->parts[$p]->parts); $pp++) { $ctype = strtolower ($decoded_msg[$msg_cnt]->parts[$p]->parts[$pp]->ctype_primary."/".$decoded_msg[$msg_cnt]->parts[$p]->parts[$pp]->ctype_secondary); if (($ctype == 'text/html' && $text_html == 0) || ($ctype == 'text/plain' && $text_html == 0 && $text_plain == 0)) { $posts[$post_cnt]['post_mail_body'] = $decoded_msg[$msg_cnt]->parts[$p]->parts[$pp]->body; if ($ctype == 'text/plain') $text_plain += 1; elseif ($ctype == 'text/html') $text_html += 1; $posts[$post_cnt]['post_ctype'] = $ctype; } else { $ctype = ''; // check if part body is a valid image file $now = str_replace(' ', '', microtime()).rand(); $fp = fopen (CFG_BASE_PATH.'/img/tmp/'.$now, 'wb'); fwrite ($fp, $decoded_msg[$msg_cnt]->parts[$p]->parts[$pp]->body); fclose($fp); $imgsize = @getimagesize(CFG_BASE_PATH.'/img/tmp/'.$now); // @unlink(CFG_BASE_PATH.'/tmp/'.$now); if ($imgsize) { if ($imgsize[2] == 1) $ctype = 'image/gif'; elseif($imgsize[2] == 2) $ctype = 'image/jpg'; elseif($imgsize[2] == 3) $ctype = 'image/png'; if ($ctype != '') { array_push ($posts[$post_cnt]['images'], $decoded_msg[$msg_cnt]->parts[$p]->parts[$pp]->body); array_push ($posts[$post_cnt]['images_mime'], $ctype); array_push ($posts[$post_cnt]['images_cid'], $decoded_msg[$msg_cnt]->parts[$p]->parts[$pp]->headers['content-id']); array_push ($posts[$post_cnt]['images_tmp'], CFG_BASE_PATH.'/img/tmp/'.$now); } else @unlink(CFG_BASE_PATH.'/img/tmp/'.$now); } } } } else { $ctype = strtolower ($decoded_msg[$msg_cnt]->parts[$p]->ctype_primary."/".$decoded_msg[$msg_cnt]->parts[$p]->ctype_secondary); if (($ctype == 'text/html' && $text_html == 0) || ($ctype == 'text/plain' && $text_html == 0 && $text_plain == 0)) { $posts[$post_cnt]['post_mail_body'] = $decoded_msg[$msg_cnt]->parts[$p]->body; if ($ctype == 'text/plain') $text_plain++; elseif ($ctype == 'text/html') $text_html++; $posts[$post_cnt]['post_ctype'] = $ctype; } else { $ctype = ''; // check if part body is a valid image file $now = str_replace(' ', '', microtime()).rand(); $fp = fopen (CFG_BASE_PATH.'/img/tmp/'.$now, 'wb'); fwrite ($fp, $decoded_msg[$msg_cnt]->parts[$p]->body); fclose($fp); $imgsize = @getimagesize(CFG_BASE_PATH.'/img/tmp/'.$now); //@unlink(CFG_BASE_PATH.'/tmp/'.$now); if ($imgsize) { if ($imgsize[2] == 1) $ctype = 'image/gif'; elseif($imgsize[2] == 2) $ctype = 'image/jpg'; elseif($imgsize[2] == 3) $ctype = 'image/png'; if ($ctype != '') { array_push ($posts[$post_cnt]['images'], $decoded_msg[$msg_cnt]->parts[$p]->body); array_push ($posts[$post_cnt]['images_mime'], $ctype); array_push ($posts[$post_cnt]['images_cid'], $decoded_msg[$msg_cnt]->parts[$p]->headers['content-id']); array_push ($posts[$post_cnt]['images_tmp'], CFG_BASE_PATH.'/img/tmp/'.$now); } else @unlink(CFG_BASE_PATH.'/img/tmp/'.$now); } } } } $post_cnt++; } else { // multipart $ctype = strtolower($decoded_msg[$msg_cnt]->ctype_primary.'/'.$decoded_msg[$msg_cnt]->ctype_secondary); if ($ctype == 'text/plain' || $ctype == 'text/html') { $posts[$post_cnt]['post_mail_body'] = $decoded_msg[$msg_cnt]->body; $posts[$post_cnt]['post_ctype'] = $ctype; } else { $ctype = ''; // check if part is a valid image file $now = str_replace(' ', '', microtime()).rand(); $fp = fopen (CFG_BASE_PATH.'/img/tmp/'.$now, 'wb'); fwrite ($fp, $decoded_msg[$msg_cnt]->body); fclose($fp); $imgsize = @getimagesize(CFG_BASE_PATH.'/img/tmp/'.$now); if ($imgsize) { if ($imgsize[2] == 1) $ctype = 'image/gif'; elseif($imgsize[2] == 2) $ctype = 'image/jpg'; elseif($imgsize[2] == 3) $ctype = 'image/png'; if ($ctype != '') { array_push ($posts[$post_cnt]['images'], $decoded_msg[$msg_cnt]->body); array_push ($posts[$post_cnt]['images_mime'], $ctype); array_push ($posts[$post_cnt]['images_cid'], $decoded_msg[$msg_cnt]->headers['content-id']); array_push ($posts[$post_cnt]['images_tmp'], CFG_BASE_PATH.'/img/tmp/'.$now); } } else @unlink(CFG_BASE_PATH.'/img/tmp/'.$now); } $post_cnt++; } } } if ($BATCH_LOG) echo "\n"; for ($c = 0; $c < $post_cnt; $c++) { $posts[$c]['id'] = post_add ($posts[$c]); if ($BATCH_LOG) echo "\n ++ msg ".($posts[$c]['msg_id']+1)." added\t(ctype: ".$posts[$c]['post_ctype'].")\t(images: ".count($posts[$c]['images']).")"; } } // close db connection mysql_close(); $date = getdate(time()); if ($BATCH_LOG) { echo "\n\n------------------------------------------------------------\n"; echo " batch terminated - ".$date['year']."-".$date['mon']."-".$date['mday']." ".$date['hours'].":".$date['minutes'].":".$date['seconds']."\n"; echo "------------------------------------------------------------\n\n"; } // // end script ////////////////////////////////////////////////////////////// ?> |