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

/application/helpers/MY_url_helper.php

https://gitlab.com/fredec/ionizecms-1.0.8.x
PHP | 542 lines | 243 code | 77 blank | 222 comment | 44 complexity | 8436e167c62a0bc146940fe590f4b87d MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Ionize, creative CMS
  4. * URL Helper
  5. *
  6. * @package Ionize
  7. * @author Ionize Dev Team
  8. * @license http://doc.ionizecms.com/en/basic-infos/license-agreement
  9. * @link http://ionizecms.com
  10. * @since Version 0.9.0
  11. *
  12. */
  13. // ------------------------------------------------------------------------
  14. /*
  15. * This function checks if SSL is on for the current request,
  16. * and if it is, it makes a secure URL. This can be used to
  17. * load resources like stylesheets, images, or javascript
  18. * in a way that won't cause a partially secure warning in the browser.
  19. *
  20. */
  21. function if_secure_base_url()
  22. {
  23. $CI = get_instance();
  24. $url = $CI->config->slash_item('base_url');
  25. if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
  26. {
  27. $url = substr($url, 0, 4).'s'.substr($url, 4);
  28. }
  29. return $url;
  30. }
  31. // ------------------------------------------------------------------------
  32. /**
  33. * Admin URL
  34. *
  35. * Returns the Ionize base URL (with or without lang segment)
  36. *
  37. * @param boolean If TRUE, adds the lang segement to the URL. Default to FALSE
  38. * @access public
  39. * @return string
  40. */
  41. if ( ! function_exists('admin_url'))
  42. {
  43. function admin_url($lang_segment = FALSE)
  44. {
  45. $CI =& get_instance();
  46. if ($lang_segment == TRUE)
  47. {
  48. return base_url().Settings::get_lang('current').'/'.$CI->config->slash_item('admin_url');
  49. }
  50. return base_url().$CI->config->slash_item('admin_url');
  51. }
  52. }
  53. // ------------------------------------------------------------------------
  54. /**
  55. * Theme URL
  56. *
  57. * Returns the Ionize current theme URL
  58. *
  59. * @access public
  60. * @return string
  61. */
  62. if ( ! function_exists('theme_url'))
  63. {
  64. function theme_url()
  65. {
  66. return base_url().Theme::get_theme_path();
  67. }
  68. }
  69. // ------------------------------------------------------------------------
  70. /**
  71. * Admin Theme Assets URL
  72. *
  73. * Returns the Ionize current theme URL
  74. *
  75. * @access public
  76. * @return string
  77. */
  78. if ( ! function_exists('admin_style_url'))
  79. {
  80. function admin_style_url()
  81. {
  82. return theme_url() .'styles/' . Settings::get('backend_ui_style') . '/';
  83. }
  84. }
  85. // ------------------------------------------------------------------------
  86. /**
  87. * Variant of site_url(), but adds the module name - if any.
  88. *
  89. * @param string|array
  90. * @return string
  91. */
  92. if( ! function_exists('module_url'))
  93. {
  94. function module_url($str = '')
  95. {
  96. global $RTR;
  97. // no module:
  98. if( ! $RTR->fetch_module_uri_seg())
  99. {
  100. return site_url($str);
  101. }
  102. // module, add segment:
  103. if( ! is_array($str))
  104. {
  105. return site_url($RTR->fetch_module_uri_seg() . '/' . $str);
  106. }
  107. else
  108. {
  109. return site_url(array_merge(array($RTR->fetch_module_uri_seg()), $str));
  110. }
  111. }
  112. }
  113. // ------------------------------------------------------------------------
  114. /**
  115. * Variant of anchor() which takes the module into account - if any.
  116. *
  117. * @param string|array
  118. * @param string
  119. * @param string|array
  120. * @return string
  121. */
  122. if( ! function_exists('module_anchor'))
  123. {
  124. function module_anchor($uri = '', $title = '', $attributes = '')
  125. {
  126. global $RTR;
  127. $title = (string) $title;
  128. if ( ! is_array($uri))
  129. {
  130. $site_url = ( ! preg_match('!^\w+://! i', $uri)) ? module_url($uri) : $uri;
  131. }
  132. else
  133. {
  134. $site_url = site_url($uri);
  135. }
  136. if ($title == '')
  137. {
  138. $title = $site_url;
  139. }
  140. if ($attributes != '')
  141. {
  142. $attributes = _parse_attributes($attributes);
  143. }
  144. return '<a href="'.$site_url.'"'.$attributes.'>'.$title.'</a>';
  145. }
  146. }
  147. // ------------------------------------------------------------------------
  148. /**
  149. * Variant of redirect() which takes the module into account - if any.
  150. *
  151. * @param string|array
  152. * @param string
  153. * @param int
  154. * @return string
  155. */
  156. if( ! function_exists('module_redirect'))
  157. {
  158. function module_redirect($uri = '', $method = 'location', $http_response_code = 302)
  159. {
  160. if ( ! preg_match('#^https?://#i', $uri))
  161. {
  162. $uri = module_url($uri);
  163. }
  164. // call "parent"
  165. redirect($uri, $method, $http_response_code);
  166. }
  167. }
  168. // ------------------------------------------------------------------------
  169. /**
  170. * Creates an url to the specified uri for the specified language key.
  171. *
  172. * @param string
  173. * @param string
  174. * @return string
  175. */
  176. if( ! function_exists('lang_url'))
  177. {
  178. function lang_url($lang_key, $uri = '')
  179. {
  180. global $LANG;
  181. return $LANG->lang_url($lang_key, $uri);
  182. }
  183. }
  184. if( ! function_exists('current_lang_url'))
  185. {
  186. /**
  187. * Returns the base_url for current lang
  188. * @return string
  189. */
  190. function current_lang_url()
  191. {
  192. return base_url().Settings::get_lang('current').'/';
  193. }
  194. }
  195. // ------------------------------------------------------------------------
  196. /**
  197. * Create URL Title compatible with all latin characters
  198. *
  199. * Takes a "title" string as input and creates a
  200. * human-friendly URL string with either a dash
  201. * or an underscore as the word separator.
  202. *
  203. * @access public
  204. * @param string the string
  205. * @param string the separator: dash, or underscore
  206. * @return string
  207. */
  208. if( ! function_exists('url_title'))
  209. {
  210. function url_title($str, $separator = 'dash')
  211. {
  212. if($separator == 'underscore')
  213. {
  214. $separator = '_';
  215. }
  216. else
  217. {
  218. $separator = '-';
  219. }
  220. $charset = config_item('charset');
  221. $str = strtolower(htmlentities($str, ENT_COMPAT, $charset));
  222. $str = preg_replace('/&(.)(acute|cedil|circ|lig|grave|ring|tilde|uml);/', "$1", $str);
  223. $str = preg_replace('/([^a-z0-9.]+)/', $separator, html_entity_decode($str, ENT_COMPAT, $charset));
  224. $str = trim($str, $separator);
  225. return $str;
  226. }
  227. }
  228. // ------------------------------------------------------------------------
  229. /**
  230. * Alternative languages helper
  231. *
  232. * Returns a string with links to the content in alternative languages
  233. *
  234. * version 0.2
  235. * @author Luis <luis@piezas.org.es>
  236. * @modified by Ionut <contact@quasiperfect.eu>
  237. if( ! function_exists('alt_site_url'))
  238. {
  239. function alt_site_url($uri = '')
  240. {
  241. $CI =& get_instance();
  242. global $RTR;
  243. $original_route = explode('/', $RTR->uri->_parse_request_uri());
  244. $actual_lang = ( ! isset($original_route[1]) ) ? false : $original_route[1];
  245. $languages=$CI->config->item('languages');
  246. $languages_useimg=$CI->config->item('lang_useimg');
  247. $ignore_lang=$CI->config->item('lang_ignore');
  248. if (empty($actual_lang))
  249. {
  250. $uri=$ignore_lang.$CI->uri->uri_string();
  251. $actual_lang=$ignore_lang;
  252. }
  253. else
  254. {
  255. if (!array_key_exists($actual_lang,$languages))
  256. {
  257. $uri=$ignore_lang.$CI->uri->uri_string();
  258. $actual_lang=$ignore_lang;
  259. }
  260. else
  261. {
  262. $uri=$CI->uri->uri_string();
  263. $uri=substr_replace($uri,'',0,1);
  264. }
  265. }
  266. $alt_url='<ul>';
  267. //i use ul because for me formating a list from css is easy
  268. foreach ($languages as $lang=>$lang_desc)
  269. {
  270. if ($actual_lang!=$lang)
  271. {
  272. $alt_url.='<li><a href="'.config_item('base_url');
  273. if ($lang==$ignore_lang)
  274. {
  275. $new_uri=ereg_replace('^'.$actual_lang,'',$uri);
  276. $new_uri=substr_replace($new_uri,'',0,1);
  277. }
  278. else
  279. {
  280. $new_uri=ereg_replace('^'.$actual_lang,$lang,$uri);
  281. }
  282. $alt_url.=$new_uri.'">';
  283. if ($languages_useimg){
  284. //change the path on u'r needs
  285. //in images u need to have for example en.gif and so on for every
  286. //language u use
  287. //the language description will be used as alternative
  288. $alt_url.= '<img src="'.base_url().'images/'.$lang.'.gif" alt="'.$lang_desc.'"></a></li>';
  289. }
  290. else
  291. {
  292. $alt_url.= $lang_desc.'</a></li>';
  293. }
  294. }
  295. }
  296. $alt_url.='</ul>';
  297. return $alt_url;
  298. }
  299. }
  300. */
  301. // ------------------------------------------------------------------------
  302. /**
  303. * Auto-linker
  304. *
  305. * Corrected so it takes URLs without space before (begining of line, for example).
  306. *
  307. * Adds the subject attribute in email.
  308. * Example : mailto:my.name@domain.tld?subject='My subject' will be linked correctly
  309. *
  310. * Automatically links URL and Email addresses.
  311. * Note: There's a bit of extra code here to deal with
  312. * URLs or emails that end in a period. We'll strip these
  313. * off and add them after the link.
  314. *
  315. * @access public
  316. * @param string the string
  317. * @param string the type: email, url, or both
  318. * @param bool whether to create pop-up links
  319. * @return string
  320. *
  321. */
  322. function auto_link($str, $type = 'both', $popup = FALSE)
  323. {
  324. $m = array();
  325. if(preg_match_all('(<a\ .+?>.+?</a>)', $str, $m))
  326. {
  327. foreach($m[0] as $k => $val)
  328. {
  329. $str = str_replace($val, '[[[a'.$k.']]]', $str);
  330. }
  331. }
  332. if ($type != 'email')
  333. {
  334. // (|\b) : Includes href="..." in auto_link, which isn't good
  335. // if (preg_match_all("#(^|\s|\(|\b)((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
  336. if (preg_match_all("#(^|\>|\s|\()((http(s?)://)|(www\.))(\w+[^\s\)\<]+)#i", $str, $matches))
  337. {
  338. $pop = ($popup == TRUE) ? " target=\"_blank\" " : "";
  339. for ($i = 0; $i < count($matches['0']); $i++)
  340. {
  341. $period = '';
  342. if (preg_match("|\.$|", $matches['6'][$i]))
  343. {
  344. $period = '.';
  345. $matches['6'][$i] = substr($matches['6'][$i], 0, -1);
  346. }
  347. $str = str_replace($matches['0'][$i],
  348. $matches['1'][$i].'<a href="http'.
  349. $matches['4'][$i].'://'.
  350. $matches['5'][$i].
  351. $matches['6'][$i].'"'.$pop.'>http'.
  352. $matches['4'][$i].'://'.
  353. $matches['5'][$i].
  354. $matches['6'][$i].'</a>'.
  355. $period, $str);
  356. }
  357. }
  358. }
  359. if ($type != 'url')
  360. {
  361. if (preg_match_all("#([a-zA-Z0-9_\.\-\+]+)@([a-zA-Z0-9\-]+)\.([a-zA-Z0-9\-\.]*)((\?subject\=)(\')(.*)(\'))*#i", $str, $matches))
  362. {
  363. for ($i = 0; $i < count($matches['0']); $i++)
  364. {
  365. $period = '';
  366. if (preg_match("|\.$|", $matches['3'][$i]))
  367. {
  368. $period = '.';
  369. $matches['3'][$i] = substr($matches['3'][$i], 0, -1);
  370. }
  371. $comp_email = $matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i].$matches['5'][$i].$matches['7'][$i];
  372. $email = $matches['1'][$i].'@'.$matches['2'][$i].'.'.$matches['3'][$i];
  373. $str = str_replace($matches['0'][$i], safe_mailto($comp_email, $email).$period, $str);
  374. }
  375. }
  376. }
  377. if(!empty($m))
  378. {
  379. foreach($m[0] as $k => $val)
  380. {
  381. $str = str_replace('[[[a'.$k.']]]', $val, $str);
  382. }
  383. }
  384. return $str;
  385. }
  386. if( ! function_exists('validate_url'))
  387. {
  388. function validate_url($url)
  389. {
  390. $pattern = "/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i";
  391. return (bool) preg_match($pattern, $url);
  392. }
  393. }
  394. /**
  395. * Returns the HTTP answer for one URL or FALSE if the URL wasn't found
  396. *
  397. */
  398. if( ! function_exists('check_url'))
  399. {
  400. function check_url($url)
  401. {
  402. if (function_exists('curl_init'))
  403. {
  404. $url = prep_url($url);
  405. $c = curl_init();
  406. curl_setopt($c, CURLOPT_URL, $url);
  407. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  408. curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
  409. curl_setopt($c, CURLOPT_NOBODY, true);
  410. $output = @curl_exec($c);
  411. if($output !== FALSE)
  412. {
  413. $httpCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
  414. curl_close($c);
  415. return $httpCode;
  416. }
  417. return FALSE;
  418. }
  419. else
  420. {
  421. return @fsockopen("$url", 80, $errno, $errstr, 30);
  422. }
  423. return FALSE;
  424. }
  425. }
  426. // ------------------------------------------------------------------------
  427. /**
  428. * Header Redirect
  429. * Modified to redirect 303 by default.
  430. *
  431. * Header redirect in two flavors
  432. * For very fine grained control over headers, you could use the Output
  433. * Library's set_header() function.
  434. *
  435. * @access public
  436. * @param string the URL
  437. * @param string the method: location or redirect
  438. * @return string
  439. */
  440. if ( ! function_exists('redirect'))
  441. {
  442. function redirect($uri = '', $method = 'location', $http_response_code = 303)
  443. {
  444. if ( ! preg_match('#^https?://#i', $uri))
  445. {
  446. $uri = site_url($uri);
  447. }
  448. switch($method)
  449. {
  450. case 'refresh' : header('Refresh:0;url='.$uri);
  451. break;
  452. default : header('Location: '.$uri, TRUE, $http_response_code);
  453. break;
  454. }
  455. exit;
  456. }
  457. }
  458. /* End of file MY_url_helper.php */
  459. /* Location: ./application/helpers/MY_url_helper.php */