CommonHelper.php 24 KB

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