<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// save new article
	// ========================================================================================
	
	
	
	$query = "select * from `".$Settings['DBPrefix']."na-Settings`";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$naSettings[$row['Name']] = $row['Value'];
	}
	
	
	$PageContents = '';
	
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_articles.gif" width="22" height="21"> '.$MsgText[7][7];
	$BrowserTitle = $MsgText[7][7];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[7][7];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[701] == 'y') {
			
			ReadFormArguments();
			Global $FormArguments;
			
			
			if ($FormArguments['PageType'] == 2 || $FormArguments['PageType'] == 3) {
				$FormArguments['PageContents'] = $FormArguments['PageContents2'];
			}
			
			$FormArguments['PageContents'] = str_replace('%7BFlexMainURL%7D','{FlexMainURL}',$FormArguments['PageContents']);	
			
			if (count($FormArguments) > 0) {
			
				if ($FormArguments['ApproveArticle'] == 'y' && $Permissions[702] == 'y') {
					$ApprovedBy = $UserNumber;
					$ApprovedDateCode = GetTimestamp();
				}
				
				
						if ($_FILES['FileData'] != '' && $_FILES['FileData']['size'] > 0) {
							// upload handling
							$FileData = $_FILES['FileData']['tmp_name'];
							$FileSize = $_FILES['FileData']['size'];
							$FileType = $_FILES['FileData']['type'];
							$PreviousName = $_FILES['FileData']['name'];
							
										
							$TypeExtensions[1] = '.gif';
							$TypeExtensions[2] = '.jpg';
							$TypeExtensions[3] = '.png';
							
							
							if (function_exists("gd_info")) {
								$GDInfo = @gd_info();
							}
							if ($GDInfo['GIF Create Support'] == true) {
								$GIFWriteSupport = true;
							}
							
							
							if ($FormArguments['NewName'] == 'Previous') {
							
								// take the previous file name and strip out any slashes
								//that windows may have allowed in the file name
								$NewFilename = str_replace('/','-',$PreviousName);
								
							}
							else if ($FormArguments['NewName'] == 'Random') {
							
								$NewFilename = sprintf("%06d", rand(0,999999)) . sprintf("%06d", rand(0,999999));
																							
								$NewFilename .= $TypeExtensions[$FileType];
										
							}
							else {
							
								// strip out any slashes that may have been entered
								$NewFilename = str_replace('/','-',$FormArguments['NewNameEnter']);
							
							} 
			
											
							$DestinationPath = './images/articles/';
							
							while (is_file($DestinationPath.$NewFilename)) {
															
								$FilenamePieces = explode('.',$NewFilename);
								$NewFilename = '';				
								for ($i = 0; $i < count($FilenamePieces)-1; $i++) {
									if ($i > 0) {
										$NewFilename .= '.';
									}
									$NewFilename .= $FilenamePieces[$i];
								}
								$NewFilename .= '1.'.$FilenamePieces[count($FilenamePieces)-1];
								
							}
				
							if (!is_file($DestinationPath.$NewFilename)){
							
								move_uploaded_file($FileData, $DestinationPath.$NewFilename);
								
								if ($Settings['FilesCHMOD'] != '') {
									@chmod($DestinationPath.$NewFilename, $Settings['FilesCHMOD']);
								}
								
								
								$FileData = $DestinationPath.$NewFilename;
								
							
								$ImageInfo = @getimagesize($FileData);
							
								$ImageWidth = $ImageInfo[0];
								$ImageHeight = $ImageInfo[1];
								$FileType = $ImageInfo[2];
							
					
								if (strtolower(substr($NewFilename,strlen($NewFilename)-4,4)) != $TypeExtensions[$FileType]) {
									$OldFilename = $NewFilename;
									$NewFilename .= $TypeExtensions[$FileType];
									$FileData = $DestinationPath.$NewFilename;
									rename($DestinationPath.$OldFilename,$DestinationPath.$NewFilename);
								}
								
								$FilenamePieces = explode('.',$NewFilename);
								$FileTypeNumber = $FileTypes[$FilenamePieces[count($FilenamePieces)-1]];
							
								for ($i = 0; $i < count($FilenamePieces)-1; $i++) {
									if ($FilenameWithoutExt != '') {
										$FilenameWithoutExt .= '.';
									}
									$FilenameWithoutExt .= $FilenamePieces[$i];
								}
								
					
					
								if (($FileType != 1 || $GIFWriteSupport != 'true') && $FileType != 2 && $FileType != 3) {
						   			$PageTitle = $MsgText[0][328];
									$PageContents = '<b><br>'.$MsgText[0][329].'<br><br>(1)</b>';
			
									@unlink($FileData);
								}
								else if ($FileSize == 0) {
							   		$PageTitle = $MsgText[0][328];
									$PageContents = '<b><br>'.$MsgText[0][330].': '.number_format($Settings['MaxImageFileSize'],0,'','').' KBytes<br><br>(2)</b>';

									@unlink($FileData);
								}
								else if ($FileSize > ($Settings['MaxImageFileSize']*1024)) {
									$PageTitle = $MsgText[0][328];
									$PageContents = '<b><br>'.$MsgText[0][331].': '.number_format($Settings['MaxImageFileSize'],0,'','').' KBytes.<br><br>(3)</b>';

									@unlink($FileData);
								}
								else if (!is_file($FileData)){
									// if the uploaded file doesn't exist on the server
						
									$PageTitle = $MsgText[0][328];
									$PageContents = '<b><br>'.$MsgText[0][332].'<br><br>(4)</b>';
			   
								}	
								else {
			   		
									$DBFileName = $NewFilename;
					
									// generate the thumbnail and medium size preview images
					
									$FileSource = $DestinationPath.$NewFilename;
		
									// thumbnail
									$NewWidth = $naSettings['ThumbMaxWidth'];
									$NewHeight = $naSettings['ThumbMaxHeight'];
					
									$WidthRatio = $ImageWidth / $NewWidth;
									$HeightRatio = $ImageHeight / $NewHeight;
								
									if ($WidthRatio >= $HeightRatio) {
									
										$NewWidthCalc = number_format($ImageWidth / $WidthRatio, 0);
										$NewHeightCalc = number_format($ImageHeight / $WidthRatio, 0);
									
									}
									else {
									
										$NewWidthCalc = number_format($ImageWidth / $HeightRatio, 0);
										$NewHeightCalc = number_format($ImageHeight / $HeightRatio, 0);
					
									}
				
					
									if ($FileType == 1) {
										$SourceData = imagecreatefromgif($FileSource);
										$Extension = '-thumb.gif';
									}
									else if ($FileType == 2) {
										$SourceData = imagecreatefromjpeg($FileSource);
										$Extension = '-thumb.jpg';
									}
									else if ($FileType == 3) {
										$SourceData = imagecreatefrompng($FileSource);
										$Extension = '-thumb.png';
									} 
					
									$DestinationImage = imagecreatetruecolor($NewWidthCalc, $NewHeightCalc);
									imagecopyresampled($DestinationImage, $SourceData, 0, 0, 0, 0, $NewWidthCalc, $NewHeightCalc, $ImageWidth, $ImageHeight);
					
					
									if ($FileType == 1) {
										imagegif($DestinationImage, $DestinationPath.$FilenameWithoutExt.$Extension);
									}
									else if ($FileType == 2) {
										imagejpeg($DestinationImage, $DestinationPath.$FilenameWithoutExt.$Extension);
									}
									else if ($FileType == 3) {
										imagepng($DestinationImage, $DestinationPath.$FilenameWithoutExt.$Extension);
									}
									
									if ($Settings['FilesCHMOD'] != '') {
										@chmod($DestinationPath.$FilenameWithoutExt.$Extension, $Settings['FilesCHMOD']);
									}
					
								
									imagedestroy($SourceData);
									imagedestroy($DestinationImage);
					
				
									if ($ImageWidth > $naSettings['MaxWidth'] || $ImageHeight > $naSettings['MaxHeight']) {

										$NewWidth = $naSettings['MaxWidth'];
										$NewHeight = $naSettings['MaxHeight'];
									
										$WidthRatio = $ImageWidth / $NewWidth;
										$HeightRatio = $ImageHeight / $NewHeight;
									
										if ($WidthRatio >= $HeightRatio) {
										
											$NewWidthCalc = number_format($ImageWidth / $WidthRatio, 0);
											$NewHeightCalc = number_format($ImageHeight / $WidthRatio, 0);
						
										}
										else {
										
											$NewWidthCalc = number_format($ImageWidth / $HeightRatio, 0);
											$NewHeightCalc = number_format($ImageHeight / $HeightRatio, 0);
						
										}
						
										if ($FileType == 1) {
											$SourceData = imagecreatefromgif($FileSource);
											$Extension = '.gif';
										}										
										else if ($FileType == 2) {
											$SourceData = imagecreatefromjpeg($FileSource);
											$Extension = '.jpg';
										}
										else if ($FileType == 3) {
											$SourceData = imagecreatefrompng($FileSource);
											$Extension = '.png';
										} 
						
										$DestinationImage = imagecreatetruecolor($NewWidthCalc, $NewHeightCalc);
										imagecopyresampled($DestinationImage, $SourceData, 0, 0, 0, 0, $NewWidthCalc, $NewHeightCalc, $ImageWidth, $ImageHeight);
										
						
										if ($FileType == 1) {
											imagegif($DestinationImage, $DestinationPath.$FilenameWithoutExt.$Extension);
										}										
										else if ($FileType == 2) {
											imagejpeg($DestinationImage, $DestinationPath.$FilenameWithoutExt.$Extension);
										}
										else if ($FileType == 3) {
											imagepng($DestinationImage, $DestinationPath.$FilenameWithoutExt.$Extension);
										} 
										
										if ($Settings['FilesCHMOD'] != '') {
											@chmod($DestinationPath.$FilenameWithoutExt.$Extension, $Settings['FilesCHMOD']);
										}
									
										//unlink($FileSource); 
								
										imagedestroy($SourceData);
										imagedestroy($DestinationImage);
						
						
										$ImageWidth = $NewWidthCalc;
										$ImageHeight = $NewHeightCalc;
										
										//$FileSize = filesize($FileSource.$Extension);
						
									
									} // end if larger than max dimensions
									else {
										
										/*
										if ($FileType == 1) {
											$Extension = '.gif';
										}
										else if ($FileType == 2) {
											$Extension = '.jpg';
										}
										else if ($FileType == 3) {
											$Extension = '.png';
										}
										
										rename($FileSource,$FileSource.$Extension);
										*/
						 					
					
									} // end if not larger than max dimensions
									
									
									$FileTypeNumber = $FileType;
									
									
									$NoUploadErrors = true;
								
								} // end if successful upload
								
							} // end if destination file doesn't exist
									
						} // end if processing upload
						else {
							// no upload attached
							$FileTypes['gif'] = 1;
							$FileTypes['jpg'] = 2;
							$FileTypes['png'] = 3;
							
							$FilenamePieces = explode('.',$FormArguments['ImagesList']);
							$FileTypeNumber = $FileTypes[$FilenamePieces[count($FilenamePieces)-1]];
							
							for ($i = 0; $i < count($FilenamePieces)-1; $i++) {
								if ($FilenameWithoutExt != '') {
									$FilenameWithoutExt .= '.';
								}
								$FilenameWithoutExt .= $FilenamePieces[$i];
							}
							
							$NoUploadErrors = true;
						}
				
				
					if ($NoUploadErrors == true) {
			
						if ($FormArguments['PublishDate'] == 'Specify') {
							$SetPublishDate = 1;
							if ($FormArguments['PDAmPm'] == 'pm') {
								$FormArguments['PDHour'] += 12;
							}
							$PublishDate = mktime($FormArguments['PDHour'], $FormArguments['PDMinute'], 0, $FormArguments['PDMonth'], $FormArguments['PDDay'], $FormArguments['PDYear']);
						}
						else {
							$SetPublishDate = 0;
							$PublishDate = GetTimestamp();
						}
			
							
						$query = "INSERT INTO `".$Settings['DBPrefix']."na-Articles` (Poster, DateCode, ApprovedBy, ApprovedDateCode, Title, Subtitle, Teaser, Article, Category, Category2, Category3, Category4, Category5, Category6, Category7, Category8, Category9, Category10, Image, ImageType, ImageCaption, ArticleFooter, Source, SourceLink, PageType, HidePostedDate, PublishDate, SetPublishDate, BrowserTitle, MetaKeywords, MetaDescription) VALUES ('".$UserNumber."', '".GetTimestamp()."', '".$ApprovedBy."', '".$ApprovedDateCode."', '".$FormArguments['Title']."', '".$FormArguments['Subtitle']."', '".$FormArguments['Teaser']."', '".$FormArguments['PageContents']."', '".$FormArguments['Category']."', '".$FormArguments['Category2']."', '".$FormArguments['Category3']."', '".$FormArguments['Category4']."', '".$FormArguments['Category5']."', '".$FormArguments['Category6']."', '".$FormArguments['Category7']."', '".$FormArguments['Category8']."', '".$FormArguments['Category9']."', '".$FormArguments['Category10']."', '".$FilenameWithoutExt."', '".$FileTypeNumber."', '".$FormArguments['ImageCaption']."', '".$FormArguments['ArticleFooter']."', '".$FormArguments['Source']."', '".$FormArguments['SourceLink']."', '".$FormArguments['PageType']."', '".$FormArguments['HidePostedDate']."', '".$PublishDate."', '".$SetPublishDate."', '".$FormArguments['BrowserTitle']."', '".$FormArguments['MetaKeywords']."', '".$FormArguments['MetaDescription']."')";
					  	$result = mysql_query($query) or die (mysql_error());
		
				
						header('Location: ' . $MainURL . '/admin/articles.html');
						
					}
			
			
			
			}
			else {
				$PageContents = $MsgText[0][98];
			}


		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

	// ========================================================================================




?>
