Example #1
0
File: move.php Project: rair/yacs
if ($output['success'] === null) {
    // do it
    $fields = array('anchor' => $target->get_reference());
    if ($file->item['thumbnail_url']) {
        // set new thumbnail url
        $fields['thumbnail_url'] = $context['url_to_master'] . $context['url_to_root'] . Files::get_path($target->get_reference()) . '/thumbs/' . urlencode($file->item['file_name']);
    }
    $output['success'] = $file->set_values($fields);
    // move file physicaly
    if ($output['success']) {
        $from = $context['path_to_root'] . Files::get_path($last_parent->get_reference()) . '/' . $file->item['file_name'];
        $dir = $context['path_to_root'] . Files::get_path($target->get_reference());
        $to = $dir . '/' . $file->item['file_name'];
        // check that dir exists
        if (!is_dir($dir)) {
            Safe::make_path($dir);
        }
        Safe::rename($from, $to);
        // move thumb if any
        if ($file->item['thumbnail_url']) {
            $from = Files::get_path($last_parent->get_reference()) . '/thumbs/' . $file->item['file_name'];
            // make directory thumbs
            $to = $dir . '/thumbs/' . $file->item['file_name'];
            // check that dir exist
            if (!is_dir($dir . '/thumbs')) {
                Safe::mkdir($dir . '/thumbs');
            }
            Safe::rename($from, $to);
        }
    }
}
Example #2
0
 /**
  * create a referenced image
  *
  * @param array of entity attributes (e.g., 'Content-Disposition')
  * @param string image actual content
  * @param array poster attributes
  * @param string the target anchor (e.g., 'article:123')
  * @param string reference of the object to be extended, if any
  * @return string reference to the created object, or NULL
  */
 public static function submit_image($entity_headers, $content, $user, $anchor, $target = NULL)
 {
     global $context;
     // retrieve queue parameters
     list($server, $account, $password, $allowed, $match, $section, $options, $hooks, $prefix, $suffix) = $context['mail_queue'];
     // locate content-disposition
     foreach ($entity_headers as $header) {
         if (preg_match('/Content-Disposition/i', $header['name'])) {
             $content_disposition = $header['value'];
             break;
         }
     }
     // find file name in content-disposition
     $file_name = '';
     if ($content_disposition && preg_match('/filename="*([a-zA-Z0-9\'\\(\\)\\+_,-\\.\\/:=\\? ]+)"*\\s*/i', $content_disposition, $matches)) {
         $file_name = $matches[1];
     }
     // as an alternative, look in content-type
     if (!$file_name) {
         // locate content-type
         foreach ($entity_headers as $header) {
             if (preg_match('/Content-Type/i', $header['name'])) {
                 $content_type = $header['value'];
                 break;
             }
         }
         // find file name in content-type
         if ($content_type && preg_match('/name="*([a-zA-Z0-9\'\\(\\)\\+_,-\\.\\/:=\\? ]+)"*\\s*/i', $content_type, $matches)) {
             $file_name = $matches[1];
         }
     }
     // as an alternative, look in content-description
     if (!$file_name) {
         // locate content-description
         foreach ($entity_headers as $header) {
             if (preg_match('/Content-Description/i', $header['name'])) {
                 $content_description = $header['value'];
                 break;
             }
         }
         // find file name in content-description
         $file_name = $content_description;
     }
     // sanity check
     if (!$file_name) {
         Logger::remember('agents/messages.php: No file name to use for submitted image');
         return NULL;
     }
     // file size
     $file_size = strlen($content);
     // sanity check
     if ($file_size < 7) {
         Logger::remember('agents/messages.php: Short image skipped', $file_name);
         return NULL;
     }
     // sanity check
     if (!$anchor) {
         Logger::remember('agents/messages.php: No anchor to use for submitted image', $file_name);
         return NULL;
     }
     // get anchor data -- this is a mutable object
     $host = Anchors::get($anchor, TRUE);
     if (!is_object($host)) {
         Logger::remember('agents/messages.php: Unknown anchor ' . $anchor, $file_name);
         return NULL;
     }
     // create target folders
     $file_path = Files::get_path($anchor, 'images');
     if (!Safe::make_path($file_path)) {
         Logger::remember('agents/messages.php: Impossible to create ' . $file_path);
         return NULL;
     }
     if (!Safe::make_path($file_path . '/thumbs')) {
         Logger::remember('agents/messages.php: Impossible to create ' . $file_path . '/thumbs');
         return NULL;
     }
     $file_path = $context['path_to_root'] . $file_path . '/';
     // save the entity in the file system
     if (!($file = Safe::fopen($file_path . $file_name, 'wb'))) {
         Logger::remember('agents/messages.php: Impossible to open ' . $file_path . $file_name);
         return NULL;
     }
     if (fwrite($file, $content) === FALSE) {
         Logger::remember('agents/messages.php: Impossible to write to ' . $file_path . $file_name);
         return NULL;
     }
     fclose($file);
     // get image information
     if (!($image_information = Safe::GetImageSize($file_path . $file_name))) {
         Safe::unlink($file_path . $file_name);
         Logger::remember('agents/messages.php: No image information in ' . $file_path . $file_name);
         return NULL;
     }
     // we accept only gif, jpeg and png
     if ($image_information[2] != 1 && $image_information[2] != 2 && $image_information[2] != 3) {
         Safe::unlink($file_path . $file_name);
         Logger::remember('agents/messages.php: Rejected image type for ' . $file_path . $file_name);
         return NULL;
     }
     // build a thumbnail
     $thumbnail_name = 'thumbs/' . $file_name;
     // do not stop on error
     include_once $context['path_to_root'] . 'images/image.php';
     if (!Image::shrink($file_path . $file_name, $file_path . $thumbnail_name, FALSE, FALSE)) {
         Logger::remember('agents/messages.php: No thumbnail has been created for ' . $file_path . $file_name);
     }
     // resize the image where applicable
     if (Image::adjust($file_path . $file_name, FALSE)) {
         $file_size = Safe::filesize($file_path . $file_name);
     }
     // all details
     $details = array();
     // image size
     if ($image_information = Safe::GetImageSize($file_path . $file_name)) {
         $details[] = i18n::c('Size') . ': ' . $image_information[0] . ' x ' . $image_information[1];
     }
     // update image description
     $item = array();
     $item['anchor'] = $anchor;
     $item['image_name'] = $file_name;
     $item['thumbnail_name'] = $thumbnail_name;
     $item['image_size'] = $file_size;
     $item['description'] = '';
     if (isset($content_description) && $content_description != $file_name) {
         $item['description'] .= $content_description;
     }
     if (@count($details)) {
         $item['description'] .= "\n\n" . '<p class="details">' . implode("<br />\n", $details) . "</p>\n";
     }
     $item['edit_date'] = gmstrftime('%Y-%m-%d %H:%M:%S', time());
     $item['edit_name'] = $user['nick_name'];
     $item['edit_id'] = $user['id'];
     $item['edit_address'] = $user['email'];
     // create an image record in the database
     include_once $context['path_to_root'] . 'images/images.php';
     if (!($item['id'] = Images::post($item))) {
         Logger::remember('agents/messages.php: Impossible to save image ' . $item['image_name']);
         return NULL;
     }
     if ($context['debug_messages'] == 'Y') {
         Logger::remember('agents/messages.php: Messages::submit_image()', $item, 'debug');
     }
     // insert the image in the anchor page
     $host->touch('image:create', $item['id'], TRUE);
     return 'image:' . $item['id'];
 }
Example #3
0
File: image.php Project: rair/yacs
 /**
  * create a thumbnail
  *
  * @param string the full path to the original file
  * @param string the pull path to write the thumbnail
  * @param boolean TRUE to resize to 60x60
  * @param boolean TRUE to see error messages, if any
  * @return TRUE on success, FALSE on error
  */
 public static function shrink($original, $target, $fixed = FALSE, $verbose = TRUE)
 {
     global $context;
     $file_name = basename($original);
     $open = Image::open($original);
     if ($open === FALSE) {
         return FALSE;
     }
     list($image, $image_information) = $open;
     // actual width
     $width = $image_information[0];
     // standard width
     if ($fixed) {
         $maximum_width = 60;
     } elseif (isset($context['thumbnail_width']) && $context['thumbnail_width'] >= 32) {
         $maximum_width = $context['thumbnail_width'];
     } else {
         $maximum_width = 60;
     }
     // actual height
     $height = $image_information[1];
     // standard height
     if ($fixed) {
         $maximum_height = 60;
     } elseif (isset($context['thumbnail_height']) && $context['thumbnail_height'] >= 32) {
         $maximum_height = $context['thumbnail_height'];
     } else {
         $maximum_height = 60;
     }
     // assume resize is not necessary
     $thumbnail_height = $height;
     $thumbnail_width = $width;
     // the image is laid vertically
     if ($height > $width) {
         // set the thumbnail size
         if ($height > $maximum_height) {
             $thumbnail_height = $maximum_height;
             $thumbnail_width = $width * $thumbnail_height / $height;
         }
         // the image is laid horizontally
     } else {
         // set the thumbnail size
         if ($width > $maximum_width) {
             $thumbnail_width = $maximum_width;
             $thumbnail_height = $height * $thumbnail_width / $width;
         }
     }
     // create target folder for the thumbnail
     if ($target_path = dirname($target)) {
         Safe::make_path($target_path);
     }
     // we already have a small image
     if ($thumbnail_width == $width && $thumbnail_height == $height) {
         // copy file content to the thumbnail
         if (!copy($original, $target)) {
             if ($verbose) {
                 Logger::error(sprintf(i18n::s('Cannot copy image to %s'), $target));
             }
             return FALSE;
         }
         // this will be filtered by umask anyway
         Safe::chmod($target, $context['file_mask']);
         // job done
         return TRUE;
     }
     // create the thumbnail in memory
     $thumbnail = NULL;
     if (Image::use_magic()) {
         $thumbnail = $image->resizeImage($thumbnail_width, $thumbnail_height, Imagick::FILTER_POINT, 1);
     } else {
         if ($image_information[2] == 2 && is_callable('ImageCreateTrueColor') && ($thumbnail = ImageCreateTrueColor($thumbnail_width, $thumbnail_height))) {
             ImageCopyResampled($thumbnail, $image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $width, $height);
         }
         if (!$thumbnail && is_callable('ImageCreate') && ($thumbnail = ImageCreate($thumbnail_width, $thumbnail_height))) {
             ImageCopyResized($thumbnail, $image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $width, $height);
         }
     }
     // sanity check
     if (!$thumbnail) {
         if ($verbose) {
             Logger::error(sprintf(i18n::s('Impossible to skrink image %s'), $file_name));
         }
         return FALSE;
     }
     // save the thumbnail in the file system
     $result = FALSE;
     if (Image::use_magic()) {
         $result = $image->writeImage($target);
     } else {
         if ($image_information[2] == 1 && is_callable('ImageGIF')) {
             ImageGIF($thumbnail, $target);
         } elseif ($image_information[2] == 2 && is_callable('ImageJPEG')) {
             ImageJPEG($thumbnail, $target, IMG_JPEG_QUALITY);
         } elseif (($image_information[2] == 1 || $image_information[2] == 3) && is_callable('ImagePNG')) {
             ImagePNG($thumbnail, $target);
         } else {
             if ($verbose) {
                 Logger::error(sprintf(i18n::s('Impossible to write to %s.'), $target));
             }
             return FALSE;
         }
     }
     // this will be filtered by umask anyway
     Safe::chmod($target, $context['file_mask']);
     // job done
     if (Image::use_magic()) {
         $image->destroy();
     } else {
         ImageDestroy($thumbnail);
     }
     return TRUE;
 }
Example #4
0
File: safe.php Project: rair/yacs
 /**
  * create a complete path to a file
  *
  * The target path can be relative to YACS, or an absolute path pointing
  * almost anywhere.
  *
  * @param the target path
  * @return TRUE on success, or FALSE on failure
  */
 public static function make_path($path)
 {
     global $context;
     // sanity check
     if (!$path) {
         return TRUE;
     }
     // translate path
     $translated = Safe::realpath($path);
     // the path exists
     if (is_dir($translated)) {
         return TRUE;
     }
     // create upper level first
     $dir_name = dirname($path);
     if ($dir_name != $path && preg_match('|/|', $dir_name)) {
         // it is mandatory to have upper level
         if (!Safe::make_path($dir_name)) {
             return FALSE;
         }
     }
     // create last level directory
     return Safe::mkdir($translated);
 }
Example #5
0
File: import.php Project: rair/yacs
 function process($template, $directory = 'blogger_import')
 {
     global $context;
     // we also have to prepare a skin -- the skin split is for scripts/validate.php
     $skin = '<?php' . "\n" . 'class Sk' . 'in extends Skin_skeleton {' . "\n";
     // fix images relative path
     $template = preg_replace('/src="(.+?)"/ie', "'src=\"'.fix_relative('\$1', '{$directory}').'\"'", $template);
     $template = preg_replace('/background="(.+?)"/ie', "'background=\"'.fix_relative('\$1', '{$directory}').'\"'", $template);
     $template = preg_replace('/url\\((.+?)\\)/ie', "'url('.fix_relative('\$1', '{$directory}').')'", $template);
     // <$BlogArchiveFileName$>
     $from = '/<\\$BlogArchiveFileName\\$>/i';
     $to = '<?php echo $context[\'url_to_root\'].\'categories/view.php?id=monthly\'; ?>';
     $template = preg_replace($from, $to, $template);
     // <$BlogArchiveName$>
     $from = '/<\\$BlogArchiveName\\$>/i';
     $to = '<?php echo \'Monthly Archives\'; ?>';
     $template = preg_replace($from, $to, $template);
     // <$BlogArchiveLink$>
     $from = '/<\\$BlogArchiveLink\\$>/i';
     $to = '<?php echo $context[\'url_to_root\'].\'categories/view.php?id=monthly\'; ?>';
     $template = preg_replace($from, $to, $template);
     // <$BlogArchiveURL$>
     $from = '/<\\$BlogArchiveURL\\$>/i';
     $to = '<?php echo $context[\'url_to_root\'].\'categories/view.php?id=monthly\'; ?>';
     $template = preg_replace($from, $to, $template);
     // <$BlogDescription$>
     $from = '/<\\$BlogDescription\\$>/i';
     $to = '<?php' . "\n" . 'if(is_object($anchor))' . "\n" . '	echo $anchor->get_teaser();' . "\n" . '?>';
     $template = preg_replace($from, $to, $template);
     // <$BlogMemberProfile$>
     $from = '/<\\$BlogMemberProfile\\$>/i';
     $to = '<?php echo $context[\'creator_profile\']; ?>';
     $template = preg_replace($from, $to, $template);
     // <$BlogMetaData$>
     $from = '/<\\$BlogMetaData\\$>/i';
     $to = '<?php echo $context[\'page_header\']; ?>';
     $template = preg_replace($from, $to, $template);
     // <$BlogPageTitle$>
     $from = '/<\\$BlogPageTitle\\$>/i';
     $to = '<?php echo ucfirst(strip_tags($context[\'page_title\'])); ?>';
     $template = preg_replace($from, $to, $template);
     // <$BlogTitle$>
     $from = '/<\\$BlogTitle\\$>/i';
     $to = '<?php echo ucfirst(strip_tags($context[\'page_title\'])); ?>';
     $template = preg_replace($from, $to, $template);
     // <$BlogURL$>
     $from = '/<\\$BlogURL\\$>/i';
     $to = '<?php echo $context[\'url_to_home\'].$context[\'url_to_root\']; ?>';
     $template = preg_replace($from, $to, $template);
     // only one type of new lines
     $template = str_replace("\r\n", "\n", $template);
     $template = str_replace("\r", "\n", $template);
     // <MainOrArchivePage>
     $from = '/<MainOrArchivePage>(.*?)<\\/MainOrArchivePage>/is';
     $to = '';
     $template = preg_replace($from, $to, $template);
     // the <BlogItemComments>...</BlogItemComments> block
     $areas = preg_split('/<BlogItemComments>(.*?)<\\/BlogItemComments>/is', trim($template), -1, PREG_SPLIT_DELIM_CAPTURE);
     $template = '';
     $index = 0;
     foreach ($areas as $area) {
         switch ($index % 3) {
             case 0:
                 // prefix block
                 $template .= $area;
                 break;
             case 1:
                 // commenting area
                 // <$BlogCommentDateTime$>
                 $from = '/<\\$BlogCommentDateTime\\$>/i';
                 $to = '\'.Skin::build_date($item[\'create_date\']).\'';
                 $area = preg_replace($from, $to, $area);
                 // <$BlogCommentNumber$>
                 $from = '/<\\$BlogCommentNumber\\$>/i';
                 $to = '\'.$item[\'id\'].\'';
                 $area = preg_replace($from, $to, $area);
                 // <$BlogCommentAuthor$>
                 $from = '/<\\$BlogCommentAuthor\\$>/i';
                 $to = '\'.$item[\'create_name\'].\'';
                 $area = preg_replace($from, $to, $area);
                 // <$BlogCommentAuthorNickname$>
                 $from = '/<\\$BlogCommentAuthorNickname\\$>/i';
                 $to = '\'.$item[\'create_name\'].\'';
                 $area = preg_replace($from, $to, $area);
                 // <$BlogCommentBody$>
                 $comment_prefix .= 'unset($BlogCommentBody);' . "\n" . '$BlogCommentBody .= Codes::beautify(trim($item[\'description\']));' . "\n" . "\n";
                 $from = '/<\\$BlogCommentBody\\$>/i';
                 $to = '\'.$BlogCommentBody.\'';
                 $area = preg_replace($from, $to, $area);
                 // make a skin
                 $skin .= "\n" . '	function layout_comment($item, $variant = \'compact\') {' . "\n" . '		global $context;' . "\n" . '		' . str_replace("\n", "\n\t\t", $comment_prefix) . "\n" . '		//	array($prefix, $title, $suffix, $type, $icon)' . "\n" . '		$prefix = \'' . trim($item_prefix) . '\';' . "\n" . '		$title = \'_\';' . "\n" . '		$suffix = \'' . trim($area) . '\';' . "\n" . '		return array($prefix, $title, $suffix, \'comment\', NULL);' . "\n" . "\t}\n";
                 break;
             case 2:
                 // suffix block
                 $template .= $area;
                 break;
         }
         $index++;
     }
     // the <Blogger>...</Blogger> block
     $areas = preg_split('/<Blogger>(.*?)<\\/Blogger>/is', trim($template), -1, PREG_SPLIT_DELIM_CAPTURE);
     $template = '';
     $index = 0;
     foreach ($areas as $area) {
         // blogging area
         if ($index == 1) {
             $template .= '<?php ' . "\n" . '// display the menu bar, if any' . "\n" . 'if(@count($context[\'page_menu\']) > 0)' . "\n" . '	echo Skin::build_list($context[\'page_menu\'], \'page_menu\');' . "\n" . "\n" . '// display the prefix, if any' . "\n" . 'if($context[\'prefix\'])' . "\n" . '	echo $context[\'prefix\'];' . "\n" . "\n" . '// display the error message, if any' . "\n" . 'if($context[\'error\'])' . "\n" . '	  echo Skin::build_block($context[\'error\'], \'error\');' . "\n" . "\n" . '// display the page image, if any' . "\n" . 'if($context[\'page_image\'])' . "\n" . '	  echo \'<img src="\'.$context[\'page_image\'].\'" class="icon" alt="" />\';' . "\n" . "\n" . '// the main part of the page' . "\n" . 'echo $context[\'text\'];' . "\n" . "\n" . '// display the suffix, if any' . "\n" . 'if($context[\'suffix\'])' . "\n" . '	echo \'<p>\'.$context[\'suffix\'].\'</p>\';' . "\n" . '?>';
             // make a skin for each item of the blogging area
             // break lines to not interfere with regular code
             $area = str_replace("\n", "'\n\t\t\t.'", addcslashes(trim($area), "'"));
             // <$BlogDateHeaderDate$>
             $from = '/<\\$BlogDateHeaderDate\\$>/i';
             $to = '\'.Skin::build_date($item[\'create_date\']).\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemArchiveFileName$>
             $from = '/<\\$BlogItemArchiveFileName\\$>/i';
             $to = '\'.$context[\'url_to_root\'].Articles::get_permalink($item).\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemAuthor$>
             $from = '/<\\$BlogItemAuthor\\$>/i';
             $to = '\'.$item[\'create_name\'].\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemAuthorNickname$>
             $from = '/<\\$BlogItemAuthorNickname\\$>/i';
             $to = '\'.$item[\'create_name\'].\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemBody$>
             $article_prefix .= 'unset($BlogItemBody);' . "\n" . '// the introduction' . "\n" . 'if($item[\'introduction\'])' . "\n" . '	$BlogItemBody .= Codes::beautify(trim($item[\'introduction\']));' . "\n" . 'elseif(!is_object($overlay)) {' . "\n" . '	// extract up to markup, if any' . "\n" . '	$raw = preg_split(\'/(\\[|<)/\', $item[\'description\']);' . "\n" . '	$BlogItemBody .= Skin::strip(trim($raw[0]), 30);' . "\n" . '}' . "\n" . 'if($suffix)' . "\n" . '	$BlogItemBody = \' -&nbsp;\'.$suffix;' . "\n" . "\n" . '// insert overlay data, if any' . "\n" . 'if(is_object($overlay))' . "\n" . '	$BlogItemBody .= $overlay->get_text(\'list\', $item);' . "\n" . "\n";
             $from = '/<\\$BlogItemBody\\$>/i';
             $to = '\'.$BlogItemBody.\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemCommentCount$>
             $article_prefix .= 'unset($BlogItemCommentCount);' . "\n" . '// info on related comments' . "\n" . 'include_once $context[\'path_to_root\'].\'comments/comments.php\';' . "\n" . '$BlogItemCommentCount = Comments::count_for_anchor(\'article:\'.$item[\'id\']);' . "\n" . "\n";
             $from = '/<\\$BlogItemCommentCount\\$>/i';
             $to = '\'.$BlogItemCommentCount.\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemControl$> -- the menu bar for associates and poster
             $article_prefix .= 'unset($BlogItemControl);' . "\n" . 'if(Surfer::is_associate() || Surfer::is($item[\'create_id\']) || Surfer::is($item[\'edit_id\'])) {' . "\n" . '	$menu = array( Articles::get_url($item[\'id\'], \'edit\') => i18n::s(\'edit\'),' . "\n" . '		Articles::get_url($item[\'id\'], \'delete\') => i18n::s(\'delete\') );' . "\n" . '	$BlogItemControl = \' \'.Skin::build_list($menu, \'menu\');' . "\n" . '}' . "\n" . "\n";
             $from = '/<\\$BlogItemControl\\$>/i';
             $to = '\'.$BlogItemControl.\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemDateTime$>
             $from = '/<\\$BlogItemDateTime\\$>/i';
             $to = '\'.Skin::build_date($item[\'create_date\']).\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemNumber$>
             $from = '/<\\$BlogItemNumber\\$>/i';
             $to = '\'.$item[\'id\'].\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemPermalinkURL$>
             $from = '/<\\$BlogItemPermalinkURL\\$>/i';
             $to = '\'.$context[\'url_to_root\'].Articles::get_permalink($item).\'';
             $area = preg_replace($from, $to, $area);
             // <$BlogItemTitle$> -- it has to be the last one for this item
             $from = '/<\\$BlogItemTitle\\$>/i';
             list($item_prefix, $item_suffix) = preg_split($from, $area);
             // make a skin
             $skin .= "\n" . '	function layout_article($item, $variant = \'compact\') {' . "\n" . '		global $context;' . "\n" . '		' . str_replace("\n", "\n\t\t", $article_prefix) . "\n" . '		//	array($prefix, $title, $suffix, $type, $icon)' . "\n" . '		$prefix = \'' . trim($item_prefix) . '\';' . "\n" . '		$title = trim($item[\'title\']);' . "\n" . '		$suffix = \'' . trim($item_suffix) . '\';' . "\n" . '		return array($prefix, $title, $suffix, \'article\', NULL);' . "\n" . "\t}\n";
         } else {
             // suffix block
             $template .= $area;
         }
         $index++;
     }
     // skin end
     $skin .= "}\n" . '?>' . "\n";
     // backup the old skin, if any
     Safe::unlink($context['path_to_root'] . 'skins/' . $directory . '/skin.php.bak');
     Safe::rename($context['path_to_root'] . 'skins/' . $directory . '/skin.php', $context['path_to_root'] . 'skins/' . $directory . '/skin.php.bak');
     // create a new skin file
     if (!$skin) {
         Logger::error(i18n::s('No blogging block has been found.'));
     } elseif (!Safe::make_path('skins/' . $directory)) {
         Logger::error(sprintf(i18n::s('Impossible to create path %s.'), 'skins/' . $directory));
     } elseif (!($handle = Safe::fopen($context['path_to_root'] . 'skins/' . $directory . '/skin.php', 'wb'))) {
         Logger::error(sprintf(i18n::s('Impossible to write to %s.'), $context['path_to_root'] . 'skins/' . $directory . '/skin.php'));
     } else {
         fwrite($handle, $skin);
         fclose($handle);
     }
     // backup the old template, if any
     Safe::unlink($context['path_to_root'] . 'skins/' . $directory . '/template.php.bak');
     if (!$template) {
         Logger::error(i18n::s('Empty template file'));
     } else {
         Safe::rename($context['path_to_root'] . 'skins/' . $directory . '/template.php', $context['path_to_root'] . 'skins/' . $directory . '/template.php.bak');
     }
     // create a new template file
     if (!Safe::make_path('skins/' . $directory)) {
         Logger::error(sprintf(i18n::s('Impossible to create path %s.'), 'skins/' . $directory));
     } elseif (!($handle = Safe::fopen($context['path_to_root'] . 'skins/' . $directory . '/template.php', 'wb'))) {
         Logger::error(sprintf(i18n::s('Impossible to write to %s.'), $context['path_to_root'] . 'skins/' . $directory . '/template.php'));
     } else {
         fwrite($handle, $template);
         fclose($handle);
         $context['text'] .= '<p>' . sprintf(i18n::s('Template has been imported. Check skin %s'), Skin::build_link('skins/test.php?skin=' . $directory, $directory, 'shortcut')) . "</p>\n";
     }
     return NULL;
 }
Example #6
0
File: derive.php Project: rair/yacs
 // copy files
 $context['text'] .= '<p>' . i18n::s('Copying files...') . BR . "\n";
 // analyse each script
 foreach ($files as $file) {
     // ensure we have enough time to process this script
     Safe::set_time_limit(30);
     // the origin file
     $origin = 'skins/' . $skin . $file;
     // the target file
     if ($file == '/' . $skin . '.css') {
         $target = 'skins/' . $directory . '/' . $directory . '.css';
     } else {
         $target = 'skins/' . $directory . $file;
     }
     // ensure the path has been created
     Safe::make_path(dirname($target));
     // unlink previous files, if any
     Safe::unlink($context['path_to_root'] . $target);
     // transcode php files
     if (preg_match('/(\\.php|\\.css)$/i', $target) && ($content = Safe::file_get_contents($context['path_to_root'] . $origin))) {
         // change internal reference
         $content = preg_replace('/skins\\/' . preg_quote($skin, '/') . '/i', 'skins/' . $directory, $content);
         $content = preg_replace('/\'' . preg_quote($skin, '/') . '\'/i', "'" . $directory . "'", $content);
         $content = preg_replace('/' . preg_quote($skin, '/') . '\\.css/i', $directory . ".css", $content);
         // not part of the reference set anymore
         $content = preg_replace('/\\s*\\*\\s+@reference\\s*\\n/i', "\n", $content);
         // save it as the new cache file
         if (Safe::file_put_contents($target, $content)) {
             $context['text'] .= sprintf(i18n::s('%s has been transcoded'), $target) . BR . "\n";
         } else {
             $context['text'] .= sprintf(i18n::s('Impossible to write to %s.'), $target) . BR . "\n";
Example #7
0
File: files.php Project: rair/yacs
 /**
  * process uploaded file
  *
  * This function processes files from the temporary directory, and put them at their definitive
  * place.
  *
  * It returns FALSE if there is a disk error, or if some virus has been detected, or if
  * the operation fails for some other reason (e.g., file size).
  *
  * @param array usually, $_FILES['upload']
  * @param string target location for the file
  * @param mixed reference to the target anchor, of a function to parse every file individually
  * @return mixed file name or array of file names or FALSE if an error has occured
  */
 public static function upload($input, $file_path, $target = NULL, $overlay = NULL)
 {
     global $context, $_REQUEST;
     // size exceeds php.ini settings -- UPLOAD_ERR_INI_SIZE
     if (isset($input['error']) && $input['error'] == 1) {
         Logger::error(i18n::s('The size of this file is over limit.'));
     } elseif (isset($input['error']) && $input['error'] == 2) {
         Logger::error(i18n::s('The size of this file is over limit.'));
     } elseif (isset($input['error']) && $input['error'] == 3) {
         Logger::error(i18n::s('No file has been transmitted.'));
     } elseif (isset($input['error']) && $input['error'] == 4) {
         Logger::error(i18n::s('No file has been transmitted.'));
     } elseif (!$input['size']) {
         Logger::error(i18n::s('No file has been transmitted.'));
     }
     // do we have a file?
     if (!isset($input['name']) || !$input['name'] || $input['name'] == 'none') {
         return FALSE;
     }
     // access the temporary uploaded file
     $file_upload = $input['tmp_name'];
     // $_FILES transcoding to utf8 is not automatic
     $input['name'] = utf8::encode($input['name']);
     // enhance file name
     $file_name = $input['name'];
     $file_extension = '';
     $position = strrpos($input['name'], '.');
     if ($position !== FALSE) {
         $file_name = substr($input['name'], 0, $position);
         $file_extension = strtolower(substr($input['name'], $position + 1));
     }
     $input['name'] = $file_name;
     if ($file_extension) {
         $input['name'] .= '.' . $file_extension;
     }
     // ensure we have a file name
     $file_name = utf8::to_ascii($input['name']);
     // uploads are not allowed
     if (!Surfer::may_upload()) {
         Logger::error(i18n::s('You are not allowed to perform this operation.'));
     } elseif (!Files::is_authorized($input['name'])) {
         Logger::error(i18n::s('This type of file is not allowed.'));
     } elseif ($file_path && !Safe::is_uploaded_file($file_upload)) {
         Logger::error(i18n::s('Possible file attack.'));
     } else {
         // create folders
         if ($file_path) {
             Safe::make_path($file_path);
         }
         // sanity check
         if ($file_path && $file_path[strlen($file_path) - 1] != '/') {
             $file_path .= '/';
         }
         // move the uploaded file
         if ($file_path && !Safe::move_uploaded_file($file_upload, $context['path_to_root'] . $file_path . $file_name)) {
             Logger::error(sprintf(i18n::s('Impossible to move the upload file to %s.'), $file_path . $file_name));
         } else {
             // process the file where it is
             if (!$file_path) {
                 $file_path = str_replace($context['path_to_root'], '', dirname($file_upload));
                 $file_name = basename($file_upload);
             }
             // check against viruses
             $result = Files::has_virus($context['path_to_root'] . $file_path . '/' . $file_name);
             // no virus has been found in this file
             if ($result == 'N') {
                 $context['text'] .= Skin::build_block(i18n::s('No virus has been found.'), 'note');
             }
             // this file has been infected!
             if ($result == 'Y') {
                 // delete this file immediately
                 Safe::unlink($file_path . '/' . $file_name);
                 Logger::error(i18n::s('This file has been infected by a virus and has been rejected!'));
                 return FALSE;
             }
             // explode a .zip file
             include_once $context['path_to_root'] . 'shared/zipfile.php';
             if (preg_match('/\\.zip$/i', $file_name) && isset($_REQUEST['explode_files'])) {
                 $zipfile = new zipfile();
                 // check files extracted from the archive file
                 function explode_callback($name)
                 {
                     global $context;
                     // reject all files put in sub-folders
                     if (($path = substr($name, strlen($context['uploaded_path'] . '/'))) && strpos($path, '/') !== FALSE) {
                         Safe::unlink($name);
                     } elseif (!Files::is_authorized($name)) {
                         Safe::unlink($name);
                     } else {
                         // make it easy to download
                         $ascii = utf8::to_ascii(basename($name));
                         Safe::rename($name, $context['uploaded_path'] . '/' . $ascii);
                         // remember this name
                         $context['uploaded_files'][] = $ascii;
                     }
                 }
                 // extract archive components and save them in mentioned directory
                 $context['uploaded_files'] = array();
                 $context['uploaded_path'] = $file_path;
                 if (!($count = $zipfile->explode($context['path_to_root'] . $file_path . '/' . $file_name, $file_path, '', 'explode_callback'))) {
                     Logger::error(sprintf('Nothing has been extracted from %s.', $file_name));
                     return FALSE;
                 }
                 // one single file has been uploaded
             } else {
                 $context['uploaded_files'] = array($file_name);
             }
             // ensure we know the surfer
             Surfer::check_default_editor($_REQUEST);
             // post-process all uploaded files
             foreach ($context['uploaded_files'] as $file_name) {
                 // this will be filtered by umask anyway
                 Safe::chmod($context['path_to_root'] . $file_path . $file_name, $context['file_mask']);
                 // invoke post-processing function
                 if ($target && is_callable($target)) {
                     call_user_func($target, $file_name, $context['path_to_root'] . $file_path);
                     // we have to update an anchor page
                 } elseif ($target && is_string($target)) {
                     $fields = array();
                     // update a file with the same name for this anchor
                     if ($matching =& Files::get_by_anchor_and_name($target, $file_name)) {
                         $fields['id'] = $matching['id'];
                     } elseif (isset($input['id']) && ($matching = Files::get($input['id']))) {
                         $fields['id'] = $matching['id'];
                         // silently delete the previous version of the file
                         if (isset($matching['file_name'])) {
                             Safe::unlink($file_path . '/' . $matching['file_name']);
                         }
                     }
                     // prepare file record
                     $fields['file_name'] = $file_name;
                     $fields['file_size'] = filesize($context['path_to_root'] . $file_path . $file_name);
                     $fields['file_href'] = '';
                     $fields['anchor'] = $target;
                     // change title
                     if (isset($_REQUEST['title'])) {
                         $fields['title'] = $_REQUEST['title'];
                     }
                     // change has been documented
                     if (!isset($_REQUEST['version']) || !$_REQUEST['version']) {
                         $_REQUEST['version'] = '';
                     } else {
                         $_REQUEST['version'] = ' - ' . $_REQUEST['version'];
                     }
                     // always remember file uploads, for traceability
                     $_REQUEST['version'] = $fields['file_name'] . ' (' . Skin::build_number($fields['file_size'], i18n::s('bytes')) . ')' . $_REQUEST['version'];
                     // add to file history
                     $fields['description'] = Files::add_to_history($matching, $_REQUEST['version']);
                     // if this is an image, maybe we can derive a thumbnail for it?
                     if (Files::is_image($file_name)) {
                         include_once $context['path_to_root'] . 'images/image.php';
                         Image::shrink($context['path_to_root'] . $file_path . $file_name, $context['path_to_root'] . $file_path . 'thumbs/' . $file_name);
                         if (file_exists($context['path_to_root'] . $file_path . 'thumbs/' . $file_name)) {
                             $fields['thumbnail_url'] = $context['url_to_home'] . $context['url_to_root'] . $file_path . 'thumbs/' . rawurlencode($file_name);
                         }
                     }
                     // change active_set
                     if (isset($_REQUEST['active_set'])) {
                         $fields['active_set'] = $_REQUEST['active_set'];
                     }
                     // change source
                     if (isset($_REQUEST['source'])) {
                         $fields['source'] = $_REQUEST['source'];
                     }
                     // change keywords
                     if (isset($_REQUEST['keywords'])) {
                         $fields['keywords'] = $_REQUEST['keywords'];
                     }
                     // change alternate_href
                     if (isset($_REQUEST['alternate_href'])) {
                         $fields['alternate_href'] = $_REQUEST['alternate_href'];
                     }
                     // overlay, if any
                     if (is_object($overlay)) {
                         // allow for change detection
                         $overlay->snapshot();
                         // update the overlay from form content
                         $overlay->parse_fields($_REQUEST);
                         // save content of the overlay in this item
                         $fields['overlay'] = $overlay->save();
                         $fields['overlay_id'] = $overlay->get_id();
                     }
                     // create the record in the database
                     if (!($fields['id'] = Files::post($fields))) {
                         return FALSE;
                     }
                     // record surfer activity
                     Activities::post('file:' . $fields['id'], 'upload');
                 }
             }
             // so far so good
             if (count($context['uploaded_files']) == 1) {
                 return $context['uploaded_files'][0];
             } else {
                 return $context['uploaded_files'];
             }
         }
     }
     // some error has occured
     return FALSE;
 }
     $remote_reference = 'http://' . $context['reference_server'] . '/scripts/fetch.php?script=' . urlencode($file);
 } else {
     $remote_reference = 'http://' . $context['reference_server'] . '/scripts/reference/' . $file;
 }
 // get the file locally
 if (file_exists($local_reference)) {
     $content = Safe::file_get_contents($local_reference);
 } elseif (($content = http::proceed($remote_reference)) === FALSE) {
     $local['error_en'] = 'Unable to get ' . $file;
     $local['error_fr'] = 'Impossible d\'obtenir ' . $file;
     echo i18n::user('error') . "<br />\n";
 }
 // we have something in hand
 if ($content) {
     // create missing directories where applicable
     Safe::make_path(dirname($file));
     // create backups, if possible
     if (file_exists($context['path_to_root'] . $file)) {
         Safe::unlink($context['path_to_root'] . $file . '.bak');
         Safe::rename($context['path_to_root'] . $file, $context['path_to_root'] . $file . '.bak');
     }
     // update the target file
     if (!Safe::file_put_contents($file, $content)) {
         $local['label_en'] = 'Impossible to write to the file ' . $file . '.';
         $local['label_fr'] = 'Impossible d\'&eacute;crire le fichier ' . $file . '.';
         echo i18n::user('label') . "<br />\n";
     } else {
         $local['label_en'] = 'has been updated';
         $local['label_fr'] = 'a &eacute;t&eacute; mis &agrave; jour';
         echo $file . ' ' . i18n::user('label') . "<br />\n";
     }
Example #9
0
File: images.php Project: rair/yacs
 /**
  * duplicate all images for a given anchor
  *
  * This function duplicates records in the database, and changes anchors
  * to attach new records as per second parameter.
  *
  * @param string the source anchor
  * @param string the target anchor
  * @return int the number of duplicated records
  *
  * @see shared/anchors.php
  */
 public static function duplicate_for_anchor($anchor_from, $anchor_to)
 {
     global $context;
     // look for records attached to this anchor
     $count = 0;
     $query = "SELECT * FROM " . SQL::table_name('images') . " WHERE anchor LIKE '" . SQL::escape($anchor_from) . "'";
     if (($result = SQL::query($query)) && SQL::count($result)) {
         // create target folders
         $file_to = $context['path_to_root'] . Files::get_path($item['anchor'], 'images');
         if (!Safe::make_path($file_to . '/thumbs')) {
             Logger::error(sprintf(i18n::s('Impossible to create path %s.'), $file_to . '/thumbs'));
         }
         $file_to = $context['path_to_root'] . $file_to . '/';
         // the list of transcoded strings
         $transcoded = array();
         // process all matching records one at a time
         $file_from = $context['path_to_root'] . Files::get_path($anchor_from, 'images');
         while ($item = SQL::fetch($result)) {
             // sanity check
             if (!file_exists($context['path_to_root'] . $file_from . '/' . $item['image_name'])) {
                 continue;
             }
             // duplicate image file
             if (!copy($context['path_to_root'] . $file_from . '/' . $item['image_name'], $file_to . $item['image_name'])) {
                 Logger::error(sprintf(i18n::s('Impossible to copy file %s.'), $item['image_name']));
                 continue;
             }
             // this will be filtered by umask anyway
             Safe::chmod($file_to . $item['image_name'], $context['file_mask']);
             // copy the thumbnail as well
             Safe::copy($context['path_to_root'] . $file_from . '/' . $item['thumbnail_name'], $file_to . $item['thumbnail_name']);
             // this will be filtered by umask anyway
             Safe::chmod($file_to . $item['thumbnail_name'], $context['file_mask']);
             // a new id will be allocated
             $old_id = $item['id'];
             unset($item['id']);
             // target anchor
             $item['anchor'] = $anchor_to;
             // actual duplication
             if ($new_id = Images::post($item)) {
                 // more pairs of strings to transcode --no automatic solution for [images=...]
                 $transcoded[] = array('/\\[image=' . preg_quote($old_id, '/') . '/i', '[image=' . $new_id);
                 // duplicate elements related to this item
                 Anchors::duplicate_related_to('image:' . $old_id, 'image:' . $new_id);
                 // stats
                 $count++;
             }
         }
         // transcode in anchor
         if ($anchor = Anchors::get($anchor_to)) {
             $anchor->transcode($transcoded);
         }
     }
     // number of duplicated records
     return $count;
 }
Example #10
0
File: build.php Project: rair/yacs
 foreach ($scripts as $file) {
     // silently skip configuration files
     if (strpos($file, '.include.php')) {
         continue;
     }
     // process only reference scripts
     if (!($footprint = Scripts::hash($file))) {
         $context['text'] .= sprintf(i18n::s('%s is not a reference script'), $file) . BR . "\n";
         continue;
     }
     // store the footprint for later use --number of lines, content hash
     $footprints[$file] = array($footprint[0], $footprint[1]);
     // ensure a clean reference store
     Safe::unlink($context['path_to_reference'] . $file);
     // create adequate path
     if (!Safe::make_path($context['path_to_reference'] . dirname($file))) {
         $context['text'] .= sprintf(i18n::s('Impossible to create path %s.'), $context['path_to_reference'] . dirname($file)) . BR . "\n";
     } elseif (!Safe::copy($context['path_to_root'] . $file, $context['path_to_reference'] . $file)) {
         $context['text'] .= sprintf(i18n::s('Impossible to copy file %s.'), $file) . BR . "\n";
     } else {
         // try to preserve the modification date
         Safe::touch($context['path_to_reference'] . $file, Safe::filemtime($context['path_to_root'] . $file));
         // this will be filtered by umask anyway
         Safe::chmod($context['path_to_reference'] . $file, $context['file_mask']);
     }
     // avoid timeouts
     if (!(count($footprints) % 50)) {
         Safe::set_time_limit(30);
         SQL::ping();
     }
 }