function createThumbnail($image, $size, $complete_tag = false, $alt = 'alt=""')
 {
     // Parse size parameter
     if (preg_match('@^(\\d+)x(\\d+)$@', $size, $matches)) {
         $width = $matches[1];
         $height = $matches[2];
     } else {
         if (preg_match('@^(\\d+)$@', $size)) {
             $width = $size;
             $height = $size;
         } else {
             MARKET_Base::raiseError(MARKET_ERROR_RETURN, __FUNCTION__ . '(): Size "' . htmlspecialchars($size) . '" is not acceptable.', __FILE__, __LINE__);
             return MARKET_Filter::_defaultThumbnail();
         }
     }
     $type = strtolower(substr(strrchr($image, '.'), 1));
     $out = '/cache/' . dirname($image) . '/' . substr(basename($image), 0, strrpos(basename($image), '.')) . '.' . $width . 'x' . $height . '.' . $type;
     if (@is_file(MARKET_ROOT_DIR . '/' . $out) && @is_readable(MARKET_ROOT_DIR . '/' . $out)) {
         // Do nothing
     } else {
         // Create thumbnail
         // Requires GD
         if (extension_loaded('gd')) {
             $gd_info = gd_info();
             $in = MARKET_ROOT_DIR . '/' . $image;
             if (@is_file($in) && @is_readable($in)) {
                 if (!(list($w, $h) = getimagesize($in))) {
                     MARKET_Base::raiseError(MARKET_ERROR_RETURN, __FUNCTION__ . '(): Image "' . htmlspecialchars($image) . '" is not supported', __FILE__, __LINE__);
                     return MARKET_Filter::_defaultThumbnail();
                 }
                 if ($type == 'jpeg') {
                     $type = 'jpg';
                 }
                 switch ($type) {
                     case 'bmp':
                         $img = imagecreatefromwbmp($in);
                         break;
                     case 'gif':
                         $img = imagecreatefromgif($in);
                         break;
                     case 'jpg':
                         $img = imagecreatefromjpeg($in);
                         break;
                     case 'png':
                         $img = imagecreatefrompng($in);
                         break;
                     default:
                         MARKET_Base::raiseError(MARKET_ERROR_RETURN, __FUNCTION__ . '(): Image "' . htmlspecialchars($image) . '" is not supported', __FILE__, __LINE__);
                         return MARKET_Filter::_defaultThumbnail();
                 }
                 // Resize and crop
                 $sratio = $w / $h;
                 $dratio = $width / $height;
                 if ($sratio > $dratio) {
                     $temp_width = (int) ($h * $dratio);
                     $temp_height = $h;
                     $x = (int) (($w - $temp_width) / 2);
                     $y = 0;
                 } else {
                     $temp_width = $w;
                     $temp_height = (int) ($w / $dratio);
                     $x = 0;
                     $y = (int) (($h - $temp_height) / 2);
                 }
                 $source_width = $temp_width;
                 $source_height = $temp_height;
                 $dst = imagecreatetruecolor($width, $height);
                 // Preserve transparency
                 if ($type == 'gif' || $type == 'png') {
                     imagecolortransparent($dst, imagecolorallocatealpha($dst, 0, 0, 0, 127));
                     imagealphablending($dst, false);
                     imagesavealpha($dst, true);
                 }
                 imagecopyresampled($dst, $img, 0, 0, $x, $y, $width, $height, $source_width, $source_height);
                 // Create dir
                 MARKET_Base::makeDir(dirname(MARKET_ROOT_DIR . $out));
                 switch ($type) {
                     case 'bmp':
                         imagewbmp($dst, MARKET_ROOT_DIR . $out);
                         break;
                     case 'gif':
                         imagegif($dst, MARKET_ROOT_DIR . $out);
                         break;
                     case 'jpg':
                         imagejpeg($dst, MARKET_ROOT_DIR . $out);
                         break;
                     case 'png':
                         imagepng($dst, MARKET_ROOT_DIR . $out);
                         break;
                 }
             } else {
                 MARKET_Base::raiseError(MARKET_ERROR_WARNING, __FUNCTION__ . '(): Image "' . htmlspecialchars($image) . '" not found or not readable', __FILE__, __LINE__);
                 return MARKET_Filter::_defaultThumbnail('', $width, $height, $complete_tag, $alt);
             }
         } else {
             MARKET_Base::raiseError(MARKET_ERROR_WARNING, __FUNCTION__ . '(): The GD extension is not loaded', __FILE__, __LINE__);
             return MARKET_Filter::_defaultThumbnail($image, $width, $height, $complete_tag, $alt);
         }
     }
     if ($complete_tag) {
         return '<img src="' . MARKET_WEB_DIR . $out . '" width="' . $width . '" height="' . $height . '" ' . $alt . ' />';
     } else {
         return MARKET_WEB_DIR . '/' . $out;
     }
 }
 function assignNavigationValues(&$sql, $layout = 'default', $start = 0, $show = 10, $limit = 0, $javascript = false, $group = false, $absolute = false)
 {
     if (preg_match('@\\d+@', $_GET['show'])) {
         $show = $_GET['show'];
     }
     if (preg_match('@\\d+@', $_GET['page'])) {
         $start = ($_GET['page'] - 1) * $show;
     }
     if (isset($_GET['start']) && preg_match('@\\d+@', $_GET['start'])) {
         $start = $_GET['start'];
     }
     if ($limit && $show > $limit) {
         $show = $limit;
     }
     if (!$show) {
         $show = 10;
     }
     if ($start < 0) {
         $start = 0;
     }
     $start = floor($start / $show) * $show;
     if (preg_match('@^SELECT@', $sql)) {
         $sig = md5($sql . $_GET['group']);
         $sqp =& $this->getRef('Sql_Parser');
         $sqp->parseSQL($sql);
         if ($_SESSION['NAV.Vars']['NAV.Sig'] == $sig) {
             $total = $_SESSION['NAV.Vars']['NAV.Total'];
             $stotal = $_SESSION['NAV.Vars']['NAV.STotal'];
             // Group on parent_id
             if ($group && $_GET['group'] && !$_GET['gid'] && !$_GET['expand']) {
                 if (strstr($sqp->sql['FROM'], ' ')) {
                     $table = substr($sqp->sql['FROM'], 0, strpos($sqp->sql['FROM'], ' '));
                 } else {
                     $table = $sqp->sql['FROM'];
                 }
                 $sqp->sql['WHERE'] .= " AND " . $table . ".parent_id=''";
                 $sql = $sqp->getSQL();
             }
         } else {
             $count_sql = $this->getCountSql($sqp);
             if (sqlQuery($count_sql, $res)) {
                 $total = sqlResult($res, 0);
                 if ($_GET['gid'] && preg_match('@UNION@', $sql)) {
                     $total++;
                 }
             }
             // Group on parent_id
             if ($group && $_GET['group'] && !$_GET['gid'] && !$_GET['expand']) {
                 if (strstr($sqp->sql['FROM'], ' ')) {
                     $table = substr($sqp->sql['FROM'], 0, strpos($sqp->sql['FROM'], ' '));
                 } else {
                     $table = $sqp->sql['FROM'];
                 }
                 $sqp->sql['WHERE'] .= " AND " . $table . ".parent_id=''";
                 $count_sql = $this->getCountSql($sqp);
                 if (sqlQuery($count_sql, $res)) {
                     $stotal = $total;
                     $total = sqlResult($res, 0);
                 }
                 $sql = $sqp->getSQL();
             }
         }
         $_SESSION['NAV.Vars']['NAV.Sig'] = $sig;
         $_SESSION['NAV.Vars']['NAV.Total'] = $total;
         $_SESSION['NAV.Vars']['NAV.STotal'] = $stotal;
         if ($start > $total) {
             $start = 0;
         }
         // Modify the sql
         $sql .= " LIMIT {$start}, {$show}";
     } else {
         if (preg_match('@^\\d+$@', $sql)) {
             $total = $sql;
         } else {
             $this->raiseError(MARKET_ERROR_WARNING, 'assignNavigationValues(): wrong datatype for first argument', __FILE__, __LINE__);
             $total = 0;
         }
     }
     if ($start > $total) {
         $start = 0;
     }
     // First
     if ($start == 0) {
         $first = -1;
     }
     // Previous
     if (($previous = $start - $show) < 0) {
         $previous = 0;
     }
     // Next
     if (($next = $start + $show) >= $total) {
         $next = -1;
         $end = $total;
     } else {
         $end = $start + $show;
     }
     // Last
     if (($last = floor(($total - 1) / $show) * $show) <= $start) {
         $last = -1;
     }
     $lng =& $this->getRef('Lang');
     // Found
     if ($total == 0) {
         $found = '0 ' . $lng->strs['items_total'];
     } else {
         if ($total == 1) {
             if ($stotal) {
                 if ($stotal == 1) {
                     $found = '1 ' . $lng->strs['item_total'] . ' ' . $lng->strs['in'];
                 } else {
                     $found = $stotal . ' ' . $lng->strs['items_total'] . ' ' . $lng->strs['in'];
                 }
                 $found .= ' 1 ' . $lng->strs['set'];
             } else {
                 $found = '1 ' . $lng->strs['item_total'];
             }
         } else {
             if ($stotal) {
                 $found = MARKET_Filter::marketNumber($stotal) . ' ' . $lng->strs['items_total'] . ' ' . $lng->strs['in'] . ' ' . MARKET_Filter::marketNumber($total) . ' ' . $lng->strs['sets'] . ' [' . $lng->strs['sets'] . ' ' . ($start + 1) . ' - ' . $end . ']';
             } else {
                 if ($show == 1) {
                     $found = MARKET_Filter::marketNumber($total) . ' ' . $lng->strs['items_total'] . ' [' . ($start + 1) . ']';
                 } else {
                     $found = MARKET_Filter::marketNumber($total) . ' ' . $lng->strs['items_total'] . ' [' . ($start + 1) . ' - ' . $end . ']';
                 }
             }
         }
     }
     $req =& $this->getRef('Request');
     // Pages
     $num_of_pages = ceil($total / $show);
     if ($num_of_pages) {
         $pagesx = '';
         $page_num = floor($start / $show);
         // Previous
         if ($first == -1 || $start == 0) {
             $pages = '<span class="disabled prev_page">' . $lng->strs['Previous'] . '</span><span class="gap"> &nbsp;</span> ';
             $this->assignGlobal('PAGESX.Previous', '<span class="disabled prev_page">&laquo; ' . substr($lng->strs['Previous'], 4) . '</span> ');
         } else {
             $href = $javascript ? "javascript:rIU('start', '" . $previous . "')" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', $previous);
             $pages = '<a class="previous_page" href="' . $href . '">' . $lng->strs['Previous'] . '</a><span class="gap"> &nbsp;</span> ';
             $this->assignGlobal('PAGESX.Previous', '<a class="previous_page" href="' . $href . '">&laquo; ' . substr($lng->strs['Previous'], 4) . '</a> ');
         }
         if ($page_num < 4) {
             $start_from = 1;
             $up_to = $page_num == 3 ? 6 : 5;
         } else {
             // First
             $href = $javascript ? "javascript:rIU('start', '0')" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', 0);
             if ($num_of_pages != 5) {
                 $pages .= '<a class="first_page" href="' . $href . '">1<span class="ellipsis">...</span></a> ';
                 $pagesx .= '<a href="' . $href . '">1</a> <span class="gap">…</span> ';
             }
             if ($page_num > $num_of_pages - 5) {
                 $start_from = $page_num == $num_of_pages - 4 ? $num_of_pages - 5 : $num_of_pages - 4;
                 $up_to = $num_of_pages;
             } else {
                 $start_from = $page_num - 1;
                 $up_to = $page_num + 3;
             }
         }
         for ($i = $start_from; $i <= $up_to; $i++) {
             if ($i <= $num_of_pages) {
                 if ($i == $page_num + 1) {
                     $pages .= '<span class="current_page">[' . $i . ']</span> ';
                     $pagesx .= '<span class="current">' . $i . '</span> ';
                 } else {
                     $href = $javascript ? "javascript:rIU('start', '" . ($i - 1) * $show . "')" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', ($i - 1) * $show);
                     $pages .= '<a class="some_page" href="' . $href . '">' . $i . '</a> ';
                     $pagesx .= '<a href="' . $href . '">' . $i . '</a> ';
                 }
             }
         }
         // Last
         if ($up_to < $num_of_pages - 1) {
             $href = $javascript ? "javascript:rIU('start', '" . ($num_of_pages - 1) * $show . "')" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', ($num_of_pages - 1) * $show);
             $pages .= '<a class="last_page" href="' . $href . '"><span class="ellipsis">...</span>' . $num_of_pages . '</a> ';
             $pagesx .= '<span class="gap">…</span> <a href="' . $href . '">' . $num_of_pages . '</a> ';
         }
         $this->assignGlobal('PAGESX.Pages', $pagesx);
         // Next
         if ($last == -1) {
             $pages .= '<span class="gap">&nbsp; </span><span class="disabled next_page">' . $lng->strs['Next'] . '</span>';
             $this->assignGlobal('PAGESX.Next', '<span class="disabled next_page">' . substr($lng->strs['Next'], 0, -4) . ' &raquo;</span> ');
         } else {
             $href = $javascript ? "javascript:rIU('start', '" . $next . "')" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', $next);
             $pages .= '<span class="gap">&nbsp; </span><a class="next_page" href="' . $href . '">' . $lng->strs['Next'] . '</a>';
             $this->assignGlobal('PAGESX.Next', '<a class="next_page" href="' . $href . '">' . substr($lng->strs['Next'], 0, -4) . ' &raquo;</a> ');
         }
         $this->loadTemplate($layout . '/navigation', MARKET_INDIRECT_CALL);
         $this->parseTemplate('foo', 'pagesx');
     }
     if ($layout) {
         $rels = '';
         if ($first == -1) {
             $this->assignGlobal(array('NAV.Lnk_First' => '<a class="disabled"><i>' . $lng->strs['First'] . '</i></a>', 'NAV.Lnk_Previous' => '<a class="disabled"><i>' . $lng->strs['Previous'] . '</i></a>'));
         } else {
             $href1 = $javascript ? "javascript:rIU('start', '0');" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', 0);
             $href2 = $javascript ? "javascript:rIU('start', '" . $previous . "');" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', $previous);
             $this->assignGlobal(array('NAV.Lnk_First' => '<a href="' . $href1 . '"><i>' . $lng->strs['First'] . '</i></a>', 'NAV.Lnk_Previous' => '<a href="' . $href2 . '"><i>' . $lng->strs['Previous'] . '</i></a>'));
             $rels .= '<link rel="first" href="' . ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', 0) . '" />' . "\n";
             $rels .= '<link rel="previous" href="' . ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', $previous) . '" />' . "\n";
         }
         if ($last == -1) {
             $this->assignGlobal(array('NAV.Lnk_Next' => '<a class="disabled"><i>' . $lng->strs['Next'] . '</i></a>', 'NAV.Lnk_Last' => '<a class="disabled"><i>' . $lng->strs['Last'] . '</i></a>'));
         } else {
             $href1 = $javascript ? "javascript:rIU('start', '" . $next . "');" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', $next);
             $href2 = $javascript ? "javascript:rIU('start', '" . $last . "');" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', $last);
             $this->assignGlobal(array('NAV.Lnk_Next' => '<a href="' . $href1 . '"><i>' . $lng->strs['Next'] . '</i></a>', 'NAV.Lnk_Last' => '<a href="' . $href2 . '"><i>' . $lng->strs['Last'] . '</i></a>'));
             $rels .= '<link rel="next" href="' . ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', $next) . '" />' . "\n";
             $rels .= '<link rel="last" href="' . ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('start', $last) . '" />' . "\n";
         }
         if ($group) {
             if ($_GET['group']) {
                 $href = $javascript ? "javascript:rIU('group', '0');" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('group', '0');
                 $this->assignGlobal('NAV.Group', '<span id="group"><a class="selected" href="' . $href . '"><i>' . $lng->strs['Group'] . '</i></a></span>');
             } else {
                 if ($_GET['version_history'] || is_object($sqp) && !preg_match('@parent_id@', $sqp->sql['SELECT'])) {
                     $this->assignGlobal('NAV.Group', '<span id="group"><a class="disabled"><i>' . $lng->strs['Group'] . '</i></a></span>');
                 } else {
                     $href = $javascript ? "javascript:rIU('group', '1');" : ($absolute ? '{MARKET.WebDir}/' : '') . $req->replaceInUrl('group', '1');
                     $this->assignGlobal('NAV.Group', '<span id="group"><a href="' . $href . '"><i>' . $lng->strs['Group'] . '</i></a></span>');
                 }
             }
         }
         $this->assignGlobal('PAGE.Rels', $rels);
         $this->loadTemplate($layout . '/navigation', MARKET_INDIRECT_CALL);
         $this->parseTemplate('foo', 'toolbar');
     }
     $this->assignGlobal(array('NAV.Start' => $start + 1, 'NAV.Page' => $page_num + 1, 'NAV.End' => $end, 'NAV.Found' => $found, 'NAV.Show' => $show, 'NAV.Total' => $total, 'NAV.Previous' => $previous, 'NAV.Next' => $next, 'NAV.Last' => $last, 'NAV.Pages' => $pages));
     // Next Page
     if ($next != -1) {
         $this->assignGlobal('NAV.NextPage', $page_num + 2);
     } else {
         $this->assignGlobal('NAV.NextPage', 1);
     }
     return array($start, $show, $total, $end);
 }