common.inc.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. include SITEADM_PRIVATE_DIR."/include/autoload.inc.php";
  3. include SITEADM_PRIVATE_DIR."/include/db.inc.php";
  4. /*
  5. * INFOS UTILES
  6. */
  7. $civilite_list = array
  8. (
  9. "m"=>"Monsieur",
  10. "mme"=>"Madame",
  11. "mlle"=>"Mademoiselle"
  12. );
  13. /*
  14. * GESTION DE COMPTE
  15. */
  16. $account_type_list = array
  17. (
  18. "user"=>"Utilisateur",
  19. "manager"=>"Manager",
  20. "admin"=>"Administrateur"
  21. );
  22. /*
  23. * LISTE DES OFFRES
  24. */
  25. $offre_list = array();
  26. $query = mysql_query("SELECT * FROM offre");
  27. while($row=mysql_fetch_assoc($query))
  28. {
  29. $offre_list[$row["id"]] = $row;
  30. }
  31. /*
  32. * Template Files
  33. */
  34. // Common replacements for templates
  35. function replace_map()
  36. {
  37. return array
  38. (
  39. "{SITEADM_SCRIPT_DIR}" => SITEADM_SCRIPT_DIR,
  40. "{INIT_SCRIPT_DIR}" => INIT_SCRIPT_DIR,
  41. "{ROOT_EMAIL}" => ROOT_EMAIL,
  42. "{POSTMASTER_EMAIL}" => POSTMASTER_EMAIL,
  43. "{WEBMASTER_EMAIL}" => WEBMASTER_EMAIL
  44. );
  45. }
  46. /**
  47. * Merge 2 maps, replacing in first map only if we have not null values in second map
  48. * @param array $map
  49. * @param array $map_merge
  50. */
  51. function replace_map_merge(&$map, $map_merge)
  52. {
  53. if (!is_array($map) || !is_array($map_merge))
  54. return;
  55. foreach($map_merge as $key=>$value)
  56. {
  57. if (!isset($map[$key]) || ($value !== null))
  58. $map[$key] = $value;
  59. }
  60. }
  61. /**
  62. * Copy a template file to specified location
  63. * Used in account and common
  64. * @param string $file_from
  65. * @param string $file_to
  66. * @param [] $replace_map
  67. * @param string $mode
  68. * @param int $user_id
  69. */
  70. function copy_tpl($file_from, $file_to, $replace_map=array(), $mode="0644", $usergroup=null)
  71. {
  72. echo "GENERATING TEMPLATE : $file_to ...\n";
  73. // MAP
  74. $replace_from = array();
  75. $replace_to = array();
  76. if (is_array($replace_map)) foreach($replace_map as $i=>$j)
  77. {
  78. $replace_from[] = $i;
  79. $replace_to[] = $j;
  80. }
  81. $filecontents_from = file_get_contents(SITEADM_TEMPLATE_DIR."/$file_from");
  82. $filecontents_to = str_replace($replace_from, $replace_to, $filecontents_from);
  83. // Write
  84. filesystem::write($file_to, $filecontents_to);
  85. // CHOWN
  86. if (!is_null($usergroup))
  87. filesystem::chown($file_to, $usergroup);
  88. // CHMOD
  89. filesystem::chmod($file_to, $mode);
  90. }
  91. // Divers
  92. /**
  93. * Returns a password
  94. * @param int $length
  95. * @return string
  96. */
  97. function password_create($length=8)
  98. {
  99. $specialchars = "-+*$%!?:";
  100. $numbers = "0123456789";
  101. $lettersl = "abcdefghijklmnopqrstuvwxyz";
  102. $lettersu = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  103. $chars = "$numbers$lettersl$lettersu$specialchars";
  104. $chars_nb = strlen($chars);
  105. $ok = false;
  106. while (!$ok)
  107. {
  108. $passwd = "";
  109. for ($i=0; $i < $length; $i++)
  110. $passwd .= $chars{mt_rand(0,$chars_nb)};
  111. $okl = array("specialchars"=>0, "numbers"=>0, "lettersl"=>0, "lettersu"=>0);
  112. foreach($okl as $name=>$nb)
  113. {
  114. for ($i=0; $i < strlen(${$name}); $i++)
  115. if (strpos($passwd, ${$name}{$i}))
  116. $okl[$name]++;;
  117. }
  118. $ok = true;
  119. foreach($okl as $name=>$nb)
  120. {
  121. $ok = ($ok && $nb);
  122. }
  123. }
  124. return $passwd;
  125. }
  126. /**
  127. * Execute a script in the background
  128. * @param string $command
  129. * @param string $params
  130. */
  131. function script_exec($command, $params="")
  132. {
  133. if (!$command || is_numeric(strpos($command, "/")))
  134. return;
  135. if (file_exists(SITEADM_SCRIPT_DIR."/".$command))
  136. exec(SITEADM_SCRIPT_DIR."/".$command." ".$params." > /dev/null 2>/dev/null &");
  137. elseif (file_exists(INIT_SCRIPT_DIR."/".$command))
  138. exec(INIT_SCRIPT_DIR."/".$command." ".$params." > /dev/null 2>/dev/null &");
  139. }
  140. ?>