PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/functions.php

https://github.com/monkeymars/Noht
PHP | 144 lines | 106 code | 29 blank | 9 comment | 20 complexity | 4ee3f906e5ee78192a6dad0dba48287e MD5 | raw file
  1. <?
  2. // Global functionality
  3. // should be included on every page
  4. session_start();
  5. $user = $_COOKIE['noht_user_hash'] ? $_COOKIE['noht_user_hash'] : setHashCookie();
  6. $new_user;
  7. $a_year = time()+(60*60*24*365);
  8. function setHashCookie() {
  9. $cookie = sha1(time().str_replace(".", $_SERVER['REMOTE_ADDR']));
  10. setcookie("noht_user_hash", $cookie, $a_year);
  11. global $new_user;
  12. $new_user = true;
  13. return $cookie;
  14. }
  15. $title = "Noht. sharing drawings. A simple HTML5 mobile canvas drawing, social sharing, application";
  16. function alphaID($in, $to_num = false, $pad_up = false, $passKey = null) {
  17. $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  18. if ($passKey !== null) {
  19. // Although this function's purpose is to just make the
  20. // ID short - and not so much secure,
  21. // with this patch by Simon Franz (http://blog.snaky.org/)
  22. // you can optionally supply a password to make it harder
  23. // to calculate the corresponding numeric ID
  24. for ($n = 0; $n<strlen($index); $n++) {
  25. $i[] = substr( $index,$n ,1);
  26. }
  27. $passhash = hash('sha256',$passKey);
  28. $passhash = (strlen($passhash) < strlen($index))
  29. ? hash('sha512',$passKey)
  30. : $passhash;
  31. for ($n=0; $n < strlen($index); $n++) {
  32. $p[] = substr($passhash, $n ,1);
  33. }
  34. array_multisort($p, SORT_DESC, $i);
  35. $index = implode($i);
  36. }
  37. $base = strlen($index);
  38. if ($to_num) {
  39. // Digital number <<-- alphabet letter code
  40. $in = strrev($in);
  41. $out = 0;
  42. $len = strlen($in) - 1;
  43. for ($t = 0; $t <= $len; $t++) {
  44. $bcpow = bcpow($base, $len - $t);
  45. $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
  46. }
  47. if (is_numeric($pad_up)) {
  48. $pad_up--;
  49. if ($pad_up > 0) {
  50. $out -= pow($base, $pad_up);
  51. }
  52. }
  53. $out = sprintf('%F', $out);
  54. $out = substr($out, 0, strpos($out, '.'));
  55. } else {
  56. // Digital number -->> alphabet letter code
  57. if (is_numeric($pad_up)) {
  58. $pad_up--;
  59. if ($pad_up > 0) {
  60. $in += pow($base, $pad_up);
  61. }
  62. }
  63. $out = "";
  64. for ($t = floor(log($in, $base)); $t >= 0; $t--) {
  65. $bcp = bcpow($base, $t);
  66. $a = floor($in / $bcp) % $base;
  67. $out = $out . substr($index, $a, 1);
  68. $in = $in - ($a * $bcp);
  69. }
  70. $out = strrev($out); // reverse
  71. }
  72. return $out;
  73. }
  74. $colors = array(
  75. "black" => array("hex" => "000", "rgb" => array("r" => 0, "g" => 0, "b" => 0)),
  76. "white" => array("hex" => "fff", "rgb" => array("r" => 255, "g" => 255, "b" => 255)),
  77. "blue" => array("hex" => "6cf", "rgb" => array("r" => 102, "g" => 204, "b" => 255)),
  78. "red" => array("hex" => "d6012a", "rgb" => array("r" => 214, "g" => 1, "b" => 42)),
  79. "orange" => array("hex" => "e36731", "rgb" => array("r" => 227, "g" => 103, "b" => 49)),
  80. "yellow" => array("hex" => "dbce0a", "rgb" => array("r" => 219, "g" => 206, "b" => 10)),
  81. "green" => array("hex" => "4a6e00", "rgb" => array("r" => 74, "g" => 110, "b" => 0)),
  82. "purple" => array("hex" => "503662", "rgb" => array("r" => 80, "g" => 54, "b" => 98)),
  83. "pink" => array("hex" => "da6aa5", "rgb" => array("r" => 218, "g" => 106, "b" => 165)),
  84. );
  85. function timeAgo($time) {
  86. $time = time() - $time; // to get the time since that moment
  87. $tokens = array (
  88. 31536000 => 'year',
  89. 2592000 => 'month',
  90. 604800 => 'week',
  91. 86400 => 'day',
  92. 3600 => 'hour',
  93. 60 => 'minute',
  94. 1 => 'second'
  95. );
  96. foreach ($tokens as $unit => $text) {
  97. if ($time < $unit) continue;
  98. $numberOfUnits = floor($time / $unit);
  99. return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
  100. }
  101. }
  102. function deviceCheck() {
  103. if( stristr($_SERVER['HTTP_USER_AGENT'],'ipad') ) {
  104. $device = "ipad";
  105. } else if( stristr($_SERVER['HTTP_USER_AGENT'],'iphone') || strstr($_SERVER['HTTP_USER_AGENT'],'iphone') ) {
  106. $device = "iphone";
  107. } else if( stristr($_SERVER['HTTP_USER_AGENT'],'blackberry') ) {
  108. $device = "blackberry";
  109. } else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
  110. $device = "android";
  111. }
  112. return ($device) ? $device : false;
  113. }
  114. $device = deviceCheck();
  115. ?>