PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/CRM/Utils/System/Drupal.php

https://github.com/ksecor/civicrm
PHP | 289 lines | 131 code | 28 blank | 130 comment | 24 complexity | fb6645d6a2dcc425e1096178dca98c59 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. */
  33. /**
  34. * Drupal specific stuff goes here
  35. */
  36. class CRM_Utils_System_Drupal {
  37. /**
  38. * sets the title of the page
  39. *
  40. * @param string $title
  41. * @paqram string $pageTitle
  42. *
  43. * @return void
  44. * @access public
  45. */
  46. function setTitle( $title, $pageTitle = null ) {
  47. if ( $pageTitle ) {
  48. $title = $pageTitle;
  49. }
  50. drupal_set_title( $title );
  51. }
  52. /**
  53. * Append an additional breadcrumb tag to the existing breadcrumb
  54. *
  55. * @param string $title
  56. * @param string $url
  57. *
  58. * @return void
  59. * @access public
  60. * @static
  61. */
  62. static function appendBreadCrumb( $breadCrumbs ) {
  63. $breadCrumb = drupal_get_breadcrumb( );
  64. if ( is_array( $breadCrumbs ) ) {
  65. foreach ( $breadCrumbs as $crumbs ) {
  66. if ( stripos($crumbs['url'], 'id%%') ) {
  67. $args = array( 'cid', 'mid' );
  68. foreach ( $args as $a ) {
  69. $val = CRM_Utils_Request::retrieve( $a, 'Positive', CRM_Core_DAO::$_nullObject,
  70. false, null, $_GET );
  71. if ( $val ) {
  72. $crumbs['url'] = str_ireplace( "%%{$a}%%", $val, $crumbs['url'] );
  73. }
  74. }
  75. }
  76. $breadCrumb[] = "<a href=\"{$crumbs['url']}\">{$crumbs['title']}</a>";
  77. }
  78. }
  79. drupal_set_breadcrumb( $breadCrumb );
  80. }
  81. /**
  82. * Reset an additional breadcrumb tag to the existing breadcrumb
  83. *
  84. * @return void
  85. * @access public
  86. * @static
  87. */
  88. static function resetBreadCrumb( ) {
  89. $bc = array( );
  90. drupal_set_breadcrumb( $bc );
  91. }
  92. /**
  93. * Append a string to the head of the html file
  94. *
  95. * @param string $head the new string to be appended
  96. *
  97. * @return void
  98. * @access public
  99. * @static
  100. */
  101. static function addHTMLHead( $head ) {
  102. drupal_set_html_head( $head );
  103. }
  104. /**
  105. * rewrite various system urls to https
  106. *
  107. * @param null
  108. *
  109. * @return void
  110. * @access public
  111. * @static
  112. */
  113. static function mapConfigToSSL( ) {
  114. global $base_url;
  115. $base_url = str_replace( 'http://', 'https://', $base_url );
  116. }
  117. /**
  118. * figure out the post url for the form
  119. *
  120. * @param mix $action the default action if one is pre-specified
  121. *
  122. * @return string the url to post the form
  123. * @access public
  124. * @static
  125. */
  126. static function postURL( $action ) {
  127. if ( ! empty( $action ) ) {
  128. return $action;
  129. }
  130. return self::url( $_GET['q'] );
  131. }
  132. /**
  133. * Generate an internal CiviCRM URL (copied from DRUPAL/includes/common.inc#url)
  134. *
  135. * @param $path string The path being linked to, such as "civicrm/add"
  136. * @param $query string A query string to append to the link.
  137. * @param $absolute boolean Whether to force the output to be an absolute link (beginning with http:).
  138. * Useful for links that will be displayed outside the site, such as in an
  139. * RSS feed.
  140. * @param $fragment string A fragment identifier (named anchor) to append to the link.
  141. * @param $htmlize boolean whether to convert to html eqivalant
  142. * @param $frontend boolean a gross joomla hack
  143. *
  144. * @return string an HTML string containing a link to the given path.
  145. * @access public
  146. *
  147. */
  148. function url($path = null, $query = null, $absolute = false,
  149. $fragment = null, $htmlize = true,
  150. $frontend = false ) {
  151. $config =& CRM_Core_Config::singleton( );
  152. $script = 'index.php';
  153. if (isset($fragment)) {
  154. $fragment = '#'. $fragment;
  155. }
  156. if ( ! isset( $config->useFrameworkRelativeBase ) ) {
  157. $base = parse_url( $config->userFrameworkBaseURL );
  158. $config->useFrameworkRelativeBase = $base['path'];
  159. }
  160. $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
  161. $separator = $htmlize ? '&amp;' : '&';
  162. if (! $config->cleanURL ) {
  163. if ( isset( $path ) ) {
  164. if ( isset( $query ) ) {
  165. return $base . $script .'?q=' . $path . $separator . $query . $fragment;
  166. } else {
  167. return $base . $script .'?q=' . $path . $fragment;
  168. }
  169. } else {
  170. if ( isset( $query ) ) {
  171. return $base . $script .'?'. $query . $fragment;
  172. } else {
  173. return $base . $fragment;
  174. }
  175. }
  176. } else {
  177. if ( isset( $path ) ) {
  178. if ( isset( $query ) ) {
  179. return $base . $path .'?'. $query . $fragment;
  180. } else {
  181. return $base . $path . $fragment;
  182. }
  183. } else {
  184. if ( isset( $query ) ) {
  185. return $base . $script .'?'. $query . $fragment;
  186. } else {
  187. return $base . $fragment;
  188. }
  189. }
  190. }
  191. }
  192. /**
  193. * Authenticate the user against the drupal db
  194. *
  195. * @param string $name the user name
  196. * @param string $password the password for the above user name
  197. *
  198. * @return mixed false if no auth
  199. * array( contactID, ufID, unique string ) if success
  200. * @access public
  201. * @static
  202. */
  203. static function authenticate( $name, $password ) {
  204. require_once 'DB.php';
  205. $config =& CRM_Core_Config::singleton( );
  206. $dbDrupal = DB::connect( $config->userFrameworkDSN );
  207. if ( DB::isError( $dbDrupal ) ) {
  208. CRM_Core_Error::fatal( "Cannot connect to drupal db via $config->userFrameworkDSN, " . $dbDrupal->getMessage( ) );
  209. }
  210. $password = md5( $password );
  211. $name = $dbDrupal->escapeSimple( strtolower( $name ) );
  212. $sql = 'SELECT u.* FROM ' . $config->userFrameworkUsersTableName .
  213. " u WHERE LOWER(u.name) = '$name' AND u.pass = '$password' AND u.status = 1";
  214. $query = $dbDrupal->query( $sql );
  215. $user = null;
  216. // need to change this to make sure we matched only one row
  217. require_once 'CRM/Core/BAO/UFMatch.php';
  218. while ( $row = $query->fetchRow( DB_FETCHMODE_ASSOC ) ) {
  219. CRM_Core_BAO_UFMatch::synchronizeUFMatch( $user, $row['uid'], $row['mail'], 'Drupal' );
  220. $contactID = CRM_Core_BAO_UFMatch::getContactId( $row['uid'] );
  221. if ( ! $contactID ) {
  222. return false;
  223. }
  224. return array( $contactID, $row['uid'], mt_rand() );
  225. }
  226. return false;
  227. }
  228. /**
  229. * Set a message in the UF to display to a user
  230. *
  231. * @param string $message the message to set
  232. *
  233. * @access public
  234. * @static
  235. */
  236. static function setMessage( $message ) {
  237. drupal_set_message( $message );
  238. }
  239. static function permissionDenied( ) {
  240. drupal_access_denied( );
  241. }
  242. static function logout( ) {
  243. module_load_include( 'inc', 'user', 'user.pages' );
  244. return user_logout( );
  245. }
  246. static function updateCategories( ) {
  247. // copied this from profile.module. Seems a bit inefficient, but i dont know a better way
  248. // CRM-3600
  249. cache_clear_all();
  250. menu_rebuild();
  251. }
  252. /**
  253. * Get the locale set in the hosting CMS
  254. * @return string with the locale or null for none
  255. */
  256. static function getUFLocale()
  257. {
  258. # FIXME: actally fetch the locale from Drupal
  259. return null;
  260. }
  261. }