function _pugpig_show_batch($rows, $file_warning_size_kb = 250, $file_warning_time_secs = 10)
{
    echo "<table style='font-size:small;''>\n";
    echo "<tr>";
    echo "<th colspan='5'>Metrics</th>";
    echo "<th colspan='2'>Cache Headers</th>";
    echo "<th colspan='4'>Edge</th>";
    echo "<th></th>";
    echo "</tr>";
    echo "<tr>";
    echo "<th>HTTP</th>";
    echo "<th>Time</th>";
    echo "<th>Type</th>";
    echo "<th>Size</th>";
    echo "<th>Valid</th>";
    echo "<th>Modified</th>";
    echo "<th>Expires</th>";
    echo "<th>Server</th>";
    echo "<th>Encoding</th>";
    echo "<th>Auth</th>";
    echo "<th>Status</th>";
    echo "<th>URL</th>";
    echo "</tr>";
    foreach ($rows as $key => $vals) {
        echo "<tr>";
        $vals['h'] = _getHeadersFromString($vals['headers']);
        if (isset($vals['curl_info'])) {
            $http_code = $vals['curl_info']['http_code'];
            $http_code_style = '';
            if ($http_code != 200) {
                $http_code_style = 'background:#ff6600';
            }
            echo "<td style='{$http_code_style}'>{$http_code}</td>\n";
            $time = $vals['curl_info']['total_time'];
            $percentage = $time / $file_warning_time_secs * 100;
            $barcolor = "lightblue";
            if ($percentage > 100) {
                $barcolor = "orange";
            }
            if ($percentage > 200) {
                $barcolor = "red";
            }
            $time_style = "white-space: nowrap; background: -webkit-gradient(linear, left top,right top, color-stop({$percentage}%,{$barcolor}), color-stop({$percentage}%,white))";
            echo "<td style='{$time_style}'>" . $vals['curl_info']['total_time'] . "</td>\n";
        } else {
            echo "<td> - </td>\n";
            echo "<td> - </td>\n";
        }
        $content_type = "";
        $content_type_style = "";
        if (isset($vals['h']['content-type'])) {
            $content_type = $vals['h']['content-type'];
        } else {
            $content_type_style = 'background: pink';
        }
        $char = pugpig_get_download_char($key, $content_type);
        echo "<td style='{$content_type_style}'>" . $char . "</td>";
        $bytes = 0;
        if (file_exists($vals['file'])) {
            $bytes = filesize($vals['file']);
        }
        $percentage = $bytes / (1024 * $file_warning_size_kb) * 100;
        $barcolor = "lightblue";
        if ($percentage > 100) {
            $barcolor = "orange";
        }
        if ($percentage > 200) {
            $barcolor = "red";
        }
        $size_style = "white-space: nowrap; background: -webkit-gradient(linear, left top,right top, color-stop({$percentage}%,{$barcolor}), color-stop({$percentage}%,white))";
        echo "<td style='{$size_style}'>" . pugpig_bytestosize($bytes) . "</td>\n";
        $error = pugpig_validate_file($vals['file'], $content_type);
        if (!empty($error)) {
            echo "<td style='background: pink'>{$error}</td>\n";
        } else {
            echo "<td>Y</td>\n";
        }
        if (isset($vals['h']['last-modified'])) {
            echo "<td>" . _ago(strtotime($vals['h']['last-modified']), 0, true) . " ago</td>\n";
        } else {
            echo "<td style='background: pink'>?</td>\n";
        }
        if (isset($vals['h']['expires'])) {
            echo "<td>in " . _ago(strtotime($vals['h']['expires']), 0, false) . "</td>\n";
        } else {
            if (isset($vals['h']['etag'])) {
                echo "<td>eTag</td>\n";
            } else {
                echo "<td style='background: pink'>?</td>\n";
            }
        }
        $cache_layer = _pugpig_get_cache_layer($vals['h']);
        $cache_layer_style = "";
        if (in_array($cache_layer, array('PPITC', 'AKAMAI', 'CLOUDFRONT'))) {
            $cache_layer_style = 'background:#6ce1c4';
        } elseif (in_array($cache_layer, array('VARNISH'))) {
            $cache_layer_style = 'background:#66ccff';
        } elseif (in_array($cache_layer, array('PHP'))) {
            $cache_layer_style = 'background:#b20047';
        }
        echo "<td style='{$cache_layer_style}'>{$cache_layer}</td>\n";
        $content_encoding = "";
        $content_encoding_style = '';
        if (isset($vals['h']['content-encoding'])) {
            $content_encoding .= $vals['h']['content-encoding'] . " ";
        }
        if (isset($vals['h']['transfer-encoding'])) {
            $content_encoding .= $vals['h']['transfer-encoding'] . " ";
        }
        if (in_array($content_encoding, array('gzip'))) {
            $content_encoding_style = 'background:#6ce1c4';
        }
        echo "<td style='{$content_encoding_style}'>{$content_encoding}</td>\n";
        if (isset($vals['h']["x-pugpig-entitlement"])) {
            echo "<td>LOCKED</td>\n";
        } else {
            echo "<td>-</td>\n";
        }
        $status = "";
        if (isset($vals['h']["x-pugpig-status"])) {
            $status .= $vals['h']["x-pugpig-status"];
        }
        echo "<td>{$status}</td>\n";
        $suspect_style = pugpig_check_suspect_path($key) ? 'background:#ff6600' : ($char == 'f' ? 'background:#66ccff' : '');
        echo "<td style=' {$suspect_style}'><a title='" . $vals['headers'] . "' target='_blank' href='{$key}'>" . pugpig_strip_domain($key) . "</a></td>\n";
        echo "</tr>";
    }
    echo "</table>\n";
}
 function bytesToSize($bytes, $precision = 2, $type = 'long')
 {
     return pugpig_bytestosize($bytes, $precision = 2, $type = 'long');
 }
 function get_edition_data($edition_num, $is_package)
 {
     $title_parts = array();
     $title_owner_prefix = empty($_SERVER['PHP_AUTH_USER']) ? '' : $_SERVER['PHP_AUTH_USER'] . '\'s ';
     if (!empty($title_owner_prefix)) {
         $title_parts[] = $title_owner_prefix;
     }
     $num_editions = get_number_of_editions();
     $edition_key = get_edition_id($edition_num);
     $edition_date = add_date(time(), $edition_num);
     $modified = add_date_timestamp(time(), $edition_num);
     $cycle = $edition_num % 10 + 1;
     // http response
     $http_responses = array(200, 404, 401, 403, 500);
     $http_reponses_index = $num_editions - 10 - $edition_num;
     $http_status = 0;
     if ($http_reponses_index >= 0 && $http_reponses_index < count($http_responses)) {
         $http_status = $http_responses[$http_reponses_index];
     }
     $is_timeout = $http_reponses_index == count($http_responses);
     $is_broken = $is_timeout || $http_status > 0;
     if ($is_broken) {
         $title_parts[] = 'Broken Test';
     }
     // edition flags
     $is_published = $edition_num % 3;
     $is_paid = !$is_broken && $edition_num % 7;
     $is_vampire = $edition_num == $num_editions - 20;
     if ($is_broken) {
         $title_parts[] = 'Vampire';
     }
     $is_deleted = $is_vampire && rand(0, 1) == 1;
     // strings
     $price = $is_paid ? 'PAID' : 'FREE';
     $status = $is_published ? 'published' : 'draft';
     // url
     if ($http_status == 0) {
         if ($is_timeout) {
             $url = '../auth_test/responses/timeout.php';
         } else {
             $query = empty($_SERVER['QUERY_STRING']) ? '' : '?' . $_SERVER['QUERY_STRING'];
             $url = "edition/{$edition_key}/" . ($is_package ? 'package.xml' : 'content.xml') . $query;
         }
     } else {
         $url = "../auth_test/responses/{$http_status}.php";
         // todo: fix relative path, so we can find authtest from custom content
     }
     $http_status_summary = array(200 => 'All lies. I return a fake 200 code.', 404 => 'Not Found. I return a 404 code.', 401 => 'Unauthorized. I return a 401 code.', 403 => 'Forbidden. I return a 403 code.', 500 => 'Broken. I return a 500 code.');
     // summary
     $always_updates = $edition_num % 9 == 0;
     $long_ttl = $edition_num % 27 == 0;
     $summary = "You really should read edition {$edition_num}. It's the best {$status} ever. ";
     if ($is_published) {
         $summary .= "It is {$price}. ";
     } else {
         $summary .= "All draft editions are treated as free. ";
     }
     if ($always_updates) {
         $summary .= "And it ALWAYS updates. ";
     }
     if ($long_ttl) {
         $summary .= "I have a long 5 minute TTL on the Atom and Package feeds. ";
     }
     if ($is_vampire) {
         $summary .= "I get deleted and then raise from the dead. ";
     }
     if ($http_status > 0) {
         $summary = $http_status_summary[$http_status] . ' ' . $summary;
     }
     if ($is_timeout) {
         $summary = 'Timeout. I take 60 seconds.' . ' ' . $summary;
     }
     // categories
     $custom_categories = array();
     if ($is_package) {
         $size = 0;
         $parts = get_package_parts($edition_num);
         foreach ($parts as $part_name => $part_info) {
             $size += filesize($part_info['path']);
         }
         $custom_categories['download_size'] = pugpig_bytestosize($size, 0);
     }
     // pages
     $pages = array();
     $num_pages = get_number_of_pages($edition_num);
     for ($page_num = 1; $page_num <= $num_pages; $page_num++) {
         $pages[] = get_page_id($edition_num, $page_num);
     }
     $subtitles = array(null, ' ', null, ' ', "Edition {$edition_num} Sub Title", null, ' ', null, ' ', "This is the stupidly long subtitle for Edition {$edition_num} which isn't going to fit even on an iPad in portrait, especially since all this extra text was added to make this an even longer stupidly long subtitle.");
     $subtitle = $subtitles[$edition_num % count($subtitles)];
     if ($subtitle != null) {
         $custom_categories['subtitle'] = $subtitle;
     }
     // pane model
     $pane_models = array(null, 'scrolling', 'horizontal', 'vertical');
     $pane_model_granularity = 4;
     $pane_model = $pane_models[$edition_num / $pane_model_granularity % count($pane_models)];
     if ($pane_model === null) {
         $summary .= 'With no pane model.';
     } else {
         $custom_categories['pane_model'] = $pane_model;
         $summary .= 'With ' . ucwords($pane_model) . ' pane model.';
     }
     $title_parts[] = "Edition Number {$edition_num}";
     $title_parts = array_filter($title_parts);
     $title = join(' ', $title_parts);
     return array('key' => $edition_key, 'date' => $edition_date, 'title' => $title, 'modified' => !$always_updates ? $modified : time(), 'packaged' => !$always_updates ? $modified : time(), 'ttl' => $long_ttl ? 300 : 0, 'status' => $status, 'price' => $price, 'thumbnail' => "http://lorempixel.com/600/800/abstract/{$cycle}/Issue-{$edition_num}-{$edition_date}/", 'newsstand_cover_art_icon_source' => "http://dummyimage.com/768x1024/000000/ffffff.png?text=Newstand+Cover+768x1024+Issue+{$edition_num}/", 'summary' => $summary, 'tombstone' => $is_deleted, 'url_type' => $is_package ? 'application/pugpigpkg+xml' : 'application/atom+xml', 'url' => $url, 'newsstand_summary' => 'This is a longer summary that can appear in Newsstand. ' . $summary, 'page_ids' => $pages, 'custom_categories' => $custom_categories);
 }
 private function _processData($multiInfo)
 {
     $handleString = (string) $multiInfo['handle'];
     $this->info[$handleString]['multi'] = $multiInfo;
     $this->info[$handleString]['curl'] = curl_getinfo($multiInfo['handle']);
     $http_url = $this->info[$handleString]['url'];
     $http_code = $this->info[$handleString]['curl']['http_code'];
     $content_type = $this->info[$handleString]['curl']['content_type'];
     $content_length = $this->info[$handleString]['curl']['download_content_length'];
     $total_time = $this->info[$handleString]['curl']['total_time'];
     $starttransfer_time = $this->info[$handleString]['curl']['starttransfer_time'];
     $connect_time = $this->info[$handleString]['curl']['connect_time'];
     $cinfo = $this->info[$handleString]['curl'];
     // $request_header = $this->info[$multiInfo['handle']]['curl']['request_header'];
     // print_r($this->info[$multiInfo['handle']]['curl']);
     // Close the file we're downloading into
     fclose($this->filehandles[$http_url]);
     fclose($this->headerhandles[$http_url]);
     $name = $this->entries[$http_url];
     $file_exists = file_exists($name);
     $file_size = $file_exists ? filesize($name) : 0;
     if ($http_code != 200) {
         $this->failures[$http_url] = "HTTP Error after " . $total_time . " seconds: " . $http_code;
         if ($http_code == 0) {
             $this->failures[$http_url] .= " (possibly too many concurrent connections).";
         }
         unlink($name);
     } else {
         if (!$file_exists) {
             $this->failures[$http_url] = "Unable to save file after download. Maybe file name is too long?";
         } elseif ($file_size == 0) {
             // Delete it so that we have to download it again if the user refreshes
             unlink($name);
             $this->failures[$http_url] = "The file is {$file_size} bytes in length.";
         }
     }
     $char = pugpig_get_download_char($name, $content_type);
     $headerfile = $name . ".pugpigheaders";
     $curlopt_file_name = $name . ".pugpigcurlopt";
     file_put_contents($curlopt_file_name, serialize($cinfo));
     $headers = file_get_contents($headerfile);
     $headers_array = _getHeadersFromString($headers);
     _print_immediately('<a class="pass" href="' . $http_url . '" target="_blank" title="' . $http_url . "\n" . '[Response: ' . $http_code . ', Size: ' . $content_length . ' (' . pugpig_bytestosize($file_size) . '), Type: ' . $content_type . ', Time: ' . $total_time . ', TTFB: ' . $starttransfer_time . ', Connect Time: ' . $connect_time . ']' . "\n" . htmlspecialchars($headers) . ' ">' . $char . '</a>');
     $this->successes[$http_url]['file'] = $name;
     $this->successes[$http_url]['headers'] = $headers;
     $this->successes[$http_url]['curl_info'] = $cinfo;
     $this->successes[$http_url]['fetched'] = TRUE;
     $this->processedCount++;
     if ($this->processedCount % 100 == 0 || $this->processedCount == count($this->entries)) {
         _print_immediately("<br />");
     }
 }
function _package_edition_package_size($edition_tag, $precision = 2, $type = 'long')
{
    return pugpig_bytestosize(_package_edition_package_size_raw($edition_tag), $precision, $type);
}
function pugpig_ppitc_create_zip($edition_zip_root, $zip_prefix, $entries, $ts)
{
    $edition_zip_root .= DIRECTORY_SEPARATOR . date('Y\\' . DIRECTORY_SEPARATOR . 'm\\' . DIRECTORY_SEPARATOR . 'd', $ts) . DIRECTORY_SEPARATOR;
    $zip_path = pugpig_get_local_save_path($edition_zip_root, $zip_prefix . "-" . $ts . ".zip");
    echo "<font color='green'>ZIP {$zip_path}</font><br />";
    // Make the folder
    if (!file_exists($edition_zip_root)) {
        mkdir($edition_zip_root, 0777, true);
    }
    if (!file_exists($zip_path)) {
        $zip = new ZipArchive();
        if ($zip->open($zip_path, ZipArchive::CREATE) === TRUE) {
            foreach ($entries as $src => $dest) {
                $src = pugpig_strip_domain($src);
                if (startsWith($src, '/')) {
                    $src = substr($src, 1);
                }
                $zip->addFile($dest, $src);
            }
            $zip->close();
            if (file_exists($zip_path)) {
                echo "<font color='green'>Created ZIP {$zip_path}:<br>" . count($entries) . " file(s)</font> [" . pugpig_bytestosize(filesize($zip_path)) . "]<br />";
            }
        }
    } else {
        echo "<font color='grey'>Found ZIP {$zip_path}:<br>" . count($entries) . " file(s)</font> [" . pugpig_bytestosize(filesize($zip_path)) . "]<br />";
    }
}