PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Loggix/Utility.php

https://github.com/kaz6120/Loggix
PHP | 495 lines | 301 code | 50 blank | 144 comment | 23 complexity | 2edcf82267b1991a1453e85440ce57f2 MD5 | raw file
  1. <?php
  2. /**
  3. * Loggix_Utility
  4. *
  5. * PHP version 5
  6. *
  7. * @package Loggix
  8. * @copyright Copyright (C) Loggix Project
  9. * @link http://loggix.gotdns.org/
  10. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  11. * @since 5.5.16
  12. * @version 11.5.28
  13. */
  14. /**
  15. * Loggix Utility Class
  16. *
  17. * Example 1:
  18. * <code>
  19. * Loggix_Utility::getDataLastModified();
  20. * </code>
  21. *
  22. * Example 2:
  23. * <code>
  24. * $app = new Loggix_Application;
  25. * $app->getDataLastModified();
  26. * </code>
  27. *
  28. */
  29. class Loggix_Utility
  30. {
  31. /**
  32. * Set delemited identifier in SQL
  33. *
  34. * @return string $sql
  35. */
  36. public function setDelimitedIdentifier($sql)
  37. {
  38. return $sql = (Loggix_Core::LOGGIX_DATABASE_TYPE == 'SQLite')
  39. ? str_replace('`', '', $sql)
  40. : $sql;
  41. }
  42. /**
  43. * Insert Strings into Database.
  44. *
  45. * This function escapes strings and strip XHTML tags
  46. * before inserting strings into SQL query.
  47. *
  48. * @see insertTagSafe
  49. * @since 5.5.16
  50. * @version 9.5.20
  51. */
  52. public function insertSafe()
  53. {
  54. if (get_magic_quotes_gpc()) {
  55. $_REQUEST = array_map('stripslashes', $_REQUEST);
  56. $_GET = array_map('stripslashes', $_GET);
  57. $_POST = array_map('stripslashes', $_POST);
  58. $_COOKIE = array_map('stripslashes', $_COOKIE);
  59. }
  60. $_GET[] = array_map('strip_tags',
  61. array_map('trim', $_GET));
  62. $_POST[] = array_map('strip_tags',
  63. array_map('trim', $_POST));
  64. }
  65. /**
  66. * Insert Strings into Database.
  67. *
  68. * This function escapes strings without striping XHTML tags
  69. * when inserting strings into SQL query.
  70. *
  71. * @see insertSafe
  72. * @since 5.5.16
  73. * @version 9.5.20
  74. */
  75. public function insertTagSafe()
  76. {
  77. if (get_magic_quotes_gpc()) {
  78. $_REQUEST = array_map('stripslashes', $_REQUEST);
  79. $_GET = array_map('stripslashes', $_GET);
  80. $_POST = array_map('stripslashes', $_POST);
  81. $_COOKIE = array_map('stripslashes', $_COOKIE);
  82. }
  83. $_GET[] = array_walk_recursive($_GET, 'trim');
  84. $_POST[] = array_walk_recursive($_POST, 'trim');
  85. }
  86. /**
  87. * Get DateTime Array
  88. *
  89. * @uses gmdate
  90. * @uses time
  91. * @return array
  92. * @usee
  93. */
  94. public function getDateArray()
  95. {
  96. global $config;
  97. return array(
  98. 'Y' => gmdate('Y', time() + ($config['tz'] * 3600)),
  99. 'm' => gmdate('m', time() + ($config['tz'] * 3600)),
  100. 'd' => gmdate('d', time() + ($config['tz'] * 3600)),
  101. 'H' => gmdate('H', time() + ($config['tz'] * 3600)),
  102. 'i' => gmdate('i', time() + ($config['tz'] * 3600)),
  103. 's' => gmdate('s', time() + ($config['tz'] * 3600))
  104. );
  105. }
  106. /**
  107. * Set DateTime Array
  108. *
  109. * @param string $str
  110. * @uses gmdate
  111. * @uses time
  112. * @return array
  113. */
  114. public function setDateArray($str)
  115. {
  116. global $config;
  117. return array(
  118. 'Y' => gmdate('Y', strtotime($str) + ($config['tz'] * 3600)),
  119. 'm' => gmdate('m', strtotime($str) + ($config['tz'] * 3600)),
  120. 'd' => gmdate('d', strtotime($str) + ($config['tz'] * 3600)),
  121. 'H' => gmdate('H', strtotime($str) + ($config['tz'] * 3600)),
  122. 'i' => gmdate('i', strtotime($str) + ($config['tz'] * 3600)),
  123. 's' => gmdate('s', strtotime($str) + ($config['tz'] * 3600))
  124. );
  125. }
  126. /**
  127. * Loggix_Utility BBCode
  128. *
  129. * This function converts text strings with BBcode into XHML.
  130. * This function converts HTML entities automatically.
  131. *
  132. * @param string $var
  133. * @return string $var
  134. * @since 5.5.16
  135. * @version 9.8.19
  136. */
  137. public function setBBCode($str)
  138. {
  139. $lines = explode("\n", htmlspecialchars($str));
  140. $uriPatternWithTitle = '/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)(\()(.+?)(\))/';
  141. $uriPattern = '/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/';
  142. for ($i = 0, $j = count($lines); $i < $j; $i++) {
  143. $lines[$i] = $lines[$i];
  144. // Convert URI
  145. if (preg_match($uriPatternWithTitle, $lines[$i])) {
  146. $referenceUriTitle = trim(preg_replace($uriPatternWithTitle, '$4', $lines[$i]));
  147. $referenceUri = trim(preg_replace($uriPatternWithTitle, '$2', $lines[$i]));
  148. $lines[$i] = '<a href="' . $referenceUri.'" class="ex-ref">' . $referenceUriTitle . '</a>';
  149. } else {
  150. $lines[$i] = preg_replace($uriPattern, '$1<a href="$2">$2</a>', $lines[$i]);
  151. }
  152. }
  153. $str = join("\n", $lines);
  154. // Format line breaks and paragraphs
  155. $str = str_replace("\n", "<br />",
  156. str_replace("\n\n", "</p><p>",
  157. str_replace("\r\n", "\n",
  158. $str)));
  159. // Convert <pre> tags
  160. $str = str_replace('[/pre]', '</pre><p>',
  161. str_replace('[pre]', '</p><pre>',
  162. $str));
  163. // Quote box coloring
  164. $str = str_replace('[/q3]', '</p></blockquote><p>',
  165. str_replace('[q3]', '</p><blockquote class="quote3"><p>',
  166. str_replace('[/q2]', '</p></blockquote><p>',
  167. str_replace('[q2]', '</p><blockquote class="quote2"><p>',
  168. str_replace('[/q1]', '</p></blockquote><p>',
  169. str_replace('[q1]', '</p><blockquote class="quote1"><p>',
  170. str_replace('[/q]', '</p></blockquote><p>',
  171. str_replace('[q]', '</p><blockquote class="quote1"><p>',
  172. $str))))))));
  173. // Remove bad patterns
  174. $str = str_replace('</p></p>', '</p>',
  175. str_replace('<p><p>', '<p>',
  176. str_replace('</pre></p>', '</pre>',
  177. str_replace('<p><pre>', '<pre>',
  178. str_replace('<br /></pre>', '</pre>',
  179. str_replace('<pre><br />', '<pre>',
  180. str_replace('<br /></p>', '</p>',
  181. str_replace('</p><br />', '</p>',
  182. str_replace('<p><br />', '<p>',
  183. $str)))))))));
  184. // Put all together into the paragraph
  185. $str = '<p>' . $str . "</p>\n";
  186. // Remove bad patterns
  187. $str = str_replace('<br /><br />', '',
  188. str_replace('<pre></pre>', '',
  189. str_replace('<p></p>', '',
  190. $str)));
  191. // Clean up the XHTML code
  192. $str = str_replace("><", ">\n<", $str);
  193. return $str;
  194. }
  195. /**
  196. * Get Time Zone
  197. *
  198. * This function returns time zone based on user config setting.
  199. *
  200. * @return string
  201. * @since 5.5.16
  202. * @version 6.2.22
  203. */
  204. public function getTimeZone()
  205. {
  206. global $config;
  207. switch($config['tz']) {
  208. case '-12':
  209. return '-1200';
  210. break;
  211. case '-11':
  212. return '-1100';
  213. break;
  214. case '-10':
  215. return '-1000';
  216. break;
  217. case '-9':
  218. return '-0900';
  219. break;
  220. case '-8':
  221. return '-0800';
  222. break;
  223. case '-7':
  224. return '-0700';
  225. break;
  226. case '-6':
  227. return '-0600';
  228. break;
  229. case '-5':
  230. return '-0500';
  231. break;
  232. case '-4':
  233. return '-0400';
  234. break;
  235. case '-3':
  236. return '-0300';
  237. break;
  238. case '-2':
  239. return '-0200';
  240. break;
  241. case '-1':
  242. return '+0100';
  243. break;
  244. case '12':
  245. return '+1200';
  246. break;
  247. case '11':
  248. return '+1100';
  249. break;
  250. case '10':
  251. return '+1000';
  252. break;
  253. case '9':
  254. return '+0900';
  255. break;
  256. case '8':
  257. return '+0800';
  258. break;
  259. case '7':
  260. return '+0700';
  261. break;
  262. case '6':
  263. return '+0600';
  264. break;
  265. case '5':
  266. return '+0500';
  267. break;
  268. case '4':
  269. return '+0400';
  270. break;
  271. case '3':
  272. return '+0300';
  273. break;
  274. case '2':
  275. return '+0200';
  276. break;
  277. case '1':
  278. return '+0100';
  279. break;
  280. default:
  281. return '+0000';
  282. break;
  283. }
  284. }
  285. /**
  286. * Get the last modified time of the files in "/data/" directory.
  287. *
  288. * Example:
  289. * <code>
  290. * getDataLastModified('F d Y H:i:s')
  291. * getDataLastModified('RFC822')
  292. * getDataLastModified('UTC')
  293. * </code>
  294. *
  295. * @param string $timeFormat;
  296. * @uses getTimeZone
  297. * @return string
  298. * @since 5.10.18
  299. * @version 6.2.18
  300. */
  301. public function getDataLastModified($timeFormat = 'Y-m-d H:i:s')
  302. {
  303. global $config;
  304. $expanderDataDir = './data/';
  305. if (file_exists($expanderDataDir)) {
  306. if ($dh = @opendir($expanderDataDir)) {
  307. $modifiedTimes = array();
  308. while ($file = readdir($dh)) {
  309. if ($file != '.' && $file != '..' &&
  310. preg_match('/^.+\.(inc|php|html|txt|text)$/', $file)) {
  311. $modifiedTimes[] .= filemtime($expanderDataDir . $file);
  312. }
  313. }
  314. rsort($modifiedTimes);
  315. }
  316. if ($timeFormat == 'RFC822') {
  317. $timeFormat = 'D, d M Y H:i:s '.$this->getTimeZone();
  318. $local = '';
  319. } else if ($timeFormat == 'UTC') {
  320. $timeFormat = 'D, d M Y H:i:s O';
  321. $local = '';
  322. } else {
  323. $timeFormat = $timeFormat;
  324. $local = $config['tz'] * 3600;
  325. }
  326. }
  327. return ((!empty($timeFormat)) && (isset($modifiedTimes[0])))
  328. ? gmdate($timeFormat, $modifiedTimes[0] + $local)
  329. : '';
  330. }
  331. /**
  332. * Convert Smiley
  333. *
  334. * This function converts text smilies into graphical ones.
  335. *
  336. * @param string $str
  337. * @return string $str
  338. * @see /theme/images/smiley/
  339. * @since 5.5.16
  340. * @version 9.5.6
  341. */
  342. public function setSmiley($str)
  343. {
  344. global $pathToIndex;
  345. $smileyDir = Loggix_Core::getRootUri() . 'theme/images/smiley/';
  346. $smilies = array(
  347. ':-)' => 'smile.png',
  348. ';-)' => 'wink.png',
  349. ':-D' => 'laugh.png',
  350. ':-!' => 'foot_in_mouth.png',
  351. ':-(' => 'frown.png',
  352. '=-o' => 'gasp.png',
  353. '8-)' => 'cool.png',
  354. ':-P' => 'tongue.png',
  355. '}:-[' => 'angry.png',
  356. ':-|' => 'ambivalent.png',
  357. ':-/' => 'undecided.png',
  358. ':-d' => 'yum.png',
  359. ':.(' => 'cry.png',
  360. ':-X' => 'sealed.png',
  361. ':.)' => 'touched.png',
  362. '|-|' => 'sleep.png',
  363. '}}:' => 'focus.png',
  364. '}-)' => 'strong.png',
  365. ':-S' => 'confused.png',
  366. ':-}' => 'embarassed.png',
  367. );
  368. $iconWidth = 18;
  369. $iconHeight = 18;
  370. foreach ($smilies as $smileyText => $smileyImage) {
  371. $str = str_replace(
  372. $smileyText,
  373. '<img' .
  374. ' src="' . $smileyDir . $smileyImage . '"' .
  375. ' width="' . $iconWidth . '"' .
  376. ' height="' . $iconHeight . '"' .
  377. ' alt="' . $smileyText . '"' .
  378. ' />',
  379. $str
  380. );
  381. }
  382. return $str;
  383. }
  384. /**
  385. * Convert byte value to KB or MB
  386. *
  387. * @param int $str
  388. * @return string $str
  389. * @since 5.5.16
  390. * @version 5.11.6
  391. */
  392. public function toMegaByte($str)
  393. {
  394. $str = round(stripslashes($str) / 1024, 1); // Byte => KB
  395. return ($str > 1024) ? round($str / 1024, 1).' MB' : $str.' KB';
  396. }
  397. /**
  398. * Get Page Generation Time
  399. *
  400. * @since 6.2.21
  401. * @version 6.2.21
  402. */
  403. public function getGenerationTime($startTime)
  404. {
  405. function getMicroseconds($microtime)
  406. {
  407. $tmpArray = explode(' ', $microtime);
  408. return (((float)$tmpArray[0]) + ((float)$tmpArray[1]));
  409. }
  410. function getLapsedTime($startTime, $finishTime)
  411. {
  412. return (getMicroseconds($finishTime) - getMicroseconds($startTime));
  413. }
  414. return number_format(getLapsedTime($startTime, microtime()), 4);
  415. }
  416. public function joinPath()
  417. {
  418. $numArgs = func_num_args();
  419. $args = func_get_args();
  420. $path = $args[0];
  421. if ( $numArgs > 1 ) {
  422. for ($i = 1; $i < $numArgs; $i++) {
  423. $path .= DIRECTORY_SEPARATOR . $args[$i];
  424. }
  425. }
  426. return $path;
  427. }
  428. /**
  429. * @param str $separator
  430. * @param str $newline
  431. * @param str $data
  432. * @since 8.3.1
  433. * @version 8.3.1
  434. */
  435. public static function explodeAssoc($separator, $newline, $data)
  436. {
  437. $lines = explode($newline, $data);
  438. foreach($lines as $value) {
  439. $pos = strpos($value, $separator);
  440. $key = substr($value, 0, $pos);
  441. $array[$key] = trim(substr($value, $pos+1, strlen($value)));
  442. }
  443. return $array;
  444. }
  445. }