PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/bx/plugins/newsletter.php

https://github.com/whiletrue/fluxcms
PHP | 370 lines | 251 code | 59 blank | 60 comment | 54 complexity | cf2faba3c16da391b9ba6f8bf44be434 MD5 | raw file
  1. <?php
  2. /**
  3. * User interface for the newsletter plugin
  4. */
  5. class bx_plugins_newsletter extends bx_plugin implements bxIplugin {
  6. static public $instance = array ();
  7. public static function getInstance ( $mode ) {
  8. if (! isset ( self::$instance [ $mode ])) {
  9. self::$instance[$mode] = new bx_plugins_newsletter ( $mode );
  10. }
  11. return self::$instance [ $mode ];
  12. }
  13. public function __construct ( $mode = "output") {
  14. $this -> mode = $mode ;
  15. }
  16. public function getPermissionList() {
  17. return array( "newsletter-back-feed",
  18. "newsletter-back-send",
  19. "newsletter-back-archive",
  20. "newsletter-back-manage",
  21. "admin_dbforms2-back-newsletter_feeds",
  22. "admin_dbforms2-back-newsletter_from",
  23. "admin_dbforms2-back-newsletter_groups",
  24. "admin_dbforms2-back-newsletter_users",
  25. "admin_dbforms2-back-newsletter_mailservers");
  26. }
  27. public function getEditorsById($path, $id) {
  28. return array("newsletter");
  29. }
  30. public function getMimeTypes() {
  31. return array("text/html");
  32. }
  33. public function isRealResource ( $path , $id) {
  34. return true ;
  35. }
  36. public function adminResourceExists($path, $id, $ext=null, $sample = false) {
  37. if($ext == 'xhtml') {
  38. return false;
  39. }
  40. return true;
  41. }
  42. /**
  43. * Newsletter subscription form
  44. */
  45. public function getContentById ($path , $id) {
  46. $xml ='<newsletter>';
  47. // enable to unsubscribe directly over the URL by appending ?unsubsribe={email}
  48. // this is needed in order to add unsubsribe-links to newsletter mails
  49. if(isset($_GET["unsubscribe"]) && !empty($_GET['really']))
  50. {
  51. if(!$this->removeSubscriber($_GET['unsubscribe'], ($_GET['groups']))) {
  52. $_POST["notfound"] = "true";
  53. }
  54. }
  55. // activate an account in case double-opt-in is enabled
  56. else if(isset($_GET["activate"]))
  57. {
  58. if($this->activateSubscriber($_GET['activate'])) {
  59. $xml .= '<status>SUB_ACT_OK</status>';
  60. }
  61. else {
  62. $xml .= '<status>SUB_ACT_ID_NOTFOUND</status>';
  63. }
  64. }
  65. if(isset($_POST["invemail"]))
  66. {
  67. $xml .= '<status>SUB_EMAIL_INVAL</status>';
  68. }
  69. else if(isset($_POST["notfound"]))
  70. {
  71. $xml .= '<status>SUB_NOT_FOUND</status>';
  72. }
  73. else if((isset($_GET["unsubscribe"]) && !empty($_GET['really'])) or isset($_POST["unsubscribe"]))
  74. {
  75. $xml .= '<status>SUB_UNSUB_SUCCESS</status>';
  76. $xml .= '<extended>SUB_CANCELED</extended>';
  77. }
  78. else if(isset($_GET["unsubscribe"]) or isset($_POST["unsubscribe"]))
  79. {
  80. $xml .= '<status>SUB_UNSUB_CONFIRM</status>';
  81. $xml .= '<extended link="./?unsubscribe='.urlencode($_GET['unsubscribe']).'&really=1&groups='.urlencode($_GET['groups']).'">SUB_UNSUB_CONFIRM_LINK</extended>';
  82. }
  83. else if(isset($_POST["duplicate"]))
  84. {
  85. $xml .= '<status>SUB_EMAIL_INUSE</status>';
  86. }
  87. else if(isset($_POST["subscribe"]))
  88. {
  89. $xml .= '<status>SUB_THANKS</status>';
  90. $xml .= '<extended>SUB_OK</extended>';
  91. }
  92. // pass through the list of public groups to the static.xsl
  93. foreach ($this->getGroups() as $row)
  94. {
  95. $xml .= '<group id="'.$row['id'].'">'.$row['name'].'</group>';
  96. }
  97. $xml .='</newsletter>';
  98. $dom = new DomDocument();
  99. if (!@$dom->loadXML($xml)) {
  100. //if it didn't work loading, try with replacing ampersand
  101. //FIXME: DIRTY HACK, works only in special cases..
  102. $xml = str_replace("&amp;","§amp;",$xml);
  103. $xml = preg_replace("#\&([^\#])#", "&#38;$1", $xml);
  104. $xml = str_replace("§amp;","&amp;",$xml);
  105. $dom->loadXML($xml);
  106. }
  107. return $dom;
  108. }
  109. /**
  110. * add and remove subscription events
  111. */
  112. public function handlePublicPost($path, $id, $data) {
  113. // write to db
  114. if(isset($data['subscribe'])){
  115. // validate email address
  116. if($this->checkEmailAddress($data['field_email']) == false) {
  117. $_POST["invemail"] = "true";
  118. return;
  119. }
  120. if($this->addSubscriber($data, $path) === false) {
  121. $_POST["duplicate"] = "true";
  122. }
  123. }
  124. else if(isset($data['unsubscribe'])){
  125. if($this->removeSubscriber($data['email'], $data['groups']) === false) {
  126. $_POST["notfound"] = "true";
  127. }
  128. }
  129. }
  130. /**
  131. * Add a new subscriber
  132. */
  133. protected function addSubscriber($data, $path) {
  134. $prefix = $GLOBALS['POOL']->config->getTablePrefix();
  135. // create a query with all input fields starting with 'field_'
  136. $queryFields = "";
  137. $queryValues = "";
  138. foreach($data as $key => $value)
  139. {
  140. if(strncmp($key, "field_", 6) == 0) {
  141. $queryFields .= substr($key, 6) . ",";
  142. $queryValues .= $GLOBALS['POOL']->dbwrite->quote($value."") . ",";
  143. $data[substr($key, 6)] = $value;
  144. }
  145. }
  146. // create a random activation id
  147. $activation = $activation = mt_rand(10000000,99999999);
  148. $data['activation'] = $activation;
  149. $status = 1;
  150. // check if the user wants to join a double-opt-in group
  151. $query = "select id from ".$prefix."newsletter_groups WHERE optin=1 AND public=1";
  152. $optinGroups = $GLOBALS['POOL']->db->queryCol($query);
  153. $doubleopt = false;
  154. if(count(array_intersect($optinGroups, $data['groups'])) > 0) {
  155. $doubleopt = true;
  156. $status = 2; // needs activation
  157. }
  158. // delete old entries with the same email address
  159. $email = $GLOBALS['POOL']->dbwrite->quote($data['field_email']);
  160. $query = "delete from ".$prefix."newsletter_users where email=".$email." AND status=3";
  161. $GLOBALS['POOL']->dbwrite->exec($query);
  162. // add to database
  163. $seq = $GLOBALS['POOL']->dbwrite->nextID($prefix.'_sequences');
  164. $query = "insert into ".$prefix."newsletter_users (ID, $queryFields activation,status,created) value($seq ,".$queryValues."'','',NOW())";
  165. if($GLOBALS['POOL']->dbwrite->exec($query) !== 1) {
  166. // could not insert user
  167. return false;
  168. }
  169. $userid = $this->getUserId($data['field_email']);
  170. // add to selected groups
  171. foreach($data['groups'] as $grp) {
  172. $seq = $GLOBALS['POOL']->dbwrite->nextID($prefix.'_sequences');
  173. $query = "insert into ".$prefix."newsletter_users2groups (ID, fk_user, fk_group) value('$seq', '$userid', '$grp')";
  174. $GLOBALS['POOL']->dbwrite->exec($query);
  175. }
  176. if($doubleopt == true) {
  177. // send user a mail with his activation id
  178. $newsmailer = bx_editors_newsmailer_newsmailer::newsMailerFactory($this->getParameter($path,"sendclass"));
  179. $newsmailer->sendActivationMail($data, $this->getParameter($path,"activation-server"),
  180. $this->getParameter($path,"activation-from"), $this->getParameter($path,"activation-subject"),
  181. $this->getParameter($path,"activation-text"), $this->getParameter($path,"activation-html"));
  182. }
  183. }
  184. /**
  185. * Remove a subscriber from all lists
  186. */
  187. protected function removeSubscriber($email,$groups = ""){
  188. if (!$groups) {
  189. return false;
  190. }
  191. $userid = $this->getUserId($email);
  192. $prefix = $GLOBALS['POOL']->config->getTablePrefix();
  193. $db = $GLOBALS['POOL']->dbwrite;
  194. //delete groups
  195. $query = "delete from ".$prefix."newsletter_users2groups where fk_user='".$userid."' and fk_group in ($groups);";
  196. $db->exec($query);
  197. //TODO cp newsletter_users2groups to newsletter_users2groups_unsub and enable this secion
  198. //insert into unsub table
  199. /*foreach (explode(",",$groups) as $group) {
  200. $query = "insert into ".$prefix."newsletter_users2groups_unsub (fk_user,fk_group) VALUES('".$userid."',".$group.")";
  201. $db->exec($query);
  202. */
  203. // check if there are more groups
  204. $res = $db->query("select id from ".$prefix."newsletter_users2groups where fk_user = ".$userid);
  205. if ($res->numRows() == 0) {
  206. // set status to deactivated
  207. $query = "UPDATE ".$prefix."newsletter_users SET status='3' WHERE id='".$userid."'";
  208. if($db->exec($query) !== 1) {
  209. // could not deactivate user
  210. return false;
  211. }
  212. }
  213. return true;
  214. }
  215. /**
  216. * Activate the subscriber with the given activation-id
  217. */
  218. protected function activateSubscriber($id)
  219. {
  220. if($id < 10000000 or $id > 99999999)
  221. return false;
  222. $id = $GLOBALS['POOL']->dbwrite->quote($id);
  223. $prefix = $GLOBALS['POOL']->config->getTablePrefix();
  224. $query = "UPDATE ".$prefix."newsletter_users SET status='1' WHERE activation = ".$id;
  225. if($GLOBALS['POOL']->dbwrite->exec($query) !== 1) {
  226. // could not find user
  227. return false;
  228. }
  229. return true;
  230. }
  231. /**
  232. * Returns an associated array of newsletter groups
  233. */
  234. protected function getGroups()
  235. {
  236. $prefix = $GLOBALS['POOL']->config->getTablePrefix();
  237. $query = "select * from ".$prefix."newsletter_groups WHERE public=1";
  238. $res = $GLOBALS['POOL']->db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  239. return $res;
  240. }
  241. /**
  242. * Retrieves the primary key for a user from his unique email address
  243. */
  244. protected function getUserId($email)
  245. {
  246. $email = $GLOBALS['POOL']->dbwrite->quote($email);
  247. $prefix = $GLOBALS['POOL']->config->getTablePrefix();
  248. $query = "select id from ".$prefix."newsletter_users where email=".$email;
  249. return $GLOBALS['POOL']->db->queryOne($query);
  250. }
  251. /**
  252. * Admin view collection interface
  253. */
  254. public function getOverviewSections($path) {
  255. $perm = bx_permm::getInstance();
  256. $sections = array();
  257. $dom = new bx_domdocs_overview();
  258. $dom->setTitle("Newsletter", "Newsletters");
  259. $dom->setPath($path);
  260. $dom->setType("newsletter");
  261. $dom->setIcon("gallery");
  262. // first tab
  263. if ($perm->isAllowed($path.'drafts/',array('xhtml-back-create'))) {
  264. $dom->addLink("Create Newsletter",'addresource'.$path.'drafts/?type=xhtml');
  265. }
  266. if ($perm->isAllowed($path,array('newsletter-back-send'))) {
  267. $dom->addLink("Send Newsletter",'edit'.$path.'send/');
  268. }
  269. if ($perm->isAllowed($path,array('newsletter-back-archive'))) {
  270. $dom->addLink("Newsletter Archive",'edit'.$path.'manage/');
  271. }
  272. if ($perm->isAllowed($path,array('newsletter-back-feed'))) {
  273. $dom->addLink("Generate from Feed",'edit'.$path.'feed/');
  274. }
  275. // second tab
  276. $dom->addTab("Management");
  277. if ($perm->isAllowed($path,array('newsletter-back-manage'))) {
  278. $dom->addLink("User Management",'edit'.$path.'users/');
  279. }
  280. if ($perm->isAllowed('/dbforms2/',array('admin_dbforms2-back-newsletter_users'))) {
  281. $dom->addLink("Edit Users",'dbforms2/newsletter_users/');
  282. }
  283. if ($perm->isAllowed('/dbforms2/',array('admin_dbforms2-back-newsletter_groups'))) {
  284. $dom->addLink("Edit Groups",'dbforms2/newsletter_groups/');
  285. }
  286. if ($perm->isAllowed('/dbforms2/',array('admin_dbforms2-back-newsletter_from'))) {
  287. $dom->addLink("Edit Senders",'dbforms2/newsletter_from/');
  288. }
  289. if ($perm->isAllowed('/dbforms2/',array('admin_dbforms2-back-newsletter_mailservers'))) {
  290. $dom->addLink("Edit Mail Servers",'dbforms2/newsletter_mailservers/');
  291. }
  292. if ($perm->isAllowed('/dbforms2/',array('admin_dbforms2-back-newsletter_feeds'))) {
  293. $dom->addLink("Edit RSS Feeds",'dbforms2/newsletter_feeds/');
  294. }
  295. return $dom;
  296. }
  297. /**
  298. * Returns true if the supplied email address is valid and points to an existing DNS record
  299. */
  300. protected function checkEmailAddress($email) {
  301. if(eregi(".+@.+\..+.", $email)) {
  302. // doesn't work on windows
  303. list($userName, $mailDomain) = split("@", $email);
  304. if(checkdnsrr($mailDomain, "MX")) {
  305. return true;
  306. }
  307. }
  308. return false;
  309. }
  310. }
  311. ?>