PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Quản lý Web php và xây dựng chương trình Newsletter PHP/Source/mailinglist/mlm_fns.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 580 lines | 457 code | 94 blank | 29 comment | 80 complexity | b97ed8f7cdd519b6ee3a1b1b8c26ef22 MD5 | raw file
  1. <?php
  2. function subscriber_exists($email)
  3. {
  4. if(!$email)
  5. return false;
  6. if(!db_connect())
  7. return false;
  8. $query = "select count(*) from subscribers where email = '$email'";
  9. $result = mysql_query($query);
  10. if(!$result)
  11. return false;
  12. return (mysql_result($result, 0, 0)>0);
  13. }
  14. // kiem tra dia chi email da dang ky thu tin trong CSDL
  15. function subscribed($email, $listid)
  16. {
  17. if(!$email||!$listid)
  18. return false;
  19. if(!db_connect())
  20. return false;
  21. $query = "select count(*) from sub_lists where email = '$email'
  22. and listid = $listid";
  23. $result = mysql_query($query);
  24. if(!$result)
  25. return false;
  26. return (mysql_result($result, 0, 0)>0);
  27. }
  28. // kiem tra su ton tai cua danh sach?
  29. function list_exists($listid)
  30. {
  31. if(!$listid)
  32. return false;
  33. if(!db_connect())
  34. return false;
  35. $query = "select count(*) from lists where listid = '$listid'";
  36. $result = mysql_query($query);
  37. if(!$result)
  38. return false;
  39. return (mysql_result($result, 0, 0)>0);
  40. }
  41. // ham nhan ten user cua dia chi email
  42. function get_real_name($email)
  43. {
  44. if(!$email)
  45. return false;
  46. if(!db_connect())
  47. return false;
  48. $query = "select realname from subscribers where email = '$email'";
  49. $result = mysql_query($query);
  50. if(!$result)
  51. return false;
  52. return trim(mysql_result($result, 0, 0));
  53. }
  54. // nhan kieu thu tin (HTML hoac Text) ma user da dang ky
  55. function get_mimetype($email)
  56. {
  57. if(!$email)
  58. return false;
  59. if(!db_connect())
  60. return false;
  61. $query = "select mimetype from subscribers where email = '$email'";
  62. $result = mysql_query($query);
  63. if(!$result)
  64. return false;
  65. return trim(mysql_result($result, 0, 0));
  66. }
  67. // dang ky dia chi email den danh sach trong CSDL
  68. function subscribe($email, $listid)
  69. {
  70. if(!$email||!$listid||!list_exists($listid)||!subscriber_exists($email))
  71. return false;
  72. //neu mail dang ky da ton tai tren danh sach
  73. if(subscribed($email, $listid))
  74. return false;
  75. if(!db_connect())
  76. return false;
  77. $query = "insert into sub_lists values ('$email', $listid)";
  78. $result = mysql_query($query);
  79. return $result;
  80. }
  81. // huy bo dang ky doi voi dia chi email tren mot danh sach
  82. function unsubscribe($email, $listid)
  83. {
  84. if(!$email||!$listid)
  85. return false;
  86. if(!db_connect())
  87. return false;
  88. $query = "delete from sub_lists where email = '$email' and listid = $listid";
  89. $result = mysql_query($query);
  90. return $result;
  91. }
  92. // nap thong tin cua thu tin tu CSDL
  93. function load_mail_info($mailid)
  94. {
  95. if(!$mailid)
  96. return false;
  97. if(!db_connect())
  98. return false;
  99. $query = "select subject, listid, status, sent from mail
  100. where mailid = $mailid";
  101. $result = mysql_query($query);
  102. if(!$result)
  103. {
  104. echo "Khong the tim lai mail nay $query";
  105. return false;
  106. }
  107. return mysql_fetch_array($result);
  108. }
  109. // nap thong tin cua danh sach tu CSDL
  110. function load_list_info($listid)
  111. {
  112. if(!$listid)
  113. return false;
  114. if(!db_connect())
  115. return false;
  116. $query = "select listname, blurb from lists where listid = $listid";
  117. $result = mysql_query($query);
  118. if(!$result)
  119. {
  120. echo 'Khong the lay lai danh sach nay';
  121. return false;
  122. }
  123. $info = mysql_fetch_array($result);
  124. $query = "select count(*) from sub_lists where listid = $listid";
  125. $result = mysql_query($query);
  126. if($result)
  127. {
  128. $info['subscribers'] = mysql_result($result, 0, 0);
  129. }
  130. $query = "select count(*) from mail where listid = $listid
  131. and status = 'SENT'";
  132. $result = mysql_query($query);
  133. if($result)
  134. {
  135. $info['archive'] = mysql_result($result, 0, 0);
  136. }
  137. return $info;
  138. }
  139. // ham nhan tin cua danh sach tro den listid
  140. function get_list_name($listid)
  141. {
  142. if(!$listid)
  143. return false;
  144. if(!db_connect())
  145. return false;
  146. $query = "select listname from lists where listid = $listid";
  147. $result = mysql_query($query);
  148. if(!$result)
  149. {
  150. return false;
  151. }
  152. return mysql_result($result, 0);
  153. }
  154. // them mot danh sach moi vao CSDL
  155. function store_list($admin_user, $details)
  156. {
  157. if(!filled_out($details))
  158. {
  159. echo 'Tat ca cac truong phai duoc dien day du. Thu lai lan nua.<br /><br />';
  160. return false;
  161. }
  162. else
  163. {
  164. if(!check_admin_user($admin_user))
  165. return false;
  166. if(!db_connect())
  167. {
  168. return false;
  169. }
  170. $query = "select count(*) from lists where listname = '".$details['name']."'";
  171. $result = mysql_query($query);
  172. if(mysql_result($result, 0, 0) > 0)
  173. {
  174. echo 'Loi, ten danh sach nay da ton tai.';
  175. return false;
  176. }
  177. $query = "insert into lists values (NULL,
  178. '".$details['name']."',
  179. '".$details['blurb']."')";
  180. $result = mysql_query($query);
  181. return $result;
  182. }
  183. }
  184. // ham nhan danh sach ma user dang ky
  185. function get_subscribed_lists($email)
  186. {
  187. $list = array();
  188. $query = "select lists.listid, listname from sub_lists, lists
  189. where email='$email' and lists.listid = sub_lists.listid
  190. order by listname";
  191. if(db_connect())
  192. {
  193. $result = mysql_query($query);
  194. if(!$result)
  195. echo '<p>Khong the lay danh sach tu CSDL.';
  196. $num = mysql_numrows($result);
  197. for($i = 0; $i<$num; $i++)
  198. {
  199. array_push($list, array(mysql_result($result, $i, 0),
  200. mysql_result($result, $i, 1)));
  201. }
  202. }
  203. return $list;
  204. }
  205. //nhan danh sach ma user huy bo
  206. function get_unsubscribed_lists($email)
  207. {
  208. $list = array();
  209. $query = "select lists.listid, listname, email from lists left join sub_lists
  210. on lists.listid = sub_lists.listid
  211. and email='$email' where email is NULL order by listname";
  212. if(db_connect())
  213. {
  214. $result = mysql_query($query);
  215. if(!$result)
  216. echo '<p>Khong the lay danh sach tu CSDL.';
  217. $num = mysql_numrows($result);
  218. for($i = 0; $i<$num; $i++)
  219. {
  220. array_push($list, array(mysql_result($result, $i, 0),
  221. mysql_result($result, $i, 1)));
  222. }
  223. }
  224. return $list;
  225. }
  226. // nhan tat ca danh sach
  227. function get_all_lists()
  228. {
  229. $list = array();
  230. $query = 'select listid, listname from lists order by listname';
  231. if(db_connect())
  232. {
  233. $result = mysql_query($query);
  234. if(!$result)
  235. echo "<p>Khong the lay danh sach tu CSDL - $query.";
  236. $num = mysql_numrows($result);
  237. for($i = 0; $i<$num; $i++)
  238. {
  239. array_push($list, array(mysql_result($result, $i, 0),
  240. mysql_result($result, $i, 1)));
  241. }
  242. }
  243. return $list;
  244. }
  245. function get_archive($listid)
  246. {
  247. //tra ve mot day cac tin luu tru cho danh sach chon
  248. $list = array();
  249. $listname = get_list_name($listid);
  250. $query = "select mailid, subject, listid from mail
  251. where listid = $listid and status = 'SENT' order by sent";
  252. if(db_connect())
  253. {
  254. $result = mysql_query($query);
  255. if(!$result)
  256. {
  257. echo "<p>Khong the lay danh sach tu CSDL - $query.";
  258. return false;
  259. }
  260. $num = mysql_numrows($result);
  261. for($i = 0; $i<$num; $i++)
  262. {
  263. $row = array(mysql_result($result, $i, 0),
  264. mysql_result($result, $i, 1), $listname, $listid);
  265. array_push($list, $row);
  266. }
  267. }
  268. return $list;
  269. }
  270. // nhan danh sach cua mail duoc tao nhung chua gui
  271. function get_unsent_mail($email)
  272. {
  273. if(!check_admin_user($email))
  274. {
  275. return false;
  276. }
  277. $list = array();
  278. $query = "select mailid, subject, listid from mail
  279. where status = 'STORED' or status = 'TESTED' order by modified";
  280. if(db_connect())
  281. {
  282. $result = mysql_query($query);
  283. if(!$result)
  284. {
  285. echo '<p>Khong the lay danh sach tu CSDL.';
  286. return false;
  287. }
  288. $num = mysql_numrows($result);
  289. for($i = 0; $i<$num; $i++)
  290. {
  291. array_push($list, array(mysql_result($result, $i, 0),
  292. mysql_result($result, $i, 1),
  293. get_list_name(mysql_result($result, $i, 2)),
  294. mysql_result($result, $i, 2)
  295. )
  296. );
  297. }
  298. }
  299. return $list;
  300. }
  301. // them mot user dang ky moi vao CSDL hoac user thay doi thong tin tai khoan
  302. function store_account($normal_user, $admin_user, $details)
  303. {
  304. if(!filled_out($details))
  305. {
  306. echo 'Tat ca cac truong phai duoc dien day du. Thu lai lan nua.<br /><br />';
  307. return false;
  308. }
  309. else
  310. {
  311. if(subscriber_exists($details['email']))
  312. {
  313. if(get_email()==$details['email'])
  314. {
  315. $query = "update subscribers set realname = '$details[realname]',
  316. mimetype = '$details[mimetype]'
  317. where email = '" . $details[email] . "'";
  318. if(db_connect() && mysql_query($query))
  319. {
  320. return true;
  321. }
  322. else
  323. {
  324. echo 'Khong the luu cac thay doi.<br /><br /><br /><br /><br /><br />';
  325. return false;
  326. }
  327. }
  328. else
  329. {
  330. echo '<p>Loi, dia chi email nay da duoc dang ky.';
  331. echo '<p>Ban can dang nhap voi dia chi email nay de thay doi cac thong tin.';
  332. return false;
  333. }
  334. }
  335. else // tai khoan moi
  336. {
  337. $query = "insert into subscribers
  338. values ('$details[email]',
  339. '$details[realname]',
  340. '$details[mimetype]',
  341. password('$details[new_password]'),
  342. 0)";
  343. if(db_connect() && mysql_query($query))
  344. {
  345. return true;
  346. }
  347. else
  348. {
  349. echo 'Khong the luu lai tai khoan moi.<br /><br /><br /><br /><br /><br />';
  350. return false;
  351. }
  352. }
  353. }
  354. }
  355. //gui thu tin
  356. function send($mailid, $admin_user)
  357. {
  358. if(!check_admin_user($admin_user))
  359. return false;
  360. if(!($info = load_mail_info($mailid)))
  361. {
  362. echo "Khong the lay thong tin cho message $mailid";
  363. return false;
  364. }
  365. $subject = $info[0];
  366. $listid = $info[1];
  367. $status = $info[2];
  368. $sent = $info[3];
  369. $from_name = 'Newsletter';
  370. $from_address = 'return@address';
  371. $query = "select email from sub_lists where listid = $listid";
  372. $result = mysql_query($query);
  373. if (!$result)
  374. {
  375. echo $query;
  376. return false;
  377. }
  378. else if (mysql_num_rows($result)==0)
  379. {
  380. echo "Hien tai chua co dang ky nao cho danh sach nay $listid";
  381. return false;
  382. }
  383. else
  384. {
  385. // trieu goi lop mail trong thu vien PEAR
  386. include('Mail.php');
  387. include('Mail/mime.php');
  388. $message = new Mail_mime("\r\n");
  389. $textfilename = "archive/$listid/$mailid/text.txt";
  390. $tfp = fopen($textfilename, "r");
  391. $text = fread($tfp, filesize($textfilename));
  392. fclose($tfp);
  393. $htmlfilename = "archive/$listid/$mailid/index.html";
  394. $hfp = fopen($htmlfilename, "r");
  395. $html = fread($hfp, filesize($htmlfilename));
  396. fclose($hfp);
  397. $message->setTXTBody($text);
  398. $message->setHTMLBody($html);
  399. // nhan cac hinh anh lien quan cua message
  400. $query = "select path, mimetype from images where mailid = $mailid";
  401. if(db_connect())
  402. {
  403. $result = mysql_query($query);
  404. if(!$result)
  405. {
  406. echo '<p>Khong the lay hinh tu CSDL.';
  407. return false;
  408. }
  409. $num = mysql_numrows($result);
  410. for($i = 0; $i<$num; $i++)
  411. {
  412. //nap tung hinh mot tu dia
  413. $imgfilename = "archive/$listid/$mailid/".mysql_result($result, $i, 0);
  414. $imgtype = mysql_result($result, $i, 1);
  415. // them tung hinh mot vao trong doi tuong
  416. $message->addHTMLImage($imgfilename, $imgtype, $imgfilename, true);
  417. }
  418. }
  419. // tao body cua message
  420. $body = $message->get();
  421. // tao header cua message
  422. $from = '"'.get_real_name($admin_user).'" <'.$admin_user.'>';
  423. $hdrarray = array(
  424. 'From' => $from,
  425. 'Subject' => $subject);
  426. $hdrs = $message->headers($hdrarray);
  427. $sender =& Mail::factory('mail');
  428. if($status == 'STORED')
  429. {
  430. // gui message HTML den admin
  431. $sender->send($admin_user, $hdrs, $body);
  432. mail($admin_user, $subject, $text, 'From: "'.get_real_name($admin_user).'" <'.$admin_user.">");
  433. echo "Tin duoc gui den $admin_user";
  434. $query = "update mail set status = 'TESTED' where mailid = $mailid";
  435. if(db_connect())
  436. {
  437. $result = mysql_query($query);
  438. }
  439. echo '<p>Nhan send lan nua de gui tin den cac user da dang ky.<center>';
  440. display_button('send', "&id=$mailid");
  441. echo '</center>';
  442. }
  443. else if($status == 'TESTED')
  444. {
  445. //gui message den danh sach dang ky
  446. $query = "select subscribers.realname, sub_lists.email,
  447. subscribers.mimetype
  448. from sub_lists, subscribers
  449. where listid = $listid and
  450. sub_lists.email = subscribers.email";
  451. if(!db_connect())
  452. return false;
  453. $result = mysql_query($query);
  454. if(!$result)
  455. echo '<p>Loi nhan danh sach user dang ky';
  456. $count = 0;
  457. // cho moi user dang ky
  458. while( $subscriber = mysql_fetch_row($result) )
  459. {
  460. if($subscriber[2]=='H')
  461. {
  462. //gui message HTML den user dang ky HTML
  463. $sender->send($subscriber[1], $hdrs, $body);
  464. }
  465. else
  466. {
  467. //gui message text den user khong dang ky HTML
  468. mail($subscriber[1], $subject, $text,
  469. 'From: "'.get_real_name($admin_user).'" <'.$admin_user.">");
  470. }
  471. $count++;
  472. }
  473. $query = "update mail set status = 'SENT', sent = now()
  474. where mailid = $mailid";
  475. if(db_connect())
  476. {
  477. $result = mysql_query($query);
  478. }
  479. echo "<p>Tong cong $count tin da duoc gui.";
  480. }
  481. else if($status == 'SENT')
  482. {
  483. echo '<p>Tin nay da duoc gui.';
  484. }
  485. }
  486. }