PageRenderTime 29ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Utils/System/Drupal.php

https://github.com/michaelmcandrew/ste
PHP | 423 lines | 244 code | 36 blank | 143 comment | 39 complexity | ae696357ab405152b9a01a3e9d72100b MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. *
  29. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2011
  31. * $Id$
  32. *
  33. */
  34. /**
  35. * Drupal specific stuff goes here
  36. */
  37. class CRM_Utils_System_Drupal {
  38. /**
  39. * sets the title of the page
  40. *
  41. * @param string $title
  42. * @paqram string $pageTitle
  43. *
  44. * @return void
  45. * @access public
  46. */
  47. function setTitle( $title, $pageTitle = null ) {
  48. if ( !$pageTitle ) {
  49. $pageTitle = $title;
  50. }
  51. if ( arg(0) == 'civicrm' ) {
  52. //set drupal title
  53. drupal_set_title( $pageTitle );
  54. }
  55. }
  56. /**
  57. * Append an additional breadcrumb tag to the existing breadcrumb
  58. *
  59. * @param string $title
  60. * @param string $url
  61. *
  62. * @return void
  63. * @access public
  64. * @static
  65. */
  66. static function appendBreadCrumb( $breadCrumbs ) {
  67. $breadCrumb = drupal_get_breadcrumb( );
  68. if ( is_array( $breadCrumbs ) ) {
  69. foreach ( $breadCrumbs as $crumbs ) {
  70. if ( stripos($crumbs['url'], 'id%%') ) {
  71. $args = array( 'cid', 'mid' );
  72. foreach ( $args as $a ) {
  73. $val = CRM_Utils_Request::retrieve( $a, 'Positive', CRM_Core_DAO::$_nullObject,
  74. false, null, $_GET );
  75. if ( $val ) {
  76. $crumbs['url'] = str_ireplace( "%%{$a}%%", $val, $crumbs['url'] );
  77. }
  78. }
  79. }
  80. $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
  81. }
  82. }
  83. drupal_set_breadcrumb( $breadCrumb );
  84. }
  85. /**
  86. * Reset an additional breadcrumb tag to the existing breadcrumb
  87. *
  88. * @return void
  89. * @access public
  90. * @static
  91. */
  92. static function resetBreadCrumb( ) {
  93. $bc = array( );
  94. drupal_set_breadcrumb( $bc );
  95. }
  96. /**
  97. * Append a string to the head of the html file
  98. *
  99. * @param string $head the new string to be appended
  100. *
  101. * @return void
  102. * @access public
  103. * @static
  104. */
  105. static function addHTMLHead( $head ) {
  106. drupal_set_html_head( $head );
  107. }
  108. /**
  109. * rewrite various system urls to https
  110. *
  111. * @param null
  112. *
  113. * @return void
  114. * @access public
  115. * @static
  116. */
  117. static function mapConfigToSSL( ) {
  118. global $base_url;
  119. $base_url = str_replace( 'http://', 'https://', $base_url );
  120. }
  121. /**
  122. * figure out the post url for the form
  123. *
  124. * @param mix $action the default action if one is pre-specified
  125. *
  126. * @return string the url to post the form
  127. * @access public
  128. * @static
  129. */
  130. static function postURL( $action ) {
  131. if ( ! empty( $action ) ) {
  132. return $action;
  133. }
  134. return self::url( $_GET['q'] );
  135. }
  136. /**
  137. * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
  138. *
  139. * @param $path string The path being linked to, such as "civicrm/add"
  140. * @param $query string A query string to append to the link.
  141. * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:).
  142. * Useful for links that will be displayed outside the site, such as in an
  143. * RSS feed.
  144. * @param $fragment string A fragment identifier (named anchor) to append to the link.
  145. * @param $htmlize boolean whether to convert to html eqivalant
  146. * @param $frontend boolean a gross joomla hack
  147. *
  148. * @return string an HTML string containing a link to the given path.
  149. * @access public
  150. *
  151. */
  152. function url($path = null, $query = null, $absolute = false,
  153. $fragment = null, $htmlize = true,
  154. $frontend = false ) {
  155. $config = CRM_Core_Config::singleton( );
  156. $script = 'index.php';
  157. require_once 'CRM/Utils/String.php';
  158. $path = CRM_Utils_String::stripPathChars( $path );
  159. if (isset($fragment)) {
  160. $fragment = '#'. $fragment;
  161. }
  162. if ( ! isset( $config->useFrameworkRelativeBase ) ) {
  163. $base = parse_url( $config->userFrameworkBaseURL );
  164. $config->useFrameworkRelativeBase = $base['path'];
  165. }
  166. $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
  167. $separator = $htmlize ? '&amp;' : '&';
  168. if (! $config->cleanURL ) {
  169. if ( isset( $path ) ) {
  170. if ( isset( $query ) ) {
  171. return $base . $script .'?q=' . $path . $separator . $query . $fragment;
  172. } else {
  173. return $base . $script .'?q=' . $path . $fragment;
  174. }
  175. } else {
  176. if ( isset( $query ) ) {
  177. return $base . $script .'?'. $query . $fragment;
  178. } else {
  179. return $base . $fragment;
  180. }
  181. }
  182. } else {
  183. if ( isset( $path ) ) {
  184. if ( isset( $query ) ) {
  185. return $base . $path .'?'. $query . $fragment;
  186. } else {
  187. return $base . $path . $fragment;
  188. }
  189. } else {
  190. if ( isset( $query ) ) {
  191. return $base . $script .'?'. $query . $fragment;
  192. } else {
  193. return $base . $fragment;
  194. }
  195. }
  196. }
  197. }
  198. /**
  199. * Authenticate the user against the drupal db
  200. *
  201. * @param string $name the user name
  202. * @param string $password the password for the above user name
  203. *
  204. * @return mixed false if no auth
  205. * array( contactID, ufID, unique string ) if success
  206. * @access public
  207. * @static
  208. */
  209. static function authenticate( $name, $password ) {
  210. require_once 'DB.php';
  211. $config = CRM_Core_Config::singleton( );
  212. $dbDrupal = DB::connect( $config->userFrameworkDSN );
  213. if ( DB::isError( $dbDrupal ) ) {
  214. CRM_Core_Error::fatal( "Cannot connect to drupal db via $config->userFrameworkDSN, " . $dbDrupal->getMessage( ) );
  215. }
  216. $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
  217. $password = md5( $password );
  218. $name = $dbDrupal->escapeSimple( $strtolower( $name ) );
  219. $sql = 'SELECT u.* FROM ' . $config->userFrameworkUsersTableName .
  220. " u WHERE LOWER(u.name) = '$name' AND u.pass = '$password' AND u.status = 1";
  221. $query = $dbDrupal->query( $sql );
  222. $user = null;
  223. // need to change this to make sure we matched only one row
  224. require_once 'CRM/Core/BAO/UFMatch.php';
  225. while ( $row = $query->fetchRow( DB_FETCHMODE_ASSOC ) ) {
  226. CRM_Core_BAO_UFMatch::synchronizeUFMatch( $user, $row['uid'], $row['mail'], 'Drupal' );
  227. $contactID = CRM_Core_BAO_UFMatch::getContactId( $row['uid'] );
  228. if ( ! $contactID ) {
  229. return false;
  230. }
  231. return array( $contactID, $row['uid'], mt_rand() );
  232. }
  233. return false;
  234. }
  235. /**
  236. * Set a message in the UF to display to a user
  237. *
  238. * @param string $message the message to set
  239. *
  240. * @access public
  241. * @static
  242. */
  243. static function setMessage( $message ) {
  244. drupal_set_message( $message );
  245. }
  246. static function permissionDenied( ) {
  247. drupal_access_denied( );
  248. }
  249. static function logout( ) {
  250. module_load_include( 'inc', 'user', 'user.pages' );
  251. return user_logout( );
  252. }
  253. static function updateCategories( ) {
  254. // copied this from profile.module. Seems a bit inefficient, but i dont know a better way
  255. // CRM-3600
  256. cache_clear_all();
  257. menu_rebuild();
  258. }
  259. /**
  260. * Get the locale set in the hosting CMS
  261. * @return string with the locale or null for none
  262. */
  263. static function getUFLocale()
  264. {
  265. // return CiviCRM’s xx_YY locale that either matches Drupal’s Chinese locale
  266. // (for CRM-6281), Drupal’s xx_YY or is retrieved based on Drupal’s xx
  267. global $language;
  268. switch (true) {
  269. case $language->language == 'zh-hans': return 'zh_CN';
  270. case $language->language == 'zh-hant': return 'zh_TW';
  271. case preg_match('/^.._..$/', $language->language): return $language->language;
  272. default:
  273. require_once 'CRM/Core/I18n/PseudoConstant.php';
  274. return CRM_Core_I18n_PseudoConstant::longForShort(substr($language->language, 0, 2));
  275. }
  276. }
  277. /**
  278. * load drupal bootstrap
  279. *
  280. * @param $name string optional username for login
  281. * @param $pass string optional password for login
  282. */
  283. static function loadBootStrap($name = null, $pass = null, $uid = null )
  284. {
  285. //take the cms root path.
  286. $cmsPath = self::cmsRootPath( );
  287. if ( !file_exists( "$cmsPath/includes/bootstrap.inc" ) ) {
  288. echo '<br />Sorry, could not able to locate bootstrap.inc.';
  289. exit( );
  290. }
  291. chdir($cmsPath);
  292. require_once 'includes/bootstrap.inc';
  293. @drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  294. if ( !function_exists('module_exists') ||
  295. !module_exists( 'civicrm' ) ) {
  296. echo '<br />Sorry, could not able to load drupal bootstrap.';
  297. exit( );
  298. }
  299. //load user, we need to check drupal permissions.
  300. $name = $name ? $name : trim(CRM_Utils_Array::value('name', $_REQUEST));
  301. $pass = $pass ? $pass : trim(CRM_Utils_Array::value('pass', $_REQUEST));
  302. if ( $name ) {
  303. $user = user_authenticate( array( 'name' => $name, 'pass' => $pass ) );
  304. if ( empty( $user->uid ) ) {
  305. echo '<br />Sorry, unrecognized username or password.';
  306. exit( );
  307. }
  308. } else if ( $uid ) {
  309. $account = user_load( array( 'uid' => $uid ) );
  310. if ( empty( $account->uid ) ) {
  311. echo '<br />Sorry, unrecognized user id.';
  312. exit( );
  313. } else {
  314. global $user;
  315. $user = $account;
  316. }
  317. }
  318. }
  319. static function cmsRootPath( )
  320. {
  321. $cmsRoot = $valid = null;
  322. $path = $_SERVER['SCRIPT_FILENAME'];
  323. if ( function_exists( 'drush_get_context' ) ) {
  324. // drush anyway takes care of multisite install etc
  325. return drush_get_context('DRUSH_DRUPAL_ROOT');
  326. }
  327. // CRM-7582
  328. $pathVars = explode( '/',
  329. str_replace('//', '/',
  330. str_replace( '\\', '/', $path ) ) );
  331. //lets store first var,
  332. //need to get back for windows.
  333. $firstVar = array_shift( $pathVars );
  334. //lets remove sript name to reduce one iteration.
  335. array_pop( $pathVars );
  336. //CRM-7429 --do check for upper most 'includes' dir,
  337. //which would effectually work for multisite installation.
  338. do {
  339. $cmsRoot = $firstVar . '/'.implode( '/', $pathVars );
  340. $cmsIncludePath = "$cmsRoot/includes";
  341. //stop as we found bootstrap.
  342. if ( @opendir( $cmsIncludePath ) &&
  343. file_exists( "$cmsIncludePath/bootstrap.inc" ) ) {
  344. $valid = true;
  345. break;
  346. }
  347. //remove one directory level.
  348. array_pop( $pathVars );
  349. } while ( count( $pathVars ) );
  350. return ( $valid ) ? $cmsRoot : null;
  351. }
  352. /**
  353. * check is user logged in.
  354. *
  355. * @return boolean true/false.
  356. */
  357. public static function isUserLoggedIn( ) {
  358. $isloggedIn = false;
  359. if ( function_exists( 'user_is_logged_in' ) ) {
  360. $isloggedIn = user_is_logged_in( );
  361. }
  362. return $isloggedIn;
  363. }
  364. /**
  365. * Get currently logged in user uf id.
  366. *
  367. * @return int $userID logged in user uf id.
  368. */
  369. public static function getLoggedInUfID( ) {
  370. $ufID = null;
  371. if ( function_exists( 'user_is_logged_in' ) &&
  372. user_is_logged_in( ) &&
  373. function_exists( 'user_uid_optional_to_arg' ) ) {
  374. $ufID = user_uid_optional_to_arg( array( ) );
  375. }
  376. return $ufID;
  377. }
  378. }