1: <?php
2:
3: /**
4: * ProcessWire Functions
5: *
6: * Common API functions useful outside of class scope
7: *
8: * ProcessWire 2.x
9: * Copyright (C) 2011 by Ryan Cramer
10: * Licensed under GNU/GPL v2, see LICENSE.TXT
11: *
12: * http://www.processwire.com
13: * http://www.ryancramer.com
14: *
15: */
16:
17: /**
18: * Return a ProcessWire API variable, or NULL if it doesn't exist
19: *
20: * And the wire() function is the recommended way to access the API when included from other PHP scripts.
21: * Like the fuel() function, except that ommitting $name returns the current ProcessWire instance rather than the fuel.
22: * The distinction may not matter in most cases.
23: *
24: * @param string $name If ommitted, returns a Fuel object with references to all the fuel.
25: * @return mixed Fuel value if available, NULL if not.
26: *
27: */
28: function wire($name = 'wire') {
29: return Wire::getFuel($name);
30: }
31:
32: /**
33: * Return all Fuel, or specified ProcessWire API variable, or NULL if it doesn't exist.
34: *
35: * Same as Wire::getFuel($name) and Wire::getAllFuel();
36: * When a $name is specified, this function is identical to the wire() function.
37: * Both functions exist more for consistent naming depending on usage.
38: *
39: * @param string $name If ommitted, returns a Fuel object with references to all the fuel.
40: * @return mixed Fuel value if available, NULL if not.
41: *
42: */
43: function fuel($name = '') {
44: if(!$name) return Wire::getAllFuel();
45: return Wire::getFuel($name);
46: }
47:
48:
49: /**
50: * Indent the given string with $numTabs tab characters
51: *
52: * Newlines are assumed to be \n
53: *
54: * Watch out when using this function with strings that have a <textarea>, you may want to have it use \r newlines, at least temporarily.
55: *
56: * @param string $str String that needs the tabs
57: * @param int $numTabs Number of tabs to insert per line (note any existing tabs are left as-is, so indentation is retained)
58: * @param string $str The provided string but with tabs inserted
59: *
60: */
61: if(!function_exists("tabIndent")):
62: function tabIndent($str, $numTabs) {
63: $tabs = str_repeat("\t", $numTabs);
64: $str = str_replace("\n", "\n$tabs", $str);
65: return $str;
66: }
67: endif;
68:
69: /**
70: * Remove newlines from the given string and return it
71: *
72: * @param string $str
73: * @return string
74: *
75: */
76: function removeNewlines($str) {
77: return str_replace(array("\r", "\n", "\r\n"), ' ', $str);
78: }
79:
80: /**
81: * Emulate register globals OFF
82: *
83: * Should be called after session_start()
84: *
85: * This function is from the PHP documentation at:
86: * http://www.php.net/manual/en/faq.misc.php#faq.misc.registerglobals
87: *
88: */
89: function unregisterGLOBALS() {
90:
91: if(!ini_get('register_globals')) {
92: return;
93: }
94:
95: // Might want to change this perhaps to a nicer error
96: if(isset($_REQUEST['GLOBALS']) || isset($_FILES['GLOBALS'])) {
97: die();
98: }
99:
100: // Variables that shouldn't be unset
101: $noUnset = array('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
102:
103: $input = array_merge($_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
104:
105: foreach ($input as $k => $v) {
106: if(!in_array($k, $noUnset) && isset($GLOBALS[$k])) {
107: unset($GLOBALS[$k]);
108: }
109: }
110: }
111:
112: /**
113: * Encode array for storage and remove empty values
114: *
115: * Uses json_encode and works the same way except this function clears out empty root-level values.
116: * It also forces number strings that can be integers to be integers.
117: *
118: * The end result of all this is more optimized JSON.
119: *
120: * Use json_encode() instead if you don't want any empty values removed.
121: *
122: * @param array $data Array to be encoded to JSON
123: * @param bool|array $allowEmpty Should empty values be allowed in the encoded data?
124: * - Specify false to exclude all empty values (this is the default if not specified).
125: * - Specify true to allow all empty values to be retained.
126: * - Specify an array of keys (from data) that should be retained if you want some retained and not others.
127: * - Specify the digit 0 to retain values that are 0, but not other types of empty values.
128: * @param array $keepKeys Array of keys from $data that should still be included even if blank (optional). Applicable only if $allowEmpty is true.
129: * @return string String of JSON data
130: *
131: */
132: function wireEncodeJSON(array $data, $allowEmpty = false) {
133:
134: foreach($data as $key => $value) {
135:
136: // make sure ints are stored as ints
137: if(is_string($value) && ctype_digit("$value") && $value <= PHP_INT_MAX) $value = (int) $value;
138:
139: $data[$key] = $value;
140:
141: // skip empty values whether blank, 0, empty array, etc.
142: if(empty($value)) {
143:
144: if($allowEmpty === 0 && $value === 0) {
145: // keep it because $allowEmpty === 0 means to keep 0 values only
146:
147: } else if(is_array($allowEmpty) && !in_array($key, $allowEmpty)) {
148: // remove it because it's not specifically allowed in allowEmpty
149: unset($data[$key]);
150:
151: } else if(!$allowEmpty) {
152: // remove the empty value
153: unset($data[$key]);
154: }
155: }
156: }
157: if(!count($data)) return '';
158: return json_encode($data);
159: }
160:
161: /**
162: * Decode JSON to array
163: *
164: * Uses json_decode and works the same way except that arrays are forced.
165: * This is the counterpart to the wireEncodeJSON() function.
166: *
167: * @param string $json A JSON encoded string
168: * @return array
169: *
170: */
171: function wireDecodeJSON($json) {
172: if(empty($json) || $json == '[]') return array();
173: return json_decode($json, true);
174: }
175:
176:
177: /**
178: * Create a directory that is writable to ProcessWire and uses the $config chmod settings
179: *
180: * @param string $path
181: * @return bool
182: *
183: */
184: function wireMkdir($path) {
185: if(!is_dir($path)) if(!@mkdir($path)) return false;
186: $chmodDir = wire('config')->chmodDir;
187: if($chmodDir) chmod($path, octdec($chmodDir));
188: return true;
189: }
190:
191:
192: