PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/_html-warrior/includes/functions.php

https://github.com/halka139/html-warrior
PHP | 530 lines | 405 code | 36 blank | 89 comment | 56 complexity | dad95ac396f17c0ca58325b0305d5c5f MD5 | raw file
  1. <?php
  2. /**
  3. * Debug variable - print_r variable but also add pre tags
  4. * @param bool|string|array $arr
  5. * @param bool $die optional Also die after output
  6. * @param bool $see_html optional
  7. * @return string of the debugable array
  8. */
  9. function arr($arr, $die=false, $see_html=false) {
  10. echo '<hr/>';
  11. $tmp = '';
  12. ob_start();
  13. print_r($arr);
  14. $tmp = ob_get_contents();
  15. ob_end_clean();
  16. echo '<pre style="text-align: left;">';
  17. echo $see_html ? htmlspecialchars($tmp) : $tmp;
  18. echo '</pre>';
  19. echo '<hr/>';
  20. if ($die) {
  21. die();
  22. }
  23. return $arr;
  24. }
  25. /**
  26. * Saves opened urls to database
  27. * @global <type> $db
  28. * @param array $arr
  29. */
  30. function add_access_log($arr=array()) {
  31. global $db;
  32. $q = "
  33. INSERT INTO
  34. access_log
  35. (
  36. id,
  37. site_dir,
  38. url,
  39. date
  40. )
  41. VALUES (
  42. NULL ,
  43. '" . $arr["site_dir"] . "',
  44. '" . $arr["url"] . "',
  45. '" . datetime() . "'
  46. );
  47. ";
  48. $db->queryExec($q);
  49. }
  50. /**
  51. * Gets latest viewed pages
  52. * @global $db
  53. * @param array $arr
  54. * @return array
  55. */
  56. function get_access_log($arr=array()) {
  57. global $db;
  58. $out = array();
  59. if ($arr["limit"]) {
  60. $limit = " LIMIT 0, " . $arr["limit"] . " ";
  61. }
  62. $results = $db->arrayQuery("
  63. SELECT
  64. site_dir, url
  65. FROM
  66. access_log
  67. ORDER BY
  68. date desc
  69. $limit");
  70. foreach ($results as $entry) {
  71. $out[] = $entry;
  72. $name = explode("/", $entry["url"]);
  73. $name = $name[1];
  74. $out[count($out) - 1]["url_wo_slash"] = trim($entry["url"], "/");
  75. $out[count($out) - 1]["name"] = $name;
  76. }
  77. return $out;
  78. }
  79. /**
  80. * Gets latest viewed pages
  81. * @global $db
  82. * @param array $arr
  83. * @return array
  84. */
  85. function get_site_access_log($arr=array()) {
  86. global $db;
  87. $out = array();
  88. $limit = "";
  89. if (@$arr["limit"]) {
  90. $limit = " LIMIT 0, " . $arr["limit"] . " ";
  91. }
  92. $results = $db->arrayQuery("
  93. SELECT
  94. DISTINCT
  95. site_dir
  96. FROM
  97. access_log
  98. ORDER BY
  99. date desc
  100. $limit
  101. ");
  102. foreach ($results as $entry) {
  103. $out[] = $entry["site_dir"] . "/";
  104. }
  105. return $out;
  106. }
  107. function datetime($timestamp=false) {
  108. global $htmlwarrior;
  109. if ($timestamp) {
  110. $date = date("Y-m-d H:M:s", $timestamp + $htmlwarrior->config["timeoffset"]);
  111. } else {
  112. $date = date("Y-m-d H:i:s", time() + $htmlwarrior->config["timeoffset"]);
  113. }
  114. return $date;
  115. }
  116. /**
  117. * Dir copy
  118. * @param string $source
  119. * @param string $target
  120. */
  121. function full_copy($source, $target) {
  122. if (is_dir($source)) {
  123. @mkdir($target);
  124. $d = dir($source);
  125. while (FALSE !== ( $entry = $d->read() )) {
  126. if ($entry == '.' || $entry == '..') {
  127. continue;
  128. }
  129. $Entry = $source . '/' . $entry;
  130. if (is_dir($Entry)) {
  131. full_copy($Entry, $target . '/' . $entry);
  132. continue;
  133. }
  134. copy($Entry, $target . '/' . $entry);
  135. touch($target . '/' . $entry, filemtime($Entry)); // set time
  136. }
  137. $d->close();
  138. } else {
  139. copy($source, $target);
  140. touch($target, filemtime($source)); // set time
  141. }
  142. }
  143. /**
  144. * dir_list is currently used to get the latest projects in ordered by dir
  145. * create date (newer first)
  146. * @param string $dir
  147. * @return array
  148. * @todo sort by
  149. */
  150. function dir_list($dir) {
  151. if ($dir[strlen($dir) - 1] != '/')
  152. $dir .= '/';
  153. if (!is_dir($dir))
  154. return array();
  155. $dir_handle = opendir($dir);
  156. $dir_objects = array();
  157. while (false !== ($object = readdir($dir_handle))) {
  158. if (!in_array($object, array('.', '..'))) {
  159. $filename = $dir . $object;
  160. $file_object = array(
  161. 'name' => $object,
  162. 'size' => filesize($filename),
  163. 'type' => filetype($filename),
  164. 'time' => date("d F Y H:i:s", filemtime($filename)),
  165. 'timestamp' => filemtime($filename)
  166. );
  167. $dir_objects[] = $file_object;
  168. }
  169. }
  170. return $dir_objects;
  171. }
  172. /**
  173. * Unified javascript tag. Used mainly in script partial.
  174. * @param string $file required URI to js. Js extension can be omitted. PHP
  175. * extension can be used.
  176. * @return string <script> tag
  177. */
  178. function html_javascript($file, $scripts_as_root = true) {
  179. $a_file = explode('/', $file);
  180. $tpl = end($a_file);
  181. $tpl_escaped = str_replace('.', '\.', $tpl);
  182. $path = preg_replace('/' . $tpl_escaped . '$/isU', '', $file);
  183. if (strpos($tpl, '.php') === false) {
  184. $path .= $tpl . '.js';
  185. } else {
  186. $path .= $tpl;
  187. }
  188. $src = ($scripts_as_root ? 'scripts/' : '') . $path;
  189. return '<script type="text/javascript" src="' . $src . '"></script>';
  190. }
  191. /**
  192. * Unified style link tag
  193. * @param string $file required URI to css. If extension is missing, .css is added
  194. * to path
  195. * @param string $media optional Can be used to define css media type
  196. * @return <type>
  197. */
  198. function html_css($file, $media=false) {
  199. $a_file = explode("/", $file);
  200. $template_name = end($a_file);
  201. $file = str_replace($template_name, "", $file);
  202. if (strpos($template_name, ".php") === false) {
  203. $file .= $template_name . ".css";
  204. } else {
  205. $file .= $template_name;
  206. }
  207. return '<link rel="stylesheet" type="text/css" href="style/' . $file . '" ' . ($media ? " media=\"" . $media . "\"" : " media=\"all\"") . ' title="" />';
  208. }
  209. // write file to $path
  210. function build_template($path, $content, $touchtime = false) {
  211. $fh = fopen($path, 'w') or die("can't open file");
  212. fwrite($fh, $content);
  213. fclose($fh);
  214. if ($touchtime) {
  215. touch($path, $touchtime); // set time
  216. }
  217. }
  218. // get variables from template
  219. // example:
  220. // @layout = "contact"
  221. function parse_variables($content) {
  222. $variables = array();
  223. $file = explode("\n", $content);
  224. foreach ($file as $key => $var) {
  225. if (preg_match('/^@/', $var)) {
  226. $tempvar = explode('=', $var);
  227. $tempvar_key = trim(str_replace("@", '', $tempvar[0]));
  228. $tempvar_value = trim(str_replace('"', '', $tempvar[1]));
  229. $variables[$tempvar_key][] = $tempvar_value;
  230. }
  231. }
  232. foreach ($variables as $key => $var) {
  233. if (count($var) == 1) {
  234. $tempvar = $var[0];
  235. } else {
  236. $$tempvar = $var;
  237. }
  238. if ($tempvar === "false") {
  239. $tempvar = false;
  240. }
  241. $variables[$key] = $tempvar;
  242. }
  243. return $variables;
  244. }
  245. // remove @ variables from template
  246. function remove_variables($content) {
  247. $file = explode("\n", $content);
  248. foreach ($file as $key => $var) {
  249. if (preg_match("/^@/", $var)) {
  250. unset($file[$key]);
  251. }
  252. }
  253. return implode("\n", $file);
  254. }
  255. /*
  256. $indent = amount of tabs or spaces
  257. */
  258. function indent($content, $indent, $ignore_first_row=true, $char=" ") {
  259. $file = explode("\n", $content);
  260. $real_indent = "";
  261. for ($i = 0; $i < $indent; $i++) {
  262. $real_indent = " " . $real_indent;
  263. }
  264. for ($i = 0; $i < count($file); $i++) {
  265. if ($ignore_first_row && $i == 0) {
  266. continue;
  267. }
  268. $file[$i] = $real_indent . $file[$i];
  269. }
  270. return implode("\n", $file);
  271. }
  272. // todo: ($pos-1)/2; --> calculate real indent with help of config
  273. function get_indents_for_variables($content) {
  274. global $htmlwarrior;
  275. $indents = array();
  276. $file = explode("\n", $content);
  277. for ($i = 0; $i < count($file); $i++) {
  278. $pos = strpos($file[$i], "{\$");
  279. if ($pos !== false) {
  280. preg_match("/{\\$.*}/iU", $file[$i], $mt);
  281. $variable = str_replace(array("{", "\$", "}"), "", $mt[0]);
  282. $indents[$variable] = $pos;
  283. }
  284. }
  285. return $indents;
  286. }
  287. function url_remove_parameters($uri) {
  288. $uri_new = explode('?', $uri);
  289. return $uri_new[0];
  290. }
  291. /**
  292. * Find first tag name from string
  293. * @param string $subject
  294. * @return string Tag name
  295. */
  296. function get_first_tag_name($subject) {
  297. preg_match("/<([a-z].*)(\s.*)>/msiU", $subject, $matches);
  298. return $matches[1];
  299. }
  300. /**
  301. *
  302. * @global array $htmlwarrior->config required htmlwarrior config
  303. * @global string $site_dir required Currently active site name (directory)
  304. * @param <type> $tpl_name
  305. * @return <type>
  306. */
  307. function mk_partial_edit_link($tpl_name) {
  308. global $htmlwarrior;
  309. return (!$htmlwarrior->config['template_edit_links_downloadable'] ?
  310. $htmlwarrior->config['basepath_local'] : '') .
  311. '/' . $htmlwarrior->runtime['site_dir'] .
  312. $htmlwarrior->config['path_templates_partials'] . '/' . $tpl_name;
  313. }
  314. /**
  315. * Get current page url
  316. * @return string
  317. */
  318. function get_cur_page_url() {
  319. $pageurl = 'http';
  320. if ($_SERVER['HTTPS'] == 'on') {
  321. $pageurl .= 's';
  322. }
  323. $pageurl .= '://';
  324. if ($_SERVER['SERVER_PORT'] != '80') {
  325. $pageurl .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
  326. } else {
  327. $pageurl .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  328. }
  329. return $pageurl;
  330. }
  331. /**
  332. * Get baseurl. Used in config.
  333. * @return string
  334. */
  335. function get_baseurl() {
  336. $baseurl = 'http';
  337. if (@$_SERVER['HTTPS'] == 'on') {
  338. $baseurl .= 's';
  339. }
  340. $baseurl .= '://';
  341. if ($_SERVER['SERVER_PORT'] != '80') {
  342. $baseurl .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
  343. } else {
  344. $baseurl .= $_SERVER['SERVER_NAME'];
  345. }
  346. return $baseurl;
  347. }
  348. /**
  349. * Removes BOM from UTF-8. Although it's not recommended to use it
  350. * someone might do it. And it breaks HTML.
  351. * @param string $str
  352. * @return string
  353. */
  354. function remove_bom($str='') {
  355. if (substr($str, 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf)) {
  356. $str = substr($str, 3);
  357. }
  358. return $str;
  359. }
  360. /**
  361. * Get page template path based on url path ( /site_name/foo )
  362. * @param string $url_path
  363. * @return string
  364. */
  365. function get_page_template_path($url_path) {
  366. global $htmlwarrior;
  367. $template_path = '';
  368. $array_splice_offset = 1;
  369. if ($htmlwarrior->config['frontpage_site']) {
  370. $array_splice_offset = 0;
  371. }
  372. if ($htmlwarrior->config['multilingual']) {
  373. $array_splice_offset++;
  374. }
  375. $a_url_path = explode('/', trim($url_path, "/"));
  376. $a_url_path_without_site = array_splice($a_url_path, $array_splice_offset, count($a_url_path));
  377. $url_path_without_site = join('/', $a_url_path_without_site);
  378. if (strlen($url_path_without_site) === 0) {
  379. $url_path_without_site = 'index';
  380. }
  381. $find = array('__logged', '.html');
  382. $replace = '';
  383. $template_path = $htmlwarrior->config['basepath'] . '/' .
  384. $htmlwarrior->runtime['site_dir'] .
  385. $htmlwarrior->config['path_templates_pages'] . '/' .
  386. str_replace($find, $replace, $url_path_without_site);
  387. $template_path_with_ext = $template_path . '.tpl';
  388. // if
  389. if (is_dir($template_path)) {
  390. // check if index exists
  391. if (file_exists($template_path . '/index.tpl')) {
  392. return $template_path . '/index.tpl';
  393. }
  394. } else {
  395. return $template_path_with_ext;
  396. }
  397. }
  398. /**
  399. * Include class file and return an object
  400. * @global object $htmlwarrior
  401. * @param string $classname
  402. * @return instance
  403. */
  404. function classload($classname) {
  405. global $htmlwarrior;
  406. require_once($htmlwarrior->config['basepath'] . $htmlwarrior->config['path_code'] . '/classes/' . $classname . '.php');
  407. $instance = new $classname();
  408. return $instance;
  409. }
  410. /**
  411. * Create orb url
  412. * @global object $htmlwarrior
  413. * @param string $class
  414. * @param string $action
  415. * @param array $arr
  416. * @return string orb link
  417. */
  418. function mk_orb($class, $action, $arr = array()) {
  419. global $htmlwarrior;
  420. $orb = $htmlwarrior->config["path_code"] .
  421. '/orb.php?class=' . $class .
  422. '&amp;action=' . $action;
  423. if (isset($arr["return_url"])) {
  424. $ru = $arr["return_url"];
  425. unset($arr["return_url"]);
  426. }
  427. foreach ($arr as $key => $var) {
  428. $orb .= '&amp;' . $key . '=' . $var;
  429. }
  430. $orb .= '&amp;return_url=' . urlencode($ru);
  431. return $orb;
  432. }
  433. function get_cur_lang() {
  434. global $htmlwarrior;
  435. $out = false;
  436. $prefix = $htmlwarrior->config['htmlwarrior_prefix'];
  437. $cookie_name = $htmlwarrior->config['lang_cookie_name'];
  438. $path = $htmlwarrior->runtime['parsed_url']['path'];
  439. $pathtrimmed = trim($path, "/");
  440. $a_path = explode('/', $pathtrimmed);
  441. $possible_lang = $a_path[0];
  442. if ($_COOKIE[$cookie_name]) {
  443. $out = $_COOKIE[$cookie_name];
  444. } elseif (strlen($possible_lang) == 2) {
  445. $out = $possible_lang;
  446. } else {
  447. $out = $htmlwarrior->config['lang_default'];
  448. }
  449. return $out;
  450. }
  451. /**
  452. * This PHP function scans a given directory and deletes all files and
  453. * subdirectories it finds and has permission to delete.
  454. * http://lixlpixel.org/recursive_function/php/recursive_directory_delete/
  455. * @param string $directory
  456. * @param bool $empty remove only stuff inside directory or remove dir also
  457. * @return bool
  458. */
  459. function recursive_remove_directory($directory, $empty=false) {
  460. if (substr($directory, -1) == '/') {
  461. $directory = substr($directory, 0, -1);
  462. }
  463. if (!file_exists($directory) || !is_dir($directory)) {
  464. return false;
  465. } elseif (is_readable($directory)) {
  466. $handle = opendir($directory);
  467. while (false !== ($item = readdir($handle))) {
  468. if ($item != '.' && $item != '..') {
  469. $path = $directory . '/' . $item;
  470. if (is_dir($path)) {
  471. recursive_remove_directory($path);
  472. } else {
  473. unlink($path);
  474. }
  475. }
  476. }
  477. closedir($handle);
  478. if ($empty == false) {
  479. if (!rmdir($directory)) {
  480. return false;
  481. }
  482. }
  483. }
  484. return true;
  485. }