path = $path; $this->clean_path(); $this->entry = array(); if($xml_file) { $this->file = $this->path.$xml_file; $this->get_cart(); $this->nb_items = sizeof($this->entry); } else { $this->create_cart(); } return; } // mise à jour de la description function set_description($desc='') { $this->description = $desc; } // mise en forme du path function clean_path() { if(!$this->path) { $this->path = './'; } else { $this->path = preg_replace('/\/$|\/\s$|\s$/', '', $this->path); $this->path .= '/'; } } // liste des paniers disponibles static function get_cart_list($path='') { // nettoyage du path if(!$path) { $path = '.'; } else { $path = preg_replace('/\/$|\/\s$|\s$/', '', $path); } $cart_list=array(); if ($dir = @opendir($path)) { while($file = readdir($dir)) { if(preg_match('/\.xml$/i', $file)) { $myCart = new cart($file, $path); if($myCart->name) { $cart_list[] = array( 'name' => $myCart->name, 'file' => $myCart->file, 'items'=> $myCart->nb_items, 'description' => $myCart->description); } } } closedir($dir); } return $cart_list; } // création d'un panier vide function create_cart() { $this->name = 'CART'.time(); $this->file = $this->path.$this->name.".xml"; if( $fp = fopen($this->file, "w")) fclose($fp); else die( "PMB cart parser error : can't create cart ".$this->file); $this->nb_items = 0; } // ajout d'un item function add_item($item=0) { if(!(int)$item || in_array($item, $this->entry)) return; $this->entry[] = $item; $this->nb_items = sizeof($this->entry); } // suppression d'un item function del_item($item=0) { if(!(int)$item) return; for($i=0 ; $i < sizeof($this->entry); $i++) { if( (int) $this->entry[$i] == $item) { $this->entry[$i] = 0; $this->nb_items--; } } $this->entry = array_filter($this->entry, 'array_clean'); } // suppression d'un fichier de panier function delete() { if(@unlink($this->file)) { $this->entry=array(); $this->name=''; $this->nb_items=0; $this->description=''; $this->file=''; } } // sauvegarde du panier function save_cart() { if($fp = @fopen($this->file, 'w')) { $header = ""; $header .= "\ndtd_path."\">"; $header .= "\nname; $header .= "\" description=\"".$this->description."\">"; fputs($fp, $header); // élimination des valeurs nulles $this->entry = array_filter($this->entry, 'array_clean'); for($i=0 ; $i < sizeof($this->entry); $i++) { if( (int) $this->entry[$i]) fputs($fp, "\n\t".$this->entry[$i].""); } $footer = "\n\n"; fputs($fp, $footer); fflush($fp); fclose($fp); } else { die( "PMB cart parser error : can't store datas in ".$this->file); } } // fonctions du gestionnaire d'éléments function debutBalise($parser, $nom, $attributs) { switch($nom) { case 'CART': $this->name = $attributs['NAME']; $this->description = $attributs['DESCRIPTION']; break; case 'ITEM': break; default: break; } return; } function finBalise($parser, $nom) { return; } // content() -> gestionnaire de données function content($parser, $data) { if((int)$data) { $this->entry[] = $data; } return; } // get_cart() : ouvre un fichier et récupère le panier function get_cart() { global $charset; if(! $fp = @fopen($this->file, 'r')) { die( "PMB cart parser error : can't access ".$this->file); } else { $file_size=filesize ($this->file); $data = fread ($fp, $file_size); $rx = "//m"; if (preg_match($rx, $data, $m)) $encoding = strtoupper($m[1]); else $encoding = "ISO-8859-1"; $this->parser = xml_parser_create($encoding); xml_parser_set_option($p, XML_OPTION_TARGET_ENCODING, $charset); xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, true); xml_set_object($this->parser, $this); xml_set_element_handler($this->parser, "debutBalise", "finBalise"); xml_set_character_data_handler($this->parser, "content"); while($data = fread($fp, 4096)) { if( !xml_parse($this->parser, $data, feof($fp))) { die( sprintf("XML error : %s
at line %d\n\n'", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser))); } } fclose($fp); xml_parser_free($this->parser); } } } // fin de déclaration de la classe cart } # fin de déclaration du fichier cart.class ?>