| 1 |
<?php |
| 2 |
class MaxGalleriaCommon { |
| 3 |
public function format_seconds_to_time($seconds) { |
| 4 |
$h = (int)($seconds / 3600); |
| 5 |
$m = (int)(($seconds - $h * 3600) / 60); |
| 6 |
$s = (int)($seconds - $h * 3600 - $m * 60); |
| 7 |
|
| 8 |
return (($h) ? (($h < 10) ? ("0" . $h) : $h) : "00") . ":" . (($m) ? (($m < 10) ? ("0" . $m) : $m) : "00") . ":" . (($s) ? (($s < 10) ? ("0" . $s) : $s) : "00"); |
| 9 |
} |
| 10 |
|
| 11 |
public function get_api_url() { |
| 12 |
if ($this->url_contains('localhost')) { |
| 13 |
return 'http://localhost/maxgalleria/api/api.php'; |
| 14 |
} |
| 15 |
|
| 16 |
return 'http://maxgalleria.com/api/api.php'; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_browser() { |
| 20 |
// http://www.php.net/manual/en/function.get-browser.php#101125. |
| 21 |
// Cleaned up a bit, but overall it's the same. |
| 22 |
|
| 23 |
$user_agent = $_SERVER['HTTP_USER_AGENT']; |
| 24 |
$browser_name = 'Unknown'; |
| 25 |
$platform = 'Unknown'; |
| 26 |
$version= ""; |
| 27 |
|
| 28 |
// First get the platform |
| 29 |
if (preg_match('/linux/i', $user_agent)) { |
| 30 |
$platform = 'Linux'; |
| 31 |
} |
| 32 |
elseif (preg_match('/macintosh|mac os x/i', $user_agent)) { |
| 33 |
$platform = 'Mac'; |
| 34 |
} |
| 35 |
elseif (preg_match('/windows|win32/i', $user_agent)) { |
| 36 |
$platform = 'Windows'; |
| 37 |
} |
| 38 |
|
| 39 |
// Next get the name of the user agent yes seperately and for good reason |
| 40 |
if (preg_match('/MSIE/i', $user_agent) && !preg_match('/Opera/i', $user_agent)) { |
| 41 |
$browser_name = 'Internet Explorer'; |
| 42 |
$browser_name_short = "MSIE"; |
| 43 |
} |
| 44 |
elseif (preg_match('/Firefox/i', $user_agent)) { |
| 45 |
$browser_name = 'Mozilla Firefox'; |
| 46 |
$browser_name_short = "Firefox"; |
| 47 |
} |
| 48 |
elseif (preg_match('/Chrome/i', $user_agent)) { |
| 49 |
$browser_name = 'Google Chrome'; |
| 50 |
$browser_name_short = "Chrome"; |
| 51 |
} |
| 52 |
elseif (preg_match('/Safari/i', $user_agent)) { |
| 53 |
$browser_name = 'Apple Safari'; |
| 54 |
$browser_name_short = "Safari"; |
| 55 |
} |
| 56 |
elseif (preg_match('/Opera/i', $user_agent)) { |
| 57 |
$browser_name = 'Opera'; |
| 58 |
$browser_name_short = "Opera"; |
| 59 |
} |
| 60 |
elseif (preg_match('/Netscape/i', $user_agent)) { |
| 61 |
$browser_name = 'Netscape'; |
| 62 |
$browser_name_short = "Netscape"; |
| 63 |
} |
| 64 |
|
| 65 |
// Finally get the correct version number |
| 66 |
$known = array('Version', $browser_name_short, 'other'); |
| 67 |
$pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#'; |
| 68 |
if (!preg_match_all($pattern, $user_agent, $matches)) { |
| 69 |
// We have no matching number just continue |
| 70 |
} |
| 71 |
|
| 72 |
// See how many we have |
| 73 |
$i = count($matches['browser']); |
| 74 |
if ($i != 1) { |
| 75 |
// We will have two since we are not using 'other' argument yet |
| 76 |
// See if version is before or after the name |
| 77 |
if (strripos($user_agent, "Version") < strripos($user_agent, $browser_name_short)){ |
| 78 |
$version= $matches['version'][0]; |
| 79 |
} |
| 80 |
else { |
| 81 |
$version= $matches['version'][1]; |
| 82 |
} |
| 83 |
} |
| 84 |
else { |
| 85 |
$version= $matches['version'][0]; |
| 86 |
} |
| 87 |
|
| 88 |
// Check if we have a number |
| 89 |
if ($version == null || $version == "") { $version = "?"; } |
| 90 |
|
| 91 |
return array( |
| 92 |
'user_agent' => $user_agent, |
| 93 |
'name' => $browser_name, |
| 94 |
'version' => $version, |
| 95 |
'platform' => $platform, |
| 96 |
'pattern' => $pattern |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
public function get_cart_url() { |
| 101 |
if ($this->url_contains('localhost')) { |
| 102 |
return 'http://localhost/maxgalleria/shop/cart/'; |
| 103 |
} |
| 104 |
|
| 105 |
return 'http://maxgalleria.com/shop/cart/'; |
| 106 |
} |
| 107 |
|
| 108 |
public function get_next_menu_order($gallery_id) { |
| 109 |
global $wpdb; |
| 110 |
$menu_order = 0; |
| 111 |
|
| 112 |
$highest_ordered_image = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = %d ORDER BY menu_order DESC LIMIT 1", $gallery_id)); |
| 113 |
|
| 114 |
if (isset($highest_ordered_image)) { |
| 115 |
// We already have images in the gallery, so increment |
| 116 |
$menu_order = $highest_ordered_image->menu_order + 1; |
| 117 |
} |
| 118 |
else { |
| 119 |
// This is the first image in the gallery, so start at 1. |
| 120 |
$menu_order = 1; |
| 121 |
} |
| 122 |
|
| 123 |
return $menu_order; |
| 124 |
} |
| 125 |
|
| 126 |
public function get_url() { |
| 127 |
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://'; |
| 128 |
$url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 129 |
return $url; |
| 130 |
} |
| 131 |
|
| 132 |
public function log_me($message) { |
| 133 |
if (WP_DEBUG === true) { |
| 134 |
if (is_array($message) || is_object($message)) { |
| 135 |
error_log(print_r($message, true)); |
| 136 |
} else { |
| 137 |
error_log($message); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public function string_contains($haystack, $needle) { |
| 143 |
// Notice the use of the === operator |
| 144 |
if (strpos($haystack, $needle) === false) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
public function string_contains_embeddable_element($string) { |
| 152 |
if ($this->string_contains($string, '<iframe') || |
| 153 |
$this->string_contains($string, '<object') || |
| 154 |
$this->string_contains($string, '<embed')) { |
| 155 |
return true; |
| 156 |
} |
| 157 |
else { |
| 158 |
return false; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public function string_starts_with($haystack, $needle) { |
| 163 |
// Notice the use of the === operator |
| 164 |
$length = strlen($needle); |
| 165 |
return (substr($haystack, 0, $length) === $needle); |
| 166 |
} |
| 167 |
|
| 168 |
public function string_ends_with($haystack, $needle) { |
| 169 |
// Notice the use of the === operator |
| 170 |
$length = strlen($needle); |
| 171 |
return (substr($haystack, -$length) === $needle); |
| 172 |
} |
| 173 |
|
| 174 |
public function url_matches_patterns($url, $patterns) { |
| 175 |
foreach ($patterns as $regex) { |
| 176 |
if (preg_match($regex, $url) == 1) { |
| 177 |
return true; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
public function url_contains($string) { |
| 185 |
$url = $this->get_url(); |
| 186 |
return $this->string_contains($url, $string); |
| 187 |
} |
| 188 |
} |
| 189 |
?> |