<?php
/* Ulyxex version 1.6.7.8 ***************/
/* code http://ulyxex.logz.org **********/
/* Andre Lozano http://andre-lozano.org */
class PageModify extends Admin {
	public object $h;
	public object $t;
	public object $tree;
	public int $page_id;
	public string $Select;
	public string $From;
	public array $Cond;
	
	function __construct($pageId=null){
		$this->h = New Htmlz();
		$this->t = New Translate();
        $this->tree = New Tree();
		$this->page_id = $pageId;
		$this->Select = SELECT.
							PAGES.".*,".USERS.".USERNAME,".USERS.".ID USERID,".USERS.".USEROWNER";
		$this->From = 	FROM.
							PAGES.
							INNERJOIN.USERS._ON_.PAGES.".USERID = ".USERS.".ID";
		$this->Cond = array();
	}
	public function update_page(){
		$h = $this->h;$t = $this->t;
		if ($this->pageNameExist($h->safe(strip_tags($_POST['name'])),$this->page_id) && !isset($_POST['forcename'])) {
			return $h->h3($t->w("the name is already taken!"),$h->v("class","warning"));
		}
		
		//~ if page is home change home params
		$query = SELECT."PAGEID".FROM.PARAMS.WHERE."ID = 1";
		$query = $this->q($query);
		$pageHomeId = $this->fetch($query);
		$date = $h->htmlDate2Php($_POST['newdatepage']);
		
		$hide = (isset($_POST['hide']))?1:0;
		if (!empty($_FILES['addfile']["name"])) {
			$fileName = $_FILES['addfile']["name"];
			$ext = $h->get_ext($fileName);
			if ( !in_array($ext, $h->extUploadAllowed()) && $_SESSION['level'] > 1) exit("file extension not allowed ! ".$ext);
			$fileName = sprintf("%03d",rand(0,999))."_".$h->text2ascii($fileName);
			$sendFile = move_uploaded_file($_FILES['addfile']["tmp_name"],"files/".$fileName);
			if ($sendFile) {
				$query = INSERTINTO.FILES." (FILENAME,USERID,DATEFILE) VALUES (".
										"'"."files/".$fileName."',".$h->num($_POST['userid']).
										",'".$date."');";
				$query = $this->q($query);
				$addFile = $h->br($h->tag_by_fileType("files/".$fileName,$_FILES['addfile']["type"]));
			} else {
				$addFile = $h->br($h->color("file ".$fileName." not uploaded!","red"));
			}
		} else {
			$addFile = "";
		}
		if (empty($_POST['link'])) {
			if (empty($_POST['type'])) $_POST['type'] = $_POST['set_type'];
		} else {
			$_POST['type'] = "link";
		}
		if (empty($_POST['name'])){return $h->h3($t->wr("title empty !"));}
		$name = strip_tags($_POST['name']);
		//~ add to SANBOX (id2) if not parent
		if ( !isset($_POST['parent']) ) {
			$_POST['parent'] = 2;
		}
		$query = UPDATE.PAGES.SET.  "NAME = '".$h->safe($name)."',".
									"PARENTID = '".$h->num($_POST['parent'])."',".
									"DESCRIPTION = '".$h->safe($addFile).$h->safe($_POST['content'])."',".
									"TYPEPAGE = '".$h->safe($_POST['type'])."',".
									"LANG = '".$h->safe($_POST['lang'])."',".
									"LINK = '".$h->safe($_POST['link'])."',".
									"SORTSTYLE = '".$h->num($_POST['sortstyle'])."',".
									"PAGEHEAD = '".$h->safe($_POST['head'])."',".
									"HIDE = '".$hide."',".
									"DATEPAGE = '".$h->safe($date)."',".
									"PRIORITY = '".$h->num($_POST['priority'])."'";
									
		if ($_POST['userid'] != $_POST['old_userid']) $query .=	",USERID = ".$h->num($_POST['userid']); // change userId
		$query .=					WHERE."ID = ".$this->page_id;

		$queryHomeUpdate = UPDATE.PARAMS.SET."TYPEPAGE = '".$h->safe($_POST['type'])."'".WHERE."ID = 1";
		
		if ($this->tree->pathTest($page=$this->page_id,$parent=$_POST['parent'])){
			$query = $this->q($query);
			if ($pageHomeId["PAGEID"] == $this->page_id) $query = $this->q($queryHomeUpdate);
			return $h->h3($t->wr("page").$name.$t->wl("updated !"),$h->v("class","warning"));
		} else {
			return $h->h3($t->w("bad lineage !"),$h->v("class","warning"));
		}
	}
	public function data_page(){
		$h = $this->h;$t = $this->t;
		// 1) can create users etc.
		// 2) can create articles and pages
		// 3) can create articles everywhere
		// 4) can create articles only in sandbox
		switch ($_SESSION['level']){
			case 1: 
				if ($_SESSION['userid'] != 1){
					// all level 2, 3 and 4 user's pages, user's pages 
					$this->Cond[] = "(".USERS.".USERLEVEL > ".$_SESSION['level']._OR_.USERS.".ID = ".$_SESSION['userid'].")"; 
					$this->Cond[] = USERS.".ID != 1"; // but not admin articles
				}
				$this->Cond[] = PAGES.".ID = ".$this->page_id;
				break;
			case 2:
				$this->Cond[] = PAGES.".ID = ".$this->page_id;
				break;
		}
		$query = $this->Select.$this->From.$this->Where($this->Cond);
		$query = $this->q($query);
		if ($dat = $this->fetch($query)){
			$sandboxParams = "";
			if ($dat['NAME'] == "SANDBOX"){
				$sandboxParams = $h->v("readonly","readonly");
			}
			$hideParams = ($dat['HIDE'])?$h->v("checked","checked"):null;
			$optionsDate = array($t->w("date")=>0,$t->w("subject")=>1,$t->w("reverse date")=>2);
			$_[] = "<div ".$h->v("class","admincontent").">";// start admincontent
			$_[] = $h->input("hidden","page",$dat['ID']);
			$_[] = $h->input("hidden","update",1);
            $_page = $h->lbl($t->wr("child page of"),"parent").$this->tree->pathsSelect($dat['PARENTID'],1,"page");
            
            //~ modification délicate
			$_user = ($_SESSION['level'] == 1)?$h->input("hidden","old_userid",$dat['USERID']).$h->lbl($t->wr("choose user"),"userid").$this->array_users($dat['USERID']):$h->input("hidden","old_userid",$dat['USERID']).$h->input("hidden","userid",$dat['USERID']);
			
			$_[] = $h->p($_page);
			$_[] = $h->p($_user);
			$_[] = $h->p(
				$h->lbl($t->wr("redirect to link"),"link").
				$h->input("text","link",$dat['LINK'],$h->v("size",round(FIELDWIDTH/1.5)).$h->v("class","admintext").$h->alt($t->w("def redirect link"))) 
			);
			$langsNames = array(
				"en"=>"English","zh"=>"Chinese","hi"=>"Hindi","es"=>"Spanish","ar"=>"Arabic","fr"=>"French","bn"=>"Bengali",
				"ru"=>"Russian","pt"=>"Portuguese","id"=>"Indonesian"
			);
			asort($langsNames);
			foreach($langsNames as $iso=>$name) {$options[$iso." {".$name."}"] = $iso;}
			$select = $h->select($options,"langs",$dat['LANG'],$h->v("onchange","updateBasicLang(this.value)"));
			$defType = ($_SESSION['level'] < 2)?$h->lbl($t->wlr("or add new type"),"type").$h->input("text","type","",$h->v("class","admintext").$h->alt($t->w("def new type"))):"";
			$_[] = $h->p( 
				$h->lbl(
					$t->wr("type").$t->wr("page"),"set_type",$h->alt($t->w("references types pages"))
				).
				$h->select($this->getPageTypes(),"set_type",$dat['TYPEPAGE'],$h->alt($t->w("references types pages"))).
				$defType." -- ".
				$h->lbl($t->wlr("lang"),"lang").$h->input("text","lang",$dat['LANG'],$h->v("size","3").$h->v("class","centered"))." ".
				$select
			);
			$_[] = $h->p( 
				$h->lbl($t->wr("sort articles by"),"sortstyle").$h->select($optionsDate,"sortstyle",$dat['SORTSTYLE']).
				$h->lbl($t->wlr("hide").$t->wr("page"),"hide").$h->input("checkbox","hide","on",$hideParams)." -- ".
				$h->lbl($t->wr("modify date"),"newdatepage").$h->input("datetime-local","newdatepage",$h->phpDate2Html($dat['DATEPAGE'])).
				" ".
				$h->button("&#10227;",$h->v("onclick","updateDateTime('newdatepage')").$h->alt($t->w("refresh")))
			);
			$_[] = $h->p( $h->lbl($h->span(" &lt;title&gt; ",$h->v("class","htmtag")),"name") );
			$optionspriority = array($t->w("none")=>"99");
			for ($i=1; $i < 51; $i++){
				$optionspriority["^".sprintf("%'02d", $i)] = sprintf("%'02d", $i);
			}
			$priority = ($_SESSION['level'] < 2)
				?
				" -- ".$h->lbl($t->wLr("priority").$t->wr("page"),"priority").$h->select($optionspriority,"priority",$dat['PRIORITY'])
				:
				"";
			$_[] = $h->p(
				"(id:".$dat['ID'].") ".
				$h->input("text","name",$h->entities($dat['NAME']),$sandboxParams.$h->v("size",round(FIELDWIDTH/1.3)).$h->v("class","admintext").$h->alt($t->w("def title tag")))." ".
				$h->ahref(
					"index.php?k=".$dat['TYPEPAGE']."&amp;v=".$dat['ID'],
					$h->img(
						EYE,
						$h->v("width","16").$h->v("height","16").$h->v("alt","eye")
					),
					$h->v("target","blank").$h->v("class","eye").$h->alt($t->w("voir"))
				)." ".
				$priority
			);
			$_[] = $h->p( 
				$h->span(" &lt;/title&gt; ",$h->v("class","htmtag"))." ".
				$h->lbl($t->wr("allow duplicate this title"),"forcename").$h->input("checkbox","forcename")
			,$h->alt($t->w("duplicate title")));
			$_[] = $h->p( $h->lbl($h->span("&lt;head&gt;",$h->v("class","htmtag")),"head") );
			$_[] = $h->p( $h->textarea($dat['PAGEHEAD'],"head",array(3,FIELDWIDTH),$h->v("class","admintext").$h->alt($t->w("def head"))) );
			$_[] = $h->p( $h->span(" &lt;/head&gt;",$h->v("class","htmtag")) );
			$_[] = $h->p( 
				$h->span("&lt;body&gt; &lt;header&gt; ".
				$h->lbl($t->w("top content"),"content"),$h->v("class","htmtag")).
				" ".
				$h->input("submit",null,$t->w("update")),
				$h->alt($t->w("top content before articles"))
			);
			$_[] = $h->p( 
				$h->lbl($t->wr("add file"),"addfile").$h->input("file","addfile","",$h->v("size","30")).
				$t->wlr("or").
				$h->ahref("admin.php?action=list_files",$t->w("list files in a new tab"),$h->v("target","blank").$h->v("class","linkbtn")) 
			);
			$_[] = "</div>\n";// end admincontent
			$_[] = $h->div(
				$h->p(
					$h->textarea($dat['DESCRIPTION'],"content",array(20,FIELDWIDTH*2)),$h->alt($t->w("top content before articles"))
				)
			,$h->v("class","pagecontent"));
			$_[] = "<div ".$h->v("class","admincontent").">";// start admincontent
			$_[] = $h->p( 
				$h->span("&lt;/header&gt; ... ".$t->w("articles related")." ... &lt;/body&gt; ",$h->v("class","htmtag")).
				$h->input("submit",null,$t->w("update")) 
			);
			$_[] = "</div>\n";// end admincontent
			$inputs = implode("",$_);
			return "</div>\n".$h->form($inputs,"page","post","admin.php?action=modify_page","multipart/form-data")."<div ".$h->v("class","admincontent").">";
		} else {
			return $h->p("error");
		}
	}
}
?>

page url