]> git.mxchange.org Git - friendica.git/blobdiff - mod/photos.php
applied coding convention:
[friendica.git] / mod / photos.php
index 6d3ab284658d0d4b1f2f4566fc7e7eefa887fa83..27d8499c2809699d655b375f745c55a36dcb9d12 100644 (file)
@@ -10,7 +10,9 @@ require_once('include/tags.php');
 require_once('include/threads.php');
 require_once('include/Probe.php');
 
-function photos_init(&$a) {
+use \Friendica\Core\Config;
+
+function photos_init(App $a) {
 
        if ($a->argc > 1)
                auto_redir($a, $a->argv[1]);
@@ -112,7 +114,7 @@ function photos_init(&$a) {
 
 
 
-function photos_post(&$a) {
+function photos_post(App $a) {
 
        logger('mod-photos: photos_post: begin' , LOGGER_DEBUG);
 
@@ -165,7 +167,7 @@ function photos_post(&$a) {
                intval($page_owner_uid)
        );
 
-       if (! count($r)) {
+       if (! dbm::is_result($r)) {
                notice( t('Contact information unavailable') . EOL);
                logger('photos_post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
                killme();
@@ -182,11 +184,11 @@ function photos_post(&$a) {
                        return; // NOTREACHED
                }
 
-               $r = qu("SELECT count(*) FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
+               $r = qu("SELECT `album` FROM `photo` WHERE `album` = '%s' AND `uid` = %d",
                        dbesc($album),
                        intval($page_owner_uid)
                );
-               if (! count($r)) {
+               if (!dbm::is_result($r)) {
                        notice( t('Album not found.') . EOL);
                        goaway($_SESSION['photo_return']);
                        return; // NOTREACHED
@@ -208,6 +210,9 @@ function photos_post(&$a) {
                                dbesc($album),
                                intval($page_owner_uid)
                        );
+                       // Update the photo albums cache
+                       photo_albums($page_owner_uid, true);
+
                        $newurl = str_replace(bin2hex($album),bin2hex($newalbum),$_SESSION['photo_return']);
                        goaway($newurl);
                        return; // NOTREACHED
@@ -294,7 +299,11 @@ function photos_post(&$a) {
                                                proc_run(PRIORITY_HIGH, "include/notifier.php", "drop", $drop_id);
                                }
                        }
+
+                       // Update the photo albums cache
+                       photo_albums($page_owner_uid, true);
                }
+
                goaway('photos/' . $a->data['user']['nickname']);
                return; // NOTREACHED
        }
@@ -346,7 +355,7 @@ function photos_post(&$a) {
                                dbesc($r[0]['resource-id']),
                                intval($page_owner_uid)
                        );
-                       if (count($i)) {
+                       if (dbm::is_result($i)) {
                                q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s' WHERE `parent-uri` = '%s' AND `uid` = %d",
                                        dbesc(datetime_convert()),
                                        dbesc(datetime_convert()),
@@ -356,9 +365,12 @@ function photos_post(&$a) {
                                create_tags_from_itemuri($i[0]['uri'], $page_owner_uid);
                                delete_thread_uri($i[0]['uri'], $page_owner_uid);
 
-                               $url = $a->get_baseurl();
+                               $url = App::get_baseurl();
                                $drop_id = intval($i[0]['id']);
 
+                               // Update the photo albums cache
+                               photo_albums($page_owner_uid, true);
+
                                if ($i[0]['visible'])
                                        proc_run(PRIORITY_HIGH, "include/notifier.php", "drop", $drop_id);
                        }
@@ -370,10 +382,11 @@ function photos_post(&$a) {
 
        if (($a->argc > 2) && ((x($_POST,'desc') !== false) || (x($_POST,'newtag') !== false)) || (x($_POST,'albname') !== false)) {
 
-               $desc        = ((x($_POST,'desc'))    ? notags(trim($_POST['desc']))    : '');
-               $rawtags     = ((x($_POST,'newtag'))  ? notags(trim($_POST['newtag']))  : '');
-               $item_id     = ((x($_POST,'item_id')) ? intval($_POST['item_id'])       : 0);
-               $albname     = ((x($_POST,'albname')) ? notags(trim($_POST['albname'])) : '');
+               $desc        = ((x($_POST,'desc'))      ? notags(trim($_POST['desc']))    : '');
+               $rawtags     = ((x($_POST,'newtag'))    ? notags(trim($_POST['newtag']))  : '');
+               $item_id     = ((x($_POST,'item_id'))   ? intval($_POST['item_id'])       : 0);
+               $albname     = ((x($_POST,'albname'))   ? notags(trim($_POST['albname'])) : '');
+               $origaname   = ((x($_POST,'origaname')) ? notags(trim($_POST['origaname'])) : '');
                $str_group_allow   = perms2str($_POST['group_allow']);
                $str_contact_allow = perms2str($_POST['contact_allow']);
                $str_group_deny    = perms2str($_POST['group_deny']);
@@ -457,6 +470,10 @@ function photos_post(&$a) {
                                dbesc($resource_id),
                                intval($page_owner_uid)
                        );
+                       // Update the photo albums cache if album name was changed
+                       if ($albname !== $origaname) {
+                               photo_albums($page_owner_uid, true);
+                       }
                }
 
                /* Don't make the item visible if the only change was the album name */
@@ -496,8 +513,8 @@ function photos_post(&$a) {
                        $arr['visible']       = $visibility;
                        $arr['origin']        = 1;
 
-                       $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']'
-                                               . '[img]' . $a->get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.'. $ext . '[/img]'
+                       $arr['body']          = '[url=' . App::get_baseurl() . '/photos/' . $a->data['user']['nickname'] . '/image/' . $p[0]['resource-id'] . ']'
+                                               . '[img]' . App::get_baseurl() . '/photo/' . $p[0]['resource-id'] . '-' . $p[0]['scale'] . '.'. $ext . '[/img]'
                                                . '[/url]';
 
                        $item_id = item_store($arr);
@@ -615,7 +632,7 @@ function photos_post(&$a) {
                                                }
                                        } elseif (strpos($tag,'#') === 0) {
                                                $tagname = substr($tag, 1);
-                                               $str_tags .= '#[url='.$a->get_baseurl()."/search?tag=".$tagname.']'.$tagname.'[/url]';
+                                               $str_tags .= '#[url='.App::get_baseurl()."/search?tag=".$tagname.']'.$tagname.'[/url]';
                                        }
                                }
                        }
@@ -685,8 +702,8 @@ function photos_post(&$a) {
                                        $arr['tag']           = $tagged[4];
                                        $arr['inform']        = $tagged[2];
                                        $arr['origin']        = 1;
-                                       $arr['body']          = sprintf( t('%1$s was tagged in %2$s by %3$s'), '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]', '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . t('a photo') . '[/url]', '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]') ;
-                                       $arr['body'] .= "\n\n" . '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '[/img][/url]' . "\n" ;
+                                       $arr['body']          = sprintf( t('%1$s was tagged in %2$s by %3$s'), '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]', '[url=' . App::get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . t('a photo') . '[/url]', '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]') ;
+                                       $arr['body'] .= "\n\n" . '[url=' . App::get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . App::get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '[/img][/url]' . "\n" ;
 
                                        $arr['object'] = '<object><type>' . ACTIVITY_OBJ_PERSON . '</type><title>' . $tagged[0] . '</title><id>' . $tagged[1] . '/' . $tagged[0] . '</id>';
                                        $arr['object'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $tagged[1] . '" />' . "\n");
@@ -695,8 +712,8 @@ function photos_post(&$a) {
                                        $arr['object'] .= '</link></object>' . "\n";
 
                                        $arr['target'] = '<target><type>' . ACTIVITY_OBJ_IMAGE . '</type><title>' . $p[0]['desc'] . '</title><id>'
-                                               . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
-                                       $arr['target'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="'.$p[0]['type'].'" href="' . $a->get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>';
+                                               . App::get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '</id>';
+                                       $arr['target'] .= '<link>' . xmlify('<link rel="alternate" type="text/html" href="' . App::get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . '" />' . "\n" . '<link rel="preview" type="'.$p[0]['type'].'" href="' . App::get_baseurl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '" />') . '</link></target>';
 
                                        $item_id = item_store($arr);
                                        if ($item_id) {
@@ -748,7 +765,7 @@ function photos_post(&$a) {
                dbesc($album),
                intval($page_owner_uid)
        );
-       if ((! count($r)) || ($album == t('Profile Photos')))
+       if ((! dbm::is_result($r)) || ($album == t('Profile Photos')))
                $visible = 1;
        else
                $visible = 0;
@@ -803,22 +820,23 @@ function photos_post(&$a) {
        $imagedata = @file_get_contents($src);
 
 
-
-       $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
-               intval($a->data['user']['uid'])
-       );
-
        $limit = service_class_fetch($a->data['user']['uid'],'photo_upload_limit');
 
-       if (($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
-               notice( upgrade_message() . EOL );
-               @unlink($src);
-               $foo = 0;
-               call_hooks('photo_post_end',$foo);
-               killme();
+       if ($limit) {
+               $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
+                       intval($a->data['user']['uid'])
+               );
+               $size = $r[0]['total'];
+
+               if (($size + strlen($imagedata)) > $limit) {
+                       notice( upgrade_message() . EOL );
+                       @unlink($src);
+                       $foo = 0;
+                       call_hooks('photo_post_end',$foo);
+                       killme();
+               }
        }
 
-
        $ph = new Photo($imagedata, $type);
 
        if (! $ph->is_valid()) {
@@ -908,11 +926,13 @@ function photos_post(&$a) {
        $arr['visible']       = $visible;
        $arr['origin']        = 1;
 
-       $arr['body']          = '[url=' . $a->get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']'
-                               . '[img]' . $a->get_baseurl() . "/photo/{$photo_hash}-{$smallest}.".$ph->getExt() . '[/img]'
+       $arr['body']          = '[url=' . App::get_baseurl() . '/photos/' . $owner_record['nickname'] . '/image/' . $photo_hash . ']'
+                               . '[img]' . App::get_baseurl() . "/photo/{$photo_hash}-{$smallest}.".$ph->getExt() . '[/img]'
                                . '[/url]';
 
        $item_id = item_store($arr);
+       // Update the photo albums cache
+       photo_albums($page_owner_uid, true);
 
        if ($visible)
                proc_run(PRIORITY_HIGH, "include/notifier.php", 'wall-new', $item_id);
@@ -928,7 +948,7 @@ function photos_post(&$a) {
 
 
 
-function photos_content(&$a) {
+function photos_content(App $a) {
 
        // URLs:
        // photos/name
@@ -1290,7 +1310,7 @@ function photos_content(&$a) {
 
        }
 
-       /** 
+       /**
         * Display one photo
         */
 
@@ -1321,35 +1341,38 @@ function photos_content(&$a) {
                $prevlink = '';
                $nextlink = '';
 
-               if ($_GET['order'] === 'posted')
-                       $order = 'ASC';
-               else
-                       $order = 'DESC';
-
+               /// @todo This query is totally bad, the whole functionality has to be changed
+               // The query leads to a really intense used index.
+               // By now we hide it if someone wants to.
+               if (!Config::get('system', 'no_count', false)) {
+                       if ($_GET['order'] === 'posted')
+                               $order = 'ASC';
+                       else
+                               $order = 'DESC';
 
-               $prvnxt = qu("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
-                       $sql_extra ORDER BY `created` $order ",
-                       dbesc($ph[0]['album']),
-                       intval($owner_uid)
-               );
+                       $prvnxt = qu("SELECT `resource-id` FROM `photo` WHERE `album` = '%s' AND `uid` = %d AND `scale` = 0
+                               $sql_extra ORDER BY `created` $order ",
+                               dbesc($ph[0]['album']),
+                               intval($owner_uid)
+                       );
 
-               if (count($prvnxt)) {
-                       for($z = 0; $z < count($prvnxt); $z++) {
-                               if ($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
-                                       $prv = $z - 1;
-                                       $nxt = $z + 1;
-                                       if ($prv < 0)
-                                               $prv = count($prvnxt) - 1;
-                                       if ($nxt >= count($prvnxt))
-                                               $nxt = 0;
-                                       break;
+                       if (count($prvnxt)) {
+                               for($z = 0; $z < count($prvnxt); $z++) {
+                                       if ($prvnxt[$z]['resource-id'] == $ph[0]['resource-id']) {
+                                               $prv = $z - 1;
+                                               $nxt = $z + 1;
+                                               if ($prv < 0)
+                                                       $prv = count($prvnxt) - 1;
+                                               if ($nxt >= count($prvnxt))
+                                                       $nxt = 0;
+                                               break;
+                                       }
                                }
-                       }
-                       $edit_suffix = ((($cmd === 'edit') && ($can_post)) ? '/edit' : '');
-                       $prevlink = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
-                       $nextlink = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
-               }
-
+                               $edit_suffix = ((($cmd === 'edit') && ($can_post)) ? '/edit' : '');
+                               $prevlink = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$prv]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
+                               $nextlink = 'photos/' . $a->data['user']['nickname'] . '/image/' . $prvnxt[$nxt]['resource-id'] . $edit_suffix . (($_GET['order'] === 'posted') ? '?f=&order=posted' : '');
+                       }
+               }
 
                if (count($ph) == 1)
                        $hires = $lores = $ph[0];
@@ -1566,14 +1589,13 @@ function photos_content(&$a) {
                                        '$id' => $link_item['id'],
                                        '$likethis' => t("I like this \x28toggle\x29"),
                                        '$nolike' => (feature_enabled(local_user(), 'dislike') ? t("I don't like this \x28toggle\x29") : ''),
-                                       '$share' => t('Share'),
                                        '$wait' => t('Please wait'),
                                        '$return_path' => $a->query_string,
                                ));
                        }
 
                        $comments = '';
-                       if (! count($r)) {
+                       if (! dbm::is_result($r)) {
                                if ($can_post || can_write_wall($a,$owner_uid)) {
                                        if ($link_item['last-child']) {
                                                $comments .= replace_macros($cmnt_tpl,array(
@@ -1809,8 +1831,6 @@ function photos_content(&$a) {
                intval($a->pager['itemspage'])
        );
 
-
-
        $photos = array();
        if (dbm::is_result($r)) {
                $twist = 'rotright';