function podPress_checkmem($txt, $start = false) { global $podPress_memoryUsage, $podPress_memoryIncrease; if (isset($_GET['podpress_showmem'])) { $mem = memory_get_usage(); if ($start) { $podPress_memoryUsage[$txt] = array('start' => $mem); } else { if (!is_array($podPress_memoryUsage[$txt])) { if (count($podPress_memoryUsage) > 0) { $prevval = end($podPress_memoryUsage); $prevval = $prevval['finish']; } else { $prevval = $mem; } $podPress_memoryUsage[$txt] = array('start' => $prevval, 'fromprev' => 'X'); unset($prevval); } $podPress_memoryUsage[$txt]['finish'] = $mem; $increase = $mem - $podPress_memoryUsage[$txt]['start']; $podPress_memoryUsage[$txt]['increase'] = $increase; $podPress_memoryIncrease = $podPress_memoryIncrease + $increase; if ($_GET['podpress_showmem'] == 1) { echo sprintf(__('%1$s: Increased memory %2$s for a total of %3$s', 'podpress'), $txt, podPress_bytes($increase), podPress_bytes($mem)) . "<br/>\n"; } } } }
function graphByPost() { // ntm: stat logging: Counts Only if ($this->checkWritableTempFileDir() && $this->checkFontFile()) { $chronometry1 = getmicrotime(); $start = isset($_GET['start']) ? $this->podSafeDigit($_GET['start']) : 0; $limit = 20; $image_width = 1200; $image_height = 470; $col_width = 25; $col_space = 15; $filename = 'graph-by-post.jpg'; $file = $this->tempfilesystempath . '/' . $filename; $sidemargin = 20; $baseline = $image_height - 90; $topmargin = 60; $maxheight = $baseline - $topmargin; $timelimit = time() - 5 * 7 * 86400; ini_set('memory_limit', '120M'); ini_set('max_execution_time', '60'); $start_memory = memory_get_usage(); /* get data */ $total = $this->wpdb->get_var('SELECT COUNT(postID) FROM ' . $this->wpdb->prefix . "podpress_statcounts WHERE postID != 0;"); $query = 'SELECT post_title AS title, total, feed, web, play, UNIX_TIMESTAMP(post_date) AS post_date ' . 'FROM ' . $this->wpdb->prefix . 'podpress_statcounts, ' . $this->wpdb->prefix . 'posts ' . 'WHERE ' . $this->wpdb->prefix . 'posts.ID = ' . $this->wpdb->prefix . 'podpress_statcounts.postID ' . 'AND postID !=0 GROUP BY postID ORDER BY post_date DESC LIMIT ' . $start . ', ' . $limit . ';'; $data = $this->wpdb->get_results($query); $cnt_data = count($data); $first_post = $data[$cnt_data - 1]->post_date; $last_post = $data[0]->post_date; $stats = array(); foreach ($data as $idata) { $stats[$idata->day][$idata->method]++; $stats[$idata->day]['total']++; $stats_total[$idata->method]++; $stats_total['total']++; } /* get min an max values */ $value_min = 0; $value_max = 0; $stats_total = array(); foreach ($data as $idata) { if ($value_min == 0) { $value_min = $idata->total; } elseif ($idata->total < $value_min) { $value_min = $idata->total; } if ($idata->total > $value_max) { $value_max = $idata->total; } $stats_total['feed'] = $stats_total['feed'] + $idata->feed; $stats_total['web'] = $stats_total['web'] + $idata->web; $stats_total['play'] = $stats_total['play'] + $idata->play; $stats_total['total'] = $stats_total['total'] + $idata->total; } /* Do we have enough data? */ if (intval($value_max) > 0) { $h_cscale = $maxheight / $value_max; $h_vscale = $maxheight / $value_min / 4; $w_scale = intval($image_width - 2 * $sidemargin + intval(($image_width - 2 * $sidemargin) / $limit / 4)) / $limit; /* create image */ $chronometry2 = getmicrotime(); $image = imagecreatetruecolor($image_width, $image_height); if (function_exists('imageantialias')) { imageantialias($image, 1); } $colors = array('background' => imagecolorallocate($image, 51, 51, 51), 'line' => imagecolorallocate($image, 79, 79, 79), 'text' => imagecolorallocate($image, 255, 255, 255), 'copytext' => imagecolorallocate($image, 79, 79, 79), 'total' => imagecolorallocate($image, 0, 0, 0), 'feed' => imagecolorallocate($image, 143, 53, 53), 'web' => imagecolorallocate($image, 71, 143, 88), 'play' => imagecolorallocate($image, 142, 143, 71)); imagefill($image, 0, 0, $colors['background']); /* draw background-lines and scale-text */ $step = 0; $h = ($baseline - $topmargin) / 10; while ($step <= 10) { $pos_y = $topmargin + $h * $step; imageline($image, 0, $topmargin + $h * $step, $image_width, $topmargin + $h * $step, $colors['line']); imagettftext($image, 8, 0, $sidemargin, $pos_y + 13, $colors['line'], $this->fontface, number_format(($baseline - $pos_y) / $h_cscale, 0, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1])); $step++; } /* create the legend */ imagettftext($image, 14, 0, $sidemargin, 25, $colors['text'], $this->fontface, get_option('blogname')); imagettftext($image, 8, 0, $sidemargin, 42, $colors['text'], $this->fontface, __('Posts', 'podpress') . ' » ' . strftime('%d.%m.%Y', $first_post) . ' - ' . strftime('%d.%m.%Y', $last_post)); $text_total = number_format($stats_total['total'], 0, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]); $text_feed = number_format($stats_total['feed'], 0, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]) . ' (' . number_format($stats_total['feed'] * 100 / $stats_total['total'], 1, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]) . '%)'; $text_web = number_format($stats_total['web'], 0, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]) . ' (' . number_format($stats_total['web'] * 100 / $stats_total['total'], 1, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]) . '%)'; $text_play = number_format($stats_total['play'], 0, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]) . ' (' . number_format($stats_total['play'] * 100 / $stats_total['total'], 1, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]) . '%)'; $text_total = __('Total stats from displayed posts', 'podpress') . ': ' . $text_total . ' / ' . __('Feed', 'podpress') . ': ' . $text_feed . ' / ' . __('Download', 'podpress') . ': ' . $text_web . ' / ' . __('Play', 'podpress') . ': ' . $text_play . ' '; imagettftext($image, 8, 0, $sidemargin, $image_height - 15, $colors['text'], $this->fontface, $text_total); $pos_y = $image_height - 32; imagefilledrectangle($image, $sidemargin + 0, $pos_y - 10, $sidemargin + 10, $pos_y, $colors['feed']); imagettftext($image, 8, 0, $sidemargin + 15, $pos_y, $colors['text'], $this->fontface, __('Feed', 'podpress')); imagefilledrectangle($image, $sidemargin + 50, $pos_y - 10, $sidemargin + 60, $pos_y, $colors['web']); imagettftext($image, 8, 0, $sidemargin + 65, $pos_y, $colors['text'], $this->fontface, __('Download', 'podpress')); imagefilledrectangle($image, $sidemargin + 125, $pos_y - 10, $sidemargin + 135, $pos_y, $colors['play']); imagettftext($image, 8, 0, $sidemargin + 140, $pos_y, $colors['text'], $this->fontface, __('Play', 'podpress')); imagettftext($image, 23, 0, $image_width - 128, 30, $colors['copytext'], $this->fontface, 'podPress'); imagettftext($image, 8, 0, $image_width - 115, 43, $colors['copytext'], $this->fontface, __('Plugin for WordPress', 'podpress')); imagettftext($image, 6, 90, $image_width - 15, $image_height - 10, $colors['copytext'], $this->fontface, strftime($this->local_settings['creation_date'], time()) . ' (' . PODPRESS_VERSION . ')'); $pos_x = $image_width - $sidemargin - 15; /* draw the posts */ foreach ($data as $idata) { /* Total stats */ $col_total_height = $idata->total * $h_cscale; imageline($image, $pos_x - 2, $baseline, $pos_x - 2, $baseline - $col_total_height, $colors['total']); imagettftext($image, 8, 0, $pos_x - $col_width - 3, $baseline - 3 - $idata->total * $h_cscale, $colors['text'], $this->fontface, $idata->total); /* Feeds */ $perc_feed = number_format($idata->feed * 100 / $idata->total, 0, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]); $col_feed_height = $idata->feed * $h_cscale; if ($col_feed_height < 0) { imagefilledrectangle($image, $pos_x - $col_width - 3, $baseline, $pos_x - 3, $baseline - $col_feed_height, $colors['feed']); } else { imagefilledrectangle($image, $pos_x - $col_width - 3, $baseline - $col_feed_height, $pos_x - 3, $baseline, $colors['feed']); } if ($col_feed_height > 11) { imagettftext($image, 8, 0, $pos_x - $col_width - 2, $baseline - $idata->feed * $h_cscale + 11, $colors['text'], $this->fontface, $perc_feed . '%'); } /* Web */ $perc_web = number_format($idata->web * 100 / $idata->total, 0, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]); $col_web_height = $idata->web * $h_cscale; if ($col_web_height < 0) { imagefilledrectangle($image, $pos_x - $col_width - 3, $baseline - $col_feed_height, $pos_x - 3, $baseline - $col_web_height - $col_feed_height, $colors['web']); } else { imagefilledrectangle($image, $pos_x - $col_width - 3, $baseline - $col_web_height - $col_feed_height, $pos_x - 3, $baseline - $col_feed_height, $colors['web']); } if ($col_web_height > 11) { imagettftext($image, 8, 0, $pos_x - $col_width - 2, $baseline - $col_web_height - $col_feed_height + 11, $colors['text'], $this->fontface, $perc_web . '%'); } /* Play */ $perc_play = number_format($idata->play * 100 / $idata->total, 0, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]); $col_play_height = $idata->play * $h_cscale; if ($col_play_height < 0) { imagefilledrectangle($image, $pos_x - $col_width - 3, $baseline - $col_feed_height - $col_web_height, $pos_x - 3, $baseline - $col_play_height - $col_web_height - $col_feed_height, $colors['play']); } else { imagefilledrectangle($image, $pos_x - $col_width - 3, $baseline - $col_play_height - $col_web_height - $col_feed_height, $pos_x - 3, $baseline - $col_feed_height - $col_web_height, $colors['play']); } if ($col_play_height > 11) { imagettftext($image, 8, 0, $pos_x - $col_width - 2, $baseline - $col_play_height - $col_web_height - $col_feed_height + 11, $colors['text'], $this->fontface, $perc_play . '%'); } /* Set Date and Title */ $title = strlen($idata->title) > 70 ? substr($idata->title, 0, 70) . '...' : $idata->title; imagettftext($image, 8, 90, $pos_x + 10, $baseline, $colors['text'], $this->fontface, $title); imagettftext($image, 8, 0, $pos_x - $col_width - 3, $baseline + 14, $colors['text'], $this->fontface, strftime($this->local_settings['short_date'], $idata->post_date)); $pos_x = $pos_x - $col_width - $col_space - 15; } imagejpeg($image, $file, 100); $chronometry_end = getmicrotime(); $chronometry1 = $chronometry_end - $chronometry1; $chronometry2 = $chronometry_end - $chronometry2; imagedestroy($image); $end_memory = memory_get_usage(); $memory_used = podPress_bytes($end_memory - $start_memory); echo '<div id="podPress_graph" style="width: ' . $image_width . 'px;">' . "\n"; echo ' <p style="padding-top: 0;"><img src="' . $this->tempfileurlpath . '/' . $filename . '" width="' . $image_width . '" height="' . $image_height . '" alt="podPress-Statistics" /></p>' . "\n"; echo $this->paging($start, $limit, $total, 'Posts'); echo ' <div class="clear"></div>' . "\n"; echo "</div>\n"; echo '<p>' . __('Time to generate the graph', 'podpress') . ': ' . number_format($chronometry1, 3, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]) . ' seconds (image: ' . number_format($chronometry2, 3, $this->local_settings['numbers'][0], $this->local_settings['numbers'][1]) . " seconds).\n"; echo '<br/>' . __('Memory to generate the graph', 'podpress') . ': ' . $memory_used . ".</p>\n"; } else { echo '<p>' . __('We\'re sorry. At the moment we don\'t have enough data collected to display the graph.', 'podpress') . "</p>\n"; } } }