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

/system/core/core.functions.php

https://github.com/danboy/Croissierd
PHP | 2947 lines | 1920 code | 581 blank | 446 comment | 419 complexity | de85f026a15f646b846bf9ad4a44afdb MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. =====================================================
  4. ExpressionEngine - by EllisLab
  5. -----------------------------------------------------
  6. http://expressionengine.com/
  7. -----------------------------------------------------
  8. Copyright (c) 2003 - 2010 EllisLab, Inc.
  9. =====================================================
  10. THIS IS COPYRIGHTED SOFTWARE
  11. PLEASE READ THE LICENSE AGREEMENT
  12. http://expressionengine.com/docs/license.html
  13. =====================================================
  14. File: core.functions.php
  15. -----------------------------------------------------
  16. Purpose: Shared system functions.
  17. =====================================================
  18. */
  19. if ( ! defined('EXT'))
  20. {
  21. exit('Invalid file request');
  22. }
  23. class Functions {
  24. var $seed = FALSE; // Whether we've seeded our rand() function. We only seed once per script execution
  25. var $cached_url = array();
  26. var $cached_path = array();
  27. var $cached_index = array();
  28. var $cached_captcha = '';
  29. var $template_map = array();
  30. var $template_type = '';
  31. var $action_ids = array();
  32. var $file_paths = array();
  33. var $conditional_debug = FALSE;
  34. /** ----------------------------------------
  35. /** Set Full server path
  36. /** ----------------------------------------*/
  37. function set_realpath($path)
  38. {
  39. if (@realpath($path) !== FALSE)
  40. {
  41. $path = realpath($path).'/';
  42. }
  43. return str_replace("\\", "/", $path);
  44. }
  45. /* END */
  46. /** ----------------------------------------
  47. /** Fetch base site index
  48. /** ----------------------------------------*/
  49. function fetch_site_index($add_slash = 0, $sess_id = 1)
  50. {
  51. global $PREFS, $TMPL, $SESS;
  52. if (isset($this->cached_index[$add_slash.$sess_id.$this->template_type]))
  53. {
  54. return $this->cached_index[$add_slash.$sess_id.$this->template_type];
  55. }
  56. $url = $PREFS->ini('site_url', 1);
  57. if (USER_BLOG !== FALSE)
  58. {
  59. $url .= USER_BLOG.'/';
  60. }
  61. $url .= $PREFS->ini('site_index');
  62. if ($PREFS->ini('force_query_string') == 'y')
  63. {
  64. $url .= '?';
  65. }
  66. if (is_object($SESS) && ! empty($SESS->userdata['session_id']) && REQ != 'CP' && $sess_id == 1 &&
  67. $PREFS->ini('user_session_type') != 'c' && $this->template_type == 'webpage')
  68. {
  69. $url .= "/S=".$SESS->userdata('session_id')."/";
  70. }
  71. if ($add_slash == 1)
  72. {
  73. if (substr($url, -1) != '/')
  74. {
  75. $url .= "/";
  76. }
  77. }
  78. $this->cached_index[$add_slash.$sess_id.$this->template_type] = $url;
  79. return $url;
  80. }
  81. /* END */
  82. /** ----------------------------------------
  83. /** Create a custom URL
  84. /** ----------------------------------------*/
  85. // The input to this function is parsed and added to the
  86. // full site URL to create a full URL/URI
  87. function create_url($segment, $trailing_slash = true, $sess_id = 1)
  88. {
  89. global $PREFS, $REGX, $SESS;
  90. // Since this function can be used via a callback
  91. // we'll fetch the segiment if it's an array
  92. if (is_array($segment))
  93. {
  94. $segment = $segment['1'];
  95. }
  96. if (isset($this->cached_url[$segment]))
  97. {
  98. return $this->cached_url[$segment];
  99. }
  100. $full_segment = $segment;
  101. $segment = str_replace(array("'", '"'), '', $segment);
  102. $segment = preg_replace("/(.+?(&#47;|\/))index(&#47;|\/)(.*?)/", "\\1\\2", $segment);
  103. $segment = preg_replace("/(.+?(&#47;|\/))index$/", "\\1", $segment);
  104. /** --------------------------
  105. /** Specials
  106. /** --------------------------*/
  107. // These are exceptions to the normal path rules
  108. if (strtolower($segment) == 'site_index')
  109. {
  110. return $this->fetch_site_index();
  111. }
  112. if (strtolower($segment) == 'logout')
  113. {
  114. $qs = ($PREFS->ini('force_query_string') == 'y') ? '' : '?';
  115. return $this->fetch_site_index(0, 0).$qs.'ACT='.$this->fetch_action_id('Member', 'member_logout');
  116. }
  117. // END Specials
  118. $base = $this->fetch_site_index(0, $sess_id).'/'.$REGX->trim_slashes($segment);
  119. if (substr($base, -1) != '/' && $trailing_slash == TRUE)
  120. {
  121. $base .= '/';
  122. }
  123. $out = $this->remove_double_slashes($base);
  124. $this->cached_url[$full_segment] = $out;
  125. return $out;
  126. }
  127. /* END */
  128. /** ----------------------------------------
  129. /** Fetch site index with URI query string
  130. /** ----------------------------------------*/
  131. function fetch_current_uri()
  132. {
  133. global $IN;
  134. return $this->remove_double_slashes($this->fetch_site_index().$IN->URI);
  135. }
  136. /* END */
  137. /** -----------------------------------------
  138. /** Remove duplicate slashes from URL
  139. /** -----------------------------------------*/
  140. // With all the URL/URI parsing/building, there is the potential
  141. // to end up with double slashes. This is a clean-up function.
  142. function remove_double_slashes($str)
  143. {
  144. $str = str_replace("://", "{:SS}", $str);
  145. $str = str_replace(":&#47;&#47;", "{:SHSS}", $str); // Super HTTP slashes saved!
  146. $str = preg_replace("#/+#", "/", $str);
  147. $str = preg_replace("/(&#47;)+/", "/", $str);
  148. $str = str_replace("&#47;/", "/", $str);
  149. $str = str_replace("{:SHSS}", ":&#47;&#47;", $str);
  150. $str = str_replace("{:SS}", "://", $str);
  151. return $str;
  152. }
  153. /* END */
  154. /** ----------------------------------------
  155. /** Remove session ID from string
  156. /** ----------------------------------------*/
  157. // This function is used mainly by the Input class to strip
  158. // session IDs if they are used in public pages.
  159. function remove_session_id($str)
  160. {
  161. return preg_replace("#S=.+?/#", "", $str);
  162. }
  163. /* END */
  164. /** -----------------------------------------
  165. /** Extract path info
  166. /** -----------------------------------------*/
  167. // We use this to extract the template group/template name
  168. // from path variables, like {some_var path="weblog/index"}
  169. function extract_path($str)
  170. {
  171. global $REGX;
  172. if (preg_match("#=(.*)#", $str, $match))
  173. {
  174. if (isset($this->cached_path[$match['1']]))
  175. {
  176. return $this->cached_path[$match['1']];
  177. }
  178. $path = $REGX->trim_slashes(str_replace(array("'",'"'), "", $match['1']));
  179. if (substr($path, -6) == 'index/')
  180. {
  181. $path = str_replace('/index', '', $path);
  182. }
  183. if (substr($path, -5) == 'index')
  184. {
  185. $path = str_replace('/index', '', $path);
  186. }
  187. $this->cached_path[$match['1']] = $path;
  188. return $path;
  189. }
  190. else
  191. {
  192. return 'SITE_INDEX';
  193. }
  194. }
  195. /* END */
  196. /** ----------------------------------------
  197. /** Replace variables
  198. /** ----------------------------------------*/
  199. function var_swap($str, $data)
  200. {
  201. if ( ! is_array($data))
  202. {
  203. return FALSE;
  204. }
  205. foreach ($data as $key => $val)
  206. {
  207. $str = str_replace('{'.$key.'}', $val, $str);
  208. }
  209. return $str;
  210. }
  211. /* END */
  212. /** ----------------------------------------
  213. /** Redirect
  214. /** ----------------------------------------*/
  215. function redirect($location)
  216. {
  217. global $PREFS;
  218. $location = str_replace('&amp;', '&', $this->insert_action_ids($location));
  219. switch($PREFS->ini('redirect_method'))
  220. {
  221. case 'refresh' : header("Refresh: 0;url=$location");
  222. break;
  223. default : header("Location: $location");
  224. break;
  225. }
  226. exit;
  227. }
  228. /* END */
  229. /** ----------------------------------------
  230. /** Bounce
  231. /** ----------------------------------------*/
  232. function bounce($location = '')
  233. {
  234. if ($location == '')
  235. $location = BASE;
  236. $this->redirect($location);
  237. exit;
  238. }
  239. /* END */
  240. /** -------------------------------------------------
  241. /** Convert a string into an encrypted hash
  242. /** -------------------------------------------------*/
  243. // SHA1 or MD5 is supported
  244. function hash($str)
  245. {
  246. global $PREFS;
  247. if ($PREFS->ini('encryption_type') == 'md5')
  248. {
  249. return md5($str);
  250. }
  251. if ( ! function_exists('sha1'))
  252. {
  253. if ( ! function_exists('mhash'))
  254. {
  255. if ( ! class_exists('SHA'))
  256. {
  257. require PATH_CORE.'core.sha1'.EXT;
  258. }
  259. $SH = new SHA;
  260. return $SH->encode_hash($str);
  261. }
  262. else
  263. {
  264. return bin2hex(mhash(MHASH_SHA1, $str));
  265. }
  266. }
  267. else
  268. {
  269. return sha1($str);
  270. }
  271. }
  272. /* END */
  273. /** -------------------------------------------------
  274. /** Random number/password generator
  275. /** -------------------------------------------------*/
  276. function random($type = 'encrypt', $len = 8)
  277. {
  278. if ($this->seed == FALSE)
  279. {
  280. if (phpversion() >= 4.2)
  281. mt_srand();
  282. else
  283. mt_srand(hexdec(substr(md5(microtime()), -8)) & 0x7fffffff);
  284. $this->seed = TRUE;
  285. }
  286. switch($type)
  287. {
  288. case 'basic' : return mt_rand();
  289. break;
  290. case 'alpha' :
  291. case 'numeric' :
  292. case 'nozero' :
  293. switch ($type)
  294. {
  295. case 'alpha' : $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  296. break;
  297. case 'numeric' : $pool = '0123456789';
  298. break;
  299. case 'nozero' : $pool = '123456789';
  300. break;
  301. }
  302. $str = '';
  303. for ($i=0; $i < $len; $i++)
  304. {
  305. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  306. }
  307. return $str;
  308. break;
  309. case 'md5' : return md5(uniqid(mt_rand(), TRUE));
  310. break;
  311. case 'encrypt' : return $this->hash(uniqid(mt_rand(), TRUE));
  312. break;
  313. }
  314. }
  315. /* END */
  316. /** ----------------------------------------
  317. /** Form declaration
  318. /** ----------------------------------------*/
  319. // This function is used by modules when they need to create forms
  320. function form_declaration($data)
  321. {
  322. global $PREFS, $EXT, $REGX;
  323. $deft = array(
  324. 'hidden_fields' => array(),
  325. 'action' => '',
  326. 'id' => '',
  327. 'secure' => TRUE,
  328. 'enctype' => '',
  329. 'onsubmit' => '',
  330. );
  331. foreach ($deft as $key => $val)
  332. {
  333. if ( ! isset($data[$key]))
  334. {
  335. $data[$key] = $val;
  336. }
  337. }
  338. if (is_array($data['hidden_fields']) && ! isset($data['hidden_fields']['site_id']))
  339. {
  340. $data['hidden_fields']['site_id'] = $PREFS->ini('site_id');
  341. }
  342. // -------------------------------------------
  343. // 'form_declaration_modify_data' hook.
  344. // - Modify the $data parameters before they are processed
  345. //
  346. if ($EXT->active_hook('form_declaration_modify_data') === TRUE)
  347. {
  348. $data = $EXT->call_extension('form_declaration_modify_data', $data);
  349. }
  350. //
  351. // -------------------------------------------
  352. // -------------------------------------------
  353. // 'form_declaration_return' hook.
  354. // - Take control of the form_declaration function
  355. //
  356. if ($EXT->active_hook('form_declaration_return') === TRUE)
  357. {
  358. $form = $EXT->call_extension('form_declaration_return', $data);
  359. if ($EXT->end_script === TRUE) return $form;
  360. }
  361. //
  362. // -------------------------------------------
  363. if ($data['action'] == '')
  364. {
  365. $data['action'] = $this->fetch_site_index();
  366. }
  367. if ($data['onsubmit'] != '')
  368. {
  369. $data['onsubmit'] = 'onsubmit="'.trim($data['onsubmit']).'"';
  370. }
  371. $data['action'] = rtrim($data['action'], '?');
  372. $data['name'] = (isset($data['name']) && $data['name'] != '') ? "name='".$data['name']."' " : '';
  373. $data['id'] = ($data['id'] != '') ? "id='".$data['id']."' " : '';
  374. if ($data['enctype'] == 'multi' OR strtolower($data['enctype']) == 'multipart/form-data')
  375. {
  376. $data['enctype'] = 'enctype="multipart/form-data" ';
  377. }
  378. $form = '<form '.$data['id'].$data['name'].'method="post" action="'.$data['action'].'" '.$data['onsubmit'].' '.$data['enctype'].">\n";
  379. if ($data['secure'] == TRUE)
  380. {
  381. if ($PREFS->ini('secure_forms') == 'y')
  382. {
  383. if ( ! isset($data['hidden_fields']['XID']))
  384. {
  385. $data['hidden_fields'] = array_merge(array('XID' => '{XID_HASH}'), $data['hidden_fields']);
  386. }
  387. elseif ($data['hidden_fields']['XID'] == '')
  388. {
  389. $data['hidden_fields']['XID'] = '{XID_HASH}';
  390. }
  391. }
  392. }
  393. if (is_array($data['hidden_fields']))
  394. {
  395. $form .= "<div class='hiddenFields'>\n";
  396. foreach ($data['hidden_fields'] as $key => $val)
  397. {
  398. $form .= '<input type="hidden" name="'.$key.'" value="'.$REGX->form_prep($val).'" />'."\n";
  399. }
  400. $form .= "</div>\n\n";
  401. }
  402. return $form;
  403. }
  404. /* END */
  405. /** ----------------------------------------
  406. /** Form backtrack
  407. /** ----------------------------------------*/
  408. // This function lets us return a user to a previously
  409. // visited page after submitting a form. The page
  410. // is determined by the offset that the admin
  411. // places in each form
  412. function form_backtrack($offset = '')
  413. {
  414. global $SESS, $PREFS;
  415. $ret = $this->fetch_site_index();
  416. if ($offset != '')
  417. {
  418. if (isset($SESS->tracker[$offset]))
  419. {
  420. if ($SESS->tracker[$offset] != 'index')
  421. {
  422. return $this->remove_double_slashes($this->fetch_site_index().$SESS->tracker[$offset]);
  423. }
  424. }
  425. }
  426. if (isset($_POST['RET']))
  427. {
  428. if (substr($_POST['RET'], 0, 1) == '-')
  429. {
  430. $return = str_replace("-", "", $_POST['RET']);
  431. if (isset($SESS->tracker[$return]))
  432. {
  433. if ($SESS->tracker[$return] != 'index')
  434. {
  435. $ret = $this->fetch_site_index().$SESS->tracker[$return];
  436. }
  437. }
  438. }
  439. else
  440. {
  441. $_POST['RET'] = str_replace(SLASH, '/', $_POST['RET']);
  442. if (strpos($_POST['RET'], '/') !== FALSE)
  443. {
  444. if (stristr($_POST['RET'], 'http://') OR
  445. stristr($_POST['RET'], 'https://') OR
  446. stristr($_POST['RET'], 'www.'))
  447. {
  448. $ret = $_POST['RET'];
  449. }
  450. else
  451. {
  452. $ret = $this->create_url($_POST['RET']);
  453. }
  454. }
  455. else
  456. {
  457. $ret = $_POST['RET'];
  458. }
  459. }
  460. // We need to slug in the session ID if the admin is running
  461. // their site using sessions only. Normally the $FNS->fetch_site_index()
  462. // function adds the session ID automatically, except in cases when the
  463. // $_POST['RET'] variable is set. Since the login routine relies on the RET
  464. // info to know where to redirect back to we need to sandwich in the session ID.
  465. if ($PREFS->ini('user_session_type') != 'c')
  466. {
  467. if ($SESS->userdata['session_id'] != '' && ! stristr($ret, $SESS->userdata['session_id']))
  468. {
  469. $url = $PREFS->ini('site_url', 1);
  470. if (USER_BLOG !== FALSE)
  471. {
  472. $url .= USER_BLOG.'/';
  473. }
  474. $url .= $PREFS->ini('site_index');
  475. if ($PREFS->ini('force_query_string') == 'y')
  476. {
  477. $url .= '?';
  478. }
  479. $sess_id = "/S=".$SESS->userdata['session_id']."/";
  480. $ret = str_replace($url, $url.$sess_id, $ret);
  481. }
  482. }
  483. }
  484. return $this->remove_double_slashes($ret);
  485. }
  486. /* END */
  487. /** ----------------------------------------
  488. /** eval()
  489. /** ----------------------------------------*/
  490. // Evaluates a string as PHP
  491. function evaluate($str)
  492. {
  493. return eval('?>'.$str.'<?php ');
  494. // ?><?php // BBEdit syntax coloring bug fix
  495. }
  496. /* END */
  497. /** ----------------------------------------
  498. /** Encode email from template callback
  499. /** ----------------------------------------*/
  500. function encode_email($str)
  501. {
  502. $email = (is_array($str)) ? trim($str['1']) : trim($str);
  503. $title = '';
  504. $email = str_replace(array('"', "'"), '', $email);
  505. if ($p = strpos($email, "title="))
  506. {
  507. $title = substr($email, $p + 6);
  508. $email = trim(substr($email, 0, $p));
  509. }
  510. if ( ! class_exists('Typography'))
  511. {
  512. require PATH_CORE.'core.typography'.EXT;
  513. }
  514. return Typography::encode_email($email, $title, TRUE);
  515. }
  516. /* END */
  517. /** ----------------------------------------
  518. /** Delete spam prevention hashes
  519. /** ----------------------------------------*/
  520. function clear_spam_hashes()
  521. {
  522. global $PREFS, $DB;
  523. if ($PREFS->ini('secure_forms') == 'y')
  524. {
  525. $DB->query("DELETE FROM exp_security_hashes WHERE date < UNIX_TIMESTAMP()-7200");
  526. }
  527. }
  528. /* END */
  529. /** ----------------------------------------
  530. /** Set Cookie
  531. /** ----------------------------------------*/
  532. function set_cookie($name = '', $value = '', $expire = '')
  533. {
  534. global $PREFS;
  535. if ( ! is_numeric($expire))
  536. {
  537. $expire = time() - 86500;
  538. }
  539. else
  540. {
  541. if ($expire > 0)
  542. {
  543. $expire = time() + $expire;
  544. }
  545. else
  546. {
  547. $expire = 0;
  548. }
  549. }
  550. $prefix = ( ! $PREFS->ini('cookie_prefix')) ? 'exp_' : $PREFS->ini('cookie_prefix').'_';
  551. $path = ( ! $PREFS->ini('cookie_path')) ? '/' : $PREFS->ini('cookie_path');
  552. if (REQ == 'CP' && $PREFS->ini('multiple_sites_enabled') == 'y')
  553. {
  554. $domain = $PREFS->cp_cookie_domain;
  555. }
  556. else
  557. {
  558. $domain = ( ! $PREFS->ini('cookie_domain')) ? '' : $PREFS->ini('cookie_domain');
  559. }
  560. $value = stripslashes($value);
  561. setcookie($prefix.$name, $value, $expire, $path, $domain, 0);
  562. }
  563. /* END */
  564. /** ----------------------------------------
  565. /** Character limiter
  566. /** ----------------------------------------*/
  567. function char_limiter($str, $num = 500)
  568. {
  569. if (strlen($str) < $num)
  570. {
  571. return $str;
  572. }
  573. $str = str_replace("\n", " ", $str);
  574. $str = preg_replace("/\s+/", " ", $str);
  575. if (strlen($str) <= $num)
  576. {
  577. return $str;
  578. }
  579. $str = trim($str);
  580. $out = "";
  581. foreach (explode(" ", trim($str)) as $val)
  582. {
  583. $out .= $val;
  584. if (strlen($out) >= $num)
  585. {
  586. return (strlen($out) == strlen($str)) ? $out : $out.'&#8230;';
  587. }
  588. $out .= ' ';
  589. }
  590. }
  591. /* END */
  592. /** ----------------------------------------
  593. /** Word limiter
  594. /** ----------------------------------------*/
  595. function word_limiter($str, $num = 100)
  596. {
  597. if (strlen($str) < $num)
  598. {
  599. return $str;
  600. }
  601. // allows the split to work properly with multi-byte Unicode characters
  602. if (version_compare(phpversion(), '4.3.2', '>') === TRUE)
  603. {
  604. $word = preg_split('/\s/u', $str, -1, PREG_SPLIT_NO_EMPTY);
  605. }
  606. else
  607. {
  608. $word = preg_split('/\s/', $str, -1, PREG_SPLIT_NO_EMPTY);
  609. }
  610. if (count($word) <= $num)
  611. {
  612. return $str;
  613. }
  614. $str = "";
  615. for ($i = 0; $i < $num; $i++)
  616. {
  617. $str .= $word[$i]." ";
  618. }
  619. return trim($str).'&#8230;';
  620. }
  621. /* END */
  622. /** ----------------------------------------
  623. /** Fetch Email Template
  624. /** ----------------------------------------*/
  625. function fetch_email_template($name)
  626. {
  627. global $IN, $DB, $SESS, $PREFS;
  628. $query = $DB->query("SELECT template_name, data_title, template_data, enable_template FROM exp_specialty_templates WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' AND template_name = '".$DB->escape_str($name)."'");
  629. // Unlikely that this is necessary but it's possible a bad template request could
  630. // happen if a user hasn't run the update script.
  631. if ($query->num_rows == 0)
  632. {
  633. return array('title' => '', 'data' => '');
  634. }
  635. if ($query->row['enable_template'] == 'y')
  636. {
  637. return array('title' => $query->row['data_title'], 'data' => $query->row['template_data']);
  638. }
  639. if ($SESS->userdata['language'] != '')
  640. {
  641. $user_lang = $SESS->userdata['language'];
  642. }
  643. else
  644. {
  645. if ($IN->GBL('language', 'COOKIE'))
  646. {
  647. $user_lang = $IN->GBL('language', 'COOKIE');
  648. }
  649. elseif ($PREFS->ini('deft_lang') != '')
  650. {
  651. $user_lang = $PREFS->ini('deft_lang');
  652. }
  653. else
  654. {
  655. $user_lang = 'english';
  656. }
  657. }
  658. $user_lang = $this->filename_security($user_lang);
  659. if ( function_exists($name))
  660. {
  661. $title = $name.'_title';
  662. return array('title' => $title(), 'data' => $name());
  663. }
  664. else
  665. {
  666. if ( ! @include(PATH_LANG.$user_lang.'/email_data'.EXT))
  667. {
  668. return array('title' => $query->row['data_title'], 'data' => $query->row['template_data']);
  669. }
  670. if (function_exists($name))
  671. {
  672. $title = $name.'_title';
  673. return array('title' => $title(), 'data' => $name());
  674. }
  675. else
  676. {
  677. return array('title' => $query->row['data_title'], 'data' => $query->row['template_data']);
  678. }
  679. }
  680. }
  681. /* END */
  682. /** -----------------------------------------
  683. /** Create character encoding menu
  684. /** -----------------------------------------*/
  685. function encoding_menu($which, $name, $selected = '')
  686. {
  687. global $DSP;
  688. $files = array('languages', 'charsets');
  689. if ( ! in_array($which, $files))
  690. {
  691. return FALSE;
  692. }
  693. $file = PATH.'lib/'.$which.EXT;
  694. if ( ! file_exists($file))
  695. {
  696. return FALSE;
  697. }
  698. include($file);
  699. $r = $DSP->input_select_header($name);
  700. foreach ($$which as $key => $val)
  701. {
  702. if ($which == 'languages')
  703. {
  704. $r .= $DSP->input_select_option($val, $key, ($selected == $val) ? 1 : '');
  705. }
  706. else
  707. {
  708. $r .= $DSP->input_select_option($val, $val, ($selected == $val) ? 1 : '');
  709. }
  710. }
  711. $r .= $DSP->input_select_footer();
  712. return $r;
  713. }
  714. /* END */
  715. /** -----------------------------
  716. /** Create Directory Map
  717. /** -----------------------------*/
  718. function create_directory_map($source_dir, $top_level_only = FALSE)
  719. {
  720. if ( ! isset($filedata))
  721. $filedata = array();
  722. if ($fp = @opendir($source_dir))
  723. {
  724. while (FALSE !== ($file = readdir($fp)))
  725. {
  726. if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.' AND $top_level_only == FALSE)
  727. {
  728. $temp_array = array();
  729. $temp_array = $this->create_directory_map($source_dir.$file."/");
  730. $filedata[$file] = $temp_array;
  731. }
  732. elseif (substr($file, 0, 1) != "." && $file != 'index.html')
  733. {
  734. $filedata[] = $file;
  735. }
  736. }
  737. return $filedata;
  738. }
  739. }
  740. /* END */
  741. /** -------------------------------------------
  742. /** Create pull-down optios from dirctory map
  743. /** -------------------------------------------*/
  744. function render_map_as_select_options($zarray, $array_name = '')
  745. {
  746. foreach ($zarray as $key => $val)
  747. {
  748. if ( is_array($val))
  749. {
  750. if ($array_name != "")
  751. $key = $array_name.'/'.$key;
  752. $this->render_map_as_select_options($val, $key);
  753. }
  754. else
  755. {
  756. if ($array_name != "")
  757. {
  758. $val = $array_name.'/'.$val;
  759. }
  760. if (substr($val, -4) == '.php')
  761. {
  762. if ($val != 'theme_master.php')
  763. {
  764. $this->template_map[] = $val;
  765. }
  766. }
  767. }
  768. }
  769. }
  770. /* END */
  771. /** -----------------------------------------
  772. /** Fetch names of installed language packs
  773. /** -----------------------------------------*/
  774. function language_pack_names($default)
  775. {
  776. global $PREFS;
  777. $source_dir = PATH_LANG;
  778. $dirs = array();
  779. if ($fp = @opendir($source_dir))
  780. {
  781. while (FALSE !== ($file = readdir($fp)))
  782. {
  783. if (is_dir($source_dir.$file) && substr($file, 0, 1) != ".")
  784. {
  785. $dirs[] = $file;
  786. }
  787. }
  788. closedir($fp);
  789. }
  790. sort($dirs);
  791. $r = "<div class='default'>";
  792. $r .= "<select name='deft_lang' class='select'>\n";
  793. foreach ($dirs as $dir)
  794. {
  795. $selected = ($dir == $default) ? " selected='selected'" : '';
  796. $r .= "<option value='{$dir}'{$selected}>".ucfirst($dir)."</option>\n";
  797. }
  798. $r .= "</select>";
  799. $r .= "</div>";
  800. return $r;
  801. }
  802. /* END */
  803. /** -----------------------------------------
  804. /** Delete cache files
  805. /** -----------------------------------------*/
  806. function clear_caching($which, $sub_dir = '', $relationships=FALSE)
  807. {
  808. global $IN, $DB, $PREFS;
  809. $actions = array('page', 'tag', 'db', 'sql', 'relationships', 'all');
  810. if ( ! in_array($which, $actions))
  811. return;
  812. /* -------------------------------------
  813. /* Disable Tag Caching
  814. /*
  815. /* All for you, Nevin! Disables tag caching, which if used unwisely
  816. /* on a high traffic site can lead to disastrous disk i/o
  817. /* This setting allows quick thinking admins to temporarily disable
  818. /* it without hacking or modifying folder permissions
  819. /*
  820. /* Hidden Configuration Variable
  821. /* - disable_tag_caching => Disable tag caching? (y/n)
  822. /* -------------------------------------*/
  823. if ($which == 'tag' && $PREFS->ini('disable_tag_caching') == 'y')
  824. {
  825. return;
  826. }
  827. if ($sub_dir != '')
  828. {
  829. $sub_dir = '/'.md5($sub_dir).'/';
  830. }
  831. switch ($which)
  832. {
  833. case 'page' : $this->delete_directory(PATH_CACHE.'page_cache'.$sub_dir);
  834. break;
  835. case 'db' : $this->delete_directory(PATH_CACHE.'db_cache'.$sub_dir);
  836. break;
  837. case 'tag' : $this->delete_directory(PATH_CACHE.'tag_cache'.$sub_dir);
  838. break;
  839. case 'sql' : $this->delete_directory(PATH_CACHE.'sql_cache'.$sub_dir);
  840. break;
  841. case 'relationships' : $DB->query("UPDATE exp_relationships SET rel_data = '', reverse_rel_data = ''");
  842. break;
  843. case 'all' :
  844. $this->delete_directory(PATH_CACHE.'page_cache'.$sub_dir);
  845. $this->delete_directory(PATH_CACHE.'db_cache'.$sub_dir);
  846. $this->delete_directory(PATH_CACHE.'sql_cache'.$sub_dir);
  847. if ($PREFS->ini('disable_tag_caching') != 'y')
  848. {
  849. $this->delete_directory(PATH_CACHE.'tag_cache'.$sub_dir);
  850. }
  851. if ($relationships === TRUE)
  852. {
  853. $DB->query("UPDATE exp_relationships SET rel_data = '', reverse_rel_data = ''");
  854. }
  855. break;
  856. }
  857. }
  858. /* END */
  859. /** -----------------------------------------
  860. /** Delete Direcories
  861. /** -----------------------------------------*/
  862. function delete_directory($path, $del_root = FALSE)
  863. {
  864. $path = rtrim($path, '/');
  865. if ( ! is_dir($path))
  866. {
  867. return FALSE;
  868. }
  869. // let's try this the sane way first
  870. @exec("mv {$path} {$path}_delete", $out, $ret);
  871. if (isset($ret) && $ret == 0)
  872. {
  873. if ($del_root === FALSE)
  874. {
  875. @mkdir($path, 0777);
  876. if ($fp = @fopen($path.'/index.html', 'wb'))
  877. {
  878. fclose($fp);
  879. }
  880. }
  881. @exec("rm -r -f {$path}_delete");
  882. }
  883. else
  884. {
  885. if ( ! $current_dir = @opendir($path))
  886. {
  887. return;
  888. }
  889. while($filename = @readdir($current_dir))
  890. {
  891. if ($filename != "." AND $filename != "..")
  892. {
  893. if (@is_dir($path.'/'.$filename))
  894. {
  895. if (substr($filename, 0, 1) != '.')
  896. {
  897. $this->delete_directory($path.'/'.$filename, TRUE);
  898. }
  899. }
  900. else
  901. {
  902. @unlink($path.'/'.$filename);
  903. }
  904. }
  905. }
  906. closedir($current_dir);
  907. if (substr($path, -6) == '_cache' && $fp = @fopen($path.'/index.html', 'wb'))
  908. {
  909. fclose($fp);
  910. }
  911. if ($del_root == TRUE)
  912. {
  913. @rmdir($path);
  914. }
  915. }
  916. }
  917. /* END */
  918. /** -----------------------------------------
  919. /** Fetch allowed weblogs
  920. /** -----------------------------------------*/
  921. // This function fetches the ID numbers of the
  922. // weblogs assigned to the currently logged in user.
  923. function fetch_assigned_weblogs($all_sites = FALSE)
  924. {
  925. global $SESS, $DB, $PREFS;
  926. $allowed_blogs = array();
  927. // If the 'weblog_id' index is not zero, it means the
  928. // current user has been assigned a specifc blog
  929. if (isset($SESS->userdata['weblog_id']) AND $SESS->userdata['weblog_id'] != 0)
  930. {
  931. $allowed_blogs[] = $SESS->userdata['weblog_id'];
  932. }
  933. else
  934. {
  935. if (REQ == 'CP' AND isset($SESS->userdata['assigned_weblogs']) && $all_sites === FALSE)
  936. {
  937. $allowed_blogs = array_keys($SESS->userdata['assigned_weblogs']);
  938. }
  939. elseif ($SESS->userdata['group_id'] == 1)
  940. {
  941. if ($all_sites === TRUE)
  942. {
  943. $query = $DB->query("SELECT weblog_id FROM exp_weblogs WHERE is_user_blog = 'n'");
  944. }
  945. else
  946. {
  947. $query = $DB->query("SELECT weblog_id FROM exp_weblogs WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' AND is_user_blog = 'n'");
  948. }
  949. if ($query->num_rows > 0)
  950. {
  951. foreach ($query->result as $row)
  952. {
  953. $allowed_blogs[] = $row['weblog_id'];
  954. }
  955. }
  956. }
  957. else
  958. {
  959. if ($all_sites === TRUE)
  960. {
  961. $result = $DB->query("SELECT exp_weblog_member_groups.weblog_id FROM exp_weblog_member_groups
  962. WHERE exp_weblog_member_groups.group_id = '".$DB->escape_str($SESS->userdata['group_id'])."'");
  963. }
  964. else
  965. {
  966. $result = $DB->query("SELECT exp_weblogs.weblog_id FROM exp_weblogs, exp_weblog_member_groups
  967. WHERE exp_weblogs.weblog_id = exp_weblog_member_groups.weblog_id
  968. AND exp_weblogs.site_id = '".$DB->escape_str($PREFS->ini('site_id'))."'
  969. AND exp_weblog_member_groups.group_id = '".$DB->escape_str($SESS->userdata['group_id'])."'");
  970. }
  971. if ($result->num_rows > 0)
  972. {
  973. foreach ($result->result as $row)
  974. {
  975. $allowed_blogs[] = $row['weblog_id'];
  976. }
  977. }
  978. }
  979. }
  980. return array_values($allowed_blogs);
  981. }
  982. /* END */
  983. /** -----------------------------------------
  984. /** Fetch allowed template group
  985. /** -----------------------------------------*/
  986. // This function fetches the ID number of the
  987. // template assigned to the currently logged in user.
  988. function fetch_assigned_template_group()
  989. {
  990. global $SESS;
  991. $allowed_tg = 0;
  992. if ($SESS->userdata['tmpl_group_id'] != 0)
  993. {
  994. $allowed_tg = $SESS->userdata['tmpl_group_id'];
  995. }
  996. return $allowed_tg;
  997. }
  998. /* END */
  999. /** ----------------------------------------------
  1000. /** Log Search terms
  1001. /** ----------------------------------------------*/
  1002. function log_search_terms($terms = '', $type = 'site')
  1003. {
  1004. global $IN, $SESS, $DB, $LOC, $PREFS, $REGX;
  1005. if ($terms == '')
  1006. return;
  1007. if ($PREFS->ini('enable_search_log') == 'n')
  1008. return;
  1009. $search_log = array(
  1010. 'id' => '',
  1011. 'member_id' => $SESS->userdata('member_id'),
  1012. 'screen_name' => $SESS->userdata('screen_name'),
  1013. 'ip_address' => $IN->IP,
  1014. 'search_date' => $LOC->now,
  1015. 'search_type' => $type,
  1016. 'search_terms' => $REGX->xml_convert($REGX->encode_ee_tags($REGX->xss_clean($terms), TRUE)),
  1017. 'site_id' => $PREFS->ini('site_id')
  1018. );
  1019. $DB->query($DB->insert_string('exp_search_log', $search_log));
  1020. /** ----------------------------------
  1021. /** Prune Database
  1022. /** ----------------------------------*/
  1023. srand(time());
  1024. if ((rand() % 100) < 5)
  1025. {
  1026. $max = ( ! is_numeric($PREFS->ini('max_logged_searches'))) ? 500 : $PREFS->ini('max_logged_searches');
  1027. $query = $DB->query("SELECT MAX(id) as search_id FROM exp_search_log WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."'");
  1028. if (isset($query->row['search_id']) && $query->row['search_id'] > $max)
  1029. {
  1030. $DB->query("DELETE FROM exp_search_log WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' AND id < ".($query->row['search_id']-$max)."");
  1031. }
  1032. }
  1033. }
  1034. /* END */
  1035. /** ----------------------------------------------
  1036. /** Log Referrer data
  1037. /** ----------------------------------------------*/
  1038. function log_referrer()
  1039. {
  1040. global $IN, $PREFS, $DB, $LOC, $REGX, $SESS;
  1041. /** ----------------------------------------
  1042. /** Is the nation of the user banend?
  1043. /** ----------------------------------------*/
  1044. if ($SESS->nation_ban_check(FALSE) === FALSE)
  1045. return;
  1046. if ($PREFS->ini('log_referrers') == 'n' OR ! isset($_SERVER['HTTP_REFERER']))
  1047. {
  1048. return;
  1049. }
  1050. $site_url = $PREFS->ini('site_url');
  1051. $ref = ( ! isset($_SERVER['HTTP_REFERER'])) ? '' : $REGX->xss_clean($REGX->_html_entity_decode($_SERVER['HTTP_REFERER']));
  1052. $test_ref = strtolower($ref); // Yes, a copy, not a reference
  1053. $domain = ( ! $PREFS->ini('cookie_domain')) ? '' : $PREFS->ini('cookie_domain');
  1054. /** ---------------------------------------------
  1055. /** Throttling - Ten hits a minute is the limit
  1056. /** ---------------------------------------------*/
  1057. $query = $DB->query("SELECT COUNT(*) AS count
  1058. FROM exp_referrers
  1059. WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."'
  1060. AND (ref_from = '".$DB->escape_str($ref)."' OR ref_ip = '{$IN->IP}')
  1061. AND ref_date > '".($LOC->now-60)."'");
  1062. if ($query->row['count'] > 10)
  1063. {
  1064. return FALSE;
  1065. }
  1066. if (stristr($ref, '{') !== FALSE OR stristr($ref, '}') !== FALSE)
  1067. {
  1068. return FALSE;
  1069. }
  1070. if ( ! preg_match("#^http://\w+\.\w+\.\w*#", $ref))
  1071. {
  1072. if (substr($test_ref, 0, 7) == 'http://' AND substr($test_ref, 0, 11) != 'http://www.')
  1073. {
  1074. $test_ref = preg_replace("#^http://(.+?)#", "http://www.\\1", $test_ref);
  1075. }
  1076. }
  1077. if ( ! preg_match("#^http://\w+\.\w+\.\w*#", $site_url))
  1078. {
  1079. if (substr($site_url, 0, 7) == 'http://' AND substr($site_url, 0, 11) != 'http://www.')
  1080. {
  1081. $site_url = preg_replace("#^http://(.+?)#", "http://www.\\1", $site_url);
  1082. }
  1083. }
  1084. if ($test_ref != ''
  1085. && ! stristr($test_ref, $site_url)
  1086. && ($domain == '' || !stristr($test_ref,$domain))
  1087. && ($IN->whitelisted == 'y' OR $IN->blacklisted == 'n'))
  1088. {
  1089. /** --------------------------------
  1090. /** INSERT into database
  1091. /** --------------------------------*/
  1092. $ref_to = $REGX->xss_clean($this->fetch_current_uri());
  1093. if (stristr($ref_to, '{') !== FALSE OR stristr($ref_to, '}') !== FALSE)
  1094. {
  1095. return FALSE;
  1096. }
  1097. $insert_data = array ( 'ref_id' => '',
  1098. 'ref_from' => $ref,
  1099. 'ref_to' => $ref_to,
  1100. 'user_blog' => (USER_BLOG === FALSE) ? '' : USER_BLOG,
  1101. 'ref_ip' => $IN->IP,
  1102. 'ref_date' => $LOC->now,
  1103. 'ref_agent' => $IN->AGENT,
  1104. 'site_id' => $PREFS->ini('site_id')
  1105. );
  1106. $DB->query($DB->insert_string('exp_referrers', $insert_data));
  1107. /** ----------------------------------
  1108. /** Prune Database
  1109. /** ----------------------------------*/
  1110. srand(time());
  1111. if ((rand() % 100) < 5)
  1112. {
  1113. $max = ( ! is_numeric($PREFS->ini('max_referrers'))) ? 500 : $PREFS->ini('max_referrers');
  1114. $query = $DB->query("SELECT MAX(ref_id) as ref_id FROM exp_referrers WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."'");
  1115. if (isset($query->row['ref_id']) && $query->row['ref_id'] > $max)
  1116. {
  1117. $DB->query("DELETE FROM exp_referrers WHERE site_id = '".$DB->escape_str($PREFS->ini('site_id'))."' AND ref_id < ".($query->row['ref_id']-$max)."");
  1118. }
  1119. }
  1120. }
  1121. }
  1122. /* END */
  1123. /** ----------------------------------------------
  1124. /** Fetch Action ID
  1125. /** ----------------------------------------------*/
  1126. function fetch_action_id($class, $method)
  1127. {
  1128. global $DB;
  1129. if ($class == '' || $method == '')
  1130. {
  1131. return FALSE;
  1132. }
  1133. $this->action_ids[ucfirst($class)][$method] = $method;
  1134. return LD.'AID:'.ucfirst($class).':'.$method.RD;
  1135. }
  1136. /* END */
  1137. /** ----------------------------------------------
  1138. /** Insert Action IDs
  1139. /** ----------------------------------------------*/
  1140. function insert_action_ids($str)
  1141. {
  1142. global $DB;
  1143. if (count($this->action_ids) == 0) return $str;
  1144. $sql = "SELECT action_id, class, method FROM exp_actions WHERE";
  1145. foreach($this->action_ids as $key => $value)
  1146. {
  1147. foreach($value as $k => $v)
  1148. {
  1149. $sql .= " (class= '".$DB->escape_str($key)."' AND method = '".$DB->escape_str($v)."') OR";
  1150. }
  1151. }
  1152. $query = $DB->query(substr($sql, 0, -3));
  1153. if ($query->num_rows > 0)
  1154. {
  1155. foreach($query->result as $row)
  1156. {
  1157. $str = str_replace(LD.'AID:'.$row['class'].':'.$row['method'].RD, $row['action_id'], $str);
  1158. }
  1159. }
  1160. return $str;
  1161. }
  1162. /* END */
  1163. /** ----------------------------------------
  1164. /** Compile and cache relationship data
  1165. /** ----------------------------------------*/
  1166. // This is used when submitting new weblog entries or gallery posts.
  1167. // It serializes the related entry data. The reason it's in this
  1168. // file is becuase it gets called from the publish class and the
  1169. // gallery class so we need it somewhere that is accessible to both.
  1170. function compile_relationship($data, $parent_entry = TRUE, $reverse = FALSE)
  1171. {
  1172. global $DB;
  1173. if ($data['type'] == 'blog' OR ($reverse === TRUE && $parent_entry === FALSE))
  1174. {
  1175. $sql = "SELECT t.entry_id, t.weblog_id, t.forum_topic_id, t.author_id, t.ip_address, t.title, t.url_title, t.status, t.dst_enabled, t.view_count_one, t.view_count_two, t.view_count_three, t.view_count_four, t.allow_comments, t.comment_expiration_date, t.allow_trackbacks, t.sticky, t.entry_date, t.year, t.month, t.day, t.entry_date, t.edit_date, t.expiration_date, t.recent_comment_date, t.comment_total, t.trackback_total, t.sent_trackbacks, t.recent_trackback_date, t.site_id as entry_site_id,
  1176. w.blog_title, w.blog_name, w.blog_url, w.comment_url, w.tb_return_url, w.comment_moderate, w.weblog_html_formatting, w.weblog_allow_img_urls, w.weblog_auto_link_urls, w.enable_trackbacks, w.trackback_field, w.trackback_use_captcha, w.trackback_system_enabled,
  1177. m.username, m.email, m.url, m.screen_name, m.location, m.occupation, m.interests, m.aol_im, m.yahoo_im, m.msn_im, m.icq, m.signature, m.sig_img_filename, m.sig_img_width, m.sig_img_height, m.avatar_filename, m.avatar_width, m.avatar_height, m.photo_filename, m.photo_width, m.photo_height, m.group_id, m.member_id, m.bday_d, m.bday_m, m.bday_y, m.bio,
  1178. md.*,
  1179. wd.*
  1180. FROM exp_weblog_titles AS t
  1181. LEFT JOIN exp_weblogs AS w ON t.weblog_id = w.weblog_id
  1182. LEFT JOIN exp_weblog_data AS wd ON t.entry_id = wd.entry_id
  1183. LEFT JOIN exp_members AS m ON m.member_id = t.author_id
  1184. LEFT JOIN exp_member_data AS md ON md.member_id = m.member_id
  1185. WHERE t.entry_id = '".(($reverse === TRUE && $parent_entry === FALSE) ? $data['parent_id'] : $data['child_id'])."'";
  1186. $entry_query = $DB->query($sql);
  1187. // Is there a category group associated with this blog?
  1188. $query = $DB->query("SELECT cat_group FROM exp_weblogs WHERE weblog_id = '".$entry_query->row['weblog_id']."'");
  1189. $cat_group = ($query->num_rows == 0) ? FALSE : $query->row['cat_group'];
  1190. $this->cat_array = array();
  1191. if ($cat_group !== FALSE)
  1192. {
  1193. $this->get_categories($cat_group, ($reverse === TRUE && $parent_entry === FALSE) ? $data['parent_id'] : $data['child_id']);
  1194. $cat_array = $this->cat_array;
  1195. }
  1196. if ($parent_entry == TRUE)
  1197. {
  1198. $DB->query("INSERT INTO exp_relationships (rel_id, rel_parent_id, rel_child_id, rel_type, rel_data)
  1199. VALUES ('', '".$data['parent_id']."', '".$data['child_id']."', '".$data['type']."',
  1200. '".addslashes(serialize(array('query' => $entry_query, 'cats_fixed' => '1', 'categories' => $cat_array)))."')");
  1201. return $DB->insert_id;
  1202. }
  1203. else
  1204. {
  1205. if ($reverse === TRUE)
  1206. {
  1207. $DB->query("UPDATE exp_relationships
  1208. SET reverse_rel_data = '".addslashes(serialize(array('query' => $entry_query, 'cats_fixed' => '1', 'categories' => $cat_array)))."'
  1209. WHERE rel_type = '".$DB->escape_str($data['type'])."' AND rel_parent_id = '".$data['parent_id']."'");
  1210. }
  1211. else
  1212. {
  1213. $DB->query("UPDATE exp_relationships
  1214. SET rel_data = '".addslashes(serialize(array('query' => $entry_query, 'cats_fixed' => '1', 'categories' => $cat_array)))."'
  1215. WHERE rel_type = 'blog' AND rel_child_id = '".$data['child_id']."'");
  1216. }
  1217. }
  1218. }
  1219. elseif ($data['type'] == 'gallery')
  1220. {
  1221. $sql = "SELECT e.*,
  1222. p.gallery_image_url, p.gallery_thumb_prefix, p.gallery_medium_prefix, p.gallery_text_formatting, p.gallery_auto_link_urls, p.gallery_cf_one_formatting, p.gallery_cf_one_auto_link, p.gallery_cf_two_formatting, p.gallery_cf_two_auto_link, p.gallery_cf_three_formatting, p.gallery_cf_three_auto_link, p.gallery_cf_four_formatting, p.gallery_cf_four_auto_link, p.gallery_cf_five_formatting, p.gallery_cf_five_auto_link, p.gallery_cf_six_formatting, p.gallery_cf_six_auto_link,
  1223. c.cat_folder, c.cat_name, c.cat_description,
  1224. m.screen_name, m.username
  1225. FROM exp_gallery_entries AS e
  1226. LEFT JOIN exp_galleries AS p ON p.gallery_id = e.gallery_id
  1227. LEFT JOIN exp_gallery_categories AS c ON c.cat_id = e.cat_id
  1228. LEFT JOIN exp_members AS m ON e.author_id = m.member_id
  1229. WHERE e.entry_id = '".$data['child_id']."'";
  1230. $sql = str_replace("\t", " ", $sql);
  1231. $entry_query = $DB->query($sql);
  1232. if ($parent_entry == TRUE)
  1233. {
  1234. $DB->query("INSERT INTO exp_relationships (rel_id, rel_parent_id, rel_child_id, rel_type, rel_data) VALUES ('', '".$data['parent_id']."', '".$data['child_id']."', '".$data['type']."', '".addslashes(serialize(array('query' => $entry_query)))."')");
  1235. return $DB->insert_id;
  1236. }
  1237. else
  1238. {
  1239. $DB->query("UPDATE exp_relationships SET rel_data = '".addslashes(serialize(array('query' => $entry_query)))."' WHERE rel_type = 'gallery' AND rel_child_id = '".$data['child_id']."'");
  1240. }
  1241. }
  1242. }
  1243. /* END */
  1244. /** --------------------------------
  1245. /** Get Categories for Weblog Entry/Entries
  1246. /** --------------------------------*/
  1247. function get_categories($cat_group, $entry_id)
  1248. {
  1249. global $DB;
  1250. $sql = "SELECT c.cat_name, c.cat_url_title, c.cat_id, c.cat_image, p.cat_id, c.parent_id, c.cat_description, c.group_id
  1251. FROM exp_categories AS c, exp_category_posts AS p
  1252. WHERE c.group_id IN ('".str_replace('|', "','", $DB->escape_str($cat_group))."')
  1253. AND p.entry_id = '".$entry_id."'
  1254. AND c.cat_id = p.cat_id
  1255. ORDER BY c.parent_id, c.cat_order";
  1256. $sql = str_replace("\t", " ", $sql);
  1257. $query = $DB->query($sql);
  1258. $this->cat_array = array();
  1259. $parents = array();
  1260. if ($query->num_rows > 0)
  1261. {
  1262. $this->temp_array = array();
  1263. foreach ($query->result as $row)
  1264. {
  1265. $this->temp_array[$row['cat_id']] = array($row['cat_id'], $row['parent_id'], $row['cat_name'], $row['cat_image'], $row['cat_description'], $row['group_id'], $row['cat_url_title']);
  1266. if ($row['parent_id'] > 0 && ! isset($this->temp_array[$row['parent_id']])) $parents[$row['parent_id']] = '';
  1267. unset($parents[$row['cat_id']]);
  1268. }
  1269. foreach($this->temp_array as $k => $v)
  1270. {
  1271. if (isset($parents[$v['1']])) $v['1'] = 0;
  1272. if (0 == $v['1'])
  1273. {
  1274. $this->cat_array[] = $v;
  1275. $this->process_subcategories($k);
  1276. }
  1277. }
  1278. unset($this->temp_array);
  1279. }
  1280. }
  1281. /* END */
  1282. /** --------------------------------
  1283. /** Process Subcategories
  1284. /** --------------------------------*/
  1285. function process_subcategories($parent_id)
  1286. {
  1287. foreach($this->temp_array as $key => $val)
  1288. {
  1289. if ($parent_id == $val['1'])
  1290. {
  1291. $this->cat_array[] = $val;
  1292. $this->process_subcategories($key);
  1293. }
  1294. }
  1295. }
  1296. /* END */
  1297. /** -----------------------------------
  1298. /** Add security hashes to forms
  1299. /** -----------------------------------*/
  1300. function add_form_security_hash($str)
  1301. {
  1302. global $PREFS, $IN, $DB;
  1303. if ($PREFS->ini('secure_forms') == 'y')
  1304. {
  1305. if (preg_match_all("/({XID_HASH})/", $str, $matches))
  1306. {
  1307. $db_reset = FALSE;
  1308. // Disable DB caching if it's currently set
  1309. if ($DB->enable_cache == TRUE)
  1310. {
  1311. $DB->enable_cache = FALSE;
  1312. $db_reset = TRUE;
  1313. }
  1314. // Add security hashes
  1315. $sql = "INSERT INTO exp_security_hashes (date, ip_address, hash) VALUES";
  1316. foreach ($matches['1'] as $val)
  1317. {
  1318. $hash = $this->random('encrypt');
  1319. $str = preg_replace("/{XID_HASH}/", $hash, $str, 1);
  1320. $sql .= "(UNIX_TIMESTAMP(), '".$IN->IP."', '".$hash."'),";
  1321. }
  1322. $DB->query(substr($sql,0,-1));
  1323. // Re-enable DB caching
  1324. if ($db_reset == TRUE)
  1325. {
  1326. $DB->enable_cache = TRUE;
  1327. }
  1328. }
  1329. }
  1330. return $str;
  1331. }
  1332. /** -----------------------------------
  1333. /** Remap pMachine Pro URLs
  1334. /** -----------------------------------*/
  1335. // Since pM URLs are different than EE URLs,
  1336. // for those who have migrated from pM we will
  1337. // check the URL formatting. If the request is
  1338. // for a pMachine URL, we'll remap it to the new EE location
  1339. function remap_pm_urls()
  1340. {
  1341. global $DB, $IN, $PREFS;
  1342. if ($PREFS->ini('remap_pm_urls') == 'y' AND $PREFS->ini('remap_pm_dest') !== FALSE AND $IN->URI != '')
  1343. {
  1344. $p_uri = ( ! isset($_GET['id'])) ? $IN->URI : '/'.$_GET['id'].'/';
  1345. if (preg_match("#^/[0-9]{1,6}\_[0-9]{1,4}\_[0-9]{1,4}\_[0-9]{1,4}.*$#", $p_uri))
  1346. {
  1347. $pentry_id = substr($p_uri, 1, (strpos($p_uri, '_')-1));
  1348. }
  1349. elseif (preg_match("#^/P[0-9]{1,6}.*$#", $p_uri))
  1350. {
  1351. $p_uri = str_replace("/", "", $p_uri);
  1352. $pentry_id = substr($p_uri, 1);
  1353. }
  1354. if (isset($pentry_id) AND $pentry_id != '')
  1355. {
  1356. $query = $DB->query("SELECT url_title FROM exp_weblog_titles WHERE pentry_id = '".$DB->escape_str($pentry_id)."'");
  1357. if ($query->num_rows == 1)
  1358. {
  1359. $this->redirect($PREFS->ini('remap_pm_dest', 1).$query->row['url_title'].'/');
  1360. exit;
  1361. }
  1362. }
  1363. }
  1364. }
  1365. /* END */
  1366. /** -----------------------------------
  1367. /** Generate Captcha
  1368. /** -----------------------------------*/
  1369. function create_captcha($old_word = '')
  1370. {
  1371. global $DB, $IN, $PREFS, $SESS, $EXT;
  1372. if ($PREFS->ini('captcha_require_members') == 'n' AND $SESS->userdata['member_id'] != 0)
  1373. {
  1374. return '';
  1375. }
  1376. // -------------------------------------------
  1377. // 'create_captcha_start' hook.
  1378. // - Allows rewrite of how CAPTCHAs are created
  1379. //
  1380. if ($EXT->active_hook('create_captcha_start') === TRUE)
  1381. {
  1382. $edata = $EXT->call_extension('create_captcha_start', $old_word);
  1383. if ($EXT->end_script === TRUE) return $edata;
  1384. }
  1385. //
  1386. // -------------------------------------------
  1387. $img_path = $PREFS->ini('captcha_path', 1, TRUE);
  1388. $img_url = $PREFS->ini('captcha_url', 1);
  1389. $use_font = ($PREFS->ini('captcha_font') == 'y') ? TRUE : FALSE;
  1390. $font_face = "texb.ttf";
  1391. $font_size = 16;
  1392. $expiration = 60*60*2; // 2 hours
  1393. $img_width = 140; // Image width
  1394. $img_height = 30; // Image height
  1395. if ($img_path == '' || $img_url == '')
  1396. {
  1397. return FALSE;
  1398. }
  1399. if ( ! @is_dir($img_path))
  1400. {
  1401. return FALSE;
  1402. }
  1403. if ( ! is_writable($img_pat

Large files files are truncated, but you can click here to view the full file