CommonHelper.php 22 KB

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