PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/utility.inc.php

https://github.com/awriel/s3st15_matoa
PHP | 297 lines | 170 code | 32 blank | 95 comment | 51 complexity | 4f847a186f8cdc35caad2d52796008b8 MD5 | raw file
  1. <?php
  2. /**
  3. * utility class
  4. * A Collection of static utility methods
  5. *
  6. * Copyright (C) 2007,2008 Arie Nugraha (dicarve@yahoo.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. // be sure that this file not accessed directly
  24. if (!defined('INDEX_AUTH')) {
  25. die("can not access this file directly");
  26. } elseif (INDEX_AUTH != 1) {
  27. die("can not access this file directly");
  28. }
  29. class utility
  30. {
  31. /**
  32. * Static Method to send out javascript alert
  33. *
  34. * @param string $str_message
  35. * @return void
  36. */
  37. public static function jsAlert($str_message)
  38. {
  39. if (!$str_message) {
  40. return;
  41. }
  42. // replace newline with javascripts newline
  43. $str_message = str_replace("\n", '\n', $str_message);
  44. echo '<script type="text/javascript">'."\n";
  45. echo 'alert("'.addslashes($str_message).'")'."\n";
  46. echo '</script>'."\n";
  47. }
  48. /**
  49. * Static Method to load application settings from database
  50. *
  51. * @param object $obj_db
  52. * @return void
  53. */
  54. public static function loadSettings($obj_db)
  55. {
  56. global $sysconf;
  57. $_setting_query = $obj_db->query('SELECT * FROM setting');
  58. if (!$obj_db->errno) {
  59. while ($_setting_data = $_setting_query->fetch_assoc()) {
  60. $_value = unserialize($_setting_data['setting_value']);
  61. if (is_array($_value)) {
  62. foreach ($_value as $_idx=>$_curr_value) {
  63. $sysconf[$_setting_data['setting_name']][$_idx] = $_curr_value;
  64. }
  65. } else {
  66. $sysconf[$_setting_data['setting_name']] = $_value;
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * Static Method to check privileges of application module form current user
  73. *
  74. * @param string $str_module_name
  75. * @param string $str_privilege_type
  76. * @return boolean
  77. */
  78. public static function havePrivilege($str_module_name, $str_privilege_type = 'r')
  79. {
  80. // checking checksum
  81. $server_addr = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
  82. $_checksum = defined('UCS_BASE_DIR')?md5($server_addr.UCS_BASE_DIR.'admin'):md5($server_addr.SENAYAN_BASE_DIR.'admin');
  83. if ($_SESSION['checksum'] != $_checksum) {
  84. return false;
  85. }
  86. // check privilege type
  87. if (!in_array($str_privilege_type, array('r', 'w'))) {
  88. return false;
  89. }
  90. if (isset($_SESSION['priv'][$str_module_name][$str_privilege_type]) AND $_SESSION['priv'][$str_module_name][$str_privilege_type]) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Static Method to write application activities logs
  97. *
  98. * @param object $obj_db
  99. * @param string $str_log_type
  100. * @param string $str_value_id
  101. * @param string $str_location
  102. * @param string $str_log_msg
  103. * @return void
  104. */
  105. public static function writeLogs($obj_db, $str_log_type, $str_value_id, $str_location, $str_log_msg)
  106. {
  107. if (!$obj_db->error) {
  108. // log table
  109. $_log_date = date('Y-m-d H:i:s');
  110. $_log_table = 'system_log';
  111. // filter input
  112. $str_log_type = $obj_db->escape_string(trim($str_log_type));
  113. $str_value_id = $obj_db->escape_string(trim($str_value_id));
  114. $str_location = $obj_db->escape_string(trim($str_location));
  115. $str_log_msg = $obj_db->escape_string(trim($str_log_msg));
  116. // insert log data to database
  117. @$obj_db->query('INSERT INTO '.$_log_table.'
  118. VALUES (NULL, \''.$str_log_type.'\', \''.$str_value_id.'\', \''.$str_location.'\', \''.$str_log_msg.'\', \''.$_log_date.'\')');
  119. }
  120. }
  121. /**
  122. * Static Method to get an ID of database table record
  123. *
  124. * @param object $obj_db
  125. * @param string $str_table_name
  126. * @param string $str_id_field
  127. * @param string $str_value_field
  128. * @param string $str_value
  129. * @param array $arr_cache
  130. * @return mixed
  131. */
  132. public static function getID($obj_db, $str_table_name, $str_id_field, $str_value_field, $str_value, &$arr_cache = false)
  133. {
  134. $str_value = trim($str_value);
  135. if ($arr_cache) {
  136. if (isset($arr_cache[$str_value])) {
  137. return $arr_cache[$str_value];
  138. }
  139. }
  140. if (!$obj_db->error) {
  141. $id_q = $obj_db->query('SELECT '.$str_id_field.' FROM '.$str_table_name.' WHERE '.$str_value_field.'=\''.$obj_db->escape_string($str_value).'\'');
  142. if ($id_q->num_rows > 0) {
  143. $id_d = $id_q->fetch_row();
  144. unset($id_q);
  145. // cache
  146. if ($arr_cache) {
  147. $arr_cache[$str_value] = $id_d[0];
  148. }
  149. return $id_d[0];
  150. } else {
  151. $_curr_date = date('Y-m-d');
  152. // if not found then we insert it as new value
  153. $obj_db->query('INSERT IGNORE INTO '.$str_table_name.' ('.$str_value_field.', input_date, last_update)
  154. VALUES (\''.$obj_db->escape_string($str_value).'\', \''.$_curr_date.'\', \''.$_curr_date.'\')');
  155. if (!$obj_db->error) {
  156. // cache
  157. if ($arr_cache) {
  158. $arr_cache[$str_value] = $obj_db->insert_id;
  159. }
  160. return $obj_db->insert_id;
  161. }
  162. }
  163. }
  164. }
  165. /**
  166. * Static method to detect mobile browser
  167. * Some Patches by Indra Sutriadi
  168. *
  169. * @return boolean
  170. * this script is taken from http://mobiforge.com/developing/story/lightweight-device-detection-php
  171. **/
  172. public static function isMobileBrowser()
  173. {
  174. $_is_mobile_browser = '0';
  175. if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower(@$_SERVER['HTTP_USER_AGENT']))) {
  176. $_is_mobile_browser++;
  177. }
  178. if((strpos(strtolower(@$_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
  179. $_is_mobile_browser++;
  180. }
  181. $_mobile_ua = strtolower(substr(@$_SERVER['HTTP_USER_AGENT'],0,4));
  182. $_mobile_agents = array(
  183. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  184. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  185. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  186. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  187. 'newt','noki','palm','pana','pant','phil','play','port','prox',
  188. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  189. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  190. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  191. 'wapr','webc','winw','winw','xda','xda-');
  192. if(in_array($_mobile_ua, $_mobile_agents)) {
  193. $_is_mobile_browser++;
  194. }
  195. if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']),'operamini')>0) {
  196. $_is_mobile_browser++;
  197. }
  198. if (strpos(strtolower(@$_SERVER['HTTP_USER_AGENT']),' ppc;')>0) {
  199. $_is_mobile_browser++;
  200. }
  201. if (strpos(strtolower(@$_SERVER['HTTP_USER_AGENT']),'windows ce')>0) {
  202. $_is_mobile_browser++;
  203. }
  204. if (strpos(strtolower(@$_SERVER['HTTP_USER_AGENT']),'windows')>0) {
  205. $_is_mobile_browser=0;
  206. }
  207. if (strpos(strtolower(@$_SERVER['HTTP_USER_AGENT']),'iemobile')>0) {
  208. $_is_mobile_browser++;
  209. }
  210. if (strpos(strtolower(@$_SERVER['HTTP_ACCEPT']),'j2me')>0 || strpos(strtolower(@$_SERVER['HTTP_ACCEPT']),'midp')>0) {
  211. $_is_mobile_browser++;
  212. }
  213. if (isset($_SERVER['HTTP_X_OPERAMINI_PHONE'])) {
  214. $_is_mobile_browser++;
  215. }
  216. if ($_is_mobile_browser > 0) {
  217. return true;
  218. }
  219. return false;
  220. }
  221. /**
  222. * Static method to check if member already logged in or not
  223. *
  224. * @return boolean
  225. **/
  226. public static function isMemberLogin()
  227. {
  228. $_logged_in = false;
  229. $_logged_in = isset($_SESSION['mid']) && isset($_SESSION['m_name']) && isset($_SESSION['m_email']);
  230. return $_logged_in;
  231. }
  232. /**
  233. * Static method to filter data
  234. *
  235. * @param mixed $mix_input: input data
  236. * @param string $str_input_type: input type
  237. * @param boolean $bool_trim: are input string trimmed
  238. *
  239. * @return mixed
  240. **/
  241. public static function filterData($mix_input, $str_input_type = 'get', $bool_escape_sql = true, $bool_trim = true, $bool_strip_html = false) {
  242. global $dbs;
  243. if (extension_loaded('filter')) {
  244. if ($str_input_type == 'var') {
  245. $mix_input = filter_var($mix_input, FILTER_SANITIZE_STRING);
  246. } else if ($str_input_type == 'post') {
  247. $mix_input = filter_input(INPUT_POST, $mix_input);
  248. } else if ($str_input_type == 'cookie') {
  249. $mix_input = filter_input(INPUT_COOKIE, $mix_input);
  250. } else if ($str_input_type == 'session') {
  251. $mix_input = filter_input(INPUT_SESSION, $mix_input);
  252. } else {
  253. $mix_input = filter_input(INPUT_GET, $mix_input);
  254. }
  255. }
  256. // trim whitespace on string
  257. if ($bool_trim) { $mix_input = trim($mix_input); }
  258. // strip html
  259. if ($bool_strip_html) { $mix_input = strip_tags($mix_input); }
  260. // escape SQL string
  261. if ($bool_escape_sql) { $mix_input = $dbs->escape_string($mix_input); }
  262. return $mix_input;
  263. }
  264. }
  265. ?>