Exemple #1
0
 public static function importBis()
 {
     @set_time_limit(0);
     $returnMessage = '';
     //If memory_limit less than 128M
     $limit = jnews::convertToBytes(@ini_get('memory_limit'));
     if ($limit < jnews::convertToBytes('128M')) {
         @ini_set('memory_limit', '128M');
     }
     $allAvailableListsA = JRequest::getVar('sub_list_id', '');
     $list2SubcribeToA = JRequest::getVar('subscribed', '');
     $filename = $_FILES['importfile']['name'];
     if (!empty($filename)) {
         //if we import a file
         //the flag is true coz we add subscribers in frontend through rows
         $saveSubsInRows = true;
         $path = JNEWS_JPATH_ROOT_NO_ADMIN . $GLOBALS[JNEWS . 'upload_url'];
         $path = str_replace(array('/', '\\'), DS, $path);
         if (!is_writable($path)) {
             $returnMessage .= '<br/>The file could not be imported because the folder is not writable.';
             $returnMessage .= '<br/>Please make sure the path "' . JNEWS_JPATH_ROOT_NO_ADMIN . $GLOBALS[JNEWS . 'upload_url'] . '" is writable';
             return $returnMessage;
         }
         if (!@move_uploaded_file($_FILES['importfile']['tmp_name'], $path . $filename)) {
             $path .= DS;
             if (!@move_uploaded_file($_FILES['importfile']['tmp_name'], $path . $filename)) {
                 $returnMessage .= _JNEWS_ERROR_MOVING_UPLOAD;
                 $returnMessage .= '<br/>Please make sure the path "' . JNEWS_JPATH_ROOT_NO_ADMIN . $GLOBALS[JNEWS . 'upload_url'] . '" is writable';
                 return $returnMessage;
             }
         }
         $import = file_get_contents($path . $filename);
         $import = str_replace(array("\r\n", "\r"), "\n", $import);
         $allLinesA = explode("\n", $import);
     } else {
         //if we add subscribers in frontend through rows
         //the flag is true coz we add subscribers in frontend through rows
         $saveSubsInRows = true;
         //we get the number of rows
         $rows = JRequest::getVar('count', '');
         $nRows = intval($rows) - 1;
         if ($nRows == 0) {
             $nRows = intval($GLOBALS[JNEWS . 'create_newsubsrows']);
         }
         //we get the value of each element from the rows
         for ($count = 1; $count <= $nRows; $count++) {
             $allLinesA[$count] = JRequest::getVar('name' . $count, '') . ', ' . JRequest::getVar('email' . $count, '') . ', ' . JRequest::getVar('receive_html' . $count, '') . ', ' . JRequest::getVar('confirmed' . $count, '') . ', ' . JRequest::getVar('column1' . $count, '') . ', ' . JRequest::getVar('column2' . $count, '') . ', ' . JRequest::getVar('column3' . $count, '') . ', ' . JRequest::getVar('column4' . $count, '') . ', ' . JRequest::getVar('column5' . $count, '');
         }
     }
     if (empty($allLinesA)) {
         return false;
     }
     $db = JFactory::getDBO();
     $insertQuery = '';
     $selectQuery = '';
     $firstValue = true;
     $time = time();
     $totalInsert = 0;
     $allSubscribersIDA = array();
     foreach ($allLinesA as $line) {
         $line = trim($line);
         if (empty($line)) {
             continue;
         }
         $line = str_replace(';', ',', $line);
         $subscriberA = explode(',', $line);
         $email = trim($subscriberA[1], ' "\'');
         if (!jNews_Subscribers::validEmail($email)) {
             if (!$saveSubsInRows) {
                 //if import of file is done
                 echo '<br />' . jnews::printM('red', _JNEWS_EMAIL_INVALID . ': ' . $email);
                 continue;
             } else {
                 continue;
             }
         }
         if ($GLOBALS[JNEWS . 'level'] > 2) {
             $column4 = !empty($subscriberA[4]) ? mysql_escape_string(trim($subscriberA[4], '"')) : '';
             $column5 = !empty($subscriberA[5]) ? mysql_escape_string(trim($subscriberA[5], '"')) : '';
             $column6 = !empty($subscriberA[6]) ? mysql_escape_string(trim($subscriberA[6], '"')) : '';
             $column7 = !empty($subscriberA[7]) ? mysql_escape_string(trim($subscriberA[7], '"')) : '';
             $column8 = !empty($subscriberA[8]) ? mysql_escape_string(trim($subscriberA[8], '"')) : '';
             $ip = !empty($subscriberA[9]) ? mysql_escape_string(trim($subscriberA[9], '"')) : '';
         }
         $comfirmation = $GLOBALS[JNEWS . 'require_confirmation'] ? 0 : (int) trim($subscriberA[3], ' "\'');
         // i need to know if exist user
         $db = JFactory::getDBO();
         $quer = "SELECT id FROM `#__jnews_subscribers` WHERE `email` = '{$email}'";
         $db->setQuery($quer);
         $res = $db->loadResult();
         if ($res) {
             //i need to update
             $isResiveHtml = (int) trim($subscriberA[2], ' "\'');
             $querUpdate = "UPDATE `#__jnews_subscribers` SET confirmed = '{$comfirmation}', receive_html= '{$isResiveHtml}',column1 = '{$column4}', column2 = '{$column5}', column3 = '{$column5}', column4 = '{$column7}', column5= '{$column8}',ip = '{$ip}' WHERE `email` = '{$email}'";
             $db->setQuery($querUpdate);
             $db->query();
             if (!empty($allAvailableListsA) && !empty($list2SubcribeToA)) {
                 foreach ($list2SubcribeToA as $oneneedList => $noneed) {
                     $listId = $allAvailableListsA[$oneneedList];
                     //
                     $query = "INSERT IGNORE `#__jnews_listssubscribers` (`list_id`,`subscriber_id`,`subdate`) VALUES ({$listId}, {$res}, {$time})";
                     $db->setQuery($query);
                     $db->query();
                 }
             }
             continue;
         }
         if (!$firstValue) {
             $insertQuery .= ',';
         }
         /*$insertQuery .= '(\''. htmlentities(trim($subscriberA[0],' "\'') ) . '\',\''.$email.'\','.$comfirmation.','.(int)trim($subscriberA[2],' "\'').',\''.$time.'\',\''.$column4.'\',\''.$column5.'\',\''.$column6.'' .
         		'\',\''.$column7.'\',\''.$column8.'\',\''.$ip.'\')';*/
         $insertQuery .= '(\'' . addslashes(trim($subscriberA[0], ' "\'')) . '\',\'' . $email . '\',' . $comfirmation . ',' . (int) trim($subscriberA[2], ' "\'') . ',\'' . $time . '\',\'' . $column4 . '\',\'' . $column5 . '\',\'' . $column6 . '' . '\',\'' . $column7 . '\',\'' . $column8 . '\',\'' . $ip . '\')';
         if (!$firstValue) {
             $selectQuery .= ',';
         }
         $selectQuery .= '\'' . $email . '\'';
         $firstValue = false;
         $totalInsert++;
         if ($totalInsert % 50 == 0) {
             if (empty($insertQuery)) {
                 continue;
             }
             $db = JFactory::getDBO();
             $query = 'INSERT IGNORE `#__jnews_subscribers` (`name`,`email`,`confirmed`,`receive_html`,`subscribe_date`,`column1`,`column2`,`column3`,`column4`,`column5`,`ip`) VALUES ' . $insertQuery;
             $db->setQuery($query);
             $db->query();
             $firstValue = true;
             $insertQuery = '';
             //get the id of the subscribers
             $query = 'SELECT `id` FROM `#__jnews_subscribers` WHERE `email` IN (' . $selectQuery . ')';
             $db->setQuery($query);
             $loadResultArray = $db->loadObjectList();
             $resultsSubClA = jnews::convertObjectList2Array($loadResultArray);
             $allSubscribersIDA = array_merge($allSubscribersIDA, $resultsSubClA);
             $selectQuery = '';
         } else {
             if (empty($insertQuery)) {
                 return false;
             }
             $db = JFactory::getDBO();
             $query = 'INSERT IGNORE `#__jnews_subscribers` (`name`,`email`,`confirmed`,`receive_html`,`subscribe_date`,`column1`,`column2`,`column3`,`column4`,`column5`,`ip`) VALUES ' . $insertQuery;
             $db->setQuery($query);
             $db->query();
         }
     }
     //send confirmation email if required
     if ($GLOBALS[JNEWS . 'require_confirmation'] && !empty($list2SubcribeToA)) {
         $list4Message = $allAvailableListsA[key($list2SubcribeToA)];
         $myList = jNews_Lists::getOneList($list4Message);
         $myList->subscribemessage = trim($myList->subscribemessage);
         if (empty($myList->subscribemessage) || substr_count($myList->subscribemessage, '{tag:confirm}') < 1) {
             $Sub_TAG = '{tag:confirm}';
             //'[CONFIRM]';
         } else {
             $Sub_TAG = '';
         }
         $mailing = new stdClass();
         if (!empty($myList->subscribemessage)) {
             $mailing->htmlcontent = $myList->subscribemessage;
             $mailing->textonly = $myList->subscribemessage;
         } else {
             // default message
             $mailing->textonly = 'Please confirm your subscription to our website by clicking on the following link.';
             $mailing->htmlcontent = '<br />' . $mailing->textonly . '<br /><br />';
         }
         $mailing->textonly .= $Sub_TAG;
         $mailing->htmlcontent .= $Sub_TAG;
         //insert the mailing in the mailing table
         $mailing->mailing_type = 1;
         $mailing->template_id = 0;
         $mailing->senddate = $time;
         $mailing->subject = _JNEWS_SUBSCRIBE_SUBJECT_MESS;
         $mailing->attachments = '';
         $mailing->images = '';
         $mailing->published = 1;
         $mailing->html = 1;
         $mailing->visible = 0;
         $mailing->fromname = '';
         $mailing->fromemail = '';
         $mailing->frombounce = '';
         $mailing->author_id = 0;
         $mailing->delay = 0;
         $mailing->follow_up = 0;
         $mailing->cat_id = 0;
         $mailing->delay_min = 0;
         $mailing->delay_max = 0;
         $mailing->notify_id = 0;
         $mailing->next_date = 0;
         $mailing->start_date = $time - 2;
         $mailing->issue_nb = 0;
         $mailing->createdate = $time;
         $mailing->acc_level = 0;
         $mailingID = jNews_Mailing::insertMailingData($mailing);
         //enter the emails in the queue
         $query = 'INSERT IGNORE INTO `#__jnews_queue` (`subscriber_id`,`mailing_id`,`send_date`,`priority`) ';
         $query .= 'SELECT `id`,' . intval($mailingID) . ',' . $time . ',1';
         $query .= ' FROM `#__jnews_subscribers` ';
         $query .= 'WHERE `subscribe_date`=' . $time . ' AND `confirmed`=0';
         $db->setQuery($query);
         $db->query();
     }
     if (!empty($selectQuery)) {
         //get the id of the subscribers
         $query = 'SELECT `id` FROM `#__jnews_subscribers` WHERE `email` IN (' . $selectQuery . ')';
         $db->setQuery($query);
         $loadResultArray = $db->loadObjectList();
         $resultsSubClA = jnews::convertObjectList2Array($loadResultArray);
         $allSubscribersIDA = array_merge($allSubscribersIDA, $resultsSubClA);
     }
     if (!empty($allAvailableListsA)) {
         //if lists in checkbox are not empty
         if (empty($list2SubcribeToA)) {
             jnews::printM('red', 'You need to select at least one list. Emails have been imported but not subscribed.');
             if ($GLOBALS[JNEWS . 'require_confirmation']) {
                 return 'The confirmation email cannot be sent because there is not list selected to subscribe the users.';
             } else {
                 return _JNEWS_IMPORT_FINISHED;
             }
         }
         foreach ($list2SubcribeToA as $oneselected => $vval) {
             $seleeected[] = $allAvailableListsA[$oneselected];
         }
         foreach ($list2SubcribeToA as $oneneedList => $noneed) {
             $listId = $allAvailableListsA[$oneneedList];
             //
             $query = 'INSERT IGNORE `#__jnews_listssubscribers` (`list_id`,`subscriber_id`,`subdate`) VALUES ';
             $firstTime = true;
             if (!empty($allSubscribersIDA)) {
                 foreach ($allSubscribersIDA as $oneSubcriber) {
                     if (!$firstTime) {
                         $query .= ',';
                     }
                     $query .= '(' . $listId . ',' . $oneSubcriber . ',' . $time . ')';
                     $firstTime = false;
                     if (jNews_Lists::getListType($listId) == 2) {
                         //we add directly the imported subscribers to the queue if they are imported to an autoresponder type of list
                         jNews_ListsSubs::subscribeARtoQueue($oneSubcriber, $listId);
                         //
                     } else {
                         if (jNews_Lists::getListType($listId) == 1) {
                             $arrayFollowUp = jNews_Subscribers::followInSelected($listId);
                             if (!$arrayFollowUp) {
                                 continue;
                             }
                             foreach ($arrayFollowUp as $onefollow_id) {
                                 if (in_array($onefollow_id, $seleeected)) {
                                     continue;
                                 }
                                 $query .= ',(' . $onefollow_id . ',' . $oneSubcriber . ',' . $time . ')';
                                 jNews_ListsSubs::subscribeARtoQueue($oneSubcriber, $onefollow_id);
                             }
                         }
                     }
                 }
                 $db->setQuery($query);
                 $db->query();
             }
         }
     } else {
         //otherwise we get the value from dropdown box
         $listId = JRequest::getVar('opted_list', '');
         if ($listId) {
             $query = 'INSERT IGNORE `#__jnews_listssubscribers` (`list_id`,`subscriber_id`,`subdate`) VALUES ';
             $firstTime = true;
             if (!empty($allSubscribersIDA)) {
                 foreach ($allSubscribersIDA as $oneSubcriber) {
                     if (!$firstTime) {
                         $query .= ',';
                     }
                     $query .= '(' . $listId . ',' . $oneSubcriber . ',' . $time . ')';
                     $firstTime = false;
                     if (jNews_Lists::getListType($listId) == 2) {
                         //we add directly the imported subscribers to the queue if they are imported to an autoresponder type of list
                         jNews_ListsSubs::subscribeARtoQueue($oneSubcriber, $listId);
                     } else {
                         if (jNews_Lists::getListType($listId) == 1) {
                             $arrayFollowUp = jNews_Subscribers::followInSelected($listId);
                             if (!$arrayFollowUp) {
                                 continue;
                             }
                             foreach ($arrayFollowUp as $onefollow_id) {
                                 if (in_array($onefollow_id, $seleeected)) {
                                     continue;
                                 }
                                 $query .= ',(' . $onefollow_id . ',' . $oneSubcriber . ',' . $time . ')';
                                 jNews_ListsSubs::subscribeARtoQueue($oneSubcriber, $onefollow_id);
                             }
                         }
                     }
                 }
                 $db->setQuery($query);
                 $db->query();
             }
         }
     }
     if ($saveSubsInRows) {
         //if adding subscribers in frontend through rows
         jnews::printM('green', count($allSubscribersIDA) . ' subscribers successfully added!');
     } else {
         //otherwise we import subscribers
         jnews::printM('green', count($allSubscribersIDA) . ' subscribers successfully imported!');
     }
     return _JNEWS_IMPORT_FINISHED;
 }