Path : /var/www/html/main/
File Upload :
Current File : /var/www/html/main/function.php

<?php
/*!
 * Copyright Anucha Puangpaka
 * Contact anucha.ppk@icloud.com 062-479-9836
 */

function login($id,$user,$password){
	if(!empty($_SESSION['id']) && !empty($_SESSION['user']) && !empty($_SESSION['password'])){
		include('connDB.php');
    if(mysqli_num_rows(mysqli_query($conn,"select * from ap_users where id='$id' and user='$user' and password='$password'"))<1){
			header("Location:index.php?nu=logout");
		}else{
			header("Location:index.php?nu=dashboard");
		}
	}
}

function login_($id,$user,$password){
	if(empty($_SESSION['id']) && empty($_SESSION['user']) && empty($_SESSION['password'])){
    header("Location:index.php");
    exit;
	}else{
		include('connDB.php');
    if(mysqli_num_rows(mysqli_query($conn,"select * from ap_users where id='$id' and user='$user' and password='$password'"))<1){
			header("Location:index.php");
		}else{
			if(!empty($_SERVER["HTTP_CLIENT_IP"])){
				$ip = $_SERVER["HTTP_CLIENT_IP"];
			}elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
				$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
			}else{
				$ip = $_SERVER["REMOTE_ADDR"];
			}

			$datetime = date('Y-m-d H:i:s');
			mysqli_query($conn,"update ap_users set date_time='$datetime', login_ip='$ip' where id='$id' and login='1'");
		}
	}
}

function wordcloud($text){
  if(!empty($text)){
    $year = date('Y');
    $month = date('Ym');
    $day = date('Ymd');
    include('connDB.php');
    mysqli_query($conn,"insert into ap_wordcloud (dateTime,lang,xip,xyear,xmonth,xday,keyword,site) values ('".date('Y-m-d H:i:s')."','$_GET[lang]','$_SERVER[REMOTE_ADDR]','$year','$month','$day','$text','".urldecode($_SERVER[REQUEST_URI])."')");
  }
}

function https_to_http($text) {
  $text = str_replace("https","http",$text);
  return $text;
}

function dir_to_url($text) {
  include('ap.php');
  $text = str_replace("../../","$ap_site$ap_folder",$text);
  return $text;
}

function date_reformat($date){
	$date = date_create($date);
	echo date_format($date,"d-m-Y");
}
function date_reformat2($date){
  $date = date_create($date);
  $text = date_format($date,"Y-m-d");
  return $text;
}

function date_reformat_d($date){
	$date = date_create($date);
	echo date_format($date,"d-m H:i");
}

function date_reformat_year($date){
	$date = date_create($date);
	echo date_format($date,"Y");
}

function autolink($temp){
	if(preg_match('#(http://[^\s]+(?=\.(jpe?g|png|gif)))#i', $temp)){
		$temp = preg_replace('#(http://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<p><img src="$1.$2" title="$1.$2" width="100%" /><p>', $temp);

		$temp = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\"><font color=#FF6600>\\2@\\3</font></a>", $temp);
		$temp = preg_replace("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#is", "\\1<a href=\"\\2\" target=\"_blank\"><font color=#FF6600>\\2</font></a>", $temp);
		$temp = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\"><font color=#FF6600>\\2</font></a>", $temp);
	}elseif(preg_match('#(https://[^\s]+(?=\.(jpe?g|png|gif)))#i', $temp)){
		$temp = preg_replace('#(https://[^\s]+(?=\.(jpe?g|png|gif)))(\.(jpe?g|png|gif))#i', '<p><img src="$1.$2" title="$1.$2" width="100%" /><p>', $temp);

		$temp = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\"><font color=#FF6600>\\2@\\3</font></a>", $temp);
		$temp = preg_replace("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#is", "\\1<a href=\"\\2\" target=\"_blank\"><font color=#FF6600>\\2</font></a>", $temp);
		$temp = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\"><font color=#FF6600>\\2</font></a>", $temp);
	}else{
		$temp = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\"><font color=#FF6600>\\2@\\3</font></a>", $temp);
		$temp = preg_replace("#(^|[\n ])([\w]+?://[^ \"\n\r\t<]*)#is", "\\1<a href=\"\\2\" target=\"_blank\"><font color=#FF6600>\\2</font></a>", $temp);
		$temp = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\"><font color=#FF6600>\\2</font></a>", $temp);
	}
	return ($temp);
}

function detect_devices(){
  if(preg_match('/(iPhone|iPad|iPod|Android)/i', $_SERVER['HTTP_USER_AGENT'])){
    $device = 1;
  }else{
    $device = 2;
  }
  return $device;
}

function fb_like($fb){/*
?>
	<div class="fb-like" data-href="<?php echo $fb;?>" data-layout="button_count" data-action="like" data-size="small" data-show-faces="false" data-share="true"></div>
<?php*/
}

function fb_comments($fb){ /*
?>
	<div align="center">
		<div class="fb-comments" data-href="<?php echo $fb;?>" data-numposts="5" data-colorscheme="light"></div>
	</div>
<?php */
}

function getFileList($dir, $recurse=false, $depth=false){
  if(function_exists('mime_content_type')){
    $finfo = false;
  } else {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
  }

  $retval = array();

  if(substr($dir, -1) != "/") $dir .= "/";

  $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
  while(false !== ($entry = $d->read())) {
    if($entry[0] == ".") continue;
    if(is_dir("$dir$entry")) {
      $retval[] = array(
        'name' => "$dir$entry/",
        'type' => filetype("$dir$entry"),
        'size' => 0,
        'lastmod' => filemtime("$dir$entry")
      );
      if($recurse && is_readable("$dir$entry/")) {
        if($depth === false) {
          $retval = array_merge($retval, getFileList("$dir$entry/", true));
        } elseif($depth > 0) {
          $retval = array_merge($retval, getFileList("$dir$entry/", true, $depth-1));
        }
      }
    } elseif(is_readable("$dir$entry")) {
      $retval[] = array(
        'name' => "$dir$entry",
        'type' => ($finfo) ? finfo_file($finfo, "$dir$entry") : mime_content_type("$dir$entry"),
        'size' => filesize("$dir$entry"),
        'lastmod' => filemtime("$dir$entry")
      );
    }
  }
  $d->close();

  /*usort($retval, function($a, $b) { return $a['type'] != 'dir'? -1 : 1; });*/

  return $retval;
}

function url_parameters($text) {
  $text = str_replace("&","%26",$text);
  return $text;
}

function escapeCode($text){
	$text = str_replace("&","&amp;",$text);
	$text = str_replace("'","&apos;",$text);
	$text = str_replace('"',"&quot;",$text);
	$text = str_replace("&gt;",">",$text);
	$text = str_replace("&lt;","<",$text);
  $text = str_replace(".th//",".th/",$text);
	return $text;
}

function changeText($text){
  $text = str_replace(", ",",",$text);
  return $text;
}

function get_client_ip(){
  $ipaddress = '';
  if (getenv('HTTP_CLIENT_IP'))
    $ipaddress = getenv('HTTP_CLIENT_IP');
  else if(getenv('HTTP_X_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
  else if(getenv('HTTP_X_FORWARDED'))
    $ipaddress = getenv('HTTP_X_FORWARDED');
  else if(getenv('HTTP_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_FORWARDED_FOR');
  else if(getenv('HTTP_FORWARDED'))
    $ipaddress = getenv('HTTP_FORWARDED');
  else if(getenv('REMOTE_ADDR'))
    $ipaddress = getenv('REMOTE_ADDR');
  else
    $ipaddress = 'UNKNOWN';

  return $ipaddress;
}

function line_notify($message, $token, $id){
  if(!empty($id)){
    /*$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://notify-api.line.me/api/notify");
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "message=$message");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $headers = array("Content-type: application/x-www-form-urlencoded", "Authorization: Bearer $token",);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);*/
    $headers = array('Method: POST', 'Content-type: multipart/form-data', 'Authorization: Bearer '.$token );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://notify-api.line.me/api/notify");
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($ch);
    if(curl_error($ch)) {
      $error_ = array('status' => "Error",'massage' => curl_error($ch));
      $result_ = json_decode($error_, true);
    }else{
      $result_ = json_decode($result, true);
    }
    curl_close($ch);
    /*return $result_;*/
    include('connDB.php');
    mysqli_query($conn,"update ap_pages set counters='1' where id='$id' ");
  }
}

function line_notify_messages($message, $token){
  $headers = array('Method: POST', 'Content-type: multipart/form-data', 'Authorization: Bearer '.$token );
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://notify-api.line.me/api/notify");
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $result = curl_exec($ch);
  if(curl_error($ch)) {
    $error_ = array('status' => "Error",'massage' => curl_error($ch));
    $result_ = json_decode($error_, true);
  }else{
    $result_ = json_decode($result, true);
  }
  curl_close($ch);
}

function line_notify_questionnaire($message, $token){
  $headers = array('Method: POST', 'Content-type: multipart/form-data', 'Authorization: Bearer '.$token );
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://notify-api.line.me/api/notify");
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  $result = curl_exec($ch);
  if(curl_error($ch)) {
    $error_ = array('status' => "Error",'massage' => curl_error($ch));
    $result_ = json_decode($error_, true);
  }else{
    $result_ = json_decode($result, true);
  }
  curl_close($ch);
}

function line_push($arrayHeader,$arrayPostData){
  $strUrl = "https://api.line.me/v2/bot/message/push";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$strUrl);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $arrayHeader);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($arrayPostData));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($ch);
  curl_close ($ch);
}

function substr_utf8($str,$start_p,$len_p){  
  return preg_replace( '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start_p.'}'.  
    '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len_p.'}).*#s',  
    '$1' , $str );  
}

function hex2rgb($hex) {
  $hex = str_replace("#", "", $hex);

  if(strlen($hex) == 3) {
    $r = hexdec(substr($hex,0,1).substr($hex,0,1));
    $g = hexdec(substr($hex,1,1).substr($hex,1,1));
    $b = hexdec(substr($hex,2,1).substr($hex,2,1));
  } else {
    $r = hexdec(substr($hex,0,2));
    $g = hexdec(substr($hex,2,2));
    $b = hexdec(substr($hex,4,2));
  }
  $rgb = array($r, $g, $b);
  return $rgb;
}

function rgb2hex($rgb) {
  $hex = "#";
  $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
  $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
  $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

  return $hex;
}

function dateThai($str){
  $strYear = (date("Y",strtotime($str)))+543;
  $strMonth= date("n",strtotime($str));
  $strDay= date("j",strtotime($str));
  $strMonthCut = Array("","มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
  $strMonthThai = $strMonthCut[$strMonth];
  return "$strDay $strMonthThai $strYear";
}

function dateThai2($str){
  $strYear = date("Y",strtotime($str));
  if($strYear<2500){
    $strYear = $strYear+543;
  }
  $strMonth= date("n",strtotime($str));
  $strDay= date("j",strtotime($str));
  $strMonthCut = Array("","ม.ค.","ก.พ.","มี.ค.","เม.ย.","พ.ค.","มิ.ย.","ก.ค.","ส.ค.","ก.ย.","ต.ค.","พ.ย.","ธ.ค.");
  $strMonthThai = $strMonthCut[$strMonth];
  return "$strDay $strMonthThai $strYear";
}

function dateTimeThai($str){
  $strYear = (date("Y",strtotime($str)))+543;
  $strMonth = date("n",strtotime($str));
  $strDay = date("j",strtotime($str));
  $strTime = date("H:i:s",strtotime($str));
  $strMonthCut = Array("","มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
  $strMonthThai = $strMonthCut[$strMonth];
  return "$strDay $strMonthThai $strYear $strTime";
}

function dateThaiMonth($str){
  $strYear = (date("Y",strtotime($str)))+543;
  $strMonth = date("n",strtotime($str));
  $strDay = date("j",strtotime($str));
  $strMonthCut = Array("","มกราคม","กุมภาพันธ์","มีนาคม","เมษายน","พฤษภาคม","มิถุนายน","กรกฎาคม","สิงหาคม","กันยายน","ตุลาคม","พฤศจิกายน","ธันวาคม");
  $strMonthThai = $strMonthCut[$strMonth];
  return "$strMonthThai";
}

function social2hex($text){
  $text = str_replace(":","%3a",$text);
  $text = str_replace("/","%2f",$text);
  $text = str_replace("&","%26",$text);
  return $text;
}
function social2hex2($text){
  $text = str_replace(":","%3a",$text);
  $text = str_replace("/","%2f",$text);
  $text = str_replace("&","%26",$text);
  $text = str_replace("=","%3d",$text);
  return $text;
}

function AdSense(){/*
?>
  <p></p>
  <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  <ins class="adsbygoogle"
       style="display:block"
       data-ad-client="ca-pub-4462371339965200"
       data-ad-slot="2366910579"
       data-ad-format="auto"></ins>
  <script>
  (adsbygoogle = window.adsbygoogle || []).push({});
  </script>
  <p></p>
<?PHP*/
}

function social(){
  include('ap.php');
  $wh = "width=1000,height=600";
  ?>
  <div class="">
    <p></p>
    <a href="javascript:void(0);" onClick="window.open('https://www.facebook.com/sharer.php?u=<?php echo urlencode(substr($ap_site,0,-1)."$_SERVER[REQUEST_URI]");?>','facebook','scrollbars=yes,resizable=yes,<?php echo $wh; ?>')">
      <img src="<?php echo "$ap_site$ap_folder"; ?>contents/images/facebook.png" width="45px" alt="Facebook">
    </a>
    <a href="javascript:void(0);" onClick="window.open('https://twitter.com/intent/tweet?text=<?php echo $_GET['text'];?>&url=<?php echo social2hex(substr($ap_site,0,-1)."$_SERVER[REQUEST_URI]");?>#','twitter','scrollbars=yes,resizable=yes,<?php echo $wh; ?>')">
      <img src="<?php echo "$ap_site$ap_folder"; ?>contents/images/twitter.png" width="45px" alt="Twitter">
    </a>
    <a href="javascript:void(0);" onClick="window.open('https://plus.google.com/share?url=<?php echo social2hex(substr($ap_site,0,-1)."$_SERVER[REQUEST_URI]");?>','LINE','scrollbars=yes,resizable=yes,<?php echo $wh; ?>')">
      <img src="<?php echo "$ap_site$ap_folder"; ?>contents/images/google-plus.png" width="45px" alt="Google Plus">
    </a>
    <a href="javascript:void(0);" onClick="window.open('https://social-plugins.line.me/lineit/share?url=<?php echo social2hex(substr($ap_site,0,-1)."$_SERVER[REQUEST_URI]");?>','LINE','scrollbars=yes,resizable=yes,<?php echo $wh; ?>')">
      <img src="<?php echo "$ap_site$ap_folder"; ?>contents/images/line.png" width="45px" alt="LINE">
    </a>
    <a href="javascript:void(0);" onClick="window.open('https://www.linkedin.com/shareArticle?mini=true&url=<?php echo social2hex2(substr($ap_site,0,-1)."$_SERVER[REQUEST_URI]");?>','Linkedin','scrollbars=yes,resizable=yes,<?php echo $wh; ?>')">
      <img src="<?php echo "$ap_site$ap_folder"; ?>contents/images/linkedin.png" width="45px" alt="Linkedin">
    </a>
  </div>
  <div class="row">
  <?php /* ?><p></p>
  <div class="col-lg-12 col-md-12 col-sm-12"><?php fb_like("$ap_site"."$_SERVER[REQUEST_URI]"); ?></div><?php */ ?>
  <?php /* ?>
  <div class="col-lg-6 col-md-6 col-sm-6"><?php fb_like("$ap_site$_SERVER[REQUEST_URI]"); ?></div>
  <div class="col-lg-6 col-md-6 col-sm-6"><div class="line-it-button" data-lang="<?php $lang ?>" data-type="like" data-url="<?php echo $ap_site.$_SERVER['REQUEST_URI'] ?>" data-share="true" style="display: none;"></div></div>
  <script src="https://d.line-scdn.net/r/web/social-plugin/js/thirdparty/loader.min.js" async="async" defer="defer"></script>
  <?php */ ?>
  </div>
  <?php
}

function is_connected(){
  $fp = fsockopen("www.app.com", 443, $errno, $errstr);
  if(!$fp) {
    $text = "1";
  }else{
    $text = "0";
  }
  return $text;
}

function textReplace($text){
  $text = str_replace('((((opacCataloguing CO [','',$text);
  $text = str_replace('((((opacCataloguing ~ "','',$text);
  $text = str_replace('] ))))','',$text);
  $text = str_replace('" ))))','',$text);
  $text = str_replace('((((Subject= "','',$text);
  $text = str_replace('((((Author= " ','',$text);
  $text = str_replace('((((Author= "','',$text);
  $text = str_replace('((((Publisher= " ','',$text);
  $text = str_replace('((((Publisher= "','',$text);
  $text = str_replace(' AND (secured="" OR secured=false)','',$text);
  $text = str_replace(' AND (isHiddenInOpac="shown")','',$text);
  $text = str_replace(' AND (parentCatalogUuid="" OR isSerial="false")','',$text);
  $text = str_replace(']  AND  opacCataloguing CO [','',$text);
  $text = str_replace('AND (isHiddenInOpac<>"notShown")','',$text);
  $text = str_replace('?','',$text);
  return $text;
}

function resize($newWidth, $targetFile, $originalFile) {
  $info = getimagesize($originalFile);
  if(empty($info[0])){
    unlink($originalFile);
  }else{
    $mime = $info['mime'];

    switch ($mime) {
      case 'image/jpeg':
        $image_create_func = 'imagecreatefromjpeg';
        $image_save_func = 'imagejpeg';
        break;

      case 'image/png':
        $image_create_func = 'imagecreatefrompng';
        $image_save_func = 'imagepng';
        break;

      case 'image/gif':
        $image_create_func = 'imagecreatefromgif';
        $image_save_func = 'imagegif';
        break;

      default: 
        throw Exception('Unknown image type.');
    }

    $img = $image_create_func($originalFile);
    list($width, $height) = getimagesize($originalFile);
    $newHeight = ($height / $width) * $newWidth;
    $tmp = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

    if (file_exists($targetFile)) {
      unlink($targetFile);
    }
    $image_save_func($tmp, "$targetFile");
  }
}

function filedSort($unsorted, $column) { 
  $sorted = $unsorted; 
  for ($i=0; $i<sizeof($sorted)-1; $i++) { 
    for ($j=0; $j<sizeof($sorted)-1-$i; $j++) 
      if (($sorted[$j][$column] < $sorted[$j+1][$column])) { 
        $tmp = $sorted[$j]; 
        $sorted[$j] = $sorted[$j+1]; 
        $sorted[$j+1] = $tmp; 
    } 
  }
  return $sorted; 
}
function filedSort2($unsorted, $column, $column2) { 
  $sorted = $unsorted; 
  for ($i=0; $i < sizeof($sorted)-1; $i++) { 
    for ($j=0; $j<sizeof($sorted)-1-$i; $j++) 
      if ($sorted[$j][$column] < $sorted[$j+1][$column] && $sorted[$j][$column2] < $sorted[$j+1][$column2]) { 
        $tmp = $sorted[$j]; 
        $sorted[$j] = $sorted[$j+1]; 
        $sorted[$j+1] = $tmp; 
    } 
  }
  return $sorted; 
}
function filedSort3($unsorted, $column, $column2, $column3) { 
  $sorted = $unsorted; 
  for ($i=0; $i < sizeof($sorted)-1; $i++) { 
    for ($j=0; $j<sizeof($sorted)-1-$i; $j++) 
      if (($sorted[$j][$column] < $sorted[$j+1][$column] && $sorted[$j][$column2] < $sorted[$j+1][$column2]) && $sorted[$j][$column3] > $sorted[$j+1][$column3]) {  
        $tmp = $sorted[$j]; 
        $sorted[$j] = $sorted[$j+1]; 
        $sorted[$j+1] = $tmp; 
    } 
  }
  return $sorted; 
}
?>