Xataface Email Module  0.3.2
Email/Mailmerge Module for Xataface
 All Data Structures Files Functions Variables Pages
email_cron_job.php
Go to the documentation of this file.
1 <?php
2 
4  const JOB_IN_PROGRESS = 201;
5  const JOB_NOT_FOUND = 404;
6  const JOB_COMPLETE = 200;
7  const JOB_CANCELLED = 301;
8 
9  private $mutex;
10  function handle($params){
11  session_write_close();
12  ignore_user_abort(true);
13  set_time_limit(0);
14  $app = Dataface_Application::getInstance();
15  $app->_conf['nocache'] = 1;
16  $query = $app->getQuery();
17 
18  if ( !@$query['--job-id'] ){
19  throw new Exception("No job id provided");
20  }
21 
22  $res = $this->performJob($query['--job-id']);
23 
24  switch ($res){
25 
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);
29  if ( !$row ){
30  // No progress listed
31  throw new Exception("The job could not be found", self::JOB_NOT_FOUND);
32  }
33  $row['start_time'] = strftime('%c', $row['start_time']);
34  $out = array(
35  'code' => self::JOB_IN_PROGRESS,
36  'message' => 'Job in progress',
37  'data' => $row
38  );
39 
40  $this->out($out);
41  break;
42 
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);
46  if ( !$row ){
47  // No progress listed
48  throw new Exception("The job could not be found", self::JOB_NOT_FOUND);
49  }
50  $row['start_time'] = strftime('%c', $row['start_time']);
51  $row['end_time'] = strftime('%c', $row['end_time']);
52  $out = array(
53  'code' => self::JOB_COMPLETE,
54  'message' => 'Job complete',
55  'data' => $row
56  );
57 
58  $this->out($out);
59  break;
60 
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);
64  if ( !$row ){
65  // No progress listed
66  throw new Exception("The job could not be found", self::JOB_NOT_FOUND);
67  }
68  $row['start_time'] = strftime('%c', $row['start_time']);
69  $out = array(
70  'code' => self::JOB_CANCELLED,
71  'message' => 'Job paused',
72  'data' => $row
73  );
74 
75  $this->out($out);
76  break;
77 
78 
79 
80  default:
81  $out = array(
82  'code' => $res,
83  'message' => 'An error occurred trying to perform job.'
84  );
85  $this->out($out);
86  break;
87  }
88 
89 
90 
91  }
92 
93 
94  function performJob($jobId){
95  if (!$this->mutex('email_cron_job_'.basename($jobId)) ){
96  return self::JOB_IN_PROGRESS;
97  }
98  require_once dirname(__FILE__).'/email.php';
99  $action = new actions_email;
100 
101  $res = df_q("select * from xataface__email_jobs where job_id='".addslashes($jobId)."'");
102 
103  $row = mysql_fetch_assoc($res);
104  if ( !$row ){
105  return self::JOB_NOT_FOUND;
106  }
107 
108  if ( $row['complete'] ){
109  return self::JOB_COMPLETE;
110  }
111 
112  if ( $row['cancelled'] ){
113  return self::JOB_CANCELLED;
114  }
115 
116  //echo "\nSending mail for job $row[job_id] ...";
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']);
119 
120  // check to see if all the messages for this job have been sent yet
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);
125  if ( $num==0 ){
126 
127  $res2 = df_q("update xataface__email_jobs set active=0, complete=1, end_time='".addslashes(time())."' where job_id='".addslashes($jobId)."'", df_db());
128 
129  return self::JOB_COMPLETE;
130  //echo "\nJob $row[job_id] is complete! Deleting job...";
131  //$res2 = mysql_query("delete from dataface__email_jobs where job_id='".addslashes($row['job_id'])."' limit 1", df_db());
132  //if ( !$res2 ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
133  } else {
134  //echo "\nAfter sending mail for job $row[job_id], there are still $num messages left to send.";
135  $res = df_q("select * from xataface__email_jobs where job_id='".addslashes($jobId)."'");
136 
137  $row = mysql_fetch_assoc($res);
138  if ( !$row ){
139  return self::JOB_NOT_FOUND;
140  }
141 
142  if ( $row['cancelled'] ){
143  return self::JOB_CANCELLED;
144  } else {
145 
146  return self::JOB_IN_PROGRESS;
147  }
148  }
149 
150  }
151 
152 
153 
162  function mutex($name){
163 
164  $path = sys_get_temp_dir().'/'.$name.'.mutex';
165  //echo $path;
166  $this->mutex = fopen($path, 'w');
167  if ( flock($this->mutex, LOCK_EX | LOCK_NB) ){
168  register_shutdown_function(array($this,'clear_mutex'));
169  return true;
170  } else {
171  return false;
172  }
173 
174  }
175 
179  function clear_mutex(){
180 
181  if ( $this->mutex ){
182  fclose($this->mutex);
183  }
184  }
185 
186 
187  function out($out){
188  header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"');
189  echo json_encode($out);
190  }
191 
192 }