PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/shopp/core/legacy.php

https://bitbucket.org/sanders_nick/legacy-media
PHP | 352 lines | 330 code | 2 blank | 20 comment | 7 complexity | 31214a27ba80c65fb1560f89a8d2128c MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0, BSD-3-Clause, GPL-3.0
  1. <?php
  2. /**
  3. * legacy.php
  4. * A library of functions for compatibility with older version of PHP and WordPress
  5. *
  6. * @author Jonathan Davis
  7. * @version 1.0
  8. * @copyright Ingenesis Limited, November 18, 2009
  9. * @license GNU GPL version 3 (or later) {@see license.txt}
  10. * @package shopp
  11. **/
  12. if (!function_exists('json_encode')) {
  13. /**
  14. * Builds JSON {@link http://www.json.org/} formatted strings from PHP data structures
  15. *
  16. * @author Jonathan Davis
  17. * @since PHP 5.2.0+
  18. *
  19. * @param mixed $a PHP data structure
  20. * @return string JSON encoded string
  21. **/
  22. function json_encode ($a = false) {
  23. if (is_null($a)) return 'null';
  24. if ($a === false) return 'false';
  25. if ($a === true) return 'true';
  26. if (is_scalar($a)) {
  27. if (is_float($a)) {
  28. // Always use "." for floats.
  29. return floatval(str_replace(",", ".", strval($a)));
  30. }
  31. if (is_string($a)) {
  32. static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
  33. return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
  34. } else return $a;
  35. }
  36. $isList = true;
  37. for ($i = 0, reset($a); $i < count($a); $i++, next($a)) {
  38. if (key($a) !== $i) {
  39. $isList = false;
  40. break;
  41. }
  42. }
  43. $result = array();
  44. if ($isList) {
  45. foreach ($a as $v) $result[] = json_encode($v);
  46. return '[' . join(',', $result) . ']';
  47. } else {
  48. foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
  49. return '{' . join(',', $result) . '}';
  50. }
  51. }
  52. }
  53. if(!function_exists('scandir')) {
  54. /**
  55. * Lists files and directories inside the specified path
  56. *
  57. * @author Jonathan Davis
  58. * @since PHP 5.0+
  59. *
  60. * @param string $dir Directory path to scan
  61. * @param int $sortorder The sort order of the file listing (0=alphabetic, 1=reversed)
  62. * @return array|boolean The list of files or false if not available
  63. **/
  64. function scandir($dir, $sortorder = 0) {
  65. if(is_dir($dir) && $dirlist = @opendir($dir)) {
  66. $files = array();
  67. while(($file = readdir($dirlist)) !== false) $files[] = $file;
  68. closedir($dirlist);
  69. ($sortorder == 0) ? asort($files) : rsort($files);
  70. return $files;
  71. } else return false;
  72. }
  73. }
  74. if (!function_exists('property_exists')) {
  75. /**
  76. * Checks an object for a declared property
  77. *
  78. * @author Jonathan Davis
  79. * @since PHP 5.1.0+
  80. *
  81. * @param object $Object The object to inspect
  82. * @param string $property The name of the property to look for
  83. * @return boolean True if the property exists, false otherwise
  84. **/
  85. function property_exists($object, $property) {
  86. return array_key_exists($property, get_object_vars($object));
  87. }
  88. }
  89. if ( !function_exists('sys_get_temp_dir')) {
  90. /**
  91. * Determines the temporary directory for the local system
  92. *
  93. * @author Jonathan Davis
  94. * @since PHP 5.2.1+
  95. *
  96. * @return string The path to the system temp directory
  97. **/
  98. function sys_get_temp_dir() {
  99. if (!empty($_ENV['TMP'])) return realpath($_ENV['TMP']);
  100. if (!empty($_ENV['TMPDIR'])) return realpath( $_ENV['TMPDIR']);
  101. if (!empty($_ENV['TEMP'])) return realpath( $_ENV['TEMP']);
  102. $tempfile = tempnam(uniqid(rand(),TRUE),'');
  103. if (file_exists($tempfile)) {
  104. unlink($tempfile);
  105. return realpath(dirname($tempfile));
  106. }
  107. }
  108. }
  109. if (!function_exists('get_class_property')) {
  110. /**
  111. * Gets the property of an uninstantiated class
  112. *
  113. * Provides support for getting a property of an uninstantiated
  114. * class by dynamic name. As of PHP 5.3.0 this function is no
  115. * longer necessary as you can simply reference as $Classname::$property
  116. *
  117. * @author Jonathan Davis
  118. * @since PHP 5.3.0
  119. *
  120. * @param string $classname Name of the class
  121. * @param string $property Name of the property
  122. * @return mixed Value of the property
  123. **/
  124. function get_class_property ($classname, $property) {
  125. if(!class_exists($classname)) return;
  126. if(!property_exists($classname, $property)) return;
  127. $vars = get_class_vars($classname);
  128. return $vars[$property];
  129. }
  130. }
  131. if (!function_exists('array_replace')) {
  132. /**
  133. * Replaces elements from passed arrays into the first array
  134. *
  135. * Provides backwards compatible support for the PHP 5.3.0
  136. * array_replace() function.
  137. *
  138. * @author Jonathan Davis
  139. * @since PHP 5.3.0
  140. *
  141. * @return array
  142. **/
  143. function array_replace (&$array, &$array1) {
  144. $args = func_get_args();
  145. $count = func_num_args();
  146. for ($i = 1; $i < $count; $i++) {
  147. if (is_array($args[$i]))
  148. foreach ($args[$i] as $k => $v) $array[$k] = $v;
  149. }
  150. return $array;
  151. }
  152. }
  153. if (!function_exists('array_intersect_key')) {
  154. /**
  155. * Computes the intersection of arrays using keys for comparison
  156. *
  157. * @author Jonathan Davis
  158. * @since PHP 5.1.0
  159. *
  160. * @return array
  161. **/
  162. function array_intersect_key () {
  163. $arrays = func_get_args();
  164. $result = array_shift($arrays);
  165. foreach ($arrays as $array) {
  166. foreach ($result as $key => $v)
  167. if (!array_key_exists($key, $array)) unset($result[$key]);
  168. }
  169. return $result;
  170. }
  171. }
  172. if (defined('SHOPP_PROXY_CONNECT') && SHOPP_PROXY_CONNECT) {
  173. /**
  174. * Converts legacy Shopp proxy config macros to WP_HTTP_Proxy config macros
  175. *
  176. * @author Jonathan Davis
  177. * @since 1.2
  178. *
  179. * @deprecated SHOPP_PROXY_CONNECT
  180. * @deprecated SHOPP_PROXY_SERVER
  181. * @deprecated SHOPP_PROXY_USERPWD
  182. *
  183. * @return void
  184. **/
  185. function shopp_convert_proxy_config () {
  186. if (!defined('SHOPP_PROXY_SERVER') || !defined('SHOPP_PROXY_USERPWD')) return;
  187. $host = SHOPP_PROXY_SERVER;
  188. $user = SHOPP_PROXY_USERPWD;
  189. if (false !== strpos($host,':')) list($host,$port) = explode(':',$host);
  190. if (false !== strpos($user,':')) list($user,$pwd) = explode(':',$user);
  191. if (!defined('WP_PROXY_HOST')) define('WP_PROXY_HOST',$host);
  192. if (!defined('WP_PROXY_PORT') && $port) define('WP_PROXY_PORT',$port);
  193. if (!defined('WP_PROXY_USERNAME')) define('WP_PROXY_USERNAME',$user);
  194. if (!defined('WP_PROXY_PASSWORD') && $pwd) define('WP_PROXY_PASSWORD',$pwd);
  195. }
  196. shopp_convert_proxy_config();
  197. }
  198. if (!function_exists('get_class_property')) {
  199. /**
  200. * Provides dynamic access to a specified class property
  201. *
  202. * @author Jonathan Davis
  203. * @since 1.2
  204. *
  205. * @param string $Class Name of the class to lookup a property from
  206. * @param string $property The name of the property to retrieve
  207. * @return mixed The value of the requested property
  208. **/
  209. function get_class_property ($Class,$property) {
  210. if(!class_exists($Class)) return null;
  211. if(!property_exists($Class, $property)) return null;
  212. $vars = get_class_vars($Class);
  213. return $vars[$property];
  214. }
  215. }
  216. if (!function_exists('shopp_suhosin_warning')) {
  217. /**
  218. * Detect Suhosin enabled with problematic settings
  219. *
  220. * @author Jonathan Davis
  221. * @since 1.2
  222. *
  223. * @return void Description...
  224. **/
  225. function shopp_suhosin_warning () {
  226. return ( // Is Suhosin loaded or available?
  227. (extension_loaded('Suhosin') || (defined('SUHOSIN_PATCH') && SUHOSIN_PATCH))
  228. && // Are the known problem settings defined?
  229. (
  230. @ini_get('suhosin.max_array_index_length') > 0 && @ini_get('suhosin.max_array_index_length') < 256
  231. && @ini_get('suhosin.post.max_array_index_length') > 0 && @ini_get('suhosin.post.max_array_index_length') < 256
  232. && @ini_get('suhosin.post.max_totalname_length') > 0 && @ini_get('suhosin.post.max_totalname_length') < 65535
  233. && @ini_get('suhosin.post.max_vars') > 0 && @ini_get('suhosin.post.max_vars') < 1024
  234. && @ini_get('suhosin.request.max_array_index_length') > 0 && @ini_get('suhosin.request.max_array_index_length') < 256
  235. && @ini_get('suhosin.request.max_totalname_length') > 0 && @ini_get('suhosin.request.max_totalname_length') < 65535
  236. && @ini_get('suhosin.request.max_vars') > 0 && @ini_get('suhosin.request.max_vars') < 1024
  237. )
  238. );
  239. }
  240. }
  241. /**
  242. * Checks for prerequisite technologies needed for Shopp
  243. *
  244. * @author Jonathan Davis
  245. * @since 1.0
  246. * @version 1.2
  247. *
  248. * @return void
  249. **/
  250. if (!function_exists('shopp_prereqs')) {
  251. function shopp_prereqs () {
  252. $activation = false;
  253. if ( isset($_GET['action']) && isset($_GET['plugin']) ) {
  254. $activation = ('activate' == $_GET['action']);
  255. if ($activation) {
  256. $plugin = $_GET['plugin'];
  257. if (function_exists('check_admin_referer'))
  258. check_admin_referer('activate-plugin_' . $plugin);
  259. }
  260. }
  261. $errors = array();
  262. // Check PHP version
  263. if (version_compare(PHP_VERSION, '5.0','<')) array_push($errors,'phpversion','php5');
  264. if (version_compare(PHP_VERSION, '5.1.3','==')) array_push($errors,'phpversion','php513');
  265. // Check WordPress version
  266. if (version_compare(get_bloginfo('version'),'3.1','<'))
  267. array_push($errors,'wpversion','wp31');
  268. // Check for cURL
  269. $curl_func = array('curl_init','curl_setopt','curl_exec','curl_close');
  270. $curl_support = array_filter($curl_func,'function_exists');
  271. if (count($curl_func) != count($curl_support)) $errors[] = 'curl';
  272. // Check for GD
  273. if (!function_exists("gd_info")) $errors[] = 'gd';
  274. else if (!array_keys(gd_info(),array('JPG Support','JPEG Support'))) $errors[] = 'jpgsupport';
  275. if (empty($errors)) return (!defined('SHOPP_UNSUPPORTED')?define('SHOPP_UNSUPPORTED',false):true);
  276. $plugin_path = dirname(dirname(__FILE__));
  277. // Manually load text domain for translated activation errors
  278. $languages_path = str_replace('\\', '/', $plugin_path.'/lang');
  279. load_plugin_textdomain('Shopp',false,$languages_path);
  280. // Define translated messages
  281. $_ = array(
  282. 'header' => __('Shopp Activation Error','Shopp'),
  283. 'intro' => __('Sorry! Shopp cannot be activated for this WordPress install.'),
  284. 'phpversion' => sprintf(__('Your server is running PHP %s!','Shopp'),PHP_VERSION),
  285. 'php5' => __('Shopp requires PHP 5.0+.','Shopp'),
  286. 'php513' => __('Shopp will not work with PHP 5.1.3 because of a critical bug in that version.','Shopp'),
  287. 'wpversion' => sprintf(__('This site is running WordPress %s!','Shopp'),get_bloginfo('version')),
  288. 'wp31' => __('Shopp requires WordPress 3.1+.','Shopp'),
  289. 'curl' => __('Your server does not have cURL support available! Shopp requires the cURL library for server-to-server communication.','Shopp'),
  290. 'gdsupport' => __('Your server does not have GD support! Shopp requires the GD image library with JPEG support for generating gallery and thumbnail images.','Shopp'),
  291. 'jpgsupport' => __('Your server does not have JPEG support for the GD library! Shopp requires JPEG support in the GD image library to generate JPEG images.','Shopp'),
  292. 'nextstep' => sprintf(__('Try contacting your web hosting provider or server administrator to upgrade your server. For more information about the requirements for running Shopp, see the %sShopp Documentation%s','Shopp'),'<a href="'.SHOPP_DOCS.'Requirements">','</a>'),
  293. 'continue' => __('Return to Plugins page')
  294. );
  295. if ($activation) {
  296. $string = '<h1>'.$_['header'].'</h1><p>'.$_['intro'].'</h1></p><ul>';
  297. foreach ((array)$errors as $error) if (isset($_[$error])) $string .= "<li>{$_[$error]}</li>";
  298. $string .= '</ul><p>'.$_['nextstep'].'</p><p><a class="button" href="javascript:history.go(-1);">&larr; '.$_['continue'].'</a></p>';
  299. wp_die($string);
  300. }
  301. if (!function_exists('deactivate_plugins'))
  302. require( ABSPATH . 'wp-admin/includes/plugin.php' );
  303. $plugin = basename($plugin_path)."/Shopp.php";
  304. deactivate_plugins($plugin,true);
  305. $phperror = '';
  306. if ( is_array($errors) && ! empty($errors) ) {
  307. foreach ( $errors as $error ) {
  308. if ( isset($_[$error]) )
  309. $phperror .= $_[$error].' ';
  310. trigger_error($phperror,E_USER_WARNING);
  311. }
  312. }
  313. if (!defined('SHOPP_UNSUPPORTED'))
  314. define('SHOPP_UNSUPPORTED',true);
  315. }
  316. }
  317. shopp_prereqs(); // Check for Shopp requisite technologies for activation
  318. ?>