CommonHelper.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. /**
  4. * All site-wide neccessary stuff for the view layer
  5. */
  6. class CommonHelper extends AppHelper {
  7. public $helpers = array ('Session', 'Html');
  8. public $packages = array(
  9. 'Tools.Jquery' //Used by showDebug
  10. );
  11. /* Layout Stuff */
  12. /**
  13. * convinience function for clean ROBOTS allowance
  14. * @param STRING private/public
  15. * 2008-12-08 ms
  16. */
  17. public function metaRobots($type = null) {
  18. if (($meta = Configure::read('Config.robots')) !== null) {
  19. $type = $meta;
  20. }
  21. $content = array();
  22. if ($type == 'public') {
  23. $this->privatePage = false;
  24. $content['robots']= array('index','follow','noarchive');
  25. } else {
  26. $this->privatePage = true;
  27. $content['robots']= array('noindex','nofollow','noarchive');
  28. }
  29. $return = '<meta name="robots" content="'.implode(',',$content['robots']).'" />';
  30. return $return;
  31. }
  32. /**
  33. * convinience function for clean meta name tags
  34. * @param STRING $name: author, date, generator, revisit-after, language
  35. * @param MIXED $content: if array, it will be seperated by commas
  36. * 2009-07-07 ms
  37. */
  38. public function metaName($name = null, $content = null) {
  39. if (empty($name) || empty($content)) {
  40. return '';
  41. }
  42. if (!is_array($content)) {
  43. $content = (array)$content;
  44. }
  45. $return = '<meta name="'.$name.'" content="'.implode(', ',$content).'" />';
  46. return $return;
  47. }
  48. public function metaDescription($content, $language = null, $options = array()) {
  49. if (!empty($language)) {
  50. $options['lang'] = mb_strtolower($language);
  51. } elseif ($language !== false) {
  52. $options['lang'] = Configure::read('Config.locale'); // DEFAULT_LANGUAGE
  53. }
  54. return $this->Html->meta('description', $content, $options);
  55. }
  56. public function metaKeywords($keywords = null, $language = null, $escape = true) {
  57. if ($keywords === null) {
  58. $keywords = Configure::read('Config.keywords');
  59. }
  60. if (is_array($keywords)) {
  61. $keywords = implode(', ', $keywords);
  62. }
  63. if ($escape) {
  64. $keywords = h($keywords);
  65. }
  66. if (!empty($language)) {
  67. $options['lang'] = mb_strtolower($language);
  68. } elseif ($language !== false) {
  69. $options['lang'] = Configure::read('Config.locale'); // DEFAULT_LANGUAGE
  70. }
  71. return $this->Html->meta('keywords', $keywords, $options);
  72. }
  73. /**
  74. * convinience function for "canonical" SEO links
  75. * 2010-03-03 ms
  76. */
  77. public function metaCanonical($url = null, $full = false) {
  78. $canonical = $this->Html->url($url, $full);
  79. //return $this->Html->meta('canonical', $canonical, array('rel'=>'canonical', 'type'=>null, 'title'=>null));
  80. return '<link rel="canonical" href="'.$canonical.'" />';
  81. }
  82. /**
  83. * convinience function for "alternate" SEO links
  84. * @param mixed $url
  85. * @param mixed $lang (lang(iso2) or array of langs)
  86. * lang: language (in ISO 6391-1 format) + optionally the region (in ISO 3166-1 Alpha 2 format)
  87. * - de
  88. * - de-ch
  89. * etc
  90. * 2011-12-12 ms
  91. */
  92. public function metaAlternate($url, $lang, $full = false) {
  93. $canonical = $this->Html->url($url, $full);
  94. //return $this->Html->meta('canonical', $canonical, array('rel'=>'canonical', 'type'=>null, 'title'=>null));
  95. $lang = (array)$lang;
  96. $res = array();
  97. foreach ($lang as $language => $countries) {
  98. if (is_numeric($language)) {
  99. $language = '';
  100. } else {
  101. $language .= '-';
  102. }
  103. $countries = (array)$countries;
  104. foreach ($countries as $country) {
  105. $l = $language.$country;
  106. $res[] = $this->Html->meta('alternate', $url, array('rel'=>'alternate', 'hreflang'=>$l, 'type'=>null, 'title'=>null)).PHP_EOL;
  107. }
  108. }
  109. return implode('', $res);
  110. }
  111. /**
  112. * convinience function for META Tags
  113. * @param STRING type
  114. * @param STRING content
  115. * 2008-12-08 ms
  116. */
  117. public function metaRss($url = null, $title = null) {
  118. $tags = array(
  119. 'meta' => '<link rel="alternate" type="application/rss+xml" title="%s" href="%s" />',
  120. );
  121. $content = array();
  122. if (empty($url)) { return ''; }
  123. if (empty($title)) {
  124. $title = 'Diesen Feed abonnieren';
  125. }
  126. return sprintf($tags['meta'], $title, $this->url($url));
  127. }
  128. /**
  129. * convinience function for META Tags
  130. * @param STRING type
  131. * @param STRING content
  132. * 2008-12-08 ms
  133. */
  134. public function metaEquiv($type = null, $value = null, $escape = true) {
  135. $tags = array(
  136. 'meta' => '<meta http-equiv="%s"%s />',
  137. );
  138. if (empty($value)) {
  139. return '';
  140. }
  141. if ($escape) {
  142. $value = h($value);
  143. }
  144. if ($type == 'language') {
  145. return sprintf($tags['meta'],'language',' content="'.$value.'"');
  146. } elseif ($type == 'pragma') {
  147. return sprintf($tags['meta'],'pragma',' content="'.$value.'"');
  148. } elseif ($type == 'expires') {
  149. return sprintf($tags['meta'],'expires',' content="'.$value.'"');
  150. } elseif ($type == 'cache-control') {
  151. return sprintf($tags['meta'],'cache-control',' content="'.$value.'"');
  152. } elseif ($type == 'refresh') {
  153. return sprintf($tags['meta'],'refresh',' content="'.$value.'"');
  154. }
  155. return '';
  156. }
  157. /**
  158. * (example): array(x, Tools|y, Tools.Jquery|jquery/sub/z)
  159. * => x is in webroot/
  160. * => y is in plugins/tools/webroot/
  161. * => z is in plugins/tools/packages/jquery/files/jquery/sub/
  162. * 2011-03-23 ms
  163. */
  164. public function css($files = array(), $rel = null, $options = array()) {
  165. $files = (array)$files;
  166. $pieces = array();
  167. foreach ($files as $file) {
  168. $pieces[] = 'file='.$file;
  169. }
  170. if ($v = Configure::read('Config.layout_v')) {
  171. $pieces[] = 'v='.$v;
  172. }
  173. $string = implode('&', $pieces);
  174. return $this->Html->css('/css.php?'.$string, $rel, $options);
  175. }
  176. /**
  177. * (example): array(x, Tools|y, Tools.Jquery|jquery/sub/z)
  178. * => x is in webroot/
  179. * => y is in plugins/tools/webroot/
  180. * => z is in plugins/tools/packages/jquery/files/jquery/sub/
  181. * 2011-03-23 ms
  182. */
  183. public function script($files = array(), $options = array()) {
  184. $files = (array)$files;
  185. foreach ($files as $file) {
  186. $pieces[] = 'file='.$file;
  187. }
  188. if ($v = Configure::read('Config.layout_v')) {
  189. $pieces[] = 'v='.$v;
  190. }
  191. $string = implode('&', $pieces);
  192. return $this->Html->script('/js.php?'.$string, $options);
  193. }
  194. /**
  195. * special css tag generator with the option to add '?...' to the link (for caching prevention)
  196. * IN USAGE
  197. * needs manual adjustment, but still better than the core one!
  198. * @example needs Asset.cssversion => xyz (going up with counter)
  199. * 2008-12-08 ms
  200. */
  201. public function cssDyn($path, $rel = null, $htmlAttributes = array(), $return = true) {
  202. $v = (int)Configure::read('Asset.version');
  203. return $this->Html->css($path.'.css?'.$v, $rel, $htmlAttributes, $return);
  204. }
  205. /**
  206. * NOT IN USAGE
  207. * but better than the core one!
  208. * @example needs Asset.timestamp => force
  209. * 2008-12-08 ms
  210. */
  211. public function cssAuto($path, $rel = null, $htmlAttributes = array(), $return = true) {
  212. define('COMPRESS_CSS',true);
  213. $time = date('YmdHis', filemtime(APP . 'webroot' . DS . CSS_URL . $path . '.css'));
  214. $url = "{$this->request->webroot}" . (COMPRESS_CSS ? 'c' : '') . CSS_URL . $this->themeWeb . $path . ".css?" . $time;
  215. return $url;
  216. }
  217. /* Content Stuff */
  218. public function displayErrors($fields = null) {
  219. $res = '';
  220. if (!empty($this->validationErrors)) {
  221. if ($fields === null) { # catch ALL
  222. foreach ($this->validationErrors as $alias => $error) {
  223. list($alias, $fieldname) = explode('.', $error);
  224. $this->validationErrors[$alias][$fieldname];
  225. }
  226. } elseif (!empty($fields)) {
  227. foreach ($fields as $field) {
  228. list($alias, $fieldname) = explode('.', $field);
  229. if (!empty($this->validationErrors[$alias][$fieldname])) {
  230. $res .= $this->_renderError($this->validationErrors[$alias][$fieldname]);
  231. }
  232. }
  233. }
  234. /*
  235. if (!empty($catched)) {
  236. foreach ($catched as $c) {
  237. }
  238. }
  239. */
  240. }
  241. return $res;
  242. }
  243. public function _renderError($error, $escape = true) {
  244. if ($escape !== false) {
  245. $error = h($error);
  246. }
  247. return '<div class="error-message">'.$error.'</div>';
  248. }
  249. /**
  250. * Alternates between two or more strings.
  251. *
  252. * echo CommonHelper::alternate('one', 'two'); // "one"
  253. * echo CommonHelper::alternate('one', 'two'); // "two"
  254. * echo CommonHelper::alternate('one', 'two'); // "one"
  255. *
  256. * Note that using multiple iterations of different strings may produce
  257. * unexpected results.
  258. * TODO: move to booststrap!!!
  259. *
  260. * @param string strings to alternate between
  261. * @return string
  262. */
  263. public static function alternate() {
  264. static $i;
  265. if (func_num_args() === 0) {
  266. $i = 0;
  267. return '';
  268. }
  269. $args = func_get_args();
  270. return $args[($i++ % count($args))];
  271. }
  272. /**
  273. * check if session works due to allowed cookies
  274. * 2009-06-29 ms
  275. */
  276. public function sessionCheck() {
  277. return !CommonComponent::cookiesDisabled();
  278. /*
  279. if (!empty($_COOKIE) && !empty($_COOKIE[Configure::read('Session.cookie')])) {
  280. return true;
  281. }
  282. return false;
  283. */
  284. }
  285. /**
  286. * display warning if cookies are disallowed (and session won't work)
  287. * 2009-06-29 ms
  288. */
  289. public function sessionCheckAlert() {
  290. if (!$this->sessionCheck()) {
  291. return '<div class="cookieWarning">'.__('Please enable cookies').'</div>';
  292. }
  293. return '';
  294. }
  295. /**
  296. * //TODO: move boostrap
  297. * auto-pluralizing a word using the Inflection class
  298. * @param string $s = the string to be pl.
  299. * @param int $c = the count
  300. * @return $string "member" or "members" OR "Mitglied"/"Mitglieder" if autoTranslate TRUE
  301. * 2009-07-23 ms
  302. */
  303. public function asp($s, $c, $autoTranslate = false) {
  304. if ((int)$c != 1) {
  305. $p = Inflector::pluralize($s);
  306. } else {
  307. $p = null; # no pluralization neccessary
  308. }
  309. return $this->sp($s, $p, $c, $autoTranslate);
  310. }
  311. /**
  312. * //TODO: move boostrap
  313. * manual pluralizing a word using the Inflection class
  314. * 2009-07-23 ms
  315. */
  316. public function sp($s, $p, $c, $autoTranslate = false) {
  317. if ((int)$c != 1) {
  318. $ret = $p;
  319. } else {
  320. $ret = $s;
  321. }
  322. if ($autoTranslate) {
  323. $ret = __($ret);
  324. }
  325. return $ret;
  326. }
  327. /**
  328. * @deprecated
  329. */
  330. public function showDebug() {
  331. $output = '';
  332. $groupout = '';
  333. foreach (debugTab::$content as $group => $debug) {
  334. if (is_int($group)) {
  335. $output .= '<div class="common-debug">';
  336. $output .= "<span style=\"cursor:pointer\" onclick=\"$(this).parent().children('pre').slideToggle('fast');\"><strong>" . h($debug['file']) . '</strong>';
  337. $output .= ' (line <strong>' . $debug['line'] . '</strong>)</span>';
  338. if ($debug['display'])
  339. $debug['display'] = 'block';
  340. else
  341. $debug['display'] = 'none';
  342. $output .= "\n<pre style=\"display:" . $debug['display'] . "\" class=\"cake-debug\">\n";
  343. $output .= h($debug['debug']);
  344. $output .= "\n</pre>\n</div>";
  345. }
  346. }
  347. foreach (debugTab::$groups as $group => $data) {
  348. $groupout .= '<div class="common-debug">';
  349. $groupout .= "<span style=\"cursor:pointer\" onclick=\"$(this).parent().children('div').slideToggle('fast');\"><strong>" . h($group) . '</strong></span>';
  350. foreach ($data as $debug) {
  351. $groupout .= "<div style=\"display:none\"><br/><span style=\"cursor:pointer\" onclick=\"$(this).parent().children('pre').slideToggle('fast');\"><strong>" . h($debug['file']) . '</strong></span>';
  352. $groupout .= ' (line <strong>' . h($debug['line']) . '</strong>)</span>';
  353. if ($debug['display'])
  354. $debug['display'] = 'block';
  355. else
  356. $debug['display'] = 'none';
  357. $groupout .= "\n<pre style=\"display:" . $debug['display'] . "\" class=\"cake-debug\">\n";
  358. $groupout .= h($debug['debug']);
  359. $groupout .= "\n</pre>\n</div>";
  360. }
  361. $groupout .= "\n</div>";
  362. }
  363. return $groupout . $output;
  364. }
  365. /**
  366. * Show FlashMessages
  367. * @param boolean unsorted true/false [default:FALSE = sorted by priority]
  368. * TODO: export div wrapping method (for static messaging on a page)
  369. * TODO: sorting
  370. * 2010-11-22 ms
  371. */
  372. public function flash($unsorted = false, $backwardsComp = true) {
  373. // Get the messages from the session
  374. $messages = (array)$this->Session->read('messages');
  375. $cMessages = (array)Configure::read('messages');
  376. if (!empty($cMessages)) {
  377. $messages = (array)Set::merge($messages, $cMessages);
  378. }
  379. $html='';
  380. if (!empty($messages)) {
  381. $html = '<div class="flashMessages">';
  382. if ($unsorted !== true) {
  383. // Add a div for each message using the type as the class
  384. foreach ($messages as $type => $msgs) {
  385. foreach ((array)$msgs as $msg) {
  386. $html .= $this->_message($msg, $type);
  387. }
  388. }
  389. } else {
  390. foreach ($messages as $type) {
  391. //
  392. }
  393. }
  394. $html .= '</div>';
  395. if (method_exists($this->Session, 'delete')) {
  396. $this->Session->delete('messages');
  397. } else {
  398. CakeSession::delete('messages');
  399. }
  400. }
  401. return $html;
  402. }
  403. /**
  404. * output a single flashMessage
  405. * 2010-11-22 ms
  406. */
  407. public function flashMessage($msg, $type = 'info', $escape = true) {
  408. $html = '<div class="flashMessages">';
  409. if ($escape) {
  410. $msg = h($msg);
  411. }
  412. $html .= $this->_message($msg, $type);
  413. $html .= '</div>';
  414. return $html;
  415. }
  416. protected function _message($msg, $type) {
  417. if (!empty($msg)) {
  418. return '<div class="message'.(!empty($type) ? ' '.$type : '').'">'.$msg.'</div>';
  419. }
  420. return '';
  421. }
  422. /**
  423. * add a message on the fly
  424. * 2011-05-25 ms
  425. */
  426. public function transientFlashMessage($msg, $class = null) {
  427. return CommonComponent::transientFlashMessage($msg, $class);
  428. }
  429. /**
  430. * SINGLE ROLES function
  431. * currently: isRole('admin'), isRole('user')
  432. * 2009-07-06 ms
  433. */
  434. public function isRole($role) {
  435. $sessionRole = $this->Session->read('Auth.User.role_id');
  436. $roles = array(
  437. ROLE_USER => 'user',
  438. ROLE_ADMIN => 'admin',
  439. ROLE_SUPERADMIN => 'superadmin',
  440. ROLE_GUEST => 'guest',
  441. );
  442. if (!empty($roles[$sessionRole]) && $role = $roles[$sessionRole]) {
  443. return true;
  444. }
  445. return false;
  446. }
  447. /**
  448. * Checks if a role is in the current users session
  449. * @param neccessary right(s) as array - or a single one as string possible
  450. * Note: all of them need to be in the user roles to return true by default
  451. */
  452. public function roleNames($sessionRoles = null) {
  453. $tmp = array();
  454. if ($sessionRoles === null) {
  455. $sessionRoles = $this->Session->read('Auth.User.Role');
  456. }
  457. $roles = Cache::read('User.Role');
  458. if (empty($roles) || !is_array($roles)) {
  459. $Role = ClassRegistry::init('Role');
  460. /*
  461. if ($Role->useDbConfig == 'test_suite') {
  462. return array();
  463. }
  464. */
  465. $roles = $Role->getActive('list');
  466. Cache::write('User.Role', $roles);
  467. }
  468. //$roleKeys = Set::combine($roles, '/Role/id','/Role/name'); // on find(all)
  469. if (!empty($sessionRoles)) {
  470. if (is_array($sessionRoles)) {
  471. foreach ($sessionRoles as $sessionRole) {
  472. if (!$sessionRole) {
  473. continue;
  474. }
  475. if (array_key_exists((int)$sessionRole, $roles)) {
  476. $tmp[$sessionRole] = $roles[(int)$sessionRole];
  477. }
  478. }
  479. } else {
  480. if (array_key_exists($sessionRoles, $roles)) {
  481. $tmp[$sessionRoles] = $roles[$sessionRoles];
  482. }
  483. }
  484. }
  485. return $tmp;
  486. }
  487. /**
  488. * Display Roles separated by Commas
  489. * 2009-07-17 ms
  490. */
  491. public function displayRoles($sessionRoles = null, $placeHolder = '---') {
  492. $roles = $this->roleNames($sessionRoles);
  493. if (!empty($roles)) {
  494. return implode(', ',$roles);
  495. }
  496. return $placeHolder;
  497. }
  498. /**
  499. * escape text
  500. * @param string $text
  501. * @param options
  502. * - nl2br: true/false (defaults to true)
  503. * - escape: false prevents h() and space transformation (defaults to true)
  504. * - tabsToSpaces: int (defaults to 4)
  505. * 2010-11-20 ms
  506. */
  507. public function esc($text, $options = array()) {
  508. if (!isset($options['escape']) || $options['escape'] !== false) {
  509. //$text = str_replace(' ', '&nbsp;', h($text));
  510. $text = h($text);
  511. # try to fix indends made out of spaces
  512. $text = explode(NL, $text);
  513. foreach ($text as $key => $t) {
  514. $i = 0;
  515. while (!empty($t[$i]) && $t[$i] === ' ') {
  516. $i++;
  517. }
  518. if ($i > 0) {
  519. $t = str_repeat('&nbsp;', $i) . substr($t, $i);
  520. $text[$key] = $t;
  521. }
  522. }
  523. $text = implode(NL, $text);
  524. $esc = true;
  525. }
  526. if (!isset($options['nl2br']) || $options['nl2br'] !== false) {
  527. $text = nl2br($text);
  528. }
  529. if (!isset($options['tabsToSpaces'])) {
  530. $options['tabsToSpaces'] = 4;
  531. }
  532. if (!empty($options['tabsToSpaces'])) {
  533. $text = str_replace(TB, str_repeat(!empty($esc) ? '&nbsp;' : ' ', $options['tabsToSpaces']), $text);
  534. }
  535. return $text;
  536. }
  537. /**
  538. * takes int / array(int) and finds the role name to it
  539. * @return array roles
  540. */
  541. public function roleNamesTranslated($value) {
  542. if (empty($value)) { return array(); }
  543. $ret = array();
  544. $translate = (array)Configure::read('Role');
  545. if (is_array($value)) {
  546. foreach ($value as $k => $v) {
  547. $ret[$v] = __($translate[$v]);
  548. }
  549. } else {
  550. $ret[$value] = __($translate[$value]);
  551. }
  552. return $ret;
  553. }
  554. /**
  555. * //TODO: move to TextExt?
  556. * minimizes the given url to a maximum length
  557. * @param string $url the url
  558. * @param int $max the maximum length
  559. * @param options
  560. * - placeholder
  561. * @return string the manipulated url (+ eventuell ...)
  562. */
  563. public function minimizeUrl($url = null, $max = null, $options = array()) {
  564. // check if there is nothing to do
  565. if (empty($url) || mb_strlen($url) <= (int)$max)
  566. return (string)$url;
  567. // http:// has not to be displayed, so
  568. if (mb_substr($url,0,7) == 'http://')
  569. $url = mb_substr($url,7);
  570. // cut the parameters
  571. if (mb_strpos($url,'/') !== false)
  572. $url = strtok($url,'/');
  573. // return if the url is short enough
  574. if (mb_strlen($url) <= (int)$max)
  575. return $url;
  576. // otherwise cut a part in the middle (but only if long enough!!!)
  577. # TODO: more dynamically
  578. $placeholder = CHAR_HELLIP;
  579. if (!empty($options['placeholder'])) {
  580. $placeholder = $options['placeholder'];
  581. }
  582. $end = mb_substr($url,-5,5);
  583. $front = mb_substr($url,0,(int)$max - 8);
  584. return $front.$placeholder.$end;
  585. }
  586. /** should be in format END **/
  587. /**
  588. * prevents site being opened/included by others/websites inside frames
  589. * 2009-01-08 ms
  590. */
  591. public function framebuster() {
  592. return $this->Html->scriptBlock('
  593. if (top!=self) top.location.ref=self.location.href;
  594. ');
  595. }
  596. /**
  597. * currenctly only alerts on IE6/IE7
  598. * options
  599. * - engine (js, jquery)
  600. * - escape
  601. * needs the id element to be a present (div) container in the layout
  602. * 2009-12-26 ms
  603. **/
  604. public function browserAlert($id, $message, $options = array()) {
  605. $engine = 'js';
  606. if (!isset($options['escape']) || $options['escape'] !== false) {
  607. $message = h($message);
  608. }
  609. return $this->Html->scriptBlock('
  610. // Returns the version of Internet Explorer or a -1
  611. function getInternetExplorerVersion() {
  612. var rv = -1; // Return value assumes failure.
  613. if (navigator.appName == "Microsoft Internet Explorer") {
  614. var ua = navigator.userAgent;
  615. var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  616. if (re.exec(ua) != null)
  617. rv = parseFloat( RegExp.$1 );
  618. }
  619. return rv;
  620. }
  621. if ((document.all) && (navigator.appVersion.indexOf("MSIE 7.") != -1) || typeof document.body.style.maxHeight === "undefined") {
  622. document.getElementById(\''.$id.'\').innerHTML = \''.$message.'\';
  623. }
  624. /*
  625. jQuery(document).ready(function() {
  626. if ($.browser.msie && $.browser.version.substring(0,1) < 8) {
  627. document.getElementById(\''.$id.'\').innerHTML = \''.$message.'\';
  628. }
  629. });
  630. */
  631. ');
  632. /*
  633. if ($.browser.msie) {
  634. var version = $.browser.version.substring(0,1);
  635. }
  636. */
  637. }
  638. /**
  639. * in noscript tags:
  640. * - link which should not be followed by bots!
  641. * - "pseudo"image which triggers log
  642. * 2009-12-28 ms
  643. */
  644. public function honeypot($noFollowUrl, $noscriptUrl = array()) {
  645. $res = '<div class="invisible" style="display:none"><noscript>';
  646. $res .= $this->Html->defaultLink('Email', $noFollowUrl, array('rel'=>'nofollow'));
  647. if (!empty($noscriptUrl)) {
  648. $res .= BR.$this->Html->image($this->Html->defaultUrl($noscriptUrl, true)); //$this->Html->link($noscriptUrl);
  649. }
  650. $res .= '</noscript></div>';
  651. return $res;
  652. }
  653. /**
  654. * Creates an HTML link.
  655. *
  656. * If $url starts with "http://" this is treated as an external link. Else,
  657. * it is treated as a path to controller/action and parsed with the
  658. * HtmlHelper::url() method.
  659. *
  660. * If the $url is empty, $title is used instead.
  661. *
  662. * @param string $title The content to be wrapped by <a> tags.
  663. * @param mixed $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  664. * @param array $htmlAttributes Array of HTML attributes.
  665. * @param string $confirmMessage JavaScript confirmation message.
  666. * @param boolean $escapeTitle Whether or not $title should be HTML escaped.
  667. * @return string An <a /> element.
  668. * // core-hack! $rel = null | !!!!!!!!! Somehow causes trouble with routing functionality of this helper function... careful!
  669. */
  670. public function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true, $rel = null) {
  671. if ($url !== null) {
  672. /** core-hack $rel (relative to current position/routing) **/
  673. if ($rel === true || !is_array($url)) {
  674. // leave it as it is
  675. } else {
  676. $defaultArray = array('admin'=>false, 'prefix'=>0);
  677. $url = array_merge($defaultArray,$url);
  678. }
  679. /** core-hack END **/
  680. return $this->Html->link($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle);
  681. }
  682. }
  683. /** Stats **/
  684. /**
  685. * print js-visit-stats-link to layout
  686. * uses Piwik open source statistics framework
  687. * 2009-04-15 ms
  688. */
  689. public function visitStats($viewPath = null) {
  690. $res = '';
  691. if (!defined('HTTP_HOST_LIVESERVER')) {
  692. return '';
  693. }
  694. if (HTTP_HOST == HTTP_HOST_LIVESERVER && (int)Configure::read('Config.tracking') === 1) {
  695. $trackingUrl = Configure::read('Config.tracking_url');
  696. if (empty($trackingUrl)) {
  697. $trackingUrl = 'visit_stats';
  698. }
  699. $error = false;
  700. if (!empty($viewPath) && $viewPath == 'errors') {
  701. $error = true;
  702. }
  703. $res .= '
  704. <script type="text/javascript">
  705. var pkBaseURL = (("https:" == document.location.protocol) ? "https://'.HTTP_HOST.'/'.$trackingUrl.'/" : "http://'.HTTP_HOST.'/'.$trackingUrl.'/");
  706. document.write(unescape("%3Cscript src=\'" + pkBaseURL + "piwik.js\' type=\'text/javascript\'%3E%3C/script%3E"));
  707. </script>
  708. <script type="text/javascript">
  709. try {
  710. var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
  711. piwikTracker.trackPageView();
  712. piwikTracker.enableLinkTracking();
  713. '.($error?'piwikTracker.setDocumentTitle(\'404/URL = \'+encodeURIComponent(document.location.pathname+document.location.search) + \'/From = \' + encodeURIComponent(document.referrer));':'').'
  714. } catch( err ) {}
  715. </script>
  716. <noscript><p>'.$this->visitStatsImg().'</p></noscript>
  717. ';
  718. }
  719. return $res;
  720. }
  721. /**
  722. * non js browsers
  723. * 2009-09-07 ms
  724. */
  725. public function visitStatsImg($trackingUrl = null) {
  726. if (empty($trackingUrl)) {
  727. $trackingUrl = Configure::read('Config.tracking_url');
  728. }
  729. if (empty($trackingUrl)) {
  730. $trackingUrl = 'visit_stats';
  731. }
  732. return '<img src="'.Router::url('/').$trackingUrl.'/piwik.php?idsite=1" style="border:0" alt=""/>';
  733. }
  734. }