Your IP : 216.73.217.64


Current Path : /home3/gp97/docs.sapat20172020.97gp.net/php/
Upload File :
Current File : //home3/gp97/docs.sapat20172020.97gp.net/php/functions.php

<?php
/* *************************************************
 *
 * Document Manager (C) 2002-2010
 * Version 4 - dmanager MySQL
 * Benjamin Sonntag <benjamin@sonntag.fr>
 * Chantal Bernard-Putz <chantal@bernard-putz.net>
 * Franck Denis
 * This software is protected by copyright laws,
 * please read the file COPYRIGHT
 * This software is provided as is with no warranty.
 *
 ************************************************* */

// Affiche un tableau en concervant son formatage
function printVar($array)
{
	echo "<pre style=\"border: 1px solid black; font-size: 11px; padding: 3px; background-color: #DDDDDD;\">\n";
	print_r($array);
	echo "</pre>\n";
}

function headerLocation($url, $external = false)
{
	global $customErrorHandler, $phpErrorList;

	if ($customErrorHandler && !empty($phpErrorList))
		echo str_pad("&nbsp;", 4096);

	if ($external)
		$location = $url;
	else
		$location = ROOT_HTTP . $url;

	header ("Location: " . $location);
	displayPhpError(false);
	exit();
}

// Addslash une variable
function quote($value)
{
	if (!is_numeric($value))
		$value = addslashes($value);

	return $value;
}


function getFields($fields, $requestOnly = false)
{
  $vars = array();
  $methodType = array ("get", "post", "request");

  foreach ($fields AS $name => $options)
    {
      if (in_array($options[0], $methodType) === false)
	die ("Illegal method type used for field " . $name . " : " . $options[0]);


      // Here start modification for PHP 5.3 compatibility
      if ($requestOnly === true)
	die('Illegal method REQUEST, do NOT use getfield with a second argument');
      //$method = "_REQUEST";
    else
      $method = "_" . strtoupper($options[0]);

      if ( $method == "_REQUEST") {
	if ( isset($GLOBALS["_GET"][$name] ) ) {
	  $method="_GET";
	} elseif ( isset($GLOBALS["_POST"][$name]) ) {
	  $method="_POST";
	}
      }
      // Here ends modification for PHP 5.3 compatibility. No more in this file.

      switch ($options[1])
	{
	case "integer":

	  $vars[$name] = (isset($GLOBALS[$method][$name]) && is_numeric($GLOBALS[$method][$name]) ? intval($GLOBALS[$method][$name]) : $options[2]);
	  break;

	case "float":

	  $vars[$name] = (isset($GLOBALS[$method][$name]) && is_numeric($GLOBALS[$method][$name]) ? floatval($GLOBALS[$method][$name]) : $options[2]);
	  break;

	case "string":

	  $vars[$name] = (isset($GLOBALS[$method][$name]) ? trim($GLOBALS[$method][$name]) : $options[2]);
	  break;

	case "array":

	  $vars[$name] = (isset($GLOBALS[$method][$name]) && is_array($GLOBALS[$method][$name]) ? $GLOBALS[$method][$name] : $options[2]);
	  break;

	case "boolean":

	  $vars[$name] = (isset($GLOBALS[$method][$name]) ? $GLOBALS[$method][$name] : $options[2]);
	  break;

	default:
	  die ("Illegal method type used for field " . $name . " : " . $options[1]);
	}
    }

  // Insert into $GLOBALS
  foreach ($vars AS $var => $value)
    $GLOBALS[$var] = $value;

  //  return $vars;
}


function ifDefine($key, $value)
{
	if (!defined($key))
		define($key, $value);
}

$queryCount = 0;
// Ex�cute une requete
function query($q)
{
	global $queryCount;
	$r = mysql_query($q);
	if ($r === false)
		die ("Invalid query: " . mysql_error() . "<br />Query: " . $q);

	$queryCount++;
	return $r;
}

// Ex�cute une requete et renvoie les r�sultats dans un tableau
function queryList($q)
{
	$res = array();
	$r = query($q);

	if (mysql_num_rows($r))
	{
		while ($n = mysql_fetch_array($r))
			$res[] = $n;
	}

	return $res;
}

// Ex�cute une requete et renvoie le premier champ des r�sultats dans un tableau
function queryListOne($q)
{
	$res = array();
	$r = query($q);

	if (mysql_num_rows($r))
	{
		while ($n = mysql_fetch_array($r))
			$res[] = $n[0];
	}

	return $res;
}

function removeExtension($fileName)
{

}

// Ex�cute une requete et renvoie les r�sultats dans un tableau associatif (champ0 => champ1)
function queryAssoc($q)
{
	$res = array();
	$r = query($q);

	if (mysql_num_rows($r))
	{
		while ($n = mysql_fetch_array($r))
			$res[$n[0]] = $n[1];
	}

	return $res;
}

// Ex�cute une requete et renvoie le r�sultat dans un tableau
function queryOne($q)
{
	$res = array();
	$r = query($q);

	if (mysql_num_rows($r))
	{
		while ($n = mysql_fetch_array($r))
			$res = $n;
	}

	return $res;
}

// Ex�cute une requete et renvoie le r�sultat
function queryOneField($q)
{
	$res = "";
	$r = query($q);

	if (mysql_num_rows($r))
		list($res) = mysql_fetch_array($r);

	return $res;
}

// Permet de rechercher r�cursivement dans un tableau un couple cl� => valeur
function findInArray($key, $value, $array, $returnValue = false)
{
	if (is_array($array))
	{
		foreach ($array AS $tKey => $tValue)
		{
			if (is_array($tValue))
			{
				if (findInArray($key, $value, $tValue))
				{
					if ($returnValue)
					{
						if (isset($tValue[$returnValue]))
							return $tValue[$returnValue];
						else
							return false;
					}
					else
						return true;
				}
			}
			else
			{
				if ($tKey == $key && $tValue == $value)
					return true;
			}
		}
	}

	return false;
}

// V�rifie une adresse email
function checkEmail($email, $dns = false)
{
	$atom = '[-a-z0-9!#$%&\'*+/=?^_`{|}~]';
	$domain = '([a-z]([-a-z0-9]*[a-z0-9]+)?)';
	$regex = '^' . $atom . '+' . '(\.' . $atom . '+)*' . '@' . '(' . $domain . '{1,63}\.)+' . $domain . '{2,63}' . '$';

	if (eregi($regex, $email))
	{
		if ($dns)
		{
			if (checkdnsrr(array_pop(explode("@", $email)), "MX"))
				return true;
			else
				return false;
		}

		return true;
	}

	return false;
}

function eOption($values, $current, $opt = "")
{
  if (is_array($values))
	{
		foreach ($values AS $k => $v)
		{
			echo "<option value=\"" . $k . "\"";
			if ($k == $current) echo " selected=\"selected\"";
			echo ">" . $v . "</option>";
		}
	}
	else
	{
		if (is_array($opt))
			$res = query("SELECT " . $opt[0] . ", " . $opt[1] . " FROM " . $values . " ORDER BY " . $opt[0]);
		else
      $res = query("SELECT * FROM " . $values . " ORDER BY 2");

    foreach ($res AS $data)
		{
      echo "<option value=\"" . current($data) . "\"";
      if (current($data) == $current) echo " selected=\"selected\"";
      echo ">" . next($data) . "</option>";
    }
  }
}

function formatSize($size)
{
	$size = (float) $size;
	if ($size < 1024)
		$r = $size . "&nbsp;o";
	else
	{
		$size = $size / 1024;
		if ($size < 1024)
			$r = round($size, 2) . "&nbsp;Ko";
		else
		{
			$size = $size / 1024;
			if ($size < 1024)
				$r = round($size, 2) . "&nbsp;Mo";
			else
			{
				$size = $size / 1024;
				if ($size < 1024)
					$r = round($size, 2) . "&nbsp;Go";
				else
					$r = round($size / 1024, 2) . "&nbsp;To";
			}
		}
	}
	return $r;
}

function dateMy2fr($date)
{
	return ereg_replace("^([0-9]{4})-([0-9]{2})-([0-9]{2})$", "\\3/\\2/\\1", $date);
}

function dateFr2my($date)
{
	return ereg_replace("^([0-9]{2})/([0-9]{2})/([0-9]{4})$", "\\3-\\2-\\1", $date);
}

function dateTimeMy2fr($date)
{
	return preg_replace("#^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})$#", "\\3/\\2/\\1 \\4:\\5:\\6", $date);
}

function dateTs2fr($ts)
{
	return date("d/m/Y", $ts);
}

function dateTs2my($ts)
{
	return date("Y-m-d", $ts);
}

function dateFr2ts($date)
{
	return strtotime(dateFr2my($date));
}

function dateMy2ts($date)
{
	return strtotime($date);
}

function maxLen($str, $maxLength = 200, $more = " ... ")
{
	if (strlen($str) > $maxLength)
		return substr($str, 0, $maxLength) . $more;

	return $str;
}

function he($str)
{
	return htmlentities($str);
}

function hsc($str)
{
	return htmlspecialchars($str);
}

function pager($offset, $count, $total, $url, $class = "", $before = "", $after = "")
{
  // On nettoie les variables hein ...
  $offset = intval($offset);
  $count = intval($count);
  $total = intval($total);
  if ($offset <= 0) $offset = "0";
  if ($count <= 1) $count = "1";
  if ($total <= 0) $total = "0";
  if ($total < $offset) $offset = max(0, $total - $count);
	$class = ($class ? " class=\"" . $class . "\"" : "");

  if ($total <= $count)	// Cas o� l'on n'a pas assez d'�l�ments pour afficher un pager :)
    return true;

	echo $before;
	// Doit-on afficher Page pr�c�dente ?
	if ($offset)
	{
		$o = max(0, $offset - $count);
		echo "<a href=\"" . str_replace("%%offset%%", $o, $url) . "\"" . $class . " alt=\"(Ctl/Alt-p)\" title=\"(Alt-p)\" accesskey=\"p\">Pr�c�dente</a> ";
	}
	else
    echo "Pr�c�dente ";

	if ($total > (2 * $count)) // On n'affiche le pager central (0 1 2 ...) s'il y a au moins 2 pages.
	{
		echo " - ";
		if (($total < ($count * 10)) && ($total > $count)) // moins de 10 pages :
		{
			for ($i = 1; $i < $total / $count; $i++)
			{
				$o = $i * $count;
				if ($offset == $o)
					echo ($i + 1) . " ";
				else
					echo "<a href=\"" . str_replace("%%offset%%", $o, $url) . "\"" . $class . ">" . ($i + 1) . "</a> ";
			}
		}
		else // Plus de 10 pages, on affiche 0 1 2 , 2 avant et 2 apr�s la page courante, et les 3 dernieres
		{
			for($i = 0; $i < 3; $i++)
			{
				$o = $i * $count;
				if ($offset == $o)
					echo ($i + 1) . " ";
				else
					echo "<a href=\"" . str_replace("%%offset%%", $o, $url) . "\"" . $class . ">" . ($i + 1) . "</a> ";
			}

			if ($offset >= $count && $offset < ($total - 2 * $count)) // On est entre les milieux ...
			{
				// On affiche 2 avant jusque 2 apr�s l'offset courant mais sans d�border sur les indices affich�s autour
				$start = max(3, intval($offset / $count) - 2);
				$end = min(intval($offset / $count) + 3, intval($total / $count) - 3);
				if ($start != 3)
					echo " ... ";
				for ($i = $start; $i < $end; $i++)
				{
					$o = $i * $count;
					if ($offset == $o)
						echo ($i + 1) . " ";
					else
						echo "<a href=\"" . str_replace("%%offset%%", $o, $url) . "\"" . $class . ">" . ($i + 1) . "</a> ";
				}

				if ($end != intval($total / $count) - 3)
					echo " ... ";
			}
			else
				echo " ... ";

			for ($i = intval($total / $count) - 3; $i < $total / $count; $i++)
			{
				$o = $i * $count;
				if ($offset == $o)
					echo ($i + 1) . " ";
				else
					echo "<a href=\"" . str_replace("%%offset%%", $o, $url) . "\"" . $class . ">" . ($i + 1) . "</a> ";
			}
			echo " - ";
		} // PLUS de 10 Pages ?
  }

  // Doit-on afficher Page suivante ?
  if ($offset + $count < $total)
	{
		$o = $offset + $count;
		echo "<a href=\"" . str_replace("%%offset%%", $o, $url) . "\"" . $class . " alt=\"(Ctl/Alt-s)\" title=\"(Alt-s)\" accesskey=\"s\">Suivante</a> ";
	}
	else
		echo "Suivante ";

	echo $after;
}

// Permet de trier un tableau sur plusieurs crit�res
function arraySort()
{
	$args = func_get_args();
	$mArray = array_shift($args);
	$i = 0;
	$mSortLine = "return(array_multisort(";
	foreach ($args AS $arg)
	{
		$i++;
		if (is_string($arg))
		{
			foreach ($mArray AS $row)
				$sortArr[$i][] = strtolower($row[$arg]);
		}
		else
			$sortArr[$i] = $arg;

		$mSortLine .= "\$sortArr[" . $i . "],";
	}

	$mSortLine .= "\$mArray));";
	eval($mSortLine);
	return $mArray;
}

function isEmpty($var)
{
	if (is_bool($var))
		return false;

	if (is_array($var))
	{
		if (empty($var))
			return true;
		else
			return false;
	}
	else
	{
		if (trim($var) == "")
			return true;
		else
			return false;
	}
}


  /** 
   * Send anonymous statistics of your document manager to the product creators 
   * We only send a small message at install time,
   * and the number of document in the document manager once a year.
   * You can see the statistics on our website at dmanager.org :) 
   **/
  function sendAnonymousStats($isinstall=0) {
    global $doc;
    if ($isinstall) {
      $mode="install";
      $count="";
    } else {
      // Shall we send them or not ? 
      if (date('Y')==date('Y',filemtime($doc->root."files/.htaccess"))) {
	return false;
      }
      @touch($doc->root."files/.htaccess");
      $count=queryOneField("SELECT COUNT(*) FROM " . TABLE_PREFIX . "files;");
      $mode="yearly";
    }
    $sock = @fsockopen("91.194.60.2", "80", $errno, $errstr, 4); // timeout 4 sec.  
    socket_set_blocking($sock,false);
    @fputs($sock,"GET /stat/".$mode."/".md5($_SERVER["HTTP_HOST"])."/".$count." HTTP/1.0\r\n" . 
	   "Host: www.dmanager.org\r\n" .
	   "User-Agent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.8) Gecko/20090201 Debian/3.0.0.5-4\r\n" .
	   "\r\n");
    sleep(1);
    if ($sock && !feof($sock)) {
      @fgets($sock,1024);
    }
    @fclose($sock);
  }



if (file_exists("my_functions.php"))
	include ("my_functions.php");


?>