PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/funcs.php

https://bitbucket.org/Balancer/bors-core
PHP | 290 lines | 189 code | 61 blank | 40 comment | 51 complexity | 1812d483016a0cb34325ad435b8d183e MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. /*
  3. Подключать по bors_funcs::noop();
  4. */
  5. function class_include($class_name, &$args = array()) { return bors_class_loader::load_file($class_name, $args); }
  6. function mkpath($strPath, $mode=0777)
  7. {
  8. if(!$strPath || is_dir($strPath) || $strPath=='/')
  9. return true;
  10. if(!($pStrPath = dirname($strPath)))
  11. return true;
  12. if(!mkpath($pStrPath, $mode))
  13. return false;
  14. $err = @mkdir($strPath, $mode);
  15. @chmod($strPath, $mode);
  16. return $err;
  17. }
  18. function register_vhost($host, $documents_root=NULL, $bors_host=NULL)
  19. {
  20. $host = preg_replace('/^www\./', '', $host);
  21. global $bors_data;
  22. if(empty($documents_root))
  23. $documents_root = '/var/www/'.$host.'/htdocs';
  24. if(empty($bors_host))
  25. {
  26. $bors_host = dirname($documents_root).'/bors-host';
  27. $bors_site = dirname($documents_root).'/bors-site';
  28. }
  29. else
  30. $bors_site = $bors_host;
  31. $map = array();
  32. if(defined('BORS_LOCAL') && file_exists($file = BORS_LOCAL.'/vhosts/'.$host.'/handlers/bors_map.php'))
  33. require_once($file);
  34. elseif(file_exists($file = BORS_CORE.'/vhosts/'.$host.'/handlers/bors_map.php'))
  35. require_once($file);
  36. $map2 = $map;
  37. if(file_exists($file = $bors_site.'/handlers/bors_map.php'))
  38. require_once($file);
  39. if(file_exists($file = $bors_site.'/bors_map.php'))
  40. require_once($file);
  41. if(file_exists($file = $bors_site.'/url_map.php'))
  42. require_once($file);
  43. if(file_exists($file = $bors_host.'/handlers/bors_map.php'))
  44. require_once($file);
  45. if(empty($bors_data['vhosts'][$host]['bors_map']))
  46. $prev = array();
  47. else
  48. $prev = $bors_data['vhosts'][$host]['bors_map'];
  49. $bors_map = array_merge($prev, $map2, $map);
  50. if(!empty($bors_data['vhosts'][$host]['bors_map']))
  51. $bors_map = array_merge($bors_data['vhosts'][$host]['bors_map'], $bors_map);
  52. $bors_data['vhosts'][$host] = array(
  53. 'bors_map' => $bors_map,
  54. 'bors_local' => $bors_host,
  55. 'bors_site' => $bors_site,
  56. 'document_root' => $documents_root,
  57. );
  58. }
  59. /**
  60. * Извлекает поле $name из массива $data, если оно есть.
  61. * В противном случае возвращает $default.
  62. * @param array $data
  63. * @param string $name
  64. * @param mixed $default
  65. * @return mixed
  66. */
  67. function defval($data, $name, $default=NULL)
  68. {
  69. if($data && array_key_exists($name, $data))
  70. return $data[$name];
  71. return $default;
  72. }
  73. /**
  74. * Работает как и defval(), но при отсутствии
  75. * соответствующего элемента массива он создаётся в нём.
  76. * @param array $data
  77. * @param string $name
  78. * @param mixed $default
  79. * @return mixed
  80. */
  81. function defvalset(&$data, $name, $default=NULL)
  82. {
  83. if($data && array_key_exists($name, $data))
  84. return $data[$name];
  85. return $data[$name] = $default;
  86. }
  87. /**
  88. * Аналогично defval(), но удаляет из массива данных извлечённое значение
  89. * @param array $data
  90. * @param string $name
  91. * @param mixed $default
  92. * @return mixed
  93. */
  94. function popval(&$data, $name, $default=NULL)
  95. {
  96. if(!$data || !is_array($data) || !array_key_exists($name, $data))
  97. return $default;
  98. $ret = $data[$name];
  99. unset($data[$name]);
  100. return $ret;
  101. }
  102. /**
  103. * Аналогично defval(), но читается только непустое значение.
  104. * @param array $data
  105. * @param string $name
  106. * @param mixed $default
  107. * @return mixed
  108. */
  109. function defval_ne(&$data, $name, $default=NULL)
  110. {
  111. if(!empty($data[$name]))
  112. return $data[$name];
  113. return $default;
  114. }
  115. /**
  116. Устновить элемент массива $name в переменную $value, если он до этого не определён
  117. */
  118. function set_def(&$data, $name, $value)
  119. {
  120. if($data && array_key_exists($name, $data))
  121. return $data[$name];
  122. return $data[$name] = $value;
  123. }
  124. function bors_dirs($skip_config = false, $host = NULL)
  125. {
  126. static $dirs = NULL;
  127. if(!$host)
  128. $host = @$_SERVER['HTTP_HOST'];
  129. if(isset($dirs[$skip_config][$host]))
  130. return $dirs[$skip_config][$host];
  131. $vhost = '/vhosts/'.$host;
  132. $data = array();
  133. if(!$skip_config && defined('BORS_APPEND'))
  134. $data = array_merge($data, explode(' ', BORS_APPEND));
  135. if(defined('BORS_SITE') && is_dir(BORS_SITE))
  136. $data[] = BORS_SITE;
  137. if(defined('BORS_HOST') && is_dir(BORS_HOST))
  138. $data[] = BORS_HOST;
  139. if(defined('BORS_LOCAL'))
  140. {
  141. if(is_dir(BORS_LOCAL.$vhost))
  142. $data[] = BORS_LOCAL.$vhost;
  143. if(is_dir(BORS_LOCAL))
  144. $data[] = BORS_LOCAL;
  145. }
  146. if(defined('BORS_EXT') && is_dir(BORS_EXT))
  147. $data[] = BORS_EXT;
  148. $data[] = dirname(__DIR__); // BORS_CORE
  149. if(defined('BORS_3RD_PARTY') && is_dir(BORS_3RD_PARTY))
  150. $data[] = BORS_3RD_PARTY;
  151. if(!empty($GLOBALS['bors_data']['projects']))
  152. foreach($GLOBALS['bors_data']['projects'] as $project_name => $x)
  153. $data[] = $x['project_path'];
  154. return $dirs[$skip_config][$host] = array_unique(array_filter($data));
  155. }
  156. if(empty($GLOBALS['cms']) || empty($GLOBALS['cms']['config']))
  157. $GLOBALS['cms']['config'] = array();
  158. function config_set_ref($key, &$value) { $GLOBALS['cms']['config'][$key] = $value; }
  159. if(!function_exists('config_set'))
  160. {
  161. function config_set($key, $value) { return $GLOBALS['cms']['config'][$key] = $value; }
  162. }
  163. if(!function_exists('config'))
  164. {
  165. // Не максировать через @!
  166. function config($key, $def = NULL)
  167. {
  168. if(array_key_exists($key, $GLOBALS['cms']['config']))
  169. return $GLOBALS['cms']['config'][$key];
  170. return $def;
  171. }
  172. }
  173. function config_seth($section, $hash, $key, $value) { return $GLOBALS['cms']['config'][$section][$hash][$key] = $value; }
  174. // Не максировать через @!
  175. function configh($section, $hash, $key, $def = NULL)
  176. {
  177. return !empty($GLOBALS['cms']['config'][$section][$hash]) && array_key_exists($key, $GLOBALS['cms']['config'][$section][$hash])
  178. ? $GLOBALS['cms']['config'][$section][$hash][$key]
  179. : $def;
  180. }
  181. function mysql_access($db, $login = NULL, $password = NULL, $host='localhost')
  182. {
  183. if(preg_match('/^(\w+)=>([\w\-]+)$/', nospace($db), $m))
  184. {
  185. $db = $m[1];
  186. $db_real = $m[2];
  187. }
  188. else
  189. $db_real = $db;
  190. $conn = config('__database_connections', array());
  191. $conn[$db] = array(
  192. 'host' => $host,
  193. 'database' => $db_real,
  194. 'username' => $login,
  195. 'password' => $password,
  196. );
  197. config_set('__database_connections', $conn);
  198. $GLOBALS["_bors_conf_mysql_{$db}_db_real"] = $db_real;
  199. $GLOBALS["_bors_conf_mysql_{$db}_login"] = $login;
  200. $GLOBALS["_bors_conf_mysql_{$db}_password"]= $password;
  201. $GLOBALS["_bors_conf_mysql_{$db}_server"] = $host;
  202. }
  203. function nospace($str) { return str_replace(' ', '', $str); }
  204. if(function_exists('mb_strtolower') && strtolower(ini_get('default_charset')) == 'utf-8')
  205. {
  206. // Фиг его знает, с чего PHP без этого перестал работать в консоли
  207. // Маскируем, ибо DEPRECATED
  208. @ini_set('mbstring.internal_encoding', 'UTF-8');
  209. function bors_upper($str) { return mb_strtoupper($str); }
  210. function bors_lower($str) { return mb_strtolower($str); }
  211. function bors_strlen($str) { return mb_strlen($str); }
  212. function bors_substr($str, $start, $length=NULL) { return is_null($length) ? mb_substr($str, $start) : mb_substr($str, $start, $length); }
  213. function bors_strpos($str, $need, $start=NULL) { return is_null($start) ? mb_strpos($str, $need) : mb_strpos($str, $need, $start); }
  214. function bors_strrpos($str, $need, $start=NULL) { return is_null($start) ? mb_strrpos($str, $need) : mb_strrpos($str, $need, $start); }
  215. function bors_stripos($str, $need, $start=NULL) { return is_null($start) ? mb_stripos($str, $need) : mb_stripos($str, $need, $start); }
  216. function bors_ucfirst($str) { return mb_substr(mb_strtoupper($str), 0, 1).mb_substr(mb_strtolower($str), 1); }
  217. }
  218. else
  219. {
  220. function bors_lower($str) { return strtolower($str); }
  221. function bors_upper($str) { return strtoupper($str); }
  222. function bors_strlen($str) { return strlen($str); }
  223. function bors_substr($str, $start, $length=NULL) { return is_null($length) ? substr($str, $start) : substr($str, $start, $length); }
  224. function bors_strpos($str, $need, $start=NULL) { return is_null($start) ? strpos($str, $need) : strpos($str, $need, $start); }
  225. function bors_strrpos($str, $need, $start=NULL) { return is_null($start) ? strrpos($str, $need) : strrpos($str, $need, $start); }
  226. function bors_stripos($str, $need, $start=NULL) { return is_null($start) ? stripos($str, $need) : stripos($str, $need, $start); }
  227. function bors_ucfirst($str) { return ucfirst($str); }
  228. }
  229. eval('class bors_log extends '.config('log.class', 'bors_log_stub').' { } ');