WYSIJA_help_user

The MailPoet Newsletters WYSIJA help user class.

Defined (1)

The class is defined in the following location(s).

/helpers/user.php  
  1. class WYSIJA_help_user extends WYSIJA_object { 
  2.  
  3. var $no_confirmation_email = false; 
  4.  
  5. /** 
  6. * Used whend confirming email with dbl optin 
  7. * @param type $user_id 
  8. * @param type $status 
  9. * @param type $auto 
  10. */ 
  11. function subscribe($user_id, $status = true, $auto = false, $listids = array()) { 
  12. $time = time(); 
  13. $cols = false; 
  14. // get the enabled lists 
  15. $model_list = WYSIJA::get('list', 'model'); 
  16. $listsdata = $model_list->get(array('list_id'), array('is_enabled' => '1')); 
  17. $enabled_list_ids = array(); 
  18. foreach ($listsdata as $listdt) { 
  19. $enabled_list_ids[] = $listdt['list_id']; 
  20.  
  21. // get list_id from user 
  22. $model_user_list = WYSIJA::get('user_list', 'model'); 
  23. $listidsfromuser = $model_user_list->get(array('list_id'), array('user_id' => $user_id, 'list_id' => $enabled_list_ids)); 
  24.  
  25. $listidsenableduser = array(); 
  26. foreach ($listidsfromuser as $listdt) { 
  27. $listidsenableduser[] = $listdt['list_id']; 
  28.  
  29. if ($status) { 
  30. $status = 1; 
  31.  
  32. // if undo unsubscribe then we re-subscribe all the previously unsubscribed lists based on the unsub_date value 
  33. if( !empty( $_REQUEST['action'] ) && $_REQUEST['action'] == 'undounsubscribe' ) { 
  34. // get latest time when users unsubcribe from a list 
  35. $model_user_list->columns['MAX(`unsub_date`) as `max_unsub_date`']=array("type"=>"integer"); 
  36. $max_unsub_date = $model_user_list->get(array('MAX(`unsub_date`) as `max_unsub_date`'), array('user_id' => $user_id)); 
  37. $max_unsub_date = $max_unsub_date[0]['max_unsub_date']; 
  38. // when somebody undo - unsubscribe, let's reset the unsub_date of all of the latest lists which users belong to 
  39. $model_user_list->update(array('unsub_date' => 0, 'sub_date' => time()), array('user_id' => $user_id, 'unsub_date' => $max_unsub_date)); 
  40.  
  41.  
  42. // the data we update on the user row 
  43. $user_updated_data = array( 'status' => $status , 'confirmed_ip' => $this->getIP() , 'confirmed_at' => time() ); 
  44. } else { 
  45. // when we unsubscribe somebody automatically(through the bounce) we set the status to -2 instead of -1 to make the difference 
  46. $status = -1; 
  47. $modelU = WYSIJA::get('queue', 'model'); 
  48. $modelU->delete(array('user_id' => $user_id)); 
  49.  
  50. // when somebody unsubscribe, let's set the unsub_date and reset the sub_date of all the lists which users belong to 
  51. $model_user_list->update(array('unsub_date' => time(), 'sub_date' => 0), array('user_id' => $user_id, 'unsub_date' => 0)); 
  52.  
  53. // the data we update on the user row 
  54. $user_updated_data = array('status' => $status); 
  55.  
  56. $modelUser = WYSIJA::get('user', 'model'); 
  57. $modelUser->update($user_updated_data, array('user_id' => $user_id)); 
  58.  
  59. // if the status is subscribed then we modify the user_list status and process the autonewsletter 
  60. if ($status) { 
  61. // update the sub_date col in the user_list table 
  62. if (!$auto) { 
  63. $model_user_list = WYSIJA::get('user_list', 'model'); 
  64. $cols = array('sub_date' => $time, 'unsub_date' => 0); 
  65. $model_user_list->update($cols, array('user_id' => $user_id, 'list_id' => $listids)); 
  66.  
  67. // process the auto newsletter once the status changed 
  68. $lists = $this->getUserLists($user_id, $listids); 
  69. $this->sendAutoNl($user_id, $lists); 
  70.  
  71. return $listidsenableduser; 
  72.  
  73. /** 
  74. * check that a bot didn't stick his hand in our honey pot 
  75. * @param type $data 
  76. * @return boolean 
  77. */ 
  78. function checkData(&$data) { 
  79. if (isset($data['user']['abs'])) { 
  80. foreach ($data['user']['abs'] as $honeyKey => $honeyVal) { 
  81. //if honey val then robotty is out ! 
  82. if ($honeyVal) 
  83. return false; 
  84. unset($data['user']['abs']); 
  85. $user_keys = array('email', 'firstname', 'lastname'); 
  86. foreach($data['user'] as $keyi => $val) { 
  87. if(!in_array($keyi, $user_keys)) { 
  88. unset($data['user'][$keyi]); 
  89.  
  90.  
  91. return true; 
  92.  
  93. /** 
  94. * function to insert subscribers into wysija 
  95. * data parameter is a multidimensional array 
  96. * @param array $data=array( 
  97. * 'user'=>array('email'=>$myuserEMAIL, 'firstname'=>$myuserFIRSTNAME),  
  98. * 'user_lists'=>array('list_ids'=>array($listid1, $listid2)) 
  99. * ); 
  100. * @param boolean $subscribing_from_backend 
  101. * @return type 
  102. */ 
  103. function addSubscriber( $data , $subscribing_from_backend = false) { 
  104.  
  105. $has_error = false; 
  106. // 0 - action before any processing call third party services WangGuard for instance 
  107. if ( !$subscribing_from_backend ) { 
  108. $valid_email = apply_filters('wysija_beforeAddSubscriber', true, $data['user']['email']); 
  109. if ( !$valid_email ) { 
  110. $this->error( __( 'The email is not valid!' , WYSIJA ), true ); 
  111. $has_error = true; 
  112.  
  113. // 1-check if email is valid 
  114. if ( !$this->validEmail( $data['user']['email'] ) ) { 
  115. $this->error( __( 'The email is not valid!' , WYSIJA ), true ); 
  116. $has_error = true; 
  117.  
  118. // check if lists are specified? 
  119. if ( empty( $data['user_list']['list_ids'] ) ) { 
  120. $this->error( __( 'You need to select at least one list.' , WYSIJA ) , true); 
  121. $has_error = true; 
  122.  
  123. // make sure we get all the errors in one shot 
  124. if ($has_error === true) { 
  125. return false; 
  126.  
  127. // 2-check if email doesn't exists already 
  128. $model_user = WYSIJA::get('user', 'model'); 
  129. $subscriber_exists_already = $model_user->getOne(false, array('email' => trim($data['user']['email']))); 
  130.  
  131. $model_config = WYSIJA::get('config', 'model'); 
  132. $confirm_dbloptin = $model_config->getValue('confirm_dbleoptin'); 
  133.  
  134. // message success escaping, striping, setting 
  135. $message_success = ''; 
  136. if ( isset( $data['message_success'] ) ) { 
  137. $message_success = strip_tags( $data['message_success'] , '<p><em><span><b><strong><i><h1><h2><h3><a><ul><ol><li><br>'); 
  138. } else if ( isset( $data['success_message'] ) ) { 
  139. $message_success = strip_tags( nl2br(base64_decode( $data['success_message'] ) ) , '<p><em><span><b><strong><i><h1><h2><h3><a><ul><ol><li><br>'); 
  140. } else if ( isset( $data['form_id'] ) ) { 
  141. // we have a form_id parameter so let's fetch the form success message 
  142. $model_forms = WYSIJA::get('forms', 'model'); 
  143. $form = $model_forms->getOne( array( 'data' ) , array( 'form_id' => (int) $data['form_id'] ) ); 
  144. $form_data = unserialize( base64_decode( $form['data'] ) ); 
  145.  
  146. // if the on_success action is 'message', display message 
  147. if ( $form_data['settings']['on_success'] === 'message') { 
  148. $message_success = nl2br( $form_data['settings']['success_message'] ); 
  149.  
  150. if ( $subscriber_exists_already ) { 
  151. // show a message for that case scenario in the admin panel 
  152. if ( $subscribing_from_backend ) { 
  153. $this->error(str_replace(array('[link]', '[/link]'), array('<a href="admin.php?page=wysija_subscribers&action=edit&id=' . $subscriber_exists_already['user_id'] . '" >', "</a>"), __('Subscriber already exists. [link]Click to edit[/link].', WYSIJA)), true); 
  154. return false; 
  155.  
  156.  
  157. $model_user_list = WYSIJA::get( 'user_list' , 'model' ); 
  158. $subscribed_but_require_confirmation = false; 
  159. if( $subscriber_exists_already['status'] == 1 ) { 
  160. $unsubscribed_lists = $model_user_list->get( array( 'list_id' ) , array( 'greater' => array( 'unsub_date' => 0 ), 'equal' => array( 'user_id' => $subscriber_exists_already['user_id'] ) ) ); 
  161. $already_unsubscribed_list_ids_formatted = array(); 
  162.  
  163. foreach ($unsubscribed_lists as $user_list_detail) { 
  164. $already_unsubscribed_list_ids_formatted[] = $user_list_detail['list_id']; 
  165.  
  166. foreach ($data['user_list']['list_ids'] as $list_id) { 
  167. if ( in_array( $list_id, $already_unsubscribed_list_ids_formatted ) ) { 
  168. $subscribed_but_require_confirmation = true; 
  169.  
  170.  
  171. // if the status of the user is either unsubscribed or not confirmed 
  172. // or he is unsubscribed from one of the list he is trying to subscribe again 
  173. // we resend him the activation email 
  174. if ( (int) $subscriber_exists_already['status'] < 1 || $subscribed_but_require_confirmation ) { 
  175. $model_user->reset(); 
  176. $model_user->update( array( 'status' => 0 ), array( 'user_id' => $subscriber_exists_already['user_id'] ) ); 
  177.  
  178. $subscribe_to_list = 0; 
  179. if ( !$confirm_dbloptin ) { 
  180. $subscribe_to_list = time(); 
  181.  
  182. $this->addToLists( $data['user_list']['list_ids'], $subscriber_exists_already['user_id'], $subscribe_to_list); 
  183.  
  184. // this is the double optin case, where we simply send the signup confirmation 
  185. if ( $confirm_dbloptin ) { 
  186. $this->sendConfirmationEmail( (object) $subscriber_exists_already, true, $data['user_list']['list_ids']); 
  187. } else { 
  188. // this is the single optin case, where we fire the autoresponders directly 
  189. $lists = $this->getUserLists( $subscriber_exists_already['user_id'], $data['user_list']['list_ids'] ); 
  190. $this->sendAutoNl( $subscriber_exists_already['user_id'], $lists ); 
  191.  
  192. if ( $model_config->getValue( 'emails_notified' ) && $model_config->getValue( 'emails_notified_when_sub' ) ) { 
  193. // notify the administrators of a new subscribption 
  194. $this->uid = $subscriber_exists_already['user_id']; 
  195. if ( !$subscribing_from_backend ) 
  196. $this->_notify( $data['user']['email'], true, $data['user_list']['list_ids'] ); 
  197.  
  198.  
  199. if ( !empty( $message_success ) ) { 
  200. $this->notice( $message_success ); 
  201.  
  202. return true; 
  203.  
  204. $model_user_list = WYSIJA::get('user_list', 'model'); 
  205. $already_subscribed_list_ids = $model_user_list->get(array('list_id'), array('greater' => array('sub_date' => 0), 'equal' => array( 'user_id' => $subscriber_exists_already['user_id'] ) ) ); 
  206. $already_subscribed_list_ids_formatted = array(); 
  207.  
  208. foreach ($already_subscribed_list_ids as $user_list_detail) { 
  209. $already_subscribed_list_ids_formatted[] = $user_list_detail['list_id']; 
  210.  
  211. // a confirmation needs to be resend for those lists 
  212. $list_ids_require_confirmation = array(); 
  213. foreach ( $data['user_list']['list_ids'] as $list_id ) { 
  214. // only the list for which the subscribe request is made and is not already subscribed too willr equire confirmation 
  215. if ( !in_array($list_id, $already_subscribed_list_ids_formatted ) ) { 
  216. $list_ids_require_confirmation[] = $list_id; 
  217.  
  218. // this process require either a confirmation email to be sent or 
  219. // the autoresponders to be triggerred 
  220. if ( !empty( $list_ids_require_confirmation ) ) { 
  221. $subscribe_to_list = $subscriber_status = 0; 
  222.  
  223. if ( isset( $data['user']['status'] ) ) { 
  224. $subscriber_status = $data['user']['status']; 
  225.  
  226. // if double optin is activated and the subscriber status is 1 (subscribed) 
  227. // or this is single optin, then we directly subscribe the user to the list 
  228. if ( ( $confirm_dbloptin && $subscriber_status ) || !$confirm_dbloptin ) { 
  229. $subscribe_to_list = time(); 
  230.  
  231. // we can add the subscribers to the lists passed 
  232. $this->addToLists( $data['user_list']['list_ids'] , $subscriber_exists_already['user_id'] , $subscribe_to_list ); 
  233.  
  234. // send a confirmation message when double optin is on 
  235. if ( $confirm_dbloptin ) { 
  236. //if we have lists that are going to be added we send a confirmation email for double optin 
  237. $this->sendConfirmationEmail( (object) $subscriber_exists_already, true, $list_ids_require_confirmation ); 
  238. }else{ 
  239. // send auto nl to single optin which have lists added 
  240. if ( !empty( $list_ids_require_confirmation ) ) { 
  241. $lists = $this->getUserLists( $subscriber_exists_already['user_id'] , $data['user_list']['list_ids'] ); 
  242. $this->sendAutoNl( $subscriber_exists_already['user_id'] , $lists ); 
  243. if ( !empty( $message_success ) ) { 
  244. $this->notice( $message_success ); 
  245.  
  246. } else { 
  247. //no lists need to be added so we can simply return the message 
  248. $this->notice(__("Oops! You're already subscribed.", WYSIJA)); 
  249. return true; 
  250.  
  251. return true; 
  252.  
  253. // 3-insert the subscriber with the right status based on optin status 
  254.  
  255. $subscriber_data = $data['user']; 
  256. $subscriber_data['ip'] = $this->getIP(); 
  257.  
  258. $model_user->reset(); 
  259. $user_id = $model_user->insert($subscriber_data); 
  260.  
  261.  
  262. if ( (int)$user_id > 0 ) { 
  263. // if a form id is specified, let's increment its "subscribed count" 
  264. if (isset($data['form_id']) && (int) $data['form_id'] > 0) { 
  265. // check if the form exists 
  266. $model_forms = WYSIJA::get('forms', 'model'); 
  267. $form = $model_forms->getOne( array('form_id', 'subscribed') , array('form_id' => (int) $data['form_id']) ); 
  268. if ( isset( $form['form_id'] ) && (int) $form['form_id'] === (int) $data['form_id'] ) { 
  269. // the form exists so let's increment the "subscribed" count 
  270. $model_forms->update( array( 
  271. 'subscribed' => $form['subscribed'] + 1 
  272. ), array( 
  273. 'form_id' => (int) $form['form_id'] 
  274. )); 
  275.  
  276. // set user profile data 
  277. if( !empty( $data['user_field'] ) ) { 
  278. WJ_FieldHandler::handle_all( $data['user_field'], $user_id ); 
  279.  
  280. // display success message 
  281. if ( !empty( $message_success ) ) { 
  282. $this->notice( $message_success ); 
  283.  
  284. }else { 
  285. if ($subscribing_from_backend) { 
  286. $this->notice(__('Subscriber has not been saved.', WYSIJA)); 
  287. } else { 
  288. $this->notice(__('Oops! We could not add you!', WYSIJA)); 
  289. return false; 
  290.  
  291. $subscribe_to_list = $subscriber_status = 0; 
  292. if ( isset( $data['user']['status'] ) ) { 
  293. $subscriber_status = $data['user']['status']; 
  294.  
  295. if ( ( $confirm_dbloptin && $subscriber_status ) || !$confirm_dbloptin ) { 
  296. $subscribe_to_list = time(); 
  297.  
  298. //4-we add the user to the lists 
  299. $this->addToLists( $data['user_list']['list_ids'], $user_id, $subscribe_to_list ); 
  300.  
  301. // 5-send a confirmation email or add the user to the lists depending on the status 
  302. $can_send_autoresponders = false; 
  303. if ( $subscriber_status > -1 ) { 
  304. if ( $confirm_dbloptin ) { 
  305. if ( $subscriber_status == 0 ) { 
  306. $model_user->reset(); 
  307. $model_user->getFormat = OBJECT; 
  308. $receiver = $model_user->getOne( false , array('email' => trim($data['user']['email']) ) ); 
  309. $this->sendConfirmationEmail( $receiver, true, $data['user_list']['list_ids'] ); 
  310. } else { 
  311. //the subscriber status is set to subscribed so we send the auto nl straight away 
  312. $can_send_autoresponders = true; 
  313. } else { 
  314. // single optin - we send a notification to the admin if settings are set 
  315. $can_send_autoresponders = true; 
  316.  
  317. if ($model_config->getValue( 'emails_notified' ) && $model_config->getValue( 'emails_notified_when_sub' ) ) { 
  318. $this->uid = $user_id; 
  319. if (!$subscribing_from_backend) { 
  320. $this->_notify( $data['user']['email'] , true, $data['user_list']['list_ids'] ); 
  321.  
  322.  
  323. // let's send the autoresponders 
  324. if ( $can_send_autoresponders ) { 
  325. $lists = $this->getUserLists( $user_id, $data['user_list']['list_ids'] ); 
  326. $this->sendAutoNl( $user_id, $lists, 'subs-2-nl', $subscribing_from_backend ); 
  327.  
  328. return $user_id; 
  329.  
  330. /** 
  331. * send auto nl based on the params passed 
  332. * @staticvar type $emails 
  333. * @param int $user_id 
  334. * @param mixed $params_based_on_type the params which are needed for this type of newsletter 
  335. * @param string $process_type the type of auto newsletter that needs to be processed 
  336. * @param boolean $added_by_admin to make the distinction between subscribers added through the admin interface or not 
  337. * @return boolean 
  338. */ 
  339. function sendAutoNl($user_id, $params_based_on_type = false, $process_type = 'subs-2-nl', $added_by_admin = false) { 
  340.  
  341. // check that the user we're trying to send the newsletter to has the right status 
  342. $modelUser=WYSIJA::get('user', 'model'); 
  343. $modelC=WYSIJA::get('config', 'model'); 
  344. $dbloptin=(int)$modelC->getValue('confirm_dbleoptin'); 
  345.  
  346. // we send an autonl if the subscriber is not added to the list by the admin and the user's status is greater or equal to 0 or 1 (if a user switch from double optin to single optin the greater or equal suddenly make sense) 
  347. if(!$added_by_admin && !$modelUser->exists( array( 'equal' => array( 'user_id' => $user_id ), 'greater_eq' => array( 'status' => $dbloptin )))) { 
  348. return false; 
  349.  
  350. $userListss = array(); 
  351. if($dbloptin && !$added_by_admin) { 
  352. $modelUserList=WYSIJA::get('user_list', 'model'); 
  353. $userListssres=$modelUserList->get(array('list_id'), array('equal'=>array('user_id'=>$user_id), 'greater'=>array('sub_date'=>0))); 
  354. foreach($userListssres as $res) { 
  355. $userListss[]=$res['list_id']; 
  356. }elseif(!$dbloptin) { 
  357. // in the case where double optin is off we're filling an array of lists from which the subscriber could receive autoresponders 
  358. $modelUserList = WYSIJA::get('user_list', 'model'); 
  359. $userListssres = $modelUserList->get(array('list_id'), array('equal'=>array('user_id'=>$user_id))); 
  360. foreach($userListssres as $res) { 
  361. $userListss[] = $res['list_id']; 
  362.  
  363. // loading active automatic emails 
  364. static $emails; 
  365. if (empty($emails)) { 
  366. $modelEmail = WYSIJA::get('email', 'model'); 
  367. $modelEmail->reset(); 
  368. $emails = $modelEmail->get(false, array('type' => 2, 'status' => array(1, 3, 99))); 
  369. if (is_object($emails)) { 
  370. $emailarr = null; 
  371. foreach ($emails as $keyob => $valobj) { 
  372. $emailarr[$keyob] = $valobj; 
  373. $emails = $emailarr; 
  374. if (is_array($emails) && isset($emails['body'])) 
  375. $emails = array($emails); 
  376.  
  377.  
  378. foreach ($emails as $key => $email) { 
  379. if ($email['params']['autonl']['event'] != $process_type) 
  380. continue; 
  381. switch ($process_type) { 
  382. case 'subs-2-nl': 
  383. // extra params in that case is an array of lists 
  384. foreach ($params_based_on_type as $details) { 
  385.  
  386. if (isset($email['params']['autonl']['subscribetolist']) && isset($details['list_id']) && $email['params']['autonl']['subscribetolist'] == $details['list_id']) { 
  387. $ok = true; 
  388. if (!$added_by_admin && $dbloptin && !in_array($details['list_id'], $userListss)) { 
  389. // make sure the list has sub_date 
  390. $ok = false; 
  391.  
  392. if ($ok) 
  393. $this->insertAutoQueue($user_id, $email['email_id'], $email['params']['autonl']); 
  394.  
  395. break; 
  396. case 'new-user': 
  397. // extra params in that case is a user object 
  398. $okInsert = false; 
  399. switch ($email['params']['autonl']['roles']) { 
  400. case 'any': 
  401. $okInsert = true; 
  402. break; 
  403. default: 
  404. foreach ($params_based_on_type->roles as $rolename) { 
  405. if ($rolename == $email['params']['autonl']['roles']) 
  406. $okInsert = true; 
  407.  
  408. break; 
  409. if ($okInsert) 
  410. $this->insertAutoQueue($user_id, $email['email_id'], $email['params']['autonl']); 
  411. break; 
  412.  
  413. /** 
  414. * this is used when we want to send the email immediately after inserting it into the queue 
  415. * @param int $user_id 
  416. * @param int $email_id 
  417. * @param array $email_params_autonl 
  418. * @return void 
  419. */ 
  420. function insertAutoQueue($user_id, $email_id, $email_params_autonl) { 
  421. $model_queue = WYSIJA::get('queue', 'model'); 
  422. $queueData = array('priority' => '-1', 'email_id' => $email_id, 'user_id' => $user_id); 
  423.  
  424. $delay = $model_queue->calculate_delay($email_params_autonl); 
  425.  
  426. $queueData['send_at'] = time() + $delay; 
  427.  
  428. // if the email is already queued for that autoresponder, we don't queue it 
  429. if(!$model_queue->exists(array('email_id'=>$email_id, 'user_id'=>$user_id))) { 
  430. // if the email has already been sent and is now in the stat table, we don't queue it either 
  431. $modelEUS = WYSIJA::get('email_user_stat', 'model'); 
  432. if(!$modelEUS->exists(array('email_id'=>$email_id, 'user_id'=>$user_id))) { 
  433. $model_queue->insert($queueData, true); 
  434.  
  435.  
  436. //if delay is = to 0 then we need to try to send the email straight away 
  437. if ($delay == 0) { 
  438. $queueH = WYSIJA::get('queue', 'helper'); 
  439. $queueH->report = false; 
  440. WYSIJA::log('insertAutoQueue queue process', array('email_id' => $email_id, 'user_id' => $user_id), 'queue_process'); 
  441. $queueH->process($email_id, $user_id); 
  442.  
  443. /** 
  444. * unsubscribe a user 
  445. * @param type $user_id 
  446. * @param type $auto 
  447. * @return type 
  448. */ 
  449. function unsubscribe($user_id, $auto = false) { 
  450. return $this->subscribe($user_id, false, $auto); 
  451.  
  452. /** 
  453. * Undo the action "unsubscribe" by a mistake 
  454. * @param type $user_id 
  455. * @param type $auto 
  456. */ 
  457. function undounsubscribe($user_id, $auto = false) { 
  458. return $this->subscribe($user_id, true, $auto); 
  459.  
  460. /** 
  461. * send confirmation email 
  462. * @param type $user_ids 
  463. * @param type $sendone 
  464. * @param type $listids 
  465. * @return boolean 
  466. */ 
  467. function sendConfirmationEmail($user_ids, $sendone = false, $listids = array()) { 
  468. if ($this->no_confirmation_email === true) 
  469. return; 
  470.  
  471. if ($sendone || is_object($user_ids)) { 
  472. /** in this case user_ids is just one user object */ 
  473. $users = array($user_ids); 
  474. } else { 
  475. if (!is_array($user_ids)) { 
  476. $user_ids = (array) $user_ids; 
  477. /** get users objects */ 
  478. $modelU = WYSIJA::get('user', 'model'); 
  479. $modelU->getFormat = OBJECT_K; 
  480. $users = $modelU->get(false, array('equal' => array('user_id' => $user_ids, 'status' => 0))); 
  481.  
  482. $config = WYSIJA::get('config', 'model'); 
  483. $mailer = WYSIJA::get('mailer', 'helper'); 
  484.  
  485. //check if there are lists 
  486. if ($listids) { 
  487. $mailer->listids = $listids; 
  488. $mList = WYSIJA::get('list', 'model'); 
  489. $listnamesarray = $mList->get(array('name'), array('list_id' => $listids)); 
  490. $arrayNames = array(); 
  491. foreach ($listnamesarray as $detailname) { 
  492. $arrayNames[] = $detailname['name']; 
  493. $mailer->listnames = $arrayNames; 
  494.  
  495. //load confirmation email, and if it doesn't exists create a new one 
  496. $mEmail = WYSIJA::get('email', 'model'); 
  497. $mEmail->getFormat = OBJECT; 
  498. $emailConfirmationData = $mEmail->getOne(false, array('email_id' => $config->getValue('confirm_email_id'))); 
  499.  
  500. if (empty($emailConfirmationData)) { 
  501. //somehow the confirmation email has been lost so we need to create a new one 
  502. $dataEmailCon = array('from_name' => $config->getValue('from_name'), 'from_email' => $config->getValue('from_email'),  
  503. 'replyto_name' => $config->getValue('replyto_name'), 'replyto_email' => $config->getValue('replyto_email'),  
  504. 'subject' => $config->getValue('confirm_email_title'), 'body' => $config->getValue('confirm_email_body'), 'type' => '0', 'status' => '99'); 
  505.  
  506. $confemailid = $mEmail->insert($dataEmailCon); 
  507. if ($confemailid) 
  508. $config->save(array('confirm_email_id' => $confemailid)); 
  509.  
  510. $mEmail->reset(); 
  511.  
  512. $mEmail->getFormat = OBJECT; 
  513. $emailConfirmationData = $mEmail->getOne(false, array('email_id' => $config->getValue('confirm_email_id'))); 
  514.  
  515. $result_send = false; 
  516. foreach ($users as $userObj) { 
  517. $result_send = $mailer->sendOne($emailConfirmationData, $userObj, true); 
  518.  
  519. if (!$sendone) { 
  520. if (count($users) <= 0) { 
  521. $this->notice(__('No email sent.', WYSIJA)); 
  522. } else { 
  523. $this->notice( _n( 'One email has been sent.', '%d emails have been sent to unconfirmed subscribers.', count($users), WYSIJA ) ); 
  524. return true; 
  525. else 
  526. return $result_send; 
  527.  
  528. /** 
  529. * Get all unconfirmed subscribers. 
  530. * @param 
  531. * @return Array $unconfirmed_ids Array with all the IDs of unconfirmed subscribers. 
  532. */ 
  533. function get_unconfirmed_subscribers() { 
  534.  
  535. // Get all unconfirmed users ids. 
  536. $model_user = WYSIJA::get('user', 'model'); 
  537. $query = 'SELECT user_id 
  538. FROM ' . '[wysija]' . $model_user->table_name . ' 
  539. WHERE status = 0'; 
  540. $result = $model_user->query('get_res', $query); 
  541.  
  542. // Convert the array to a simple ids array. 
  543. $unconfirmed_subscribers = array(); 
  544. foreach ($result as $unconfirmed_user) { 
  545. $unconfirmed_subscribers[] = $unconfirmed_user['user_id']; 
  546.  
  547. return $unconfirmed_subscribers; 
  548.  
  549. /** 
  550. * bulk delete users 
  551. * @param type $user_ids 
  552. * @param type $sendone 
  553. * @return boolean 
  554. */ 
  555. function delete($user_ids, $sendone = false, $is_batch_select = false) { 
  556. $list_user_ids = null; // list of user ids to query, in a list or sub-query 
  557. $user_model = WYSIJA::get('user', 'model'); 
  558. if ($sendone) { 
  559. // actually, this case is not implemeted yet 
  560. // in this case user_ids is just one user object 
  561. // $users=array($user_ids); 
  562. exit('Not implemented! Please contact mailpoet.com'); 
  563. } elseif ($is_batch_select) { 
  564. // in this case $users_ids = an associated array('query', 'from', 'select', ...etc) 
  565. $list_user_ids = $user_ids['query']; 
  566. } else { 
  567. if (!is_array($user_ids)) { 
  568. $user_ids = (array) $user_ids; 
  569. $list_user_ids = implode(', ', $user_ids); 
  570. // table queue to delete 
  571. $tables = array( 
  572. 'user_history',  
  573. 'email_user_url',  
  574. 'email_user_stat',  
  575. 'user_list',  
  576. 'queue',  
  577. 'user' 
  578. ); 
  579. try { 
  580. foreach ($tables as $table_name) { 
  581. $query = 'DELETE FROM [wysija]' . $table_name . ' WHERE user_id IN (' . $list_user_ids . ')'; 
  582. $user_model->query($query); 
  583. } catch (Exception $e) { 
  584. return false; 
  585. return true; 
  586.  
  587. /** 
  588. * add many subsribers to one list 
  589. * @param int $list_id 
  590. * @param int $user_ids 
  591. * @return boolean 
  592. */ 
  593. function addToList($list_id, $user_ids, $is_batch_select = false) { 
  594. $model_user = WYSIJA::get('user', 'model'); 
  595. $sub_date = time(); 
  596. if (!$is_batch_select) { 
  597. $total = count($user_ids); 
  598. $list_user_ids = implode(', ', $user_ids); 
  599. $query = 'REPLACE INTO `[wysija]user_list` (`list_id`, `user_id`, `sub_date`) VALUES '; 
  600. foreach ($user_ids as $key => $uid) { 
  601. $query .= '(' . (int) $list_id . ', ' . (int) $uid . ', ' . $sub_date . ")\n"; 
  602. if ($total > ($key + 1)) 
  603. $query.=', '; 
  604. }else { 
  605. $list_user_ids = $user_ids['query']; 
  606. $query = 'REPLACE INTO `[wysija]user_list` (`list_id`, `user_id`, `sub_date`) 
  607. SELECT ' . $list_id . ' AS `list_id`, `user_id` AS `user_id`, ' . $sub_date . ' AS `sub_date` ' . $user_ids['from']; 
  608. return $model_user->query($query); 
  609.  
  610. /** 
  611. * add one subscribers to some lists 
  612. * @param array $list_ids 
  613. * @param int $user_id 
  614. * @param int $subscribed_at 
  615. * @return boolean 
  616. */ 
  617. function addToLists($list_ids, $user_id, $subscribed_at = 0) { 
  618.  
  619. if (empty($list_ids)) { 
  620. // don't add subscriber to lists if no list ids specified 
  621. return false; 
  622.  
  623. $modelUser = WYSIJA::get('user', 'model'); 
  624. $extraFieldsName = $extraFieldsVal = ''; 
  625. if ($subscribed_at) { 
  626. $extraFieldsName = ', `sub_date`'; 
  627. $extraFieldsVal = ', ' . $subscribed_at; 
  628. $query = 'INSERT IGNORE INTO `[wysija]user_list` (`list_id`, `user_id`' . $extraFieldsName . ')'; 
  629. $query.=' VALUES '; 
  630. $total = count($list_ids); 
  631. foreach ($list_ids as $key => $listid) { 
  632. $query.='(' . (int) $listid . ', ' . (int) $user_id . $extraFieldsVal . ")\n"; 
  633. if ($total > ($key + 1)) 
  634. $query.=', '; 
  635.  
  636. return $modelUser->query($query); 
  637.  
  638. /** 
  639. * Move bulk users to a list 
  640. * @param int $listid 
  641. * @param array $userids = array(int, int, int, ...) OR array('where'=>'', 'from'=>'', 'select'=>'', 'query'=>'', 'row_counts_query'=>'') 
  642. * @return type 
  643. */ 
  644. function moveToList($listid, $userids, $is_batch_select = false) { 
  645. //1. Remove from all existing list 
  646. $frozen_list = $this->_getHiddenLists(); 
  647. if (empty($frozen_list)) 
  648. $frozen_list_where = ''; 
  649. else 
  650. $frozen_list_where = ' AND `list_id` NOT IN (' . implode(', ', $frozen_list) . ')'; 
  651. if (!$is_batch_select) 
  652. $userids_query = implode(', ', $userids); 
  653. else 
  654. $userids_query = $userids['query']; 
  655. $query1 = 'DELETE FROM `[wysija]user_list` WHERE `user_id` IN (' . $userids_query . ')' . $frozen_list_where; 
  656.  
  657. //2. Add to the new list 
  658. $sub_date = time(); 
  659. if (!$is_batch_select) { 
  660. $records = array(); 
  661. foreach ($userids as $key => $uid) { 
  662. $records[] = implode(', ', array($listid, $uid, $sub_date)); 
  663. $values = '(' . implode('), (', $records) . ');'; 
  664. $query2 = 'INSERT INTO `[wysija]user_list` (`list_id`, `user_id`, `sub_date`) VALUES ' . $values; 
  665. } else { 
  666. $query2 = 'INSERT INTO `[wysija]user_list` (`list_id`, `user_id`, `sub_date`) 
  667. SELECT ' . $listid . ' AS `list_id`, `user_id` AS `user_id`, ' . $sub_date . ' AS `sub_date` ' . $userids['from']; 
  668.  
  669. //3. Execute 
  670. $model_user = WYSIJA::get('user', 'model'); 
  671. $model_user->query($query1); 
  672. $model_user->query($query2); 
  673. return true; 
  674.  
  675. /** 
  676. * Remove user(s) from list(s) 
  677. * @param array $listids 
  678. * @param array $userids 
  679. * @return boolean 
  680. */ 
  681. function removeFromLists($listids = array(), $userids = array(), $is_batch_select = false) { 
  682. if (empty($userids) || (empty($listids) && empty($userids))) 
  683. return true; 
  684. if ($is_batch_select) 
  685. $list_userids = $userids['query']; 
  686. else 
  687. $list_userids = implode(', ', $userids); 
  688.  
  689. $frozen_list = $this->_getHiddenLists(); 
  690. if (empty($frozen_list)) { 
  691. $frozen_list_where = ''; 
  692. } else { 
  693. $frozen_list_where = ' AND `list_id` NOT IN (' . implode(', ', $frozen_list) . ')'; 
  694.  
  695. if (empty($listids)) {//remove from all lists 
  696. $query = 'DELETE FROM `[wysija]user_list` WHERE `user_id` IN (' . $list_userids . ')'; 
  697. } else { // remove from specific lists 
  698. $list_listids = implode(', ', $listids); 
  699. $query = 'DELETE FROM `[wysija]user_list` WHERE `user_id` IN (' . $list_userids . ') AND `list_id` IN (' . $list_listids . ')'; 
  700. $query .= $frozen_list_where; 
  701.  
  702. //Execute 
  703. $model_user = WYSIJA::get('user', 'model'); 
  704. return $model_user->query($query); 
  705.  
  706. /** 
  707. * Bulk confirm user(s) 
  708. * @param array $userids 
  709. * @return boolean 
  710. */ 
  711. function confirmUsers($userids = array(), $is_batch_select = false) { 
  712. if (empty($userids)) 
  713. return true; 
  714.  
  715. $model_user = WYSIJA::get('user', 'model'); 
  716. if ($is_batch_select) { 
  717. $row_count = $userids['count']; 
  718. $list_userids = $userids['query']; 
  719. } else { 
  720. $list_userids = implode(', ', $userids); 
  721. $row_count = !empty($_REQUEST['wysija']['user']['user_id']) ? count($_REQUEST['wysija']['user']['user_id']) : 0; 
  722. $query1 = 'UPDATE `[wysija]user` SET status = 1 WHERE `user_id` IN (' . $list_userids . ')'; 
  723. $query2 = 'UPDATE `[wysija]user_list` SET sub_date = ' . time() . ', unsub_date = 0 WHERE `user_id` IN (' . $list_userids . ')'; 
  724.  
  725. //Execute 
  726. $model_user->query($query1); 
  727. $model_user->query($query2); 
  728. return $row_count; 
  729.  
  730. /** 
  731. * Get hidden lists (typically Wordpress user list) 
  732. * @return array(int, int) 
  733. */ 
  734. function _getHiddenLists() { 
  735. $result = WYSIJA::get('user', 'model')->getResults('SELECT GROUP_CONCAT(`list_id`) as ids FROM `[wysija]list` WHERE `is_enabled` = 0'); 
  736. return $result[0]['ids'] ? explode(', ', $result[0]['ids']) : array(); 
  737.  
  738. /** 
  739. * send Admin notification about new subscriber or new unsubscribed user 
  740. * @param type $email 
  741. * @param type $subscribed 
  742. * @param type $list_ids 
  743. */ 
  744. function _notify($email, $subscribed = true, $list_ids = false) { 
  745. // get the public list to which user is subscribed 
  746. $model_user = WYSIJA::get('user_list', 'model'); 
  747.  
  748. if ($list_ids) { 
  749. $query = "Select B.name from `[wysija]list` as B where B.list_id IN ('" . implode("', '", $list_ids) . "') and B.is_enabled>0"; 
  750. } else { 
  751. $query = 'Select B.name from `[wysija]user_list` as A join `[wysija]list` as B on A.list_id=B.list_id where A.user_id=' . $this->uid . ' and B.is_enabled>0'; 
  752.  
  753. $result = $model_user->query('get_res', $query); 
  754. $list_names = array(); 
  755. foreach ($result as $arra) { 
  756. $list_names[] = $arra['name']; 
  757.  
  758. if ($subscribed) { 
  759. $title = sprintf(__('New subscriber to %1$s', WYSIJA), implode(', ', $list_names)); 
  760. $body = sprintf(__("Howdy, \n\n The subscriber %1\$s has just subscribed to your list '%2\$s' \n\n Cheers, \n\n The MailPoet Plugin", WYSIJA), "<strong>" . $email . "</strong>", "<strong>" . implode(', ', $list_names) . "</strong>"); 
  761. } else { 
  762. $title = sprintf(__('One less subscriber to %1$s', WYSIJA), implode(', ', $list_names)); 
  763. $body = sprintf(__("Howdy, \n\n The subscriber %1\$s has just unsubscribed to your list '%2\$s' \n\n Cheers, \n\n The MailPoet Plugin", WYSIJA), "<strong>" . $email . "</strong>", "<strong>" . implode(', ', $list_names) . "</strong>"); 
  764.  
  765. $model_config = WYSIJA::get('config', 'model'); 
  766. $mailer = WYSIJA::get('mailer', 'helper'); 
  767. $notifieds = $model_config->getValue('emails_notified'); 
  768. //$mailer->report=false; 
  769. $notifieds = explode(', ', $notifieds); 
  770. $mailer->testemail = true; 
  771. $body = nl2br($body); 
  772. foreach ($notifieds as $receiver) { 
  773. $mailer->sendSimple(trim($receiver), $title, $body); 
  774.  
  775. /** 
  776. * refresh all related info of users 
  777. * @return boolean 
  778. */ 
  779. function refreshUsers() { 
  780. $model_user = WYSIJA::get('user', 'model'); 
  781. $model_user->reset(); 
  782. $model_config = WYSIJA::get('config', 'model'); 
  783. if ($model_config->getValue('confirm_dbleoptin')) { 
  784. $model_user->setConditions(array('greater' => array('status' => 0))); 
  785. } else { 
  786. $model_user->setConditions(array('greater' => array('status' => -1))); 
  787.  
  788. $count = $model_user->count(); 
  789. $model_config->save(array('total_subscribers' => $count)); 
  790. return true; 
  791.  
  792. /** 
  793. * Regenerate domains based on users' emails 
  794. */ 
  795. public function generate_domains() { 
  796. $query = "UPDATE [wysija]user SET `domain` = SUBSTRING(`email`, LOCATE('@', `email`)+1) WHERE ISNULL(`domain`)"; 
  797. $model_user = WYSIJA::get('user', 'model'); 
  798. return $model_user->query($query); 
  799.  
  800. /** 
  801. * function used to update the synchronisation with a plugin list or WP's one table 
  802. * @global object $wpdb 
  803. * @param int $list_id 
  804. * @param boolean $synch_all_wp_user_base means complete synch of the whole user base, it's used for multisite and the WordPress users list 
  805. * @return type 
  806. */ 
  807. function synchList($list_id, $synch_all_wp_user_base = false) { 
  808. $model = WYSIJA::get('list', 'model'); 
  809. $data = $model->getOne(false, array('list_id' => (int) $list_id, 'is_enabled' => '0')); 
  810.  
  811. if ($data) { 
  812.  
  813. if (strpos($data['namekey'], '-listimported-') !== false) { 
  814. // plugins list table synch 
  815. $model->reset(); 
  816.  
  817. // import synch list 
  818. $listdata = explode('-listimported-', $data['namekey']); 
  819. $dataMainList = $model->getOne(false, array('namekey' => $listdata[0], 'is_enabled' => '0')); 
  820.  
  821. $importHelper = WYSIJA::get('plugins_import', 'helper'); 
  822. $connection_info = $importHelper->getPluginsInfo($listdata[0]); 
  823. $lists_ids = array( 
  824. 'wysija_list_main_id' => $dataMainList['list_id'],  
  825. 'wysija_list_id' => $data['list_id'],  
  826. 'plug_list_id' => $listdata[1] 
  827. ); 
  828. $importHelper->import($listdata[0], $connection_info, false, false, $lists_ids); 
  829. } elseif ($data['namekey'] == 'users') { 
  830. // wordpress user table synch 
  831. //importwp 
  832. $ismainsite = true; 
  833. if (is_multisite()) { 
  834. global $wpdb; 
  835. if ($wpdb->prefix != $wpdb->base_prefix) { 
  836. $ismainsite = false; 
  837.  
  838. $connection_info = array('name' => 'WordPress',  
  839. 'pk' => 'ID',  
  840. 'matches' => array('ID' => 'wpuser_id', 'user_email' => 'email', 'first_name' => 'firstname', 'last_name' => 'lastname'),  
  841. 'matchesvar' => array('status' => 1)); 
  842.  
  843. $importHelper = WYSIJA::get('plugins_import', 'helper'); 
  844. $lists_ids = array( 
  845. 'wysija_list_main_id' => $data['list_id'] 
  846. ); 
  847.  
  848. $importHelper->import($data['namekey'], $connection_info, false, $synch_all_wp_user_base, $lists_ids); 
  849.  
  850. $this->cleanWordpressUsersList(); 
  851. } elseif (strpos($data['namekey'], 'query-') !== false) { 
  852. // special query look for an action to run the refresh 
  853. //TODO maybe we don't need to create a list that we sync but simply need a way to make a filter 
  854. $queryUid = apply_filters('wysija_synch_' . str_replace('-', '_', $data['namekey'])); 
  855. $importHelper = WYSIJA::get('plugins_import', 'helper'); 
  856. $queryUid = str_replace(array('[list_id]', '[created_at]'), array($data['list_id'], time()), $queryUid); 
  857. $importHelper->insertUserList(0, 0, 0, $queryUid); 
  858. } else { 
  859. // plugins table synch 
  860.  
  861. $config = WYSIJA::get('config', 'model'); 
  862. $importables = $config->getValue('pluginsImportableEgg'); 
  863.  
  864. if (in_array($data['namekey'], array_keys($importables))) { 
  865. $importHelper = WYSIJA::get('plugins_import', 'helper'); 
  866. $dataMainList = $model->getOne(false, array('namekey' => $data['namekey'], 'is_enabled' => '0')); 
  867.  
  868. $importHelper->import($data['namekey'], $importHelper->getPluginsInfo($data['namekey']), false, false, array('wysija_list_main_id' => $dataMainList['list_id'])); 
  869. $this->notice(sprintf(__('List "%1$s" has been synchronised.', WYSIJA), $data['name'])); 
  870. return true; 
  871. } else { 
  872. $this->error(__('The list does not exists or cannot be synched.', WYSIJA), true); 
  873. return false; 
  874.  
  875. /** 
  876. * function used to cleanup the lost wpuser_id 
  877. * @return boolean 
  878. */ 
  879. function cleanWordpressUsersList() { 
  880. // update the screwed up wpuser_id 
  881. $model_list = WYSIJA::get('list', 'model'); 
  882. $query = 'UPDATE [wysija]user as A JOIN [wp]users as B on A.email=B.user_email SET A.wpuser_id = B.ID WHERE A.wpuser_id = 0'; 
  883. $model_list->query($query); 
  884.  
  885. // get all the wysija user with a wpuser_id >0 and insert them into the WordPress user list 
  886. $model_config = WYSIJA::get('config', 'model'); 
  887. $selectuserCreated = 'SELECT [wysija]user.user_id, ' . $model_config->getValue('importwp_list_id') . ', ' . time() . ' FROM [wysija]user WHERE wpuser_id > 0'; 
  888. $query = 'INSERT IGNORE INTO `[wysija]user_list` (`user_id`, `list_id`, `sub_date`) ' . $selectuserCreated; 
  889. $model_list->reset(); 
  890. $model_list->query($query); 
  891. return true; 
  892.  
  893. /** 
  894. * get an IP for the current visitor/user 
  895. * @return string IP address 
  896. */ 
  897. function getIP() { 
  898. $ip = ''; 
  899.  
  900. // cloudFlare IP check 
  901. if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { 
  902. $ip = strip_tags($_SERVER['HTTP_CF_CONNECTING_IP']); 
  903. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) AND strlen($_SERVER['HTTP_X_FORWARDED_FOR']) > 6) { 
  904. $ip = strip_tags($_SERVER['HTTP_X_FORWARDED_FOR']); 
  905. } elseif (!empty($_SERVER['HTTP_CLIENT_IP']) AND strlen($_SERVER['HTTP_CLIENT_IP']) > 6) { 
  906. $ip = strip_tags($_SERVER['HTTP_CLIENT_IP']); 
  907. } elseif (!empty($_SERVER['REMOTE_ADDR']) AND strlen($_SERVER['REMOTE_ADDR']) > 6) { 
  908. $ip = strip_tags($_SERVER['REMOTE_ADDR']); 
  909. }//endif 
  910. if (empty($ip)) 
  911. $ip = '127.0.0.1'; 
  912. return strip_tags($ip); 
  913.  
  914. /** 
  915. * verify that an email string is valid 
  916. * @param string $email 
  917. * @return boolean 
  918. */ 
  919. function validEmail($email) { 
  920. //1 - check if the email parameter looks like an email 
  921. if (empty($email) || !is_string($email) || strpos($email, '@') === false) 
  922. return false; 
  923.  
  924. //2 - pregmatch the email 
  925. $check_domain = false; 
  926. $preg_check = true; 
  927. $string_special_chars = '\\\\\\'; 
  928. $email_pattern = '/^([a-z0-9_\'&\.\-\+' . $string_special_chars . '])+\@(([a-z0-9\-' . $string_special_chars . '])+\.)+([a-z0-9\-' . $string_special_chars . ']{2, 10})+$/i'; 
  929. //$string_special_chars = ''; 
  930. $model_config = WYSIJA::get('config', 'model'); 
  931. if ($model_config->getValue('email_cyrillic')) { 
  932. // check domain 
  933. $preg_check = false; 
  934.  
  935. if ($model_config->getValue('check_domain')) { 
  936. // check domain 
  937. $check_domain = true; 
  938.  
  939. if ($preg_check) { 
  940. if (!preg_match($email_pattern, $email)) 
  941. return false; 
  942.  
  943.  
  944. if ($check_domain) { 
  945. //3 - make sure the domain of the email exists 
  946. $helper_toolbox = WYSIJA::get('toolbox', 'helper'); 
  947.  
  948. if (!$helper_toolbox->check_email_domain($email)) 
  949. return false; 
  950. return true; 
  951.  
  952. /** 
  953. * make sure that the request wysija-key parameter correspond to a user in the db 
  954. * @return boolean 
  955. */ 
  956. function checkUserKey($user_id = false) { 
  957.  
  958. // the !is numeric condition is here because if we input wysija-key=3 in the url 
  959. // our model will search keyuser=3 which will return the result of the user with key user 3b61ac508456ab6ee7594c47cddb86a5 
  960. if((!empty($_REQUEST['wysija-key']) && !is_numeric($_REQUEST['wysija-key'])) || $user_id !==false) { 
  961. $modelUser=WYSIJA::get('user', 'model'); 
  962.  
  963. if ($user_id === false) { 
  964. $where_condition = array('keyuser' => $_REQUEST['wysija-key']); 
  965. } else { 
  966. $where_condition = array('user_id' => $user_id); 
  967.  
  968. $result = $modelUser->getDetails($where_condition); 
  969. if ($result) { 
  970. return $result; 
  971. } else { 
  972. $this->error(__('Page is not accessible.', WYSIJA), true); 
  973. return false; 
  974. } else { 
  975. $this->error(__('Page is not accessible.', WYSIJA), true); 
  976. return false; 
  977.  
  978. /** 
  979. * get all of the lists a user has subscribed to 
  980. * @param int $user_id 
  981. * @param array $list_ids 
  982. * @return array results 
  983. */ 
  984. function getUserLists($user_id, $list_ids = array()) { 
  985. $model_user = WYSIJA::get('user', 'model'); 
  986. $list_id_in = ''; 
  987. $clean_ids = array(); 
  988. foreach ($list_ids as $id) { 
  989. $clean_ids[] = (int)$id; 
  990. if (!empty($clean_ids)) { 
  991. $list_id_in = "AND A.list_id IN(" . implode(", ", $clean_ids) . ")"; 
  992. $query = 'SELECT A.* FROM [wysija]user_list as A LEFT JOIN [wysija]list as B on A.list_id=B.list_id WHERE A.user_id=' . (int)$user_id . ' AND B.is_enabled=1 ' . $list_id_in; 
  993. return $model_user->getResults($query); 
  994.  
  995. /** 
  996. * if we confirm the user manually we pass it a user_id otherwise it works with a global key 
  997. * @param type $user_id 
  998. * @return boolean 
  999. */ 
  1000. function confirm_user($user_id = false) { 
  1001. $model_config = WYSIJA::get('config', 'model'); 
  1002. // we need to call the translation otherwise it will not be loaded and translated 
  1003. $model_config->add_translated_default(); 
  1004.  
  1005. $list_ids = array(); 
  1006. if ( isset( $_REQUEST['wysiconf'] ) ) { 
  1007. $list_ids = json_decode( base64_decode( $_REQUEST['wysiconf'] ), true ); 
  1008.  
  1009. if(empty( $list_ids ) || !is_array( $list_ids )) { 
  1010. $this->title = __('Your confirmation link expired, please subscribe again.', WYSIJA); 
  1011. return; 
  1012.  
  1013. // START part linked to the view 
  1014. $this->title = $model_config->getValue( 'subscribed_title' ); 
  1015. if ( !empty( $list_ids ) && is_array( $list_ids ) ) { 
  1016. $model_list = WYSIJA::get('list', 'model'); 
  1017. $lists_names_res = $model_list->get(array('name'), array('list_id' => $list_ids)); 
  1018. $names = array(); 
  1019. foreach ($lists_names_res as $nameob) { 
  1020. $names[] = $nameob['name']; 
  1021.  
  1022.  
  1023. if (!isset($model_config->values['subscribed_title'])) { 
  1024. $this->title = __('You\'ve subscribed to: %1$s', WYSIJA); 
  1025.  
  1026. $this->title = sprintf($this->title, implode(', ', $names)); 
  1027.  
  1028. $this->subtitle = $model_config->getValue('subscribed_subtitle'); 
  1029. if ( !isset( $model_config->values['subscribed_subtitle'] ) ) { 
  1030. $this->subtitle = __("Yup, we've added you to our list. You'll hear from us shortly.", WYSIJA); 
  1031. // END part linked to the view 
  1032.  
  1033. $user_data = $this->checkUserKey( $user_id ); 
  1034.  
  1035. if ( $user_data ) { 
  1036. //user is not confirmed yet 
  1037. $model_config = WYSIJA::get('config', 'model'); 
  1038.  
  1039. if ( (int) $user_data['details']['status'] < 1) { 
  1040. // let's confirm the subscriber 
  1041. $this->subscribe( $user_data['details']['user_id'], true, false, $list_ids ); 
  1042. $this->uid = $user_data['details']['user_id']; 
  1043.  
  1044. // send a notification to the email specified in the settings if required to 
  1045. if ( $model_config->getValue( 'emails_notified' ) && $model_config->getValue( 'emails_notified_when_sub' ) ) { 
  1046. $this->_notify( $user_data['details']['email'] ); 
  1047. return true; 
  1048. } else { 
  1049. if ( isset( $_REQUEST['wysiconf'] ) ) { 
  1050. $needs_subscription = false; 
  1051. foreach ( $user_data['lists'] as $list ) { 
  1052. if ( in_array( $list['list_id'], $list_ids ) && (int) $list['sub_date'] < 1) { 
  1053. $needs_subscription = true; 
  1054.  
  1055. if ( $needs_subscription ) { 
  1056.  
  1057. $this->subscribe( $user_data['details']['user_id'] , true, false, $list_ids ); 
  1058. $this->title = sprintf( $model_config->getValue('subscribed_title') , implode( ', ', $names ) ); 
  1059. $this->subtitle = $model_config->getValue( 'subscribed_subtitle' ); 
  1060. // send a notification to the email specified in the settings if required to 
  1061. if ( $model_config->getValue( 'emails_notified' ) && $model_config->getValue( 'emails_notified_when_sub' ) ) { 
  1062. $this->_notify( $user_data['details']['email'] , true , $list_ids ); 
  1063. } else { 
  1064. $this->title = sprintf( __('You are already subscribed to : %1$s' , WYSIJA ) , implode( ', ', $names ) ); 
  1065. } else { 
  1066. $this->title = __( 'You are already subscribed.' , WYSIJA ); 
  1067. return true;