1<?php
2
3/*
4 * GitBackedUtil.php
5 *
6 * PHP common utility function lib
7 *
8 * @package    GitBackedUtil.php
9 * @version    1.0
10 * @author     Markus Hoffrogge
11 * @copyright  Copyright 2023 Markus Hoffrogge
12 * @repo       https://github.com/woolfg/dokuwiki-plugin-gitbacked
13 */
14
15if (__FILE__ == $_SERVER['SCRIPT_FILENAME']) die('Bad load order');
16
17// ------------------------------------------------------------------------
18
19/**
20 * GitBacked Utility Class
21 *
22 * This class provides common utility functions.
23 *
24 * @class  GitBackedUtil
25 */
26class GitBackedUtil {
27
28    /**
29     * GitBacked temp directory
30     *
31     * @var string
32     */
33    protected static $temp_dir = '';
34
35    /**
36     * Checks, if a given path name is an absolute path or not.
37     * This function behaves like absolute path determination in dokuwiki init.php fullpath() method.
38     * The relevant code has been copied from there.
39     *
40     * @access  public
41     * @param   string $path    a file path name
42     * @return  bool
43     */
44    public static function isAbsolutePath($path) {
45        $ret = false;
46
47        $iswin = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' || !empty($GLOBALS['DOKU_UNITTEST_ASSUME_WINDOWS']));
48        // check for the (indestructable) root of the path - keeps windows stuff intact
49        if($path[0] == '/') {
50            $ret = true;
51        } else if($iswin) {
52            // match drive letter and UNC paths
53            if(preg_match('!^([a-zA-z]:)(.*)!',$path,$match)) {
54                $ret = true;
55            } else if(preg_match('!^(\\\\\\\\[^\\\\/]+\\\\[^\\\\/]+[\\\\/])(.*)!',$path,$match)) {
56                $ret = true;
57            }
58        }
59
60        return $ret;
61    }
62
63    /**
64     * Returns the $path as is, if it is an absolute path.
65     * Otherwise it will prepend the base path appropriate
66     * to the type of DW instance.
67     *
68     * @access  public
69     * @param   string $path    a file path name
70     * @return  string          an appropriate absolute path
71     */
72    public static function getEffectivePath($path) {
73        $ret = $path;
74
75        if (self::isAbsolutePath($ret)) {
76            return $ret;
77        }
78        if (defined('DOKU_FARM')) {
79            $ret = DOKU_CONF.'../'.$ret;
80        } else {
81            $ret = DOKU_INC.$ret;
82        }
83        return $ret;
84    }
85
86    /**
87     * Returns the temp dir for GitBacked plugin.
88     * It ensures that the temp dir will be created , if not yet existing.
89     *
90     * @access  public
91     * @return  string          the gitbacked temp directory name
92     */
93    public static function getTempDir() {
94        if (empty(self::$temp_dir)) {
95            global $conf;
96            self::$temp_dir = $conf['tmpdir'].'/gitbacked';
97            io_mkdir_p(self::$temp_dir);
98        }
99        return self::$temp_dir;
100    }
101
102    /**
103     * Creates a temp file and writes the $message to it.
104     * It ensures that the temp dir will be created , if not yet existing.
105     *
106     * @access  public
107     * @param   string $message the text message
108     * @return  string          the temp filename created
109     */
110    public static function createMessageFile($message) {
111        $tmpfname = tempnam(self::getTempDir(), 'gitMessage_');
112        $handle = fopen($tmpfname, "w");
113        if (!empty($message)) {
114            fwrite($handle, $message);
115        }
116        fclose($handle);
117        return $tmpfname;
118    }
119
120}
121/* End of file */
122