Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

264 строки
5.4 KiB

  1. <?php
  2. /**
  3. * pick values from an array
  4. *
  5. * @param array $array input array
  6. * @param string|integer $key key
  7. * @param boolean $keepIndex if keep index
  8. * @return array
  9. * @since 1.0
  10. */
  11. function rock_array_pick($array, $key, $keepIndex = false) {
  12. if (!is_array($array)) {
  13. return array();
  14. }
  15. $ret = array();
  16. foreach ($array as $index => $row) {
  17. if (is_array($row)) {
  18. $value = rock_array_get($row, $key);
  19. if ($keepIndex) {
  20. $ret[$index] = $value;
  21. }
  22. else {
  23. $ret[] = $value;
  24. }
  25. }
  26. }
  27. return $ret;
  28. }
  29. /**
  30. * sort multiple-array by key
  31. *
  32. * @param array $array array to sort
  33. * @param mixed $key string|array
  34. * @param boolean $asc if asc
  35. * @return array
  36. */
  37. function rock_array_sort(array $array, $key = null, $asc = true) {
  38. if (empty($array)) {
  39. return $array;
  40. }
  41. if (empty($key)) {
  42. $asc ? asort($array) : arsort($array);
  43. }
  44. else {
  45. $GLOBALS["ROCK_ARRAY_SORT_KEY_" . nil] = $key;
  46. uasort($array,
  47. $asc ? create_function('$p1,$p2', '$key=$GLOBALS["ROCK_ARRAY_SORT_KEY_" . nil];$p1=rock_array_get($p1,$key);$p2=rock_array_get($p2,$key);if ($p1>$p2){return 1;}elseif($p1==$p2){return 0;}else{return -1;}')
  48. :
  49. create_function('$p1,$p2', '$key=$GLOBALS["rock_ARRAY_SORT_KEY_" . nil];$p1=rock_array_get($p1,$key);$p2=rock_array_get($p2,$key);if ($p1<$p2){return 1;}elseif($p1==$p2){return 0;}else{return -1;}')
  50. );
  51. unset($GLOBALS["ROCK_ARRAY_SORT_KEY_" . nil]);
  52. }
  53. return $array;
  54. }
  55. /**
  56. * read cookie
  57. *
  58. * @param string $name Cookie Name
  59. * @param mixed $default default value
  60. * @return mixed
  61. */
  62. function rock_cookie($name, $default = null) {
  63. return isset($_COOKIE[$name]) ? $_COOKIE[$name] : $default;
  64. }
  65. /**
  66. * Construct a real ID from a mixed ID
  67. *
  68. * @param mixed $id id in mixed type
  69. */
  70. function rock_real_id($id) {
  71. if (is_object($id)) {
  72. return $id;
  73. }
  74. if (preg_match("/^rid_(\\w+):(.+)$/", $id, $match)) {
  75. $type = $match[1];
  76. $value = $match[2];
  77. switch ($type) {
  78. case "string":
  79. return $value;
  80. case "float":
  81. return floatval($value);
  82. case "double":
  83. return doubleval($value);
  84. case "boolean":
  85. return (bool)$value;
  86. case "integer":
  87. return intval($value);
  88. case "long":
  89. return doubleval($value);
  90. case "object":
  91. return new MongoId($value);
  92. case "MongoInt32":
  93. return new MongoInt32($value);
  94. case "MongoInt64":
  95. return new MongoInt64($value);
  96. case "mixed":
  97. $eval = new VarEval(base64_decode($value));
  98. $realId = $eval->execute();
  99. return $realId;
  100. }
  101. return;
  102. }
  103. if (is_numeric($id)) {
  104. return floatval($id);
  105. }
  106. if (preg_match("/^[0-9a-z]{24}$/i", $id)) {
  107. return new MongoId($id);
  108. }
  109. return $id;
  110. }
  111. /**
  112. * Format ID to string
  113. *
  114. * @param mixed $id object ID
  115. */
  116. function rock_id_string($id) {
  117. if (is_object($id) && $id instanceof MongoId) {
  118. return "rid_object:" . $id->__toString();
  119. }
  120. if (is_object($id)) {
  121. return "rid_" . get_class($id) . ":" . $id->__toString();
  122. }
  123. if (is_scalar($id)) {
  124. return "rid_" . gettype($id) . ":" . $id;
  125. }
  126. return "rid_mixed:" . base64_encode(var_export($id, true));
  127. }
  128. /**
  129. * Output a variable
  130. *
  131. * @param mixed $var a variable
  132. */
  133. function h($var) {
  134. if (is_array($var)) {
  135. echo json_encode($var);
  136. return;
  137. }
  138. if (is_null($var)) {
  139. echo "NULL";
  140. return;
  141. }
  142. if (is_bool($var)) {
  143. echo $var ? "TRUE":"FALSE";
  144. return;
  145. }
  146. echo $var;
  147. }
  148. /**
  149. * Output a variable escaped
  150. *
  151. * @param mixed $var a variable
  152. */
  153. function h_escape($var) {
  154. if (is_array($var)) {
  155. echo htmlspecialchars(json_encode($var));
  156. return;
  157. }
  158. if (is_null($var)) {
  159. echo "";
  160. return;
  161. }
  162. if (is_bool($var)) {
  163. echo $var;
  164. return;
  165. }
  166. echo htmlspecialchars($var);
  167. }
  168. /**
  169. * Output a I18N message
  170. *
  171. * @param string $var message key
  172. */
  173. function hm($var) {
  174. echo rock_lang($var);
  175. }
  176. /**
  177. * Load all lanugages
  178. *
  179. * @return array
  180. */
  181. function rock_load_languages() {
  182. $dir = __ROOT__ . DS . "langs";
  183. $handler = opendir($dir);
  184. $languages = array();
  185. while(($file = readdir($handler)) !== false) {
  186. $langDir = $dir . DS . $file;
  187. if (is_dir($langDir) && preg_match("/^\\w+_\\w+$/", $file)) {
  188. $message = array(
  189. "TRANSLATION_NAME" => ""
  190. );
  191. require $langDir . DS . "message.php";
  192. $languages[$file] = array( "code" => $file, "name" => $message["TRANSLATION_NAME"], "id" => $message["TRANSLATION_ID"]);
  193. }
  194. }
  195. closedir($handler);
  196. $languages = rock_array_sort($languages, "id");
  197. return rock_array_combine($languages, "code", "name");
  198. }
  199. /**
  200. * Get current path of theme
  201. *
  202. * @return string
  203. * @since 1.1.0
  204. */
  205. function rock_theme_path() {
  206. global $MONGO;
  207. if (isset($MONGO["features"]["theme"])) {
  208. return "themes/" . $MONGO["features"]["theme"];
  209. }
  210. else {
  211. return "themes/default";
  212. }
  213. }
  214. /**
  215. * Get real value from one string
  216. *
  217. * @param MongoDB $mongodb current mongodb
  218. * @param integer $dataType data type
  219. * @param string $format data format
  220. * @param string $value value in string format
  221. * @return mixed
  222. * @throws Exception
  223. * @since 1.1.0
  224. */
  225. function rock_real_value($mongodb, $dataType, $format, $value) {
  226. $realValue = null;
  227. switch ($dataType) {
  228. case "integer":
  229. case "float":
  230. case "double":
  231. $realValue = doubleval($value);
  232. break;
  233. case "string":
  234. $realValue = $value;
  235. break;
  236. case "boolean":
  237. $realValue = ($value == "true");
  238. break;
  239. case "null":
  240. $realValue = NULL;
  241. break;
  242. case "mixed":
  243. $eval = new VarEval($value, $format, $mongodb);
  244. $realValue = $eval->execute();
  245. if ($realValue === false) {
  246. throw new Exception("Unable to parse mixed value, just check syntax!");
  247. }
  248. break;
  249. }
  250. return $realValue;
  251. }
  252. ?>