<?php
/* Ulyxex version 1.6.7.8 ***************/
/* code http://ulyxex.logz.org **********/
/* Andre Lozano http://andre-lozano.org */
class ArticleModify extends Admin {
	public object $tree;
	public int $article_id;
	public string $Select;
	public string $From;
	public array $Cond;
	
	function __construct($artId=null){
		$this->h = New Htmlz();
		$this->t = New Translate();
		$this->tree = New Tree();
		$this->article_id = $artId;
		$this->Select = 	SELECT.
									ARTICLES.".*,".
									USERS.".USERNAME,".USERS.".ID IDUSERID,".USERS.".USERLEVEL,".
									PAGES.".NAME,".PAGES.".ID PAGEID,".PAGES.".USERID USERPAGEID";
		$this->From = 		FROM.
									ARTICLES.
									INNERJOIN.PAGES._ON_.ARTICLES.".PAGEID = ".PAGES.".ID".
									INNERJOIN.USERS._ON_.ARTICLES.".USERID = ".USERS.".ID";
		$this->Cond = array();
	}
	public function update_article(){
		$h = $this->h;$t = $this->t;
		$hide = (isset($_POST['hide']))?1:0;
		$date = $h->htmlDate2Php($_POST['newDate']);
		$addFile = "";
		if ($_SESSION['level'] < 4) {
			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"));
				}
			}
		}
		/* in case of no page created		*/
		$content = $h->safe($addFile).$h->safe($_POST['content']);
		$subject = strip_tags($_POST['subject']);
		$pageId = isset($_POST['parent'])?$h->num($_POST['parent']):2; // 2 = sandbox id
		$query = UPDATE.ARTICLES.SET."SUBJECT = '".$h->safe($subject)."'";
		if (isset($_POST['userid']) && $_POST['userid'] != $_POST['old_userid']) $query .=	",USERID = ".$h->num($_POST['userid']); // change userId
		$query .= 					",DATEPAGE = '".$h->safe($date)."'".
									",LANG = '".$h->safe($_POST['lang'])."'".
									",CONTENT = '".$content."'".
									",HIDE = ".$hide.
									",PAGEID = ".$pageId.
									WHERE."ID = ".$this->article_id;
		$query = $this->q($query);
		return $h->h3("article ".$subject." updated !",$h->v("class","warning"));
	}
	public function data_article(){
		$h = $this->h;$t = $this->t;
		switch ($_SESSION['level']){
			case 1:
				// user level 1 can't edit others user level 1
				if ($_SESSION['userid'] != 1){ // admin edit all
					$this->Cond[] = "(".USERS.".USERLEVEL > ".$_SESSION['level']._OR_.USERS.".ID = ".$_SESSION['userid'].")"; // all level 2, 3 and 4 user's articles, user's articles 
					$this->Cond[] = USERS.".ID != 1"; // but not admin articles
				}
				$this->Cond[] = ARTICLES.".ID = ".$this->article_id;
				break;
			case 2:
				$this->Cond[] = "(".USERS.".ID = ".$_SESSION['userid']._OR_.PAGES.".USERID = ".$_SESSION['userid'].")"; // user's articles and articles in user's pages
				$this->Cond[] = USERS.".ID != 1"; // but not admin articles
				$this->Cond[] = ARTICLES.".ID = ".$this->article_id;
				break;
			default:
				$this->Cond[] = ARTICLES.".USERID = ".$_SESSION['userid']; // user's articles only
				$this->Cond[] = ARTICLES.".ID = ".$this->article_id;
		}
		$query = $this->Select.$this->From.$this->Where($this->Cond);
		$query = $this->q($query);
		if ($dat = $this->fetch($query)){
			$date = $h->phpDate2Html($dat['DATEPAGE']);
			$hideParams = ($dat['HIDE'])?$h->v(" checked","checked"):null;
			
			$_[] = "<div ".$h->v("class","admincontent").">";// start admincontent
			$_[] = $h->input("hidden","update",1);
			$_[] = $h->input("hidden","art",$dat['ID']);
			$_page = ($_SESSION['level'] <= 3)
				?
				$h->lbl($t->wr("choose page"),"parent").$this->tree->pathsSelect($dat['PAGEID'],0,"page")
				:
				"page: SandBox".$h->input("hidden","pageid",2) // 2 = sandbox id
				;
				
			$_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);
			$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)"));
			$_[] = $h->p(
				$h->lbl($t->wr("hide"),"hide").$h->input("checkbox","hide","on",$hideParams)." -- ".
				$h->lbl($t->wr("modify date"),"newDate").$h->input("datetime-local","newDate",$date)." ".
				$h->button("&#10227;",$h->v("onclick","updateDateTime('newDate')").$h->alt($t->w("refresh"))).
				" -- ".$h->lbl($t->wr("lang"),"lang").$h->input("text","lang",$dat['LANG'],$h->v("size","3").$h->v("class","centered"))." ".
				$select
			);
			$_[] = $h->p(
				"(id:".$dat['ID'].") ".
				$h->lbl($h->b($t->wr("subject")),"subject").
				$h->input("text","subject",$h->entities($dat['SUBJECT']),$h->v("size",round(FIELDWIDTH/1.3)).$h->v("class","admtxtbold").
				$h->alt($t->w("def subject")))." ".
				$h->ahref(
					"index.php?k=article&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"))
				)
			);
			$_[] = $h->p(
				$h->lbl($h->span("&lt;article&gt; ",$h->v("class","htmtag")),"content").$h->input("submit",null,$t->w("update")),
				$h->alt($t->w("content of article"))
			);
			$tmp = ($_SESSION['level'] <= 3)?$h->label($t->wr("add file"),"addfile").$h->input("file","addfile","",$h->v("size","30")).$t->wlr("or"):null;
			$_[] = $h->p($tmp.$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['CONTENT'],"content",array(20,FIELDWIDTH*2),$h->v("class","admtxtbold")),
					$h->alt($t->w("content of article"))
				)
			,$h->v("class","pagecontent")
			);
			$_[] = "<div ".$h->v("class","admincontent").">";// start admincontent
			$_[] = $h->p($h->span("&lt;/article&gt; ",$h->v("class","htmtag")).$h->input("submit",null,$t->w("update")));
			$_[] = "</div>\n";// end admincontent
			$inputs = implode("",$_);
			return "</div>\n".$h->form($inputs,"article","post","admin.php?action=modify_article","multipart/form-data")."<div ".$h->v("class","admincontent").">";
		} else {
			return $h->p("error article data ");
		}
	}
}
?>

page url