PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/usr.php

https://github.com/phpfalcon/Kleeja-2.0.0-alpha
PHP | 595 lines | 401 code | 96 blank | 98 comment | 84 complexity | 52029fa3ed0b6cda9ff55a95bbbe0972 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. *
  4. * @package Kleeja
  5. * @version $Id$
  6. * @copyright (c) 2007 Kleeja.com
  7. * @license ./docs/license.txt
  8. *
  9. */
  10. //no for directly open
  11. if (!defined('IN_COMMON'))
  12. {
  13. exit();
  14. }
  15. /**
  16. * Usefull constants to force some settings :
  17. *
  18. * FORCE_COOKIES, DISABLE_INTR
  19. */
  20. class usrcp
  21. {
  22. // this function like a traffic sign :)
  23. function data ($name, $pass, $hashed = false, $expire = 86400, $loginadm = false)
  24. {
  25. global $config;
  26. //return user system to normal
  27. if(defined('DISABLE_INTR') || $config['user_system'] == '' || empty($config['user_system']))
  28. {
  29. $config['user_system'] = '1';
  30. }
  31. //expire
  32. $expire = time() + ((int) $expire ? intval($expire) : 86400);
  33. ($hook = kleeja_run_hook('data_func_usr_class')) ? eval($hook) : null; //run hook
  34. if((int) $config['user_system'] != 1)
  35. {
  36. if(file_exists(PATH . 'includes/auth_integration/' . trim($config['user_system']) . '.php'))
  37. {
  38. include_once (PATH . 'includes/auth_integration/' . trim($config['user_system']) . '.php');
  39. return (kleeja_auth_login(trim($name), trim($pass), $hashed, $expire, $loginadm) ? true : false);
  40. }
  41. }
  42. //normal
  43. return $this->normal(trim($name), trim($pass), $hashed, $expire, $loginadm);
  44. }
  45. //get username by id
  46. function usernamebyid ($user_id)
  47. {
  48. global $config;
  49. //return user system to normal
  50. if(defined('DISABLE_INTR'))
  51. {
  52. $config['user_system'] = 1;
  53. }
  54. if((int) $config['user_system'] != 1)
  55. {
  56. if(file_exists(PATH . 'includes/auth_integration/' . trim($config['user_system']) . '.php'))
  57. {
  58. include_once (PATH . 'includes/auth_integration/' . trim($config['user_system']) . '.php');
  59. return kleeja_auth_username($user_id);
  60. }
  61. }
  62. //normal system
  63. $u = $this->get_data('name', $user_id);
  64. return $u['name'];
  65. }
  66. //now .. .. our table
  67. function normal ($name, $pass, $hashed = false, $expire, $loginadm = false)
  68. {
  69. global $SQL, $dbprefix, $config, $userinfo;
  70. $query = array(
  71. 'SELECT' => '*',
  72. 'FROM' => "`{$dbprefix}users`",
  73. );
  74. if($hashed)
  75. {
  76. $query['WHERE'] = "id=" . intval($name) . " and password='" . $SQL->escape($pass) . "'";
  77. }
  78. else
  79. {
  80. $query['WHERE'] = "clean_name='" . $SQL->real_escape($this->cleanusername($name)) . "'";
  81. }
  82. ($hook = kleeja_run_hook('qr_select_usrdata_n_usr_class')) ? eval($hook) : null; //run hook
  83. $result = $SQL->build($query);
  84. if ($SQL->num_rows($result) != 0)
  85. {
  86. while($row=$SQL->fetch_array($result))
  87. {
  88. if(empty($row['password'])) //more security
  89. {
  90. return false;
  91. }
  92. $phppass = $hashed ? $pass : $pass . $row['password_salt'];
  93. //CHECK IF IT'S MD5 PASSWORD
  94. if(strlen($row['password']) == '32' && empty($row['password_salt']))
  95. {
  96. $passmd5 = md5($pass);
  97. //update old md5 hash to phpass hash
  98. if($row['password'] == $passmd5)
  99. {
  100. //new salt
  101. $new_salt = substr(kleeja_base64_encode(pack("H*", sha1(mt_rand()))), 0, 7);
  102. //new password hash
  103. $new_password = $this->kleeja_hash_password(trim($pass) . $new_salt);
  104. ($hook = kleeja_run_hook('qr_update_usrdata_md5_n_usr_class')) ? eval($hook) : null; //run hook
  105. //update now !!
  106. $update_query = array(
  107. 'UPDATE' => "`{$dbprefix}users`",
  108. 'SET' => "password='" . $new_password . "' ,password_salt='" . $new_salt . "'",
  109. 'WHERE' => "id=" . intval($row['id'])
  110. );
  111. $SQL->build($update_query);
  112. }
  113. else //if the password is wrong
  114. {
  115. return false;
  116. }
  117. }
  118. else if(($phppass != $row['password'] && $hashed) || ($this->kleeja_hash_password($phppass, $row['password']) != true && $hashed == false))
  119. {
  120. return false;
  121. }
  122. //Avoid dfining constants again
  123. if(!$loginadm)
  124. {
  125. define('USER_ID', $row['id']);
  126. define('USER_NAME', $row['name']);
  127. define('USER_MAIL', $row['mail']);
  128. define('USER_ADMIN', $row['admin']);
  129. define('LAST_VISIT', $row['last_visit']);
  130. }
  131. //all user fileds info
  132. $userinfo = $row;
  133. $user_y = kleeja_base64_encode(serialize(array('id'=>$row['id'], 'name'=>$row['name'], 'mail'=>$row['mail'], 'last_visit'=>$row['last_visit'])));
  134. if(!$hashed)
  135. {
  136. $hash_key_expire = sha1(md5($config['h_key'] . $row['password']). $expire);
  137. if(!$loginadm)
  138. {
  139. $this->kleeja_set_cookie('ulogu', $this->en_de_crypt($row['id'] . '|' . $row['password'] . '|' . $expire . '|' . $hash_key_expire . '|' . $row['admin'] . '|' . $user_y), $expire);
  140. }
  141. else
  142. {
  143. //update now !!
  144. $update_last_visit = array(
  145. 'UPDATE' => "`{$dbprefix}users`",
  146. 'SET' => "last_visit=" . time() . "",
  147. 'WHERE' => "id=" . intval($row['id'])
  148. );
  149. $SQL->build($update_last_visit);
  150. }
  151. }
  152. ($hook = kleeja_run_hook('qr_while_usrdata_n_usr_class')) ? eval($hook) : null; //run hook
  153. }
  154. $SQL->freeresult($result);
  155. unset($pass);
  156. return true;
  157. }
  158. else
  159. {
  160. return false;
  161. }
  162. }
  163. /*
  164. get user data
  165. new function:1rc5+
  166. */
  167. function get_data($type="*", $user_id = false)
  168. {
  169. global $dbprefix, $SQL;
  170. if(!$user_id)
  171. {
  172. $user_id = $this->id();
  173. }
  174. //todo :
  175. //if type != '*' and contains no , and type in 'name, id, email' return $this->id .. etc
  176. //te get files and update them !!
  177. $query_name = array(
  178. 'SELECT' => $type,
  179. 'FROM' => "{$dbprefix}users",
  180. 'WHERE' => "id=" . intval($user_id)
  181. );
  182. ($hook = kleeja_run_hook('qr_select_userdata_in_usrclass')) ? eval($hook) : null; //run hook
  183. $data_user = $SQL->fetch_array($SQL->build($query_name));
  184. return $data_user;
  185. }
  186. /*
  187. user ids
  188. */
  189. function id ()
  190. {
  191. ($hook = kleeja_run_hook('id_func_usr_class')) ? eval($hook) : null; //run hook
  192. return defined('USER_ID') ? USER_ID : false;
  193. }
  194. /*
  195. user name
  196. */
  197. function name ()
  198. {
  199. ($hook = kleeja_run_hook('name_func_usr_class')) ? eval($hook) : null; //run hook
  200. return defined('USER_NAME') ? USER_NAME : false;
  201. }
  202. /*
  203. user mail
  204. */
  205. function mail ()
  206. {
  207. ($hook = kleeja_run_hook('mail_func_usr_class')) ? eval($hook) : null; //run hook
  208. return defined('USER_MAIL') ? USER_MAIL : false;
  209. }
  210. /*
  211. is user admin ?
  212. */
  213. function admin ()
  214. {
  215. ($hook = kleeja_run_hook('admin_func_usr_class')) ? eval($hook) : null; //run hook
  216. return defined('USER_ADMIN') ? USER_ADMIN : false;
  217. }
  218. /*
  219. logout func
  220. */
  221. function logout()
  222. {
  223. ($hook = kleeja_run_hook('logout_func_usr_class')) ? eval($hook) : null; //run hook
  224. //adm
  225. if(defined('USER_ADMIN') && USER_ADMIN == 1 && !empty($_SESSION['ADMINLOGIN']))
  226. {
  227. $this->logout_cp();
  228. }
  229. //is ther any cookies
  230. $this->kleeja_set_cookie('ulogu', '', time() - 31536000);//31536000 = year
  231. return true;
  232. }
  233. /*
  234. logut just from acp
  235. */
  236. function logout_cp()
  237. {
  238. ($hook = kleeja_run_hook('logout_cp_func_usr_class')) ? eval($hook) : null; //run hook
  239. if(!empty($_SESSION['ADMINLOGIN']))
  240. {
  241. unset($_SESSION['ADMINLOGIN'], $_SESSION['USER_SESS'] /*, $_SESSION['LAST_VISIT']*/);
  242. }
  243. return true;
  244. }
  245. //clean usernames
  246. function cleanusername($uname)
  247. {
  248. if(!function_exists('kleeja_base64_decode'))
  249. {
  250. include_once (PATH . 'includes/functions_alternative.php');
  251. }
  252. ($hook = kleeja_run_hook('cleanusername_func_usr_class')) ? eval($hook) : null; //run hook
  253. static $arabic_t = array();
  254. static $latin_t = array(
  255. array('á','à','â','ã','å','Á','À','Â','Ã','Å','é','è','ê','ë','É','È','Ê','í','ì','ï','î','Í','Ì','Î','Ï','ò','ó','ô','õ','º','ø','Ó','Ò','Ô','Õ','Ø','ú','ù','û','Ú','Ù','Û','ç','Ç','Ñ','ñ','ÿ','Ë'),
  256. array('a','a','a','a','a','a','a','a','a','a','e','e','e','e','e','e','e','i','i','i','i','i','i','i','i','o','o','o','o','o','o','o','o','o','o','o','u','u','u','u','u','u','c','c','n','n','y','e')
  257. );
  258. if(empty($arabic_t))
  259. {
  260. //Arabic chars must be stay in utf8 format, so we encoded them
  261. $arabic_t = unserialize(kleeja_base64_decode('YToyOntpOjA7YToxMjp7aTowO3M6Mjoi2KMiO2k6MTtzOjI6ItilIjtpOjI7czoyOiLYpCI7aTozO3M6Mjoi2YAiO2k6NDtzOjI6Itm' .
  262. 'LIjtpOjU7czoyOiLZjCI7aTo2O3M6Mjoi2Y8iO2k6NztzOjI6ItmOIjtpOjg7czoyOiLZkCI7aTo5O3M6Mjoi2ZIiO2k6MTA7czoyOiLYoiI7aToxMTtzOjI6ItimIjt9aToxO' .
  263. '2E6MTI6e2k6MDtzOjI6ItinIjtpOjE7czoyOiLYpyI7aToyO3M6Mjoi2YgiO2k6MztzOjA6IiI7aTo0O3M6MDoiIjtpOjU7czowOiIiO2k6NjtzOjA6IiI7aTo3O3M6MDoiIjt' .
  264. 'pOjg7czowOiIiO2k6OTtzOjA6IiI7aToxMDtzOjI6ItinIjtpOjExO3M6Mjoi2YkiO319'));
  265. }
  266. $uname = str_replace($latin_t[0], $latin_t[1], $uname); //replace confusable Latin chars
  267. $uname = str_replace($arabic_t[0], $arabic_t[1], $uname); //replace confusable Arabic chars
  268. $uname = preg_replace('#(?:[\x00-\x1F\x7F]+|(?:\xC2[\x80-\x9F])+)#', '', $uname); //un-wanted utf8 control chars
  269. $uname = preg_replace('# {2,}#', ' ', $uname); //2+ spaces with one space
  270. return strtolower($uname);
  271. }
  272. //depand on phpass class
  273. function kleeja_hash_password($password, $check_pass = false)
  274. {
  275. include_once('phpass.php');
  276. ($hook = kleeja_run_hook('kleeja_hash_password_func_usr_class')) ? eval($hook) : null; //run hook
  277. $return = false;
  278. $hasher = new PasswordHash(8, true);
  279. $return = $hasher->HashPassword($password);
  280. //return check or hash
  281. return $check_pass != false ? $hasher->CheckPassword($password, $check_pass) : $return;
  282. }
  283. //kleeja cookie
  284. function kleeja_set_cookie($name, $value, $expire)
  285. {
  286. global $config;
  287. ($hook = kleeja_run_hook('kleeja_set_cookie_func_usr_class')) ? eval($hook) : null; //run hook
  288. //
  289. //when user add cookie_* in config this will replace the current ones
  290. //
  291. global $config_cookie_name, $config_cookie_domian, $config_cookie_secure, $config_cookie_path;
  292. $config['cookie_name'] = isset($config_cookie_name) ? $config_cookie_name : $config['cookie_name'];
  293. $config['cookie_domain'] = isset($config_cookie_domain) ? $config_cookie_domain : $config['cookie_domain'];
  294. $config['cookie_secure'] = isset($config_cookie_secure) ? $config_cookie_secure : $config['cookie_secure'];
  295. $config['cookie_path'] = isset($config_cookie_path) ? $config_cookie_path : $config['cookie_path'];
  296. //
  297. //when user add define('FORCE_COOKIES', true) in config.php we will make our settings of cookies
  298. //
  299. if(defined('FORCE_COOKIES'))
  300. {
  301. $config['cookie_domain'] = (!empty($_SERVER['HTTP_HOST'])) ? strtolower($_SERVER['HTTP_HOST']) : ((!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : @getenv('SERVER_NAME'));
  302. $config['cookie_domain'] = str_replace('www.', '.', substr($config['cookie_domain'], 0, strpos($config['cookie_domain'], ':')));
  303. $config['cookie_path'] = '/';
  304. $config['cookie_secure'] = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? true : false;
  305. }
  306. // Enable sending of a P3P header
  307. header('P3P: CP="CUR ADM"');
  308. $name_data = rawurlencode($config['cookie_name'] . '_' . $name) . '=' . rawurlencode($value);
  309. $rexpire = gmdate('D, d-M-Y H:i:s \\G\\M\\T', $expire);
  310. $domain = (!$config['cookie_domain'] || $config['cookie_domain'] == 'localhost' || $config['cookie_domain'] == '127.0.0.1') ? '' : '; domain=' . $config['cookie_domain'];
  311. header('Set-Cookie: ' . $name_data . (($expire) ? '; expires=' . $rexpire : '') . '; path=' . $config['cookie_path'] . $domain . ((!$config['cookie_secure']) ? '' : '; secure') . '; HttpOnly', false);
  312. }
  313. //encrypt and decrypt any data with our function
  314. function en_de_crypt($data, $type = 1)
  315. {
  316. global $config;
  317. static $txt = array();
  318. if(empty($txt))
  319. {
  320. if(empty($config['h_key']))
  321. {
  322. $config['h_key'] = sha1('2^#@qr39)]k%$_-(');//default !
  323. }
  324. $chars = str_split($config['h_key']);
  325. foreach(range('a', 'z') as $k=>$v)
  326. {
  327. if(!isset($chars[$k]))
  328. {
  329. break;
  330. }
  331. $txt[$v] = $chars[$k] . $k . '-';
  332. }
  333. }
  334. switch($type)
  335. {
  336. case 1:
  337. $data = str_replace('=', '_', kleeja_base64_encode($data));
  338. $data = strtr($data, $txt);
  339. break;
  340. case 2:
  341. $txtx = array_flip($txt);
  342. $txtx = array_reverse($txtx, true);
  343. $data = strtr($data, $txtx);
  344. $data = kleeja_base64_decode(str_replace('_', '=', $data));
  345. break;
  346. }
  347. return $data;
  348. }
  349. //
  350. //get cookie
  351. //
  352. function kleeja_get_cookie($name)
  353. {
  354. global $config;
  355. ($hook = kleeja_run_hook('kleeja_get_cookie_func_usr_class')) ? eval($hook) : null; //run hook
  356. return isset($_COOKIE[$config['cookie_name'] . '_' . $name]) ? $_COOKIE[$config['cookie_name'] . '_' . $name] : false;
  357. }
  358. //check if user is admin or not
  359. //return : mean return true or false, but if return is false will show msg
  360. function kleeja_check_user()
  361. {
  362. global $config, $SQL, $dbprefix;
  363. ($hook = kleeja_run_hook('kleeja_check_user_func_usr_class')) ? eval($hook) : null; //run hook
  364. //if login up
  365. if($this->kleeja_get_cookie('ulogu'))
  366. {
  367. $user_data = false;
  368. list($user_id, $hashed_password, $expire_at, $hashed_expire, $adm_or_not, $u_info) = @explode('|', $this->en_de_crypt($this->kleeja_get_cookie('ulogu'), 2));
  369. //if not expire
  370. if(($hashed_expire == sha1(md5($config['h_key'] . $hashed_password) . $expire_at)) && ($expire_at > time()))
  371. {
  372. /* For better performance we will take the risks */
  373. /*
  374. !defined('IN_DOWNLOAD')
  375. */
  376. if((int) $adm_or_not == 1)
  377. {
  378. $user_data = $this->data($user_id, $hashed_password, true, $expire_at);
  379. }
  380. else
  381. {
  382. if(!empty($u_info))
  383. {
  384. $uu_info = unserialize(kleeja_base64_decode($u_info));
  385. define('USER_ID', $uu_info['id']);
  386. define('USER_NAME', $uu_info['name']);
  387. define('USER_MAIL', $uu_info['mail']);
  388. define('USER_ADMIN', '0');
  389. define('LAST_VISIT', $uu_info['last_visit']);
  390. $user_data = true;
  391. }
  392. }
  393. }
  394. if($user_data == false)
  395. {
  396. $this->logout();
  397. }
  398. else
  399. {
  400. return $user_data;
  401. }
  402. }
  403. return false; //nothing
  404. }
  405. /*
  406. * convert from utf8 to cp1256 and vice versa
  407. */
  408. function kleeja_utf8($str, $to_utf8 = true)
  409. {
  410. $utf8 = new kleeja_utf8;
  411. if($to_utf8)
  412. {
  413. //return iconv('CP1256', "UTF-8//IGNORE", $str);
  414. return $utf8->to_utf8($str);
  415. }
  416. return $utf8->from_utf8($str);
  417. //return iconv('UTF-8', "CP1256//IGNORE", $str);
  418. }
  419. }#end class
  420. /**
  421. * Deep modifieded by Kleeja team ...
  422. * depend on class by Alexander Minkovsky (a_minkovsky@hotmail.com)
  423. */
  424. class kleeja_utf8
  425. {
  426. var $ascMap = array();
  427. var $utfMap = array();
  428. //ignore the untranslated char, of you put true we will translate it to html tags
  429. //it's same the action of //IGNORE in iconv
  430. var $ignore = false;
  431. //Constructor
  432. function kleeja_utf8()
  433. {
  434. static $lines = array();
  435. if(empty($lines))
  436. {
  437. $lines = explode("\n", preg_replace(array("/#.*$/m", "/\n\n/"), '', file_get_contents(PATH . 'includes/CP1256.MAP')));
  438. }
  439. if(empty($this->ascMap))
  440. {
  441. foreach($lines as $line)
  442. {
  443. $parts = explode('0x', $line);
  444. if(sizeof($parts) == 3)
  445. $this->ascMap[hexdec(trim($parts[1]))] = hexdec(trim($parts[2]));
  446. }
  447. $this->utfMap = array_flip($this->ascMap);
  448. }
  449. }
  450. //Translate string ($str) to UTF-8 from given charset
  451. function to_utf8($str)
  452. {
  453. $chars = unpack('C*', $str);
  454. $cnt = sizeof($chars);
  455. for($i=1;$i <= $cnt; ++$i)
  456. $this->_charToUtf8($chars[$i]);
  457. return implode('', $chars);
  458. }
  459. //Translate UTF-8 string to single byte string in the given charset
  460. function from_utf8($utf)
  461. {
  462. $chars = unpack('C*', $utf);
  463. $cnt = sizeof($chars);
  464. $res = ''; //No simple way to do it in place... concatenate char by char
  465. for ($i=1;$i<=$cnt;$i++)
  466. $res .= $this->_utf8ToChar($chars, $i);
  467. return $res;
  468. }
  469. //Char to UTF-8 sequence
  470. function _charToUtf8(&$char)
  471. {
  472. $c = (int) $this->ascMap[$char];
  473. if ($c < 0x80)
  474. $char = chr($c);
  475. else if($c<0x800) // 2 bytes
  476. $char = (chr(0xC0 | $c>>6) . chr(0x80 | $c & 0x3F));
  477. else if($c<0x10000) // 3 bytes
  478. $char = (chr(0xE0 | $c>>12) . chr(0x80 | $c>>6 & 0x3F) . chr(0x80 | $c & 0x3F));
  479. else if($c<0x200000) // 4 bytes
  480. $char = (chr(0xF0 | $c>>18) . chr(0x80 | $c>>12 & 0x3F) . chr(0x80 | $c>>6 & 0x3F) . chr(0x80 | $c & 0x3F));
  481. }
  482. //UTF-8 sequence to single byte character
  483. function _utf8ToChar(&$chars, &$idx)
  484. {
  485. if(($chars[$idx] >= 240) && ($chars[$idx] <= 255))// 4 bytes
  486. $utf = (intval($chars[$idx]-240) << 18) + (intval($chars[++$idx]-128) << 12) + (intval($chars[++$idx]-128) << 6) + (intval($chars[++$idx]-128) << 0);
  487. else if (($chars[$idx] >= 224) && ($chars[$idx] <= 239)) // 3 bytes
  488. $utf = (intval($chars[$idx]-224) << 12) + (intval($chars[++$idx]-128) << 6) + (intval($chars[++$idx]-128) << 0);
  489. else if (($chars[$idx] >= 192) && ($chars[$idx] <= 223))// 2 bytes
  490. $utf = (intval($chars[$idx]-192) << 6) + (intval($chars[++$idx]-128) << 0);
  491. else// 1 byte
  492. $utf = $chars[$idx];
  493. if(array_key_exists($utf, $this->utfMap))
  494. return chr($this->utfMap[$utf]);
  495. else
  496. return $this->ignore ? '' : '&#' . $utf . ';';
  497. }
  498. }
  499. #<-- EOF