PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/html/AppCode/expressionengine/modules/moblog/mod.moblog.php

https://github.com/w3bg/www.hsifin.com
PHP | 2890 lines | 2079 code | 441 blank | 370 comment | 377 complexity | e80e0a236412cacd76122eaeac5dee7e MD5 | raw file
Possible License(s): AGPL-3.0

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 ExpressionEngine Dev Team
  7. * @copyright Copyright (c) 2003 - 2010, EllisLab, Inc.
  8. * @license http://expressionengine.com/user_guide/license.html
  9. * @link http://expressionengine.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 ExpressionEngine Dev Team
  21. * @link http://expressionengine.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 = ''; // true/false (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 $pings_sent = 0; // Number of servers pinged
  48. var $upload_dir_code = ''; // {filedir_2} for entry's
  49. var $upload_path = ''; // Server path for upload directory
  50. var $entry_data = array(); // Data for entry's custom fields
  51. var $post_data = array(); // Post data retrieved from email being processed: Subject, IP, Categories, Status
  52. var $template = ''; // Moblog's template
  53. var $sticky = 'n'; // Default Sticky Value
  54. // These settings are for a specific problem with AT&T phones
  55. var $attach_as_txt = FALSE; // Email's Message as txt file?
  56. var $attach_text = ''; // If $attach_as_txt is true, this is the text
  57. var $attach_name = ''; // If $attach_as_txt is true, this is the name
  58. var $time_offset = '5'; // Number of seconds entries are offset by negatively, higher if you are putting in many entries
  59. var $movie = array(); // Suffixes for accepted movie files
  60. var $audio = array(); // Suffixes for accepted audio files
  61. var $image = array(); // Suffixes for accepted image files
  62. var $files = array(); // Suffixes for other types of accepted files
  63. var $txt_override = FALSE; // When set to TRUE, all .txt files are treated as message text
  64. // ------------------------------------------------------------------------
  65. /**
  66. * Constructor
  67. */
  68. function Moblog()
  69. {
  70. // Make a local reference to the ExpressionEngine super object
  71. $this->EE =& get_instance();
  72. /** -----------------------------
  73. /** Default file formats
  74. /** -----------------------------*/
  75. $this->movie = array('3gp','mov','mpg','avi','movie');
  76. $this->audio = array('mid','midi','mp2','mp3','aac','mp4','aif','aiff','aifc','ram','rm','rpm','wav','ra','rv','wav');
  77. $this->image = array('bmp','gif','jpeg','jpg','jpe','png','tiff','tif');
  78. $this->files = array('doc','xls','zip','tar','tgz','swf','sit','php','txt','html','asp','js','rtf', 'pdf');
  79. if ( ! defined('LD'))
  80. define('LD', '{');
  81. if ( ! defined('RD'))
  82. define('RD', '}');
  83. if ( ! defined('SLASH'))
  84. define('SLASH', '&#47;');
  85. $this->max_size = $this->max_size * 1024 * 1000;
  86. }
  87. // ------------------------------------------------------------------------
  88. /**
  89. * Check for Expired Moblogs
  90. */
  91. function check()
  92. {
  93. $which = ( ! $this->EE->TMPL->fetch_param('which')) ? '' : $this->EE->TMPL->fetch_param('which');
  94. $this->silent = ( ! $this->EE->TMPL->fetch_param('silent')) ? 'true' : $this->EE->TMPL->fetch_param('silent');
  95. if ($which == '')
  96. {
  97. $this->return_data = ($this->silent == 'true') ? '' : 'No Moblog Indicated';
  98. return $this->return_data ;
  99. }
  100. $this->EE->lang->loadfile('moblog');
  101. $sql = "SELECT * FROM exp_moblogs WHERE moblog_enabled = 'y'";
  102. $sql .= ($which == 'all') ? '' : $this->EE->functions->sql_andor_string($which, 'moblog_short_name', 'exp_moblogs');
  103. $query = $this->EE->db->query($sql);
  104. if ($query->num_rows() == 0)
  105. {
  106. $this->return_data = ($this->silent == 'true') ? '' : $this->EE->lang->line('no_moblogs');
  107. return $this->return_data;
  108. }
  109. // Check Cache
  110. if ( ! @is_dir(APPPATH.'cache/'.$this->cache_name))
  111. {
  112. if ( ! @mkdir(APPPATH.'cache/'.$this->cache_name, DIR_WRITE_MODE))
  113. {
  114. $this->return_data = ($this->silent == 'true') ? '' : $this->EE->lang->line('no_cache');
  115. return $this->return_data;
  116. }
  117. }
  118. @chmod(APPPATH.'cache/'.$this->cache_name, DIR_WRITE_MODE);
  119. //$this->EE->functions->delete_expired_files(APPPATH.'cache/'.$this->cache_name);
  120. $expired = array();
  121. foreach($query->result_array() as $row)
  122. {
  123. $cache_file = APPPATH.'cache/'.$this->cache_name.'/t_moblog_'.$row['moblog_id'];
  124. if ( ! file_exists($cache_file) OR (time() > (filemtime($cache_file) + ($row['moblog_time_interval'] * 60))))
  125. {
  126. $this->set_cache($row['moblog_id']);
  127. $expired[] = $row['moblog_id'];
  128. }
  129. elseif ( ! $fp = @fopen($cache_file, FOPEN_READ_WRITE))
  130. {
  131. if ($this->silent == 'false')
  132. {
  133. $this->return_data .= '<p><strong>'.$row['moblog_full_name'].'</strong><br />'.
  134. $this->EE->lang->line('no_cache')."\n</p>";
  135. }
  136. }
  137. }
  138. if (count($expired) == 0)
  139. {
  140. $this->return_data = ($this->silent == 'true') ? '' : $this->EE->lang->line('moblog_current');
  141. return $this->return_data;
  142. }
  143. /** ------------------------------
  144. /** Process Expired Moblogs
  145. /** ------------------------------*/
  146. foreach($query->result_array() as $row)
  147. {
  148. if (in_array($row['moblog_id'],$expired))
  149. {
  150. $this->moblog_array = $row;
  151. if ($this->moblog_array['moblog_email_type'] == 'imap')
  152. {
  153. if ( ! $this->check_imap_moblog())
  154. {
  155. if ($this->silent == 'false' && count($this->message_array) > 0)
  156. {
  157. $this->return_data .= '<p><strong>'.$this->moblog_array['moblog_full_name'].'</strong><br />'.
  158. $this->errors()."\n</p>";
  159. }
  160. }
  161. }
  162. else
  163. {
  164. if ( ! $this->check_pop_moblog())
  165. {
  166. if ($this->silent == 'false' && count($this->message_array) > 0)
  167. {
  168. $this->return_data .= '<p><strong>'.$this->moblog_array['moblog_full_name'].'</strong><br />'.
  169. $this->errors()."\n</p>";
  170. }
  171. }
  172. }
  173. $this->message_array = array();
  174. }
  175. }
  176. if ($this->silent == 'false')
  177. {
  178. $this->return_data .= $this->EE->lang->line('moblog_successful_check')."<br />\n";
  179. $this->return_data .= $this->EE->lang->line('emails_done')." {$this->emails_done}<br />\n";
  180. $this->return_data .= $this->EE->lang->line('entries_added')." {$this->entries_added}<br />\n";
  181. $this->return_data .= $this->EE->lang->line('attachments_uploaded')." {$this->uploads}<br />\n";
  182. $this->return_data .= $this->EE->lang->line('pings_sent')." {$this->pings_sent}<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 == 'true')
  208. {
  209. return $message;
  210. }
  211. foreach($this->message_array as $row)
  212. {
  213. $message .= ($message == '') ? '' : "<br />\n";
  214. $message .= ( ! $this->EE->lang->line($row)) ? $row : $this->EE->lang->line($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\}/", $this->body, $matches) OR
  639. preg_match("/\<file_archive\>(.*)\<\/file_archive\>/", $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\}/", $this->body, $cats) &&
  656. ! preg_match("/\<category\>(.*)\<\/category\>/", $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'] = $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\}/", $this->body, $cats) &&
  671. ! preg_match("/\<status\>(.*)\<\/status\>/", $this->body, $cats)))
  672. {
  673. $this->post_data['status'] = trim($this->moblog_array['moblog_status']);
  674. }
  675. else
  676. {
  677. $this->post_data['status'] = $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\}/", $this->body, $mayo) &&
  684. ! preg_match("/\<sticky\>(.*)\<\/sticky\>/", $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\}/", $this->body, $matches) OR
  697. preg_match("/\<field\>(.*)\<\/field\>/", $this->body, $matches)))
  698. {
  699. $this->EE->db->select('field_id');
  700. $this->EE->db->from('channel_fields, channels');
  701. $this->EE->db->where('channels.field_group', 'channel_fields.group_id');
  702. $this->EE->db->where('channels.channel_id', $this->moblog_array['moblog_channel_id']);
  703. $this->EE->db->where('channel_fields.group_id', $query->row('field_group'));
  704. $this->EE->db->where('(channel_fields.field_name = "'.$matches[1].'" OR '.$this->EE->db->dbprefix('channel_fields').'.field_label = "'.$matches[1].'")', NULL, FALSE);
  705. /* -------------------------------------
  706. /* Hidden Configuration Variable
  707. /* - moblog_allow_nontextareas => Removes the textarea only restriction
  708. /* for custom fields in the moblog module (y/n)
  709. /* -------------------------------------*/
  710. if ($this->EE->config->item('moblog_allow_nontextareas') != 'y')
  711. {
  712. $this->EE->db->where('channel_fields.field_name', 'textarea');
  713. }
  714. $results = $this->EE->db->get();
  715. if ($results->num_rows() > 0)
  716. {
  717. $this->moblog_array['moblog_field_id'] = trim($results->row('field_id') );
  718. }
  719. $this->body = str_replace($matches['0'],'',$this->body);
  720. }
  721. /** -----------------------------
  722. /** Set Entry Title in Email
  723. /** -----------------------------*/
  724. if (preg_match("/\{entry_title\}(.*)\{\/entry_title\}/", $this->body, $matches) OR preg_match("/\<entry_title\>(.*)\<\/entry_title\>/", $this->body, $matches))
  725. {
  726. if (strlen($matches['1']) > 1)
  727. {
  728. $this->post_data['subject'] = trim(str_replace($this->newline,"\n",$matches['1']));
  729. }
  730. $this->body = str_replace($matches['0'],'',$this->body);
  731. }
  732. /** ----------------------------
  733. /** Post Entry
  734. /** ----------------------------*/
  735. if ($this->moblog_array['moblog_channel_id'] != '0' && $this->moblog_array['moblog_file_archive'] == 'n')
  736. {
  737. $this->template = $this->moblog_array['moblog_template'];
  738. $tag = 'field';
  739. if($this->moblog_array['moblog_field_id'] != 'none' OR
  740. preg_match("/".LD.'field:'."(.*?)".RD."(.*?)".LD.'\/'.'field:'."(.*?)".RD."/s", $this->template, $matches) OR
  741. preg_match("/[\<\{]field\:(.*?)[\}\>](.*?)[\<\{]\/field\:(.*?)[\}\>]/", $this->body, $matches)
  742. )
  743. {
  744. $this->post_entry();
  745. }
  746. else
  747. {
  748. $this->emails_done++;
  749. continue;
  750. }
  751. }
  752. /** -------------------------
  753. /** Delete Email
  754. /** -------------------------*/
  755. if (strncasecmp($this->pop_command("DELE {$email_id}"), '+OK', 3) != 0)
  756. {
  757. $this->message_array[] = 'undeletable_email'; //.$email_id;
  758. return FALSE;
  759. }
  760. /** -------------------------
  761. /** Send Pings
  762. /** -------------------------*/
  763. if (isset($this->moblog_array['moblog_ping_servers']) && $this->moblog_array['moblog_ping_servers'] != '')
  764. {
  765. if($pings_sent = $this->send_pings($this->moblog_array['channel_title'], $this->moblog_array['channel_url'], $this->moblog_array['rss_url']))
  766. {
  767. $this->pings_sent = $this->pings_sent + count($pings_sent);
  768. }
  769. }
  770. $this->emails_done++;
  771. }
  772. /** -----------------------------
  773. /** Close Email Connection
  774. /** -----------------------------*/
  775. $line = $this->pop_command("QUIT");
  776. @fclose($this->fp);
  777. /** ---------------------------------
  778. /** Clear caches if needed
  779. /** ---------------------------------*/
  780. if ($this->emails_done > 0)
  781. {
  782. if ($this->EE->config->item('new_posts_clear_caches') == 'y')
  783. {
  784. $this->EE->functions->clear_caching('all');
  785. }
  786. else
  787. {
  788. $this->EE->functions->clear_caching('sql_cache');
  789. }
  790. }
  791. return TRUE;
  792. }
  793. // ------------------------------------------------------------------------
  794. /**
  795. * Post Entry
  796. */
  797. function post_entry()
  798. {
  799. // Default Channel Data
  800. $channel_id = $this->moblog_array['moblog_channel_id'];
  801. $this->EE->db->select('site_id, channel_title, channel_url, rss_url, ping_return_url, comment_url, deft_comments, cat_group, field_group, channel_notify, channel_notify_emails');
  802. $query = $this->EE->db->get_where('channels', array('channel_id' => $channel_id));
  803. if ($query->num_rows() == 0)
  804. {
  805. $this->message_array[] = 'invalid_channel'; // How the hell did this happen?
  806. return FALSE;
  807. }
  808. $site_id = $query->row('site_id');
  809. $notify_address = ($query->row('channel_notify') == 'y' AND $query->row('channel_notify_emails') != '') ? $query->row('channel_notify_emails') : '';
  810. // Collect the meta data
  811. $this->post_data['subject'] = strip_tags($this->post_data['subject']);
  812. $this->moblog_array['moblog_author_id'] = ($this->moblog_array['moblog_author_id'] == 'none') ? '1' : $this->moblog_array['moblog_author_id'];
  813. $author_id = ($this->author != '') ? $this->author : $this->moblog_array['moblog_author_id'];
  814. if ( ! is_numeric($author_id) OR $author_id == '0')
  815. {
  816. $author_id = '1';
  817. }
  818. // Load the text helper
  819. $this->EE->load->helper('text');
  820. $entry_date = ($this->EE->localize->now + $this->entries_added - $this->time_offset);
  821. $data = array(
  822. 'channel_id' => $channel_id,
  823. 'site_id' => $site_id,
  824. 'author_id' => $author_id,
  825. 'title' => ($this->EE->config->item('auto_convert_high_ascii') == 'y') ? ascii_to_entities($this->post_data['subject']) : $this->post_data['subject'],
  826. 'ip_address' => $this->post_data['ip'],
  827. 'entry_date' => $entry_date,
  828. 'edit_date' => gmdate("YmdHis", $entry_date),
  829. 'year' => gmdate('Y', $entry_date),
  830. 'month' => gmdate('m', $entry_date),
  831. 'day' => gmdate('d', $entry_date),
  832. 'sticky' => (isset($this->post_data['sticky'])) ? $this->post_data['sticky'] : $this->sticky,
  833. 'status' => ($this->post_data['status'] == 'none') ? 'open' : $this->post_data['status'],
  834. 'allow_comments' => $query->row('deft_comments'),
  835. 'ping_servers' => FALSE // Pings are already sent above. Should probably be hooked into API CHannel Entries as well.
  836. );
  837. if ($this->EE->config->item('honor_entry_dst') == 'y')
  838. {
  839. $data['dst_enabled'] = ($this->EE->config->item('daylight_savings') == 'y') ? 'y' : 'n';
  840. }
  841. // Remove ignore text
  842. $this->body = preg_replace("#<img\s+src=\s*[\"']cid:(.*?)\>#si", '', $this->body); // embedded images
  843. $this->moblog_array['moblog_ignore_text'] = $this->remove_newlines($this->moblog_array['moblog_ignore_text'],$this->newline);
  844. // One biggo chunk
  845. if ($this->moblog_array['moblog_ignore_text'] != '' && stristr($this->body,$this->moblog_array['moblog_ignore_text']) !== FALSE)
  846. {
  847. $this->body = str_replace($this->moblog_array['moblog_ignore_text'], '',$this->body);
  848. }
  849. elseif($this->moblog_array['moblog_ignore_text'] != '')
  850. {
  851. // By line
  852. $delete_text = $this->remove_newlines($this->moblog_array['moblog_ignore_text'],$this->newline);
  853. $delete_array = explode($this->newline,$delete_text);
  854. if (count($delete_array) > 0)
  855. {
  856. foreach($delete_array as $ignore)
  857. {
  858. if (trim($ignore) != '')
  859. {
  860. $this->body = str_replace(trim($ignore), '',$this->body);
  861. }
  862. }
  863. }
  864. }
  865. /** -------------------------------------
  866. /** Specified Fields for Email Text
  867. /** -------------------------------------*/
  868. if (preg_match_all("/[\<\{]field\:(.*?)[\}\>](.*?)[\<\{]\/field\:(.*?)[\}\>]/", $this->body, $matches))
  869. {
  870. $this->EE->db->select('channel_fields.field_id, channel_fields.field_name, channel_fields.field_label, channel_fields.field_fmt');
  871. $this->EE->db->from('channels, channel_fields');
  872. $this->EE->db->where('channels.field_group = '.$this->EE->db->dbprefix('channel_fields').'.group_id', NULL, FALSE);
  873. $this->EE->db->where('channels.channel_id', $this->moblog_array['moblog_channel_id']);
  874. /* -------------------------------------
  875. /* Hidden Configuration Variable
  876. /* - moblog_allow_nontextareas => Removes the textarea only restriction
  877. /* for custom fields in the moblog module (y/n)
  878. /* -------------------------------------*/
  879. if ($this->EE->config->item('moblog_allow_nontextareas') != 'y')
  880. {
  881. $this->EE->db->where('channel_fields.field_name', 'textarea');
  882. }
  883. $results = $this->EE->db->get();
  884. if ($results->num_rows() > 0)
  885. {
  886. $field_name = array();
  887. $field_label = array();
  888. $field_format = array();
  889. foreach($results->result_array() as $row)
  890. {
  891. $field_name[$row['field_id']] = $row['field_name'];
  892. $field_label[$row['field_id']] = $row['field_label'];
  893. $field_format[$row['field_id']] = $row['field_fmt'];
  894. }
  895. unset($results);
  896. for($i=0; $i < count($matches[0]); $i++)
  897. {
  898. $x = preg_split("/[\s]+/", $matches['1'][$i]);
  899. if ($key = array_search($x['0'],$field_name) OR $key = array_search($x['0'],$field_label))
  900. {
  901. $format = ( ! isset($x['1']) OR ! stristr($x['1'],"format")) ? $field_format[$key] : preg_replace("/format\=[\"\'](.*?)[\'\"]/","$1",trim($x['1']));
  902. $matches['2'][$i] = str_replace($this->newline, "\n",$matches['2'][$i]);
  903. if ( ! isset($this->entry_data[$key]))
  904. {
  905. $this->entry_data[$key] = array('data' => $matches['2'][$i],
  906. 'format' => $format);
  907. }
  908. else
  909. {
  910. $this->entry_data[$key] = array('data' => $matches['2'][$i].$this->entry_data[$key]['data'],
  911. 'format' => $format);
  912. }
  913. $this->body = str_replace($matches['0'][$i], '', $this->body);
  914. }
  915. }
  916. }
  917. }
  918. // Return New Lines
  919. $this->body = str_replace($this->newline, "\n",$this->body);
  920. // Parse template
  921. $tag = 'field';
  922. if( ! preg_match_all("/".LD.$tag."(.*?)".RD."(.*?)".LD.'\/'.$tag.RD."/s", $this->template, $matches))
  923. {
  924. $this->parse_field($this->moblog_array['moblog_field_id'],$this->template, $query->row('field_group') );
  925. }
  926. else
  927. {
  928. for($i=0; $i < count($matches['0']) ; $i++)
  929. {
  930. $params = $this->assign_parameters($matches['1'][$i]);
  931. $params['format'] = ( ! isset($params['format'])) ? '' : $params['format'];
  932. $params['name'] = ( ! isset($params['name'])) ? '' : $params['name'];
  933. $this->parse_field($params,$matches['2'][$i], $query->row('field_group') );
  934. $this->template = str_replace($matches['0'],'',$this->template);
  935. }
  936. if (trim($this->template) != '')
  937. {
  938. $this->parse_field($this->moblog_array['moblog_field_id'],$this->template, $query->row('field_group') );
  939. }
  940. }
  941. // Prep entry data
  942. if (count($this->entry_data) > 0)
  943. {
  944. foreach($this->entry_data as $key => $value)
  945. {
  946. // ----------------------------------------
  947. // Put this in here in case some one has
  948. // {field:body}{/field:body} in their email
  949. // and yet has their default field set to none
  950. // ----------------------------------------
  951. if ($key == 'none')
  952. {
  953. continue;
  954. }
  955. // Load the text helper
  956. $this->EE->load->helper('text');
  957. $combined_data = $value['data'];
  958. $combined_data = ($this->EE->config->item('auto_convert_high_ascii') == 'y') ? ascii_to_entities(trim($combined_data)) : trim($combined_data);
  959. $data['field_id_'.$key] = $combined_data;
  960. $data['field_ft_'.$key] = $value['format'];
  961. }
  962. }
  963. $data['category'] = array();
  964. if ($this->post_data['categories'] == 'all')
  965. {
  966. $cat_groups = explode('|', $query->row('cat_group'));
  967. $this->EE->load->model('category_model');
  968. foreach($cat_groups as $cat_group_id)
  969. {
  970. $cats_q = $this->EE->category_model->get_channel_categories($cat_group_id);
  971. if ($cats_q->num_rows() > 0)
  972. {
  973. foreach($cats_q->result() as $row)
  974. {
  975. $data['category'][] = $row->cat_id;
  976. }
  977. }
  978. }
  979. $data['category'] = array_unique($data['category']);
  980. }
  981. elseif ($this->post_data['categories'] != 'none')
  982. {
  983. $data['category'] = explode('|', $this->post_data['categories']);
  984. $data['category'] = array_unique($data['category']);
  985. }
  986. // forgive me, please.
  987. $orig_group_id = $this->EE->session->userdata('group_id');
  988. $orig_can_assign = $this->EE->session->userdata('can_assign_post_authors');
  989. $orig_can_edit = $this->EE->session->userdata('can_edit_other_entries');
  990. $this->EE->session->userdata['group_id'] = 1;
  991. $this->EE->session->userdata['can_assign_post_authors'] = 'y';
  992. $this->EE->session->userdata['can_edit_other_entries'] = 'y';
  993. // Insert the Entry
  994. $this->EE->load->library('api');
  995. $this->EE->api->instantiate('channel_entries');
  996. $result = $this->EE->api_channel_entries->submit_new_entry($data['channel_id'], $data);
  997. if ( ! $result)
  998. {
  999. // echo '<pre>';print_r($this->EE->api_channel_entries->errors);echo'</pre>';
  1000. }
  1001. else
  1002. {
  1003. $this->entries_added++;
  1004. }
  1005. $this->EE->session->userdata['can_assign_post_authors'] = $orig_can_assign;
  1006. $this->EE->session->userdata['group_id'] = $orig_group_id;
  1007. $this->EE->session->userdata['can_edit_other_entries'] = $orig_can_edit;
  1008. }
  1009. // ------------------------------------------------------------------------
  1010. /**
  1011. * Send Pings
  1012. *
  1013. * @param string title
  1014. * @param string url
  1015. *
  1016. */
  1017. function send_pings($title, $url)
  1018. {
  1019. $ping_servers = explode('|', $this->moblog_array['moblog_ping_servers']);
  1020. $sql = "SELECT server_name, server_url, port FROM exp_ping_servers WHERE id IN (";
  1021. foreach ($ping_servers as $id)
  1022. {
  1023. $sql .= "'$id',";
  1024. }
  1025. $sql = substr($sql, 0, -1).') ';
  1026. $query = $this->EE->db->query($sql);
  1027. if ($query->num_rows() == 0)
  1028. {
  1029. return FALSE;
  1030. }
  1031. if ( ! class_exists('XML_RPC'))
  1032. {
  1033. require APPPATH.'libraries/Xmlrpc'.EXT;
  1034. }
  1035. $XRPC = new XML_RPC;
  1036. $result = array();
  1037. foreach ($query->result_array() as $row)
  1038. {
  1039. if ($XRPC->channels_com_ping($row['server_url'], $row['port'], $title, $url))
  1040. {
  1041. $result[] = $row['server_name'];
  1042. }
  1043. }
  1044. return $result;
  1045. }
  1046. // ------------------------------------------------------------------------
  1047. /**
  1048. * Assign Params
  1049. *
  1050. * Creates an associative array from a string
  1051. * of parameters: sort="asc" limit="2" etc.
  1052. *
  1053. * Return parameters as an array - Use TMPL one eventually
  1054. *
  1055. * @param string
  1056. */
  1057. function assign_parameters($str)
  1058. {
  1059. if ($str == "")
  1060. {
  1061. return FALSE;
  1062. }
  1063. // \047 - Single quote octal
  1064. // \042 - Double quote octal
  1065. // I don't know for sure, but I suspect using octals is more reliable than ASCII.
  1066. // I ran into a situation where a quote wasn't being matched until I switched to octal.
  1067. // I have no idea why, so just to be safe I used them here. - Rick
  1068. if (preg_match_all("/(\S+?)\s*=[\042\047](\s*.+?\s*)[\042\047]\s*/", $str, $matches))
  1069. {
  1070. $result = array();
  1071. for ($i = 0; $i < count($matches['1']); $i++)
  1072. {
  1073. $result[$matches['1'][$i]] = $matches['2'][$i];
  1074. }
  1075. return $result;
  1076. }
  1077. return FALSE;
  1078. }
  1079. // ------------------------------------------------------------------------
  1080. /**
  1081. * parse_field
  1082. *
  1083. * @param mixed - params
  1084. * @param
  1085. * @param string
  1086. */
  1087. function parse_field($params, $field_data, $field_group)
  1088. {
  1089. $field_id = '1';
  1090. $format = 'none';
  1091. /** -----------------------------
  1092. /** Determine Field Id and Format
  1093. /** -----------------------------*/
  1094. if ( ! is_array($params))
  1095. {
  1096. $field_id = $params;
  1097. $this->EE->db->select('field_fmt');
  1098. $this->EE->db->where('field_id', $field_id);
  1099. $results = $this->EE->db->get('channel_fields');
  1100. $format = ($results->num_rows() > 0) ? $results->row('field_fmt') : 'none';
  1101. }
  1102. else
  1103. {
  1104. if ($params['name'] != '' && $params['format'] == '')
  1105. {
  1106. $xsql = ($this->EE->config->item('moblog_allow_nontextareas') == 'y') ? "" : " AND exp_channel_fields.field_type = 'textarea' ";
  1107. $this->EE->db->select('field_id, field_fmt');
  1108. $this->EE->db->where('group_id', $field_id);
  1109. $this->EE->db->where('(field_name = "'.$params['name'].'" OR field_label = "'.$params['name'].'")', NULL, FALSE);
  1110. if ($this->EE->config->item('moblog_allow_nontextareas') != 'y')
  1111. {
  1112. $this->EE->db->where('field_type', 'textarea');
  1113. }
  1114. $results = $this->EE->db->get('channel_fields');
  1115. $field_id = ($results->num_rows() > 0) ? $results->row('field_id') : $this->moblog_array['moblog_field_id'];
  1116. $format = ($results->num_rows() > 0) ? $results->row('field_fmt') : 'none';
  1117. }
  1118. elseif($params['name'] == '' && $params['format'] == '')
  1119. {
  1120. $field_id = $this->moblog_array['moblog_field_id'];
  1121. $this->EE->db->seledct('field_fmt');
  1122. $this->EE->db->where('field_id', $field_id);
  1123. $results = $this->EE->db->get('channel_fields');
  1124. $format = $results->row('field_fmt') ;
  1125. }
  1126. elseif($params['name'] == '' && $params['format'] != '')
  1127. {
  1128. $field_id = $this->moblog_array['moblog_field_id'];
  1129. $format = $params['format'];
  1130. }
  1131. elseif($params['name'] != '' && $params['format'] != '')
  1132. {
  1133. $xsql = ($this->EE->config->item('moblog_allow_nontextareas') == 'y') ? "" : " AND exp_channel_fields.field_type = 'textarea' ";
  1134. $this->EE->db->select('field_id');
  1135. $this->EE->db->where('group_id', $field_group);
  1136. $this->EE->db->where('(field_name = "'.$params['name'].'" OR field_label = "'.$params['name'].'")');
  1137. if ($this->EE->config->item('moblog_allow_nontextareas') != 'y')
  1138. {
  1139. $this->EE->db->where('field_type', 'textarea');
  1140. }
  1141. $results = $this->EE->db->get('channel_fields');
  1142. $field_id = ($results->num_rows() > 0) ? $results->row('field_id') : $this->moblog_array['moblog_field_id'];
  1143. $format = $params['format'];
  1144. }
  1145. }
  1146. /** -----------------------------
  1147. /** Parse Content
  1148. /** -----------------------------*/
  1149. $pair_array = array('images','audio','movie','files');
  1150. $float_data = $this->post_data;
  1151. $params = array();
  1152. foreach ($pair_array as $type)
  1153. {
  1154. if ( ! preg_match_all("/".LD.$type."(.*?)".RD."(.*?)".LD.'\/'.$type.RD."/s", $field_data, $matches))
  1155. {
  1156. continue;
  1157. }
  1158. if(count($matches['0']) == 0)
  1159. {
  1160. continue;
  1161. }
  1162. for ($i=0; $i < count($matches['0']) ; $i++)
  1163. {
  1164. $template_data = '';
  1165. if ($type != 'files' && ( ! isset($float_data[$type]) OR count($float_data[$type]) == 0))
  1166. {
  1167. $field_data = str_replace($matches['0'][$i],'',$field_data);
  1168. continue;
  1169. }
  1170. // Assign parameters, if any
  1171. if(isset($matches['1'][$i]) && trim($matches['1'][$i]) != '')
  1172. {
  1173. $params = $this->assign_parameters(trim($matches['1'][$i]));
  1174. }
  1175. $params['match'] = ( ! isset($params['match'])) ? '' : $params['match'];
  1176. /** ----------------------------
  1177. /** Parse Pairs
  1178. /** ----------------------------*/
  1179. // Files is a bit special. It goes last and will clear out remaining files. Has match parameter
  1180. if ($type == 'files' && $params['match'] != '')
  1181. {
  1182. if (count($float_data) > 0)
  1183. {
  1184. foreach ($float_data as $ftype => $value)
  1185. {
  1186. if (in_array($ftype,$pair_array) && ($params['match'] == 'all' OR stristr($params['match'],$ftype)))
  1187. {
  1188. foreach($float_data[$ftype] as $k => $file)
  1189. {
  1190. if ( ! is_array($file))
  1191. {
  1192. $template_data .= str_replace('{file}',$this->upload_dir_code.$file,$matches['2'][$i]);
  1193. }
  1194. elseif(is_array($file) && $ftype == 'images')
  1195. {
  1196. $temp_data = '';
  1197. $details = array();
  1198. $filename = ( ! isset($file['filename'])) ? '' : $this->upload_dir_code.$file['filename'];
  1199. $details['width'] = ( ! isset($file['width'])) ? '' : $file['width'];
  1200. $details['height'] = ( ! isset($file['height'])) ? '' : $file['height'];
  1201. $details['thumbnail'] = ( ! isset($file['thumbnail'])) ? '' : $this->upload_dir_code.$file['thumbnail'];
  1202. $details['thumb_width'] = ( ! isset($file['thumb_width'])) ? '' : $file['thumb_width'];
  1203. $details['thumb_height'] = ( ! isset($file['thumb_height'])) ? '' : $file['thumb_height'];
  1204. $temp_data = str_replace('{file}',$filename,$matches['2'][$i]);
  1205. foreach($details as $d => $dv)
  1206. {
  1207. $temp_data = str_replace('{'.$d.'}',$dv,$temp_data);
  1208. }
  1209. $template_data .= $temp_data;
  1210. }
  1211. //unset($float_data[$ftype][$k]);
  1212. }
  1213. }
  1214. }
  1215. }
  1216. }
  1217. elseif(isset($float_data[$type]))
  1218. {
  1219. foreach($float_data[$type] as $k => $file)
  1220. {
  1221. if ( ! is_array($file))
  1222. {
  1223. $template_data .= str_replace('{file}',$this->upload_dir_code.$file,$matches['2'][$i]);
  1224. }
  1225. elseif(is_array($file) && $type == 'images')
  1226. {
  1227. $temp_data = '';
  1228. $details = array();
  1229. $filename = ( ! isset($file['filename'])) ? '' : $this->upload_dir_code.$file['filename'];
  1230. $details['width'] = ( ! isset($file['width'])) ? '' : $file['width'];
  1231. $details['height'] = ( ! isset($file['height'])) ? '' : $file['height'];
  1232. $details['thumbnail'] = ( ! isset($file['thumbnail'])) ? '' : $this->upload_dir_code.$file['thumbnail'];
  1233. $details['thumb_width'] = ( ! isset($file['thumb_width'])) ? '' : $file['thumb_width'];
  1234. $details['thumb_height'] = ( ! isset($file['thumb_height'])) ? '' : $file['thumb_height'];
  1235. $temp_data = str_replace('{file}',$filename,$matches['2'][$i]);
  1236. foreach($details as $d => $dv)
  1237. {
  1238. $temp_data = str_replace('{'.$d.'}',$dv,$temp_data);
  1239. }
  1240. $template_data .= $temp_data;
  1241. }
  1242. //unset($float_data[$type][$k]);
  1243. }
  1244. }
  1245. // Replace tag pair with template data
  1246. $field_data = str_replace($matches['0'][$i],$template_data,$field_data);
  1247. // Unset member of float data array
  1248. if (isset($float_data[$type]) && count($float_data[$type]) == 0)
  1249. {
  1250. unset($float_data[$type]);
  1251. }
  1252. }
  1253. }
  1254. /** ------------------------------
  1255. /** Variable Single: text
  1256. /** ------------------------------*/
  1257. $field_data = str_replace(array('{text}', '{sender_email}'), array($this->body, $this->sender_email), $field_data);
  1258. $this->entry_data[$field_id]['data'] = ( ! isset($this->entry_data[$field_id])) ? $field_data : $this->entry_data[$field_id]['data']."\n".$field_data;
  1259. $this->entry_data[$field_id]['format'] = $format;
  1260. }
  1261. // ------------------------------------------------------------------------
  1262. /**
  1263. * Parse Email
  1264. *
  1265. * @param mixed - Email Data
  1266. * @param
  1267. */
  1268. function parse_email($email_data,$type='norm')
  1269. {
  1270. $boundary = ($type != 'norm') ? $this->multi_boundary : $this->boundary;
  1271. $email_data = str_replace('boundary='.substr($boundary,2),'BOUNDARY_HERE',$email_data);
  1272. $email_parts = explode($boundary, $email_data);
  1273. if (count($email_parts) < 2)
  1274. {
  1275. $boundary = str_replace("+","\+", $boundary);
  1276. $email_parts = explode($boundary, $email_data);
  1277. }
  1278. if (count($email_parts) < 2)
  1279. {
  1280. return FALSE;
  1281. unset($email_parts);
  1282. unset($email_data);
  1283. }
  1284. /** ---------------------------
  1285. /** Determine Upload Path
  1286. /** ---------------------------*/
  1287. $query = $this->EE->db->query("SELECT server_path FROM exp_upload_prefs
  1288. WHERE id = '".$this->EE->db->escape_str($this->moblog_array['moblog_upload_directory'])."'");
  1289. if ($query->num_rows() == 0)
  1290. {
  1291. $this->message_array[] = 'invalid_upload_directory';
  1292. return FALSE;
  1293. }
  1294. $this->upload_path = $query->row('server_path');
  1295. if ( ! is_really_writable($this->upload_path))
  1296. {
  1297. $system_absolute = str_replace('/modules/moblog/mod.moblog.php','',__FILE__);
  1298. $addon = (substr($this->upload_path,0,2) == './') ? substr($this->upload_path,2) : $this->upload_path;
  1299. while(substr($addon,0,3) == '../')
  1300. {
  1301. $addon = substr($addon,3);
  1302. $p1 = (strrpos($system_absolute,'/') !== FALSE) ? strrpos($system_absolute,'/') : strlen($system_absolute);
  1303. $system_absolute = substr($system_absolute,0,$p1);
  1304. }
  1305. if (substr($system_absolute,-1) != '/')
  1306. {
  1307. $system_absolute .= '/';
  1308. }
  1309. $this->upload_path =

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