PageRenderTime 41ms CodeModel.GetById 15ms 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
  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 = $system_absolute.$addon;
  1310. if ( ! is_really_writable($this->upload_path))
  1311. {
  1312. $this->message_array[] = 'upload_directory_unwriteable';
  1313. return FALSE;
  1314. }
  1315. }
  1316. if (substr($this->upload_path, -1) != '/')
  1317. {
  1318. $this->upload_path .= '/';
  1319. }
  1320. $this->upload_dir_code = '{filedir_'.$this->moblog_array['moblog_upload_directory'].'}';
  1321. /** ---------------------------
  1322. /** Find Attachments
  1323. /** ---------------------------*/
  1324. foreach($email_parts as $key => $value)
  1325. {
  1326. // Skip headers and those with no content-type
  1327. if ($key == '0' OR stristr($value, 'Content-Type:') === FALSE)
  1328. {
  1329. continue;
  1330. }
  1331. $contents = $this->find_data($value, "Content-Type:", $this->newline);
  1332. $x = explode(';',$contents);
  1333. $content_type = $x['0'];
  1334. $content_type = strtolower($content_type);
  1335. $pieces = explode('/',trim($content_type));
  1336. $type = trim($pieces['0']);
  1337. $subtype = ( ! isset($pieces['1'])) ? '0' : trim($pieces['1']);
  1338. $charset = 'auto';
  1339. /** --------------------------
  1340. /** Outlook Exception
  1341. /** --------------------------*/
  1342. if ($type == 'multipart' && $subtype != 'appledouble')
  1343. {
  1344. if( ! stristr($value,'boundary='))
  1345. {
  1346. continue;
  1347. }
  1348. $this->multi_boundary = "--".$this->find_data($value, "boundary=", $this->newline);
  1349. $this->multi_boundary = trim(str_replace('"','',$this->multi_boundary));
  1350. if (strlen($this->multi_boundary) == 0)
  1351. {
  1352. continue;
  1353. }
  1354. $this->parse_email($value,'multi');
  1355. $this->multi_boundary = '';
  1356. continue;
  1357. }
  1358. /** --------------------------
  1359. /** Quick Grab of Headers
  1360. /** --------------------------*/
  1361. $headers = $this->find_data($value, '', $this->newline.$this->newline);
  1362. /** ---------------------------
  1363. /** Text : plain, html, rtf
  1364. /** ---------------------------*/
  1365. if ($type == 'text' && $headers != '' &&
  1366. (($this->txt_override === TRUE && $subtype == 'plain') OR ! stristr($headers,'name=')))
  1367. {
  1368. $duo = $this->newline.$this->newline;
  1369. $text = $this->find_data($value, $duo,'');
  1370. if ($text == '')
  1371. {
  1372. $text = $this->find_data($value, $this->newline,'');
  1373. }
  1374. /** ------------------------------------
  1375. /** Charset Available?
  1376. /** ------------------------------------*/
  1377. if (preg_match("/charset=(.*?)(\s|".$this->newline.")/is", $headers, $match))
  1378. {
  1379. $charset = trim(str_replace(array("'", '"', ';'), '', $match['1']));
  1380. }
  1381. /** ------------------------------------
  1382. /** Check for Encoding of Text
  1383. /** ------------------------------------*/
  1384. if (stristr($value,'Content-Transfer-Encoding'))
  1385. {
  1386. $encoding = $this->find_data($value, "Content-Transfer-Encoding:", $this->newline);
  1387. /** ------------------------------------
  1388. /** Check for Quoted-Printable encoding
  1389. /** ------------------------------------*/
  1390. if(stristr($encoding,"quoted-printable"))
  1391. {
  1392. $text = str_replace($this->newline,"\n",$text);
  1393. $text = quoted_printable_decode($text);
  1394. $text = (substr($text,0,1) != '=') ? $text : substr($text,1);
  1395. $text = (substr($text,-1) != '=') ? $text : substr($text,0,-1);
  1396. $text = $this->remove_newlines($text,$this->newline);
  1397. }
  1398. /** ------------------------------------
  1399. /** Check for Base 64 encoding: MIME
  1400. /** ------------------------------------*/
  1401. elseif(stristr($encoding,"base64"))
  1402. {
  1403. $text = str_replace($this->newline,"\n", $text);
  1404. $text = base64_decode(trim($text));
  1405. $text = $this->remove_newlines($text,$this->newline);
  1406. }
  1407. }
  1408. /** ----------------------------------
  1409. /** Spring PCS - Picture Share
  1410. /** ----------------------------------*/
  1411. // http://pictures.sprintpcs.com//shareImage/13413001858_235.jpg
  1412. // http://pictures.sprintpcs.com/mi/8516539_30809087_0.jpeg?inviteToken=sETr4TJ9m85YizVzoka0
  1413. if (trim($text) != '' && strpos($text, 'pictures.sprintpcs.com') !== FALSE)
  1414. {
  1415. // Find Message
  1416. $sprint_msg = $this->find_data($value, '<b>Message:</b>', '</font>');
  1417. // Find Image
  1418. if ($this->sprint_image($text) && $sprint_msg != '')
  1419. {
  1420. $text = $sprint_msg;
  1421. }
  1422. else
  1423. {
  1424. continue;
  1425. }
  1426. }
  1427. /** ----------------------------------
  1428. /** Bell Canada - Episode Two, Attack of the Sprint Clones
  1429. /** ----------------------------------*/
  1430. // http://mypictures.bell.ca//i/99376001_240.jpg?invite=SELr4RJHhma1cknzLQoU
  1431. if (trim($text) != '' && strpos($text, 'mypictures.bell.ca') !== FALSE)
  1432. {
  1433. // Find Message
  1434. $bell_msg = $this->find_data($value, 'Vous avez re&ccedil;u une photo de <b>5147103855', '<img');
  1435. $bell_msg = $this->find_data($bell_msg, '<p>', '</p>');
  1436. // Find Image
  1437. if ($this->bell_image($text) && $bell_msg != '')
  1438. {
  1439. $text = trim($bell_msg);
  1440. }
  1441. else
  1442. {
  1443. continue;
  1444. }
  1445. }
  1446. /** ----------------------------------
  1447. /** T-Mobile - In cyberspace, no one can hear you cream.
  1448. /** ----------------------------------*/
  1449. if (trim($text) != '' && stristr($text, 'This message was sent from a T-Mobile wireless phone') !== FALSE)
  1450. {
  1451. $text = '';
  1452. }
  1453. if ($this->charset != $this->EE->config->item('charset'))
  1454. {
  1455. if (function_exists('mb_convert_encoding'))
  1456. {
  1457. $text = mb_convert_encoding($text, strtoupper($this->EE->config->item('charset')), strtoupper($this->charset));
  1458. }
  1459. elseif(function_exists('iconv') AND ($iconvstr = @iconv(strtoupper($this->charset), strtoupper($this->EE->config->item('charset')), $text)) !== FALSE)
  1460. {
  1461. $text = $iconvstr;
  1462. }
  1463. elseif(strtolower($this->EE->config->item('charset')) == 'utf-8' && strtolower($this->charset) == 'iso-8859-1')
  1464. {
  1465. $text = utf8_encode($text);
  1466. }
  1467. elseif(strtolower($this->EE->config->item('charset')) == 'iso-8859-1' && strtolower($this->charset) == 'utf-8')
  1468. {
  1469. $text = utf8_decode($text);
  1470. }
  1471. }
  1472. // RTF and HTML are considered alternative text
  1473. $subtype = ($subtype != 'html' && $subtype != 'rtf') ? 'plain' : 'alt';
  1474. // Same content type, then join together
  1475. $this->post_data[$type][$subtype] = (isset($this->post_data[$type][$subtype])) ? $this->post_data[$type][$subtype]." $text" : $text;
  1476. // Plain text takes priority for body data.
  1477. $this->body = ( ! isset($this->post_data[$type]['plain'])) ? $this->post_data[$type]['alt'] : $this->post_data[$type]['plain'];
  1478. }
  1479. elseif($type == 'image' OR $type == 'application' OR $type == 'audio' OR $type == 'video' OR $subtype == 'appledouble' OR $type == 'text') // image or application
  1480. {
  1481. if ($subtype == 'appledouble')
  1482. {
  1483. if ( ! $data = $this->appledouble($value))
  1484. {
  1485. continue;
  1486. }
  1487. else
  1488. {
  1489. $value = $data['value'];
  1490. $subtype = $data['subtype'];
  1491. $type = $data['type'];
  1492. unset($data);
  1493. }
  1494. }
  1495. /** ------------------------------
  1496. /** Determine Filename
  1497. /** ------------------------------*/
  1498. $contents = $this->find_data($value, "name=", $this->newline);
  1499. if ($contents == '')
  1500. {
  1501. $contents = $this->find_data($value, 'Content-Location:', $this->newline);
  1502. }
  1503. if ($contents == '')
  1504. {
  1505. $contents = $this->find_data($value, 'Content-ID:', $this->newline);
  1506. $contents = str_replace('<','', $contents);
  1507. $contents = str_replace('<','', $contents);
  1508. }
  1509. $x = explode(';',trim($contents));
  1510. $filename = ($x['0'] == '') ? 'moblogfile' : $x['0'];
  1511. $filename = trim(str_replace('"','',$filename));
  1512. $filename = str_replace($this->newline,'',$filename);
  1513. $filename = $this->safe_filename($filename);
  1514. if (stristr($filename, 'dottedline') OR stristr($filename, 'spacer.gif') OR stristr($filename, 'masthead.jpg'))
  1515. {
  1516. continue;
  1517. }
  1518. /** ------------------------------
  1519. /** Check and adjust for multiple files with same file name
  1520. /** ------------------------------*/
  1521. $filename = $this->unique_filename($filename, $subtype);
  1522. /** --------------------------------
  1523. /** File/Image Code and Cleanup
  1524. /** --------------------------------*/
  1525. $duo = $this->newline.$this->newline;
  1526. $file_code = $this->find_data($value, $duo,'');
  1527. if ($file_code == '')
  1528. {
  1529. $file_code = $this->find_data($value, $this->newline,'');
  1530. if ($file_code == '')
  1531. {
  1532. $this->message_array = 'invalid_file_data';
  1533. return FALSE;
  1534. }
  1535. }
  1536. /** --------------------------------
  1537. /** Determine Encoding
  1538. /** --------------------------------*/
  1539. $contents = $this->find_data($value, "Content-Transfer-Encoding:", $this->newline);
  1540. $x = explode(';',$contents);
  1541. $encoding = $x['0'];
  1542. $encoding = trim(str_replace('"','',$encoding));
  1543. $encoding = str_replace($this->newline,'',$encoding);
  1544. if ( ! stristr($encoding,"base64") && ! stristr($encoding,"7bit") && ! stristr($encoding,"8bit") && ! stristr($encoding,"quoted-printable"))
  1545. {
  1546. if ($type == 'text')
  1547. {
  1548. // RTF and HTML are considered alternative text
  1549. $subtype = ($subtype != 'html' && $subtype != 'rtf') ? 'plain' : 'alt';
  1550. // Same content type, then join together
  1551. $this->post_data[$type][$subtype] = (isset($this->post_data[$type][$subtype])) ? $this->post_data[$type][$subtype].' '.$file_code : $file_code;
  1552. // Plain text takes priority for body data.
  1553. $this->body = ( ! isset($this->post_data[$type]['plain'])) ? $this->post_data[$type]['alt'] : $this->post_data[$type]['plain'];
  1554. }
  1555. continue;
  1556. }
  1557. // Eudora and Mail.app use this by default
  1558. if(stristr($encoding,"quoted-printable"))
  1559. {
  1560. $file_code = quoted_printable_decode($file_code);
  1561. }
  1562. // Base64 gets no space and no line breaks
  1563. $replace = ( ! stristr($encoding,"base64")) ? "\n" : '';
  1564. $file_code = trim(str_replace($this->newline,$replace,$file_code));
  1565. // PHP function sometimes misses opening and closing equal signs
  1566. if(stristr($encoding,"quoted-printable"))
  1567. {
  1568. $file_code = (substr($file_code,0,1) != '=') ? $file_code : substr($file_code,1);
  1569. $file_code = (substr($file_code,-1) != '=') ? $file_code : substr($file_code,0,-1);
  1570. }
  1571. // Clean out 7bit and 8bit files.
  1572. if ( ! stristr($encoding,"base64"))
  1573. {
  1574. $file_code = $this->EE->security->xss_clean($file_code);
  1575. }
  1576. /** ------------------------------
  1577. /** Check and adjust for multiple files with same file name
  1578. /** ------------------------------*/
  1579. $filename = $this->unique_filename($filename, $subtype);
  1580. /** ---------------------------
  1581. /** Put Info in Post Data array
  1582. /** ---------------------------*/
  1583. if (in_array(substr($filename,-3),$this->movie) OR in_array(substr($filename,-5),$this->movie)) // Movies
  1584. {
  1585. $this->post_data['movie'][] = $filename;
  1586. }
  1587. elseif (in_array(substr($filename,-3),$this->audio) OR in_array(substr($filename,-4),$this->audio) OR in_array(substr($filename,-2),$this->audio)) // Audio
  1588. {
  1589. $this->post_data['audio'][] = $filename;
  1590. }
  1591. elseif (in_array(substr($filename,-3),$this->image) OR in_array(substr($filename,-4),$this->image)) // Images
  1592. {
  1593. $this->post_data['images'][] = array('filename' => $filename);
  1594. $key = count($this->post_data['images']) - 1;
  1595. $type = 'image'; // For those crazy application/octet-stream images
  1596. }
  1597. elseif (in_array(substr($filename,-2),$this->files) OR in_array(substr($filename,-3),$this->files) OR in_array(substr($filename,-4),$this->files)) // Files
  1598. {
  1599. $this->post_data['files'][] = $filename;
  1600. }
  1601. else
  1602. {
  1603. // $this->post_data['files'][] = $filename;
  1604. continue;
  1605. }
  1606. // AT&T phones send the message as a .txt file
  1607. // This checks to see if this email is from an AT&T phone,
  1608. // not an encoded file, and has a .txt file extension in the filename
  1609. if ($this->attach_as_txt === TRUE && ! stristr($encoding,"base64"))
  1610. {
  1611. if(stristr($filename,'.txt') && preg_match("/Content-Disposition:\s*inline/i",$headers,$found))
  1612. {
  1613. $this->attach_text = $file_code;
  1614. $this->attach_name = $filename;
  1615. continue; // No upload of file.
  1616. }
  1617. }
  1618. /** ------------------------------
  1619. /** Write File to Upload Directory
  1620. /** ------------------------------*/
  1621. if ( ! $fp = @fopen($this->upload_path.$filename,FOPEN_WRITE_CREATE_DESTRUCTIVE))
  1622. {
  1623. $this->message_array[] = 'error_writing_attachment'; //.$this->upload_path.$filename;
  1624. return FALSE;
  1625. }
  1626. $attachment = ( ! stristr($encoding,"base64")) ? $file_code : base64_decode($file_code);
  1627. fwrite($fp,$attachment);
  1628. fclose($fp);
  1629. @chmod($this->upload_path.$filename, FILE_WRITE_MODE);
  1630. unset($attachment);
  1631. unset($file_code);
  1632. $this->email_files[] = $filename;
  1633. $this->uploads++;
  1634. // Only images beyond this point.
  1635. if ($type != 'image')
  1636. {
  1637. continue;
  1638. }
  1639. $this->image_resize($filename, $key);
  1640. } // End files/images section
  1641. } // End foreach
  1642. return TRUE;
  1643. }
  1644. // ------------------------------------------------------------------------
  1645. /**
  1646. * Remove Images from Sprint.
  1647. *
  1648. * eg: <img src="http://pictures.sprintpcs.com//shareImage/13413001858_235.jpg?border=1,255,255,255,1,0,0,0&amp;invite=OEKJJD5XYYhMZ5hY8amx" border="0" />
  1649. *
  1650. * @param string
  1651. */
  1652. function sprint_image($text)
  1653. {
  1654. if (preg_match_all("|(http://pictures.sprintpcs.com(.*?))\?inviteToken\=(.*?)&|i", str_replace($this->newline,"\n",$text), $matches))
  1655. {
  1656. for($i = 0; $i < count($matches['0']); $i++)
  1657. {
  1658. /*
  1659. if (stristr($matches['1'][$i], 'jpeg') === FALSE && stristr($matches['1'][$i], 'jpg') === FALSE)
  1660. {
  1661. continue;
  1662. }
  1663. */
  1664. /** ------------------------------
  1665. /** Filename Creation
  1666. /** ------------------------------*/
  1667. $x = explode('/', $matches['1'][$i]);
  1668. $filename = array_pop($x);
  1669. if (strlen($filename) < 4)
  1670. {
  1671. $filename .= array_pop($x);
  1672. }
  1673. if (stristr($filename, 'jpeg') === FALSE && stristr($filename, 'jpg') === FALSE)
  1674. {
  1675. $filename .= '.jpg';
  1676. }
  1677. /** -------------------------------
  1678. /** Download Image
  1679. /** -------------------------------*/
  1680. $image_url = $matches['1'][$i];
  1681. $invite = $matches['3'][$i];
  1682. $r = "\r\n";
  1683. $bits = parse_url($image_url);
  1684. if ( ! isset($bits['path']))
  1685. {
  1686. return FALSE;
  1687. }
  1688. $host = $bits['host'];
  1689. $path = ( ! isset($bits['path'])) ? '/' : $bits['path'];
  1690. $path .= "inviteToken={$invite}";
  1691. if ( ! $fp = @fsockopen ($host, 80))
  1692. {
  1693. continue;
  1694. }
  1695. fputs ($fp, "GET " . $path . " HTTP/1.0\r\n" );
  1696. fputs ($fp, "Host: " . $bits['host'] . "\r\n" );
  1697. fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" );
  1698. fputs ($fp, "User-Agent: EE/EllisLab PHP/" . phpversion() . "\r\n");
  1699. fputs ($fp, "Connection: close\r\n\r\n" );
  1700. $this->external_image($fp, $filename);
  1701. }
  1702. }
  1703. if(preg_match_all("#<img\s+src=\s*[\"']http://pictures.sprintpcs.com/+shareImage/(.+?)[\"'](.*?)\s*\>#si", $text, $matches))
  1704. {
  1705. for($i = 0; $i < count($matches['0']); $i++)
  1706. {
  1707. $parts = explode('jpg',$matches['1'][$i]);
  1708. if ( ! isset($parts['1']))
  1709. {
  1710. continue;
  1711. }
  1712. $filename = $parts['0'].'jpg';
  1713. $image_url = 'http://pictures.sprintpcs.com/shareImage/'.$filename;
  1714. $invite = $this->find_data($parts['1'], 'invite=','');
  1715. if ($invite == '')
  1716. {
  1717. $invite = $this->find_data($parts['1'], 'invite=','&');
  1718. if ($invite == '')
  1719. {
  1720. continue;
  1721. }
  1722. }
  1723. /** -------------------------------
  1724. /** Download Image
  1725. /** -------------------------------*/
  1726. $r = "\r\n";
  1727. $bits = parse_url($image_url);
  1728. if ( ! isset($bits['path']))
  1729. {
  1730. return FALSE;
  1731. }
  1732. $host = $bits['host'];
  1733. $path = ( ! isset($bits['path'])) ? '/' : $bits['path'];
  1734. $data = "invite={$invite}";
  1735. if ( ! $fp = @fsockopen ($host, 80))
  1736. {
  1737. continue;
  1738. }
  1739. fputs ($fp, "GET " . $path . " HTTP/1.0\r\n" );
  1740. fputs ($fp, "Host: " . $bits['host'] . "\r\n" );
  1741. fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" );
  1742. fputs ($fp, "User-Agent: EE/EllisLab PHP/" . phpversion() . "\r\n");
  1743. fputs ($fp, "Content-length: " . strlen($data) . "\r\n" );
  1744. fputs ($fp, "Connection: close\r\n\r\n" );
  1745. fputs ($fp, $data);
  1746. $this->external_image($fp, $filename);
  1747. }
  1748. }
  1749. return TRUE;
  1750. }
  1751. // ------------------------------------------------------------------------
  1752. /**
  1753. * Bell Images
  1754. *
  1755. * eg: <img src="http://mypictures.bell.ca//i/99376001_240.jpg?invite=SELr4RJHhma1cknzLQoU" alt="Retrieving picture..."/>
  1756. *
  1757. * @param string
  1758. */
  1759. function bell_image($text)
  1760. {
  1761. $text = trim(str_replace($this->newline,"\n",$text));
  1762. if(preg_match_all("#<img\s+src=\"http://mypictures.bell.ca(.*?)\"(.*?)alt=\"Retrieving picture\.\.\.\"(.*?)\/\>#i", $text, $matches))
  1763. {
  1764. for($i = 0; $i < count($matches['0']); $i++)
  1765. {
  1766. $parts = explode('jpg',$matches['1'][$i]);
  1767. if ( ! isset($parts['1']))
  1768. {
  1769. continue;
  1770. }
  1771. else
  1772. {
  1773. $pos = strrpos($parts['0'], '/');
  1774. if ($pos === FALSE)
  1775. {
  1776. continue;
  1777. }
  1778. $parts['0'] = substr($parts['0'], $pos+1, strlen($parts['0'])-$pos-1);
  1779. }
  1780. $filename = $parts['0'].'jpg';
  1781. $image_url = 'http://mypictures.bell.ca'.$matches['1'][$i];
  1782. /** -------------------------------
  1783. /** Download Image
  1784. /** -------------------------------*/
  1785. $r = "\r\n";
  1786. $bits = parse_url($image_url);
  1787. if ( ! isset($bits['path']))
  1788. {
  1789. return FALSE;
  1790. }
  1791. $host = $bits['host'];
  1792. $path = ( ! isset($bits['path'])) ? '/' : $bits['path'];
  1793. if ( ! $fp = @fsockopen ($host, 80))
  1794. {
  1795. continue;
  1796. }
  1797. fputs ($fp, "GET " . $path.'?'.$bits['query'] . " HTTP/1.0\r\n" );
  1798. fputs ($fp, "Host: " . $bits['host'] . "\r\n" );
  1799. fputs ($fp, "User-Agent: EE/EllisLab PHP/" . phpversion() . "\r\n");
  1800. fputs ($fp, "Connection: close\r\n\r\n" );
  1801. $this->external_image($fp, $filename);
  1802. }
  1803. }
  1804. return TRUE;
  1805. }
  1806. // ------------------------------------------------------------------------
  1807. /**
  1808. * Get Images from External Server
  1809. *
  1810. * @param string
  1811. * @param string
  1812. */
  1813. function external_image($fp, $filename)
  1814. {
  1815. $data = '';
  1816. $headers = '';
  1817. $getting_headers = TRUE;
  1818. while ( ! feof($fp))
  1819. {
  1820. $line = fgets($fp, 4096);
  1821. if ($getting_headers == FALSE)
  1822. {
  1823. $data .= $line;
  1824. }
  1825. elseif (trim($line) == '')
  1826. {
  1827. $getting_headers = FALSE;
  1828. }
  1829. else
  1830. {
  1831. $headers .= $line;
  1832. }
  1833. }
  1834. /** -------------------------------
  1835. /** Save Image
  1836. /** -------------------------------*/
  1837. $filename = $this->safe_filename($filename);
  1838. $filename = $this->unique_filename($filename);
  1839. $this->post_data['images'][] = array( 'filename' => $filename);
  1840. $key = count($this->post_data['images']) - 1;
  1841. if ( ! $fp = @fopen($this->upload_path.$filename,FOPEN_WRITE_CREATE_DESTRUCTIVE))
  1842. {
  1843. $this->message_array[] = 'error_writing_attachment'; //.$this->upload_path.$filename;
  1844. return FALSE;
  1845. }
  1846. @fwrite($fp,$data);
  1847. @fclose($fp);
  1848. @chmod($this->upload_path.$filename, FILE_WRITE_MODE);
  1849. $this->email_files[] = $filename;
  1850. $this->uploads++;
  1851. /** -------------------------------
  1852. /** Image Resizing
  1853. /** -------------------------------*/
  1854. $this->image_resize($filename,$key);
  1855. return TRUE;
  1856. }
  1857. // ------------------------------------------------------------------------
  1858. /**
  1859. * Strip Apple Double Crap
  1860. *
  1861. * @param string
  1862. */
  1863. function appledouble($data)
  1864. {
  1865. if (stristr($data, 'boundary=') === FALSE)
  1866. {
  1867. return FALSE;
  1868. }
  1869. $boundary = "--".$this->find_data($data, "boundary=", $this->newline);
  1870. $boundary = trim(str_replace('"','',$boundary));
  1871. $boundary = str_replace("+","\+", $boundary);
  1872. $email_parts = explode($boundary, $data);
  1873. if (count($email_parts) < 2)
  1874. {
  1875. return FALSE;
  1876. }
  1877. foreach($email_parts as $value)
  1878. {
  1879. $content_type = $this->find_data($value, "Content-Type:", ";");
  1880. $pieces = explode('/',trim($content_type));
  1881. $type = trim($pieces['0']);
  1882. $subtype = ( ! isset($pieces['1'])) ? '0' : trim($pieces['1']);
  1883. if ($type == 'image' OR $type == 'audio' OR $type == 'video')
  1884. {
  1885. $data = array( 'value' => $value,
  1886. 'type' => $type,
  1887. 'subtype' => $subtype);
  1888. return $data;
  1889. }
  1890. }
  1891. return FALSE;
  1892. }
  1893. // ------------------------------------------------------------------------
  1894. /**
  1895. * Check Login
  1896. */
  1897. function check_login()
  1898. {
  1899. $this->body = trim($this->body);
  1900. $login = $this->find_data($this->body, '', $this->newline);
  1901. if ($login == '' OR ! stristr($login,':'))
  1902. {
  1903. $login = $this->find_data($this->body, 'AUTH:', $this->newline);
  1904. }
  1905. if ($login == '' OR ! stristr($login,':'))
  1906. {
  1907. return FALSE;
  1908. }
  1909. $x = explode(":", $login);
  1910. $username = (isset($x['1']) && $x['0'] == 'AUTH') ? $x['1'] : $x['0'];
  1911. $password = (isset($x['2']) && $x['0'] == 'AUTH') ? $x['2'] : $x['1'];
  1912. /** --------------------------------------
  1913. /** Check Username and Password, First
  1914. /** --------------------------------------*/
  1915. $this->EE->db->select('member_id, group_id');
  1916. $this->EE->db->where('username', $username);
  1917. $this->EE->db->where('password', $this->EE->functions->hash(stripslashes($password)));
  1918. $query = $this->EE->db->get('members');
  1919. if ($query->num_rows() == 0)
  1920. {
  1921. return FALSE;
  1922. }
  1923. elseif($query->row('group_id') == '1')
  1924. {
  1925. $this->author = $query->row('member_id') ;
  1926. $this->body = str_replace($login,'',$this->body);
  1927. return TRUE;
  1928. }
  1929. $this->EE->db->where('group_id', $query->row('group_id'));
  1930. $this->EE->db->where('channel_id', $this->moblog_array['moblog_channel_id']);
  1931. $count = $this->EE->db->count_all_results('channel_member_groups');
  1932. if ($count == 0)
  1933. {
  1934. return FALSE;
  1935. }
  1936. $this->author = $query->row('member_id') ;
  1937. $this->body = str_replace($login,'',$this->body);
  1938. return TRUE;
  1939. }
  1940. // ------------------------------------------------------------------------
  1941. /**
  1942. * Find Boundary
  1943. */
  1944. function find_boundary($email_data)
  1945. {
  1946. if (stristr($email_data, 'boundary=') === FALSE)
  1947. {
  1948. return FALSE;
  1949. }
  1950. else
  1951. {
  1952. $this->boundary = "--".$this->find_data($email_data, "boundary=", $this->newline);
  1953. $x = explode(';',$this->boundary);
  1954. $this->boundary = trim(str_replace('"','',$x['0']));
  1955. return TRUE;
  1956. }
  1957. }
  1958. // ------------------------------------------------------------------------
  1959. /**
  1960. * Pop Command.
  1961. *
  1962. * Send pop command to the server.
  1963. *
  1964. * @param string
  1965. * @return string
  1966. */
  1967. function pop_command($cmd = "")
  1968. {
  1969. if ( ! $this->fp)
  1970. {
  1971. return FALSE;
  1972. }
  1973. if ($cmd != "")
  1974. {
  1975. fwrite($this->fp, $cmd.$this->pop_newline);
  1976. }
  1977. $line = $this->remove_newlines(fgets($this->fp, 1024));
  1978. return $line;
  1979. }
  1980. // ------------------------------------------------------------------------
  1981. /**
  1982. * Remove New Lines
  1983. *
  1984. * @param string
  1985. * @param string
  1986. * @return string
  1987. */
  1988. function remove_newlines($str,$replace='')
  1989. {
  1990. if (strpos($str, "\r") !== FALSE OR strpos($str, "\n") !== FALSE)
  1991. {
  1992. $str = str_replace(array("\r\n", "\r", "\n"), $replace, $str);
  1993. }
  1994. return $str;
  1995. }
  1996. // ------------------------------------------------------------------------
  1997. /**
  1998. * ISO Clean
  1999. *
  2000. * @param string
  2001. * @return string
  2002. */
  2003. function iso_clean($str)
  2004. {
  2005. if (stristr($str, '=?') === FALSE)
  2006. {
  2007. return $str;
  2008. }
  2009. // -------------------------------------------------
  2010. // There exists two functions that do this for us
  2011. // but they are not available on all servers and some
  2012. // seem to work better than others I have found. The
  2013. // base64_decode() method works for many encodings
  2014. // but I am not sure how well it handles non Latin
  2015. // characters.
  2016. //
  2017. // The mb_decode_mimeheader() function seems to trim
  2018. // any line breaks off the end of the str, so we put
  2019. // those back because we need it for the Header
  2020. // matching stuff. I added it on for the imap_utf8()
  2021. // function just in case.
  2022. // -------------------------------------------------
  2023. if (function_exists('imap_utf8') && strtoupper($this->EE->config->item('charset')) == 'UTF-8')
  2024. {
  2025. return rtrim(imap_utf8($str))."\r\n";
  2026. }
  2027. if (function_exists('mb_decode_mimeheader'))
  2028. {
  2029. // mb_decode_mimeheader() doesn't replace underscores
  2030. return str_replace('_', ' ', rtrim(mb_decode_mimeheader($str)))."\r\n";
  2031. }
  2032. if (function_exists('iconv_mime_decode'))
  2033. {
  2034. return rtrim(iconv_mime_decode($str))."\r\n";
  2035. }
  2036. if (substr(trim($str), -2) != '?=')
  2037. {
  2038. $str = trim($str).'?=';
  2039. }
  2040. if (preg_match("|\=\?iso\-(.*?)\?[A-Z]{1}\?(.*?)\?\=|i", trim($str), $mime))
  2041. {
  2042. if ($mime['1'] == '8859-1')
  2043. {
  2044. $charHex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  2045. for ($z=0, $sz=count($charHex); $z < $sz; ++$z)
  2046. {
  2047. for ($i=0, $si=count($charHex); $i < $si; ++$i)
  2048. {
  2049. $mime['2'] = str_replace('='.$charHex[$z].$charHex[$i], chr(hexdec($charHex[$z].$charHex[$i])), $mime['2']);
  2050. }
  2051. }
  2052. $str = str_replace($mime['0'], $mime['2'], $str);
  2053. }
  2054. else
  2055. {
  2056. $str = str_replace($mime['0'], base64_decode($mime['2']), $str);
  2057. }
  2058. $str = str_replace('_', ' ', $str);
  2059. }
  2060. return ltrim($str);
  2061. }
  2062. // ------------------------------------------------------------------------
  2063. /**
  2064. * Find Data
  2065. *
  2066. * @param string
  2067. * @param string
  2068. * @param string
  2069. * @return string
  2070. */
  2071. function find_data($str, $begin, $end)
  2072. {
  2073. $new = '';
  2074. if ($begin == '')
  2075. {
  2076. $p1 = 0;
  2077. }
  2078. else
  2079. {
  2080. if (strpos(strtolower($str), strtolower($begin)) === FALSE)
  2081. {
  2082. return $new;
  2083. }
  2084. $p1 = strpos(strtolower($str), strtolower($begin)) + strlen($begin);
  2085. }
  2086. if ($end == '')
  2087. {
  2088. $p2 = strlen($str);
  2089. }
  2090. else
  2091. {
  2092. if (strpos(strtolower($str), strtolower($end), $p1) === FALSE)
  2093. {
  2094. return $new;
  2095. }
  2096. $p2 = strpos(strtolower($str), strtolower($end), $p1);
  2097. }
  2098. $new = substr($str, $p1, ($p2-$p1));
  2099. return $new;
  2100. }
  2101. // ------------------------------------------------------------------------
  2102. /**
  2103. * Image Properties
  2104. *
  2105. */
  2106. function image_properties($file)
  2107. {
  2108. if (function_exists('getimagesize'))
  2109. {
  2110. if ( ! $D = @getimagesize($file))
  2111. {
  2112. return FALSE;
  2113. }
  2114. $parray = array();
  2115. $parray['width'] = $D['0'];
  2116. $parray['height'] = $D['1'];
  2117. $parray['imgtype'] = $D['2'];
  2118. return $parray;
  2119. }
  2120. return FALSE;
  2121. }
  2122. // ------------------------------------------------------------------------
  2123. /**
  2124. * Safe Filename
  2125. *
  2126. * @param string
  2127. * @return string
  2128. */
  2129. function safe_filename($str)
  2130. {
  2131. $str = strip_tags(strtolower($str));
  2132. $str = preg_replace('/\&#\d+\;/', "", $str);
  2133. // Use dash as separator
  2134. if ($this->EE->config->item('word_separator') == 'dash')
  2135. {
  2136. $trans = array(
  2137. "_" => '-',
  2138. "\&\#\d+?\;" => '',
  2139. "\&\S+?\;" => '',
  2140. "['\"\?\!*\$\#@%;:,\_=\(\)\[\]]" => '',
  2141. "\s+" => '-',
  2142. "\/" => '-',
  2143. "[^a-z0-9-_\.]" => '',
  2144. "-+" => '-',
  2145. "\&" => '',
  2146. "-$" => '',
  2147. "^_" => ''
  2148. );
  2149. }
  2150. else // Use underscore as separator
  2151. {
  2152. $trans = array(
  2153. "-" => '_',
  2154. "\&\#\d+?\;" => '',
  2155. "\&\S+?\;" => '',
  2156. "['\"\?\!*\$\#@%;:,\-=\(\)\[\]]" => '',
  2157. "\s+" => '_',
  2158. "\/" => '_',
  2159. "[^a-z0-9-_\.]" => '',
  2160. "_+" => '_',
  2161. "\&" => '',
  2162. "_$" => '',
  2163. "^_" => ''
  2164. );
  2165. }
  2166. foreach ($trans as $key => $val)
  2167. {
  2168. $str = preg_replace("#".$key."#", $val, $str);
  2169. }
  2170. $str = trim(stripslashes($str));
  2171. return $str;
  2172. }
  2173. // ------------------------------------------------------------------------
  2174. /**
  2175. * Resize Images
  2176. *
  2177. * @param string
  2178. * @param string
  2179. * @return string
  2180. */
  2181. function image_resize($filename, $key)
  2182. {
  2183. /** --------------------------
  2184. /** Set Properties for Image
  2185. /** --------------------------*/
  2186. if( ! $properties = $this->image_properties($this->upload_path.$filename))
  2187. {
  2188. $properties = array('width' => $this->moblog_array['moblog_image_width'],
  2189. 'height' => $this->moblog_array['moblog_image_height']);
  2190. }
  2191. $this->post_data['images'][$key]['width'] = $properties['width'];
  2192. $this->post_data['images'][$key]['height'] = $properties['height'];
  2193. $this->EE->load->library('image_lib');
  2194. $this->EE->image_lib->clear();
  2195. /** ------------------------------
  2196. /** Resize Image
  2197. /** ------------------------------*/
  2198. if ($this->moblog_array['moblog_resize_image'] == 'y')
  2199. {
  2200. if ($this->moblog_array['moblog_resize_width'] != 0 OR $this->moblog_array['moblog_resize_height'] != 0)
  2201. {
  2202. // Temp vars
  2203. $resize_width = $this->moblog_array['moblog_resize_width'];
  2204. $resize_height = $this->moblog_array['moblog_resize_height'];
  2205. /** ----------------------------
  2206. /** Calculations based on one side?
  2207. /** ----------------------------*/
  2208. if ($this->moblog_array['moblog_resize_width'] == 0 && $this->moblog_array['moblog_resize_height'] != 0)
  2209. {
  2210. // Resize based on height, calculate width
  2211. $resize_width = ceil(($this->moblog_array['moblog_resize_height']/$properties['height']) * $properties['width']);
  2212. }
  2213. elseif ($this->moblog_array['moblog_resize_width'] != 0 && $this->moblog_array['moblog_resize_height'] == 0)
  2214. {
  2215. // Resize based on width, calculate height
  2216. $resize_height = ceil(($this->moblog_array['moblog_resize_width']/$properties['width']) * $properties['height']);
  2217. }
  2218. $config = array(
  2219. 'resize_protocol' => $this->EE->config->item('image_resize_protocol'),
  2220. 'libpath' => $this->EE->config->item('image_library_path'),
  2221. 'source_image' => $this->upload_path.$filename,
  2222. 'quality' => '90',
  2223. 'width' => $resize_width,
  2224. 'height' => $resize_height
  2225. );
  2226. $this->EE->image_lib->initialize($config);
  2227. if ($this->EE->image_lib->resize() === FALSE)
  2228. {
  2229. $this->message_array[] = 'unable_to_resize';
  2230. $this->message_array = array_merge($this->message_array,$this->EE->image_lib->error_msg);
  2231. return FALSE;
  2232. }
  2233. $this->post_data['images'][$key]['width'] = $this->EE->image_lib->width;
  2234. $this->post_data['images'][$key]['height'] = $this->EE->image_lib->height;
  2235. if( ! $properties = $this->image_properties($this->upload_path.$filename))
  2236. {
  2237. $properties = array('width' => $this->EE->image_lib->width,
  2238. 'height' => $this->EE->image_lib->height);
  2239. }
  2240. }
  2241. }
  2242. /** ------------------------------
  2243. /** Create Thumbnail
  2244. /** ------------------------------*/
  2245. if ($this->moblog_array['moblog_create_thumbnail'] == 'y')
  2246. {
  2247. if ($this->moblog_array['moblog_thumbnail_width'] != 0 OR $this->moblog_array['moblog_thumbnail_height'] != 0)
  2248. {
  2249. // Temp vars
  2250. $resize_width = $this->moblog_array['moblog_thumbnail_width'];
  2251. $resize_height = $this->moblog_array['moblog_thumbnail_height'];
  2252. /** ----------------------------
  2253. /** Calculations based on one side?
  2254. /** ----------------------------*/
  2255. if ($this->moblog_array['moblog_thumbnail_width'] == 0 && $this->moblog_array['moblog_thumbnail_height'] != 0)
  2256. {
  2257. // Resize based on height, calculate width
  2258. $resize_width = ceil(($this->moblog_array['moblog_thumbnail_height']/$properties['height']) * $properties['width']);
  2259. }
  2260. elseif ($this->moblog_array['moblog_thumbnail_width'] != 0 && $this->moblog_array['moblog_thumbnail_height'] == 0)
  2261. {
  2262. // Resize based on width, calculate height
  2263. $resize_height = ceil(($this->moblog_array['moblog_thumbnail_width']/$properties['width']) * $properties['height']);
  2264. }
  2265. $this->EE->image_lib->clear();
  2266. $config = array(
  2267. 'resize_protocol' => $this->EE->config->item('image_resize_protocol'),
  2268. 'libpath' => $this->EE->config->item('image_library_path'),
  2269. 'source_image' => $this->upload_path.$filename,
  2270. 'thumb_prefix' => 'thumb',
  2271. 'quality' => '90',
  2272. 'width' => $resize_width,
  2273. 'height' => $resize_height
  2274. );
  2275. $this->EE->image_lib->initialize($config);
  2276. if ($this->EE->image_lib->resize() === FALSE)
  2277. {
  2278. $this->message_array[] = 'unable_to_resize';
  2279. $this->message_array = array_merge($this->message_array,$this->EE->image_lib->error_msg);
  2280. return FALSE;
  2281. }
  2282. $name = substr($filename, 0, strpos($filename, "."));
  2283. $ext = substr($filename, strpos($filename, "."));
  2284. $this->post_data['images'][$key]['thumbnail'] = $name.'_thumb'.$ext;
  2285. $this->post_data['images'][$key]['thumb_width'] = $resize_width;
  2286. $this->post_data['images'][$key]['thumb_height'] = $resize_height;
  2287. $this->email_files[] = $name.'_thumb'.$ext;
  2288. $this->uploads++;
  2289. } // End thumbnail resize conditional
  2290. } // End thumbnail
  2291. }
  2292. // ------------------------------------------------------------------------
  2293. /**
  2294. * Unique Filename
  2295. *
  2296. * @param string
  2297. * @param string
  2298. * @return string
  2299. */
  2300. function unique_filename($filename, $subtype='0')
  2301. {
  2302. $i = 0;
  2303. $subtype = ($subtype != '0') ? '.'.$subtype : '';
  2304. /** ----------------------------
  2305. /** Strips out _ and - at end of name part of file name
  2306. /** ----------------------------*/
  2307. $x = explode('.',$filename);
  2308. $name = ( ! isset($x['1'])) ? $filename : $x['0'];
  2309. $sfx = ( ! isset($x['1']) OR is_numeric($x[count($x) - 1])) ? $subtype : '.'.$x[count($x) - 1];
  2310. $name = (substr($name,-1) == '_' OR substr($name,-1) == '-') ? substr($name,0,-1) : $name;
  2311. $filename = $name.$sfx;
  2312. while (file_exists($this->upload_path.$filename))
  2313. {
  2314. $i++;
  2315. $n = ($i > 10) ? -2 : -1;
  2316. $x = explode('.',$filename);
  2317. $name = ( ! isset($x['1'])) ? $filename : $x['0'];
  2318. $sfx = ( ! isset($x['1'])) ? '' : '.'.$x[count($x) - 1];
  2319. $name = ($i==1) ? $name : substr($name,0,$n);
  2320. $name = (substr($name,-1) == '_' OR substr($name,-1) == '-') ? substr($name,0,-1) : $name;
  2321. $filename = $name."$i".$sfx;
  2322. }
  2323. return $filename;
  2324. }
  2325. }
  2326. // END CLASS
  2327. /* End of file mod.moblog.php */
  2328. /* Location: ./system/expressionengine/modules/moblog/mod.moblog.php */