+
+/* Following functions are copied from MediaWiki GlobalFunctions.php
+ * and written by Evan Prodromou. */
+
+function common_accept_to_prefs($accept, $def = '*/*') {
+ # No arg means accept anything (per HTTP spec)
+ if(!$accept) {
+ return array($def => 1);
+ }
+
+ $prefs = array();
+
+ $parts = explode(',', $accept);
+
+ foreach($parts as $part) {
+ # FIXME: doesn't deal with params like 'text/html; level=1'
+ @list($value, $qpart) = explode(';', $part);
+ $match = array();
+ if(!isset($qpart)) {
+ $prefs[$value] = 1;
+ } elseif(preg_match('/q\s*=\s*(\d*\.\d+)/', $qpart, $match)) {
+ $prefs[$value] = $match[1];
+ }
+ }
+
+ return $prefs;
+}
+
+function common_mime_type_match($type, $avail) {
+ if(array_key_exists($type, $avail)) {
+ return $type;
+ } else {
+ $parts = explode('/', $type);
+ if(array_key_exists($parts[0] . '/*', $avail)) {
+ return $parts[0] . '/*';
+ } elseif(array_key_exists('*/*', $avail)) {
+ return '*/*';
+ } else {
+ return NULL;
+ }
+ }
+}
+
+function common_negotiate_type($cprefs, $sprefs) {
+ $combine = array();
+
+ foreach(array_keys($sprefs) as $type) {
+ $parts = explode('/', $type);
+ if($parts[1] != '*') {
+ $ckey = common_mime_type_match($type, $cprefs);
+ if($ckey) {
+ $combine[$type] = $sprefs[$type] * $cprefs[$ckey];
+ }
+ }
+ }
+
+ foreach(array_keys($cprefs) as $type) {
+ $parts = explode('/', $type);
+ if($parts[1] != '*' && !array_key_exists($type, $sprefs)) {
+ $skey = common_mime_type_match($type, $sprefs);
+ if($skey) {
+ $combine[$type] = $sprefs[$skey] * $cprefs[$type];
+ }
+ }
+ }
+
+ $bestq = 0;
+ $besttype = "text/html";
+
+ foreach(array_keys($combine) as $type) {
+ if($combine[$type] > $bestq) {
+ $besttype = $type;
+ $bestq = $combine[$type];
+ }
+ }
+
+ return $besttype;
+}
+
+function common_config($main, $sub) {
+ global $config;
+ return $config[$main][$sub];
+}
+
+function common_copy_args($from) {
+ $to = array();
+ $strip = get_magic_quotes_gpc();
+ foreach ($from as $k => $v) {
+ $to[$k] = ($strip) ? stripslashes($v) : $v;
+ }
+ return $to;
+}
+
+function common_user_uri(&$user) {
+ return common_local_url('userbyid', array('id' => $user->id));
+}
+
+function common_notice_uri(&$notice) {
+ return common_local_url('shownotice',
+ array('notice' => $notice->id));
+}
+
+# 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits
+
+function common_confirmation_code($bits) {
+ # 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits
+ static $codechars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
+ $chars = ceil($bits/5);
+ $code = '';
+ for ($i = 0; $i < $chars; $i++) {
+ # XXX: convert to string and back
+ $num = hexdec(common_good_rand(1));
+ # XXX: randomness is too precious to throw away almost
+ # 40% of the bits we get!
+ $code .= $codechars[$num%32];
+ }
+ return $code;
+}
+
+# convert markup to HTML
+
+function common_markup_to_html($c) {
+ $c = preg_replace('/%%action.(\w+)%%/e', "common_local_url('\\1')", $c);
+ $c = preg_replace('/%%doc.(\w+)%%/e', "common_local_url('doc', array('title'=>'\\1'))", $c);
+ $c = preg_replace('/%%(\w+).(\w+)%%/e', 'common_config(\'\\1\', \'\\2\')', $c);
+ return Markdown($c);
+}
+
+function common_profile_avatar_url($profile, $size=AVATAR_PROFILE_SIZE) {
+ $avatar = $profile->getAvatar($size);
+ if ($avatar) {
+ return common_avatar_display_url($avatar);
+ } else {
+ return common_default_avatar($size);
+ }
+}
+
+function common_profile_uri($profile) {
+ if (!$profile) {
+ return NULL;
+ }
+ $user = User::staticGet($profile->id);
+ if ($user) {
+ return $user->uri;
+ }
+
+ $remote = Remote_profile::staticGet($profile->id);
+ if ($remote) {
+ return $remote->uri;
+ }
+ # XXX: this is a very bad profile!
+ return NULL;
+}
\ No newline at end of file