예제 #1
0
/**
 * A lot going on in this function, and some of it is old cruft and some is new cruft
 * and the entire thing probably needs to be refactored. It started out just storing
 * files, before we had DAV. It was made extensible to do extra stuff like edit an 
 * existing file or optionally store a separate revision using $options to choose between different
 * storage models. Along the way we moved from
 * DB data storage to file system storage. 
 * Then DAV came along and used different upload methods depending on whether the 
 * file was stored as a DAV directory object or updated as a file object. One of these 
 * is essentially an update and the other is basically an upload, but doesn't use the traditional PHP
 * upload workflow. 
 * Then came hubzilla and we tried to merge photo functionality with the file storage. Most of
 * that integration occurs within this function. 
 * This required overlap with the old photo_upload stuff and photo albums were
 * completely different concepts from directories which needed to be reconciled somehow.
 * The old revision stuff is kind of orphaned currently. There's new revision stuff for photos
 * which attaches (2) etc. onto the name, but doesn't integrate with the attach table revisioning.
 * That's where it sits currently. I repeat it needs to be refactored, and this note is here
 * for future explorers and those who may be doing that work to understand where it came
 * from and got to be the monstrosity of tangled unrelated code that it currently is.
 */
function attach_store($channel, $observer_hash, $options = '', $arr = null)
{
    require_once 'include/photos.php';
    call_hooks('photo_upload_begin', $arr);
    $ret = array('success' => false);
    $channel_id = $channel['channel_id'];
    $sql_options = '';
    $source = $arr ? $arr['source'] : '';
    $album = $arr ? $arr['album'] : '';
    $newalbum = $arr ? $arr['newalbum'] : '';
    $hash = $arr && $arr['hash'] ? $arr['hash'] : null;
    $upload_path = $arr && $arr['directory'] ? $arr['directory'] : '';
    $visible = $arr && $arr['visible'] ? $arr['visible'] : '';
    $observer = array();
    $dosync = array_key_exists('nosync', $arr) && $arr['nosync'] ? 0 : 1;
    if ($observer_hash) {
        $x = q("select * from xchan where xchan_hash = '%s' limit 1", dbesc($observer_hash));
        if ($x) {
            $observer = $x[0];
        }
    }
    logger('arr: ' . print_r($arr, true), LOGGER_DATA);
    if (!perm_is_allowed($channel_id, $observer_hash, 'write_storage')) {
        $ret['message'] = t('Permission denied.');
        return $ret;
    }
    $str_group_allow = perms2str($arr['group_allow']);
    $str_contact_allow = perms2str($arr['contact_allow']);
    $str_group_deny = perms2str($arr['group_deny']);
    $str_contact_deny = perms2str($arr['contact_deny']);
    // The 'update' option sets db values without uploading a new attachment
    // 'replace' replaces the existing uploaded data
    // 'revision' creates a new revision with new upload data
    // Default is to upload a new file
    // revise or update must provide $arr['hash'] of the thing to revise/update
    // By default remove $src when finished
    $remove_when_processed = true;
    if ($options === 'import') {
        $src = $arr['src'];
        $filename = $arr['filename'];
        $filesize = @filesize($src);
        $hash = $arr['resource_id'];
        if (array_key_exists('hash', $arr)) {
            $hash = $arr['hash'];
        }
        if (array_key_exists('type', $arr)) {
            $type = $arr['type'];
        }
        if ($arr['preserve_original']) {
            $remove_when_processed = false;
        }
        // if importing a directory, just do it now and go home - we're done.
        if (array_key_exists('is_dir', $arr) && intval($arr['is_dir'])) {
            $x = attach_mkdir($channel, $observer_hash, $arr);
            if ($x['message']) {
                logger('import_directory: ' . $x['message']);
            }
            return;
        }
    } elseif ($options !== 'update') {
        $f = array('src' => '', 'filename' => '', 'filesize' => 0, 'type' => '');
        call_hooks('photo_upload_file', $f);
        call_hooks('attach_upload_file', $f);
        if (x($f, 'src') && x($f, 'filesize')) {
            $src = $f['src'];
            $filename = $f['filename'];
            $filesize = $f['filesize'];
            $type = $f['type'];
        } else {
            if (!x($_FILES, 'userfile')) {
                $ret['message'] = t('No source file.');
                return $ret;
            }
            $src = $_FILES['userfile']['tmp_name'];
            $filename = basename($_FILES['userfile']['name']);
            $filesize = intval($_FILES['userfile']['size']);
        }
    }
    // AndStatus sends jpegs with a non-standard mimetype
    if ($type === 'image/jpg') {
        $type = 'image/jpeg';
    }
    $existing_size = 0;
    if ($options === 'replace') {
        $x = q("select id, hash, filesize from attach where id = %d and uid = %d limit 1", intval($arr['id']), intval($channel_id));
        if (!$x) {
            $ret['message'] = t('Cannot locate file to replace');
            return $ret;
        }
        $existing_id = $x[0]['id'];
        $existing_size = intval($x[0]['filesize']);
        $hash = $x[0]['hash'];
    }
    if ($options === 'revise' || $options === 'update') {
        $sql_options = " order by revision desc ";
        if ($options === 'update' && $arr && array_key_exists('revision', $arr)) {
            $sql_options = " and revision = " . intval($arr['revision']) . " ";
        }
        $x = q("select id, aid, uid, filename, filetype, filesize, hash, revision, folder, os_storage, is_photo, flags, created, edited, allow_cid, allow_gid, deny_cid, deny_gid from attach where hash = '%s' and uid = %d {$sql_options} limit 1", dbesc($arr['hash']), intval($channel_id));
        if (!$x) {
            $ret['message'] = t('Cannot locate file to revise/update');
            return $ret;
        }
        $hash = $x[0]['hash'];
    }
    $def_extension = '';
    $is_photo = 0;
    $gis = @getimagesize($src);
    logger('getimagesize: ' . print_r($gis, true), LOGGER_DATA);
    if ($gis && ($gis[2] === IMAGETYPE_GIF || $gis[2] === IMAGETYPE_JPEG || $gis[2] === IMAGETYPE_PNG)) {
        $is_photo = 1;
        if ($gis[2] === IMAGETYPE_GIF) {
            $def_extension = '.gif';
        }
        if ($gis[2] === IMAGETYPE_JPEG) {
            $def_extension = '.jpg';
        }
        if ($gis[2] === IMAGETYPE_PNG) {
            $def_extension = '.png';
        }
    }
    $pathname = '';
    if ($is_photo) {
        if ($newalbum) {
            $pathname = filepath_macro($newalbum);
        } elseif (array_key_exists('folder', $arr)) {
            $x = q("select filename from attach where hash = '%s' and uid = %d limit 1", dbesc($arr['folder']), intval($channel['channel_id']));
            if ($x) {
                $pathname = $x[0]['filename'];
            }
        } else {
            $pathname = filepath_macro($album);
        }
    }
    if (!$pathname) {
        $pathname = filepath_macro($upload_path);
    }
    $darr = array('pathname' => $pathname);
    // if we need to create a directory, use the channel default permissions.
    $darr['allow_cid'] = $channel['allow_cid'];
    $darr['allow_gid'] = $channel['allow_gid'];
    $darr['deny_cid'] = $channel['deny_cid'];
    $darr['deny_gid'] = $channel['deny_gid'];
    $direct = null;
    if ($pathname) {
        $x = attach_mkdirp($channel, $observer_hash, $darr);
        $folder_hash = $x['success'] ? $x['data']['hash'] : '';
        $direct = $x['success'] ? $x['data'] : null;
        if (!$str_contact_allow && !$str_group_allow && !$str_contact_deny && !$str_group_deny) {
            $str_contact_allow = $x['data']['allow_cid'];
            $str_group_allow = $x['data']['allow_gid'];
            $str_contact_deny = $x['data']['deny_cid'];
            $str_group_deny = $x['data']['deny_gid'];
        }
    } else {
        $folder_hash = $arr && array_key_exists('folder', $arr) ? $arr['folder'] : '';
    }
    if (!$options || $options === 'import') {
        // A freshly uploaded file. Check for duplicate and resolve with the channel's overwrite settings.
        $r = q("select filename, id, hash, filesize from attach where filename = '%s' and folder = '%s' ", dbesc($filename), dbesc($folder_hash));
        if ($r) {
            $overwrite = get_pconfig($channel_id, 'system', 'overwrite_dup_files');
            if ($overwrite || $options === 'import') {
                $options = 'replace';
                $existing_id = $x[0]['id'];
                $existing_size = intval($x[0]['filesize']);
                $hash = $x[0]['hash'];
            } else {
                if (strpos($filename, '.') !== false) {
                    $basename = substr($filename, 0, strrpos($filename, '.'));
                    $ext = substr($filename, strrpos($filename, '.'));
                } else {
                    $basename = $filename;
                    $ext = $def_extension;
                }
                $r = q("select filename from attach where ( filename = '%s' OR filename like '%s' ) and folder = '%s' ", dbesc($basename . $ext), dbesc($basename . '(%)' . $ext), dbesc($folder_hash));
                if ($r) {
                    $x = 1;
                    do {
                        $found = false;
                        foreach ($r as $rr) {
                            if ($rr['filename'] === $basename . '(' . $x . ')' . $ext) {
                                $found = true;
                                break;
                            }
                        }
                        if ($found) {
                            $x++;
                        }
                    } while ($found);
                    $filename = $basename . '(' . $x . ')' . $ext;
                } else {
                    $filename = $basename . $ext;
                }
            }
        }
    }
    if (!$hash) {
        $hash = random_string();
    }
    // Check storage limits
    if ($options !== 'update') {
        $maxfilesize = get_config('system', 'maxfilesize');
        if ($maxfilesize && $filesize > $maxfilesize) {
            $ret['message'] = sprintf(t('File exceeds size limit of %d'), $maxfilesize);
            if ($remove_when_processed) {
                @unlink($src);
            }
            call_hooks('photo_upload_end', $ret);
            return $ret;
        }
        $limit = service_class_fetch($channel_id, 'attach_upload_limit');
        if ($limit !== false) {
            $r = q("select sum(filesize) as total from attach where aid = %d ", intval($channel['channel_account_id']));
            if ($r && $r[0]['total'] + $filesize > $limit - $existing_size) {
                $ret['message'] = upgrade_message(true) . sprintf(t("You have reached your limit of %1\$.0f Mbytes attachment storage."), $limit / 1024000);
                if ($remove_when_processed) {
                    @unlink($src);
                }
                call_hooks('photo_upload_end', $ret);
                return $ret;
            }
        }
        $mimetype = isset($type) && $type ? $type : z_mime_content_type($filename);
    }
    $os_basepath = 'store/' . $channel['channel_address'] . '/';
    $os_relpath = '';
    if ($folder_hash) {
        $curr = find_folder_hash_by_attach_hash($channel_id, $folder_hash, true);
        if ($curr) {
            $os_relpath .= $curr . '/';
        }
        $os_relpath .= $folder_hash . '/';
    }
    $os_relpath .= $hash;
    if ($src) {
        @file_put_contents($os_basepath . $os_relpath, @file_get_contents($src));
    }
    if (array_key_exists('created', $arr)) {
        $created = $arr['created'];
    } else {
        $created = datetime_convert();
    }
    if (array_key_exists('edited', $arr)) {
        $edited = $arr['edited'];
    } else {
        $edited = $created;
    }
    if ($options === 'replace') {
        $r = q("update attach set filename = '%s', filetype = '%s', folder = '%s', filesize = %d, os_storage = %d, is_photo = %d, content = '%s', edited = '%s' where id = %d and uid = %d", dbesc($filename), dbesc($mimetype), dbesc($folder_hash), intval($filesize), intval(1), intval($is_photo), dbesc($os_basepath . $os_relpath), dbesc($created), intval($existing_id), intval($channel_id));
    } elseif ($options === 'revise') {
        $r = q("insert into attach ( aid, uid, hash, creator, filename, filetype, folder, filesize, revision, os_storage, is_photo, content, created, edited, allow_cid, allow_gid, deny_cid, deny_gid )\n\t\t\tVALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ", intval($x[0]['aid']), intval($channel_id), dbesc($x[0]['hash']), dbesc($observer_hash), dbesc($filename), dbesc($mimetype), dbesc($folder_hash), intval($filesize), intval($x[0]['revision'] + 1), intval(1), intval($is_photo), dbesc($os_basepath . $os_relpath), dbesc($created), dbesc($created), dbesc($x[0]['allow_cid']), dbesc($x[0]['allow_gid']), dbesc($x[0]['deny_cid']), dbesc($x[0]['deny_gid']));
    } elseif ($options === 'update') {
        $r = q("update attach set filename = '%s', filetype = '%s', folder = '%s', edited = '%s', os_storage = %d, is_photo = %d, \n\t\t\tallow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid  = '%s' where id = %d and uid = %d", dbesc(array_key_exists('filename', $arr) ? $arr['filename'] : $x[0]['filename']), dbesc(array_key_exists('filetype', $arr) ? $arr['filetype'] : $x[0]['filetype']), dbesc($folder_hash ? $folder_hash : $x[0]['folder']), dbesc($created), dbesc(array_key_exists('os_storage', $arr) ? $arr['os_storage'] : $x[0]['os_storage']), dbesc(array_key_exists('is_photo', $arr) ? $arr['is_photo'] : $x[0]['is_photo']), dbesc(array_key_exists('allow_cid', $arr) ? $arr['allow_cid'] : $x[0]['allow_cid']), dbesc(array_key_exists('allow_gid', $arr) ? $arr['allow_gid'] : $x[0]['allow_gid']), dbesc(array_key_exists('deny_cid', $arr) ? $arr['deny_cid'] : $x[0]['deny_cid']), dbesc(array_key_exists('deny_gid', $arr) ? $arr['deny_gid'] : $x[0]['deny_gid']), intval($x[0]['id']), intval($x[0]['uid']));
    } else {
        $r = q("INSERT INTO attach ( aid, uid, hash, creator, filename, filetype, folder, filesize, revision, os_storage, is_photo, content, created, edited, allow_cid, allow_gid,deny_cid, deny_gid )\n\t\t\tVALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ", intval($channel['channel_account_id']), intval($channel_id), dbesc($hash), dbesc(get_observer_hash()), dbesc($filename), dbesc($mimetype), dbesc($folder_hash), intval($filesize), intval(0), intval(1), intval($is_photo), dbesc($os_basepath . $os_relpath), dbesc($created), dbesc($created), dbesc($arr && array_key_exists('allow_cid', $arr) ? $arr['allow_cid'] : $str_contact_allow), dbesc($arr && array_key_exists('allow_gid', $arr) ? $arr['allow_gid'] : $str_group_allow), dbesc($arr && array_key_exists('deny_cid', $arr) ? $arr['deny_cid'] : $str_contact_deny), dbesc($arr && array_key_exists('deny_gid', $arr) ? $arr['deny_gid'] : $str_group_deny));
    }
    if ($is_photo) {
        $args = array('source' => $source, 'visible' => $visible, 'resource_id' => $hash, 'album' => basename($pathname), 'os_path' => $os_basepath . $os_relpath, 'filename' => $filename, 'getimagesize' => $gis, 'directory' => $direct, 'options' => $options);
        if ($arr['contact_allow']) {
            $args['contact_allow'] = $arr['contact_allow'];
        }
        if ($arr['group_allow']) {
            $args['group_allow'] = $arr['group_allow'];
        }
        if ($arr['contact_deny']) {
            $args['contact_deny'] = $arr['contact_deny'];
        }
        if ($arr['group_deny']) {
            $args['group_deny'] = $arr['group_deny'];
        }
        if (array_key_exists('allow_cid', $arr)) {
            $args['allow_cid'] = $arr['allow_cid'];
        }
        if (array_key_exists('allow_gid', $arr)) {
            $args['allow_gid'] = $arr['allow_gid'];
        }
        if (array_key_exists('deny_cid', $arr)) {
            $args['deny_cid'] = $arr['deny_cid'];
        }
        if (array_key_exists('deny_gid', $arr)) {
            $args['deny_gid'] = $arr['deny_gid'];
        }
        $args['created'] = $created;
        $args['edited'] = $edited;
        if ($arr['item']) {
            $args['item'] = $arr['item'];
        }
        if ($arr['body']) {
            $args['body'] = $arr['body'];
        }
        if ($arr['description']) {
            $args['description'] = $arr['description'];
        }
        $args['deliver'] = $dosync;
        $p = photo_upload($channel, $observer, $args);
        if ($p['success']) {
            $ret['body'] = $p['body'];
        }
    }
    if ($options !== 'update' && $remove_when_processed) {
        @unlink($src);
    }
    if (!$r) {
        $ret['message'] = t('File upload failed. Possible system limit or action terminated.');
        call_hooks('photo_upload_end', $ret);
        return $ret;
    }
    // Caution: This re-uses $sql_options set further above
    $r = q("select * from attach where uid = %d and hash = '%s' {$sql_options} limit 1", intval($channel_id), dbesc($hash));
    if (!$r) {
        $ret['message'] = t('Stored file could not be verified. Upload failed.');
        call_hooks('photo_upload_end', $ret);
        return $ret;
    }
    $ret['success'] = true;
    $ret['data'] = $r[0];
    if (!$is_photo) {
        // This would've been called already with a success result in photos_upload() if it was a photo.
        call_hooks('photo_upload_end', $ret);
    }
    if ($dosync) {
        $sync = attach_export_data($channel, $hash);
        if ($sync) {
            build_sync_packet($channel['channel_id'], array('file' => array($sync)));
        }
    }
    return $ret;
}
예제 #2
0
/**
 * @brief Changes permissions of a file.
 *
 * @param int $channel_id
 * @param array $resource
 * @param string $allow_cid
 * @param string $allow_gid
 * @param string $deny_cid
 * @param string $deny_gid
 * @param boolean $recurse (optional) default false
 */
function attach_change_permissions($channel_id, $resource, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $recurse = false, $sync = false)
{
    $channel = channelx_by_n($channel_id);
    if (!$channel) {
        return;
    }
    $r = q("select hash, flags, is_dir, is_photo from attach where hash = '%s' and uid = %d limit 1", dbesc($resource), intval($channel_id));
    if (!$r) {
        return;
    }
    if (intval($r[0]['is_dir'])) {
        if ($recurse) {
            $r = q("select hash, flags, is_dir from attach where folder = '%s' and uid = %d", dbesc($resource), intval($channel_id));
            if ($r) {
                foreach ($r as $rr) {
                    attach_change_permissions($channel_id, $rr['hash'], $allow_cid, $allow_gid, $deny_cid, $deny_gid, $recurse, $sync);
                }
            }
        }
    }
    $x = q("update attach set allow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid = '%s' where hash = '%s' and uid = %d", dbesc($allow_cid), dbesc($allow_gid), dbesc($deny_cid), dbesc($deny_gid), dbesc($resource), intval($channel_id));
    if ($r[0]['is_photo']) {
        $x = q("update photo set allow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid = '%s' where resource_id = '%s' and uid = %d", dbesc($allow_cid), dbesc($allow_gid), dbesc($deny_cid), dbesc($deny_gid), dbesc($resource), intval($channel_id));
    }
    if ($sync) {
        $data = attach_export_data($channel, $resource);
        if ($data) {
            build_sync_packet($channel['channel_id'], array('file' => array($data)));
        }
    }
}
예제 #3
0
 function post()
 {
     logger('mod-photos: photos_post: begin', LOGGER_DEBUG);
     logger('mod_photos: REQUEST ' . print_r($_REQUEST, true), LOGGER_DATA);
     logger('mod_photos: FILES ' . print_r($_FILES, true), LOGGER_DATA);
     $ph = photo_factory('');
     $phototypes = $ph->supportedTypes();
     $can_post = false;
     $page_owner_uid = \App::$data['channel']['channel_id'];
     if (perm_is_allowed($page_owner_uid, get_observer_hash(), 'write_storage')) {
         $can_post = true;
     }
     if (!$can_post) {
         notice(t('Permission denied.') . EOL);
         if (is_ajax()) {
             killme();
         }
         return;
     }
     $s = abook_self($page_owner_uid);
     if (!$s) {
         notice(t('Page owner information could not be retrieved.') . EOL);
         logger('mod_photos: post: unable to locate contact record for page owner. uid=' . $page_owner_uid);
         if (is_ajax()) {
             killme();
         }
         return;
     }
     $owner_record = $s[0];
     $acl = new \Zotlabs\Access\AccessList(\App::$data['channel']);
     if (argc() > 3 && argv(2) === 'album') {
         $album = hex2bin(argv(3));
         if ($album === t('Profile Photos')) {
             // not allowed
             goaway(z_root() . '/' . $_SESSION['photo_return']);
         }
         if (!photos_album_exists($page_owner_uid, $album)) {
             notice(t('Album not found.') . EOL);
             goaway(z_root() . '/' . $_SESSION['photo_return']);
         }
         /*
          * DELETE photo album and all its photos
          */
         if ($_REQUEST['dropalbum'] == t('Delete Album')) {
             // This is dangerous because we combined file storage and photos into one interface
             // This function will remove all photos from any directory with the same name since
             // we have not passed the path value.
             // The correct solution would be to use a full pathname from your storage root for 'album'
             // We also need to prevent/block removing the storage root folder.
             $folder_hash = '';
             $r = q("select * from attach where is_dir = 1 and uid = %d and filename = '%s'", intval($page_owner_uid), dbesc($album));
             if (!$r) {
                 notice(t('Album not found.') . EOL);
                 return;
             }
             if (count($r) > 1) {
                 notice(t('Multiple storage folders exist with this album name, but within different directories. Please remove the desired folder or folders using the Files manager') . EOL);
                 return;
             } else {
                 $folder_hash = $r[0]['hash'];
             }
             $res = array();
             // get the list of photos we are about to delete
             if (remote_channel() && !local_channel()) {
                 $str = photos_album_get_db_idstr($page_owner_uid, $album, remote_channel());
             } elseif (local_channel()) {
                 $str = photos_album_get_db_idstr(local_channel(), $album);
             } else {
                 $str = null;
             }
             if (!$str) {
                 goaway(z_root() . '/' . $_SESSION['photo_return']);
             }
             $r = q("select id from item where resource_id in ( {$str} ) and resource_type = 'photo' and uid = %d " . item_normal(), intval($page_owner_uid));
             if ($r) {
                 foreach ($r as $i) {
                     attach_delete($page_owner_uid, $i['resource_id'], 1);
                 }
             }
             // remove the associated photos in case they weren't attached to an item
             q("delete from photo where resource_id in ( {$str} ) and uid = %d", intval($page_owner_uid));
             // @FIXME do the same for the linked attach
             if ($folder_hash) {
                 attach_delete($page_owner_uid, $folder_hash, 1);
                 $sync = attach_export_data(\App::$data['channel'], $folder_hash, true);
                 if ($sync) {
                     build_sync_packet($page_owner_uid, array('file' => array($sync)));
                 }
             }
         }
         goaway(z_root() . '/photos/' . \App::$data['channel']['channel_address']);
     }
     if (argc() > 2 && x($_REQUEST, 'delete') && $_REQUEST['delete'] === t('Delete Photo')) {
         // same as above but remove single photo
         $ob_hash = get_observer_hash();
         if (!$ob_hash) {
             goaway(z_root() . '/' . $_SESSION['photo_return']);
         }
         $r = q("SELECT `id`, `resource_id` FROM `photo` WHERE ( xchan = '%s' or `uid` = %d ) AND `resource_id` = '%s' LIMIT 1", dbesc($ob_hash), intval(local_channel()), dbesc(\App::$argv[2]));
         if ($r) {
             attach_delete($page_owner_uid, $r[0]['resource_id'], 1);
             $sync = attach_export_data(\App::$data['channel'], $r[0]['resource_id'], true);
             if ($sync) {
                 build_sync_packet($page_owner_uid, array('file' => array($sync)));
             }
         }
         goaway(z_root() . '/photos/' . \App::$data['channel']['channel_address'] . '/album/' . $_SESSION['album_return']);
     }
     if (argc() > 2 && array_key_exists('move_to_album', $_POST)) {
         $m = q("select folder from attach where hash = '%s' and uid = %d limit 1", dbesc(argv(2)), intval($page_owner_uid));
         if ($m && $m[0]['folder'] != $_POST['move_to_album']) {
             attach_move($page_owner_uid, argv(2), $_POST['move_to_album']);
             if (!($_POST['desc'] && $_POST['newtag'])) {
                 goaway(z_root() . '/' . $_SESSION['photo_return']);
             }
         }
     }
     if (argc() > 2 && (x($_POST, 'desc') !== false || x($_POST, 'newtag') !== 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;
         $is_nsfw = x($_POST, 'adult') ? intval($_POST['adult']) : 0;
         $acl->set_from_array($_POST);
         $perm = $acl->get();
         $resource_id = argv(2);
         if (x($_POST, 'rotate') !== false && (intval($_POST['rotate']) == 1 || intval($_POST['rotate']) == 2)) {
             logger('rotate');
             $r = q("select * from photo where `resource_id` = '%s' and uid = %d and imgscale = 0 limit 1", dbesc($resource_id), intval($page_owner_uid));
             if (count($r)) {
                 $d = $r[0]['os_storage'] ? @file_get_contents($r[0]['content']) : dbunescbin($r[0]['content']);
                 $ph = photo_factory($d, $r[0]['mimetype']);
                 if ($ph->is_valid()) {
                     $rotate_deg = intval($_POST['rotate']) == 1 ? 270 : 90;
                     $ph->rotate($rotate_deg);
                     $width = $ph->getWidth();
                     $height = $ph->getHeight();
                     if (intval($r[0]['os_storage'])) {
                         @file_put_contents($r[0]['content'], $ph->imageString());
                         $data = $r[0]['content'];
                         $fsize = @filesize($r[0]['content']);
                         q("update attach set filesize = %d where hash = '%s' and uid = %d limit 1", intval($fsize), dbesc($resource_id), intval($page_owner_uid));
                     } else {
                         $data = $ph->imageString();
                         $fsize = strlen($data);
                     }
                     $x = q("update photo set content = '%s', filesize = %d, height = %d, width = %d where `resource_id` = '%s' and uid = %d and imgscale = 0", dbescbin($data), intval($fsize), intval($height), intval($width), dbesc($resource_id), intval($page_owner_uid));
                     if ($width > 1024 || $height > 1024) {
                         $ph->scaleImage(1024);
                     }
                     $width = $ph->getWidth();
                     $height = $ph->getHeight();
                     $x = q("update photo set content = '%s', height = %d, width = %d where `resource_id` = '%s' and uid = %d and imgscale = 1", dbescbin($ph->imageString()), intval($height), intval($width), dbesc($resource_id), intval($page_owner_uid));
                     if ($width > 640 || $height > 640) {
                         $ph->scaleImage(640);
                     }
                     $width = $ph->getWidth();
                     $height = $ph->getHeight();
                     $x = q("update photo set content = '%s', height = %d, width = %d where `resource_id` = '%s' and uid = %d and imgscale = 2", dbescbin($ph->imageString()), intval($height), intval($width), dbesc($resource_id), intval($page_owner_uid));
                     if ($width > 320 || $height > 320) {
                         $ph->scaleImage(320);
                     }
                     $width = $ph->getWidth();
                     $height = $ph->getHeight();
                     $x = q("update photo set content = '%s', height = %d, width = %d where `resource_id` = '%s' and uid = %d and imgscale = 3", dbescbin($ph->imageString()), intval($height), intval($width), dbesc($resource_id), intval($page_owner_uid));
                 }
             }
         }
         $p = q("SELECT mimetype, is_nsfw, description, resource_id, imgscale, allow_cid, allow_gid, deny_cid, deny_gid FROM photo WHERE resource_id = '%s' AND uid = %d ORDER BY imgscale DESC", dbesc($resource_id), intval($page_owner_uid));
         if ($p) {
             $ext = $phototypes[$p[0]['mimetype']];
             $r = q("UPDATE `photo` SET `description` = '%s', `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `resource_id` = '%s' AND `uid` = %d", dbesc($desc), dbesc($perm['allow_cid']), dbesc($perm['allow_gid']), dbesc($perm['deny_cid']), dbesc($perm['deny_gid']), dbesc($resource_id), intval($page_owner_uid));
         }
         $item_private = $str_contact_allow || $str_group_allow || $str_contact_deny || $str_group_deny ? true : false;
         $old_is_nsfw = $p[0]['is_nsfw'];
         if ($old_is_nsfw != $is_nsfw) {
             $r = q("update photo set is_nsfw = %d where resource_id = '%s' and uid = %d", intval($is_nsfw), dbesc($resource_id), intval($page_owner_uid));
         }
         /* Don't make the item visible if the only change was the album name */
         $visibility = 0;
         if ($p[0]['description'] !== $desc || strlen($rawtags)) {
             $visibility = 1;
         }
         if (!$item_id) {
             $item_id = photos_create_item(\App::$data['channel'], get_observer_hash(), $p[0], $visibility);
         }
         if ($item_id) {
             $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", intval($item_id), intval($page_owner_uid));
             if ($r) {
                 $old_tag = $r[0]['tag'];
                 $old_inform = $r[0]['inform'];
             }
         }
         // make sure the linked item has the same permissions as the photo regardless of any other changes
         $x = q("update item set allow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid = '%s', item_private = %d\n\t\t\t\twhere id = %d", dbesc($perm['allow_cid']), dbesc($perm['allow_gid']), dbesc($perm['deny_cid']), dbesc($perm['deny_gid']), intval($acl->is_private()), intval($item_id));
         // make sure the attach has the same permissions as the photo regardless of any other changes
         $x = q("update attach set allow_cid = '%s', allow_gid = '%s', deny_cid = '%s', deny_gid = '%s' where hash = '%s' and uid = %d and is_photo = 1", dbesc($perm['allow_cid']), dbesc($perm['allow_gid']), dbesc($perm['deny_cid']), dbesc($perm['deny_gid']), dbesc($resource_id), intval($page_owner_uid));
         if (strlen($rawtags)) {
             $str_tags = '';
             $inform = '';
             // if the new tag doesn't have a namespace specifier (@foo or #foo) give it a mention
             $x = substr($rawtags, 0, 1);
             if ($x !== '@' && $x !== '#') {
                 $rawtags = '@' . $rawtags;
             }
             require_once 'include/text.php';
             $profile_uid = \App::$profile['profile_uid'];
             $results = linkify_tags($a, $rawtags, local_channel() ? local_channel() : $profile_uid);
             $success = $results['success'];
             $post_tags = array();
             foreach ($results as $result) {
                 $success = $result['success'];
                 if ($success['replaced']) {
                     $post_tags[] = array('uid' => $profile_uid, 'ttype' => $success['termtype'], 'otype' => TERM_OBJ_POST, 'term' => $success['term'], 'url' => $success['url']);
                 }
             }
             $r = q("select * from item where id = %d and uid = %d limit 1", intval($item_id), intval($page_owner_uid));
             if ($r) {
                 $r = fetch_post_tags($r, true);
                 $datarray = $r[0];
                 if ($post_tags) {
                     if (!array_key_exists('term', $datarray) || !is_array($datarray['term'])) {
                         $datarray['term'] = $post_tags;
                     } else {
                         $datarray['term'] = array_merge($datarray['term'], $post_tags);
                     }
                 }
                 item_store_update($datarray, $execflag);
             }
         }
         $sync = attach_export_data(\App::$data['channel'], $resource_id);
         if ($sync) {
             build_sync_packet($page_owner_uid, array('file' => array($sync)));
         }
         goaway(z_root() . '/' . $_SESSION['photo_return']);
         return;
         // NOTREACHED
     }
     /**
      * default post action - upload a photo
      */
     $channel = \App::$data['channel'];
     $observer = \App::$data['observer'];
     $_REQUEST['source'] = 'photos';
     require_once 'include/attach.php';
     if (!local_channel()) {
         $_REQUEST['contact_allow'] = expand_acl($channel['channel_allow_cid']);
         $_REQUEST['group_allow'] = expand_acl($channel['channel_allow_gid']);
         $_REQUEST['contact_deny'] = expand_acl($channel['channel_deny_cid']);
         $_REQUEST['group_deny'] = expand_acl($channel['channel_deny_gid']);
     }
     $r = attach_store($channel, get_observer_hash(), '', $_REQUEST);
     if (!$r['success']) {
         notice($r['message'] . EOL);
     }
     if ($_REQUEST['newalbum']) {
         goaway(z_root() . '/photos/' . \App::$data['channel']['channel_address'] . '/album/' . bin2hex($_REQUEST['newalbum']));
     } else {
         goaway(z_root() . '/photos/' . \App::$data['channel']['channel_address'] . '/album/' . bin2hex(datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y')));
     }
 }
예제 #4
0
 /**
  * @brief delete directory
  */
 public function delete()
 {
     logger('delete file ' . basename($this->red_path), LOGGER_DEBUG);
     if (!$this->auth->owner_id || !perm_is_allowed($this->auth->owner_id, $this->auth->observer, 'write_storage')) {
         throw new DAV\Exception\Forbidden('Permission denied.');
     }
     if ($this->auth->owner_id !== $this->auth->channel_id) {
         if ($this->auth->observer !== $this->data['creator'] || intval($this->data['is_dir'])) {
             throw new DAV\Exception\Forbidden('Permission denied.');
         }
     }
     attach_delete($this->auth->owner_id, $this->folder_hash);
     $ch = channelx_by_n($this->auth->owner_id);
     if ($ch) {
         $sync = attach_export_data($ch, $this->folder_hash, true);
         if ($sync) {
             build_sync_packet($ch['channel_id'], array('file' => array($sync)));
         }
     }
 }
예제 #5
0
파일: File.php 프로젝트: BlaBlaNet/hubzilla
 /**
  * @brief Delete the file.
  *
  * This method checks the permissions and then calls attach_delete() function
  * to actually remove the file.
  *
  * @throw \Sabre\DAV\Exception\Forbidden
  */
 public function delete()
 {
     logger('delete file ' . basename($this->name), LOGGER_DEBUG);
     if (!$this->auth->owner_id || !perm_is_allowed($this->auth->owner_id, $this->auth->observer, 'write_storage')) {
         throw new DAV\Exception\Forbidden('Permission denied.');
     }
     if ($this->auth->owner_id !== $this->auth->channel_id) {
         if ($this->auth->observer !== $this->data['creator'] || intval($this->data['is_dir'])) {
             throw new DAV\Exception\Forbidden('Permission denied.');
         }
     }
     if (get_pconfig($this->auth->owner_id, 'system', 'os_delete_prohibit') && \App::$module == 'dav') {
         throw new DAV\Exception\Forbidden('Permission denied.');
     }
     attach_delete($this->auth->owner_id, $this->data['hash']);
     $ch = channelx_by_n($this->auth->owner_id);
     if ($ch) {
         $sync = attach_export_data($ch, $this->data['hash'], true);
         if ($sync) {
             build_sync_packet($ch['channel_id'], array('file' => array($sync)));
         }
     }
 }