]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
Merge request from chimo adding getvaliddaemons to stopdaemons.php
[quix0rs-gnu-social.git] / lib / util.php
index 7b75a467843f1c56452c3d1218c76cd166000712..fd89bb491cd44d2e3351854ae16654029b783ae1 100644 (file)
@@ -635,17 +635,13 @@ function common_linkify_mention($mention)
         $xs = new XMLStringer(false);
 
         $attrs = array('href' => $mention['url'],
-                       'class' => 'url');
+                       'class' => 'h-card '.$mention['type']);
 
         if (!empty($mention['title'])) {
             $attrs['title'] = $mention['title'];
         }
 
-        $xs->elementStart('span', 'vcard');
-        $xs->elementStart('a', $attrs);
-        $xs->element('span', 'fn nickname '.$mention['type'], $mention['text']);
-        $xs->elementEnd('a');
-        $xs->elementEnd('span');
+        $xs->element('a', $attrs, $mention['text']);
 
         $output = $xs->getString();
 
@@ -832,7 +828,7 @@ function common_find_mentions_raw($text)
 
 function common_render_text($text)
 {
-    $r = htmlspecialchars($text);
+    $r = nl2br(htmlspecialchars($text));
 
     $r = preg_replace('/[\x{0}-\x{8}\x{b}-\x{c}\x{e}-\x{19}]/', '', $r);
     $r = common_replace_urls_callback($r, 'common_linkify');
@@ -951,24 +947,7 @@ function callback_helper($matches, $callback, $arg=null) {
     return substr($matches[0],0,$left) . $result . substr($matches[0],$right);
 }
 
-if (version_compare(PHP_VERSION, '5.3.0', 'ge')) {
-    // lambda implementation in a separate file; PHP 5.2 won't parse it.
-    require_once INSTALLDIR . "/lib/curry.php";
-} else {
-    function curry($fn) {
-        $args = func_get_args();
-        array_shift($args);
-        $id = uniqid('_partial');
-        $GLOBALS[$id] = array($fn, $args);
-        return create_function('',
-                               '$args = func_get_args(); '.
-                               'return call_user_func_array('.
-                               '$GLOBALS["'.$id.'"][0],'.
-                               'array_merge('.
-                               '$args,'.
-                               '$GLOBALS["'.$id.'"][1]));');
-    }
-}
+require_once INSTALLDIR . "/lib/curry.php";
 
 function common_linkify($url) {
     // It comes in special'd, so we unspecial it before passing to the stringifying
@@ -1006,22 +985,27 @@ function common_linkify($url) {
 
     $f = File::getKV('url', $longurl);
 
-    if (empty($f)) {
+    if (!$f instanceof File) {
         if (common_config('attachments', 'process_links')) {
             // XXX: this writes to the database. :<
-            $f = File::processNew($longurl);
+            try {
+                $f = File::processNew($longurl);
+            } catch (ServerException $e) {
+                $f = null;
+            }
         }
     }
 
-    if (!empty($f)) {
-        if ($f->getEnclosure()) {
+    if ($f instanceof File) {
+        try {
+            $enclosure = $f->getEnclosure();
             $is_attachment = true;
             $attachment_id = $f->id;
 
             $thumb = File_thumbnail::getKV('file_id', $f->id);
-            if (!empty($thumb)) {
-                $has_thumb = true;
-            }
+            $has_thumb = ($thumb instanceof File_thumbnail);
+        } catch (ServerException $e) {
+            // There was not enough metadata available
         }
     }
 
@@ -1393,7 +1377,8 @@ function common_exact_date($dt)
     $dateStr = date('d F Y H:i:s', strtotime($dt));
     $d = new DateTime($dateStr, $_utc);
     $d->setTimezone($_siteTz);
-    return $d->format(DATE_RFC850);
+    // TRANS: Human-readable full date-time specification (formatting on http://php.net/date)
+    return $d->format(_('l, d-M-Y H:i:s T'));
 }
 
 function common_date_w3dtf($dt)
@@ -1797,6 +1782,44 @@ function common_accept_to_prefs($accept, $def = '*/*')
     return $prefs;
 }
 
+// Match by our supported file extensions
+function common_supported_ext_to_mime($fileext)
+{
+    // Accept a filename and take out the extension
+    if (strpos($fileext, '.') !== false) {
+        $fileext = substr(strrchr($fileext, '.'), 1);
+    }
+
+    $supported = common_config('attachments', 'supported');
+    foreach($supported as $type => $ext) {
+        if ($ext === $fileext) {
+            return $type;
+        }
+    }
+
+    throw new ServerException('Unsupported file extension');
+}
+
+// Match by our supported mime types
+function common_supported_mime_to_ext($mimetype)
+{
+    $supported = common_config('attachments', 'supported');
+    foreach($supported as $type => $ext) {
+        if ($mimetype === $type) {
+            return $ext;
+        }
+    }
+
+    throw new ServerException('Unsupported MIME type');
+}
+
+// The MIME "media" is the part before the slash (video in video/webm)
+function common_get_mime_media($type)
+{
+    $tmp = explode('/', $type);
+    return strtolower($tmp[0]);
+}
+
 function common_mime_type_match($type, $avail)
 {
     if(array_key_exists($type, $avail)) {
@@ -1928,13 +1951,6 @@ function common_user_uri(&$user)
                             null, null, false);
 }
 
-function common_notice_uri(&$notice)
-{
-    return common_local_url('shownotice',
-                            array('notice' => $notice->id),
-                            null, null, false);
-}
-
 // 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits
 
 function common_confirmation_code($bits)
@@ -1954,7 +1970,6 @@ function common_confirmation_code($bits)
 }
 
 // convert markup to HTML
-
 function common_markup_to_html($c, $args=null)
 {
     if ($c === null) {
@@ -1975,7 +1990,8 @@ function common_markup_to_html($c, $args=null)
     $c = preg_replace_callback('/%%action.(\w+)%%/', function ($m) { return common_local_url($m[1]); }, $c);
     $c = preg_replace_callback('/%%doc.(\w+)%%/', function ($m) { return common_local_url('doc', array('title'=>$m[1])); }, $c);
     $c = preg_replace_callback('/%%(\w+).(\w+)%%/', function ($m) { return common_config($m[1], $m[2]); }, $c);
-    return Markdown($c);
+
+    return \Michelf\Markdown::defaultTransform($c);
 }
 
 function common_user_property($property)
@@ -2017,7 +2033,7 @@ function common_profile_uri($profile)
         if (Event::handle('StartCommonProfileURI', array($profile, &$uri))) {
             $user = User::getKV('id', $profile->id);
             if ($user instanceof User) {
-                $uri = $user->uri;
+                $uri = $user->getUri();
             }
             Event::handle('EndCommonProfileURI', array($profile, &$uri));
         }
@@ -2154,17 +2170,16 @@ function common_shorten_url($long_url, User $user=null, $force = false)
     if (Event::handle('StartShortenUrl',
                       array($long_url, $shortenerName, &$shortenedUrl))) {
         if ($shortenerName == 'internal') {
-            $f = File::processNew($long_url);
-            if (empty($f)) {
-                return $long_url;
-            } else {
-                $shortenedUrl = common_local_url('redirecturl',
-                                                 array('id' => $f->id));
+            try {
+                $f = File::processNew($long_url);
+                $shortenedUrl = common_local_url('redirecturl', array('id' => $f->id));
                 if ((mb_strlen($shortenedUrl) < mb_strlen($long_url)) || $force) {
                     return $shortenedUrl;
                 } else {
                     return $long_url;
                 }
+            } catch (ServerException $e) {
+                return $long_url;
             }
         } else {
             return $long_url;
@@ -2345,3 +2360,12 @@ function common_log_delta($comment=null)
 
     common_debug(sprintf("%s: %d %d", $comment, $mtotal, round($ttotal * 1000000)));
 }
+
+function common_strip_html($html, $trim=true, $save_whitespace=false)
+{
+    if (!$save_whitespace) {
+        $html = preg_replace('/\s+/', ' ', $html);
+    }
+    $text = html_entity_decode(strip_tags($html), ENT_QUOTES, 'UTF-8');
+    return $trim ? trim($text) : $text;
+}