11 session_write_close();
12 ignore_user_abort(
true);
14 $app = Dataface_Application::getInstance();
15 $app->_conf[
'nocache'] = 1;
16 $query = $app->getQuery();
18 if ( !@$query[
'--job-id'] ){
19 throw new Exception(
"No job id provided");
26 case self::JOB_IN_PROGRESS:
27 $res2 = df_q(
"select * from xataface__email_jobs where job_id='".addslashes($query[
'--job-id']).
"'");
28 $row = mysql_fetch_assoc($res2);
31 throw new Exception(
"The job could not be found", self::JOB_NOT_FOUND);
33 $row[
'start_time'] = strftime(
'%c', $row[
'start_time']);
35 'code' => self::JOB_IN_PROGRESS,
36 'message' =>
'Job in progress',
43 case self::JOB_COMPLETE:
44 $res2 = df_q(
"select * from xataface__email_jobs where job_id='".addslashes($query[
'--job-id']).
"'");
45 $row = mysql_fetch_assoc($res2);
48 throw new Exception(
"The job could not be found", self::JOB_NOT_FOUND);
50 $row[
'start_time'] = strftime(
'%c', $row[
'start_time']);
51 $row[
'end_time'] = strftime(
'%c', $row[
'end_time']);
53 'code' => self::JOB_COMPLETE,
54 'message' =>
'Job complete',
61 case self::JOB_CANCELLED:
62 $res2 = df_q(
"select * from xataface__email_jobs where job_id='".addslashes($query[
'--job-id']).
"'");
63 $row = mysql_fetch_assoc($res2);
66 throw new Exception(
"The job could not be found", self::JOB_NOT_FOUND);
68 $row[
'start_time'] = strftime(
'%c', $row[
'start_time']);
70 'code' => self::JOB_CANCELLED,
71 'message' =>
'Job paused',
83 'message' =>
'An error occurred trying to perform job.'
95 if (!$this->
mutex(
'email_cron_job_'.basename($jobId)) ){
98 require_once dirname(__FILE__).
'/email.php';
101 $res = df_q(
"select * from xataface__email_jobs where job_id='".addslashes($jobId).
"'");
103 $row = mysql_fetch_assoc($res);
108 if ( $row[
'complete'] ){
112 if ( $row[
'cancelled'] ){
117 $res2 = mysql_query(
"delete from `".$row[
'join_table'].
"` where recipient_email='' and messageid='".addslashes($row[
'email_id']).
"'", df_db());
118 $action->sendMail($row[
'email_id'],$row[
'email_table'],$row[
'join_table'],$row[
'recipients_table'],$row[
'email_column']);
121 $res2 = mysql_query(
"select count(*) from `".$row[
'join_table'].
"` where sent<>1 and messageid='".addslashes($row[
'email_id']).
"'", df_db());
122 if ( !$res2 ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
123 list($num)=mysql_fetch_row($res2);
124 @mysql_free_result($res2);
127 $res2 = df_q(
"update xataface__email_jobs set active=0, complete=1, end_time='".addslashes(time()).
"' where job_id='".addslashes($jobId).
"'", df_db());
135 $res = df_q(
"select * from xataface__email_jobs where job_id='".addslashes($jobId).
"'");
137 $row = mysql_fetch_assoc($res);
142 if ( $row[
'cancelled'] ){
164 $path = sys_get_temp_dir().
'/'.$name.
'.mutex';
166 $this->
mutex = fopen($path,
'w');
167 if ( flock($this->
mutex, LOCK_EX | LOCK_NB) ){
168 register_shutdown_function(array($this,
'clear_mutex'));
182 fclose($this->
mutex);
188 header(
'Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf[
'oe'].
'"');
189 echo json_encode($out);