示例#1
0
function old_category_list($lang="en") {
  global $lists_dir;

  if($state=category_check_state()!==true) {
    return array('status'=>$state);
  }

  $ret=array();
  lock_dir($lists_dir);

  $d=opendir("$lists_dir");
  while($f=readdir($d)) {
    if(preg_match("/^(.*)\.xml$/", $f, $m)) {
      $x=new DOMDocument();
      $x->loadXML(file_get_contents("$lists_dir/$f"));
      $tags=new tags();
      $tags->readDOM($x->firstChild);

      //if($tags->get("hide")!="yes") {
	$ret[$m[1]]=$tags;
      //}
    }
  }
  closedir($d);

  unlock_dir($lists_dir);

  return $ret;
}
示例#2
0
function category_save($request_id, $content, $param=array()) {
  global $db_central;
  global $current_user;

  // Create a sql-statement to import whole category in one transaction
  $sql="begin;";

  // Load file into $file
  $file=new DOMDocument();
  if(!($file->loadXML($content))) {
    return array("status"=>"Could not load data");
  }

  // Calculate a new version ID
  $version=uniqid();

  // read main tags
  $tags=new tags();
  $root=$file->firstChild;
  $tags->readDOM($root);

  // compile version tags
  $version_tags=new tags();
  $version_tags->set("user", $current_user->username);
  $version_tags->set("date", Date("c"));
  $version_tags->set("msg", $param['msg']);
  if(($current_user->tags->get("admin")=="yes")&&($param['lock']=="yes"))
    $version_tags->set("lock", "yes");

  // and old version
  $old_version=$root->getAttribute("version");
  if(!$old_version)
    $pg_old_version="null";
  else
    $pg_old_version="Array[".postgre_escape($old_version)."]";
  
  // check what we want as new id
  $new_id=$tags->get("id");
  if(!$new_id)
    $new_id=$request_id;
  if(!$new_id)
    $new_id="cat_{$version}";

  // is id available?
  $res=sql_query("select * from category_current where category_id=".postgre_escape($new_id), $db_central);
  if(($elem=pg_fetch_assoc($res))&&($elem['version']!=$old_version)) {
    // already taken by another category - we should include a message
    $tags->set("id:message", "ID '$new_id' has already been taken");
    $id="cat_{$version}";
  }
  else {
    $id=$new_id;
  }

  if($old_version&&($id==$tags->get("id"))) {
    $res=sql_query("select * from category where version=".postgre_escape($old_version), $db_central);
    $old_cat=pg_fetch_assoc($res);
    $old_version_tags=parse_hstore($old_cat['version_tags']);
    if(($old_version_tags['lock'])&&($current_user->tags->get("admin")!="yes")) {
      // category is locked and we are not admin
      $tags->set("id:message", "Category '$id' is locked.");
      $id="cat_{$version}";
    }
  }

  // add id to tags
  $tags->set("id", $id);

  // write main tags to db
  $sql.="insert into category values (".
    postgre_escape($id).", ".
    array_to_hstore($tags->data()).", ".
    "'$version', ".
    "$pg_old_version, ".
    array_to_hstore($version_tags->data()).
    ");";

  // process rules
  $current=$root->firstChild;
  while($current) {
    if($current->nodeName=="rule") {
      // read rule tags
      $rule_id=$current->getAttribute("id");
      $tags=new tags();
      $tags->readDOM($current);

      // write rule tags to db
      $sql.="insert into category_rule values (".
	postgre_escape($id).", ".
	postgre_escape($rule_id).", ".
	array_to_hstore($tags->data()).", ".
	"'$version');";
    }

    $current=$current->nextSibling;
  }

  // delete old version from category_current
  if($old_version)
    $sql.="delete from category_current ".
      "where version=".postgre_escape($old_version).";";

  // set current category version
  $sql.="insert into category_current values (".
    postgre_escape($id).", ".
    "'$version', now());";

  // inform other cluster servers of new category
  if(plugins_loaded("cluster_call")) {
    $sql.="select cluster_call('category_save', ".
      postgre_escape($id).");";
  }
  else {
    categories_has_saved($id);
  }

  // we are done.
  $sql.="commit;";
  sql_query($sql, $db_central);
  return array("status"=>true, "id"=>$id, "version"=>$version);
}