PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/expressionengine/modules/moblog/mod.moblog.php

https://bitbucket.org/tdevonshire/hoolux
PHP | 2292 lines | 1550 code | 404 blank | 338 comment | 351 complexity | 3ccb719834a6c8cd1f7e19b3ccef466e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2012, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Moblog Module
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Modules
  19. * @category Modules
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class Moblog {
  24. var $cache_name = 'moblog_cache'; // Name of cache directory
  25. var $url_title_word = 'moblog'; // If duplicate url title, this is added along with number
  26. var $message_array = array(); // Array of return messages
  27. var $return_data = ''; // When silent mode is off
  28. var $silent = ''; // yes/no (string) - Returns error information
  29. var $moblog_array = array(); // Row information for moblog being processed
  30. var $fp = ''; // fopen resource
  31. var $pop_newline = "\n"; // Newline for POP Server. Switch to \r\n for Microsoft servers
  32. var $total_size = 0; // Total size of emails being checked in bytes
  33. var $checked_size = 0; // Accumulated size of emails checked thus far in bytes
  34. var $max_size = 5; // Maximum amount of email to check, in MB
  35. var $email_sizes = array(); // The sizes of the new emails being checked, in bytes
  36. var $boundary = FALSE; // Boundary marker in emails
  37. var $multi_boundary = ''; // Boundary for multipart content types
  38. var $newline = '1n2e3w4l5i6n7e8'; // Newline replacement
  39. var $charset = 'auto'; // Character set for main body of email
  40. var $author = ''; // Author of current email being processed
  41. var $body = ''; // Main text contents of email being processed
  42. var $sender_email = ''; // Email address that sent email
  43. var $uploads = 0; // Number of file uploads for this check
  44. var $email_files = array(); // Array containing filenames of uploads for this email
  45. var $emails_done = 0; // Number of emails processed
  46. var $entries_added = 0; // Number of entries added
  47. var $upload_dir_code = ''; // {filedir_2} for entry's
  48. var $upload_path = ''; // Server path for upload directory
  49. var $entry_data = array(); // Data for entry's custom fields
  50. var $post_data = array(); // Post data retrieved from email being processed: Subject, IP, Categories, Status
  51. var $template = ''; // Moblog's template
  52. var $sticky = 'n'; // Default Sticky Value
  53. // These settings are for a specific problem with AT&T phones
  54. var $attach_as_txt = FALSE; // Email's Message as txt file?
  55. var $attach_text = ''; // If $attach_as_txt is true, this is the text
  56. var $attach_name = ''; // If $attach_as_txt is true, this is the name
  57. var $time_offset = '5'; // Number of seconds entries are offset by negatively, higher if you are putting in many entries
  58. var $movie = array(); // Suffixes for accepted movie files
  59. var $audio = array(); // Suffixes for accepted audio files
  60. var $image = array(); // Suffixes for accepted image files
  61. var $files = array(); // Suffixes for other types of accepted files
  62. var $txt_override = FALSE; // When set to TRUE, all .txt files are treated as message text
  63. // ------------------------------------------------------------------------
  64. /**
  65. * Constructor
  66. */
  67. function Moblog()
  68. {
  69. // Make a local reference to the ExpressionEngine super object
  70. $this->EE =& get_instance();
  71. /** -----------------------------
  72. /** Default file formats
  73. /** -----------------------------*/
  74. $this->movie = array('3gp','mov','mpg','avi','movie');
  75. $this->audio = array('mid','midi','mp2','mp3','aac','mp4','aif','aiff','aifc','ram','rm','rpm','wav','ra','rv','wav');
  76. $this->image = array('bmp','gif','jpeg','jpg','jpe','png','tiff','tif');
  77. $this->files = array('doc','xls','zip','tar','tgz','swf','sit','php','txt','html','asp','js','rtf', 'pdf');
  78. if ( ! defined('LD'))
  79. define('LD', '{');
  80. if ( ! defined('RD'))
  81. define('RD', '}');
  82. if ( ! defined('SLASH'))
  83. define('SLASH', '&#47;');
  84. $this->max_size = $this->max_size * 1024 * 1000;
  85. }
  86. // ------------------------------------------------------------------------
  87. /**
  88. * Check for Expired Moblogs
  89. */
  90. function check()
  91. {
  92. $which = $this->EE->TMPL->fetch_param('which', '');
  93. $silent = $this->EE->TMPL->fetch_param('silent', 'yes');
  94. // Backwards compatible with previously documented "true/false" parameters (now "yes/no")
  95. $this->silent = ($silent == 'true' OR $silent == 'yes') ? 'yes' : 'no';
  96. if ($which == '')
  97. {
  98. $this->return_data = ($this->silent == 'yes') ? '' : 'No Moblog Indicated';
  99. return $this->return_data ;
  100. }
  101. $this->EE->lang->loadfile('moblog');
  102. $sql = "SELECT * FROM exp_moblogs WHERE moblog_enabled = 'y'";
  103. $sql .= ($which == 'all') ? '' : $this->EE->functions->sql_andor_string($which, 'moblog_short_name', 'exp_moblogs');
  104. $query = $this->EE->db->query($sql);
  105. if ($query->num_rows() == 0)
  106. {
  107. $this->return_data = ($this->silent == 'yes') ? '' : lang('no_moblogs');
  108. return $this->return_data;
  109. }
  110. // Check Cache
  111. if ( ! @is_dir(APPPATH.'cache/'.$this->cache_name))
  112. {
  113. if ( ! @mkdir(APPPATH.'cache/'.$this->cache_name, DIR_WRITE_MODE))
  114. {
  115. $this->return_data = ($this->silent == 'yes') ? '' : lang('no_cache');
  116. return $this->return_data;
  117. }
  118. }
  119. @chmod(APPPATH.'cache/'.$this->cache_name, DIR_WRITE_MODE);
  120. //$this->EE->functions->delete_expired_files(APPPATH.'cache/'.$this->cache_name);
  121. $expired = array();
  122. foreach($query->result_array() as $row)
  123. {
  124. $cache_file = APPPATH.'cache/'.$this->cache_name.'/t_moblog_'.$row['moblog_id'];
  125. if ( ! file_exists($cache_file) OR (time() > (filemtime($cache_file) + ($row['moblog_time_interval'] * 60))))
  126. {
  127. $this->set_cache($row['moblog_id']);
  128. $expired[] = $row['moblog_id'];
  129. }
  130. elseif ( ! $fp = @fopen($cache_file, FOPEN_READ_WRITE))
  131. {
  132. if ($this->silent == 'no')
  133. {
  134. $this->return_data .= '<p><strong>'.$row['moblog_full_name'].'</strong><br />'.
  135. lang('no_cache')."\n</p>";
  136. }
  137. }
  138. }
  139. if (count($expired) == 0)
  140. {
  141. $this->return_data = ($this->silent == 'yes') ? '' : lang('moblog_current');
  142. return $this->return_data;
  143. }
  144. /** ------------------------------
  145. /** Process Expired Moblogs
  146. /** ------------------------------*/
  147. foreach($query->result_array() as $row)
  148. {
  149. if (in_array($row['moblog_id'],$expired))
  150. {
  151. $this->moblog_array = $row;
  152. if ($this->moblog_array['moblog_email_type'] == 'imap')
  153. {
  154. if ( ! $this->check_imap_moblog())
  155. {
  156. if ($this->silent == 'no' && count($this->message_array) > 0)
  157. {
  158. $this->return_data .= '<p><strong>'.$this->moblog_array['moblog_full_name'].'</strong><br />'.
  159. $this->errors()."\n</p>";
  160. }
  161. }
  162. }
  163. else
  164. {
  165. if ( ! $this->check_pop_moblog())
  166. {
  167. if ($this->silent == 'no' && count($this->message_array) > 0)
  168. {
  169. $this->return_data .= '<p><strong>'.$this->moblog_array['moblog_full_name'].'</strong><br />'.
  170. $this->errors()."\n</p>";
  171. }
  172. }
  173. }
  174. $this->message_array = array();
  175. }
  176. }
  177. if ($this->silent == 'no')
  178. {
  179. $this->return_data .= lang('moblog_successful_check')."<br />\n";
  180. $this->return_data .= lang('emails_done')." {$this->emails_done}<br />\n";
  181. $this->return_data .= lang('entries_added')." {$this->entries_added}<br />\n";
  182. $this->return_data .= lang('attachments_uploaded')." {$this->uploads}<br />\n";
  183. }
  184. return $this->return_data ;
  185. }
  186. /** -------------------------------------
  187. /** Set cache
  188. /** -------------------------------------*/
  189. function set_cache($moblog_id)
  190. {
  191. $cache_file = APPPATH.'cache/'.$this->cache_name.'/t_moblog_'.$moblog_id;
  192. if ($fp = @fopen($cache_file, FOPEN_WRITE_CREATE_DESTRUCTIVE))
  193. {
  194. flock($fp, LOCK_EX);
  195. fwrite($fp, 'hi');
  196. flock($fp, LOCK_UN);
  197. fclose($fp);
  198. }
  199. @chmod($cache_file, FILE_WRITE_MODE);
  200. }
  201. /** -------------------------------------
  202. /** Return errors
  203. /** -------------------------------------*/
  204. function errors()
  205. {
  206. $message = '';
  207. if (count($this->message_array) == 0 OR $this->silent == 'yes')
  208. {
  209. return $message;
  210. }
  211. foreach($this->message_array as $row)
  212. {
  213. $message .= ($message == '') ? '' : "<br />\n";
  214. $message .= ( ! lang($row)) ? $row : lang($row);
  215. }
  216. return $message;
  217. }
  218. // ------------------------------------------------------------------------
  219. /**
  220. * Check Pop3 Moblog
  221. *
  222. *
  223. */
  224. function check_pop_moblog()
  225. {
  226. /** ------------------------------
  227. /** Email Login Check
  228. /** ------------------------------*/
  229. $port = 110;
  230. $ssl = (substr($this->moblog_array['moblog_email_server'], 0, 6) == 'ssl://');
  231. if ($ssl OR stripos($this->moblog_array['moblog_email_server'], 'gmail') !== FALSE)
  232. {
  233. if ( ! $ssl)
  234. {
  235. $this->moblog_array['moblog_email_server'] = 'ssl://'.$this->moblog_array['moblog_email_server'];
  236. }
  237. $port = 995;
  238. }
  239. if ( ! $this->fp = @fsockopen($this->moblog_array['moblog_email_server'], $port, $errno, $errstr, 20))
  240. {
  241. $this->message_array[] = 'no_server_connection';
  242. return FALSE;
  243. }
  244. if (strncasecmp(fgets($this->fp, 1024), '+OK', 3) != 0)
  245. {
  246. $this->message_array[] = 'invalid_server_response';
  247. @fclose($this->fp);
  248. return FALSE;
  249. }
  250. if (strncasecmp($this->pop_command("USER ".base64_decode($this->moblog_array['moblog_email_login'])), '+OK', 3) != 0)
  251. {
  252. // Windows servers something require a different line break.
  253. // So, we change the line break and try again.
  254. $this->pop_newline = "\r\n";
  255. if (strncasecmp($this->pop_command("USER ".base64_decode($this->moblog_array['moblog_email_login'])), '+OK', 3) != 0)
  256. {
  257. $this->message_array[] = 'invalid_username';
  258. $line = $this->pop_command("QUIT");
  259. @fclose($this->fp);
  260. return FALSE;
  261. }
  262. }
  263. if (strncasecmp($this->pop_command("PASS ".base64_decode($this->moblog_array['moblog_email_password'])), '+OK', 3) != 0)
  264. {
  265. $this->message_array[] = 'invalid_password';
  266. $line = $this->pop_command("QUIT");
  267. @fclose($this->fp);
  268. return FALSE;
  269. }
  270. /** ------------------------------
  271. /** Got Mail?
  272. /** ------------------------------*/
  273. if ( ! $line = $this->pop_command("STAT"))
  274. {
  275. $this->message_array[] = 'unable_to_retrieve_emails';
  276. $line = $this->pop_command("QUIT");
  277. @fclose($this->fp);
  278. return FALSE;
  279. }
  280. $stats = explode(" ", $line);
  281. $total = ( ! isset($stats['1'])) ? 0 : $stats['1'];
  282. $this->total_size = ( ! isset($stats['2'])) ? 0 : $stats['2'];
  283. if ($total == 0)
  284. {
  285. $this->message_array[] = 'no_valid_emails';
  286. $line = $this->pop_command("QUIT");
  287. @fclose($this->fp);
  288. return;
  289. }
  290. /** ------------------------------
  291. /** Determine Sizes of Emails
  292. /** ------------------------------*/
  293. if ($this->total_size > $this->max_size)
  294. {
  295. if ( ! $line = $this->pop_command("LIST"))
  296. {
  297. $this->message_array[] = 'unable_to_retrieve_emails';
  298. $line = $this->pop_command("QUIT");
  299. @fclose($this->fp);
  300. return FALSE;
  301. }
  302. do {
  303. $data = fgets($this->fp, 1024);
  304. $data = $this->iso_clean($data);
  305. if(empty($data) OR trim($data) == '.')
  306. {
  307. break;
  308. }
  309. $x = explode(' ', $data);
  310. if (count($x) == 1) break;
  311. $this->email_sizes[$x['0']] = $x['1'];
  312. } while (strncmp($data, ".\r\n", 3) != 0);
  313. }
  314. /** ------------------------------
  315. /** Find Valid Emails
  316. /** ------------------------------*/
  317. $valid_emails = array();
  318. $valid_froms = explode("|",$this->moblog_array['moblog_valid_from']);
  319. for ($i=1; $i <= $total; $i++)
  320. {
  321. if (strncasecmp($this->pop_command("TOP {$i} 0"), '+OK', 3) != 0)
  322. {
  323. $line = $this->pop_command("QUIT");
  324. @fclose($this->fp);
  325. return FALSE;
  326. }
  327. $valid_subject = 'n';
  328. $valid_from = ($this->moblog_array['moblog_valid_from'] != '') ? 'n' : 'y';
  329. $str = fgets($this->fp, 1024);
  330. while (strncmp($str, ".\r\n", 3) != 0)
  331. {
  332. $str = fgets($this->fp, 1024);
  333. $str = $this->iso_clean($str);
  334. if (empty($str))
  335. {
  336. break;
  337. }
  338. // ------------------------
  339. // Does email contain correct prefix? (if prefix is set)
  340. // Liberal interpretation of prefix location
  341. // ------------------------
  342. if($this->moblog_array['moblog_subject_prefix'] == '')
  343. {
  344. $valid_subject = 'y';
  345. }
  346. elseif (preg_match("/Subject:(.*)/", $str, $subject))
  347. {
  348. if(strpos(trim($subject['1']), $this->moblog_array['moblog_subject_prefix']) !== FALSE)
  349. {
  350. $valid_subject = 'y';
  351. }
  352. }
  353. if ($this->moblog_array['moblog_valid_from'] != '')
  354. {
  355. if (preg_match("/From:\s*(.*)\s*\<(.*)\>/", $str, $from) OR preg_match("/From:\s*(.*)\s*/", $str, $from))
  356. {
  357. $address = ( ! isset($from['2'])) ? $from['1'] : $from['2'];
  358. if(in_array(trim($address),$valid_froms))
  359. {
  360. $valid_from = 'y';
  361. }
  362. }
  363. }
  364. }
  365. if ($valid_subject == 'y' && $valid_from == 'y')
  366. {
  367. $valid_emails[] = $i;
  368. }
  369. }
  370. unset($subject);
  371. unset($str);
  372. if (count($valid_emails) == 0)
  373. {
  374. $this->message_array[] = 'no_valid_emails';
  375. $line = $this->pop_command("QUIT");
  376. @fclose($this->fp);
  377. return;
  378. }
  379. /** ------------------------------
  380. /** Process Valid Emails
  381. /** ------------------------------*/
  382. foreach ($valid_emails as $email_id)
  383. {
  384. // Reset Variables
  385. $this->post_data = array();
  386. $this->email_files = array();
  387. $this->body = '';
  388. $this->sender_email = '';
  389. $this->entry_data = array();
  390. $email_data = '';
  391. $this->attach_as_txt = FALSE;
  392. /** ------------------------------------------
  393. /** Do Not Exceed Max Size During a Moblog Check
  394. /** ------------------------------------------*/
  395. if ($this->total_size > $this->max_size && isset($this->email_sizes[$email_id]))
  396. {
  397. if ($this->checked_size + $this->email_sizes[$email_id] > $this->max_size)
  398. {
  399. continue;
  400. }
  401. $this->checked_size += $this->email_sizes[$email_id];
  402. }
  403. /** ---------------------------------------
  404. /** Failure does happen at times
  405. /** ---------------------------------------*/
  406. if (strncasecmp($this->pop_command("RETR {$email_id}"), '+OK', 3) != 0)
  407. {
  408. continue;
  409. }
  410. // Under redundant, see redundant
  411. $this->post_data['subject'] = 'Moblog Entry';
  412. $this->post_data['ip'] = '127.0.0.1';
  413. $format_flow = 'n';
  414. /** ------------------------------
  415. /** Retrieve Email data
  416. /** ------------------------------*/
  417. do{
  418. $data = fgets($this->fp, 1024);
  419. $data = $this->iso_clean($data);
  420. if(empty($data))
  421. {
  422. break;
  423. }
  424. if ($format_flow == 'n' && stristr($data,'format=flowed'))
  425. {
  426. $format_flow = 'y';
  427. }
  428. $email_data .= $data;
  429. } while (strncmp($data, ".\r\n", 3) != 0);
  430. //echo $email_data."<br /><br />\n\n";
  431. if (preg_match("/charset=(.*?)(\s|".$this->newline.")/is", $email_data, $match))
  432. {
  433. $this->charset = trim(str_replace(array("'", '"', ';'), '', $match['1']));
  434. }
  435. /** --------------------------
  436. /** Set Subject, Remove Moblog Prefix
  437. /** --------------------------*/
  438. if (preg_match("/Subject:(.*)/", trim($email_data), $subject))
  439. {
  440. if($this->moblog_array['moblog_subject_prefix'] == '')
  441. {
  442. $this->post_data['subject'] = (trim($subject['1']) != '') ? trim($subject['1']) : 'Moblog Entry';
  443. }
  444. elseif (strpos(trim($subject['1']), $this->moblog_array['moblog_subject_prefix']) !== FALSE)
  445. {
  446. $str_subject = str_replace($this->moblog_array['moblog_subject_prefix'],'',$subject['1']);
  447. $this->post_data['subject'] = (trim($str_subject) != '') ? trim($str_subject) : 'Moblog Entry';
  448. }
  449. // If the subject header was read with imap_utf8() in the iso_clean() method, then
  450. // we don't need to do anything further
  451. if ( ! function_exists('imap_utf8'))
  452. {
  453. // If subject header was processed with MB or Iconv functions, then the internal encoding
  454. // must be used to decode the subject, not the charset used by the email
  455. if (function_exists('mb_convert_encoding'))
  456. {
  457. $this->post_data['subject'] = mb_convert_encoding($this->post_data['subject'], strtoupper($this->EE->config->item('charset')), mb_internal_encoding());
  458. }
  459. elseif(function_exists('iconv'))
  460. {
  461. $this->post_data['subject'] = iconv(iconv_get_encoding('internal_encoding'), strtoupper($this->EE->config->item('charset')), $this->post_data['subject']);
  462. }
  463. elseif(strtolower($this->EE->config->item('charset')) == 'utf-8' && strtolower($this->charset) == 'iso-8859-1')
  464. {
  465. $this->post_data['subject'] = utf8_encode($this->post_data['subject']);
  466. }
  467. elseif(strtolower($this->EE->config->item('charset')) == 'iso-8859-1' && strtolower($this->charset) == 'utf-8')
  468. {
  469. $this->post_data['subject'] = utf8_decode($this->post_data['subject']);
  470. }
  471. }
  472. }
  473. /** --------------------------
  474. /** IP Address of Sender
  475. /** --------------------------*/
  476. if (preg_match("/Received:\s*from\s*(.*)\[+(.*)\]+/", $email_data, $subject))
  477. {
  478. if (isset($subject['2']) && $this->EE->input->valid_ip(trim($subject['2'])))
  479. {
  480. $this->post_data['ip'] = trim($subject['2']);
  481. }
  482. }
  483. /** --------------------------
  484. /** Check if AT&T email
  485. /** --------------------------*/
  486. if (preg_match("/From:\s*(.*)\s*\<(.*)\>/", $email_data, $from) OR preg_match("/From:\s*(.*)\s*/", $email_data, $from))
  487. {
  488. $this->sender_email = ( ! isset($from['2'])) ? $from['1'] : $from['2'];
  489. if (strpos(trim($this->sender_email),'mobile.att.net') !== FALSE)
  490. {
  491. $this->attach_as_txt = TRUE;
  492. }
  493. }
  494. /** -------------------------------------
  495. /** Eliminate new line confusion
  496. /** -------------------------------------*/
  497. $email_data = $this->remove_newlines($email_data,$this->newline);
  498. /** -------------------------------------
  499. /** Determine Boundary
  500. /** -------------------------------------*/
  501. if ( ! $this->find_boundary($email_data)) // OR $this->moblog_array['moblog_upload_directory'] == '0')
  502. {
  503. /** -------------------------
  504. /** No files, just text
  505. /** -------------------------*/
  506. $duo = $this->newline.$this->newline;
  507. $this->body = $this->find_data($email_data, $duo,$duo.'.'.$this->newline);
  508. if ($this->body == '')
  509. {
  510. $this->body = $this->find_data($email_data, $duo,$this->newline.'.'.$this->newline);
  511. }
  512. // Check for Quoted-Printable and Base64 encoding
  513. if (stristr($email_data,'Content-Transfer-Encoding'))
  514. {
  515. $encoding = $this->find_data($email_data, "Content-Transfer-Encoding: ", $this->newline);
  516. if ( ! stristr(trim($encoding), "quoted-printable") AND ! stristr(trim($encoding), "base64"))
  517. {
  518. // try it without the space after the colon...
  519. $encoding = $this->find_data($email_data, "Content-Transfer-Encoding:", $this->newline);
  520. }
  521. if(stristr(trim($encoding),"quoted-printable"))
  522. {
  523. $this->body = str_replace($this->newline,"\n",$this->body);
  524. $this->body = quoted_printable_decode($this->body);
  525. $this->body = (substr($this->body,0,1) != '=') ? $this->body : substr($this->body,1);
  526. $this->body = (substr($this->body,-1) != '=') ? $this->body : substr($this->body,0,-1);
  527. $this->body = $this->remove_newlines($this->body,$this->newline);
  528. }
  529. elseif(stristr(trim($encoding),"base64"))
  530. {
  531. $this->body = str_replace($this->newline,"\n",$this->body);
  532. $this->body = base64_decode(trim($this->body));
  533. $this->body = $this->remove_newlines($this->body,$this->newline);
  534. }
  535. }
  536. if ($this->charset != $this->EE->config->item('charset'))
  537. {
  538. if (function_exists('mb_convert_encoding'))
  539. {
  540. $this->body = mb_convert_encoding($this->body, strtoupper($this->EE->config->item('charset')), strtoupper($this->charset));
  541. }
  542. elseif(function_exists('iconv') AND ($iconvstr = @iconv(strtoupper($this->charset), strtoupper($this->EE->config->item('charset')), $this->body)) !== FALSE)
  543. {
  544. $this->body = $iconvstr;
  545. }
  546. elseif(strtolower($this->EE->config->item('charset')) == 'utf-8' && strtolower($this->charset) == 'iso-8859-1')
  547. {
  548. $this->body = utf8_encode($this->body);
  549. }
  550. elseif(strtolower($this->EE->config->item('charset')) == 'iso-8859-1' && strtolower($this->charset) == 'utf-8')
  551. {
  552. $this->body = utf8_decode($this->body);
  553. }
  554. }
  555. }
  556. else
  557. {
  558. if ( ! $this->parse_email($email_data))
  559. {
  560. $this->message_array[] = 'unable_to_parse';
  561. return FALSE;
  562. }
  563. // Email message as .txt file?
  564. // Make the email body the attachment's contents
  565. // Unset attachment from files array.
  566. if ($this->attach_as_txt === TRUE && trim($this->body) == '' && $this->attach_text != '')
  567. {
  568. $this->body = $this->attach_text;
  569. $this->attach_text = '';
  570. foreach ($this->post_data['files'] as $key => $value)
  571. {
  572. if ($value == $this->attach_name)
  573. {
  574. unset($this->post_data['files'][$key]);
  575. }
  576. }
  577. }
  578. }
  579. /** ---------------------------
  580. /** Authorization Check
  581. /** ---------------------------*/
  582. if ( ! $this->check_login())
  583. {
  584. if ($this->moblog_array['moblog_auth_required'] == 'y')
  585. {
  586. /** -----------------------------
  587. /** Delete email?
  588. /** -----------------------------*/
  589. if ($this->moblog_array['moblog_auth_delete'] == 'y' && strncasecmp($this->pop_command("DELE {$email_id}"), '+OK', 3) != 0)
  590. {
  591. $this->message_array[] = 'undeletable_email'; //.$email_id;
  592. return FALSE;
  593. }
  594. /** -----------------------------
  595. /** Delete any uploaded images
  596. /** -----------------------------*/
  597. if (count($this->email_files) > 0)
  598. {
  599. foreach ($this->email_files as $axe)
  600. {
  601. @unlink($this->upload_path.$axe);
  602. }
  603. }
  604. // Error...
  605. $this->message_array[] = 'authorization_failed';
  606. $this->message_array[] = $this->post_data['subject'];
  607. continue;
  608. }
  609. }
  610. /** -----------------------------
  611. /** Format Flow Fix - Oh Joy!
  612. /** -----------------------------*/
  613. if ($format_flow == 'y')
  614. {
  615. $x = explode($this->newline,$this->body);
  616. $wrap_point = 10;
  617. if (count($x) > 1)
  618. {
  619. $this->body = '';
  620. // First, find wrap point
  621. for($p=0; $p < count($x); $p++)
  622. {
  623. $wrap_point = (strlen($x[$p]) > $wrap_point) ? strlen($x[$p]) : $wrap_point;
  624. }
  625. // Unwrap the Content
  626. for($p=0; $p < count($x); $p++)
  627. {
  628. $next = (isset($x[$p+1]) && count($y = explode(' ',$x[$p+1]))) ? $y['0'] : '';
  629. $this->body .= (strlen($x[$p]) < $wrap_point && strlen($x[$p].$next) <= $wrap_point) ? $x[$p].$this->newline : $x[$p];
  630. }
  631. }
  632. }
  633. $allow_overrides = ( ! isset($this->moblog_array['moblog_allow_overrides'])) ? 'y' : $this->moblog_array['moblog_allow_overrides'];
  634. /** -----------------------------
  635. /** Image Archive set in email?
  636. /** -----------------------------*/
  637. if ($allow_overrides == 'y' &&
  638. (preg_match("/\{file_archive\}(.*)\{\/file_archive\}/s", $this->body, $matches) OR
  639. preg_match("/\<file_archive\>(.*)\<\/file_archive\>/s", $this->body, $matches)))
  640. {
  641. $matches['1'] = trim($matches['1']);
  642. if ($matches['1'] == 'y' OR $matches['1'] == 'true' OR $matches['1'] == '1')
  643. {
  644. $this->moblog_array['moblog_file_archive'] = 'y';
  645. }
  646. else
  647. {
  648. $this->moblog_array['moblog_file_archive'] = 'n';
  649. }
  650. $this->body = str_replace($matches['0'],'',$this->body);
  651. }
  652. /** -----------------------------
  653. /** Categories set in email?
  654. /** -----------------------------*/
  655. if ($allow_overrides == 'n' OR ( ! preg_match("/\{category\}(.*)\{\/category\}/s", $this->body, $cats) &&
  656. ! preg_match("/\<category\>(.*)\<\/category\>/s", $this->body, $cats)))
  657. {
  658. $this->post_data['categories'] = trim($this->moblog_array['moblog_categories']);
  659. }
  660. else
  661. {
  662. $cats['1'] = str_replace(':','|',$cats['1']);
  663. $cats['1'] = str_replace(',','|',$cats['1']);
  664. $this->post_data['categories'] = trim($cats['1']);
  665. $this->body = str_replace($cats['0'],'',$this->body);
  666. }
  667. /** -----------------------------
  668. /** Status set in email
  669. /** -----------------------------*/
  670. if ($allow_overrides == 'n' OR ( ! preg_match("/\{status\}(.*)\{\/status\}/s", $this->body, $cats) &&
  671. ! preg_match("/\<status\>(.*)\<\/status\>/s", $this->body, $cats)))
  672. {
  673. $this->post_data['status'] = trim($this->moblog_array['moblog_status']);
  674. }
  675. else
  676. {
  677. $this->post_data['status'] = trim($cats['1']);
  678. $this->body = str_replace($cats['0'],'',$this->body);
  679. }
  680. /** -----------------------------
  681. /** Sticky Set in Email
  682. /** -----------------------------*/
  683. if ($allow_overrides == 'n' OR ( ! preg_match("/\{sticky\}(.*)\{\/sticky\}/s", $this->body, $mayo) &&
  684. ! preg_match("/\<sticky\>(.*)\<\/sticky\>/s", $this->body, $mayo)))
  685. {
  686. $this->post_data['sticky'] = ( ! isset($this->moblog_array['moblog_sticky_entry'])) ? $this->sticky : $this->moblog_array['moblog_sticky_entry'];
  687. }
  688. else
  689. {
  690. $this->post_data['sticky'] = (trim($mayo['1']) == 'yes' OR trim($mayo['1']) == 'y') ? 'y' : 'n';
  691. $this->body = str_replace($mayo['0'],'',$this->body);
  692. }
  693. /** -----------------------------
  694. /** Default Field set in email?
  695. /** -----------------------------*/
  696. if ($allow_overrides == 'y' && (preg_match("/\{field\}(.*)\{\/field\}/s", $this->body, $matches) OR
  697. preg_match("/\<field\>(.*)\<\/field\>/s", $this->body, $matches)))
  698. {
  699. $matches[1] = trim($matches[1]);
  700. $this->EE->db->select('field_id');
  701. $this->EE->db->from('channel_fields, channels');
  702. $this->EE->db->where('channels.field_group', 'channel_fields.group_id');
  703. $this->EE->db->where('channels.channel_id', $this->moblog_array['moblog_channel_id']);
  704. $this->EE->db->where('channel_fields.group_id', $query->row('field_group'));
  705. $this->EE->db->where('(channel_fields.field_name = "'.$matches[1].'" OR '.$this->EE->db->dbprefix('channel_fields').'.field_label = "'.$matches[1].'")', NULL, FALSE);
  706. /* -------------------------------------
  707. /* Hidden Configuration Variable
  708. /* - moblog_allow_nontextareas => Removes the textarea only restriction
  709. /* for custom fields in the moblog module (y/n)
  710. /* -------------------------------------*/
  711. if ($this->EE->config->item('moblog_allow_nontextareas') != 'y')
  712. {
  713. $this->EE->db->where('channel_fields.field_type', 'textarea');
  714. }
  715. $results = $this->EE->db->get();
  716. if ($results->num_rows() > 0)
  717. {
  718. $this->moblog_array['moblog_field_id'] = trim($results->row('field_id') );
  719. }
  720. $this->body = str_replace($matches['0'],'',$this->body);
  721. }
  722. /** -----------------------------
  723. /** Set Entry Title in Email
  724. /** -----------------------------*/
  725. if (preg_match("/\{entry_title\}(.*)\{\/entry_title\}/", $this->body, $matches) OR preg_match("/\<entry_title\>(.*)\<\/entry_title\>/", $this->body, $matches))
  726. {
  727. if (strlen($matches['1']) > 1)
  728. {
  729. $this->post_data['subject'] = trim(str_replace($this->newline,"\n",$matches['1']));
  730. }
  731. $this->body = str_replace($matches['0'],'',$this->body);
  732. }
  733. /** ----------------------------
  734. /** Post Entry
  735. /** ----------------------------*/
  736. if ($this->moblog_array['moblog_channel_id'] != '0' && $this->moblog_array['moblog_file_archive'] == 'n')
  737. {
  738. $this->template = $this->moblog_array['moblog_template'];
  739. $tag = 'field';
  740. if($this->moblog_array['moblog_field_id'] != 'none' OR
  741. preg_match("/".LD.'field:'."(.*?)".RD."(.*?)".LD.'\/'.'field:'."(.*?)".RD."/s", $this->template, $matches) OR
  742. preg_match("/[\<\{]field\:(.*?)[\}\>](.*?)[\<\{]\/field\:(.*?)[\}\>]/", $this->body, $matches)
  743. )
  744. {
  745. $this->post_entry();
  746. }
  747. else
  748. {
  749. $this->emails_done++;
  750. continue;
  751. }
  752. }
  753. /** -------------------------
  754. /** Delete Email
  755. /** -------------------------*/
  756. if (strncasecmp($this->pop_command("DELE {$email_id}"), '+OK', 3) != 0)
  757. {
  758. $this->message_array[] = 'undeletable_email'; //.$email_id;
  759. return FALSE;
  760. }
  761. $this->emails_done++;
  762. }
  763. /** -----------------------------
  764. /** Close Email Connection
  765. /** -----------------------------*/
  766. $line = $this->pop_command("QUIT");
  767. @fclose($this->fp);
  768. /** ---------------------------------
  769. /** Clear caches if needed
  770. /** ---------------------------------*/
  771. if ($this->emails_done > 0)
  772. {
  773. if ($this->EE->config->item('new_posts_clear_caches') == 'y')
  774. {
  775. $this->EE->functions->clear_caching('all');
  776. }
  777. else
  778. {
  779. $this->EE->functions->clear_caching('sql_cache');
  780. }
  781. }
  782. return TRUE;
  783. }
  784. // ------------------------------------------------------------------------
  785. /**
  786. * Post Entry
  787. */
  788. function post_entry()
  789. {
  790. // Default Channel Data
  791. $channel_id = $this->moblog_array['moblog_channel_id'];
  792. $this->EE->db->select('site_id, channel_title, channel_url, rss_url, comment_url, deft_comments, cat_group, field_group, channel_notify, channel_notify_emails');
  793. $query = $this->EE->db->get_where('channels', array('channel_id' => $channel_id));
  794. if ($query->num_rows() == 0)
  795. {
  796. $this->message_array[] = 'invalid_channel'; // How the hell did this happen?
  797. return FALSE;
  798. }
  799. $site_id = $query->row('site_id');
  800. $notify_address = ($query->row('channel_notify') == 'y' AND $query->row('channel_notify_emails') != '') ? $query->row('channel_notify_emails') : '';
  801. // Collect the meta data
  802. $this->post_data['subject'] = strip_tags($this->post_data['subject']);
  803. $this->moblog_array['moblog_author_id'] = ($this->moblog_array['moblog_author_id'] == 'none') ? '1' : $this->moblog_array['moblog_author_id'];
  804. $author_id = ($this->author != '') ? $this->author : $this->moblog_array['moblog_author_id'];
  805. if ( ! is_numeric($author_id) OR $author_id == '0')
  806. {
  807. $author_id = '1';
  808. }
  809. // Load the text helper
  810. $this->EE->load->helper('text');
  811. $entry_date = ($this->EE->localize->now + $this->entries_added - $this->time_offset);
  812. $data = array(
  813. 'channel_id' => $channel_id,
  814. 'site_id' => $site_id,
  815. 'author_id' => $author_id,
  816. 'title' => ($this->EE->config->item('auto_convert_high_ascii') == 'y') ? ascii_to_entities($this->post_data['subject']) : $this->post_data['subject'],
  817. 'ip_address' => $this->post_data['ip'],
  818. 'entry_date' => $entry_date,
  819. 'edit_date' => gmdate("YmdHis", $entry_date),
  820. 'year' => gmdate('Y', $entry_date),
  821. 'month' => gmdate('m', $entry_date),
  822. 'day' => gmdate('d', $entry_date),
  823. 'sticky' => (isset($this->post_data['sticky'])) ? $this->post_data['sticky'] : $this->sticky,
  824. 'status' => ($this->post_data['status'] == 'none') ? 'open' : $this->post_data['status'],
  825. 'allow_comments' => $query->row('deft_comments')
  826. );
  827. // Remove ignore text
  828. $this->body = preg_replace("#<img\s+src=\s*[\"']cid:(.*?)\>#si", '', $this->body); // embedded images
  829. $this->moblog_array['moblog_ignore_text'] = $this->remove_newlines($this->moblog_array['moblog_ignore_text'],$this->newline);
  830. // One biggo chunk
  831. if ($this->moblog_array['moblog_ignore_text'] != '' && stristr($this->body,$this->moblog_array['moblog_ignore_text']) !== FALSE)
  832. {
  833. $this->body = str_replace($this->moblog_array['moblog_ignore_text'], '',$this->body);
  834. }
  835. elseif($this->moblog_array['moblog_ignore_text'] != '')
  836. {
  837. // By line
  838. $delete_text = $this->remove_newlines($this->moblog_array['moblog_ignore_text'],$this->newline);
  839. $delete_array = explode($this->newline,$delete_text);
  840. if (count($delete_array) > 0)
  841. {
  842. foreach($delete_array as $ignore)
  843. {
  844. if (trim($ignore) != '')
  845. {
  846. $this->body = str_replace(trim($ignore), '',$this->body);
  847. }
  848. }
  849. }
  850. }
  851. /** -------------------------------------
  852. /** Specified Fields for Email Text
  853. /** -------------------------------------*/
  854. if (preg_match_all("/[\<\{]field\:(.*?)[\}\>](.*?)[\<\{]\/field\:(.*?)[\}\>]/", $this->body, $matches))
  855. {
  856. $this->EE->db->select('channel_fields.field_id, channel_fields.field_name, channel_fields.field_label, channel_fields.field_fmt');
  857. $this->EE->db->from('channels, channel_fields');
  858. $this->EE->db->where('channels.field_group = '.$this->EE->db->dbprefix('channel_fields').'.group_id', NULL, FALSE);
  859. $this->EE->db->where('channels.channel_id', $this->moblog_array['moblog_channel_id']);
  860. /* -------------------------------------
  861. /* Hidden Configuration Variable
  862. /* - moblog_allow_nontextareas => Removes the textarea only restriction
  863. /* for custom fields in the moblog module (y/n)
  864. /* -------------------------------------*/
  865. if ($this->EE->config->item('moblog_allow_nontextareas') != 'y')
  866. {
  867. $this->EE->db->where('channel_fields.field_type', 'textarea');
  868. }
  869. $results = $this->EE->db->get();
  870. if ($results->num_rows() > 0)
  871. {
  872. $field_name = array();
  873. $field_label = array();
  874. $field_format = array();
  875. foreach($results->result_array() as $row)
  876. {
  877. $field_name[$row['field_id']] = $row['field_name'];
  878. $field_label[$row['field_id']] = $row['field_label'];
  879. $field_format[$row['field_id']] = $row['field_fmt'];
  880. }
  881. unset($results);
  882. for($i=0; $i < count($matches[0]); $i++)
  883. {
  884. $x = preg_split("/[\s]+/", $matches['1'][$i]);
  885. if ($key = array_search($x['0'],$field_name) OR $key = array_search($x['0'],$field_label))
  886. {
  887. $format = ( ! isset($x['1']) OR ! stristr($x['1'],"format")) ? $field_format[$key] : preg_replace("/format\=[\"\'](.*?)[\'\"]/","$1",trim($x['1']));
  888. $matches['2'][$i] = str_replace($this->newline, "\n",$matches['2'][$i]);
  889. if ( ! isset($this->entry_data[$key]))
  890. {
  891. $this->entry_data[$key] = array('data' => $matches['2'][$i],
  892. 'format' => $format);
  893. }
  894. else
  895. {
  896. $this->entry_data[$key] = array('data' => $matches['2'][$i].$this->entry_data[$key]['data'],
  897. 'format' => $format);
  898. }
  899. $this->body = str_replace($matches['0'][$i], '', $this->body);
  900. }
  901. }
  902. }
  903. }
  904. // Return New Lines
  905. $this->body = str_replace($this->newline, "\n",$this->body);
  906. // Parse template
  907. $tag = 'field';
  908. if( ! preg_match_all("/".LD.$tag."(.*?)".RD."(.*?)".LD.'\/'.$tag.RD."/s", $this->template, $matches))
  909. {
  910. $this->parse_field($this->moblog_array['moblog_field_id'],$this->template, $query->row('field_group') );
  911. }
  912. else
  913. {
  914. for($i=0; $i < count($matches['0']) ; $i++)
  915. {
  916. $params = $this->assign_parameters($matches['1'][$i]);
  917. $params['format'] = ( ! isset($params['format'])) ? '' : $params['format'];
  918. $params['name'] = ( ! isset($params['name'])) ? '' : $params['name'];
  919. $this->parse_field($params,$matches['2'][$i], $query->row('field_group') );
  920. $this->template = str_replace($matches['0'],'',$this->template);
  921. }
  922. if (trim($this->template) != '')
  923. {
  924. $this->parse_field($this->moblog_array['moblog_field_id'],$this->template, $query->row('field_group') );
  925. }
  926. }
  927. // Prep entry data
  928. if (count($this->entry_data) > 0)
  929. {
  930. foreach($this->entry_data as $key => $value)
  931. {
  932. // ----------------------------------------
  933. // Put this in here in case some one has
  934. // {field:body}{/field:body} in their email
  935. // and yet has their default field set to none
  936. // ----------------------------------------
  937. if ($key == 'none')
  938. {
  939. continue;
  940. }
  941. // Load the text helper
  942. $this->EE->load->helper('text');
  943. $combined_data = $value['data'];
  944. $combined_data = ($this->EE->config->item('auto_convert_high_ascii') == 'y') ? ascii_to_entities(trim($combined_data)) : trim($combined_data);
  945. $data['field_id_'.$key] = $combined_data;
  946. $data['field_ft_'.$key] = $value['format'];
  947. }
  948. }
  949. $data['category'] = array();
  950. if ($this->post_data['categories'] == 'all')
  951. {
  952. $cat_groups = explode('|', $query->row('cat_group'));
  953. $this->EE->load->model('category_model');
  954. foreach($cat_groups as $cat_group_id)
  955. {
  956. $cats_q = $this->EE->category_model->get_channel_categories($cat_group_id);
  957. if ($cats_q->num_rows() > 0)
  958. {
  959. foreach($cats_q->result() as $row)
  960. {
  961. $data['category'][] = $row->cat_id;
  962. }
  963. }
  964. }
  965. $data['category'] = array_unique($data['category']);
  966. }
  967. elseif ($this->post_data['categories'] != 'none')
  968. {
  969. $data['category'] = explode('|', $this->post_data['categories']);
  970. $data['category'] = array_unique($data['category']);
  971. }
  972. // forgive me, please.
  973. $orig_group_id = $this->EE->session->userdata('group_id');
  974. $orig_can_assign = $this->EE->session->userdata('can_assign_post_authors');
  975. $orig_can_edit = $this->EE->session->userdata('can_edit_other_entries');
  976. $this->EE->session->userdata['group_id'] = 1;
  977. $this->EE->session->userdata['can_assign_post_authors'] = 'y';
  978. $this->EE->session->userdata['can_edit_other_entries'] = 'y';
  979. // Insert the Entry
  980. $this->EE->load->library('api');
  981. $this->EE->api->instantiate('channel_entries');
  982. $this->EE->api->instantiate('channel_fields');
  983. $this->EE->api_channel_fields->setup_entry_settings($data['channel_id'], $data);
  984. $result = $this->EE->api_channel_entries->submit_new_entry($data['channel_id'], $data);
  985. if ($result)
  986. {
  987. $this->entries_added++;
  988. }
  989. $this->EE->session->userdata['can_assign_post_authors'] = $orig_can_assign;
  990. $this->EE->session->userdata['group_id'] = $orig_group_id;
  991. $this->EE->session->userdata['can_edit_other_entries'] = $orig_can_edit;
  992. }
  993. // ------------------------------------------------------------------------
  994. /**
  995. * Assign Params
  996. *
  997. * Creates an associative array from a string
  998. * of parameters: sort="asc" limit="2" etc.
  999. *
  1000. * Return parameters as an array - Use TMPL one eventually
  1001. *
  1002. * @param string
  1003. */
  1004. function assign_parameters($str)
  1005. {
  1006. if ($str == "")
  1007. {
  1008. return FALSE;
  1009. }
  1010. // \047 - Single quote octal
  1011. // \042 - Double quote octal
  1012. // I don't know for sure, but I suspect using octals is more reliable than ASCII.
  1013. // I ran into a situation where a quote wasn't being matched until I switched to octal.
  1014. // I have no idea why, so just to be safe I used them here. - Rick
  1015. if (preg_match_all("/(\S+?)\s*=[\042\047](\s*.+?\s*)[\042\047]\s*/", $str, $matches))
  1016. {
  1017. $result = array();
  1018. for ($i = 0; $i < count($matches['1']); $i++)
  1019. {
  1020. $result[$matches['1'][$i]] = $matches['2'][$i];
  1021. }
  1022. return $result;
  1023. }
  1024. return FALSE;
  1025. }
  1026. // ------------------------------------------------------------------------
  1027. /**
  1028. * parse_field
  1029. *
  1030. * @param mixed - params
  1031. * @param
  1032. * @param string
  1033. */
  1034. function parse_field($params, $field_data, $field_group)
  1035. {
  1036. $field_id = '1';
  1037. $format = 'none';
  1038. /** -----------------------------
  1039. /** Determine Field Id and Format
  1040. /** -----------------------------*/
  1041. if ( ! is_array($params))
  1042. {
  1043. $field_id = $params;
  1044. $this->EE->db->select('field_fmt');
  1045. $this->EE->db->where('field_id', $field_id);
  1046. $results = $this->EE->db->get('channel_fields');
  1047. $format = ($results->num_rows() > 0) ? $results->row('field_fmt') : 'none';
  1048. }
  1049. else
  1050. {
  1051. if ($params['name'] != '' && $params['format'] == '')
  1052. {
  1053. $xsql = ($this->EE->config->item('moblog_allow_nontextareas') == 'y') ? "" : " AND exp_channel_fields.field_type = 'textarea' ";
  1054. $this->EE->db->select('field_id, field_fmt');
  1055. $this->EE->db->where('group_id', $field_id);
  1056. $this->EE->db->where('(field_name = "'.$params['name'].'" OR field_label = "'.$params['name'].'")', NULL, FALSE);
  1057. if ($this->EE->config->item('moblog_allow_nontextareas') != 'y')
  1058. {
  1059. $this->EE->db->where('field_type', 'textarea');
  1060. }
  1061. $results = $this->EE->db->get('channel_fields');
  1062. $field_id = ($results->num_rows() > 0) ? $results->row('field_id') : $this->moblog_array['moblog_field_id'];
  1063. $format = ($results->num_rows() > 0) ? $results->row('field_fmt') : 'none';
  1064. }
  1065. elseif($params['name'] == '' && $params['format'] == '')
  1066. {
  1067. $field_id = $this->moblog_array['moblog_field_id'];
  1068. $this->EE->db->select('field_fmt');
  1069. $this->EE->db->where('field_id', $field_id);
  1070. $results = $this->EE->db->get('channel_fields');
  1071. $format = $results->row('field_fmt') ;
  1072. }
  1073. elseif($params['name'] == '' && $params['format'] != '')
  1074. {
  1075. $field_id = $this->moblog_array['moblog_field_id'];
  1076. $format = $params['format'];
  1077. }
  1078. elseif($params['name'] != '' && $params['format'] != '')
  1079. {
  1080. $xsql = ($this->EE->config->item('moblog_allow_nontextareas') == 'y') ? "" : " AND exp_channel_fields.field_type = 'textarea' ";
  1081. $this->EE->db->select('field_id');
  1082. $this->EE->db->where('group_id', $field_group);
  1083. $this->EE->db->where('(field_name = "'.$params['name'].'" OR field_label = "'.$params['name'].'")');
  1084. if ($this->EE->config->item('moblog_allow_nontextareas') != 'y')
  1085. {
  1086. $this->EE->db->where('field_type', 'textarea');
  1087. }
  1088. $results = $this->EE->db->get('channel_fields');
  1089. $field_id = ($results->num_rows() > 0) ? $results->row('field_id') : $this->moblog_array['moblog_field_id'];
  1090. $format = $params['format'];
  1091. }
  1092. }
  1093. $dir_id = $this->moblog_array['moblog_upload_directory'];
  1094. $this->EE->load->model('file_model');
  1095. $this->EE->load->model('file_upload_preferences_model');
  1096. $prefs_q = $this->EE->file_upload_preferences_model->get_file_upload_preferences(1, $dir_id);
  1097. $sizes_q = $this->EE->file_model->get_dimensions_by_dir_id($dir_id);
  1098. $dir_server_path = $prefs_q['server_path'];
  1099. // @todo if 0 skip!!
  1100. $thumb_data = array();
  1101. $image_data = array();
  1102. foreach ($sizes_q->result() as $row)
  1103. {
  1104. foreach (array('thumb', 'image') as $which)
  1105. {
  1106. if ($row->id == $this->moblog_array['moblog_'.$which.'_size'])
  1107. {
  1108. ${$which.'_data'} = array(
  1109. 'dir' => '_'.$row->short_name.'/',
  1110. 'height' => $row->height,
  1111. 'width' => $row->width
  1112. );
  1113. }
  1114. }
  1115. }
  1116. /** -----------------------------
  1117. /** Parse Content
  1118. /** -----------------------------*/
  1119. $pair_array = array('images','audio','movie','files');
  1120. $float_data = $this->post_data;
  1121. $params = array();
  1122. foreach ($pair_array as $type)
  1123. {
  1124. if ( ! preg_match_all("/".LD.$type."(.*?)".RD."(.*?)".LD.'\/'.$type.RD."/s", $field_data, $matches))
  1125. {
  1126. continue;
  1127. }
  1128. if(count($matches['0']) == 0)
  1129. {
  1130. continue;
  1131. }
  1132. for ($i=0; $i < count($matches['0']) ; $i++)
  1133. {
  1134. $template_data = '';
  1135. if ($type != 'files' && ( ! isset($float_data[$type]) OR count($float_data[$type]) == 0))
  1136. {
  1137. $field_data = str_replace($matches['0'][$i],'',$field_data);
  1138. continue;
  1139. }
  1140. // Assign parameters, if any
  1141. if(isset($matches['1'][$i]) && trim($matches['1'][$i]) != '')
  1142. {
  1143. $params = $this->assign_parameters(trim($matches['1'][$i]));
  1144. }
  1145. $params['match'] = ( ! isset($params['match'])) ? '' : $params['match'];
  1146. /** ----------------------------
  1147. /** Parse Pairs
  1148. /** ----------------------------*/
  1149. // Files is a bit special. It goes last and will clear out remaining files. Has match parameter
  1150. if ($type == 'files' && $params['match'] != '')
  1151. {
  1152. if ( ! count($float_data))
  1153. {
  1154. break;
  1155. }
  1156. foreach ($float_data as $ftype => $value)
  1157. {
  1158. if ( ! in_array($ftype, $pair_array) OR ! ($params['match'] == 'all' OR stristr($params['match'], $ftype)))
  1159. {
  1160. continue;
  1161. }
  1162. foreach ($float_data[$ftype] as $k => $file)
  1163. {
  1164. // not an image
  1165. if ($ftype != 'images')
  1166. {
  1167. $template_data .= str_replace('{file}',$this->upload_dir_code.$file,$matches['2'][$i]);
  1168. continue;
  1169. }
  1170. // most definitely an image
  1171. // Figure out sizes
  1172. $file_rel_path = empty($image_data) ? $file : $image_data['dir'].$file;
  1173. $file_dimensions = @getimagesize($dir_server_path.$file_rel_path);
  1174. $filename = $this->upload_dir_code.$file_rel_path;
  1175. $thumb_replace = '';
  1176. $thumb_dimensions = FALSE;
  1177. if ( ! empty($thumb_data))
  1178. {
  1179. $thumb_rel_path = $thumb_data['dir'].$file;
  1180. $thumb_replace = $this->upload_dir_code.$thumb_rel_path;
  1181. $thumb_dimensions = @getimagesize($dir_server_path.$thumb_rel_path);
  1182. }
  1183. $details = array(
  1184. 'width' => $file_dimensions ? $file_dimensions[0] : '',
  1185. 'height' => $file_dimensions ? $file_dimensions[1] : '',
  1186. 'thumbnail' => $thumb_replace,
  1187. 'thumb_width' => $thumb_dimensions ? $thumb_dimensions[0] : '',
  1188. 'thumb_height' => $thumb_dimensions ? $thumb_dimensions[1] : ''
  1189. );
  1190. $temp_data = str_replace('{file}',$filename,$matches['2'][$i]);
  1191. foreach ($details as $d => $dv)
  1192. {
  1193. $temp_data = str_replace('{'.$d.'}', $dv, $temp_data);
  1194. }
  1195. $template_data .= $temp_data;
  1196. }
  1197. }
  1198. }
  1199. elseif (isset($float_data[$type]))
  1200. {
  1201. foreach ($float_data[$type] as $k => $file)
  1202. {
  1203. if ($type != 'images')
  1204. {
  1205. $template_data .= str_replace('{file}',$this->upload_dir_code.$file,$matches['2'][$i]);
  1206. continue;
  1207. }
  1208. // It's an image, work out sizes
  1209. // Figure out sizes
  1210. $file_rel_path = empty($image_data) ? $file : $image_data['dir'].$file;
  1211. $file_dimensions = @getimagesize($dir_server_path.$file_rel_path);
  1212. $filename = $this->upload_dir_code.$file_rel_path;
  1213. $thumb_replace = '';
  1214. $thumb_dimensions = FALSE;
  1215. if ( ! empty($thumb_data))
  1216. {
  1217. $thumb_rel_path = $thumb_data['dir'].$file;
  1218. $thumb_replace = $this->upload_dir_code.$thumb_rel_path;
  1219. $thumb_dimensions = @getimagesize($dir_server_path.$thumb_rel_path);
  1220. }
  1221. $details = array(
  1222. 'width' => $file_dimensions ? $file_dimensions[0] : '',
  1223. 'height' => $file_dimensions ? $file_dimensions[1] : '',
  1224. 'thumbnail' => $thumb_replace,
  1225. 'thumb_width' => $thumb_dimensions ? $thumb_dimensions[0] : '',
  1226. 'thumb_height' => $thumb_dimensions ? $thumb_dimensions[1] : ''
  1227. );
  1228. $temp_data = str_replace('{file}',$filename,$matches['2'][$i]);
  1229. foreach ($details as $d => $dv)
  1230. {
  1231. $temp_data = str_replace('{'.$d.'}', $dv, $temp_data);
  1232. }
  1233. $template_data .= $temp_data;
  1234. }
  1235. }
  1236. // Replace tag pair with template data
  1237. $field_data = str_replace($matches['0'][$i],$template_data,$field_data);
  1238. // Unset member of float data array
  1239. if (isset($float_data[$type]) && count($float_data[$type]) == 0)
  1240. {
  1241. unset($float_data[$type]);
  1242. }
  1243. }
  1244. }
  1245. /** ------------------------------
  1246. /** Variable Single: text
  1247. /** ------------------------------*/
  1248. $field_data = str_replace(array('{text}', '{sender_email}'), array($this->body, $this->sender_email), $field_data);
  1249. $this->entry_data[$field_id]['data'] = ( ! isset($this->entry_data[$field_id])) ? $field_data : $this->entry_data[$field_id]['data']."\n".$field_data;
  1250. $this->entry_data[$field_id]['format'] = $format;
  1251. }
  1252. // ------------------------------------------------------------------------
  1253. /**
  1254. * Parse Email
  1255. *
  1256. * @param mixed - Email Data
  1257. * @param
  1258. */
  1259. function parse_email($email_data,$type='norm')
  1260. {
  1261. $this->EE->load->library('filemanager');
  1262. $boundary = ($type != 'norm') ? $this->multi_boundary : $this->boundary;
  1263. $email_data = str_replace('boundary='.substr($boundary,2),'BOUNDARY_HERE',$email_data);
  1264. $email_parts = explode($boundary, $email_data);
  1265. if (count($email_parts) < 2)
  1266. {
  1267. $boundary = str_replace("+","\+", $boundary);
  1268. $email_parts = explode($boundary, $email_data);
  1269. }
  1270. if (count($email_parts) < 2)
  1271. {
  1272. return FALSE;
  1273. unset($email_parts);
  1274. unset($email_data);
  1275. }
  1276. $upload_dir_id = $this->moblog_array['moblog_upload_directory'];
  1277. if ($upload_dir_id != 0)
  1278. {
  1279. $this->upload_dir_code = '{filedir_'.$upload_dir_id.'}';
  1280. }
  1281. // Find Attachments
  1282. foreach ($email_parts as $key => $value)
  1283. {
  1284. // Skip headers and those with no content-type
  1285. if ($key == '0' OR stristr($value, 'Content-Type:') === FALSE)
  1286. {
  1287. continue;
  1288. }
  1289. $contents = $this->find_data($value, "Content-Type:", $this->newline);
  1290. $x = explode(';',$contents);
  1291. $content_type = $x['0'];
  1292. $content_type = strtolower($content_type);
  1293. $pieces = explode('/',trim($content_type));
  1294. $type = trim($pieces['0']);
  1295. $subtype = ( ! isset($pieces['1'])) ? '0' : trim($pieces['1']);
  1296. $charset = 'auto';
  1297. /** --------------------------
  1298. /** Outlook Exception
  1299. /** --------------------------*/
  1300. if ($type == 'multipart' && $subtype != 'appledouble')
  1301. {
  1302. if ( ! stristr($value,'boundary='))
  1303. {
  1304. continue;
  1305. }
  1306. $this->multi_boundary = "--".$this->find_data($value, "boundary=", $this->newline);
  1307. $this->multi_boundary = trim(str_replace('"','',$this->multi_boundary));
  1308. if (strlen($this->multi_boundary) == 0)
  1309. {
  1310. continue;
  1311. }
  1312. $this->parse_email($value,'multi');
  1313. $this->multi_boundary = '';
  1314. continue;
  1315. }
  1316. /** --------------------------
  1317. /** Quick Grab of Headers
  1318. /** --------------------------*/
  1319. $headers = $this->find_data($value, '', $this->newline.$this->newline);
  1320. /** ---------------------------
  1321. /** Text : plain, h…

Large files files are truncated, but you can click here to view the full file