render($lf_cat, $lf_page); function encodeString($string) { return strtolower(preg_replace(";\s+;", "_", preg_replace(";\'|\&;", "", $string))); } function getBaseUrl() { $base_url = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); return $base_url; } class LF_Client { var $catalog; var $params; var $errorString; var $errors = array ( "file_not_exists" => "You must create writable file %s", "file_not_readable" => "Cache file must be readable", "file_not_writable" => "Cache file must be writable. You must create writable file %s", "file_open_error" => "Cache file opening error", "incorrect_params" => "Incorrect params. Try reuploading catalog code", ); function LF_Client($params) { $this->params = $params; /** Init catalog */ $this->catalog = array("pagesize" => 0, "cats" => array()); $cacheFilename = $this->params["script_base"] . "/" . $this->params["cache_filename"]; if ($this->params['file_life_time'] < 0 || $this->params['file_life_time'] > 48 || $this->params['catalog_ncols'] <= 0 || $this->params['catalog_ncols'] > 5) { $this->errorString = $this->errors['incorrect_params']; die($this->errorString); } $fileExists = file_exists($cacheFilename); if ($fileExists) { $fileExpired = (time() - filemtime($cacheFilename)) > 3600*$this->params["file_life_time"]; $isReadable = is_readable($cacheFilename); $isWritable = is_writable($cacheFilename); } if ($fileExists && ! $fileExpired && $isReadable && filesize($cacheFilename)) { /** Read catalog from cache */ $this->catalog = unserialize(file_get_contents($cacheFilename)); } elseif (! $fileExists) { $this->errorString = sprintf($this->errors['file_not_exists'], $cacheFilename); } elseif (! $isReadable) { $this->errorString = sprintf($this->errors["file_not_readable"], $cacheFilename); } elseif (! $isWritable) { $this->errorString = sprintf($this->errors["file_not_writable"], $cacheFilename); } else { /** Request server for catalog */ $responseXml = $this->sendRequest(); $reader = new LF_XmlReader($responseXml, $this->catalog); $reader->parse(); if (! $reader->errorString) { $this->catalog = $reader->catalog; /** Serialize catalog */ $fp = @fopen($cacheFilename, "w"); if (is_resource($fp)) { fwrite($fp, serialize($this->catalog)); fclose($fp); } else { $this->errorString = $this->errors["file_open_error"]; } } else { $this->errorString = $reader->errorString; } } } function _formatUrl($cat) { if ($this->params["use_mod_rewrite"]) { $urlname = $this->params["rewrite_base"] . $cat["uf_title"]; $cat["url"] = $urlname . ".html"; $cat["paging_format"] = $urlname . "_%d.html"; } else { $cat["url"] = "?cat={$cat['id']}"; $cat["paging_format"] = "?cat={$cat['id']}&p=%d"; } return $cat; } function render($cat = 0, $page = 1) { if ($this->errorString) { print $this->errorString; return; } if ($this->params["use_mod_rewrite"] && $cat) { $catId = ""; foreach ($this->catalog["cats"] as $id => $category) { if ($category["uf_title"] == $cat) { $catId = $id; break; } } } else { $catId = $cat; } if (! $catId) //root category { $cats = $this->selectSubCats(0); foreach ($cats as $i => $cat) { $cats[$i] = $this->_formatUrl($cat); $subcats = $this->selectSubCats($i); foreach ($subcats as $si => $subcat) { $subcats[$si] = $this->_formatUrl($subcat); } $cats[$i]["subcats"] = $subcats; } $ncols = $this->params["catalog_ncols"]; $this->paintCatalog($cats, $ncols); } else //some subcategory { $cat = $this->catalog["cats"][$catId]; $cat = $this->_formatUrl($cat); $cat["npages"] = sizeof($cat["linkpages"]); $parent_cat = $this->catalog["cats"][$cat["parent_id"]]; $parent_cat = $this->_formatUrl($parent_cat); $cat["parent_title"] = $parent_cat["title"]; $cat["parent_url"] = $parent_cat["url"]; $cat["subcats"] = $this->selectSubCats($cat["id"]); foreach ($cat["subcats"] as $si => $subcat) { $cat["subcats"][$si] = $this->_formatUrl($subcat); } $cat["links"] = array(); $counter = $this->catalog["pagesize"]*($page-1)+1; if (is_array($cat["linkpages"][$page-1])) { foreach ($cat["linkpages"][$page-1] as $link) { $cat["links"][$counter] = $link; $counter++; } } unset($cat["linkpages"]); $this->paintCat($cat, $page); } } function splitCats($cats, $ncols) { $colsize = ceil(sizeof($cats)/$ncols); $cols = array_fill(0, $colsize, array()); for ($i=0; $i<$ncols; $i++) { for ($j=0; $j<$colsize && !empty($cats); $j++) { $cols[$i][$j] = array_shift($cats); } } return $cols; } function paintCatalog($cats, $ncols) { ?>