Beispiel #1
0
function post_notification_send()
{
    global $wpdb, $timestart;
    if (get_option('post_notification_lock') == 'db') {
        if (!$wpdb->get_var("SELECT GET_LOCK('" . $wpdb->prefix . 'post_notification_lock' . "', 0)")) {
            return;
        }
    } else {
        $mutex = @fopen(POST_NOTIFICATION_PATH . '_temp/post_notification.lock', 'w');
        if (!$mutex || !flock($mutex, LOCK_EX | LOCK_NB, $eWouldBlock) || $eWouldBlock) {
            // There is already someone mailing.
            @fclose($mutex);
            return;
        }
    }
    //Make sure plugins don't think we're a page or something....
    $GLOBALS['wp_query']->init_query_flags();
    ignore_user_abort(true);
    //Let's get this done....
    //some general stuff
    $t_emails = $wpdb->prefix . 'post_notification_emails';
    $t_posts = $wpdb->prefix . 'post_notification_posts';
    $t_cats = $wpdb->prefix . 'post_notification_cats';
    $posts = $wpdb->get_results("SELECT id, notification_sent " . post_notification_sql_posts_to_send());
    if (!$posts) {
        //This shouldn't happen, but never mind.
        post_notification_set_next_send();
        return;
        //Nothing to do.
    }
    //Include user functions, if they exist.
    //We don't want an error if the file does not exist.
    if (file_exists(POST_NOTIFICATION_PATH . 'userfunctions.php')) {
        include_once POST_NOTIFICATION_PATH . 'userfunctions.php';
    }
    // Mail out mails
    $maxsend = get_option('post_notification_maxsend');
    $mailssent = -1;
    $endtime = ini_get('max_execution_time');
    if ($endtime != 0) {
        $endtime += floor($timestart) - 5;
        //Make shure we will have a least 5 sek left.
    }
    $time_remain = 1;
    foreach ($posts as $post) {
        if (get_option('post_notification_debug') == 'yes') {
            echo '<hr />Sending post: ' . $post->id . '<br />';
        }
        //Find the categories
        if (get_option('db_version') < 6124) {
            $cats = $wpdb->get_results("SELECT category_id FROM " . $wpdb->post2cat . " WHERE post_id = " . $post->id);
        } else {
            $cats = $wpdb->get_results("SELECT term_id \r\n\t\t\t\t\t\t\t\t\t\tFROM " . $wpdb->term_relationships . " \r\n\t\t\t\t\t\t\t\t\t\tJOIN " . $wpdb->term_taxonomy . " USING (term_taxonomy_id)\r\n\t\t\t\t\t\t\t\t\t\tWHERE taxonomy = 'category' AND object_id = " . $post->id);
        }
        $cat_ids = array();
        foreach ($cats as $cat) {
            if (get_option('db_version') < 6124) {
                $last_cat = $cat->category_id;
            } else {
                $last_cat = $cat->term_id;
            }
            while ($last_cat != 0) {
                $cat_ids[] = (string) $last_cat;
                if (get_option('db_version') < 6124) {
                    $last_cat = $wpdb->get_var("SELECT category_parent FROM " . $wpdb->categories . " WHERE cat_ID = {$last_cat}");
                } else {
                    $last_cat = $wpdb->get_var("SELECT parent FROM " . $wpdb->term_taxonomy . " \r\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE term_id = {$last_cat} AND taxonomy = 'category'");
                }
            }
        }
        $cat_ids[] = (string) $last_cat;
        $cat_ids = implode(", ", $cat_ids);
        //convert to string
        if (get_option('post_notification_debug') == 'yes') {
            echo 'The cat-Ids are: ' . $cat_ids . '<br />';
        }
        // Get the Emailadds
        $emails = $wpdb->get_results(" SELECT e.email_addr, e.id, e.act_code" . " FROM {$t_emails} e, {$t_cats} c " . " WHERE c.cat_id IN ({$cat_ids}) AND c.id = e.id AND e.gets_mail = 1 AND e.id >= " . $post->notification_sent . " GROUP BY e.id " . " ORDER BY e.id ASC");
        //We need this. Otherwise we can't be shure whether we already have sent mail.
        if (get_option('post_notification_debug') == 'yes') {
            echo count($emails) . ' Emails were found.<br />';
        }
        if ($emails) {
            //Check wheater ther are any mails to send anything.
            //Get Data
            $maildata = post_notification_create_email($post->id);
            foreach ($emails as $email) {
                if (get_option('post_notification_debug') == 'yes') {
                    echo 'Sending mail to: ' . $email->email_addr . '<br />';
                }
                if ($endtime != 0) {
                    //if this is 0 we have as much time as we want.
                    $time_remain = $endtime - time();
                }
                if ($maxsend < 1 || $time_remain < 0) {
                    //Are we allowed to send any more mails?
                    $mailssent = $email->id;
                    //Save where we stoped
                    break;
                }
                post_notification_sendmail($maildata, $email->email_addr, $email->act_code);
                $maxsend--;
            }
        }
        // Update notification_sent to -1 (We're done)
        $wpdb->query(" UPDATE {$t_posts} " . " SET " . " notification_sent = " . $mailssent . " WHERE post_id = " . $post->id);
        if ($maxsend < 1) {
            break;
        }
        //We dont need to go on, if there's nothing to send.
    }
    update_option('post_notification_lastsend', time());
    post_notification_set_next_send();
    if (get_option('post_notification_lock') == 'db') {
        $wpdb->query("SELECT RELEASE_LOCK('" . $wpdb->prefix . 'post_notification_lock' . "')");
    } else {
        flock($mutex, LOCK_UN);
        fclose($mutex);
    }
}
Beispiel #2
0
function post_notification_admin_sub()
{
    global $wpdb;
    require_once POST_NOTIFICATION_PATH . 'sendmail.php';
    echo '<h3>' . __('Test', 'post_notification') . '</h3>';
    ?>

<form id="test" method="post" action="admin.php?page=post_notification/admin.php&amp;action=test">
<table width="100%">
	
	<tr class="alternate">
		<th style="text-align:right;padding-right:10px;"><?php 
    _e('Post id:', 'post_notification');
    ?>
</th>
		<td>
			<input name="pid" type="text" size="35" value="<?php 
    echo $_POST['pid'];
    ?>
" />
		</td>
	</tr>
	<tr class="alternate">
		<td />
		<td>
			<?php 
    _e('This must be the ID of the post you want to send. You can find the ID under Manage->Posts.', 'post_notification');
    ?>
		</td>
		
	</tr>
<?php 
    if (($email = $_POST['email']) == '') {
        $email = get_option('post_notification_from_email');
    }
    ?>
	<tr class="alternate">
		<th style="text-align:right;padding-right:10px;"><?php 
    _e('Email:', 'post_notification');
    ?>
</th>
		<td>
			<input name="email" type="text" size="35" value="<?php 
    echo $email;
    ?>
" />
		</td>
	</tr>
<?php 
    ///Find templates
    if (($template = $_POST['template']) == '') {
        $template = get_option('post_notification_template');
    }
    $dir_handle = opendir(post_notification_get_profile_dir());
    while (false !== ($file = readdir($dir_handle))) {
        if (substr($file, -5) == '.html' or substr($file, -4) == '.txt') {
            $en_templates .= "<option value=\"{$file}\" ";
            if ($file == $template) {
                $en_templates .= ' selected="selected"';
            }
            $en_templates .= ">{$file}</option>";
        }
    }
    closedir($dir_handle);
    ?>
	<tr class="alternate">
		<th style="text-align:right;padding-right:10px;"><?php 
    _e('Template:', 'post_notification');
    ?>
</th>
		<td>
	        <select name="template" >
				<?php 
    echo $en_templates;
    ?>
	        </select>	
		</td>
	</tr>
	<tr class="alternate">
		<th style="text-align:right;padding-right:10px;"><?php 
    _e('Do not send mail:', 'post_notification');
    ?>
</th>
		<td>
	        <input type="checkbox" name="nosend" value="true"
	        	<?php 
    if ($_POST['nosend'] == 'true') {
        echo ' checked="checked" ';
    }
    ?>
 
	        />	
		</td>
	</tr>
	
	
	<tr class="alternate">
		<td>&nbsp;</td>
		<td><input type="submit" name="updateSettings" value="<?php 
    _e('Send test mail.', 'post_notification');
    ?>
" /></td>
	</tr>

</table>
</form>
<?php 
    //send of email
    if (isset($_POST['email']) && isset($_POST['pid'])) {
        echo '<h3>' . __('Email', 'post_notification') . '</h3>';
        $t_emails = $wpdb->prefix . 'post_notification_emails';
        $emails = $wpdb->get_results(" SELECT e.email_addr, e.id, e.act_code" . " FROM {$t_emails} e" . " WHERE e.email_addr = '" . $_POST['email'] . "'");
        //We need this. Otherwise we can't be sure whether we already have sent mail.
        if (!$emails) {
            echo '<div class="error"> ' . __('Error:', 'post_notification') . '  ' . __('Email has to be in the database.', 'post_notification') . '</div>';
        } else {
            $email = $emails[0];
            if (file_exists(POST_NOTIFICATION_PATH . 'userfunctions.php')) {
                include_once POST_NOTIFICATION_PATH . 'userfunctions.php';
            }
            $GLOBALS['wp_query']->init_query_flags();
            $maildata = post_notification_create_email($_POST['pid'], $_POST['template']);
            $send = $_POST['nosend'] == 'true' ? false : true;
            $maildata = post_notification_sendmail($maildata, $email->email_addr, '', $send);
            //returns the modified body.
            if ($maildata['sent'] == false) {
                global $phpmailer;
                echo '<br/><b>The mail has not been sent!</b><br/>';
                echo 'PHP-Mailer - Dump: <br/><pre>';
                var_dump($phpmailer);
                echo '</pre>';
            }
            echo '<b>Header:</b><BR><pre>';
            echo $maildata['header'];
            echo '</pre><br /> <b>Subject:</b><pre>' . $maildata['subject'] . '</pre> </br>';
            echo '<b>Body:</b><BR><pre>';
            echo htmlspecialchars($maildata['body']);
            echo '</pre><br/>';
        }
    }
}