public function log_resources($resourceIDs = false)
 {
     if ($resourceIDs === false) {
         $Resources = new PerchResources();
         $resourceIDs = $Resources->get_logged_ids();
     }
     if (PerchUtil::count($resourceIDs) && $this->api) {
         PerchUtil::debug('Logging resources for ' . $this->api->app_id);
         $app_id = $this->api->app_id;
         $sql = 'DELETE FROM ' . PERCH_DB_PREFIX . 'resource_log WHERE appID=' . $this->db->pdb($app_id) . ' AND itemFK=' . $this->db->pdb($this->pk) . ' AND itemRowID=' . $this->db->pdb((int) $this->id());
         $this->db->execute($sql);
         $sql = 'INSERT IGNORE INTO ' . PERCH_DB_PREFIX . 'resource_log(`appID`, `itemFK`, `itemRowID`, `resourceID`) VALUES';
         $vals = array();
         foreach ($resourceIDs as $id) {
             $vals[] = '(' . $this->db->pdb($app_id) . ',' . $this->db->pdb($this->pk) . ',' . (int) $this->id() . ',' . (int) $id . ')';
         }
         $sql .= implode(',', $vals);
         $this->db->execute($sql);
     } else {
         PerchUtil::debug('No ids to log.');
     }
 }
 public function get_logged_ids()
 {
     $ids = PerchResources::$logged;
     PerchResources::$logged = array();
     return $ids;
 }
     PerchUtil::redirect()
 }
 */
 $created = false;
 $postvars = array('resourceTitle', 'resourceInLibrary', 'resourceBucket');
 $data = $Form->receive($postvars);
 if (isset($data['resourceBucket'])) {
     $FieldTag->set('bucket', $data['resourceBucket']);
 }
 if (!isset($data['resourceInLibrary'])) {
     $data['resourceInLibrary'] = 0;
 }
 $FieldType = PerchFieldTypes::get($FieldTag->type(), $Form, $FieldTag);
 $var = $FieldType->get_raw();
 if (PerchUtil::count($var)) {
     $Resources = new PerchResources();
     $ids = $Resources->get_logged_ids();
     $assetID = array_shift($ids);
     $Asset = $Assets->find($assetID);
     $created = true;
 }
 if ($Asset) {
     if ($data['resourceInLibrary'] == '1') {
         $Asset->mark_as_library();
     }
     $Asset->update($data);
     $Asset->reindex();
     // Tags
     if (isset($_POST['tags']) && trim($_POST['tags']) != '') {
         $tag_string = trim($_POST['tags']);
         $Tags->assign_tag_string($Asset->id(), $tag_string, true);
 public function import_from_perch_gallery()
 {
     $sql = 'SELECT COUNT(*) FROM ' . PERCH_DB_PREFIX . 'gallery_images';
     $count = $this->db->get_count($sql);
     if ($count) {
         $Resources = new PerchResources();
         $sql = "SELECT i.imageID, i.imageBucket AS bucket, i.albumID, iv.versionPath AS file, i.imageAlt AS title, iv.versionWidth AS width, iv.versionHeight AS height\n                    FROM `" . PERCH_DB_PREFIX . "gallery_image_versions` iv, `" . PERCH_DB_PREFIX . "gallery_images` i\n                    WHERE iv.imageID=i.imageID AND iv.versionKey='original' AND i.imageStatus='active'";
         $originals = $this->db->get_rows($sql);
         if (PerchUtil::count($originals)) {
             foreach ($originals as $orig) {
                 $sql = 'SELECT COUNT(*) FROM ' . PERCH_DB_PREFIX . 'resources WHERE resourceFile=' . $this->db->pdb($orig['file']) . ' AND resourceBucket=' . $this->db->pdb($orig['bucket']) . ' AND resourceKey=' . $this->db->pdb('orig');
                 if ($this->db->get_count($sql)) {
                     continue;
                 }
                 $parentID = $Resources->log('perch_gallery', $orig['bucket'], $orig['file'], 0, 'orig', false, array('w' => $orig['width'], 'h' => $orig['height'], 'title' => $orig['title']));
                 if ($parentID) {
                     $this->db->insert(PERCH_DB_PREFIX . 'resource_log', array('appID' => 'perch_gallery', 'itemFK' => 'albumID', 'itemRowID' => $orig['albumID'], 'resourceID' => $parentID), true);
                     $sql = "SELECT i.imageID, i.imageBucket AS bucket, i.albumID, iv.versionPath AS file, i.imageAlt AS title, iv.versionWidth AS width, iv.versionHeight AS height\n                                FROM `" . PERCH_DB_PREFIX . "gallery_image_versions` iv, `" . PERCH_DB_PREFIX . "gallery_images` i\n                                WHERE iv.imageID=i.imageID AND iv.versionKey='admin_thumb' AND i.imageStatus='active' AND i.imageID=" . $this->db->pdb($orig['imageID']) . ' LIMIT 1';
                     $thumb = $this->db->get_row($sql);
                     if ($thumb) {
                         $thumbID = $Resources->log('perch_gallery', $thumb['bucket'], $thumb['file'], $parentID, 'thumb', false, array('w' => $thumb['width'], 'h' => $thumb['height'], 'target_w' => '150', 'target_h' => '150', 'title' => $thumb['title']));
                     }
                 }
             }
         }
     }
 }
Exemple #5
0
     if (isset($_POST['sharpen'])) {
         $sharpen = (int) $_POST['sharpen'];
     }
     $Tag->set('sharpen', $sharpen);
     $density = 1;
     if (isset($_POST['density'])) {
         $density = (int) $_POST['density'];
     }
     $Tag->set('density', $density);
 }
 $Assets = new PerchAssets_Assets();
 $message = false;
 $assetID = false;
 $Asset = false;
 $Form = new PerchForm('edit');
 $Resources = new PerchResources();
 $data = array();
 $FieldType = PerchFieldTypes::get($Tag->type(), $Form, $Tag, array($Tag), 'markitup');
 $var = $FieldType->get_raw();
 if (PerchUtil::count($var)) {
     $ids = $Resources->get_logged_ids();
     $Resources->mark_group_as_library($ids);
     $assetID = $ids[0];
     $Asset = $Assets->find($assetID);
     if (isset($_POST['miu_image_upload_title']) && $_POST['miu_image_upload_title'] != '') {
         $Asset->update(array('resourceTitle' => $_POST['miu_image_upload_title']));
     }
     $Asset->reindex();
     if (PerchUtil::count($ids)) {
         if (!PerchSession::is_set('resourceIDs')) {
             $logged_ids = array();
 public function clean_up_resources()
 {
     $subquery = 'SELECT resourceID FROM ' . PERCH_DB_PREFIX . 'resource_log WHERE appID=' . $this->db->pdb('content');
     $Resources = new PerchResources();
     $resources = $Resources->get_not_in_subquery('content', $subquery);
     if (PerchUtil::count($resources)) {
         foreach ($resources as $Resource) {
             if ($Resource->is_not_in_use()) {
                 $Resource->delete();
             }
         }
     }
     $Perch = Perch::fetch();
     $Perch->event('region.cleanup', $this);
 }
Exemple #7
0
    /*
          UPDATE `__PREFIX__settings` SET `settingValue` = 'rgb(54,54,54)' WHERE `settingID` = 'headerColour' LIMIT 1;
          UPDATE `__PREFIX__settings` SET `settingValue` = 'dark' WHERE `settingID` = 'headerScheme';
    */
}
$sql = str_replace('__PREFIX__', PERCH_DB_PREFIX, $sql);
$queries = explode(';', $sql);
if (PerchUtil::count($queries) > 0) {
    foreach ($queries as $query) {
        $query = trim($query);
        if ($query != '') {
            $DB->execute($query);
            if ($DB->errored && strpos($DB->error_msg, 'Duplicate') === false) {
                echo '<li class="icon failure error">' . PerchUtil::html(PerchLang::get('The following error occurred:')) . '</li>';
                echo '<li class="failure"><code class="sql">' . PerchUtil::html($query) . '</code></li>';
                echo '<li class="failure"><code>' . PerchUtil::html($DB->error_msg) . '</code></p></li>';
                $errors = true;
            }
        }
    }
}
// Resource logging - failsafes
if (PerchUtil::count($DB->get_rows('SHOW TABLES LIKE \'' . PERCH_DB_PREFIX . 'resource_log\'')) == 0) {
    $sql = "CREATE TABLE IF NOT EXISTS `__PREFIX__resource_log` (\n              `logID` int(10) unsigned NOT NULL AUTO_INCREMENT,\n              `appID` char(32) NOT NULL DEFAULT 'content',\n              `itemFK` char(32) NOT NULL DEFAULT 'itemRowID',\n              `itemRowID` int(10) unsigned NOT NULL DEFAULT '0',\n              `resourceID` int(10) unsigned NOT NULL DEFAULT '0',\n              PRIMARY KEY (`logID`),\n              KEY `idx_resource` (`resourceID`),\n              KEY `idx_fk` (`itemFK`,`itemRowID`),\n              UNIQUE KEY `idx_uni` (`appID`,`itemFK`,`itemRowID`,`resourceID`)\n            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
    $DB->execute($query);
}
// Is there anything in the resource log table?
if ($DB->get_count('SELECT COUNT(*) FROM ' . PERCH_DB_PREFIX . 'resource_log') == 0) {
    $Resources = new PerchResources();
    $Resources->log_unlogged_resources_for_safety();
}
 public function get_raw($post = false, $Item = false)
 {
     $store = array();
     $Perch = Perch::fetch();
     $Bucket = PerchResourceBuckets::get($this->Tag->bucket());
     $image_folder_writable = $Bucket->ready_to_write();
     $item_id = $this->Tag->input_id();
     $asset_reference_used = false;
     $target = false;
     $filesize = false;
     if (!class_exists('PerchAssets_Assets', false)) {
         include_once PERCH_CORE . '/apps/assets/PerchAssets_Assets.class.php';
         include_once PERCH_CORE . '/apps/assets/PerchAssets_Asset.class.php';
     }
     $Assets = new PerchAssets_Assets();
     $AssetMeta = false;
     // Asset ID?
     if (isset($post[$this->Tag->id() . '_assetID']) && $post[$this->Tag->id() . '_assetID'] != '') {
         $new_assetID = $post[$this->Tag->id() . '_assetID'];
         $Asset = $Assets->find($new_assetID);
         if (is_object($Asset)) {
             $target = $Asset->file_path();
             $filename = $Asset->resourceFile();
             $store['assetID'] = $Asset->id();
             $store['title'] = $Asset->resourceTitle();
             $store['_default'] = $Asset->web_path();
             $store['bucket'] = $Asset->resourceBucket();
             if ($store['bucket'] != $Bucket->get_name()) {
                 $Bucket = PerchResourceBuckets::get($store['bucket']);
             }
             $asset_reference_used = true;
         }
     }
     if ($image_folder_writable && isset($_FILES[$item_id]) && (int) $_FILES[$item_id]['size'] > 0) {
         if (!isset(self::$file_paths[$this->Tag->id()])) {
             // We do this before writing to the bucket, as it performs better for remote buckets.
             $AssetMeta = $Assets->get_meta_data($_FILES[$item_id]['tmp_name'], $_FILES[$item_id]['name']);
             $result = $Bucket->write_file($_FILES[$item_id]['tmp_name'], $_FILES[$item_id]['name']);
             $target = $result['path'];
             $filename = $result['name'];
             $filesize = (int) $_FILES[$item_id]['size'];
             $store['_default'] = rtrim($Bucket->get_web_path(), '/') . '/' . $filename;
             // fire events
             if ($this->Tag->type() == 'image') {
                 $PerchImage = new PerchImage();
                 $profile = $PerchImage->get_resize_profile($target);
                 $profile['original'] = true;
                 $Perch->event('assets.upload_image', new PerchAssetFile($profile));
             }
         }
     }
     if ($target && $filename && is_file($target)) {
         self::$file_paths[$this->Tag->id()] = $target;
         $store['path'] = $filename;
         $store['size'] = $filesize ?: filesize($target);
         $store['bucket'] = $Bucket->get_name();
         // Is this an SVG?
         $svg = false;
         $size = getimagesize($target);
         if (PerchUtil::count($size)) {
             $store['w'] = $size[0];
             $store['h'] = $size[1];
             if (isset($size['mime'])) {
                 $store['mime'] = $size['mime'];
             }
         } else {
             $PerchImage = new PerchImage();
             if ($PerchImage->is_webp($target)) {
                 $store['mime'] = 'image/webp';
             } elseif ($PerchImage->is_svg($target)) {
                 $svg = true;
                 $size = $PerchImage->get_svg_size($target);
                 if (PerchUtil::count($size)) {
                     $store['w'] = $size['w'];
                     $store['h'] = $size['h'];
                     if (isset($size['mime'])) {
                         $store['mime'] = $size['mime'];
                     }
                 }
             } else {
                 // It's not an image (according to getimagesize) and not an SVG.
                 if ($this->Tag->detect_type()) {
                     // if we have permission to guess, our guess is that it's a file.
                     PerchUtil::debug('Guessing file', 'error');
                     $this->Tag->set('type', 'file');
                 }
                 $store['mime'] = PerchUtil::get_mime_type($target);
             }
         }
         // thumbnail
         if ($this->Tag->type() == 'image') {
             $PerchImage = new PerchImage();
             $PerchImage->set_density(2);
             $result = false;
             if ($asset_reference_used) {
                 $result = $Assets->get_resize_profile($store['assetID'], 150, 150, false, 'thumb', $PerchImage->get_density());
             }
             if (!$result) {
                 $result = $PerchImage->resize_image($target, 150, 150, false, 'thumb');
             }
             if (is_array($result)) {
                 //PerchUtil::debug($result, 'notice');
                 if (!isset($store['sizes'])) {
                     $store['sizes'] = array();
                 }
                 $variant_key = 'thumb';
                 $tmp = array();
                 $tmp['w'] = $result['w'];
                 $tmp['h'] = $result['h'];
                 $tmp['target_w'] = 150;
                 $tmp['target_h'] = 150;
                 $tmp['density'] = 2;
                 $tmp['path'] = $result['file_name'];
                 $tmp['size'] = filesize($result['file_path']);
                 $tmp['mime'] = isset($result['mime']) ? $result['mime'] : '';
                 if ($result && isset($result['_resourceID'])) {
                     $tmp['assetID'] = $result['_resourceID'];
                 }
                 $store['sizes'][$variant_key] = $tmp;
             }
             unset($result);
             unset($PerchImage);
         }
         if ($this->Tag->type() == 'file') {
             $PerchImage = new PerchImage();
             $PerchImage->set_density(2);
             $result = $PerchImage->thumbnail_file($target, 150, 150, false);
             if (is_array($result)) {
                 if (!isset($store['sizes'])) {
                     $store['sizes'] = array();
                 }
                 $variant_key = 'thumb';
                 $tmp = array();
                 $tmp['w'] = $result['w'];
                 $tmp['h'] = $result['h'];
                 $tmp['target_w'] = 150;
                 $tmp['target_h'] = 150;
                 $tmp['density'] = 2;
                 $tmp['path'] = $result['file_name'];
                 $tmp['size'] = filesize($result['file_path']);
                 $tmp['mime'] = isset($result['mime']) ? $result['mime'] : '';
                 if ($result && isset($result['_resourceID'])) {
                     $tmp['assetID'] = $result['_resourceID'];
                 }
                 $store['sizes'][$variant_key] = $tmp;
             }
             unset($result);
             unset($PerchImage);
         }
     }
     // Loop through all tags with this ID, get their dimensions and resize the images.
     $all_tags = $this->get_sibling_tags();
     if (PerchUtil::count($all_tags)) {
         foreach ($all_tags as $Tag) {
             if ($Tag->id() == $this->Tag->id()) {
                 // This is either this tag, or another tag in the template with the same ID.
                 if ($Tag->type() == 'image' && ($Tag->width() || $Tag->height()) && isset(self::$file_paths[$Tag->id()])) {
                     $variant_key = 'w' . $Tag->width() . 'h' . $Tag->height() . 'c' . ($Tag->crop() ? '1' : '0') . ($Tag->density() ? '@' . $Tag->density() . 'x' : '');
                     if (!isset($store['sizes'][$variant_key])) {
                         $PerchImage = new PerchImage();
                         if ($Tag->quality()) {
                             $PerchImage->set_quality($Tag->quality());
                         }
                         if ($Tag->is_set('sharpen')) {
                             $PerchImage->set_sharpening($Tag->sharpen());
                         }
                         if ($Tag->density()) {
                             $PerchImage->set_density($Tag->density());
                         }
                         $result = false;
                         if ($asset_reference_used) {
                             $result = $Assets->get_resize_profile($store['assetID'], $Tag->width(), $Tag->height(), $Tag->crop(), false, $PerchImage->get_density());
                         }
                         if (!$result) {
                             $result = $PerchImage->resize_image(self::$file_paths[$Tag->id()], $Tag->width(), $Tag->height(), $Tag->crop());
                         }
                         if (is_array($result)) {
                             if (!isset($store['sizes'])) {
                                 $store['sizes'] = array();
                             }
                             $tmp = array();
                             $tmp['w'] = $result['w'];
                             $tmp['h'] = $result['h'];
                             $tmp['target_w'] = $Tag->width();
                             $tmp['target_h'] = $Tag->height();
                             $tmp['crop'] = $Tag->crop();
                             $tmp['density'] = $Tag->density() ? $Tag->density() : '1';
                             $tmp['path'] = $result['file_name'];
                             $tmp['size'] = filesize($result['file_path']);
                             $tmp['mime'] = isset($result['mime']) ? $result['mime'] : '';
                             if ($result && isset($result['_resourceID'])) {
                                 $tmp['assetID'] = $result['_resourceID'];
                             }
                             $store['sizes'][$variant_key] = $tmp;
                             unset($tmp);
                         }
                         unset($result);
                         unset($PerchImage);
                     }
                 }
             }
         }
     }
     if (isset($_POST[$item_id . '_remove'])) {
         $store = array();
     }
     // If a file isn't uploaded...
     if (!$asset_reference_used && (!isset($_FILES[$item_id]) || (int) $_FILES[$item_id]['size'] == 0)) {
         // If remove is checked, remove it.
         if (isset($_POST[$item_id . '_remove'])) {
             $store = array();
         } else {
             // Else get the previous data and reuse it.
             if (is_object($Item)) {
                 $json = PerchUtil::json_safe_decode($Item->itemJSON(), true);
                 if (PerchUtil::count($json) && $this->Tag->in_repeater() && $this->Tag->tag_context()) {
                     $waypoints = preg_split('/_([0-9]+)_/', $this->Tag->tag_context(), null, PREG_SPLIT_DELIM_CAPTURE);
                     if (PerchUtil::count($waypoints) > 0) {
                         $subject = $json;
                         foreach ($waypoints as $waypoint) {
                             if (isset($subject[$waypoint])) {
                                 $subject = $subject[$waypoint];
                             } else {
                                 $subject = false;
                             }
                             $store = $subject;
                         }
                     }
                 }
                 if (PerchUtil::count($json) && isset($json[$this->Tag->id()])) {
                     $store = $json[$this->Tag->id()];
                 }
             } else {
                 if (is_array($Item)) {
                     $json = $Item;
                     if (PerchUtil::count($json) && isset($json[$this->Tag->id()])) {
                         $store = $json[$this->Tag->id()];
                     }
                 }
             }
         }
     }
     // log resources
     if (PerchUtil::count($store) && isset($store['path'])) {
         $Resources = new PerchResources();
         // Main image
         $parentID = $Resources->log($this->app_id, $store['bucket'], $store['path'], 0, 'orig', false, $store, $AssetMeta);
         // variants
         if (isset($store['sizes']) && PerchUtil::count($store['sizes'])) {
             foreach ($store['sizes'] as $key => $size) {
                 $Resources->log($this->app_id, $store['bucket'], $size['path'], $parentID, $key, false, $size, $AssetMeta);
             }
         }
         // Additional IDs from the session
         if (PerchSession::is_set('resourceIDs')) {
             $ids = PerchSession::get('resourceIDs');
             if (is_array($ids) && PerchUtil::count($ids)) {
                 $Resources->log_extra_ids($ids);
             }
             PerchSession::delete('resourceIDs');
         }
     }
     self::$file_paths = array();
     // Check it's not an empty array
     if (is_array($store) && count($store) === 0) {
         return null;
     }
     return $store;
 }