PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/joomla.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 6162 lines | 5299 code | 229 blank | 634 comment | 243 complexity | cf8c9fc2ca538599acf0e77cba0d55f8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

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

  1. <?php
  2. /**
  3. * @version $Id: joomla.php 5993 2006-12-13 00:24:58Z friesengeist $
  4. * @package Joomla
  5. * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. // no direct access
  14. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  15. define( '_MOS_MAMBO_INCLUDED', 1 );
  16. /**
  17. * Page generation time
  18. * @package Joomla
  19. */
  20. class mosProfiler {
  21. /** @var int Start time stamp */
  22. var $start=0;
  23. /** @var string A prefix for mark messages */
  24. var $prefix='';
  25. /**
  26. * Constructor
  27. * @param string A prefix for mark messages
  28. */
  29. function mosProfiler( $prefix='' ) {
  30. $this->start = $this->getmicrotime();
  31. $this->prefix = $prefix;
  32. }
  33. /**
  34. * @return string A format message of the elapsed time
  35. */
  36. function mark( $label ) {
  37. return sprintf ( "\n<div class=\"profiler\">$this->prefix %.3f $label</div>", $this->getmicrotime() - $this->start );
  38. }
  39. /**
  40. * @return float The current time in milliseconds
  41. */
  42. function getmicrotime(){
  43. list($usec, $sec) = explode(" ",microtime());
  44. return ((float)$usec + (float)$sec);
  45. }
  46. }
  47. if (phpversion() < '4.2.0') {
  48. require_once( dirname( __FILE__ ) . '/compat.php41x.php' );
  49. }
  50. if (phpversion() < '4.3.0') {
  51. require_once( dirname( __FILE__ ) . '/compat.php42x.php' );
  52. }
  53. if (version_compare( phpversion(), '5.0' ) < 0) {
  54. require_once( dirname( __FILE__ ) . '/compat.php50x.php' );
  55. }
  56. @set_magic_quotes_runtime( 0 );
  57. if ( @$mosConfig_error_reporting === 0 || @$mosConfig_error_reporting === '0' ) {
  58. error_reporting( 0 );
  59. } else if (@$mosConfig_error_reporting > 0) {
  60. error_reporting( $mosConfig_error_reporting );
  61. }
  62. require_once( $mosConfig_absolute_path . '/includes/version.php' );
  63. require_once( $mosConfig_absolute_path . '/includes/database.php' );
  64. require_once( $mosConfig_absolute_path . '/includes/gacl.class.php' );
  65. require_once( $mosConfig_absolute_path . '/includes/gacl_api.class.php' );
  66. require_once( $mosConfig_absolute_path . '/includes/phpmailer/class.phpmailer.php' );
  67. require_once( $mosConfig_absolute_path . '/includes/joomla.xml.php' );
  68. require_once( $mosConfig_absolute_path . '/includes/phpInputFilter/class.inputfilter.php' );
  69. $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  70. if ($database->getErrorNum()) {
  71. $mosSystemError = $database->getErrorNum();
  72. $basePath = dirname( __FILE__ );
  73. include $basePath . '/../configuration.php';
  74. include $basePath . '/../offline.php';
  75. exit();
  76. }
  77. $database->debug( $mosConfig_debug );
  78. $acl = new gacl_api();
  79. // platform neurtral url handling
  80. if ( isset( $_SERVER['REQUEST_URI'] ) ) {
  81. $request_uri = $_SERVER['REQUEST_URI'];
  82. } else {
  83. $request_uri = $_SERVER['SCRIPT_NAME'];
  84. // Append the query string if it exists and isn't null
  85. if ( isset( $_SERVER['QUERY_STRING'] ) && !empty( $_SERVER['QUERY_STRING'] ) ) {
  86. $request_uri .= '?' . $_SERVER['QUERY_STRING'];
  87. }
  88. }
  89. $_SERVER['REQUEST_URI'] = $request_uri;
  90. // current server time
  91. $now = date( 'Y-m-d H:i', time() );
  92. DEFINE( '_CURRENT_SERVER_TIME', $now );
  93. DEFINE( '_CURRENT_SERVER_TIME_FORMAT', '%Y-%m-%d %H:%M:%S' );
  94. // Non http/https URL Schemes
  95. $url_schemes = 'data:, file:, ftp:, gopher:, imap:, ldap:, mailto:, news:, nntp:, telnet:, javascript:, irc:, mms:';
  96. DEFINE( '_URL_SCHEMES', $url_schemes );
  97. // disable strict mode in MySQL 5
  98. if (!defined( '_JOS_SET_SQLMODE' )) {
  99. /** ensure that functions are declared only once */
  100. define( '_JOS_SET_SQLMODE', 1 );
  101. // if running mysql 5, set sql-mode to mysql40 - thereby circumventing strict mode problems
  102. if ( strpos( $database->getVersion(), '5' ) === 0 ) {
  103. $query = "SET sql_mode = 'MYSQL40'";
  104. $database->setQuery( $query );
  105. $database->query();
  106. }
  107. }
  108. /**
  109. * @package Joomla
  110. * @abstract
  111. */
  112. class mosAbstractLog {
  113. /** @var array */
  114. var $_log = null;
  115. /**
  116. * Constructor
  117. */
  118. function mosAbstractLog() {
  119. $this->__constructor();
  120. }
  121. /**
  122. * Generic constructor
  123. */
  124. function __constructor() {
  125. $this->_log = array();
  126. }
  127. /**
  128. * @param string Log message
  129. * @param boolean True to append to last message
  130. */
  131. function log( $text, $append=false ) {
  132. $n = count( $this->_log );
  133. if ($append && $n > 0) {
  134. $this->_log[count( $this->_log )-1] .= $text;
  135. } else {
  136. $this->_log[] = $text;
  137. }
  138. }
  139. /**
  140. * @param string The glue for each log item
  141. * @return string Returns the log
  142. */
  143. function getLog( $glue='<br/>', $truncate=9000, $htmlSafe=false ) {
  144. $logs = array();
  145. foreach ($this->_log as $log) {
  146. if ($htmlSafe) {
  147. $log = htmlspecialchars( $log );
  148. }
  149. $logs[] = substr( $log, 0, $truncate );
  150. }
  151. return implode( $glue, $logs );
  152. }
  153. }
  154. /**
  155. * Task routing class
  156. * @package Joomla
  157. * @abstract
  158. */
  159. class mosAbstractTasker {
  160. /** @var array An array of the class methods to call for a task */
  161. var $_taskMap = null;
  162. /** @var string The name of the current task*/
  163. var $_task = null;
  164. /** @var array An array of the class methods*/
  165. var $_methods = null;
  166. /** @var string A url to redirect to */
  167. var $_redirect = null;
  168. /** @var string A message about the operation of the task */
  169. var $_message = null;
  170. // action based access control
  171. /** @var string The ACO Section */
  172. var $_acoSection = null;
  173. /** @var string The ACO Section value */
  174. var $_acoSectionValue = null;
  175. /**
  176. * Constructor
  177. * @param string Set the default task
  178. */
  179. function mosAbstractTasker( $default='' ) {
  180. $this->_taskMap = array();
  181. $this->_methods = array();
  182. foreach (get_class_methods( get_class( $this ) ) as $method) {
  183. if (substr( $method, 0, 1 ) != '_') {
  184. $this->_methods[] = strtolower( $method );
  185. // auto register public methods as tasks
  186. $this->_taskMap[strtolower( $method )] = $method;
  187. }
  188. }
  189. $this->_redirect = '';
  190. $this->_message = '';
  191. if ($default) {
  192. $this->registerDefaultTask( $default );
  193. }
  194. }
  195. /**
  196. * Sets the access control levels
  197. * @param string The ACO section (eg, the component)
  198. * @param string The ACO section value (if using a constant value)
  199. */
  200. function setAccessControl( $section, $value=null ) {
  201. $this->_acoSection = $section;
  202. $this->_acoSectionValue = $value;
  203. }
  204. /**
  205. * Access control check
  206. */
  207. function accessCheck( $task ) {
  208. global $acl, $my;
  209. // only check if the derived class has set these values
  210. if ($this->_acoSection) {
  211. // ensure user has access to this function
  212. if ($this->_acoSectionValue) {
  213. // use a 'constant' task for this task handler
  214. $task = $this->_acoSectionValue;
  215. }
  216. return $acl->acl_check( $this->_acoSection, $task, 'users', $my->usertype );
  217. } else {
  218. return true;
  219. }
  220. }
  221. /**
  222. * Set a URL to redirect the browser to
  223. * @param string A URL
  224. */
  225. function setRedirect( $url, $msg = null ) {
  226. $this->_redirect = $url;
  227. if ($msg !== null) {
  228. $this->_message = $msg;
  229. }
  230. }
  231. /**
  232. * Redirects the browser
  233. */
  234. function redirect() {
  235. if ($this->_redirect) {
  236. mosRedirect( $this->_redirect, $this->_message );
  237. }
  238. }
  239. /**
  240. * Register (map) a task to a method in the class
  241. * @param string The task
  242. * @param string The name of the method in the derived class to perform for this task
  243. */
  244. function registerTask( $task, $method ) {
  245. if (in_array( strtolower( $method ), $this->_methods )) {
  246. $this->_taskMap[strtolower( $task )] = $method;
  247. } else {
  248. $this->methodNotFound( $method );
  249. }
  250. }
  251. /**
  252. * Register the default task to perfrom if a mapping is not found
  253. * @param string The name of the method in the derived class to perform if the task is not found
  254. */
  255. function registerDefaultTask( $method ) {
  256. $this->registerTask( '__default', $method );
  257. }
  258. /**
  259. * Perform a task by triggering a method in the derived class
  260. * @param string The task to perform
  261. * @return mixed The value returned by the function
  262. */
  263. function performTask( $task ) {
  264. $this->_task = $task;
  265. $task = strtolower( $task );
  266. if (isset( $this->_taskMap[$task] )) {
  267. $doTask = $this->_taskMap[$task];
  268. } else if (isset( $this->_taskMap['__default'] )) {
  269. $doTask = $this->_taskMap['__default'];
  270. } else {
  271. return $this->taskNotFound( $this->_task );
  272. }
  273. if ($this->accessCheck( $doTask )) {
  274. return call_user_func( array( &$this, $doTask ) );
  275. } else {
  276. return $this->notAllowed( $task );
  277. }
  278. }
  279. /**
  280. * Get the last task that was to be performed
  281. * @return string The task that was or is being performed
  282. */
  283. function getTask() {
  284. return $this->_task;
  285. }
  286. /**
  287. * Basic method if the task is not found
  288. * @param string The task
  289. * @return null
  290. */
  291. function taskNotFound( $task ) {
  292. echo 'Task ' . $task . ' not found';
  293. return null;
  294. }
  295. /**
  296. * Basic method if the registered method is not found
  297. * @param string The name of the method in the derived class
  298. * @return null
  299. */
  300. function methodNotFound( $name ) {
  301. echo 'Method ' . $name . ' not found';
  302. return null;
  303. }
  304. /**
  305. * Basic method if access is not permitted to the task
  306. * @param string The name of the method in the derived class
  307. * @return null
  308. */
  309. function notAllowed( $name ) {
  310. echo _NOT_AUTH;
  311. return null;
  312. }
  313. }
  314. /**
  315. * Class to support function caching
  316. * @package Joomla
  317. */
  318. class mosCache {
  319. /**
  320. * @return object A function cache object
  321. */
  322. function &getCache( $group='' ) {
  323. global $mosConfig_absolute_path, $mosConfig_caching, $mosConfig_cachepath, $mosConfig_cachetime;
  324. require_once( $mosConfig_absolute_path . '/includes/joomla.cache.php' );
  325. $options = array(
  326. 'cacheDir' => $mosConfig_cachepath . '/',
  327. 'caching' => $mosConfig_caching,
  328. 'defaultGroup' => $group,
  329. 'lifeTime' => $mosConfig_cachetime
  330. );
  331. $cache = new JCache_Lite_Function( $options );
  332. return $cache;
  333. }
  334. /**
  335. * Cleans the cache
  336. */
  337. function cleanCache( $group=false ) {
  338. global $mosConfig_caching;
  339. if ($mosConfig_caching) {
  340. $cache =& mosCache::getCache( $group );
  341. $cache->clean( $group );
  342. }
  343. }
  344. }
  345. /**
  346. * Joomla! Mainframe class
  347. *
  348. * Provide many supporting API functions
  349. * @package Joomla
  350. */
  351. class mosMainFrame {
  352. /** @var database Internal database class pointer */
  353. var $_db = null;
  354. /** @var object An object of configuration variables */
  355. var $_config = null;
  356. /** @var object An object of path variables */
  357. var $_path = null;
  358. /** @var mosSession The current session */
  359. var $_session = null;
  360. /** @var string The current template */
  361. var $_template = null;
  362. /** @var array An array to hold global user state within a session */
  363. var $_userstate = null;
  364. /** @var array An array of page meta information */
  365. var $_head = null;
  366. /** @var string Custom html string to append to the pathway */
  367. var $_custom_pathway = null;
  368. /** @var boolean True if in the admin client */
  369. var $_isAdmin = false;
  370. /**
  371. * Class constructor
  372. * @param database A database connection object
  373. * @param string The url option
  374. * @param string The path of the mos directory
  375. */
  376. function mosMainFrame( &$db, $option, $basePath, $isAdmin=false ) {
  377. $this->_db =& $db;
  378. // load the configuration values
  379. $this->_setTemplate( $isAdmin );
  380. $this->_setAdminPaths( $option, $this->getCfg( 'absolute_path' ) );
  381. if (isset( $_SESSION['session_userstate'] )) {
  382. $this->_userstate =& $_SESSION['session_userstate'];
  383. } else {
  384. $this->_userstate = null;
  385. }
  386. $this->_head = array();
  387. $this->_head['title'] = $GLOBALS['mosConfig_sitename'];
  388. $this->_head['meta'] = array();
  389. $this->_head['custom'] = array();
  390. //set the admin check
  391. $this->_isAdmin = (boolean) $isAdmin;
  392. $now = date( 'Y-m-d H:i:s', time() );
  393. $this->set( 'now', $now );
  394. }
  395. /**
  396. * Gets the id number for a client
  397. * @param mixed A client identifier
  398. */
  399. function getClientID( $client ) {
  400. switch ($client) {
  401. case '2':
  402. case 'installation':
  403. return 2;
  404. break;
  405. case '1':
  406. case 'admin':
  407. case 'administrator':
  408. return 1;
  409. break;
  410. case '0':
  411. case 'site':
  412. case 'front':
  413. default:
  414. return 0;
  415. break;
  416. }
  417. }
  418. /**
  419. * Gets the client name
  420. * @param int The client identifier
  421. * @return strint The text name of the client
  422. */
  423. function getClientName( $client_id ) {
  424. // do not translate
  425. $clients = array( 'site', 'admin', 'installer' );
  426. return mosGetParam( $clients, $client_id, 'unknown' );
  427. }
  428. /**
  429. * Gets the base path for the client
  430. * @param mixed A client identifier
  431. * @param boolean True (default) to add traling slash
  432. */
  433. function getBasePath( $client=0, $addTrailingSlash=true ) {
  434. global $mosConfig_absolute_path;
  435. switch ($client) {
  436. case '0':
  437. case 'site':
  438. case 'front':
  439. default:
  440. return mosPathName( $mosConfig_absolute_path, $addTrailingSlash );
  441. break;
  442. case '2':
  443. case 'installation':
  444. return mosPathName( $mosConfig_absolute_path . '/installation', $addTrailingSlash );
  445. break;
  446. case '1':
  447. case 'admin':
  448. case 'administrator':
  449. return mosPathName( $mosConfig_absolute_path . '/administrator', $addTrailingSlash );
  450. break;
  451. }
  452. }
  453. /**
  454. * @param string
  455. */
  456. function setPageTitle( $title=null ) {
  457. if (@$GLOBALS['mosConfig_pagetitles']) {
  458. $title = trim( htmlspecialchars( $title ) );
  459. $title = stripslashes($title);
  460. $this->_head['title'] = $title ? $GLOBALS['mosConfig_sitename'] . ' - '. $title : $GLOBALS['mosConfig_sitename'];
  461. }
  462. //Organic: TO make XHTML strict target="_blank" must be replaced with rel="external"
  463. $this->addCustomHeadTag("<script type='text/javascript' src='".$GLOBALS['mosConfig_live_site']."/includes/externalLink.js'></script>");
  464. }
  465. /**
  466. * @param string The value of the name attibute
  467. * @param string The value of the content attibute
  468. * @param string Text to display before the tag
  469. * @param string Text to display after the tag
  470. */
  471. function addMetaTag( $name, $content, $prepend='', $append='' ) {
  472. $name = trim( htmlspecialchars( $name ) );
  473. $content = trim( htmlspecialchars( $content ) );
  474. $prepend = trim( $prepend );
  475. $append = trim( $append );
  476. $this->_head['meta'][] = array( $name, $content, $prepend, $append );
  477. }
  478. /**
  479. * @param string The value of the name attibute
  480. * @param string The value of the content attibute to append to the existing
  481. * Tags ordered in with Site Keywords and Description first
  482. */
  483. function appendMetaTag( $name, $content ) {
  484. $name = trim( htmlspecialchars( $name ) );
  485. $n = count( $this->_head['meta'] );
  486. for ($i = 0; $i < $n; $i++) {
  487. if ($this->_head['meta'][$i][0] == $name) {
  488. $content = trim( htmlspecialchars( $content ) );
  489. if ( $content ) {
  490. if ( !$this->_head['meta'][$i][1] ) {
  491. $this->_head['meta'][$i][1] = $content ;
  492. } else {
  493. $this->_head['meta'][$i][1] = $content .', '. $this->_head['meta'][$i][1];
  494. }
  495. }
  496. return;
  497. }
  498. }
  499. $this->addMetaTag( $name , $content );
  500. }
  501. /**
  502. * @param string The value of the name attibute
  503. * @param string The value of the content attibute to append to the existing
  504. */
  505. function prependMetaTag( $name, $content ) {
  506. $name = trim( htmlspecialchars( $name ) );
  507. $n = count( $this->_head['meta'] );
  508. for ($i = 0; $i < $n; $i++) {
  509. if ($this->_head['meta'][$i][0] == $name) {
  510. $content = trim( htmlspecialchars( $content ) );
  511. $this->_head['meta'][$i][1] = $content . $this->_head['meta'][$i][1];
  512. return;
  513. }
  514. }
  515. $this->addMetaTag( $name, $content );
  516. }
  517. /**
  518. * Adds a custom html string to the head block
  519. * @param string The html to add to the head
  520. */
  521. function addCustomHeadTag( $html ) {
  522. $this->_head['custom'][] = trim( $html );
  523. }
  524. /**
  525. * @return string
  526. */
  527. function getHead() {
  528. global $mosConfig_mootools, $mosConfig_live_site;
  529. $head = array();
  530. $head[] = '<title>' . $this->_head['title'] . '</title>';
  531. foreach ($this->_head['meta'] as $meta) {
  532. if ($meta[2]) {
  533. $head[] = $meta[2];
  534. }
  535. $head[] = '<meta name="' . $meta[0] . '" content="' . $meta[1] . '" />';
  536. if ($meta[3]) {
  537. $head[] = $meta[3];
  538. }
  539. }
  540. if($mosConfig_mootools) $head[] = "<script type='text/javascript' src='$mosConfig_live_site/includes/js/mootools-release-1.11.js'></script>";
  541. foreach ($this->_head['custom'] as $html) {
  542. $head[] = $html;
  543. }
  544. return implode( "\n", $head ) . "\n";
  545. }
  546. /**
  547. * @return string
  548. */
  549. function getPageTitle() {
  550. return $this->_head['title'];
  551. }
  552. /**
  553. * @return string
  554. */
  555. function getCustomPathWay() {
  556. return $this->_custom_pathway;
  557. }
  558. function appendPathWay( $html ) {
  559. $this->_custom_pathway[] = $html;
  560. }
  561. /**
  562. * Gets the value of a user state variable
  563. * @param string The name of the variable
  564. */
  565. function getUserState( $var_name ) {
  566. if (is_array( $this->_userstate )) {
  567. return mosGetParam( $this->_userstate, $var_name, null );
  568. } else {
  569. return null;
  570. }
  571. }
  572. /**
  573. * Gets the value of a user state variable
  574. * @param string The name of the user state variable
  575. * @param string The name of the variable passed in a request
  576. * @param string The default value for the variable if not found
  577. */
  578. function getUserStateFromRequest( $var_name, $req_name, $var_default=null ) {
  579. if (is_array( $this->_userstate )) {
  580. if (isset( $_REQUEST[$req_name] )) {
  581. $this->setUserState( $var_name, $_REQUEST[$req_name] );
  582. } else if (!isset( $this->_userstate[$var_name] )) {
  583. $this->setUserState( $var_name, $var_default );
  584. }
  585. // filter input
  586. $iFilter = new InputFilter();
  587. $this->_userstate[$var_name] = $iFilter->process( $this->_userstate[$var_name] );
  588. return $this->_userstate[$var_name];
  589. } else {
  590. return null;
  591. }
  592. }
  593. /**
  594. * Sets the value of a user state variable
  595. * @param string The name of the variable
  596. * @param string The value of the variable
  597. */
  598. function setUserState( $var_name, $var_value ) {
  599. if (is_array( $this->_userstate )) {
  600. $this->_userstate[$var_name] = $var_value;
  601. }
  602. }
  603. /**
  604. * Initialises the user session
  605. *
  606. * Old sessions are flushed based on the configuration value for the cookie
  607. * lifetime. If an existing session, then the last access time is updated.
  608. * If a new session, a session id is generated and a record is created in
  609. * the jos_sessions table.
  610. */
  611. function initSession() {
  612. // initailize session variables
  613. $session =& $this->_session;
  614. $session = new mosSession( $this->_db );
  615. // purge expired sessions
  616. $session->purge('core');
  617. // Session Cookie `name`
  618. $sessionCookieName = mosMainFrame::sessionCookieName();
  619. // Get Session Cookie `value`
  620. $sessioncookie = strval( mosGetParam( $_COOKIE, $sessionCookieName, null ) );
  621. // Session ID / `value`
  622. $sessionValueCheck = mosMainFrame::sessionCookieValue( $sessioncookie );
  623. // Check if existing session exists in db corresponding to Session cookie `value`
  624. // extra check added in 1.0.8 to test sessioncookie value is of correct length
  625. if ( $sessioncookie && strlen($sessioncookie) == 32 && $sessioncookie != '-' && $session->load($sessionValueCheck) ) {
  626. // update time in session table
  627. $session->time = time();
  628. $session->update();
  629. } else {
  630. // Remember Me Cookie `name`
  631. $remCookieName = mosMainFrame::remCookieName_User();
  632. // test if cookie found
  633. $cookie_found = false;
  634. if ( isset($_COOKIE[$sessionCookieName]) || isset($_COOKIE[$remCookieName]) || isset($_POST['force_session']) ) {
  635. $cookie_found = true;
  636. }
  637. // check if neither remembermecookie or sessioncookie found
  638. if (!$cookie_found) {
  639. // create sessioncookie and set it to a test value set to expire on session end
  640. setcookie( $sessionCookieName, '-', false, '/' );
  641. } else {
  642. // otherwise, sessioncookie was found, but set to test val or the session expired, prepare for session registration and register the session
  643. $url = strval( mosGetParam( $_SERVER, 'REQUEST_URI', null ) );
  644. // stop sessions being created for requests to syndicated feeds
  645. if ( strpos( $url, 'option=com_rss' ) === false && strpos( $url, 'feed=' ) === false ) {
  646. $session->guest = 1;
  647. $session->username = '';
  648. $session->time = time();
  649. $session->gid = 0;
  650. // Generate Session Cookie `value`
  651. $session->generateId();
  652. if (!$session->insert()) {
  653. die( $session->getError() );
  654. }
  655. // create Session Tracking Cookie set to expire on session end
  656. setcookie( $sessionCookieName, $session->getCookie(), false, '/' );
  657. }
  658. }
  659. // Cookie used by Remember me functionality
  660. $remCookieValue = strval( mosGetParam( $_COOKIE, $remCookieName, null ) );
  661. // test if cookie is correct length
  662. if ( strlen($remCookieValue) > 64 ) {
  663. // Separate Values from Remember Me Cookie
  664. $remUser = substr( $remCookieValue, 0, 32 );
  665. $remPass = substr( $remCookieValue, 32, 32 );
  666. $remID = intval( substr( $remCookieValue, 64 ) );
  667. // check if Remember me cookie exists. Login with usercookie info.
  668. if ( strlen($remUser) == 32 && strlen($remPass) == 32 ) {
  669. $this->login( $remUser, $remPass, 1, $remID );
  670. }
  671. }
  672. }
  673. }
  674. /*
  675. * Function used to conduct admin session duties
  676. * Added as of 1.0.8
  677. * Deperciated 1.1
  678. */
  679. function initSessionAdmin($option, $task) {
  680. global $_VERSION, $mosConfig_admin_expired;
  681. // logout check
  682. if ($option == 'logout') {
  683. require $GLOBALS['mosConfig_absolute_path'] .'/administrator/logout.php';
  684. exit();
  685. }
  686. $site = $GLOBALS['mosConfig_live_site'];
  687. // check if session name corresponds to correct format
  688. if ( session_name() != md5( $site ) ) {
  689. echo "<script>document.location.href='index.php'</script>\n";
  690. exit();
  691. }
  692. // restore some session variables
  693. $my = new mosUser( $this->_db );
  694. $my->id = intval( mosGetParam( $_SESSION, 'session_user_id', '' ) );
  695. $my->username = strval( mosGetParam( $_SESSION, 'session_username', '' ) );
  696. $my->usertype = strval( mosGetParam( $_SESSION, 'session_usertype', '' ) );
  697. $my->gid = intval( mosGetParam( $_SESSION, 'session_gid', '' ) );
  698. $my->params = mosGetParam( $_SESSION, 'session_user_params', '' );
  699. //Create params object
  700. $params = new mosParameters($my->params);
  701. $session_id = mosGetParam( $_SESSION, 'session_id', '' );
  702. $logintime = mosGetParam( $_SESSION, 'session_logintime', '' );
  703. if ($session_id != session_id()) {
  704. // session id does not correspond to required session format
  705. echo "<script>document.location.href='index.php?mosmsg=Invalid Session'</script>\n";
  706. exit();
  707. }
  708. // check to see if session id corresponds with correct format
  709. if ( $session_id == md5( $my->id . $my->username . $my->usertype . $logintime ) ) {
  710. // if task action is to `save` or `apply` complete action before doing session checks.
  711. if ($task != 'save' && $task != 'apply') {
  712. // test for session_life_admin
  713. if ( @$GLOBALS['mosConfig_session_life_admin'] ) {
  714. $session_life_admin = $GLOBALS['mosConfig_session_life_admin'];
  715. } else {
  716. $session_life_admin = 1800;
  717. }
  718. // purge expired admin sessions only
  719. $past = time() - $session_life_admin;
  720. $query = "DELETE FROM #__session"
  721. . "\n WHERE time < '" . (int) $past . "'"
  722. . "\n AND guest = 1"
  723. . "\n AND gid = 0"
  724. . "\n AND userid <> 0"
  725. ;
  726. $this->_db->setQuery( $query );
  727. if(!$params->get('keep_session',0)) $this->_db->query();
  728. // update session timestamp
  729. $current_time = time();
  730. $query = "UPDATE #__session"
  731. . "\n SET time = " . $this->_db->Quote( $current_time )
  732. . "\n WHERE session_id = " . $this->_db->Quote( $session_id )
  733. ;
  734. $this->_db->setQuery( $query );
  735. $this->_db->query();
  736. // set garbage cleaning timeout
  737. $this->setSessionGarbageClean();
  738. // check against db record of session
  739. $query = "SELECT COUNT( session_id )"
  740. . "\n FROM #__session"
  741. . "\n WHERE session_id = " . $this->_db->Quote( $session_id )
  742. . "\n AND username = ". $this->_db->Quote( $my->username )
  743. . "\n AND userid = ". intval( $my->id )
  744. ;
  745. $this->_db->setQuery( $query );
  746. $count = $this->_db->loadResult();
  747. // if no entry in session table that corresponds boot from admin area
  748. if ( $count == 0 ) {
  749. $link = NULL;
  750. if ($_SERVER['QUERY_STRING']) {
  751. $link = 'index2.php?'. $_SERVER['QUERY_STRING'];
  752. }
  753. // check if site designated as a production site
  754. // for a demo site disallow expired page functionality
  755. // link must also be a Joomla link to stop malicious redirection
  756. if ( $link && strpos( $link, 'index2.php?option=com_' ) === 0 && $_VERSION->SITE == 1 && @$mosConfig_admin_expired === '1' ) {
  757. $now = time();
  758. $file = $this->getPath( 'com_xml', 'com_users' );
  759. $params = new mosParameters( $my->params, $file, 'component' );
  760. // return to expired page functionality
  761. $params->set( 'expired', $link );
  762. $params->set( 'expired_time', $now );
  763. // param handling
  764. if (is_array( $params->toArray() )) {
  765. $txt = array();
  766. foreach ( $params->toArray() as $k=>$v) {
  767. $txt[] = "$k=$v";
  768. }
  769. $saveparams = implode( "\n", $txt );
  770. }
  771. // save expired page info to user data
  772. $query = "UPDATE #__users"
  773. . "\n SET params = ". $this->_db->Quote( $saveparams )
  774. . "\n WHERE id = " . (int) $my->id
  775. . "\n AND username = ". $this->_db->Quote( $my->username )
  776. . "\n AND usertype = ". $this->_db->Quote( $my->usertype )
  777. ;
  778. $this->_db->setQuery( $query );
  779. $this->_db->query();
  780. }
  781. echo "<script>document.location.href='index.php?mosmsg=Admin Session Expired'</script>\n";
  782. exit();
  783. } else {
  784. // load variables into session, used to help secure /popups/ functionality
  785. $_SESSION['option'] = $option;
  786. $_SESSION['task'] = $task;
  787. }
  788. }
  789. } else if ($session_id == '') {
  790. // no session_id as user has not attempted to login, or session.auto_start is switched on
  791. if (ini_get( 'session.auto_start' ) || !ini_get( 'session.use_cookies' )) {
  792. echo "<script>document.location.href='index.php?mosmsg=You need to login. If PHP\'s session.auto_start setting is on or session.use_cookies setting is off, you may need to correct this before you will be able to login.'</script>\n";
  793. } else {
  794. echo "<script>document.location.href='index.php?mosmsg=You need to login'</script>\n";
  795. }
  796. exit();
  797. } else {
  798. // session id does not correspond to required session format
  799. echo "<script>document.location.href='index.php?mosmsg=Invalid Session'</script>\n";
  800. exit();
  801. }
  802. return $my;
  803. }
  804. /*
  805. * Function used to set Session Garbage Cleaning
  806. * garbage cleaning set at configured session time + 600 seconds
  807. * Added as of 1.0.8
  808. * Deperciated 1.1
  809. */
  810. function setSessionGarbageClean() {
  811. /** ensure that funciton is only called once */
  812. if (!defined( '_JOS_GARBAGECLEAN' )) {
  813. define( '_JOS_GARBAGECLEAN', 1 );
  814. $garbage_timeout = $this->getCfg('session_life_admin') + 600;
  815. @ini_set('session.gc_maxlifetime', $garbage_timeout);
  816. }
  817. }
  818. /*
  819. * Static Function used to generate the Session Cookie Name
  820. * Added as of 1.0.8
  821. * Deperciated 1.1
  822. */
  823. function sessionCookieName() {
  824. global $mainframe, $mosConfig_live_site;
  825. if( substr( $mosConfig_live_site, 0, 7 ) == 'http://' ) {
  826. $hash = md5( 'site' . substr( $mosConfig_live_site, 7 ) );
  827. } elseif( substr( $mosConfig_live_site, 0, 8 ) == 'https://' ) {
  828. $hash = md5( 'site' . substr( $mosConfig_live_site, 8 ) );
  829. } else {
  830. $hash = md5( 'site' . $mainframe->getCfg( 'live_site' ) );
  831. }
  832. return $hash;
  833. }
  834. /*
  835. * Static Function used to generate the Session Cookie Value
  836. * Added as of 1.0.8
  837. * Deperciated 1.1
  838. */
  839. function sessionCookieValue( $id=null ) {
  840. global $mainframe;
  841. $type = $mainframe->getCfg( 'session_type' );
  842. $browser = @$_SERVER['HTTP_USER_AGENT'];
  843. switch ($type) {
  844. case 2:
  845. // 1.0.0 to 1.0.7 Compatibility
  846. // lowest level security
  847. $value = md5( $id . $_SERVER['REMOTE_ADDR'] );
  848. break;
  849. case 1:
  850. // slightly reduced security - 3rd level IP authentication for those behind IP Proxy
  851. $remote_addr = explode('.',$_SERVER['REMOTE_ADDR']);
  852. $ip = $remote_addr[0] .'.'. $remote_addr[1] .'.'. $remote_addr[2];
  853. $value = mosHash( $id . $ip . $browser );
  854. break;
  855. default:
  856. // Highest security level - new default for 1.0.8 and beyond
  857. $ip = $_SERVER['REMOTE_ADDR'];
  858. $value = mosHash( $id . $ip . $browser );
  859. break;
  860. }
  861. return $value;
  862. }
  863. /*
  864. * Static Function used to generate the Rememeber Me Cookie Name for Username information
  865. * Added as of 1.0.8
  866. * Depreciated 1.1
  867. */
  868. function remCookieName_User() {
  869. $value = mosHash( 'remembermecookieusername'. mosMainFrame::sessionCookieName() );
  870. return $value;
  871. }
  872. /*
  873. * Static Function used to generate the Rememeber Me Cookie Name for Password information
  874. * Added as of 1.0.8
  875. * Depreciated 1.1
  876. */
  877. function remCookieName_Pass() {
  878. $value = mosHash( 'remembermecookiepassword'. mosMainFrame::sessionCookieName() );
  879. return $value;
  880. }
  881. /*
  882. * Static Function used to generate the Remember Me Cookie Value for Username information
  883. * Added as of 1.0.8
  884. * Depreciated 1.1
  885. */
  886. function remCookieValue_User( $username ) {
  887. $value = md5( $username . mosHash( @$_SERVER['HTTP_USER_AGENT'] ) );
  888. return $value;
  889. }
  890. /*
  891. * Static Function used to generate the Remember Me Cookie Value for Password information
  892. * Added as of 1.0.8
  893. * Depreciated 1.1
  894. */
  895. function remCookieValue_Pass( $passwd ) {
  896. $value = md5( $passwd . mosHash( @$_SERVER['HTTP_USER_AGENT'] ) );
  897. return $value;
  898. }
  899. /**
  900. * Login validation function
  901. *
  902. * Username and encoded password is compare to db entries in the jos_users
  903. * table. A successful validation updates the current session record with
  904. * the users details.
  905. */
  906. function login( $username=null,$passwd=null, $remember=0, $userid=NULL ) {
  907. global $acl, $_VERSION;
  908. $bypost = 0;
  909. $valid_remember = false;
  910. // if no username and password passed from function, then function is being called from login module/component
  911. if (!$username || !$passwd) {
  912. $username = stripslashes( strval( mosGetParam( $_POST, 'username', '' ) ) );
  913. $passwd = stripslashes( strval( mosGetParam( $_POST, 'passwd', '' ) ) );
  914. $bypost = 1;
  915. // extra check to ensure that Joomla! sessioncookie exists
  916. if (!$this->_session->session_id) {
  917. mosErrorAlert( _ALERT_ENABLED );
  918. return;
  919. }
  920. josSpoofCheck(NULL,1);
  921. }
  922. $row = null;
  923. if (!$username || !$passwd) {
  924. mosErrorAlert( _LOGIN_INCOMPLETE );
  925. exit();
  926. } else {
  927. if ( $remember && strlen($username) == 32 && $userid ) {
  928. // query used for remember me cookie
  929. $harden = mosHash( @$_SERVER['HTTP_USER_AGENT'] );
  930. $query = "SELECT id, name, username, password, usertype, block, gid"
  931. . "\n FROM #__users"
  932. . "\n WHERE id = " . (int) $userid
  933. ;
  934. $this->_db->setQuery( $query );
  935. $this->_db->loadObject($user);
  936. list($hash, $salt) = explode(':', $user->password);
  937. $check_username = md5( $user->username . $harden );
  938. $check_password = md5( $hash . $harden );
  939. if ( $check_username == $username && $check_password == $passwd ) {
  940. $row = $user;
  941. $valid_remember = true;
  942. }
  943. } else {
  944. // query used for login via login module
  945. $query = "SELECT id, name, username, password, usertype, block, gid"
  946. . "\n FROM #__users"
  947. . "\n WHERE username = ". $this->_db->Quote( $username )
  948. ;
  949. $this->_db->setQuery( $query );
  950. $this->_db->loadObject( $row );
  951. }
  952. if (is_object($row)) {
  953. // user blocked from login
  954. if ($row->block == 1) {
  955. mosErrorAlert(_LOGIN_BLOCKED);
  956. }
  957. if (!$valid_remember) {
  958. // Conversion to new type
  959. if ((strpos($row->password, ':') === false) && $row->password == md5($passwd)) {
  960. // Old password hash storage but authentic ... lets convert it
  961. $salt = mosMakePassword(16);
  962. $crypt = md5($passwd.$salt);
  963. $row->password = $crypt.':'.$salt;
  964. // Now lets store it in the database
  965. $query = 'UPDATE #__users'
  966. . ' SET password = '.$this->_db->Quote($row->password)
  967. . ' WHERE id = '.(int)$row->id;
  968. $this->_db->setQuery($query);
  969. if (!$this->_db->query()) {
  970. // This is an error but not sure what to do with it ... we'll still work for now
  971. }
  972. }
  973. list($hash, $salt) = explode(':', $row->password);
  974. $cryptpass = md5($passwd.$salt);
  975. if ($hash != $cryptpass) {
  976. if ( $bypost ) {
  977. mosErrorAlert(_LOGIN_INCORRECT);
  978. } else {
  979. $this->logout();
  980. mosRedirect('index.php');
  981. }
  982. exit();
  983. }
  984. }
  985. // fudge the group stuff
  986. $grp = $acl->getAroGroup( $row->id );
  987. $row->gid = 1;
  988. if ($acl->is_group_child_of( $grp->name, 'Registered', 'ARO' ) || $acl->is_group_child_of( $grp->name, 'Public Backend', 'ARO' )) {
  989. // fudge Authors, Editors, Publishers and Super Administrators into the Special Group
  990. $row->gid = 2;
  991. }
  992. $row->usertype = $grp->name;
  993. // initialize session data
  994. $session =& $this->_session;
  995. $session->guest = 0;
  996. $session->username = $row->username;
  997. $session->userid = intval( $row->id );
  998. $session->usertype = $row->usertype;
  999. $session->gid = intval( $row->gid );
  1000. $session->update();
  1001. // check to see if site is a production site
  1002. // allows multiple logins with same user for a demo site
  1003. if ( $_VERSION->SITE ) {
  1004. // delete any old front sessions to stop duplicate sessions
  1005. $query = "DELETE FROM #__session"
  1006. . "\n WHERE session_id != ". $this->_db->Quote( $session->session_id )
  1007. . "\n AND username = ". $this->_db->Quote( $row->username )
  1008. . "\n AND userid = " . (int) $row->id
  1009. . "\n AND gid = " . (int) $row->gid
  1010. . "\n AND guest = 0"
  1011. ;
  1012. $this->_db->setQuery( $query );
  1013. $this->_db->query();
  1014. }
  1015. // update user visit data
  1016. $currentDate = date("Y-m-d\TH:i:s");
  1017. $query = "UPDATE #__users"
  1018. . "\n SET lastvisitDate = ". $this->_db->Quote( $currentDate )
  1019. . "\n WHERE id = " . (int) $session->userid
  1020. ;
  1021. $this->_db->setQuery($query);
  1022. if (!$this->_db->query()) {
  1023. die($this->_db->stderr(true));
  1024. }
  1025. // set remember me cookie if selected
  1026. $remember = strval( mosGetParam( $_POST, 'remember', '' ) );
  1027. if ( $remember == 'yes' ) {
  1028. // cookie lifetime of 365 days
  1029. $lifetime = time() + 365*24*60*60;
  1030. $remCookieName = mosMainFrame::remCookieName_User();
  1031. $remCookieValue = mosMainFrame::remCookieValue_User( $row->username ) . mosMainFrame::remCookieValue_Pass( $hash ) . $row->id;
  1032. setcookie( $remCookieName, $remCookieValue, $lifetime, '/' );
  1033. }
  1034. mosCache::cleanCache();
  1035. } else {
  1036. if ( $bypost ) {
  1037. mosErrorAlert(_LOGIN_INCORRECT);
  1038. } else {
  1039. $this->logout();
  1040. mosRedirect('index.php');
  1041. }
  1042. exit();
  1043. }
  1044. }
  1045. }
  1046. /**
  1047. * User logout
  1048. *
  1049. * Reverts the current session record back to 'anonymous' parameters
  1050. */
  1051. function logout() {
  1052. mosCache::cleanCache();
  1053. $session =& $this->_session;
  1054. $session->guest = 1;
  1055. $session->username = '';
  1056. $session->userid = '';
  1057. $session->usertype = '';
  1058. $session->gid = 0;
  1059. $session->update();
  1060. // kill remember me cookie
  1061. $lifetime = time() - 86400;
  1062. $remCookieName = mosMainFrame::remCookieName_User();
  1063. setcookie( $remCookieName, ' ', $lifetime, '/' );
  1064. @session_destroy();
  1065. }
  1066. /**
  1067. * @return mosUser A user object with the information from the current session
  1068. */
  1069. function getUser() {
  1070. global $database;
  1071. $user = new mosUser( $this->_db );
  1072. $user->id = intval( $this->_session->userid );
  1073. $user->username = $this->_session->username;
  1074. $user->usertype = $this->_session->usertype;
  1075. $user->gid = intval( $this->_session->gid );
  1076. if ($user->id) {
  1077. $query = "SELECT id, name, email, block, sendEmail, registerDate, lastvisitDate, activation, params"
  1078. . "\n FROM #__users"
  1079. . "\n WHERE id = " . (int) $user->id
  1080. ;
  1081. $database->setQuery( $query );
  1082. $database->loadObject( $my );
  1083. $user->params = $my->params;
  1084. $user->name = $my->name;
  1085. $user->email = $my->email;
  1086. $user->block = $my->block;
  1087. $user->sendEmail = $my->sendEmail;
  1088. $user->registerDate = $my->registerDate;
  1089. $user->lastvisitDate = $my->lastvisitDate;
  1090. $user->activation = $my->activation;
  1091. }
  1092. return $user;
  1093. }
  1094. /**
  1095. * @param string The name of the variable (from configuration.php)
  1096. * @return mixed The value of the configuration variable or null if not found
  1097. */
  1098. function getCfg( $varname ) {
  1099. $varname = 'mosConfig_' . $varname;
  1100. if (isset( $GLOBALS[$varname] )) {
  1101. return $GLOBALS[$varname];
  1102. } else {
  1103. return null;
  1104. }
  1105. }
  1106. function _setTemplate( $isAdmin=false ) {
  1107. global $Itemid;
  1108. $mosConfig_absolute_path = $this->getCfg( 'absolute_path' );
  1109. if ($isAdmin) {
  1110. $query = "SELECT template"
  1111. . "\n FROM #__templates_menu"
  1112. . "\n WHERE client_id = 1"
  1113. . "\n AND menuid = 0"
  1114. ;
  1115. $this->_db->setQuery( $query );
  1116. $cur_template = $this->_db->loadResult();
  1117. $path = "$mosConfig_absolute_path/administrator/templates/$cur_template/index.php";
  1118. if (!file_exists( $path )) {
  1119. $cur_template = 'joomla_admin';
  1120. }
  1121. } else {
  1122. $assigned = ( !empty( $Itemid ) ? " OR menuid = " . (int) $Itemid : '' );
  1123. $query = "SELECT template"
  1124. . "\n FROM #__templates_menu"
  1125. . "\n WHERE client_id = 0"
  1126. . "\n AND ( menuid = 0 $assigned )"
  1127. . "\n ORDER BY menuid DESC"
  1128. ;
  1129. $this->_db->setQuery( $query, 0, 1 );
  1130. $cur_template = $this->_db->loadResult();
  1131. // TemplateChooser Start
  1132. $jos_user_template = strval( mosGetParam( $_COOKIE, 'jos_user_template', '' ) );
  1133. $jos_change_template = strval( mosGetParam( $_REQUEST, 'jos_change_template', $jos_user_template ) );
  1134. if ($jos_change_template) {
  1135. // clean template name
  1136. $jos_change_template = preg_replace( '#\W#', '', $jos_change_template );
  1137. if ( strlen( $jos_change_template ) >= 40 ) {
  1138. $jos_change_template = substr($jos_change_template, 0 , 39);
  1139. }
  1140. // check that template exists in case it was deleted
  1141. if (file_exists( $mosConfig_absolute_path .'/templates/'. $jos_change_template .'/index.php' )) {
  1142. $lifetime = 60*10;
  1143. $cur_template = $jos_change_template;
  1144. setcookie( 'jos_user_template', "$jos_change_template", time()+$lifetime);
  1145. } else {
  1146. setcookie( 'jos_user_template', '', time()-3600 );
  1147. }
  1148. }
  1149. // TemplateChooser End
  1150. }
  1151. $this->_template = $cur_template;
  1152. }
  1153. function getTemplate() {
  1154. return $this->_template;
  1155. }
  1156. /**
  1157. * Determines the paths for including engine and menu files
  1158. * @param string The current option used in the url
  1159. * @param string The base path from which to load the configuration file
  1160. */
  1161. function _setAdminPaths( $option, $basePath='.' ) {
  1162. $option = strtolower( $option );
  1163. $this->_path = new stdClass();
  1164. // security check to disable use of `/`, `\\` and `:` in $options variable
  1165. if (strpos($option, '/') !== false || strpos($option, '\\') !== false || strpos($option, ':') !== false) {
  1166. mosErrorAlert( 'Restricted access' );
  1167. return;
  1168. }
  1169. $prefix = substr( $option, 0, 4 );
  1170. if ($prefix != 'com_' && $prefix != 'mod_') {
  1171. // ensure backward compatibility with existing links
  1172. $name = $option;
  1173. $option = "com_$option";
  1174. } else {
  1175. $name = substr( $option, 4 );
  1176. }
  1177. // components
  1178. if (file_exists( "$basePath/templates/$this->_template/components/$name.html.php" )) {
  1179. $this->_path->front = "$basePath/components/$option/$name.php";
  1180. $this->_path->front_html = "$basePath/templates/$this->_template/components/$name.html.php";
  1181. } else if (file_exists( "$basePath/components/$option/$name.php" )) {
  1182. $this->_path->front = "$basePath/components/$option/$name.php";
  1183. $this->_path->front_html = "$basePath/components/$option/$name.html.php";
  1184. }
  1185. if (file_exists( "$basePath/administrator/components/$option/admin.$name.php" )) {
  1186. $this->_path->admin = "$basePath/administrator/components/$option/admin.$name.php";
  1187. $this->_path->admin_html = "$basePath/administrator/components/$option/admin.$name.html.php";
  1188. }
  1189. if (file_exists( "$basePath/administrator/components/$option/toolbar.$name.php" )) {
  1190. $this->_path->toolbar = "$basePath/administrator/components/$option/toolbar.$name.php";
  1191. $this->_path->toolbar_html = "$basePath/administrator/components/$option/toolbar.$name.html.php";
  1192. $this->_path->toolbar_default = "$basePath/administrator/includes/toolbar.html.php";
  1193. }
  1194. if (file_exists( "$basePath/components/$option/$name.class.php" )) {
  1195. $this->_path->class = "$basePath/components/$option/$name.class.php";
  1196. } else if (file_exists( "$basePath/administrator/components/$option/$name.class.php" )) {
  1197. $this->_path->class = "$basePath/administrator/components/$option/$name.class.php";
  1198. } else if (file_exists( "$basePath/includes/$name.php" )) {
  1199. $this->_path->class = "$basePath/includes/$name.php";
  1200. }
  1201. if ($prefix == 'mod_' && file_exists("$basePath/administrator/modules/$option.php")) {
  1202. $this->_path->admin = "$basePath/administrator/modules/$option.php";
  1203. $this->_path->admin_html = "$basePath/administrator/modules/mod_$name.html.php";
  1204. } else if (file_exists("$basePath/administrator/components/$option/admin.$name.php" )) {
  1205. $this->_path->admin = "$basePath/administrator/components/$option/admin.$name.php";
  1206. $this->_path->admin_html = "$basePath/administrator/components/$option/admin.$name.html.php";
  1207. } else {
  1208. $this->_path->admin = "$basePath/administrator/components/com_admin/admin.admin.php";
  1209. $this->_path->admin_html = "$basePath/administrator/components/com_admin/admin.admin.html.php";
  1210. }
  1211. }
  1212. /**
  1213. * Returns a stored path variable
  1214. *
  1215. */
  1216. function getPath( $varname, $option='' ) {
  1217. global $mosConfig_absolute_path;
  1218. if ($option) {
  1219. $temp = $this->_path;
  1220. $this->_setAdminPaths( $option, $this->getCfg( 'absolute_path' ) );
  1221. }
  1222. $result = null;
  1223. if (isset( $this->_path->$varname )) {
  1224. $result = $this->_path->$varname;
  1225. } else {
  1226. switch ($varname) {
  1227. case 'com_xml':
  1228. $name = substr( $option, 4 );
  1229. $path = "$mosConfig_absolute_path/administrator/components/$option/$name.xml";
  1230. if (file_exists( $path )) {
  1231. $result = $path;
  1232. } else {
  1233. $path = "$mosConfig_absolute_path/components/$option/$name.xml";
  1234. if (file_exists( $path )) {
  1235. $result = $path;
  1236. }
  1237. }
  1238. break;
  1239. case 'mod0_xml':
  1240. // Site modules
  1241. if ($option == '') {
  1242. $path = $mosConfig_absolute_path . "/modules/custom.xml";
  1243. } else {
  1244. $path = $mosConfig_absolute_path . "/modules/$option.xml";
  1245. }
  1246. if (file_exists( $path )) {
  1247. $result = $path;
  1248. }
  1249. break;
  1250. case 'mod1_xml':
  1251. // admin modules
  1252. if ($option == '') {
  1253. $path = $mosConfig_absolute_path . '/administrator/modules/custom.xml';
  1254. } else {
  1255. $path = $mosConfig_absolute_path . "/administrator/modules/$option.xml";
  1256. }
  1257. if (file_exists( $path )) {
  1258. $result = $path;
  1259. }
  1260. break;
  1261. case 'bot_xml':
  1262. // Site mambots
  1263. $path = $mosConfig_absolute_path . "/mambots/$option.xml";
  1264. if (file_exists( $path )) {
  1265. $result = $path;
  1266. }
  1267. break;
  1268. case 'menu_xml':
  1269. $path = $mosConfig_absolute_path . "/administrator/components/com_menus/$option/$option.xml";
  1270. if (file_exists( $path )) {
  1271. $result = $path;
  1272. }
  1273. break;
  1274. case 'installer_html':
  1275. $path = $mosConfig_absolute_path . "/administrator/components/com_installer/$option/$option.html.php";
  1276. if (file_exists( $path )) {
  1277. $result = $path;
  1278. }
  1279. break;
  1280. case 'installer_class':
  1281. $path = $mosConfig_absolute_path . "/administrator/components/com_installer/$option/$option.class.php";
  1282. if (file_exists( $path )) {
  1283. $result = $path;
  1284. }
  1285. break;
  1286. }
  1287. }
  1288. if ($option) {
  1289. $this->_path = $temp;
  1290. }
  1291. return $result;
  1292. }
  1293. /**
  1294. * Detects a 'visit'
  1295. *
  1296. * This function updates the agent and domain table hits for a particular
  1297. * visitor. The user agent is recorded/incremented if this is the first visit.
  1298. * A cookie is set to mark the first visit.
  1299. */
  1300. function detect() {
  1301. global $mosConfig_enable_stats;
  1302. if ($mosConfig_enable_stats == 1) {
  1303. if (mosGetParam( $_COOKIE, 'mosvisitor', 0 )) {
  1304. return;
  1305. }
  1306. setcookie( 'mosvisitor', 1 );
  1307. if (phpversion() <= '4.2.1') {
  1308. $agent = getenv( 'HTTP_USER_AGENT' );
  1309. $domain = @gethostbyaddr( getenv( "REMOTE_ADDR" ) );
  1310. } else {
  1311. if ( isset($_SERVER['HTTP_USER_AGENT']) ) {
  1312. $agent = $_SERVER['HTTP_USER_AGENT'];
  1313. } else {
  1314. $agent = 'Unknown';
  1315. }
  1316. $domain = @gethostbyaddr( $_SERVER['REMOTE_ADDR'] );
  1317. }
  1318. $browser = mosGetBrowser( $agent );
  1319. $query = "SELECT COUNT(*)"
  1320. . "\n FROM #__stats_agents"
  1321. . "\n WHERE agent = " . $this->_db->Quote( $browser )
  1322. . "\n AND type = 0"
  1323. ;
  1324. $this->_db->setQuery( $query );
  1325. if ($this->_db->loadResult()) {
  1326. $query = "UPDATE #__stats_agents"
  1327. . "\n SET hits = ( hits + 1 )"
  1328. . "\n WHERE agent = " . $this->_db->Quote( $browser )
  1329. . "\n AND type = 0"
  1330. ;
  1331. $this->_db->setQuery( $query );
  1332. } else {
  1333. $query = "INSERT INTO #__stats_agents"
  1334. . "\n ( agent, type )"
  1335. . "\n VALUES ( " . $this->_db->Quote( $browser ) . ", 0 )"
  1336. ;
  1337. $this->_db->setQuery( $query );
  1338. }
  1339. $this->_db->query();
  1340. $os = mosGetOS( $agent );
  1341. $query = "SELECT COUNT(*)"
  1342. . "\n FROM #__stats_agents"
  1343. . "\n WHERE agent = " . $this->_db->Quote( $os )
  1344. . "\n AND type = 1"
  1345. ;
  1346. $this->_db->setQuery( $query );
  1347. if ($this->_db->loadResult()) {
  1348. $query = "UPDATE #__stats_agents"
  1349. . "\n SET hits = ( hits + 1 )"
  1350. . "\n WHERE agent = " . $this->_db->Quote( $os )
  1351. . "\n AND type = 1"
  1352. ;
  1353. $this->_db->setQuery( $query );
  1354. } else {
  1355. $query = "INSERT INTO #__stats_agents"
  1356. . "\n ( agent, type )"
  1357. . "\n VALUES ( " . $this->_db->Quote( $os ) . ", 1 )"
  1358. ;
  1359. $this->_db->setQuery( $query );
  1360. }
  1361. $this->_db->query();
  1362. // tease out the last element of the domain
  1363. $tldomain = split( "\.", $domain );
  1364. $tldomain = $tldomain[count( $tldomain )-1];
  1365. if (is_numeric( $tldomain )) {
  1366. $tldomain = "Unknown";
  1367. }
  1368. $query = "SELECT COUNT(*)"
  1369. . "\n FROM #__stats_agents"
  1370. . "\n WHERE agent = " . $this->_db->Quote( $tldomain )
  1371. . "\n AND type = 2"
  1372. ;
  1373. $this->_db->setQuery( $query );
  1374. if ($this->_db->loadResult()) {
  1375. $query = "UPDATE #__stats_agents"
  1376. . "\n SET hits = ( hits + 1 )"
  1377. . "\n WHERE agent = " . $this->_db->Quote( $tldomain )
  1378. . "\n AND type = 2"
  1379. ;
  1380. $this->_db->setQuery( $query );
  1381. } else {
  1382. $query = "INSERT INTO #__stats_agents"
  1383. . "\n ( agent, type )"
  1384. . "\n VALUES ( " . $this->_db->Quote( $tldomain ) . ", 2 )"
  1385. ;
  1386. $this->_db->setQuery( $query );
  1387. }
  1388. $this->_db->query();
  1389. }
  1390. }
  1391. /**
  1392. * @return correct Itemid for Content Item
  1393. */
  1394. function getItemid( $id, $typed=1, $link=1, $bs=1, $bc=1, $gbs=1 ) {
  1395. global $Itemid;
  1396. // getItemid compatibility mode, holds maintenance version number
  1397. $compat = (int) $this->getCfg('itemid_compat');
  1398. $compat = ($compat == 0)? 12 : $compat;
  1399. $_Itemid = '';
  1400. if ($_Itemid == '' && $typed && $this->getStaticContentCount()) {
  1401. $exists = 0;
  1402. foreach( $this->get( '_ContentTyped', array() ) as $key => $value ) {
  1403. // check if id has been tested before, if it is pull from class variable store
  1404. if ( $key == $id ) {
  1405. $_Itemid = $value;
  1406. $exists = 1;
  1407. break;
  1408. }
  1409. }
  1410. // if id hasnt been checked before initaite query
  1411. if ( !$exists ) {
  1412. // Search for typed link
  1413. $query = "SELECT id"
  1414. . "\n FROM #__menu"
  1415. . "\n WHERE type = 'content_typed'"
  1416. . "\n AND published = 1"
  1417. . "\n AND link = 'index.php?option=com_content&task=view&id=" . (int) $id . "'"
  1418. ;
  1419. $this->_db->setQuery( $query );
  1420. // pull existing query storage into temp variable
  1421. $ContentTyped = $this->get( '_ContentTyped', array() );
  1422. // add query result to temp array storage
  1423. $ContentTyped[$id] = $this->_db->loadResult();
  1424. // save temp array to main array storage
  1425. $this->set( '_ContentTyped', $ContentTyped );
  1426. $_Itemid = $ContentTyped[$id];
  1427. }
  1428. }
  1429. if ($_Itemid == '' && $link && $this->getContentItemLinkCount()) {
  1430. $exists = 0;
  1431. foreach( $this->get( '_ContentItemLink', array() ) as $key => $value ) {
  1432. // check if id has been tested before, if it is pull from class variable store
  1433. if ( $key == $id ) {
  1434. $_Itemid = $value;
  1435. $exists = 1;
  1436. break;
  1437. }
  1438. }
  1439. // if id hasnt been checked before initaite query
  1440. if ( !$exists ) {
  1441. // Search for item link
  1442. $query = "SELECT id"
  1443. ."\n FROM #__menu"
  1444. ."\n WHERE type = 'content_item_link'"
  1445. . "\n AND published = 1"
  1446. . "\n AND link = 'index.php?option=com_content&task=view&id=" . (int) $id . "'"
  1447. ;
  1448. $this->_db->setQuery( $query );
  1449. // pull existing query storage into temp variable
  1450. $ContentItemLink = $this->get( '_ContentItemLink', array() );
  1451. // add query result to temp array storage
  1452. $ContentItemLink[$id] = $this->_db->loadResult();
  1453. // save temp array to main array storage
  1454. $this->set( '_ContentItemLink', $ContentItemLink );
  1455. $_Itemid = $ContentItemLink[$id];
  1456. }
  1457. }
  1458. if ($_Itemid == '') {
  1459. $exists = 0;
  1460. foreach( $this->get( '_ContentSection', array() ) as $key => $value ) {
  1461. // check if id has been tested before, if it is pull from class variable store
  1462. if ( $key == $id ) {
  1463. $_Itemid = $value;
  1464. $exists = 1;
  1465. break;
  1466. }
  1467. }
  1468. // if id hasnt been checked before initaite query
  1469. if ( !$exists ) {
  1470. $query = "SELECT ms.id AS sid, ms.type AS stype, mc.id AS cid, mc.type AS ctype, i.id as sectionid, i.id As catid, ms.published AS spub, mc.published AS cpub"
  1471. . "\n FROM #__content AS i"
  1472. . "\n LEFT JOIN #__sections AS s ON i.sectionid = s.id"
  1473. . "\n LEFT JOIN #__menu AS ms ON ms.componentid = s.id "
  1474. . "\n LEFT JOIN #__categories AS c ON i.catid = c.id"
  1475. . "\n LEFT JOIN #__menu AS mc ON mc.componentid = c.id "
  1476. . "\n W…

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