#!/usr/bin/php loadHTML($html); $xpath = new DOMXPath($doc); // see: https://dzone.com/articles/php-don%E2%80%99t-call-destructor $doc = NULL; // destroy doc return $xpath; } $xpath = curl_everyoneprint_req(); // everyoneprint geeft twee verschillende resultaten bij // het aanroepen van de hoofdurl: // - een loginpagina, herkenbaar aan form met method login.cfm // - een overzichtspagine, herkenbaar aan form method webprint.cfm $form = $xpath->query('//form')->item(0); if ($form->getAttribute('action') == 'login.cfm') { $action = $form->getAttribute('action'); $method = $form->getAttribute('method'); dom_form_set_input($form, $xpath, 'Username', $username); dom_form_set_input($form, $xpath, 'Password', $password); $postfields = dom_form($form, $xpath); //curl_safe('setopt', CURLOPT_POST, true); //curl_safe('setopt', CURLOPT_URL, $url.$action); //curl_safe('setopt', CURLOPT_POSTFIELDS, dom_form($form, $xpath)); $xpath = NULL; //destruct object $xpath = curl_everyoneprint_req($action, $postfields); $form = $xpath->query('//form')->item(0); if ($form->getAttribute('action') == 'login.cfm') fatal('inloggen mislukt, gebruikersnaam/wachtwoord onjuist? website veranderd?'); } if ($form->getAttribute('action') != 'webprint.cfm') fatal('website gevonden met eerste form != webprint.cfm ('.$form->getAttribute('action').'), API veranderd'); function read_table($xpath) { $table = $xpath->query('//table[@class="tablelist"]/tr'); $legend = array(); $info = array(); $ignore = 0; foreach ($table as $row) { if (count($legend) == 0) { $ths = $xpath->query('th', $row); foreach ($ths as $idx => $th) if ($th->textContent != ' ') $legend[$idx] = $th->textContent; } else { if ($ignore) { //echo("info_idx=$info_idx ignore=$ignore\n"); if ($ignore == 2) { $tds = $xpath->query('td', $row); $info[$info_idx]['forms'] = $tds->item(2); } $ignore--; continue; } $tds = $xpath->query('td', $row); $info_row = array(); foreach ($tds as $idx => $td) { if (isset($legend[$idx])) $info_row[$legend[$idx]] = $td->textContent; } if ($info_row['Status'] == 'Wachten op vrijgeven') { $ignore = 2; $info_idx = count($info); } $info[] = $info_row; } } return $info; } switch ($command) { case 'list': $info = read_table($xpath); foreach ($info as $row) { echo($row['Bestandsnaam'].' '.$row['Status']."\n"); } break; case 'upload': $uploadname = $sha64.'.'.microtime(true).'.pdf'; $postfields = dom_form($form, $xpath); $file_upload = $xpath->query('.//input[@name="FileToPrint" and @type="file" and not(@disabled)]', $form)->item(0); if (!$file_upload) fatal("file upload form element not found"); $postfields['FileToPrint'] = new CurlFile($filename, $mime, $uploadname); $xpath = curl_everyoneprint_req('webprint.cfm', $postfields); $counter = 0; do { $info = read_table($xpath); $status = ''; foreach ($info as $row) { if ($row['Bestandsnaam'] == $uploadname) { $status = $row['Status']; break; } } if ($status == '') fatal("uploaded file not to be found on website"); //echo($status."\n"); if ($status == 'Wachten op vrijgeven') break; if ($counter++ > 10) fatal('timeout waiting on processing'); sleep(3); $xpath = curl_everyoneprint_req(); } while (true); //print_r($row); $forms = $xpath->query('.//form', $row['forms']); $button = NULL; foreach ($forms as $idx => $form) { $button = $xpath->query('.//input[@type="button" and @value="'.$default_printer.'"]', $form)->item(0); if ($button) break; //print_r($button); //if ($button) //echo("idx=$idx ".$button->item(0)->getAttribute('value')."\n"); } if (!$button) fatal("print button for default printer not found"); // don't send button, it seems to confuse the website $button->setAttribute('name', ''); dom_form_set_input($form, $xpath, 'NumberOfCopies', $copies); switch ($duplex) { case 'SINGLE': dom_form_check_radio($form, $xpath, 'Duplex', "1"); break; case 'DUPLEX_LONG': dom_form_check_radio($form, $xpath, 'Duplex', "2"); break; case 'DUPLEX_SHORT': dom_form_check_radio($form, $xpath, 'Duplex', "3"); break; default; fatal("impossible"); } $postfields = dom_form($form, $xpath); $postfields['method'] = 'printjob'; print_r($postfields); $xpath = curl_everyoneprint_req('afunctions.cfm', $postfields); break; default: fatal("command $command not implemented"); } ?>