BLOG:CMS :: Support Forum

Support Forum for BLOG:CMS

You are not logged in.

#1 23.10.2004 15:19

ihra
BLOG:CMS Senior
From: Helsinki, Finland
Registered: 03.08.2004
Posts: 83
Website

NP_PostMan (improved, with mms/mail support)

Version 1.1 big_smile

See more info at: http://forum.nucleuscms.org/viewtopic.php?t=5043

Get whole package at www.nominaali.com/media/public/NP_PostM … logcms.zip - you will need additional files along this code. (you can get version for nucleus cms from above forum-link)
- added: support for prefix and options to body (mms-messages don't have subject) so now spam-filtering works in mms-messages. If there is no prefix in body or subject messages won't get posted (spam prevention).

Code itself for version 1.1:

Code:

<?php
/**
  * BLOG:CMS: PHP/MySQL Personal Content Management System
  * http://blogcms.com/
  * http://forum.blogcms.com/
  *
  * 2003-2004, (c) Radek HULAN
  * http://hulan.info/
  *
  * Portions of this software are based on Nucleus CMS
  * http://nucleuscms.org/
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
**/

/**
   NP_PostMan usage:
    - point your browser to www.yoursite.com/action.php?action=plugin&name=PostMan
    or
    - set wget / lynx to your cron to extract messages automatically (like in every 5 minutes)
**/
   
global $DIR_PLUGINS;
if (!is_dir($DIR_PLUGINS)) die;
require($DIR_PLUGINS."postman/POP3.php");
require($DIR_PLUGINS."postman/mimeDecode.php");


class NP_PostMan extends NucleusPlugin {
   function getName() { return 'PostMan';    }
   function getAuthor()  { return 'anand, Ihra, Radek HULAN';    }
   function getURL() {   return 'http://blogcms.com/'; }
   function getVersion() { return '1.1'; }
   function getDescription( ) { return 'Post to blog by mail and MMS. Usage: Point your browser to www.yoursite.com/action.php?action=plugin&name=PostMan. Or set wget / lynx to your cron to extract messages automatically (like in every 5 minutes).'; }
   
   function install() {
       $this->createOption('MailUser','Enter the mail user name.','text','');
       $this->createOption('MailPassword','Enter the mail password.','password','');
       $this->createOption('MailURL','Enter the mail host','text','localhost');
       $this->createOption('MailPort','Enter the mail POP3 port','text','110');
       $this->createOption('DefaultUser','Enter the BLOG:CMS user name.','text','');
       $this->createOption('DefaultPassword','Enter the BLOG:CMS password.','password','');
       $this->createOption('MustContainPrefix','Should the mail contain a prefix in body or in subject?','yesno','yes');
       $this->createOption('SubjectPrefix','If yes, what is the prefix','text','BLOGIT:');
       $this->createOption('OutputToBrowser','Send output to browser ?','yesno','yes');
       $this->createOption('DefaultPublish','Publish straight away ?','yesno','yes');
   }

   function init() {
       $this->moreKeyword     = '[more]';
       $this->categoryKeyword = 'category';
       $this->blogKeyword     = 'blog';
       $this->optionsKeyword  = '[options]';
       $this->type            = "html";
       $this->user            = '';
       $this->password        = '';
   }
   
   function doAction($type) {
       global $manager, $blog, $CONF;

      // connect to pop3-server
       $npop3 =& new Net_POP3();
       if (!$npop3->connect($this->getOption('MailURL'),$this->getOption('MailPort') )) die('Cannot connect to mail server');
       if (!$npop3->login($this->getOption('MailUser'), $this->getOption('MailPassword'))) die('Cannot login to mail server');
       $num = $npop3->numMsg();
      
      // to mime::decode
       $params['include_bodies'] = TRUE;
       $params['decode_bodies']  = FALSE;
       $params['decode_headers'] = TRUE;

      // login to BLOG:CMS
      $this->user     = $this->getOption('DefaultUser');
      $this->password = $this->getOption('DefaultPassword');
      $mem = new MEMBER();
      if (!$mem->login($this->user, $this->password)) {
        echo "Could not log in";
        return ;
      }
   
      // default blog a team rights?
      $blog    =& $manager->getBlog($CONF['DefaultBlog']);
      $blogid = $blog->blogid;
      if (!BLOG::existsID($blogid)) {
        echo "No such blog";
        return ;
      }
      if (!$mem->teamRights($blogid)) {
        echo "Not a team member";
        return;
      }

       for ($msg = 1; $msg <= $num; $msg++) {
         // get email
           $message = $npop3->getMsg($msg);
           $params['input'] = $message;
           $structure = Mail_mimeDecode::decode($params);

         // from
         $from = trim($structure->headers['from']);
         if (preg_match('/^[^<>]+<([^<>]+)>$/',$from,$matches)) $from = $matches[1];

         // content
         $body = get_content($structure, $mem->getID());
         
         // subject
           $subject = $structure->headers['subject'];
         if (!empty($structure->ctype_parameters['charset']))
         if (function_exists('iconv'))
               $subject = iconv($structure->ctype_parameters['charset'],_CHARSET,$subject);

           if ( $this->getOption('OutputToBrowser') == 'yes' ) {
               $messageType = $this->createMT($structure);
               echo "Message Type is ".$messageType."<br>";
           }

           if ( (preg_match('/'.$this->getOption('SubjectPrefix').'/', $subject)) || 
               ($this->getOption('MustContainPrefix') == "no") || 
               preg_match('/'.$this->getOption('SubjectPrefix').'/', $body))
         {
            // remove BLOGIT: prefix
               if ( $this->getOption('MustContainPrefix') == "yes")
               $subject = trim(str_replace($this->getOption('SubjectPrefix'), '', $subject));

            // Remove everything before p.smil -attachment (operator-specified "not supporting mms" -message)
            if (preg_match('/'.'.smil'.'/',$body)) {
               $smilPos = intval(strpos($body, '.smil'));
               $body    = substr($body,$smilPos+17, strlen($body)-$smilPos);
            }
              
               // body
              $body = trim($body);
              $words = explode($this->moreKeyword,$more);
            // more
               $more = $words[1];
               $category = false;
            $timestamp = $blog->getCorrectTime();

            // blog/category options in subject
            if (preg_match('/'.$this->optionsKeyword.'/', $subject)) {
              // different blog?
                  if (preg_match('/'.$this->blogKeyword.'/', $subject)) $blogid  = $this->parseToken($subject,$this->blogKeyword);
              // different category?
                  if (preg_match('/'.$this->categoryKeyword.'/', $subject))  $category  = getCatIDFromName($this->parseToken($subject,$this->categoryKeyword));
                  $optionPos = intval(strpos($subject, $this->optionsKeyword));
                  if ( $optionPos ) $subject = substr($subject, 0, $optionPos);
                  $subject = trim($subject);
               }
               // blog/catogory options in body (might be both, but so what)
            if (preg_match('/'.$this->getOption('SubjectPrefix').'/', $body)) 
              {
              // different blog?
                  if (preg_match('/'.$this->blogKeyword.'/', $body)) 
                   { 
                     $blogid  = $this->parseToken($body,$this->blogKeyword);
                   }
              // different category?
                  if (preg_match('/'.$this->categoryKeyword.'/', $body)) $category  = getCatIDFromName($this->parseToken($body,$this->categoryKeyword));
                  $prefixplace =0;
                  if ( $this->getOption('MustContainPrefix') == "yes")
                  {
                       $prefixplace = strpos($body,$this->getOption('SubjectPrefix'));
                     $body = trim(str_replace($this->getOption('SubjectPrefix'), '', $body));
                  }
                  // take subject from body (no subject in subject-line = between prefix and options -keywords in body)
                  // remove <p> and/or \n (3 characters)
                    $subjectlength = strpos($body,';');
                  $subject = trim(substr($body,$prefixplace,$subjectlength-3));
                  $body = substr($body,strrpos($body,';')+1,strlen($body));
                  $body = trim($body);
                   
              } // body options
              
              $morePos = intval(strpos($body, $this->moreKeyword));
               if ( $morePos ) {
                   $more     = substr($body,$morePos,strlen($body)-$morePos);
                   $body     = substr($body, 0, $morePos);
               }
            
                 if ( $this->getOption('OutputToBrowser') == 'yes' )  
                 {       
                    print '<p><b>From</b>: ' . $from . '</p>' . "\n";
                 }
    
            if (!$category) $category = $blog->getDefaultCategory();   
               $this->_addDatedItem($blogid,$subject,$body,$more,0,$timestamp,0,$category,$mem->getID());
            // to debug without deleting your messages, comment this line
               $npop3->deleteMsg($msg);
   
            } // SubjectPrefix
        } // for
        $npop3->disconnect();
   }   

   function createMT(&$structure) {
       return $structure->ctype_primary."/".$structure->ctype_secondary;
   }

   function parseToken($line,$keyword) {
       $words = explode($keyword,$line);
       $words = explode('=',$words[1]);
       $word = explode(';',$words[1]);
       return $word[0];
   }

   function _addDatedItem($blogid,$title, $body, $more,$closed, $timestamp, $future, $category, $memberid) {
      $blog = new BLOG($blogid);
      if ( $this->getOption('DefaultPublish') == 'yes' )
           $draft = 0;
       else
           $draft = 1;
      if ($closed != 1) $closed = 0;
      $store = $blog->getSetting('bconvertbreaks');
      if ( $this->type == "html") $blog->setConvertBreaks(0);
      $itemid = $blog->additem($category, $title, $body, $more, $blogid, $memberid, $timestamp, $closed, $draft);
      if ( $this->getOption('OutputToBrowser') == 'yes' )  {       
         print '<b>Blog</b>: ' . $blogid . '<br />' . "\n";
         print '<b>Category</b>: ' . $category. '<br />' . "\n";
         print '<b>Itemid</b>: ' . $itemid . '<br />' . "\n";
         print '<b>Author</b>: ' . $memberid . '<br />' . "\n";
         print '<b>Subject</b>: ' . $title . '<br />' . "\n";
         print '<b>Body:</b></p><hr />' . $body . '<br>' . $more . '<hr />';
      }
      $blog->setConvertBreaks($store);
   }
   
}   // NP_PostMan

// This function checks if the content is base64 or quoted-printable
function _unencode ( &$part ){
   if ( strtolower($part->headers['content-transfer-encoding']) == 'base64' ) {
      // $part->headers['content-transfer-encoding'] = 'plain';
      $part->body = base64_decode($part->body);
   }
   if ( strtolower($part->headers['content-transfer-encoding']) == 'quoted-printable' ) {
      // $part->headers['content-transfer-encoding'] = 'plain';
      $part->body = quoted_printable_decode($part->body);
   }
}

// if utf-8 is used, decode it correctly
function charsetcheck ( &$part ) {
   if ( strtolower($part->ctype_parameters['charset']) == 'utf-8' ) {
      if (function_exists('iconv'))
        return iconv('utf-8',_CHARSET,$part->body);
      else
        return utf8_decode($part->body);
   } else {
      if (function_exists('iconv'))
        return iconv(strtolower($part->ctype_parameters['charset']),_CHARSET,$part->body);
   }
   return $part->body;
}

// explode meta-parts to find content
function get_content ($part, $memberid) {
   _unencode($part);
   switch ( strtolower($part->ctype_primary) ) {
   case 'multipart':
      $meta_return = '';
      foreach ($part->parts as $section) $meta_return .= get_content($section, $memberid);
      break;
   case 'text':
      if ($part->ctype_secondary=='enriched')
         $meta_return = paragraph( removesignature(charsetcheck($part)), false) . "\n";
      else
         $meta_return = paragraph( removesignature(charsetcheck($part)), true) . "\n";
      break;
   case 'image':
      global $DIR_MEDIA, $CONF;
      // make sure filename does not exists yet
      do {
         $filename =  "$memberid/mms-" . rand(0,999999999) . '.' . $part->ctype_secondary;
      } while ( @file_exists($DIR_MEDIA.$filename) );
      $fp = fopen($DIR_MEDIA.$filename, 'w');
      if (!$fp) die('Cannot write image to '.$DIR_MEDIA.$filename);
      fwrite($fp, $part->body);
      fclose($fp);
      $img = @getimagesize($DIR_MEDIA.$filename);
      if (is_array($img))
        // got image size
        $meta_return = '<div class=leftbox"><%image('. $filename .'|'.$img[0].'|'.$img[1].'|' . $part->ctype_parameters['name'] . ')%></div>'."\n".$meta_return;
      else
        // do not have image size, so just embed it directly
        $meta_return = '<div class="leftbox"><img src="'.$CONF['MediaURL'].$filename.'" alt="'.$part->ctype_parameters['name'].'" /></div>'."\n".$meta_return;
      break;
   case 'application':
      //pgp signature
      if ( $part->ctype_secondary == 'pgp-signature' ) break;
      global $DIR_MEDIA, $CONF;
      do {
         $filename =  "$memberid/app-" . rand(0,999999999) . '.' . $part->ctype_secondary;
      } while ( @file_exists($DIR_MEDIA.$filename) );
      $fp = fopen($DIR_MEDIA.$filename,'w');
      if (!$fp) die('Cannot write attachement to '.$DIR_MEDIA.$filename);
      fwrite($fp, $part->body );
      fclose($fp);
      $meta_return .= '<a href="'.$CONF['MediaURL'].$filename.'">'.$part->ctype_parameters['name'].'</a>'."\n";
      break;
   }      
   return $meta_return;
}
      
// Keeps content until finds a line with '--', removes rest of the content
function removesignature ( $content ) {
   $arrcontent = explode("\n", $content);
   $strcontent = '';
   for ($i = 0; $i<=count($arrcontent); $i++) {
      if ( preg_match('/^--$/',trim($arrcontent[$i])) ) break;
      $strcontent .= $arrcontent[$i];
   }
   $search = array ('/ (\n|\r\n|\r)/','/(\n|\r\n|\r)/');
   $replace = array (' ',"\n");
   return preg_replace($search,$replace,$strcontent);      
}


?>

Last edited by ihra (30.06.2005 13:17)

Offline

 

#2 29.10.2004 08:40

ihra
BLOG:CMS Senior
From: Helsinki, Finland
Registered: 03.08.2004
Posts: 83
Website

Re: NP_PostMan (improved, with mms/mail support)

Upgraded code and links to version 1.1.

Offline

 

#3 29.10.2004 13:18

skara
BLOG:CMS Junior
From: Greece
Registered: 09.10.2004
Posts: 39

Re: NP_PostMan (improved, with mms/mail support)

Thanx 4 updating !
This plugin is very usefull, keep up the good work  wink


the art of war is to know the weakness of your enemy wink

Offline

 

#4 17.11.2004 16:54

ihra
BLOG:CMS Senior
From: Helsinki, Finland
Registered: 03.08.2004
Posts: 83
Website

Re: NP_PostMan (improved, with mms/mail support)

And now wiki is updated as well. Documentation for plugin, with examples, can be found at http://wakka.xiffy.nl/PostMan?v=127a

Offline

 

#5 17.11.2004 17:06

Radek Hulán
Site Admin
From: Prague, Czech Republic
Registered: 17.03.2004
Posts: 2509
Website

Re: NP_PostMan (improved, with mms/mail support)

FYI: this plugin is included in BLOG:CMS 3.5 release smile


--= BLOG:CMS developer =--

Offline

 

#6 19.11.2004 06:53

ihra
BLOG:CMS Senior
From: Helsinki, Finland
Registered: 03.08.2004
Posts: 83
Website

Re: NP_PostMan (improved, with mms/mail support)

Yeah, way to go... we should transfer documentation from NucleusWiki to blog:cms. Mostly to show body/subject options to users.

Offline

 

#7 15.04.2005 23:41

nuckle
newbie...
Registered: 05.09.2004
Posts: 6

Re: NP_PostMan (improved, with mms/mail support)

Well its currently 2.3 and there needs to be a little cohesion I think.

I am having a problem whereby when using postman I am getting an extra but broken p tag at the beginning of the body, like: <p>p> and its ruining content. Plus any chance of getting a newer version of the plugin to install? Any chance of changing the image div from class to id, or allowing that to be asked in the preferences, including the id name, please?
I changed class="leftbox" to id="myvar" so that it uses the css. although I dont have a $meta_return + more $meta_return; at all. maybe my version of the plugin is different.

Anyone know what I am talking about?

Offline

 

#8 15.04.2005 23:43

nuckle
newbie...
Registered: 05.09.2004
Posts: 6

Re: NP_PostMan (improved, with mms/mail support)

When posting I got an error stating something about the search thing not working!!

Offline

 

#9 16.04.2005 01:35

nuckle
newbie...
Registered: 05.09.2004
Posts: 6

Re: NP_PostMan (improved, with mms/mail support)

Ok what I did was uninstall - go through the code with a toothcomb, with Radeks version from http://hulan.cz/download/postman.zip

and fixed a <br> to <br /> but nothing else, and kept my id=myvar instead of using class, of which I would like to see in the options part, i.e. what css var do you want to display the images? and a textbox for entry
Would this require an extra field in the database, or can we just write it in?
Shall I just experiment? [nuckle starts quivering] wink

then I reinstalled it.

At any rate the broken p tag has gone thank goodness, and my problem with the body disspaearing I think had something to do with me putting the prefix in these things [] like this [myblog] in the subject line!!

Oh well, maybe a bit more error trap coding needs to be inserted!!

Last edited by nuckle (16.04.2005 01:39)

Offline

 

#10 16.04.2005 01:41

nuckle
newbie...
Registered: 05.09.2004
Posts: 6

Re: NP_PostMan (improved, with mms/mail support)

and of course the prefix is case sensitive!! duh me!!!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson

TOPlist