PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/utils.php

https://gitlab.com/jiyath/pligg-cms
PHP | 487 lines | 393 code | 57 blank | 37 comment | 73 complexity | 282ed960e857ad462c3a37903d1e0991 MD5 | raw file
Possible License(s): CC-BY-3.0, GPL-2.0, Apache-2.0
  1. <?php
  2. if(!defined('mnminclude')){header('Location: ../error_404.php');die();}
  3. function mailer_start(){
  4. // Usually a module will define Pligg_Mailer
  5. // If defined, then include call the function thats starts (includes) it
  6. if(defined('Pligg_Mailer') && function_exists(Pligg_Mailer . '_mailer_start')){
  7. call_user_func(Pligg_Mailer . '_mailer_start');
  8. } else {
  9. include_once(mnminclude.'mailer.php');
  10. }
  11. }
  12. function check_if_table_exists($table) {
  13. // checks to see if a table in the database exists
  14. $result = mysql_query('select * from ' . $table);
  15. if (!$result) {
  16. return false;
  17. }
  18. return true;
  19. }
  20. function pligg_version(){
  21. // returns the version of Pligg that's installed
  22. $ver = get_misc_data('pligg_version');
  23. return $ver;
  24. }
  25. function pligg_hash(){
  26. // returns the version of Pligg that's installed
  27. $hash = get_misc_data('hash');
  28. return $hash;
  29. }
  30. function pligg_validate(){
  31. // returns the value for register validation
  32. $vars = array('validate' => misc_validate);
  33. check_actions('pligg_validate', $vars);
  34. return $vars['validate'];
  35. }
  36. function get_misc_data($name){
  37. // returns data from the misc_data table
  38. global $db;
  39. $sql = "SELECT `data` FROM `" . table_misc_data . "` WHERE `name` = '" . $db->escape($name) . "';";
  40. $var = $db->get_var($sql);
  41. return $var;
  42. }
  43. function misc_data_update($name, $data){
  44. // updates a row in the misc_data table
  45. global $db;
  46. $name = $db->escape($name);
  47. $sql = "SELECT `data` FROM `" . table_misc_data . "` WHERE `name` = '" . $name . "';";
  48. if(count($db->get_results($sql)) == 0){
  49. $sql = "INSERT INTO `" . table_misc_data . "` (`data`, `name`) VALUES ('" . $data . "', '" . $name . "');";
  50. } else {
  51. $sql = "UPDATE `" . table_misc_data . "` SET `data` = '" . $data . "' WHERE `name` = '$name';";
  52. }
  53. $db->query($sql);
  54. }
  55. function safeAddSlashes($string) {
  56. // if function get_magic_quotes_gpc exists, returns a string with backslashes before characters that need to be quoted in database queries etc
  57. // if (get_magic_quotes_gpc()) {
  58. // return $string;
  59. // }
  60. // else {
  61. return addslashes($string);
  62. // }
  63. }
  64. function unixtimestamp($timestamp){
  65. if(strlen($timestamp) == 14) {
  66. $time = substr($timestamp,0,4)."-".substr($timestamp,4,2)."-".substr($timestamp,6,2);
  67. $time .= " ";
  68. $time .= substr($timestamp,8,2).":".substr($timestamp,10,2).":".substr($timestamp,12,2);
  69. return strtotime($time);
  70. } else {
  71. if(strlen($timestamp) == 0) {
  72. return 0;
  73. } else {
  74. return strtotime($timestamp);
  75. }
  76. }
  77. }
  78. function user_exists($username) {
  79. // checks to see if user already exists in database
  80. global $db;
  81. $username = $db->escape($username);
  82. $res=$db->get_var("SELECT count(*) FROM " . table_users . " WHERE user_login='$username'");
  83. if ($res>0) return true;
  84. return false;
  85. }
  86. function email_exists($email) {
  87. // checks to see if email already exists in database
  88. global $db;
  89. $email = $db->escape($email);
  90. $res=$db->get_var("SELECT count(*) FROM " . table_users . " WHERE user_email='$email'");
  91. if ($res>0) return $res;
  92. return false;
  93. }
  94. function check_email($email) {
  95. // checks to see if email is valid
  96. return preg_match('/^[a-zA-Z0-9!#\\$%&\'\\*\\+\\-\\/=\\?\\^_`\\{\\|\\}~\\.]+@[a-zA-Z0-9_\\-\\.]+\.[a-zA-Z]{2,4}$/', $email);
  97. }
  98. function check_email_address($email) {
  99. //from http://www.ilovejackdaniels.com/php/email-address-validation/
  100. // First, we check that there's one @ symbol, and that the lengths are right
  101. if (!preg_match('/^[^@]{1,64}@[^@]{1,255}$/', $email)) {
  102. // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
  103. return false;
  104. }
  105. // Split it into sections to make life easier
  106. $email_array = explode("@", $email);
  107. $local_array = explode(".", $email_array[0]);
  108. for ($i = 0; $i < sizeof($local_array); $i++) {
  109. if (!preg_match('/^(([A-Za-z0-9!#$%&\'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&\'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/', $local_array[$i])) {
  110. return false;
  111. }
  112. }
  113. if (!preg_match('/^\[?[0-9\.]+\]?$/', $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
  114. $domain_array = explode(".", $email_array[1]);
  115. if (sizeof($domain_array) < 2) {
  116. return false; // Not enough parts to domain
  117. }
  118. for ($i = 0; $i < sizeof($domain_array); $i++) {
  119. if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))/', $domain_array[$i])) {
  120. return false;
  121. }
  122. }
  123. }
  124. return true;
  125. }
  126. function txt_time_diff($from, $now=0){
  127. global $main_smarty;
  128. if (empty($from))
  129. return "No date provided";
  130. $txt = '';
  131. if($now==0) $now = time();
  132. $diff=$now-$from;
  133. if ($diff < 0)
  134. {
  135. $diff = -$diff;
  136. $txt = '-';
  137. }
  138. $days=intval($diff/86400);
  139. $diff=$diff%86400;
  140. $hours=intval($diff/3600);
  141. $diff=$diff%3600;
  142. $minutes=intval($diff/60);
  143. if($days>1) $txt .= " $days ".$main_smarty->get_config_vars('PLIGG_Visual_Story_Times_Days');
  144. else if ($days==1) $txt .= " $days ".$main_smarty->get_config_vars('PLIGG_Visual_Story_Times_Day');
  145. if($days < 2){
  146. if($hours>1) $txt .= " $hours ".$main_smarty->get_config_vars('PLIGG_Visual_Story_Times_Hours');
  147. else if ($hours==1) $txt .= " $hours ".$main_smarty->get_config_vars('PLIGG_Visual_Story_Times_Hour');
  148. if($hours < 3){
  149. if($minutes>1) $txt .= " $minutes ".$main_smarty->get_config_vars('PLIGG_Visual_Story_Times_Minutes');
  150. else if ($minutes==1) $txt .= " $minutes ".$main_smarty->get_config_vars('PLIGG_Visual_Story_Times_Minute');
  151. }
  152. }
  153. if($txt=='') $txt = ' '. $main_smarty->get_config_vars('PLIGG_Visual_Story_Times_FewSeconds') . ' ';
  154. return $txt;
  155. }
  156. function txt_shorter($string, $len=80) {
  157. // shorten a string to 80 characters
  158. if (strlen($string) > $len)
  159. $string = substr($string, 0, $len-3) . "...";
  160. return $string;
  161. }
  162. function save_text_to_html($string) {
  163. $string = strip_tags(trim($string));
  164. $string= htmlspecialchars($string);
  165. // $string= text_to_html($string);
  166. $string = preg_replace("/[\r\n]{2,}/", "<br /><br />\n", $string);
  167. return $string;
  168. }
  169. function text_to_html($string) {
  170. return preg_replace('/([hf][tps]{2,4}:\/\/[^ \t\n\r]+[^ .\t,\n\r\(\)"\'])/', '<a href="$1">$1</a>', $string);
  171. }
  172. function check_integer($which) {
  173. // checks to make sure it's an integer greater than 0
  174. if(isset($_REQUEST[$which])){
  175. if (intval($_REQUEST[$which])>0) {
  176. return intval($_REQUEST[$which]);
  177. } else {
  178. return false;
  179. }
  180. }
  181. return false;
  182. }
  183. function check_string($which) {
  184. if (!empty($_REQUEST[$which])) {
  185. return intval($_REQUEST[$which]);
  186. } else {
  187. return false;
  188. }
  189. }
  190. function get_current_page() {
  191. if(($var=check_integer('page'))) {
  192. return $var;
  193. } else {
  194. return 1;
  195. }
  196. }
  197. function get_date($epoch) {
  198. // get date in the format year-month-day
  199. return date("Y-m-d", $epoch);
  200. }
  201. function get_base_url($url){
  202. // get base of URL. For example, get_base_url will return www.pligg.com if the URL was www.pligg.com/support/
  203. $req = $url;
  204. $pos = strpos($req, '://');
  205. $protocol = strtolower(substr($req, 0, $pos));
  206. $req = substr($req, $pos+3);
  207. $pos = strpos($req, '/');
  208. if($pos === false)
  209. $pos = strlen($req);
  210. $host = substr($req, 0, $pos);
  211. return $host;
  212. }
  213. function get_permalink($id) {
  214. return getmyFullurl("story", $id);
  215. }
  216. function get_trackback($id) {
  217. return getmyurl("trackback", $id);
  218. }
  219. function checklevel($levl){
  220. global $current_user;
  221. if(isset($current_user->user_level)){
  222. if ($current_user->user_level == $levl)
  223. {
  224. return 1;
  225. }
  226. }
  227. }
  228. function makeUrlFriendly($output, $isPage=false) {
  229. global $db;
  230. if(function_exists('utils_makeUrlFriendly')) {
  231. $output = utils_makeUrlFriendly($output);
  232. }
  233. if ($isPage===true) return $output;
  234. // check to see if the story title already exists. If so, add an integer to the end of the title
  235. $n = $db->get_var("SELECT COUNT(*) FROM " . table_links . " WHERE link_title_url like '$output%'" .
  236. ($isPage > 0 ? " AND link_id!=$isPage" : ''));
  237. if ($n > 0)
  238. return $output . '-' . ($n+1);
  239. else
  240. return $output;
  241. }
  242. function utils_makeUrlFriendly($output)
  243. {
  244. if ($output == '') return $input;
  245. //$input = remove_error_creating_chars($input);
  246. $output = utf8_substr($output, 0, 240);
  247. $output = utf8_strtolower($output);
  248. if (file_exists(mnmpath.'languages/translit.txt'))
  249. {
  250. $translations = parse_ini_file(mnmpath.'languages/translit.txt');
  251. $output = strtr($output, $translations);
  252. }
  253. $output = preg_replace("/\s/e" , "_" , $output); // Replace spaces with underscores
  254. $output = str_replace("_", "-", $output);
  255. $output = str_replace("&amp;", "", $output);
  256. $output = str_replace("__", "_", $output);
  257. $output = str_replace("---", "-", $output);
  258. $output = str_replace("/", "", $output);
  259. $output = str_replace("\\", "", $output);
  260. $output = str_replace("'", "", $output);
  261. $output = str_replace(",", "", $output);
  262. $output = str_replace(";", "", $output);
  263. $output = str_replace(":", "", $output);
  264. $output = str_replace(".", "-", $output);
  265. $output = str_replace("?", "", $output);
  266. $output = str_replace("=", "-", $output);
  267. $output = str_replace("+", "", $output);
  268. $output = str_replace("$", "", $output);
  269. $output = str_replace("&", "", $output);
  270. $output = str_replace("!", "", $output);
  271. $output = str_replace(">>", "-", $output);
  272. $output = str_replace(">", "-", $output);
  273. $output = str_replace("<<", "-", $output);
  274. $output = str_replace("<", "-", $output);
  275. $output = str_replace("*", "", $output);
  276. $output = str_replace(")", "", $output);
  277. $output = str_replace("(", "", $output);
  278. $output = str_replace("[", "", $output);
  279. $output = str_replace("]", "", $output);
  280. $output = str_replace("^", "", $output);
  281. $output = str_replace("%", "", $output);
  282. // $output = str_replace("ť", "-", $output);
  283. // $output = str_replace("|", "", $output);
  284. $output = str_replace("#", "", $output);
  285. $output = str_replace("@", "", $output);
  286. $output = str_replace("`", "", $output);
  287. // $output = str_replace("”", "", $output);
  288. // $output = str_replace("“", "", $output);
  289. $output = str_replace("\"", "", $output);
  290. $output = str_replace("--", "-", $output);
  291. return $output;
  292. }
  293. // function makeCategoryFriendly has been moved to admin_categories.php
  294. function remove_error_creating_chars($chars) {
  295. $replace=array(
  296. 'Á' => 'A',
  297. 'Ĺ' => 'A',
  298. 'Ä' => 'A',
  299. 'ä' => 'a',
  300. 'á' => 'a',
  301. 'ŕ' => 'a',
  302. 'â' => 'a',
  303. 'ă' => 'a',
  304. 'ĺ' => 'a',
  305. 'Ć' => 'ae',
  306. 'ć' => 'ae',
  307. 'ç' => 'c',
  308. 'Ç' => 'C',
  309. 'é' => 'e',
  310. 'Č' => 'E',
  311. 'É' => 'E',
  312. 'Ë' => 'E',
  313. 'ë' => 'e',
  314. 'Ě' => 'I',
  315. 'ě' => 'i',
  316. 'Í' => 'I',
  317. 'í' => 'i',
  318. 'Ď' => 'I',
  319. 'ď' => 'i',
  320. 'ź' => '',
  321. 'ž' => '',
  322. 'ż' => '',
  323. 'ń' => 'n',
  324. 'Ń' => 'N',
  325. 'Ň' => 'O',
  326. 'ň' => 'o',
  327. 'Ö' => 'O',
  328. 'Ő' => 'O',
  329. 'Ó' => 'O',
  330. 'ô' => 'o',
  331. 'ó' => 'o',
  332. 'ő' => 'o',
  333. 'ö' => 'o',
  334. 'Š' => 's',
  335. 'š' => 's',
  336. 'ß' => 'ss',
  337. 'Ű' => 'U',
  338. 'Ú' => 'U',
  339. 'Ü' => 'U',
  340. 'ű' => 'u',
  341. 'ú' => 'u',
  342. 'ü' => 'u',
  343. 'Ý' => 'Y',
  344. 'ý' => 'y',
  345. 'Ÿ' => 'Y',
  346. '˙' => 'y',
  347. 'Ž' => 'Z',
  348. 'ž' => 'z',
  349. '€' => ''
  350. );
  351. foreach ($replace as $key => $value) {
  352. $chars = str_replace($key, $value, $chars );
  353. }
  354. return $chars;
  355. }
  356. function loghack($page, $extradata, $silent=false){
  357. // This function will be used for logging hacking attempts.
  358. // you'd also want IP Address
  359. // - date / time
  360. // email or log to file
  361. if($silent == false){
  362. die("Hacking attempt on ". $page);
  363. }
  364. }
  365. function checkforfield($fieldname, $table) {
  366. // checks to see if field exists in table
  367. $result = mysql_query('select * from ' . $table . ' LIMIT 1');
  368. if (!$result) {
  369. echo "<HR />ERROR! The table " . $table . " is missing! Are you sure you should be doing an upgrade?<HR />";
  370. return true;
  371. }
  372. $i = 0;
  373. while ($i < mysql_num_fields($result)) {
  374. $meta = mysql_fetch_field($result, $i);
  375. if (!$meta) {
  376. echo "No information available<br />\n";
  377. }
  378. else {
  379. if(strtolower($meta->name) == strtolower($fieldname)){
  380. return true;
  381. }
  382. }
  383. $i++;
  384. }
  385. return false;
  386. }
  387. function checkforindex($indexname, $table) {
  388. // checks to see if field exists in table
  389. $result = mysql_query('SHOW INDEX from ' . $table);
  390. if (!$result) {
  391. echo "<HR />ERROR! The table " . $table . " is missing! Are you sure you should be doing an upgrade?<HR />";
  392. return true;
  393. }
  394. while ($row = mysql_fetch_array($result))
  395. if(strtolower($row['Key_name']) == strtolower($indexname))
  396. return true;
  397. return false;
  398. }
  399. function object_2_array($result, $cur_depth = 0, $depth_limit = 1000) {
  400. // $cur_depth and $depth_limit is used for php 4 only
  401. // prevents the function from doing extra checking to see if
  402. // it should 'explore' the object further. saves a few cpu cycles
  403. // using this because (array)$user will not work in php 4
  404. $array = array();
  405. if(isset($result)){
  406. foreach ($result as $key=>$value) {
  407. if ($cur_depth < $depth_limit && is_object($value)) {
  408. $array[$key]=object_2_array($value, $cur_depth + 1, $depth_limit);
  409. }
  410. elseif ($cur_depth < $depth_limit && is_array($value)) {
  411. $array[$key]=object_2_array($value, $cur_depth + 1, $depth_limit);
  412. }
  413. else {
  414. $array[$key]=$value;
  415. }
  416. }
  417. }
  418. return $array;
  419. }
  420. function phpnum() {
  421. // returns the php version number
  422. $version = explode('.', phpversion());
  423. return (int) $version[0];
  424. }
  425. ?>