|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * This file implements a facade class for using the AdLib webservice |
| 6 | + */ |
| 7 | + |
| 8 | +define('ADLIB_JOBTYPE_PDF', 0); |
| 9 | +define('ADLIB_JOBTYPE_HTML', 1); |
| 10 | +define('ADLIB_DISABLED', $_SERVER['SERVER_ADDR'] != '10.255.1.46'); |
| 11 | +//define('ADLIB_DISABLED', TRUE); |
| 12 | + |
| 13 | +/** |
| 14 | + * Description of AdLibWS |
| 15 | + * |
| 16 | + * @author jm |
| 17 | + */ |
| 18 | +class AdLibWS { |
| 19 | + |
| 20 | + protected $client; |
| 21 | + // Private static $base_path = 'd:\\wwwroot\\'; |
| 22 | + protected static $basePath = 'C:\\InetPub\\wwwroot\\'; |
| 23 | + protected static $baseUrl = 'http://10.1.1.180/'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Heuristic to validate url availability. |
| 27 | + */ |
| 28 | + private function checkUrl($url) { |
| 29 | + $handle = curl_init($url); |
| 30 | + curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); |
| 31 | + curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 2); |
| 32 | + curl_exec($handle); |
| 33 | + $http_code = curl_getinfo($handle, CURLINFO_HTTP_CODE); |
| 34 | + curl_close($handle); |
| 35 | + // Since this is just a heuristic, we allow access denied (401) for further check. |
| 36 | + return in_array($http_code, array(200, 401)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Initializes connector. |
| 41 | + */ |
| 42 | + public function __construct() { |
| 43 | + if (ADLIB_DISABLED) { |
| 44 | + // error_log('MARK - ' . basename(__FILE__) . ':' . __LINE__ . ' - AdLib presently disabled.'); |
| 45 | + return; |
| 46 | + } |
| 47 | + if (!isset($this->client)) { |
| 48 | + $options = array( |
| 49 | + 'soapversion' => SOAP_1_2, |
| 50 | + 'trace' => TRUE, |
| 51 | + ); |
| 52 | + if (variable_get('os2web_pws_proxy', FALSE)) { |
| 53 | + $options['proxy_host'] = variable_get('os2web_pws_proxy_host'); |
| 54 | + $options['proxy_port'] = variable_get('os2web_pws_proxy_port'); |
| 55 | + } |
| 56 | + $url = variable_get('os2web_adlib_url', FALSE); |
| 57 | + if ($this->checkUrl($url)) { |
| 58 | + if (function_exists('xdebug_disable')) { |
| 59 | + xdebug_disable(); |
| 60 | + } |
| 61 | + try { |
| 62 | + if (FALSE !== $url) { |
| 63 | + $this->client = new SoapClient($url, $options); |
| 64 | + } |
| 65 | + } catch (Exception $exc) { |
| 66 | + watchdog('adlib_api', 'Error creating AdLib client, message: %var', array('%var' => $exc->getMessage()), WATCHDOG_ERROR); |
| 67 | + } |
| 68 | + if (function_exists('xdebug_enable')) { |
| 69 | + xdebug_enable(); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Starts a job on the adlib server. |
| 77 | + * |
| 78 | + * @param array $file_info |
| 79 | + * Fileinfo structure |
| 80 | + */ |
| 81 | + public function addJob($file_info, $type = ADLIB_JOBTYPE_PDF, $priority = 3) { |
| 82 | + if (ADLIB_DISABLED) { |
| 83 | + return FALSE; |
| 84 | + } |
| 85 | + if (!isset($this->client)) { |
| 86 | + return FALSE; |
| 87 | + } |
| 88 | + $job_info = '<?xml version="1.0" encoding="UTF-8"?> |
| 89 | +<JobInfo> |
| 90 | + <JobSettings JobID="" UserJobID="" JobPriority="!priority" AdlibServer="" AdlibServerGroup=""/> |
| 91 | + <JobFileMsg Count=""/> |
| 92 | + <JobFileList> |
| 93 | + <JobFile Filename="" Folder=""/> |
| 94 | + </JobFileList> |
| 95 | +</JobInfo>'; |
| 96 | + $job_info = format_string($job_info, array('!priority' => $priority)); |
| 97 | + $job_ticket = '<?xml version="1.0" encoding="UTF-8"?> |
| 98 | +<?AdlibExpress applanguage="USA" appversion="2.5" dtdversion="1.0" ?> |
| 99 | +<!DOCTYPE JOBS SYSTEM "C:\Adlib Express\DTD\AdlibExpress.dtd"> |
| 100 | +<JOBS xmlns:JOBS="http://www.Adlibsoftware.com" xmlns:JOB="http://www.Adlibsoftware.com"> |
| 101 | + <JOB> |
| 102 | + <JOB:DOCINPUTS> |
| 103 | + <JOB:DOCINPUT FILENAME="!infile" FOLDER="!infolder" /> |
| 104 | + </JOB:DOCINPUTS> |
| 105 | + <JOB:DOCOUTPUTS> |
| 106 | + <JOB:DOCOUTPUT FILENAME="!outfile" FOLDER="!outfolder" DOCTYPE="!jobtype" /> |
| 107 | + </JOB:DOCOUTPUTS> |
| 108 | + <JOB:SETTINGS> |
| 109 | + <JOB:HEADER ENABLED="Yes" TEXTRIGHT="&[Page] of &[Pages]" LAYER="Foreground" /> |
| 110 | + <JOB:NATIVEAPPSETTINGS> |
| 111 | + <JOB:MSOUTLOOK CONVERSIONMODE="IFTS" /> |
| 112 | + </JOB:NATIVEAPPSETTINGS> |
| 113 | + </JOB:SETTINGS> |
| 114 | + </JOB> |
| 115 | +</JOBS>'; |
| 116 | + $job_ticket = format_string($job_ticket, array( |
| 117 | + '!infile' => $this->safeFilename($file_info['filename']), |
| 118 | + '!infolder' => self::$basePath . $file_info['in_folder'], |
| 119 | + '!outfile' => $this->safeFilename($file_info['out_filename']), |
| 120 | + '!outfolder' => self::$basePath . $file_info['out_folder'], |
| 121 | + '!jobtype' => ($type == ADLIB_JOBTYPE_HTML ? 'HTML' : 'PDF'), |
| 122 | + )); |
| 123 | +<<<<<<< HEAD |
| 124 | + $result = simplexml_load_string($this->client->addJob(array('jobInfo' => $job_info, 'jobTicket' => $job_ticket))->AddJobResult); |
| 125 | +======= |
| 126 | + $result = simplexml_load_string($this->client->addJob(array('job_info' => $job_info, 'job_ticket' => $job_ticket))->AddJobResult); |
| 127 | +>>>>>>> Prod_check module hooks |
| 128 | + if (is_object($result) && isset($result->JobSettings['JobID'])) { |
| 129 | + return $result; |
| 130 | + } |
| 131 | + else { |
| 132 | + $msg = (string) array_pop($result->xpath('//@Message')); |
| 133 | + if ($msg == '') { |
| 134 | + $msg = 'Unknown reason.'; |
| 135 | + } |
| 136 | + error_log(basename(__FILE__) . ':' . __LINE__ . " jm@bellcom.dk: Failed to add job. Reason: " . $msg); |
| 137 | + return FALSE; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Invokes deleteJob. |
| 143 | + * |
| 144 | + * @param string $id |
| 145 | + * Job id |
| 146 | + * |
| 147 | + * @return object |
| 148 | + * Result and status |
| 149 | + */ |
| 150 | + public function deleteJob($id) { |
| 151 | + if (ADLIB_DISABLED) { |
| 152 | + $return->status = 'Disabled'; |
| 153 | + $return->result = ''; |
| 154 | + return $return; |
| 155 | + } |
| 156 | + if (isset($id->JobSettings['JobID'])) { |
| 157 | + $id = $id->JobSettings['JobID']; |
| 158 | + } |
| 159 | + if (isset($this->client)) { |
| 160 | + $this->client->deleteJob(array('jobID' => $id, 'isUserJobID' => FALSE)); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Invokes deleteFolder. |
| 166 | + * |
| 167 | + * @param string $id |
| 168 | + * Folder id |
| 169 | + * |
| 170 | + * @return object |
| 171 | + * False on error |
| 172 | + */ |
| 173 | + public function deleteJobFolder($id) { |
| 174 | + if (ADLIB_DISABLED) { |
| 175 | + return FALSE; |
| 176 | + } |
| 177 | + if (isset($this->client)) { |
| 178 | + $reply = $this->client->deleteJobFolder(array('JobFolder' => $id)); |
| 179 | + return $reply; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Gets a list of connectors and their status. |
| 185 | + * |
| 186 | + * @return string |
| 187 | + * Connector status message |
| 188 | + */ |
| 189 | + public function getConnectorStatusList() { |
| 190 | + if (ADLIB_DISABLED) { |
| 191 | + return 'Adblib connection disabled.'; |
| 192 | + } |
| 193 | + if (isset($this->client)) { |
| 194 | + $result = $this->client->getConnectorStatusList()->GetConnectorStatusListResult; |
| 195 | + $result = simplexml_load_string($result); |
| 196 | + if (is_object($result)) { |
| 197 | + return $result->xpath('//Server'); |
| 198 | + } |
| 199 | + else { |
| 200 | + return 'Invalid response from AdLib.'; |
| 201 | + } |
| 202 | + } |
| 203 | + return 'AdLib connection failed.'; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Invokes getJobStatus. |
| 208 | + * |
| 209 | + * @param string $id |
| 210 | + * Job id |
| 211 | + * |
| 212 | + * @return object |
| 213 | + * Result and status |
| 214 | + */ |
| 215 | + public function getJobStatus($id) { |
| 216 | + if (ADLIB_DISABLED) { |
| 217 | + $return->status = 'Disabled'; |
| 218 | + $return->result = ''; |
| 219 | + return $return; |
| 220 | + } |
| 221 | + if (isset($id->JobSettings['JobID'])) { |
| 222 | + $id = $id->JobSettings['JobID']; |
| 223 | + } |
| 224 | + if (isset($this->client)) { |
| 225 | + $result = $this->client->getJobStatus(array('jobID' => $id, 'isUserJobID' => FALSE))->GetJobStatusResult; |
| 226 | + $return = (object) array(); |
| 227 | + if (strncmp('<', $result, 1) === 0) { |
| 228 | + $return->status = (string) simplexml_load_string($result)->CurrentJobStatus['Status']; |
| 229 | + $return->result = simplexml_load_string($result); |
| 230 | + } |
| 231 | + else { |
| 232 | + $return->status = 'Not found'; |
| 233 | + $return->result = $result; |
| 234 | + } |
| 235 | + } |
| 236 | + else { |
| 237 | + $return->status = 'Not found'; |
| 238 | + } |
| 239 | + if ($return->status === '') { |
| 240 | + $message = (string) $return->result->ServerInfo[0]->Error['Message']; |
| 241 | + error_log(basename(__FILE__) . ':' . __LINE__ . ' AdLib Error: ' . $message); |
| 242 | + $return->status = 'Not Available'; |
| 243 | + } |
| 244 | + elseif ($return->status === 'Pending') { |
| 245 | + // error_log("ConnectorStatus is " . $return->result->CurrentJobStatus['ConnectorStatus']); |
| 246 | + } |
| 247 | + else { |
| 248 | + // error_log(basename(__FILE__) . ':' . __LINE__ . " jm@bellcom.dk: Looked up status for job: " . $id . " Status: " . $return->status); |
| 249 | + } |
| 250 | + return $return; |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Invokes addJobFolder. |
| 255 | + * |
| 256 | + * @return string |
| 257 | + * Adlib folder |
| 258 | + */ |
| 259 | + public function addJobFolder() { |
| 260 | + if (ADLIB_DISABLED) { |
| 261 | + return FALSE; |
| 262 | + } |
| 263 | + if ($this->client) { |
| 264 | + $reply = simplexml_load_string($this->client->addJobFolder()->AddJobFolderResult); |
| 265 | + $folder_info = $reply; |
| 266 | + $result = 'ExponentJobs\\' . ((string) $folder_info['FolderID']); |
| 267 | + watchdog('adlib_api', 'Added a folder: %folder', array('%folder' => $result), WATCHDOG_DEBUG); |
| 268 | + return $result; |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * This method uploads a file to AdLib server. |
| 274 | + * |
| 275 | + * @param string $filename |
| 276 | + * Filename |
| 277 | + * @param mixed $data |
| 278 | + * Binary contents of data |
| 279 | + * @param string $folder |
| 280 | + * Optional. Adlib folder, if ommited, new folder is created. |
| 281 | + * |
| 282 | + * @return mixed |
| 283 | + * False if call failed, otherwise a fileinfo structure |
| 284 | + */ |
| 285 | + public function uploadData($filename, $data, $type = ADLIB_JOBTYPE_PDF, $folder = NULL) { |
| 286 | + if (ADLIB_DISABLED) { |
| 287 | + return FALSE; |
| 288 | + } |
| 289 | + if (!isset($this->client)) { |
| 290 | + return FALSE; |
| 291 | + } |
| 292 | + switch ($type) { |
| 293 | + case ADLIB_JOBTYPE_HTML: |
| 294 | + $jobtype = 'HTML'; |
| 295 | + $jobext = 'htm'; |
| 296 | + break; |
| 297 | + |
| 298 | + default: |
| 299 | + $jobtype = 'PDF'; |
| 300 | + $jobext = 'pdf'; |
| 301 | + break; |
| 302 | + } |
| 303 | + if (!isset($folder)) { |
| 304 | + $folder = $this->addJobFolder(); |
| 305 | + } |
| 306 | + if (!$folder) { |
| 307 | + error_log('MARK - ' . basename(__FILE__) . ':' . __LINE__ . ' in ' . __FUNCTION__ . '() - Adding folder failed.'); |
| 308 | + return FALSE; |
| 309 | + } |
| 310 | + $payload = ' |
| 311 | + <JobFiles> |
| 312 | + <JobFile FileName="' . $this->safeFilename($filename) . '" Data="' . base64_encode($data) . '" Length="' . strlen($data) . '" /> |
| 313 | + </JobFiles>'; |
| 314 | + $result = $this->client->AddJobFilesAsStream(array( |
| 315 | + 'JobFolder' => $folder . '\\Input', |
| 316 | + 'XmlStream' => $payload, |
| 317 | + ))->AddJobFilesAsStreamResult; |
| 318 | + $status = (string) simplexml_load_string($result)->System['Status']; |
| 319 | + if (strcmp($status, 'Success') == 0) { |
| 320 | + error_log(basename(__FILE__) . ':' . __LINE__ . " jm@bellcom.dk: Uploaded file: " . $filename); |
| 321 | + return array( |
| 322 | + 'basefolder' => $folder, |
| 323 | + 'in_folder' => $folder . '\\Input', |
| 324 | + 'out_folder' => $folder . '\\Output', |
| 325 | + 'out_url' => self::$baseUrl . str_replace('\\', '/', $folder . '/Output/') . rawurlencode($this->safeFilename(pathinfo($filename, PATHINFO_FILENAME) . '.' . $jobext)), |
| 326 | + 'filename' => $filename, |
| 327 | + 'job_type' => $jobtype, |
| 328 | + 'out_filename' => pathinfo($filename, PATHINFO_FILENAME) . '.' . $jobext, |
| 329 | + ); |
| 330 | + } |
| 331 | + else { |
| 332 | + error_log(basename(__FILE__) . ':' . __LINE__ . " jm@bellcom.dk: Failed to upload: " . $filename); |
| 333 | + watchdog('adlib_api', 'Upload of %filename failed with message: %msg', array('%file' => $filename, '%msg' => $result), WATCHDOG_ERROR); |
| 334 | + $folder = explode('\\', $folder); |
| 335 | + $this->deleteJobFolder($folder[1]); |
| 336 | + return FALSE; |
| 337 | + } |
| 338 | + } |
| 339 | + |
| 340 | + /** |
| 341 | + * Encrypts filename, making is safe for use on AdLib. |
| 342 | + * |
| 343 | + * @param string $fname |
| 344 | + * File name |
| 345 | + * |
| 346 | + * @return string |
| 347 | + * Encrypted file name |
| 348 | + */ |
| 349 | + protected function safeFilename($fname) { |
| 350 | + $pi = pathinfo($fname); |
| 351 | + $safe_name = md5($pi['filename']); |
| 352 | + if (isset($pi['extension'])) { |
| 353 | + $safe_name .= '.' . $pi['extension']; |
| 354 | + } |
| 355 | + return $safe_name; |
| 356 | + } |
| 357 | + |
| 358 | +} |
0 commit comments